Annotate

net/adns.lua @ 870:4fd5d8f1657c

net.adns: Add helper module for performing non-blocking DNS lookups
author Matthew Wild <mwild1@gmail.com>
date Wed, 04 Mar 2009 12:59:35 +0000
child 871:642af1bdf74e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
870
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local server = require "net.server";
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local dns = require "net.dns2";
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local log = require "util.logger".init("adns");
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local coroutine, tostring = coroutine, tostring;
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 module "adns"
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function lookup(handler, qname, qtype, qclass)
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 return dns.peek(qname, qtype, qclass) or
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 coroutine.wrap(function ()
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 log("debug", "Records for "..qname.." not in cache, sending query (%s)...", tostring(coroutine.running()));
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 dns.query(qname, qtype, qclass);
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 coroutine.yield(nil); -- Wait for reply
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 log("debug", "Reply for "..qname.." (%s)", tostring(coroutine.running()));
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 handler(dns.peek(qname, qtype, qclass));
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end)();
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 function new_async_socket(sock)
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local newconn = {};
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local listener = {};
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 function listener.incoming(conn, data)
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 dns.feed(sock, data);
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 function listener.disconnect()
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 newconn.handler, newconn._socket = server.wrapclient(sock, "dns", 53, listener);
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 newconn.handler.settimeout = function () end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 newconn.handler.setsockname = function (_, ...) return sock:setsockname(...); end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 newconn.handler.setpeername = function (_, ...) return sock:setpeername(...); end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 newconn.handler.connect = function (_, ...) return sock:connect(...) end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 newconn.handler.send = function (_, data) return _.write(data) end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return newconn.handler;
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
4fd5d8f1657c net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 dns:socket_wrapper_set(new_async_socket);