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 806b05cd316f8f6d3697b8d04b11e75538fcb6ac
Author: chenjian <[email protected]>
AuthorDate: Tue Apr 7 14:50:47 2026 +0800

    net/udp: fix udp_find_conn to check both local addresses for overlap
    
    The existing udp_find_conn only checks if the existing connection's
    local address is INADDR_ANY when detecting bind conflicts, but does
    not check if the new binding address is INADDR_ANY. This means:
    
      bind(10.0.2.15:7400) then bind(0.0.0.0:7400) succeeds incorrectly
    
    Add the missing check for the new address being INADDR_ANY (IPv4) or
    in6addr_any (IPv6), making the overlap detection symmetric and
    consistent with Linux UDP bind behavior:
    
      - Same address + same port: conflict (need SO_REUSEADDR)
      - Either is wildcard + same port: conflict (need SO_REUSEADDR)
      - Different specific addresses + same port: no conflict
    
    Signed-off-by: chenjian <[email protected]>
---
 net/udp/udp_conn.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/net/udp/udp_conn.c b/net/udp/udp_conn.c
index a730f559e45..d009a146304 100644
--- a/net/udp/udp_conn.c
+++ b/net/udp/udp_conn.c
@@ -193,9 +193,10 @@ static FAR struct udp_conn_s *udp_find_conn(uint8_t domain,
 #endif
 
       /* If the port local port number assigned to the connections matches
-       * AND the IP address of the connection matches, then return a
-       * reference to the connection structure.  INADDR_ANY is a special
-       * case:  There can only be instance of a port number with INADDR_ANY.
+       * AND the IP address of the connection overlaps, then return a
+       * reference to the connection structure.  Addresses overlap when
+       * they are equal, or either is INADDR_ANY (wildcard).  Different
+       * specific addresses on the same port do not conflict.
        */
 
 #ifdef CONFIG_NET_IPv4
@@ -205,7 +206,8 @@ static FAR struct udp_conn_s *udp_find_conn(uint8_t domain,
         {
           if (conn->domain == PF_INET && conn->lport == portno &&
               (net_ipv4addr_cmp(conn->u.ipv4.laddr, ipaddr->ipv4.laddr) ||
-               net_ipv4addr_cmp(conn->u.ipv4.laddr, INADDR_ANY)))
+               net_ipv4addr_cmp(conn->u.ipv4.laddr, INADDR_ANY) ||
+               net_ipv4addr_cmp(ipaddr->ipv4.laddr, INADDR_ANY)))
             {
               break;
             }
@@ -219,7 +221,8 @@ static FAR struct udp_conn_s *udp_find_conn(uint8_t domain,
         {
           if (conn->domain == PF_INET6 && conn->lport == portno &&
               (net_ipv6addr_cmp(conn->u.ipv6.laddr, ipaddr->ipv6.laddr) ||
-               net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr)))
+               net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr) ||
+               net_ipv6addr_cmp(ipaddr->ipv6.laddr, g_ipv6_unspecaddr)))
             {
               break;
             }

Reply via email to