Robbie Shade | 8d5196d | 2015-08-13 19:10:28 -0400 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Robbie Shade | 8d5196d | 2015-08-13 19:10:28 -0400 | [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 | |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 34 | /* FIXME: "posix" files shouldn't be depending on _GNU_SOURCE */ |
| 35 | #ifndef _GNU_SOURCE |
| 36 | #define _GNU_SOURCE |
| 37 | #endif |
| 38 | |
| 39 | #include <grpc/support/port_platform.h> |
| 40 | |
| 41 | #ifdef GPR_POSIX_SOCKET |
| 42 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 43 | #include "src/core/lib/iomgr/udp_server.h" |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 44 | |
| 45 | #include <errno.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <limits.h> |
| 48 | #include <netinet/in.h> |
| 49 | #include <netinet/tcp.h> |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 50 | #include <string.h> |
| 51 | #include <sys/socket.h> |
| 52 | #include <sys/stat.h> |
| 53 | #include <sys/types.h> |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 54 | #include <unistd.h> |
| 55 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 56 | #include <grpc/grpc.h> |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 57 | #include <grpc/support/alloc.h> |
| 58 | #include <grpc/support/log.h> |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 59 | #include <grpc/support/string_util.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 60 | #include <grpc/support/sync.h> |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 61 | #include <grpc/support/time.h> |
Nicolas "Pixel" Noble | f55adec | 2016-07-14 02:20:16 +0200 | [diff] [blame] | 62 | #include "src/core/lib/iomgr/error.h" |
Craig Tiller | 8a03448 | 2016-03-28 16:09:04 -0700 | [diff] [blame] | 63 | #include "src/core/lib/iomgr/ev_posix.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 64 | #include "src/core/lib/iomgr/resolve_address.h" |
| 65 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
| 66 | #include "src/core/lib/iomgr/socket_utils_posix.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 67 | #include "src/core/lib/support/string.h" |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 68 | |
| 69 | #define INIT_PORT_CAP 2 |
| 70 | |
| 71 | /* one listening port */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | typedef struct { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 73 | int fd; |
| 74 | grpc_fd *emfd; |
| 75 | grpc_udp_server *server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 76 | union { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 77 | uint8_t untyped[GRPC_MAX_SOCKADDR_SIZE]; |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 78 | struct sockaddr sockaddr; |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 79 | } addr; |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 80 | size_t addr_len; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 81 | grpc_closure read_closure; |
| 82 | grpc_closure destroyed_closure; |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 83 | grpc_udp_server_read_cb read_cb; |
Robbie Shade | 9aa6f40 | 2016-05-12 13:28:04 -0400 | [diff] [blame] | 84 | grpc_udp_server_orphan_cb orphan_cb; |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 85 | } server_port; |
| 86 | |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 87 | /* the overall server */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | struct grpc_udp_server { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 89 | gpr_mu mu; |
| 90 | gpr_cv cv; |
| 91 | |
| 92 | /* active port count: how many ports are actually still listening */ |
| 93 | size_t active_ports; |
| 94 | /* destroyed port count: how many ports are completely destroyed */ |
| 95 | size_t destroyed_ports; |
| 96 | |
| 97 | /* is this server shutting down? (boolean) */ |
| 98 | int shutdown; |
| 99 | |
| 100 | /* all listening ports */ |
| 101 | server_port *ports; |
| 102 | size_t nports; |
| 103 | size_t port_capacity; |
| 104 | |
| 105 | /* shutdown callback */ |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 106 | grpc_closure *shutdown_complete; |
Robbie Shade | 1cef6dc | 2015-07-15 14:16:48 -0400 | [diff] [blame] | 107 | |
| 108 | /* all pollsets interested in new connections */ |
| 109 | grpc_pollset **pollsets; |
| 110 | /* number of pollsets in the pollsets array */ |
| 111 | size_t pollset_count; |
Robbie Shade | 147fe70 | 2015-09-25 15:04:40 -0400 | [diff] [blame] | 112 | /* The parent grpc server */ |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 113 | grpc_server *grpc_server; |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 114 | }; |
| 115 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | grpc_udp_server *grpc_udp_server_create(void) { |
| 117 | grpc_udp_server *s = gpr_malloc(sizeof(grpc_udp_server)); |
| 118 | gpr_mu_init(&s->mu); |
| 119 | gpr_cv_init(&s->cv); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 120 | s->active_ports = 0; |
| 121 | s->destroyed_ports = 0; |
| 122 | s->shutdown = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 124 | s->nports = 0; |
| 125 | s->port_capacity = INIT_PORT_CAP; |
| 126 | |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 127 | return s; |
| 128 | } |
| 129 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_udp_server *s) { |
Nicolas "Pixel" Noble | f55adec | 2016-07-14 02:20:16 +0200 | [diff] [blame] | 131 | grpc_exec_ctx_sched(exec_ctx, s->shutdown_complete, GRPC_ERROR_NONE, NULL); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 132 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 133 | gpr_mu_destroy(&s->mu); |
| 134 | gpr_cv_destroy(&s->cv); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 135 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | gpr_free(s->ports); |
| 137 | gpr_free(s); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Bogdan Drutu | f89fdf3 | 2016-02-16 14:38:38 -0800 | [diff] [blame] | 140 | static void destroyed_port(grpc_exec_ctx *exec_ctx, void *server, |
Nicolas "Pixel" Noble | f55adec | 2016-07-14 02:20:16 +0200 | [diff] [blame] | 141 | grpc_error *error) { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 142 | grpc_udp_server *s = server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 143 | gpr_mu_lock(&s->mu); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 144 | s->destroyed_ports++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 145 | if (s->destroyed_ports == s->nports) { |
| 146 | gpr_mu_unlock(&s->mu); |
| 147 | finish_shutdown(exec_ctx, s); |
| 148 | } else { |
| 149 | gpr_mu_unlock(&s->mu); |
| 150 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 153 | /* called when all listening endpoints have been shutdown, so no further |
| 154 | events will be received on them - at this point it's safe to destroy |
| 155 | things */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | static void deactivated_all_ports(grpc_exec_ctx *exec_ctx, grpc_udp_server *s) { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 157 | size_t i; |
| 158 | |
| 159 | /* delete ALL the things */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 160 | gpr_mu_lock(&s->mu); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 161 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | if (!s->shutdown) { |
| 163 | gpr_mu_unlock(&s->mu); |
| 164 | return; |
| 165 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 166 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 167 | if (s->nports) { |
| 168 | for (i = 0; i < s->nports; i++) { |
| 169 | server_port *sp = &s->ports[i]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | sp->destroyed_closure.cb = destroyed_port; |
| 171 | sp->destroyed_closure.cb_arg = s; |
Robbie Shade | 9aa6f40 | 2016-05-12 13:28:04 -0400 | [diff] [blame] | 172 | |
Robbie Shade | ce8f977 | 2016-08-01 16:06:00 -0400 | [diff] [blame] | 173 | /* Call the orphan_cb to signal that the FD is about to be closed and |
| 174 | * should no longer be used. */ |
Robbie Shade | 9aa6f40 | 2016-05-12 13:28:04 -0400 | [diff] [blame] | 175 | GPR_ASSERT(sp->orphan_cb); |
| 176 | sp->orphan_cb(sp->emfd); |
| 177 | |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 178 | grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure, NULL, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 179 | "udp_listener_shutdown"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 180 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 181 | gpr_mu_unlock(&s->mu); |
| 182 | } else { |
| 183 | gpr_mu_unlock(&s->mu); |
| 184 | finish_shutdown(exec_ctx, s); |
| 185 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 188 | void grpc_udp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_udp_server *s, |
| 189 | grpc_closure *on_done) { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 190 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 191 | gpr_mu_lock(&s->mu); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 192 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | GPR_ASSERT(!s->shutdown); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 194 | s->shutdown = 1; |
| 195 | |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 196 | s->shutdown_complete = on_done; |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 197 | |
| 198 | /* shutdown all fd's */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | if (s->active_ports) { |
| 200 | for (i = 0; i < s->nports; i++) { |
Robbie Shade | ce8f977 | 2016-08-01 16:06:00 -0400 | [diff] [blame] | 201 | /* Call the orphan_cb to signal that the FD is about to be closed and |
| 202 | * should no longer be used. */ |
| 203 | GPR_ASSERT(sp->orphan_cb); |
| 204 | sp->orphan_cb(sp->emfd); |
| 205 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | grpc_fd_shutdown(exec_ctx, s->ports[i].emfd); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 207 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 208 | gpr_mu_unlock(&s->mu); |
| 209 | } else { |
| 210 | gpr_mu_unlock(&s->mu); |
| 211 | deactivated_all_ports(exec_ctx, s); |
| 212 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* Prepare a recently-created socket for listening. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 216 | static int prepare_socket(int fd, const struct sockaddr *addr, |
| 217 | size_t addr_len) { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 218 | struct sockaddr_storage sockname_temp; |
| 219 | socklen_t sockname_len; |
Robbie Shade | 3f30e63 | 2016-06-08 08:40:56 -0400 | [diff] [blame] | 220 | /* Set send/receive socket buffers to 1 MB */ |
| 221 | int buffer_size_bytes = 1024 * 1024; |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 222 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | if (fd < 0) { |
| 224 | goto error; |
| 225 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 226 | |
Robbie Shade | ae8c64d | 2016-06-30 11:15:36 -0400 | [diff] [blame] | 227 | if (grpc_set_socket_nonblocking(fd, 1) != GRPC_ERROR_NONE) { |
| 228 | gpr_log(GPR_ERROR, "Unable to set nonblocking %d: %s", fd, strerror(errno)); |
| 229 | goto error; |
| 230 | } |
| 231 | if (grpc_set_socket_cloexec(fd, 1) != GRPC_ERROR_NONE) { |
| 232 | gpr_log(GPR_ERROR, "Unable to set cloexec %d: %s", fd, strerror(errno)); |
| 233 | goto error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 234 | } |
Robbie Shade | 6f03dff | 2015-09-01 10:35:39 -0400 | [diff] [blame] | 235 | |
Robbie Shade | ae8c64d | 2016-06-30 11:15:36 -0400 | [diff] [blame] | 236 | if (grpc_set_socket_ip_pktinfo_if_possible(fd) != GRPC_ERROR_NONE) { |
| 237 | gpr_log(GPR_ERROR, "Unable to set ip_pktinfo."); |
| 238 | goto error; |
| 239 | } else if (addr->sa_family == AF_INET6) { |
| 240 | if (grpc_set_socket_ipv6_recvpktinfo_if_possible(fd) != GRPC_ERROR_NONE) { |
| 241 | gpr_log(GPR_ERROR, "Unable to set ipv6_recvpktinfo."); |
| 242 | goto error; |
| 243 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 244 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 245 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 246 | GPR_ASSERT(addr_len < ~(socklen_t)0); |
| 247 | if (bind(fd, addr, (socklen_t)addr_len) < 0) { |
| 248 | char *addr_str; |
| 249 | grpc_sockaddr_to_string(&addr_str, addr, 0); |
| 250 | gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno)); |
| 251 | gpr_free(addr_str); |
| 252 | goto error; |
| 253 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 254 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | sockname_len = sizeof(sockname_temp); |
| 256 | if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) { |
| 257 | goto error; |
| 258 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 259 | |
Robbie Shade | ae8c64d | 2016-06-30 11:15:36 -0400 | [diff] [blame] | 260 | if (grpc_set_socket_sndbuf(fd, buffer_size_bytes) != GRPC_ERROR_NONE) { |
Robbie Shade | 3f30e63 | 2016-06-08 08:40:56 -0400 | [diff] [blame] | 261 | gpr_log(GPR_ERROR, "Failed to set send buffer size to %d bytes", |
yang-g | 773a882 | 2016-06-13 11:09:29 -0700 | [diff] [blame] | 262 | buffer_size_bytes); |
Robbie Shade | 3f30e63 | 2016-06-08 08:40:56 -0400 | [diff] [blame] | 263 | goto error; |
| 264 | } |
| 265 | |
Robbie Shade | ae8c64d | 2016-06-30 11:15:36 -0400 | [diff] [blame] | 266 | if (grpc_set_socket_rcvbuf(fd, buffer_size_bytes) != GRPC_ERROR_NONE) { |
Robbie Shade | 3f30e63 | 2016-06-08 08:40:56 -0400 | [diff] [blame] | 267 | gpr_log(GPR_ERROR, "Failed to set receive buffer size to %d bytes", |
yang-g | 773a882 | 2016-06-13 11:09:29 -0700 | [diff] [blame] | 268 | buffer_size_bytes); |
Robbie Shade | 3f30e63 | 2016-06-08 08:40:56 -0400 | [diff] [blame] | 269 | goto error; |
| 270 | } |
| 271 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 272 | return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 273 | |
| 274 | error: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 275 | if (fd >= 0) { |
| 276 | close(fd); |
| 277 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | /* event manager callback when reads are ready */ |
Nicolas "Pixel" Noble | f55adec | 2016-07-14 02:20:16 +0200 | [diff] [blame] | 282 | static void on_read(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 283 | server_port *sp = arg; |
| 284 | |
Robbie Shade | 7f6864a | 2016-06-30 15:11:28 -0400 | [diff] [blame] | 285 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 286 | gpr_mu_lock(&sp->server->mu); |
| 287 | if (0 == --sp->server->active_ports) { |
| 288 | gpr_mu_unlock(&sp->server->mu); |
| 289 | deactivated_all_ports(exec_ctx, sp->server); |
| 290 | } else { |
| 291 | gpr_mu_unlock(&sp->server->mu); |
Robbie Shade | f2eb48a | 2015-08-04 19:19:36 -0400 | [diff] [blame] | 292 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 293 | return; |
| 294 | } |
Robbie Shade | f2eb48a | 2015-08-04 19:19:36 -0400 | [diff] [blame] | 295 | |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 296 | /* Tell the registered callback that data is available to read. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 297 | GPR_ASSERT(sp->read_cb); |
Robbie Shade | c6787b2 | 2015-10-07 10:13:53 -0400 | [diff] [blame] | 298 | sp->read_cb(exec_ctx, sp->emfd, sp->server->grpc_server); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 299 | |
| 300 | /* Re-arm the notification event so we get another chance to read. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 301 | grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 302 | } |
| 303 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 304 | static int add_socket_to_server(grpc_udp_server *s, int fd, |
| 305 | const struct sockaddr *addr, size_t addr_len, |
Robbie Shade | 9aa6f40 | 2016-05-12 13:28:04 -0400 | [diff] [blame] | 306 | grpc_udp_server_read_cb read_cb, |
| 307 | grpc_udp_server_orphan_cb orphan_cb) { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 308 | server_port *sp; |
| 309 | int port; |
| 310 | char *addr_str; |
| 311 | char *name; |
| 312 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 313 | port = prepare_socket(fd, addr, addr_len); |
| 314 | if (port >= 0) { |
| 315 | grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1); |
| 316 | gpr_asprintf(&name, "udp-server-listener:%s", addr_str); |
Craig Tiller | 7db24e0 | 2015-09-24 23:44:07 -0700 | [diff] [blame] | 317 | gpr_free(addr_str); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 318 | gpr_mu_lock(&s->mu); |
| 319 | /* append it to the list under a lock */ |
| 320 | if (s->nports == s->port_capacity) { |
| 321 | s->port_capacity *= 2; |
| 322 | s->ports = gpr_realloc(s->ports, sizeof(server_port) * s->port_capacity); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 323 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 324 | sp = &s->ports[s->nports++]; |
| 325 | sp->server = s; |
| 326 | sp->fd = fd; |
| 327 | sp->emfd = grpc_fd_create(fd, name); |
| 328 | memcpy(sp->addr.untyped, addr, addr_len); |
| 329 | sp->addr_len = addr_len; |
| 330 | sp->read_cb = read_cb; |
Robbie Shade | 9aa6f40 | 2016-05-12 13:28:04 -0400 | [diff] [blame] | 331 | sp->orphan_cb = orphan_cb; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 332 | GPR_ASSERT(sp->emfd); |
| 333 | gpr_mu_unlock(&s->mu); |
Craig Tiller | 7db24e0 | 2015-09-24 23:44:07 -0700 | [diff] [blame] | 334 | gpr_free(name); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 335 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 336 | |
| 337 | return port; |
| 338 | } |
| 339 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 340 | int grpc_udp_server_add_port(grpc_udp_server *s, const void *addr, |
Robbie Shade | 9aa6f40 | 2016-05-12 13:28:04 -0400 | [diff] [blame] | 341 | size_t addr_len, grpc_udp_server_read_cb read_cb, |
| 342 | grpc_udp_server_orphan_cb orphan_cb) { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 343 | int allocated_port1 = -1; |
| 344 | int allocated_port2 = -1; |
| 345 | unsigned i; |
| 346 | int fd; |
| 347 | grpc_dualstack_mode dsmode; |
| 348 | struct sockaddr_in6 addr6_v4mapped; |
| 349 | struct sockaddr_in wild4; |
| 350 | struct sockaddr_in6 wild6; |
| 351 | struct sockaddr_in addr4_copy; |
| 352 | struct sockaddr *allocated_addr = NULL; |
| 353 | struct sockaddr_storage sockname_temp; |
| 354 | socklen_t sockname_len; |
| 355 | int port; |
| 356 | |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 357 | /* Check if this is a wildcard port, and if so, try to keep the port the same |
| 358 | as some previously created listener. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 359 | if (grpc_sockaddr_get_port(addr) == 0) { |
| 360 | for (i = 0; i < s->nports; i++) { |
| 361 | sockname_len = sizeof(sockname_temp); |
| 362 | if (0 == getsockname(s->ports[i].fd, (struct sockaddr *)&sockname_temp, |
| 363 | &sockname_len)) { |
| 364 | port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
| 365 | if (port > 0) { |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 366 | allocated_addr = gpr_malloc(addr_len); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 367 | memcpy(allocated_addr, addr, addr_len); |
| 368 | grpc_sockaddr_set_port(allocated_addr, port); |
| 369 | addr = allocated_addr; |
| 370 | break; |
| 371 | } |
| 372 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 373 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 374 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 375 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 376 | if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) { |
| 377 | addr = (const struct sockaddr *)&addr6_v4mapped; |
| 378 | addr_len = sizeof(addr6_v4mapped); |
| 379 | } |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 380 | |
| 381 | /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 382 | if (grpc_sockaddr_is_wildcard(addr, &port)) { |
| 383 | grpc_sockaddr_make_wildcards(port, &wild4, &wild6); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 384 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 385 | /* Try listening on IPv6 first. */ |
| 386 | addr = (struct sockaddr *)&wild6; |
| 387 | addr_len = sizeof(wild6); |
Robbie Shade | 7e8b417 | 2016-06-30 15:06:12 -0400 | [diff] [blame] | 388 | // TODO(rjshade): Test and propagate the returned grpc_error*: |
| 389 | grpc_create_dualstack_socket(addr, SOCK_DGRAM, IPPROTO_UDP, &dsmode, &fd); |
Robbie Shade | 9aa6f40 | 2016-05-12 13:28:04 -0400 | [diff] [blame] | 390 | allocated_port1 = |
| 391 | add_socket_to_server(s, fd, addr, addr_len, read_cb, orphan_cb); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 392 | if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) { |
| 393 | goto done; |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 394 | } |
| 395 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 396 | /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */ |
| 397 | if (port == 0 && allocated_port1 > 0) { |
| 398 | grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 399 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 400 | addr = (struct sockaddr *)&wild4; |
| 401 | addr_len = sizeof(wild4); |
| 402 | } |
| 403 | |
Robbie Shade | 7e8b417 | 2016-06-30 15:06:12 -0400 | [diff] [blame] | 404 | // TODO(rjshade): Test and propagate the returned grpc_error*: |
| 405 | grpc_create_dualstack_socket(addr, SOCK_DGRAM, IPPROTO_UDP, &dsmode, &fd); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 406 | if (fd < 0) { |
| 407 | gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno)); |
| 408 | } |
| 409 | if (dsmode == GRPC_DSMODE_IPV4 && |
| 410 | grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) { |
| 411 | addr = (struct sockaddr *)&addr4_copy; |
| 412 | addr_len = sizeof(addr4_copy); |
| 413 | } |
Robbie Shade | 9aa6f40 | 2016-05-12 13:28:04 -0400 | [diff] [blame] | 414 | allocated_port2 = |
| 415 | add_socket_to_server(s, fd, addr, addr_len, read_cb, orphan_cb); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 416 | |
| 417 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 418 | gpr_free(allocated_addr); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 419 | return allocated_port1 >= 0 ? allocated_port1 : allocated_port2; |
| 420 | } |
| 421 | |
Vijay Pai | 7b080ba | 2015-09-29 22:22:36 +0000 | [diff] [blame] | 422 | int grpc_udp_server_get_fd(grpc_udp_server *s, unsigned port_index) { |
| 423 | return (port_index < s->nports) ? s->ports[port_index].fd : -1; |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 424 | } |
| 425 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 426 | void grpc_udp_server_start(grpc_exec_ctx *exec_ctx, grpc_udp_server *s, |
Robbie Shade | 147fe70 | 2015-09-25 15:04:40 -0400 | [diff] [blame] | 427 | grpc_pollset **pollsets, size_t pollset_count, |
Robbie Shade | 109a8dc | 2015-09-25 15:18:49 -0400 | [diff] [blame] | 428 | grpc_server *server) { |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 429 | size_t i, j; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 430 | gpr_mu_lock(&s->mu); |
| 431 | GPR_ASSERT(s->active_ports == 0); |
Robbie Shade | 1cef6dc | 2015-07-15 14:16:48 -0400 | [diff] [blame] | 432 | s->pollsets = pollsets; |
Robbie Shade | 109a8dc | 2015-09-25 15:18:49 -0400 | [diff] [blame] | 433 | s->grpc_server = server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 434 | for (i = 0; i < s->nports; i++) { |
| 435 | for (j = 0; j < pollset_count; j++) { |
| 436 | grpc_pollset_add_fd(exec_ctx, pollsets[j], s->ports[i].emfd); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 437 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 438 | s->ports[i].read_closure.cb = on_read; |
| 439 | s->ports[i].read_closure.cb_arg = &s->ports[i]; |
| 440 | grpc_fd_notify_on_read(exec_ctx, s->ports[i].emfd, |
| 441 | &s->ports[i].read_closure); |
| 442 | s->active_ports++; |
| 443 | } |
| 444 | gpr_mu_unlock(&s->mu); |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Robbie Shade | a4e21a1 | 2015-07-10 14:16:31 -0400 | [diff] [blame] | 447 | #endif |