Adapt socket.bind() to create a IPv4 or IPv6 depending on the address given

This commit is contained in:
Florian Zeitz 2011-06-14 02:04:37 +02:00 committed by Sam Roberts
parent 5065a2585c
commit 4df1618aab

View File

@ -29,7 +29,15 @@ function connect(address, port, laddress, lport)
end
function bind(host, port, backlog)
local sock, err = socket.tcp()
if host == "*" then host = "0.0.0.0" end
local addrinfo, err = socket.dns.getaddrinfo(host);
if not addrinfo then return nil, err end
local sock, err;
if addrinfo[1].family == "inet" then
sock, err = socket.tcp()
else
sock, err = socket.tcp6()
end
if not sock then return nil, err end
sock:setoption("reuseaddr", true)
local res, err = sock:bind(host, port)