Add support for DCHP NTP server (option 42) (#2709)
* Add DHCP option 42 / NTP * Update dhcp.c * resolve merge conflict * add lineend at end of file * fix merge conflict resolution error
This commit is contained in:
parent
6d9c5a49a4
commit
73e6651fe8
|
@ -53,6 +53,7 @@ struct dhcp
|
|||
ip_addr_t offered_ip_addr;
|
||||
ip_addr_t offered_sn_mask;
|
||||
ip_addr_t offered_gw_addr;
|
||||
ip_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) */
|
||||
|
@ -207,6 +208,9 @@ void dhcp_fine_tmr(void);
|
|||
#define DHCP_OPTION_TCP_TTL 37
|
||||
#define DHCP_OPTION_END 255
|
||||
|
||||
/* time */
|
||||
#define DHCP_OPTION_NTP 42
|
||||
|
||||
/**add options for support more router by liuHan**/
|
||||
#define DHCP_OPTION_DOMAIN_NAME 15
|
||||
#define DHCP_OPTION_PRD 31
|
||||
|
|
|
@ -119,7 +119,8 @@ static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
|
|||
#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_NTP 8
|
||||
#define DHCP_OPTION_IDX_DNS_SERVER 9
|
||||
#define DHCP_OPTION_IDX_MAX (DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS)
|
||||
|
||||
/** Holds the decoded option values, only valid while in dhcp_recv.
|
||||
|
@ -292,19 +293,20 @@ dhcp_select(struct netif *netif)
|
|||
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
|
||||
dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->server_ip_addr)));
|
||||
|
||||
dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 12/*num options*/);
|
||||
dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 13/*num options*/);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NTP);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_DOMAIN_NAME);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINS);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINT);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TIS);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_PRD);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_STATIC_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_CLASSLESS_STATIC_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_VSN);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINS);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINT);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TIS);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_PRD);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_STATIC_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_CLASSLESS_STATIC_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_VSN);
|
||||
|
||||
#if LWIP_NETIF_HOSTNAME
|
||||
if (netif->hostname != NULL) {
|
||||
|
@ -383,7 +385,7 @@ dhcp_fine_tmr()
|
|||
if (netif->dhcp != NULL) {
|
||||
/*add DHCP retries processing by LiuHan*/
|
||||
if (DHCP_MAXRTX != 0) {
|
||||
if (netif->dhcp->tries >= DHCP_MAXRTX){
|
||||
if (netif->dhcp->tries >= DHCP_MAXRTX){
|
||||
os_printf("DHCP timeout\n");
|
||||
if (netif->dhcp_event != NULL)
|
||||
netif->dhcp_event();
|
||||
|
@ -536,6 +538,7 @@ dhcp_handle_ack(struct netif *netif)
|
|||
#if LWIP_DHCP_BOOTP_FILE
|
||||
ip_addr_set_zero(&dhcp->offered_si_addr);
|
||||
#endif /* LWIP_DHCP_BOOTP_FILE */
|
||||
ip_addr_set_zero(&dhcp->offered_ntp_addr);
|
||||
|
||||
/* lease time given? */
|
||||
if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_LEASE_TIME)) {
|
||||
|
@ -593,6 +596,10 @@ dhcp_handle_ack(struct netif *netif)
|
|||
n++;
|
||||
}
|
||||
#endif /* LWIP_DNS */
|
||||
|
||||
if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_NTP)) {
|
||||
ip4_addr_set_u32(&dhcp->offered_ntp_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_NTP)));
|
||||
}
|
||||
}
|
||||
|
||||
/** Set a statically allocated struct dhcp to work with.
|
||||
|
@ -915,19 +922,20 @@ dhcp_discover(struct netif *netif)
|
|||
}
|
||||
}
|
||||
#endif /* LWIP_NETIF_HOSTNAME */
|
||||
dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 12/*num options*/);
|
||||
dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 13/*num options*/);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NTP);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_DOMAIN_NAME);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINS);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINT);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TIS);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_PRD);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_STATIC_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_CLASSLESS_STATIC_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_VSN);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINS);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINT);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_NB_TIS);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_PRD);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_STATIC_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_CLASSLESS_STATIC_ROUTER);
|
||||
dhcp_option_byte(dhcp, DHCP_OPTION_VSN);
|
||||
|
||||
dhcp_option_trailer(dhcp);
|
||||
|
||||
|
@ -1253,6 +1261,7 @@ dhcp_release(struct netif *netif)
|
|||
#if LWIP_DHCP_BOOTP_FILE
|
||||
ip_addr_set_zero(&dhcp->offered_si_addr);
|
||||
#endif /* LWIP_DHCP_BOOTP_FILE */
|
||||
ip_addr_set_zero(&dhcp->offered_ntp_addr);
|
||||
dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
|
||||
|
||||
/* create and initialize the DHCP message header */
|
||||
|
@ -1463,6 +1472,10 @@ again:
|
|||
LWIP_ASSERT("len >= decode_len", len >= decode_len);
|
||||
decode_idx = DHCP_OPTION_IDX_DNS_SERVER;
|
||||
break;
|
||||
case(DHCP_OPTION_NTP):
|
||||
LWIP_ASSERT("len == 4", len == 4);
|
||||
decode_idx = DHCP_OPTION_IDX_NTP;
|
||||
break;
|
||||
case(DHCP_OPTION_LEASE_TIME):
|
||||
LWIP_ASSERT("len == 4", len == 4);
|
||||
decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
|
||||
|
|
|
@ -39,6 +39,8 @@
|
|||
#include "osapi.h"
|
||||
#include "lwip/udp.h"
|
||||
#include <stdlib.h>
|
||||
#include "lwip/inet.h"
|
||||
#include "lwip/dhcp.h"
|
||||
#include "user_modules.h"
|
||||
#include "lwip/dns.h"
|
||||
#include "task/task.h"
|
||||
|
@ -48,6 +50,8 @@
|
|||
#include "rtc/rtctime.h"
|
||||
#endif
|
||||
|
||||
struct netif * eagle_lwip_getif(uint8 index);
|
||||
|
||||
#define max(a,b) ((a < b) ? b : a)
|
||||
|
||||
#define NTP_PORT 123
|
||||
|
@ -805,15 +809,23 @@ static int sntp_sync (lua_State *L)
|
|||
server_count++;
|
||||
}
|
||||
} else if (server_count == 0) {
|
||||
// default to ntp pool
|
||||
lua_newtable(L);
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
lua_pushnumber(L, i + 1);
|
||||
char buf[64];
|
||||
sprintf(buf, "%d.nodemcu.pool.ntp.org", i);
|
||||
lua_pushstring(L, buf);
|
||||
lua_settable(L, -3);
|
||||
struct netif *iface = (struct netif *)eagle_lwip_getif(0x00);
|
||||
if (iface->dhcp && iface->dhcp->offered_ntp_addr.addr) {
|
||||
ip_addr_t ntp_addr = iface->dhcp->offered_ntp_addr;
|
||||
lua_pushnumber(L, 1);
|
||||
lua_pushstring(L, inet_ntoa(ntp_addr));
|
||||
lua_settable(L, -3);
|
||||
} else {
|
||||
// default to ntp pool
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
lua_pushnumber(L, i + 1);
|
||||
char buf[64];
|
||||
sprintf(buf, "%d.nodemcu.pool.ntp.org", i);
|
||||
lua_pushstring(L, buf);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
}
|
||||
luaL_unref (L, LUA_REGISTRYINDEX, state->list_ref);
|
||||
state->list_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
|
|
Loading…
Reference in New Issue