Simplifying getaddrinfo treatment.

This commit is contained in:
Diego Nehab 2013-05-27 21:05:48 +08:00
parent 5e0b56b8d3
commit 834a3cf520

View File

@ -79,24 +79,22 @@ static int inet_global_tohostname(lua_State *L) {
} }
static int inet_global_getnameinfo(lua_State *L) { static int inet_global_getnameinfo(lua_State *L) {
char hbuf[NI_MAXHOST];
char sbuf[NI_MAXSERV];
int i, ret; int i, ret;
char host[1024];
char serv[32];
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *resolved, *iter; struct addrinfo *resolved, *iter;
const char *node = luaL_optstring(L, 1, NULL); const char *host = luaL_optstring(L, 1, NULL);
const char *service = luaL_optstring(L, 2, NULL); const char *serv = luaL_optstring(L, 2, NULL);
if (!(node || service)) if (!(host || serv))
luaL_error(L, "You have to specify a hostname, a service, or both"); luaL_error(L, "host and serv cannot be both nil");
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_family = PF_UNSPEC; hints.ai_family = PF_UNSPEC;
/* getaddrinfo must get a node and a service argument */ ret = getaddrinfo(host, serv, &hints, &resolved);
ret = getaddrinfo(node ? node : "127.0.0.1", service ? service : "7",
&hints, &resolved);
if (ret != 0) { if (ret != 0) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, socket_gaistrerror(ret)); lua_pushstring(L, socket_gaistrerror(ret));
@ -105,19 +103,19 @@ static int inet_global_getnameinfo(lua_State *L) {
lua_newtable(L); lua_newtable(L);
for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) {
getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, host, getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen,
node ? (socklen_t) sizeof(host) : 0, serv, service ? (socklen_t) sizeof(serv) : 0, 0); hbuf, host? (socklen_t) sizeof(hbuf): 0,
sbuf, serv? (socklen_t) sizeof(sbuf): 0, 0);
if (node) { if (host) {
lua_pushnumber(L, i); lua_pushnumber(L, i);
lua_pushstring(L, host); lua_pushstring(L, hbuf);
lua_settable(L, -3); lua_settable(L, -3);
} }
} }
freeaddrinfo(resolved); freeaddrinfo(resolved);
if (service) { if (serv) {
lua_pushstring(L, serv); lua_pushstring(L, sbuf);
return 2; return 2;
} else { } else {
return 1; return 1;
@ -176,19 +174,9 @@ static int inet_global_getaddrinfo(lua_State *L)
} }
lua_newtable(L); lua_newtable(L);
for (iterator = resolved; iterator; iterator = iterator->ai_next) { for (iterator = resolved; iterator; iterator = iterator->ai_next) {
char hbuf[NI_MAXHOST] char hbuf[NI_MAXHOST];
#ifndef _WINDOWS ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen,
,sbuf[NI_MAXSERV] hbuf, (socklen_t) sizeof(hbuf), NULL, 0, NI_NUMERICHOST);
#endif
;
ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf,
(socklen_t) sizeof(hbuf),
#ifdef _WINDOWS
NULL, 0,
#else
sbuf, 0,
#endif
NI_NUMERICHOST);
if (ret){ if (ret){
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, socket_gaistrerror(ret)); lua_pushstring(L, socket_gaistrerror(ret));
@ -218,7 +206,6 @@ static int inet_global_getaddrinfo(lua_State *L)
return 1; return 1;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Gets the host name * Gets the host name
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
@ -237,7 +224,6 @@ static int inet_global_gethostname(lua_State *L)
} }
/*=========================================================================*\ /*=========================================================================*\
* Lua methods * Lua methods
\*=========================================================================*/ \*=========================================================================*/