(test) / use a random number generator to get a free port, when the bind to port 0 strategy does not work out
This commit is contained in:
parent
662f66e501
commit
3d8297247e
@ -17,6 +17,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
@ -70,10 +72,12 @@ namespace ix
|
|||||||
Logger() << msg;
|
Logger() << msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getAnyFreePortSimple()
|
int getAnyFreePortRandom()
|
||||||
{
|
{
|
||||||
static int defaultPort = 8090;
|
std::random_device rd;
|
||||||
return defaultPort++;
|
std::uniform_int_distribution<int> dist(1024 + 1, 65535);
|
||||||
|
|
||||||
|
return dist(rd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int getAnyFreePort()
|
int getAnyFreePort()
|
||||||
@ -83,7 +87,7 @@ namespace ix
|
|||||||
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||||
{
|
{
|
||||||
log("Cannot compute a free port. socket error.");
|
log("Cannot compute a free port. socket error.");
|
||||||
return defaultPort;
|
return getAnyFreePortRandom();
|
||||||
}
|
}
|
||||||
|
|
||||||
int enable = 1;
|
int enable = 1;
|
||||||
@ -91,7 +95,7 @@ namespace ix
|
|||||||
(char*) &enable, sizeof(enable)) < 0)
|
(char*) &enable, sizeof(enable)) < 0)
|
||||||
{
|
{
|
||||||
log("Cannot compute a free port. setsockopt error.");
|
log("Cannot compute a free port. setsockopt error.");
|
||||||
return defaultPort;
|
return getAnyFreePortRandom();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind to port 0. This is the standard way to get a free port.
|
// Bind to port 0. This is the standard way to get a free port.
|
||||||
@ -105,7 +109,7 @@ namespace ix
|
|||||||
log("Cannot compute a free port. bind error.");
|
log("Cannot compute a free port. bind error.");
|
||||||
|
|
||||||
::close(sockfd);
|
::close(sockfd);
|
||||||
return defaultPort;
|
return getAnyFreePortRandom();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_in sa; // server address information
|
struct sockaddr_in sa; // server address information
|
||||||
@ -115,7 +119,7 @@ namespace ix
|
|||||||
log("Cannot compute a free port. getsockname error.");
|
log("Cannot compute a free port. getsockname error.");
|
||||||
|
|
||||||
::close(sockfd);
|
::close(sockfd);
|
||||||
return defaultPort;
|
return getAnyFreePortRandom();
|
||||||
}
|
}
|
||||||
|
|
||||||
int port = ntohs(sa.sin_port);
|
int port = ntohs(sa.sin_port);
|
||||||
@ -130,7 +134,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
#if defined(__has_feature)
|
#if defined(__has_feature)
|
||||||
# if __has_feature(address_sanitizer)
|
# if __has_feature(address_sanitizer)
|
||||||
int port = getAnyFreePortSimple();
|
int port = getAnyFreePortRandom();
|
||||||
# else
|
# else
|
||||||
int port = getAnyFreePort();
|
int port = getAnyFreePort();
|
||||||
# endif
|
# endif
|
||||||
|
Loading…
Reference in New Issue
Block a user