Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
77b1dfa1
Commit
77b1dfa1
authored
May 17, 2015
by
Vowstar
Browse files
Merge pull request #424 from jorgempy/dev
added net.dns.setdnsserver('ip',[index]), net.dns.getdnsserver([inde…
parents
61f65fb0
70580dc4
Changes
1
Hide whitespace changes
Inline
Side-by-side
app/modules/net.c
View file @
77b1dfa1
...
@@ -204,13 +204,12 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
...
@@ -204,13 +204,12 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
NODE_DBG
(
"self_ref null.
\n
"
);
NODE_DBG
(
"self_ref null.
\n
"
);
return
;
return
;
}
}
/* original
if(ipaddr == NULL)
if(ipaddr == NULL)
{
{
NODE_ERR( "DNS Fail!\n" );
NODE_ERR( "DNS Fail!\n" );
goto end;
goto end;
}
}
// ipaddr->addr is a uint32_t ip
// ipaddr->addr is a uint32_t ip
char ip_str[20];
char ip_str[20];
c_memset(ip_str, 0, sizeof(ip_str));
c_memset(ip_str, 0, sizeof(ip_str));
...
@@ -220,9 +219,30 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
...
@@ -220,9 +219,30 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
}
}
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
lua_rawgeti
(
gL
,
LUA_REGISTRYINDEX
,
nud
->
self_ref
);
// pass the userdata(conn) to callback func in lua
//
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
lua_pushstring(gL, ip_str); // the ip para
lua_pushstring(gL, ip_str); // the ip para
lua_call
(
gL
,
2
,
0
);
*/
// "enhanced"
lua_rawgeti
(
gL
,
LUA_REGISTRYINDEX
,
nud
->
cb_dns_found_ref
);
// the callback function
if
(
ipaddr
==
NULL
)
{
NODE_DBG
(
"DNS Fail!
\n
"
);
lua_pushnil
(
gL
);
}
else
{
// ipaddr->addr is a uint32_t ip
char
ip_str
[
20
];
c_memset
(
ip_str
,
0
,
sizeof
(
ip_str
));
if
(
host_ip
.
addr
==
0
&&
ipaddr
->
addr
!=
0
)
{
c_sprintf
(
ip_str
,
IPSTR
,
IP2STR
(
&
(
ipaddr
->
addr
)));
}
lua_pushstring
(
gL
,
ip_str
);
// the ip para
}
// "enhanced" end
lua_call
(
gL
,
1
,
0
);
end:
end:
if
((
pesp_conn
->
type
==
ESPCONN_TCP
&&
pesp_conn
->
proto
.
tcp
->
remote_port
==
0
)
if
((
pesp_conn
->
type
==
ESPCONN_TCP
&&
pesp_conn
->
proto
.
tcp
->
remote_port
==
0
)
...
@@ -1119,6 +1139,70 @@ static int net_dns( lua_State* L, const char* mt )
...
@@ -1119,6 +1139,70 @@ static int net_dns( lua_State* L, const char* mt )
return
0
;
return
0
;
}
}
// Lua: net.dns.resolve( domain, function(ip) )
static
int
net_dns_static
(
lua_State
*
L
)
{
const
char
*
mt
=
"net.socket"
;
if
(
!
lua_isstring
(
L
,
1
))
return
luaL_error
(
L
,
"wrong parameter type (domain)"
);
int
rfunc
=
LUA_NOREF
;
//save reference to func
if
(
lua_type
(
L
,
2
)
==
LUA_TFUNCTION
||
lua_type
(
L
,
2
)
==
LUA_TLIGHTFUNCTION
){
rfunc
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
}
int
rdom
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
//save reference to domain
lua_settop
(
L
,
0
);
//empty stack
lua_getfield
(
L
,
LUA_GLOBALSINDEX
,
"net"
);
lua_getfield
(
L
,
-
1
,
"createConnection"
);
lua_remove
(
L
,
-
2
);
//remove "net" from stack
lua_pushinteger
(
L
,
UDP
);
// we are going to create a new dummy UDP socket
lua_call
(
L
,
1
,
1
);
// after this the stack should have a socket
lua_rawgeti
(
gL
,
LUA_REGISTRYINDEX
,
rdom
);
// load domain back to the stack
lua_rawgeti
(
gL
,
LUA_REGISTRYINDEX
,
rfunc
);
// load the callback function back to the stack
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
rdom
);
//free reference
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
rfunc
);
//free reference
bool
isserver
=
false
;
struct
espconn
*
pesp_conn
=
NULL
;
lnet_userdata
*
nud
;
size_t
l
;
nud
=
(
lnet_userdata
*
)
luaL_checkudata
(
L
,
1
,
mt
);
luaL_argcheck
(
L
,
nud
,
1
,
"Server/Socket expected"
);
if
(
nud
==
NULL
){
NODE_DBG
(
"userdata is nil.
\n
"
);
return
0
;
}
if
(
nud
->
pesp_conn
==
NULL
){
NODE_DBG
(
"nud->pesp_conn is NULL.
\n
"
);
return
0
;
}
pesp_conn
=
nud
->
pesp_conn
;
lua_pushvalue
(
L
,
1
);
// copy to the top of stack
if
(
nud
->
self_ref
!=
LUA_NOREF
)
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
nud
->
self_ref
);
nud
->
self_ref
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
const
char
*
domain
=
luaL_checklstring
(
L
,
2
,
&
l
);
if
(
l
>
128
||
domain
==
NULL
)
return
luaL_error
(
L
,
"need <128 domain"
);
if
(
lua_type
(
L
,
3
)
==
LUA_TFUNCTION
||
lua_type
(
L
,
3
)
==
LUA_TLIGHTFUNCTION
){
lua_pushvalue
(
L
,
3
);
// copy argument (func) to the top of stack
if
(
nud
->
cb_dns_found_ref
!=
LUA_NOREF
)
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
nud
->
cb_dns_found_ref
);
nud
->
cb_dns_found_ref
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
}
host_ip
.
addr
=
0
;
espconn_gethostbyname
(
pesp_conn
,
domain
,
&
host_ip
,
net_dns_found
);
return
0
;
}
// Lua: s = net.createServer(type, function(server))
// Lua: s = net.createServer(type, function(server))
static
int
net_createServer
(
lua_State
*
L
)
static
int
net_createServer
(
lua_State
*
L
)
...
@@ -1284,6 +1368,48 @@ static int net_socket_dns( lua_State* L )
...
@@ -1284,6 +1368,48 @@ static int net_socket_dns( lua_State* L )
return
net_dns
(
L
,
mt
);
return
net_dns
(
L
,
mt
);
}
}
// Lua: s = net.dns.setdnsserver(ip_addr, [index])
static
int
net_setdnsserver
(
lua_State
*
L
)
{
size_t
l
;
u32_t
ip32
;
const
char
*
server
=
luaL_checklstring
(
L
,
1
,
&
l
);
if
(
l
>
16
||
server
==
NULL
||
(
ip32
=
ipaddr_addr
(
server
))
==
IPADDR_NONE
||
ip32
==
IPADDR_ANY
)
return
luaL_error
(
L
,
"invalid dns server ip"
);
int
numdns
=
luaL_optint
(
L
,
2
,
0
);
if
(
numdns
>=
DNS_MAX_SERVERS
)
return
luaL_error
(
L
,
"server index out of range [0-%d]"
,
DNS_MAX_SERVERS
-
1
);
ip_addr_t
ipaddr
;
ip4_addr_set_u32
(
&
ipaddr
,
ip32
);
dns_setserver
(
numdns
,
&
ipaddr
);
return
0
;
}
// Lua: s = net.dns.getdnsserver([index])
static
int
net_getdnsserver
(
lua_State
*
L
)
{
int
numdns
=
luaL_optint
(
L
,
1
,
0
);
if
(
numdns
>=
DNS_MAX_SERVERS
)
return
luaL_error
(
L
,
"server index out of range [0-%d]"
,
DNS_MAX_SERVERS
-
1
);
ip_addr_t
ipaddr
;
dns_getserver
(
numdns
,
&
ipaddr
);
if
(
ip_addr_isany
(
&
ipaddr
)
)
{
lua_pushnil
(
L
);
}
else
{
char
temp
[
20
]
=
{
0
};
c_sprintf
(
temp
,
IPSTR
,
IP2STR
(
&
ipaddr
)
);
lua_pushstring
(
L
,
temp
);
}
return
1
;
}
#if 0
#if 0
static int net_array_index( lua_State* L )
static int net_array_index( lua_State* L )
{
{
...
@@ -1354,11 +1480,22 @@ static const LUA_REG_TYPE net_array_map[] =
...
@@ -1354,11 +1480,22 @@ static const LUA_REG_TYPE net_array_map[] =
{ LNILKEY, LNILVAL }
{ LNILKEY, LNILVAL }
};
};
#endif
#endif
static
const
LUA_REG_TYPE
net_dns_map
[]
=
{
{
LSTRKEY
(
"setdnsserver"
),
LFUNCVAL
(
net_setdnsserver
)
},
{
LSTRKEY
(
"getdnsserver"
),
LFUNCVAL
(
net_getdnsserver
)
},
{
LSTRKEY
(
"resolve"
),
LFUNCVAL
(
net_dns_static
)
},
{
LNILKEY
,
LNILVAL
}
};
const
LUA_REG_TYPE
net_map
[]
=
const
LUA_REG_TYPE
net_map
[]
=
{
{
{
LSTRKEY
(
"createServer"
),
LFUNCVAL
(
net_createServer
)
},
{
LSTRKEY
(
"createServer"
),
LFUNCVAL
(
net_createServer
)
},
{
LSTRKEY
(
"createConnection"
),
LFUNCVAL
(
net_createConnection
)
},
{
LSTRKEY
(
"createConnection"
),
LFUNCVAL
(
net_createConnection
)
},
#if LUA_OPTIMIZE_MEMORY > 0
#if LUA_OPTIMIZE_MEMORY > 0
{
LSTRKEY
(
"dns"
),
LROVAL
(
net_dns_map
)
},
{
LSTRKEY
(
"TCP"
),
LNUMVAL
(
TCP
)
},
{
LSTRKEY
(
"TCP"
),
LNUMVAL
(
TCP
)
},
{
LSTRKEY
(
"UDP"
),
LNUMVAL
(
UDP
)
},
{
LSTRKEY
(
"UDP"
),
LNUMVAL
(
UDP
)
},
...
@@ -1421,6 +1558,12 @@ LUALIB_API int luaopen_net( lua_State *L )
...
@@ -1421,6 +1558,12 @@ LUALIB_API int luaopen_net( lua_State *L )
// Setup the methods inside metatable
// Setup the methods inside metatable
luaL_register( L, NULL, net_array_map );
luaL_register( L, NULL, net_array_map );
#endif
#endif
lua_settop
(
L
,
n
);
lua_newtable
(
L
);
luaL_register
(
L
,
NULL
,
net_dns_map
);
lua_setfield
(
L
,
-
2
,
"dns"
);
return
1
;
return
1
;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
#endif // #if LUA_OPTIMIZE_MEMORY > 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