mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-11-08 14:28:21 +01:00
Fix. getaddrinfo returns garbage as address on Windows.
Add. test_getaddrinfo.lua
This commit is contained in:
parent
fbe184f28a
commit
56dbda39ed
21
src/inet.c
21
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) {
|
||||
|
15
test/test_getaddrinfo.lua
Normal file
15
test/test_getaddrinfo.lua
Normal 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!")
|
Loading…
Reference in New Issue
Block a user