diff options
author | Diego Nehab <diego.nehab@gmail.com> | 2012-04-23 01:40:31 +0800 |
---|---|---|
committer | Diego Nehab <diego.nehab@gmail.com> | 2012-04-23 01:40:31 +0800 |
commit | 1acf8188cd732de4fd5fcdc016eeab501c86a773 (patch) | |
tree | 5d773638ae76b0d4d09c32b6f8a2dddc59449f25 | |
parent | 966642f76abeea579ddcf8da8651244fcf5cbd5a (diff) | |
download | luasocket-1acf8188cd732de4fd5fcdc016eeab501c86a773.tar.gz luasocket-1acf8188cd732de4fd5fcdc016eeab501c86a773.tar.bz2 luasocket-1acf8188cd732de4fd5fcdc016eeab501c86a773.zip |
socket.bind also tries all addresses returned by getaddrinfo.
-rw-r--r-- | TODO | 3 | ||||
-rw-r--r-- | src/socket.lua | 36 |
2 files changed, 25 insertions, 14 deletions
@@ -1,3 +1,4 @@ | |||
1 | - document bind and connect behavior. | ||
1 | - getsockname should also support IPv6, no? | 2 | - getsockname should also support IPv6, no? |
2 | - shouldn't we instead make the code compatible to Lua 5.2 | 3 | - shouldn't we instead make the code compatible to Lua 5.2 |
3 | without any compat stuff, and use a compatibility layer to | 4 | without any compat stuff, and use a compatibility layer to |
@@ -5,7 +6,6 @@ | |||
5 | - add what's new to manual | 6 | - add what's new to manual |
6 | - should there be an equivalent to tohostname for IPv6? | 7 | - should there be an equivalent to tohostname for IPv6? |
7 | - should we add service name resolution as well to getaddrinfo? | 8 | - should we add service name resolution as well to getaddrinfo? |
8 | - document bind and connect behavior based on address? | ||
9 | 9 | ||
10 | - add http POST sample to manual | 10 | - add http POST sample to manual |
11 | people keep asking stupid questions | 11 | people keep asking stupid questions |
@@ -16,6 +16,7 @@ | |||
16 | 16 | ||
17 | Done: | 17 | Done: |
18 | 18 | ||
19 | - connect and bind try all adresses returned by getaddrinfo | ||
19 | - document headers.lua? | 20 | - document headers.lua? |
20 | - update copyright date everywhere? | 21 | - update copyright date everywhere? |
21 | - remove RCSID from files? | 22 | - remove RCSID from files? |
diff --git a/src/socket.lua b/src/socket.lua index 9035443..8c5f231 100644 --- a/src/socket.lua +++ b/src/socket.lua | |||
@@ -19,8 +19,8 @@ function connect(address, port, laddress, lport) | |||
19 | if address == "*" then address = "0.0.0.0" end | 19 | if address == "*" then address = "0.0.0.0" end |
20 | local addrinfo, err = socket.dns.getaddrinfo(address); | 20 | local addrinfo, err = socket.dns.getaddrinfo(address); |
21 | if not addrinfo then return nil, err end | 21 | if not addrinfo then return nil, err end |
22 | local err = "no info on address" | ||
23 | local sock, res | 22 | local sock, res |
23 | err = "no info on address" | ||
24 | for i, alt in base.ipairs(addrinfo) do | 24 | for i, alt in base.ipairs(addrinfo) do |
25 | if alt.family == "inet" then | 25 | if alt.family == "inet" then |
26 | sock, err = socket.tcp() | 26 | sock, err = socket.tcp() |
@@ -49,19 +49,29 @@ function bind(host, port, backlog) | |||
49 | if host == "*" then host = "0.0.0.0" end | 49 | if host == "*" then host = "0.0.0.0" end |
50 | local addrinfo, err = socket.dns.getaddrinfo(host); | 50 | local addrinfo, err = socket.dns.getaddrinfo(host); |
51 | if not addrinfo then return nil, err end | 51 | if not addrinfo then return nil, err end |
52 | local sock, err; | 52 | local sock, res |
53 | if addrinfo[1].family == "inet" then | 53 | err = "no info on address" |
54 | sock, err = socket.tcp() | 54 | for i, alt in base.ipairs(addrinfo) do |
55 | else | 55 | if alt.family == "inet" then |
56 | sock, err = socket.tcp6() | 56 | sock, err = socket.tcp() |
57 | else | ||
58 | sock, err = socket.tcp6() | ||
59 | end | ||
60 | if not sock then return nil, err end | ||
61 | sock:setoption("reuseaddr", true) | ||
62 | res, err = sock:bind(alt.addr, port) | ||
63 | if not res then | ||
64 | sock:close() | ||
65 | else | ||
66 | res, err = sock:listen(backlog) | ||
67 | if not res then | ||
68 | sock:close() | ||
69 | else | ||
70 | return sock | ||
71 | end | ||
72 | end | ||
57 | end | 73 | end |
58 | if not sock then return nil, err end | 74 | return nil, err |
59 | sock:setoption("reuseaddr", true) | ||
60 | local res, err = sock:bind(host, port) | ||
61 | if not res then return nil, err end | ||
62 | res, err = sock:listen(backlog) | ||
63 | if not res then return nil, err end | ||
64 | return sock | ||
65 | end | 75 | end |
66 | 76 | ||
67 | try = newtry() | 77 | try = newtry() |