blob: 99c76dcbe9aeddfd7d303e994fba5526e9057bea [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 */
Craig Tillera82950e2015-09-22 12:33:20 -070077typedef struct {
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;
Craig Tiller33825112015-09-18 07:44:19 -070087 grpc_closure read_closure;
88 grpc_closure destroyed_closure;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089} server_port;
90
Craig Tillera82950e2015-09-22 12:33:20 -070091static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) {
Craig Tilleraa31da42015-02-17 16:33:35 -080092 struct stat st;
93
Craig Tillera82950e2015-09-22 12:33:20 -070094 if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
95 unlink(un->sun_path);
96 }
Craig Tilleraa31da42015-02-17 16:33:35 -080097}
98
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080099/* the overall server */
Craig Tillera82950e2015-09-22 12:33:20 -0700100struct grpc_tcp_server {
Robbie Shade3cd2d182015-08-28 14:30:35 -0400101 /* Called whenever accept() succeeds on a server port. */
102 grpc_tcp_server_cb on_accept_cb;
103 void *on_accept_cb_arg;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104
105 gpr_mu mu;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106
107 /* active port count: how many ports are actually still listening */
Craig Tilleraec96aa2015-04-07 14:32:15 -0700108 size_t active_ports;
109 /* destroyed port count: how many ports are completely destroyed */
110 size_t destroyed_ports;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111
Craig Tiller6174b9a2015-06-18 08:13:05 -0700112 /* is this server shutting down? (boolean) */
Craig Tiller6f051402015-05-13 11:42:42 -0700113 int shutdown;
114
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115 /* all listening ports */
116 server_port *ports;
117 size_t nports;
118 size_t port_capacity;
Craig Tilleraec96aa2015-04-07 14:32:15 -0700119
120 /* shutdown callback */
Craig Tillerd1bec032015-09-18 17:29:00 -0700121 grpc_closure *shutdown_complete;
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 Tillera82950e2015-09-22 12:33:20 -0700129grpc_tcp_server *grpc_tcp_server_create(void) {
130 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;
Robbie Shade3cd2d182015-08-28 14:30:35 -0400135 s->on_accept_cb = NULL;
136 s->on_accept_cb_arg = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700137 s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138 s->nports = 0;
139 s->port_capacity = INIT_PORT_CAP;
140 return s;
141}
142
Craig Tillera82950e2015-09-22 12:33:20 -0700143static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) {
144 grpc_exec_ctx_enqueue(exec_ctx, s->shutdown_complete, 1);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700145
Craig Tillera82950e2015-09-22 12:33:20 -0700146 gpr_mu_destroy(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700147
Craig Tillera82950e2015-09-22 12:33:20 -0700148 gpr_free(s->ports);
149 gpr_free(s);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700150}
151
Craig Tillera82950e2015-09-22 12:33:20 -0700152static void destroyed_port(grpc_exec_ctx *exec_ctx, void *server, int success) {
Craig Tilleraec96aa2015-04-07 14:32:15 -0700153 grpc_tcp_server *s = server;
Craig Tillera82950e2015-09-22 12:33:20 -0700154 gpr_mu_lock(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700155 s->destroyed_ports++;
Craig Tillera82950e2015-09-22 12:33:20 -0700156 if (s->destroyed_ports == s->nports) {
157 gpr_mu_unlock(&s->mu);
158 finish_shutdown(exec_ctx, s);
159 } else {
160 GPR_ASSERT(s->destroyed_ports < s->nports);
161 gpr_mu_unlock(&s->mu);
162 }
Craig Tilleraec96aa2015-04-07 14:32:15 -0700163}
164
Craig Tiller6174b9a2015-06-18 08:13:05 -0700165/* called when all listening endpoints have been shutdown, so no further
166 events will be received on them - at this point it's safe to destroy
167 things */
Craig Tillera82950e2015-09-22 12:33:20 -0700168static void deactivated_all_ports(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170
171 /* delete ALL the things */
Craig Tillera82950e2015-09-22 12:33:20 -0700172 gpr_mu_lock(&s->mu);
Craig Tiller6f051402015-05-13 11:42:42 -0700173
Craig Tillera82950e2015-09-22 12:33:20 -0700174 if (!s->shutdown) {
175 gpr_mu_unlock(&s->mu);
176 return;
177 }
Craig Tiller45724b32015-09-22 10:42:19 -0700178
Craig Tillera82950e2015-09-22 12:33:20 -0700179 if (s->nports) {
180 for (i = 0; i < s->nports; i++) {
181 server_port *sp = &s->ports[i];
182 if (sp->addr.sockaddr.sa_family == AF_UNIX) {
183 unlink_if_unix_domain_socket(&sp->addr.un);
184 }
185 sp->destroyed_closure.cb = destroyed_port;
186 sp->destroyed_closure.cb_arg = s;
187 grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure,
188 "tcp_listener_shutdown");
Craig Tiller45724b32015-09-22 10:42:19 -0700189 }
Craig Tillera82950e2015-09-22 12:33:20 -0700190 gpr_mu_unlock(&s->mu);
191 } else {
192 gpr_mu_unlock(&s->mu);
193 finish_shutdown(exec_ctx, s);
194 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195}
196
Craig Tillera82950e2015-09-22 12:33:20 -0700197void grpc_tcp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s,
198 grpc_closure *closure) {
Craig Tiller6f051402015-05-13 11:42:42 -0700199 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700200 gpr_mu_lock(&s->mu);
Craig Tiller6f051402015-05-13 11:42:42 -0700201
Craig Tillera82950e2015-09-22 12:33:20 -0700202 GPR_ASSERT(!s->shutdown);
Craig Tiller6f051402015-05-13 11:42:42 -0700203 s->shutdown = 1;
204
Craig Tillerd1bec032015-09-18 17:29:00 -0700205 s->shutdown_complete = closure;
Craig Tiller6f051402015-05-13 11:42:42 -0700206
207 /* shutdown all fd's */
Craig Tillera82950e2015-09-22 12:33:20 -0700208 if (s->active_ports) {
209 for (i = 0; i < s->nports; i++) {
210 grpc_fd_shutdown(exec_ctx, s->ports[i].emfd);
Craig Tiller6f051402015-05-13 11:42:42 -0700211 }
Craig Tillera82950e2015-09-22 12:33:20 -0700212 gpr_mu_unlock(&s->mu);
213 } else {
214 gpr_mu_unlock(&s->mu);
215 deactivated_all_ports(exec_ctx, s);
216 }
Craig Tiller6f051402015-05-13 11:42:42 -0700217}
218
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219/* get max listen queue size on linux */
Craig Tillera82950e2015-09-22 12:33:20 -0700220static void init_max_accept_queue_size(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800221 int n = SOMAXCONN;
222 char buf[64];
Craig Tillera82950e2015-09-22 12:33:20 -0700223 FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r");
224 if (fp == NULL) {
225 /* 2.4 kernel. */
226 s_max_accept_queue_size = SOMAXCONN;
227 return;
228 }
229 if (fgets(buf, sizeof buf, fp)) {
230 char *end;
231 long i = strtol(buf, &end, 10);
232 if (i > 0 && i <= INT_MAX && end && *end == 0) {
233 n = (int)i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800234 }
Craig Tillera82950e2015-09-22 12:33:20 -0700235 }
236 fclose(fp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800237 s_max_accept_queue_size = n;
238
Craig Tillera82950e2015-09-22 12:33:20 -0700239 if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) {
240 gpr_log(GPR_INFO,
241 "Suspiciously small accept queue (%d) will probably lead to "
242 "connection drops",
243 s_max_accept_queue_size);
244 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800245}
246
Craig Tillera82950e2015-09-22 12:33:20 -0700247static int get_max_accept_queue_size(void) {
248 gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800249 return s_max_accept_queue_size;
250}
251
nnoble0c475f02014-12-05 15:37:39 -0800252/* Prepare a recently-created socket for listening. */
Craig Tillera82950e2015-09-22 12:33:20 -0700253static int prepare_socket(int fd, const struct sockaddr *addr,
254 size_t addr_len) {
ctiller570d1f42015-01-12 16:29:52 -0800255 struct sockaddr_storage sockname_temp;
256 socklen_t sockname_len;
257
Craig Tillera82950e2015-09-22 12:33:20 -0700258 if (fd < 0) {
259 goto error;
260 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800261
Craig Tillera82950e2015-09-22 12:33:20 -0700262 if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) ||
263 (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) ||
264 !grpc_set_socket_reuse_addr(fd, 1))) ||
265 !grpc_set_socket_no_sigpipe_if_possible(fd)) {
266 gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
267 strerror(errno));
268 goto error;
269 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800270
Craig Tillera82950e2015-09-22 12:33:20 -0700271 GPR_ASSERT(addr_len < ~(socklen_t)0);
272 if (bind(fd, addr, (socklen_t)addr_len) < 0) {
273 char *addr_str;
274 grpc_sockaddr_to_string(&addr_str, addr, 0);
275 gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno));
276 gpr_free(addr_str);
277 goto error;
278 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800279
Craig Tillera82950e2015-09-22 12:33:20 -0700280 if (listen(fd, get_max_accept_queue_size()) < 0) {
281 gpr_log(GPR_ERROR, "listen: %s", strerror(errno));
282 goto error;
283 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284
Craig Tillera82950e2015-09-22 12:33:20 -0700285 sockname_len = sizeof(sockname_temp);
286 if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) {
287 goto error;
288 }
ctiller570d1f42015-01-12 16:29:52 -0800289
Craig Tillera82950e2015-09-22 12:33:20 -0700290 return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800291
292error:
Craig Tillera82950e2015-09-22 12:33:20 -0700293 if (fd >= 0) {
294 close(fd);
295 }
ctiller570d1f42015-01-12 16:29:52 -0800296 return -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800297}
298
299/* event manager callback when reads are ready */
Craig Tillera82950e2015-09-22 12:33:20 -0700300static void on_read(grpc_exec_ctx *exec_ctx, void *arg, int success) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800301 server_port *sp = arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700302 grpc_fd *fdobj;
303 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800304
Craig Tillera82950e2015-09-22 12:33:20 -0700305 if (!success) {
306 goto error;
307 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800308
309 /* loop until accept4 returns EAGAIN, and then re-arm notification */
Craig Tillera82950e2015-09-22 12:33:20 -0700310 for (;;) {
311 struct sockaddr_storage addr;
312 socklen_t addrlen = sizeof(addr);
313 char *addr_str;
314 char *name;
315 /* Note: If we ever decide to return this address to the user, remember to
316 strip off the ::ffff:0.0.0.0/96 prefix first. */
317 int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1);
318 if (fd < 0) {
319 switch (errno) {
320 case EINTR:
321 continue;
322 case EAGAIN:
323 grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure);
324 return;
325 default:
326 gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno));
327 goto error;
328 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800329 }
330
Craig Tillera82950e2015-09-22 12:33:20 -0700331 grpc_set_socket_no_sigpipe_if_possible(fd);
332
333 addr_str = grpc_sockaddr_to_uri((struct sockaddr *)&addr);
334 gpr_asprintf(&name, "tcp-server-connection:%s", addr_str);
335
336 if (grpc_tcp_trace) {
337 gpr_log(GPR_DEBUG, "SERVER_CONNECT: incoming connection: %s", addr_str);
338 }
339
340 fdobj = grpc_fd_create(fd, name);
341 /* TODO(ctiller): revise this when we have server-side sharding
342 of channels -- we certainly should not be automatically adding every
343 incoming channel to every pollset owned by the server */
344 for (i = 0; i < sp->server->pollset_count; i++) {
345 grpc_pollset_add_fd(exec_ctx, sp->server->pollsets[i], fdobj);
346 }
347 sp->server->on_accept_cb(
348 exec_ctx, sp->server->on_accept_cb_arg,
349 grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str));
350
351 gpr_free(name);
352 gpr_free(addr_str);
353 }
354
yang-gb063c872015-10-07 11:40:13 -0700355 GPR_UNREACHABLE_CODE(return );
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800356
357error:
Craig Tillera82950e2015-09-22 12:33:20 -0700358 gpr_mu_lock(&sp->server->mu);
359 if (0 == --sp->server->active_ports) {
360 gpr_mu_unlock(&sp->server->mu);
361 deactivated_all_ports(exec_ctx, sp->server);
362 } else {
363 gpr_mu_unlock(&sp->server->mu);
364 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800365}
366
Craig Tillera82950e2015-09-22 12:33:20 -0700367static int add_socket_to_server(grpc_tcp_server *s, int fd,
368 const struct sockaddr *addr, size_t addr_len) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800369 server_port *sp;
ctiller570d1f42015-01-12 16:29:52 -0800370 int port;
Craig Tillerfa275a92015-06-01 13:55:54 -0700371 char *addr_str;
372 char *name;
nnoble0c475f02014-12-05 15:37:39 -0800373
Craig Tillera82950e2015-09-22 12:33:20 -0700374 port = prepare_socket(fd, addr, addr_len);
375 if (port >= 0) {
376 grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1);
377 gpr_asprintf(&name, "tcp-server-listener:%s", addr_str);
378 gpr_mu_lock(&s->mu);
379 GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server");
380 /* append it to the list under a lock */
381 if (s->nports == s->port_capacity) {
382 s->port_capacity *= 2;
383 s->ports = gpr_realloc(s->ports, sizeof(server_port) * s->port_capacity);
ctiller570d1f42015-01-12 16:29:52 -0800384 }
Craig Tillera82950e2015-09-22 12:33:20 -0700385 sp = &s->ports[s->nports++];
386 sp->server = s;
387 sp->fd = fd;
388 sp->emfd = grpc_fd_create(fd, name);
389 memcpy(sp->addr.untyped, addr, addr_len);
390 sp->addr_len = addr_len;
391 GPR_ASSERT(sp->emfd);
392 gpr_mu_unlock(&s->mu);
393 gpr_free(addr_str);
394 gpr_free(name);
395 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800396
ctiller570d1f42015-01-12 16:29:52 -0800397 return port;
nnoble0c475f02014-12-05 15:37:39 -0800398}
399
Craig Tillera82950e2015-09-22 12:33:20 -0700400int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr,
401 size_t addr_len) {
ctiller570d1f42015-01-12 16:29:52 -0800402 int allocated_port1 = -1;
403 int allocated_port2 = -1;
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100404 unsigned i;
nnoble0c475f02014-12-05 15:37:39 -0800405 int fd;
406 grpc_dualstack_mode dsmode;
407 struct sockaddr_in6 addr6_v4mapped;
408 struct sockaddr_in wild4;
409 struct sockaddr_in6 wild6;
410 struct sockaddr_in addr4_copy;
ctiller570d1f42015-01-12 16:29:52 -0800411 struct sockaddr *allocated_addr = NULL;
412 struct sockaddr_storage sockname_temp;
413 socklen_t sockname_len;
nnoble0c475f02014-12-05 15:37:39 -0800414 int port;
415
Craig Tillera82950e2015-09-22 12:33:20 -0700416 if (((struct sockaddr *)addr)->sa_family == AF_UNIX) {
417 unlink_if_unix_domain_socket(addr);
418 }
Craig Tilleraa31da42015-02-17 16:33:35 -0800419
ctiller570d1f42015-01-12 16:29:52 -0800420 /* Check if this is a wildcard port, and if so, try to keep the port the same
421 as some previously created listener. */
Craig Tillera82950e2015-09-22 12:33:20 -0700422 if (grpc_sockaddr_get_port(addr) == 0) {
423 for (i = 0; i < s->nports; i++) {
424 sockname_len = sizeof(sockname_temp);
425 if (0 == getsockname(s->ports[i].fd, (struct sockaddr *)&sockname_temp,
426 &sockname_len)) {
427 port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
428 if (port > 0) {
429 allocated_addr = malloc(addr_len);
430 memcpy(allocated_addr, addr, addr_len);
431 grpc_sockaddr_set_port(allocated_addr, port);
432 addr = allocated_addr;
433 break;
434 }
435 }
ctiller570d1f42015-01-12 16:29:52 -0800436 }
Craig Tillera82950e2015-09-22 12:33:20 -0700437 }
ctiller570d1f42015-01-12 16:29:52 -0800438
Craig Tillera82950e2015-09-22 12:33:20 -0700439 if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
440 addr = (const struct sockaddr *)&addr6_v4mapped;
441 addr_len = sizeof(addr6_v4mapped);
442 }
nnoble0c475f02014-12-05 15:37:39 -0800443
444 /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
Craig Tillera82950e2015-09-22 12:33:20 -0700445 if (grpc_sockaddr_is_wildcard(addr, &port)) {
446 grpc_sockaddr_make_wildcards(port, &wild4, &wild6);
nnoble0c475f02014-12-05 15:37:39 -0800447
Craig Tillera82950e2015-09-22 12:33:20 -0700448 /* Try listening on IPv6 first. */
449 addr = (struct sockaddr *)&wild6;
450 addr_len = sizeof(wild6);
451 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
452 allocated_port1 = add_socket_to_server(s, fd, addr, addr_len);
453 if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) {
454 goto done;
nnoble0c475f02014-12-05 15:37:39 -0800455 }
456
Craig Tillera82950e2015-09-22 12:33:20 -0700457 /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
458 if (port == 0 && allocated_port1 > 0) {
459 grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1);
ctiller570d1f42015-01-12 16:29:52 -0800460 }
Craig Tillera82950e2015-09-22 12:33:20 -0700461 addr = (struct sockaddr *)&wild4;
462 addr_len = sizeof(wild4);
463 }
464
465 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
466 if (fd < 0) {
467 gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno));
468 }
469 if (dsmode == GRPC_DSMODE_IPV4 &&
470 grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) {
471 addr = (struct sockaddr *)&addr4_copy;
472 addr_len = sizeof(addr4_copy);
473 }
474 allocated_port2 = add_socket_to_server(s, fd, addr, addr_len);
ctiller570d1f42015-01-12 16:29:52 -0800475
476done:
Craig Tillera82950e2015-09-22 12:33:20 -0700477 gpr_free(allocated_addr);
ctiller570d1f42015-01-12 16:29:52 -0800478 return allocated_port1 >= 0 ? allocated_port1 : allocated_port2;
nnoble0c475f02014-12-05 15:37:39 -0800479}
480
Vijay Pai7b080ba2015-09-29 22:22:36 +0000481int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned port_index) {
482 return (port_index < s->nports) ? s->ports[port_index].fd : -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800483}
484
Craig Tillera82950e2015-09-22 12:33:20 -0700485void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s,
486 grpc_pollset **pollsets, size_t pollset_count,
487 grpc_tcp_server_cb on_accept_cb,
488 void *on_accept_cb_arg) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800489 size_t i, j;
Craig Tillera82950e2015-09-22 12:33:20 -0700490 GPR_ASSERT(on_accept_cb);
491 gpr_mu_lock(&s->mu);
492 GPR_ASSERT(!s->on_accept_cb);
493 GPR_ASSERT(s->active_ports == 0);
Robbie Shade3cd2d182015-08-28 14:30:35 -0400494 s->on_accept_cb = on_accept_cb;
495 s->on_accept_cb_arg = on_accept_cb_arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700496 s->pollsets = pollsets;
497 s->pollset_count = pollset_count;
Craig Tillera82950e2015-09-22 12:33:20 -0700498 for (i = 0; i < s->nports; i++) {
499 for (j = 0; j < pollset_count; j++) {
500 grpc_pollset_add_fd(exec_ctx, pollsets[j], s->ports[i].emfd);
ctiller58393c22015-01-07 14:03:30 -0800501 }
Craig Tillera82950e2015-09-22 12:33:20 -0700502 s->ports[i].read_closure.cb = on_read;
503 s->ports[i].read_closure.cb_arg = &s->ports[i];
504 grpc_fd_notify_on_read(exec_ctx, s->ports[i].emfd,
505 &s->ports[i].read_closure);
506 s->active_ports++;
507 }
508 gpr_mu_unlock(&s->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800509}
Craig Tiller0c0b60c2015-01-21 15:49:28 -0800510
Craig Tiller190d3602015-02-18 09:23:38 -0800511#endif