MCPcopy Create free account
hub / github.com/apache/impala / FindUnusedEphemeralPort

Function FindUnusedEphemeralPort

be/src/util/network-util.cc:305–327  ·  view source on GitHub ↗

Pick a random port in the range of ephemeral ports https://tools.ietf.org/html/rfc6335

Source from the content-addressed store, hash-verified

303/// Pick a random port in the range of ephemeral ports
304/// https://tools.ietf.org/html/rfc6335
305int FindUnusedEphemeralPort() {
306 static uint32_t LOWER = 49152, UPPER = 65000;
307 random_device rd;
308 srand(rd());
309
310 int sockfd = socket(AF_INET, SOCK_STREAM, 0);
311 if (sockfd < 0) return -1;
312 struct sockaddr_in server_address;
313 bzero(reinterpret_cast<char*>(&server_address), sizeof(server_address));
314 server_address.sin_family = AF_INET;
315 server_address.sin_addr.s_addr = INADDR_ANY;
316 for (int tries = 0; tries < 100; ++tries) {
317 int port = LOWER + rand() % (UPPER - LOWER);
318 server_address.sin_port = htons(port);
319 if (bind(sockfd, reinterpret_cast<struct sockaddr*>(&server_address),
320 sizeof(server_address)) == 0) {
321 close(sockfd);
322 return port;
323 }
324 }
325 close(sockfd);
326 return -1;
327}
328
329Status NetworkAddressPBToSockaddr(
330 const NetworkAddressPB& address, bool use_uds, kudu::Sockaddr* sockaddr) {

Callers 8

TESTFunction · 0.85
GetServerPortFunction · 0.85
mainFunction · 0.85
SetUpMethod · 0.85
TEST_PFunction · 0.85
TEST_PFunction · 0.85
mainFunction · 0.85

Calls 2

bzeroFunction · 0.85
bindFunction · 0.85

Tested by 7

TESTFunction · 0.68
GetServerPortFunction · 0.68
mainFunction · 0.68
SetUpMethod · 0.68
TEST_PFunction · 0.68
TEST_PFunction · 0.68
mainFunction · 0.68