Skip to content
Permalink
Browse files
luasocket unstable building on XP with mingw - based on Paul K fix
  • Loading branch information
stevedonovan committed Apr 10, 2013
1 parent 62ba225 commit 59e5bbc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 10 deletions.
@@ -25,6 +25,11 @@ typedef union {
struct sockaddr_in6 sa6;
} SA_union;

#ifdef INET_PTON
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
int inet_pton(int af, const char *src, void *dst);
#endif


/*=========================================================================*\
* Internal function prototypes.
@@ -608,4 +613,54 @@ int inet_aton(const char *cp, struct in_addr *inp)
}
#endif

// inet_ntop/inet_pton for MinGW from http://mingw-users.1079350.n2.nabble.com/IPv6-getaddrinfo-amp-inet-ntop-td5891996.html

#ifdef INET_PTON
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
if (af == AF_INET)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
in.sin_family = AF_INET;
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
else if (af == AF_INET6)
{
struct sockaddr_in6 in;
memset(&in, 0, sizeof(in));
in.sin6_family = AF_INET6;
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
return dst;
}
return NULL;
}

int inet_pton(int af, const char *src, void *dst)
{
struct addrinfo hints, *res, *ressave;

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = af;

if (getaddrinfo(src, NULL, &hints, &res) != 0)
{
return -1;
}

ressave = res;

while (res)
{
memcpy(dst, res->ai_addr, res->ai_addrlen);
res = res->ai_next;
}

freeaddrinfo(ressave);
return 0;
}

#endif
@@ -92,6 +92,11 @@ static int base_open(lua_State *L) {
lua_pushstring(L, "_DEBUG");
lua_pushboolean(L, 1);
lua_rawset(L, -3);
#endif
#ifdef INET_PTON
lua_pushstring(L, "_BROKEN_XP");
lua_pushboolean(L, 1);
lua_rawset(L, -3);
#endif
/* make version string available to scripts */
lua_pushstring(L, "_VERSION");
@@ -24,6 +24,16 @@ function connect6(address, port, laddress, lport)
end

function bind(host, port, backlog)
if socket._BROKEN_XP and (host == "localhost" or host == "*") then
sock, err = socket.tcp()
if not sock then return nil, err end
sock:setoption("reuseaddr",true)
res, err = sock:bind(host,port)
if not res then return nil, err end
res, err = sock:listen(backlog)
if not res then sock:close(); return nil,err end
return sock
end
if host == "*" then host = "0.0.0.0" end
local addrinfo, err = socket.dns.getaddrinfo(host);
if not addrinfo then return nil, err end
@@ -38,16 +48,16 @@ function bind(host, port, backlog)
if not sock then return nil, err end
sock:setoption("reuseaddr", true)
res, err = sock:bind(alt.addr, port)
if not res then
if not res then
sock:close()
else
else
res, err = sock:listen(backlog)
if not res then
if not res then
sock:close()
else
return sock
end
end
end
end
return nil, err
end
@@ -1,13 +1,13 @@
socket = require("socket");
socket = require("socket")
host = host or "localhost"
port = port or "8383"
server = assert(socket.bind(host, port))
loadstring = loadstring or load -- Lua 5.2 compat
host = host or "localhost";
port = port or "8383";
server = assert(socket.bind(host, port));
ack = "\n";
while 1 do
print("server: waiting for client connection...");
control = assert(server:accept());
while 1 do
while 1 do
command, emsg = control:receive();
if emsg == "closed" then
control:close()
@@ -3,7 +3,7 @@ COMMON='timeout buffer auxiliar options io'
COMMON = COMMON..' '..choose(WINDOWS,'wsocket','usocket')
SCORE=COMMON..' luasocket inet tcp udp except select'

luabuild.lua('ftp.lua http.lua smtp.lua tp.lua url.lua','socket')
luabuild.lua('ftp.lua http.lua smtp.lua headers.lua tp.lua url.lua','socket')
luabuild.lua('socket.lua ltn12.lua')
luabuild.test 'test-driver.lua'

@@ -13,6 +13,7 @@ if not luabuild.config.no_lua51_compat then
end
if WINDOWS then
libs = 'ws2_32'
defines = defines..' _WIN32_WINNT=0x0501 IPV6_V6ONLY=27 INET_PTON'
end

-- needs='sockets',

0 comments on commit 59e5bbc

Please sign in to comment.