Skip to content

Commit a2097cf

Browse files
zhhyu7xiaoxiang781216
authored andcommitted
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>
1 parent a66f0a6 commit a2097cf

File tree

6 files changed

+59
-204
lines changed

6 files changed

+59
-204
lines changed

net/icmp/icmp_sendmsg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ ssize_t icmp_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
306306
#ifdef CONFIG_NET_BINDTODEVICE
307307
if (conn->sconn.s_boundto != 0)
308308
{
309-
dev = net_bound_device(&conn->sconn);
309+
dev = netdev_findbyindex(conn->sconn.s_boundto);
310310
}
311311
else
312312
#endif

net/tcp/tcp_finddev.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ static int tcp_find_ipv4_device(FAR struct tcp_conn_s *conn,
7777
{
7878
if (local)
7979
{
80-
conn->dev = net_bound_device(&conn->sconn);
80+
#ifdef CONFIG_NET_BINDTODEVICE
81+
if (conn->sconn.s_boundto != 0)
82+
{
83+
conn->dev = netdev_findbyindex(conn->sconn.s_boundto);
84+
}
85+
#endif
86+
8187
return OK;
8288
}
8389

@@ -133,7 +139,13 @@ static int tcp_find_ipv6_device(FAR struct tcp_conn_s *conn,
133139
{
134140
if (local)
135141
{
136-
conn->dev = net_bound_device(&conn->sconn);
142+
#ifdef CONFIG_NET_BINDTODEVICE
143+
if (conn->sconn.s_boundto != 0)
144+
{
145+
conn->dev = netdev_findbyindex(conn->sconn.s_boundto);
146+
}
147+
#endif
148+
137149
return OK;
138150
}
139151

net/udp/udp_finddev.c

Lines changed: 44 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -143,61 +143,41 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn,
143143
{
144144
in_addr_t raddr;
145145

146-
if (remote)
147-
{
148-
FAR const struct sockaddr_in *inaddr =
149-
(FAR const struct sockaddr_in *)remote;
150-
net_ipv4addr_copy(raddr, inaddr->sin_addr.s_addr);
151-
}
152-
else
146+
if (conn->u.ipv4.laddr != INADDR_ANY)
153147
{
154-
net_ipv4addr_copy(raddr, conn->u.ipv4.raddr);
155-
}
148+
/* If the socket is bound to some non-zero, local address.
149+
* Normal lookup using the verified local address.
150+
*/
156151

157-
/* Check if the remote, destination address is the broadcast
158-
* or multicast address. If this is the case, select the device
159-
* using the locally bound address (assuming that there is one).
160-
*/
152+
return netdev_findby_lipv4addr(conn->u.ipv4.laddr);
153+
}
161154

162-
if (raddr == INADDR_BROADCAST || IN_MULTICAST(NTOHL(raddr)))
155+
#ifdef CONFIG_NET_BINDTODEVICE
156+
if (conn->sconn.s_boundto != 0)
163157
{
164-
/* Make sure that the socket is bound to some non-zero, local
165-
* address. Zero is used as an indication that the laddr is
166-
* uninitialized and that the socket is, hence, not bound.
158+
/* If the socket is bound to a local network device.
159+
* Select the network device that has been bound.
160+
* If the index is invalid, return NULL.
167161
*/
168162

169-
if (conn->u.ipv4.laddr == 0) /* INADDR_ANY */
170-
{
171-
/* Return the device bound to this UDP socket, if any */
172-
173-
return net_bound_device(&conn->sconn);
174-
}
175-
else
176-
{
177-
return netdev_findby_ripv4addr(conn->u.ipv4.laddr,
178-
conn->u.ipv4.laddr);
179-
}
163+
return netdev_findbyindex(conn->sconn.s_boundto);
180164
}
165+
#endif
181166

182-
/* There is no unique device associated with the unspecified
183-
* address.
184-
*/
185-
186-
else if (raddr != INADDR_ANY)
167+
if (remote)
187168
{
188-
/* Normal lookup using the verified remote address */
189-
190-
return netdev_findby_ripv4addr(conn->u.ipv4.laddr,
191-
raddr);
169+
FAR const struct sockaddr_in *inaddr =
170+
(FAR const struct sockaddr_in *)remote;
171+
net_ipv4addr_copy(raddr, inaddr->sin_addr.s_addr);
192172
}
193173
else
194174
{
195-
/* Not a suitable IPv4 unicast address for device lookup.
196-
* Return the device bound to this UDP socket, if any.
197-
*/
198-
199-
return net_bound_device(&conn->sconn);
175+
net_ipv4addr_copy(raddr, conn->u.ipv4.raddr);
200176
}
177+
178+
/* Normal lookup using the verified remote address */
179+
180+
return netdev_findby_ripv4addr(conn->u.ipv4.laddr, raddr);
201181
}
202182
#endif
203183

@@ -208,62 +188,41 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn,
208188
{
209189
net_ipv6addr_t raddr;
210190

211-
if (remote)
212-
{
213-
FAR const struct sockaddr_in6 *inaddr =
214-
(FAR const struct sockaddr_in6 *)remote;
215-
net_ipv6addr_copy(raddr, inaddr->sin6_addr.s6_addr16);
216-
}
217-
else
191+
if (!net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr))
218192
{
219-
net_ipv6addr_copy(raddr, conn->u.ipv6.raddr);
220-
}
193+
/* If the socket is bound to some non-zero, local address.
194+
* Normal lookup using the verified local address.
195+
*/
221196

222-
/* Check if the remote, destination address is a multicast
223-
* address. If this is the case, select the device
224-
* using the locally bound address (assuming that there is one).
225-
*/
197+
return netdev_findby_lipv6addr(conn->u.ipv6.laddr);
198+
}
226199

227-
if (net_is_addr_mcast(raddr))
200+
#ifdef CONFIG_NET_BINDTODEVICE
201+
if (conn->sconn.s_boundto != 0)
228202
{
229-
/* Make sure that the socket is bound to some non-zero, local
230-
* address. The IPv6 unspecified address is used as an
231-
* indication that the laddr is uninitialized and that the
232-
* socket is, hence, not bound.
203+
/* If the socket is bound to a local network device.
204+
* Select the network device that has been bound.
205+
* If the index is invalid, return NULL.
233206
*/
234207

235-
if (net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr))
236-
{
237-
/* Return the device bound to this UDP socket, if any */
238-
239-
return net_bound_device(&conn->sconn);
240-
}
241-
else
242-
{
243-
return netdev_findby_ripv6addr(conn->u.ipv6.laddr,
244-
conn->u.ipv6.laddr);
245-
}
208+
return netdev_findbyindex(conn->sconn.s_boundto);
246209
}
210+
#endif
247211

248-
/* There is no unique device associated with the unspecified
249-
* address.
250-
*/
251-
252-
else if (!net_ipv6addr_cmp(raddr, g_ipv6_unspecaddr))
212+
if (remote)
253213
{
254-
/* Normal lookup using the verified remote address */
255-
256-
return netdev_findby_ripv6addr(conn->u.ipv6.laddr,
257-
raddr);
214+
FAR const struct sockaddr_in6 *inaddr =
215+
(FAR const struct sockaddr_in6 *)remote;
216+
net_ipv6addr_copy(raddr, inaddr->sin6_addr.s6_addr16);
258217
}
259218
else
260219
{
261-
/* Not a suitable IPv6 unicast address for device lookup.
262-
* Return the device bound to this UDP socket, if any.
263-
*/
264-
265-
return net_bound_device(&conn->sconn);
220+
net_ipv6addr_copy(raddr, conn->u.ipv6.raddr);
266221
}
222+
223+
/* Normal lookup using the verified remote address */
224+
225+
return netdev_findby_ripv6addr(conn->u.ipv6.laddr, raddr);
267226
}
268227
#endif
269228
}

net/utils/Make.defs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ else ifeq ($(CONFIG_NET_ICMPv6),y)
5353
endif
5454
endif
5555

56-
# Bound device find
57-
58-
ifeq ($(CONFIG_NET_BINDTODEVICE),y)
59-
NET_CSRCS += net_bounddev.c
60-
endif
61-
6256
# Include utility build support
6357

6458
DEPPATH += --dep-path utils

net/utils/net_bounddev.c

Lines changed: 0 additions & 85 deletions
This file was deleted.

net/utils/utils.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -311,31 +311,6 @@ uint16_t icmp_chksum_iob(FAR struct iob_s *iob);
311311
uint16_t icmpv6_chksum(FAR struct net_driver_s *dev, unsigned int iplen);
312312
#endif
313313

314-
/****************************************************************************
315-
* Name: net_bound_device
316-
*
317-
* Description:
318-
* If the socket is bound to a device, return the reference to the
319-
* bound device.
320-
*
321-
* Input Parameters:
322-
* sconn - Socket connection structure (not currently used).
323-
*
324-
* Returned Value:
325-
* A reference to the bound device. If the retained interface index no
326-
* longer refers to a valid device, this function will unbind the device
327-
* and return an arbitrary network device at the head of the list of
328-
* registered devices. This supports legacy IPv4 DHCPD behavior when
329-
* there is only a single registered network device.
330-
*
331-
****************************************************************************/
332-
333-
#ifdef CONFIG_NET_BINDTODEVICE
334-
FAR struct net_driver_s *net_bound_device(FAR struct socket_conn_s *sconn);
335-
#else
336-
# define net_bound_device(c) netdev_default();
337-
#endif
338-
339314
#undef EXTERN
340315
#ifdef __cplusplus
341316
}

0 commit comments

Comments
 (0)