Comparison

net/dns.lua @ 3324:070c8ba71b76

net.dns: Handle our own timeouts, including falling onto other servers in resolv.conf if necessary
author Matthew Wild <mwild1@gmail.com>
date Mon, 05 Jul 2010 11:50:21 +0100
parent 3057:ca1fe9f74bd0
child 3325:b3117a1da834
comparison
equal deleted inserted replaced
3323:5228a395999b 3324:070c8ba71b76
14 -- reference: http://tools.ietf.org/html/rfc1876 (LOC) 14 -- reference: http://tools.ietf.org/html/rfc1876 (LOC)
15 15
16 16
17 local socket = require "socket"; 17 local socket = require "socket";
18 local ztact = require "util.ztact"; 18 local ztact = require "util.ztact";
19 local timer = require "util.timer";
20
19 local _, windows = pcall(require, "util.windows"); 21 local _, windows = pcall(require, "util.windows");
20 local is_windows = (_ and windows) or os.getenv("WINDIR"); 22 local is_windows = (_ and windows) or os.getenv("WINDIR");
21 23
22 local coroutine, io, math, string, table = 24 local coroutine, io, math, string, table =
23 coroutine, io, math, string, table; 25 coroutine, io, math, string, table;
25 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack = 27 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack =
26 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack; 28 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack;
27 29
28 local get, set = ztact.get, ztact.set; 30 local get, set = ztact.get, ztact.set;
29 31
32 local dns_timeout = 15;
30 33
31 -------------------------------------------------- module dns 34 -------------------------------------------------- module dns
32 module('dns') 35 module('dns')
33 local dns = _M; 36 local dns = _M;
34 37
676 if co then 679 if co then
677 set(self.wanted, qclass, qtype, qname, co, true); 680 set(self.wanted, qclass, qtype, qname, co, true);
678 --set(self.yielded, co, qclass, qtype, qname, true); 681 --set(self.yielded, co, qclass, qtype, qname, true);
679 end 682 end
680 683
681 self:getsocket (o.server):send (o.packet) 684 local conn = self:getsocket(o.server)
685 conn:send (o.packet)
686
687 if timer then
688 local num_servers = #self.server;
689 local i = 1;
690 timer.add_task(dns_timeout, function ()
691 if get(self.wanted, qclass, qtype, qname, co) then
692 if i < num_servers then
693 i = i + 1;
694 self:servfail(conn);
695 o.server = self.best_server;
696 conn = self:getsocket(o.server);
697 conn:send(o.packet);
698 return dns_timeout;
699 else
700 -- Tried everything, failed
701 resolver:cancel({qclass, qtype, qname, co}, true);
702 end
703 end
704 end)
705 end
682 end 706 end
683 707
684 function resolver:servfail(sock) 708 function resolver:servfail(sock)
685 -- Resend all queries for this server 709 -- Resend all queries for this server
686 710