Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -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 | |
David Garcia Quintas | 64ddf13 | 2016-03-30 23:53:50 -0700 | [diff] [blame] | 34 | #include <stdbool.h> |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | #include <string.h> |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 37 | |
| 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/host_port.h> |
David Garcia Quintas | f47240e | 2016-04-04 13:51:29 -0700 | [diff] [blame] | 40 | #include <grpc/support/port_platform.h> |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 41 | #include <grpc/support/string_util.h> |
| 42 | |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 43 | #ifdef GPR_HAVE_UNIX_SOCKET |
| 44 | #include <sys/un.h> |
| 45 | #endif |
| 46 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 47 | #include "src/core/ext/client_config/lb_policy_registry.h" |
| 48 | #include "src/core/ext/client_config/resolver_registry.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 49 | #include "src/core/lib/iomgr/resolve_address.h" |
| 50 | #include "src/core/lib/iomgr/unix_sockets_posix.h" |
| 51 | #include "src/core/lib/support/string.h" |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 52 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | typedef struct { |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 54 | /** base class: must be first */ |
| 55 | grpc_resolver base; |
| 56 | /** refcount */ |
| 57 | gpr_refcount refs; |
| 58 | /** subchannel factory */ |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 59 | grpc_client_channel_factory *client_channel_factory; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 60 | /** load balancing policy name */ |
| 61 | char *lb_policy_name; |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 62 | |
David Garcia Quintas | 2bfd275 | 2015-08-20 11:42:29 -0700 | [diff] [blame] | 63 | /** the addresses that we've 'resolved' */ |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 64 | grpc_resolved_addresses *addresses; |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 65 | |
| 66 | /** mutex guarding the rest of the state */ |
| 67 | gpr_mu mu; |
| 68 | /** have we published? */ |
| 69 | int published; |
| 70 | /** pending next completion, or NULL */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 71 | grpc_closure *next_completion; |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 72 | /** target config address for next completion */ |
| 73 | grpc_client_config **target_config; |
| 74 | } sockaddr_resolver; |
| 75 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 76 | static void sockaddr_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 77 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | static void sockaddr_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 79 | sockaddr_resolver *r); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 80 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 81 | static void sockaddr_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
| 82 | static void sockaddr_channel_saw_error(grpc_exec_ctx *exec_ctx, |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 83 | grpc_resolver *r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | static void sockaddr_next(grpc_exec_ctx *exec_ctx, grpc_resolver *r, |
| 85 | grpc_client_config **target_config, |
| 86 | grpc_closure *on_complete); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 87 | |
| 88 | static const grpc_resolver_vtable sockaddr_resolver_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 89 | sockaddr_destroy, sockaddr_shutdown, sockaddr_channel_saw_error, |
| 90 | sockaddr_next}; |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 91 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | static void sockaddr_shutdown(grpc_exec_ctx *exec_ctx, |
| 93 | grpc_resolver *resolver) { |
| 94 | sockaddr_resolver *r = (sockaddr_resolver *)resolver; |
| 95 | gpr_mu_lock(&r->mu); |
| 96 | if (r->next_completion != NULL) { |
| 97 | *r->target_config = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 98 | grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | r->next_completion = NULL; |
| 100 | } |
| 101 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 104 | static void sockaddr_channel_saw_error(grpc_exec_ctx *exec_ctx, |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 105 | grpc_resolver *resolver) { |
Craig Tiller | 486ea35 | 2015-11-24 16:58:41 -0800 | [diff] [blame] | 106 | sockaddr_resolver *r = (sockaddr_resolver *)resolver; |
| 107 | gpr_mu_lock(&r->mu); |
| 108 | r->published = 0; |
| 109 | sockaddr_maybe_finish_next_locked(exec_ctx, r); |
| 110 | gpr_mu_unlock(&r->mu); |
| 111 | } |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 112 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | static void sockaddr_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver, |
| 114 | grpc_client_config **target_config, |
| 115 | grpc_closure *on_complete) { |
| 116 | sockaddr_resolver *r = (sockaddr_resolver *)resolver; |
| 117 | gpr_mu_lock(&r->mu); |
| 118 | GPR_ASSERT(!r->next_completion); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 119 | r->next_completion = on_complete; |
| 120 | r->target_config = target_config; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | sockaddr_maybe_finish_next_locked(exec_ctx, r); |
| 122 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | static void sockaddr_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 126 | sockaddr_resolver *r) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | if (r->next_completion != NULL && !r->published) { |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 128 | grpc_client_config *cfg = grpc_client_config_create(); |
| 129 | grpc_lb_policy_args lb_policy_args; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | memset(&lb_policy_args, 0, sizeof(lb_policy_args)); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 131 | lb_policy_args.addresses = r->addresses; |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 132 | lb_policy_args.client_channel_factory = r->client_channel_factory; |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 133 | grpc_lb_policy *lb_policy = |
| 134 | grpc_lb_policy_create(exec_ctx, r->lb_policy_name, &lb_policy_args); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | grpc_client_config_set_lb_policy(cfg, lb_policy); |
| 136 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "sockaddr"); |
| 137 | r->published = 1; |
| 138 | *r->target_config = cfg; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 139 | grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | r->next_completion = NULL; |
| 141 | } |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 144 | static void sockaddr_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) { |
| 145 | sockaddr_resolver *r = (sockaddr_resolver *)gr; |
| 146 | gpr_mu_destroy(&r->mu); |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 147 | grpc_client_channel_factory_unref(exec_ctx, r->client_channel_factory); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 148 | grpc_resolved_addresses_destroy(r->addresses); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 149 | gpr_free(r->lb_policy_name); |
| 150 | gpr_free(r); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 153 | static char *ip_get_default_authority(grpc_uri *uri) { |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 154 | const char *path = uri->path; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | if (path[0] == '/') ++path; |
| 156 | return gpr_strdup(path); |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | static char *ipv4_get_default_authority(grpc_resolver_factory *factory, |
| 160 | grpc_uri *uri) { |
| 161 | return ip_get_default_authority(uri); |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 164 | static char *ipv6_get_default_authority(grpc_resolver_factory *factory, |
| 165 | grpc_uri *uri) { |
| 166 | return ip_get_default_authority(uri); |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 169 | #ifdef GPR_HAVE_UNIX_SOCKET |
| 170 | static int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, |
| 171 | size_t *len) { |
| 172 | struct sockaddr_un *un = (struct sockaddr_un *)addr; |
| 173 | |
| 174 | un->sun_family = AF_UNIX; |
| 175 | strcpy(un->sun_path, uri->path); |
| 176 | *len = strlen(un->sun_path) + sizeof(un->sun_family) + 1; |
| 177 | |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | char *unix_get_default_authority(grpc_resolver_factory *factory, |
| 182 | grpc_uri *uri) { |
| 183 | return gpr_strdup("localhost"); |
| 184 | } |
| 185 | #endif |
| 186 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | static int parse_ipv4(grpc_uri *uri, struct sockaddr_storage *addr, |
| 188 | size_t *len) { |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 189 | const char *host_port = uri->path; |
| 190 | char *host; |
| 191 | char *port; |
| 192 | int port_num; |
| 193 | int result = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 194 | struct sockaddr_in *in = (struct sockaddr_in *)addr; |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 195 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 196 | if (*host_port == '/') ++host_port; |
| 197 | if (!gpr_split_host_port(host_port, &host, &port)) { |
| 198 | return 0; |
| 199 | } |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 200 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 201 | memset(in, 0, sizeof(*in)); |
| 202 | *len = sizeof(*in); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 203 | in->sin_family = AF_INET; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 204 | if (inet_pton(AF_INET, host, &in->sin_addr) == 0) { |
| 205 | gpr_log(GPR_ERROR, "invalid ipv4 address: '%s'", host); |
| 206 | goto done; |
| 207 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 208 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 209 | if (port != NULL) { |
| 210 | if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 || |
| 211 | port_num > 65535) { |
| 212 | gpr_log(GPR_ERROR, "invalid ipv4 port: '%s'", port); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 213 | goto done; |
| 214 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 215 | in->sin_port = htons((uint16_t)port_num); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 216 | } else { |
| 217 | gpr_log(GPR_ERROR, "no port given for ipv4 scheme"); |
| 218 | goto done; |
| 219 | } |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 220 | |
| 221 | result = 1; |
| 222 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | gpr_free(host); |
| 224 | gpr_free(port); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 225 | return result; |
| 226 | } |
| 227 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 228 | static int parse_ipv6(grpc_uri *uri, struct sockaddr_storage *addr, |
| 229 | size_t *len) { |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 230 | const char *host_port = uri->path; |
| 231 | char *host; |
| 232 | char *port; |
| 233 | int port_num; |
| 234 | int result = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 235 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 236 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 237 | if (*host_port == '/') ++host_port; |
| 238 | if (!gpr_split_host_port(host_port, &host, &port)) { |
| 239 | return 0; |
| 240 | } |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 241 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 242 | memset(in6, 0, sizeof(*in6)); |
| 243 | *len = sizeof(*in6); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 244 | in6->sin6_family = AF_INET6; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 245 | if (inet_pton(AF_INET6, host, &in6->sin6_addr) == 0) { |
| 246 | gpr_log(GPR_ERROR, "invalid ipv6 address: '%s'", host); |
| 247 | goto done; |
| 248 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 249 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 250 | if (port != NULL) { |
| 251 | if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 || |
| 252 | port_num > 65535) { |
| 253 | gpr_log(GPR_ERROR, "invalid ipv6 port: '%s'", port); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 254 | goto done; |
| 255 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 256 | in6->sin6_port = htons((uint16_t)port_num); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 257 | } else { |
| 258 | gpr_log(GPR_ERROR, "no port given for ipv6 scheme"); |
| 259 | goto done; |
| 260 | } |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 261 | |
| 262 | result = 1; |
| 263 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | gpr_free(host); |
| 265 | gpr_free(port); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 266 | return result; |
| 267 | } |
| 268 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 269 | static void do_nothing(void *ignored) {} |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 270 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 271 | static grpc_resolver *sockaddr_create( |
| 272 | grpc_resolver_args *args, const char *default_lb_policy_name, |
| 273 | int parse(grpc_uri *uri, struct sockaddr_storage *dst, size_t *len)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 274 | int errors_found = 0; /* GPR_FALSE */ |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 275 | sockaddr_resolver *r; |
David Garcia Quintas | 2bfd275 | 2015-08-20 11:42:29 -0700 | [diff] [blame] | 276 | gpr_slice path_slice; |
| 277 | gpr_slice_buffer path_parts; |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 278 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 279 | if (0 != strcmp(args->uri->authority, "")) { |
| 280 | gpr_log(GPR_ERROR, "authority based uri's not supported by the %s scheme", |
| 281 | args->uri->scheme); |
| 282 | return NULL; |
| 283 | } |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 284 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 285 | r = gpr_malloc(sizeof(sockaddr_resolver)); |
| 286 | memset(r, 0, sizeof(*r)); |
David Garcia Quintas | 2bfd275 | 2015-08-20 11:42:29 -0700 | [diff] [blame] | 287 | |
David Garcia Quintas | f9a7e28 | 2016-03-31 09:35:57 -0700 | [diff] [blame] | 288 | r->lb_policy_name = |
| 289 | gpr_strdup(grpc_uri_get_query_arg(args->uri, "lb_policy")); |
| 290 | const char *lb_enabled_qpart = |
| 291 | grpc_uri_get_query_arg(args->uri, "lb_enabled"); |
| 292 | /* anything other than "0" is interpreted as true */ |
| 293 | const bool lb_enabled = |
| 294 | (lb_enabled_qpart != NULL && (strcmp("0", lb_enabled_qpart) != 0)); |
David Garcia Quintas | fe7a636 | 2015-09-10 11:06:46 -0700 | [diff] [blame] | 295 | |
David Garcia Quintas | 64ddf13 | 2016-03-30 23:53:50 -0700 | [diff] [blame] | 296 | if (r->lb_policy_name != NULL && strcmp("grpclb", r->lb_policy_name) == 0 && |
| 297 | !lb_enabled) { |
| 298 | /* we want grpclb but the "resolved" addresses aren't LB enabled. Bail |
| 299 | * out, as this is meant mostly for tests. */ |
| 300 | gpr_log(GPR_ERROR, |
| 301 | "Requested 'grpclb' LB policy but resolved addresses don't " |
| 302 | "support load balancing."); |
| 303 | abort(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 304 | } |
David Garcia Quintas | 64ddf13 | 2016-03-30 23:53:50 -0700 | [diff] [blame] | 305 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 306 | if (r->lb_policy_name == NULL) { |
| 307 | r->lb_policy_name = gpr_strdup(default_lb_policy_name); |
| 308 | } |
David Garcia Quintas | fe7a636 | 2015-09-10 11:06:46 -0700 | [diff] [blame] | 309 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 310 | path_slice = |
| 311 | gpr_slice_new(args->uri->path, strlen(args->uri->path), do_nothing); |
| 312 | gpr_slice_buffer_init(&path_parts); |
David Garcia Quintas | 2bfd275 | 2015-08-20 11:42:29 -0700 | [diff] [blame] | 313 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 314 | gpr_slice_split(path_slice, ",", &path_parts); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 315 | r->addresses = gpr_malloc(sizeof(grpc_resolved_addresses)); |
| 316 | r->addresses->naddrs = path_parts.count; |
| 317 | r->addresses->addrs = |
| 318 | gpr_malloc(sizeof(grpc_resolved_address) * r->addresses->naddrs); |
David Garcia Quintas | 2bfd275 | 2015-08-20 11:42:29 -0700 | [diff] [blame] | 319 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 320 | for (size_t i = 0; i < r->addresses->naddrs; i++) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 321 | grpc_uri ith_uri = *args->uri; |
| 322 | char *part_str = gpr_dump_slice(path_parts.slices[i], GPR_DUMP_ASCII); |
| 323 | ith_uri.path = part_str; |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 324 | if (!parse(&ith_uri, |
| 325 | (struct sockaddr_storage *)(&r->addresses->addrs[i].addr), |
| 326 | &r->addresses->addrs[i].len)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 327 | errors_found = 1; /* GPR_TRUE */ |
David Garcia Quintas | 2bfd275 | 2015-08-20 11:42:29 -0700 | [diff] [blame] | 328 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 329 | gpr_free(part_str); |
| 330 | if (errors_found) break; |
| 331 | } |
David Garcia Quintas | 2bfd275 | 2015-08-20 11:42:29 -0700 | [diff] [blame] | 332 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 333 | gpr_slice_buffer_destroy(&path_parts); |
| 334 | gpr_slice_unref(path_slice); |
| 335 | if (errors_found) { |
Craig Tiller | e8ab66c | 2015-12-11 14:47:01 -0800 | [diff] [blame] | 336 | gpr_free(r->lb_policy_name); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 337 | grpc_resolved_addresses_destroy(r->addresses); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 338 | gpr_free(r); |
| 339 | return NULL; |
| 340 | } |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 341 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 342 | gpr_ref_init(&r->refs, 1); |
| 343 | gpr_mu_init(&r->mu); |
| 344 | grpc_resolver_init(&r->base, &sockaddr_resolver_vtable); |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 345 | r->client_channel_factory = args->client_channel_factory; |
| 346 | grpc_client_channel_factory_ref(r->client_channel_factory); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 347 | |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 348 | return &r->base; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * FACTORY |
| 353 | */ |
| 354 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 355 | static void sockaddr_factory_ref(grpc_resolver_factory *factory) {} |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 356 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 357 | static void sockaddr_factory_unref(grpc_resolver_factory *factory) {} |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 358 | |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 359 | #define DECL_FACTORY(name) \ |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 360 | static grpc_resolver *name##_factory_create_resolver( \ |
Craig Tiller | 06a43f5 | 2015-09-15 07:41:28 -0700 | [diff] [blame] | 361 | grpc_resolver_factory *factory, grpc_resolver_args *args) { \ |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 362 | return sockaddr_create(args, "pick_first", parse_##name); \ |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 363 | } \ |
| 364 | static const grpc_resolver_factory_vtable name##_factory_vtable = { \ |
| 365 | sockaddr_factory_ref, sockaddr_factory_unref, \ |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 366 | name##_factory_create_resolver, name##_get_default_authority, #name}; \ |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 367 | static grpc_resolver_factory name##_resolver_factory = { \ |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 368 | &name##_factory_vtable} |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 369 | |
ahedberg | fcbcbff | 2016-03-22 13:23:47 -0400 | [diff] [blame] | 370 | #ifdef GPR_HAVE_UNIX_SOCKET |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 371 | DECL_FACTORY(unix); |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 372 | #endif |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 373 | DECL_FACTORY(ipv4); |
| 374 | DECL_FACTORY(ipv6); |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 375 | |
| 376 | void grpc_resolver_sockaddr_init(void) { |
| 377 | grpc_register_resolver_type(&ipv4_resolver_factory); |
| 378 | grpc_register_resolver_type(&ipv6_resolver_factory); |
| 379 | #ifdef GPR_HAVE_UNIX_SOCKET |
| 380 | grpc_register_resolver_type(&unix_resolver_factory); |
| 381 | #endif |
| 382 | } |
| 383 | |
| 384 | void grpc_resolver_sockaddr_shutdown(void) {} |