Software /
code /
prosody
Comparison
net/resolvers/basic.lua @ 8531:601681acea73
net.connect: New API for outgoing connections, based on 'service resolvers'
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 23 Feb 2018 15:53:45 +0000 |
child | 8775:ae7cf011e46a |
comparison
equal
deleted
inserted
replaced
8530:075df839c110 | 8531:601681acea73 |
---|---|
1 local adns = require "net.adns"; | |
2 | |
3 local methods = {}; | |
4 local resolver_mt = { __index = methods }; | |
5 | |
6 -- Find the next target to connect to, and | |
7 -- pass it to cb() | |
8 function methods:next(cb) | |
9 if self.targets then | |
10 if #self.targets == 0 then | |
11 cb(nil); | |
12 return; | |
13 end | |
14 local next_target = table.remove(self.targets, 1); | |
15 cb(unpack(next_target, 1, 4)); | |
16 return; | |
17 end | |
18 | |
19 local targets = {}; | |
20 local n = 2; | |
21 local function ready() | |
22 n = n - 1; | |
23 if n > 0 then return; end | |
24 self.targets = targets; | |
25 self:next(cb); | |
26 end | |
27 | |
28 -- Resolve DNS to target list | |
29 local dns_resolver = adns.resolver(); | |
30 dns_resolver:lookup(function (answer) | |
31 if answer then | |
32 for _, record in ipairs(answer) do | |
33 table.insert(targets, { self.conn_type, record.a, self.port, self.extra }); | |
34 end | |
35 end | |
36 ready(); | |
37 end, self.hostname, "A", "IN"); | |
38 | |
39 dns_resolver:lookup(function (answer) | |
40 if answer then | |
41 for _, record in ipairs(answer) do | |
42 table.insert(targets, { self.conn_type.."6", record.aaaa, self.port, self.extra }); | |
43 end | |
44 end | |
45 ready(); | |
46 end, self.hostname, "AAAA", "IN"); | |
47 end | |
48 | |
49 local function new(hostname, port, conn_type, extra) | |
50 return setmetatable({ | |
51 hostname = hostname; | |
52 port = port; | |
53 conn_type = conn_type or "tcp"; | |
54 extra = extra; | |
55 }, resolver_mt); | |
56 end | |
57 | |
58 return { | |
59 new = new; | |
60 }; |