summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-11-18 07:20:18 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-11-18 07:20:18 -0600
commitae0fd528b6611b348a84690881447432a8407a31 (patch)
tree7e5e622b4e18f57ca4180cdeb8c64c83233ed15a
parent61edbcaa9a4bc2d052e8063e8f5c1289a6ab36cf (diff)
downloadnuttx-ae0fd528b6611b348a84690881447432a8407a31.tar.gz
nuttx-ae0fd528b6611b348a84690881447432a8407a31.tar.bz2
nuttx-ae0fd528b6611b348a84690881447432a8407a31.zip
DHCPD: Add support for the dhcp options for netmask, router and dns. From Brennan Ashton
-rw-r--r--apps/netutils/dhcpd/Kconfig12
-rw-r--r--apps/netutils/dhcpd/dhcpd.c71
2 files changed, 82 insertions, 1 deletions
diff --git a/apps/netutils/dhcpd/Kconfig b/apps/netutils/dhcpd/Kconfig
index 3f586a74b..2a38b7a00 100644
--- a/apps/netutils/dhcpd/Kconfig
+++ b/apps/netutils/dhcpd/Kconfig
@@ -61,6 +61,18 @@ config NETUTILS_DHCPD_STARTIP
hex "First IP address"
default 0x0a000002
+config NETUTILS_DHCPD_ROUTERIP
+ hex "Router IP (0 to disable)"
+ default 0x0a000001
+
+config NETUTILS_DHCPD_NETMASK
+ hex "Netmask (0 to disable)"
+ default 0xffffff00
+
+config NETUTILS_DHCPD_DNSIP
+ hex "DNS (0 to disable)"
+ default 0x08080808
+
config NETUTILS_DHCPD_OFFERTIME
int "Offer time (seconds)"
default 3600
diff --git a/apps/netutils/dhcpd/dhcpd.c b/apps/netutils/dhcpd/dhcpd.c
index b0f171384..817c5d1c0 100644
--- a/apps/netutils/dhcpd/dhcpd.c
+++ b/apps/netutils/dhcpd/dhcpd.c
@@ -86,6 +86,10 @@
/* Code Data Description */
/* Length */
#define DHCP_OPTION_PAD 0 /* 1 Pad */
+#define DHCP_OPTION_SUBNET_MASK 1 /* 1 Subnet Mask */
+#define DHCP_OPTION_ROUTER 3 /* 4 Router */
+#define DHCP_OPTION_DNS_SERVER 6 /* 4N DNS */
+#define DHCP_OPTION_DNS_SERVER_L 4 /* DNS Length */
#define DHCP_OPTION_REQ_IPADDR 50 /* 4 Requested IP Address */
#define DHCP_OPTION_LEASE_TIME 51 /* 4 IP address lease time */
#define DHCP_OPTION_OVERLOAD 52 /* 1 Option overload */
@@ -181,6 +185,21 @@
# define CONFIG_NETUTILS_DHCPD_DECLINETIME (60*60) /* 1 hour */
#endif
+#undef HAVE_ROUTERIP
+#if defined(CONFIG_NETUTILS_DHCPD_ROUTERIP) && CONFIG_NETUTILS_DHCPD_ROUTERIP
+# define HAVE_ROUTERIP 1
+#endif
+
+#undef HAVE_NETMASK
+#if defined(CONFIG_NETUTILS_DHCPD_NETMASK) && CONFIG_NETUTILS_DHCPD_NETMASK
+# define HAVE_NETMASK 1
+#endif
+
+#undef HAVE_DNSIP
+#if defined(CONFIG_NETUTILS_DHCPD_DNSIP) && CONFIG_NETUTILS_DHCPD_DNSIP
+# define HAVE_DNSIP 1
+#endif
+
#undef HAVE_LEASE_TIME
#if defined(CONFIG_NETUTILS_DHCPD_HOST) || !defined(CONFIG_DISABLE_POSIX_TIMERS)
# define HAVE_LEASE_TIME 1
@@ -737,6 +756,31 @@ static int dhcpd_addoption32(uint8_t code, uint32_t value)
}
/****************************************************************************
+ * Name: dhcpd_addoption_n
+ ****************************************************************************/
+
+#if HAVE_DNSIP
+static int dhcpd_addoption_n(uint8_t code, uint8_t *value, uint8_t len)
+{
+ /* REVISIT: This form may not be supported by older compilers. It may
+ * need to be replaced with malloc() and free() for portability.
+ */
+
+ uint8_t option[len+2];
+
+ /* Construct the option sequence */
+
+ option[DHCPD_OPTION_CODE] = code;
+ option[DHCPD_OPTION_LENGTH] = len;
+ memcpy(&option[DHCPD_OPTION_DATA], value, len);
+
+ /* Add the option sequence to the response */
+
+ return dhcpd_addoption(option);
+}
+#endif
+
+/****************************************************************************
* Name: dhcpd_soclet
****************************************************************************/
@@ -953,7 +997,10 @@ static int dhcpd_sendpacket(int bbroadcast)
static inline int dhcpd_sendoffer(in_addr_t ipaddr, uint32_t leasetime)
{
in_addr_t netaddr;
-
+#if HAVE_DNSIP
+ uint32_t dnsaddr;
+ dnsaddr = htonl(CONFIG_NETUTILS_DHCPD_DNSIP);
+#endif
/* IP address is in host order */
nvdbg("Sending offer: %08lx\n", (long)ipaddr);
@@ -970,6 +1017,15 @@ static inline int dhcpd_sendoffer(in_addr_t ipaddr, uint32_t leasetime)
/* Add the leasetime to the response options */
dhcpd_addoption32(DHCP_OPTION_LEASE_TIME, htonl(leasetime));
+#if HAVE_NETMASK
+ dhcpd_addoption32(DHCP_OPTION_SUBNET_MASK, htonl(CONFIG_NETUTILS_DHCPD_NETMASK));
+#endif
+#if HAVE_ROUTERIP
+ dhcpd_addoption32(DHCP_OPTION_ROUTER, htonl(CONFIG_NETUTILS_DHCPD_ROUTERIP));
+#endif
+#if HAVE_DNSIP
+ dhcpd_addoption_n(DHCP_OPTION_DNS_SERVER, (uint8_t*)&dnsaddr, DHCP_OPTION_DNS_SERVER_L);
+#endif
/* Send the offer response */
@@ -1001,6 +1057,10 @@ int dhcpd_sendack(in_addr_t ipaddr)
{
uint32_t leasetime = CONFIG_NETUTILS_DHCPD_LEASETIME;
in_addr_t netaddr;
+#if HAVE_DNSIP
+ uint32_t dnsaddr;
+ dnsaddr = htonl(CONFIG_NETUTILS_DHCPD_DNSIP);
+#endif
/* Initialize the ACK response */
@@ -1019,6 +1079,15 @@ int dhcpd_sendack(in_addr_t ipaddr)
/* Add the lease time to the response */
dhcpd_addoption32(DHCP_OPTION_LEASE_TIME, htonl(leasetime));
+#if HAVE_NETMASK
+ dhcpd_addoption32(DHCP_OPTION_SUBNET_MASK, htonl(CONFIG_NETUTILS_DHCPD_NETMASK));
+#endif
+#if HAVE_ROUTERIP
+ dhcpd_addoption32(DHCP_OPTION_ROUTER, htonl(CONFIG_NETUTILS_DHCPD_ROUTERIP));
+#endif
+#if HAVE_DNSIP
+ dhcpd_addoption_n(DHCP_OPTION_DNS_SERVER, (uint8_t*)&dnsaddr, DHCP_OPTION_DNS_SERVER_L);
+#endif
#ifdef CONFIG_NETUTILS_DHCPD_IGNOREBROADCAST
if (dhcpd_sendpacket(true) < 0)