From 6addf24fa2d567560ca3bd394eebe8596304e509 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Tue, 2 Sep 2025 14:18:15 +0800 Subject: [PATCH] net/tcp: replace TCP_CLOSE with TCP_ABORT since we added TCP_TXCLOSE and TCP_RXCLOSE, only RST-like exception flows now trigger TCP_CLOSE events, so replace the remaining TCP_CLOSE with TCP_ABORT. Signed-off-by: zhanghongyu --- net/devif/devif.h | 36 +++++++++++++++--------------------- net/tcp/tcp_callback.c | 8 ++++---- net/tcp/tcp_connect.c | 11 +++++------ net/tcp/tcp_monitor.c | 13 ++++++------- net/tcp/tcp_netpoll.c | 14 ++++++++++---- net/tcp/tcp_recvfrom.c | 17 +---------------- 6 files changed, 41 insertions(+), 58 deletions(-) diff --git a/net/devif/devif.h b/net/devif/devif.h index d59ddde9106..872cf916cf6 100644 --- a/net/devif/devif.h +++ b/net/devif/devif.h @@ -62,8 +62,8 @@ /* The following flags may be set in the set of flags by the lower, device- * interfacing layer before calling through the socket layer callback. The - * TCP_ACKDATA, XYZ_NEWDATA, TCP_CLOSE, TCP_RXCLOSE and TCP_TXCLOSE flags - * may be set at the same time, whereas the others are mutually exclusive. + * TCP_ACKDATA, XYZ_NEWDATA, TCP_RXCLOSE and TCP_TXCLOSE flags may be set + * at the same time, whereas the others are mutually exclusive. * * Connection Specific Events: These are events that may be notified * through callback lists residing in TCP, UDP, or PKT port connection @@ -103,11 +103,6 @@ * up by the listen() command. (TCP only) * OUT: Not used * - * TCP_CLOSE IN: The remote host has closed the connection, thus the - * connection has gone away. (TCP only) - * OUT: The socket layer signals that it wants to close the - * connection. (TCP only) - * * TCP_ABORT IN: The remote host has aborted the connection, thus * the connection has gone away. (TCP only) * OUT: The socket layer signals that it wants to abort the @@ -174,7 +169,7 @@ * OUT: Not used */ -/* Bits 0-12: Connection specific event bits */ +/* Bits 0-11: Connection specific event bits */ #define TCP_ACKDATA (1 << 0) #define TCP_NEWDATA (1 << 1) @@ -197,25 +192,24 @@ #define IEEE802154_POLL TCP_POLL #define WPAN_POLL TCP_POLL #define TCP_BACKLOG (1 << 5) -#define TCP_CLOSE (1 << 6) -#define TCP_ABORT (1 << 7) -#define TCP_CONNECTED (1 << 8) -#define TCP_TIMEDOUT (1 << 9) -#define TCP_WAITALL (1 << 10) -#define TCP_TXCLOSE (1 << 11) -#define TCP_RXCLOSE (1 << 12) +#define TCP_ABORT (1 << 6) +#define TCP_CONNECTED (1 << 7) +#define TCP_TIMEDOUT (1 << 8) +#define TCP_WAITALL (1 << 9) +#define TCP_TXCLOSE (1 << 10) +#define TCP_RXCLOSE (1 << 11) -/* Bit 13: Device specific event bits */ +/* Bit 12: Device specific event bits */ -#define NETDEV_DOWN (1 << 13) +#define NETDEV_DOWN (1 << 12) -/* Bits 14-16: Encoded device specific poll events. Unlike connection +/* Bits 13-15: Encoded device specific poll events. Unlike connection * oriented poll events, device related poll events must distinguish * between what is being polled for since the callbacks all reside in * the same list in the network device structure. */ -#define DEVPOLL_SHIFT (14) +#define DEVPOLL_SHIFT (13) #define DEVPOLL_MASK (7 << DEVPOLL_SHIFT) # define DEVPOLL_NONE (0 << DEVPOLL_SHIFT) # define ARP_POLL (1 << DEVPOLL_SHIFT) @@ -226,11 +220,11 @@ /* The set of events that and implications to the TCP connection state */ #define TCP_CONN_EVENTS \ - (TCP_CLOSE | TCP_ABORT | TCP_CONNECTED | TCP_TIMEDOUT | NETDEV_DOWN | \ + (TCP_ABORT | TCP_CONNECTED | TCP_TIMEDOUT | NETDEV_DOWN | \ TCP_TXCLOSE | TCP_RXCLOSE) #define TCP_DISCONN_EVENTS \ - (TCP_CLOSE | TCP_ABORT | TCP_TIMEDOUT | NETDEV_DOWN) + (TCP_ABORT | TCP_TIMEDOUT | NETDEV_DOWN) /* IPv4/IPv6 Helpers */ diff --git a/net/tcp/tcp_callback.c b/net/tcp/tcp_callback.c index 21aee97b5b6..8f9d5631e9c 100644 --- a/net/tcp/tcp_callback.c +++ b/net/tcp/tcp_callback.c @@ -271,10 +271,10 @@ uint16_t tcp_callback(FAR struct net_driver_s *dev, return 0; } - /* Preserve the TCP_ACKDATA, TCP_CLOSE, TCP_RXCLOSE, TCP_TXCLOSE, and - * TCP_ABORT in the response. These is needed by the network to handle - * responses and buffer state. The TCP_NEWDATA indication will trigger - * the ACK response, but must be explicitly set in the callback. + /* Preserve the TCP_ACKDATA, TCP_RXCLOSE, TCP_TXCLOSE, and TCP_ABORT in the + * response. These is needed by the network to handle responses and buffer + * state. The TCP_NEWDATA indication will trigger the ACK response, but + * must be explicitly set in the callback. */ ninfo("flags: %04x\n", flags); diff --git a/net/tcp/tcp_connect.c b/net/tcp/tcp_connect.c index 79142e8e734..1c3a705c497 100644 --- a/net/tcp/tcp_connect.c +++ b/net/tcp/tcp_connect.c @@ -103,8 +103,8 @@ static inline int psock_setup_callbacks(FAR struct socket *psock, { /* Set up the connection event handler */ - pstate->tc_cb->flags = (TCP_NEWDATA | TCP_CLOSE | TCP_ABORT | - TCP_TIMEDOUT | TCP_CONNECTED | NETDEV_DOWN); + pstate->tc_cb->flags = (TCP_NEWDATA | TCP_ABORT | TCP_TIMEDOUT | + TCP_CONNECTED | NETDEV_DOWN); pstate->tc_cb->priv = (FAR void *)pstate; pstate->tc_cb->event = psock_connect_eventhandler; ret = OK; @@ -135,7 +135,7 @@ static void psock_teardown_callbacks(FAR struct tcp_connect_s *pstate, { /* Failed to connect. Stop the connection event monitor */ - tcp_stop_monitor(conn, TCP_CLOSE); + tcp_stop_monitor(conn, TCP_ABORT); } } @@ -184,11 +184,10 @@ static uint16_t psock_connect_eventhandler(FAR struct net_driver_s *dev, * busy to accept new connections. */ - /* TCP_CLOSE: The remote host has closed the connection - * TCP_ABORT: The remote host has aborted the connection + /* TCP_ABORT: The remote host has aborted the connection */ - if ((flags & (TCP_CLOSE | TCP_ABORT)) != 0) + if ((flags & TCP_ABORT) != 0) { /* Indicate that remote host refused the connection */ diff --git a/net/tcp/tcp_monitor.c b/net/tcp/tcp_monitor.c index 63ae6b9243e..2a559b32448 100644 --- a/net/tcp/tcp_monitor.c +++ b/net/tcp/tcp_monitor.c @@ -74,7 +74,6 @@ static void tcp_close_connection(FAR struct tcp_conn_s *conn, uint16_t flags) { /* These loss-of-connection events may be reported: * - * TCP_CLOSE: The remote host has closed the connection * TCP_ABORT: The remote host has aborted the connection * TCP_TIMEDOUT: Connection aborted due to too many retransmissions. * NETDEV_DOWN: The network device went down @@ -91,7 +90,7 @@ static void tcp_close_connection(FAR struct tcp_conn_s *conn, uint16_t flags) * (eventually) be reported as an ENOTCONN error. */ - if ((flags & TCP_CLOSE) != 0) + if ((flags & TCP_ABORT) != 0) { /* The peer gracefully closed the connection. Marking the * connection as disconnected will suppress some subsequent @@ -102,7 +101,7 @@ static void tcp_close_connection(FAR struct tcp_conn_s *conn, uint16_t flags) conn->sconn.s_flags &= ~_SF_CONNECTED; conn->sconn.s_flags |= _SF_CLOSED; } - else if ((flags & (TCP_ABORT | TCP_TIMEDOUT | NETDEV_DOWN)) != 0) + else if ((flags & (TCP_TIMEDOUT | NETDEV_DOWN)) != 0) { /* The loss of connection was less than graceful. This will * (eventually) be reported as an ENOTCONN error. @@ -140,8 +139,8 @@ static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev, { ninfo("flags: %04x s_flags: %02x\n", flags, conn->sconn.s_flags); - /* TCP_DISCONN_EVENTS: TCP_CLOSE, TCP_ABORT, TCP_TIMEDOUT, or - * NETDEV_DOWN. All loss-of-connection events. + /* TCP_DISCONN_EVENTS: TCP_ABORT, TCP_TIMEDOUT, or NETDEV_DOWN. + * All loss-of-connection events. */ if ((flags & TCP_DISCONN_EVENTS) != 0) @@ -268,9 +267,9 @@ int tcp_start_monitor(FAR struct socket *psock) if (!(conn->tcpstateflags == TCP_ESTABLISHED || conn->tcpstateflags == TCP_SYN_RCVD || nonblock_conn)) { - /* Invoke the TCP_CLOSE connection event now */ + /* Invoke the TCP_ABORT connection event now */ - tcp_shutdown_monitor(conn, TCP_CLOSE); + tcp_shutdown_monitor(conn, TCP_ABORT); /* If the peer close the connection before we call accept, * in order to allow user to read the readahead data, diff --git a/net/tcp/tcp_netpoll.c b/net/tcp/tcp_netpoll.c index 2c60796af19..093ea200df9 100644 --- a/net/tcp/tcp_netpoll.c +++ b/net/tcp/tcp_netpoll.c @@ -122,9 +122,7 @@ static uint16_t tcp_poll_eventhandler(FAR struct net_driver_s *dev, reason = ENETUNREACH; } - /* TCP_CLOSE: The remote host has closed the connection - * TCP_ABORT: The remote host has aborted the connection - */ + /* TCP_ABORT: The remote host has aborted the connection */ else { @@ -368,7 +366,15 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds) * exceptional event. */ - _SO_CONN_SETERRNO(conn, ENOTCONN); + if (_SS_ISCLOSED(conn->sconn.s_flags)) + { + _SO_CONN_SETERRNO(conn, ECONNREFUSED); + } + else + { + _SO_CONN_SETERRNO(conn, ENOTCONN); + } + eventset |= POLLERR | POLLHUP; } else if (_SS_ISCONNECTED(conn->sconn.s_flags) && diff --git a/net/tcp/tcp_recvfrom.c b/net/tcp/tcp_recvfrom.c index 4dbcc677478..010d3c753a9 100644 --- a/net/tcp/tcp_recvfrom.c +++ b/net/tcp/tcp_recvfrom.c @@ -480,7 +480,6 @@ static uint16_t tcp_recvhandler(FAR struct net_driver_s *dev, /* Check for a loss of connection. * * TCP_DISCONN_EVENTS: - * TCP_CLOSE: The remote host has closed the connection * TCP_ABORT: The remote host has aborted the connection * TCP_TIMEDOUT: Connection aborted due to too many retransmissions. * NETDEV_DOWN: The network device went down @@ -505,21 +504,7 @@ static uint16_t tcp_recvhandler(FAR struct net_driver_s *dev, tcp_lost_connection(conn, pstate->ir_cb, flags); } - /* Check if the peer gracefully closed the connection. */ - - if ((flags & TCP_CLOSE) != 0) - { - /* This case should always return success (zero)! The value of - * ir_recvlen, if zero, will indicate that the connection was - * gracefully closed. - */ - - pstate->ir_result = 0; - } - else - { - pstate->ir_result = -ENOTCONN; - } + pstate->ir_result = -ENOTCONN; /* Wake up the waiting thread */