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 a6b3725014c000f0289240191c96f0bbaf79760a
Author: daichuan <[email protected]>
AuthorDate: Tue Oct 14 16:28:50 2025 +0800

    net/pkt: add the sender's address information with recvfrom
    
    Standardize the code process.
    
    Signed-off-by: daichuan <[email protected]>
---
 net/pkt/pkt_recvmsg.c | 48 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 4 deletions(-)

diff --git a/net/pkt/pkt_recvmsg.c b/net/pkt/pkt_recvmsg.c
index 3ed20b5a238..1072b36a603 100644
--- a/net/pkt/pkt_recvmsg.c
+++ b/net/pkt/pkt_recvmsg.c
@@ -40,14 +40,17 @@
 #include <arch/irq.h>
 
 #include <nuttx/semaphore.h>
+#include <nuttx/mm/iob.h>
 #include <nuttx/net/net.h>
 #include <nuttx/net/netdev.h>
+#include <nuttx/net/ethernet.h>
 
 #include "netdev/netdev.h"
 #include "devif/devif.h"
 #include "pkt/pkt.h"
 #include "socket/socket.h"
 #include "utils/utils.h"
+#include <netinet/if_ether.h>
 #include <netpacket/packet.h>
 
 /****************************************************************************
@@ -154,9 +157,35 @@ static void pkt_recvfrom_newdata(FAR struct net_driver_s 
*dev,
  *
  ****************************************************************************/
 
-static inline void pkt_recvfrom_sender(FAR struct net_driver_s *dev,
+static inline void pkt_recvfrom_sender(int ifindex, FAR void *llhdr,
+                                       unsigned int llhdrlen,
                                        FAR struct pkt_recvfrom_s *pstate)
 {
+  struct sockaddr_ll from;
+  socklen_t copylen;
+
+  if (pstate->pr_msg->msg_name != NULL)
+    {
+      memset(&from, 0, sizeof(from));
+      from.sll_family   = AF_PACKET;
+      from.sll_ifindex  = ifindex;
+      from.sll_protocol = pstate->pr_type;
+      from.sll_halen    = ETH_ALEN;
+
+      if (llhdr != NULL && llhdrlen >= sizeof(struct eth_hdr_s))
+        {
+          FAR struct eth_hdr_s *ethhdr = (FAR struct eth_hdr_s *)llhdr;
+          memcpy(from.sll_addr, ethhdr->src, ETH_ALEN);
+        }
+
+      /* POSIX allows a too-small address buffer: copy at most the number
+       * of bytes the caller provided (truncation), never more.
+       */
+
+      copylen = MIN(pstate->pr_msg->msg_namelen, sizeof(struct sockaddr_ll));
+      memcpy(pstate->pr_msg->msg_name, &from, copylen);
+      pstate->pr_msg->msg_namelen = sizeof(struct sockaddr_ll);
+    }
 }
 
 /****************************************************************************
@@ -215,7 +244,8 @@ static uint32_t pkt_recvfrom_eventhandler(FAR struct 
net_driver_s *dev,
 
           /* Save the sender's address in the caller's 'from' location */
 
-          pkt_recvfrom_sender(dev, pstate);
+          pkt_recvfrom_sender(dev->d_ifindex, dev->d_buf, dev->d_len,
+                              pstate);
 
           /* indicate that the data has been consumed */
 
@@ -371,6 +401,7 @@ static inline int pkt_readdata(FAR struct pkt_recvfrom_s 
*pstate,
                                                 FAR struct iob_s *))
 {
   FAR struct pkt_conn_s *conn = pstate->pr_conn;
+  FAR struct net_driver_s *dev;
   FAR struct iob_s *iob;
   int recvlen;
   int offset = 0;
@@ -385,9 +416,10 @@ static inline int pkt_readdata(FAR struct pkt_recvfrom_s 
*pstate,
 
       /* Copy to user */
 
+      dev = pkt_find_device(conn);
+
       if (pstate->pr_type == SOCK_DGRAM)
         {
-          FAR struct net_driver_s *dev = pkt_find_device(conn);
           if (dev != NULL)
             {
               /* For SOCK_DGRAM, we need skip the l2 header */
@@ -411,6 +443,14 @@ static inline int pkt_readdata(FAR struct pkt_recvfrom_s 
*pstate,
 
       tsfunc(pstate, iob);
 
+      /* Fill in the sender's address information if requested */
+
+      if (dev != NULL && pstate->pr_msg->msg_name != NULL)
+        {
+          pkt_recvfrom_sender(dev->d_ifindex, IOB_DATA(iob),
+                              iob->io_pktlen, pstate);
+        }
+
       /* And free the I/O buffer chain */
 
       iob_free_chain(iob);
@@ -454,7 +494,7 @@ static inline int pkt_readdata(FAR struct pkt_recvfrom_s 
*pstate,
 ssize_t pkt_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
                     int flags)
 {
-  FAR struct sockaddr *from = msg->msg_name;
+  FAR struct sockaddr_ll *from = msg->msg_name;
   FAR socklen_t *fromlen = &msg->msg_namelen;
   FAR struct pkt_conn_s *conn = psock->s_conn;
   FAR struct net_driver_s *dev;

Reply via email to