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 dns_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;
125 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
126 function rr_metatable.__tostring(rr)
127 local s0 = string.format('%2s %-5s %6i %-28s', rr.class, rr.type, rr.ttl, rr.name);
129 if rr.type == 'A' then
131 elseif rr.type == 'MX' then
132 s1 = string.format(' %2i %s', rr.pref, rr.mx);
133 elseif rr.type == 'CNAME' then
135 elseif rr.type == 'LOC' then
136 s1 = ' '..resolver.LOC_tostring(rr);
137 elseif rr.type == 'NS' then
139 elseif rr.type == 'SRV' then
140 s1 = ' '..SRV_tostring(rr);
141 elseif rr.type == 'TXT' then
144 s1 = ' <UNKNOWN RDATA TYPE>';
150 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
151 function rrs_metatable.__tostring(rrs)
153 for i,rr in pairs(rrs) do
154 append(t, tostring(rr)..'\n');
156 return table.concat(t);
160 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable
161 function cache_metatable.__tostring(cache)
162 local time = socket.gettime();
164 for class,types in pairs(cache) do
165 for type,names in pairs(types) do
166 for name,rrs in pairs(names) do
168 append(t, tostring(rrs));
172 return table.concat(t);
176 function resolver:new() -- - - - - - - - - - - - - - - - - - - - - resolver
177 local r = { active = {}, cache = {}, unsorted = {} };
178 setmetatable(r, resolver);
179 setmetatable(r.cache, cache_metatable);
180 setmetatable(r.unsorted, { __mode = 'kv' });
185 -- packet layer -------------------------------------------------- packet layer
188 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
189 math.randomseed(math.floor(10000*socket.gettime()));
190 dns.random = math.random;
191 return dns.random(...);
195 local function encodeHeader(o) -- - - - - - - - - - - - - - - encodeHeader
197 o.id = o.id or dns.random(0, 0xffff); -- 16b (random) id
199 o.rd = o.rd or 1; -- 1b 1 recursion desired
200 o.tc = o.tc or 0; -- 1b 1 truncated response
201 o.aa = o.aa or 0; -- 1b 1 authoritative response
202 o.opcode = o.opcode or 0; -- 4b 0 query
204 -- 2 server status request
206 o.qr = o.qr or 0; -- 1b 0 query, 1 response
208 o.rcode = o.rcode or 0; -- 4b 0 no error
215 o.z = o.z or 0; -- 3b 0 resvered
216 o.ra = o.ra or 0; -- 1b 1 recursion available
218 o.qdcount = o.qdcount or 1; -- 16b number of question RRs
219 o.ancount = o.ancount or 0; -- 16b number of answers RRs
220 o.nscount = o.nscount or 0; -- 16b number of nameservers RRs
221 o.arcount = o.arcount or 0; -- 16b number of additional RRs
223 -- string.char() rounds, so prevent roundup with -0.4999
224 local header = string.char(
225 highbyte(o.id), o.id %0x100,
226 o.rd + 2*o.tc + 4*o.aa + 8*o.opcode + 128*o.qr,
227 o.rcode + 16*o.z + 128*o.ra,
228 highbyte(o.qdcount), o.qdcount %0x100,
229 highbyte(o.ancount), o.ancount %0x100,
230 highbyte(o.nscount), o.nscount %0x100,
231 highbyte(o.arcount), o.arcount %0x100
238 local function encodeName(name) -- - - - - - - - - - - - - - - - encodeName
240 for part in string.gmatch(name, '[^.]+') do
241 append(t, string.char(string.len(part)));
244 append(t, string.char(0));
245 return table.concat(t);
249 local function encodeQuestion(qname, qtype, qclass) -- - - - encodeQuestion
250 qname = encodeName(qname);
251 qtype = dns.typecode[qtype or 'a'];
252 qclass = dns.classcode[qclass or 'in'];
253 return qname..qtype..qclass;
257 function resolver:byte(len) -- - - - - - - - - - - - - - - - - - - - - byte
259 local offset = self.offset;
260 local last = offset + len - 1;
261 if last > #self.packet then
262 error(string.format('out of bounds: %i>%i', last, #self.packet));
264 self.offset = offset + len;
265 return string.byte(self.packet, offset, last);
269 function resolver:word() -- - - - - - - - - - - - - - - - - - - - - - word
270 local b1, b2 = self:byte(2);
271 return 0x100*b1 + b2;
275 function resolver:dword () -- - - - - - - - - - - - - - - - - - - - - dword
276 local b1, b2, b3, b4 = self:byte(4);
277 --print('dword', b1, b2, b3, b4);
278 return 0x1000000*b1 + 0x10000*b2 + 0x100*b3 + b4;
282 function resolver:sub(len) -- - - - - - - - - - - - - - - - - - - - - - sub
284 local s = string.sub(self.packet, self.offset, self.offset + len - 1);
285 self.offset = self.offset + len;
290 function resolver:header(force) -- - - - - - - - - - - - - - - - - - header
291 local id = self:word();
292 --print(string.format(':header id %x', id));
293 if not self.active[id] and not force then return nil; end
295 local h = { id = id };
297 local b1, b2 = self:byte(2);
309 h.qdcount = self:word();
310 h.ancount = self:word();
311 h.nscount = self:word();
312 h.arcount = self:word();
314 for k,v in pairs(h) do h[k] = v-v%1; end
320 function resolver:name() -- - - - - - - - - - - - - - - - - - - - - - name
321 local remember, pointers = nil, 0;
322 local len = self:byte();
325 if len >= 0xc0 then -- name is "compressed"
326 pointers = pointers + 1;
327 if pointers >= 20 then error('dns error: 20 pointers'); end;
328 local offset = ((len-0xc0)*0x100) + self:byte();
329 remember = remember or self.offset;
330 self.offset = offset + 1; -- +1 for lua
331 else -- name is not compressed
332 append(n, self:sub(len)..'.');
336 self.offset = remember or self.offset;
337 return table.concat(n);
341 function resolver:question() -- - - - - - - - - - - - - - - - - - question
343 q.name = self:name();
344 q.type = dns.type[self:word()];
345 q.class = dns.class[self:word()];
350 function resolver:A(rr) -- - - - - - - - - - - - - - - - - - - - - - - - A
351 local b1, b2, b3, b4 = self:byte(4);
352 rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
356 function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
357 rr.cname = self:name();
361 function resolver:MX(rr) -- - - - - - - - - - - - - - - - - - - - - - - MX
362 rr.pref = self:word();
367 function resolver:LOC_nibble_power() -- - - - - - - - - - LOC_nibble_power
368 local b = self:byte();
369 --print('nibbles', ((b-(b%0x10))/0x10), (b%0x10));
370 return ((b-(b%0x10))/0x10) * (10^(b%0x10));
374 function resolver:LOC(rr) -- - - - - - - - - - - - - - - - - - - - - - LOC
375 rr.version = self:byte();
376 if rr.version == 0 then
377 rr.loc = rr.loc or {};
378 rr.loc.size = self:LOC_nibble_power();
379 rr.loc.horiz_pre = self:LOC_nibble_power();
380 rr.loc.vert_pre = self:LOC_nibble_power();
381 rr.loc.latitude = self:dword();
382 rr.loc.longitude = self:dword();
383 rr.loc.altitude = self:dword();
388 local function LOC_tostring_degrees(f, pos, neg) -- - - - - - - - - - - - -
390 if f < 0 then pos = neg; f = -f; end
391 local deg, min, msec;
396 return string.format('%3d %2d %2.3f %s', deg, min, msec/1000, pos);
400 function resolver.LOC_tostring(rr) -- - - - - - - - - - - - - LOC_tostring
404 for k,name in pairs { 'size', 'horiz_pre', 'vert_pre', 'latitude', 'longitude', 'altitude' } do
405 append(t, string.format('%4s%-10s: %12.0f\n', '', name, rr.loc[name]));
409 append(t, string.format(
410 '%s %s %.2fm %.2fm %.2fm %.2fm',
411 LOC_tostring_degrees (rr.loc.latitude, 'N', 'S'),
412 LOC_tostring_degrees (rr.loc.longitude, 'E', 'W'),
413 (rr.loc.altitude - 10000000) / 100,
415 rr.loc.horiz_pre / 100,
416 rr.loc.vert_pre / 100
419 return table.concat(t);
423 function resolver:NS(rr) -- - - - - - - - - - - - - - - - - - - - - - - NS
428 function resolver:SOA(rr) -- - - - - - - - - - - - - - - - - - - - - - SOA
432 function resolver:SRV(rr) -- - - - - - - - - - - - - - - - - - - - - - SRV
434 rr.srv.priority = self:word();
435 rr.srv.weight = self:word();
436 rr.srv.port = self:word();
437 rr.srv.target = self:name();
441 function SRV_tostring(rr) -- - - - - - - - - - - - - - - - - - SRV_tostring
443 return string.format( '%5d %5d %5d %s', s.priority, s.weight, s.port, s.target );
447 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
448 rr.txt = self:sub (rr.rdlength);
452 function resolver:rr() -- - - - - - - - - - - - - - - - - - - - - - - - rr
454 setmetatable(rr, rr_metatable);
455 rr.name = self:name(self);
456 rr.type = dns.type[self:word()] or rr.type;
457 rr.class = dns.class[self:word()] or rr.class;
458 rr.ttl = 0x10000*self:word() + self:word();
459 rr.rdlength = self:word();
462 rr.tod = self.time + 30;
464 rr.tod = self.time + rr.ttl;
467 local remember = self.offset;
468 local rr_parser = self[dns.type[rr.type]];
469 if rr_parser then rr_parser(self, rr); end
470 self.offset = remember;
471 rr.rdata = self:sub(rr.rdlength);
476 function resolver:rrs (count) -- - - - - - - - - - - - - - - - - - - - - rrs
478 for i = 1,count do append(rrs, self:rr()); end
483 function resolver:decode(packet, force) -- - - - - - - - - - - - - - decode
484 self.packet, self.offset = packet, 1;
485 local header = self:header(force);
486 if not header then return nil; end
487 local response = { header = header };
489 response.question = {};
490 local offset = self.offset;
491 for i = 1,response.header.qdcount do
492 append(response.question, self:question());
494 response.question.raw = string.sub(self.packet, offset, self.offset - 1);
497 if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
502 response.answer = self:rrs(response.header.ancount);
503 response.authority = self:rrs(response.header.nscount);
504 response.additional = self:rrs(response.header.arcount);
510 -- socket layer -------------------------------------------------- socket layer
513 resolver.delays = { 1, 3 };
516 function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
517 self.server = self.server or {};
518 append(self.server, address);
522 function resolver:setnameserver(address) -- - - - - - - - - - setnameserver
524 self:addnameserver(address);
528 function resolver:adddefaultnameservers() -- - - - - adddefaultnameservers
531 for _, server in ipairs(windows.get_nameservers()) do
532 self:addnameserver(server);
535 if not self.server or #self.server == 0 then
536 -- TODO log warning about no nameservers, adding opendns servers as fallback
537 self:addnameserver("208.67.222.222");
538 self:addnameserver("208.67.220.220");
541 local resolv_conf = io.open("/etc/resolv.conf");
543 for line in resolv_conf:lines() do
544 line = line:gsub("#.*$", "")
545 :match('^%s*nameserver%s+(.*)%s*$');
547 line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address)
548 self:addnameserver(address)
553 if not self.server or #self.server == 0 then
554 -- TODO log warning about no nameservers, adding localhost as the default nameserver
555 self:addnameserver("127.0.0.1");
561 function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket
562 self.socket = self.socket or {};
563 self.socketset = self.socketset or {};
565 local sock = self.socket[servernum];
566 if sock then return sock; end
569 if self.socket_wrapper then sock = self.socket_wrapper(sock, self); end
571 -- todo: attempt to use a random port, fallback to 0
572 sock:setsockname('*', 0);
573 sock:setpeername(self.server[servernum], 53);
574 self.socket[servernum] = sock;
575 self.socketset[sock] = servernum;
579 function resolver:voidsocket(sock)
580 if self.socket[sock] then
581 self.socketset[self.socket[sock]] = nil;
582 self.socket[sock] = nil;
583 elseif self.socketset[sock] then
584 self.socket[self.socketset[sock]] = nil;
585 self.socketset[sock] = nil;
589 function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
590 self.socket_wrapper = func;
594 function resolver:closeall () -- - - - - - - - - - - - - - - - - - closeall
595 for i,sock in ipairs(self.socket) do
596 self.socket[i] = nil;
597 self.socketset[sock] = nil;
603 function resolver:remember(rr, type) -- - - - - - - - - - - - - - remember
604 --print ('remember', type, rr.class, rr.type, rr.name)
605 local qname, qtype, qclass = standardize(rr.name, rr.type, rr.class);
609 local all = get(self.cache, qclass, '*', qname);
610 --print('remember all', all);
611 if all then append(all, rr); end
614 self.cache = self.cache or setmetatable({}, cache_metatable);
615 local rrs = get(self.cache, qclass, type, qname) or
616 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
619 if type == 'MX' then self.unsorted[rrs] = true; end
623 local function comp_mx(a, b) -- - - - - - - - - - - - - - - - - - - comp_mx
624 return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
628 function resolver:peek (qname, qtype, qclass) -- - - - - - - - - - - - peek
629 qname, qtype, qclass = standardize(qname, qtype, qclass);
630 local rrs = get(self.cache, qclass, qtype, qname);
631 if not rrs then return nil; end
632 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
633 set(self.cache, qclass, qtype, qname, nil);
636 if self.unsorted[rrs] then table.sort (rrs, comp_mx); end
641 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
642 if soft == 'soft' then
643 self.time = socket.gettime();
644 for class,types in pairs(self.cache or {}) do
645 for type,names in pairs(types) do
646 for name,rrs in pairs(names) do
647 prune(rrs, self.time, 'soft')
651 else self.cache = {}; end
655 function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
656 qname, qtype, qclass = standardize(qname, qtype, qclass)
658 if not self.server then self:adddefaultnameservers(); end
660 local question = encodeQuestion(qname, qtype, qclass);
661 local peek = self:peek (qname, qtype, qclass);
662 if peek then return peek; end
664 local header, id = encodeHeader();
665 --print ('query id', id, qclass, qtype, qname)
667 packet = header..question,
668 server = self.best_server,
670 retry = socket.gettime() + self.delays[1]
673 -- remember the query
674 self.active[id] = self.active[id] or {};
675 self.active[id][question] = o;
677 -- remember which coroutine wants the answer
678 local co = coroutine.running();
680 set(self.wanted, qclass, qtype, qname, co, true);
681 --set(self.yielded, co, qclass, qtype, qname, true);
684 local conn = self:getsocket(o.server)
688 local num_servers = #self.server;
690 timer.add_task(dns_timeout, function ()
691 if get(self.wanted, qclass, qtype, qname, co) then
692 if i < num_servers then
695 o.server = self.best_server;
696 conn = self:getsocket(o.server);
700 -- Tried everything, failed
701 resolver:cancel({qclass, qtype, qname, co}, true);
708 function resolver:servfail(sock)
709 -- Resend all queries for this server
711 local num = self.socketset[sock]
713 -- Socket is dead now
714 self:voidsocket(sock);
716 -- Find all requests to the down server, and retry on the next server
717 self.time = socket.gettime();
718 for id,queries in pairs(self.active) do
719 for question,o in pairs(queries) do
720 if o.server == num then -- This request was to the broken server
721 o.server = o.server + 1 -- Use next server
722 if o.server > #self.server then
726 o.retries = (o.retries or 0) + 1;
727 if o.retries >= #self.server then
729 queries[question] = nil;
731 local _a = self:getsocket(o.server);
732 if _a then _a:send(o.packet); end
738 if num == self.best_server then
739 self.best_server = self.best_server + 1;
740 if self.best_server > #self.server then
741 -- Exhausted all servers, try first again
742 self.best_server = 1;
747 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
748 --print('receive'); print(self.socket);
749 self.time = socket.gettime();
750 rset = rset or self.socket;
753 for i,sock in pairs(rset) do
755 if self.socketset[sock] then
756 local packet = sock:receive();
758 response = self:decode(packet);
759 if response and self.active[response.header.id]
760 and self.active[response.header.id][response.question.raw] then
761 --print('received response');
762 --self.print(response);
764 for j,rr in pairs(response.answer) do
765 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
766 self:remember(rr, response.question[1].type)
771 local queries = self.active[response.header.id];
772 queries[response.question.raw] = nil;
774 if not next(queries) then self.active[response.header.id] = nil; end
775 if not next(self.active) then self:closeall(); end
777 -- was the query on the wanted list?
778 local q = response.question[1];
779 local cos = get(self.wanted, q.class, q.type, q.name);
781 for co in pairs(cos) do
782 set(self.yielded, co, q.class, q.type, q.name, nil);
783 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
785 set(self.wanted, q.class, q.type, q.name, nil);
796 function resolver:feed(sock, packet)
797 --print('receive'); print(self.socket);
798 self.time = socket.gettime();
800 local response = self:decode(packet);
801 if response and self.active[response.header.id]
802 and self.active[response.header.id][response.question.raw] then
803 --print('received response');
804 --self.print(response);
806 for j,rr in pairs(response.answer) do
807 self:remember(rr, response.question[1].type);
811 local queries = self.active[response.header.id];
812 queries[response.question.raw] = nil;
813 if not next(queries) then self.active[response.header.id] = nil; end
814 if not next(self.active) then self:closeall(); end
816 -- was the query on the wanted list?
817 local q = response.question[1];
819 local cos = get(self.wanted, q.class, q.type, q.name);
821 for co in pairs(cos) do
822 set(self.yielded, co, q.class, q.type, q.name, nil);
823 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
825 set(self.wanted, q.class, q.type, q.name, nil);
833 function resolver:cancel(data)
834 local cos = get(self.wanted, unpack(data, 1, 3));
840 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
842 while self:receive() do end
843 if not next(self.active) then return nil; end
845 self.time = socket.gettime();
846 for id,queries in pairs(self.active) do
847 for question,o in pairs(queries) do
848 if self.time >= o.retry then
850 o.server = o.server + 1;
851 if o.server > #self.server then
853 o.delay = o.delay + 1;
856 if o.delay > #self.delays then
858 queries[question] = nil;
859 if not next(queries) then self.active[id] = nil; end
860 if not next(self.active) then return nil; end
862 --print('retry', o.server, o.delay);
863 local _a = self.socket[o.server];
864 if _a then _a:send(o.packet); end
865 o.retry = self.time + self.delays[o.delay];
871 if next(self.active) then return true; end
876 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
877 self:query (qname, qtype, qclass)
878 while self:pulse() do
880 for i, s in ipairs(self.socket) do
883 socket.select(recvt, nil, 4)
886 return self:peek(qname, qtype, qclass);
889 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
890 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
894 --print ---------------------------------------------------------------- print
897 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
898 qr = { [0]='query', 'response' },
899 opcode = { [0]='query', 'inverse query', 'server status request' },
900 aa = { [0]='non-authoritative', 'authoritative' },
901 tc = { [0]='complete', 'truncated' },
902 rd = { [0]='recursion not desired', 'recursion desired' },
903 ra = { [0]='recursion not available', 'recursion available' },
904 z = { [0]='(reserved)' },
905 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
912 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
913 return (hints[s] and hints[s][p[s]]) or '';
917 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
918 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
919 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
920 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
923 for i,question in ipairs(response.question) do
924 print(string.format ('question[%i].name ', i), question.name);
925 print(string.format ('question[%i].type ', i), question.type);
926 print(string.format ('question[%i].class ', i), question.class);
929 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
931 for s,s in pairs({'answer', 'authority', 'additional'}) do
932 for i,rr in pairs(response[s]) do
933 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
934 tmp = string.format('%s[%i].%s', s, i, t);
935 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
937 for j,t in pairs(rr) do
938 if not common[j] then
939 tmp = string.format('%s[%i].%s', s, i, j);
940 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
948 -- module api ------------------------------------------------------ module api
951 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
952 -- this function seems to be redundant with resolver.new ()
954 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {}, best_server = 1 };
955 setmetatable (r, resolver);
956 setmetatable (r.cache, cache_metatable);
957 setmetatable (r.unsorted, { __mode = 'kv' });
961 local _resolver = dns.resolver();
962 dns._resolver = _resolver;
964 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
965 return _resolver:lookup(...);
968 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
969 return _resolver:purge(...);
972 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
973 return _resolver:peek(...);
976 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
977 return _resolver:query(...);
980 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - - feed
981 return _resolver:feed(...);
984 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
985 return _resolver:cancel(...);
988 function dns.socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
989 return _resolver:socket_wrapper_set(...);