blob: 0c5e0053dd30a054dee68e05012e180178c9125c [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 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 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 */
Craig Tillerd1bec032015-09-18 17:29:00 -0700120 grpc_closure *shutdown_complete;
Craig Tiller0189ac02015-05-11 14:59:19 -0700121
Craig Tiller6174b9a2015-06-18 08:13:05 -0700122 /* all pollsets interested in new connections */
Craig Tiller0189ac02015-05-11 14:59:19 -0700123 grpc_pollset **pollsets;
Craig Tiller6174b9a2015-06-18 08:13:05 -0700124 /* number of pollsets in the pollsets array */
Craig Tiller0189ac02015-05-11 14:59:19 -0700125 size_t pollset_count;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126};
127
Craig Tiller32946d32015-01-15 11:37:30 -0800128grpc_tcp_server *grpc_tcp_server_create(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800129 grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server));
130 gpr_mu_init(&s->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131 s->active_ports = 0;
Craig Tilleraec96aa2015-04-07 14:32:15 -0700132 s->destroyed_ports = 0;
Craig Tiller6f051402015-05-13 11:42:42 -0700133 s->shutdown = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134 s->cb = NULL;
135 s->cb_arg = NULL;
136 s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP);
137 s->nports = 0;
138 s->port_capacity = INIT_PORT_CAP;
139 return s;
140}
141
Craig Tillerd1bec032015-09-18 17:29:00 -0700142static void finish_shutdown(grpc_tcp_server *s, grpc_call_list *call_list) {
143 grpc_call_list_add(call_list, s->shutdown_complete, 1);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700144
145 gpr_mu_destroy(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700146
147 gpr_free(s->ports);
148 gpr_free(s);
149}
150
Craig Tillerd1bec032015-09-18 17:29:00 -0700151static void destroyed_port(void *server, int success,
152 grpc_call_list *call_list) {
Craig Tilleraec96aa2015-04-07 14:32:15 -0700153 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);
Craig Tillerd1bec032015-09-18 17:29:00 -0700158 finish_shutdown(s, call_list);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700159 } else {
Craig Tiller0613e582015-07-30 11:55:43 -0700160 GPR_ASSERT(s->destroyed_ports < s->nports);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700161 gpr_mu_unlock(&s->mu);
162 }
163}
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 Tillerd1bec032015-09-18 17:29:00 -0700168static void deactivated_all_ports(grpc_tcp_server *s,
169 grpc_call_list *call_list) {
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 Tillerd1bec032015-09-18 17:29:00 -0700188 grpc_fd_orphan(sp->emfd, &sp->destroyed_closure, "tcp_listener_shutdown",
189 call_list);
Craig Tillerae7fe922015-02-13 23:16:32 -0800190 }
Craig Tiller954d7a22015-04-13 13:01:50 -0700191 gpr_mu_unlock(&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700192 } else {
Craig Tiller954d7a22015-04-13 13:01:50 -0700193 gpr_mu_unlock(&s->mu);
Craig Tillerd1bec032015-09-18 17:29:00 -0700194 finish_shutdown(s, call_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800196}
197
Craig Tillerd1bec032015-09-18 17:29:00 -0700198void grpc_tcp_server_destroy(grpc_tcp_server *s, grpc_closure *closure,
199 grpc_call_list *call_list) {
Craig Tiller6f051402015-05-13 11:42:42 -0700200 size_t i;
201 gpr_mu_lock(&s->mu);
202
203 GPR_ASSERT(!s->shutdown);
204 s->shutdown = 1;
205
Craig Tillerd1bec032015-09-18 17:29:00 -0700206 s->shutdown_complete = closure;
Craig Tiller6f051402015-05-13 11:42:42 -0700207
208 /* shutdown all fd's */
209 if (s->active_ports) {
210 for (i = 0; i < s->nports; i++) {
Craig Tillerd1bec032015-09-18 17:29:00 -0700211 grpc_fd_shutdown(s->ports[i].emfd, call_list);
Craig Tiller6f051402015-05-13 11:42:42 -0700212 }
213 gpr_mu_unlock(&s->mu);
214 } else {
215 gpr_mu_unlock(&s->mu);
Craig Tillerd1bec032015-09-18 17:29:00 -0700216 deactivated_all_ports(s, call_list);
Craig Tiller6f051402015-05-13 11:42:42 -0700217 }
218}
219
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800220/* get max listen queue size on linux */
Craig Tiller32946d32015-01-15 11:37:30 -0800221static void init_max_accept_queue_size(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222 int n = SOMAXCONN;
223 char buf[64];
224 FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r");
225 if (fp == NULL) {
226 /* 2.4 kernel. */
227 s_max_accept_queue_size = SOMAXCONN;
228 return;
229 }
230 if (fgets(buf, sizeof buf, fp)) {
231 char *end;
232 long i = strtol(buf, &end, 10);
233 if (i > 0 && i <= INT_MAX && end && *end == 0) {
Craig Tillerf96dfc32015-09-10 14:43:18 -0700234 n = (int)i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800235 }
236 }
237 fclose(fp);
238 s_max_accept_queue_size = n;
239
240 if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) {
241 gpr_log(GPR_INFO,
242 "Suspiciously small accept queue (%d) will probably lead to "
243 "connection drops",
244 s_max_accept_queue_size);
245 }
246}
247
Craig Tiller32946d32015-01-15 11:37:30 -0800248static int get_max_accept_queue_size(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800249 gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size);
250 return s_max_accept_queue_size;
251}
252
nnoble0c475f02014-12-05 15:37:39 -0800253/* Prepare a recently-created socket for listening. */
Craig Tiller32ca48c2015-09-10 11:47:15 -0700254static int prepare_socket(int fd, const struct sockaddr *addr,
255 size_t addr_len) {
ctiller570d1f42015-01-12 16:29:52 -0800256 struct sockaddr_storage sockname_temp;
257 socklen_t sockname_len;
258
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800259 if (fd < 0) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800260 goto error;
261 }
262
263 if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) ||
Craig Tillerd209ed02015-02-13 23:18:15 -0800264 (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) ||
Craig Tiller2da02962015-05-06 16:14:25 -0700265 !grpc_set_socket_reuse_addr(fd, 1))) ||
266 !grpc_set_socket_no_sigpipe_if_possible(fd)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800267 gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
268 strerror(errno));
269 goto error;
270 }
271
Craig Tillerf96dfc32015-09-10 14:43:18 -0700272 GPR_ASSERT(addr_len < ~(socklen_t)0);
273 if (bind(fd, addr, (socklen_t)addr_len) < 0) {
nnoble0c475f02014-12-05 15:37:39 -0800274 char *addr_str;
275 grpc_sockaddr_to_string(&addr_str, addr, 0);
276 gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno));
277 gpr_free(addr_str);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800278 goto error;
279 }
280
281 if (listen(fd, get_max_accept_queue_size()) < 0) {
282 gpr_log(GPR_ERROR, "listen: %s", strerror(errno));
283 goto error;
284 }
285
ctiller570d1f42015-01-12 16:29:52 -0800286 sockname_len = sizeof(sockname_temp);
287 if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) {
288 goto error;
289 }
290
291 return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800292
293error:
294 if (fd >= 0) {
295 close(fd);
296 }
ctiller570d1f42015-01-12 16:29:52 -0800297 return -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800298}
299
300/* event manager callback when reads are ready */
Craig Tillerd1bec032015-09-18 17:29:00 -0700301static void on_read(void *arg, int success, grpc_call_list *call_list) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800302 server_port *sp = arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700303 grpc_fd *fdobj;
304 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800305
ctiller58393c22015-01-07 14:03:30 -0800306 if (!success) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800307 goto error;
308 }
309
310 /* loop until accept4 returns EAGAIN, and then re-arm notification */
311 for (;;) {
312 struct sockaddr_storage addr;
313 socklen_t addrlen = sizeof(addr);
Craig Tillerfa275a92015-06-01 13:55:54 -0700314 char *addr_str;
315 char *name;
nnoble0c475f02014-12-05 15:37:39 -0800316 /* Note: If we ever decide to return this address to the user, remember to
317 strip off the ::ffff:0.0.0.0/96 prefix first. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800318 int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1);
319 if (fd < 0) {
320 switch (errno) {
321 case EINTR:
322 continue;
323 case EAGAIN:
Craig Tillerd1bec032015-09-18 17:29:00 -0700324 grpc_fd_notify_on_read(sp->emfd, &sp->read_closure, call_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800325 return;
326 default:
327 gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno));
328 goto error;
329 }
330 }
331
Craig Tiller2da02962015-05-06 16:14:25 -0700332 grpc_set_socket_no_sigpipe_if_possible(fd);
333
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700334 addr_str = grpc_sockaddr_to_uri((struct sockaddr *)&addr);
Craig Tillerfa275a92015-06-01 13:55:54 -0700335 gpr_asprintf(&name, "tcp-server-connection:%s", addr_str);
336
Craig Tiller67280382015-09-16 10:58:41 -0700337 if (grpc_tcp_trace) {
338 gpr_log(GPR_DEBUG, "SERVER_CONNECT: incoming connection: %s", addr_str);
339 }
340
Craig Tillerd1bec032015-09-18 17:29:00 -0700341 fdobj = grpc_fd_create(fd, name);
Craig Tiller0189ac02015-05-11 14:59:19 -0700342 /* TODO(ctiller): revise this when we have server-side sharding
343 of channels -- we certainly should not be automatically adding every
344 incoming channel to every pollset owned by the server */
345 for (i = 0; i < sp->server->pollset_count; i++) {
Craig Tillerd1bec032015-09-18 17:29:00 -0700346 grpc_pollset_add_fd(sp->server->pollsets[i], fdobj, call_list);
Craig Tiller0189ac02015-05-11 14:59:19 -0700347 }
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700348 sp->server->cb(
349 sp->server->cb_arg,
Craig Tillerd1bec032015-09-18 17:29:00 -0700350 grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str),
351 call_list);
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);
Craig Tillerd1bec032015-09-18 17:29:00 -0700363 deactivated_all_ports(sp->server, call_list);
Craig Tiller6f051402015-05-13 11:42:42 -0700364 } 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 Tillerd1bec032015-09-18 17:29:00 -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,
Craig Tillerd1bec032015-09-18 17:29:00 -0700489 void *cb_arg, grpc_call_list *call_list) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800490 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++) {
Craig Tillerd1bec032015-09-18 17:29:00 -0700501 grpc_pollset_add_fd(pollsets[j], s->ports[i].emfd, call_list);
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];
Craig Tillerd1bec032015-09-18 17:29:00 -0700505 grpc_fd_notify_on_read(s->ports[i].emfd, &s->ports[i].read_closure,
506 call_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800507 s->active_ports++;
508 }
509 gpr_mu_unlock(&s->mu);
510}
Craig Tiller0c0b60c2015-01-21 15:49:28 -0800511
Craig Tiller190d3602015-02-18 09:23:38 -0800512#endif