Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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 | 81bcc4c | 2015-07-20 14:04:18 -0700 | [diff] [blame] | 40 | #ifdef GPR_POSIX_SOCKET |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 41 | #include <sys/un.h> |
Craig Tiller | 81bcc4c | 2015-07-20 14:04:18 -0700 | [diff] [blame] | 42 | #endif |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 43 | |
| 44 | #include <grpc/support/alloc.h> |
| 45 | #include <grpc/support/host_port.h> |
| 46 | #include <grpc/support/string_util.h> |
| 47 | |
| 48 | #include "src/core/client_config/lb_policies/pick_first.h" |
| 49 | #include "src/core/iomgr/resolve_address.h" |
| 50 | #include "src/core/support/string.h" |
| 51 | |
| 52 | typedef struct { |
| 53 | /** base class: must be first */ |
| 54 | grpc_resolver base; |
| 55 | /** refcount */ |
| 56 | gpr_refcount refs; |
| 57 | /** subchannel factory */ |
| 58 | grpc_subchannel_factory *subchannel_factory; |
| 59 | /** load balancing policy factory */ |
| 60 | grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, |
| 61 | size_t num_subchannels); |
| 62 | |
| 63 | /** the address that we've 'resolved' */ |
| 64 | struct sockaddr_storage addr; |
| 65 | int addr_len; |
| 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 */ |
| 72 | grpc_iomgr_closure *next_completion; |
| 73 | /** target config address for next completion */ |
| 74 | grpc_client_config **target_config; |
| 75 | } sockaddr_resolver; |
| 76 | |
| 77 | static void sockaddr_destroy(grpc_resolver *r); |
| 78 | |
| 79 | static void sockaddr_maybe_finish_next_locked(sockaddr_resolver *r); |
| 80 | |
| 81 | static void sockaddr_shutdown(grpc_resolver *r); |
| 82 | static void sockaddr_channel_saw_error(grpc_resolver *r, |
| 83 | struct sockaddr *failing_address, |
| 84 | int failing_address_len); |
| 85 | static void sockaddr_next(grpc_resolver *r, grpc_client_config **target_config, |
| 86 | grpc_iomgr_closure *on_complete); |
| 87 | |
| 88 | static const grpc_resolver_vtable sockaddr_resolver_vtable = { |
| 89 | sockaddr_destroy, sockaddr_shutdown, sockaddr_channel_saw_error, |
| 90 | sockaddr_next}; |
| 91 | |
| 92 | static void sockaddr_shutdown(grpc_resolver *resolver) { |
| 93 | sockaddr_resolver *r = (sockaddr_resolver *)resolver; |
| 94 | gpr_mu_lock(&r->mu); |
| 95 | if (r->next_completion != NULL) { |
| 96 | *r->target_config = NULL; |
| 97 | /* TODO(ctiller): add delayed callback */ |
| 98 | grpc_iomgr_add_callback(r->next_completion); |
| 99 | r->next_completion = NULL; |
| 100 | } |
| 101 | gpr_mu_unlock(&r->mu); |
| 102 | } |
| 103 | |
| 104 | static void sockaddr_channel_saw_error(grpc_resolver *resolver, |
| 105 | struct sockaddr *sa, int len) {} |
| 106 | |
| 107 | static void sockaddr_next(grpc_resolver *resolver, |
| 108 | grpc_client_config **target_config, |
| 109 | grpc_iomgr_closure *on_complete) { |
| 110 | sockaddr_resolver *r = (sockaddr_resolver *)resolver; |
| 111 | gpr_mu_lock(&r->mu); |
| 112 | GPR_ASSERT(!r->next_completion); |
| 113 | r->next_completion = on_complete; |
| 114 | r->target_config = target_config; |
| 115 | sockaddr_maybe_finish_next_locked(r); |
| 116 | gpr_mu_unlock(&r->mu); |
| 117 | } |
| 118 | |
| 119 | static void sockaddr_maybe_finish_next_locked(sockaddr_resolver *r) { |
| 120 | grpc_client_config *cfg; |
| 121 | grpc_lb_policy *lb_policy; |
| 122 | grpc_subchannel *subchannel; |
| 123 | grpc_subchannel_args args; |
| 124 | |
| 125 | if (r->next_completion != NULL && !r->published) { |
| 126 | cfg = grpc_client_config_create(); |
| 127 | memset(&args, 0, sizeof(args)); |
| 128 | args.addr = (struct sockaddr *)&r->addr; |
| 129 | args.addr_len = r->addr_len; |
| 130 | subchannel = |
| 131 | grpc_subchannel_factory_create_subchannel(r->subchannel_factory, &args); |
| 132 | lb_policy = r->lb_policy_factory(&subchannel, 1); |
| 133 | grpc_client_config_set_lb_policy(cfg, lb_policy); |
| 134 | GRPC_LB_POLICY_UNREF(lb_policy, "unix"); |
| 135 | r->published = 1; |
| 136 | *r->target_config = cfg; |
| 137 | grpc_iomgr_add_callback(r->next_completion); |
| 138 | r->next_completion = NULL; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void sockaddr_destroy(grpc_resolver *gr) { |
| 143 | sockaddr_resolver *r = (sockaddr_resolver *)gr; |
| 144 | gpr_mu_destroy(&r->mu); |
| 145 | grpc_subchannel_factory_unref(r->subchannel_factory); |
| 146 | gpr_free(r); |
| 147 | } |
| 148 | |
| 149 | #ifdef GPR_POSIX_SOCKET |
| 150 | static int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, int *len) { |
| 151 | struct sockaddr_un *un = (struct sockaddr_un *)addr; |
| 152 | |
| 153 | un->sun_family = AF_UNIX; |
| 154 | strcpy(un->sun_path, uri->path); |
| 155 | *len = strlen(un->sun_path) + sizeof(un->sun_family) + 1; |
| 156 | |
| 157 | return 1; |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | static int parse_ipv4(grpc_uri *uri, struct sockaddr_storage *addr, int *len) { |
| 162 | const char *host_port = uri->path; |
| 163 | char *host; |
| 164 | char *port; |
| 165 | int port_num; |
| 166 | int result = 0; |
| 167 | struct sockaddr_in *in = (struct sockaddr_in *)addr; |
| 168 | |
| 169 | if (*host_port == '/') ++host_port; |
| 170 | if (!gpr_split_host_port(host_port, &host, &port)) { |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | memset(in, 0, sizeof(*in)); |
| 175 | *len = sizeof(*in); |
| 176 | in->sin_family = AF_INET; |
Craig Tiller | 81bcc4c | 2015-07-20 14:04:18 -0700 | [diff] [blame] | 177 | if (inet_pton(AF_INET, host, &in->sin_addr) == 0) { |
Craig Tiller | 698d00c | 2015-07-20 12:32:58 -0700 | [diff] [blame] | 178 | gpr_log(GPR_ERROR, "invalid ipv4 address: '%s'", host); |
| 179 | goto done; |
| 180 | } |
| 181 | |
| 182 | if (port != NULL) { |
| 183 | if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 || |
| 184 | port_num > 65535) { |
| 185 | gpr_log(GPR_ERROR, "invalid ipv4 port: '%s'", port); |
| 186 | goto done; |
| 187 | } |
| 188 | in->sin_port = htons(port_num); |
| 189 | } else { |
| 190 | gpr_log(GPR_ERROR, "no port given for ipv4 scheme"); |
| 191 | goto done; |
| 192 | } |
| 193 | |
| 194 | result = 1; |
| 195 | done: |
| 196 | gpr_free(host); |
| 197 | gpr_free(port); |
| 198 | return result; |
| 199 | } |
| 200 | |
| 201 | static int parse_ipv6(grpc_uri *uri, struct sockaddr_storage *addr, int *len) { |
| 202 | const char *host_port = uri->path; |
| 203 | char *host; |
| 204 | char *port; |
| 205 | int port_num; |
| 206 | int result = 0; |
| 207 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; |
| 208 | |
| 209 | if (*host_port == '/') ++host_port; |
| 210 | if (!gpr_split_host_port(host_port, &host, &port)) { |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | memset(in6, 0, sizeof(*in6)); |
| 215 | *len = sizeof(*in6); |
| 216 | in6->sin6_family = AF_INET6; |
| 217 | if (inet_pton(AF_INET6, host, &in6->sin6_addr) == 0) { |
| 218 | gpr_log(GPR_ERROR, "invalid ipv6 address: '%s'", host); |
| 219 | goto done; |
| 220 | } |
| 221 | |
| 222 | if (port != NULL) { |
| 223 | if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 || |
| 224 | port_num > 65535) { |
| 225 | gpr_log(GPR_ERROR, "invalid ipv6 port: '%s'", port); |
| 226 | goto done; |
| 227 | } |
| 228 | in6->sin6_port = htons(port_num); |
| 229 | } else { |
| 230 | gpr_log(GPR_ERROR, "no port given for ipv6 scheme"); |
| 231 | goto done; |
| 232 | } |
| 233 | |
| 234 | result = 1; |
| 235 | done: |
| 236 | gpr_free(host); |
| 237 | gpr_free(port); |
| 238 | return result; |
| 239 | } |
| 240 | |
| 241 | static grpc_resolver *sockaddr_create( |
| 242 | grpc_uri *uri, |
| 243 | grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, |
| 244 | size_t num_subchannels), |
| 245 | grpc_subchannel_factory *subchannel_factory, |
| 246 | int parse(grpc_uri *uri, struct sockaddr_storage *dst, int *len)) { |
| 247 | sockaddr_resolver *r; |
| 248 | |
| 249 | if (0 != strcmp(uri->authority, "")) { |
| 250 | gpr_log(GPR_ERROR, "authority based uri's not supported"); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | r = gpr_malloc(sizeof(sockaddr_resolver)); |
| 255 | memset(r, 0, sizeof(*r)); |
| 256 | if (!parse(uri, &r->addr, &r->addr_len)) { |
| 257 | gpr_free(r); |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | gpr_ref_init(&r->refs, 1); |
| 262 | gpr_mu_init(&r->mu); |
| 263 | grpc_resolver_init(&r->base, &sockaddr_resolver_vtable); |
| 264 | r->subchannel_factory = subchannel_factory; |
| 265 | r->lb_policy_factory = lb_policy_factory; |
| 266 | |
| 267 | grpc_subchannel_factory_ref(subchannel_factory); |
| 268 | return &r->base; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * FACTORY |
| 273 | */ |
| 274 | |
| 275 | static void sockaddr_factory_ref(grpc_resolver_factory *factory) {} |
| 276 | |
| 277 | static void sockaddr_factory_unref(grpc_resolver_factory *factory) {} |
| 278 | |
| 279 | #define DECL_FACTORY(name) \ |
| 280 | static grpc_resolver *name##_factory_create_resolver( \ |
| 281 | grpc_resolver_factory *factory, grpc_uri *uri, \ |
| 282 | grpc_subchannel_factory *subchannel_factory) { \ |
| 283 | return sockaddr_create(uri, grpc_create_pick_first_lb_policy, \ |
| 284 | subchannel_factory, parse_##name); \ |
| 285 | } \ |
| 286 | static const grpc_resolver_factory_vtable name##_factory_vtable = { \ |
| 287 | sockaddr_factory_ref, sockaddr_factory_unref, \ |
| 288 | name##_factory_create_resolver}; \ |
| 289 | static grpc_resolver_factory name##_resolver_factory = { \ |
| 290 | &name##_factory_vtable}; \ |
| 291 | grpc_resolver_factory *grpc_##name##_resolver_factory_create() { \ |
| 292 | return &name##_resolver_factory; \ |
| 293 | } |
| 294 | |
| 295 | #ifdef GPR_POSIX_SOCKET |
| 296 | DECL_FACTORY(unix) |
| 297 | #endif |
| 298 | DECL_FACTORY(ipv4) |
| 299 | DECL_FACTORY(ipv6) |