socket.connect() loops over addresses returned by getaddrinfo

This commit is contained in:
Diego Nehab 2012-04-23 01:29:41 +08:00
parent f960b3872a
commit 966642f76a

View File

@ -19,20 +19,30 @@ function connect(address, port, laddress, lport)
if address == "*" then address = "0.0.0.0" end if address == "*" then address = "0.0.0.0" end
local addrinfo, err = socket.dns.getaddrinfo(address); local addrinfo, err = socket.dns.getaddrinfo(address);
if not addrinfo then return nil, err end if not addrinfo then return nil, err end
local sock, err; local err = "no info on address"
if addrinfo[1].family == "inet" then local sock, res
sock, err = socket.tcp() for i, alt in base.ipairs(addrinfo) do
else if alt.family == "inet" then
sock, err = socket.tcp6() sock, err = socket.tcp()
else
sock, err = socket.tcp6()
end
if not sock then return nil, err end
if laddress then
res, err = sock:bind(laddress, lport)
if not res then
sock:close()
return nil, err
end
end
res, err = sock:connect(alt.addr, port)
if not res then
sock:close()
else
return sock
end
end end
if not sock then return nil, err end return nil, err
if laddress then
local res, err = sock:bind(laddress, lport, -1)
if not res then return nil, err end
end
local res, err = sock:connect(address, port)
if not res then return nil, err end
return sock
end end
function bind(host, port, backlog) function bind(host, port, backlog)