Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

socket.bind also tries all addresses returned by getaddrinfo.

  • Loading branch information...
commit 1acf8188cd732de4fd5fcdc016eeab501c86a773 1 parent 966642f
Diego Nehab diegonehab authored

Showing 2 changed files with 25 additions and 14 deletions. Show diff stats Hide diff stats

  1. +2 1  TODO
  2. +23 13 src/socket.lua
3  TODO
... ... @@ -1,3 +1,4 @@
  1 +- document bind and connect behavior.
1 2 - getsockname should also support IPv6, no?
2 3 - shouldn't we instead make the code compatible to Lua 5.2
3 4 without any compat stuff, and use a compatibility layer to
@@ -5,7 +6,6 @@
5 6 - add what's new to manual
6 7 - should there be an equivalent to tohostname for IPv6?
7 8 - should we add service name resolution as well to getaddrinfo?
8   -- document bind and connect behavior based on address?
9 9
10 10 - add http POST sample to manual
11 11 people keep asking stupid questions
@@ -16,6 +16,7 @@
16 16
17 17 Done:
18 18
  19 +- connect and bind try all adresses returned by getaddrinfo
19 20 - document headers.lua?
20 21 - update copyright date everywhere?
21 22 - remove RCSID from files?
36 src/socket.lua
@@ -19,8 +19,8 @@ function connect(address, port, laddress, lport)
19 19 if address == "*" then address = "0.0.0.0" end
20 20 local addrinfo, err = socket.dns.getaddrinfo(address);
21 21 if not addrinfo then return nil, err end
22   - local err = "no info on address"
23 22 local sock, res
  23 + err = "no info on address"
24 24 for i, alt in base.ipairs(addrinfo) do
25 25 if alt.family == "inet" then
26 26 sock, err = socket.tcp()
@@ -49,19 +49,29 @@ function bind(host, port, backlog)
49 49 if host == "*" then host = "0.0.0.0" end
50 50 local addrinfo, err = socket.dns.getaddrinfo(host);
51 51 if not addrinfo then return nil, err end
52   - local sock, err;
53   - if addrinfo[1].family == "inet" then
54   - sock, err = socket.tcp()
55   - else
56   - sock, err = socket.tcp6()
  52 + local sock, res
  53 + err = "no info on address"
  54 + for i, alt in base.ipairs(addrinfo) do
  55 + if alt.family == "inet" then
  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 73 end
58   - if not sock then return nil, err end
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
  74 + return nil, err
65 75 end
66 76
67 77 try = newtry()

0 comments on commit 1acf818

Please sign in to comment.
Something went wrong with that request. Please try again.