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 if not self.server then self:adddefaultnameservers(); end
727 local question = encodeQuestion(qname, qtype, qclass);
728 local peek = self:peek (qname, qtype, qclass);
729 if peek then return peek; end
731 local header, id = encodeHeader();
732 --print ('query id', id, qclass, qtype, qname)
734 packet = header..question,
735 server = self.best_server,
737 retry = socket.gettime() + self.delays[1]
740 -- remember the query
741 self.active[id] = self.active[id] or {};
742 self.active[id][question] = o;
744 -- remember which coroutine wants the answer
745 local co = coroutine.running();
747 set(self.wanted, qclass, qtype, qname, co, true);
748 --set(self.yielded, co, qclass, qtype, qname, true);
751 local conn, err = self:getsocket(o.server)
757 if timer and self.timeout then
758 local num_servers = #self.server;
760 timer.add_task(self.timeout, function ()
761 if get(self.wanted, qclass, qtype, qname, co) then
762 if i < num_servers then
765 o.server = self.best_server;
766 conn, err = self:getsocket(o.server);
772 -- Tried everything, failed
773 self:cancel(qclass, qtype, qname, co, true);
780 function resolver:servfail(sock)
781 -- Resend all queries for this server
783 local num = self.socketset[sock]
785 -- Socket is dead now
786 self:voidsocket(sock);
788 -- Find all requests to the down server, and retry on the next server
789 self.time = socket.gettime();
790 for id,queries in pairs(self.active) do
791 for question,o in pairs(queries) do
792 if o.server == num then -- This request was to the broken server
793 o.server = o.server + 1 -- Use next server
794 if o.server > #self.server then
798 o.retries = (o.retries or 0) + 1;
799 if o.retries >= #self.server then
801 queries[question] = nil;
803 local _a = self:getsocket(o.server);
804 if _a then _a:send(o.packet); end
808 if next(queries) == nil then
809 self.active[id] = nil;
813 if num == self.best_server then
814 self.best_server = self.best_server + 1;
815 if self.best_server > #self.server then
816 -- Exhausted all servers, try first again
817 self.best_server = 1;
822 function resolver:settimeout(seconds)
823 self.timeout = seconds;
826 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
827 --print('receive'); print(self.socket);
828 self.time = socket.gettime();
829 rset = rset or self.socket;
832 for i,sock in pairs(rset) do
834 if self.socketset[sock] then
835 local packet = sock:receive();
837 response = self:decode(packet);
838 if response and self.active[response.header.id]
839 and self.active[response.header.id][response.question.raw] then
840 --print('received response');
841 --self.print(response);
843 for j,rr in pairs(response.answer) do
844 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
845 self:remember(rr, response.question[1].type)
850 local queries = self.active[response.header.id];
851 queries[response.question.raw] = nil;
853 if not next(queries) then self.active[response.header.id] = nil; end
854 if not next(self.active) then self:closeall(); end
856 -- was the query on the wanted list?
857 local q = response.question[1];
858 local cos = get(self.wanted, q.class, q.type, q.name);
860 for co in pairs(cos) do
861 set(self.yielded, co, q.class, q.type, q.name, nil);
862 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
864 set(self.wanted, q.class, q.type, q.name, nil);
876 function resolver:feed(sock, packet, force)
877 --print('receive'); print(self.socket);
878 self.time = socket.gettime();
880 local response = self:decode(packet, force);
881 if response and self.active[response.header.id]
882 and self.active[response.header.id][response.question.raw] then
883 --print('received response');
884 --self.print(response);
886 for j,rr in pairs(response.answer) do
887 self:remember(rr, response.question[1].type);
891 local queries = self.active[response.header.id];
892 queries[response.question.raw] = nil;
893 if not next(queries) then self.active[response.header.id] = nil; end
894 if not next(self.active) then self:closeall(); end
896 -- was the query on the wanted list?
897 local q = response.question[1];
899 local cos = get(self.wanted, q.class, q.type, q.name);
901 for co in pairs(cos) do
902 set(self.yielded, co, q.class, q.type, q.name, nil);
903 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
905 set(self.wanted, q.class, q.type, q.name, nil);
913 function resolver:cancel(qclass, qtype, qname, co, call_handler)
914 local cos = get(self.wanted, qclass, qtype, qname);
917 coroutine.resume(co);
923 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
925 while self:receive() do end
926 if not next(self.active) then return nil; end
928 self.time = socket.gettime();
929 for id,queries in pairs(self.active) do
930 for question,o in pairs(queries) do
931 if self.time >= o.retry then
933 o.server = o.server + 1;
934 if o.server > #self.server then
936 o.delay = o.delay + 1;
939 if o.delay > #self.delays then
941 queries[question] = nil;
942 if not next(queries) then self.active[id] = nil; end
943 if not next(self.active) then return nil; end
945 --print('retry', o.server, o.delay);
946 local _a = self.socket[o.server];
947 if _a then _a:send(o.packet); end
948 o.retry = self.time + self.delays[o.delay];
954 if next(self.active) then return true; end
959 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
960 self:query (qname, qtype, qclass)
961 while self:pulse() do
963 for i, s in ipairs(self.socket) do
966 socket.select(recvt, nil, 4)
969 return self:peek(qname, qtype, qclass);
972 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
973 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
976 function resolver:tohostname(ip)
977 return dns.lookup(ip:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)", "%4.%3.%2.%1.in-addr.arpa."), "PTR");
980 --print ---------------------------------------------------------------- print
983 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
984 qr = { [0]='query', 'response' },
985 opcode = { [0]='query', 'inverse query', 'server status request' },
986 aa = { [0]='non-authoritative', 'authoritative' },
987 tc = { [0]='complete', 'truncated' },
988 rd = { [0]='recursion not desired', 'recursion desired' },
989 ra = { [0]='recursion not available', 'recursion available' },
990 z = { [0]='(reserved)' },
991 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
998 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
999 return (hints[s] and hints[s][p[s]]) or '';
1003 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
1004 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
1005 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
1006 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
1009 for i,question in ipairs(response.question) do
1010 print(string.format ('question[%i].name ', i), question.name);
1011 print(string.format ('question[%i].type ', i), question.type);
1012 print(string.format ('question[%i].class ', i), question.class);
1015 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
1017 for s,s in pairs({'answer', 'authority', 'additional'}) do
1018 for i,rr in pairs(response[s]) do
1019 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
1020 tmp = string.format('%s[%i].%s', s, i, t);
1021 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
1023 for j,t in pairs(rr) do
1024 if not common[j] then
1025 tmp = string.format('%s[%i].%s', s, i, j);
1026 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
1034 -- module api ------------------------------------------------------ module api
1037 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
1038 -- this function seems to be redundant with resolver.new ()
1040 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {}, best_server = 1 };
1041 setmetatable (r, resolver);
1042 setmetatable (r.cache, cache_metatable);
1043 setmetatable (r.unsorted, { __mode = 'kv' });
1047 local _resolver = dns.resolver();
1048 dns._resolver = _resolver;
1050 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
1051 return _resolver:lookup(...);
1054 function dns.tohostname(...)
1055 return _resolver:tohostname(...);
1058 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
1059 return _resolver:purge(...);
1062 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
1063 return _resolver:peek(...);
1066 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
1067 return _resolver:query(...);
1070 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - - feed
1071 return _resolver:feed(...);
1074 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
1075 return _resolver:cancel(...);
1078 function dns.settimeout(...)
1079 return _resolver:settimeout(...);
1082 function dns.cache()
1083 return _resolver.cache;
1086 function dns.socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
1087 return _resolver:socket_wrapper_set(...);