Software /
code /
prosody
Comparison
net/dns.lua @ 6509:7cb69eba3e95
net.dns: Try next server if peer name can not be set (thanks wirehack7)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 30 Oct 2014 12:28:07 +0100 |
parent | 6508:4a28b8320d7f |
child | 6510:8273236a995f |
child | 6631:45222bfb206f |
comparison
equal
deleted
inserted
replaced
6508:4a28b8320d7f | 6509:7cb69eba3e95 |
---|---|
620 self.socketset = self.socketset or {}; | 620 self.socketset = self.socketset or {}; |
621 | 621 |
622 local sock = self.socket[servernum]; | 622 local sock = self.socket[servernum]; |
623 if sock then return sock; end | 623 if sock then return sock; end |
624 | 624 |
625 local err; | 625 local ok, err; |
626 sock, err = socket.udp(); | 626 sock, err = socket.udp(); |
627 if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end | 627 if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end |
628 if not sock then | 628 if not sock then |
629 return nil, err; | 629 return nil, err; |
630 end | 630 end |
631 sock:settimeout(0); | 631 sock:settimeout(0); |
632 -- todo: attempt to use a random port, fallback to 0 | 632 -- todo: attempt to use a random port, fallback to 0 |
633 sock:setsockname('*', 0); | |
634 sock:setpeername(self.server[servernum], 53); | |
635 self.socket[servernum] = sock; | 633 self.socket[servernum] = sock; |
636 self.socketset[sock] = servernum; | 634 self.socketset[sock] = servernum; |
635 -- set{sock,peer}name can fail, eg because of local routing table | |
636 -- if so, try the next server | |
637 ok, err = sock:setsockname('*', 0); | |
638 if not ok then return self:servfail(sock, err); end | |
639 ok, err = sock:setpeername(self.server[servernum], 53); | |
640 if not ok then return self:servfail(sock, err); end | |
637 return sock; | 641 return sock; |
638 end | 642 end |
639 | 643 |
640 function resolver:voidsocket(sock) | 644 function resolver:voidsocket(sock) |
641 if self.socket[sock] then | 645 if self.socket[sock] then |
779 end) | 783 end) |
780 end | 784 end |
781 return true; | 785 return true; |
782 end | 786 end |
783 | 787 |
784 function resolver:servfail(sock) | 788 function resolver:servfail(sock, err) |
785 -- Resend all queries for this server | 789 -- Resend all queries for this server |
786 | 790 |
787 local num = self.socketset[sock] | 791 local num = self.socketset[sock] |
788 | 792 |
789 -- Socket is dead now | 793 -- Socket is dead now |
802 o.retries = (o.retries or 0) + 1; | 806 o.retries = (o.retries or 0) + 1; |
803 if o.retries >= #self.server then | 807 if o.retries >= #self.server then |
804 --print('timeout'); | 808 --print('timeout'); |
805 queries[question] = nil; | 809 queries[question] = nil; |
806 else | 810 else |
807 sock = self:getsocket(o.server); | 811 sock, err = self:getsocket(o.server); |
808 if sock then sock:send(o.packet); end | 812 if sock then sock:send(o.packet); end |
809 end | 813 end |
810 end | 814 end |
811 end | 815 end |
812 if next(queries) == nil then | 816 if next(queries) == nil then |
819 if self.best_server > #self.server then | 823 if self.best_server > #self.server then |
820 -- Exhausted all servers, try first again | 824 -- Exhausted all servers, try first again |
821 self.best_server = 1; | 825 self.best_server = 1; |
822 end | 826 end |
823 end | 827 end |
824 return sock; | 828 return sock, err; |
825 end | 829 end |
826 | 830 |
827 function resolver:settimeout(seconds) | 831 function resolver:settimeout(seconds) |
828 self.timeout = seconds; | 832 self.timeout = seconds; |
829 end | 833 end |