Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Klempner | 78b7992 | 2015-02-04 10:18:59 -0800 | [diff] [blame] | 34 | /* FIXME: "posix" files shouldn't be depending on _GNU_SOURCE */ |
| 35 | #ifndef _GNU_SOURCE |
| 36 | #define _GNU_SOURCE |
| 37 | #endif |
| 38 | |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 39 | #include <grpc/support/port_platform.h> |
| 40 | |
Craig Tiller | a172ad7 | 2015-01-21 15:51:47 -0800 | [diff] [blame] | 41 | #ifdef GPR_POSIX_SOCKET |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 42 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 43 | #include "src/core/iomgr/tcp_server.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 45 | #include <errno.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <fcntl.h> |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 47 | #include <limits.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | #include <netinet/in.h> |
| 49 | #include <netinet/tcp.h> |
| 50 | #include <stdio.h> |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 51 | #include <string.h> |
| 52 | #include <sys/socket.h> |
| 53 | #include <sys/stat.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | #include <sys/types.h> |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 55 | #include <sys/un.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | #include <unistd.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 58 | #include "src/core/iomgr/pollset_posix.h" |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 59 | #include "src/core/iomgr/resolve_address.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 60 | #include "src/core/iomgr/sockaddr_utils.h" |
| 61 | #include "src/core/iomgr/socket_utils_posix.h" |
| 62 | #include "src/core/iomgr/tcp_posix.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | #include <grpc/support/alloc.h> |
| 64 | #include <grpc/support/log.h> |
| 65 | #include <grpc/support/sync.h> |
| 66 | #include <grpc/support/time.h> |
| 67 | |
| 68 | #define INIT_PORT_CAP 2 |
| 69 | #define MIN_SAFE_ACCEPT_QUEUE_SIZE 100 |
| 70 | |
| 71 | static gpr_once s_init_max_accept_queue_size; |
| 72 | static int s_max_accept_queue_size; |
| 73 | |
| 74 | /* one listening port */ |
| 75 | typedef struct { |
| 76 | int fd; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 77 | grpc_fd *emfd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | grpc_tcp_server *server; |
Craig Tiller | 772c97b | 2015-02-17 08:07:34 -0800 | [diff] [blame] | 79 | union { |
| 80 | gpr_uint8 untyped[GRPC_MAX_SOCKADDR_SIZE]; |
| 81 | struct sockaddr sockaddr; |
| 82 | struct sockaddr_un un; |
| 83 | } addr; |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 84 | int addr_len; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 85 | grpc_iomgr_closure read_closure; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | } server_port; |
| 87 | |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 88 | static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) { |
| 89 | struct stat st; |
| 90 | |
| 91 | if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) { |
| 92 | unlink(un->sun_path); |
| 93 | } |
| 94 | } |
| 95 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 96 | /* the overall server */ |
| 97 | struct grpc_tcp_server { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 98 | grpc_tcp_server_cb cb; |
| 99 | void *cb_arg; |
| 100 | |
| 101 | gpr_mu mu; |
| 102 | gpr_cv cv; |
| 103 | |
| 104 | /* active port count: how many ports are actually still listening */ |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame^] | 105 | size_t active_ports; |
| 106 | /* destroyed port count: how many ports are completely destroyed */ |
| 107 | size_t destroyed_ports; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 108 | |
| 109 | /* all listening ports */ |
| 110 | server_port *ports; |
| 111 | size_t nports; |
| 112 | size_t port_capacity; |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame^] | 113 | |
| 114 | /* shutdown callback */ |
| 115 | void (*shutdown_complete)(void *); |
| 116 | void *shutdown_complete_arg; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 119 | grpc_tcp_server *grpc_tcp_server_create(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server)); |
| 121 | gpr_mu_init(&s->mu); |
| 122 | gpr_cv_init(&s->cv); |
| 123 | s->active_ports = 0; |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame^] | 124 | s->destroyed_ports = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 125 | s->cb = NULL; |
| 126 | s->cb_arg = NULL; |
| 127 | s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP); |
| 128 | s->nports = 0; |
| 129 | s->port_capacity = INIT_PORT_CAP; |
| 130 | return s; |
| 131 | } |
| 132 | |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame^] | 133 | static void finish_shutdown(grpc_tcp_server *s) { |
| 134 | s->shutdown_complete(s->shutdown_complete_arg); |
| 135 | |
| 136 | gpr_mu_destroy(&s->mu); |
| 137 | gpr_cv_destroy(&s->cv); |
| 138 | |
| 139 | gpr_free(s->ports); |
| 140 | gpr_free(s); |
| 141 | } |
| 142 | |
| 143 | static void destroyed_port(void *server, int success) { |
| 144 | grpc_tcp_server *s = server; |
| 145 | gpr_mu_lock(&s->mu); |
| 146 | s->destroyed_ports++; |
| 147 | if (s->destroyed_ports == s->nports) { |
| 148 | gpr_mu_unlock(&s->mu); |
| 149 | finish_shutdown(s); |
| 150 | } else { |
| 151 | gpr_mu_unlock(&s->mu); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static void dont_care_about_shutdown_completion(void *ignored) {} |
| 156 | |
| 157 | void grpc_tcp_server_destroy(grpc_tcp_server *s, |
| 158 | void (*shutdown_complete)(void *shutdown_complete_arg), |
| 159 | void *shutdown_complete_arg) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | size_t i; |
| 161 | gpr_mu_lock(&s->mu); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame^] | 162 | |
| 163 | s->shutdown_complete = shutdown_complete ? shutdown_complete : dont_care_about_shutdown_completion; |
| 164 | s->shutdown_complete_arg = shutdown_complete_arg; |
| 165 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | /* shutdown all fd's */ |
| 167 | for (i = 0; i < s->nports; i++) { |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 168 | grpc_fd_shutdown(s->ports[i].emfd); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 169 | } |
| 170 | /* wait while that happens */ |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame^] | 171 | /* TODO(ctiller): make this asynchronous also */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 172 | while (s->active_ports) { |
| 173 | gpr_cv_wait(&s->cv, &s->mu, gpr_inf_future); |
| 174 | } |
| 175 | gpr_mu_unlock(&s->mu); |
| 176 | |
| 177 | /* delete ALL the things */ |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame^] | 178 | if (s->nports) { |
| 179 | for (i = 0; i < s->nports; i++) { |
| 180 | server_port *sp = &s->ports[i]; |
| 181 | if (sp->addr.sockaddr.sa_family == AF_UNIX) { |
| 182 | unlink_if_unix_domain_socket(&sp->addr.un); |
| 183 | } |
| 184 | grpc_fd_orphan(sp->emfd, destroyed_port, s); |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 185 | } |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame^] | 186 | } else { |
| 187 | finish_shutdown(s); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 188 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* get max listen queue size on linux */ |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 192 | static void init_max_accept_queue_size(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 193 | int n = SOMAXCONN; |
| 194 | char buf[64]; |
| 195 | FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r"); |
| 196 | if (fp == NULL) { |
| 197 | /* 2.4 kernel. */ |
| 198 | s_max_accept_queue_size = SOMAXCONN; |
| 199 | return; |
| 200 | } |
| 201 | if (fgets(buf, sizeof buf, fp)) { |
| 202 | char *end; |
| 203 | long i = strtol(buf, &end, 10); |
| 204 | if (i > 0 && i <= INT_MAX && end && *end == 0) { |
| 205 | n = i; |
| 206 | } |
| 207 | } |
| 208 | fclose(fp); |
| 209 | s_max_accept_queue_size = n; |
| 210 | |
| 211 | if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) { |
| 212 | gpr_log(GPR_INFO, |
| 213 | "Suspiciously small accept queue (%d) will probably lead to " |
| 214 | "connection drops", |
| 215 | s_max_accept_queue_size); |
| 216 | } |
| 217 | } |
| 218 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 219 | static int get_max_accept_queue_size(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 220 | gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size); |
| 221 | return s_max_accept_queue_size; |
| 222 | } |
| 223 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 224 | /* Prepare a recently-created socket for listening. */ |
| 225 | static int prepare_socket(int fd, const struct sockaddr *addr, int addr_len) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 226 | struct sockaddr_storage sockname_temp; |
| 227 | socklen_t sockname_len; |
| 228 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 229 | if (fd < 0) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | goto error; |
| 231 | } |
| 232 | |
| 233 | if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) || |
Craig Tiller | d209ed0 | 2015-02-13 23:18:15 -0800 | [diff] [blame] | 234 | (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) || |
| 235 | !grpc_set_socket_reuse_addr(fd, 1)))) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 236 | gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd, |
| 237 | strerror(errno)); |
| 238 | goto error; |
| 239 | } |
| 240 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 241 | if (bind(fd, addr, addr_len) < 0) { |
| 242 | char *addr_str; |
| 243 | grpc_sockaddr_to_string(&addr_str, addr, 0); |
| 244 | gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno)); |
| 245 | gpr_free(addr_str); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 246 | goto error; |
| 247 | } |
| 248 | |
| 249 | if (listen(fd, get_max_accept_queue_size()) < 0) { |
| 250 | gpr_log(GPR_ERROR, "listen: %s", strerror(errno)); |
| 251 | goto error; |
| 252 | } |
| 253 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 254 | sockname_len = sizeof(sockname_temp); |
| 255 | if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) { |
| 256 | goto error; |
| 257 | } |
| 258 | |
| 259 | return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 260 | |
| 261 | error: |
| 262 | if (fd >= 0) { |
| 263 | close(fd); |
| 264 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 265 | return -1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* event manager callback when reads are ready */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 269 | static void on_read(void *arg, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 270 | server_port *sp = arg; |
| 271 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 272 | if (!success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 273 | goto error; |
| 274 | } |
| 275 | |
| 276 | /* loop until accept4 returns EAGAIN, and then re-arm notification */ |
| 277 | for (;;) { |
| 278 | struct sockaddr_storage addr; |
| 279 | socklen_t addrlen = sizeof(addr); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 280 | /* Note: If we ever decide to return this address to the user, remember to |
| 281 | strip off the ::ffff:0.0.0.0/96 prefix first. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 282 | int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1); |
| 283 | if (fd < 0) { |
| 284 | switch (errno) { |
| 285 | case EINTR: |
| 286 | continue; |
| 287 | case EAGAIN: |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 288 | grpc_fd_notify_on_read(sp->emfd, &sp->read_closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 289 | return; |
| 290 | default: |
| 291 | gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno)); |
| 292 | goto error; |
| 293 | } |
| 294 | } |
| 295 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 296 | sp->server->cb( |
| 297 | sp->server->cb_arg, |
| 298 | grpc_tcp_create(grpc_fd_create(fd), GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | abort(); |
| 302 | |
| 303 | error: |
| 304 | gpr_mu_lock(&sp->server->mu); |
| 305 | if (0 == --sp->server->active_ports) { |
| 306 | gpr_cv_broadcast(&sp->server->cv); |
| 307 | } |
| 308 | gpr_mu_unlock(&sp->server->mu); |
| 309 | } |
| 310 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 311 | static int add_socket_to_server(grpc_tcp_server *s, int fd, |
| 312 | const struct sockaddr *addr, int addr_len) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 313 | server_port *sp; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 314 | int port; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 315 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 316 | port = prepare_socket(fd, addr, addr_len); |
| 317 | if (port >= 0) { |
| 318 | gpr_mu_lock(&s->mu); |
| 319 | GPR_ASSERT(!s->cb && "must add ports before starting server"); |
| 320 | /* append it to the list under a lock */ |
| 321 | if (s->nports == s->port_capacity) { |
| 322 | s->port_capacity *= 2; |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 323 | s->ports = gpr_realloc(s->ports, sizeof(server_port) * s->port_capacity); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 324 | } |
| 325 | sp = &s->ports[s->nports++]; |
| 326 | sp->server = s; |
| 327 | sp->fd = fd; |
| 328 | sp->emfd = grpc_fd_create(fd); |
Craig Tiller | 772c97b | 2015-02-17 08:07:34 -0800 | [diff] [blame] | 329 | memcpy(sp->addr.untyped, addr, addr_len); |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 330 | sp->addr_len = addr_len; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 331 | GPR_ASSERT(sp->emfd); |
| 332 | gpr_mu_unlock(&s->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 333 | } |
| 334 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 335 | return port; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Craig Tiller | a172ad7 | 2015-01-21 15:51:47 -0800 | [diff] [blame] | 338 | int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 339 | int addr_len) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 340 | int allocated_port1 = -1; |
| 341 | int allocated_port2 = -1; |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 342 | unsigned i; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 343 | int fd; |
| 344 | grpc_dualstack_mode dsmode; |
| 345 | struct sockaddr_in6 addr6_v4mapped; |
| 346 | struct sockaddr_in wild4; |
| 347 | struct sockaddr_in6 wild6; |
| 348 | struct sockaddr_in addr4_copy; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 349 | struct sockaddr *allocated_addr = NULL; |
| 350 | struct sockaddr_storage sockname_temp; |
| 351 | socklen_t sockname_len; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 352 | int port; |
| 353 | |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 354 | if (((struct sockaddr *)addr)->sa_family == AF_UNIX) { |
| 355 | unlink_if_unix_domain_socket(addr); |
| 356 | } |
| 357 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 358 | /* Check if this is a wildcard port, and if so, try to keep the port the same |
| 359 | as some previously created listener. */ |
| 360 | if (grpc_sockaddr_get_port(addr) == 0) { |
| 361 | for (i = 0; i < s->nports; i++) { |
| 362 | sockname_len = sizeof(sockname_temp); |
| 363 | if (0 == getsockname(s->ports[i].fd, (struct sockaddr *)&sockname_temp, |
| 364 | &sockname_len)) { |
| 365 | port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
| 366 | if (port > 0) { |
| 367 | allocated_addr = malloc(addr_len); |
| 368 | memcpy(allocated_addr, addr, addr_len); |
| 369 | grpc_sockaddr_set_port(allocated_addr, port); |
| 370 | addr = allocated_addr; |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 377 | if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) { |
| 378 | addr = (const struct sockaddr *)&addr6_v4mapped; |
| 379 | addr_len = sizeof(addr6_v4mapped); |
| 380 | } |
| 381 | |
| 382 | /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */ |
| 383 | if (grpc_sockaddr_is_wildcard(addr, &port)) { |
| 384 | grpc_sockaddr_make_wildcards(port, &wild4, &wild6); |
| 385 | |
| 386 | /* Try listening on IPv6 first. */ |
| 387 | addr = (struct sockaddr *)&wild6; |
| 388 | addr_len = sizeof(wild6); |
| 389 | fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 390 | allocated_port1 = add_socket_to_server(s, fd, addr, addr_len); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 391 | if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 392 | goto done; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */ |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 396 | if (port == 0 && allocated_port1 > 0) { |
| 397 | grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1); |
| 398 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 399 | addr = (struct sockaddr *)&wild4; |
| 400 | addr_len = sizeof(wild4); |
| 401 | } |
| 402 | |
| 403 | fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode); |
| 404 | if (fd < 0) { |
| 405 | gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno)); |
| 406 | } |
| 407 | if (dsmode == GRPC_DSMODE_IPV4 && |
| 408 | grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) { |
| 409 | addr = (struct sockaddr *)&addr4_copy; |
| 410 | addr_len = sizeof(addr4_copy); |
| 411 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 412 | allocated_port2 = add_socket_to_server(s, fd, addr, addr_len); |
| 413 | |
| 414 | done: |
| 415 | gpr_free(allocated_addr); |
| 416 | return allocated_port1 >= 0 ? allocated_port1 : allocated_port2; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 419 | int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned index) { |
| 420 | return (index < s->nports) ? s->ports[index].fd : -1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 423 | void grpc_tcp_server_start(grpc_tcp_server *s, grpc_pollset **pollsets, |
| 424 | size_t pollset_count, grpc_tcp_server_cb cb, |
| 425 | void *cb_arg) { |
| 426 | size_t i, j; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 427 | GPR_ASSERT(cb); |
| 428 | gpr_mu_lock(&s->mu); |
| 429 | GPR_ASSERT(!s->cb); |
| 430 | GPR_ASSERT(s->active_ports == 0); |
| 431 | s->cb = cb; |
| 432 | s->cb_arg = cb_arg; |
| 433 | for (i = 0; i < s->nports; i++) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 434 | for (j = 0; j < pollset_count; j++) { |
| 435 | grpc_pollset_add_fd(pollsets[j], s->ports[i].emfd); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 436 | } |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 437 | s->ports[i].read_closure.cb = on_read; |
| 438 | s->ports[i].read_closure.cb_arg = &s->ports[i]; |
| 439 | grpc_fd_notify_on_read(s->ports[i].emfd, &s->ports[i].read_closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 440 | s->active_ports++; |
| 441 | } |
| 442 | gpr_mu_unlock(&s->mu); |
| 443 | } |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 444 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 445 | #endif |