added net.dns.setdnsserver('ip',[index]), net.dns.getdnsserver([index]), net.dns.resolve('domain.com', function cb('ip' or nil if fail) )

modified:   app/modules/net.c
This commit is contained in:
Jorge Mesquita 2015-05-16 19:37:26 -04:00
parent 4c2ad0f545
commit c7fd04d372
1 changed files with 144 additions and 4 deletions

View File

@ -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)
} }
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)
@ -1331,6 +1351,112 @@ static int net_multicastLeave( lua_State* L )
return net_multicastJoinLeave(L,0); return net_multicastJoinLeave(L,0);
} }
// 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;
}
// 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;
}
#if 0 #if 0
static int net_array_index( lua_State* L ) static int net_array_index( lua_State* L )
@ -1402,6 +1528,15 @@ 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 ) },
@ -1409,6 +1544,7 @@ const LUA_REG_TYPE net_map[] =
{ LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) }, { LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) },
{ LSTRKEY( "multicastLeave"), LFUNCVAL( net_multicastLeave ) }, { LSTRKEY( "multicastLeave"), LFUNCVAL( net_multicastLeave ) },
#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 ) },
@ -1471,6 +1607,10 @@ 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
} }