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