[gPXE-devel] [PATCH 3/6] [tcp] Randomize TCP bind port

cooldavid at cooldavid.org cooldavid at cooldavid.org
Tue Jul 13 11:15:41 EDT 2010


From: Guo-Fu Tseng <cooldavid at cooldavid.org>

To reduce possible TCP port collision.

Signed-off-by: Guo-Fu Tseng <cooldavid at cooldavid.org>
---
 src/net/tcp.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/net/tcp.c b/src/net/tcp.c
index 20bcf51..9241572 100644
--- a/src/net/tcp.c
+++ b/src/net/tcp.c
@@ -176,13 +176,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, try_limit = 64512;
 
+	try_port = ( currticks() % 64512 ) + 1023;
 	/* If no port specified, find the first available port */
 	if ( ! port ) {
-		while ( try_port ) {
-			try_port++;
-			if ( try_port < 1024 )
+		while ( try_limit-- ) {
+			while ( ++try_port < 1024 )
 				continue;
 			if ( tcp_bind ( tcp, htons ( try_port ) ) == 0 )
 				return 0;
-- 
1.7.1



More information about the gPXE-devel mailing list