diff --git a/src/inet.c b/src/inet.c index 51e8cfe..eab325e 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) { @@ -463,6 +478,9 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv, struct addrinfo *iterator = NULL, *resolved = NULL; const char *err = NULL; t_socket sock = *ps; + /* translate luasocket special values to C */ + if (strcmp(address, "*") == 0) address = NULL; + if (!serv) serv = "0"; /* try resolving */ err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved)); if (err) { diff --git a/src/tcp.c b/src/tcp.c index ca8eec2..60c1e8a 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -222,7 +222,6 @@ static int meth_bind(lua_State *L) bindhints.ai_socktype = SOCK_STREAM; bindhints.ai_family = tcp->family; bindhints.ai_flags = AI_PASSIVE; - address = strcmp(address, "*")? address: NULL; err = inet_trybind(&tcp->sock, address, port, &bindhints); if (err) { lua_pushnil(L); diff --git a/test/test_bind.lua b/test/test_bind.lua new file mode 100644 index 0000000..93c42d7 --- /dev/null +++ b/test/test_bind.lua @@ -0,0 +1,6 @@ +local socket = require "socket" +local u = socket.udp() assert(u:setsockname("*", 5088)) u:close() +local u = socket.udp() assert(u:setsockname("*", 0)) u:close() +local t = socket.tcp() assert(t:bind("*", 5088)) t:close() +local t = socket.tcp() assert(t:bind("*", 0)) t:close() +print("done!") \ No newline at end of file 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!") diff --git a/test/testclnt.lua b/test/testclnt.lua index 8acb3d0..315783b 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua @@ -642,7 +642,10 @@ local tcp_methods = { "shutdown", } test_methods(socket.tcp(), tcp_methods) -test_methods(socket.tcp6(), tcp_methods) +do local sock = socket.tcp6() +if sock then test_methods(socket.tcp6(), tcp_methods) +else io.stderr:write("Warning! IPv6 does not support!\n") end +end local udp_methods = { "close", @@ -666,7 +669,10 @@ local udp_methods = { ------------------------------------------------------------------------ test_methods(socket.udp(), udp_methods) -test_methods(socket.udp6(), udp_methods) +do local sock = socket.tcp6() +if sock then test_methods(socket.udp6(), udp_methods) +else io.stderr:write("Warning! IPv6 does not support!\n") end +end test("partial receive") test_partialrecv()