Comparison

net/resolvers/service.lua @ 9388:a5d11627ce5d

net.resolvers.service: net.connect resolver that uses SRV records
author Kim Alvefur <zash@zash.se>
date Thu, 27 Sep 2018 12:25:46 +0200
parent 8775:net/resolvers/basic.lua@ae7cf011e46a
child 9392:f2d71e4284b7
comparison
equal deleted inserted replaced
9387:33e52f727f0f 9388:a5d11627ce5d
1 local adns = require "net.adns";
2 local basic = require "net.resolvers.basic";
3
4 local methods = {};
5 local resolver_mt = { __index = methods };
6
7 -- Find the next target to connect to, and
8 -- pass it to cb()
9 function methods:next(cb)
10 if self.targets then
11 if #self.targets == 0 then
12 cb(nil);
13 return;
14 end
15 local next_target = table.remove(self.targets, 1);
16 self.resolver = basic.new(unpack(next_target, 1, 4));
17 self.resolver:next(function (...)
18 if ... == nil then
19 self:next(cb);
20 else
21 cb(...);
22 end
23 end);
24 return;
25 end
26
27 local targets = {};
28 local function ready()
29 self.targets = targets;
30 self:next(cb);
31 end
32
33 -- Resolve DNS to target list
34 local dns_resolver = adns.resolver();
35 dns_resolver:lookup(function (answer)
36 if answer then
37 table.sort(answer, function (a, b) return a.priority > b.priority end);
38 for _, record in ipairs(answer) do
39 table.insert(targets, { record.srv.target, record.srv.port, self.conn_type, self.extra });
40 end
41 end
42 ready();
43 end, "_" .. self.service .. "._" .. self.conn_type .. "." .. self.domain, "SRV", "IN");
44 end
45
46 local function new(domain, service, conn_type, extra)
47 return setmetatable({
48 domain = domain;
49 service = service;
50 conn_type = conn_type or "tcp";
51 extra = extra;
52 }, resolver_mt);
53 end
54
55 return {
56 new = new;
57 };