dhcpd_allocipaddr() should not call ntohl(). The return value is already in host order. From Brennan Ashton
This commit is contained in:
parent
003c3ded18
commit
9b172004c0
@ -851,3 +851,6 @@
|
|||||||
'cat' command. This prevents the NSH prompt from be in the same line
|
'cat' command. This prevents the NSH prompt from be in the same line
|
||||||
as the final line of the file in the case where there is no newline
|
as the final line of the file in the case where there is no newline
|
||||||
at the end of the file (2014-2-27).
|
at the end of the file (2014-2-27).
|
||||||
|
* netutils/dhcpd/dhcpd.c: allocipaddr() should not call ntohl(), the
|
||||||
|
returned IP address is already in host order.
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* netutils/dhcpd/dhcpd.c
|
* 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>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@ -38,26 +38,26 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifdef CONFIG_NETUTILS_DHCPD_HOST
|
#ifdef CONFIG_NETUTILS_DHCPD_HOST
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
|
|
||||||
# define HTONS(a) htons(a)
|
# define HTONS(a) htons(a)
|
||||||
# define HTONL(a) htonl(a)
|
# define HTONL(a) htonl(a)
|
||||||
|
|
||||||
# define CONFIG_CPP_HAVE_WARNING 1
|
# define CONFIG_CPP_HAVE_WARNING 1
|
||||||
# define FAR
|
# define FAR
|
||||||
|
|
||||||
# define ndbg(...) printf(__VA_ARGS__)
|
# define ndbg(...) printf(__VA_ARGS__)
|
||||||
# define nvdbg(...) printf(__VA_ARGS__)
|
# define nvdbg(...) printf(__VA_ARGS__)
|
||||||
|
|
||||||
# define ERROR (-1)
|
# define ERROR (-1)
|
||||||
# define OK (0)
|
# define OK (0)
|
||||||
#else
|
#else
|
||||||
# include <nuttx/config.h> /* NuttX configuration */
|
# include <nuttx/config.h> /* NuttX configuration */
|
||||||
# include <debug.h> /* For ndbg, vdbg */
|
# include <debug.h> /* For ndbg, vdbg */
|
||||||
# include <nuttx/compiler.h> /* For CONFIG_CPP_HAVE_WARNING */
|
# include <nuttx/compiler.h> /* For CONFIG_CPP_HAVE_WARNING */
|
||||||
# include <arch/irq.h> /* For irqstore() and friends -- REVISIT */
|
# include <arch/irq.h> /* For irqstore() and friends -- REVISIT */
|
||||||
# include <nuttx/net/uip/uip-arp.h> /* For low-level ARP interfaces -- REVISIT */
|
# include <nuttx/net/uip/uip-arp.h> /* For low-level ARP interfaces -- REVISIT */
|
||||||
# include <apps/netutils/dhcpd.h> /* Advertised DHCPD APIs */
|
# include <apps/netutils/dhcpd.h> /* Advertised DHCPD APIs */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@ -143,29 +143,29 @@
|
|||||||
#define DHCPD_SNAME_FIELD 2
|
#define DHCPD_SNAME_FIELD 2
|
||||||
|
|
||||||
#ifndef CONFIG_NETUTILS_DHCPD_LEASETIME
|
#ifndef CONFIG_NETUTILS_DHCPD_LEASETIME
|
||||||
# define CONFIG_NETUTILS_DHCPD_LEASETIME (60*60*24*10) /* 10 days */
|
# define CONFIG_NETUTILS_DHCPD_LEASETIME (60*60*24*10) /* 10 days */
|
||||||
# undef CONFIG_NETUTILS_DHCPD_MINLEASETIME
|
# undef CONFIG_NETUTILS_DHCPD_MINLEASETIME
|
||||||
# undef CONFIG_NETUTILS_DHCPD_MAXLEASETIME
|
# undef CONFIG_NETUTILS_DHCPD_MAXLEASETIME
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_NETUTILS_DHCPD_MINLEASETIME
|
#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
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_NETUTILS_DHCPD_MAXLEASETIME
|
#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
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_NETUTILS_DHCPD_INTERFACE
|
#ifndef CONFIG_NETUTILS_DHCPD_INTERFACE
|
||||||
# define CONFIG_NETUTILS_DHCPD_INTERFACE "eth0"
|
# define CONFIG_NETUTILS_DHCPD_INTERFACE "eth0"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_NETUTILS_DHCPD_MAXLEASES
|
#ifndef CONFIG_NETUTILS_DHCPD_MAXLEASES
|
||||||
# define CONFIG_NETUTILS_DHCPD_MAXLEASES 16
|
# define CONFIG_NETUTILS_DHCPD_MAXLEASES 16
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_NETUTILS_DHCPD_STARTIP
|
#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
|
#endif
|
||||||
|
|
||||||
#undef CONFIG_NETUTILS_DHCP_OPTION_ENDIP
|
#undef CONFIG_NETUTILS_DHCP_OPTION_ENDIP
|
||||||
@ -173,16 +173,16 @@
|
|||||||
(CONFIG_NETUTILS_DHCPD_STARTIP + CONFIG_NETUTILS_DHCPD_MAXLEASES - 1)
|
(CONFIG_NETUTILS_DHCPD_STARTIP + CONFIG_NETUTILS_DHCPD_MAXLEASES - 1)
|
||||||
|
|
||||||
#ifndef CONFIG_NETUTILS_DHCPD_OFFERTIME
|
#ifndef CONFIG_NETUTILS_DHCPD_OFFERTIME
|
||||||
# define CONFIG_NETUTILS_DHCPD_OFFERTIME (60*60) /* 1 hour */
|
# define CONFIG_NETUTILS_DHCPD_OFFERTIME (60*60) /* 1 hour */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_NETUTILS_DHCPD_DECLINETIME
|
#ifndef CONFIG_NETUTILS_DHCPD_DECLINETIME
|
||||||
# define CONFIG_NETUTILS_DHCPD_DECLINETIME (60*60) /* 1 hour */
|
# define CONFIG_NETUTILS_DHCPD_DECLINETIME (60*60) /* 1 hour */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#undef HAVE_LEASE_TIME
|
#undef HAVE_LEASE_TIME
|
||||||
#if defined(CONFIG_NETUTILS_DHCPD_HOST) || !defined(CONFIG_DISABLE_POSIX_TIMERS)
|
#if defined(CONFIG_NETUTILS_DHCPD_HOST) || !defined(CONFIG_DISABLE_POSIX_TIMERS)
|
||||||
# define HAVE_LEASE_TIME 1
|
# define HAVE_LEASE_TIME 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -300,6 +300,7 @@ static time_t dhcpd_time(void)
|
|||||||
{
|
{
|
||||||
ret = ts.tv_sec;
|
ret = ts.tv_sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#else
|
#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)
|
if (ndx >= 0 && ndx < CONFIG_NETUTILS_DHCPD_MAXLEASES)
|
||||||
{
|
{
|
||||||
ret = &g_state.ds_leases[ndx];
|
ret = &g_state.ds_leases[ndx];
|
||||||
memcpy(ret->mac, mac, DHCP_HLEN_ETHERNET);
|
memcpy(ret->mac, mac, DHCP_HLEN_ETHERNET);
|
||||||
ret->allocated = true;
|
ret->allocated = true;
|
||||||
#ifdef HAVE_LEASE_TIME
|
#ifdef HAVE_LEASE_TIME
|
||||||
ret->expiry = dhcpd_time() + expiry;
|
ret->expiry = dhcpd_time() + expiry;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,6 +405,7 @@ static struct lease_s *dhcpd_findbyipaddr(in_addr_t ipaddr)
|
|||||||
return lease;
|
return lease;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -411,7 +413,7 @@ static struct lease_s *dhcpd_findbyipaddr(in_addr_t ipaddr)
|
|||||||
* Name: dhcpd_allocipaddr
|
* Name: dhcpd_allocipaddr
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
in_addr_t dhcpd_allocipaddr(void)
|
static in_addr_t dhcpd_allocipaddr(void)
|
||||||
{
|
{
|
||||||
struct lease_s *lease = NULL;
|
struct lease_s *lease = NULL;
|
||||||
in_addr_t ipaddr;
|
in_addr_t ipaddr;
|
||||||
@ -419,11 +421,15 @@ in_addr_t dhcpd_allocipaddr(void)
|
|||||||
ipaddr = CONFIG_NETUTILS_DHCPD_STARTIP;
|
ipaddr = CONFIG_NETUTILS_DHCPD_STARTIP;
|
||||||
for (; ipaddr <= CONFIG_NETUTILS_DHCP_OPTION_ENDIP; ipaddr++)
|
for (; ipaddr <= CONFIG_NETUTILS_DHCP_OPTION_ENDIP; ipaddr++)
|
||||||
{
|
{
|
||||||
|
/* Skip over address ending in 0 or 255 */
|
||||||
|
|
||||||
if ((ipaddr & 0xff) == 0 || (ipaddr & 0xff) == 0xff)
|
if ((ipaddr & 0xff) == 0 || (ipaddr & 0xff) == 0xff)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Is there already a lease on this address? If so, has it expired? */
|
||||||
|
|
||||||
lease = dhcpd_findbyipaddr(ipaddr);
|
lease = dhcpd_findbyipaddr(ipaddr);
|
||||||
if ((!lease || dhcpd_leaseexpired(lease)))
|
if ((!lease || dhcpd_leaseexpired(lease)))
|
||||||
{
|
{
|
||||||
@ -436,9 +442,12 @@ in_addr_t dhcpd_allocipaddr(void)
|
|||||||
#ifdef HAVE_LEASE_TIME
|
#ifdef HAVE_LEASE_TIME
|
||||||
g_state.ds_leases[ipaddr - CONFIG_NETUTILS_DHCPD_STARTIP].expiry = dhcpd_time() + CONFIG_NETUTILS_DHCPD_OFFERTIME;
|
g_state.ds_leases[ipaddr - CONFIG_NETUTILS_DHCPD_STARTIP].expiry = dhcpd_time() + CONFIG_NETUTILS_DHCPD_OFFERTIME;
|
||||||
#endif
|
#endif
|
||||||
return ntohl(ipaddr);
|
/* Return the address in host order */
|
||||||
|
|
||||||
|
return ipaddr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -468,7 +477,7 @@ static inline bool dhcpd_parseoptions(void)
|
|||||||
|
|
||||||
/* Set up to parse the options */
|
/* Set up to parse the options */
|
||||||
|
|
||||||
ptr += 4;
|
ptr += 4;
|
||||||
remaining = DHCPD_OPTIONS_SIZE - 4;
|
remaining = DHCPD_OPTIONS_SIZE - 4;
|
||||||
overloaded = DHCPD_OPTION_FIELD;
|
overloaded = DHCPD_OPTION_FIELD;
|
||||||
currfield = DHCPD_OPTION_FIELD;
|
currfield = DHCPD_OPTION_FIELD;
|
||||||
@ -595,6 +604,7 @@ static inline bool dhcpd_parseoptions(void)
|
|||||||
remaining -= optlen;
|
remaining -= optlen;
|
||||||
}
|
}
|
||||||
while (remaining > 0);
|
while (remaining > 0);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -622,6 +632,7 @@ static inline bool dhcpd_verifyreqip(void)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -653,6 +664,7 @@ static inline bool dhcpd_verifyreqleasetime(uint32_t *leasetime)
|
|||||||
*leasetime = tmp;
|
*leasetime = tmp;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -681,6 +693,7 @@ static int dhcpd_addoption(uint8_t *option)
|
|||||||
*g_state.ds_optend = DHCP_OPTION_END;
|
*g_state.ds_optend = DHCP_OPTION_END;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1259,6 +1272,7 @@ static inline int dhcpd_decline(void)
|
|||||||
lease->expiry = dhcpd_time() + CONFIG_NETUTILS_DHCPD_DECLINETIME;
|
lease->expiry = dhcpd_time() + CONFIG_NETUTILS_DHCPD_DECLINETIME;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1275,6 +1289,7 @@ static inline int dhcpd_release(void)
|
|||||||
|
|
||||||
memset(lease, 0, sizeof(struct lease_s));
|
memset(lease, 0, sizeof(struct lease_s));
|
||||||
}
|
}
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1308,6 +1323,7 @@ static inline int dhcpd_openlistener(void)
|
|||||||
close(sockfd);
|
close(sockfd);
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_state.ds_serverip = ((struct sockaddr_in*)&req.ifr_addr)->sin_addr.s_addr;
|
g_state.ds_serverip = ((struct sockaddr_in*)&req.ifr_addr)->sin_addr.s_addr;
|
||||||
nvdbg("serverip: %08lx\n", ntohl(g_state.ds_serverip));
|
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));
|
ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
|
||||||
if (ret < 0)
|
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);
|
addr.sin_port, (long)addr.sin_addr.s_addr, errno);
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sockfd;
|
return sockfd;
|
||||||
}
|
}
|
||||||
@ -1429,5 +1445,6 @@ int dhcpd_run(void)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user