Allow server to bind an OS-selected UDP port with -p 0.

It looks like Mosh used to be able to do this but the capability was
lost when port ranges were added in 141ec23.
This commit is contained in:
John Hood
2016-05-27 17:43:11 -04:00
parent c599987ff8
commit 8945efeb82
4 changed files with 21 additions and 9 deletions
+10 -5
View File
@@ -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;
}