You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2021/07/23 11:25:43 UTC

[incubator-nuttx-apps] branch master updated: netutils/dhcpd: bind socket to the interface

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

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 496aa3e  netutils/dhcpd: bind socket to the interface
496aa3e is described below

commit 496aa3ef4f389fbb00616eeabbfedc93bb7bfd17
Author: chao.an <an...@xiaomi.com>
AuthorDate: Wed Jun 16 16:11:34 2021 +0800

    netutils/dhcpd: bind socket to the interface
    
    Change-Id: Icf7cf8147ceca5e27b7b923d520e2189741d9882
    Signed-off-by: chao.an <an...@xiaomi.com>
---
 netutils/dhcpd/dhcpd.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/netutils/dhcpd/dhcpd.c b/netutils/dhcpd/dhcpd.c
index e883868..31e2690 100644
--- a/netutils/dhcpd/dhcpd.c
+++ b/netutils/dhcpd/dhcpd.c
@@ -56,6 +56,7 @@
 
 #include <net/if.h>
 #include <netinet/in.h>
+#include <netinet/udp.h>
 #include <arpa/inet.h>
 
 #include "netutils/netlib.h"
@@ -820,7 +821,7 @@ static int dhcp_addoption32p(uint8_t code, FAR uint8_t *value)
  * Name: dhcpd_socket
  ****************************************************************************/
 
-static inline int dhcpd_socket(void)
+static inline int dhcpd_socket(FAR const char *interface)
 {
   int sockfd;
 #if defined(HAVE_SO_REUSEADDR) || defined(HAVE_SO_BROADCAST)
@@ -863,6 +864,22 @@ static inline int dhcpd_socket(void)
     }
 #endif
 
+#ifdef CONFIG_NET_UDP_BINDTODEVICE
+  /* Bind socket to interface, because UDP packets have to be sent to the
+   * broadcast address at a moment when it is not possible to decide the
+   * target network device using the local or remote address (which is,
+   * by definition and purpose of DHCP, undefined yet).
+   */
+
+  if (setsockopt(sockfd, IPPROTO_UDP, UDP_BINDTODEVICE,
+                 interface, strlen(interface)) < 0)
+    {
+      ninfo("ERROR: setsockopt UDP_BINDTODEVICE failed: %d\n", errno);
+      close(sockfd);
+      return ERROR;
+    }
+#endif
+
   return sockfd;
 }
 
@@ -1396,7 +1413,7 @@ static inline int dhcpd_openlistener(FAR const char *interface)
 
   /* Create a socket to listen for requests from DHCP clients */
 
-  sockfd = dhcpd_socket();
+  sockfd = dhcpd_socket(interface);
   if (sockfd < 0)
     {
       nerr("ERROR: socket failed: %d\n", errno);