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