2 -- This file is included with Prosody IM. It has modifications,
3 -- which are hereby placed in the public domain.
5 -- public domain 20080404 lua@ztact.com
8 -- todo: quick (default) header generation
9 -- todo: nxdomain, error handling
10 -- todo: cache results of encodeName
13 -- reference: http://tools.ietf.org/html/rfc1035
14 -- reference: http://tools.ietf.org/html/rfc1876 (LOC)
17 local socket = require "socket";
18 local ztact = require "util.ztact";
19 local _, windows = pcall(require, "util.windows");
20 local is_windows = (_ and windows) or os.getenv("WINDIR");
22 local coroutine, io, math, string, table =
23 coroutine, io, math, string, table;
25 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack =
26 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack;
28 local get, set = ztact.get, ztact.set;
31 -------------------------------------------------- module dns
36 -- dns type & class codes ------------------------------ dns type & class codes
39 local append = table.insert
42 local function highbyte(i) -- - - - - - - - - - - - - - - - - - - highbyte
43 return (i-(i%0x100))/0x100;
47 local function augment (t) -- - - - - - - - - - - - - - - - - - - - augment
49 for i,s in pairs(t) do
52 a[string.lower(s)] = s;
58 local function encode (t) -- - - - - - - - - - - - - - - - - - - - - encode
60 for i,s in pairs(t) do
61 local word = string.char(highbyte(i), i%0x100);
64 code[string.lower(s)] = word;
71 'A', 'NS', 'MD', 'MF', 'CNAME', 'SOA', 'MB', 'MG', 'MR', 'NULL', 'WKS',
72 'PTR', 'HINFO', 'MINFO', 'MX', 'TXT',
73 [ 28] = 'AAAA', [ 29] = 'LOC', [ 33] = 'SRV',
74 [252] = 'AXFR', [253] = 'MAILB', [254] = 'MAILA', [255] = '*' };
77 dns.classes = { 'IN', 'CS', 'CH', 'HS', [255] = '*' };
80 dns.type = augment (dns.types);
81 dns.class = augment (dns.classes);
82 dns.typecode = encode (dns.types);
83 dns.classcode = encode (dns.classes);
87 local function standardize(qname, qtype, qclass) -- - - - - - - standardize
88 if string.byte(qname, -1) ~= 0x2E then qname = qname..'.'; end
89 qname = string.lower(qname);
90 return qname, dns.type[qtype or 'A'], dns.class[qclass or 'IN'];
94 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune
95 time = time or socket.gettime();
96 for i,rr in pairs(rrs) do
98 -- rr.tod = rr.tod - 50 -- accelerated decripitude
99 rr.ttl = math.floor(rr.tod - time);
101 table.remove(rrs, i);
102 return prune(rrs, time, soft); -- Re-iterate
104 elseif soft == 'soft' then -- What is this? I forget!
112 -- metatables & co. ------------------------------------------ metatables & co.
116 resolver.__index = resolver;
122 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
123 function rr_metatable.__tostring(rr)
124 local s0 = string.format('%2s %-5s %6i %-28s', rr.class, rr.type, rr.ttl, rr.name);
126 if rr.type == 'A' then
128 elseif rr.type == 'MX' then
129 s1 = string.format(' %2i %s', rr.pref, rr.mx);
130 elseif rr.type == 'CNAME' then
132 elseif rr.type == 'LOC' then
133 s1 = ' '..resolver.LOC_tostring(rr);
134 elseif rr.type == 'NS' then
136 elseif rr.type == 'SRV' then
137 s1 = ' '..SRV_tostring(rr);
138 elseif rr.type == 'TXT' then
141 s1 = ' <UNKNOWN RDATA TYPE>';
147 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
148 function rrs_metatable.__tostring(rrs)
150 for i,rr in pairs(rrs) do
151 append(t, tostring(rr)..'\n');
153 return table.concat(t);
157 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable
158 function cache_metatable.__tostring(cache)
159 local time = socket.gettime();
161 for class,types in pairs(cache) do
162 for type,names in pairs(types) do
163 for name,rrs in pairs(names) do
165 append(t, tostring(rrs));
169 return table.concat(t);
173 function resolver:new() -- - - - - - - - - - - - - - - - - - - - - resolver
174 local r = { active = {}, cache = {}, unsorted = {} };
175 setmetatable(r, resolver);
176 setmetatable(r.cache, cache_metatable);
177 setmetatable(r.unsorted, { __mode = 'kv' });
182 -- packet layer -------------------------------------------------- packet layer
185 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
186 math.randomseed(10000*socket.gettime());
187 dns.random = math.random;
188 return dns.random(...);
192 local function encodeHeader(o) -- - - - - - - - - - - - - - - encodeHeader
194 o.id = o.id or dns.random(0, 0xffff); -- 16b (random) id
196 o.rd = o.rd or 1; -- 1b 1 recursion desired
197 o.tc = o.tc or 0; -- 1b 1 truncated response
198 o.aa = o.aa or 0; -- 1b 1 authoritative response
199 o.opcode = o.opcode or 0; -- 4b 0 query
201 -- 2 server status request
203 o.qr = o.qr or 0; -- 1b 0 query, 1 response
205 o.rcode = o.rcode or 0; -- 4b 0 no error
212 o.z = o.z or 0; -- 3b 0 resvered
213 o.ra = o.ra or 0; -- 1b 1 recursion available
215 o.qdcount = o.qdcount or 1; -- 16b number of question RRs
216 o.ancount = o.ancount or 0; -- 16b number of answers RRs
217 o.nscount = o.nscount or 0; -- 16b number of nameservers RRs
218 o.arcount = o.arcount or 0; -- 16b number of additional RRs
220 -- string.char() rounds, so prevent roundup with -0.4999
221 local header = string.char(
222 highbyte(o.id), o.id %0x100,
223 o.rd + 2*o.tc + 4*o.aa + 8*o.opcode + 128*o.qr,
224 o.rcode + 16*o.z + 128*o.ra,
225 highbyte(o.qdcount), o.qdcount %0x100,
226 highbyte(o.ancount), o.ancount %0x100,
227 highbyte(o.nscount), o.nscount %0x100,
228 highbyte(o.arcount), o.arcount %0x100
235 local function encodeName(name) -- - - - - - - - - - - - - - - - encodeName
237 for part in string.gmatch(name, '[^.]+') do
238 append(t, string.char(string.len(part)));
241 append(t, string.char(0));
242 return table.concat(t);
246 local function encodeQuestion(qname, qtype, qclass) -- - - - encodeQuestion
247 qname = encodeName(qname);
248 qtype = dns.typecode[qtype or 'a'];
249 qclass = dns.classcode[qclass or 'in'];
250 return qname..qtype..qclass;
254 function resolver:byte(len) -- - - - - - - - - - - - - - - - - - - - - byte
256 local offset = self.offset;
257 local last = offset + len - 1;
258 if last > #self.packet then
259 error(string.format('out of bounds: %i>%i', last, #self.packet));
261 self.offset = offset + len;
262 return string.byte(self.packet, offset, last);
266 function resolver:word() -- - - - - - - - - - - - - - - - - - - - - - word
267 local b1, b2 = self:byte(2);
268 return 0x100*b1 + b2;
272 function resolver:dword () -- - - - - - - - - - - - - - - - - - - - - dword
273 local b1, b2, b3, b4 = self:byte(4);
274 --print('dword', b1, b2, b3, b4);
275 return 0x1000000*b1 + 0x10000*b2 + 0x100*b3 + b4;
279 function resolver:sub(len) -- - - - - - - - - - - - - - - - - - - - - - sub
281 local s = string.sub(self.packet, self.offset, self.offset + len - 1);
282 self.offset = self.offset + len;
287 function resolver:header(force) -- - - - - - - - - - - - - - - - - - header
288 local id = self:word();
289 --print(string.format(':header id %x', id));
290 if not self.active[id] and not force then return nil; end
292 local h = { id = id };
294 local b1, b2 = self:byte(2);
306 h.qdcount = self:word();
307 h.ancount = self:word();
308 h.nscount = self:word();
309 h.arcount = self:word();
311 for k,v in pairs(h) do h[k] = v-v%1; end
317 function resolver:name() -- - - - - - - - - - - - - - - - - - - - - - name
318 local remember, pointers = nil, 0;
319 local len = self:byte();
322 if len >= 0xc0 then -- name is "compressed"
323 pointers = pointers + 1;
324 if pointers >= 20 then error('dns error: 20 pointers'); end;
325 local offset = ((len-0xc0)*0x100) + self:byte();
326 remember = remember or self.offset;
327 self.offset = offset + 1; -- +1 for lua
328 else -- name is not compressed
329 append(n, self:sub(len)..'.');
333 self.offset = remember or self.offset;
334 return table.concat(n);
338 function resolver:question() -- - - - - - - - - - - - - - - - - - question
340 q.name = self:name();
341 q.type = dns.type[self:word()];
342 q.class = dns.class[self:word()];
347 function resolver:A(rr) -- - - - - - - - - - - - - - - - - - - - - - - - A
348 local b1, b2, b3, b4 = self:byte(4);
349 rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
353 function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
354 rr.cname = self:name();
358 function resolver:MX(rr) -- - - - - - - - - - - - - - - - - - - - - - - MX
359 rr.pref = self:word();
364 function resolver:LOC_nibble_power() -- - - - - - - - - - LOC_nibble_power
365 local b = self:byte();
366 --print('nibbles', ((b-(b%0x10))/0x10), (b%0x10));
367 return ((b-(b%0x10))/0x10) * (10^(b%0x10));
371 function resolver:LOC(rr) -- - - - - - - - - - - - - - - - - - - - - - LOC
372 rr.version = self:byte();
373 if rr.version == 0 then
374 rr.loc = rr.loc or {};
375 rr.loc.size = self:LOC_nibble_power();
376 rr.loc.horiz_pre = self:LOC_nibble_power();
377 rr.loc.vert_pre = self:LOC_nibble_power();
378 rr.loc.latitude = self:dword();
379 rr.loc.longitude = self:dword();
380 rr.loc.altitude = self:dword();
385 local function LOC_tostring_degrees(f, pos, neg) -- - - - - - - - - - - - -
387 if f < 0 then pos = neg; f = -f; end
388 local deg, min, msec;
393 return string.format('%3d %2d %2.3f %s', deg, min, msec/1000, pos);
397 function resolver.LOC_tostring(rr) -- - - - - - - - - - - - - LOC_tostring
401 for k,name in pairs { 'size', 'horiz_pre', 'vert_pre', 'latitude', 'longitude', 'altitude' } do
402 append(t, string.format('%4s%-10s: %12.0f\n', '', name, rr.loc[name]));
406 append(t, string.format(
407 '%s %s %.2fm %.2fm %.2fm %.2fm',
408 LOC_tostring_degrees (rr.loc.latitude, 'N', 'S'),
409 LOC_tostring_degrees (rr.loc.longitude, 'E', 'W'),
410 (rr.loc.altitude - 10000000) / 100,
412 rr.loc.horiz_pre / 100,
413 rr.loc.vert_pre / 100
416 return table.concat(t);
420 function resolver:NS(rr) -- - - - - - - - - - - - - - - - - - - - - - - NS
425 function resolver:SOA(rr) -- - - - - - - - - - - - - - - - - - - - - - SOA
429 function resolver:SRV(rr) -- - - - - - - - - - - - - - - - - - - - - - SRV
431 rr.srv.priority = self:word();
432 rr.srv.weight = self:word();
433 rr.srv.port = self:word();
434 rr.srv.target = self:name();
438 function SRV_tostring(rr) -- - - - - - - - - - - - - - - - - - SRV_tostring
440 return string.format( '%5d %5d %5d %s', s.priority, s.weight, s.port, s.target );
444 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
445 rr.txt = self:sub (rr.rdlength);
449 function resolver:rr() -- - - - - - - - - - - - - - - - - - - - - - - - rr
451 setmetatable(rr, rr_metatable);
452 rr.name = self:name(self);
453 rr.type = dns.type[self:word()] or rr.type;
454 rr.class = dns.class[self:word()] or rr.class;
455 rr.ttl = 0x10000*self:word() + self:word();
456 rr.rdlength = self:word();
459 rr.tod = self.time + 30;
461 rr.tod = self.time + rr.ttl;
464 local remember = self.offset;
465 local rr_parser = self[dns.type[rr.type]];
466 if rr_parser then rr_parser(self, rr); end
467 self.offset = remember;
468 rr.rdata = self:sub(rr.rdlength);
473 function resolver:rrs (count) -- - - - - - - - - - - - - - - - - - - - - rrs
475 for i = 1,count do append(rrs, self:rr()); end
480 function resolver:decode(packet, force) -- - - - - - - - - - - - - - decode
481 self.packet, self.offset = packet, 1;
482 local header = self:header(force);
483 if not header then return nil; end
484 local response = { header = header };
486 response.question = {};
487 local offset = self.offset;
488 for i = 1,response.header.qdcount do
489 append(response.question, self:question());
491 response.question.raw = string.sub(self.packet, offset, self.offset - 1);
494 if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
499 response.answer = self:rrs(response.header.ancount);
500 response.authority = self:rrs(response.header.nscount);
501 response.additional = self:rrs(response.header.arcount);
507 -- socket layer -------------------------------------------------- socket layer
510 resolver.delays = { 1, 3 };
513 function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
514 self.server = self.server or {};
515 append(self.server, address);
519 function resolver:setnameserver(address) -- - - - - - - - - - setnameserver
521 self:addnameserver(address);
525 function resolver:adddefaultnameservers() -- - - - - adddefaultnameservers
528 for _, server in ipairs(windows.get_nameservers()) do
529 self:addnameserver(server);
532 if not self.server or #self.server == 0 then
533 -- TODO log warning about no nameservers, adding opendns servers as fallback
534 self:addnameserver("208.67.222.222");
535 self:addnameserver("208.67.220.220") ;
538 local resolv_conf = io.open("/etc/resolv.conf");
540 for line in resolv_conf:lines() do
541 local address = line:gsub("#.*$", ""):match('^%s*nameserver%s+(%d+%.%d+%.%d+%.%d+)%s*$');
542 if address then self:addnameserver(address) end
545 if not self.server or #self.server == 0 then
546 -- TODO log warning about no nameservers, adding localhost as the default nameserver
547 self:addnameserver("127.0.0.1");
553 function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket
554 self.socket = self.socket or {};
555 self.socketset = self.socketset or {};
557 local sock = self.socket[servernum];
558 if sock then return sock; end
561 if self.socket_wrapper then sock = self.socket_wrapper(sock, self); end
563 -- todo: attempt to use a random port, fallback to 0
564 sock:setsockname('*', 0);
565 sock:setpeername(self.server[servernum], 53);
566 self.socket[servernum] = sock;
567 self.socketset[sock] = servernum;
571 function resolver:voidsocket(sock)
572 if self.socket[sock] then
573 self.socketset[self.socket[sock]] = nil;
574 self.socket[sock] = nil;
575 elseif self.socketset[sock] then
576 self.socket[self.socketset[sock]] = nil;
577 self.socketset[sock] = nil;
581 function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
582 self.socket_wrapper = func;
586 function resolver:closeall () -- - - - - - - - - - - - - - - - - - closeall
587 for i,sock in ipairs(self.socket) do
588 self.socket[i] = nil;
589 self.socketset[sock] = nil;
595 function resolver:remember(rr, type) -- - - - - - - - - - - - - - remember
596 --print ('remember', type, rr.class, rr.type, rr.name)
600 local all = get(self.cache, rr.class, '*', rr.name);
601 --print('remember all', all);
602 if all then append(all, rr); end
605 self.cache = self.cache or setmetatable({}, cache_metatable);
606 local rrs = get(self.cache, rr.class, type, rr.name) or
607 set(self.cache, rr.class, type, rr.name, setmetatable({}, rrs_metatable));
610 if type == 'MX' then self.unsorted[rrs] = true; end
614 local function comp_mx(a, b) -- - - - - - - - - - - - - - - - - - - comp_mx
615 return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
619 function resolver:peek (qname, qtype, qclass) -- - - - - - - - - - - - peek
620 qname, qtype, qclass = standardize(qname, qtype, qclass);
621 local rrs = get(self.cache, qclass, qtype, qname);
622 if not rrs then return nil; end
623 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
624 set(self.cache, qclass, qtype, qname, nil);
627 if self.unsorted[rrs] then table.sort (rrs, comp_mx); end
632 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
633 if soft == 'soft' then
634 self.time = socket.gettime();
635 for class,types in pairs(self.cache or {}) do
636 for type,names in pairs(types) do
637 for name,rrs in pairs(names) do
638 prune(rrs, self.time, 'soft')
642 else self.cache = {}; end
646 function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
647 qname, qtype, qclass = standardize(qname, qtype, qclass)
649 if not self.server then self:adddefaultnameservers(); end
651 local question = encodeQuestion(qname, qtype, qclass);
652 local peek = self:peek (qname, qtype, qclass);
653 if peek then return peek; end
655 local header, id = encodeHeader();
656 --print ('query id', id, qclass, qtype, qname)
658 packet = header..question,
659 server = self.best_server,
661 retry = socket.gettime() + self.delays[1]
664 -- remember the query
665 self.active[id] = self.active[id] or {};
666 self.active[id][question] = o;
668 -- remember which coroutine wants the answer
669 local co = coroutine.running();
671 set(self.wanted, qclass, qtype, qname, co, true);
672 --set(self.yielded, co, qclass, qtype, qname, true);
675 self:getsocket (o.server):send (o.packet)
678 function resolver:servfail(sock)
679 -- Resend all queries for this server
681 local num = self.socketset[sock]
683 -- Socket is dead now
684 self:voidsocket(sock);
686 -- Find all requests to the down server, and retry on the next server
687 self.time = socket.gettime();
688 for id,queries in pairs(self.active) do
689 for question,o in pairs(queries) do
690 if o.server == num then -- This request was to the broken server
691 o.server = o.server + 1 -- Use next server
692 if o.server > #self.server then
696 o.retries = (o.retries or 0) + 1;
697 if o.retries >= #self.server then
699 queries[question] = nil;
701 local _a = self:getsocket(o.server);
702 if _a then _a:send(o.packet); end
708 if num == self.best_server then
709 self.best_server = self.best_server + 1;
710 if self.best_server > #self.server then
711 -- Exhausted all servers, try first again
712 self.best_server = 1;
717 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
718 --print('receive'); print(self.socket);
719 self.time = socket.gettime();
720 rset = rset or self.socket;
723 for i,sock in pairs(rset) do
725 if self.socketset[sock] then
726 local packet = sock:receive();
728 response = self:decode(packet);
729 if response and self.active[response.header.id]
730 and self.active[response.header.id][response.question.raw] then
731 --print('received response');
732 --self.print(response);
734 for j,rr in pairs(response.answer) do
735 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
736 self:remember(rr, response.question[1].type)
741 local queries = self.active[response.header.id];
742 queries[response.question.raw] = nil;
744 if not next(queries) then self.active[response.header.id] = nil; end
745 if not next(self.active) then self:closeall(); end
747 -- was the query on the wanted list?
748 local q = response.question;
749 local cos = get(self.wanted, q.class, q.type, q.name);
751 for co in pairs(cos) do
752 set(self.yielded, co, q.class, q.type, q.name, nil);
753 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
755 set(self.wanted, q.class, q.type, q.name, nil);
766 function resolver:feed(sock, packet)
767 --print('receive'); print(self.socket);
768 self.time = socket.gettime();
770 local response = self:decode(packet);
771 if response and self.active[response.header.id]
772 and self.active[response.header.id][response.question.raw] then
773 --print('received response');
774 --self.print(response);
776 for j,rr in pairs(response.answer) do
777 self:remember(rr, response.question[1].type);
781 local queries = self.active[response.header.id];
782 queries[response.question.raw] = nil;
783 if not next(queries) then self.active[response.header.id] = nil; end
784 if not next(self.active) then self:closeall(); end
786 -- was the query on the wanted list?
787 local q = response.question[1];
789 local cos = get(self.wanted, q.class, q.type, q.name);
791 for co in pairs(cos) do
792 set(self.yielded, co, q.class, q.type, q.name, nil);
793 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
795 set(self.wanted, q.class, q.type, q.name, nil);
803 function resolver:cancel(data)
804 local cos = get(self.wanted, unpack(data, 1, 3));
810 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
812 while self:receive() do end
813 if not next(self.active) then return nil; end
815 self.time = socket.gettime();
816 for id,queries in pairs(self.active) do
817 for question,o in pairs(queries) do
818 if self.time >= o.retry then
820 o.server = o.server + 1;
821 if o.server > #self.server then
823 o.delay = o.delay + 1;
826 if o.delay > #self.delays then
828 queries[question] = nil;
829 if not next(queries) then self.active[id] = nil; end
830 if not next(self.active) then return nil; end
832 --print('retry', o.server, o.delay);
833 local _a = self.socket[o.server];
834 if _a then _a:send(o.packet); end
835 o.retry = self.time + self.delays[o.delay];
841 if next(self.active) then return true; end
846 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
847 self:query (qname, qtype, qclass)
848 while self:pulse() do socket.select(self.socket, nil, 4); end
850 return self:peek(qname, qtype, qclass);
853 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
854 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
858 --print ---------------------------------------------------------------- print
861 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
862 qr = { [0]='query', 'response' },
863 opcode = { [0]='query', 'inverse query', 'server status request' },
864 aa = { [0]='non-authoritative', 'authoritative' },
865 tc = { [0]='complete', 'truncated' },
866 rd = { [0]='recursion not desired', 'recursion desired' },
867 ra = { [0]='recursion not available', 'recursion available' },
868 z = { [0]='(reserved)' },
869 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
876 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
877 return (hints[s] and hints[s][p[s]]) or '';
881 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
882 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
883 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
884 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
887 for i,question in ipairs(response.question) do
888 print(string.format ('question[%i].name ', i), question.name);
889 print(string.format ('question[%i].type ', i), question.type);
890 print(string.format ('question[%i].class ', i), question.class);
893 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
895 for s,s in pairs({'answer', 'authority', 'additional'}) do
896 for i,rr in pairs(response[s]) do
897 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
898 tmp = string.format('%s[%i].%s', s, i, t);
899 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
901 for j,t in pairs(rr) do
902 if not common[j] then
903 tmp = string.format('%s[%i].%s', s, i, j);
904 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
912 -- module api ------------------------------------------------------ module api
915 local function resolve(func, ...) -- - - - - - - - - - - - - - resolver_get
916 return func(dns._resolver, ...);
920 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
921 -- this function seems to be redundant with resolver.new ()
923 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {}, best_server = 1 };
924 setmetatable (r, resolver);
925 setmetatable (r.cache, cache_metatable);
926 setmetatable (r.unsorted, { __mode = 'kv' });
931 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
932 return resolve(resolver.lookup, ...);
936 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
937 return resolve(resolver.purge, ...);
940 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
941 return resolve(resolver.peek, ...);
945 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
946 return resolve(resolver.query, ...);
949 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - feed
950 return resolve(resolver.feed, ...);
953 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
954 return resolve(resolver.cancel, ...);
957 function dns:socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
958 return resolve(resolver.socket_wrapper_set, ...);
961 dns._resolver = dns.resolver();