Skip to content

Commit

Permalink
Support getting NTP server from DHCP lease
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Sep 14, 2021
1 parent 93ecc11 commit 2cff4ac
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
1 change: 1 addition & 0 deletions include/lwip/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extern "C" {
#define LWIP_POSIX_SOCKETS_IO_NAMES 0
#define LWIP_STATS 0
#define LWIP_TCP_KEEPALIVE 1
#define LWIP_DHCP_GET_NTP_SRV 1

#define SYS_LIGHTWEIGHT_PROT 1

Expand Down
5 changes: 4 additions & 1 deletion lwip/src/core/ipv4/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,11 @@ dhcp_handle_ack(struct netif *netif, struct dhcp_msg *msg_in)
/* NTP servers */
for (n = 0; (n < LWIP_DHCP_MAX_NTP_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n); n++) {
ip4_addr_set_u32(&ntp_server_addrs[n], lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n)));
if (n == 0) {
ip4_addr_set_u32(&dhcp->offered_ntp_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_NTP_SERVER)));
}
}
dhcp_set_ntp_servers(n, ntp_server_addrs);
//dhcp_set_ntp_servers(n, ntp_server_addrs);
#endif /* LWIP_DHCP_GET_NTP_SRV */

#if LWIP_DHCP_PROVIDE_DNS_SERVERS
Expand Down
1 change: 1 addition & 0 deletions lwip/src/include/lwip/dhcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct dhcp
ip4_addr_t offered_sn_mask;
ip4_addr_t offered_gw_addr;
ip4_addr_t offered_dns_addr;
ip4_addr_t offered_ntp_addr;

u32_t offered_t0_lease; /* lease period (in seconds) */
u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
Expand Down
52 changes: 35 additions & 17 deletions src/esp8266/sdk_lwip/src/core/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,22 @@ static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
* This might be moved into the struct dhcp (not necessarily since
* lwIP is single-threaded and the array is only used while in recv
* callback). */
#define DHCP_OPTION_IDX_OVERLOAD 0
#define DHCP_OPTION_IDX_MSG_TYPE 1
#define DHCP_OPTION_IDX_SERVER_ID 2
#define DHCP_OPTION_IDX_LEASE_TIME 3
#define DHCP_OPTION_IDX_T1 4
#define DHCP_OPTION_IDX_T2 5
#define DHCP_OPTION_IDX_SUBNET_MASK 6
#define DHCP_OPTION_IDX_ROUTER 7
#define DHCP_OPTION_IDX_DNS_SERVER 8
#define DHCP_OPTION_IDX_MAX (DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS)
#define NTP_MAX_SERVERS 1
enum dhcp_option_idx {
DHCP_OPTION_IDX_OVERLOAD = 0,
DHCP_OPTION_IDX_MSG_TYPE,
DHCP_OPTION_IDX_SERVER_ID,
DHCP_OPTION_IDX_LEASE_TIME,
DHCP_OPTION_IDX_T1,
DHCP_OPTION_IDX_T2,
DHCP_OPTION_IDX_SUBNET_MASK,
DHCP_OPTION_IDX_ROUTER,
DHCP_OPTION_IDX_DNS_SERVER,
DHCP_OPTION_IDX_DNS_SERVER_LAST = DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS - 1,
DHCP_OPTION_IDX_NTP_SERVER,
DHCP_OPTION_IDX_NTP_SERVER_LAST = DHCP_OPTION_IDX_NTP_SERVER + NTP_MAX_SERVERS - 1,
DHCP_OPTION_IDX_MAX
};

/** Holds the decoded option values, only valid while in dhcp_recv.
@todo: move this into struct dhcp? */
Expand Down Expand Up @@ -614,23 +620,27 @@ dhcp_handle_ack(struct netif *netif)
if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_ROUTER)) {
ip4_addr_set_u32(&dhcp->offered_gw_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_ROUTER)));
}

#if LWIP_DNS
/* DNS servers */
n = 0;
if(manual_set_flag == false) {
while(dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n) && (n < DNS_MAX_SERVERS)) {
ip_addr_t dns_addr;
ip4_addr_set_u32(&dns_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
dns_setserver(n, &dns_addr);
n++;
}
while(dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n) && (n < DNS_MAX_SERVERS)) {
ip_addr_t dns_addr;
ip4_addr_set_u32(&dns_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
dns_setserver(n, &dns_addr);
n++;
}
}
#endif /* LWIP_DNS */
ip_addr_set_zero(&dhcp->offered_dns_addr);
if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER)) {
ip4_addr_set_u32(&dhcp->offered_dns_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER)));
}
ip_addr_set_zero(&dhcp->offered_ntp_addr);
if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_NTP_SERVER)) {
ip4_addr_set_u32(&dhcp->offered_ntp_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_NTP_SERVER)));
}
}

/** Set a statically allocated struct dhcp to work with.
Expand Down Expand Up @@ -1567,6 +1577,14 @@ dhcp_parse_reply(struct dhcp *dhcp, struct pbuf *p)
LWIP_ASSERT("len == 4", len == 4);
decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
break;
case (DHCP_OPTION_NTP):
/* special case: there might be more than one server */
LWIP_ERROR("len %% 4 == 0", len % 4 == 0, return ERR_VAL;);
/* limit number of NTP servers */
decode_len = LWIP_MIN(len, 4 * NTP_MAX_SERVERS);
LWIP_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
decode_idx = DHCP_OPTION_IDX_NTP_SERVER;
break;
case(DHCP_OPTION_OVERLOAD):
LWIP_ASSERT("len == 1", len == 1);
decode_idx = DHCP_OPTION_IDX_OVERLOAD;
Expand Down
8 changes: 4 additions & 4 deletions src/mgos_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
#include "lwip/netif.h"
#include "lwip/tcpip.h"

bool mgos_lwip_if_get_ip_info(const struct netif *nif,
const char *dns_override,
bool mgos_lwip_if_get_ip_info(const struct netif *nif, const char *dns_override,
struct mgos_net_ip_info *ip_info) {
if (nif == NULL || !(nif->flags & NETIF_FLAG_UP)) return false;
memset(ip_info, 0, sizeof(*ip_info));
Expand All @@ -39,21 +38,22 @@ bool mgos_lwip_if_get_ip_info(const struct netif *nif,
struct dhcp *dhcp = netif_dhcp_data(nif);
if (dhcp != NULL) {
ip_info->dns.sin_addr.s_addr = ip4_addr_get_u32(&dhcp->offered_dns_addr);
ip_info->ntp.sin_addr.s_addr = ip4_addr_get_u32(&dhcp->offered_ntp_addr);
}
}
return true;
}

#if CS_PLATFORM != CS_P_ESP32 && CS_PLATFORM != CS_P_ESP8266
static void tcpip_init_done(void *arg) {
*((bool *)arg) = true;
*((bool *) arg) = true;
}
#endif

bool mgos_lwip_init(void) {
#if CS_PLATFORM != CS_P_ESP32 && CS_PLATFORM != CS_P_ESP8266
volatile bool lwip_inited = false;
tcpip_init(tcpip_init_done, (void *)&lwip_inited);
tcpip_init(tcpip_init_done, (void *) &lwip_inited);
while (!lwip_inited) {
}
#endif
Expand Down

0 comments on commit 2cff4ac

Please sign in to comment.