sub init
{
+ require IO::Socket::IP;
my $class = shift;
my ($filter,
$execute,
$debug,
$isdtls) = @_;
+ my $test_client_port;
+
+ # Sometimes, our random selection of client ports gets unlucky
+ # And we randomly select a port thats already in use. This causes
+ # this test to fail, so lets harden ourselves against that by doing
+ # a test bind to the randomly selected port, and only continue once we
+ # find a port thats available.
+ my $test_client_addr = $have_IPv6 ? "[::1]" : "127.0.0.1";
+ my $found_port = 0;
+ for (my $i = 0; $i <= 10; $i++) {
+ $test_client_port = 49152 + int(rand(65535 - 49152));
+ my $test_sock;
+ if ($have_IPv6) {
+ $test_sock = IO::Socket::IP->new(Family => AF_INET6,
+ LocalPort => $test_client_port,
+ LocalAddr => $test_client_addr);
+ } else {
+ $test_sock = IO::Socket::IP->new(Family => AF_INET,
+ LocalPort => $test_client_port,
+ LocalAddr => $test_client_addr);
+ }
+ if ($test_sock) {
+ $found_port = 1;
+ $test_sock->close();
+ print "Found available client port ${test_client_port}\n";
+ last;
+ }
+ print "Port ${test_client_port} in use. Trying again\n";
+ }
+
+ if ($found_port == 0) {
+ die "Unable to find usable port for TLSProxy";
+ }
+
my $self = {
#Public read/write
- proxy_addr => $have_IPv6 ? "[::1]" : "127.0.0.1",
- client_addr => $have_IPv6 ? "[::1]" : "127.0.0.1",
+ proxy_addr => $test_client_addr,
+ client_addr => $test_client_addr,
filter => $filter,
serverflags => "",
clientflags => "",
#Public read
isdtls => $isdtls,
proxy_port => 0,
- client_port => 49152 + int(rand(65535 - 49152)),
+ client_port => $test_client_port,
server_port => 0,
serverpid => 0,
clientpid => 0,