blob: 5da6114a3d04ccf968fe3ddc6272c3a50d9bc9c4 [file] [log] [blame]
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070016 *
17 */
18
Craig Tiller9eb0fde2017-03-31 16:59:30 -070019#include "src/core/ext/filters/client_channel/resolver_registry.h"
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070020
21#include <string.h>
22
23#include <grpc/support/alloc.h>
24#include <grpc/support/log.h>
25#include <grpc/support/string_util.h>
26
27#define MAX_RESOLVERS 10
David Garcia Quintasfa0896b2016-09-23 16:10:19 -070028#define DEFAULT_RESOLVER_PREFIX_MAX_LENGTH 32
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070029
Craig Tillerbaa14a92017-11-03 09:09:36 -070030static grpc_resolver_factory* g_all_of_the_resolvers[MAX_RESOLVERS];
Craig Tillereb3b12e2015-06-26 14:42:49 -070031static int g_number_of_resolvers = 0;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070032
David Garcia Quintasfa0896b2016-09-23 16:10:19 -070033static char g_default_resolver_prefix[DEFAULT_RESOLVER_PREFIX_MAX_LENGTH] =
34 "dns:///";
Craig Tillereb3b12e2015-06-26 14:42:49 -070035
David Garcia Quintasfa0896b2016-09-23 16:10:19 -070036void grpc_resolver_registry_init() {}
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070037
Craig Tillera82950e2015-09-22 12:33:20 -070038void grpc_resolver_registry_shutdown(void) {
David Garcia Quintasfa0896b2016-09-23 16:10:19 -070039 for (int i = 0; i < g_number_of_resolvers; i++) {
Craig Tillera82950e2015-09-22 12:33:20 -070040 grpc_resolver_factory_unref(g_all_of_the_resolvers[i]);
41 }
Craig Tiller5deda3d2016-05-05 12:34:19 -070042 // FIXME(ctiller): this should live in grpc_resolver_registry_init,
Mark D. Roth2137cd82016-09-14 09:04:00 -070043 // however that would have the client_channel plugin call this AFTER we start
Craig Tiller5deda3d2016-05-05 12:34:19 -070044 // registering resolvers from third party plugins, and so they'd never show
45 // up.
46 // We likely need some kind of dependency system for plugins.... what form
47 // that takes is TBD.
48 g_number_of_resolvers = 0;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070049}
50
David Garcia Quintasfa0896b2016-09-23 16:10:19 -070051void grpc_resolver_registry_set_default_prefix(
Craig Tillerbaa14a92017-11-03 09:09:36 -070052 const char* default_resolver_prefix) {
David Garcia Quintasa60dcca2016-09-26 11:09:29 -070053 const size_t len = strlen(default_resolver_prefix);
54 GPR_ASSERT(len < DEFAULT_RESOLVER_PREFIX_MAX_LENGTH &&
55 "default resolver prefix too long");
56 GPR_ASSERT(len > 0 && "default resolver prefix can't be empty");
57 // By the previous assert, default_resolver_prefix is safe to be copied with a
58 // plain strcpy.
59 strcpy(g_default_resolver_prefix, default_resolver_prefix);
David Garcia Quintasfa0896b2016-09-23 16:10:19 -070060}
61
Craig Tillerbaa14a92017-11-03 09:09:36 -070062void grpc_register_resolver_type(grpc_resolver_factory* factory) {
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070063 int i;
Craig Tillera82950e2015-09-22 12:33:20 -070064 for (i = 0; i < g_number_of_resolvers; i++) {
Yuchen Zeng3d803232016-11-30 15:17:31 -080065 GPR_ASSERT(0 != strcmp(factory->vtable->scheme,
66 g_all_of_the_resolvers[i]->vtable->scheme));
Craig Tillera82950e2015-09-22 12:33:20 -070067 }
68 GPR_ASSERT(g_number_of_resolvers != MAX_RESOLVERS);
69 grpc_resolver_factory_ref(factory);
Craig Tillerbc85be12015-08-24 10:36:39 -070070 g_all_of_the_resolvers[g_number_of_resolvers++] = factory;
Craig Tillereb3b12e2015-06-26 14:42:49 -070071}
72
Craig Tillerbaa14a92017-11-03 09:09:36 -070073static grpc_resolver_factory* lookup_factory(const char* name) {
Craig Tillereb3b12e2015-06-26 14:42:49 -070074 int i;
75
Craig Tillera82950e2015-09-22 12:33:20 -070076 for (i = 0; i < g_number_of_resolvers; i++) {
Craig Tiller65938df2016-03-31 13:08:49 -070077 if (0 == strcmp(name, g_all_of_the_resolvers[i]->vtable->scheme)) {
Craig Tillera82950e2015-09-22 12:33:20 -070078 return g_all_of_the_resolvers[i];
Craig Tillereb3b12e2015-06-26 14:42:49 -070079 }
Craig Tillera82950e2015-09-22 12:33:20 -070080 }
Craig Tiller4782d922017-11-10 09:53:21 -080081 return nullptr;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070082}
83
Craig Tillerbaa14a92017-11-03 09:09:36 -070084grpc_resolver_factory* grpc_resolver_factory_lookup(const char* name) {
85 grpc_resolver_factory* f = lookup_factory(name);
Craig Tiller65938df2016-03-31 13:08:49 -070086 if (f) grpc_resolver_factory_ref(f);
87 return f;
88}
89
Craig Tillerbaa14a92017-11-03 09:09:36 -070090static grpc_resolver_factory* lookup_factory_by_uri(grpc_uri* uri) {
Craig Tiller4782d922017-11-10 09:53:21 -080091 if (!uri) return nullptr;
Craig Tiller65938df2016-03-31 13:08:49 -070092 return lookup_factory(uri->scheme);
93}
94
Craig Tillerbaa14a92017-11-03 09:09:36 -070095static grpc_resolver_factory* resolve_factory(grpc_exec_ctx* exec_ctx,
96 const char* target,
97 grpc_uri** uri,
98 char** canonical_target) {
Craig Tiller4782d922017-11-10 09:53:21 -080099 grpc_resolver_factory* factory = nullptr;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700100
Craig Tiller4782d922017-11-10 09:53:21 -0800101 GPR_ASSERT(uri != nullptr);
Yuchen Zengc40d1d82017-02-15 20:42:06 -0800102 *uri = grpc_uri_parse(exec_ctx, target, 1);
Craig Tiller65938df2016-03-31 13:08:49 -0700103 factory = lookup_factory_by_uri(*uri);
Craig Tiller4782d922017-11-10 09:53:21 -0800104 if (factory == nullptr) {
David Garcia Quintasa60dcca2016-09-26 11:09:29 -0700105 grpc_uri_destroy(*uri);
Mark D. Roth201db7d2016-12-12 09:36:02 -0800106 gpr_asprintf(canonical_target, "%s%s", g_default_resolver_prefix, target);
Yuchen Zengc40d1d82017-02-15 20:42:06 -0800107 *uri = grpc_uri_parse(exec_ctx, *canonical_target, 1);
David Garcia Quintasa60dcca2016-09-26 11:09:29 -0700108 factory = lookup_factory_by_uri(*uri);
Craig Tiller4782d922017-11-10 09:53:21 -0800109 if (factory == nullptr) {
Yuchen Zengc40d1d82017-02-15 20:42:06 -0800110 grpc_uri_destroy(grpc_uri_parse(exec_ctx, target, 0));
111 grpc_uri_destroy(grpc_uri_parse(exec_ctx, *canonical_target, 0));
Mark D. Roth201db7d2016-12-12 09:36:02 -0800112 gpr_log(GPR_ERROR, "don't know how to resolve '%s' or '%s'", target,
113 *canonical_target);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700114 }
Craig Tillera82950e2015-09-22 12:33:20 -0700115 }
Craig Tillerbc85be12015-08-24 10:36:39 -0700116 return factory;
117}
118
Craig Tillerbaa14a92017-11-03 09:09:36 -0700119grpc_resolver* grpc_resolver_create(grpc_exec_ctx* exec_ctx, const char* target,
120 const grpc_channel_args* args,
121 grpc_pollset_set* pollset_set,
122 grpc_combiner* combiner) {
Craig Tiller4782d922017-11-10 09:53:21 -0800123 grpc_uri* uri = nullptr;
124 char* canonical_target = nullptr;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700125 grpc_resolver_factory* factory =
Yuchen Zengc40d1d82017-02-15 20:42:06 -0800126 resolve_factory(exec_ctx, target, &uri, &canonical_target);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700127 grpc_resolver* resolver;
Mark D. Roth98abfd32016-10-21 08:10:51 -0700128 grpc_resolver_args resolver_args;
129 memset(&resolver_args, 0, sizeof(resolver_args));
130 resolver_args.uri = uri;
131 resolver_args.args = args;
Yuchen Zeng63e3e3b2016-12-15 12:06:33 -0800132 resolver_args.pollset_set = pollset_set;
Craig Tiller972470b2017-02-09 15:05:36 -0800133 resolver_args.combiner = combiner;
Yuchen Zeng63e3e3b2016-12-15 12:06:33 -0800134 resolver =
135 grpc_resolver_factory_create_resolver(exec_ctx, factory, &resolver_args);
Craig Tillera82950e2015-09-22 12:33:20 -0700136 grpc_uri_destroy(uri);
Mark D. Roth201db7d2016-12-12 09:36:02 -0800137 gpr_free(canonical_target);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700138 return resolver;
139}
Craig Tillerbc85be12015-08-24 10:36:39 -0700140
Craig Tillerbaa14a92017-11-03 09:09:36 -0700141char* grpc_get_default_authority(grpc_exec_ctx* exec_ctx, const char* target) {
Craig Tiller4782d922017-11-10 09:53:21 -0800142 grpc_uri* uri = nullptr;
143 char* canonical_target = nullptr;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700144 grpc_resolver_factory* factory =
Yuchen Zengc40d1d82017-02-15 20:42:06 -0800145 resolve_factory(exec_ctx, target, &uri, &canonical_target);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700146 char* authority = grpc_resolver_factory_get_default_authority(factory, uri);
Craig Tillera82950e2015-09-22 12:33:20 -0700147 grpc_uri_destroy(uri);
Mark D. Roth201db7d2016-12-12 09:36:02 -0800148 gpr_free(canonical_target);
Craig Tillerbc85be12015-08-24 10:36:39 -0700149 return authority;
150}
Mark D. Roth201db7d2016-12-12 09:36:02 -0800151
Craig Tillerbaa14a92017-11-03 09:09:36 -0700152char* grpc_resolver_factory_add_default_prefix_if_needed(
153 grpc_exec_ctx* exec_ctx, const char* target) {
Craig Tiller4782d922017-11-10 09:53:21 -0800154 grpc_uri* uri = nullptr;
155 char* canonical_target = nullptr;
Yuchen Zengc40d1d82017-02-15 20:42:06 -0800156 resolve_factory(exec_ctx, target, &uri, &canonical_target);
Mark D. Roth201db7d2016-12-12 09:36:02 -0800157 grpc_uri_destroy(uri);
Craig Tiller4782d922017-11-10 09:53:21 -0800158 return canonical_target == nullptr ? gpr_strdup(target) : canonical_target;
Mark D. Roth201db7d2016-12-12 09:36:02 -0800159}