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

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

commit 05920ae3e3d1d1763cd4b2cc97b6bd4cc3cfde94
Author: yangjianqing <[email protected]>
AuthorDate: Mon Mar 2 16:42:30 2026 +0800

    sim/usrsock: add TCP socket option support in host usrsock
    
    Add TCP-level socket option mapping (TCP_NODELAY, TCP_KEEPIDLE,
    TCP_KEEPINTVL, TCP_KEEPCNT, TCP_MAXSEG, TCP_CORK) to the sim
    host usrsock implementation.
    
    Signed-off-by: yangjianqing <[email protected]>
---
 arch/sim/src/sim/posix/sim_hostusrsock.c | 40 +++++++++++++++++++++++++-------
 arch/sim/src/sim/sim_hostusrsock.h       | 12 ++++++++--
 2 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/arch/sim/src/sim/posix/sim_hostusrsock.c 
b/arch/sim/src/sim/posix/sim_hostusrsock.c
index 52cd495ecad..1975e9c3344 100644
--- a/arch/sim/src/sim/posix/sim_hostusrsock.c
+++ b/arch/sim/src/sim/posix/sim_hostusrsock.c
@@ -40,6 +40,7 @@
 #include <syslog.h>
 
 #include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <sys/un.h>
 
 #include "sim_internal.h"
@@ -261,6 +262,34 @@ static int optname_to_native(int optname)
     }
 }
 
+static int tcp_optname_to_native(int optname)
+{
+  switch (optname)
+    {
+      case NUTTX_TCP_NODELAY:
+        return TCP_NODELAY;
+
+      case NUTTX_TCP_KEEPIDLE:
+        return TCP_KEEPIDLE;
+
+      case NUTTX_TCP_KEEPINTVL:
+        return TCP_KEEPINTVL;
+
+      case NUTTX_TCP_KEEPCNT:
+        return TCP_KEEPCNT;
+
+      case NUTTX_TCP_MAXSEG:
+        return TCP_MAXSEG;
+
+      case NUTTX_TCP_CORK:
+        return TCP_CORK;
+
+      default:
+        syslog(LOG_ERR, "Invalid TCP optname: %x\n", optname);
+        return -ENOPROTOOPT;
+    }
+}
+
 static int host_usrsock_sockopt(int sockfd, int level, int optname,
                                 const void *optval, nuttx_socklen_t *optlen,
                                 bool set)
@@ -274,25 +303,18 @@ static int host_usrsock_sockopt(int sockfd, int level, 
int optname,
   if (level == NUTTX_SOL_SOCKET)
     {
       level = SOL_SOCKET;
+      optname = optname_to_native(optname);
     }
   else if (level == NUTTX_IPPROTO_TCP)
     {
       level = IPPROTO_TCP;
-    }
-  else if (level == NUTTX_IPPROTO_UDP)
-    {
-      level = IPPROTO_UDP;
-    }
-  else if (level == NUTTX_IPPROTO_IP)
-    {
-      level = IPPROTO_IP;
+      optname = tcp_optname_to_native(optname);
     }
   else
     {
       return -ENOPROTOOPT;
     }
 
-  optname = optname_to_native(optname);
   if (optname < 0)
     {
       return optname;
diff --git a/arch/sim/src/sim/sim_hostusrsock.h 
b/arch/sim/src/sim/sim_hostusrsock.h
index 367e98ae7d7..3507f64ff92 100644
--- a/arch/sim/src/sim/sim_hostusrsock.h
+++ b/arch/sim/src/sim/sim_hostusrsock.h
@@ -142,6 +142,9 @@
 #define NUTTX_SO_TIMESTAMPING       9
 #define NUTTX_SO_RCVTIMEO           10
 #define NUTTX_SO_SNDTIMEO           11
+
+#define NUTTX__SO_PROTOCOL          11
+
 #define NUTTX_SO_RCVBUF             64
 #define NUTTX_SO_RCVLOWAT           65
 #define NUTTX_SO_SNDBUF             66
@@ -167,8 +170,6 @@
 #define NUTTX_SOL_SCO               17
 #define NUTTX_SOL_RFCOMM            18
 
-#define NUTTX___SO_PROTOCOL         16
-
 #define NUTTX_SHUT_RD               1
 #define NUTTX_SHUT_WR               2
 #define NUTTX_SHUT_RDWR             3
@@ -177,6 +178,13 @@
 #define NUTTX_SCM_CREDENTIALS       0x02
 #define NUTTX_SCM_SECURITY          0x03
 
+#define NUTTX_TCP_NODELAY               (NUTTX__SO_PROTOCOL + 0)  /* Avoid 
coalescing of small segments */
+#define NUTTX_TCP_KEEPIDLE              (NUTTX__SO_PROTOCOL + 1)  /* Start 
keepalives after this IDLE period */
+#define NUTTX_TCP_KEEPINTVL             (NUTTX__SO_PROTOCOL + 2)  /* Interval 
between keepalives */
+#define NUTTX_TCP_KEEPCNT               (NUTTX__SO_PROTOCOL + 3)  /* Number of 
keepalives before death */
+#define NUTTX_TCP_MAXSEG                (NUTTX__SO_PROTOCOL + 4)  /* The 
maximum segment size */
+#define NUTTX_TCP_CORK                  (NUTTX__SO_PROTOCOL + 5)  /* 
Coalescing of small segments */
+
 #define NUTTX_IP_MULTICAST_IF           (NUTTX__SO_PROTOCOL + 1)
 #define NUTTX_IP_MULTICAST_TTL          (NUTTX__SO_PROTOCOL + 2)
 #define NUTTX_IP_MULTICAST_LOOP         (NUTTX__SO_PROTOCOL + 3)

Reply via email to