fe35d2dd73c35f6390a5de97b3223aa33d2612f6
[prosody.git] / net / adns.lua
1 local server = require "net.server";
2 local dns = require "net.dns2";
3
4 local log = require "util.logger".init("adns");
5
6 local coroutine, tostring = coroutine, tostring;
7
8 module "adns"
9
10 function lookup(handler, qname, qtype, qclass)
11         return dns.peek(qname, qtype, qclass) or
12                 coroutine.wrap(function ()
13                                 log("debug", "Records for "..qname.." not in cache, sending query (%s)...", tostring(coroutine.running()));
14                                 dns.query(qname, qtype, qclass);
15                                 coroutine.yield(nil); -- Wait for reply
16                                 log("debug", "Reply for "..qname.." (%s)", tostring(coroutine.running()));
17                                 handler(dns.peek(qname, qtype, qclass));
18                         end)();
19 end
20
21 function new_async_socket(sock)
22         local newconn = {};
23         local listener = {};
24         function listener.incoming(conn, data)
25                 dns.feed(sock, data);
26         end
27         function listener.disconnect()
28         end
29         newconn.handler, newconn._socket = server.wrapclient(sock, "dns", 53, listener);
30         newconn.handler.settimeout = function () end
31         newconn.handler.setsockname = function (_, ...) return sock:setsockname(...); end
32         newconn.handler.setpeername = function (_, ...) return sock:setpeername(...); end
33         newconn.handler.connect = function (_, ...) return sock:connect(...) end        
34         newconn.handler.send = function (_, data) return _.write(data) end      
35         return newconn.handler;
36 end
37
38 dns:socket_wrapper_set(new_async_socket);