You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/01/16 17:37:52 UTC

[incubator-nuttx-apps] branch pr17 updated: Fix dhcpd (#17)

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

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


The following commit(s) were added to refs/heads/pr17 by this push:
     new f67d1ae  Fix dhcpd (#17)
f67d1ae is described below

commit f67d1aec783b53da6bd7d8d9ec8b691481742529
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Fri Jan 17 01:37:45 2020 +0800

    Fix dhcpd (#17)
    
    * dhcpd: fix unused const variable
    
    dhcpd.c:273:29: warning: 'g_anyipaddr' defined but not used [-Wunused-const-variable=]
     static const uint8_t        g_anyipaddr[4] = {0, 0, 0, 0};
                                 ^~~~~~~~~~~
    
    * dhcpd: make dhcpd interface configurable
    * dhcpd: lease address directly if hit in address range
    * netinit/dhcpc: initialize dhcpc_state.  Update the addresses only on request success
---
 examples/dhcpd/host.c      | 17 +++++++++++++++--
 examples/dhcpd/target.c    | 34 ++++++++++++++++++++++++++++------
 include/netutils/dhcpd.h   |  2 +-
 netutils/dhcpd/Kconfig     |  4 ----
 netutils/dhcpd/dhcpd.c     | 36 +++++++++++++++++++++++-------------
 netutils/netinit/netinit.c | 31 ++++++++++++++++++-------------
 6 files changed, 85 insertions(+), 39 deletions(-)

diff --git a/examples/dhcpd/host.c b/examples/dhcpd/host.c
index 479a845..d66170f 100644
--- a/examples/dhcpd/host.c
+++ b/examples/dhcpd/host.c
@@ -37,6 +37,9 @@
  * Included Files
  ****************************************************************************/
 
+#include <stdio.h>
+#include <stdlib.h>
+
 /****************************************************************************
  * Private Data
  ****************************************************************************/
@@ -45,7 +48,7 @@
  * Public Functions
  ****************************************************************************/
 
-extern int dhcpd_run(void);
+extern int dhcpd_run(const char *interface);
 
 /****************************************************************************
  * main
@@ -53,6 +56,16 @@ extern int dhcpd_run(void);
 
 int main(int argc, char **argv, char **envp)
 {
-  dhcpd_run();
+  /* One and only one argument is expected:  The network device name. */
+
+  if (argc != 2)
+    {
+      fprintf(stderr, "ERROR: Invalid number of arguments\n");
+      fprintf(stderr, "Usage: %s <device-name>\n", argv[0]);
+      exit(EXIT_FAILURE);
+    }
+
+  dhcpd_run(argv[1]);
+
   return 0;
 }
diff --git a/examples/dhcpd/target.c b/examples/dhcpd/target.c
index 10f0cc6..77bb8b2 100644
--- a/examples/dhcpd/target.c
+++ b/examples/dhcpd/target.c
@@ -40,6 +40,7 @@
 #include <nuttx/config.h>
 
 #include <stdint.h>
+#include <stdlib.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <net/if.h>
@@ -86,16 +87,37 @@
  ****************************************************************************/
 
 /****************************************************************************
+ * dhcpd_showusage
+ ****************************************************************************/
+
+static void dhcpd_showusage(FAR const char *progname, int exitcode)
+{
+  fprintf(stderr, "Usage: %s <device-name>\n", progname);
+  exit(exitcode);
+}
+
+/****************************************************************************
  * Name: dhcpd_main
  ****************************************************************************/
 
 int main(int argc, FAR char *argv[])
 {
+  FAR const char *devname;
   struct in_addr addr;
 #if defined(CONFIG_EXAMPLES_DHCPD_NOMAC)
   uint8_t mac[IFHWADDRLEN];
 #endif
 
+  /* One and only one argument is expected:  The network device name. */
+
+  if (argc != 2)
+    {
+      fprintf(stderr, "ERROR: Invalid number of arguments\n");
+      dhcpd_showusage(argv[0], EXIT_FAILURE);
+    }
+
+  devname = argv[1];
+
 /* Many embedded network interfaces must have a software assigned MAC */
 
 #ifdef CONFIG_EXAMPLES_DHCPD_NOMAC
@@ -105,32 +127,32 @@ int main(int argc, FAR char *argv[])
   mac[3] = 0xad;
   mac[4] = 0xbe;
   mac[5] = 0xef;
-  netlib_setmacaddr("eth0", mac);
+  netlib_setmacaddr(devname, mac);
 #endif
 
   /* Set up our host address */
 
   addr.s_addr = HTONL(CONFIG_EXAMPLES_DHCPD_IPADDR);
-  netlib_set_ipv4addr("eth0", &addr);
+  netlib_set_ipv4addr(devname, &addr);
 
   /* Set up the default router address */
 
   addr.s_addr = HTONL(CONFIG_EXAMPLES_DHCPD_DRIPADDR);
-  netlib_set_dripv4addr("eth0", &addr);
+  netlib_set_dripv4addr(devname, &addr);
 
   /* Setup the subnet mask */
 
   addr.s_addr = HTONL(CONFIG_EXAMPLES_DHCPD_NETMASK);
-  netlib_set_ipv4netmask("eth0", &addr);
+  netlib_set_ipv4netmask(devname, &addr);
 
   /* New versions of netlib_set_ipvXaddr will not bring the network up,
    * So ensure the network is really up at this point.
    */
 
-  netlib_ifup("eth0");
+  netlib_ifup(devname);
 
   /* Then start the server */
 
-  dhcpd_run();
+  dhcpd_run(devname);
   return 0;
 }
diff --git a/include/netutils/dhcpd.h b/include/netutils/dhcpd.h
index 3e36410..089aa46 100644
--- a/include/netutils/dhcpd.h
+++ b/include/netutils/dhcpd.h
@@ -62,7 +62,7 @@ extern "C"
 #define EXTERN extern
 #endif
 
-int dhcpd_run(void);
+int dhcpd_run(FAR const char *interface);
 
 #undef EXTERN
 #ifdef __cplusplus
diff --git a/netutils/dhcpd/Kconfig b/netutils/dhcpd/Kconfig
index a10e77e..9f6e4c7 100644
--- a/netutils/dhcpd/Kconfig
+++ b/netutils/dhcpd/Kconfig
@@ -32,10 +32,6 @@ config NETUTILS_DHCPD_IGNOREBROADCAST
 		cause the server to ignore the client broadcast flag and always respond
 		with multicast; the value 0 to allows clients to request unicast.
 
-config NETUTILS_DHCPD_INTERFACE
-	string "DHCPD network interface"
-	default "eth0"
-
 config NETUTILS_DHCPD_LEASETIME
 	int "Lease time (seconds)"
 	default 864000
diff --git a/netutils/dhcpd/dhcpd.c b/netutils/dhcpd/dhcpd.c
index 16c7a36..edaee29 100644
--- a/netutils/dhcpd/dhcpd.c
+++ b/netutils/dhcpd/dhcpd.c
@@ -159,10 +159,6 @@
 #  define CONFIG_NETUTILS_DHCPD_MAXLEASETIME (60*60*24*30) /* 30 days */
 #endif
 
-#ifndef CONFIG_NETUTILS_DHCPD_INTERFACE
-#  define CONFIG_NETUTILS_DHCPD_INTERFACE "eth0"
-#endif
-
 #ifndef CONFIG_NETUTILS_DHCPD_MAXLEASES
 #  define CONFIG_NETUTILS_DHCPD_MAXLEASES 16
 #endif
@@ -274,7 +270,6 @@ struct dhcpd_state_s
  ****************************************************************************/
 
 static const uint8_t        g_magiccookie[4] = {99, 130, 83, 99};
-static const uint8_t        g_anyipaddr[4] = {0, 0, 0, 0};
 static struct dhcpd_state_s g_state;
 
 /****************************************************************************
@@ -931,6 +926,10 @@ static int dhcpd_sendpacket(int bbroadcast)
 
    ipaddr = INADDR_BROADCAST;
 #else
+   const uint8_t anyipaddr[4] =
+   {
+   };
+
   /* Determine which address to respond to (or if we need to broadcast the response)
    *
    * (1) If he caller know that it needs to multicast the response, it will set bbroadcast.
@@ -948,7 +947,7 @@ static int dhcpd_sendpacket(int bbroadcast)
     {
       ipaddr = INADDR_BROADCAST;
     }
-  else if (memcmp(g_state.ds_outpacket.ciaddr, g_anyipaddr, 4) != 0)
+  else if (memcmp(g_state.ds_outpacket.ciaddr, anyipaddr, 4) != 0)
     {
       dhcpd_arpupdate(g_state.ds_outpacket.ciaddr, g_state.ds_outpacket.chaddr);
       memcpy(&ipaddr, g_state.ds_outpacket.ciaddr, 4);
@@ -1287,10 +1286,21 @@ static inline int dhcpd_request(void)
             }
         }
 
-      /* No.. is the requested IP address in range? NAK if not */
+      /* DHCPREQUEST without DHCPDISCOVER:
+       * The request IP address is in the range but has not been leased,
+       * maybe requested before the last shutdown, lease again.
+       */
+
+      else if (g_state.ds_optreqip >= CONFIG_NETUTILS_DHCPD_STARTIP &&
+               g_state.ds_optreqip <= CONFIG_NETUTILS_DHCP_OPTION_ENDIP)
+        {
+          ipaddr = g_state.ds_optreqip;
+          response = DHCPACK;
+        }
+
+      /* The requested IP address out of range, negative */
 
-      else if (g_state.ds_optreqip < CONFIG_NETUTILS_DHCPD_STARTIP ||
-               g_state.ds_optreqip > CONFIG_NETUTILS_DHCP_OPTION_ENDIP)
+      else
         {
           response = DHCPNAK;
         }
@@ -1369,7 +1379,7 @@ static inline int dhcpd_release(void)
  * Name: dhcpd_openlistener
  ****************************************************************************/
 
-static inline int dhcpd_openlistener(void)
+static inline int dhcpd_openlistener(FAR const char *interface)
 {
   struct sockaddr_in addr;
   struct ifreq req;
@@ -1387,7 +1397,7 @@ static inline int dhcpd_openlistener(void)
 
   /* Get the IP address of the selected device */
 
-  strncpy(req.ifr_name, CONFIG_NETUTILS_DHCPD_INTERFACE, IFNAMSIZ);
+  strncpy(req.ifr_name, interface, IFNAMSIZ);
   ret = ioctl(sockfd, SIOCGIFADDR, (unsigned long)&req);
   if (ret < 0)
     {
@@ -1427,7 +1437,7 @@ static inline int dhcpd_openlistener(void)
  * Name: dhcpd_run
  ****************************************************************************/
 
-int dhcpd_run(void)
+int dhcpd_run(FAR const char *interface)
 {
   int sockfd;
   int nbytes;
@@ -1447,7 +1457,7 @@ int dhcpd_run(void)
 
       if (sockfd < 0)
         {
-          sockfd = dhcpd_openlistener();
+          sockfd = dhcpd_openlistener(interface);
           if (sockfd < 0)
             {
                 nerr("ERROR: Failed to create socket\n");
diff --git a/netutils/netinit/netinit.c b/netutils/netinit/netinit.c
index d031ddb..bc4c48d 100644
--- a/netutils/netinit/netinit.c
+++ b/netutils/netinit/netinit.c
@@ -445,23 +445,28 @@ static void netinit_net_bringup(void)
 
   if (handle != NULL)
     {
-      struct dhcpc_state ds;
-      dhcpc_request(handle, &ds);
-      netlib_set_ipv4addr(NET_DEVNAME, &ds.ipaddr);
+      struct dhcpc_state ds =
+      {
+      };
 
-      if (ds.netmask.s_addr != 0)
+      if (dhcpc_request(handle, &ds) == OK)
         {
-          netlib_set_ipv4netmask(NET_DEVNAME, &ds.netmask);
-        }
+          netlib_set_ipv4addr(NET_DEVNAME, &ds.ipaddr);
 
-      if (ds.default_router.s_addr != 0)
-        {
-          netlib_set_dripv4addr(NET_DEVNAME, &ds.default_router);
-        }
+          if (ds.netmask.s_addr != 0)
+            {
+              netlib_set_ipv4netmask(NET_DEVNAME, &ds.netmask);
+            }
 
-      if (ds.dnsaddr.s_addr != 0)
-        {
-          netlib_set_ipv4dnsaddr(&ds.dnsaddr);
+          if (ds.default_router.s_addr != 0)
+            {
+              netlib_set_dripv4addr(NET_DEVNAME, &ds.default_router);
+            }
+
+          if (ds.dnsaddr.s_addr != 0)
+            {
+              netlib_set_ipv4dnsaddr(&ds.dnsaddr);
+            }
         }
 
       dhcpc_close(handle);