Software /
code /
prosody
Annotate
net/adns.lua @ 1207:6842bacddfcb
net.adns: Fix for calling handler when cancelling DNS lookup
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 28 May 2009 02:16:43 +0100 |
parent | 1203:23725bfdeed5 |
child | 1208:5f992ddc9685 |
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) |
1005
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
11 return coroutine.wrap(function (peek) |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
12 if peek then |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
13 log("debug", "Records for %s already cached, using those...", qname); |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
14 handler(peek); |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
15 return; |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
16 end |
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
17 log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running())); |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 dns.query(qname, qtype, qclass); |
1203
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1005
diff
changeset
|
19 coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply |
1005
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
20 log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running())); |
973
b091a1a7273b
net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents:
886
diff
changeset
|
21 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
|
22 if not ok then |
b091a1a7273b
net.adns: Catch errors in DNS response callbacks
Matthew Wild <mwild1@gmail.com>
parents:
886
diff
changeset
|
23 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
|
24 end |
1005
0eed5db7758d
net.adns: Call handler for records already cached
Matthew Wild <mwild1@gmail.com>
parents:
980
diff
changeset
|
25 end)(dns.peek(qname, qtype, qclass)); |
870
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 |
1203
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1005
diff
changeset
|
28 function cancel(handle, call_handler) |
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1005
diff
changeset
|
29 dns.cancel(handle); |
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1005
diff
changeset
|
30 if call_handler then |
1207
6842bacddfcb
net.adns: Fix for calling handler when cancelling DNS lookup
Matthew Wild <mwild1@gmail.com>
parents:
1203
diff
changeset
|
31 coroutine.resume(handle[4]); |
1203
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1005
diff
changeset
|
32 end |
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1005
diff
changeset
|
33 end |
23725bfdeed5
net.adns: Add support for cancelling a non-blocking lookup, optionally calling the handler
Matthew Wild <mwild1@gmail.com>
parents:
1005
diff
changeset
|
34 |
870
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 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
|
36 local newconn = {}; |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local listener = {}; |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 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
|
39 dns.feed(sock, data); |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 function listener.disconnect() |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 return newconn.handler; |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 |
4fd5d8f1657c
net.adns: Add helper module for performing non-blocking DNS lookups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 dns:socket_wrapper_set(new_async_socket); |
872 | 53 |
54 return _M; |