2 -- This file is included with Prosody IM. It has modifications,
3 -- which are hereby placed in the public domain.
6 -- todo: quick (default) header generation
7 -- todo: nxdomain, error handling
8 -- todo: cache results of encodeName
11 -- reference: http://tools.ietf.org/html/rfc1035
12 -- reference: http://tools.ietf.org/html/rfc1876 (LOC)
15 local socket = require "socket";
16 local timer = require "util.timer";
18 local _, windows = pcall(require, "util.windows");
19 local is_windows = (_ and windows) or os.getenv("WINDIR");
21 local coroutine, io, math, string, table =
22 coroutine, io, math, string, table;
24 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type=
25 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type;
27 local ztact = { -- public domain 20080404 lua@ztact.com
28 get = function(parent, ...)
29 local len = select('#', ...);
31 parent = parent[select(i, ...)];
32 if parent == nil then break; end
36 set = function(parent, ...)
37 local len = select('#', ...);
38 local key, value = select(len-1, ...);
39 local cutpoint, cutkey;
42 local key = select (i, ...)
43 local child = parent[key]
48 elseif next(child, next(child)) then
49 cutpoint = nil; cutkey = nil;
50 elseif cutpoint == nil then
51 cutpoint = parent; cutkey = key;
53 elseif child == nil then
60 if value == nil and cutpoint then
61 cutpoint[cutkey] = nil;
68 local get, set = ztact.get, ztact.set;
70 local default_timeout = 15;
72 -------------------------------------------------- module dns
77 -- dns type & class codes ------------------------------ dns type & class codes
80 local append = table.insert
83 local function highbyte(i) -- - - - - - - - - - - - - - - - - - - highbyte
84 return (i-(i%0x100))/0x100;
88 local function augment (t) -- - - - - - - - - - - - - - - - - - - - augment
90 for i,s in pairs(t) do
93 a[string.lower(s)] = s;
99 local function encode (t) -- - - - - - - - - - - - - - - - - - - - - encode
101 for i,s in pairs(t) do
102 local word = string.char(highbyte(i), i%0x100);
105 code[string.lower(s)] = word;
112 'A', 'NS', 'MD', 'MF', 'CNAME', 'SOA', 'MB', 'MG', 'MR', 'NULL', 'WKS',
113 'PTR', 'HINFO', 'MINFO', 'MX', 'TXT',
114 [ 28] = 'AAAA', [ 29] = 'LOC', [ 33] = 'SRV',
115 [252] = 'AXFR', [253] = 'MAILB', [254] = 'MAILA', [255] = '*' };
118 dns.classes = { 'IN', 'CS', 'CH', 'HS', [255] = '*' };
121 dns.type = augment (dns.types);
122 dns.class = augment (dns.classes);
123 dns.typecode = encode (dns.types);
124 dns.classcode = encode (dns.classes);
128 local function standardize(qname, qtype, qclass) -- - - - - - - standardize
129 if string.byte(qname, -1) ~= 0x2E then qname = qname..'.'; end
130 qname = string.lower(qname);
131 return qname, dns.type[qtype or 'A'], dns.class[qclass or 'IN'];
135 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune
136 time = time or socket.gettime();
137 for i,rr in pairs(rrs) do
139 -- rr.tod = rr.tod - 50 -- accelerated decripitude
140 rr.ttl = math.floor(rr.tod - time);
142 table.remove(rrs, i);
143 return prune(rrs, time, soft); -- Re-iterate
145 elseif soft == 'soft' then -- What is this? I forget!
153 -- metatables & co. ------------------------------------------ metatables & co.
157 resolver.__index = resolver;
159 resolver.timeout = default_timeout;
163 local function default_rr_tostring(rr)
164 local rr_val = rr.type and rr[rr.type:lower()];
165 if type(rr_val) ~= "string" then
166 return "<UNKNOWN RDATA TYPE>";
171 local special_tostrings = {
172 LOC = resolver.LOC_tostring;
173 MX = function (rr) return string.format('%2i %s', rr.pref, rr.mx); end;
177 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
178 function rr_metatable.__tostring(rr)
179 local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr);
180 return string.format('%2s %-5s %6i %-28s %s', rr.class, rr.type, rr.ttl, rr.name, rr_string);
184 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
185 function rrs_metatable.__tostring(rrs)
187 for i,rr in pairs(rrs) do
188 append(t, tostring(rr)..'\n');
190 return table.concat(t);
194 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable
195 function cache_metatable.__tostring(cache)
196 local time = socket.gettime();
198 for class,types in pairs(cache) do
199 for type,names in pairs(types) do
200 for name,rrs in pairs(names) do
202 append(t, tostring(rrs));
206 return table.concat(t);
210 function resolver:new() -- - - - - - - - - - - - - - - - - - - - - resolver
211 local r = { active = {}, cache = {}, unsorted = {} };
212 setmetatable(r, resolver);
213 setmetatable(r.cache, cache_metatable);
214 setmetatable(r.unsorted, { __mode = 'kv' });
219 -- packet layer -------------------------------------------------- packet layer
222 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
223 math.randomseed(math.floor(10000*socket.gettime()));
224 dns.random = math.random;
225 return dns.random(...);
229 local function encodeHeader(o) -- - - - - - - - - - - - - - - encodeHeader
231 o.id = o.id or dns.random(0, 0xffff); -- 16b (random) id
233 o.rd = o.rd or 1; -- 1b 1 recursion desired
234 o.tc = o.tc or 0; -- 1b 1 truncated response
235 o.aa = o.aa or 0; -- 1b 1 authoritative response
236 o.opcode = o.opcode or 0; -- 4b 0 query
238 -- 2 server status request
240 o.qr = o.qr or 0; -- 1b 0 query, 1 response
242 o.rcode = o.rcode or 0; -- 4b 0 no error
249 o.z = o.z or 0; -- 3b 0 resvered
250 o.ra = o.ra or 0; -- 1b 1 recursion available
252 o.qdcount = o.qdcount or 1; -- 16b number of question RRs
253 o.ancount = o.ancount or 0; -- 16b number of answers RRs
254 o.nscount = o.nscount or 0; -- 16b number of nameservers RRs
255 o.arcount = o.arcount or 0; -- 16b number of additional RRs
257 -- string.char() rounds, so prevent roundup with -0.4999
258 local header = string.char(
259 highbyte(o.id), o.id %0x100,
260 o.rd + 2*o.tc + 4*o.aa + 8*o.opcode + 128*o.qr,
261 o.rcode + 16*o.z + 128*o.ra,
262 highbyte(o.qdcount), o.qdcount %0x100,
263 highbyte(o.ancount), o.ancount %0x100,
264 highbyte(o.nscount), o.nscount %0x100,
265 highbyte(o.arcount), o.arcount %0x100
272 local function encodeName(name) -- - - - - - - - - - - - - - - - encodeName
274 for part in string.gmatch(name, '[^.]+') do
275 append(t, string.char(string.len(part)));
278 append(t, string.char(0));
279 return table.concat(t);
283 local function encodeQuestion(qname, qtype, qclass) -- - - - encodeQuestion
284 qname = encodeName(qname);
285 qtype = dns.typecode[qtype or 'a'];
286 qclass = dns.classcode[qclass or 'in'];
287 return qname..qtype..qclass;
291 function resolver:byte(len) -- - - - - - - - - - - - - - - - - - - - - byte
293 local offset = self.offset;
294 local last = offset + len - 1;
295 if last > #self.packet then
296 error(string.format('out of bounds: %i>%i', last, #self.packet));
298 self.offset = offset + len;
299 return string.byte(self.packet, offset, last);
303 function resolver:word() -- - - - - - - - - - - - - - - - - - - - - - word
304 local b1, b2 = self:byte(2);
305 return 0x100*b1 + b2;
309 function resolver:dword () -- - - - - - - - - - - - - - - - - - - - - dword
310 local b1, b2, b3, b4 = self:byte(4);
311 --print('dword', b1, b2, b3, b4);
312 return 0x1000000*b1 + 0x10000*b2 + 0x100*b3 + b4;
316 function resolver:sub(len) -- - - - - - - - - - - - - - - - - - - - - - sub
318 local s = string.sub(self.packet, self.offset, self.offset + len - 1);
319 self.offset = self.offset + len;
324 function resolver:header(force) -- - - - - - - - - - - - - - - - - - header
325 local id = self:word();
326 --print(string.format(':header id %x', id));
327 if not self.active[id] and not force then return nil; end
329 local h = { id = id };
331 local b1, b2 = self:byte(2);
343 h.qdcount = self:word();
344 h.ancount = self:word();
345 h.nscount = self:word();
346 h.arcount = self:word();
348 for k,v in pairs(h) do h[k] = v-v%1; end
354 function resolver:name() -- - - - - - - - - - - - - - - - - - - - - - name
355 local remember, pointers = nil, 0;
356 local len = self:byte();
359 if len >= 0xc0 then -- name is "compressed"
360 pointers = pointers + 1;
361 if pointers >= 20 then error('dns error: 20 pointers'); end;
362 local offset = ((len-0xc0)*0x100) + self:byte();
363 remember = remember or self.offset;
364 self.offset = offset + 1; -- +1 for lua
365 else -- name is not compressed
366 append(n, self:sub(len)..'.');
370 self.offset = remember or self.offset;
371 return table.concat(n);
375 function resolver:question() -- - - - - - - - - - - - - - - - - - question
377 q.name = self:name();
378 q.type = dns.type[self:word()];
379 q.class = dns.class[self:word()];
384 function resolver:A(rr) -- - - - - - - - - - - - - - - - - - - - - - - - A
385 local b1, b2, b3, b4 = self:byte(4);
386 rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
390 function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
391 rr.cname = self:name();
395 function resolver:MX(rr) -- - - - - - - - - - - - - - - - - - - - - - - MX
396 rr.pref = self:word();
401 function resolver:LOC_nibble_power() -- - - - - - - - - - LOC_nibble_power
402 local b = self:byte();
403 --print('nibbles', ((b-(b%0x10))/0x10), (b%0x10));
404 return ((b-(b%0x10))/0x10) * (10^(b%0x10));
408 function resolver:LOC(rr) -- - - - - - - - - - - - - - - - - - - - - - LOC
409 rr.version = self:byte();
410 if rr.version == 0 then
411 rr.loc = rr.loc or {};
412 rr.loc.size = self:LOC_nibble_power();
413 rr.loc.horiz_pre = self:LOC_nibble_power();
414 rr.loc.vert_pre = self:LOC_nibble_power();
415 rr.loc.latitude = self:dword();
416 rr.loc.longitude = self:dword();
417 rr.loc.altitude = self:dword();
422 local function LOC_tostring_degrees(f, pos, neg) -- - - - - - - - - - - - -
424 if f < 0 then pos = neg; f = -f; end
425 local deg, min, msec;
430 return string.format('%3d %2d %2.3f %s', deg, min, msec/1000, pos);
434 function resolver.LOC_tostring(rr) -- - - - - - - - - - - - - LOC_tostring
438 for k,name in pairs { 'size', 'horiz_pre', 'vert_pre', 'latitude', 'longitude', 'altitude' } do
439 append(t, string.format('%4s%-10s: %12.0f\n', '', name, rr.loc[name]));
443 append(t, string.format(
444 '%s %s %.2fm %.2fm %.2fm %.2fm',
445 LOC_tostring_degrees (rr.loc.latitude, 'N', 'S'),
446 LOC_tostring_degrees (rr.loc.longitude, 'E', 'W'),
447 (rr.loc.altitude - 10000000) / 100,
449 rr.loc.horiz_pre / 100,
450 rr.loc.vert_pre / 100
453 return table.concat(t);
457 function resolver:NS(rr) -- - - - - - - - - - - - - - - - - - - - - - - NS
462 function resolver:SOA(rr) -- - - - - - - - - - - - - - - - - - - - - - SOA
466 function resolver:SRV(rr) -- - - - - - - - - - - - - - - - - - - - - - SRV
468 rr.srv.priority = self:word();
469 rr.srv.weight = self:word();
470 rr.srv.port = self:word();
471 rr.srv.target = self:name();
474 function resolver:PTR(rr)
475 rr.ptr = self:name();
478 function SRV_tostring(rr) -- - - - - - - - - - - - - - - - - - SRV_tostring
480 return string.format( '%5d %5d %5d %s', s.priority, s.weight, s.port, s.target );
484 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
485 rr.txt = self:sub (rr.rdlength);
489 function resolver:rr() -- - - - - - - - - - - - - - - - - - - - - - - - rr
491 setmetatable(rr, rr_metatable);
492 rr.name = self:name(self);
493 rr.type = dns.type[self:word()] or rr.type;
494 rr.class = dns.class[self:word()] or rr.class;
495 rr.ttl = 0x10000*self:word() + self:word();
496 rr.rdlength = self:word();
499 rr.tod = self.time + 30;
501 rr.tod = self.time + rr.ttl;
504 local remember = self.offset;
505 local rr_parser = self[dns.type[rr.type]];
506 if rr_parser then rr_parser(self, rr); end
507 self.offset = remember;
508 rr.rdata = self:sub(rr.rdlength);
513 function resolver:rrs (count) -- - - - - - - - - - - - - - - - - - - - - rrs
515 for i = 1,count do append(rrs, self:rr()); end
520 function resolver:decode(packet, force) -- - - - - - - - - - - - - - decode
521 self.packet, self.offset = packet, 1;
522 local header = self:header(force);
523 if not header then return nil; end
524 local response = { header = header };
526 response.question = {};
527 local offset = self.offset;
528 for i = 1,response.header.qdcount do
529 append(response.question, self:question());
531 response.question.raw = string.sub(self.packet, offset, self.offset - 1);
534 if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
539 response.answer = self:rrs(response.header.ancount);
540 response.authority = self:rrs(response.header.nscount);
541 response.additional = self:rrs(response.header.arcount);
547 -- socket layer -------------------------------------------------- socket layer
550 resolver.delays = { 1, 3 };
553 function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
554 self.server = self.server or {};
555 append(self.server, address);
559 function resolver:setnameserver(address) -- - - - - - - - - - setnameserver
561 self:addnameserver(address);
565 function resolver:adddefaultnameservers() -- - - - - adddefaultnameservers
567 if windows and windows.get_nameservers then
568 for _, server in ipairs(windows.get_nameservers()) do
569 self:addnameserver(server);
572 if not self.server or #self.server == 0 then
573 -- TODO log warning about no nameservers, adding opendns servers as fallback
574 self:addnameserver("208.67.222.222");
575 self:addnameserver("208.67.220.220");
578 local resolv_conf = io.open("/etc/resolv.conf");
580 for line in resolv_conf:lines() do
581 line = line:gsub("#.*$", "")
582 :match('^%s*nameserver%s+(.*)%s*$');
584 line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address)
585 self:addnameserver(address)
590 if not self.server or #self.server == 0 then
591 -- TODO log warning about no nameservers, adding localhost as the default nameserver
592 self:addnameserver("127.0.0.1");
598 function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket
599 self.socket = self.socket or {};
600 self.socketset = self.socketset or {};
602 local sock = self.socket[servernum];
603 if sock then return sock; end
606 sock, err = socket.udp();
610 if self.socket_wrapper then sock = self.socket_wrapper(sock, self); end
612 -- todo: attempt to use a random port, fallback to 0
613 sock:setsockname('*', 0);
614 sock:setpeername(self.server[servernum], 53);
615 self.socket[servernum] = sock;
616 self.socketset[sock] = servernum;
620 function resolver:voidsocket(sock)
621 if self.socket[sock] then
622 self.socketset[self.socket[sock]] = nil;
623 self.socket[sock] = nil;
624 elseif self.socketset[sock] then
625 self.socket[self.socketset[sock]] = nil;
626 self.socketset[sock] = nil;
630 function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
631 self.socket_wrapper = func;
635 function resolver:closeall () -- - - - - - - - - - - - - - - - - - closeall
636 for i,sock in ipairs(self.socket) do
637 self.socket[i] = nil;
638 self.socketset[sock] = nil;
644 function resolver:remember(rr, type) -- - - - - - - - - - - - - - remember
645 --print ('remember', type, rr.class, rr.type, rr.name)
646 local qname, qtype, qclass = standardize(rr.name, rr.type, rr.class);
650 local all = get(self.cache, qclass, '*', qname);
651 --print('remember all', all);
652 if all then append(all, rr); end
655 self.cache = self.cache or setmetatable({}, cache_metatable);
656 local rrs = get(self.cache, qclass, type, qname) or
657 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
660 if type == 'MX' then self.unsorted[rrs] = true; end
664 local function comp_mx(a, b) -- - - - - - - - - - - - - - - - - - - comp_mx
665 return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
669 function resolver:peek (qname, qtype, qclass) -- - - - - - - - - - - - peek
670 qname, qtype, qclass = standardize(qname, qtype, qclass);
671 local rrs = get(self.cache, qclass, qtype, qname);
672 if not rrs then return nil; end
673 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
674 set(self.cache, qclass, qtype, qname, nil);
677 if self.unsorted[rrs] then table.sort (rrs, comp_mx); end
682 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
683 if soft == 'soft' then
684 self.time = socket.gettime();
685 for class,types in pairs(self.cache or {}) do
686 for type,names in pairs(types) do
687 for name,rrs in pairs(names) do
688 prune(rrs, self.time, 'soft')
692 else self.cache = {}; end
696 function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
697 qname, qtype, qclass = standardize(qname, qtype, qclass)
699 if not self.server then self:adddefaultnameservers(); end
701 local question = encodeQuestion(qname, qtype, qclass);
702 local peek = self:peek (qname, qtype, qclass);
703 if peek then return peek; end
705 local header, id = encodeHeader();
706 --print ('query id', id, qclass, qtype, qname)
708 packet = header..question,
709 server = self.best_server,
711 retry = socket.gettime() + self.delays[1]
714 -- remember the query
715 self.active[id] = self.active[id] or {};
716 self.active[id][question] = o;
718 -- remember which coroutine wants the answer
719 local co = coroutine.running();
721 set(self.wanted, qclass, qtype, qname, co, true);
722 --set(self.yielded, co, qclass, qtype, qname, true);
725 local conn = self:getsocket(o.server)
728 if timer and self.timeout then
729 local num_servers = #self.server;
731 timer.add_task(self.timeout, function ()
732 if get(self.wanted, qclass, qtype, qname, co) then
733 if i < num_servers then
736 o.server = self.best_server;
737 conn = self:getsocket(o.server);
741 -- Tried everything, failed
742 self:cancel(qclass, qtype, qname, co, true);
749 function resolver:servfail(sock)
750 -- Resend all queries for this server
752 local num = self.socketset[sock]
754 -- Socket is dead now
755 self:voidsocket(sock);
757 -- Find all requests to the down server, and retry on the next server
758 self.time = socket.gettime();
759 for id,queries in pairs(self.active) do
760 for question,o in pairs(queries) do
761 if o.server == num then -- This request was to the broken server
762 o.server = o.server + 1 -- Use next server
763 if o.server > #self.server then
767 o.retries = (o.retries or 0) + 1;
768 if o.retries >= #self.server then
770 queries[question] = nil;
772 local _a = self:getsocket(o.server);
773 if _a then _a:send(o.packet); end
779 if num == self.best_server then
780 self.best_server = self.best_server + 1;
781 if self.best_server > #self.server then
782 -- Exhausted all servers, try first again
783 self.best_server = 1;
788 function resolver:settimeout(seconds)
789 self.timeout = seconds;
792 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
793 --print('receive'); print(self.socket);
794 self.time = socket.gettime();
795 rset = rset or self.socket;
798 for i,sock in pairs(rset) do
800 if self.socketset[sock] then
801 local packet = sock:receive();
803 response = self:decode(packet);
804 if response and self.active[response.header.id]
805 and self.active[response.header.id][response.question.raw] then
806 --print('received response');
807 --self.print(response);
809 for j,rr in pairs(response.answer) do
810 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
811 self:remember(rr, response.question[1].type)
816 local queries = self.active[response.header.id];
817 queries[response.question.raw] = nil;
819 if not next(queries) then self.active[response.header.id] = nil; end
820 if not next(self.active) then self:closeall(); end
822 -- was the query on the wanted list?
823 local q = response.question[1];
824 local cos = get(self.wanted, q.class, q.type, q.name);
826 for co in pairs(cos) do
827 set(self.yielded, co, q.class, q.type, q.name, nil);
828 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
830 set(self.wanted, q.class, q.type, q.name, nil);
841 function resolver:feed(sock, packet, force)
842 --print('receive'); print(self.socket);
843 self.time = socket.gettime();
845 local response = self:decode(packet, force);
846 if response and self.active[response.header.id]
847 and self.active[response.header.id][response.question.raw] then
848 --print('received response');
849 --self.print(response);
851 for j,rr in pairs(response.answer) do
852 self:remember(rr, response.question[1].type);
856 local queries = self.active[response.header.id];
857 queries[response.question.raw] = nil;
858 if not next(queries) then self.active[response.header.id] = nil; end
859 if not next(self.active) then self:closeall(); end
861 -- was the query on the wanted list?
862 local q = response.question[1];
864 local cos = get(self.wanted, q.class, q.type, q.name);
866 for co in pairs(cos) do
867 set(self.yielded, co, q.class, q.type, q.name, nil);
868 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
870 set(self.wanted, q.class, q.type, q.name, nil);
878 function resolver:cancel(qclass, qtype, qname, co, call_handler)
879 local cos = get(self.wanted, qclass, qtype, qname);
882 coroutine.resume(co);
888 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
890 while self:receive() do end
891 if not next(self.active) then return nil; end
893 self.time = socket.gettime();
894 for id,queries in pairs(self.active) do
895 for question,o in pairs(queries) do
896 if self.time >= o.retry then
898 o.server = o.server + 1;
899 if o.server > #self.server then
901 o.delay = o.delay + 1;
904 if o.delay > #self.delays then
906 queries[question] = nil;
907 if not next(queries) then self.active[id] = nil; end
908 if not next(self.active) then return nil; end
910 --print('retry', o.server, o.delay);
911 local _a = self.socket[o.server];
912 if _a then _a:send(o.packet); end
913 o.retry = self.time + self.delays[o.delay];
919 if next(self.active) then return true; end
924 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
925 self:query (qname, qtype, qclass)
926 while self:pulse() do
928 for i, s in ipairs(self.socket) do
931 socket.select(recvt, nil, 4)
934 return self:peek(qname, qtype, qclass);
937 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
938 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
941 function resolver:tohostname(ip)
942 return dns.lookup(ip:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)", "%4.%3.%2.%1.in-addr.arpa."), "PTR");
945 --print ---------------------------------------------------------------- print
948 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
949 qr = { [0]='query', 'response' },
950 opcode = { [0]='query', 'inverse query', 'server status request' },
951 aa = { [0]='non-authoritative', 'authoritative' },
952 tc = { [0]='complete', 'truncated' },
953 rd = { [0]='recursion not desired', 'recursion desired' },
954 ra = { [0]='recursion not available', 'recursion available' },
955 z = { [0]='(reserved)' },
956 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
963 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
964 return (hints[s] and hints[s][p[s]]) or '';
968 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
969 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
970 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
971 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
974 for i,question in ipairs(response.question) do
975 print(string.format ('question[%i].name ', i), question.name);
976 print(string.format ('question[%i].type ', i), question.type);
977 print(string.format ('question[%i].class ', i), question.class);
980 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
982 for s,s in pairs({'answer', 'authority', 'additional'}) do
983 for i,rr in pairs(response[s]) do
984 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
985 tmp = string.format('%s[%i].%s', s, i, t);
986 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
988 for j,t in pairs(rr) do
989 if not common[j] then
990 tmp = string.format('%s[%i].%s', s, i, j);
991 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
999 -- module api ------------------------------------------------------ module api
1002 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
1003 -- this function seems to be redundant with resolver.new ()
1005 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {}, best_server = 1 };
1006 setmetatable (r, resolver);
1007 setmetatable (r.cache, cache_metatable);
1008 setmetatable (r.unsorted, { __mode = 'kv' });
1012 local _resolver = dns.resolver();
1013 dns._resolver = _resolver;
1015 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
1016 return _resolver:lookup(...);
1019 function dns.tohostname(...)
1020 return _resolver:tohostname(...);
1023 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
1024 return _resolver:purge(...);
1027 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
1028 return _resolver:peek(...);
1031 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
1032 return _resolver:query(...);
1035 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - - feed
1036 return _resolver:feed(...);
1039 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
1040 return _resolver:cancel(...);
1043 function dns.settimeout(...)
1044 return _resolver:settimeout(...);
1047 function dns.socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
1048 return _resolver:socket_wrapper_set(...);