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

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

commit b167fefcf2c328174cab53e9e8155198529cb729
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 08:35:55 2026 +0100

    net/tcp: Give the error of Linux in SO_ERROR for a reset after a FIN.
    
    A RST that came after the FIN of the peer (CLOSE_WAIT, LAST_ACK) left
    SO_ERROR at 0 without a poll() waiter.  With a poll() waiter, or for a
    poll() after the RST, SO_ERROR became ECONNREFUSED, the error of a
    refused connection.
    
    Now the RST sets SO_ERROR as Linux does (tcp_reset()): EPIPE in
    CLOSE_WAIT, which agrees with the result of a send() after the FIN of
    the peer, and ECONNRESET in LAST_ACK.  A receive and a send do not
    change.  poll() keeps this error, and gives ECONNREFUSED only while a
    connect() is in progress (_SF_CONNECTING).  SO_ERROR reports the error
    once, as for the other errors.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp_input.c   | 13 ++++++++++++-
 net/tcp/tcp_netpoll.c | 30 ++++++++++++++++++++++++------
 2 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/net/tcp/tcp_input.c b/net/tcp/tcp_input.c
index ecaa34d4b79..a2ed2baca72 100644
--- a/net/tcp/tcp_input.c
+++ b/net/tcp/tcp_input.c
@@ -1161,7 +1161,9 @@ found:
           /* A RST before the FIN of the peer: the next receive or send
            * gives ECONNRESET.  In TCP_SYN_SENT the RST refuses the
            * connection (ECONNREFUSED).  After the FIN of the peer a
-           * receive gives end-of-file and a send EPIPE.
+           * receive gives end-of-file and a send EPIPE.  SO_ERROR gives
+           * EPIPE in TCP_CLOSE_WAIT and ECONNRESET in TCP_LAST_ACK, as
+           * Linux (tcp_reset()).
            */
 
           if ((conn->tcpstateflags & TCP_STATE_MASK) >= TCP_ESTABLISHED &&
@@ -1170,6 +1172,15 @@ found:
               conn->flags |= TCP_RESET | TCP_RESETERR;
               _SO_CONN_SETERRNO(conn, ECONNRESET);
             }
+          else if ((conn->tcpstateflags & TCP_STATE_MASK) ==
+                   TCP_CLOSE_WAIT)
+            {
+              _SO_CONN_SETERRNO(conn, EPIPE);
+            }
+          else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_LAST_ACK)
+            {
+              _SO_CONN_SETERRNO(conn, ECONNRESET);
+            }
 
           nwarn("WARNING: RESET TCP state: TCP_CLOSED\n");
           conn->tcpstateflags = TCP_CLOSED;
diff --git a/net/tcp/tcp_netpoll.c b/net/tcp/tcp_netpoll.c
index ec277cdbd9c..880509af70b 100644
--- a/net/tcp/tcp_netpoll.c
+++ b/net/tcp/tcp_netpoll.c
@@ -143,15 +143,22 @@ static uint32_t tcp_poll_eventhandler(FAR struct 
net_driver_s *dev,
               reason = ECONNRESET;
             }
 
-          /* TCP_ABORT: The remote host has aborted the connection */
+          /* TCP_ABORT: The remote host refused the connection */
 
-          else
+          else if (_SS_ISCONNECTING(info->conn->sconn.s_flags))
             {
-              /* Indicate that remote host refused the connection */
-
               reason = ECONNREFUSED;
             }
 
+          /* TCP_ABORT: a reset after the FIN of the peer.  tcp_input()
+           * set the error.
+           */
+
+          else
+            {
+              reason = info->conn->sconn.s_error;
+            }
+
           _SO_CONN_SETERRNO(info->conn, reason);
 #endif
 
@@ -430,8 +437,19 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct 
pollfd_s *fds)
         }
       else
         {
-          _SO_CONN_SETERRNO(conn, (_SS_ISCLOSED(conn->sconn.s_flags) ?
-                                   ECONNREFUSED : ENOTCONN));
+          /* A timeout, or a refused connection.  For a reset after the
+           * FIN of the peer, tcp_input() set the error.
+           */
+
+          if (!_SS_ISCLOSED(conn->sconn.s_flags))
+            {
+              _SO_CONN_SETERRNO(conn, ENOTCONN);
+            }
+          else if (_SS_ISCONNECTING(conn->sconn.s_flags))
+            {
+              _SO_CONN_SETERRNO(conn, ECONNREFUSED);
+            }
+
           eventset |= POLLERR | POLLHUP;
         }
 

Reply via email to