From 56dbda39ed07faf2c14427797fe104213f734e00 Mon Sep 17 00:00:00 2001 From: moteus Date: Mon, 27 May 2013 11:20:52 +0400 Subject: [PATCH] Fix. getaddrinfo returns garbage as address on Windows. Add. test_getaddrinfo.lua --- src/inet.c | 21 ++++++++++++++++++--- test/test_getaddrinfo.lua | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 test/test_getaddrinfo.lua 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!")