summaryrefslogtreecommitdiff
path: root/apps/netutils/dhcpd
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-03-03 17:46:56 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-03-03 17:46:56 -0600
commit2f32e25bb007173595e3ff240e8f523693bf272e (patch)
treea31507c11a58cc7581c16ac7ca2e7dd5eb6cfa65 /apps/netutils/dhcpd
parenta81634883601b6d67f0c789dee203b324a5f356c (diff)
downloadnuttx-2f32e25bb007173595e3ff240e8f523693bf272e.tar.gz
nuttx-2f32e25bb007173595e3ff240e8f523693bf272e.tar.bz2
nuttx-2f32e25bb007173595e3ff240e8f523693bf272e.zip
dhcpd_allocipaddr() should not call ntohl(). The return value is already in host order. From Brennan Ashton
Diffstat (limited to 'apps/netutils/dhcpd')
-rw-r--r--apps/netutils/dhcpd/dhcpd.c95
1 files changed, 56 insertions, 39 deletions
diff --git a/apps/netutils/dhcpd/dhcpd.c b/apps/netutils/dhcpd/dhcpd.c
index ce11ee248..ff6eabf94 100644
--- a/apps/netutils/dhcpd/dhcpd.c
+++ b/apps/netutils/dhcpd/dhcpd.c
@@ -1,7 +1,7 @@
/****************************************************************************
* netutils/dhcpd/dhcpd.c
*
- * Copyright (C) 2007-2009, 2011-2013 Gregory Nutt. All rights reserved.
+ * Copyright (C) 2007-2009, 2011-2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@@ -38,26 +38,26 @@
****************************************************************************/
#ifdef CONFIG_NETUTILS_DHCPD_HOST
-# include <stdio.h>
+# include <stdio.h>
-# define HTONS(a) htons(a)
-# define HTONL(a) htonl(a)
+# define HTONS(a) htons(a)
+# define HTONL(a) htonl(a)
-# define CONFIG_CPP_HAVE_WARNING 1
-# define FAR
+# define CONFIG_CPP_HAVE_WARNING 1
+# define FAR
-# define ndbg(...) printf(__VA_ARGS__)
-# define nvdbg(...) printf(__VA_ARGS__)
+# define ndbg(...) printf(__VA_ARGS__)
+# define nvdbg(...) printf(__VA_ARGS__)
-# define ERROR (-1)
-# define OK (0)
+# define ERROR (-1)
+# define OK (0)
#else
-# include <nuttx/config.h> /* NuttX configuration */
-# include <debug.h> /* For ndbg, vdbg */
-# include <nuttx/compiler.h> /* For CONFIG_CPP_HAVE_WARNING */
-# include <arch/irq.h> /* For irqstore() and friends -- REVISIT */
-# include <nuttx/net/uip/uip-arp.h> /* For low-level ARP interfaces -- REVISIT */
-# include <apps/netutils/dhcpd.h> /* Advertised DHCPD APIs */
+# include <nuttx/config.h> /* NuttX configuration */
+# include <debug.h> /* For ndbg, vdbg */
+# include <nuttx/compiler.h> /* For CONFIG_CPP_HAVE_WARNING */
+# include <arch/irq.h> /* For irqstore() and friends -- REVISIT */
+# include <nuttx/net/uip/uip-arp.h> /* For low-level ARP interfaces -- REVISIT */
+# include <apps/netutils/dhcpd.h> /* Advertised DHCPD APIs */
#endif
#include <sys/socket.h>
@@ -143,29 +143,29 @@
#define DHCPD_SNAME_FIELD 2
#ifndef CONFIG_NETUTILS_DHCPD_LEASETIME
-# define CONFIG_NETUTILS_DHCPD_LEASETIME (60*60*24*10) /* 10 days */
-# undef CONFIG_NETUTILS_DHCPD_MINLEASETIME
-# undef CONFIG_NETUTILS_DHCPD_MAXLEASETIME
+# define CONFIG_NETUTILS_DHCPD_LEASETIME (60*60*24*10) /* 10 days */
+# undef CONFIG_NETUTILS_DHCPD_MINLEASETIME
+# undef CONFIG_NETUTILS_DHCPD_MAXLEASETIME
#endif
#ifndef CONFIG_NETUTILS_DHCPD_MINLEASETIME
-# define CONFIG_NETUTILS_DHCPD_MINLEASETIME (60*60*24*1) /* 1 days */
+# define CONFIG_NETUTILS_DHCPD_MINLEASETIME (60*60*24*1) /* 1 days */
#endif
#ifndef CONFIG_NETUTILS_DHCPD_MAXLEASETIME
-# define CONFIG_NETUTILS_DHCPD_MAXLEASETIME (60*60*24*30) /* 30 days */
+# define CONFIG_NETUTILS_DHCPD_MAXLEASETIME (60*60*24*30) /* 30 days */
#endif
#ifndef CONFIG_NETUTILS_DHCPD_INTERFACE
-# define CONFIG_NETUTILS_DHCPD_INTERFACE "eth0"
+# define CONFIG_NETUTILS_DHCPD_INTERFACE "eth0"
#endif
#ifndef CONFIG_NETUTILS_DHCPD_MAXLEASES
-# define CONFIG_NETUTILS_DHCPD_MAXLEASES 16
+# define CONFIG_NETUTILS_DHCPD_MAXLEASES 16
#endif
#ifndef CONFIG_NETUTILS_DHCPD_STARTIP
-# define CONFIG_NETUTILS_DHCPD_STARTIP (10L<<24|0L<<16|0L<<16|2L)
+# define CONFIG_NETUTILS_DHCPD_STARTIP (10L<<24|0L<<16|0L<<16|2L)
#endif
#undef CONFIG_NETUTILS_DHCP_OPTION_ENDIP
@@ -173,16 +173,16 @@
(CONFIG_NETUTILS_DHCPD_STARTIP + CONFIG_NETUTILS_DHCPD_MAXLEASES - 1)
#ifndef CONFIG_NETUTILS_DHCPD_OFFERTIME
-# define CONFIG_NETUTILS_DHCPD_OFFERTIME (60*60) /* 1 hour */
+# define CONFIG_NETUTILS_DHCPD_OFFERTIME (60*60) /* 1 hour */
#endif
#ifndef CONFIG_NETUTILS_DHCPD_DECLINETIME
-# define CONFIG_NETUTILS_DHCPD_DECLINETIME (60*60) /* 1 hour */
+# define CONFIG_NETUTILS_DHCPD_DECLINETIME (60*60) /* 1 hour */
#endif
#undef HAVE_LEASE_TIME
#if defined(CONFIG_NETUTILS_DHCPD_HOST) || !defined(CONFIG_DISABLE_POSIX_TIMERS)
-# define HAVE_LEASE_TIME 1
+# define HAVE_LEASE_TIME 1
#endif
/****************************************************************************
@@ -300,6 +300,7 @@ static time_t dhcpd_time(void)
{
ret = ts.tv_sec;
}
+
return ret;
}
#else
@@ -348,11 +349,11 @@ struct lease_s *dhcpd_setlease(const uint8_t *mac, in_addr_t ipaddr, time_t expi
if (ndx >= 0 && ndx < CONFIG_NETUTILS_DHCPD_MAXLEASES)
{
- ret = &g_state.ds_leases[ndx];
- memcpy(ret->mac, mac, DHCP_HLEN_ETHERNET);
- ret->allocated = true;
+ ret = &g_state.ds_leases[ndx];
+ memcpy(ret->mac, mac, DHCP_HLEN_ETHERNET);
+ ret->allocated = true;
#ifdef HAVE_LEASE_TIME
- ret->expiry = dhcpd_time() + expiry;
+ ret->expiry = dhcpd_time() + expiry;
#endif
}
@@ -404,6 +405,7 @@ static struct lease_s *dhcpd_findbyipaddr(in_addr_t ipaddr)
return lease;
}
}
+
return NULL;
}
@@ -411,7 +413,7 @@ static struct lease_s *dhcpd_findbyipaddr(in_addr_t ipaddr)
* Name: dhcpd_allocipaddr
****************************************************************************/
-in_addr_t dhcpd_allocipaddr(void)
+static in_addr_t dhcpd_allocipaddr(void)
{
struct lease_s *lease = NULL;
in_addr_t ipaddr;
@@ -419,11 +421,15 @@ in_addr_t dhcpd_allocipaddr(void)
ipaddr = CONFIG_NETUTILS_DHCPD_STARTIP;
for (; ipaddr <= CONFIG_NETUTILS_DHCP_OPTION_ENDIP; ipaddr++)
{
+ /* Skip over address ending in 0 or 255 */
+
if ((ipaddr & 0xff) == 0 || (ipaddr & 0xff) == 0xff)
{
continue;
}
+ /* Is there already a lease on this address? If so, has it expired? */
+
lease = dhcpd_findbyipaddr(ipaddr);
if ((!lease || dhcpd_leaseexpired(lease)))
{
@@ -436,9 +442,12 @@ in_addr_t dhcpd_allocipaddr(void)
#ifdef HAVE_LEASE_TIME
g_state.ds_leases[ipaddr - CONFIG_NETUTILS_DHCPD_STARTIP].expiry = dhcpd_time() + CONFIG_NETUTILS_DHCPD_OFFERTIME;
#endif
- return ntohl(ipaddr);
+ /* Return the address in host order */
+
+ return ipaddr;
}
}
+
return 0;
}
@@ -468,7 +477,7 @@ static inline bool dhcpd_parseoptions(void)
/* Set up to parse the options */
- ptr += 4;
+ ptr += 4;
remaining = DHCPD_OPTIONS_SIZE - 4;
overloaded = DHCPD_OPTION_FIELD;
currfield = DHCPD_OPTION_FIELD;
@@ -595,6 +604,7 @@ static inline bool dhcpd_parseoptions(void)
remaining -= optlen;
}
while (remaining > 0);
+
return false;
}
@@ -622,6 +632,7 @@ static inline bool dhcpd_verifyreqip(void)
return true;
}
}
+
return false;
}
@@ -653,6 +664,7 @@ static inline bool dhcpd_verifyreqleasetime(uint32_t *leasetime)
*leasetime = tmp;
return true;
}
+
return false;
}
@@ -681,6 +693,7 @@ static int dhcpd_addoption(uint8_t *option)
*g_state.ds_optend = DHCP_OPTION_END;
}
}
+
return len;
}
@@ -1259,6 +1272,7 @@ static inline int dhcpd_decline(void)
lease->expiry = dhcpd_time() + CONFIG_NETUTILS_DHCPD_DECLINETIME;
#endif
}
+
return OK;
}
@@ -1275,6 +1289,7 @@ static inline int dhcpd_release(void)
memset(lease, 0, sizeof(struct lease_s));
}
+
return OK;
}
@@ -1308,6 +1323,7 @@ static inline int dhcpd_openlistener(void)
close(sockfd);
return ERROR;
}
+
g_state.ds_serverip = ((struct sockaddr_in*)&req.ifr_addr)->sin_addr.s_addr;
nvdbg("serverip: %08lx\n", ntohl(g_state.ds_serverip));
@@ -1321,12 +1337,12 @@ static inline int dhcpd_openlistener(void)
ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
if (ret < 0)
- {
- ndbg("bind failed, port=%d addr=%08lx: %d\n",
+ {
+ ndbg("bind failed, port=%d addr=%08lx: %d\n",
addr.sin_port, (long)addr.sin_addr.s_addr, errno);
- close(sockfd);
- return ERROR;
- }
+ close(sockfd);
+ return ERROR;
+ }
return sockfd;
}
@@ -1429,5 +1445,6 @@ int dhcpd_run(void)
break;
}
}
+
return OK;
}