You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/05/15 13:27:52 UTC

[incubator-nuttx] 01/02: net/conn: generate port base dynamically

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 7a62a59dec4448c04013a402e9039baa7bf0ca1e
Author: chao.an <an...@xiaomi.com>
AuthorDate: Fri May 15 14:01:01 2020 +0800

    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 <an...@xiaomi.com>
---
 net/tcp/tcp_conn.c | 21 +++++++++++++++------
 net/udp/udp_conn.c | 23 ++++++++++++++++-------
 2 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index 180c578..e7168ce 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -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;
 }
 
 /****************************************************************************
diff --git a/net/udp/udp_conn.c b/net/udp/udp_conn.c
index 45e0f7d..8d12298 100644
--- a/net/udp/udp_conn.c
+++ b/net/udp/udp_conn.c
@@ -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;
 }
 
 /****************************************************************************