annotate net/unbound.lua @ 14229:ce31fdde0ad1 default tip

net.unbound: Simplify conditional
author Kim Alvefur <zash@zash.se>
date Sat, 13 Jun 2026 11:35:18 +0200
parents 36aac3343563
children
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
12974
ba409c67353b net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
16 local logger = require "prosody.util.logger";
11250
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11249
diff changeset
17 local log = logger.init("unbound");
12974
ba409c67353b net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
18 local net_server = require "prosody.net.server";
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local libunbound = require"lunbound";
12974
ba409c67353b net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
20 local promise = require"prosody.util.promise";
ba409c67353b net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
21 local new_id = require "prosody.util.id".short;
14208
9c062f243d3a net.unbound: Let in-flight queries complete after re-initialization
Kim Alvefur <zash@zash.se>
parents: 14207
diff changeset
22 local timer = require "prosody.util.timer";
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
12974
ba409c67353b net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12557
diff changeset
24 local dns_utils = require"prosody.util.dns";
10962
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
14215
2719dbb358c8 net.unbound: Briefly explain default override settings
Kim Alvefur <zash@zash.se>
parents: 14210
diff changeset
28 local builtin_defaults = {
2719dbb358c8 net.unbound: Briefly explain default override settings
Kim Alvefur <zash@zash.se>
parents: 14210
diff changeset
29 -- Continue net.dns tradition of not reading /etc/hosts which does not fit well with SRV records anyway
2719dbb358c8 net.unbound: Briefly explain default override settings
Kim Alvefur <zash@zash.se>
parents: 14210
diff changeset
30 hoststxt = false;
2719dbb358c8 net.unbound: Briefly explain default override settings
Kim Alvefur <zash@zash.se>
parents: 14210
diff changeset
31 -- net.dns did read /etc/resolv.conf and we want libunbound to instead of operating in recursive mode
2719dbb358c8 net.unbound: Briefly explain default override settings
Kim Alvefur <zash@zash.se>
parents: 14210
diff changeset
32 resolvconf = true;
2719dbb358c8 net.unbound: Briefly explain default override settings
Kim Alvefur <zash@zash.se>
parents: 14210
diff changeset
33 }
12509
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12110
diff changeset
34
14216
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
35 -- Path to file containing current DNSSEC trust anchors, needed to enable DNSSEC.
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
36 -- Packagers: Adjust this path if it is different on your distribution
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
37 local trustfile = "/usr/share/dns/root.ds";
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
38
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
39 local function add_defaults(conf, use_dnssec)
12557
ee5b061588ea net.unbound: Merge luaunbound and prosody defaults in absence of user config (fixes #1763) (thanks rgd)
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
40 conf = conf or {};
ee5b061588ea net.unbound: Merge luaunbound and prosody defaults in absence of user config (fixes #1763) (thanks rgd)
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
41 for option, default in pairs(builtin_defaults) do
ee5b061588ea net.unbound: Merge luaunbound and prosody defaults in absence of user config (fixes #1763) (thanks rgd)
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
42 if conf[option] == nil then
ee5b061588ea net.unbound: Merge luaunbound and prosody defaults in absence of user config (fixes #1763) (thanks rgd)
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
43 conf[option] = default;
12509
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12110
diff changeset
44 end
12557
ee5b061588ea net.unbound: Merge luaunbound and prosody defaults in absence of user config (fixes #1763) (thanks rgd)
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
45 end
14223
37612a03220d net.unbound: Inherit trust anchors from luaunbound defaults
Kim Alvefur <zash@zash.se>
parents: 14216
diff changeset
46 for option, default in pairs(libunbound.config) do
37612a03220d net.unbound: Inherit trust anchors from luaunbound defaults
Kim Alvefur <zash@zash.se>
parents: 14216
diff changeset
47 if conf[option] == nil then
37612a03220d net.unbound: Inherit trust anchors from luaunbound defaults
Kim Alvefur <zash@zash.se>
parents: 14216
diff changeset
48 conf[option] = default;
37612a03220d net.unbound: Inherit trust anchors from luaunbound defaults
Kim Alvefur <zash@zash.se>
parents: 14216
diff changeset
49 end
37612a03220d net.unbound: Inherit trust anchors from luaunbound defaults
Kim Alvefur <zash@zash.se>
parents: 14216
diff changeset
50 end
14224
36aac3343563 net.unbound: Respect use_dnssec=false in case of default TA in lua-event
Kim Alvefur <zash@zash.se>
parents: 14223
diff changeset
51 if use_dnssec == false then
36aac3343563 net.unbound: Respect use_dnssec=false in case of default TA in lua-event
Kim Alvefur <zash@zash.se>
parents: 14223
diff changeset
52 conf["trustfile"] = nil;
36aac3343563 net.unbound: Respect use_dnssec=false in case of default TA in lua-event
Kim Alvefur <zash@zash.se>
parents: 14223
diff changeset
53 conf["trusted"] = nil;
14229
ce31fdde0ad1 net.unbound: Simplify conditional
Kim Alvefur <zash@zash.se>
parents: 14224
diff changeset
54 elseif conf["trustfile"] == nil and conf["trusted"] == nil then
ce31fdde0ad1 net.unbound: Simplify conditional
Kim Alvefur <zash@zash.se>
parents: 14224
diff changeset
55 conf["trustfile"] = trustfile;
14216
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
56 end
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 return conf;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 local unbound_config;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local function connect_server(unbound, server)
11248
a1aecd8cf7ee net.unbound: Log net.server interactions
Kim Alvefur <zash@zash.se>
parents: 10968
diff changeset
63 log("debug", "Setting up net.server event handling for %s", unbound);
10967
67aabf83230b net.unbound: Strip support for legacy net.server APIs
Kim Alvefur <zash@zash.se>
parents: 10962
diff changeset
64 return server.watchfd(unbound, function ()
11248
a1aecd8cf7ee net.unbound: Log net.server interactions
Kim Alvefur <zash@zash.se>
parents: 10968
diff changeset
65 log("debug", "Processing queries for %s", unbound);
10967
67aabf83230b net.unbound: Strip support for legacy net.server APIs
Kim Alvefur <zash@zash.se>
parents: 10962
diff changeset
66 unbound:process()
67aabf83230b net.unbound: Strip support for legacy net.server APIs
Kim Alvefur <zash@zash.se>
parents: 10962
diff changeset
67 end);
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69
11252
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
70 local unbound, server_conn;
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71
11252
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
72 local function initialize()
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
73 unbound = libunbound.new(unbound_config);
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
74 server_conn = connect_server(unbound, net_server);
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
75 end
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 local answer_mt = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 __tostring = function(self)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 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
80 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
81 if self.secure then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 h = h .. ", Secure";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 elseif self.bogus then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 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
85 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 local t = { h };
13479
d1b7edf4e2de net.unbound: Show canonical name in textual format (e.g. in shell)
Kim Alvefur <zash@zash.se>
parents: 12974
diff changeset
87 local qname = self.canonname or self.qname;
d1b7edf4e2de net.unbound: Show canonical name in textual format (e.g. in shell)
Kim Alvefur <zash@zash.se>
parents: 12974
diff changeset
88 if self.canonname then
d1b7edf4e2de net.unbound: Show canonical name in textual format (e.g. in shell)
Kim Alvefur <zash@zash.se>
parents: 12974
diff changeset
89 table.insert(t, self.qname .. "\t" .. classes[self.qclass] .. "\tCNAME\t" .. self.canonname);
d1b7edf4e2de net.unbound: Show canonical name in textual format (e.g. in shell)
Kim Alvefur <zash@zash.se>
parents: 12974
diff changeset
90 end
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 for i = 1, #self do
13479
d1b7edf4e2de net.unbound: Show canonical name in textual format (e.g. in shell)
Kim Alvefur <zash@zash.se>
parents: 12974
diff changeset
92 table.insert(t, qname .. "\t" .. classes[self.qclass] .. "\t" .. types[self.qtype] .. "\t" .. tostring(self[i]));
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 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
95 self._string = _string;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 return _string;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 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
101 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
102 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
103 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
104 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
105 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
106
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 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
108 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
109 local parser = parsers[qtype];
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 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
111 if a.bogus then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 -- Discard bogus data
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 a[i] = nil;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 else
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 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
116 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 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
119 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120
13659
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
121 local function measure(_qclass, _qtype)
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
122 return measure;
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
123 end
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
124
13981
350e16e5f9aa net.unbound: Count number of DNS response records
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
125 local count = measure;
350e16e5f9aa net.unbound: Count number of DNS response records
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
126
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 local function lookup(callback, qname, qtype, qclass)
11253
919e7b962f0b net.unbound: Delay loading until server has started or first query
Kim Alvefur <zash@zash.se>
parents: 11252
diff changeset
128 if not unbound then initialize(); end
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 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
130 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
131 local ntype, nclass = types[qtype], classes[qclass];
13659
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
132
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
133 local m;
11250
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11249
diff changeset
134 local log_query = logger.init("unbound.query"..new_id());
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 local function callback_wrapper(a, err)
13659
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
136 m();
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 if a then
13981
350e16e5f9aa net.unbound: Count number of DNS response records
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
138 count(qclass, qtype, #a);
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 prep_answer(a);
13659
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
140 log_query("debug", "Results for %s %s %s: %s (%s)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status,
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
141 a.secure and "Secure" or a.bogus or "Insecure"); -- Insecure as in unsigned
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 else
11250
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11249
diff changeset
143 log_query("error", "Results for %s %s %s: %s", qname, qclass, qtype, tostring(err));
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 local ok, cerr = pcall(callback, a, err);
11250
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11249
diff changeset
146 if not ok then log_query("error", "Error in callback: %s", cerr); end
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 end
11250
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11249
diff changeset
148 log_query("debug", "Resolve %s %s %s", qname, qclass, qtype);
13659
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
149 m = measure(qclass, qtype);
14209
c4aa9b2b4787 net.unbound: Simplify by removing cancel()
Kim Alvefur <zash@zash.se>
parents: 14208
diff changeset
150 local ret, err = unbound:resolve_async(callback_wrapper, qname, ntype, nclass);
c4aa9b2b4787 net.unbound: Simplify by removing cancel()
Kim Alvefur <zash@zash.se>
parents: 14208
diff changeset
151 if not ret then
12510
cd3b5912c9a3 net.unbound: Adjust log level of error to error to error
Kim Alvefur <zash@zash.se>
parents: 12509
diff changeset
152 log_query("error", "Resolver error: %s", err);
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 return ret, err;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 local function lookup_sync(qname, qtype, qclass)
11302
6bb2986783d0 net.unbound: Fix to initialize under prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11253
diff changeset
158 if not unbound then initialize(); end
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 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
160 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
161 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
162 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
163 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
164 return prep_answer(a);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 -- 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
168 local function purge()
14208
9c062f243d3a net.unbound: Let in-flight queries complete after re-initialization
Kim Alvefur <zash@zash.se>
parents: 14207
diff changeset
169 if server_conn then
9c062f243d3a net.unbound: Let in-flight queries complete after re-initialization
Kim Alvefur <zash@zash.se>
parents: 14207
diff changeset
170 local old_unbound, old_server_conn = unbound, server_conn;
9c062f243d3a net.unbound: Let in-flight queries complete after re-initialization
Kim Alvefur <zash@zash.se>
parents: 14207
diff changeset
171 timer.add_task(30, function()
9c062f243d3a net.unbound: Let in-flight queries complete after re-initialization
Kim Alvefur <zash@zash.se>
parents: 14207
diff changeset
172 old_server_conn:close();
14209
c4aa9b2b4787 net.unbound: Simplify by removing cancel()
Kim Alvefur <zash@zash.se>
parents: 14208
diff changeset
173 if old_unbound.cancelall then
c4aa9b2b4787 net.unbound: Simplify by removing cancel()
Kim Alvefur <zash@zash.se>
parents: 14208
diff changeset
174 old_unbound:cancelall();
c4aa9b2b4787 net.unbound: Simplify by removing cancel()
Kim Alvefur <zash@zash.se>
parents: 14208
diff changeset
175 end
14208
9c062f243d3a net.unbound: Let in-flight queries complete after re-initialization
Kim Alvefur <zash@zash.se>
parents: 14207
diff changeset
176 end)
9c062f243d3a net.unbound: Let in-flight queries complete after re-initialization
Kim Alvefur <zash@zash.se>
parents: 14207
diff changeset
177 end
11252
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
178 initialize();
10962
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
14207
23f1c71b3d56 net.unbound: Reset and apply new config on reload
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
182 if prosody then
14216
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
183 -- TODO move this glue logic into util.startup?
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
184 local config = require "prosody.core.configmanager";
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
185 unbound_config = add_defaults(config.get("*", "unbound"), config.get("*", "use_dnssec"));
14207
23f1c71b3d56 net.unbound: Reset and apply new config on reload
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
186 prosody.events.add_handler("config-reloaded", function()
14216
2ec517d8d43e net.unbound: Enable DNSSEC by default with option for turning off
Kim Alvefur <zash@zash.se>
parents: 14215
diff changeset
187 unbound_config = add_defaults(config.get("*", "unbound"), config.get("*", "use_dnssec"));
14207
23f1c71b3d56 net.unbound: Reset and apply new config on reload
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
188 purge();
23f1c71b3d56 net.unbound: Reset and apply new config on reload
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
189 end);
23f1c71b3d56 net.unbound: Reset and apply new config on reload
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
190 prosody.events.add_handler("server-started", initialize);
23f1c71b3d56 net.unbound: Reset and apply new config on reload
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
191 end
23f1c71b3d56 net.unbound: Reset and apply new config on reload
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
192
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 local function not_implemented()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 error "not implemented";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 -- Public API
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 local _M = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 lookup = lookup;
14209
c4aa9b2b4787 net.unbound: Simplify by removing cancel()
Kim Alvefur <zash@zash.se>
parents: 14208
diff changeset
199 cancel = not_implemented;
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 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
201 dns = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 lookup = lookup_sync;
14209
c4aa9b2b4787 net.unbound: Simplify by removing cancel()
Kim Alvefur <zash@zash.se>
parents: 14208
diff changeset
203 cancel = not_implemented;
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 cache = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 socket_wrapper_set = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 settimeout = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 query = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 purge = purge;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 random = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 peek = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 types = types;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 classes = classes;
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 };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216
10968
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
217 local function lookup_promise(_, qname, qtype, qclass)
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
218 return promise.new(function (resolve, reject)
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
219 local function callback(answer, err)
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
220 if err then
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
221 return reject(err);
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
222 else
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
223 return resolve(answer);
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 end
10968
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
225 end
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
226 local ret, err = lookup(callback, qname, qtype, qclass)
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
227 if not ret then reject(err); end
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10967
diff changeset
228 end);
10962
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
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 local wrapper = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 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
233 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
234 end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 lookup_promise = lookup_promise;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 _resolver = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 settimeout = function () end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 closeall = function () end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 };
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
13981
350e16e5f9aa net.unbound: Count number of DNS response records
Kim Alvefur <zash@zash.se>
parents: 13659
diff changeset
242 _M.instrument = function(measure_, count_) measure, count = measure_, count_; end;
13659
5abdcad8c2e0 net.adns: Collect DNS lookup timing metrics
Kim Alvefur <zash@zash.se>
parents: 13479
diff changeset
243
10962
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 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
245
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 return _M;