aboutsummaryrefslogtreecommitdiffstats
path: root/net/adns.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2017-09-25 23:06:00 +0100
committerMatthew Wild <mwild1@gmail.com>2017-09-25 23:06:00 +0100
commita2e27dd4fb9e2f8a6c1d46958b878f78d872eba8 (patch)
tree1241a99b32f149863f3ee6bd0114fa53018650e1 /net/adns.lua
parent30dcc732985f627643da951e0b518d61ec442559 (diff)
downloadprosody-a2e27dd4fb9e2f8a6c1d46958b878f78d872eba8.tar.gz
prosody-a2e27dd4fb9e2f8a6c1d46958b878f78d872eba8.zip
net.adns: Restructure to allow creating separate resolver objects, like net.dns
Diffstat (limited to 'net/adns.lua')
-rw-r--r--net/adns.lua79
1 files changed, 46 insertions, 33 deletions
diff --git a/net/adns.lua b/net/adns.lua
index 0b7247ed..f1196a6c 100644
--- a/net/adns.lua
+++ b/net/adns.lua
@@ -7,7 +7,7 @@
--
local server = require "net.server";
-local dns = require "net.dns";
+local new_resolver = require "net.dns".resolver;
local log = require "util.logger".init("adns");
@@ -17,35 +17,11 @@ local function dummy_send(sock, data, i, j) return (j-i)+1; end
local _ENV = nil;
-local function lookup(handler, qname, qtype, qclass)
- return coroutine.wrap(function (peek)
- if peek then
- log("debug", "Records for %s already cached, using those...", qname);
- handler(peek);
- return;
- end
- log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running()));
- local ok, err = dns.query(qname, qtype, qclass);
- if ok then
- coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply
- log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running()));
- end
- if ok then
- ok, err = pcall(handler, dns.peek(qname, qtype, qclass));
- else
- log("error", "Error sending DNS query: %s", err);
- ok, err = pcall(handler, nil, err);
- end
- if not ok then
- log("error", "Error in DNS response handler: %s", tostring(err));
- end
- end)(dns.peek(qname, qtype, qclass));
-end
+local async_resolver_methods = {};
+local async_resolver_mt = { __index = async_resolver_methods };
-local function cancel(handle, call_handler, reason)
- log("warn", "Cancelling DNS lookup for %s", tostring(handle[3]));
- dns.cancel(handle[1], handle[2], handle[3], handle[4], call_handler);
-end
+local query_methods = {};
+local query_mt = { __index = query_methods };
local function new_async_socket(sock, resolver)
local peername = "<unknown>";
@@ -54,7 +30,7 @@ local function new_async_socket(sock, resolver)
local err;
function listener.onincoming(conn, data)
if data then
- dns.feed(handler, data);
+ resolver:feed(handler, data);
end
end
function listener.ondisconnect(conn, err)
@@ -85,10 +61,47 @@ local function new_async_socket(sock, resolver)
return handler;
end
-dns.socket_wrapper_set(new_async_socket);
+function async_resolver_methods:lookup(handler, qname, qtype, qclass)
+ local resolver = self._resolver;
+ return coroutine.wrap(function (peek)
+ if peek then
+ log("debug", "Records for %s already cached, using those...", qname);
+ handler(peek);
+ return;
+ end
+ log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running()));
+ local ok, err = resolver:query(qname, qtype, qclass);
+ if ok then
+ coroutine.yield(setmetatable({ resolver, qclass or "IN", qtype or "A", qname, coroutine.running()}, query_mt)); -- Wait for reply
+ log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running()));
+ end
+ if ok then
+ ok, err = pcall(handler, resolver:peek(qname, qtype, qclass));
+ else
+ log("error", "Error sending DNS query: %s", err);
+ ok, err = pcall(handler, nil, err);
+ end
+ if not ok then
+ log("error", "Error in DNS response handler: %s", tostring(err));
+ end
+ end)(resolver:peek(qname, qtype, qclass));
+end
+
+function query_methods:cancel(call_handler, reason)
+ log("warn", "Cancelling DNS lookup for %s", tostring(self[4]));
+ self[1].cancel(self[2], self[3], self[4], self[5], call_handler);
+end
+
+local function new_async_resolver()
+ local resolver = new_resolver();
+ resolver:socket_wrapper_set(new_async_socket);
+ return setmetatable({ _resolver = resolver}, async_resolver_mt);
+end
return {
- lookup = lookup;
- cancel = cancel;
+ lookup = function (...)
+ return new_async_resolver():lookup(...);
+ end;
+ resolver = new_async_resolver;
new_async_socket = new_async_socket;
};