Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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 | |
| 34 | #include <grpc/support/port_platform.h> |
| 35 | #include "test/core/util/test_config.h" |
| 36 | #if defined(GPR_WINSOCK_SOCKET) && defined(GRPC_TEST_PICK_PORT) |
| 37 | |
| 38 | #include "test/core/util/port.h" |
| 39 | |
Craig Tiller | f726205 | 2015-04-08 16:13:58 -0700 | [diff] [blame] | 40 | #include <process.h> |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 41 | #include <stdio.h> |
| 42 | #include <errno.h> |
| 43 | #include <string.h> |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 44 | |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 45 | #include <grpc/grpc.h> |
| 46 | #include <grpc/support/alloc.h> |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 47 | #include <grpc/support/log.h> |
| 48 | |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 49 | #include "src/core/support/env.h" |
| 50 | #include "src/core/httpcli/httpcli.h" |
| 51 | #include "src/core/iomgr/sockaddr_utils.h" |
| 52 | |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 53 | #define NUM_RANDOM_PORTS_TO_PICK 100 |
| 54 | |
Craig Tiller | 4326f73 | 2015-09-02 12:36:25 -0700 | [diff] [blame] | 55 | static int *chosen_ports = NULL; |
| 56 | static size_t num_chosen_ports = 0; |
| 57 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 58 | static int has_port_been_chosen(int port) { |
Craig Tiller | 4326f73 | 2015-09-02 12:36:25 -0700 | [diff] [blame] | 59 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 60 | for (i = 0; i < num_chosen_ports; i++) { |
| 61 | if (chosen_ports[i] == port) { |
| 62 | return 1; |
Craig Tiller | 4326f73 | 2015-09-02 12:36:25 -0700 | [diff] [blame] | 63 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 64 | } |
Craig Tiller | 4326f73 | 2015-09-02 12:36:25 -0700 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 68 | static void free_chosen_ports(void) { gpr_free(chosen_ports); } |
Craig Tiller | 4326f73 | 2015-09-02 12:36:25 -0700 | [diff] [blame] | 69 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | static void chose_port(int port) { |
| 71 | if (chosen_ports == NULL) { |
| 72 | atexit(free_chosen_ports); |
| 73 | } |
Craig Tiller | 4326f73 | 2015-09-02 12:36:25 -0700 | [diff] [blame] | 74 | num_chosen_ports++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 75 | chosen_ports = gpr_realloc(chosen_ports, sizeof(int) * num_chosen_ports); |
Craig Tiller | 4326f73 | 2015-09-02 12:36:25 -0700 | [diff] [blame] | 76 | chosen_ports[num_chosen_ports - 1] = port; |
| 77 | } |
| 78 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 79 | static int is_port_available(int *port, int is_tcp) { |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 80 | const int proto = is_tcp ? IPPROTO_TCP : 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 81 | const SOCKET fd = socket(AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto); |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 82 | int one = 1; |
| 83 | struct sockaddr_in addr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | socklen_t alen = sizeof(addr); |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 85 | int actual_port; |
| 86 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 87 | GPR_ASSERT(*port >= 0); |
| 88 | GPR_ASSERT(*port <= 65535); |
| 89 | if (INVALID_SOCKET == fd) { |
| 90 | gpr_log(GPR_ERROR, "socket() failed: %s", strerror(errno)); |
| 91 | return 0; |
| 92 | } |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 93 | |
| 94 | /* Reuseaddr lets us start up a server immediately after it exits */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, |
| 96 | sizeof(one)) < 0) { |
| 97 | gpr_log(GPR_ERROR, "setsockopt() failed: %s", strerror(errno)); |
| 98 | closesocket(fd); |
| 99 | return 0; |
| 100 | } |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 101 | |
| 102 | /* Try binding to port */ |
| 103 | addr.sin_family = AF_INET; |
| 104 | addr.sin_addr.s_addr = INADDR_ANY; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | addr.sin_port = htons(*port); |
| 106 | if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 107 | gpr_log(GPR_DEBUG, "bind(port=%d) failed: %s", *port, strerror(errno)); |
| 108 | closesocket(fd); |
| 109 | return 0; |
| 110 | } |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 111 | |
| 112 | /* Get the bound port number */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | if (getsockname(fd, (struct sockaddr *)&addr, &alen) < 0) { |
| 114 | gpr_log(GPR_ERROR, "getsockname() failed: %s", strerror(errno)); |
| 115 | closesocket(fd); |
| 116 | return 0; |
| 117 | } |
| 118 | GPR_ASSERT(alen <= sizeof(addr)); |
| 119 | actual_port = ntohs(addr.sin_port); |
| 120 | GPR_ASSERT(actual_port > 0); |
| 121 | if (*port == 0) { |
| 122 | *port = actual_port; |
| 123 | } else { |
| 124 | GPR_ASSERT(*port == actual_port); |
| 125 | } |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 126 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | closesocket(fd); |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 128 | return 1; |
| 129 | } |
| 130 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | typedef struct portreq { |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 132 | grpc_pollset pollset; |
| 133 | int port; |
| 134 | } portreq; |
| 135 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 136 | static void got_port_from_server(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | const grpc_httpcli_response *response) { |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 138 | size_t i; |
| 139 | int port = 0; |
| 140 | portreq *pr = arg; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | GPR_ASSERT(response); |
| 142 | GPR_ASSERT(response->status == 200); |
| 143 | for (i = 0; i < response->body_length; i++) { |
| 144 | GPR_ASSERT(response->body[i] >= '0' && response->body[i] <= '9'); |
| 145 | port = port * 10 + response->body[i] - '0'; |
| 146 | } |
| 147 | GPR_ASSERT(port > 1024); |
| 148 | gpr_mu_lock(GRPC_POLLSET_MU(&pr->pollset)); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 149 | pr->port = port; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 150 | grpc_pollset_kick(&pr->pollset, NULL); |
| 151 | gpr_mu_unlock(GRPC_POLLSET_MU(&pr->pollset)); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 154 | static void destroy_pollset_and_shutdown(grpc_exec_ctx *exec_ctx, void *p, int success) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | grpc_pollset_destroy(p); |
| 156 | grpc_shutdown(); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | static int pick_port_using_server(char *server) { |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 160 | grpc_httpcli_context context; |
| 161 | grpc_httpcli_request req; |
| 162 | portreq pr; |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 163 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 164 | grpc_closure destroy_pollset_closure; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 165 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 166 | grpc_init(); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 167 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 168 | memset(&pr, 0, sizeof(pr)); |
| 169 | memset(&req, 0, sizeof(req)); |
| 170 | grpc_pollset_init(&pr.pollset); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 171 | pr.port = -1; |
| 172 | |
| 173 | req.host = server; |
| 174 | req.path = "/get"; |
| 175 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | grpc_httpcli_context_init(&context); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 177 | grpc_httpcli_get(&exec_ctx, &context, &pr.pollset, &req, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 178 | GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server, |
| 179 | &pr); |
| 180 | gpr_mu_lock(GRPC_POLLSET_MU(&pr.pollset)); |
| 181 | while (pr.port == -1) { |
| 182 | grpc_pollset_worker worker; |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 183 | grpc_pollset_work(&exec_ctx, &pr.pollset, &worker, gpr_now(GPR_CLOCK_MONOTONIC), |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 184 | GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1)); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 185 | gpr_mu_unlock(GRPC_POLLSET_MU(&pr.pollset)); |
| 186 | grpc_exec_ctx_flush(&exec_ctx); |
| 187 | gpr_mu_lock(GRPC_POLLSET_MU(&pr.pollset)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 188 | } |
| 189 | gpr_mu_unlock(GRPC_POLLSET_MU(&pr.pollset)); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 190 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 191 | grpc_httpcli_context_destroy(&context); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 192 | grpc_closure_init(&destroy_pollset_closure, destroy_pollset_and_shutdown, &pr.pollset); |
| 193 | grpc_pollset_shutdown(&exec_ctx, &pr.pollset, &destroy_pollset_closure); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 194 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 195 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 196 | return pr.port; |
| 197 | } |
| 198 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | int grpc_pick_unused_port(void) { |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 200 | /* We repeatedly pick a port and then see whether or not it is |
| 201 | available for use both as a TCP socket and a UDP socket. First, we |
| 202 | pick a random large port number. For subsequent |
| 203 | iterations, we bind to an anonymous port and let the OS pick the |
| 204 | port number. The random port picking reduces the probability of |
| 205 | races with other processes on kernels that want to reuse the same |
| 206 | port numbers over and over. */ |
| 207 | |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 208 | /* In alternating iterations we trial UDP ports before TCP ports UDP |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 209 | ports -- it could be the case that this machine has been using up |
| 210 | UDP ports and they are scarcer. */ |
| 211 | |
| 212 | /* Type of port to first pick in next iteration */ |
| 213 | int is_tcp = 1; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 214 | int trial = 0; |
| 215 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 216 | char *env = gpr_getenv("GRPC_TEST_PORT_SERVER"); |
| 217 | if (env) { |
| 218 | int port = pick_port_using_server(env); |
| 219 | gpr_free(env); |
| 220 | if (port != 0) { |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 221 | return port; |
| 222 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | for (;;) { |
| 226 | int port; |
| 227 | trial++; |
| 228 | if (trial == 1) { |
| 229 | port = _getpid() % (65536 - 30000) + 30000; |
| 230 | } else if (trial <= NUM_RANDOM_PORTS_TO_PICK) { |
| 231 | port = rand() % (65536 - 30000) + 30000; |
| 232 | } else { |
| 233 | port = 0; |
| 234 | } |
| 235 | |
| 236 | if (has_port_been_chosen(port)) { |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | if (!is_port_available(&port, is_tcp)) { |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | GPR_ASSERT(port > 0); |
| 245 | /* Check that the port # is free for the other type of socket also */ |
| 246 | if (!is_port_available(&port, !is_tcp)) { |
| 247 | /* In the next iteration trial to bind to the other type first |
| 248 | because perhaps it is more rare. */ |
| 249 | is_tcp = !is_tcp; |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | /* TODO(ctiller): consider caching this port in some structure, to avoid |
| 254 | handing it out again */ |
| 255 | |
| 256 | chose_port(port); |
| 257 | return port; |
| 258 | } |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 259 | |
| 260 | /* The port iterator reached the end without finding a suitable port. */ |
| 261 | return 0; |
| 262 | } |
| 263 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | int grpc_pick_unused_port_or_die(void) { |
| 265 | int port = grpc_pick_unused_port(); |
| 266 | GPR_ASSERT(port > 0); |
Craig Tiller | 34cf2f3 | 2015-04-08 16:10:00 -0700 | [diff] [blame] | 267 | return port; |
| 268 | } |
| 269 | |
| 270 | #endif /* GPR_WINSOCK_SOCKET && GRPC_TEST_PICK_PORT */ |