6 local coroutine, tostring, pcall = coroutine, tostring, pcall; |
6 local coroutine, tostring, pcall = coroutine, tostring, pcall; |
7 |
7 |
8 module "adns" |
8 module "adns" |
9 |
9 |
10 function lookup(handler, qname, qtype, qclass) |
10 function lookup(handler, qname, qtype, qclass) |
11 return dns.peek(qname, qtype, qclass) or |
11 return coroutine.wrap(function (peek) |
12 coroutine.wrap(function () |
12 if peek then |
13 log("debug", "Records for "..qname.." not in cache, sending query (%s)...", tostring(coroutine.running())); |
13 log("debug", "Records for %s already cached, using those...", qname); |
|
14 handler(peek); |
|
15 return; |
|
16 end |
|
17 log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running())); |
14 dns.query(qname, qtype, qclass); |
18 dns.query(qname, qtype, qclass); |
15 coroutine.yield(nil); -- Wait for reply |
19 coroutine.yield(nil); -- Wait for reply |
16 log("debug", "Reply for "..qname.." (%s)", tostring(coroutine.running())); |
20 log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running())); |
17 local ok, err = pcall(handler, dns.peek(qname, qtype, qclass)); |
21 local ok, err = pcall(handler, dns.peek(qname, qtype, qclass)); |
18 if not ok then |
22 if not ok then |
19 log("debug", "Error in DNS response handler: %s", tostring(err)); |
23 log("debug", "Error in DNS response handler: %s", tostring(err)); |
20 end |
24 end |
21 end)(); |
25 end)(dns.peek(qname, qtype, qclass)); |
22 end |
26 end |
23 |
27 |
24 function new_async_socket(sock) |
28 function new_async_socket(sock) |
25 local newconn = {}; |
29 local newconn = {}; |
26 local listener = {}; |
30 local listener = {}; |