Annotate

net/unbound.lua @ 10962:92f30e8ecdfc

net.unbound: Async DNS resolver library based on libunbound via luaunbound
author Kim Alvefur <zash@zash.se>
date Sat, 09 Mar 2019 21:19:24 +0100
child 10967:67aabf83230b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- libunbound based net.adns replacement for Prosody IM
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2013-2015 Kim Alvefur
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This file is MIT licensed.
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 --
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- luacheck: ignore prosody
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local setmetatable = setmetatable;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local tostring = tostring;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local t_concat = table.concat;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local s_format = string.format;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local s_lower = string.lower;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local s_upper = string.upper;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local noop = function() end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local zero = function() return 0 end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local truop = function() return true; end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local log = require "util.logger".init("unbound");
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local net_server = require "net.server";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local libunbound = require"lunbound";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local have_promise, promise = pcall(require, "util.promise");
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local gettime = require"socket".gettime;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local dns_utils = require"util.dns";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local classes, types, errors = dns_utils.classes, dns_utils.types, dns_utils.errors;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local parsers = dns_utils.parsers;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local function add_defaults(conf)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 if conf then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 for option, default in pairs(libunbound.config) do
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 if conf[option] == nil then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 conf[option] = default;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 return conf;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 local unbound_config;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 if prosody then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local config = require"core.configmanager";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 unbound_config = add_defaults(config.get("*", "unbound"));
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 prosody.events.add_handler("config-reloaded", function()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 unbound_config = add_defaults(config.get("*", "unbound"));
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 -- Note: libunbound will default to using root hints if resolvconf is unset
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local function connect_server(unbound, server)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 if server.watchfd then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 return server.watchfd(unbound, function ()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 unbound:process()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 elseif server.event and server.addevent then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 local EV_READ = server.event.EV_READ;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 local function event_callback()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 unbound:process();
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 return EV_READ;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 return server.addevent(unbound:getfd(), EV_READ, event_callback)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 elseif server.wrapclient then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local conn = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 getfd = function()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 return unbound:getfd();
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 send = zero,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 receive = noop,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 settimeout = noop,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 close = truop,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 }
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 local function process()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 unbound:process();
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 local listener = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 onincoming = process,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 onconnect = noop,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 ondisconnect = noop,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 onreadtimeout = truop,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 return server.wrapclient(conn, "dns", 0, listener, "*a" );
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 local unbound = libunbound.new(unbound_config);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 local server_conn = connect_server(unbound, net_server);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 local answer_mt = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 __tostring = function(self)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 if self._string then return self._string end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 local h = s_format("Status: %s", errors[self.status]);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 if self.secure then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 h = h .. ", Secure";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 elseif self.bogus then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 h = h .. s_format(", Bogus: %s", self.bogus);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 local t = { h };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 for i = 1, #self do
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 t[i+1]=self.qname.."\t"..classes[self.qclass].."\t"..types[self.qtype].."\t"..tostring(self[i]);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 local _string = t_concat(t, "\n");
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 self._string = _string;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 return _string;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 local waiting_queries = {};
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 local function prep_answer(a)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 if not a then return end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 local status = errors[a.rcode];
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 local qclass = classes[a.qclass];
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 local qtype = types[a.qtype];
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 a.status, a.class, a.type = status, qclass, qtype;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 local t = s_lower(qtype);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 local rr_mt = { __index = a, __tostring = function(self) return tostring(self[t]) end };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 local parser = parsers[qtype];
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 for i = 1, #a do
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 if a.bogus then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 -- Discard bogus data
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 a[i] = nil;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 else
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 a[i] = setmetatable({[t] = parser(a[i])}, rr_mt);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 return setmetatable(a, answer_mt);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 local function lookup(callback, qname, qtype, qclass)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 qtype = qtype and s_upper(qtype) or "A";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 qclass = qclass and s_upper(qclass) or "IN";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 local ntype, nclass = types[qtype], classes[qclass];
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 local startedat = gettime();
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 local ret;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 local function callback_wrapper(a, err)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 local gotdataat = gettime();
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 waiting_queries[ret] = nil;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 if a then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 prep_answer(a);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 log("debug", "Results for %s %s %s: %s (%s, %f sec)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status,
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 a.secure and "Secure" or a.bogus or "Insecure", gotdataat - startedat); -- Insecure as in unsigned
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 else
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 log("error", "Results for %s %s %s: %s", qname, qclass, qtype, tostring(err));
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 local ok, cerr = pcall(callback, a, err);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 if not ok then log("error", "Error in callback: %s", cerr); end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 log("debug", "Resolve %s %s %s", qname, qclass, qtype);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 local err;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 ret, err = unbound:resolve_async(callback_wrapper, qname, ntype, nclass);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 if ret then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 waiting_queries[ret] = callback;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 else
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 log("warn", err);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 return ret, err;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 local function lookup_sync(qname, qtype, qclass)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 qtype = qtype and s_upper(qtype) or "A";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 qclass = qclass and s_upper(qclass) or "IN";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 local ntype, nclass = types[qtype], classes[qclass];
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 local a, err = unbound:resolve(qname, ntype, nclass);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 if not a then return a, err; end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 return prep_answer(a);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 local function cancel(id)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 local cb = waiting_queries[id];
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 unbound:cancel(id);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 if cb then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 cb(nil, "canceled");
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 waiting_queries[id] = nil;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 return true;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 -- Reinitiate libunbound context, drops cache
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 local function purge()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 for id in pairs(waiting_queries) do cancel(id); end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 if server_conn then server_conn:close(); end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 unbound = libunbound.new(unbound_config);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 server_conn = connect_server(unbound, net_server);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 return true;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 local function not_implemented()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 error "not implemented";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 -- Public API
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 local _M = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 lookup = lookup;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 cancel = cancel;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 new_async_socket = not_implemented;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 dns = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 lookup = lookup_sync;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 cancel = cancel;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 cache = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 socket_wrapper_set = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 settimeout = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 query = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 purge = purge;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 random = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 peek = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 types = types;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 classes = classes;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 local lookup_promise;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 if have_promise then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 function lookup_promise(_, qname, qtype, qclass)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 return promise.new(function (resolve, reject)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 local function callback(answer, err)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 if err then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 return reject(err);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 else
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 return resolve(answer);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 local ret, err = lookup(callback, qname, qtype, qclass)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 if not ret then reject(err); end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 end);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 local wrapper = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 lookup = function (_, callback, qname, qtype, qclass)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 return lookup(callback, qname, qtype, qclass)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 lookup_promise = lookup_promise;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 _resolver = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 settimeout = function () end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 closeall = function () end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 }
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 function _M.resolver() return wrapper; end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 return _M;