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

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

commit a50d365c6a2573029df9e3cc8188a0891e7eac71
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 11 04:18:35 2026 +0100

    net/tcp: Let listen() again change the backlog of a listening socket.
    
    A second listen() on a listening TCP socket first replaced its backlog
    (tcp_backlogcreate() freed the old queue and the connections in it),
    then failed with EADDRINUSE: tcp_listen() found the socket itself in
    the listener table.  The connections that waited in the queue were
    lost.  Linux returns 0 and sets the new backlog.
    
    tcp_listen() now returns OK when the listener it finds is the socket
    itself, and tcp_backlogcreate() moves the waiting connections to the
    new backlog, as many as it holds.  tcp_islistener() has no caller left
    and is removed.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp.h         | 18 ------------------
 net/tcp/tcp_backlog.c | 14 +++++++++++++-
 net/tcp/tcp_listen.c  | 36 ++++++++++--------------------------
 3 files changed, 23 insertions(+), 45 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 22332f104e3..5ab1ddf006a 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -1161,24 +1161,6 @@ int tcp_unlisten(FAR struct tcp_conn_s *conn);
 
 int tcp_listen(FAR struct tcp_conn_s *conn);
 
-/****************************************************************************
- * Name: tcp_islistener
- *
- * Description:
- *   Return true is there is a listener for the specified port
- *
- * Assumptions:
- *   Called from network stack logic with the network stack locked
- *
- ****************************************************************************/
-
-#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
-bool tcp_islistener(FAR union ip_binding_u *uaddr, uint16_t portno,
-                    uint8_t domain);
-#else
-bool tcp_islistener(FAR union ip_binding_u *uaddr, uint16_t portno);
-#endif
-
 /****************************************************************************
  * Name: tcp_accept_connection
  *
diff --git a/net/tcp/tcp_backlog.c b/net/tcp/tcp_backlog.c
index d5144e2566a..e9ab1360a8a 100644
--- a/net/tcp/tcp_backlog.c
+++ b/net/tcp/tcp_backlog.c
@@ -60,6 +60,7 @@ int tcp_backlogcreate(FAR struct tcp_conn_s *conn, int nblg)
 {
   FAR struct tcp_backlog_s     *bls = NULL;
   FAR struct tcp_blcontainer_s *blc;
+  FAR struct tcp_conn_s        *blconn;
   int size;
   int offset;
   int i;
@@ -117,9 +118,20 @@ int tcp_backlogcreate(FAR struct tcp_conn_s *conn, int 
nblg)
         }
     }
 
-  /* Destroy any existing backlog (shouldn't be any) */
+  /* listen() again changes the backlog: move the connections that wait in
+   * the old backlog to the new one, as many as it holds.  Then destroy the
+   * old backlog.
+   */
 
   conn_lock(&conn->sconn);
+  while (bls != NULL && !sq_empty(&bls->bl_free) &&
+         (blconn = tcp_backlogremove(conn)) != NULL)
+    {
+      blc = (FAR struct tcp_blcontainer_s *)sq_remfirst(&bls->bl_free);
+      blc->bc_conn = blconn;
+      sq_addlast(&blc->bc_node, &bls->bl_pending);
+    }
+
   tcp_backlogdestroy(conn);
 
   /* Now install the backlog tear-off in the connection.  NOTE that bls may
diff --git a/net/tcp/tcp_listen.c b/net/tcp/tcp_listen.c
index c47e6a978be..db7342a98d0 100644
--- a/net/tcp/tcp_listen.c
+++ b/net/tcp/tcp_listen.c
@@ -168,6 +168,7 @@ int tcp_unlisten(FAR struct tcp_conn_s *conn)
 
 int tcp_listen(FAR struct tcp_conn_s *conn)
 {
+  FAR struct tcp_conn_s *listener;
   int ndx;
   int ret;
 
@@ -180,10 +181,17 @@ int tcp_listen(FAR struct tcp_conn_s *conn)
   /* First, check if there is already a socket listening on this port */
 
 #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
-  if (tcp_islistener(&conn->u, conn->lport, conn->domain))
+  listener = tcp_findlistener(&conn->u, conn->lport, conn->domain);
 #else
-  if (tcp_islistener(&conn->u, conn->lport))
+  listener = tcp_findlistener(&conn->u, conn->lport);
 #endif
+  if (listener == conn)
+    {
+      /* listen() again on a listening socket only changes the backlog */
+
+      ret = OK;
+    }
+  else if (listener != NULL)
     {
       /* Yes, then we must refuse this request */
 
@@ -218,30 +226,6 @@ int tcp_listen(FAR struct tcp_conn_s *conn)
   return ret;
 }
 
-/****************************************************************************
- * Name: tcp_islistener
- *
- * Description:
- *   Return true is there is a listener for the specified port
- *
- * Assumptions:
- *   This function is called from network logic with the network locked.
- *
- ****************************************************************************/
-
-#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
-bool tcp_islistener(FAR union ip_binding_u *uaddr, uint16_t portno,
-                    uint8_t domain)
-{
-  return tcp_findlistener(uaddr, portno, domain) != NULL;
-}
-#else
-bool tcp_islistener(FAR union ip_binding_u *uaddr, uint16_t portno)
-{
-  return tcp_findlistener(uaddr, portno) != NULL;
-}
-#endif
-
 /****************************************************************************
  * Name: tcp_accept_connection
  *

Reply via email to