Annotate

net/resolvers/basic.lua @ 12410:596625eed326

net.resolvers.basic: Indicate to callback if we have more targets available
author Matthew Wild <mwild1@gmail.com>
date Fri, 18 Mar 2022 16:13:09 +0000
parent 12409:9f0baf15e792
child 12413:e155f4509954
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8531
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local adns = require "net.adns";
8775
ae7cf011e46a net.resolvers.basic: Support IP address literals
Kim Alvefur <zash@zash.se>
parents: 8531
diff changeset
2 local inet_pton = require "util.net".pton;
10439
97c0f5fe5f41 net.resolvers.basic: Normalise IP literals, ensures net.server is happy
Kim Alvefur <zash@zash.se>
parents: 10437
diff changeset
3 local inet_ntop = require "util.net".ntop;
10384
94c9c574cd8a net.resolvers: Apply IDNA conversion to ascii for DNS lookups (fixes #1426)
Kim Alvefur <zash@zash.se>
parents: 9496
diff changeset
4 local idna_to_ascii = require "util.encodings".idna.to_ascii;
12408
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
5 local promise = require "util.promise";
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
6 local t_move = require "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
12408
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
13 local function do_dns_lookup(self, dns_resolver, record_type, name)
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));
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
26 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
27 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
28 end
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
29
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
30 local targets = { secure = answer.secure };
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
31 for _, record in ipairs(answer) do
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
32 if ipv then
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
33 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
34 else
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
35 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
36 end
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
37 end
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
38 return resolve(targets);
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
39 end, name, record_type, "IN");
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
40 end);
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
41 end
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
42
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
43 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
44 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
45 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
46 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
47 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
48 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
49 end
9f0baf15e792 net.resolvers.basic: Alternate IP address family targets, per RFC 8305
Matthew Wild <mwild1@gmail.com>
parents: 12408
diff changeset
50 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
51 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
52 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
53 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
54 end
12408
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
55 return result;
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
56 end
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
57
8531
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 -- 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
59 -- pass it to cb()
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 function methods:next(cb)
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 if self.targets then
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 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
63 cb(nil);
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 return;
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 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
67 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
68 return;
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
10385
62a7042e0771 net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents: 10384
diff changeset
71 if not self.hostname then
12025
6ed7fd28f5e3 net.resolvers: Report when hostname fails IDNA
Kim Alvefur <zash@zash.se>
parents: 11901
diff changeset
72 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
73 cb(nil);
10400
4c2d789a106b net.resolvers: Fix traceback from hostname failing IDNA
Kim Alvefur <zash@zash.se>
parents: 10385
diff changeset
74 return;
10385
62a7042e0771 net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents: 10384
diff changeset
75 end
62a7042e0771 net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents: 10384
diff changeset
76
12408
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
77 -- Resolve DNS to target list
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
78 local dns_resolver = adns.resolver();
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
79
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
80 local dns_lookups = {
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
81 ipv4 = do_dns_lookup(self, dns_resolver, "A", self.hostname);
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
82 ipv6 = do_dns_lookup(self, dns_resolver, "AAAA", self.hostname);
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
83 tlsa = do_dns_lookup(self, dns_resolver, "TLSA", ("_%d._%s.%s"):format(self.port, self.conntype, self.hostname));
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
84 };
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
85
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
86 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
87 -- 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
88 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
89 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
90
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
91 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
92 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
93 elseif have_ipv4 then
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
94 self.targets = dns_results.ipv4.value;
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
95 elseif have_ipv6 then
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
96 self.targets = dns_results.ipv6.value;
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
97 else
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
98 self.targets = {};
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
99 end
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
100
11414
5a71f14ab77c net.connect: Add DANE support
Kim Alvefur <zash@zash.se>
parents: 11008
diff changeset
101 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
102 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
103 self.extra.tlsa = dns_results.tlsa.value;
11708
5ef729c355f3 Revert 926d53af9a7a: Restore DANE support
Kim Alvefur <zash@zash.se>
parents: 11430
diff changeset
104 self.extra.dane_hostname = self.hostname;
5ef729c355f3 Revert 926d53af9a7a: Restore DANE support
Kim Alvefur <zash@zash.se>
parents: 11430
diff changeset
105 else
5ef729c355f3 Revert 926d53af9a7a: Restore DANE support
Kim Alvefur <zash@zash.se>
parents: 11430
diff changeset
106 self.extra.tlsa = nil;
5ef729c355f3 Revert 926d53af9a7a: Restore DANE support
Kim Alvefur <zash@zash.se>
parents: 11430
diff changeset
107 self.extra.dane_hostname = nil;
11414
5a71f14ab77c net.connect: Add DANE support
Kim Alvefur <zash@zash.se>
parents: 11008
diff changeset
108 end
5a71f14ab77c net.connect: Add DANE support
Kim Alvefur <zash@zash.se>
parents: 11008
diff changeset
109 end
12408
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
110
8531
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 self:next(cb);
12408
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
112 end):catch(function (err)
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
113 self.last_error = err;
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
114 self.targets = {};
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
115 end);
8531
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 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
119 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
120 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
121 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
122
0d702ec77f0c net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents: 10400
diff changeset
123 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
124 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
125 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
126 end
10436
0d702ec77f0c net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents: 10400
diff changeset
127 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
128 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
129 if #is_ip == 16 then
0d702ec77f0c net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents: 10400
diff changeset
130 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
131 elseif #is_ip == 4 then
0d702ec77f0c net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents: 10400
diff changeset
132 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
133 end
0d702ec77f0c net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents: 10400
diff changeset
134 end
0d702ec77f0c net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents: 10400
diff changeset
135
8531
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 return setmetatable({
10436
0d702ec77f0c net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents: 10400
diff changeset
137 hostname = ascii_host;
8531
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 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
139 conn_type = conn_type;
12408
acfc51b9530c net.resolvers.basic: Refactor to remove code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12129
diff changeset
140 extra = extra or {};
10436
0d702ec77f0c net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents: 10400
diff changeset
141 targets = targets;
8531
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 }, resolver_mt);
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 return {
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 new = new;
601681acea73 net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 };