Software /
code /
prosody
Annotate
net/adns.lua @ 980:b52d442161f2
net.adns: Flush buffer after sending DNS request
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 11 Apr 2009 16:13:40 +0100 |
parent | 973:b091a1a7273b |
child | 1005:0eed5db7758d |
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"; |
871
642af1bdf74e
net.adns: Load the correct dns lib
Matthew Wild <mwild1@gmail.com>
parents:
870
diff
changeset
|
2 local dns = require "net.dns"; |
870
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 |
973
b091a1a7273b
net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents:
886
diff
changeset
|
6 local coroutine, tostring, pcall = coroutine, tostring, pcall; |
870
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())); |
973
b091a1a7273b
net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents:
886
diff
changeset
|
17 local ok, err = pcall(handler, dns.peek(qname, qtype, qclass)); |
b091a1a7273b
net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents:
886
diff
changeset
|
18 if not ok then |
b091a1a7273b
net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents:
886
diff
changeset
|
19 log("debug", "Error in DNS response handler: %s", tostring(err)); |
b091a1a7273b
net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents:
886
diff
changeset
|
20 end |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end)(); |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 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
|
25 local newconn = {}; |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local listener = {}; |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 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
|
28 dns.feed(sock, data); |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 function listener.disconnect() |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 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
|
33 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
|
34 newconn.handler.setsockname = function (_, ...) return sock:setsockname(...); end |
886
96de7f0a41cc
net.adns: Set new send in net.server after 'connecting UDP' socket
Matthew Wild <mwild1@gmail.com>
parents:
872
diff
changeset
|
35 newconn.handler.setpeername = function (_, ...) local ret = sock:setpeername(...); _.setsend(sock.send); return ret; end |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 newconn.handler.connect = function (_, ...) return sock:connect(...) end |
980
b52d442161f2
net.adns: Flush buffer after sending DNS request
Matthew Wild <mwild1@gmail.com>
parents:
973
diff
changeset
|
37 newconn.handler.send = function (_, data) _.write(data); return _.sendbuffer(); end |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 return newconn.handler; |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 dns:socket_wrapper_set(new_async_socket); |
872 | 42 |
43 return _M; |