Fix. getaddrinfo returns garbage as address on Windows.

Add. test_getaddrinfo.lua
master
moteus 2013-05-27 11:20:52 +04:00
parent fbe184f28a
commit 56dbda39ed
2 changed files with 33 additions and 3 deletions

View File

@ -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) {

15
test/test_getaddrinfo.lua Normal file
View File

@ -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!")