blob: 5854031c9b6f1ff1217b36f8c6d165f0d7728faa [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
David Klempner78b79922015-02-04 10:18:59 -080034/* FIXME: "posix" files shouldn't be depending on _GNU_SOURCE */
35#ifndef _GNU_SOURCE
36#define _GNU_SOURCE
37#endif
38
Craig Tiller0c0b60c2015-01-21 15:49:28 -080039#include <grpc/support/port_platform.h>
40
Craig Tillera172ad72015-01-21 15:51:47 -080041#ifdef GPR_POSIX_SOCKET
Craig Tiller0c0b60c2015-01-21 15:49:28 -080042
ctiller18b49ab2014-12-09 14:39:16 -080043#include "src/core/iomgr/tcp_server.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044
Craig Tilleraa31da42015-02-17 16:33:35 -080045#include <errno.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046#include <fcntl.h>
Craig Tilleraa31da42015-02-17 16:33:35 -080047#include <limits.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048#include <netinet/in.h>
49#include <netinet/tcp.h>
50#include <stdio.h>
Craig Tilleraa31da42015-02-17 16:33:35 -080051#include <string.h>
52#include <sys/socket.h>
53#include <sys/stat.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054#include <sys/types.h>
Craig Tillerae7fe922015-02-13 23:16:32 -080055#include <sys/un.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056#include <unistd.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057
ctiller58393c22015-01-07 14:03:30 -080058#include "src/core/iomgr/pollset_posix.h"
Craig Tillerae7fe922015-02-13 23:16:32 -080059#include "src/core/iomgr/resolve_address.h"
ctiller18b49ab2014-12-09 14:39:16 -080060#include "src/core/iomgr/sockaddr_utils.h"
61#include "src/core/iomgr/socket_utils_posix.h"
62#include "src/core/iomgr/tcp_posix.h"
Craig Tillerfa275a92015-06-01 13:55:54 -070063#include "src/core/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064#include <grpc/support/alloc.h>
65#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070066#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067#include <grpc/support/sync.h>
68#include <grpc/support/time.h>
69
70#define INIT_PORT_CAP 2
71#define MIN_SAFE_ACCEPT_QUEUE_SIZE 100
72
73static gpr_once s_init_max_accept_queue_size;
74static int s_max_accept_queue_size;
75
76/* one listening port */
77typedef struct {
78 int fd;
ctiller18b49ab2014-12-09 14:39:16 -080079 grpc_fd *emfd;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080 grpc_tcp_server *server;
Craig Tiller772c97b2015-02-17 08:07:34 -080081 union {
82 gpr_uint8 untyped[GRPC_MAX_SOCKADDR_SIZE];
83 struct sockaddr sockaddr;
84 struct sockaddr_un un;
85 } addr;
Craig Tillerae7fe922015-02-13 23:16:32 -080086 int addr_len;
Craig Tiller0fcd53c2015-02-18 15:10:53 -080087 grpc_iomgr_closure read_closure;
Craig Tiller0317b3d2015-06-01 21:57:03 -070088 grpc_iomgr_closure destroyed_closure;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089} server_port;
90
Craig Tilleraa31da42015-02-17 16:33:35 -080091static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) {
92 struct stat st;
93
94 if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
95 unlink(un->sun_path);
96 }
97}
98
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080099/* the overall server */
100struct grpc_tcp_server {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101 grpc_tcp_server_cb cb;
102 void *cb_arg;
103
104 gpr_mu mu;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105
106 /* active port count: how many ports are actually still listening */
Craig Tilleraec96aa2015-04-07 14:32:15 -0700107 size_t active_ports;
108 /* destroyed port count: how many ports are completely destroyed */
109 size_t destroyed_ports;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110
Craig Tiller6174b9a2015-06-18 08:13:05 -0700111 /* is this server shutting down? (boolean) */
Craig Tiller6f051402015-05-13 11:42:42 -0700112 int shutdown;
113
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114 /* all listening ports */
115 server_port *ports;
116 size_t nports;
117 size_t port_capacity;
Craig Tilleraec96aa2015-04-07 14:32:15 -0700118
119 /* shutdown callback */
120 void (*shutdown_complete)(void *);
121 void *shutdown_complete_arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700122
Craig Tiller6174b9a2015-06-18 08:13:05 -0700123 /* all pollsets interested in new connections */
Craig Tiller0189ac02015-05-11 14:59:19 -0700124 grpc_pollset **pollsets;
Craig Tiller6174b9a2015-06-18 08:13:05 -0700125 /* number of pollsets in the pollsets array */
Craig Tiller0189ac02015-05-11 14:59:19 -0700126 size_t pollset_count;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127};
128
Craig Tiller32946d32015-01-15 11:37:30 -0800129grpc_tcp_server *grpc_tcp_server_create(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130 grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server));
131 gpr_mu_init(&s->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132 s->active_ports = 0;
Craig Tilleraec96aa2015-04-07 14:32:15 -0700133 s->destroyed_ports = 0;
Craig Tiller6f051402015-05-13 11:42:42 -0700134 s->shutdown = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135 s->cb = NULL;
136 s->cb_arg = NULL;
137 s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP);
138 s->nports = 0;
139 s->port_capacity = INIT_PORT_CAP;
140 return s;
141}
142
Craig Tilleraec96aa2015-04-07 14:32:15 -0700143static void finish_shutdown(grpc_tcp_server *s) {
144 s->shutdown_complete(s->shutdown_complete_arg);
145
146 gpr_mu_destroy(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700147
148 gpr_free(s->ports);
149 gpr_free(s);
150}
151
152static void destroyed_port(void *server, int success) {
153 grpc_tcp_server *s = server;
154 gpr_mu_lock(&s->mu);
155 s->destroyed_ports++;
156 if (s->destroyed_ports == s->nports) {
157 gpr_mu_unlock(&s->mu);
158 finish_shutdown(s);
159 } else {
160 gpr_mu_unlock(&s->mu);
161 }
162}
163
164static void dont_care_about_shutdown_completion(void *ignored) {}
165
Craig Tiller6174b9a2015-06-18 08:13:05 -0700166/* called when all listening endpoints have been shutdown, so no further
167 events will be received on them - at this point it's safe to destroy
168 things */
Craig Tiller6f051402015-05-13 11:42:42 -0700169static void deactivated_all_ports(grpc_tcp_server *s) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800171
172 /* delete ALL the things */
Craig Tiller6f051402015-05-13 11:42:42 -0700173 gpr_mu_lock(&s->mu);
174
175 if (!s->shutdown) {
176 gpr_mu_unlock(&s->mu);
177 return;
178 }
179
Craig Tilleraec96aa2015-04-07 14:32:15 -0700180 if (s->nports) {
181 for (i = 0; i < s->nports; i++) {
182 server_port *sp = &s->ports[i];
183 if (sp->addr.sockaddr.sa_family == AF_UNIX) {
184 unlink_if_unix_domain_socket(&sp->addr.un);
185 }
Craig Tiller0317b3d2015-06-01 21:57:03 -0700186 sp->destroyed_closure.cb = destroyed_port;
187 sp->destroyed_closure.cb_arg = s;
Craig Tiller4b678bd2015-06-02 16:12:24 -0700188 grpc_fd_orphan(sp->emfd, &sp->destroyed_closure, "tcp_listener_shutdown");
Craig Tillerae7fe922015-02-13 23:16:32 -0800189 }
Craig Tiller954d7a22015-04-13 13:01:50 -0700190 gpr_mu_unlock(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700191 } else {
Craig Tiller954d7a22015-04-13 13:01:50 -0700192 gpr_mu_unlock(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700193 finish_shutdown(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195}
196
Craig Tiller6f051402015-05-13 11:42:42 -0700197void grpc_tcp_server_destroy(
198 grpc_tcp_server *s, void (*shutdown_complete)(void *shutdown_complete_arg),
199 void *shutdown_complete_arg) {
200 size_t i;
201 gpr_mu_lock(&s->mu);
202
203 GPR_ASSERT(!s->shutdown);
204 s->shutdown = 1;
205
206 s->shutdown_complete = shutdown_complete
207 ? shutdown_complete
208 : dont_care_about_shutdown_completion;
209 s->shutdown_complete_arg = shutdown_complete_arg;
210
211 /* shutdown all fd's */
212 if (s->active_ports) {
213 for (i = 0; i < s->nports; i++) {
214 grpc_fd_shutdown(s->ports[i].emfd);
215 }
216 gpr_mu_unlock(&s->mu);
217 } else {
218 gpr_mu_unlock(&s->mu);
219 deactivated_all_ports(s);
220 }
221}
222
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223/* get max listen queue size on linux */
Craig Tiller32946d32015-01-15 11:37:30 -0800224static void init_max_accept_queue_size(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225 int n = SOMAXCONN;
226 char buf[64];
227 FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r");
228 if (fp == NULL) {
229 /* 2.4 kernel. */
230 s_max_accept_queue_size = SOMAXCONN;
231 return;
232 }
233 if (fgets(buf, sizeof buf, fp)) {
234 char *end;
235 long i = strtol(buf, &end, 10);
236 if (i > 0 && i <= INT_MAX && end && *end == 0) {
237 n = i;
238 }
239 }
240 fclose(fp);
241 s_max_accept_queue_size = n;
242
243 if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) {
244 gpr_log(GPR_INFO,
245 "Suspiciously small accept queue (%d) will probably lead to "
246 "connection drops",
247 s_max_accept_queue_size);
248 }
249}
250
Craig Tiller32946d32015-01-15 11:37:30 -0800251static int get_max_accept_queue_size(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800252 gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size);
253 return s_max_accept_queue_size;
254}
255
nnoble0c475f02014-12-05 15:37:39 -0800256/* Prepare a recently-created socket for listening. */
257static int prepare_socket(int fd, const struct sockaddr *addr, int addr_len) {
ctiller570d1f42015-01-12 16:29:52 -0800258 struct sockaddr_storage sockname_temp;
259 socklen_t sockname_len;
260
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800261 if (fd < 0) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800262 goto error;
263 }
264
265 if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) ||
Craig Tillerd209ed02015-02-13 23:18:15 -0800266 (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) ||
Craig Tiller2da02962015-05-06 16:14:25 -0700267 !grpc_set_socket_reuse_addr(fd, 1))) ||
268 !grpc_set_socket_no_sigpipe_if_possible(fd)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800269 gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
270 strerror(errno));
271 goto error;
272 }
273
nnoble0c475f02014-12-05 15:37:39 -0800274 if (bind(fd, addr, addr_len) < 0) {
275 char *addr_str;
276 grpc_sockaddr_to_string(&addr_str, addr, 0);
277 gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno));
278 gpr_free(addr_str);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800279 goto error;
280 }
281
282 if (listen(fd, get_max_accept_queue_size()) < 0) {
283 gpr_log(GPR_ERROR, "listen: %s", strerror(errno));
284 goto error;
285 }
286
ctiller570d1f42015-01-12 16:29:52 -0800287 sockname_len = sizeof(sockname_temp);
288 if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) {
289 goto error;
290 }
291
292 return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800293
294error:
295 if (fd >= 0) {
296 close(fd);
297 }
ctiller570d1f42015-01-12 16:29:52 -0800298 return -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800299}
300
301/* event manager callback when reads are ready */
ctiller58393c22015-01-07 14:03:30 -0800302static void on_read(void *arg, int success) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303 server_port *sp = arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700304 grpc_fd *fdobj;
305 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800306
ctiller58393c22015-01-07 14:03:30 -0800307 if (!success) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800308 goto error;
309 }
310
311 /* loop until accept4 returns EAGAIN, and then re-arm notification */
312 for (;;) {
313 struct sockaddr_storage addr;
314 socklen_t addrlen = sizeof(addr);
Craig Tillerfa275a92015-06-01 13:55:54 -0700315 char *addr_str;
316 char *name;
nnoble0c475f02014-12-05 15:37:39 -0800317 /* Note: If we ever decide to return this address to the user, remember to
318 strip off the ::ffff:0.0.0.0/96 prefix first. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800319 int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1);
320 if (fd < 0) {
321 switch (errno) {
322 case EINTR:
323 continue;
324 case EAGAIN:
Craig Tiller0fcd53c2015-02-18 15:10:53 -0800325 grpc_fd_notify_on_read(sp->emfd, &sp->read_closure);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800326 return;
327 default:
328 gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno));
329 goto error;
330 }
331 }
332
Craig Tiller2da02962015-05-06 16:14:25 -0700333 grpc_set_socket_no_sigpipe_if_possible(fd);
334
Craig Tillerfa275a92015-06-01 13:55:54 -0700335 grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1);
336 gpr_asprintf(&name, "tcp-server-connection:%s", addr_str);
337
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700338 fdobj = grpc_fd_create(fd, name);
Craig Tiller0189ac02015-05-11 14:59:19 -0700339 /* TODO(ctiller): revise this when we have server-side sharding
340 of channels -- we certainly should not be automatically adding every
341 incoming channel to every pollset owned by the server */
342 for (i = 0; i < sp->server->pollset_count; i++) {
343 grpc_pollset_add_fd(sp->server->pollsets[i], fdobj);
344 }
Craig Tiller8674cb12015-06-05 07:09:25 -0700345 sp->server->cb(sp->server->cb_arg,
346 grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE));
Craig Tillerfa275a92015-06-01 13:55:54 -0700347
Craig Tillerfa275a92015-06-01 13:55:54 -0700348 gpr_free(name);
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700349 gpr_free(addr_str);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800350 }
351
352 abort();
353
354error:
355 gpr_mu_lock(&sp->server->mu);
356 if (0 == --sp->server->active_ports) {
Craig Tiller6f051402015-05-13 11:42:42 -0700357 gpr_mu_unlock(&sp->server->mu);
358 deactivated_all_ports(sp->server);
359 } else {
360 gpr_mu_unlock(&sp->server->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800361 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800362}
363
nnoble0c475f02014-12-05 15:37:39 -0800364static int add_socket_to_server(grpc_tcp_server *s, int fd,
365 const struct sockaddr *addr, int addr_len) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800366 server_port *sp;
ctiller570d1f42015-01-12 16:29:52 -0800367 int port;
Craig Tillerfa275a92015-06-01 13:55:54 -0700368 char *addr_str;
369 char *name;
nnoble0c475f02014-12-05 15:37:39 -0800370
ctiller570d1f42015-01-12 16:29:52 -0800371 port = prepare_socket(fd, addr, addr_len);
372 if (port >= 0) {
Craig Tillerfa275a92015-06-01 13:55:54 -0700373 grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1);
374 gpr_asprintf(&name, "tcp-server-listener:%s", addr_str);
ctiller570d1f42015-01-12 16:29:52 -0800375 gpr_mu_lock(&s->mu);
376 GPR_ASSERT(!s->cb && "must add ports before starting server");
377 /* append it to the list under a lock */
378 if (s->nports == s->port_capacity) {
379 s->port_capacity *= 2;
Yang Gao5fd0d292015-01-26 00:19:48 -0800380 s->ports = gpr_realloc(s->ports, sizeof(server_port) * s->port_capacity);
ctiller570d1f42015-01-12 16:29:52 -0800381 }
382 sp = &s->ports[s->nports++];
383 sp->server = s;
384 sp->fd = fd;
Craig Tillerfa275a92015-06-01 13:55:54 -0700385 sp->emfd = grpc_fd_create(fd, name);
Craig Tiller772c97b2015-02-17 08:07:34 -0800386 memcpy(sp->addr.untyped, addr, addr_len);
Craig Tillerae7fe922015-02-13 23:16:32 -0800387 sp->addr_len = addr_len;
ctiller570d1f42015-01-12 16:29:52 -0800388 GPR_ASSERT(sp->emfd);
389 gpr_mu_unlock(&s->mu);
Craig Tillerfa275a92015-06-01 13:55:54 -0700390 gpr_free(addr_str);
391 gpr_free(name);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800392 }
393
ctiller570d1f42015-01-12 16:29:52 -0800394 return port;
nnoble0c475f02014-12-05 15:37:39 -0800395}
396
Craig Tillera172ad72015-01-21 15:51:47 -0800397int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr,
nnoble0c475f02014-12-05 15:37:39 -0800398 int addr_len) {
ctiller570d1f42015-01-12 16:29:52 -0800399 int allocated_port1 = -1;
400 int allocated_port2 = -1;
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100401 unsigned i;
nnoble0c475f02014-12-05 15:37:39 -0800402 int fd;
403 grpc_dualstack_mode dsmode;
404 struct sockaddr_in6 addr6_v4mapped;
405 struct sockaddr_in wild4;
406 struct sockaddr_in6 wild6;
407 struct sockaddr_in addr4_copy;
ctiller570d1f42015-01-12 16:29:52 -0800408 struct sockaddr *allocated_addr = NULL;
409 struct sockaddr_storage sockname_temp;
410 socklen_t sockname_len;
nnoble0c475f02014-12-05 15:37:39 -0800411 int port;
412
Craig Tilleraa31da42015-02-17 16:33:35 -0800413 if (((struct sockaddr *)addr)->sa_family == AF_UNIX) {
414 unlink_if_unix_domain_socket(addr);
415 }
416
ctiller570d1f42015-01-12 16:29:52 -0800417 /* Check if this is a wildcard port, and if so, try to keep the port the same
418 as some previously created listener. */
419 if (grpc_sockaddr_get_port(addr) == 0) {
420 for (i = 0; i < s->nports; i++) {
421 sockname_len = sizeof(sockname_temp);
422 if (0 == getsockname(s->ports[i].fd, (struct sockaddr *)&sockname_temp,
423 &sockname_len)) {
424 port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
425 if (port > 0) {
426 allocated_addr = malloc(addr_len);
427 memcpy(allocated_addr, addr, addr_len);
428 grpc_sockaddr_set_port(allocated_addr, port);
429 addr = allocated_addr;
430 break;
431 }
432 }
433 }
434 }
435
nnoble0c475f02014-12-05 15:37:39 -0800436 if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
437 addr = (const struct sockaddr *)&addr6_v4mapped;
438 addr_len = sizeof(addr6_v4mapped);
439 }
440
441 /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
442 if (grpc_sockaddr_is_wildcard(addr, &port)) {
443 grpc_sockaddr_make_wildcards(port, &wild4, &wild6);
444
445 /* Try listening on IPv6 first. */
446 addr = (struct sockaddr *)&wild6;
447 addr_len = sizeof(wild6);
448 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
ctiller570d1f42015-01-12 16:29:52 -0800449 allocated_port1 = add_socket_to_server(s, fd, addr, addr_len);
nnoble0c475f02014-12-05 15:37:39 -0800450 if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) {
ctiller570d1f42015-01-12 16:29:52 -0800451 goto done;
nnoble0c475f02014-12-05 15:37:39 -0800452 }
453
454 /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
ctiller570d1f42015-01-12 16:29:52 -0800455 if (port == 0 && allocated_port1 > 0) {
456 grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1);
457 }
nnoble0c475f02014-12-05 15:37:39 -0800458 addr = (struct sockaddr *)&wild4;
459 addr_len = sizeof(wild4);
460 }
461
462 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
463 if (fd < 0) {
464 gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno));
465 }
466 if (dsmode == GRPC_DSMODE_IPV4 &&
467 grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) {
468 addr = (struct sockaddr *)&addr4_copy;
469 addr_len = sizeof(addr4_copy);
470 }
ctiller570d1f42015-01-12 16:29:52 -0800471 allocated_port2 = add_socket_to_server(s, fd, addr, addr_len);
472
473done:
474 gpr_free(allocated_addr);
475 return allocated_port1 >= 0 ? allocated_port1 : allocated_port2;
nnoble0c475f02014-12-05 15:37:39 -0800476}
477
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100478int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned index) {
479 return (index < s->nports) ? s->ports[index].fd : -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800480}
481
Craig Tiller20bc56d2015-02-12 09:02:56 -0800482void grpc_tcp_server_start(grpc_tcp_server *s, grpc_pollset **pollsets,
483 size_t pollset_count, grpc_tcp_server_cb cb,
484 void *cb_arg) {
485 size_t i, j;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800486 GPR_ASSERT(cb);
487 gpr_mu_lock(&s->mu);
488 GPR_ASSERT(!s->cb);
489 GPR_ASSERT(s->active_ports == 0);
490 s->cb = cb;
491 s->cb_arg = cb_arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700492 s->pollsets = pollsets;
493 s->pollset_count = pollset_count;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800494 for (i = 0; i < s->nports; i++) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800495 for (j = 0; j < pollset_count; j++) {
496 grpc_pollset_add_fd(pollsets[j], s->ports[i].emfd);
ctiller58393c22015-01-07 14:03:30 -0800497 }
Craig Tiller0fcd53c2015-02-18 15:10:53 -0800498 s->ports[i].read_closure.cb = on_read;
499 s->ports[i].read_closure.cb_arg = &s->ports[i];
500 grpc_fd_notify_on_read(s->ports[i].emfd, &s->ports[i].read_closure);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800501 s->active_ports++;
502 }
503 gpr_mu_unlock(&s->mu);
504}
Craig Tiller0c0b60c2015-01-21 15:49:28 -0800505
Craig Tiller190d3602015-02-18 09:23:38 -0800506#endif