Software /
code /
prosody
Comparison
net/dns.lua @ 1788:45779d67c26c
Merge with 0.5
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 18 Sep 2009 02:48:52 +0100 |
parent | 1729:2876a0ecceab |
parent | 1786:4016d8bc30b8 |
child | 1806:c55e06eb53b2 |
comparison
equal
deleted
inserted
replaced
1784:b2bfd3b93da6 | 1788:45779d67c26c |
---|---|
486 | 486 |
487 | 487 |
488 -- socket layer -------------------------------------------------- socket layer | 488 -- socket layer -------------------------------------------------- socket layer |
489 | 489 |
490 | 490 |
491 resolver.delays = { 1, 3, 11, 45 } | 491 resolver.delays = { 1, 3 } |
492 | 492 |
493 | 493 |
494 function resolver:addnameserver (address) -- - - - - - - - - - addnameserver | 494 function resolver:addnameserver (address) -- - - - - - - - - - addnameserver |
495 self.server = self.server or {} | 495 self.server = self.server or {} |
496 append (self.server, address) | 496 append (self.server, address) |
527 | 527 |
528 local sock = self.socket[servernum] | 528 local sock = self.socket[servernum] |
529 if sock then return sock end | 529 if sock then return sock end |
530 | 530 |
531 sock = socket.udp () | 531 sock = socket.udp () |
532 if self.socket_wrapper then sock = self.socket_wrapper (sock) end | 532 if self.socket_wrapper then sock = self.socket_wrapper (sock, self) end |
533 sock:settimeout (0) | 533 sock:settimeout (0) |
534 -- todo: attempt to use a random port, fallback to 0 | 534 -- todo: attempt to use a random port, fallback to 0 |
535 sock:setsockname ('*', 0) | 535 sock:setsockname ('*', 0) |
536 sock:setpeername (self.server[servernum], 53) | 536 sock:setpeername (self.server[servernum], 53) |
537 self.socket[servernum] = sock | 537 self.socket[servernum] = sock |
538 self.socketset[sock] = sock | 538 self.socketset[sock] = servernum |
539 return sock | 539 return sock |
540 end | 540 end |
541 | 541 |
542 function resolver:voidsocket (sock) | |
543 if self.socket[sock] then | |
544 self.socketset[self.socket[sock]] = nil | |
545 self.socket[sock] = nil | |
546 elseif self.socketset[sock] then | |
547 self.socket[self.socketset[sock]] = nil | |
548 self.socketset[sock] = nil | |
549 end | |
550 end | |
542 | 551 |
543 function resolver:socket_wrapper_set (func) -- - - - - - - socket_wrapper_set | 552 function resolver:socket_wrapper_set (func) -- - - - - - - socket_wrapper_set |
544 self.socket_wrapper = func | 553 self.socket_wrapper = func |
545 end | 554 end |
546 | 555 |
610 if peek then return peek end | 619 if peek then return peek end |
611 | 620 |
612 local header, id = encodeHeader () | 621 local header, id = encodeHeader () |
613 --print ('query id', id, qclass, qtype, qname) | 622 --print ('query id', id, qclass, qtype, qname) |
614 local o = { packet = header..question, | 623 local o = { packet = header..question, |
615 server = 1, | 624 server = self.best_server, |
616 delay = 1, | 625 delay = 1, |
617 retry = socket.gettime () + self.delays[1] } | 626 retry = socket.gettime () + self.delays[1] } |
618 self:getsocket (o.server):send (o.packet) | |
619 | 627 |
620 -- remember the query | 628 -- remember the query |
621 self.active[id] = self.active[id] or {} | 629 self.active[id] = self.active[id] or {} |
622 self.active[id][question] = o | 630 self.active[id][question] = o |
623 | 631 |
625 local co = coroutine.running () | 633 local co = coroutine.running () |
626 if co then | 634 if co then |
627 set (self.wanted, qclass, qtype, qname, co, true) | 635 set (self.wanted, qclass, qtype, qname, co, true) |
628 --set (self.yielded, co, qclass, qtype, qname, true) | 636 --set (self.yielded, co, qclass, qtype, qname, true) |
629 end | 637 end |
638 | |
639 self:getsocket (o.server):send (o.packet) | |
640 | |
630 end | 641 end |
631 | 642 |
632 | 643 function resolver:servfail(sock) |
644 -- Resend all queries for this server | |
645 | |
646 local num = self.socketset[sock] | |
647 | |
648 -- Socket is dead now | |
649 self:voidsocket(sock); | |
650 | |
651 -- Find all requests to the down server, and retry on the next server | |
652 self.time = socket.gettime () | |
653 for id,queries in pairs (self.active) do | |
654 for question,o in pairs (queries) do | |
655 if o.server == num then -- This request was to the broken server | |
656 o.server = o.server + 1 -- Use next server | |
657 if o.server > #self.server then | |
658 o.server = 1 | |
659 end | |
660 | |
661 o.retries = (o.retries or 0) + 1; | |
662 if o.retries >= #self.server then | |
663 --print ('timeout') | |
664 queries[question] = nil | |
665 else | |
666 local _a = self:getsocket(o.server); | |
667 if _a then _a:send (o.packet) end | |
668 end | |
669 end | |
670 end | |
671 end | |
672 | |
673 if num == self.best_server then | |
674 self.best_server = self.best_server + 1 | |
675 if self.best_server > #self.server then | |
676 -- Exhausted all servers, try first again | |
677 self.best_server = 1 | |
678 end | |
679 end | |
680 end | |
633 | 681 |
634 function resolver:receive (rset) -- - - - - - - - - - - - - - - - - receive | 682 function resolver:receive (rset) -- - - - - - - - - - - - - - - - - receive |
635 | 683 |
636 --print 'receive' print (self.socket) | 684 --print 'receive' print (self.socket) |
637 self.time = socket.gettime () | 685 self.time = socket.gettime () |
830 | 878 |
831 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver | 879 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver |
832 | 880 |
833 -- this function seems to be redundant with resolver.new () | 881 -- this function seems to be redundant with resolver.new () |
834 | 882 |
835 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {} } | 883 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {}, |
884 best_server = 1 } | |
836 setmetatable (r, resolver) | 885 setmetatable (r, resolver) |
837 setmetatable (r.cache, cache_metatable) | 886 setmetatable (r.cache, cache_metatable) |
838 setmetatable (r.unsorted, { __mode = 'kv' }) | 887 setmetatable (r.unsorted, { __mode = 'kv' }) |
839 return r | 888 return r |
840 end | 889 end |