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 <zhanghongyu@xiaomi.com>
This commit is contained in:
parent
a66f0a6369
commit
a2097cfb71
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -143,6 +143,27 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn,
|
|||
{
|
||||
in_addr_t raddr;
|
||||
|
||||
if (conn->u.ipv4.laddr != INADDR_ANY)
|
||||
{
|
||||
/* If the socket is bound to some non-zero, local address.
|
||||
* Normal lookup using the verified local address.
|
||||
*/
|
||||
|
||||
return netdev_findby_lipv4addr(conn->u.ipv4.laddr);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_NET_BINDTODEVICE
|
||||
if (conn->sconn.s_boundto != 0)
|
||||
{
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
return netdev_findbyindex(conn->sconn.s_boundto);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (remote)
|
||||
{
|
||||
FAR const struct sockaddr_in *inaddr =
|
||||
|
@ -154,50 +175,9 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn,
|
|||
net_ipv4addr_copy(raddr, conn->u.ipv4.raddr);
|
||||
}
|
||||
|
||||
/* 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).
|
||||
*/
|
||||
/* Normal lookup using the verified remote address */
|
||||
|
||||
if (raddr == INADDR_BROADCAST || IN_MULTICAST(NTOHL(raddr)))
|
||||
{
|
||||
/* 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 (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);
|
||||
}
|
||||
}
|
||||
|
||||
/* There is no unique device associated with the unspecified
|
||||
* address.
|
||||
*/
|
||||
|
||||
else if (raddr != INADDR_ANY)
|
||||
{
|
||||
/* Normal lookup using the verified remote address */
|
||||
|
||||
return netdev_findby_ripv4addr(conn->u.ipv4.laddr,
|
||||
raddr);
|
||||
}
|
||||
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);
|
||||
}
|
||||
return netdev_findby_ripv4addr(conn->u.ipv4.laddr, raddr);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -208,6 +188,27 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn,
|
|||
{
|
||||
net_ipv6addr_t raddr;
|
||||
|
||||
if (!net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr))
|
||||
{
|
||||
/* If the socket is bound to some non-zero, local address.
|
||||
* Normal lookup using the verified local address.
|
||||
*/
|
||||
|
||||
return netdev_findby_lipv6addr(conn->u.ipv6.laddr);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_NET_BINDTODEVICE
|
||||
if (conn->sconn.s_boundto != 0)
|
||||
{
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
return netdev_findbyindex(conn->sconn.s_boundto);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (remote)
|
||||
{
|
||||
FAR const struct sockaddr_in6 *inaddr =
|
||||
|
@ -219,51 +220,9 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn,
|
|||
net_ipv6addr_copy(raddr, conn->u.ipv6.raddr);
|
||||
}
|
||||
|
||||
/* 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).
|
||||
*/
|
||||
/* Normal lookup using the verified remote address */
|
||||
|
||||
if (net_is_addr_mcast(raddr))
|
||||
{
|
||||
/* 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 (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);
|
||||
}
|
||||
}
|
||||
|
||||
/* There is no unique device associated with the unspecified
|
||||
* address.
|
||||
*/
|
||||
|
||||
else if (!net_ipv6addr_cmp(raddr, g_ipv6_unspecaddr))
|
||||
{
|
||||
/* Normal lookup using the verified remote address */
|
||||
|
||||
return netdev_findby_ripv6addr(conn->u.ipv6.laddr,
|
||||
raddr);
|
||||
}
|
||||
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);
|
||||
}
|
||||
return netdev_findby_ripv6addr(conn->u.ipv6.laddr, raddr);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue