Skip to content

Commit 9e6c6e7

Browse files
committed
sntp: simplify and tidy a bit
* list_ref can become LUA_REFNIL, because that's what rawgeti returns for LUA_NOREF. Defensively guard for this, rather than falling into the sntp_dolookups loop with nil on the stack. * set_repeat_mode should not call itself, but should rather always do what it's going to do and then optionally do the rest if directed. * sntp_sync should not try to special case the single string argument: we should be queueing that name for DNS resolution, too. Towards that end, if we are given a single string, build a table and make that the list_ref and call off to sntp_dolookups, just like we otherwise do. FIXES: #2699
1 parent 5563b8a commit 9e6c6e7

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

app/modules/sntp.c

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ static void sntp_dolookups (lua_State *L) {
623623
// Step through each element of the table, converting it to an address
624624
// at the end, start the lookups. If we have already looked everything up,
625625
// then move straight to sending the packets.
626-
if (state->list_ref == LUA_NOREF) {
626+
if ((state->list_ref == LUA_NOREF) || (state->list_ref == LUA_REFNIL)) {
627627
sntp_dosend();
628628
return;
629629
}
@@ -698,8 +698,16 @@ static char *state_init(lua_State *L) {
698698

699699
static char *set_repeat_mode(lua_State *L, bool enable)
700700
{
701+
if (repeat) {
702+
os_timer_disarm (&repeat->timer);
703+
luaL_unref (L, LUA_REGISTRYINDEX, repeat->sync_cb_ref);
704+
luaL_unref (L, LUA_REGISTRYINDEX, repeat->err_cb_ref);
705+
luaL_unref (L, LUA_REGISTRYINDEX, repeat->list_ref);
706+
c_free(repeat);
707+
repeat = NULL;
708+
}
709+
701710
if (enable) {
702-
set_repeat_mode(L, FALSE);
703711
repeat = (sntp_repeat_t *) c_malloc(sizeof(sntp_repeat_t));
704712
if (!repeat) {
705713
return "no memory";
@@ -716,16 +724,8 @@ static char *set_repeat_mode(lua_State *L, bool enable)
716724
//The function on_long_timeout returns errors to the developer
717725
//My guess: Error reporting is a good thing, resume the timer.
718726
os_timer_arm(&repeat->timer, 1000 * 1000, 1);
719-
} else {
720-
if (repeat) {
721-
os_timer_disarm (&repeat->timer);
722-
luaL_unref (L, LUA_REGISTRYINDEX, repeat->sync_cb_ref);
723-
luaL_unref (L, LUA_REGISTRYINDEX, repeat->err_cb_ref);
724-
luaL_unref (L, LUA_REGISTRYINDEX, repeat->list_ref);
725-
c_free(repeat);
726-
repeat = NULL;
727-
}
728727
}
728+
729729
return NULL;
730730
}
731731

@@ -787,22 +787,17 @@ static int sntp_sync (lua_State *L)
787787
if (lua_istable(L, 1)) {
788788
// Save a reference to the table
789789
lua_pushvalue(L, 1);
790-
luaL_unref (L, LUA_REGISTRYINDEX, state->list_ref);
791-
state->list_ref = luaL_ref(L, LUA_REGISTRYINDEX);
792-
sntp_dolookups(L);
793-
goto good_ret;
794790
} else {
795791
size_t l;
796792
const char *hostname = luaL_checklstring(L, 1, &l);
797793
if (l>128 || hostname == NULL)
798794
sync_err("need <128 hostname");
799-
err_t err = dns_gethostbyname(hostname, get_free_server(), sntp_dns_found, state);
800-
if (err == ERR_INPROGRESS) {
801-
goto good_ret;
802-
} else if (err == ERR_ARG)
803-
sync_err("bad hostname");
804795

805-
server_count++;
796+
/* Construct a singleton table containing the one server */
797+
lua_newtable(L);
798+
lua_pushnumber(L, 1);
799+
lua_pushstring(L, hostname);
800+
lua_settable(L, -3);
806801
}
807802
} else if (server_count == 0) {
808803
// default to ntp pool
@@ -815,15 +810,12 @@ static int sntp_sync (lua_State *L)
815810
lua_pushstring(L, buf);
816811
lua_settable(L, -3);
817812
}
818-
luaL_unref (L, LUA_REGISTRYINDEX, state->list_ref);
819-
state->list_ref = luaL_ref(L, LUA_REGISTRYINDEX);
820-
sntp_dolookups(L);
821-
goto good_ret;
822813
}
823814

824-
sntp_dosend ();
815+
luaL_unref (L, LUA_REGISTRYINDEX, state->list_ref);
816+
state->list_ref = luaL_ref(L, LUA_REGISTRYINDEX);
817+
sntp_dolookups(L);
825818

826-
good_ret:
827819
if (!lua_isnoneornil(L, 4)) {
828820
set_repeat_mode(L, 1);
829821
}

0 commit comments

Comments
 (0)