blob: a38b3d89951c5c795b5f1c48ba1e31961fec70e6 [file] [log] [blame]
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07001/*
2 *
Craig Tiller8a9fd522016-03-25 17:09:29 -07003 * Copyright 2015-2016, 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
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
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 Tillera82950e2015-09-22 12:33:20 -070073static grpc_resolver_factory *lookup_factory(grpc_uri *uri) {
Craig Tillereb3b12e2015-06-26 14:42:49 -070074 int i;
75
76 /* handling NULL uri's here simplifies grpc_resolver_create */
Craig Tillera82950e2015-09-22 12:33:20 -070077 if (!uri) return NULL;
Craig Tillereb3b12e2015-06-26 14:42:49 -070078
Craig Tillera82950e2015-09-22 12:33:20 -070079 for (i = 0; i < g_number_of_resolvers; i++) {
80 if (0 == strcmp(uri->scheme, g_all_of_the_resolvers[i]->vtable->scheme)) {
81 return g_all_of_the_resolvers[i];
Craig Tillereb3b12e2015-06-26 14:42:49 -070082 }
Craig Tillera82950e2015-09-22 12:33:20 -070083 }
Craig Tillereb3b12e2015-06-26 14:42:49 -070084
85 return NULL;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070086}
87
Craig Tillera82950e2015-09-22 12:33:20 -070088static grpc_resolver_factory *resolve_factory(const char *target,
89 grpc_uri **uri) {
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070090 char *tmp;
Craig Tillereb3b12e2015-06-26 14:42:49 -070091 grpc_resolver_factory *factory = NULL;
Craig Tillereb3b12e2015-06-26 14:42:49 -070092
Craig Tillera82950e2015-09-22 12:33:20 -070093 GPR_ASSERT(uri != NULL);
94 *uri = grpc_uri_parse(target, 1);
95 factory = lookup_factory(*uri);
96 if (factory == NULL) {
97 if (g_default_resolver_prefix != NULL) {
98 grpc_uri_destroy(*uri);
99 gpr_asprintf(&tmp, "%s%s", g_default_resolver_prefix, target);
100 *uri = grpc_uri_parse(tmp, 1);
101 factory = lookup_factory(*uri);
102 if (factory == NULL) {
103 grpc_uri_destroy(grpc_uri_parse(target, 0));
104 grpc_uri_destroy(grpc_uri_parse(tmp, 0));
105 gpr_log(GPR_ERROR, "don't know how to resolve '%s' or '%s'", target,
106 tmp);
107 }
108 gpr_free(tmp);
109 } else {
110 grpc_uri_destroy(grpc_uri_parse(target, 0));
111 gpr_log(GPR_ERROR, "don't know how to resolve '%s'", target);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700112 }
Craig Tillera82950e2015-09-22 12:33:20 -0700113 }
Craig Tillerbc85be12015-08-24 10:36:39 -0700114 return factory;
115}
116
Craig Tillera82950e2015-09-22 12:33:20 -0700117grpc_resolver *grpc_resolver_create(
118 const char *target, grpc_subchannel_factory *subchannel_factory) {
Craig Tillerbc85be12015-08-24 10:36:39 -0700119 grpc_uri *uri = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700120 grpc_resolver_factory *factory = resolve_factory(target, &uri);
Craig Tiller06a43f52015-09-15 07:41:28 -0700121 grpc_resolver *resolver;
122 grpc_resolver_args args;
Craig Tillera82950e2015-09-22 12:33:20 -0700123 memset(&args, 0, sizeof(args));
Craig Tiller06a43f52015-09-15 07:41:28 -0700124 args.uri = uri;
125 args.subchannel_factory = subchannel_factory;
Craig Tillera82950e2015-09-22 12:33:20 -0700126 resolver = grpc_resolver_factory_create_resolver(factory, &args);
127 grpc_uri_destroy(uri);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700128 return resolver;
129}
Craig Tillerbc85be12015-08-24 10:36:39 -0700130
Craig Tillera82950e2015-09-22 12:33:20 -0700131char *grpc_get_default_authority(const char *target) {
Craig Tillerbc85be12015-08-24 10:36:39 -0700132 grpc_uri *uri = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700133 grpc_resolver_factory *factory = resolve_factory(target, &uri);
134 char *authority = grpc_resolver_factory_get_default_authority(factory, uri);
135 grpc_uri_destroy(uri);
Craig Tillerbc85be12015-08-24 10:36:39 -0700136 return authority;
137}