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 8cf5ce244f1d3b31d71bf277883691e5afc21a26 Author: Marco Casaroli <[email protected]> AuthorDate: Thu Nov 26 15:37:59 2026 +0100 net/tcp: Accept TCP_CORK. setsockopt(TCP_CORK, 1) failed with ENOSYS. HTTP servers set it around a response that they send with sendfile(), and some end the response with an error when it fails. The stack has no Nagle algorithm and sends each write at once, so TCP_CORK can only be a hint: setsockopt() now stores it as a bit in the socket options (s_options), as CAN_RAW_FD_FRAMES does, and getsockopt() reports it; no segment is held back. struct tcp_conn_s does not change. TCP_NODELAY 0 is still refused. s_options exists only with CONFIG_NET_SOCKOPTS, and the only callers of tcp_setsockopt() and tcp_getsockopt() (inet_sockif.c) are built only then. So the two files now build only with CONFIG_NET_SOCKOPTS, as ipv4_setsockopt.c does. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/tcp/tcp_getsockopt.c | 11 +++++++---- net/tcp/tcp_setsockopt.c | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/net/tcp/tcp_getsockopt.c b/net/tcp/tcp_getsockopt.c index a3f1cacce46..77fd7352193 100644 --- a/net/tcp/tcp_getsockopt.c +++ b/net/tcp/tcp_getsockopt.c @@ -41,7 +41,7 @@ #include "utils/utils.h" #include "tcp/tcp.h" -#ifdef CONFIG_NET_TCPPROTO_OPTIONS +#if defined(CONFIG_NET_TCPPROTO_OPTIONS) && defined(CONFIG_NET_SOCKOPTS) /**************************************************************************** * Public Functions @@ -216,9 +216,12 @@ int tcp_getsockopt(FAR struct socket *psock, int option, { FAR int *nodelay = (FAR int *)value; - /* Always true here since we do not support Nagle. */ + /* TCP_NODELAY is always on (no Nagle algorithm). TCP_CORK is + * only a hint: report what setsockopt() stored. + */ - *nodelay = option == TCP_NODELAY ? 1 : 0; + *nodelay = option == TCP_NODELAY ? 1 : + _SO_GETOPT(conn->sconn.s_options, option); *value_len = sizeof(int); ret = OK; } @@ -253,4 +256,4 @@ int tcp_getsockopt(FAR struct socket *psock, int option, return ret; } -#endif /* CONFIG_NET_TCPPROTO_OPTIONS */ +#endif /* CONFIG_NET_TCPPROTO_OPTIONS && CONFIG_NET_SOCKOPTS */ diff --git a/net/tcp/tcp_setsockopt.c b/net/tcp/tcp_setsockopt.c index b53918919f4..972d1868881 100644 --- a/net/tcp/tcp_setsockopt.c +++ b/net/tcp/tcp_setsockopt.c @@ -41,7 +41,7 @@ #include "utils/utils.h" #include "tcp/tcp.h" -#ifdef CONFIG_NET_TCPPROTO_OPTIONS +#if defined(CONFIG_NET_TCPPROTO_OPTIONS) && defined(CONFIG_NET_SOCKOPTS) /**************************************************************************** * Public Functions @@ -216,22 +216,40 @@ int tcp_setsockopt(FAR struct socket *psock, int option, #endif /* CONFIG_NET_TCP_KEEPALIVE */ case TCP_NODELAY: /* Avoid coalescing of small segments. */ - case TCP_CORK: /* coalescing of small segments. */ if (value_len != sizeof(int)) { ret = -EDOM; } - else + else if (*(FAR int *)value == 0) { - int nodelay = *(FAR int *)value; + nerr("ERROR: TCP_NODELAY 0 not supported\n"); + ret = -ENOSYS; + } + break; + + case TCP_CORK: /* Coalescing of small segments. */ - if ((!nodelay && option == TCP_NODELAY) || - (nodelay && option == TCP_CORK)) + /* A hint only: the stack has no Nagle algorithm and sends each + * write at once. getsockopt() reports the value. + */ + + if (value_len != sizeof(int)) + { + ret = -EDOM; + } + else + { + conn_lock(&conn->sconn); + if (*(FAR int *)value) { - nerr("ERROR: %s not supported\n", - option == TCP_NODELAY ? "TCP_NODELAY" : "TCP_CORK"); - ret = -ENOSYS; + _SO_SETOPT(conn->sconn.s_options, option); } + else + { + _SO_CLROPT(conn->sconn.s_options, option); + } + + conn_unlock(&conn->sconn); } break; @@ -272,4 +290,4 @@ int tcp_setsockopt(FAR struct socket *psock, int option, return ret; } -#endif /* CONFIG_NET_TCPPROTO_OPTIONS */ +#endif /* CONFIG_NET_TCPPROTO_OPTIONS && CONFIG_NET_SOCKOPTS */
