Skip to content

Commit 8f83c84

Browse files
committed
netutils/dhcpc: parse DHCP NTP server option
Request DHCP option 42 and store the returned IPv4 NTP server addresses in struct dhcpc_state for later consumers. Signed-off-by: Jerry Ma <masc2008@gmail.com>
1 parent 96a0030 commit 8f83c84

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

include/netutils/dhcpc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
# define CONFIG_NETDB_DNSSERVER_NAMESERVERS 1
5656
#endif
5757

58+
#if !defined(CONFIG_NETUTILS_DHCPC_NTP_SERVER_NUM)
59+
# define CONFIG_NETUTILS_DHCPC_NTP_SERVER_NUM 1
60+
#endif
61+
5862
/****************************************************************************
5963
* Public Types
6064
****************************************************************************/
@@ -66,6 +70,8 @@ struct dhcpc_state
6670
struct in_addr netmask;
6771
struct in_addr dnsaddr[CONFIG_NETDB_DNSSERVER_NAMESERVERS];
6872
uint8_t num_dnsaddr; /* Number of DNS addresses received */
73+
struct in_addr ntpaddr[CONFIG_NETUTILS_DHCPC_NTP_SERVER_NUM];
74+
uint8_t num_ntpaddr; /* Number of NTP addresses received */
6975
struct in_addr default_router;
7076
uint32_t lease_time; /* Lease expires in this number of seconds */
7177
uint32_t renewal_time; /* Seconds to transition to RENEW state(T1) */

netutils/dhcpc/dhcpc.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
#define DHCP_OPTION_ROUTER 3
9292
#define DHCP_OPTION_DNS_SERVER 6
9393
#define DHCP_OPTION_HOST_NAME 12
94+
#define DHCP_OPTION_NTP_SERVER 42
9495
#define DHCP_OPTION_REQ_IPADDR 50
9596
#define DHCP_OPTION_LEASE_TIME 51
9697
#define DHCP_OPTION_MSG_TYPE 53
@@ -210,10 +211,11 @@ static FAR uint8_t *dhcpc_addclientid(FAR uint8_t *clientid,
210211
static FAR uint8_t *dhcpc_addreqoptions(FAR uint8_t *optptr)
211212
{
212213
*optptr++ = DHCP_OPTION_REQ_LIST;
213-
*optptr++ = 3;
214+
*optptr++ = 4;
214215
*optptr++ = DHCP_OPTION_SUBNET_MASK;
215216
*optptr++ = DHCP_OPTION_ROUTER;
216217
*optptr++ = DHCP_OPTION_DNS_SERVER;
218+
*optptr++ = DHCP_OPTION_NTP_SERVER;
217219
return optptr;
218220
}
219221

@@ -411,6 +413,39 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,
411413
}
412414
break;
413415

416+
case DHCP_OPTION_NTP_SERVER:
417+
418+
/* Get the NTP server addresses in network order.
419+
* DHCP option 42 can contain multiple IPv4 addresses,
420+
* each 4 bytes long.
421+
*/
422+
423+
if (optptr + 2 <= end)
424+
{
425+
uint8_t optlen = *(optptr + 1);
426+
uint8_t num_ntp = optlen / 4;
427+
uint8_t i;
428+
429+
if (num_ntp > CONFIG_NETUTILS_DHCPC_NTP_SERVER_NUM)
430+
{
431+
num_ntp = CONFIG_NETUTILS_DHCPC_NTP_SERVER_NUM;
432+
}
433+
434+
presult->num_ntpaddr = 0;
435+
for (i = 0; i < num_ntp && (optptr + 2 + i * 4 + 4) <= end;
436+
i++)
437+
{
438+
memcpy(&presult->ntpaddr[i].s_addr, optptr + 2 + i * 4,
439+
4);
440+
presult->num_ntpaddr++;
441+
}
442+
}
443+
else
444+
{
445+
nerr("Packet too short (NTP address missing)\n");
446+
}
447+
break;
448+
414449
case DHCP_OPTION_MSG_TYPE:
415450

416451
/* Get message type */

0 commit comments

Comments
 (0)