Software /
code /
prosody
Annotate
net/resolvers/basic.lua @ 10557:e1cb869e2f6c
mod_legacyauth: Report failure from sessionmanager (mostly invalid username)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 23 Dec 2019 23:11:28 +0100 (2019-12-23) |
parent | 10485:913276ba0c47 |
child | 10621:e5ab31845094 |
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; |
9691
e11e076f0eb8
various: Don't rely on _G.unpack existing
Kim Alvefur <zash@zash.se>
parents:
9496
diff
changeset
|
5 local unpack = table.unpack or unpack; -- luacheck: ignore 113 |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local methods = {}; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 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
|
9 |
10484
b13a31cea7d9
net.connect: Add some TODOs and FIXMEs
Kim Alvefur <zash@zash.se>
parents:
10441
diff
changeset
|
10 -- TODO Respect use_ipv4, use_ipv6 |
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 -- FIXME #1428 Reuse DNS resolver object (from service resolver) |
b13a31cea7d9
net.connect: Add some TODOs and FIXMEs
Kim Alvefur <zash@zash.se>
parents:
10441
diff
changeset
|
13 -- FIXME #1429 Close DNS resolver object when done |
b13a31cea7d9
net.connect: Add some TODOs and FIXMEs
Kim Alvefur <zash@zash.se>
parents:
10441
diff
changeset
|
14 |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 -- 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
|
16 -- pass it to cb() |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 function methods:next(cb) |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 if self.targets then |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 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
|
20 cb(nil); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 return; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local next_target = table.remove(self.targets, 1); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 cb(unpack(next_target, 1, 4)); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 return; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
10385
62a7042e0771
net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents:
10384
diff
changeset
|
28 if not self.hostname then |
62a7042e0771
net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents:
10384
diff
changeset
|
29 -- FIXME report IDNA error |
62a7042e0771
net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents:
10384
diff
changeset
|
30 cb(nil); |
10400
4c2d789a106b
net.resolvers: Fix traceback from hostname failing IDNA
Kim Alvefur <zash@zash.se>
parents:
10385
diff
changeset
|
31 return; |
10385
62a7042e0771
net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents:
10384
diff
changeset
|
32 end |
62a7042e0771
net.resolvers: Abort on hostnames not passing IDNA validation
Kim Alvefur <zash@zash.se>
parents:
10384
diff
changeset
|
33 |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local targets = {}; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local n = 2; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local function ready() |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 n = n - 1; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 if n > 0 then return; end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 self.targets = targets; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 self:next(cb); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 -- Resolve DNS to target list |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 local dns_resolver = adns.resolver(); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 dns_resolver:lookup(function (answer) |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 if answer then |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 for _, record in ipairs(answer) do |
9496
4ac3103787cc
net.resolvers.basic: Suffix IPv4 TCP socket types with '4' to match eg 'tcp6'
Kim Alvefur <zash@zash.se>
parents:
8775
diff
changeset
|
48 table.insert(targets, { self.conn_type.."4", record.a, self.port, self.extra }); |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 ready(); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 end, self.hostname, "A", "IN"); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 dns_resolver:lookup(function (answer) |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 if answer then |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 for _, record in ipairs(answer) do |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 table.insert(targets, { self.conn_type.."6", record.aaaa, self.port, self.extra }); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 ready(); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end, self.hostname, "AAAA", "IN"); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 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
|
65 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
|
66 local targets = nil; |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
67 |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 end |
10436
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
72 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
|
73 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
|
74 if #is_ip == 16 then |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
75 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
|
76 elseif #is_ip == 4 then |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
77 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
|
78 end |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
79 end |
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
80 |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 return setmetatable({ |
10436
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
82 hostname = ascii_host; |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 port = port; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 conn_type = conn_type or "tcp"; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 extra = extra; |
10436
0d702ec77f0c
net.resolvers.basic: Move IP literal check to constructor
Kim Alvefur <zash@zash.se>
parents:
10400
diff
changeset
|
86 targets = targets; |
8531
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 }, resolver_mt); |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 end |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 return { |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 new = new; |
601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 }; |