~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to tests/choose-port.pl

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- perl -*-
2
 
 
3
 
# Picks a random TCP port and attempts to bind it, retrying a few
4
 
# times if the chosen port is in use.  This is better than just
5
 
# picking a random number without checking whether it is in use (but
6
 
# of course a race window still exists).
7
 
#
8
 
# On success, prints a port number on stdout and exits with status 0.
9
 
# On failure, prints an error on stderr and exits with a nonzero status.
10
 
 
11
 
use warnings;
12
 
use strict;
13
 
use Socket;
14
 
 
15
 
socket(SOCK, PF_INET, SOCK_STREAM, 0) || die "socket: $!\n";
16
 
for (my ($i) = 0; ; $i++) {
17
 
    my ($port) = int(rand(16383)) + 49152;
18
 
    if (bind(SOCK, sockaddr_in($port, INADDR_ANY))) {
19
 
        print "$port\n";
20
 
        exit 0;
21
 
    } elsif ($i < 10 && $!{EADDRINUSE}) {
22
 
        # Address already in use.  Try again.
23
 
    } else {
24
 
        die "bind: $!\n";
25
 
    }
26
 
}