Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 | #define _POSIX_SOURCE |
| 35 | |
Nicolas "Pixel" Noble | d5a9985 | 2015-01-24 01:27:48 -0800 | [diff] [blame^] | 36 | #include "src/core/iomgr/sockaddr.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 37 | #include "src/core/iomgr/resolve_address.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
| 39 | #include <sys/types.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <string.h> |
| 41 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 42 | #include "src/core/iomgr/iomgr_internal.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 43 | #include "src/core/iomgr/sockaddr_utils.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 44 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <grpc/support/log.h> |
| 47 | #include <grpc/support/thd.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 48 | #include <grpc/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | |
| 50 | typedef struct { |
| 51 | char *name; |
| 52 | char *default_port; |
| 53 | grpc_resolve_cb cb; |
| 54 | void *arg; |
| 55 | } request; |
| 56 | |
| 57 | static void split_host_port(const char *name, char **host, char **port) { |
| 58 | const char *host_start; |
| 59 | size_t host_len; |
| 60 | const char *port_start; |
| 61 | |
| 62 | *host = NULL; |
| 63 | *port = NULL; |
| 64 | |
| 65 | if (name[0] == '[') { |
| 66 | /* Parse a bracketed host, typically an IPv6 literal. */ |
| 67 | const char *rbracket = strchr(name, ']'); |
| 68 | if (rbracket == NULL) { |
| 69 | /* Unmatched [ */ |
| 70 | return; |
| 71 | } |
| 72 | if (rbracket[1] == '\0') { |
| 73 | /* ]<end> */ |
| 74 | port_start = NULL; |
| 75 | } else if (rbracket[1] == ':') { |
| 76 | /* ]:<port?> */ |
| 77 | port_start = rbracket + 2; |
| 78 | } else { |
| 79 | /* ]<invalid> */ |
| 80 | return; |
| 81 | } |
| 82 | host_start = name + 1; |
| 83 | host_len = rbracket - host_start; |
| 84 | if (memchr(host_start, ':', host_len) == NULL) { |
| 85 | /* Require all bracketed hosts to contain a colon, because a hostname or |
| 86 | IPv4 address should never use brackets. */ |
| 87 | return; |
| 88 | } |
| 89 | } else { |
| 90 | const char *colon = strchr(name, ':'); |
| 91 | if (colon != NULL && strchr(colon + 1, ':') == NULL) { |
| 92 | /* Exactly 1 colon. Split into host:port. */ |
| 93 | host_start = name; |
| 94 | host_len = colon - name; |
| 95 | port_start = colon + 1; |
| 96 | } else { |
| 97 | /* 0 or 2+ colons. Bare hostname or IPv6 litearal. */ |
| 98 | host_start = name; |
| 99 | host_len = strlen(name); |
| 100 | port_start = NULL; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* Allocate return values. */ |
| 105 | *host = gpr_malloc(host_len + 1); |
| 106 | memcpy(*host, host_start, host_len); |
| 107 | (*host)[host_len] = '\0'; |
| 108 | |
| 109 | if (port_start != NULL) { |
| 110 | *port = gpr_strdup(port_start); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | grpc_resolved_addresses *grpc_blocking_resolve_address( |
| 115 | const char *name, const char *default_port) { |
| 116 | struct addrinfo hints; |
| 117 | struct addrinfo *result = NULL, *resp; |
| 118 | char *host; |
| 119 | char *port; |
| 120 | int s; |
| 121 | size_t i; |
| 122 | grpc_resolved_addresses *addrs = NULL; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 123 | const gpr_timespec start_time = gpr_now(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 124 | |
| 125 | /* parse name, splitting it into host and port parts */ |
| 126 | split_host_port(name, &host, &port); |
| 127 | if (host == NULL) { |
| 128 | gpr_log(GPR_ERROR, "unparseable host:port: '%s'", name); |
| 129 | goto done; |
| 130 | } |
| 131 | if (port == NULL) { |
| 132 | if (default_port == NULL) { |
| 133 | gpr_log(GPR_ERROR, "no port in name '%s'", name); |
| 134 | goto done; |
| 135 | } |
| 136 | port = gpr_strdup(default_port); |
| 137 | } |
| 138 | |
| 139 | /* Call getaddrinfo */ |
| 140 | memset(&hints, 0, sizeof(hints)); |
| 141 | hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */ |
| 142 | hints.ai_socktype = SOCK_STREAM; /* stream socket */ |
| 143 | hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */ |
| 144 | |
| 145 | s = getaddrinfo(host, port, &hints, &result); |
| 146 | if (s != 0) { |
| 147 | gpr_log(GPR_ERROR, "getaddrinfo: %s", gai_strerror(s)); |
| 148 | goto done; |
| 149 | } |
| 150 | |
| 151 | /* Success path: set addrs non-NULL, fill it in */ |
| 152 | addrs = gpr_malloc(sizeof(grpc_resolved_addresses)); |
| 153 | addrs->naddrs = 0; |
| 154 | for (resp = result; resp != NULL; resp = resp->ai_next) { |
| 155 | addrs->naddrs++; |
| 156 | } |
| 157 | addrs->addrs = gpr_malloc(sizeof(grpc_resolved_address) * addrs->naddrs); |
| 158 | i = 0; |
| 159 | for (resp = result; resp != NULL; resp = resp->ai_next) { |
| 160 | memcpy(&addrs->addrs[i].addr, resp->ai_addr, resp->ai_addrlen); |
| 161 | addrs->addrs[i].len = resp->ai_addrlen; |
| 162 | i++; |
| 163 | } |
| 164 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 165 | /* Temporary logging, to help identify flakiness in dualstack_socket_test. */ |
| 166 | { |
| 167 | const gpr_timespec delay = gpr_time_sub(gpr_now(), start_time); |
| 168 | const int delay_ms = |
| 169 | delay.tv_sec * GPR_MS_PER_SEC + delay.tv_nsec / GPR_NS_PER_MS; |
| 170 | gpr_log(GPR_INFO, "logspam: getaddrinfo(%s, %s) resolved %d addrs in %dms:", |
| 171 | host, port, addrs->naddrs, delay_ms); |
| 172 | for (i = 0; i < addrs->naddrs; i++) { |
| 173 | char *buf; |
| 174 | grpc_sockaddr_to_string(&buf, (struct sockaddr *)&addrs->addrs[i].addr, |
| 175 | 0); |
| 176 | gpr_log(GPR_INFO, "logspam: [%d] %s", i, buf); |
| 177 | gpr_free(buf); |
| 178 | } |
| 179 | } |
| 180 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | done: |
| 182 | gpr_free(host); |
| 183 | gpr_free(port); |
| 184 | if (result) { |
| 185 | freeaddrinfo(result); |
| 186 | } |
| 187 | return addrs; |
| 188 | } |
| 189 | |
| 190 | /* Thread function to asynch-ify grpc_blocking_resolve_address */ |
| 191 | static void do_request(void *rp) { |
| 192 | request *r = rp; |
ctiller | ccd27fd | 2014-12-11 09:12:02 -0800 | [diff] [blame] | 193 | grpc_resolved_addresses *resolved = |
| 194 | grpc_blocking_resolve_address(r->name, r->default_port); |
| 195 | void *arg = r->arg; |
| 196 | grpc_resolve_cb cb = r->cb; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 197 | gpr_free(r->name); |
| 198 | gpr_free(r->default_port); |
| 199 | gpr_free(r); |
ctiller | ccd27fd | 2014-12-11 09:12:02 -0800 | [diff] [blame] | 200 | cb(arg, resolved); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 201 | grpc_iomgr_unref(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void grpc_resolved_addresses_destroy(grpc_resolved_addresses *addrs) { |
| 205 | gpr_free(addrs->addrs); |
| 206 | gpr_free(addrs); |
| 207 | } |
| 208 | |
| 209 | void grpc_resolve_address(const char *name, const char *default_port, |
| 210 | grpc_resolve_cb cb, void *arg) { |
| 211 | request *r = gpr_malloc(sizeof(request)); |
| 212 | gpr_thd_id id; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 213 | grpc_iomgr_ref(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 214 | r->name = gpr_strdup(name); |
| 215 | r->default_port = gpr_strdup(default_port); |
| 216 | r->cb = cb; |
| 217 | r->arg = arg; |
| 218 | gpr_thd_new(&id, do_request, r, NULL); |
| 219 | } |