Merge branch 'moteus' of https://github.com/moteus/luasocket into moteus

This commit is contained in:
unknown 2013-05-27 20:32:54 +08:00
commit 5e0b56b8d3
5 changed files with 50 additions and 6 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) {
@ -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) {

View File

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

6
test/test_bind.lua Normal file
View File

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

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

View File

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