blob: bcbd0afe6b93559a8eef2115f70efd51e4d1da83 [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 Tillerf96dfc32015-09-10 14:43:18 -070086 size_t 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);
Craig Tiller0613e582015-07-30 11:55:43 -0700145 s->shutdown_complete = NULL;
Craig Tilleraec96aa2015-04-07 14:32:15 -0700146
147 gpr_mu_destroy(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700148
149 gpr_free(s->ports);
150 gpr_free(s);
151}
152
153static void destroyed_port(void *server, int success) {
154 grpc_tcp_server *s = server;
155 gpr_mu_lock(&s->mu);
156 s->destroyed_ports++;
157 if (s->destroyed_ports == s->nports) {
158 gpr_mu_unlock(&s->mu);
159 finish_shutdown(s);
160 } else {
Craig Tiller0613e582015-07-30 11:55:43 -0700161 GPR_ASSERT(s->destroyed_ports < s->nports);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700162 gpr_mu_unlock(&s->mu);
163 }
164}
165
166static void dont_care_about_shutdown_completion(void *ignored) {}
167
Craig Tiller6174b9a2015-06-18 08:13:05 -0700168/* called when all listening endpoints have been shutdown, so no further
169 events will be received on them - at this point it's safe to destroy
170 things */
Craig Tiller6f051402015-05-13 11:42:42 -0700171static void deactivated_all_ports(grpc_tcp_server *s) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800172 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800173
174 /* delete ALL the things */
Craig Tiller6f051402015-05-13 11:42:42 -0700175 gpr_mu_lock(&s->mu);
176
177 if (!s->shutdown) {
178 gpr_mu_unlock(&s->mu);
179 return;
180 }
181
Craig Tilleraec96aa2015-04-07 14:32:15 -0700182 if (s->nports) {
183 for (i = 0; i < s->nports; i++) {
184 server_port *sp = &s->ports[i];
185 if (sp->addr.sockaddr.sa_family == AF_UNIX) {
186 unlink_if_unix_domain_socket(&sp->addr.un);
187 }
Craig Tiller0317b3d2015-06-01 21:57:03 -0700188 sp->destroyed_closure.cb = destroyed_port;
189 sp->destroyed_closure.cb_arg = s;
Craig Tiller4b678bd2015-06-02 16:12:24 -0700190 grpc_fd_orphan(sp->emfd, &sp->destroyed_closure, "tcp_listener_shutdown");
Craig Tillerae7fe922015-02-13 23:16:32 -0800191 }
Craig Tiller954d7a22015-04-13 13:01:50 -0700192 gpr_mu_unlock(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700193 } else {
Craig Tiller954d7a22015-04-13 13:01:50 -0700194 gpr_mu_unlock(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700195 finish_shutdown(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800196 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197}
198
Craig Tiller6f051402015-05-13 11:42:42 -0700199void grpc_tcp_server_destroy(
200 grpc_tcp_server *s, void (*shutdown_complete)(void *shutdown_complete_arg),
201 void *shutdown_complete_arg) {
202 size_t i;
203 gpr_mu_lock(&s->mu);
204
205 GPR_ASSERT(!s->shutdown);
206 s->shutdown = 1;
207
208 s->shutdown_complete = shutdown_complete
209 ? shutdown_complete
210 : dont_care_about_shutdown_completion;
211 s->shutdown_complete_arg = shutdown_complete_arg;
212
213 /* shutdown all fd's */
214 if (s->active_ports) {
215 for (i = 0; i < s->nports; i++) {
216 grpc_fd_shutdown(s->ports[i].emfd);
217 }
218 gpr_mu_unlock(&s->mu);
219 } else {
220 gpr_mu_unlock(&s->mu);
221 deactivated_all_ports(s);
222 }
223}
224
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225/* get max listen queue size on linux */
Craig Tiller32946d32015-01-15 11:37:30 -0800226static void init_max_accept_queue_size(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800227 int n = SOMAXCONN;
228 char buf[64];
229 FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r");
230 if (fp == NULL) {
231 /* 2.4 kernel. */
232 s_max_accept_queue_size = SOMAXCONN;
233 return;
234 }
235 if (fgets(buf, sizeof buf, fp)) {
236 char *end;
237 long i = strtol(buf, &end, 10);
238 if (i > 0 && i <= INT_MAX && end && *end == 0) {
Craig Tillerf96dfc32015-09-10 14:43:18 -0700239 n = (int)i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800240 }
241 }
242 fclose(fp);
243 s_max_accept_queue_size = n;
244
245 if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) {
246 gpr_log(GPR_INFO,
247 "Suspiciously small accept queue (%d) will probably lead to "
248 "connection drops",
249 s_max_accept_queue_size);
250 }
251}
252
Craig Tiller32946d32015-01-15 11:37:30 -0800253static int get_max_accept_queue_size(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800254 gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size);
255 return s_max_accept_queue_size;
256}
257
nnoble0c475f02014-12-05 15:37:39 -0800258/* Prepare a recently-created socket for listening. */
Craig Tiller32ca48c2015-09-10 11:47:15 -0700259static int prepare_socket(int fd, const struct sockaddr *addr,
260 size_t addr_len) {
ctiller570d1f42015-01-12 16:29:52 -0800261 struct sockaddr_storage sockname_temp;
262 socklen_t sockname_len;
263
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800264 if (fd < 0) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800265 goto error;
266 }
267
268 if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) ||
Craig Tillerd209ed02015-02-13 23:18:15 -0800269 (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) ||
Craig Tiller2da02962015-05-06 16:14:25 -0700270 !grpc_set_socket_reuse_addr(fd, 1))) ||
271 !grpc_set_socket_no_sigpipe_if_possible(fd)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800272 gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
273 strerror(errno));
274 goto error;
275 }
276
Craig Tillerf96dfc32015-09-10 14:43:18 -0700277 GPR_ASSERT(addr_len < ~(socklen_t)0);
278 if (bind(fd, addr, (socklen_t)addr_len) < 0) {
nnoble0c475f02014-12-05 15:37:39 -0800279 char *addr_str;
280 grpc_sockaddr_to_string(&addr_str, addr, 0);
281 gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno));
282 gpr_free(addr_str);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800283 goto error;
284 }
285
286 if (listen(fd, get_max_accept_queue_size()) < 0) {
287 gpr_log(GPR_ERROR, "listen: %s", strerror(errno));
288 goto error;
289 }
290
ctiller570d1f42015-01-12 16:29:52 -0800291 sockname_len = sizeof(sockname_temp);
292 if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) {
293 goto error;
294 }
295
296 return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800297
298error:
299 if (fd >= 0) {
300 close(fd);
301 }
ctiller570d1f42015-01-12 16:29:52 -0800302 return -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303}
304
305/* event manager callback when reads are ready */
ctiller58393c22015-01-07 14:03:30 -0800306static void on_read(void *arg, int success) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800307 server_port *sp = arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700308 grpc_fd *fdobj;
309 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800310
ctiller58393c22015-01-07 14:03:30 -0800311 if (!success) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800312 goto error;
313 }
314
315 /* loop until accept4 returns EAGAIN, and then re-arm notification */
316 for (;;) {
317 struct sockaddr_storage addr;
318 socklen_t addrlen = sizeof(addr);
Craig Tillerfa275a92015-06-01 13:55:54 -0700319 char *addr_str;
320 char *name;
nnoble0c475f02014-12-05 15:37:39 -0800321 /* Note: If we ever decide to return this address to the user, remember to
322 strip off the ::ffff:0.0.0.0/96 prefix first. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800323 int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1);
324 if (fd < 0) {
325 switch (errno) {
326 case EINTR:
327 continue;
328 case EAGAIN:
Craig Tiller0fcd53c2015-02-18 15:10:53 -0800329 grpc_fd_notify_on_read(sp->emfd, &sp->read_closure);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800330 return;
331 default:
332 gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno));
333 goto error;
334 }
335 }
336
Craig Tiller2da02962015-05-06 16:14:25 -0700337 grpc_set_socket_no_sigpipe_if_possible(fd);
338
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700339 addr_str = grpc_sockaddr_to_uri((struct sockaddr *)&addr);
Craig Tillerfa275a92015-06-01 13:55:54 -0700340 gpr_asprintf(&name, "tcp-server-connection:%s", addr_str);
341
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700342 fdobj = grpc_fd_create(fd, name);
Craig Tiller0189ac02015-05-11 14:59:19 -0700343 /* TODO(ctiller): revise this when we have server-side sharding
344 of channels -- we certainly should not be automatically adding every
345 incoming channel to every pollset owned by the server */
346 for (i = 0; i < sp->server->pollset_count; i++) {
347 grpc_pollset_add_fd(sp->server->pollsets[i], fdobj);
348 }
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700349 sp->server->cb(
350 sp->server->cb_arg,
351 grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str));
Craig Tillerfa275a92015-06-01 13:55:54 -0700352
Craig Tillerfa275a92015-06-01 13:55:54 -0700353 gpr_free(name);
Craig Tiller8e0b08a2015-06-01 17:04:17 -0700354 gpr_free(addr_str);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800355 }
356
357 abort();
358
359error:
360 gpr_mu_lock(&sp->server->mu);
361 if (0 == --sp->server->active_ports) {
Craig Tiller6f051402015-05-13 11:42:42 -0700362 gpr_mu_unlock(&sp->server->mu);
363 deactivated_all_ports(sp->server);
364 } else {
365 gpr_mu_unlock(&sp->server->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800366 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800367}
368
nnoble0c475f02014-12-05 15:37:39 -0800369static int add_socket_to_server(grpc_tcp_server *s, int fd,
Craig Tiller32ca48c2015-09-10 11:47:15 -0700370 const struct sockaddr *addr, size_t addr_len) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800371 server_port *sp;
ctiller570d1f42015-01-12 16:29:52 -0800372 int port;
Craig Tillerfa275a92015-06-01 13:55:54 -0700373 char *addr_str;
374 char *name;
nnoble0c475f02014-12-05 15:37:39 -0800375
ctiller570d1f42015-01-12 16:29:52 -0800376 port = prepare_socket(fd, addr, addr_len);
377 if (port >= 0) {
Craig Tillerfa275a92015-06-01 13:55:54 -0700378 grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1);
379 gpr_asprintf(&name, "tcp-server-listener:%s", addr_str);
ctiller570d1f42015-01-12 16:29:52 -0800380 gpr_mu_lock(&s->mu);
381 GPR_ASSERT(!s->cb && "must add ports before starting server");
382 /* append it to the list under a lock */
383 if (s->nports == s->port_capacity) {
384 s->port_capacity *= 2;
Yang Gao5fd0d292015-01-26 00:19:48 -0800385 s->ports = gpr_realloc(s->ports, sizeof(server_port) * s->port_capacity);
ctiller570d1f42015-01-12 16:29:52 -0800386 }
387 sp = &s->ports[s->nports++];
388 sp->server = s;
389 sp->fd = fd;
Craig Tillerfa275a92015-06-01 13:55:54 -0700390 sp->emfd = grpc_fd_create(fd, name);
Craig Tiller772c97b2015-02-17 08:07:34 -0800391 memcpy(sp->addr.untyped, addr, addr_len);
Craig Tillerae7fe922015-02-13 23:16:32 -0800392 sp->addr_len = addr_len;
ctiller570d1f42015-01-12 16:29:52 -0800393 GPR_ASSERT(sp->emfd);
394 gpr_mu_unlock(&s->mu);
Craig Tillerfa275a92015-06-01 13:55:54 -0700395 gpr_free(addr_str);
396 gpr_free(name);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800397 }
398
ctiller570d1f42015-01-12 16:29:52 -0800399 return port;
nnoble0c475f02014-12-05 15:37:39 -0800400}
401
Craig Tillera172ad72015-01-21 15:51:47 -0800402int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr,
Craig Tiller32ca48c2015-09-10 11:47:15 -0700403 size_t addr_len) {
ctiller570d1f42015-01-12 16:29:52 -0800404 int allocated_port1 = -1;
405 int allocated_port2 = -1;
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100406 unsigned i;
nnoble0c475f02014-12-05 15:37:39 -0800407 int fd;
408 grpc_dualstack_mode dsmode;
409 struct sockaddr_in6 addr6_v4mapped;
410 struct sockaddr_in wild4;
411 struct sockaddr_in6 wild6;
412 struct sockaddr_in addr4_copy;
ctiller570d1f42015-01-12 16:29:52 -0800413 struct sockaddr *allocated_addr = NULL;
414 struct sockaddr_storage sockname_temp;
415 socklen_t sockname_len;
nnoble0c475f02014-12-05 15:37:39 -0800416 int port;
417
Craig Tilleraa31da42015-02-17 16:33:35 -0800418 if (((struct sockaddr *)addr)->sa_family == AF_UNIX) {
419 unlink_if_unix_domain_socket(addr);
420 }
421
ctiller570d1f42015-01-12 16:29:52 -0800422 /* Check if this is a wildcard port, and if so, try to keep the port the same
423 as some previously created listener. */
424 if (grpc_sockaddr_get_port(addr) == 0) {
425 for (i = 0; i < s->nports; i++) {
426 sockname_len = sizeof(sockname_temp);
427 if (0 == getsockname(s->ports[i].fd, (struct sockaddr *)&sockname_temp,
428 &sockname_len)) {
429 port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
430 if (port > 0) {
431 allocated_addr = malloc(addr_len);
432 memcpy(allocated_addr, addr, addr_len);
433 grpc_sockaddr_set_port(allocated_addr, port);
434 addr = allocated_addr;
435 break;
436 }
437 }
438 }
439 }
440
nnoble0c475f02014-12-05 15:37:39 -0800441 if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
442 addr = (const struct sockaddr *)&addr6_v4mapped;
443 addr_len = sizeof(addr6_v4mapped);
444 }
445
446 /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
447 if (grpc_sockaddr_is_wildcard(addr, &port)) {
448 grpc_sockaddr_make_wildcards(port, &wild4, &wild6);
449
450 /* Try listening on IPv6 first. */
451 addr = (struct sockaddr *)&wild6;
452 addr_len = sizeof(wild6);
453 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
ctiller570d1f42015-01-12 16:29:52 -0800454 allocated_port1 = add_socket_to_server(s, fd, addr, addr_len);
nnoble0c475f02014-12-05 15:37:39 -0800455 if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) {
ctiller570d1f42015-01-12 16:29:52 -0800456 goto done;
nnoble0c475f02014-12-05 15:37:39 -0800457 }
458
459 /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
ctiller570d1f42015-01-12 16:29:52 -0800460 if (port == 0 && allocated_port1 > 0) {
461 grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1);
462 }
nnoble0c475f02014-12-05 15:37:39 -0800463 addr = (struct sockaddr *)&wild4;
464 addr_len = sizeof(wild4);
465 }
466
467 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
468 if (fd < 0) {
469 gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno));
470 }
471 if (dsmode == GRPC_DSMODE_IPV4 &&
472 grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) {
473 addr = (struct sockaddr *)&addr4_copy;
474 addr_len = sizeof(addr4_copy);
475 }
ctiller570d1f42015-01-12 16:29:52 -0800476 allocated_port2 = add_socket_to_server(s, fd, addr, addr_len);
477
478done:
479 gpr_free(allocated_addr);
480 return allocated_port1 >= 0 ? allocated_port1 : allocated_port2;
nnoble0c475f02014-12-05 15:37:39 -0800481}
482
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100483int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned index) {
484 return (index < s->nports) ? s->ports[index].fd : -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800485}
486
Craig Tiller20bc56d2015-02-12 09:02:56 -0800487void grpc_tcp_server_start(grpc_tcp_server *s, grpc_pollset **pollsets,
488 size_t pollset_count, grpc_tcp_server_cb cb,
489 void *cb_arg) {
490 size_t i, j;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800491 GPR_ASSERT(cb);
492 gpr_mu_lock(&s->mu);
493 GPR_ASSERT(!s->cb);
494 GPR_ASSERT(s->active_ports == 0);
495 s->cb = cb;
496 s->cb_arg = cb_arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700497 s->pollsets = pollsets;
498 s->pollset_count = pollset_count;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800499 for (i = 0; i < s->nports; i++) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800500 for (j = 0; j < pollset_count; j++) {
501 grpc_pollset_add_fd(pollsets[j], s->ports[i].emfd);
ctiller58393c22015-01-07 14:03:30 -0800502 }
Craig Tiller0fcd53c2015-02-18 15:10:53 -0800503 s->ports[i].read_closure.cb = on_read;
504 s->ports[i].read_closure.cb_arg = &s->ports[i];
505 grpc_fd_notify_on_read(s->ports[i].emfd, &s->ports[i].read_closure);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800506 s->active_ports++;
507 }
508 gpr_mu_unlock(&s->mu);
509}
Craig Tiller0c0b60c2015-01-21 15:49:28 -0800510
Craig Tiller190d3602015-02-18 09:23:38 -0800511#endif