net/unbound.lua
author Kim Alvefur <zash@zash.se>
Fri, 27 May 2022 14:45:35 +0200
branch0.12
changeset 12530 252ed01896dd
parent 12514 cd3b5912c9a3
child 12561 ee5b061588ea
permissions -rw-r--r--
mod_smacks: Bounce unhandled stanzas from local origin (fix #1759) Sending stanzas with a remote session as origin when the stanzas have a local JID in the from attribute trips validation in core.stanza_router, leading to warnings: > Received a stanza claiming to be from remote.example, over a stream authed for localhost.example Using module:send() uses the local host as origin, which is fine here.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10966
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
11254
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11253
diff changeset
    16
local logger = require "util.logger";
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11253
diff changeset
    17
local log = logger.init("unbound");
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
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
    19
local libunbound = require"lunbound";
10972
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
    20
local promise = require"util.promise";
12114
b5b799a2a10c util.id: Adjust entropy levels, with rationales
Kim Alvefur <zash@zash.se>
parents: 11306
diff changeset
    21
local new_id = require "util.id".short;
10966
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
12513
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12114
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: 12114
diff changeset
    29
10966
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)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
	if conf then
12513
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12114
diff changeset
    32
		for option, default in pairs(builtin_defaults) do
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12114
diff changeset
    33
			if conf[option] == nil then
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12114
diff changeset
    34
				conf[option] = default;
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12114
diff changeset
    35
			end
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12114
diff changeset
    36
		end
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
		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
    38
			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
    39
				conf[option] = default;
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
12513
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12114
diff changeset
    42
	else
a92e1de62c9e net.unbound: Disable use of hosts file by default (fixes #1737)
Kim Alvefur <zash@zash.se>
parents: 12114
diff changeset
    43
		return builtin_defaults;
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
	end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    45
	return conf;
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
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
local unbound_config;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
if prosody then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
	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
    51
	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
    52
	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
    53
		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
    54
	end);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    55
end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    56
-- 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
    57
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    58
local function connect_server(unbound, server)
11252
a1aecd8cf7ee net.unbound: Log net.server interactions
Kim Alvefur <zash@zash.se>
parents: 10972
diff changeset
    59
	log("debug", "Setting up net.server event handling for %s", unbound);
10971
67aabf83230b net.unbound: Strip support for legacy net.server APIs
Kim Alvefur <zash@zash.se>
parents: 10966
diff changeset
    60
	return server.watchfd(unbound, function ()
11252
a1aecd8cf7ee net.unbound: Log net.server interactions
Kim Alvefur <zash@zash.se>
parents: 10972
diff changeset
    61
		log("debug", "Processing queries for %s", unbound);
10971
67aabf83230b net.unbound: Strip support for legacy net.server APIs
Kim Alvefur <zash@zash.se>
parents: 10966
diff changeset
    62
		unbound:process()
67aabf83230b net.unbound: Strip support for legacy net.server APIs
Kim Alvefur <zash@zash.se>
parents: 10966
diff changeset
    63
	end);
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    64
end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    65
11256
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11254
diff changeset
    66
local unbound, server_conn;
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    67
11256
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11254
diff changeset
    68
local function initialize()
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11254
diff changeset
    69
	unbound = libunbound.new(unbound_config);
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11254
diff changeset
    70
	server_conn = connect_server(unbound, net_server);
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11254
diff changeset
    71
end
11257
919e7b962f0b net.unbound: Delay loading until server has started or first query
Kim Alvefur <zash@zash.se>
parents: 11256
diff changeset
    72
if prosody then
919e7b962f0b net.unbound: Delay loading until server has started or first query
Kim Alvefur <zash@zash.se>
parents: 11256
diff changeset
    73
	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: 11256
diff changeset
    74
end
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    75
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    76
local answer_mt = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    77
	__tostring = function(self)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    78
		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
    79
		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
    80
		if self.secure then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
			h = h .. ", Secure";
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    82
		elseif self.bogus then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    83
			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
    84
		end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    85
		local t = { h };
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    86
		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
    87
			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
    88
		end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    89
		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
    90
		self._string = _string;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    91
		return _string;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    92
	end;
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
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    95
local waiting_queries = {};
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    96
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    97
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
    98
	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
    99
	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
   100
	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
   101
	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
   102
	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
   103
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   104
	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
   105
	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
   106
	local parser = parsers[qtype];
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   107
	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
   108
		if a.bogus then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   109
			-- Discard bogus data
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   110
			a[i] = nil;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   111
		else
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   112
			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
   113
		end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   114
	end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   115
	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
   116
end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   117
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   118
local function lookup(callback, qname, qtype, qclass)
11257
919e7b962f0b net.unbound: Delay loading until server has started or first query
Kim Alvefur <zash@zash.se>
parents: 11256
diff changeset
   119
	if not unbound then initialize(); end
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   120
	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
   121
	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
   122
	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
   123
	local startedat = gettime();
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   124
	local ret;
11254
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11253
diff changeset
   125
	local log_query = logger.init("unbound.query"..new_id());
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   126
	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
   127
		local gotdataat = gettime();
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   128
		waiting_queries[ret] = nil;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   129
		if a then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   130
			prep_answer(a);
11254
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11253
diff changeset
   131
			log_query("debug", "Results for %s %s %s: %s (%s, %f sec)", qname, qclass, qtype, a.rcode == 0 and (#a .. " items") or a.status,
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   132
				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
   133
		else
11254
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11253
diff changeset
   134
			log_query("error", "Results for %s %s %s: %s", qname, qclass, qtype, tostring(err));
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   135
		end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   136
		local ok, cerr = pcall(callback, a, err);
11254
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11253
diff changeset
   137
		if not ok then log_query("error", "Error in callback: %s", cerr); end
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   138
	end
11254
d1351683dfe5 net.unbound: Allow tracing individual queries with a logger per query
Kim Alvefur <zash@zash.se>
parents: 11253
diff changeset
   139
	log_query("debug", "Resolve %s %s %s", qname, qclass, qtype);
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   140
	local err;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   141
	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
   142
	if ret then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   143
		waiting_queries[ret] = callback;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   144
	else
12514
cd3b5912c9a3 net.unbound: Adjust log level of error to error to error
Kim Alvefur <zash@zash.se>
parents: 12513
diff changeset
   145
		log_query("error", "Resolver error: %s", err);
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   146
	end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   147
	return ret, 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
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   150
local function lookup_sync(qname, qtype, qclass)
11306
6bb2986783d0 net.unbound: Fix to initialize under prosodyctl
Kim Alvefur <zash@zash.se>
parents: 11257
diff changeset
   151
	if not unbound then initialize(); end
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   152
	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
   153
	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
   154
	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
   155
	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
   156
	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
   157
	return prep_answer(a);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   158
end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   159
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   160
local function cancel(id)
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   161
	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
   162
	unbound:cancel(id);
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   163
	if cb then
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   164
		cb(nil, "canceled");
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   165
		waiting_queries[id] = nil;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   166
	end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   167
	return true;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   168
end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   169
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   170
-- 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
   171
local function purge()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   172
	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
   173
	if server_conn then server_conn:close(); end
11256
ba335004ca60 net.unbound: Move libunbound initialization into a function
Kim Alvefur <zash@zash.se>
parents: 11254
diff changeset
   174
	initialize();
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   175
	return true;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   176
end
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   177
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   178
local function not_implemented()
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   179
	error "not implemented";
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
-- Public API
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   182
local _M = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   183
	lookup = lookup;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   184
	cancel = cancel;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   185
	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
   186
	dns = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   187
		lookup = lookup_sync;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   188
		cancel = cancel;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   189
		cache = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   190
		socket_wrapper_set = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   191
		settimeout = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   192
		query = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   193
		purge = purge;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   194
		random = noop;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   195
		peek = noop;
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
		types = types;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   198
		classes = classes;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   199
	};
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   200
};
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   201
10972
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   202
local function lookup_promise(_, qname, qtype, qclass)
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   203
	return promise.new(function (resolve, reject)
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   204
		local function callback(answer, err)
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   205
			if err then
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   206
				return reject(err);
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   207
			else
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   208
				return resolve(answer);
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   209
			end
10972
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   210
		end
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   211
		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: 10971
diff changeset
   212
		if not ret then reject(err); end
23ae55cbbeaf net.unbound: Remove compat for missing promises (pre-0.11)
Kim Alvefur <zash@zash.se>
parents: 10971
diff changeset
   213
	end);
10966
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   214
end
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
local wrapper = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   217
	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
   218
		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
   219
	end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   220
	lookup_promise = lookup_promise;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   221
	_resolver = {
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   222
		settimeout = function () end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   223
		closeall = function () end;
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   224
	};
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
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   227
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
   228
92f30e8ecdfc net.unbound: Async DNS resolver library based on libunbound via luaunbound
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   229
return _M;