]> git.proxmox.com Git - mirror_ovs.git/blob - tests/choose-port.pl
ofproto-dpif: Fix CONTROLLER actions for LLC frames.
[mirror_ovs.git] / tests / choose-port.pl
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 }