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 1980f8f8b8aa6e183bbc036967a02cc3feb05dca
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Oct 30 08:11:11 2026 +0100

    net/local: Send the header of a datagram on a socket pair.
    
    A datagram socket pair could not carry datagrams.  Since commit
    9b2e7c7e9a4, send() on a SOCK_DGRAM socket pair fails with EDESTADDRREQ:
    local_send() sends a datagram only to the address that connect() set,
    and a socket pair has no such address.  Before that commit, send() on a
    socket pair wrote only the data (since commit 9147c955dc1), but recv()
    reads the header of a datagram first (the length of the address of the
    sender and the length of the datagram).
    
    send() on a SOCK_DGRAM socket pair now writes the header (address
    length 0: a socket pair has no address) and then the data.  A datagram
    goes whole or not at all: a send that must not wait (O_NONBLOCK or
    MSG_DONTWAIT) fails with EAGAIN when the FIFO has no space for all of
    it, and a datagram larger than the FIFO fails with EMSGSIZE.  When the
    peer was closed, send() fails with ECONNREFUSED (as on Linux), not with
    EPIPE.
    
    recv() returns 0 when the peer was closed and no datagram is left (the
    FIFO has no writer), as for the orderly shutdown of a peer.  Before, it
    used the header bytes that it did not read.
    
    The same in all build modes; no architecture code.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/local/local_recvmsg.c |  10 ++++
 net/local/local_sendmsg.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+)

diff --git a/net/local/local_recvmsg.c b/net/local/local_recvmsg.c
index 686270fc10e..3f703e3df2b 100644
--- a/net/local/local_recvmsg.c
+++ b/net/local/local_recvmsg.c
@@ -544,6 +544,16 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void 
*buf, size_t len,
       return ret;
     }
 
+  /* No data: the peer of a socket pair was closed.  Return 0, as for the
+   * orderly shutdown of a peer.
+   */
+
+  if (readlen < sizeof(addrlen))
+    {
+      readlen = 0;
+      goto errout_with_infd;
+    }
+
   /* Sync to the start of the next packet in the stream and get the size of
    * the next packet.
    */
diff --git a/net/local/local_sendmsg.c b/net/local/local_sendmsg.c
index 555c0d59749..3494b74917d 100644
--- a/net/local/local_sendmsg.c
+++ b/net/local/local_sendmsg.c
@@ -30,6 +30,7 @@
 #include <sys/socket.h>
 #include <unistd.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <assert.h>
 #include <nuttx/debug.h>
 
@@ -138,6 +139,101 @@ fail:
 }
 #endif /* CONFIG_NET_LOCAL_SCM */
 
+/****************************************************************************
+ * Name: local_pair_send
+ *
+ * Description:
+ *   Send one datagram, whole or not at all, to the peer of a SOCK_DGRAM
+ *   socket pair: the header that psock_dgram_recvfrom() reads (address
+ *   length 0, datagram length), then the data.
+ *
+ * Input Parameters:
+ *   conn     The connection of the sender
+ *   buf      Data to send
+ *   len      Number of entries in buf
+ *   flags    Send flags
+ *
+ * Returned Value:
+ *   The length of the datagram, or a negated errno value: EMSGSIZE when
+ *   the datagram does not fit into the FIFO, ECONNREFUSED when the peer
+ *   was closed.
+ *
+ * Assumptions:
+ *   The caller holds lc_sendlock.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NET_LOCAL_DGRAM
+static ssize_t local_pair_send(FAR struct local_conn_s *conn,
+                               FAR const struct iovec *buf, size_t len,
+                               int flags)
+{
+  FAR const struct iovec *end = buf + len;
+  FAR const struct iovec *iov;
+  lc_size_t header[2];
+  struct iovec hdriov;
+  size_t pktlen = 0;
+  int space;
+  int ret;
+
+  for (iov = buf; iov != end; iov++)
+    {
+      pktlen += iov->iov_len;
+    }
+
+  ret = file_ioctl(&conn->lc_outfile, PIPEIOC_GETSIZE, 0);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (pktlen + sizeof(header) > ret)
+    {
+      return -EMSGSIZE;
+    }
+
+  if ((flags & MSG_DONTWAIT) != 0 ||
+      (conn->lc_outfile.f_oflags & O_NONBLOCK) != 0)
+    {
+      ret = file_ioctl(&conn->lc_outfile, FIONSPACE, &space);
+      if (ret < 0)
+        {
+          return ret;
+        }
+
+      if (space < pktlen + sizeof(header))
+        {
+          return -EAGAIN;
+        }
+    }
+
+  header[0]       = 0;
+  header[1]       = pktlen;
+  hdriov.iov_base = header;
+  hdriov.iov_len  = sizeof(header);
+
+  ret = local_send_packet(&conn->lc_outfile, &hdriov, 1);
+  if (ret == sizeof(header) && pktlen > 0)
+    {
+      ret = local_send_packet(&conn->lc_outfile, buf, len);
+    }
+  else if (ret == sizeof(header))
+    {
+      ret = 0;
+    }
+  else if (ret >= 0)
+    {
+      ret = -EPIPE;
+    }
+
+  /* -EPIPE: the peer was closed.  A datagram socket reports that as
+   * Linux does, and without SIGPIPE.
+   */
+
+  return ret == -EPIPE ? -ECONNREFUSED : ret;
+}
+#endif /* CONFIG_NET_LOCAL_DGRAM */
+
 /****************************************************************************
  * Name: local_sendto
  *
@@ -393,6 +489,26 @@ static ssize_t local_send(FAR struct socket *psock,
         {
           FAR struct local_conn_s *conn = psock->s_conn;
 
+          /* One of a socket pair: the FIFO to the peer is open */
+
+          if (conn->lc_state == LOCAL_STATE_CONNECTED)
+            {
+              if (conn->lc_outfile.f_inode == NULL)
+                {
+                  return -EPIPE;
+                }
+
+              ret = nxmutex_lock(&conn->lc_sendlock);
+              if (ret < 0)
+                {
+                  return ret;
+                }
+
+              ret = local_pair_send(conn, buf, len, flags);
+              nxmutex_unlock(&conn->lc_sendlock);
+              break;
+            }
+
           if (!conn->lc_dconnected)
             {
               return -EDESTADDRREQ;

Reply via email to