From f71c623cfb4d09151286ecbb63b4983808c5abdc Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Tue, 10 Aug 2010 16:39:34 +0000
Subject: [PATCH] Lease time is now in host order

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2839 42af7a65-404d-4744-a932-0658087f49c3
---
 examples/nsh/nsh_telnetd.c |  4 +++-
 examples/uip/main.c        |  4 +++-
 include/net/uip/dhcpc.h    |  4 ++--
 netutils/dhcpc/dhcpc.c     | 27 ++++++++++++++++++++++++---
 4 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/examples/nsh/nsh_telnetd.c b/examples/nsh/nsh_telnetd.c
index d9a5902171..26a7f854ac 100644
--- a/examples/nsh/nsh_telnetd.c
+++ b/examples/nsh/nsh_telnetd.c
@@ -839,7 +839,9 @@ int nsh_telnetmain(int argc, char *argv[])
 
   handle = dhcpc_open(&mac, IFHWADDRLEN);
 
-  /* Get an IP address */
+  /* Get an IP address.  Note that there is no logic for renewing the IP address in this
+   * example.  The address should be renewed in ds.lease_time/2 seconds.
+   */
 
   if (handle)
     {
diff --git a/examples/uip/main.c b/examples/uip/main.c
index d5df1f8070..39f451a26c 100644
--- a/examples/uip/main.c
+++ b/examples/uip/main.c
@@ -170,7 +170,9 @@ int user_start(int argc, char *argv[])
 
   handle = dhcpc_open(&mac, IFHWADDRLEN);
 
-  /* Get an IP address */
+  /* Get an IP address.  Note:  there is no logic here for renewing the address in this
+   * example.  The address should be renewed in ds.lease_time/2 seconds.
+   */
 
   printf("Getting IP address\n");
   if (handle)
diff --git a/include/net/uip/dhcpc.h b/include/net/uip/dhcpc.h
index 8535c1b57d..3d2fe41097 100644
--- a/include/net/uip/dhcpc.h
+++ b/include/net/uip/dhcpc.h
@@ -1,7 +1,7 @@
 /****************************************************************************
  * net/uip/dhcpc.n
  *
- *   Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2009-2010 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * This logic was leveraged from uIP which also has a BSD-style license:
@@ -54,12 +54,12 @@
 
 struct dhcpc_state
 {
-  uint16_t       lease_time[2];
   struct in_addr serverid;
   struct in_addr ipaddr;
   struct in_addr netmask;
   struct in_addr dnsaddr;
   struct in_addr default_router;
+  uint32_t       lease_time;      /* Lease expires in this number of seconds */
 };
 
 /****************************************************************************
diff --git a/netutils/dhcpc/dhcpc.c b/netutils/dhcpc/dhcpc.c
index e35b0a1168..79f806ad5f 100644
--- a/netutils/dhcpc/dhcpc.c
+++ b/netutils/dhcpc/dhcpc.c
@@ -273,23 +273,45 @@ static uint8_t dhcpc_parseoptions(struct dhcpc_state *presult, uint8_t *optptr,
       switch(*optptr)
         {
           case DHCP_OPTION_SUBNET_MASK:
+            /* Get subnet mask in network order */
+
             memcpy(&presult->netmask.s_addr, optptr + 2, 4);
             break;
+
           case DHCP_OPTION_ROUTER:
+            /* Get the default router address in network order */
+
             memcpy(&presult->default_router.s_addr, optptr + 2, 4);
             break;
+
           case DHCP_OPTION_DNS_SERVER:
+            /* Get the DNS server address in network order */
+
             memcpy(&presult->dnsaddr.s_addr, optptr + 2, 4);
             break;
+
           case DHCP_OPTION_MSG_TYPE:
+            /* Get message type */
+
             type = *(optptr + 2);
             break;
+
           case DHCP_OPTION_SERVER_ID:
+            /* Get server address in network order */
+
             memcpy(&presult->serverid.s_addr, optptr + 2, 4);
             break;
+
           case DHCP_OPTION_LEASE_TIME:
-            memcpy(presult->lease_time, optptr + 2, 4);
+            {
+              /* Get lease time (in seconds) in host order */
+
+              uint16_t tmp[2];
+              memcpy(tmp, optptr + 2, 4);
+              presult->lease_time = ((uint32_t)ntohs(tmp[0])) << 16 | (uint32_t)ntohs(tmp[1]);
+            }
             break;
+
           case DHCP_OPTION_END:
             return type;
         }
@@ -580,7 +602,6 @@ int dhcpc_request(void *handle, struct dhcpc_state *presult)
       (presult->default_router.s_addr >> 16 ) & 0xff,
       (presult->default_router.s_addr >> 8  ) & 0xff,
       (presult->default_router.s_addr       ) & 0xff);
-  dbg("Lease expires in %ld seconds\n",
-      ntohs(presult->lease_time[0])*65536ul + ntohs(presult->lease_time[1]));
+  dbg("Lease expires in %d seconds\n", presult->lease_time);
   return OK;
 }
-- 
GitLab