- return dns.peek(qname, qtype, qclass) or
- coroutine.wrap(function ()
- log("debug", "Records for "..qname.." not in cache, sending query (%s)...", tostring(coroutine.running()));
- dns.query(qname, qtype, qclass);
- coroutine.yield(nil); -- Wait for reply
- log("debug", "Reply for "..qname.." (%s)", tostring(coroutine.running()));
- handler(dns.peek(qname, qtype, qclass));
- end)();
+ return coroutine.wrap(function (peek)
+ if peek then
+ log("debug", "Records for %s already cached, using those...", qname);
+ handler(peek);
+ return;
+ end
+ log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running()));
+ local ok, err = dns.query(qname, qtype, qclass);
+ if ok then
+ coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply
+ log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running()));
+ end
+ if ok then
+ ok, err = pcall(handler, dns.peek(qname, qtype, qclass));
+ else
+ log("error", "Error sending DNS query: %s", err);
+ ok, err = pcall(handler, nil, err);
+ end
+ if not ok then
+ log("error", "Error in DNS response handler: %s", tostring(err));
+ end
+ end)(dns.peek(qname, qtype, qclass));
+end
+
+function cancel(handle, call_handler, reason)
+ log("warn", "Cancelling DNS lookup for %s", tostring(handle[3]));
+ dns.cancel(handle[1], handle[2], handle[3], handle[4], call_handler);