diff --git a/src/inet.c b/src/inet.c index 5bc6364..b28852b 100644 --- a/src/inet.c +++ b/src/inet.c @@ -176,9 +176,24 @@ static int inet_global_getaddrinfo(lua_State *L) } lua_newtable(L); for (iterator = resolved; iterator; iterator = iterator->ai_next) { - char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; - getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf, - (socklen_t) sizeof(hbuf), sbuf, 0, NI_NUMERICHOST); + char hbuf[NI_MAXHOST] +#ifndef _WINDOWS + ,sbuf[NI_MAXSERV] +#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){ + lua_pushnil(L); + lua_pushstring(L, socket_gaistrerror(ret)); + return 2; + } lua_pushnumber(L, i); lua_newtable(L); switch (iterator->ai_family) { diff --git a/test/test_getaddrinfo.lua b/test/test_getaddrinfo.lua new file mode 100644 index 0000000..4b52ff9 --- /dev/null +++ b/test/test_getaddrinfo.lua @@ -0,0 +1,15 @@ +local socket = require "socket" +local addresses = assert(socket.dns.getaddrinfo("localhost")) +assert(type(addresses) == 'table') + +local ipv4mask = "^%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?$" + +for i, alt in ipairs(addresses) do + if alt.family == 'inet' then + assert(type(alt.addr) == 'string') + assert(alt.addr:find(ipv4mask)) + assert(alt.addr == '127.0.0.1') + end +end + +print("done!")