Comparison

net/dns.lua @ 379:c5617678cd7b

Fix various mistakes in dns.lua
author Matthew Wild <mwild1@gmail.com>
date Sun, 23 Nov 2008 03:00:20 +0000
parent 378:47fdbd074641
child 399:93b6587d9afb
comparison
equal deleted inserted replaced
378:47fdbd074641 379:c5617678cd7b
17 17
18 18
19 local coroutine, io, math, socket, string, table = 19 local coroutine, io, math, socket, string, table =
20 coroutine, io, math, socket, string, table 20 coroutine, io, math, socket, string, table
21 21
22 local ipairs, next, pairs, print, setmetatable, tostring = 22 local ipairs, next, pairs, print, setmetatable, tostring, assert, error =
23 ipairs, next, pairs, print, setmetatable, tostring 23 ipairs, next, pairs, print, setmetatable, tostring, assert, error
24 24
25 local get, set = ztact.get, ztact.set 25 local get, set = ztact.get, ztact.set
26 26
27 27
28 -------------------------------------------------- module dns 28 -------------------------------------------------- module dns
128 end 128 end
129 129
130 130
131 local rrs_metatable = {} -- - - - - - - - - - - - - - - - - - rrs_metatable 131 local rrs_metatable = {} -- - - - - - - - - - - - - - - - - - rrs_metatable
132 function rrs_metatable.__tostring (rrs) 132 function rrs_metatable.__tostring (rrs)
133 t = {} 133 local t = {}
134 for i,rr in pairs (rrs) do append (t, tostring (rr)..'\n') end 134 for i,rr in pairs (rrs) do append (t, tostring (rr)..'\n') end
135 return table.concat (t) 135 return table.concat (t)
136 end 136 end
137 137
138 138
502 502
503 function resolver:adddefaultnameservers () -- - - - - adddefaultnameservers 503 function resolver:adddefaultnameservers () -- - - - - adddefaultnameservers
504 local resolv_conf = io.open("/etc/resolv.conf"); 504 local resolv_conf = io.open("/etc/resolv.conf");
505 if not resolv_conf then return nil; end 505 if not resolv_conf then return nil; end
506 for line in resolv_conf:lines() do 506 for line in resolv_conf:lines() do
507 address = string.match (line, 'nameserver%s+(%d+%.%d+%.%d+%.%d+)') 507 local address = string.match (line, 'nameserver%s+(%d+%.%d+%.%d+%.%d+)')
508 if address then self:addnameserver (address) end 508 if address then self:addnameserver (address) end
509 end end 509 end end
510 510
511 511
512 function resolver:getsocket (servernum) -- - - - - - - - - - - - - getsocket 512 function resolver:getsocket (servernum) -- - - - - - - - - - - - - getsocket
580 if soft == 'soft' then 580 if soft == 'soft' then
581 self.time = socket.gettime () 581 self.time = socket.gettime ()
582 for class,types in pairs (self.cache or {}) do 582 for class,types in pairs (self.cache or {}) do
583 for type,names in pairs (types) do 583 for type,names in pairs (types) do
584 for name,rrs in pairs (names) do 584 for name,rrs in pairs (names) do
585 prune (rrs, time, 'soft') 585 prune (rrs, self.time, 'soft')
586 end end end 586 end end end
587 else self.cache = {} end 587 else self.cache = {} end
588 end 588 end
589 589
590 590
592 592
593 qname, qtype, qclass = standardize (qname, qtype, qclass) 593 qname, qtype, qclass = standardize (qname, qtype, qclass)
594 594
595 if not self.server then self:adddefaultnameservers () end 595 if not self.server then self:adddefaultnameservers () end
596 596
597 local question = question or encodeQuestion (qname, qtype, qclass) 597 local question = encodeQuestion (qname, qtype, qclass)
598 local peek = self:peek (qname, qtype, qclass) 598 local peek = self:peek (qname, qtype, qclass)
599 if peek then return peek end 599 if peek then return peek end
600 600
601 local header, id = encodeHeader () 601 local header, id = encodeHeader ()
602 -- print ('query id', id, qclass, qtype, qname) 602 -- print ('query id', id, qclass, qtype, qname)
765 765
766 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver 766 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
767 767
768 -- this function seems to be redundant with resolver.new () 768 -- this function seems to be redundant with resolver.new ()
769 769
770 r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {} } 770 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {} }
771 setmetatable (r, resolver) 771 setmetatable (r, resolver)
772 setmetatable (r.cache, cache_metatable) 772 setmetatable (r.cache, cache_metatable)
773 setmetatable (r.unsorted, { __mode = 'kv' }) 773 setmetatable (r.unsorted, { __mode = 'kv' })
774 return r 774 return r
775 end 775 end