[gPXE-devel] [PATCHv2 3/6] [tcp] Randomize TCP bind port
cooldavid at cooldavid.org
cooldavid at cooldavid.org
Wed Jul 14 01:25:04 EDT 2010
From: Guo-Fu Tseng <cooldavid at cooldavid.org>
To reduce possible TCP port collision.
Referened: Michael Brown <mcb30 at ipxe.org>
Signed-off-by: Guo-Fu Tseng <cooldavid at cooldavid.org>
---
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
More information about the gPXE-devel
mailing list