Commit d3bfdf3a authored by Johny Mattsson's avatar Johny Mattsson
Browse files

Fixed race-crash in SNTP module.

parent 76f2654f
...@@ -94,6 +94,8 @@ typedef struct ...@@ -94,6 +94,8 @@ typedef struct
static sntp_state_t *state; static sntp_state_t *state;
static ip_addr_t server; static ip_addr_t server;
static void on_timeout (void *arg);
static void cleanup (lua_State *L) static void cleanup (lua_State *L)
{ {
os_timer_disarm (&state->timer); os_timer_disarm (&state->timer);
...@@ -121,6 +123,13 @@ static void handle_error (lua_State *L) ...@@ -121,6 +123,13 @@ static void handle_error (lua_State *L)
static void sntp_dosend (lua_State *L) static void sntp_dosend (lua_State *L)
{ {
if (state->attempts == 0)
{
os_timer_disarm (&state->timer);
os_timer_setfn (&state->timer, on_timeout, NULL);
os_timer_arm (&state->timer, 1000, 1);
}
++state->attempts; ++state->attempts;
sntp_dbg("sntp: attempt %d\n", state->attempts); sntp_dbg("sntp: attempt %d\n", state->attempts);
...@@ -153,7 +162,9 @@ static void sntp_dosend (lua_State *L) ...@@ -153,7 +162,9 @@ static void sntp_dosend (lua_State *L)
static void sntp_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) static void sntp_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
{ {
lua_State *L = arg; (void)arg;
lua_State *L = lua_getstate ();
if (ipaddr == NULL) if (ipaddr == NULL)
{ {
NODE_ERR("DNS Fail!\n"); NODE_ERR("DNS Fail!\n");
...@@ -169,8 +180,9 @@ static void sntp_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) ...@@ -169,8 +180,9 @@ static void sntp_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
static void on_timeout (void *arg) static void on_timeout (void *arg)
{ {
(void)arg;
sntp_dbg("sntp: timer\n"); sntp_dbg("sntp: timer\n");
lua_State *L = arg; lua_State *L = lua_getstate ();
if (state->attempts >= MAX_ATTEMPTS) if (state->attempts >= MAX_ATTEMPTS)
handle_error (L); handle_error (L);
else else
...@@ -341,10 +353,6 @@ static int sntp_sync (lua_State *L) ...@@ -341,10 +353,6 @@ static int sntp_sync (lua_State *L)
else else
state->err_cb_ref = LUA_NOREF; state->err_cb_ref = LUA_NOREF;
os_timer_disarm (&state->timer);
os_timer_setfn (&state->timer, on_timeout, L);
os_timer_arm (&state->timer, 1000, 1);
state->attempts = 0; state->attempts = 0;
// use last server, unless new one specified // use last server, unless new one specified
...@@ -354,7 +362,7 @@ static int sntp_sync (lua_State *L) ...@@ -354,7 +362,7 @@ static int sntp_sync (lua_State *L)
const char *hostname = luaL_checklstring(L, 1, &l); const char *hostname = luaL_checklstring(L, 1, &l);
if (l>128 || hostname == NULL) if (l>128 || hostname == NULL)
sync_err("need <128 hostname"); sync_err("need <128 hostname");
err_t err = dns_gethostbyname(hostname, &server, sntp_dns_found, &L); err_t err = dns_gethostbyname(hostname, &server, sntp_dns_found, state);
if (err == ERR_INPROGRESS) if (err == ERR_INPROGRESS)
return 0; // Callback function sntp_dns_found will handle sntp_dosend for us return 0; // Callback function sntp_dns_found will handle sntp_dosend for us
else if (err == ERR_ARG) else if (err == ERR_ARG)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment