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";
17 local new_ip = require "util.ip".new_ip;
19 local _, windows = pcall(require, "util.windows");
20 local is_windows = (_ and windows) or os.getenv("WINDIR");
22 local coroutine, io, math, string, table =
23 coroutine, io, math, string, table;
25 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type=
26 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type;
28 local ztact = { -- public domain 20080404 lua@ztact.com
29 get = function(parent, ...)
30 local len = select('#', ...);
32 parent = parent[select(i, ...)];
33 if parent == nil then break; end
37 set = function(parent, ...)
38 local len = select('#', ...);
39 local key, value = select(len-1, ...);
40 local cutpoint, cutkey;
43 local key = select (i, ...)
44 local child = parent[key]
49 elseif next(child, next(child)) then
50 cutpoint = nil; cutkey = nil;
51 elseif cutpoint == nil then
52 cutpoint = parent; cutkey = key;
54 elseif child == nil then
61 if value == nil and cutpoint then
62 cutpoint[cutkey] = nil;
69 local get, set = ztact.get, ztact.set;
71 local default_timeout = 15;
73 -------------------------------------------------- module dns
78 -- dns type & class codes ------------------------------ dns type & class codes
81 local append = table.insert
84 local function highbyte(i) -- - - - - - - - - - - - - - - - - - - highbyte
85 return (i-(i%0x100))/0x100;
89 local function augment (t) -- - - - - - - - - - - - - - - - - - - - augment
91 for i,s in pairs(t) do
94 a[string.lower(s)] = s;
100 local function encode (t) -- - - - - - - - - - - - - - - - - - - - - encode
102 for i,s in pairs(t) do
103 local word = string.char(highbyte(i), i%0x100);
106 code[string.lower(s)] = word;
113 'A', 'NS', 'MD', 'MF', 'CNAME', 'SOA', 'MB', 'MG', 'MR', 'NULL', 'WKS',
114 'PTR', 'HINFO', 'MINFO', 'MX', 'TXT',
115 [ 28] = 'AAAA', [ 29] = 'LOC', [ 33] = 'SRV',
116 [252] = 'AXFR', [253] = 'MAILB', [254] = 'MAILA', [255] = '*' };
119 dns.classes = { 'IN', 'CS', 'CH', 'HS', [255] = '*' };
122 dns.type = augment (dns.types);
123 dns.class = augment (dns.classes);
124 dns.typecode = encode (dns.types);
125 dns.classcode = encode (dns.classes);
129 local function standardize(qname, qtype, qclass) -- - - - - - - standardize
130 if string.byte(qname, -1) ~= 0x2E then qname = qname..'.'; end
131 qname = string.lower(qname);
132 return qname, dns.type[qtype or 'A'], dns.class[qclass or 'IN'];
136 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune
137 time = time or socket.gettime();
138 for i,rr in pairs(rrs) do
140 -- rr.tod = rr.tod - 50 -- accelerated decripitude
141 rr.ttl = math.floor(rr.tod - time);
143 table.remove(rrs, i);
144 return prune(rrs, time, soft); -- Re-iterate
146 elseif soft == 'soft' then -- What is this? I forget!
154 -- metatables & co. ------------------------------------------ metatables & co.
158 resolver.__index = resolver;
160 resolver.timeout = default_timeout;
162 local function default_rr_tostring(rr)
163 local rr_val = rr.type and rr[rr.type:lower()];
164 if type(rr_val) ~= "string" then
165 return "<UNKNOWN RDATA TYPE>";
170 local special_tostrings = {
171 LOC = resolver.LOC_tostring;
173 return string.format('%2i %s', rr.pref, rr.mx);
177 return string.format('%5d %5d %5d %s', s.priority, s.weight, s.port, s.target);
181 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
182 function rr_metatable.__tostring(rr)
183 local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr);
184 return string.format('%2s %-5s %6i %-28s %s', rr.class, rr.type, rr.ttl, rr.name, rr_string);
188 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
189 function rrs_metatable.__tostring(rrs)
191 for i,rr in pairs(rrs) do
192 append(t, tostring(rr)..'\n');
194 return table.concat(t);
198 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable
199 function cache_metatable.__tostring(cache)
200 local time = socket.gettime();
202 for class,types in pairs(cache) do
203 for type,names in pairs(types) do
204 for name,rrs in pairs(names) do
206 append(t, tostring(rrs));
210 return table.concat(t);
214 function resolver:new() -- - - - - - - - - - - - - - - - - - - - - resolver
215 local r = { active = {}, cache = {}, unsorted = {} };
216 setmetatable(r, resolver);
217 setmetatable(r.cache, cache_metatable);
218 setmetatable(r.unsorted, { __mode = 'kv' });
223 -- packet layer -------------------------------------------------- packet layer
226 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
227 math.randomseed(math.floor(10000*socket.gettime()) % 0x100000000);
228 dns.random = math.random;
229 return dns.random(...);
233 local function encodeHeader(o) -- - - - - - - - - - - - - - - encodeHeader
235 o.id = o.id or dns.random(0, 0xffff); -- 16b (random) id
237 o.rd = o.rd or 1; -- 1b 1 recursion desired
238 o.tc = o.tc or 0; -- 1b 1 truncated response
239 o.aa = o.aa or 0; -- 1b 1 authoritative response
240 o.opcode = o.opcode or 0; -- 4b 0 query
242 -- 2 server status request
244 o.qr = o.qr or 0; -- 1b 0 query, 1 response
246 o.rcode = o.rcode or 0; -- 4b 0 no error
253 o.z = o.z or 0; -- 3b 0 resvered
254 o.ra = o.ra or 0; -- 1b 1 recursion available
256 o.qdcount = o.qdcount or 1; -- 16b number of question RRs
257 o.ancount = o.ancount or 0; -- 16b number of answers RRs
258 o.nscount = o.nscount or 0; -- 16b number of nameservers RRs
259 o.arcount = o.arcount or 0; -- 16b number of additional RRs
261 -- string.char() rounds, so prevent roundup with -0.4999
262 local header = string.char(
263 highbyte(o.id), o.id %0x100,
264 o.rd + 2*o.tc + 4*o.aa + 8*o.opcode + 128*o.qr,
265 o.rcode + 16*o.z + 128*o.ra,
266 highbyte(o.qdcount), o.qdcount %0x100,
267 highbyte(o.ancount), o.ancount %0x100,
268 highbyte(o.nscount), o.nscount %0x100,
269 highbyte(o.arcount), o.arcount %0x100
276 local function encodeName(name) -- - - - - - - - - - - - - - - - encodeName
278 for part in string.gmatch(name, '[^.]+') do
279 append(t, string.char(string.len(part)));
282 append(t, string.char(0));
283 return table.concat(t);
287 local function encodeQuestion(qname, qtype, qclass) -- - - - encodeQuestion
288 qname = encodeName(qname);
289 qtype = dns.typecode[qtype or 'a'];
290 qclass = dns.classcode[qclass or 'in'];
291 return qname..qtype..qclass;
295 function resolver:byte(len) -- - - - - - - - - - - - - - - - - - - - - byte
297 local offset = self.offset;
298 local last = offset + len - 1;
299 if last > #self.packet then
300 error(string.format('out of bounds: %i>%i', last, #self.packet));
302 self.offset = offset + len;
303 return string.byte(self.packet, offset, last);
307 function resolver:word() -- - - - - - - - - - - - - - - - - - - - - - word
308 local b1, b2 = self:byte(2);
309 return 0x100*b1 + b2;
313 function resolver:dword () -- - - - - - - - - - - - - - - - - - - - - dword
314 local b1, b2, b3, b4 = self:byte(4);
315 --print('dword', b1, b2, b3, b4);
316 return 0x1000000*b1 + 0x10000*b2 + 0x100*b3 + b4;
320 function resolver:sub(len) -- - - - - - - - - - - - - - - - - - - - - - sub
322 local s = string.sub(self.packet, self.offset, self.offset + len - 1);
323 self.offset = self.offset + len;
328 function resolver:header(force) -- - - - - - - - - - - - - - - - - - header
329 local id = self:word();
330 --print(string.format(':header id %x', id));
331 if not self.active[id] and not force then return nil; end
333 local h = { id = id };
335 local b1, b2 = self:byte(2);
347 h.qdcount = self:word();
348 h.ancount = self:word();
349 h.nscount = self:word();
350 h.arcount = self:word();
352 for k,v in pairs(h) do h[k] = v-v%1; end
358 function resolver:name() -- - - - - - - - - - - - - - - - - - - - - - name
359 local remember, pointers = nil, 0;
360 local len = self:byte();
362 if len == 0 then return "." end -- Root label
364 if len >= 0xc0 then -- name is "compressed"
365 pointers = pointers + 1;
366 if pointers >= 20 then error('dns error: 20 pointers'); end;
367 local offset = ((len-0xc0)*0x100) + self:byte();
368 remember = remember or self.offset;
369 self.offset = offset + 1; -- +1 for lua
370 else -- name is not compressed
371 append(n, self:sub(len)..'.');
375 self.offset = remember or self.offset;
376 return table.concat(n);
380 function resolver:question() -- - - - - - - - - - - - - - - - - - question
382 q.name = self:name();
383 q.type = dns.type[self:word()];
384 q.class = dns.class[self:word()];
389 function resolver:A(rr) -- - - - - - - - - - - - - - - - - - - - - - - - A
390 local b1, b2, b3, b4 = self:byte(4);
391 rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
394 function resolver:AAAA(rr)
396 for i = 1, rr.rdlength, 2 do
397 local b1, b2 = self:byte(2);
398 table.insert(addr, ("%02x%02x"):format(b1, b2));
400 addr = table.concat(addr, ":"):gsub("%f[%x]0+(%x)","%1");
402 for item in addr:gmatch(":[0:]+:") do
403 table.insert(zeros, item)
408 elseif #zeros > 1 then
409 table.sort(zeros, function(a, b) return #a > #b end);
411 rr.aaaa = addr:gsub(zeros[1], "::", 1):gsub("^0::", "::"):gsub("::0$", "::");
414 function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
415 rr.cname = self:name();
419 function resolver:MX(rr) -- - - - - - - - - - - - - - - - - - - - - - - MX
420 rr.pref = self:word();
425 function resolver:LOC_nibble_power() -- - - - - - - - - - LOC_nibble_power
426 local b = self:byte();
427 --print('nibbles', ((b-(b%0x10))/0x10), (b%0x10));
428 return ((b-(b%0x10))/0x10) * (10^(b%0x10));
432 function resolver:LOC(rr) -- - - - - - - - - - - - - - - - - - - - - - LOC
433 rr.version = self:byte();
434 if rr.version == 0 then
435 rr.loc = rr.loc or {};
436 rr.loc.size = self:LOC_nibble_power();
437 rr.loc.horiz_pre = self:LOC_nibble_power();
438 rr.loc.vert_pre = self:LOC_nibble_power();
439 rr.loc.latitude = self:dword();
440 rr.loc.longitude = self:dword();
441 rr.loc.altitude = self:dword();
446 local function LOC_tostring_degrees(f, pos, neg) -- - - - - - - - - - - - -
448 if f < 0 then pos = neg; f = -f; end
449 local deg, min, msec;
454 return string.format('%3d %2d %2.3f %s', deg, min, msec/1000, pos);
458 function resolver.LOC_tostring(rr) -- - - - - - - - - - - - - LOC_tostring
462 for k,name in pairs { 'size', 'horiz_pre', 'vert_pre', 'latitude', 'longitude', 'altitude' } do
463 append(t, string.format('%4s%-10s: %12.0f\n', '', name, rr.loc[name]));
467 append(t, string.format(
468 '%s %s %.2fm %.2fm %.2fm %.2fm',
469 LOC_tostring_degrees (rr.loc.latitude, 'N', 'S'),
470 LOC_tostring_degrees (rr.loc.longitude, 'E', 'W'),
471 (rr.loc.altitude - 10000000) / 100,
473 rr.loc.horiz_pre / 100,
474 rr.loc.vert_pre / 100
477 return table.concat(t);
481 function resolver:NS(rr) -- - - - - - - - - - - - - - - - - - - - - - - NS
486 function resolver:SOA(rr) -- - - - - - - - - - - - - - - - - - - - - - SOA
490 function resolver:SRV(rr) -- - - - - - - - - - - - - - - - - - - - - - SRV
492 rr.srv.priority = self:word();
493 rr.srv.weight = self:word();
494 rr.srv.port = self:word();
495 rr.srv.target = self:name();
498 function resolver:PTR(rr)
499 rr.ptr = self:name();
502 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
503 rr.txt = self:sub (self:byte());
507 function resolver:rr() -- - - - - - - - - - - - - - - - - - - - - - - - rr
509 setmetatable(rr, rr_metatable);
510 rr.name = self:name(self);
511 rr.type = dns.type[self:word()] or rr.type;
512 rr.class = dns.class[self:word()] or rr.class;
513 rr.ttl = 0x10000*self:word() + self:word();
514 rr.rdlength = self:word();
517 rr.tod = self.time + 30;
519 rr.tod = self.time + rr.ttl;
522 local remember = self.offset;
523 local rr_parser = self[dns.type[rr.type]];
524 if rr_parser then rr_parser(self, rr); end
525 self.offset = remember;
526 rr.rdata = self:sub(rr.rdlength);
531 function resolver:rrs (count) -- - - - - - - - - - - - - - - - - - - - - rrs
533 for i = 1,count do append(rrs, self:rr()); end
538 function resolver:decode(packet, force) -- - - - - - - - - - - - - - decode
539 self.packet, self.offset = packet, 1;
540 local header = self:header(force);
541 if not header then return nil; end
542 local response = { header = header };
544 response.question = {};
545 local offset = self.offset;
546 for i = 1,response.header.qdcount do
547 append(response.question, self:question());
549 response.question.raw = string.sub(self.packet, offset, self.offset - 1);
552 if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
553 self.active[response.header.id] = nil;
558 response.answer = self:rrs(response.header.ancount);
559 response.authority = self:rrs(response.header.nscount);
560 response.additional = self:rrs(response.header.arcount);
566 -- socket layer -------------------------------------------------- socket layer
569 resolver.delays = { 1, 3 };
572 function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
573 self.server = self.server or {};
574 append(self.server, address);
578 function resolver:setnameserver(address) -- - - - - - - - - - setnameserver
580 self:addnameserver(address);
584 function resolver:adddefaultnameservers() -- - - - - adddefaultnameservers
586 if windows and windows.get_nameservers then
587 for _, server in ipairs(windows.get_nameservers()) do
588 self:addnameserver(server);
591 if not self.server or #self.server == 0 then
592 -- TODO log warning about no nameservers, adding opendns servers as fallback
593 self:addnameserver("208.67.222.222");
594 self:addnameserver("208.67.220.220");
597 local resolv_conf = io.open("/etc/resolv.conf");
599 for line in resolv_conf:lines() do
600 line = line:gsub("#.*$", "")
601 :match('^%s*nameserver%s+([%x:%.]*)%s*$');
603 local ip = new_ip(line);
605 self:addnameserver(ip.addr);
610 if not self.server or #self.server == 0 then
611 -- TODO log warning about no nameservers, adding localhost as the default nameserver
612 self:addnameserver("127.0.0.1");
618 function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket
619 self.socket = self.socket or {};
620 self.socketset = self.socketset or {};
622 local sock = self.socket[servernum];
623 if sock then return sock; end
626 local peer = self.server[servernum];
627 if peer:find(":") then
628 sock, err = socket.udp6();
630 sock, err = socket.udp();
632 if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end
637 -- todo: attempt to use a random port, fallback to 0
638 sock:setsockname('*', 0);
639 sock:setpeername(peer, 53);
640 self.socket[servernum] = sock;
641 self.socketset[sock] = servernum;
645 function resolver:voidsocket(sock)
646 if self.socket[sock] then
647 self.socketset[self.socket[sock]] = nil;
648 self.socket[sock] = nil;
649 elseif self.socketset[sock] then
650 self.socket[self.socketset[sock]] = nil;
651 self.socketset[sock] = nil;
656 function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
657 self.socket_wrapper = func;
661 function resolver:closeall () -- - - - - - - - - - - - - - - - - - closeall
662 for i,sock in ipairs(self.socket) do
663 self.socket[i] = nil;
664 self.socketset[sock] = nil;
670 function resolver:remember(rr, type) -- - - - - - - - - - - - - - remember
671 --print ('remember', type, rr.class, rr.type, rr.name)
672 local qname, qtype, qclass = standardize(rr.name, rr.type, rr.class);
676 local all = get(self.cache, qclass, '*', qname);
677 --print('remember all', all);
678 if all then append(all, rr); end
681 self.cache = self.cache or setmetatable({}, cache_metatable);
682 local rrs = get(self.cache, qclass, type, qname) or
683 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
686 if type == 'MX' then self.unsorted[rrs] = true; end
690 local function comp_mx(a, b) -- - - - - - - - - - - - - - - - - - - comp_mx
691 return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
695 function resolver:peek (qname, qtype, qclass) -- - - - - - - - - - - - peek
696 qname, qtype, qclass = standardize(qname, qtype, qclass);
697 local rrs = get(self.cache, qclass, qtype, qname);
698 if not rrs then return nil; end
699 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
700 set(self.cache, qclass, qtype, qname, nil);
703 if self.unsorted[rrs] then table.sort (rrs, comp_mx); end
708 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
709 if soft == 'soft' then
710 self.time = socket.gettime();
711 for class,types in pairs(self.cache or {}) do
712 for type,names in pairs(types) do
713 for name,rrs in pairs(names) do
714 prune(rrs, self.time, 'soft')
718 else self.cache = setmetatable({}, cache_metatable); end
722 function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
723 qname, qtype, qclass = standardize(qname, qtype, qclass)
725 local co = coroutine.running();
726 local q = get(self.wanted, qclass, qtype, qname);
728 -- We are already waiting for a reply to an identical query.
729 set(self.wanted, qclass, qtype, qname, co, true);
733 if not self.server then self:adddefaultnameservers(); end
735 local question = encodeQuestion(qname, qtype, qclass);
736 local peek = self:peek (qname, qtype, qclass);
737 if peek then return peek; end
739 local header, id = encodeHeader();
740 --print ('query id', id, qclass, qtype, qname)
742 packet = header..question,
743 server = self.best_server,
745 retry = socket.gettime() + self.delays[1]
748 -- remember the query
749 self.active[id] = self.active[id] or {};
750 self.active[id][question] = o;
752 -- remember which coroutine wants the answer
754 set(self.wanted, qclass, qtype, qname, co, true);
757 local conn, err = self:getsocket(o.server)
763 if timer and self.timeout then
764 local num_servers = #self.server;
766 timer.add_task(self.timeout, function ()
767 if get(self.wanted, qclass, qtype, qname, co) then
768 if i < num_servers then
771 o.server = self.best_server;
772 conn, err = self:getsocket(o.server);
778 -- Tried everything, failed
779 self:cancel(qclass, qtype, qname);
786 function resolver:servfail(sock)
787 -- Resend all queries for this server
789 local num = self.socketset[sock]
791 -- Socket is dead now
792 self:voidsocket(sock);
794 -- Find all requests to the down server, and retry on the next server
795 self.time = socket.gettime();
796 for id,queries in pairs(self.active) do
797 for question,o in pairs(queries) do
798 if o.server == num then -- This request was to the broken server
799 o.server = o.server + 1 -- Use next server
800 if o.server > #self.server then
804 o.retries = (o.retries or 0) + 1;
805 if o.retries >= #self.server then
807 queries[question] = nil;
809 local _a = self:getsocket(o.server);
810 if _a then _a:send(o.packet); end
814 if next(queries) == nil then
815 self.active[id] = nil;
819 if num == self.best_server then
820 self.best_server = self.best_server + 1;
821 if self.best_server > #self.server then
822 -- Exhausted all servers, try first again
823 self.best_server = 1;
828 function resolver:settimeout(seconds)
829 self.timeout = seconds;
832 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
833 --print('receive'); print(self.socket);
834 self.time = socket.gettime();
835 rset = rset or self.socket;
838 for i,sock in pairs(rset) do
840 if self.socketset[sock] then
841 local packet = sock:receive();
843 response = self:decode(packet);
844 if response and self.active[response.header.id]
845 and self.active[response.header.id][response.question.raw] then
846 --print('received response');
847 --self.print(response);
849 for j,rr in pairs(response.answer) do
850 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
851 self:remember(rr, response.question[1].type)
856 local queries = self.active[response.header.id];
857 queries[response.question.raw] = nil;
859 if not next(queries) then self.active[response.header.id] = nil; end
860 if not next(self.active) then self:closeall(); end
862 -- was the query on the wanted list?
863 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 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
869 set(self.wanted, q.class, q.type, q.name, nil);
881 function resolver:feed(sock, packet, force)
882 --print('receive'); print(self.socket);
883 self.time = socket.gettime();
885 local response = self:decode(packet, force);
886 if response and self.active[response.header.id]
887 and self.active[response.header.id][response.question.raw] then
888 --print('received response');
889 --self.print(response);
891 for j,rr in pairs(response.answer) do
892 self:remember(rr, response.question[1].type);
896 local queries = self.active[response.header.id];
897 queries[response.question.raw] = nil;
898 if not next(queries) then self.active[response.header.id] = nil; end
899 if not next(self.active) then self:closeall(); end
901 -- was the query on the wanted list?
902 local q = response.question[1];
904 local cos = get(self.wanted, q.class, q.type, q.name);
906 for co in pairs(cos) do
907 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
909 set(self.wanted, q.class, q.type, q.name, nil);
917 function resolver:cancel(qclass, qtype, qname)
918 local cos = get(self.wanted, qclass, qtype, qname);
920 for co in pairs(cos) do
921 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
923 set(self.wanted, qclass, qtype, qname, nil);
927 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
929 while self:receive() do end
930 if not next(self.active) then return nil; end
932 self.time = socket.gettime();
933 for id,queries in pairs(self.active) do
934 for question,o in pairs(queries) do
935 if self.time >= o.retry then
937 o.server = o.server + 1;
938 if o.server > #self.server then
940 o.delay = o.delay + 1;
943 if o.delay > #self.delays then
945 queries[question] = nil;
946 if not next(queries) then self.active[id] = nil; end
947 if not next(self.active) then return nil; end
949 --print('retry', o.server, o.delay);
950 local _a = self.socket[o.server];
951 if _a then _a:send(o.packet); end
952 o.retry = self.time + self.delays[o.delay];
958 if next(self.active) then return true; end
963 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
964 self:query (qname, qtype, qclass)
965 while self:pulse() do
967 for i, s in ipairs(self.socket) do
970 socket.select(recvt, nil, 4)
973 return self:peek(qname, qtype, qclass);
976 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
977 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
980 function resolver:tohostname(ip)
981 return dns.lookup(ip:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)", "%4.%3.%2.%1.in-addr.arpa."), "PTR");
984 --print ---------------------------------------------------------------- print
987 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
988 qr = { [0]='query', 'response' },
989 opcode = { [0]='query', 'inverse query', 'server status request' },
990 aa = { [0]='non-authoritative', 'authoritative' },
991 tc = { [0]='complete', 'truncated' },
992 rd = { [0]='recursion not desired', 'recursion desired' },
993 ra = { [0]='recursion not available', 'recursion available' },
994 z = { [0]='(reserved)' },
995 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
1002 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
1003 return (hints[s] and hints[s][p[s]]) or '';
1007 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
1008 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
1009 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
1010 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
1013 for i,question in ipairs(response.question) do
1014 print(string.format ('question[%i].name ', i), question.name);
1015 print(string.format ('question[%i].type ', i), question.type);
1016 print(string.format ('question[%i].class ', i), question.class);
1019 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
1021 for s,s in pairs({'answer', 'authority', 'additional'}) do
1022 for i,rr in pairs(response[s]) do
1023 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
1024 tmp = string.format('%s[%i].%s', s, i, t);
1025 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
1027 for j,t in pairs(rr) do
1028 if not common[j] then
1029 tmp = string.format('%s[%i].%s', s, i, j);
1030 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
1038 -- module api ------------------------------------------------------ module api
1041 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
1042 -- this function seems to be redundant with resolver.new ()
1044 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, best_server = 1 };
1045 setmetatable (r, resolver);
1046 setmetatable (r.cache, cache_metatable);
1047 setmetatable (r.unsorted, { __mode = 'kv' });
1051 local _resolver = dns.resolver();
1052 dns._resolver = _resolver;
1054 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
1055 return _resolver:lookup(...);
1058 function dns.tohostname(...)
1059 return _resolver:tohostname(...);
1062 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
1063 return _resolver:purge(...);
1066 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
1067 return _resolver:peek(...);
1070 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
1071 return _resolver:query(...);
1074 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - - feed
1075 return _resolver:feed(...);
1078 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
1079 return _resolver:cancel(...);
1082 function dns.settimeout(...)
1083 return _resolver:settimeout(...);
1086 function dns.cache()
1087 return _resolver.cache;
1090 function dns.socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
1091 return _resolver:socket_wrapper_set(...);