diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c index 180c5788e7..e7168ce575 100644 --- a/net/tcp/tcp_conn.c +++ b/net/tcp/tcp_conn.c @@ -54,6 +54,7 @@ #include +#include #include #include #include @@ -89,10 +90,6 @@ static dq_queue_t g_free_tcp_connections; static dq_queue_t g_active_tcp_connections; -/* Last port used by a TCP connection connection. */ - -static uint16_t g_last_tcp_port; - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -263,6 +260,20 @@ static FAR struct tcp_conn_s * static int tcp_selectport(uint8_t domain, FAR const union ip_addr_u *ipaddr, uint16_t portno) { + static uint16_t g_last_tcp_port; + + /* Generate port base dynamically */ + + if (g_last_tcp_port == 0) + { + g_last_tcp_port = clock_systime_ticks() % 32000; + + if (g_last_tcp_port < 4096) + { + g_last_tcp_port += 4096; + } + } + if (portno == 0) { /* No local port assigned. Loop until we find a valid listen port @@ -604,8 +615,6 @@ void tcp_initialize(void) g_tcp_connections[i].tcpstateflags = TCP_CLOSED; dq_addlast(&g_tcp_connections[i].node, &g_free_tcp_connections); } - - g_last_tcp_port = 1024; } /**************************************************************************** diff --git a/net/udp/udp_conn.c b/net/udp/udp_conn.c index 45e0f7d6f1..8d12298a28 100644 --- a/net/udp/udp_conn.c +++ b/net/udp/udp_conn.c @@ -54,6 +54,7 @@ #include +#include #include #include #include @@ -90,10 +91,6 @@ static sem_t g_free_sem; static dq_queue_t g_active_udp_connections; -/* Last port used by a UDP connection connection. */ - -static uint16_t g_last_udp_port; - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -197,13 +194,27 @@ static FAR struct udp_conn_s *udp_find_conn(uint8_t domain, static uint16_t udp_select_port(uint8_t domain, FAR union ip_binding_u *u) { + static uint16_t g_last_udp_port; uint16_t portno; + net_lock(); + + /* Generate port base dynamically */ + + if (g_last_udp_port == 0) + { + g_last_udp_port = clock_systime_ticks() % 32000; + + if (g_last_udp_port < 4096) + { + g_last_udp_port += 4096; + } + } + /* Find an unused local port number. Loop until we find a valid * listen port number that is not being used by any other connection. */ - net_lock(); do { /* Guess that the next available port number will be the one after @@ -542,8 +553,6 @@ void udp_initialize(void) g_udp_connections[i].lport = 0; dq_addlast(&g_udp_connections[i].node, &g_free_udp_connections); } - - g_last_udp_port = 1024; } /****************************************************************************