Software /
code /
prosody
Annotate
net/resolvers/basic.lua @ 13107:9c4dc1e6d2c9
mod_http: Add way to retrieve internal URL instead of external
This could be of help when configuring reverse proxies, as it is the
internal URL the proxy must point at.
Argument treated as an enum "internal" "external"(default) to allow for
future extensibility.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 24 May 2023 14:43:45 +0200 |
parent | 12974:ba409c67353b |
rev | line source |
---|---|
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12815
diff
changeset
|
1 local adns = require "prosody.net.adns"; |
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12815
diff
changeset
|
2 local inet_pton = require "prosody.util.net".pton; |
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12815
diff
changeset
|
3 local inet_ntop = require "prosody.util.net".ntop; |
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12815
diff
changeset
|
4 local idna_to_ascii = require "prosody.util.encodings".idna.to_ascii; |
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12815
diff
changeset
|
5 local promise = require "prosody.util.promise"; |
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12815
diff
changeset
|
6 local t_move = require "prosody.util.table".move; |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local methods = {}; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local resolver_mt = { __index = methods }; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
10485
913276ba0c47
net.connect: Mention RFC 6724 regression
Kim Alvefur <zash@zash.se>
parents:
10484
diff
changeset
|
11 -- FIXME RFC 6724 |
10484
b13a31cea7d9
net.connect: Add some TODOs and FIXMEs
Kim Alvefur <zash@zash.se>
parents:
10441
diff
changeset
|
12 |
12601
72f7bb3f30d3
net.resolvers.basic: Add opt-out argument for DNSSEC security status
Kim Alvefur <zash@zash.se>
parents:
12413
diff
changeset
|
13 local function do_dns_lookup(self, dns_resolver, record_type, name, allow_insecure) |
12408
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
14 return promise.new(function (resolve, reject) |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
15 local ipv = (record_type == "A" and "4") or (record_type == "AAAA" and "6") or nil; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
16 if ipv and self.extra["use_ipv"..ipv] == false then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
17 return reject(("IPv%s disabled - %s lookup skipped"):format(ipv, record_type)); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
18 elseif record_type == "TLSA" and self.extra.use_dane ~= true then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
19 return reject("DANE disabled - TLSA lookup skipped"); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
20 end |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
21 dns_resolver:lookup(function (answer, err) |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
22 if not answer then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
23 return reject(err); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
24 elseif answer.bogus then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
25 return reject(("Validation error in %s lookup"):format(record_type)); |
12601
72f7bb3f30d3
net.resolvers.basic: Add opt-out argument for DNSSEC security status
Kim Alvefur <zash@zash.se>
parents:
12413
diff
changeset
|
26 elseif not (answer.secure or allow_insecure) then |
72f7bb3f30d3
net.resolvers.basic: Add opt-out argument for DNSSEC security status
Kim Alvefur <zash@zash.se>
parents:
12413
diff
changeset
|
27 return reject(("Insecure response in %s lookup"):format(record_type)); |
12408
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
28 elseif answer.status and #answer == 0 then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
29 return reject(("%s in %s lookup"):format(answer.status, record_type)); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
30 end |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
31 |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
32 local targets = { secure = answer.secure }; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
33 for _, record in ipairs(answer) do |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
34 if ipv then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
35 table.insert(targets, { self.conn_type..ipv, record[record_type:lower()], self.port, self.extra }); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
36 else |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
37 table.insert(targets, record[record_type:lower()]); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
38 end |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
39 end |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
40 return resolve(targets); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
41 end, name, record_type, "IN"); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
42 end); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
43 end |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
44 |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
45 local function merge_targets(ipv4_targets, ipv6_targets) |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
46 local result = { secure = ipv4_targets.secure and ipv6_targets.secure }; |
12409
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
47 local common_length = math.min(#ipv4_targets, #ipv6_targets); |
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
48 for i = 1, common_length do |
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
49 table.insert(result, ipv6_targets[i]); |
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
50 table.insert(result, ipv4_targets[i]); |
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
51 end |
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
52 if common_length < #ipv4_targets then |
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
53 t_move(ipv4_targets, common_length+1, #ipv4_targets, common_length+1, result); |
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
54 elseif common_length < #ipv6_targets then |
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
55 t_move(ipv6_targets, common_length+1, #ipv6_targets, common_length+1, result); |
9f0baf15e792
net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents:
12408
diff
changeset
|
56 end |
12408
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
57 return result; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
58 end |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
59 |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 -- Find the next target to connect to, and |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 -- pass it to cb() |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 function methods:next(cb) |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 if self.targets then |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 if #self.targets == 0 then |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 cb(nil); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 return; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 local next_target = table.remove(self.targets, 1); |
12410
596625eed326
net.resolvers.basic: Indicate to callback if we have more targets available
Matthew Wild <mwild1@gmail.com>
parents:
12409
diff
changeset
|
69 cb(next_target[1], next_target[2], next_target[3], next_target[4], not not self.targets[1]); |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 return; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
10385
62a7042e0771
net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents:
10384
diff
changeset
|
73 if not self.hostname then |
12025
6ed7fd28f5e3
net.resolvers: Report when hostname fails IDNA
Kim Alvefur <zash@zash.se>
parents:
11901
diff
changeset
|
74 self.last_error = "hostname failed IDNA"; |
10385
62a7042e0771
net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents:
10384
diff
changeset
|
75 cb(nil); |
10400
4c2d789a106b
net.resolvers: Fix traceback from hostname failing IDNA
Kim Alvefur <zash@zash.se>
parents:
10385
diff
changeset
|
76 return; |
10385
62a7042e0771
net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents:
10384
diff
changeset
|
77 end |
62a7042e0771
net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents:
10384
diff
changeset
|
78 |
12408
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
79 -- Resolve DNS to target list |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
80 local dns_resolver = adns.resolver(); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
81 |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
82 local dns_lookups = { |
12601
72f7bb3f30d3
net.resolvers.basic: Add opt-out argument for DNSSEC security status
Kim Alvefur <zash@zash.se>
parents:
12413
diff
changeset
|
83 ipv4 = do_dns_lookup(self, dns_resolver, "A", self.hostname, true); |
72f7bb3f30d3
net.resolvers.basic: Add opt-out argument for DNSSEC security status
Kim Alvefur <zash@zash.se>
parents:
12413
diff
changeset
|
84 ipv6 = do_dns_lookup(self, dns_resolver, "AAAA", self.hostname, true); |
12413
e155f4509954
net.resolvers.basic: Fix incorrect field name (thanks CI)
Matthew Wild <mwild1@gmail.com>
parents:
12410
diff
changeset
|
85 tlsa = do_dns_lookup(self, dns_resolver, "TLSA", ("_%d._%s.%s"):format(self.port, self.conn_type, self.hostname)); |
12408
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
86 }; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
87 |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
88 promise.all_settled(dns_lookups):next(function (dns_results) |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
89 -- Combine targets, assign to self.targets, self:next(cb) |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
90 local have_ipv4 = dns_results.ipv4.status == "fulfilled"; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
91 local have_ipv6 = dns_results.ipv6.status == "fulfilled"; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
92 |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
93 if have_ipv4 and have_ipv6 then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
94 self.targets = merge_targets(dns_results.ipv4.value, dns_results.ipv6.value); |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
95 elseif have_ipv4 then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
96 self.targets = dns_results.ipv4.value; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
97 elseif have_ipv6 then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
98 self.targets = dns_results.ipv6.value; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
99 else |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
100 self.targets = {}; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
101 end |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
102 |
11414 | 103 if self.extra and self.extra.use_dane then |
12408
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
104 if self.targets.secure and dns_results.tlsa.status == "fulfilled" then |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
105 self.extra.tlsa = dns_results.tlsa.value; |
11708
5ef729c355f3
Revert 926d53af9a7a: Restore DANE support
Kim Alvefur <zash@zash.se>
parents:
11430
diff
changeset
|
106 self.extra.dane_hostname = self.hostname; |
5ef729c355f3
Revert 926d53af9a7a: Restore DANE support
Kim Alvefur <zash@zash.se>
parents:
11430
diff
changeset
|
107 else |
5ef729c355f3
Revert 926d53af9a7a: Restore DANE support
Kim Alvefur <zash@zash.se>
parents:
11430
diff
changeset
|
108 self.extra.tlsa = nil; |
5ef729c355f3
Revert 926d53af9a7a: Restore DANE support
Kim Alvefur <zash@zash.se>
parents:
11430
diff
changeset
|
109 self.extra.dane_hostname = nil; |
11414 | 110 end |
12815
2d134201dc55
net.resolvers.basic: Record hostname coming from secure SRV records
Kim Alvefur <zash@zash.se>
parents:
12601
diff
changeset
|
111 elseif self.extra and self.extra.srv_secure then |
2d134201dc55
net.resolvers.basic: Record hostname coming from secure SRV records
Kim Alvefur <zash@zash.se>
parents:
12601
diff
changeset
|
112 self.extra.secure_hostname = self.hostname; |
11414 | 113 end |
12408
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
114 |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 self:next(cb); |
12408
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
116 end):catch(function (err) |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
117 self.last_error = err; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
118 self.targets = {}; |
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
119 end); |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 local function new(hostname, port, conn_type, extra) |
10436
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
123 local ascii_host = idna_to_ascii(hostname); |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
124 local targets = nil; |
11007
1d8e1f7a587c
net.resolvers.basic: Default conn_type to 'tcp' consistently if unspecified (thanks marc0s)
Matthew Wild <mwild1@gmail.com>
parents:
10439
diff
changeset
|
125 conn_type = conn_type or "tcp"; |
10436
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
126 |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
127 local is_ip = inet_pton(hostname); |
10437
fcfc8f4d14a8
net.resolvers.basic: Fix resolution of IPv6 literals (in brackets) (fixes #1459)
Kim Alvefur <zash@zash.se>
parents:
10436
diff
changeset
|
128 if not is_ip and hostname:sub(1,1) == '[' then |
fcfc8f4d14a8
net.resolvers.basic: Fix resolution of IPv6 literals (in brackets) (fixes #1459)
Kim Alvefur <zash@zash.se>
parents:
10436
diff
changeset
|
129 is_ip = inet_pton(hostname:sub(2,-2)); |
fcfc8f4d14a8
net.resolvers.basic: Fix resolution of IPv6 literals (in brackets) (fixes #1459)
Kim Alvefur <zash@zash.se>
parents:
10436
diff
changeset
|
130 end |
10436
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
131 if is_ip then |
10439
97c0f5fe5f41
net.resolvers.basic: Normalise IP literals, ensures net.server is happy
Kim Alvefur <zash@zash.se>
parents:
10437
diff
changeset
|
132 hostname = inet_ntop(is_ip); |
10436
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
133 if #is_ip == 16 then |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
134 targets = { { conn_type.."6", hostname, port, extra } }; |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
135 elseif #is_ip == 4 then |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
136 targets = { { conn_type.."4", hostname, port, extra } }; |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
137 end |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
138 end |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
139 |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 return setmetatable({ |
10436
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
141 hostname = ascii_host; |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 port = port; |
11007
1d8e1f7a587c
net.resolvers.basic: Default conn_type to 'tcp' consistently if unspecified (thanks marc0s)
Matthew Wild <mwild1@gmail.com>
parents:
10439
diff
changeset
|
143 conn_type = conn_type; |
12408
acfc51b9530c
net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents:
12129
diff
changeset
|
144 extra = extra or {}; |
10436
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
145 targets = targets; |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 }, resolver_mt); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 return { |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 new = new; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 }; |