net.adns: Trailing whitespace
[prosody.git] / net / adns.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local server = require "net.server";
10 local dns = require "net.dns";
11
12 local log = require "util.logger".init("adns");
13
14 local t_insert, t_remove = table.insert, table.remove;
15 local coroutine, tostring, pcall = coroutine, tostring, pcall;
16
17 module "adns"
18
19 function lookup(handler, qname, qtype, qclass)
20         return coroutine.wrap(function (peek)
21                                 if peek then
22                                         log("debug", "Records for %s already cached, using those...", qname);
23                                         handler(peek);
24                                         return;
25                                 end
26                                 log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running()));
27                                 dns.query(qname, qtype, qclass);
28                                 coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply
29                                 log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running()));
30                                 local ok, err = pcall(handler, dns.peek(qname, qtype, qclass));
31                                 if not ok then
32                                         log("error", "Error in DNS response handler: %s", tostring(err));
33                                 end
34                         end)(dns.peek(qname, qtype, qclass));
35 end
36
37 function cancel(handle, call_handler)
38         log("warn", "Cancelling DNS lookup for %s", tostring(handle[3]));
39         dns.cancel(handle);
40         if call_handler then
41                 coroutine.resume(handle[4]);
42         end
43 end
44
45 function new_async_socket(sock, resolver)
46         local peername = "<unknown>";
47         local listener = {};
48         local handler = {};
49         function listener.onincoming(conn, data)
50                 dns.feed(handler, data);
51         end
52         function listener.ondisconnect(conn, err)
53                 log("warn", "DNS socket for %s disconnected: %s", peername, err);
54                 local servers = resolver.server;
55                 if resolver.socketset[conn] == resolver.best_server and resolver.best_server == #servers then
56                         log("error", "Exhausted all %d configured DNS servers, next lookup will try %s again", #servers, servers[1]);
57                 end
58                 
59                 resolver:servfail(conn); -- Let the magic commence
60         end
61         handler = server.wrapclient(sock, "dns", 53, listener);
62         if not handler then
63                 log("warn", "handler is nil");
64         end
65         
66         handler.settimeout = function () end
67         handler.setsockname = function (_, ...) return sock:setsockname(...); end
68         handler.setpeername = function (_, ...) peername = (...); local ret = sock:setpeername(...); _:set_send(sock.send); return ret; end
69         handler.connect = function (_, ...) return sock:connect(...) end
70         handler.send = function (_, data) _:write(data);  return _.sendbuffer and _.sendbuffer(); end
71         return handler;
72 end
73
74 dns:socket_wrapper_set(new_async_socket);
75
76 return _M;