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 ipairs(rrs) do
140 -- rr.tod = rr.tod - 50 -- accelerated decripitude
141 rr.ttl = math.floor(rr.tod - time);
143 rrs[rr[rr.type:lower()]] = nil;
144 table.remove(rrs, i);
145 return prune(rrs, time, soft); -- Re-iterate
147 elseif soft == 'soft' then -- What is this? I forget!
149 rrs[rr[rr.type:lower()]] = nil;
150 table.remove(rrs, i);
156 -- metatables & co. ------------------------------------------ metatables & co.
160 resolver.__index = resolver;
162 resolver.timeout = default_timeout;
164 local function default_rr_tostring(rr)
165 local rr_val = rr.type and rr[rr.type:lower()];
166 if type(rr_val) ~= "string" then
167 return "<UNKNOWN RDATA TYPE>";
172 local special_tostrings = {
173 LOC = resolver.LOC_tostring;
175 return string.format('%2i %s', rr.pref, rr.mx);
179 return string.format('%5d %5d %5d %s', s.priority, s.weight, s.port, s.target);
183 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
184 function rr_metatable.__tostring(rr)
185 local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr);
186 return string.format('%2s %-5s %6i %-28s %s', rr.class, rr.type, rr.ttl, rr.name, rr_string);
190 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
191 function rrs_metatable.__tostring(rrs)
193 for i,rr in ipairs(rrs) do
194 append(t, tostring(rr)..'\n');
196 return table.concat(t);
200 local cache_metatable = {}; -- - - - - - - - - - - - - - - - cache_metatable
201 function cache_metatable.__tostring(cache)
202 local time = socket.gettime();
204 for class,types in pairs(cache) do
205 for type,names in pairs(types) do
206 for name,rrs in pairs(names) do
208 append(t, tostring(rrs));
212 return table.concat(t);
216 function resolver:new() -- - - - - - - - - - - - - - - - - - - - - resolver
217 local r = { active = {}, cache = {}, unsorted = {} };
218 setmetatable(r, resolver);
219 setmetatable(r.cache, cache_metatable);
220 setmetatable(r.unsorted, { __mode = 'kv' });
225 -- packet layer -------------------------------------------------- packet layer
228 function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
229 math.randomseed(math.floor(10000*socket.gettime()) % 0x80000000);
230 dns.random = math.random;
231 return dns.random(...);
235 local function encodeHeader(o) -- - - - - - - - - - - - - - - encodeHeader
237 o.id = o.id or dns.random(0, 0xffff); -- 16b (random) id
239 o.rd = o.rd or 1; -- 1b 1 recursion desired
240 o.tc = o.tc or 0; -- 1b 1 truncated response
241 o.aa = o.aa or 0; -- 1b 1 authoritative response
242 o.opcode = o.opcode or 0; -- 4b 0 query
244 -- 2 server status request
246 o.qr = o.qr or 0; -- 1b 0 query, 1 response
248 o.rcode = o.rcode or 0; -- 4b 0 no error
255 o.z = o.z or 0; -- 3b 0 resvered
256 o.ra = o.ra or 0; -- 1b 1 recursion available
258 o.qdcount = o.qdcount or 1; -- 16b number of question RRs
259 o.ancount = o.ancount or 0; -- 16b number of answers RRs
260 o.nscount = o.nscount or 0; -- 16b number of nameservers RRs
261 o.arcount = o.arcount or 0; -- 16b number of additional RRs
263 -- string.char() rounds, so prevent roundup with -0.4999
264 local header = string.char(
265 highbyte(o.id), o.id %0x100,
266 o.rd + 2*o.tc + 4*o.aa + 8*o.opcode + 128*o.qr,
267 o.rcode + 16*o.z + 128*o.ra,
268 highbyte(o.qdcount), o.qdcount %0x100,
269 highbyte(o.ancount), o.ancount %0x100,
270 highbyte(o.nscount), o.nscount %0x100,
271 highbyte(o.arcount), o.arcount %0x100
278 local function encodeName(name) -- - - - - - - - - - - - - - - - encodeName
280 for part in string.gmatch(name, '[^.]+') do
281 append(t, string.char(string.len(part)));
284 append(t, string.char(0));
285 return table.concat(t);
289 local function encodeQuestion(qname, qtype, qclass) -- - - - encodeQuestion
290 qname = encodeName(qname);
291 qtype = dns.typecode[qtype or 'a'];
292 qclass = dns.classcode[qclass or 'in'];
293 return qname..qtype..qclass;
297 function resolver:byte(len) -- - - - - - - - - - - - - - - - - - - - - byte
299 local offset = self.offset;
300 local last = offset + len - 1;
301 if last > #self.packet then
302 error(string.format('out of bounds: %i>%i', last, #self.packet));
304 self.offset = offset + len;
305 return string.byte(self.packet, offset, last);
309 function resolver:word() -- - - - - - - - - - - - - - - - - - - - - - word
310 local b1, b2 = self:byte(2);
311 return 0x100*b1 + b2;
315 function resolver:dword () -- - - - - - - - - - - - - - - - - - - - - dword
316 local b1, b2, b3, b4 = self:byte(4);
317 --print('dword', b1, b2, b3, b4);
318 return 0x1000000*b1 + 0x10000*b2 + 0x100*b3 + b4;
322 function resolver:sub(len) -- - - - - - - - - - - - - - - - - - - - - - sub
324 local s = string.sub(self.packet, self.offset, self.offset + len - 1);
325 self.offset = self.offset + len;
330 function resolver:header(force) -- - - - - - - - - - - - - - - - - - header
331 local id = self:word();
332 --print(string.format(':header id %x', id));
333 if not self.active[id] and not force then return nil; end
335 local h = { id = id };
337 local b1, b2 = self:byte(2);
349 h.qdcount = self:word();
350 h.ancount = self:word();
351 h.nscount = self:word();
352 h.arcount = self:word();
354 for k,v in pairs(h) do h[k] = v-v%1; end
360 function resolver:name() -- - - - - - - - - - - - - - - - - - - - - - name
361 local remember, pointers = nil, 0;
362 local len = self:byte();
364 if len == 0 then return "." end -- Root label
366 if len >= 0xc0 then -- name is "compressed"
367 pointers = pointers + 1;
368 if pointers >= 20 then error('dns error: 20 pointers'); end;
369 local offset = ((len-0xc0)*0x100) + self:byte();
370 remember = remember or self.offset;
371 self.offset = offset + 1; -- +1 for lua
372 else -- name is not compressed
373 append(n, self:sub(len)..'.');
377 self.offset = remember or self.offset;
378 return table.concat(n);
382 function resolver:question() -- - - - - - - - - - - - - - - - - - question
384 q.name = self:name();
385 q.type = dns.type[self:word()];
386 q.class = dns.class[self:word()];
391 function resolver:A(rr) -- - - - - - - - - - - - - - - - - - - - - - - - A
392 local b1, b2, b3, b4 = self:byte(4);
393 rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
396 function resolver:AAAA(rr)
398 for i = 1, rr.rdlength, 2 do
399 local b1, b2 = self:byte(2);
400 table.insert(addr, ("%02x%02x"):format(b1, b2));
402 addr = table.concat(addr, ":"):gsub("%f[%x]0+(%x)","%1");
404 for item in addr:gmatch(":[0:]+:") do
405 table.insert(zeros, item)
410 elseif #zeros > 1 then
411 table.sort(zeros, function(a, b) return #a > #b end);
413 rr.aaaa = addr:gsub(zeros[1], "::", 1):gsub("^0::", "::"):gsub("::0$", "::");
416 function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
417 rr.cname = self:name();
421 function resolver:MX(rr) -- - - - - - - - - - - - - - - - - - - - - - - MX
422 rr.pref = self:word();
427 function resolver:LOC_nibble_power() -- - - - - - - - - - LOC_nibble_power
428 local b = self:byte();
429 --print('nibbles', ((b-(b%0x10))/0x10), (b%0x10));
430 return ((b-(b%0x10))/0x10) * (10^(b%0x10));
434 function resolver:LOC(rr) -- - - - - - - - - - - - - - - - - - - - - - LOC
435 rr.version = self:byte();
436 if rr.version == 0 then
437 rr.loc = rr.loc or {};
438 rr.loc.size = self:LOC_nibble_power();
439 rr.loc.horiz_pre = self:LOC_nibble_power();
440 rr.loc.vert_pre = self:LOC_nibble_power();
441 rr.loc.latitude = self:dword();
442 rr.loc.longitude = self:dword();
443 rr.loc.altitude = self:dword();
448 local function LOC_tostring_degrees(f, pos, neg) -- - - - - - - - - - - - -
450 if f < 0 then pos = neg; f = -f; end
451 local deg, min, msec;
456 return string.format('%3d %2d %2.3f %s', deg, min, msec/1000, pos);
460 function resolver.LOC_tostring(rr) -- - - - - - - - - - - - - LOC_tostring
464 for k,name in pairs { 'size', 'horiz_pre', 'vert_pre', 'latitude', 'longitude', 'altitude' } do
465 append(t, string.format('%4s%-10s: %12.0f\n', '', name, rr.loc[name]));
469 append(t, string.format(
470 '%s %s %.2fm %.2fm %.2fm %.2fm',
471 LOC_tostring_degrees (rr.loc.latitude, 'N', 'S'),
472 LOC_tostring_degrees (rr.loc.longitude, 'E', 'W'),
473 (rr.loc.altitude - 10000000) / 100,
475 rr.loc.horiz_pre / 100,
476 rr.loc.vert_pre / 100
479 return table.concat(t);
483 function resolver:NS(rr) -- - - - - - - - - - - - - - - - - - - - - - - NS
488 function resolver:SOA(rr) -- - - - - - - - - - - - - - - - - - - - - - SOA
492 function resolver:SRV(rr) -- - - - - - - - - - - - - - - - - - - - - - SRV
494 rr.srv.priority = self:word();
495 rr.srv.weight = self:word();
496 rr.srv.port = self:word();
497 rr.srv.target = self:name();
500 function resolver:PTR(rr)
501 rr.ptr = self:name();
504 function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
505 rr.txt = self:sub (self:byte());
509 function resolver:rr() -- - - - - - - - - - - - - - - - - - - - - - - - rr
511 setmetatable(rr, rr_metatable);
512 rr.name = self:name(self);
513 rr.type = dns.type[self:word()] or rr.type;
514 rr.class = dns.class[self:word()] or rr.class;
515 rr.ttl = 0x10000*self:word() + self:word();
516 rr.rdlength = self:word();
519 rr.tod = self.time + 30;
521 rr.tod = self.time + rr.ttl;
524 local remember = self.offset;
525 local rr_parser = self[dns.type[rr.type]];
526 if rr_parser then rr_parser(self, rr); end
527 self.offset = remember;
528 rr.rdata = self:sub(rr.rdlength);
533 function resolver:rrs (count) -- - - - - - - - - - - - - - - - - - - - - rrs
535 for i = 1,count do append(rrs, self:rr()); end
540 function resolver:decode(packet, force) -- - - - - - - - - - - - - - decode
541 self.packet, self.offset = packet, 1;
542 local header = self:header(force);
543 if not header then return nil; end
544 local response = { header = header };
546 response.question = {};
547 local offset = self.offset;
548 for i = 1,response.header.qdcount do
549 append(response.question, self:question());
551 response.question.raw = string.sub(self.packet, offset, self.offset - 1);
554 if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
555 self.active[response.header.id] = nil;
560 response.answer = self:rrs(response.header.ancount);
561 response.authority = self:rrs(response.header.nscount);
562 response.additional = self:rrs(response.header.arcount);
568 -- socket layer -------------------------------------------------- socket layer
571 resolver.delays = { 1, 3 };
574 function resolver:addnameserver(address) -- - - - - - - - - - addnameserver
575 self.server = self.server or {};
576 append(self.server, address);
580 function resolver:setnameserver(address) -- - - - - - - - - - setnameserver
582 self:addnameserver(address);
586 function resolver:adddefaultnameservers() -- - - - - adddefaultnameservers
588 if windows and windows.get_nameservers then
589 for _, server in ipairs(windows.get_nameservers()) do
590 self:addnameserver(server);
593 if not self.server or #self.server == 0 then
594 -- TODO log warning about no nameservers, adding opendns servers as fallback
595 self:addnameserver("208.67.222.222");
596 self:addnameserver("208.67.220.220");
599 local resolv_conf = io.open("/etc/resolv.conf");
601 for line in resolv_conf:lines() do
602 line = line:gsub("#.*$", "")
603 :match('^%s*nameserver%s+([%x:%.]*%%?%S*)%s*$');
605 local ip = new_ip(line);
607 self:addnameserver(ip.addr);
612 if not self.server or #self.server == 0 then
613 -- TODO log warning about no nameservers, adding localhost as the default nameserver
614 self:addnameserver("127.0.0.1");
620 function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket
621 self.socket = self.socket or {};
622 self.socketset = self.socketset or {};
624 local sock = self.socket[servernum];
625 if sock then return sock; end
628 local peer = self.server[servernum];
629 if peer:find(":") then
630 sock, err = socket.udp6();
632 sock, err = socket.udp();
634 if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end
639 -- todo: attempt to use a random port, fallback to 0
640 self.socket[servernum] = sock;
641 self.socketset[sock] = servernum;
642 -- set{sock,peer}name can fail, eg because of local routing table
643 -- if so, try the next server
644 ok, err = sock:setsockname('*', 0);
645 if not ok then return self:servfail(sock, err); end
646 ok, err = sock:setpeername(peer, 53);
647 if not ok then return self:servfail(sock, err); end
651 function resolver:voidsocket(sock)
652 if self.socket[sock] then
653 self.socketset[self.socket[sock]] = nil;
654 self.socket[sock] = nil;
655 elseif self.socketset[sock] then
656 self.socket[self.socketset[sock]] = nil;
657 self.socketset[sock] = nil;
662 function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
663 self.socket_wrapper = func;
667 function resolver:closeall () -- - - - - - - - - - - - - - - - - - closeall
668 for i,sock in ipairs(self.socket) do
669 self.socket[i] = nil;
670 self.socketset[sock] = nil;
676 function resolver:remember(rr, type) -- - - - - - - - - - - - - - remember
677 --print ('remember', type, rr.class, rr.type, rr.name)
678 local qname, qtype, qclass = standardize(rr.name, rr.type, rr.class);
682 local all = get(self.cache, qclass, '*', qname);
683 --print('remember all', all);
684 if all then append(all, rr); end
687 self.cache = self.cache or setmetatable({}, cache_metatable);
688 local rrs = get(self.cache, qclass, type, qname) or
689 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
690 if not rrs[rr[qtype:lower()]] then
691 rrs[rr[qtype:lower()]] = true;
695 if type == 'MX' then self.unsorted[rrs] = true; end
699 local function comp_mx(a, b) -- - - - - - - - - - - - - - - - - - - comp_mx
700 return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
704 function resolver:peek (qname, qtype, qclass, n) -- - - - - - - - - - - - peek
705 qname, qtype, qclass = standardize(qname, qtype, qclass);
706 local rrs = get(self.cache, qclass, qtype, qname);
708 if n then if n <= 0 then return end else n = 3 end
709 rrs = get(self.cache, qclass, "CNAME", qname);
710 if not (rrs and rrs[1]) then return end
711 return self:peek(rrs[1].cname, qtype, qclass, n - 1);
713 if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
714 set(self.cache, qclass, qtype, qname, nil);
717 if self.unsorted[rrs] then table.sort (rrs, comp_mx); self.unsorted[rrs] = nil; end
722 function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
723 if soft == 'soft' then
724 self.time = socket.gettime();
725 for class,types in pairs(self.cache or {}) do
726 for type,names in pairs(types) do
727 for name,rrs in pairs(names) do
728 prune(rrs, self.time, 'soft')
732 else self.cache = setmetatable({}, cache_metatable); end
736 function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
737 qname, qtype, qclass = standardize(qname, qtype, qclass)
739 local co = coroutine.running();
740 local q = get(self.wanted, qclass, qtype, qname);
742 -- We are already waiting for a reply to an identical query.
743 set(self.wanted, qclass, qtype, qname, co, true);
747 if not self.server then self:adddefaultnameservers(); end
749 local question = encodeQuestion(qname, qtype, qclass);
750 local peek = self:peek (qname, qtype, qclass);
751 if peek then return peek; end
753 local header, id = encodeHeader();
754 --print ('query id', id, qclass, qtype, qname)
756 packet = header..question,
757 server = self.best_server,
759 retry = socket.gettime() + self.delays[1]
762 -- remember the query
763 self.active[id] = self.active[id] or {};
764 self.active[id][question] = o;
766 local conn, err = self:getsocket(o.server)
772 -- remember which coroutine wants the answer
774 set(self.wanted, qclass, qtype, qname, co, true);
777 if timer and self.timeout then
778 local num_servers = #self.server;
780 timer.add_task(self.timeout, function ()
781 if get(self.wanted, qclass, qtype, qname, co) then
782 if i < num_servers then
785 o.server = self.best_server;
786 conn, err = self:getsocket(o.server);
792 -- Tried everything, failed
793 self:cancel(qclass, qtype, qname);
800 function resolver:servfail(sock, err)
801 -- Resend all queries for this server
803 local num = self.socketset[sock]
805 -- Socket is dead now
806 sock = self:voidsocket(sock);
808 -- Find all requests to the down server, and retry on the next server
809 self.time = socket.gettime();
810 for id,queries in pairs(self.active) do
811 for question,o in pairs(queries) do
812 if o.server == num then -- This request was to the broken server
813 o.server = o.server + 1 -- Use next server
814 if o.server > #self.server then
818 o.retries = (o.retries or 0) + 1;
819 if o.retries >= #self.server then
821 queries[question] = nil;
823 sock, err = self:getsocket(o.server);
824 if sock then sock:send(o.packet); end
828 if next(queries) == nil then
829 self.active[id] = nil;
833 if num == self.best_server then
834 self.best_server = self.best_server + 1;
835 if self.best_server > #self.server then
836 -- Exhausted all servers, try first again
837 self.best_server = 1;
843 function resolver:settimeout(seconds)
844 self.timeout = seconds;
847 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
848 --print('receive'); print(self.socket);
849 self.time = socket.gettime();
850 rset = rset or self.socket;
853 for i,sock in pairs(rset) do
855 if self.socketset[sock] then
856 local packet = sock:receive();
858 response = self:decode(packet);
859 if response and self.active[response.header.id]
860 and self.active[response.header.id][response.question.raw] then
861 --print('received response');
862 --self.print(response);
864 for j,rr in pairs(response.answer) do
865 if rr.name:sub(-#response.question[1].name, -1) == response.question[1].name then
866 self:remember(rr, response.question[1].type)
871 local queries = self.active[response.header.id];
872 queries[response.question.raw] = nil;
874 if not next(queries) then self.active[response.header.id] = nil; end
875 if not next(self.active) then self:closeall(); end
877 -- was the query on the wanted list?
878 local q = response.question[1];
879 local cos = get(self.wanted, q.class, q.type, q.name);
881 for co in pairs(cos) do
882 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
884 set(self.wanted, q.class, q.type, q.name, nil);
896 function resolver:feed(sock, packet, force)
897 --print('receive'); print(self.socket);
898 self.time = socket.gettime();
900 local response = self:decode(packet, force);
901 if response and self.active[response.header.id]
902 and self.active[response.header.id][response.question.raw] then
903 --print('received response');
904 --self.print(response);
906 for j,rr in pairs(response.answer) do
907 self:remember(rr, response.question[1].type);
911 local queries = self.active[response.header.id];
912 queries[response.question.raw] = nil;
913 if not next(queries) then self.active[response.header.id] = nil; end
914 if not next(self.active) then self:closeall(); end
916 -- was the query on the wanted list?
917 local q = response.question[1];
919 local cos = get(self.wanted, q.class, q.type, q.name);
921 for co in pairs(cos) do
922 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
924 set(self.wanted, q.class, q.type, q.name, nil);
932 function resolver:cancel(qclass, qtype, qname)
933 local cos = get(self.wanted, qclass, qtype, qname);
935 for co in pairs(cos) do
936 if coroutine.status(co) == "suspended" then coroutine.resume(co); end
938 set(self.wanted, qclass, qtype, qname, nil);
942 function resolver:pulse() -- - - - - - - - - - - - - - - - - - - - - pulse
944 while self:receive() do end
945 if not next(self.active) then return nil; end
947 self.time = socket.gettime();
948 for id,queries in pairs(self.active) do
949 for question,o in pairs(queries) do
950 if self.time >= o.retry then
952 o.server = o.server + 1;
953 if o.server > #self.server then
955 o.delay = o.delay + 1;
958 if o.delay > #self.delays then
960 queries[question] = nil;
961 if not next(queries) then self.active[id] = nil; end
962 if not next(self.active) then return nil; end
964 --print('retry', o.server, o.delay);
965 local _a = self.socket[o.server];
966 if _a then _a:send(o.packet); end
967 o.retry = self.time + self.delays[o.delay];
973 if next(self.active) then return true; end
978 function resolver:lookup(qname, qtype, qclass) -- - - - - - - - - - lookup
979 self:query (qname, qtype, qclass)
980 while self:pulse() do
982 for i, s in ipairs(self.socket) do
985 socket.select(recvt, nil, 4)
988 return self:peek(qname, qtype, qclass);
991 function resolver:lookupex(handler, qname, qtype, qclass) -- - - - - - - - - - lookup
992 return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
995 function resolver:tohostname(ip)
996 return dns.lookup(ip:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)", "%4.%3.%2.%1.in-addr.arpa."), "PTR");
999 --print ---------------------------------------------------------------- print
1002 local hints = { -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
1003 qr = { [0]='query', 'response' },
1004 opcode = { [0]='query', 'inverse query', 'server status request' },
1005 aa = { [0]='non-authoritative', 'authoritative' },
1006 tc = { [0]='complete', 'truncated' },
1007 rd = { [0]='recursion not desired', 'recursion desired' },
1008 ra = { [0]='recursion not available', 'recursion available' },
1009 z = { [0]='(reserved)' },
1010 rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
1017 local function hint(p, s) -- - - - - - - - - - - - - - - - - - - - - - hint
1018 return (hints[s] and hints[s][p[s]]) or '';
1022 function resolver.print(response) -- - - - - - - - - - - - - resolver.print
1023 for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
1024 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
1025 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
1028 for i,question in ipairs(response.question) do
1029 print(string.format ('question[%i].name ', i), question.name);
1030 print(string.format ('question[%i].type ', i), question.type);
1031 print(string.format ('question[%i].class ', i), question.class);
1034 local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
1036 for s,s in pairs({'answer', 'authority', 'additional'}) do
1037 for i,rr in pairs(response[s]) do
1038 for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
1039 tmp = string.format('%s[%i].%s', s, i, t);
1040 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
1042 for j,t in pairs(rr) do
1043 if not common[j] then
1044 tmp = string.format('%s[%i].%s', s, i, j);
1045 print(string.format('%-30s %s', tostring(tmp), tostring(t)));
1053 -- module api ------------------------------------------------------ module api
1056 function dns.resolver () -- - - - - - - - - - - - - - - - - - - - - resolver
1057 -- this function seems to be redundant with resolver.new ()
1059 local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, best_server = 1 };
1060 setmetatable (r, resolver);
1061 setmetatable (r.cache, cache_metatable);
1062 setmetatable (r.unsorted, { __mode = 'kv' });
1066 local _resolver = dns.resolver();
1067 dns._resolver = _resolver;
1069 function dns.lookup(...) -- - - - - - - - - - - - - - - - - - - - - lookup
1070 return _resolver:lookup(...);
1073 function dns.tohostname(...)
1074 return _resolver:tohostname(...);
1077 function dns.purge(...) -- - - - - - - - - - - - - - - - - - - - - - purge
1078 return _resolver:purge(...);
1081 function dns.peek(...) -- - - - - - - - - - - - - - - - - - - - - - - peek
1082 return _resolver:peek(...);
1085 function dns.query(...) -- - - - - - - - - - - - - - - - - - - - - - query
1086 return _resolver:query(...);
1089 function dns.feed(...) -- - - - - - - - - - - - - - - - - - - - - - - feed
1090 return _resolver:feed(...);
1093 function dns.cancel(...) -- - - - - - - - - - - - - - - - - - - - - - cancel
1094 return _resolver:cancel(...);
1097 function dns.settimeout(...)
1098 return _resolver:settimeout(...);
1101 function dns.cache()
1102 return _resolver.cache;
1105 function dns.socket_wrapper_set(...) -- - - - - - - - - socket_wrapper_set
1106 return _resolver:socket_wrapper_set(...);