mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-11-08 14:28:21 +01:00
Add a getnameinfo() wrapper
This wrapper takes a domain name or an IP as first argument and a service name or port as second argument. Either argument may be nil. It returns a list of names (always only one in the IP case) and a service name.
This commit is contained in:
parent
04be61f88d
commit
0c3e067af1
48
src/inet.c
48
src/inet.c
@ -16,6 +16,7 @@
|
||||
static int inet_global_toip(lua_State *L);
|
||||
static int inet_global_getaddrinfo(lua_State *L);
|
||||
static int inet_global_tohostname(lua_State *L);
|
||||
static int inet_global_getnameinfo(lua_State *L);
|
||||
static void inet_pushresolved(lua_State *L, struct hostent *hp);
|
||||
static int inet_global_gethostname(lua_State *L);
|
||||
|
||||
@ -24,6 +25,7 @@ static luaL_Reg func[] = {
|
||||
{ "toip", inet_global_toip},
|
||||
{ "getaddrinfo", inet_global_getaddrinfo},
|
||||
{ "tohostname", inet_global_tohostname},
|
||||
{ "getnameinfo", inet_global_getnameinfo},
|
||||
{ "gethostname", inet_global_gethostname},
|
||||
{ NULL, NULL}
|
||||
};
|
||||
@ -76,6 +78,52 @@ static int inet_global_tohostname(lua_State *L) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int inet_global_getnameinfo(lua_State *L) {
|
||||
int i, ret;
|
||||
char host[1024];
|
||||
char serv[32];
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *resolved, *iter;
|
||||
const char *node = luaL_optstring(L, 1, NULL);
|
||||
const char *service = luaL_optstring(L, 2, NULL);
|
||||
|
||||
if (!(node || service))
|
||||
luaL_error(L, "You have to specify a hostname, a service, or both");
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_family = PF_UNSPEC;
|
||||
|
||||
/* getaddrinfo must get a node and a service argument */
|
||||
ret = getaddrinfo(node ? node : "127.0.0.1", service ? service : "7",
|
||||
&hints, &resolved);
|
||||
if (ret != 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, socket_gaistrerror(ret));
|
||||
return 2;
|
||||
}
|
||||
|
||||
lua_newtable(L);
|
||||
for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) {
|
||||
getnameinfo(iter->ai_addr, iter->ai_addrlen, host,
|
||||
node ? sizeof(host) : 0, serv, service ? sizeof(serv) : 0, 0);
|
||||
|
||||
if (node) {
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushstring(L, host);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
}
|
||||
freeaddrinfo(resolved);
|
||||
|
||||
if (service) {
|
||||
lua_pushstring(L, serv);
|
||||
return 2;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Returns all information provided by the resolver given a host name
|
||||
* or ip address
|
||||
|
Loading…
Reference in New Issue
Block a user