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 91a5d8c1a4e19f74b41631d3e0faa5999ff66120
Author: guanyushuai1 <[email protected]>
AuthorDate: Tue Apr 14 20:34:41 2026 +0800

    net/udp: resolve local address in udp_path for fdinfo display
    
    When a UDP socket calls connect() without a prior bind(), the local
    address (laddr) remains INADDR_ANY. This causes fdinfo to display
    0.0.0.0 instead of the actual local IP.
    
    Fix this by adding a display-only fallback in udp_path(): when
    the socket is in connect mode and laddr is the wildcard address,
    resolve the actual local IP via udp_find_raddr_device() route
    lookup without modifying conn state.
    
    Signed-off-by: guanyushuai1 <[email protected]>
---
 net/udp/udp_ioctl.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/net/udp/udp_ioctl.c b/net/udp/udp_ioctl.c
index 21e2defcac2..28deed950b4 100644
--- a/net/udp/udp_ioctl.c
+++ b/net/udp/udp_ioctl.c
@@ -38,6 +38,8 @@
 #include <nuttx/mm/iob.h>
 #include <nuttx/net/net.h>
 
+#include "inet/inet.h"
+#include "netdev/netdev.h"
 #include "utils/utils.h"
 #include "udp/udp.h"
 
@@ -66,6 +68,51 @@ static void udp_path(FAR struct udp_conn_s *conn, FAR char 
*buf, size_t len)
   FAR void *laddr = net_ip_binding_laddr(&conn->u, domain);
   FAR void *raddr = net_ip_binding_raddr(&conn->u, domain);
 
+  /* If laddr is the wildcard address and socket is connected,
+   * resolve the actual local IP via routing lookup for display
+   * purposes only.
+   */
+
+  if (_UDP_ISCONNECTMODE(conn->flags))
+    {
+#ifdef CONFIG_NET_IPv4
+#  ifdef CONFIG_NET_IPv6
+      if (domain == PF_INET)
+#  endif
+        {
+          if (conn->u.ipv4.laddr == INADDR_ANY)
+            {
+              FAR struct net_driver_s *dev;
+
+              dev = udp_find_raddr_device(conn, NULL);
+              if (dev != NULL)
+                {
+                  laddr = &dev->d_ipaddr;
+                }
+            }
+        }
+#endif
+
+#ifdef CONFIG_NET_IPv6
+#  ifdef CONFIG_NET_IPv4
+      else
+#  endif
+        {
+          if (net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr))
+            {
+              FAR struct net_driver_s *dev;
+
+              dev = udp_find_raddr_device(conn, NULL);
+              if (dev != NULL)
+                {
+                  laddr = (FAR void *)netdev_ipv6_srcaddr(
+                                       dev, conn->u.ipv6.raddr);
+                }
+            }
+        }
+#endif
+    }
+
   snprintf(buf, len, "udp:["
            "%s:%" PRIu16 "<->%s:%" PRIu16
            ", tx %" PRIu32 "/%" PRId32
@@ -127,6 +174,7 @@ int udp_ioctl(FAR struct udp_conn_s *conn, int cmd, 
unsigned long arg)
         if (iob)
           {
             uint16_t datalen;
+
             iob_copyout((FAR uint8_t *)&datalen, iob, sizeof(datalen), 0);
             *(FAR int *)((uintptr_t)arg) = datalen;
           }

Reply via email to