mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 12:28:21 +01:00
Merge branch 'moteus' of https://github.com/moteus/luasocket into moteus
This commit is contained in:
commit
5e0b56b8d3
24
src/inet.c
24
src/inet.c
@ -176,9 +176,24 @@ static int inet_global_getaddrinfo(lua_State *L)
|
|||||||
}
|
}
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
for (iterator = resolved; iterator; iterator = iterator->ai_next) {
|
for (iterator = resolved; iterator; iterator = iterator->ai_next) {
|
||||||
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
char hbuf[NI_MAXHOST]
|
||||||
getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf,
|
#ifndef _WINDOWS
|
||||||
(socklen_t) sizeof(hbuf), sbuf, 0, NI_NUMERICHOST);
|
,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_pushnumber(L, i);
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
switch (iterator->ai_family) {
|
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;
|
struct addrinfo *iterator = NULL, *resolved = NULL;
|
||||||
const char *err = NULL;
|
const char *err = NULL;
|
||||||
t_socket sock = *ps;
|
t_socket sock = *ps;
|
||||||
|
/* translate luasocket special values to C */
|
||||||
|
if (strcmp(address, "*") == 0) address = NULL;
|
||||||
|
if (!serv) serv = "0";
|
||||||
/* try resolving */
|
/* try resolving */
|
||||||
err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved));
|
err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved));
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -222,7 +222,6 @@ static int meth_bind(lua_State *L)
|
|||||||
bindhints.ai_socktype = SOCK_STREAM;
|
bindhints.ai_socktype = SOCK_STREAM;
|
||||||
bindhints.ai_family = tcp->family;
|
bindhints.ai_family = tcp->family;
|
||||||
bindhints.ai_flags = AI_PASSIVE;
|
bindhints.ai_flags = AI_PASSIVE;
|
||||||
address = strcmp(address, "*")? address: NULL;
|
|
||||||
err = inet_trybind(&tcp->sock, address, port, &bindhints);
|
err = inet_trybind(&tcp->sock, address, port, &bindhints);
|
||||||
if (err) {
|
if (err) {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
|
6
test/test_bind.lua
Normal file
6
test/test_bind.lua
Normal 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
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!")
|
@ -642,7 +642,10 @@ local tcp_methods = {
|
|||||||
"shutdown",
|
"shutdown",
|
||||||
}
|
}
|
||||||
test_methods(socket.tcp(), tcp_methods)
|
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 = {
|
local udp_methods = {
|
||||||
"close",
|
"close",
|
||||||
@ -666,7 +669,10 @@ local udp_methods = {
|
|||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
test_methods(socket.udp(), 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("partial receive")
|
||||||
test_partialrecv()
|
test_partialrecv()
|
||||||
|
Loading…
Reference in New Issue
Block a user