mirror of
https://github.com/apache/nuttx.git
synced 2026-05-26 10:46:28 +08:00
net/sockopt: move BINDTODEVICE to socket level
rename the UDP_BINDTODEVICE to SO_BINDTODEVICE to follow the linux style to be compatible with non-UDP protocol binding requirements Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
@@ -34,6 +34,7 @@
|
||||
#include <arch/irq.h>
|
||||
|
||||
#include <nuttx/net/net.h>
|
||||
#include <netdev/netdev.h>
|
||||
|
||||
#include "socket/socket.h"
|
||||
#include "inet/inet.h"
|
||||
@@ -399,6 +400,51 @@ static int psock_socketlevel_option(FAR struct socket *psock, int option,
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_NET_BINDTODEVICE
|
||||
/* Handle the SO_BINDTODEVICE socket-level option.
|
||||
*
|
||||
* NOTE: this option makes sense for UDP sockets trying to broadcast
|
||||
* while their local address is not set, eg, with DHCP requests.
|
||||
* The problem is that we are not able to determine the interface to be
|
||||
* used for sending packets when multiple interfaces do not have a
|
||||
* local address yet. This option can be used to "force" the interface
|
||||
* used to send the UDP traffic in this connection. Note that it does
|
||||
* NOT only apply to broadcast packets.
|
||||
*/
|
||||
|
||||
case SO_BINDTODEVICE: /* Bind socket to a specific network device */
|
||||
{
|
||||
FAR struct net_driver_s *dev;
|
||||
|
||||
/* Check if we are are unbinding the socket */
|
||||
|
||||
if (value == NULL || value_len == 0 ||
|
||||
(value_len > 0 && ((FAR char *)value)[0] == 0))
|
||||
{
|
||||
conn->s_boundto = 0; /* This interface is no longer bound */
|
||||
break;
|
||||
}
|
||||
|
||||
/* No, we are binding a socket to the interface
|
||||
* Find the interface device with this name.
|
||||
*/
|
||||
|
||||
dev = netdev_findbyname(value);
|
||||
if (dev == NULL)
|
||||
{
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Bind the socket to the interface */
|
||||
|
||||
DEBUGASSERT(dev->d_ifindex > 0 &&
|
||||
dev->d_ifindex <= MAX_IFINDEX);
|
||||
conn->s_boundto = dev->d_ifindex;
|
||||
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* The following are not yet implemented */
|
||||
|
||||
case SO_RCVLOWAT: /* Sets the minimum number of bytes to input */
|
||||
|
||||
Reference in New Issue
Block a user