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 timer = require "util.timer";
21 local _, windows = pcall(require, "util.windows");
22 local is_windows = (_ and windows) or os.getenv("WINDIR");
24 local coroutine, io, math, string, table =
25 coroutine, io, math, string, table;
27 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack =
28 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack;
30 local get, set = ztact.get, ztact.set;
32 local default_timeout = 15;
34 -------------------------------------------------- module dns
39 -- dns type & class codes ------------------------------ dns type & class codes
42 local append = table.insert
45 local function highbyte(i) -- - - - - - - - - - - - - - - - - - - highbyte
46 return (i-(i%0x100))/0x100;
50 local function augment (t) -- - - - - - - - - - - - - - - - - - - - augment
52 for i,s in pairs(t) do
55 a[string.lower(s)] = s;
61 local function encode (t) -- - - - - - - - - - - - - - - - - - - - - encode
63 for i,s in pairs(t) do
64 local word = string.char(highbyte(i), i%0x100);
67 code[string.lower(s)] = word;
74 'A', 'NS', 'MD', 'MF', 'CNAME', 'SOA', 'MB', 'MG', 'MR', 'NULL', 'WKS',
75 'PTR', 'HINFO', 'MINFO', 'MX', 'TXT',
76 [ 28] = 'AAAA', [ 29] = 'LOC', [ 33] = 'SRV',
77 [252] = 'AXFR', [253] = 'MAILB', [254] = 'MAILA', [255] = '*' };
80 dns.classes = { 'IN', 'CS', 'CH', 'HS', [255] = '*' };
83 dns.type = augment (dns.types);
84 dns.class = augment (dns.classes);
85 dns.typecode = encode (dns.types);
86 dns.classcode = encode (dns.classes);
90 local function standardize(qname, qtype, qclass) -- - - - - - - standardize
91 if string.byte(qname, -1) ~= 0x2E then qname = qname..'.'; end
92 qname = string.lower(qname);
93 return qname, dns.type[qtype or 'A'], dns.class[qclass or 'IN'];
97 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune
98 time = time or socket.gettime();
99 for i,rr in pairs(rrs) do
101 -- rr.tod = rr.tod - 50 -- accelerated decripitude
102 rr.ttl = math.floor(rr.tod - time);
104 table.remove(rrs, i);
105 return prune(rrs, time, soft); -- Re-iterate
107 elseif soft == 'soft' then -- What is this? I forget!
115 -- metatables & co. ------------------------------------------ metatables & co.
119 resolver.__index = resolver;
121 resolver.timeout = default_timeout;
126 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
127 function rr_metatable.__tostring(rr)
128 local s0 = string.format('%2s %-5s %6i %-28s', rr.class, rr.type, rr.ttl, rr.name);
130 if rr.type == 'A' then
132 elseif rr.type == 'MX' then
133 s1 = string.format(' %2i %s', rr.pref, rr.mx);
134 elseif rr.type == 'CNAME' then
136 elseif rr.type == 'LOC' then
137 s1 = ' '..resolver.LOC_tostring(rr);
138 elseif rr.type == 'NS' then
140 elseif rr.type == 'SRV' then
141 s1 = ' '..SRV_tostring(rr);
142 elseif rr.type == 'TXT' then
145 s1 = ' <UNKNOWN RDATA TYPE>';
151 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
152 function rrs_metatable.__tostring(rrs)
154 for i,rr in pairs(rrs) do
155 append(t, tostring(rr)..'\n');
157 return table.concat(t);
161 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable
162 function cache_metatable.__tostring(cache)
163 local time = socket.gettime();
165 for class,types in pairs(cache) do
166 for type,names in pairs(types) do
167 for name,rrs in pairs(names) do
169 append(t, tostring(rrs));
173 return table.concat(t);
177 function resolver:new() -- - - - - - - - - - - - - - - - - - - - - resolver
178 local r = { active = {}, cache = {}, unsorted = {} };
179 setmetatable(r, resolver);
180 setmetatable(r.cache, cache_metatable);
181 setmetatable(r.unsorted, { __mode = 'kv' });
186 -- packet layer -------------------------------------------------- packet layer
189 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
190 math.randomseed(math.floor(10000*socket.gettime()));
191 dns.random = math.random;
192 return dns.random(...);
196 local function encodeHeader(o) -- - - - - - - - - - - - - - - encodeHeader
198 o.id = o.id or dns.random(0, 0xffff); -- 16b (random) id
200 o.rd = o.rd or 1; -- 1b 1 recursion desired
201 o.tc = o.tc or 0; -- 1b 1 truncated response
202 o.aa = o.aa or 0; -- 1b 1 authoritative response
203 o.opcode = o.opcode or 0; -- 4b 0 query
205 -- 2 server status request
207 o.qr = o.qr or 0; -- 1b 0 query, 1 response
209 o.rcode = o.rcode or 0; -- 4b 0 no error
216 o.z = o.z or 0; -- 3b 0 resvered
217 o.ra = o.ra or 0; -- 1b 1 recursion available
219 o.qdcount = o.qdcount or 1; -- 16b number of question RRs
220 o.ancount = o.ancount or 0; -- 16b number of answers RRs
221 o.nscount = o.nscount or 0; -- 16b number of nameservers RRs
222 o.arcount = o.arcount or 0; -- 16b number of additional RRs
224 -- string.char() rounds, so prevent roundup with -0.4999
225 local header = string.char(
226 highbyte(o.id), o.id %0x100,
227 o.rd + 2*o.tc + 4*o.aa + 8*o.opcode + 128*o.qr,
228 o.rcode + 16*o.z + 128*o.ra,
229 highbyte(o.qdcount), o.qdcount %0x100,
230 highbyte(o.ancount), o.ancount %0x100,
231 highbyte(o.nscount), o.nscount %0x100,
232 highbyte(o.arcount), o.arcount %0x100
239 local function encodeName(name) -- - - - - - - - - - - - - - - - encodeName
241 for part in string.gmatch(name, '[^.]+') do
242 append(t, string.char(string.len(part)));
245 append(t, string.char(0));
246 return table.concat(t);
250 local function encodeQuestion(qname, qtype, qclass) -- - - - encodeQuestion
251 qname = encodeName(qname);
252 qtype = dns.typecode[qtype or 'a'];
253 qclass = dns.classcode[qclass or 'in'];
254 return qname..qtype..qclass;
258 function resolver:byte(len) -- - - - - - - - - - - - - - - - - - - - - byte
260 local offset = self.offset;
261 local last = offset + len - 1;
262 if last > #self.packet then
263 error(string.format('out of bounds: %i>%i', last, #self.packet));
265 self.offset = offset + len;
266 return string.byte(self.packet, offset, last);
270 function resolver:word() -- - - - - - - - - - - - - - - - - - - - - - word
271 local b1, b2 = self:byte(2);
272 return 0x100*b1 + b2;
276 function resolver:dword () -- - - - - - - - - - - - - - - - - - - - - dword
277 local b1, b2, b3, b4 = self:byte(4);
278 --print('dword', b1, b2, b3, b4);
279 return 0x1000000*b1 + 0x10000*b2 + 0x100*b3 + b4;
283 function resolver:sub(len) -- - - - - - - - - - - - - - - - - - - - - - sub
285 local s = string.sub(self.packet, self.offset, self.offset + len - 1);
286 self.offset = self.offset + len;
291 function resolver:header(force) -- - - - - - - - - - - - - - - - - - header
292 local id = self:word();
293 --print(string.format(':header id %x', id));
294 if not self.active[id] and not force then return nil; end
296 local h = { id = id };
298 local b1, b2 = self:byte(2);
310 h.qdcount = self:word();
311 h.ancount = self:word();
312 h.nscount = self:word();
313 h.arcount = self:word();
315 for k,v in pairs(h) do h[k] = v-v%1; end
321 function resolver:name() -- - - - - - - - - - - - - - - - - - - - - - name
322 local remember, pointers = nil, 0;
323 local len = self:byte();
326 if len >= 0xc0 then -- name is "compressed"
327 pointers = pointers + 1;
328 if pointers >= 20 then error('dns error: 20 pointers'); end;
329 local offset = ((len-0xc0)*0x100) + self:byte();
330 remember = remember or self.offset;
331 self.offset = offset + 1; -- +1 for lua
332 else -- name is not compressed
333 append(n, self:sub(len)..'.');
337 self.offset = remember or self.offset;
338 return table.concat(n);
342 function resolver:question() -- - - - - - - - - - - - - - - - - - question
344 q.name = self:name();
345 q.type = dns.type[self:word()];
346 q.class = dns.class[self:word()];
351 function resolver:A(rr) -- - - - - - - - - - - - - - - - - - - - - - - - A
352 local b1, b2, b3, b4 = self:byte(4);
353 rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
357 function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
358 rr.cname = self:name();
362 function resolver:MX(rr) -- - - - - - - - - - - - - - - - - - - - - - - MX
363 rr.pref = self:word();
368 function resolver:LOC_nibble_power() -- - - - - - - - - - LOC_nibble_power
369 local b = self:byte();
370 --print('nibbles', ((b-(b%0x10))/0x10), (b%0x10));
371 return ((b-(b%0x10))/0x10) * (10^(b%0x10));
375 function resolver:LOC(rr) -- - - - - - - - - - - - - - - - - - - - - - LOC
376 rr.version = self:byte();
377 if rr.version == 0 then
378 rr.loc = rr.loc or {};
379 rr.loc.size = self:LOC_nibble_power();
380 rr.loc.horiz_pre = self:LOC_nibble_power();
381 rr.loc.vert_pre = self:LOC_nibble_power();
382 rr.loc.latitude = self:dword();
383 rr.loc.longitude = self:dword();
384 rr.loc.altitude = self:dword();
389 local function LOC_tostring_degrees(f, pos, neg) -- - - - - - - - - - - - -
391 if f < 0 then pos = neg; f = -f; end
392 local deg, min, msec;
397 return string.format('%3d %2d %2.3f %s', deg, min, msec/1000, pos);
401 function resolver.LOC_tostring(rr) -- - - - - - - - - - - - - LOC_tostring
405 for k,name in pairs { 'size', 'horiz_pre', 'vert_pre', 'latitude', 'longitude', 'altitude' } do
406 append(t, string.format('%4s%-10s: %12.0f\n', '', name, rr.loc[name]));
410 append(t, string.format(
411 '%s %s %.2fm %.2fm %.2fm %.2fm',
412 LOC_tostring_degrees (rr.loc.latitude, 'N', 'S'),
413 LOC_tostring_degrees (rr.loc.longitude, 'E', 'W'),
414 (rr.loc.altitude - 10000000) / 100,
416 rr.loc.horiz_pre / 100,
417 rr.loc.vert_pre / 100
420 return table.concat(t);
424 function resolver:NS(rr) -- - - - - - - - - - - - - - - - - - - - - - - NS
429 function resolver:SOA(rr) -- - - - - - - - - - - - - - - - - - - - - - SOA
433 function resolver:SRV(rr) -- - - - - - - - - - - - - - - - - - - - - - SRV
435 rr.srv.priority = self:word();
436 rr.srv.weight = self:word();
437 rr.srv.port = self:word();
438 rr.srv.target = self:name();
441 function resolver:PTR(rr)
442 rr.ptr = self:name();
445 function SRV_tostring(rr) -- - - - - - - - - - - - - - - - - - SRV_tostring
447 return string.format( '%5d %5d %5d %s', s.priority, s.weight, s.port, s.target );
451 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
452 rr.txt = self:sub (rr.rdlength);
456 function resolver:rr() -- - - - - - - - - - - - - - - - - - - - - - - - rr
458 setmetatable(rr, rr_metatable);
459 rr.name = self:name(self);
460 rr.type = dns.type[self:word()] or rr.type;
461 rr.class = dns.class[self:word()] or rr.class;
462 rr.ttl = 0x10000*self:word() + self:word();
463 rr.rdlength = self:word();
466 rr.tod = self.time + 30;
468 rr.tod = self.time + rr.ttl;
471 local remember = self.offset;
472 local rr_parser = self[dns.type[rr.type]];
473 if rr_parser then rr_parser(self, rr); end
474 self.offset = remember;
475 rr.rdata = self:sub(rr.rdlength);
480 function resolver:rrs (count) -- - - - - - - - - - - - - - - - - - - - - rrs
482 for i = 1,count do append(rrs, self:rr()); end
487 function resolver:decode(packet, force) -- - - - - - - - - - - - - - decode
488 self.packet, self.offset = packet, 1;
489 local header = self:header(force);
490 if not header then return nil; end
491 local response = { header = header };
493 response.question = {};
494 local offset = self.offset;
495 for i = 1,response.header.qdcount do
496 append(response.question, self:question());
498 response.question.raw = string.sub(self.packet, offset, self.offset - 1);
501 if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
506 response.answer = self:rrs(response.header.ancount);
507 response.authority = self:rrs(response.header.nscount);
508 response.additional = self:rrs(response.header.arcount);
514 -- socket layer -------------------------------------------------- socket layer
517 resolver.delays = { 1, 3 };
520 function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
521 self.server = self.server or {};
522 append(self.server, address);
526 function resolver:setnameserver(address) -- - - - - - - - - - setnameserver
528 self:addnameserver(address);
532 function resolver:adddefaultnameservers() -- - - - - adddefaultnameservers
535 for _, server in ipairs(windows.get_nameservers()) do
536 self:addnameserver(server);
539 if not self.server or #self.server == 0 then
540 -- TODO log warning about no nameservers, adding opendns servers as fallback
541 self:addnameserver("208.67.222.222");
542 self:addnameserver("208.67.220.220");
545 local resolv_conf = io.open("/etc/resolv.conf");
547 for line in resolv_conf:lines() do
548 line = line:gsub("#.*$", "")
549 :match('^%s*nameserver%s+(.*)%s*$');
551 line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address)
552 self:addnameserver(address)
557 if not self.server or #self.server == 0 then
558 -- TODO log warning about no nameservers, adding localhost as the default nameserver
559 self:addnameserver("127.0.0.1");
565 function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket
566 self.socket = self.socket or {};
567 self.socketset = self.socketset or {};
569 local sock = self.socket[servernum];
570 if sock then return sock; end
573 if self.socket_wrapper then sock = self.socket_wrapper(sock, self); end
575 -- todo: attempt to use a random port, fallback to 0
576 sock:setsockname('*', 0);
577 sock:setpeername(self.server[servernum], 53);
578 self.socket[servernum] = sock;
579 self.socketset[sock] = servernum;
583 function resolver:voidsocket(sock)
584 if self.socket[sock] then
585 self.socketset[self.socket[sock]] = nil;
586 self.socket[sock] = nil;
587 elseif self.socketset[sock] then
588 self.socket[self.socketset[sock]] = nil;
589 self.socketset[sock] = nil;
593 function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
594 self.socket_wrapper = func;
598 function resolver:closeall () -- - - - - - - - - - - - - - - - - - closeall
599 for i,sock in ipairs(self.socket) do
600 self.socket[i] = nil;
601 self.socketset[sock] = nil;
607 function resolver:remember(rr, type) -- - - - - - - - - - - - - - remember
608 --print ('remember', type, rr.class, rr.type, rr.name)
609 local qname, qtype, qclass = standardize(rr.name, rr.type, rr.class);
613 local all = get(self.cache, qclass, '*', qname);
614 --print('remember all', all);
615 if all then append(all, rr); end
618 self.cache = self.cache or setmetatable({}, cache_metatable);
619 local rrs = get(self.cache, qclass, type, qname) or
620 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
623 if type == 'MX' then self.unsorted[rrs] = true; end
627 local function comp_mx(a, b) -- - - - - - - - - - - - - - - - - - - comp_mx
628 return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
632 function resolver:peek (qname, qtype, qclass) -- - - - - - - - - - - - peek
633 qname, qtype, qclass = standardize(qname, qtype, qclass);
634 local rrs = get(self.cache, qclass, qtype, qname);
635 if not rrs then return nil; end
636 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
637 set(self.cache, qclass, qtype, qname, nil);
640 if self.unsorted[rrs] then table.sort (rrs, comp_mx); end
645 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
646 if soft == 'soft' then
647 self.time = socket.gettime();
648 for class,types in pairs(self.cache or {}) do
649 for type,names in pairs(types) do
650 for name,rrs in pairs(names) do
651 prune(rrs, self.time, 'soft')
655 else self.cache = {}; end
659 function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
660 qname, qtype, qclass = standardize(qname, qtype, qclass)
662 if not self.server then self:adddefaultnameservers(); end
664 local question = encodeQuestion(qname, qtype, qclass);
665 local peek = self:peek (qname, qtype, qclass);
666 if peek then return peek; end
668 local header, id = encodeHeader();
669 --print ('query id', id, qclass, qtype, qname)
671 packet = header..question,
672 server = self.best_server,
674 retry = socket.gettime() + self.delays[1]
677 -- remember the query
678 self.active[id] = self.active[id] or {};
679 self.active[id][question] = o;
681 -- remember which coroutine wants the answer
682 local co = coroutine.running();
684 set(self.wanted, qclass, qtype, qname, co, true);
685 --set(self.yielded, co, qclass, qtype, qname, true);
688 local conn = self:getsocket(o.server)
691 if timer and self.timeout then
692 local num_servers = #self.server;
694 timer.add_task(self.timeout, function ()
695 if get(self.wanted, qclass, qtype, qname, co) then
696 if i < num_servers then
699 o.server = self.best_server;
700 conn = self:getsocket(o.server);
704 -- Tried everything, failed
705 self:cancel(qclass, qtype, qname, co, true);
712 function resolver:servfail(sock)
713 -- Resend all queries for this server
715 local num = self.socketset[sock]
717 -- Socket is dead now
718 self:voidsocket(sock);
720 -- Find all requests to the down server, and retry on the next server
721 self.time = socket.gettime();
722 for id,queries in pairs(self.active) do
723 for question,o in pairs(queries) do
724 if o.server == num then -- This request was to the broken server
725 o.server = o.server + 1 -- Use next server
726 if o.server > #self.server then
730 o.retries = (o.retries or 0) + 1;
731 if o.retries >= #self.server then
733 queries[question] = nil;
735 local _a = self:getsocket(o.server);
736 if _a then _a:send(o.packet); end
742 if num == self.best_server then
743 self.best_server = self.best_server + 1;
744 if self.best_server > #self.server then
745 -- Exhausted all servers, try first again
746 self.best_server = 1;
751 function resolver:settimeout(seconds)
752 self.timeout = seconds;
755 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
756 --print('receive'); print(self.socket);
757 self.time = socket.gettime();
758 rset = rset or self.socket;
761 for i,sock in pairs(rset) do
763 if self.socketset[sock] then
764 local packet = sock:receive();
766 response = self:decode(packet);
767 if response and self.active[response.header.id]
768 and self.active[response.header.id][response.question.raw] then
769 --print('received response');
770 --self.print(response);
772 for j,rr in pairs(response.answer) do
773 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
774 self:remember(rr, response.question[1].type)
779 local queries = self.active[response.header.id];
780 queries[response.question.raw] = nil;
782 if not next(queries) then self.active[response.header.id] = nil; end
783 if not next(self.active) then self:closeall(); end
785 -- was the query on the wanted list?
786 local q = response.question[1];
787 local cos = get(self.wanted, q.class, q.type, q.name);
789 for co in pairs(cos) do
790 set(self.yielded, co, q.class, q.type, q.name, nil);
791 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
793 set(self.wanted, q.class, q.type, q.name, nil);
804 function resolver:feed(sock, packet, force)
805 --print('receive'); print(self.socket);
806 self.time = socket.gettime();
808 local response = self:decode(packet, force);
809 if response and self.active[response.header.id]
810 and self.active[response.header.id][response.question.raw] then
811 --print('received response');
812 --self.print(response);
814 for j,rr in pairs(response.answer) do
815 self:remember(rr, response.question[1].type);
819 local queries = self.active[response.header.id];
820 queries[response.question.raw] = nil;
821 if not next(queries) then self.active[response.header.id] = nil; end
822 if not next(self.active) then self:closeall(); end
824 -- was the query on the wanted list?
825 local q = response.question[1];
827 local cos = get(self.wanted, q.class, q.type, q.name);
829 for co in pairs(cos) do
830 set(self.yielded, co, q.class, q.type, q.name, nil);
831 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
833 set(self.wanted, q.class, q.type, q.name, nil);
841 function resolver:cancel(qclass, qtype, qname, co, call_handler)
842 local cos = get(self.wanted, qclass, qtype, qname);
845 coroutine.resume(co);
851 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
853 while self:receive() do end
854 if not next(self.active) then return nil; end
856 self.time = socket.gettime();
857 for id,queries in pairs(self.active) do
858 for question,o in pairs(queries) do
859 if self.time >= o.retry then
861 o.server = o.server + 1;
862 if o.server > #self.server then
864 o.delay = o.delay + 1;
867 if o.delay > #self.delays then
869 queries[question] = nil;
870 if not next(queries) then self.active[id] = nil; end
871 if not next(self.active) then return nil; end
873 --print('retry', o.server, o.delay);
874 local _a = self.socket[o.server];
875 if _a then _a:send(o.packet); end
876 o.retry = self.time + self.delays[o.delay];
882 if next(self.active) then return true; end
887 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
888 self:query (qname, qtype, qclass)
889 while self:pulse() do
891 for i, s in ipairs(self.socket) do
894 socket.select(recvt, nil, 4)
897 return self:peek(qname, qtype, qclass);
900 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
901 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
905 --print ---------------------------------------------------------------- print
908 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
909 qr = { [0]='query', 'response' },
910 opcode = { [0]='query', 'inverse query', 'server status request' },
911 aa = { [0]='non-authoritative', 'authoritative' },
912 tc = { [0]='complete', 'truncated' },
913 rd = { [0]='recursion not desired', 'recursion desired' },
914 ra = { [0]='recursion not available', 'recursion available' },
915 z = { [0]='(reserved)' },
916 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
923 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
924 return (hints[s] and hints[s][p[s]]) or '';
928 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
929 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
930 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
931 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
934 for i,question in ipairs(response.question) do
935 print(string.format ('question[%i].name ', i), question.name);
936 print(string.format ('question[%i].type ', i), question.type);
937 print(string.format ('question[%i].class ', i), question.class);
940 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
942 for s,s in pairs({'answer', 'authority', 'additional'}) do
943 for i,rr in pairs(response[s]) do
944 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
945 tmp = string.format('%s[%i].%s', s, i, t);
946 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
948 for j,t in pairs(rr) do
949 if not common[j] then
950 tmp = string.format('%s[%i].%s', s, i, j);
951 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
959 -- module api ------------------------------------------------------ module api
962 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
963 -- this function seems to be redundant with resolver.new ()
965 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {}, best_server = 1 };
966 setmetatable (r, resolver);
967 setmetatable (r.cache, cache_metatable);
968 setmetatable (r.unsorted, { __mode = 'kv' });
972 local _resolver = dns.resolver();
973 dns._resolver = _resolver;
975 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
976 return _resolver:lookup(...);
979 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
980 return _resolver:purge(...);
983 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
984 return _resolver:peek(...);
987 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
988 return _resolver:query(...);
991 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - - feed
992 return _resolver:feed(...);
995 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
996 return _resolver:cancel(...);
999 function dns.settimeout(...)
1000 return _resolver:settimeout(...);
1003 function dns.socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
1004 return _resolver:socket_wrapper_set(...);