Craig Tiller | cd29c58 | 2015-06-24 09:15: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 | cd29c58 | 2015-06-24 09:15: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 | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
| 36 | #include <grpc/support/alloc.h> |
Craig Tiller | 6b9f5c6 | 2015-07-29 18:47:35 -0700 | [diff] [blame] | 37 | #include <grpc/support/host_port.h> |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 38 | #include <grpc/support/string_util.h> |
| 39 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 40 | #include "src/core/ext/client_config/lb_policy_registry.h" |
| 41 | #include "src/core/ext/client_config/resolver_registry.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 42 | #include "src/core/lib/iomgr/resolve_address.h" |
| 43 | #include "src/core/lib/iomgr/timer.h" |
| 44 | #include "src/core/lib/support/backoff.h" |
| 45 | #include "src/core/lib/support/string.h" |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 46 | |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 47 | #define BACKOFF_MULTIPLIER 1.6 |
| 48 | #define BACKOFF_JITTER 0.2 |
| 49 | #define BACKOFF_MIN_SECONDS 1 |
| 50 | #define BACKOFF_MAX_SECONDS 120 |
| 51 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 52 | typedef struct { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 53 | /** base class: must be first */ |
| 54 | grpc_resolver base; |
| 55 | /** refcount */ |
| 56 | gpr_refcount refs; |
| 57 | /** name to resolve */ |
| 58 | char *name; |
| 59 | /** default port to use */ |
| 60 | char *default_port; |
| 61 | /** subchannel factory */ |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 62 | grpc_client_channel_factory *client_channel_factory; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 63 | /** load balancing policy name */ |
| 64 | char *lb_policy_name; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 65 | |
| 66 | /** mutex guarding the rest of the state */ |
| 67 | gpr_mu mu; |
| 68 | /** are we currently resolving? */ |
| 69 | int resolving; |
| 70 | /** which version of resolved_config have we published? */ |
| 71 | int published_version; |
| 72 | /** which version of resolved_config is current? */ |
| 73 | int resolved_version; |
| 74 | /** pending next completion, or NULL */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 75 | grpc_closure *next_completion; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 76 | /** target config address for next completion */ |
| 77 | grpc_client_config **target_config; |
| 78 | /** current (fully resolved) config */ |
| 79 | grpc_client_config *resolved_config; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 80 | /** retry timer */ |
| 81 | bool have_retry_timer; |
| 82 | grpc_timer retry_timer; |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 83 | /** retry backoff state */ |
| 84 | gpr_backoff backoff_state; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 85 | } dns_resolver; |
| 86 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 87 | static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 88 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 89 | static void dns_start_resolving_locked(dns_resolver *r); |
| 90 | static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 91 | dns_resolver *r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 92 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | static void dns_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 94 | static void dns_channel_saw_error(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *r, |
| 96 | grpc_client_config **target_config, |
| 97 | grpc_closure *on_complete); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 98 | |
| 99 | static const grpc_resolver_vtable dns_resolver_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | dns_destroy, dns_shutdown, dns_channel_saw_error, dns_next}; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 101 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | static void dns_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver) { |
| 103 | dns_resolver *r = (dns_resolver *)resolver; |
| 104 | gpr_mu_lock(&r->mu); |
Craig Tiller | e2327db | 2016-03-11 09:52:42 -0800 | [diff] [blame] | 105 | if (r->have_retry_timer) { |
| 106 | grpc_timer_cancel(exec_ctx, &r->retry_timer); |
| 107 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | if (r->next_completion != NULL) { |
| 109 | *r->target_config = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 110 | grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | r->next_completion = NULL; |
| 112 | } |
| 113 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | static void dns_channel_saw_error(grpc_exec_ctx *exec_ctx, |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 117 | grpc_resolver *resolver) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | dns_resolver *r = (dns_resolver *)resolver; |
| 119 | gpr_mu_lock(&r->mu); |
| 120 | if (!r->resolving) { |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 121 | gpr_backoff_reset(&r->backoff_state); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 122 | dns_start_resolving_locked(r); |
| 123 | } |
| 124 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver, |
| 128 | grpc_client_config **target_config, |
| 129 | grpc_closure *on_complete) { |
| 130 | dns_resolver *r = (dns_resolver *)resolver; |
| 131 | gpr_mu_lock(&r->mu); |
| 132 | GPR_ASSERT(!r->next_completion); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 133 | r->next_completion = on_complete; |
| 134 | r->target_config = target_config; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | if (r->resolved_version == 0 && !r->resolving) { |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 136 | gpr_backoff_reset(&r->backoff_state); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | dns_start_resolving_locked(r); |
| 138 | } else { |
| 139 | dns_maybe_finish_next_locked(exec_ctx, r); |
| 140 | } |
| 141 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 144 | static void dns_on_retry_timer(grpc_exec_ctx *exec_ctx, void *arg, |
| 145 | bool success) { |
| 146 | dns_resolver *r = arg; |
| 147 | |
Craig Tiller | e2327db | 2016-03-11 09:52:42 -0800 | [diff] [blame] | 148 | gpr_mu_lock(&r->mu); |
| 149 | r->have_retry_timer = false; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 150 | if (success) { |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 151 | if (!r->resolving) { |
| 152 | dns_start_resolving_locked(r); |
| 153 | } |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 154 | } |
Craig Tiller | e2327db | 2016-03-11 09:52:42 -0800 | [diff] [blame] | 155 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 156 | |
| 157 | GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "retry-timer"); |
| 158 | } |
| 159 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 160 | static void dns_on_resolved(grpc_exec_ctx *exec_ctx, void *arg, |
| 161 | grpc_resolved_addresses *addresses) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 162 | dns_resolver *r = arg; |
| 163 | grpc_client_config *config = NULL; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 164 | grpc_lb_policy *lb_policy; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 165 | gpr_mu_lock(&r->mu); |
| 166 | GPR_ASSERT(r->resolving); |
| 167 | r->resolving = 0; |
| 168 | if (addresses != NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 169 | grpc_lb_policy_args lb_policy_args; |
| 170 | config = grpc_client_config_create(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | memset(&lb_policy_args, 0, sizeof(lb_policy_args)); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 172 | lb_policy_args.addresses = addresses; |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 173 | lb_policy_args.client_channel_factory = r->client_channel_factory; |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 174 | lb_policy = |
| 175 | grpc_lb_policy_create(exec_ctx, r->lb_policy_name, &lb_policy_args); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 176 | if (lb_policy != NULL) { |
| 177 | grpc_client_config_set_lb_policy(config, lb_policy); |
| 178 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "construction"); |
| 179 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 180 | grpc_resolved_addresses_destroy(addresses); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 181 | } else { |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 182 | gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC); |
| 183 | gpr_timespec next_try = gpr_backoff_step(&r->backoff_state, now); |
| 184 | gpr_timespec timeout = gpr_time_sub(next_try, now); |
| 185 | gpr_log(GPR_DEBUG, "dns resolution failed: retrying in %d.%09d seconds", |
| 186 | timeout.tv_sec, timeout.tv_nsec); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 187 | GPR_ASSERT(!r->have_retry_timer); |
| 188 | r->have_retry_timer = true; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 189 | GRPC_RESOLVER_REF(&r->base, "retry-timer"); |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 190 | grpc_timer_init(exec_ctx, &r->retry_timer, next_try, dns_on_retry_timer, r, |
| 191 | now); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 192 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | if (r->resolved_config) { |
| 194 | grpc_client_config_unref(exec_ctx, r->resolved_config); |
| 195 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 196 | r->resolved_config = config; |
| 197 | r->resolved_version++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 198 | dns_maybe_finish_next_locked(exec_ctx, r); |
| 199 | gpr_mu_unlock(&r->mu); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 200 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 201 | GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "dns-resolving"); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 204 | static void dns_start_resolving_locked(dns_resolver *r) { |
| 205 | GRPC_RESOLVER_REF(&r->base, "dns-resolving"); |
| 206 | GPR_ASSERT(!r->resolving); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 207 | r->resolving = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 208 | grpc_resolve_address(r->name, r->default_port, dns_on_resolved, r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 212 | dns_resolver *r) { |
| 213 | if (r->next_completion != NULL && |
| 214 | r->resolved_version != r->published_version) { |
| 215 | *r->target_config = r->resolved_config; |
| 216 | if (r->resolved_config) { |
| 217 | grpc_client_config_ref(r->resolved_config); |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 218 | } |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 219 | grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | r->next_completion = NULL; |
| 221 | r->published_version = r->resolved_version; |
| 222 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 225 | static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) { |
| 226 | dns_resolver *r = (dns_resolver *)gr; |
| 227 | gpr_mu_destroy(&r->mu); |
| 228 | if (r->resolved_config) { |
| 229 | grpc_client_config_unref(exec_ctx, r->resolved_config); |
| 230 | } |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 231 | grpc_client_channel_factory_unref(exec_ctx, r->client_channel_factory); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 232 | gpr_free(r->name); |
| 233 | gpr_free(r->default_port); |
| 234 | gpr_free(r->lb_policy_name); |
| 235 | gpr_free(r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 238 | static grpc_resolver *dns_create(grpc_resolver_args *args, |
| 239 | const char *default_port, |
| 240 | const char *lb_policy_name) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 241 | dns_resolver *r; |
Craig Tiller | 06a43f5 | 2015-09-15 07:41:28 -0700 | [diff] [blame] | 242 | const char *path = args->uri->path; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 243 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 244 | if (0 != strcmp(args->uri->authority, "")) { |
| 245 | gpr_log(GPR_ERROR, "authority based dns uri's not supported"); |
| 246 | return NULL; |
| 247 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 248 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 249 | if (path[0] == '/') ++path; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 250 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 251 | r = gpr_malloc(sizeof(dns_resolver)); |
| 252 | memset(r, 0, sizeof(*r)); |
| 253 | gpr_ref_init(&r->refs, 1); |
| 254 | gpr_mu_init(&r->mu); |
| 255 | grpc_resolver_init(&r->base, &dns_resolver_vtable); |
| 256 | r->name = gpr_strdup(path); |
| 257 | r->default_port = gpr_strdup(default_port); |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 258 | r->client_channel_factory = args->client_channel_factory; |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 259 | gpr_backoff_init(&r->backoff_state, BACKOFF_MULTIPLIER, BACKOFF_JITTER, |
| 260 | BACKOFF_MIN_SECONDS * 1000, BACKOFF_MAX_SECONDS * 1000); |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 261 | grpc_client_channel_factory_ref(r->client_channel_factory); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 262 | r->lb_policy_name = gpr_strdup(lb_policy_name); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 263 | return &r->base; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * FACTORY |
| 268 | */ |
| 269 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 270 | static void dns_factory_ref(grpc_resolver_factory *factory) {} |
| 271 | |
| 272 | static void dns_factory_unref(grpc_resolver_factory *factory) {} |
| 273 | |
| 274 | static grpc_resolver *dns_factory_create_resolver( |
| 275 | grpc_resolver_factory *factory, grpc_resolver_args *args) { |
| 276 | return dns_create(args, "https", "pick_first"); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 279 | static char *dns_factory_get_default_host_name(grpc_resolver_factory *factory, |
| 280 | grpc_uri *uri) { |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 281 | const char *path = uri->path; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 282 | if (path[0] == '/') ++path; |
| 283 | return gpr_strdup(path); |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 286 | static const grpc_resolver_factory_vtable dns_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 287 | dns_factory_ref, dns_factory_unref, dns_factory_create_resolver, |
| 288 | dns_factory_get_default_host_name, "dns"}; |
| 289 | static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable}; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 290 | |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 291 | static grpc_resolver_factory *dns_resolver_factory_create() { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 292 | return &dns_resolver_factory; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 293 | } |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 294 | |
| 295 | void grpc_resolver_dns_native_init(void) { |
| 296 | grpc_register_resolver_type(dns_resolver_factory_create()); |
| 297 | } |
| 298 | |
| 299 | void grpc_resolver_dns_native_shutdown(void) {} |