util.x509: Nameprep commonName once
[prosody.git] / net / adns.lua
blobd7266958f574dec562eda573342aa3d58a429083
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
9 local server = require "net.server";
10 local new_resolver = require "net.dns".resolver;
12 local log = require "util.logger".init("adns");
14 local coroutine, pcall = coroutine, pcall;
15 local setmetatable = setmetatable;
17 local function dummy_send(sock, data, i, j) return (j-i)+1; end -- luacheck: ignore 212
19 local _ENV = nil;
20 -- luacheck: std none
22 local async_resolver_methods = {};
23 local async_resolver_mt = { __index = async_resolver_methods };
25 local query_methods = {};
26 local query_mt = { __index = query_methods };
28 local function new_async_socket(sock, resolver)
29 local peername = "<unknown>";
30 local listener = {};
31 local handler = {};
32 function listener.onincoming(conn, data) -- luacheck: ignore 212/conn
33 if data then
34 resolver:feed(handler, data);
35 end
36 end
37 function listener.ondisconnect(conn, err)
38 if err then
39 log("warn", "DNS socket for %s disconnected: %s", peername, err);
40 local servers = resolver.server;
41 if resolver.socketset[conn] == resolver.best_server and resolver.best_server == #servers then
42 log("error", "Exhausted all %d configured DNS servers, next lookup will try %s again", #servers, servers[1]);
43 end
45 resolver:servfail(conn); -- Let the magic commence
46 end
47 end
49 local err;
50 handler, err = server.wrapclient(sock, "dns", 53, listener);
51 if not handler then
52 return nil, err;
53 end
54 end
56 handler.settimeout = function () end
57 handler.setsockname = function (_, ...) return sock:setsockname(...); end
58 handler.setpeername = function (_, ...) peername = (...); local ret, err = sock:setpeername(...); _:set_send(dummy_send); return ret, err; end
59 handler.connect = function (_, ...) return sock:connect(...) end
60 --handler.send = function (_, data) _:write(data); return _.sendbuffer and _.sendbuffer(); end
61 handler.send = function (_, data)
62 log("debug", "Sending DNS query to %s", peername);
63 return sock:send(data);
64 end
65 return handler;
66 end
68 function async_resolver_methods:lookup(handler, qname, qtype, qclass)
69 local resolver = self._resolver;
70 return coroutine.wrap(function (peek)
71 if peek then
72 log("debug", "Records for %s already cached, using those...", qname);
73 handler(peek);
74 return;
75 end
76 log("debug", "Records for %s not in cache, sending query (%s)...", qname, coroutine.running());
77 local ok, err = resolver:query(qname, qtype, qclass);
78 if ok then
79 coroutine.yield(setmetatable({ resolver, qclass or "IN", qtype or "A", qname, coroutine.running()}, query_mt)); -- Wait for reply
80 log("debug", "Reply for %s (%s)", qname, coroutine.running());
81 end
82 if ok then
83 ok, err = pcall(handler, resolver:peek(qname, qtype, qclass));
84 else
85 log("error", "Error sending DNS query: %s", err);
86 ok, err = pcall(handler, nil, err);
87 end
88 if not ok then
89 log("error", "Error in DNS response handler: %s", err);
90 end
91 end)(resolver:peek(qname, qtype, qclass));
92 end
94 function query_methods:cancel(call_handler, reason) -- luacheck: ignore 212/reason
95 log("warn", "Cancelling DNS lookup for %s", self[4]);
96 self[1].cancel(self[2], self[3], self[4], self[5], call_handler);
97 end
99 local function new_async_resolver()
100 local resolver = new_resolver();
101 resolver:socket_wrapper_set(new_async_socket);
102 return setmetatable({ _resolver = resolver}, async_resolver_mt);
105 return {
106 lookup = function (...)
107 return new_async_resolver():lookup(...);
108 end;
109 resolver = new_async_resolver;
110 new_async_socket = new_async_socket;