From: Guo-Fu Tseng <[email protected]> To reduce possible TCP port collision.
Referened: Michael Brown <[email protected]> Signed-off-by: Guo-Fu Tseng <[email protected]> --- src/net/tcp.c | 11 ++++++----- 1 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/net/tcp.c b/src/net/tcp.c index 20bcf51..d79dc57 100644 --- a/src/net/tcp.c +++ b/src/net/tcp.c @@ -176,13 +176,14 @@ tcp_dump_flags ( struct tcp_connection *tcp, unsigned int flags ) { */ static int tcp_bind ( struct tcp_connection *tcp, unsigned int port ) { struct tcp_connection *existing; - static uint16_t try_port = 1023; + uint16_t try_port; + int i; - /* If no port specified, find the first available port */ + /* If no port specified, find an available port */ if ( ! port ) { - while ( try_port ) { - try_port++; - if ( try_port < 1024 ) + try_port = ( random() % 64512 ) + 1023; + for ( i = 0 ; i < 65536 ; ++i ) { + if ( ++try_port < 1024 ) continue; if ( tcp_bind ( tcp, htons ( try_port ) ) == 0 ) return 0; -- 1.7.1 _______________________________________________ gPXE-devel mailing list [email protected] http://etherboot.org/mailman/listinfo/gpxe-devel
