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 if self.socket_wrapper then sock = self.socket_wrapper(sock, self); end
608 -- todo: attempt to use a random port, fallback to 0
609 sock:setsockname('*', 0);
610 sock:setpeername(self.server[servernum], 53);
611 self.socket[servernum] = sock;
612 self.socketset[sock] = servernum;
616 function resolver:voidsocket(sock)
617 if self.socket[sock] then
618 self.socketset[self.socket[sock]] = nil;
619 self.socket[sock] = nil;
620 elseif self.socketset[sock] then
621 self.socket[self.socketset[sock]] = nil;
622 self.socketset[sock] = nil;
626 function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
627 self.socket_wrapper = func;
631 function resolver:closeall () -- - - - - - - - - - - - - - - - - - closeall
632 for i,sock in ipairs(self.socket) do
633 self.socket[i] = nil;
634 self.socketset[sock] = nil;
640 function resolver:remember(rr, type) -- - - - - - - - - - - - - - remember
641 --print ('remember', type, rr.class, rr.type, rr.name)
642 local qname, qtype, qclass = standardize(rr.name, rr.type, rr.class);
646 local all = get(self.cache, qclass, '*', qname);
647 --print('remember all', all);
648 if all then append(all, rr); end
651 self.cache = self.cache or setmetatable({}, cache_metatable);
652 local rrs = get(self.cache, qclass, type, qname) or
653 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
656 if type == 'MX' then self.unsorted[rrs] = true; end
660 local function comp_mx(a, b) -- - - - - - - - - - - - - - - - - - - comp_mx
661 return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
665 function resolver:peek (qname, qtype, qclass) -- - - - - - - - - - - - peek
666 qname, qtype, qclass = standardize(qname, qtype, qclass);
667 local rrs = get(self.cache, qclass, qtype, qname);
668 if not rrs then return nil; end
669 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
670 set(self.cache, qclass, qtype, qname, nil);
673 if self.unsorted[rrs] then table.sort (rrs, comp_mx); end
678 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
679 if soft == 'soft' then
680 self.time = socket.gettime();
681 for class,types in pairs(self.cache or {}) do
682 for type,names in pairs(types) do
683 for name,rrs in pairs(names) do
684 prune(rrs, self.time, 'soft')
688 else self.cache = {}; end
692 function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
693 qname, qtype, qclass = standardize(qname, qtype, qclass)
695 if not self.server then self:adddefaultnameservers(); end
697 local question = encodeQuestion(qname, qtype, qclass);
698 local peek = self:peek (qname, qtype, qclass);
699 if peek then return peek; end
701 local header, id = encodeHeader();
702 --print ('query id', id, qclass, qtype, qname)
704 packet = header..question,
705 server = self.best_server,
707 retry = socket.gettime() + self.delays[1]
710 -- remember the query
711 self.active[id] = self.active[id] or {};
712 self.active[id][question] = o;
714 -- remember which coroutine wants the answer
715 local co = coroutine.running();
717 set(self.wanted, qclass, qtype, qname, co, true);
718 --set(self.yielded, co, qclass, qtype, qname, true);
721 local conn = self:getsocket(o.server)
724 if timer and self.timeout then
725 local num_servers = #self.server;
727 timer.add_task(self.timeout, function ()
728 if get(self.wanted, qclass, qtype, qname, co) then
729 if i < num_servers then
732 o.server = self.best_server;
733 conn = self:getsocket(o.server);
737 -- Tried everything, failed
738 self:cancel(qclass, qtype, qname, co, true);
745 function resolver:servfail(sock)
746 -- Resend all queries for this server
748 local num = self.socketset[sock]
750 -- Socket is dead now
751 self:voidsocket(sock);
753 -- Find all requests to the down server, and retry on the next server
754 self.time = socket.gettime();
755 for id,queries in pairs(self.active) do
756 for question,o in pairs(queries) do
757 if o.server == num then -- This request was to the broken server
758 o.server = o.server + 1 -- Use next server
759 if o.server > #self.server then
763 o.retries = (o.retries or 0) + 1;
764 if o.retries >= #self.server then
766 queries[question] = nil;
768 local _a = self:getsocket(o.server);
769 if _a then _a:send(o.packet); end
775 if num == self.best_server then
776 self.best_server = self.best_server + 1;
777 if self.best_server > #self.server then
778 -- Exhausted all servers, try first again
779 self.best_server = 1;
784 function resolver:settimeout(seconds)
785 self.timeout = seconds;
788 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
789 --print('receive'); print(self.socket);
790 self.time = socket.gettime();
791 rset = rset or self.socket;
794 for i,sock in pairs(rset) do
796 if self.socketset[sock] then
797 local packet = sock:receive();
799 response = self:decode(packet);
800 if response and self.active[response.header.id]
801 and self.active[response.header.id][response.question.raw] then
802 --print('received response');
803 --self.print(response);
805 for j,rr in pairs(response.answer) do
806 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
807 self:remember(rr, response.question[1].type)
812 local queries = self.active[response.header.id];
813 queries[response.question.raw] = nil;
815 if not next(queries) then self.active[response.header.id] = nil; end
816 if not next(self.active) then self:closeall(); end
818 -- was the query on the wanted list?
819 local q = response.question[1];
820 local cos = get(self.wanted, q.class, q.type, q.name);
822 for co in pairs(cos) do
823 set(self.yielded, co, q.class, q.type, q.name, nil);
824 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
826 set(self.wanted, q.class, q.type, q.name, nil);
837 function resolver:feed(sock, packet, force)
838 --print('receive'); print(self.socket);
839 self.time = socket.gettime();
841 local response = self:decode(packet, force);
842 if response and self.active[response.header.id]
843 and self.active[response.header.id][response.question.raw] then
844 --print('received response');
845 --self.print(response);
847 for j,rr in pairs(response.answer) do
848 self:remember(rr, response.question[1].type);
852 local queries = self.active[response.header.id];
853 queries[response.question.raw] = nil;
854 if not next(queries) then self.active[response.header.id] = nil; end
855 if not next(self.active) then self:closeall(); end
857 -- was the query on the wanted list?
858 local q = response.question[1];
860 local cos = get(self.wanted, q.class, q.type, q.name);
862 for co in pairs(cos) do
863 set(self.yielded, co, q.class, q.type, q.name, nil);
864 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
866 set(self.wanted, q.class, q.type, q.name, nil);
874 function resolver:cancel(qclass, qtype, qname, co, call_handler)
875 local cos = get(self.wanted, qclass, qtype, qname);
878 coroutine.resume(co);
884 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
886 while self:receive() do end
887 if not next(self.active) then return nil; end
889 self.time = socket.gettime();
890 for id,queries in pairs(self.active) do
891 for question,o in pairs(queries) do
892 if self.time >= o.retry then
894 o.server = o.server + 1;
895 if o.server > #self.server then
897 o.delay = o.delay + 1;
900 if o.delay > #self.delays then
902 queries[question] = nil;
903 if not next(queries) then self.active[id] = nil; end
904 if not next(self.active) then return nil; end
906 --print('retry', o.server, o.delay);
907 local _a = self.socket[o.server];
908 if _a then _a:send(o.packet); end
909 o.retry = self.time + self.delays[o.delay];
915 if next(self.active) then return true; end
920 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
921 self:query (qname, qtype, qclass)
922 while self:pulse() do
924 for i, s in ipairs(self.socket) do
927 socket.select(recvt, nil, 4)
930 return self:peek(qname, qtype, qclass);
933 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
934 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
937 function resolver:tohostname(ip)
938 return dns.lookup(ip:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)", "%4.%3.%2.%1.in-addr.arpa."), "PTR");
941 --print ---------------------------------------------------------------- print
944 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
945 qr = { [0]='query', 'response' },
946 opcode = { [0]='query', 'inverse query', 'server status request' },
947 aa = { [0]='non-authoritative', 'authoritative' },
948 tc = { [0]='complete', 'truncated' },
949 rd = { [0]='recursion not desired', 'recursion desired' },
950 ra = { [0]='recursion not available', 'recursion available' },
951 z = { [0]='(reserved)' },
952 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
959 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
960 return (hints[s] and hints[s][p[s]]) or '';
964 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
965 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
966 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
967 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
970 for i,question in ipairs(response.question) do
971 print(string.format ('question[%i].name ', i), question.name);
972 print(string.format ('question[%i].type ', i), question.type);
973 print(string.format ('question[%i].class ', i), question.class);
976 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
978 for s,s in pairs({'answer', 'authority', 'additional'}) do
979 for i,rr in pairs(response[s]) do
980 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
981 tmp = string.format('%s[%i].%s', s, i, t);
982 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
984 for j,t in pairs(rr) do
985 if not common[j] then
986 tmp = string.format('%s[%i].%s', s, i, j);
987 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
995 -- module api ------------------------------------------------------ module api
998 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
999 -- this function seems to be redundant with resolver.new ()
1001 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {}, best_server = 1 };
1002 setmetatable (r, resolver);
1003 setmetatable (r.cache, cache_metatable);
1004 setmetatable (r.unsorted, { __mode = 'kv' });
1008 local _resolver = dns.resolver();
1009 dns._resolver = _resolver;
1011 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
1012 return _resolver:lookup(...);
1015 function dns.tohostname(...)
1016 return _resolver:tohostname(...);
1019 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
1020 return _resolver:purge(...);
1023 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
1024 return _resolver:peek(...);
1027 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
1028 return _resolver:query(...);
1031 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - - feed
1032 return _resolver:feed(...);
1035 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
1036 return _resolver:cancel(...);
1039 function dns.settimeout(...)
1040 return _resolver:settimeout(...);
1043 function dns.socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
1044 return _resolver:socket_wrapper_set(...);