blob: 07f29bcb27a9b1de6fdbd76243fa7446889c31f0 [file] [log] [blame]
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07004 * 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
Craig Tillerd4c98332016-03-31 13:45:47 -070034#include "src/core/ext/client_config/resolver_registry.h"
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070035
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
Craig Tillerbc85be12015-08-24 10:36:39 -070044static grpc_resolver_factory *g_all_of_the_resolvers[MAX_RESOLVERS];
Craig Tillereb3b12e2015-06-26 14:42:49 -070045static int g_number_of_resolvers = 0;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070046
Craig Tillerbc85be12015-08-24 10:36:39 -070047static char *g_default_resolver_prefix;
Craig Tillereb3b12e2015-06-26 14:42:49 -070048
Craig Tillera82950e2015-09-22 12:33:20 -070049void grpc_resolver_registry_init(const char *default_resolver_prefix) {
Craig Tillereb3b12e2015-06-26 14:42:49 -070050 g_number_of_resolvers = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070051 g_default_resolver_prefix = gpr_strdup(default_resolver_prefix);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070052}
53
Craig Tillera82950e2015-09-22 12:33:20 -070054void grpc_resolver_registry_shutdown(void) {
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070055 int i;
Craig Tillera82950e2015-09-22 12:33:20 -070056 for (i = 0; i < g_number_of_resolvers; i++) {
57 grpc_resolver_factory_unref(g_all_of_the_resolvers[i]);
58 }
59 gpr_free(g_default_resolver_prefix);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070060}
61
Craig Tillera82950e2015-09-22 12:33:20 -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++) {
65 GPR_ASSERT(0 != strcmp(factory->vtable->scheme,
66 g_all_of_the_resolvers[i]->vtable->scheme));
67 }
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 Tiller65938df2016-03-31 13:08:49 -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 Tillereb3b12e2015-06-26 14:42:49 -070081
82 return NULL;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070083}
84
Craig Tiller65938df2016-03-31 13:08:49 -070085grpc_resolver_factory *grpc_resolver_factory_lookup(const char *name) {
86 grpc_resolver_factory *f = lookup_factory(name);
87 if (f) grpc_resolver_factory_ref(f);
88 return f;
89}
90
91static grpc_resolver_factory *lookup_factory_by_uri(grpc_uri *uri) {
92 if (!uri) return NULL;
93 return lookup_factory(uri->scheme);
94}
95
Craig Tillera82950e2015-09-22 12:33:20 -070096static grpc_resolver_factory *resolve_factory(const char *target,
97 grpc_uri **uri) {
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070098 char *tmp;
Craig Tillereb3b12e2015-06-26 14:42:49 -070099 grpc_resolver_factory *factory = NULL;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700100
Craig Tillera82950e2015-09-22 12:33:20 -0700101 GPR_ASSERT(uri != NULL);
102 *uri = grpc_uri_parse(target, 1);
Craig Tiller65938df2016-03-31 13:08:49 -0700103 factory = lookup_factory_by_uri(*uri);
Craig Tillera82950e2015-09-22 12:33:20 -0700104 if (factory == NULL) {
105 if (g_default_resolver_prefix != NULL) {
106 grpc_uri_destroy(*uri);
107 gpr_asprintf(&tmp, "%s%s", g_default_resolver_prefix, target);
108 *uri = grpc_uri_parse(tmp, 1);
Craig Tiller65938df2016-03-31 13:08:49 -0700109 factory = lookup_factory_by_uri(*uri);
Craig Tillera82950e2015-09-22 12:33:20 -0700110 if (factory == NULL) {
111 grpc_uri_destroy(grpc_uri_parse(target, 0));
112 grpc_uri_destroy(grpc_uri_parse(tmp, 0));
113 gpr_log(GPR_ERROR, "don't know how to resolve '%s' or '%s'", target,
114 tmp);
115 }
116 gpr_free(tmp);
117 } else {
118 grpc_uri_destroy(grpc_uri_parse(target, 0));
119 gpr_log(GPR_ERROR, "don't know how to resolve '%s'", target);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700120 }
Craig Tillera82950e2015-09-22 12:33:20 -0700121 }
Craig Tillerbc85be12015-08-24 10:36:39 -0700122 return factory;
123}
124
Craig Tillera82950e2015-09-22 12:33:20 -0700125grpc_resolver *grpc_resolver_create(
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -0700126 const char *target, grpc_client_channel_factory *client_channel_factory) {
Craig Tillerbc85be12015-08-24 10:36:39 -0700127 grpc_uri *uri = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700128 grpc_resolver_factory *factory = resolve_factory(target, &uri);
Craig Tiller06a43f52015-09-15 07:41:28 -0700129 grpc_resolver *resolver;
130 grpc_resolver_args args;
Craig Tillera82950e2015-09-22 12:33:20 -0700131 memset(&args, 0, sizeof(args));
Craig Tiller06a43f52015-09-15 07:41:28 -0700132 args.uri = uri;
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -0700133 args.client_channel_factory = client_channel_factory;
Craig Tillera82950e2015-09-22 12:33:20 -0700134 resolver = grpc_resolver_factory_create_resolver(factory, &args);
135 grpc_uri_destroy(uri);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700136 return resolver;
137}
Craig Tillerbc85be12015-08-24 10:36:39 -0700138
Craig Tillera82950e2015-09-22 12:33:20 -0700139char *grpc_get_default_authority(const char *target) {
Craig Tillerbc85be12015-08-24 10:36:39 -0700140 grpc_uri *uri = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700141 grpc_resolver_factory *factory = resolve_factory(target, &uri);
142 char *authority = grpc_resolver_factory_get_default_authority(factory, uri);
143 grpc_uri_destroy(uri);
Craig Tillerbc85be12015-08-24 10:36:39 -0700144 return authority;
145}