Software /
code /
prosody
Annotate
net/unbound.lua @ 13227:58083329903d
mod_muc_mam: Use period option method
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 18 Jul 2023 12:33:51 +0200 |
parent | 12974:ba409c67353b |
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; |
10962
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; |
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 |
12509
a92e1de62c9e
net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents:
12110
diff
changeset
|
28 local builtin_defaults = { hoststxt = false } |
a92e1de62c9e
net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents:
12110
diff
changeset
|
29 |
10962
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 local function add_defaults(conf) |
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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 end |
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
|
37 for option, default in pairs(libunbound.config) 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
|
38 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
|
39 conf[option] = default; |
10962
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 return conf; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 local unbound_config; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 if prosody then |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12557
diff
changeset
|
47 local config = require"prosody.core.configmanager"; |
10962
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 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
|
49 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
|
50 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
|
51 end); |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 -- 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
|
54 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 local function connect_server(unbound, server) |
11248
a1aecd8cf7ee
net.unbound: Log net.server interactions
Kim Alvefur <zash@zash.se>
parents:
10968
diff
changeset
|
56 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
|
57 return server.watchfd(unbound, function () |
11248
a1aecd8cf7ee
net.unbound: Log net.server interactions
Kim Alvefur <zash@zash.se>
parents:
10968
diff
changeset
|
58 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
|
59 unbound:process() |
67aabf83230b
net.unbound: Strip support for legacy net.server APIs
Kim Alvefur <zash@zash.se>
parents:
10962
diff
changeset
|
60 end); |
10962
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 |
11252
ba335004ca60
net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents:
11250
diff
changeset
|
63 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
|
64 |
11252
ba335004ca60
net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents:
11250
diff
changeset
|
65 local function initialize() |
ba335004ca60
net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents:
11250
diff
changeset
|
66 unbound = libunbound.new(unbound_config); |
ba335004ca60
net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents:
11250
diff
changeset
|
67 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
|
68 end |
11253
919e7b962f0b
net.unbound: Delay loading until server has started or first query
Kim Alvefur <zash@zash.se>
parents:
11252
diff
changeset
|
69 if prosody then |
919e7b962f0b
net.unbound: Delay loading until server has started or first query
Kim Alvefur <zash@zash.se>
parents:
11252
diff
changeset
|
70 prosody.events.add_handler("server-started", initialize); |
919e7b962f0b
net.unbound: Delay loading until server has started or first query
Kim Alvefur <zash@zash.se>
parents:
11252
diff
changeset
|
71 end |
10962
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 answer_mt = { |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 __tostring = function(self) |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 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
|
76 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
|
77 if self.secure then |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 h = h .. ", Secure"; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 elseif self.bogus then |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 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
|
81 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 local t = { h }; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 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
|
84 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
|
85 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 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
|
87 self._string = _string; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 return _string; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 end; |
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 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 local waiting_queries = {}; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 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
|
102 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
|
103 local parser = parsers[qtype]; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 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
|
105 if a.bogus then |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 -- Discard bogus data |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 a[i] = nil; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 else |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 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
|
110 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 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
|
113 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 local startedat = gettime(); |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 local ret; |
11250
d1351683dfe5
net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents:
11249
diff
changeset
|
122 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
|
123 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
|
124 local gotdataat = gettime(); |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 waiting_queries[ret] = nil; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 if a then |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 prep_answer(a); |
11250
d1351683dfe5
net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents:
11249
diff
changeset
|
128 log_query("debug", "Results for %s %s %s: %s (%s, %f sec)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status, |
10962
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 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
|
130 else |
11250
d1351683dfe5
net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents:
11249
diff
changeset
|
131 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
|
132 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 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
|
134 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
|
135 end |
11250
d1351683dfe5
net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents:
11249
diff
changeset
|
136 log_query("debug", "Resolve %s %s %s", qname, qclass, qtype); |
10962
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 local err; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 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
|
139 if ret then |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 waiting_queries[ret] = callback; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 else |
12510
cd3b5912c9a3
net.unbound: Adjust log level of error to error to error
Kim Alvefur <zash@zash.se>
parents:
12509
diff
changeset
|
142 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
|
143 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 return ret, err; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 return prep_answer(a); |
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 cancel(id) |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 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
|
159 unbound:cancel(id); |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 if cb then |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 cb(nil, "canceled"); |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 waiting_queries[id] = nil; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 return true; |
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() |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 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
|
170 if server_conn then server_conn:close(); end |
11252
ba335004ca60
net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents:
11250
diff
changeset
|
171 initialize(); |
10962
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 return true; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 local function not_implemented() |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 error "not implemented"; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 end |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 -- Public API |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 local _M = { |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 lookup = lookup; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 cancel = cancel; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 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
|
183 dns = { |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 lookup = lookup_sync; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 cancel = cancel; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 cache = noop; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 socket_wrapper_set = noop; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 settimeout = noop; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 query = noop; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 purge = purge; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 random = noop; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 peek = noop; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 types = types; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 classes = classes; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 }; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 }; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 |
10968
23ae55cbbeaf
net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents:
10967
diff
changeset
|
199 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
|
200 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
|
201 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
|
202 if err then |
23ae55cbbeaf
net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents:
10967
diff
changeset
|
203 return reject(err); |
23ae55cbbeaf
net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents:
10967
diff
changeset
|
204 else |
23ae55cbbeaf
net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents:
10967
diff
changeset
|
205 return resolve(answer); |
10962
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 end |
10968
23ae55cbbeaf
net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents:
10967
diff
changeset
|
207 end |
23ae55cbbeaf
net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents:
10967
diff
changeset
|
208 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
|
209 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
|
210 end); |
10962
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 end |
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 local wrapper = { |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 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
|
215 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
|
216 end; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 lookup_promise = lookup_promise; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 _resolver = { |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 settimeout = function () end; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 closeall = function () end; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 }; |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 } |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 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
|
225 |
92f30e8ecdfc
net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 return _M; |