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

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


The following commit(s) were added to refs/heads/master by this push:
     new 8f3c3c57f netutils/dhcpc/dhcpc: add implementation to get renewal (T1) 
time and rebinding (T2) time from DHCP packet. According to RFC 2131, T1 and T2 
times play a critical role in lease management in DHCP clients.
8f3c3c57f is described below

commit 8f3c3c57f9997c8b01834292fbc034dc7775dffd
Author: nuttxs <zhaoqing.zh...@sony.com>
AuthorDate: Tue Aug 5 18:51:19 2025 +0800

    netutils/dhcpc/dhcpc: add implementation to get renewal (T1) time and
    rebinding (T2) time from DHCP packet.
    According to RFC 2131, T1 and T2 times play a critical role in lease
    management in DHCP clients.
    
    Signed-off-by: nuttxs <zhaoqing.zh...@sony.com>
---
 include/netutils/dhcpc.h |  2 ++
 netutils/dhcpc/dhcpc.c   | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/include/netutils/dhcpc.h b/include/netutils/dhcpc.h
index 4a5c451bd..3529e554f 100644
--- a/include/netutils/dhcpc.h
+++ b/include/netutils/dhcpc.h
@@ -58,6 +58,8 @@ struct dhcpc_state
   struct in_addr dnsaddr;
   struct in_addr default_router;
   uint32_t       lease_time;      /* Lease expires in this number of seconds */
+  uint32_t       renewal_time;    /* Seconds to transition to RENEW state(T1) 
*/
+  uint32_t       rebinding_time;  /* Seconds to transition to REBIND state(T2) 
*/
 };
 
 typedef void (*dhcpc_callback_t)(FAR struct dhcpc_state *presult);
diff --git a/netutils/dhcpc/dhcpc.c b/netutils/dhcpc/dhcpc.c
index 724894bb9..3b0c2e191 100644
--- a/netutils/dhcpc/dhcpc.c
+++ b/netutils/dhcpc/dhcpc.c
@@ -96,6 +96,8 @@
 #define DHCP_OPTION_MSG_TYPE    53
 #define DHCP_OPTION_SERVER_ID   54
 #define DHCP_OPTION_REQ_LIST    55
+#define DHCP_OPTION_T1_TIME     58
+#define DHCP_OPTION_T2_TIME     59
 #define DHCP_OPTION_CLIENT_ID   61
 #define DHCP_OPTION_END         255
 
@@ -340,6 +342,7 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state 
*presult,
 {
   FAR uint8_t *end = optptr + len;
   uint8_t type = 0;
+  uint16_t tmp[2];
 
   while (optptr < end)
     {
@@ -421,7 +424,6 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state 
*presult,
 
             if (optptr + 6 <= end)
               {
-                uint16_t tmp[2];
                 memcpy(tmp, optptr + 2, 4);
                 presult->lease_time = ((uint32_t)ntohs(tmp[0])) << 16 |
                                        (uint32_t)ntohs(tmp[1]);
@@ -432,6 +434,38 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state 
*presult,
               }
             break;
 
+          case DHCP_OPTION_T1_TIME:
+
+              /* Get renewal (T1) time (in seconds) in host order */
+
+            if (optptr + 6 <= end)
+              {
+                memcpy(tmp, optptr + 2, 4);
+                presult->renewal_time = ((uint32_t)ntohs(tmp[0])) << 16 |
+                                         (uint32_t)ntohs(tmp[1]);
+              }
+            else
+              {
+                nerr("Packet too short (renewal time missing)\n");
+              }
+            break;
+
+          case DHCP_OPTION_T2_TIME:
+
+              /* Get rebinding (T2) time (in seconds) in host order */
+
+            if (optptr + 6 <= end)
+              {
+                memcpy(tmp, optptr + 2, 4);
+                presult->rebinding_time = ((uint32_t)ntohs(tmp[0])) << 16 |
+                                           (uint32_t)ntohs(tmp[1]);
+              }
+            else
+              {
+                nerr("Packet too short (rebinding time missing)\n");
+              }
+            break;
+
           case DHCP_OPTION_END:
             return type;
         }

Reply via email to