net.server_select: Set select() timeout to 3600 by default.
[prosody.git] / net / adns.lua
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 --
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 local function dummy_send(sock, data, i, j) return (j-i)+1; end
18
19 module "adns"
20
21 function lookup(handler, qname, qtype, qclass)
22         return coroutine.wrap(function (peek)
23                                 if peek then
24                                         log("debug", "Records for %s already cached, using those...", qname);
25                                         handler(peek);
26                                         return;
27                                 end
28                                 log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running()));
29                                 dns.query(qname, qtype, qclass);
30                                 coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply
31                                 log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running()));
32                                 local ok, err = pcall(handler, dns.peek(qname, qtype, qclass));
33                                 if not ok then
34                                         log("error", "Error in DNS response handler: %s", tostring(err));
35                                 end
36                         end)(dns.peek(qname, qtype, qclass));
37 end
38
39 function cancel(handle, call_handler, reason)
40         log("warn", "Cancelling DNS lookup for %s", tostring(handle[3]));
41         dns.cancel(handle[1], handle[2], handle[3], handle[4], call_handler);
42 end
43
44 function new_async_socket(sock, resolver)
45         local peername = "<unknown>";
46         local listener = {};
47         local handler = {};
48         function listener.onincoming(conn, data)
49                 if data then
50                         dns.feed(handler, data);
51                 end
52         end
53         function listener.ondisconnect(conn, err)
54                 if err then
55                         log("warn", "DNS socket for %s disconnected: %s", peername, err);
56                         local servers = resolver.server;
57                         if resolver.socketset[conn] == resolver.best_server and resolver.best_server == #servers then
58                                 log("error", "Exhausted all %d configured DNS servers, next lookup will try %s again", #servers, servers[1]);
59                         end
60                 
61                         resolver:servfail(conn); -- Let the magic commence
62                 end
63         end
64         handler = server.wrapclient(sock, "dns", 53, listener);
65         if not handler then
66                 log("warn", "handler is nil");
67         end
68         
69         handler.settimeout = function () end
70         handler.setsockname = function (_, ...) return sock:setsockname(...); end
71         handler.setpeername = function (_, ...) peername = (...); local ret = sock:setpeername(...); _:set_send(dummy_send); return ret; end
72         handler.connect = function (_, ...) return sock:connect(...) end
73         --handler.send = function (_, data) _:write(data);  return _.sendbuffer and _.sendbuffer(); end
74         handler.send = function (_, data) return sock:send(data); end
75         return handler;
76 end
77
78 dns.socket_wrapper_set(new_async_socket);
79
80 return _M;