diff --git a/man/mosh-server.1 b/man/mosh-server.1 index cac571a..92bbe1a 100644 --- a/man/mosh-server.1 +++ b/man/mosh-server.1 @@ -70,7 +70,8 @@ IP address of the local interface to bind (for multihomed hosts) .TP .B \-p \fIPORT\fP[:\fIPORT2\fP] -UDP port number or port-range to bind +UDP port number or port-range to bind. \fB\-p 0\fP will let the +operating system pick an available UDP port. .TP .B \-c \fICOLORS\fP diff --git a/man/mosh.1 b/man/mosh.1 index 12140bd..b2e1df0 100644 --- a/man/mosh.1 +++ b/man/mosh.1 @@ -183,9 +183,12 @@ Synonym for \-\-family=inet6 Use a particular server-side UDP port or port range, for example, if this is the only port that is forwarded through a firewall to the -server. Otherwise, \fBmosh\fP will choose a port between 60000 and +server. With \fB\-p 0\fP, the server will let the operating system pick an +available UDP port. Otherwise, \fBmosh\fP will choose a port between 60000 and 61000. + + .TP .B \-\-bind\-server={ssh|any|\fIIP\fP} Control the IP address that the \fBmosh-server\fP binds to. diff --git a/scripts/mosh.pl b/scripts/mosh.pl index c511482..45d5c4c 100755 --- a/scripts/mosh.pl +++ b/scripts/mosh.pl @@ -187,13 +187,16 @@ if ( defined $port_request ) { if ( $port_request =~ m{^(\d+)(:(\d+))?$} ) { my ( $low, $clause, $high ) = ( $1, $2, $3 ); # good port or port-range - if ( $low <= 0 or $low > 65535 ) { - die "$0: Server-side (low) port ($low) must be within valid range [1..65535].\n"; + if ( $low < 0 or $low > 65535 ) { + die "$0: Server-side (low) port ($low) must be within valid range [0..65535].\n"; } if ( defined $high ) { if ( $high <= 0 or $high > 65535 ) { die "$0: Server-side high port ($high) must be within valid range [1..65535].\n"; } + if ( $low == 0 ) { + die "$0: Server-side port ranges may not be used with starting port 0 ($port_request).\n"; + } if ( $low > $high ) { die "$0: Server-side port range ($port_request): low port greater than high port.\n"; } diff --git a/src/network/network.cc b/src/network/network.cc index 6a6c72b..0f595d7 100644 --- a/src/network/network.cc +++ b/src/network/network.cc @@ -261,8 +261,8 @@ Connection::Connection( const char *desired_ip, const char *desired_port ) /* se try INADDR_ANY. If a port request is given, we bind only to that port. */ /* convert port numbers */ - int desired_port_low = 0; - int desired_port_high = 0; + int desired_port_low = -1; + int desired_port_high = -1; if ( desired_port && !parse_portrange( desired_port, desired_port_low, desired_port_high ) ) { throw NetworkException("Invalid port range", 0); @@ -307,10 +307,10 @@ bool Connection::try_bind( const char *addr, int port_low, int port_high ) int search_low = PORT_RANGE_LOW, search_high = PORT_RANGE_HIGH; - if ( port_low != 0 ) { /* low port preference */ + if ( port_low != -1 ) { /* low port preference */ search_low = port_low; } - if ( port_high != 0 ) { /* high port preference */ + if ( port_high != -1 ) { /* high port preference */ search_high = port_high; } @@ -688,7 +688,6 @@ bool Connection::parse_portrange( const char * desired_port, int & desired_port_ desired_port_high = desired_port_low; return true; } - /* port range; parse high port */ const char * cp = end + 1; errno = 0; @@ -708,5 +707,11 @@ bool Connection::parse_portrange( const char * desired_port, int & desired_port_ return false; } + if ( desired_port_low == 0 ) { + fprintf( stderr, "Low port 0 incompatible with port ranges\n" ); + return false; + } + + return true; }