Merge with backout
[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)
40         log("warn", "Cancelling DNS lookup for %s", tostring(handle[3]));
41         dns.cancel(handle);
42         if call_handler then
43                 coroutine.resume(handle[4]);
44         end
45 end
46
47 function new_async_socket(sock, resolver)
48         local peername = "<unknown>";
49         local listener = {};
50         local handler = {};
51         function listener.onincoming(conn, data)
52                 if data then
53                         dns.feed(handler, data);
54                 end
55         end
56         function listener.ondisconnect(conn, err)
57                 if err then
58                         log("warn", "DNS socket for %s disconnected: %s", peername, err);
59                         local servers = resolver.server;
60                         if resolver.socketset[conn] == resolver.best_server and resolver.best_server == #servers then
61                                 log("error", "Exhausted all %d configured DNS servers, next lookup will try %s again", #servers, servers[1]);
62                         end
63                 
64                         resolver:servfail(conn); -- Let the magic commence
65                 end
66         end
67         handler = server.wrapclient(sock, "dns", 53, listener);
68         if not handler then
69                 log("warn", "handler is nil");
70         end
71         
72         handler.settimeout = function () end
73         handler.setsockname = function (_, ...) return sock:setsockname(...); end
74         handler.setpeername = function (_, ...) peername = (...); local ret = sock:setpeername(...); _:set_send(dummy_send); return ret; end
75         handler.connect = function (_, ...) return sock:connect(...) end
76         --handler.send = function (_, data) _:write(data);  return _.sendbuffer and _.sendbuffer(); end
77         handler.send = function (_, data) return sock:send(data); end
78         return handler;
79 end
80
81 dns.socket_wrapper_set(new_async_socket);
82
83 return _M;