From 1e7e4c9a611aca0f1dbb0f64ffe15af941867f87 Mon Sep 17 00:00:00 2001 From: Guo-Fu Tseng Date: Tue, 13 Jul 2010 17:24:01 +0100 Subject: [PATCH] [tcp] Randomise local TCP port Signed-off-by: Guo-Fu Tseng Modified-by: Michael Brown Signed-off-by: Michael Brown --- src/net/tcp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/net/tcp.c b/src/net/tcp.c index 67aa39f21..88411a4d7 100644 --- a/src/net/tcp.c +++ b/src/net/tcp.c @@ -174,11 +174,13 @@ 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; + unsigned int i; - /* If no port specified, find the first available port */ + /* If no port is specified, find an available port */ if ( ! port ) { - while ( try_port ) { + try_port = random(); + for ( i = 0 ; i < 65536 ; i++ ) { try_port++; if ( try_port < 1024 ) continue;