net/conn: generate port base dynamically

In some extreme scenarios(eg. crash, reboot, reset, etc...),
an established connection cannot guarantee that the port can be
closed properly, if we try to reconnect quickly after reset, the
connection will fail since the current port is same as the
previous one, the previous port connection has been hold on server side.

dynamically apply for the port base to avoid duplication.

Change-Id: I0089244b2707ea61f553a4dae09c7af3649c70bd
Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an 2020-05-15 14:01:01 +08:00 committed by patacongo
parent 60b404e076
commit 7a62a59dec
2 changed files with 31 additions and 13 deletions

View File

@ -54,6 +54,7 @@
#include <arch/irq.h>
#include <nuttx/clock.h>
#include <nuttx/net/netconfig.h>
#include <nuttx/net/net.h>
#include <nuttx/net/netdev.h>
@ -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;
}
/****************************************************************************

View File

@ -54,6 +54,7 @@
#include <arch/irq.h>
#include <nuttx/clock.h>
#include <nuttx/semaphore.h>
#include <nuttx/net/netconfig.h>
#include <nuttx/net/net.h>
@ -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;
}
/****************************************************************************