Skip to content

Commit 8f3c3c5

Browse files
nuttxsxiaoxiang781216
authored andcommitted
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 <[email protected]>
1 parent 8d2a7e3 commit 8f3c3c5

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

include/netutils/dhcpc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct dhcpc_state
5858
struct in_addr dnsaddr;
5959
struct in_addr default_router;
6060
uint32_t lease_time; /* Lease expires in this number of seconds */
61+
uint32_t renewal_time; /* Seconds to transition to RENEW state(T1) */
62+
uint32_t rebinding_time; /* Seconds to transition to REBIND state(T2) */
6163
};
6264

6365
typedef void (*dhcpc_callback_t)(FAR struct dhcpc_state *presult);

netutils/dhcpc/dhcpc.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
#define DHCP_OPTION_MSG_TYPE 53
9797
#define DHCP_OPTION_SERVER_ID 54
9898
#define DHCP_OPTION_REQ_LIST 55
99+
#define DHCP_OPTION_T1_TIME 58
100+
#define DHCP_OPTION_T2_TIME 59
99101
#define DHCP_OPTION_CLIENT_ID 61
100102
#define DHCP_OPTION_END 255
101103

@@ -340,6 +342,7 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,
340342
{
341343
FAR uint8_t *end = optptr + len;
342344
uint8_t type = 0;
345+
uint16_t tmp[2];
343346

344347
while (optptr < end)
345348
{
@@ -421,7 +424,6 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,
421424

422425
if (optptr + 6 <= end)
423426
{
424-
uint16_t tmp[2];
425427
memcpy(tmp, optptr + 2, 4);
426428
presult->lease_time = ((uint32_t)ntohs(tmp[0])) << 16 |
427429
(uint32_t)ntohs(tmp[1]);
@@ -432,6 +434,38 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,
432434
}
433435
break;
434436

437+
case DHCP_OPTION_T1_TIME:
438+
439+
/* Get renewal (T1) time (in seconds) in host order */
440+
441+
if (optptr + 6 <= end)
442+
{
443+
memcpy(tmp, optptr + 2, 4);
444+
presult->renewal_time = ((uint32_t)ntohs(tmp[0])) << 16 |
445+
(uint32_t)ntohs(tmp[1]);
446+
}
447+
else
448+
{
449+
nerr("Packet too short (renewal time missing)\n");
450+
}
451+
break;
452+
453+
case DHCP_OPTION_T2_TIME:
454+
455+
/* Get rebinding (T2) time (in seconds) in host order */
456+
457+
if (optptr + 6 <= end)
458+
{
459+
memcpy(tmp, optptr + 2, 4);
460+
presult->rebinding_time = ((uint32_t)ntohs(tmp[0])) << 16 |
461+
(uint32_t)ntohs(tmp[1]);
462+
}
463+
else
464+
{
465+
nerr("Packet too short (rebinding time missing)\n");
466+
}
467+
break;
468+
435469
case DHCP_OPTION_END:
436470
return type;
437471
}

0 commit comments

Comments
 (0)