Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
455f0f21
Commit
455f0f21
authored
Nov 24, 2015
by
Johny Mattsson
Browse files
Merge pull request #779 from askainet/sntp_dns
Make sntp.sync work with DNS hostnames
parents
6488c954
d9638369
Changes
1
Show whitespace changes
Inline
Side-by-side
app/modules/sntp.c
View file @
455f0f21
...
@@ -39,6 +39,7 @@
...
@@ -39,6 +39,7 @@
#include "lwip/udp.h"
#include "lwip/udp.h"
#include "c_stdlib.h"
#include "c_stdlib.h"
#include "user_modules.h"
#include "user_modules.h"
#include "lwip/dns.h"
#ifdef LUA_USE_MODULES_RTCTIME
#ifdef LUA_USE_MODULES_RTCTIME
#include "rtc/rtctime.h"
#include "rtc/rtctime.h"
...
@@ -148,6 +149,22 @@ static void sntp_dosend (lua_State *L)
...
@@ -148,6 +149,22 @@ static void sntp_dosend (lua_State *L)
}
}
static
void
sntp_dns_found
(
const
char
*
name
,
ip_addr_t
*
ipaddr
,
void
*
arg
)
{
lua_State
*
L
=
arg
;
if
(
ipaddr
==
NULL
)
{
NODE_ERR
(
"DNS Fail!
\n
"
);
handle_error
(
L
);
}
else
{
server
=
*
ipaddr
;
sntp_dosend
(
L
);
}
}
static
void
on_timeout
(
void
*
arg
)
static
void
on_timeout
(
void
*
arg
)
{
{
sntp_dbg
(
"sntp: timer
\n
"
);
sntp_dbg
(
"sntp: timer
\n
"
);
...
@@ -306,13 +323,6 @@ static int sntp_sync (lua_State *L)
...
@@ -306,13 +323,6 @@ static int sntp_sync (lua_State *L)
udp_recv
(
state
->
pcb
,
on_recv
,
L
);
udp_recv
(
state
->
pcb
,
on_recv
,
L
);
// use last server, unless new one specified
if
(
!
lua_isnoneornil
(
L
,
1
))
{
if
(
!
ipaddr_aton
(
luaL_checkstring
(
L
,
1
),
&
server
))
sync_err
(
"bad IP address"
);
}
if
(
!
lua_isnoneornil
(
L
,
2
))
if
(
!
lua_isnoneornil
(
L
,
2
))
{
{
lua_pushvalue
(
L
,
2
);
lua_pushvalue
(
L
,
2
);
...
@@ -334,6 +344,21 @@ static int sntp_sync (lua_State *L)
...
@@ -334,6 +344,21 @@ static int sntp_sync (lua_State *L)
os_timer_arm
(
&
state
->
timer
,
1000
,
1
);
os_timer_arm
(
&
state
->
timer
,
1000
,
1
);
state
->
attempts
=
0
;
state
->
attempts
=
0
;
// use last server, unless new one specified
if
(
!
lua_isnoneornil
(
L
,
1
))
{
size_t
l
;
const
char
*
hostname
=
luaL_checklstring
(
L
,
1
,
&
l
);
if
(
l
>
128
||
hostname
==
NULL
)
sync_err
(
"need <128 hostname"
);
err_t
err
=
dns_gethostbyname
(
hostname
,
&
server
,
sntp_dns_found
,
&
L
);
if
(
err
==
ERR_INPROGRESS
)
return
0
;
// Callback function sntp_dns_found will handle sntp_dosend for us
else
if
(
err
==
ERR_ARG
)
sync_err
(
"bad hostname"
);
}
sntp_dosend
(
L
);
sntp_dosend
(
L
);
return
0
;
return
0
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment