blob: 7b9cfe09cb7982c7a926df4a88d29886e23035c4 [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 Tiller45724b32015-09-22 10:42:19 -070077typedef struct
78{
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079 int fd;
ctiller18b49ab2014-12-09 14:39:16 -080080 grpc_fd *emfd;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 grpc_tcp_server *server;
Craig Tiller45724b32015-09-22 10:42:19 -070082 union
83 {
Craig Tiller772c97b2015-02-17 08:07:34 -080084 gpr_uint8 untyped[GRPC_MAX_SOCKADDR_SIZE];
85 struct sockaddr sockaddr;
86 struct sockaddr_un un;
87 } addr;
Craig Tillerf96dfc32015-09-10 14:43:18 -070088 size_t addr_len;
Craig Tiller33825112015-09-18 07:44:19 -070089 grpc_closure read_closure;
90 grpc_closure destroyed_closure;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091} server_port;
92
Craig Tiller45724b32015-09-22 10:42:19 -070093static void
94unlink_if_unix_domain_socket (const struct sockaddr_un *un)
95{
Craig Tilleraa31da42015-02-17 16:33:35 -080096 struct stat st;
97
Craig Tiller45724b32015-09-22 10:42:19 -070098 if (stat (un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK)
99 {
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 Tiller45724b32015-09-22 10:42:19 -0700105struct grpc_tcp_server
106{
Robbie Shade3cd2d182015-08-28 14:30:35 -0400107 /* Called whenever accept() succeeds on a server port. */
108 grpc_tcp_server_cb on_accept_cb;
109 void *on_accept_cb_arg;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110
111 gpr_mu mu;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800112
113 /* active port count: how many ports are actually still listening */
Craig Tilleraec96aa2015-04-07 14:32:15 -0700114 size_t active_ports;
115 /* destroyed port count: how many ports are completely destroyed */
116 size_t destroyed_ports;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117
Craig Tiller6174b9a2015-06-18 08:13:05 -0700118 /* is this server shutting down? (boolean) */
Craig Tiller6f051402015-05-13 11:42:42 -0700119 int shutdown;
120
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121 /* all listening ports */
122 server_port *ports;
123 size_t nports;
124 size_t port_capacity;
Craig Tilleraec96aa2015-04-07 14:32:15 -0700125
126 /* shutdown callback */
Craig Tillerd1bec032015-09-18 17:29:00 -0700127 grpc_closure *shutdown_complete;
Craig Tiller0189ac02015-05-11 14:59:19 -0700128
Craig Tiller6174b9a2015-06-18 08:13:05 -0700129 /* all pollsets interested in new connections */
Craig Tiller0189ac02015-05-11 14:59:19 -0700130 grpc_pollset **pollsets;
Craig Tiller6174b9a2015-06-18 08:13:05 -0700131 /* number of pollsets in the pollsets array */
Craig Tiller0189ac02015-05-11 14:59:19 -0700132 size_t pollset_count;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133};
134
Craig Tiller45724b32015-09-22 10:42:19 -0700135grpc_tcp_server *
136grpc_tcp_server_create (void)
137{
138 grpc_tcp_server *s = gpr_malloc (sizeof (grpc_tcp_server));
139 gpr_mu_init (&s->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800140 s->active_ports = 0;
Craig Tilleraec96aa2015-04-07 14:32:15 -0700141 s->destroyed_ports = 0;
Craig Tiller6f051402015-05-13 11:42:42 -0700142 s->shutdown = 0;
Robbie Shade3cd2d182015-08-28 14:30:35 -0400143 s->on_accept_cb = NULL;
144 s->on_accept_cb_arg = NULL;
Craig Tiller45724b32015-09-22 10:42:19 -0700145 s->ports = gpr_malloc (sizeof (server_port) * INIT_PORT_CAP);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146 s->nports = 0;
147 s->port_capacity = INIT_PORT_CAP;
148 return s;
149}
150
Craig Tiller45724b32015-09-22 10:42:19 -0700151static void
152finish_shutdown (grpc_tcp_server * s, grpc_closure_list * closure_list)
153{
154 grpc_closure_list_add (closure_list, s->shutdown_complete, 1);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700155
Craig Tiller45724b32015-09-22 10:42:19 -0700156 gpr_mu_destroy (&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700157
Craig Tiller45724b32015-09-22 10:42:19 -0700158 gpr_free (s->ports);
159 gpr_free (s);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700160}
161
Craig Tiller45724b32015-09-22 10:42:19 -0700162static void
163destroyed_port (void *server, int success, grpc_closure_list * closure_list)
164{
Craig Tilleraec96aa2015-04-07 14:32:15 -0700165 grpc_tcp_server *s = server;
Craig Tiller45724b32015-09-22 10:42:19 -0700166 gpr_mu_lock (&s->mu);
Craig Tilleraec96aa2015-04-07 14:32:15 -0700167 s->destroyed_ports++;
Craig Tiller45724b32015-09-22 10:42:19 -0700168 if (s->destroyed_ports == s->nports)
169 {
170 gpr_mu_unlock (&s->mu);
171 finish_shutdown (s, closure_list);
172 }
173 else
174 {
175 GPR_ASSERT (s->destroyed_ports < s->nports);
176 gpr_mu_unlock (&s->mu);
177 }
Craig Tilleraec96aa2015-04-07 14:32:15 -0700178}
179
Craig Tiller6174b9a2015-06-18 08:13:05 -0700180/* called when all listening endpoints have been shutdown, so no further
181 events will be received on them - at this point it's safe to destroy
182 things */
Craig Tiller45724b32015-09-22 10:42:19 -0700183static void
184deactivated_all_ports (grpc_tcp_server * s, grpc_closure_list * closure_list)
185{
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800186 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187
188 /* delete ALL the things */
Craig Tiller45724b32015-09-22 10:42:19 -0700189 gpr_mu_lock (&s->mu);
Craig Tiller6f051402015-05-13 11:42:42 -0700190
Craig Tiller45724b32015-09-22 10:42:19 -0700191 if (!s->shutdown)
192 {
193 gpr_mu_unlock (&s->mu);
194 return;
Craig Tillerae7fe922015-02-13 23:16:32 -0800195 }
Craig Tiller45724b32015-09-22 10:42:19 -0700196
197 if (s->nports)
198 {
199 for (i = 0; i < s->nports; i++)
200 {
201 server_port *sp = &s->ports[i];
202 if (sp->addr.sockaddr.sa_family == AF_UNIX)
203 {
204 unlink_if_unix_domain_socket (&sp->addr.un);
205 }
206 sp->destroyed_closure.cb = destroyed_port;
207 sp->destroyed_closure.cb_arg = s;
208 grpc_fd_orphan (sp->emfd, &sp->destroyed_closure, "tcp_listener_shutdown", closure_list);
209 }
210 gpr_mu_unlock (&s->mu);
211 }
212 else
213 {
214 gpr_mu_unlock (&s->mu);
215 finish_shutdown (s, closure_list);
216 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800217}
218
Craig Tiller45724b32015-09-22 10:42:19 -0700219void
220grpc_tcp_server_destroy (grpc_tcp_server * s, grpc_closure * closure, grpc_closure_list * closure_list)
221{
Craig Tiller6f051402015-05-13 11:42:42 -0700222 size_t i;
Craig Tiller45724b32015-09-22 10:42:19 -0700223 gpr_mu_lock (&s->mu);
Craig Tiller6f051402015-05-13 11:42:42 -0700224
Craig Tiller45724b32015-09-22 10:42:19 -0700225 GPR_ASSERT (!s->shutdown);
Craig Tiller6f051402015-05-13 11:42:42 -0700226 s->shutdown = 1;
227
Craig Tillerd1bec032015-09-18 17:29:00 -0700228 s->shutdown_complete = closure;
Craig Tiller6f051402015-05-13 11:42:42 -0700229
230 /* shutdown all fd's */
Craig Tiller45724b32015-09-22 10:42:19 -0700231 if (s->active_ports)
232 {
233 for (i = 0; i < s->nports; i++)
234 {
235 grpc_fd_shutdown (s->ports[i].emfd, closure_list);
236 }
237 gpr_mu_unlock (&s->mu);
Craig Tiller6f051402015-05-13 11:42:42 -0700238 }
Craig Tiller45724b32015-09-22 10:42:19 -0700239 else
240 {
241 gpr_mu_unlock (&s->mu);
242 deactivated_all_ports (s, closure_list);
243 }
Craig Tiller6f051402015-05-13 11:42:42 -0700244}
245
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246/* get max listen queue size on linux */
Craig Tiller45724b32015-09-22 10:42:19 -0700247static void
248init_max_accept_queue_size (void)
249{
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800250 int n = SOMAXCONN;
251 char buf[64];
Craig Tiller45724b32015-09-22 10:42:19 -0700252 FILE *fp = fopen ("/proc/sys/net/core/somaxconn", "r");
253 if (fp == NULL)
254 {
255 /* 2.4 kernel. */
256 s_max_accept_queue_size = SOMAXCONN;
257 return;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258 }
Craig Tiller45724b32015-09-22 10:42:19 -0700259 if (fgets (buf, sizeof buf, fp))
260 {
261 char *end;
262 long i = strtol (buf, &end, 10);
263 if (i > 0 && i <= INT_MAX && end && *end == 0)
264 {
265 n = (int) i;
266 }
267 }
268 fclose (fp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800269 s_max_accept_queue_size = n;
270
Craig Tiller45724b32015-09-22 10:42:19 -0700271 if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE)
272 {
273 gpr_log (GPR_INFO, "Suspiciously small accept queue (%d) will probably lead to " "connection drops", s_max_accept_queue_size);
274 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800275}
276
Craig Tiller45724b32015-09-22 10:42:19 -0700277static int
278get_max_accept_queue_size (void)
279{
280 gpr_once_init (&s_init_max_accept_queue_size, init_max_accept_queue_size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800281 return s_max_accept_queue_size;
282}
283
nnoble0c475f02014-12-05 15:37:39 -0800284/* Prepare a recently-created socket for listening. */
Craig Tiller45724b32015-09-22 10:42:19 -0700285static int
286prepare_socket (int fd, const struct sockaddr *addr, size_t addr_len)
287{
ctiller570d1f42015-01-12 16:29:52 -0800288 struct sockaddr_storage sockname_temp;
289 socklen_t sockname_len;
290
Craig Tiller45724b32015-09-22 10:42:19 -0700291 if (fd < 0)
292 {
293 goto error;
294 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800295
Craig Tiller45724b32015-09-22 10:42:19 -0700296 if (!grpc_set_socket_nonblocking (fd, 1) || !grpc_set_socket_cloexec (fd, 1) || (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency (fd, 1) || !grpc_set_socket_reuse_addr (fd, 1))) || !grpc_set_socket_no_sigpipe_if_possible (fd))
297 {
298 gpr_log (GPR_ERROR, "Unable to configure socket %d: %s", fd, strerror (errno));
299 goto error;
300 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800301
Craig Tiller45724b32015-09-22 10:42:19 -0700302 GPR_ASSERT (addr_len < ~(socklen_t) 0);
303 if (bind (fd, addr, (socklen_t) addr_len) < 0)
304 {
305 char *addr_str;
306 grpc_sockaddr_to_string (&addr_str, addr, 0);
307 gpr_log (GPR_ERROR, "bind addr=%s: %s", addr_str, strerror (errno));
308 gpr_free (addr_str);
309 goto error;
310 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800311
Craig Tiller45724b32015-09-22 10:42:19 -0700312 if (listen (fd, get_max_accept_queue_size ()) < 0)
313 {
314 gpr_log (GPR_ERROR, "listen: %s", strerror (errno));
315 goto error;
316 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800317
Craig Tiller45724b32015-09-22 10:42:19 -0700318 sockname_len = sizeof (sockname_temp);
319 if (getsockname (fd, (struct sockaddr *) &sockname_temp, &sockname_len) < 0)
320 {
321 goto error;
322 }
ctiller570d1f42015-01-12 16:29:52 -0800323
Craig Tiller45724b32015-09-22 10:42:19 -0700324 return grpc_sockaddr_get_port ((struct sockaddr *) &sockname_temp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800325
326error:
Craig Tiller45724b32015-09-22 10:42:19 -0700327 if (fd >= 0)
328 {
329 close (fd);
330 }
ctiller570d1f42015-01-12 16:29:52 -0800331 return -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332}
333
334/* event manager callback when reads are ready */
Craig Tiller45724b32015-09-22 10:42:19 -0700335static void
336on_read (void *arg, int success, grpc_closure_list * closure_list)
337{
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800338 server_port *sp = arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700339 grpc_fd *fdobj;
340 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800341
Craig Tiller45724b32015-09-22 10:42:19 -0700342 if (!success)
343 {
344 goto error;
345 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800346
347 /* loop until accept4 returns EAGAIN, and then re-arm notification */
Craig Tiller45724b32015-09-22 10:42:19 -0700348 for (;;)
349 {
350 struct sockaddr_storage addr;
351 socklen_t addrlen = sizeof (addr);
352 char *addr_str;
353 char *name;
354 /* Note: If we ever decide to return this address to the user, remember to
355 strip off the ::ffff:0.0.0.0/96 prefix first. */
356 int fd = grpc_accept4 (sp->fd, (struct sockaddr *) &addr, &addrlen, 1, 1);
357 if (fd < 0)
358 {
359 switch (errno)
360 {
361 case EINTR:
362 continue;
363 case EAGAIN:
364 grpc_fd_notify_on_read (sp->emfd, &sp->read_closure, closure_list);
365 return;
366 default:
367 gpr_log (GPR_ERROR, "Failed accept4: %s", strerror (errno));
368 goto error;
369 }
370 }
371
372 grpc_set_socket_no_sigpipe_if_possible (fd);
373
374 addr_str = grpc_sockaddr_to_uri ((struct sockaddr *) &addr);
375 gpr_asprintf (&name, "tcp-server-connection:%s", addr_str);
376
377 if (grpc_tcp_trace)
378 {
379 gpr_log (GPR_DEBUG, "SERVER_CONNECT: incoming connection: %s", addr_str);
380 }
381
382 fdobj = grpc_fd_create (fd, name);
383 /* TODO(ctiller): revise this when we have server-side sharding
384 of channels -- we certainly should not be automatically adding every
385 incoming channel to every pollset owned by the server */
386 for (i = 0; i < sp->server->pollset_count; i++)
387 {
388 grpc_pollset_add_fd (sp->server->pollsets[i], fdobj, closure_list);
389 }
390 sp->server->on_accept_cb (sp->server->on_accept_cb_arg, grpc_tcp_create (fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str), closure_list);
391
392 gpr_free (name);
393 gpr_free (addr_str);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800394 }
395
Craig Tiller45724b32015-09-22 10:42:19 -0700396 abort ();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800397
398error:
Craig Tiller45724b32015-09-22 10:42:19 -0700399 gpr_mu_lock (&sp->server->mu);
400 if (0 == --sp->server->active_ports)
401 {
402 gpr_mu_unlock (&sp->server->mu);
403 deactivated_all_ports (sp->server, closure_list);
404 }
405 else
406 {
407 gpr_mu_unlock (&sp->server->mu);
408 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800409}
410
Craig Tiller45724b32015-09-22 10:42:19 -0700411static int
412add_socket_to_server (grpc_tcp_server * s, int fd, const struct sockaddr *addr, size_t addr_len)
413{
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800414 server_port *sp;
ctiller570d1f42015-01-12 16:29:52 -0800415 int port;
Craig Tillerfa275a92015-06-01 13:55:54 -0700416 char *addr_str;
417 char *name;
nnoble0c475f02014-12-05 15:37:39 -0800418
Craig Tiller45724b32015-09-22 10:42:19 -0700419 port = prepare_socket (fd, addr, addr_len);
420 if (port >= 0)
421 {
422 grpc_sockaddr_to_string (&addr_str, (struct sockaddr *) &addr, 1);
423 gpr_asprintf (&name, "tcp-server-listener:%s", addr_str);
424 gpr_mu_lock (&s->mu);
425 GPR_ASSERT (!s->on_accept_cb && "must add ports before starting server");
426 /* append it to the list under a lock */
427 if (s->nports == s->port_capacity)
428 {
429 s->port_capacity *= 2;
430 s->ports = gpr_realloc (s->ports, sizeof (server_port) * s->port_capacity);
431 }
432 sp = &s->ports[s->nports++];
433 sp->server = s;
434 sp->fd = fd;
435 sp->emfd = grpc_fd_create (fd, name);
436 memcpy (sp->addr.untyped, addr, addr_len);
437 sp->addr_len = addr_len;
438 GPR_ASSERT (sp->emfd);
439 gpr_mu_unlock (&s->mu);
440 gpr_free (addr_str);
441 gpr_free (name);
ctiller570d1f42015-01-12 16:29:52 -0800442 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800443
ctiller570d1f42015-01-12 16:29:52 -0800444 return port;
nnoble0c475f02014-12-05 15:37:39 -0800445}
446
Craig Tiller45724b32015-09-22 10:42:19 -0700447int
448grpc_tcp_server_add_port (grpc_tcp_server * s, const void *addr, size_t addr_len)
449{
ctiller570d1f42015-01-12 16:29:52 -0800450 int allocated_port1 = -1;
451 int allocated_port2 = -1;
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100452 unsigned i;
nnoble0c475f02014-12-05 15:37:39 -0800453 int fd;
454 grpc_dualstack_mode dsmode;
455 struct sockaddr_in6 addr6_v4mapped;
456 struct sockaddr_in wild4;
457 struct sockaddr_in6 wild6;
458 struct sockaddr_in addr4_copy;
ctiller570d1f42015-01-12 16:29:52 -0800459 struct sockaddr *allocated_addr = NULL;
460 struct sockaddr_storage sockname_temp;
461 socklen_t sockname_len;
nnoble0c475f02014-12-05 15:37:39 -0800462 int port;
463
Craig Tiller45724b32015-09-22 10:42:19 -0700464 if (((struct sockaddr *) addr)->sa_family == AF_UNIX)
465 {
466 unlink_if_unix_domain_socket (addr);
467 }
Craig Tilleraa31da42015-02-17 16:33:35 -0800468
ctiller570d1f42015-01-12 16:29:52 -0800469 /* Check if this is a wildcard port, and if so, try to keep the port the same
470 as some previously created listener. */
Craig Tiller45724b32015-09-22 10:42:19 -0700471 if (grpc_sockaddr_get_port (addr) == 0)
472 {
473 for (i = 0; i < s->nports; i++)
474 {
475 sockname_len = sizeof (sockname_temp);
476 if (0 == getsockname (s->ports[i].fd, (struct sockaddr *) &sockname_temp, &sockname_len))
477 {
478 port = grpc_sockaddr_get_port ((struct sockaddr *) &sockname_temp);
479 if (port > 0)
480 {
481 allocated_addr = malloc (addr_len);
482 memcpy (allocated_addr, addr, addr_len);
483 grpc_sockaddr_set_port (allocated_addr, port);
484 addr = allocated_addr;
485 break;
486 }
487 }
488 }
ctiller570d1f42015-01-12 16:29:52 -0800489 }
ctiller570d1f42015-01-12 16:29:52 -0800490
Craig Tiller45724b32015-09-22 10:42:19 -0700491 if (grpc_sockaddr_to_v4mapped (addr, &addr6_v4mapped))
492 {
493 addr = (const struct sockaddr *) &addr6_v4mapped;
494 addr_len = sizeof (addr6_v4mapped);
495 }
nnoble0c475f02014-12-05 15:37:39 -0800496
497 /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
Craig Tiller45724b32015-09-22 10:42:19 -0700498 if (grpc_sockaddr_is_wildcard (addr, &port))
499 {
500 grpc_sockaddr_make_wildcards (port, &wild4, &wild6);
nnoble0c475f02014-12-05 15:37:39 -0800501
Craig Tiller45724b32015-09-22 10:42:19 -0700502 /* Try listening on IPv6 first. */
503 addr = (struct sockaddr *) &wild6;
504 addr_len = sizeof (wild6);
505 fd = grpc_create_dualstack_socket (addr, SOCK_STREAM, 0, &dsmode);
506 allocated_port1 = add_socket_to_server (s, fd, addr, addr_len);
507 if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK)
508 {
509 goto done;
510 }
511
512 /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
513 if (port == 0 && allocated_port1 > 0)
514 {
515 grpc_sockaddr_set_port ((struct sockaddr *) &wild4, allocated_port1);
516 }
517 addr = (struct sockaddr *) &wild4;
518 addr_len = sizeof (wild4);
nnoble0c475f02014-12-05 15:37:39 -0800519 }
520
Craig Tiller45724b32015-09-22 10:42:19 -0700521 fd = grpc_create_dualstack_socket (addr, SOCK_STREAM, 0, &dsmode);
522 if (fd < 0)
523 {
524 gpr_log (GPR_ERROR, "Unable to create socket: %s", strerror (errno));
ctiller570d1f42015-01-12 16:29:52 -0800525 }
Craig Tiller45724b32015-09-22 10:42:19 -0700526 if (dsmode == GRPC_DSMODE_IPV4 && grpc_sockaddr_is_v4mapped (addr, &addr4_copy))
527 {
528 addr = (struct sockaddr *) &addr4_copy;
529 addr_len = sizeof (addr4_copy);
530 }
531 allocated_port2 = add_socket_to_server (s, fd, addr, addr_len);
ctiller570d1f42015-01-12 16:29:52 -0800532
533done:
Craig Tiller45724b32015-09-22 10:42:19 -0700534 gpr_free (allocated_addr);
ctiller570d1f42015-01-12 16:29:52 -0800535 return allocated_port1 >= 0 ? allocated_port1 : allocated_port2;
nnoble0c475f02014-12-05 15:37:39 -0800536}
537
Craig Tiller45724b32015-09-22 10:42:19 -0700538int
539grpc_tcp_server_get_fd (grpc_tcp_server * s, unsigned index)
540{
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100541 return (index < s->nports) ? s->ports[index].fd : -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800542}
543
Craig Tiller45724b32015-09-22 10:42:19 -0700544void
545grpc_tcp_server_start (grpc_tcp_server * s, grpc_pollset ** pollsets, size_t pollset_count, grpc_tcp_server_cb on_accept_cb, void *on_accept_cb_arg, grpc_closure_list * closure_list)
546{
Craig Tiller20bc56d2015-02-12 09:02:56 -0800547 size_t i, j;
Craig Tiller45724b32015-09-22 10:42:19 -0700548 GPR_ASSERT (on_accept_cb);
549 gpr_mu_lock (&s->mu);
550 GPR_ASSERT (!s->on_accept_cb);
551 GPR_ASSERT (s->active_ports == 0);
Robbie Shade3cd2d182015-08-28 14:30:35 -0400552 s->on_accept_cb = on_accept_cb;
553 s->on_accept_cb_arg = on_accept_cb_arg;
Craig Tiller0189ac02015-05-11 14:59:19 -0700554 s->pollsets = pollsets;
555 s->pollset_count = pollset_count;
Craig Tiller45724b32015-09-22 10:42:19 -0700556 for (i = 0; i < s->nports; i++)
557 {
558 for (j = 0; j < pollset_count; j++)
559 {
560 grpc_pollset_add_fd (pollsets[j], s->ports[i].emfd, closure_list);
561 }
562 s->ports[i].read_closure.cb = on_read;
563 s->ports[i].read_closure.cb_arg = &s->ports[i];
564 grpc_fd_notify_on_read (s->ports[i].emfd, &s->ports[i].read_closure, closure_list);
565 s->active_ports++;
ctiller58393c22015-01-07 14:03:30 -0800566 }
Craig Tiller45724b32015-09-22 10:42:19 -0700567 gpr_mu_unlock (&s->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800568}
Craig Tiller0c0b60c2015-01-21 15:49:28 -0800569
Craig Tiller190d3602015-02-18 09:23:38 -0800570#endif