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:
chao.an
2022-08-24 10:30:01 +08:00
committed by Xiang Xiao
parent 50177dbae1
commit 9cb17841d8
17 changed files with 76 additions and 185 deletions
+46
View File
@@ -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 */