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 2501 : void grpc_resolver_registry_init(const char *default_resolver_prefix) {
50 2501 : g_number_of_resolvers = 0;
51 2501 : g_default_resolver_prefix = gpr_strdup(default_resolver_prefix);
52 2501 : }
53 :
54 2501 : void grpc_resolver_registry_shutdown(void) {
55 : int i;
56 12505 : for (i = 0; i < g_number_of_resolvers; i++) {
57 10004 : grpc_resolver_factory_unref(g_all_of_the_resolvers[i]);
58 : }
59 2501 : gpr_free(g_default_resolver_prefix);
60 2501 : }
61 :
62 10004 : void grpc_register_resolver_type(grpc_resolver_factory *factory) {
63 : int i;
64 25010 : for (i = 0; i < g_number_of_resolvers; i++) {
65 15006 : GPR_ASSERT(0 != strcmp(factory->vtable->scheme,
66 : g_all_of_the_resolvers[i]->vtable->scheme));
67 : }
68 10004 : GPR_ASSERT(g_number_of_resolvers != MAX_RESOLVERS);
69 10004 : grpc_resolver_factory_ref(factory);
70 10004 : g_all_of_the_resolvers[g_number_of_resolvers++] = factory;
71 10004 : }
72 :
73 6378 : static grpc_resolver_factory *lookup_factory(grpc_uri *uri) {
74 : int i;
75 :
76 : /* handling NULL uri's here simplifies grpc_resolver_create */
77 6378 : if (!uri) return NULL;
78 :
79 16662 : for (i = 0; i < g_number_of_resolvers; i++) {
80 14431 : if (0 == strcmp(uri->scheme, g_all_of_the_resolvers[i]->vtable->scheme)) {
81 3533 : return g_all_of_the_resolvers[i];
82 : }
83 : }
84 :
85 2231 : return NULL;
86 : }
87 :
88 3533 : static grpc_resolver_factory *resolve_factory(const char *target,
89 : grpc_uri **uri) {
90 : char *tmp;
91 3533 : grpc_resolver_factory *factory = NULL;
92 :
93 3533 : GPR_ASSERT(uri != NULL);
94 3533 : *uri = grpc_uri_parse(target, 1);
95 3533 : factory = lookup_factory(*uri);
96 3533 : if (factory == NULL) {
97 2845 : if (g_default_resolver_prefix != NULL) {
98 2845 : grpc_uri_destroy(*uri);
99 2845 : gpr_asprintf(&tmp, "%s%s", g_default_resolver_prefix, target);
100 2845 : *uri = grpc_uri_parse(tmp, 1);
101 2845 : factory = lookup_factory(*uri);
102 2845 : if (factory == NULL) {
103 0 : grpc_uri_destroy(grpc_uri_parse(target, 0));
104 0 : grpc_uri_destroy(grpc_uri_parse(tmp, 0));
105 0 : gpr_log(GPR_ERROR, "don't know how to resolve '%s' or '%s'", target,
106 : tmp);
107 : }
108 2845 : 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 3533 : return factory;
115 : }
116 :
117 1761 : grpc_resolver *grpc_resolver_create(
118 : const char *target, grpc_subchannel_factory *subchannel_factory) {
119 1761 : grpc_uri *uri = NULL;
120 1761 : grpc_resolver_factory *factory = resolve_factory(target, &uri);
121 : grpc_resolver *resolver;
122 : grpc_resolver_args args;
123 1761 : memset(&args, 0, sizeof(args));
124 1761 : args.uri = uri;
125 1761 : args.subchannel_factory = subchannel_factory;
126 1761 : resolver = grpc_resolver_factory_create_resolver(factory, &args);
127 1761 : grpc_uri_destroy(uri);
128 1761 : return resolver;
129 : }
130 :
131 1772 : char *grpc_get_default_authority(const char *target) {
132 1772 : grpc_uri *uri = NULL;
133 1772 : grpc_resolver_factory *factory = resolve_factory(target, &uri);
134 1772 : char *authority = grpc_resolver_factory_get_default_authority(factory, uri);
135 1772 : grpc_uri_destroy(uri);
136 1772 : return authority;
137 : }
|