Comparison

net/dns.lua @ 6511:5f9389af5115

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Sun, 09 Nov 2014 20:32:33 +0100
parent 6510:8273236a995f
child 6632:855085439f7f
comparison
equal deleted inserted replaced
6502:8fed6ea12098 6511:5f9389af5115
622 self.socketset = self.socketset or {}; 622 self.socketset = self.socketset or {};
623 623
624 local sock = self.socket[servernum]; 624 local sock = self.socket[servernum];
625 if sock then return sock; end 625 if sock then return sock; end
626 626
627 local err; 627 local ok, err;
628 local peer = self.server[servernum]; 628 local peer = self.server[servernum];
629 if peer:find(":") then 629 if peer:find(":") then
630 sock, err = socket.udp6(); 630 sock, err = socket.udp6();
631 else 631 else
632 sock, err = socket.udp(); 632 sock, err = socket.udp();
635 if not sock then 635 if not sock then
636 return nil, err; 636 return nil, err;
637 end 637 end
638 sock:settimeout(0); 638 sock:settimeout(0);
639 -- todo: attempt to use a random port, fallback to 0 639 -- todo: attempt to use a random port, fallback to 0
640 sock:setsockname('*', 0);
641 sock:setpeername(peer, 53);
642 self.socket[servernum] = sock; 640 self.socket[servernum] = sock;
643 self.socketset[sock] = servernum; 641 self.socketset[sock] = servernum;
642 -- set{sock,peer}name can fail, eg because of local routing table
643 -- if so, try the next server
644 ok, err = sock:setsockname('*', 0);
645 if not ok then return self:servfail(sock, err); end
646 ok, err = sock:setpeername(peer, 53);
647 if not ok then return self:servfail(sock, err); end
644 return sock; 648 return sock;
645 end 649 end
646 650
647 function resolver:voidsocket(sock) 651 function resolver:voidsocket(sock)
648 if self.socket[sock] then 652 if self.socket[sock] then
786 end) 790 end)
787 end 791 end
788 return true; 792 return true;
789 end 793 end
790 794
791 function resolver:servfail(sock) 795 function resolver:servfail(sock, err)
792 -- Resend all queries for this server 796 -- Resend all queries for this server
793 797
794 local num = self.socketset[sock] 798 local num = self.socketset[sock]
795 799
796 -- Socket is dead now 800 -- Socket is dead now
797 self:voidsocket(sock); 801 sock = self:voidsocket(sock);
798 802
799 -- Find all requests to the down server, and retry on the next server 803 -- Find all requests to the down server, and retry on the next server
800 self.time = socket.gettime(); 804 self.time = socket.gettime();
801 for id,queries in pairs(self.active) do 805 for id,queries in pairs(self.active) do
802 for question,o in pairs(queries) do 806 for question,o in pairs(queries) do
809 o.retries = (o.retries or 0) + 1; 813 o.retries = (o.retries or 0) + 1;
810 if o.retries >= #self.server then 814 if o.retries >= #self.server then
811 --print('timeout'); 815 --print('timeout');
812 queries[question] = nil; 816 queries[question] = nil;
813 else 817 else
814 local _a = self:getsocket(o.server); 818 sock, err = self:getsocket(o.server);
815 if _a then _a:send(o.packet); end 819 if sock then sock:send(o.packet); end
816 end 820 end
817 end 821 end
818 end 822 end
819 if next(queries) == nil then 823 if next(queries) == nil then
820 self.active[id] = nil; 824 self.active[id] = nil;
826 if self.best_server > #self.server then 830 if self.best_server > #self.server then
827 -- Exhausted all servers, try first again 831 -- Exhausted all servers, try first again
828 self.best_server = 1; 832 self.best_server = 1;
829 end 833 end
830 end 834 end
835 return sock, err;
831 end 836 end
832 837
833 function resolver:settimeout(seconds) 838 function resolver:settimeout(seconds)
834 self.timeout = seconds; 839 self.timeout = seconds;
835 end 840 end