Comparison

net/adns.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parent 10974:3b9d533da8fe
child 11217:65ce1178d655
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local server = require "net.server"; 9 local server = require "net.server";
10 local new_resolver = require "net.dns".resolver; 10 local new_resolver = require "net.dns".resolver;
11 local promise = require "util.promise";
11 12
12 local log = require "util.logger".init("adns"); 13 local log = require "util.logger".init("adns");
13 14
14 local coroutine, tostring, pcall = coroutine, tostring, pcall; 15 log("debug", "Using legacy DNS API (missing lua-unbound?)"); -- TODO write docs about luaunbound
16 -- TODO Raise log level once packages are available
17
18 local coroutine, pcall = coroutine, pcall;
15 local setmetatable = setmetatable; 19 local setmetatable = setmetatable;
16 20
17 local function dummy_send(sock, data, i, j) return (j-i)+1; end 21 local function dummy_send(sock, data, i, j) return (j-i)+1; end -- luacheck: ignore 212
18 22
19 local _ENV = nil; 23 local _ENV = nil;
20 -- luacheck: std none 24 -- luacheck: std none
21 25
22 local async_resolver_methods = {}; 26 local async_resolver_methods = {};
27 31
28 local function new_async_socket(sock, resolver) 32 local function new_async_socket(sock, resolver)
29 local peername = "<unknown>"; 33 local peername = "<unknown>";
30 local listener = {}; 34 local listener = {};
31 local handler = {}; 35 local handler = {};
32 local err; 36 function listener.onincoming(conn, data) -- luacheck: ignore 212/conn
33 function listener.onincoming(conn, data)
34 if data then 37 if data then
35 resolver:feed(handler, data); 38 resolver:feed(handler, data);
36 end 39 end
37 end 40 end
38 function listener.ondisconnect(conn, err) 41 function listener.ondisconnect(conn, err)
44 end 47 end
45 48
46 resolver:servfail(conn); -- Let the magic commence 49 resolver:servfail(conn); -- Let the magic commence
47 end 50 end
48 end 51 end
49 handler, err = server.wrapclient(sock, "dns", 53, listener); 52 do
50 if not handler then 53 local err;
51 return nil, err; 54 handler, err = server.wrapclient(sock, "dns", 53, listener);
55 if not handler then
56 return nil, err;
57 end
52 end 58 end
53 59
54 handler.settimeout = function () end 60 handler.settimeout = function () end
55 handler.setsockname = function (_, ...) return sock:setsockname(...); end 61 handler.setsockname = function (_, ...) return sock:setsockname(...); end
56 handler.setpeername = function (_, ...) peername = (...); local ret, err = sock:setpeername(...); _:set_send(dummy_send); return ret, err; end 62 handler.setpeername = function (_, ...) peername = (...); local ret, err = sock:setpeername(...); _:set_send(dummy_send); return ret, err; end
69 if peek then 75 if peek then
70 log("debug", "Records for %s already cached, using those...", qname); 76 log("debug", "Records for %s already cached, using those...", qname);
71 handler(peek); 77 handler(peek);
72 return; 78 return;
73 end 79 end
74 log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running())); 80 log("debug", "Records for %s not in cache, sending query (%s)...", qname, coroutine.running());
75 local ok, err = resolver:query(qname, qtype, qclass); 81 local ok, err = resolver:query(qname, qtype, qclass);
76 if ok then 82 if ok then
77 coroutine.yield(setmetatable({ resolver, qclass or "IN", qtype or "A", qname, coroutine.running()}, query_mt)); -- Wait for reply 83 coroutine.yield(setmetatable({ resolver, qclass or "IN", qtype or "A", qname, coroutine.running()}, query_mt)); -- Wait for reply
78 log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running())); 84 log("debug", "Reply for %s (%s)", qname, coroutine.running());
79 end 85 end
80 if ok then 86 if ok then
81 ok, err = pcall(handler, resolver:peek(qname, qtype, qclass)); 87 ok, err = pcall(handler, resolver:peek(qname, qtype, qclass));
82 else 88 else
83 log("error", "Error sending DNS query: %s", err); 89 log("error", "Error sending DNS query: %s", err);
84 ok, err = pcall(handler, nil, err); 90 ok, err = pcall(handler, nil, err);
85 end 91 end
86 if not ok then 92 if not ok then
87 log("error", "Error in DNS response handler: %s", tostring(err)); 93 log("error", "Error in DNS response handler: %s", err);
88 end 94 end
89 end)(resolver:peek(qname, qtype, qclass)); 95 end)(resolver:peek(qname, qtype, qclass));
90 end 96 end
91 97
92 function query_methods:cancel(call_handler, reason) 98 function async_resolver_methods:lookup_promise(qname, qtype, qclass)
93 log("warn", "Cancelling DNS lookup for %s", tostring(self[4])); 99 return promise.new(function (resolve, reject)
100 local function handler(answer)
101 if not answer then
102 return reject();
103 end
104 resolve(answer);
105 end
106 self:lookup(handler, qname, qtype, qclass);
107 end);
108 end
109
110 function query_methods:cancel(call_handler, reason) -- luacheck: ignore 212/reason
111 log("warn", "Cancelling DNS lookup for %s", self[4]);
94 self[1].cancel(self[2], self[3], self[4], self[5], call_handler); 112 self[1].cancel(self[2], self[3], self[4], self[5], call_handler);
95 end 113 end
96 114
97 local function new_async_resolver() 115 local function new_async_resolver()
98 local resolver = new_resolver(); 116 local resolver = new_resolver();