You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2023/02/11 17:25:39 UTC

[nuttx] branch master updated: net: modify find device logic

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new a2097cfb71 net: modify find device logic
a2097cfb71 is described below

commit a2097cfb71bab11e78c829d477f73f802768fdd1
Author: zhanghongyu <zh...@xiaomi.com>
AuthorDate: Thu Feb 9 15:35:00 2023 +0800

    net: modify find device logic
    
    The priorities for finding a network adapter are as follows:
    1. if laddr is not ANY, use laddr to find device;
    2. if laddr is ANY, and bound index is not 0, use bound index
       to find device;
    3. if laddr is ANY and no device is bound, use raddr to find
       device.
    
    Signed-off-by: zhanghongyu <zh...@xiaomi.com>
---
 net/icmp/icmp_sendmsg.c  |   2 +-
 net/tcp/tcp_finddev.c    |  16 +++++-
 net/udp/udp_finddev.c    | 129 ++++++++++++++++-------------------------------
 net/utils/Make.defs      |   6 ---
 net/utils/net_bounddev.c |  85 -------------------------------
 net/utils/utils.h        |  25 ---------
 6 files changed, 59 insertions(+), 204 deletions(-)

diff --git a/net/icmp/icmp_sendmsg.c b/net/icmp/icmp_sendmsg.c
index 9a0a846d1f..e218d3f983 100644
--- a/net/icmp/icmp_sendmsg.c
+++ b/net/icmp/icmp_sendmsg.c
@@ -306,7 +306,7 @@ ssize_t icmp_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
 #ifdef CONFIG_NET_BINDTODEVICE
   if (conn->sconn.s_boundto != 0)
     {
-      dev = net_bound_device(&conn->sconn);
+      dev = netdev_findbyindex(conn->sconn.s_boundto);
     }
   else
 #endif
diff --git a/net/tcp/tcp_finddev.c b/net/tcp/tcp_finddev.c
index 35879fdaff..07961cc8c2 100644
--- a/net/tcp/tcp_finddev.c
+++ b/net/tcp/tcp_finddev.c
@@ -77,7 +77,13 @@ static int tcp_find_ipv4_device(FAR struct tcp_conn_s *conn,
     {
       if (local)
         {
-          conn->dev = net_bound_device(&conn->sconn);
+#ifdef CONFIG_NET_BINDTODEVICE
+          if (conn->sconn.s_boundto != 0)
+            {
+              conn->dev = netdev_findbyindex(conn->sconn.s_boundto);
+            }
+#endif
+
           return OK;
         }
 
@@ -133,7 +139,13 @@ static int tcp_find_ipv6_device(FAR struct tcp_conn_s *conn,
     {
       if (local)
         {
-          conn->dev = net_bound_device(&conn->sconn);
+#ifdef CONFIG_NET_BINDTODEVICE
+          if (conn->sconn.s_boundto != 0)
+            {
+              conn->dev = netdev_findbyindex(conn->sconn.s_boundto);
+            }
+#endif
+
           return OK;
         }
 
diff --git a/net/udp/udp_finddev.c b/net/udp/udp_finddev.c
index ceb8ba1b50..93c5d4b7ee 100644
--- a/net/udp/udp_finddev.c
+++ b/net/udp/udp_finddev.c
@@ -143,61 +143,41 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn,
         {
           in_addr_t raddr;
 
-          if (remote)
-            {
-              FAR const struct sockaddr_in *inaddr =
-                (FAR const struct sockaddr_in *)remote;
-              net_ipv4addr_copy(raddr, inaddr->sin_addr.s_addr);
-            }
-          else
+          if (conn->u.ipv4.laddr != INADDR_ANY)
             {
-              net_ipv4addr_copy(raddr, conn->u.ipv4.raddr);
-            }
+              /* If the socket is bound to some non-zero, local address.
+               * Normal lookup using the verified local address.
+               */
 
-          /* Check if the remote, destination address is the broadcast
-           * or multicast address.  If this is the case, select the device
-           * using the locally bound address (assuming that there is one).
-           */
+              return netdev_findby_lipv4addr(conn->u.ipv4.laddr);
+            }
 
-          if (raddr == INADDR_BROADCAST || IN_MULTICAST(NTOHL(raddr)))
+#ifdef CONFIG_NET_BINDTODEVICE
+          if (conn->sconn.s_boundto != 0)
             {
-              /* Make sure that the socket is bound to some non-zero, local
-               * address.  Zero is used as an indication that the laddr is
-               * uninitialized and that the socket is, hence, not bound.
+              /* If the socket is bound to a local network device.
+               * Select the network device that has been bound.
+               * If the index is invalid, return NULL.
                */
 
-              if (conn->u.ipv4.laddr == 0) /* INADDR_ANY */
-                {
-                  /* Return the device bound to this UDP socket, if any */
-
-                  return net_bound_device(&conn->sconn);
-                }
-              else
-                {
-                  return netdev_findby_ripv4addr(conn->u.ipv4.laddr,
-                                                 conn->u.ipv4.laddr);
-                }
+              return netdev_findbyindex(conn->sconn.s_boundto);
             }
+#endif
 
-          /* There is no unique device associated with the unspecified
-           * address.
-           */
-
-          else if (raddr != INADDR_ANY)
+          if (remote)
             {
-              /* Normal lookup using the verified remote address */
-
-              return netdev_findby_ripv4addr(conn->u.ipv4.laddr,
-                                             raddr);
+              FAR const struct sockaddr_in *inaddr =
+                (FAR const struct sockaddr_in *)remote;
+              net_ipv4addr_copy(raddr, inaddr->sin_addr.s_addr);
             }
           else
             {
-              /* Not a suitable IPv4 unicast address for device lookup.
-               * Return the device bound to this UDP socket, if any.
-               */
-
-              return net_bound_device(&conn->sconn);
+              net_ipv4addr_copy(raddr, conn->u.ipv4.raddr);
             }
+
+          /* Normal lookup using the verified remote address */
+
+          return netdev_findby_ripv4addr(conn->u.ipv4.laddr, raddr);
         }
 #endif
 
@@ -208,62 +188,41 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn,
         {
           net_ipv6addr_t raddr;
 
-          if (remote)
-            {
-              FAR const struct sockaddr_in6 *inaddr =
-                (FAR const struct sockaddr_in6 *)remote;
-              net_ipv6addr_copy(raddr, inaddr->sin6_addr.s6_addr16);
-            }
-          else
+          if (!net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr))
             {
-              net_ipv6addr_copy(raddr, conn->u.ipv6.raddr);
-            }
+              /* If the socket is bound to some non-zero, local address.
+               * Normal lookup using the verified local address.
+               */
 
-          /* Check if the remote, destination address is a multicast
-           * address.  If this is the case, select the device
-           * using the locally bound address (assuming that there is one).
-           */
+              return netdev_findby_lipv6addr(conn->u.ipv6.laddr);
+            }
 
-          if (net_is_addr_mcast(raddr))
+#ifdef CONFIG_NET_BINDTODEVICE
+          if (conn->sconn.s_boundto != 0)
             {
-              /* Make sure that the socket is bound to some non-zero, local
-               * address.  The IPv6 unspecified address is used as an
-               * indication that the laddr is uninitialized and that the
-               * socket is, hence, not bound.
+              /* If the socket is bound to a local network device.
+               * Select the network device that has been bound.
+               * If the index is invalid, return NULL.
                */
 
-              if (net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr))
-                {
-                  /* Return the device bound to this UDP socket, if any */
-
-                  return net_bound_device(&conn->sconn);
-                }
-              else
-                {
-                  return netdev_findby_ripv6addr(conn->u.ipv6.laddr,
-                                                 conn->u.ipv6.laddr);
-                }
+              return netdev_findbyindex(conn->sconn.s_boundto);
             }
+#endif
 
-          /* There is no unique device associated with the unspecified
-           * address.
-           */
-
-          else if (!net_ipv6addr_cmp(raddr, g_ipv6_unspecaddr))
+          if (remote)
             {
-              /* Normal lookup using the verified remote address */
-
-              return netdev_findby_ripv6addr(conn->u.ipv6.laddr,
-                                             raddr);
+              FAR const struct sockaddr_in6 *inaddr =
+                (FAR const struct sockaddr_in6 *)remote;
+              net_ipv6addr_copy(raddr, inaddr->sin6_addr.s6_addr16);
             }
           else
             {
-              /* Not a suitable IPv6 unicast address for device lookup.
-               * Return the device bound to this UDP socket, if any.
-               */
-
-              return net_bound_device(&conn->sconn);
+              net_ipv6addr_copy(raddr, conn->u.ipv6.raddr);
             }
+
+          /* Normal lookup using the verified remote address */
+
+          return netdev_findby_ripv6addr(conn->u.ipv6.laddr, raddr);
         }
 #endif
 }
diff --git a/net/utils/Make.defs b/net/utils/Make.defs
index f09accdec2..03ddb8820a 100644
--- a/net/utils/Make.defs
+++ b/net/utils/Make.defs
@@ -53,12 +53,6 @@ else ifeq ($(CONFIG_NET_ICMPv6),y)
   endif
 endif
 
-# Bound device find
-
-ifeq ($(CONFIG_NET_BINDTODEVICE),y)
-NET_CSRCS += net_bounddev.c
-endif
-
 # Include utility build support
 
 DEPPATH += --dep-path utils
diff --git a/net/utils/net_bounddev.c b/net/utils/net_bounddev.c
deleted file mode 100644
index 1885fef90f..0000000000
--- a/net/utils/net_bounddev.c
+++ /dev/null
@@ -1,85 +0,0 @@
-/****************************************************************************
- * net/utils/net_bounddev.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-#include <nuttx/config.h>
-
-#include <nuttx/net/netdev.h>
-#include <nuttx/net/net.h>
-
-#include "netdev/netdev.h"
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: net_bound_device
- *
- * Description:
- *   If the socket is bound to a device, return the reference to the
- *   bound device.
- *
- * Input Parameters:
- *   sconn - Socket connection structure (not currently used).
- *
- * Returned Value:
- *   A reference to the bound device.  If the retained interface index no
- *   longer refers to a valid device, this function will unbind the device
- *   and return an arbitrary network device at the head of the list of
- *   registered devices.  This supports legacy IPv4 DHCPD behavior when
- *   there is only a single registered network device.
- *
- ****************************************************************************/
-
-#ifdef CONFIG_NET_BINDTODEVICE
-FAR struct net_driver_s *net_bound_device(FAR struct socket_conn_s *sconn)
-{
-  FAR struct net_driver_s *dev = NULL;
-
-  /* Is the socket bound to a device? */
-
-  if (sconn->s_boundto != 0)
-    {
-      /* Yes..This socket has been bound to an interface.  Convert the
-       * interface index into a device structure reference.
-       */
-
-      dev = netdev_findbyindex(sconn->s_boundto);
-      if (dev == NULL)
-        {
-          /* No device?  It must have been unregistered.  Un-bind the
-           * socket.
-           */
-
-          sconn->s_boundto = 0;
-        }
-    }
-
-  /* If no device was bound or the bound device is no longer valid,
-   * then let's try the default network device.
-   */
-
-  return dev == NULL ? netdev_default() : dev;
-}
-#endif
-
diff --git a/net/utils/utils.h b/net/utils/utils.h
index 4bf0e343bf..4a9b529a13 100644
--- a/net/utils/utils.h
+++ b/net/utils/utils.h
@@ -311,31 +311,6 @@ uint16_t icmp_chksum_iob(FAR struct iob_s *iob);
 uint16_t icmpv6_chksum(FAR struct net_driver_s *dev, unsigned int iplen);
 #endif
 
-/****************************************************************************
- * Name: net_bound_device
- *
- * Description:
- *   If the socket is bound to a device, return the reference to the
- *   bound device.
- *
- * Input Parameters:
- *   sconn - Socket connection structure (not currently used).
- *
- * Returned Value:
- *   A reference to the bound device.  If the retained interface index no
- *   longer refers to a valid device, this function will unbind the device
- *   and return an arbitrary network device at the head of the list of
- *   registered devices.  This supports legacy IPv4 DHCPD behavior when
- *   there is only a single registered network device.
- *
- ****************************************************************************/
-
-#ifdef CONFIG_NET_BINDTODEVICE
-FAR struct net_driver_s *net_bound_device(FAR struct socket_conn_s *sconn);
-#else
-#  define net_bound_device(c) netdev_default();
-#endif
-
 #undef EXTERN
 #ifdef __cplusplus
 }