Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "src/core/client_config/resolver_registry.h"
35 :
36 : #include <string.h>
37 :
38 : #include <grpc/support/alloc.h>
39 : #include <grpc/support/log.h>
40 : #include <grpc/support/string_util.h>
41 :
42 : #define MAX_RESOLVERS 10
43 :
44 : static grpc_resolver_factory *g_all_of_the_resolvers[MAX_RESOLVERS];
45 : static int g_number_of_resolvers = 0;
46 :
47 : static char *g_default_resolver_prefix;
48 :
49 3453 : void grpc_resolver_registry_init(const char *default_resolver_prefix) {
50 3453 : g_number_of_resolvers = 0;
51 3453 : g_default_resolver_prefix = gpr_strdup(default_resolver_prefix);
52 3453 : }
53 :
54 3451 : void grpc_resolver_registry_shutdown(void) {
55 : int i;
56 17251 : for (i = 0; i < g_number_of_resolvers; i++) {
57 13800 : grpc_resolver_factory_unref(g_all_of_the_resolvers[i]);
58 : }
59 3451 : gpr_free(g_default_resolver_prefix);
60 3451 : }
61 :
62 13808 : void grpc_register_resolver_type(grpc_resolver_factory *factory) {
63 : int i;
64 34520 : for (i = 0; i < g_number_of_resolvers; i++) {
65 20712 : GPR_ASSERT(0 != strcmp(factory->vtable->scheme,
66 : g_all_of_the_resolvers[i]->vtable->scheme));
67 : }
68 13808 : GPR_ASSERT(g_number_of_resolvers != MAX_RESOLVERS);
69 13808 : grpc_resolver_factory_ref(factory);
70 13808 : g_all_of_the_resolvers[g_number_of_resolvers++] = factory;
71 13808 : }
72 :
73 9978 : static grpc_resolver_factory *lookup_factory(grpc_uri *uri) {
74 : int i;
75 :
76 : /* handling NULL uri's here simplifies grpc_resolver_create */
77 9978 : if (!uri) return NULL;
78 :
79 25472 : for (i = 0; i < g_number_of_resolvers; i++) {
80 22436 : if (0 == strcmp(uri->scheme, g_all_of_the_resolvers[i]->vtable->scheme)) {
81 5876 : return g_all_of_the_resolvers[i];
82 : }
83 : }
84 :
85 3134 : return NULL;
86 : }
87 :
88 5878 : static grpc_resolver_factory *resolve_factory(const char *target,
89 : grpc_uri **uri) {
90 : char *tmp;
91 5780 : grpc_resolver_factory *factory = NULL;
92 :
93 5878 : GPR_ASSERT(uri != NULL);
94 5878 : *uri = grpc_uri_parse(target, 1);
95 5878 : factory = lookup_factory(*uri);
96 5878 : if (factory == NULL) {
97 4100 : if (g_default_resolver_prefix != NULL) {
98 4100 : grpc_uri_destroy(*uri);
99 4100 : gpr_asprintf(&tmp, "%s%s", g_default_resolver_prefix, target);
100 4100 : *uri = grpc_uri_parse(tmp, 1);
101 4100 : factory = lookup_factory(*uri);
102 4100 : if (factory == NULL) {
103 2 : grpc_uri_destroy(grpc_uri_parse(target, 0));
104 2 : grpc_uri_destroy(grpc_uri_parse(tmp, 0));
105 2 : gpr_log(GPR_ERROR, "don't know how to resolve '%s' or '%s'", target,
106 : tmp);
107 : }
108 4100 : gpr_free(tmp);
109 : } else {
110 0 : grpc_uri_destroy(grpc_uri_parse(target, 0));
111 0 : gpr_log(GPR_ERROR, "don't know how to resolve '%s'", target);
112 : }
113 : }
114 5878 : return factory;
115 : }
116 :
117 2792 : grpc_resolver *grpc_resolver_create(
118 : const char *target, grpc_subchannel_factory *subchannel_factory) {
119 2792 : grpc_uri *uri = NULL;
120 2792 : grpc_resolver_factory *factory = resolve_factory(target, &uri);
121 : grpc_resolver *resolver;
122 : grpc_resolver_args args;
123 2792 : memset(&args, 0, sizeof(args));
124 2792 : args.uri = uri;
125 2792 : args.subchannel_factory = subchannel_factory;
126 2792 : resolver = grpc_resolver_factory_create_resolver(factory, &args);
127 2792 : grpc_uri_destroy(uri);
128 2792 : return resolver;
129 : }
130 :
131 3086 : char *grpc_get_default_authority(const char *target) {
132 3086 : grpc_uri *uri = NULL;
133 3086 : grpc_resolver_factory *factory = resolve_factory(target, &uri);
134 3086 : char *authority = grpc_resolver_factory_get_default_authority(factory, uri);
135 3086 : grpc_uri_destroy(uri);
136 3086 : return authority;
137 : }
|