Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 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 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 34 | #include "src/core/ext/client_config/resolver_registry.h" |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 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 Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 44 | static grpc_resolver_factory *g_all_of_the_resolvers[MAX_RESOLVERS]; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 45 | static int g_number_of_resolvers = 0; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 46 | |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 47 | static char *g_default_resolver_prefix; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 48 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 49 | void grpc_resolver_registry_init(const char *default_resolver_prefix) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 50 | g_number_of_resolvers = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 51 | g_default_resolver_prefix = gpr_strdup(default_resolver_prefix); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 54 | void grpc_resolver_registry_shutdown(void) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 55 | int i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | 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 Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 62 | void grpc_register_resolver_type(grpc_resolver_factory *factory) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 63 | int i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 64 | 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 Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 70 | g_all_of_the_resolvers[g_number_of_resolvers++] = factory; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 73 | static grpc_resolver_factory *lookup_factory(const char *name) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 74 | int i; |
| 75 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 76 | for (i = 0; i < g_number_of_resolvers; i++) { |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 77 | if (0 == strcmp(name, g_all_of_the_resolvers[i]->vtable->scheme)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | return g_all_of_the_resolvers[i]; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 79 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 81 | |
| 82 | return NULL; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 85 | grpc_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 | |
| 91 | static grpc_resolver_factory *lookup_factory_by_uri(grpc_uri *uri) { |
| 92 | if (!uri) return NULL; |
| 93 | return lookup_factory(uri->scheme); |
| 94 | } |
| 95 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | static grpc_resolver_factory *resolve_factory(const char *target, |
| 97 | grpc_uri **uri) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 98 | char *tmp; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 99 | grpc_resolver_factory *factory = NULL; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 100 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | GPR_ASSERT(uri != NULL); |
| 102 | *uri = grpc_uri_parse(target, 1); |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 103 | factory = lookup_factory_by_uri(*uri); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 104 | 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 Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 109 | factory = lookup_factory_by_uri(*uri); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | 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 Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 120 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | } |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 122 | return factory; |
| 123 | } |
| 124 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | grpc_resolver *grpc_resolver_create( |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 126 | const char *target, grpc_client_channel_factory *client_channel_factory) { |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 127 | grpc_uri *uri = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | grpc_resolver_factory *factory = resolve_factory(target, &uri); |
Craig Tiller | 06a43f5 | 2015-09-15 07:41:28 -0700 | [diff] [blame] | 129 | grpc_resolver *resolver; |
| 130 | grpc_resolver_args args; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | memset(&args, 0, sizeof(args)); |
Craig Tiller | 06a43f5 | 2015-09-15 07:41:28 -0700 | [diff] [blame] | 132 | args.uri = uri; |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 133 | args.client_channel_factory = client_channel_factory; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 134 | resolver = grpc_resolver_factory_create_resolver(factory, &args); |
| 135 | grpc_uri_destroy(uri); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 136 | return resolver; |
| 137 | } |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 138 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 139 | char *grpc_get_default_authority(const char *target) { |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 140 | grpc_uri *uri = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | 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 Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 144 | return authority; |
| 145 | } |