blob: 5daf406c99d6677fe76a86b67735f8f132e3cb01 [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 */
Nicolas Noble8f714622015-11-19 11:16:54 -080077struct grpc_tcp_listener {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 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 Tillera82950e2015-09-22 12:33:20 -070081 union {
Craig Tiller772c97b2015-02-17 08:07:34 -080082 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;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -080087 int port;
Craig Tiller33825112015-09-18 07:44:19 -070088 grpc_closure read_closure;
89 grpc_closure destroyed_closure;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -080090 gpr_refcount refs;
Nicolas Noble8f714622015-11-19 11:16:54 -080091 struct grpc_tcp_listener *next;
92 struct grpc_tcp_listener *dual_stack_second_port;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -080093 int is_dual_stack_second_port;
Nicolas Noble8f714622015-11-19 11:16:54 -080094};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095
Craig Tillera82950e2015-09-22 12:33:20 -070096static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) {
Craig Tilleraa31da42015-02-17 16:33:35 -080097 struct stat st;
98
Craig Tillera82950e2015-09-22 12:33:20 -070099 if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
100 unlink(un->sun_path);
101 }
Craig Tilleraa31da42015-02-17 16:33:35 -0800102}
103
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104/* the overall server */
Craig Tillera82950e2015-09-22 12:33:20 -0700105struct grpc_tcp_server {
Robbie Shade3cd2d182015-08-28 14:30:35 -0400106 /* Called whenever accept() succeeds on a server port. */
107 grpc_tcp_server_cb on_accept_cb;
108 void *on_accept_cb_arg;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109
110 gpr_mu mu;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111
112 /* active port count: how many ports are actually still listening */
Craig Tilleraec96aa2015-04-07 14:32:15 -0700113 size_t active_ports;
114 /* destroyed port count: how many ports are completely destroyed */
115 size_t destroyed_ports;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116
Craig Tiller6174b9a2015-06-18 08:13:05 -0700117 /* is this server shutting down? (boolean) */
Craig Tiller6f051402015-05-13 11:42:42 -0700118 int shutdown;
119
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800120 /* linked list of server ports */
Nicolas Noble8f714622015-11-19 11:16:54 -0800121 grpc_tcp_listener *head;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800122 unsigned nports;
Craig Tilleraec96aa2015-04-07 14:32:15 -0700123
124 /* shutdown callback */
Craig Tillerd1bec032015-09-18 17:29:00 -0700125 grpc_closure *shutdown_complete;
Craig Tiller0189ac02015-05-11 14:59:19 -0700126
Craig Tiller6174b9a2015-06-18 08:13:05 -0700127 /* all pollsets interested in new connections */
Craig Tiller0189ac02015-05-11 14:59:19 -0700128 grpc_pollset **pollsets;
Craig Tiller6174b9a2015-06-18 08:13:05 -0700129 /* number of pollsets in the pollsets array */
Craig Tiller0189ac02015-05-11 14:59:19 -0700130 size_t pollset_count;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131};
132
Craig Tillera82950e2015-09-22 12:33:20 -0700133grpc_tcp_server *grpc_tcp_server_create(void) {
134 grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server));
135 gpr_mu_init(&s->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800136 s->active_ports = 0;
Craig Tilleraec96aa2015-04-07 14:32:15 -0700137 s->destroyed_ports = 0;
Craig Tiller6f051402015-05-13 11:42:42 -0700138 s->shutdown = 0;
Robbie Shade3cd2d182015-08-28 14:30:35 -0400139 s->on_accept_cb = NULL;
140 s->on_accept_cb_arg = NULL;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800141 s->head = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142 s->nports = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800143 return s;
144}
145
Craig Tillera82950e2015-09-22 12:33:20 -0700146static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) {
147 grpc_exec_ctx_enqueue(exec_ctx, s->shutdown_complete, 1);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700148
Craig Tillera82950e2015-09-22 12:33:20 -0700149 gpr_mu_destroy(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700150
Nicolas "Pixel" Noblec6a7c6e2015-11-19 21:55:44 +0100151 while (s->head) {
152 grpc_tcp_listener *sp = s->head;
153 s->head = sp->next;
Nicolas Noble8f714622015-11-19 11:16:54 -0800154 grpc_tcp_listener_unref(sp);
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800155 }
156
Craig Tillera82950e2015-09-22 12:33:20 -0700157 gpr_free(s);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700158}
159
Craig Tillera82950e2015-09-22 12:33:20 -0700160static void destroyed_port(grpc_exec_ctx *exec_ctx, void *server, int success) {
Craig Tilleraec96aa2015-04-07 14:32:15 -0700161 grpc_tcp_server *s = server;
Craig Tillera82950e2015-09-22 12:33:20 -0700162 gpr_mu_lock(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700163 s->destroyed_ports++;
Craig Tillera82950e2015-09-22 12:33:20 -0700164 if (s->destroyed_ports == s->nports) {
165 gpr_mu_unlock(&s->mu);
166 finish_shutdown(exec_ctx, s);
167 } else {
168 GPR_ASSERT(s->destroyed_ports < s->nports);
169 gpr_mu_unlock(&s->mu);
170 }
Craig Tilleraec96aa2015-04-07 14:32:15 -0700171}
172
Craig Tiller6174b9a2015-06-18 08:13:05 -0700173/* called when all listening endpoints have been shutdown, so no further
174 events will be received on them - at this point it's safe to destroy
175 things */
Craig Tillera82950e2015-09-22 12:33:20 -0700176static void deactivated_all_ports(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177 /* delete ALL the things */
Craig Tillera82950e2015-09-22 12:33:20 -0700178 gpr_mu_lock(&s->mu);
Craig Tiller6f051402015-05-13 11:42:42 -0700179
Craig Tillera82950e2015-09-22 12:33:20 -0700180 if (!s->shutdown) {
181 gpr_mu_unlock(&s->mu);
182 return;
183 }
Craig Tiller45724b32015-09-22 10:42:19 -0700184
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800185 if (s->head) {
Nicolas Noble8f714622015-11-19 11:16:54 -0800186 grpc_tcp_listener *sp;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800187 for (sp = s->head; sp; sp = sp->next) {
Craig Tillera82950e2015-09-22 12:33:20 -0700188 if (sp->addr.sockaddr.sa_family == AF_UNIX) {
189 unlink_if_unix_domain_socket(&sp->addr.un);
190 }
191 sp->destroyed_closure.cb = destroyed_port;
192 sp->destroyed_closure.cb_arg = s;
193 grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure,
194 "tcp_listener_shutdown");
Craig Tiller45724b32015-09-22 10:42:19 -0700195 }
Craig Tillera82950e2015-09-22 12:33:20 -0700196 gpr_mu_unlock(&s->mu);
197 } else {
198 gpr_mu_unlock(&s->mu);
199 finish_shutdown(exec_ctx, s);
200 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201}
202
Craig Tillera82950e2015-09-22 12:33:20 -0700203void grpc_tcp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s,
204 grpc_closure *closure) {
Craig Tillera82950e2015-09-22 12:33:20 -0700205 gpr_mu_lock(&s->mu);
Craig Tiller6f051402015-05-13 11:42:42 -0700206
Craig Tillera82950e2015-09-22 12:33:20 -0700207 GPR_ASSERT(!s->shutdown);
Craig Tiller6f051402015-05-13 11:42:42 -0700208 s->shutdown = 1;
209
Craig Tillerd1bec032015-09-18 17:29:00 -0700210 s->shutdown_complete = closure;
Craig Tiller6f051402015-05-13 11:42:42 -0700211
212 /* shutdown all fd's */
Craig Tillera82950e2015-09-22 12:33:20 -0700213 if (s->active_ports) {
Nicolas Noble8f714622015-11-19 11:16:54 -0800214 grpc_tcp_listener *sp;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800215 for (sp = s->head; sp; sp = sp->next) {
216 grpc_fd_shutdown(exec_ctx, sp->emfd);
Craig Tiller6f051402015-05-13 11:42:42 -0700217 }
Craig Tillera82950e2015-09-22 12:33:20 -0700218 gpr_mu_unlock(&s->mu);
219 } else {
220 gpr_mu_unlock(&s->mu);
221 deactivated_all_ports(exec_ctx, s);
222 }
Craig Tiller6f051402015-05-13 11:42:42 -0700223}
224
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225/* get max listen queue size on linux */
Craig Tillera82950e2015-09-22 12:33:20 -0700226static void init_max_accept_queue_size(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800227 int n = SOMAXCONN;
228 char buf[64];
Craig Tillera82950e2015-09-22 12:33:20 -0700229 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) {
239 n = (int)i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800240 }
Craig Tillera82950e2015-09-22 12:33:20 -0700241 }
242 fclose(fp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800243 s_max_accept_queue_size = n;
244
Craig Tillera82950e2015-09-22 12:33:20 -0700245 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 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800251}
252
Craig Tillera82950e2015-09-22 12:33:20 -0700253static int get_max_accept_queue_size(void) {
254 gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800255 return s_max_accept_queue_size;
256}
257
nnoble0c475f02014-12-05 15:37:39 -0800258/* Prepare a recently-created socket for listening. */
Craig Tillera82950e2015-09-22 12:33:20 -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
Craig Tillera82950e2015-09-22 12:33:20 -0700264 if (fd < 0) {
265 goto error;
266 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800267
Craig Tillera82950e2015-09-22 12:33:20 -0700268 if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) ||
269 (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) ||
270 !grpc_set_socket_reuse_addr(fd, 1))) ||
271 !grpc_set_socket_no_sigpipe_if_possible(fd)) {
272 gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
273 strerror(errno));
274 goto error;
275 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800276
Craig Tillera82950e2015-09-22 12:33:20 -0700277 GPR_ASSERT(addr_len < ~(socklen_t)0);
278 if (bind(fd, addr, (socklen_t)addr_len) < 0) {
279 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);
283 goto error;
284 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800285
Craig Tillera82950e2015-09-22 12:33:20 -0700286 if (listen(fd, get_max_accept_queue_size()) < 0) {
287 gpr_log(GPR_ERROR, "listen: %s", strerror(errno));
288 goto error;
289 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800290
Craig Tillera82950e2015-09-22 12:33:20 -0700291 sockname_len = sizeof(sockname_temp);
292 if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) {
293 goto error;
294 }
ctiller570d1f42015-01-12 16:29:52 -0800295
Craig Tillera82950e2015-09-22 12:33:20 -0700296 return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800297
298error:
Craig Tillera82950e2015-09-22 12:33:20 -0700299 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 */
Craig Tillera82950e2015-09-22 12:33:20 -0700306static void on_read(grpc_exec_ctx *exec_ctx, void *arg, int success) {
Nicolas Noble8f714622015-11-19 11:16:54 -0800307 grpc_tcp_listener *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
Craig Tillera82950e2015-09-22 12:33:20 -0700311 if (!success) {
312 goto error;
313 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800314
315 /* loop until accept4 returns EAGAIN, and then re-arm notification */
Craig Tillera82950e2015-09-22 12:33:20 -0700316 for (;;) {
317 struct sockaddr_storage addr;
318 socklen_t addrlen = sizeof(addr);
319 char *addr_str;
320 char *name;
321 /* 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. */
323 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:
329 grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure);
330 return;
331 default:
332 gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno));
333 goto error;
334 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800335 }
336
Craig Tillera82950e2015-09-22 12:33:20 -0700337 grpc_set_socket_no_sigpipe_if_possible(fd);
338
339 addr_str = grpc_sockaddr_to_uri((struct sockaddr *)&addr);
340 gpr_asprintf(&name, "tcp-server-connection:%s", addr_str);
341
342 if (grpc_tcp_trace) {
343 gpr_log(GPR_DEBUG, "SERVER_CONNECT: incoming connection: %s", addr_str);
344 }
345
346 fdobj = grpc_fd_create(fd, name);
347 /* TODO(ctiller): revise this when we have server-side sharding
348 of channels -- we certainly should not be automatically adding every
349 incoming channel to every pollset owned by the server */
350 for (i = 0; i < sp->server->pollset_count; i++) {
351 grpc_pollset_add_fd(exec_ctx, sp->server->pollsets[i], fdobj);
352 }
353 sp->server->on_accept_cb(
354 exec_ctx, sp->server->on_accept_cb_arg,
355 grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str));
356
357 gpr_free(name);
358 gpr_free(addr_str);
359 }
360
yang-gb063c872015-10-07 11:40:13 -0700361 GPR_UNREACHABLE_CODE(return );
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800362
363error:
Craig Tillera82950e2015-09-22 12:33:20 -0700364 gpr_mu_lock(&sp->server->mu);
365 if (0 == --sp->server->active_ports) {
366 gpr_mu_unlock(&sp->server->mu);
367 deactivated_all_ports(exec_ctx, sp->server);
368 } else {
369 gpr_mu_unlock(&sp->server->mu);
370 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800371}
372
Nicolas Noble8f714622015-11-19 11:16:54 -0800373static grpc_tcp_listener *add_socket_to_server(grpc_tcp_server *s, int fd,
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800374 const struct sockaddr *addr,
375 size_t addr_len) {
Nicolas Noble8f714622015-11-19 11:16:54 -0800376 grpc_tcp_listener *sp = NULL;
ctiller570d1f42015-01-12 16:29:52 -0800377 int port;
Craig Tillerfa275a92015-06-01 13:55:54 -0700378 char *addr_str;
379 char *name;
nnoble0c475f02014-12-05 15:37:39 -0800380
Craig Tillera82950e2015-09-22 12:33:20 -0700381 port = prepare_socket(fd, addr, addr_len);
382 if (port >= 0) {
383 grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1);
384 gpr_asprintf(&name, "tcp-server-listener:%s", addr_str);
385 gpr_mu_lock(&s->mu);
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800386 s->nports++;
Craig Tillera82950e2015-09-22 12:33:20 -0700387 GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server");
Nicolas Noble8f714622015-11-19 11:16:54 -0800388 sp = gpr_malloc(sizeof(grpc_tcp_listener));
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800389 sp->next = s->head;
390 s->head = sp;
Craig Tillera82950e2015-09-22 12:33:20 -0700391 sp->server = s;
392 sp->fd = fd;
393 sp->emfd = grpc_fd_create(fd, name);
394 memcpy(sp->addr.untyped, addr, addr_len);
395 sp->addr_len = addr_len;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800396 sp->port = port;
397 sp->is_dual_stack_second_port = 0;
398 sp->dual_stack_second_port = NULL;
399 gpr_ref_init(&sp->refs, 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700400 GPR_ASSERT(sp->emfd);
401 gpr_mu_unlock(&s->mu);
402 gpr_free(addr_str);
403 gpr_free(name);
404 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800405
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800406 return sp;
nnoble0c475f02014-12-05 15:37:39 -0800407}
408
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800409grpc_tcp_listener *grpc_tcp_server_add_port(grpc_tcp_server *s,
410 const void *addr,
411 size_t addr_len) {
412 int allocated_port = -1;
Nicolas Noble8f714622015-11-19 11:16:54 -0800413 grpc_tcp_listener *sp;
414 grpc_tcp_listener *sp2 = NULL;
nnoble0c475f02014-12-05 15:37:39 -0800415 int fd;
416 grpc_dualstack_mode dsmode;
417 struct sockaddr_in6 addr6_v4mapped;
418 struct sockaddr_in wild4;
419 struct sockaddr_in6 wild6;
420 struct sockaddr_in addr4_copy;
ctiller570d1f42015-01-12 16:29:52 -0800421 struct sockaddr *allocated_addr = NULL;
422 struct sockaddr_storage sockname_temp;
423 socklen_t sockname_len;
nnoble0c475f02014-12-05 15:37:39 -0800424 int port;
425
Craig Tillera82950e2015-09-22 12:33:20 -0700426 if (((struct sockaddr *)addr)->sa_family == AF_UNIX) {
427 unlink_if_unix_domain_socket(addr);
428 }
Craig Tilleraa31da42015-02-17 16:33:35 -0800429
ctiller570d1f42015-01-12 16:29:52 -0800430 /* Check if this is a wildcard port, and if so, try to keep the port the same
431 as some previously created listener. */
Craig Tillera82950e2015-09-22 12:33:20 -0700432 if (grpc_sockaddr_get_port(addr) == 0) {
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800433 for (sp = s->head; sp; sp = sp->next) {
Craig Tillera82950e2015-09-22 12:33:20 -0700434 sockname_len = sizeof(sockname_temp);
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800435 if (0 == getsockname(sp->fd, (struct sockaddr *)&sockname_temp,
Craig Tillera82950e2015-09-22 12:33:20 -0700436 &sockname_len)) {
437 port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
438 if (port > 0) {
439 allocated_addr = malloc(addr_len);
440 memcpy(allocated_addr, addr, addr_len);
441 grpc_sockaddr_set_port(allocated_addr, port);
442 addr = allocated_addr;
443 break;
444 }
445 }
ctiller570d1f42015-01-12 16:29:52 -0800446 }
Craig Tillera82950e2015-09-22 12:33:20 -0700447 }
ctiller570d1f42015-01-12 16:29:52 -0800448
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800449 sp = NULL;
450
Craig Tillera82950e2015-09-22 12:33:20 -0700451 if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
452 addr = (const struct sockaddr *)&addr6_v4mapped;
453 addr_len = sizeof(addr6_v4mapped);
454 }
nnoble0c475f02014-12-05 15:37:39 -0800455
456 /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
Craig Tillera82950e2015-09-22 12:33:20 -0700457 if (grpc_sockaddr_is_wildcard(addr, &port)) {
458 grpc_sockaddr_make_wildcards(port, &wild4, &wild6);
nnoble0c475f02014-12-05 15:37:39 -0800459
Craig Tillera82950e2015-09-22 12:33:20 -0700460 /* Try listening on IPv6 first. */
461 addr = (struct sockaddr *)&wild6;
462 addr_len = sizeof(wild6);
463 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800464 sp = add_socket_to_server(s, fd, addr, addr_len);
465 allocated_port = sp->port;
Craig Tillera82950e2015-09-22 12:33:20 -0700466 if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) {
467 goto done;
nnoble0c475f02014-12-05 15:37:39 -0800468 }
469
Craig Tillera82950e2015-09-22 12:33:20 -0700470 /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800471 if (port == 0 && allocated_port > 0) {
472 grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port);
473 sp2 = sp;
ctiller570d1f42015-01-12 16:29:52 -0800474 }
Craig Tillera82950e2015-09-22 12:33:20 -0700475 addr = (struct sockaddr *)&wild4;
476 addr_len = sizeof(wild4);
477 }
478
479 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
480 if (fd < 0) {
481 gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno));
482 }
483 if (dsmode == GRPC_DSMODE_IPV4 &&
484 grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) {
485 addr = (struct sockaddr *)&addr4_copy;
486 addr_len = sizeof(addr4_copy);
487 }
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800488 sp = add_socket_to_server(s, fd, addr, addr_len);
489 sp->dual_stack_second_port = sp2;
490 if (sp2) sp2->is_dual_stack_second_port = 1;
ctiller570d1f42015-01-12 16:29:52 -0800491
492done:
Craig Tillera82950e2015-09-22 12:33:20 -0700493 gpr_free(allocated_addr);
Nicolas Noble8f714622015-11-19 11:16:54 -0800494 return sp;
nnoble0c475f02014-12-05 15:37:39 -0800495}
496
Vijay Pai7b080ba2015-09-29 22:22:36 +0000497int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned port_index) {
Nicolas Noble8f714622015-11-19 11:16:54 -0800498 grpc_tcp_listener *sp;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800499 for (sp = s->head; sp && port_index != 0; sp = sp->next, port_index--);
500 if (port_index == 0 && sp) {
501 return sp->fd;
502 } else {
503 return -1;
504 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800505}
506
Craig Tillera82950e2015-09-22 12:33:20 -0700507void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s,
508 grpc_pollset **pollsets, size_t pollset_count,
509 grpc_tcp_server_cb on_accept_cb,
510 void *on_accept_cb_arg) {
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800511 size_t i;
Nicolas Noble8f714622015-11-19 11:16:54 -0800512 grpc_tcp_listener *sp;
Craig Tillera82950e2015-09-22 12:33:20 -0700513 GPR_ASSERT(on_accept_cb);
514 gpr_mu_lock(&s->mu);
515 GPR_ASSERT(!s->on_accept_cb);
516 GPR_ASSERT(s->active_ports == 0);
Robbie Shade3cd2d182015-08-28 14:30:35 -0400517 s->on_accept_cb = on_accept_cb;
518 s->on_accept_cb_arg = on_accept_cb_arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700519 s->pollsets = pollsets;
520 s->pollset_count = pollset_count;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800521 for (sp = s->head; sp; sp = sp->next) {
522 for (i = 0; i < pollset_count; i++) {
523 grpc_pollset_add_fd(exec_ctx, pollsets[i], sp->emfd);
ctiller58393c22015-01-07 14:03:30 -0800524 }
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800525 sp->read_closure.cb = on_read;
526 sp->read_closure.cb_arg = sp;
527 grpc_fd_notify_on_read(exec_ctx, sp->emfd,
528 &sp->read_closure);
Craig Tillera82950e2015-09-22 12:33:20 -0700529 s->active_ports++;
530 }
531 gpr_mu_unlock(&s->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800532}
Craig Tiller0c0b60c2015-01-21 15:49:28 -0800533
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800534int grpc_tcp_listener_get_port(grpc_tcp_listener *listener) {
Nicolas Noble8f714622015-11-19 11:16:54 -0800535 grpc_tcp_listener *sp = listener;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800536 return sp->port;
537}
538
539void grpc_tcp_listener_ref(grpc_tcp_listener *listener) {
Nicolas Noble8f714622015-11-19 11:16:54 -0800540 grpc_tcp_listener *sp = listener;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800541 gpr_ref(&sp->refs);
542}
543
544void grpc_tcp_listener_unref(grpc_tcp_listener *listener) {
Nicolas Noble8f714622015-11-19 11:16:54 -0800545 grpc_tcp_listener *sp = listener;
Nicolas Noble5eb4e1c2015-11-18 17:19:45 -0800546 if (sp->is_dual_stack_second_port) return;
547 if (gpr_unref(&sp->refs)) {
548 if (sp->dual_stack_second_port) gpr_free(sp->dual_stack_second_port);
549 gpr_free(listener);
550 }
551}
552
Craig Tiller190d3602015-02-18 09:23:38 -0800553#endif