net.dns: Unconditionally cache records found in answers
[prosody.git] / net / dns.lua
1 -- Prosody IM
2 -- This file is included with Prosody IM. It has modifications,
3 -- which are hereby placed in the public domain.
4
5
6 -- todo: quick (default) header generation
7 -- todo: nxdomain, error handling
8 -- todo: cache results of encodeName
9
10
11 -- reference: http://tools.ietf.org/html/rfc1035
12 -- reference: http://tools.ietf.org/html/rfc1876 (LOC)
13
14
15 local socket = require "socket";
16 local timer = require "util.timer";
17
18 local _, windows = pcall(require, "util.windows");
19 local is_windows = (_ and windows) or os.getenv("WINDIR");
20
21 local coroutine, io, math, string, table =
22       coroutine, io, math, string, table;
23
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;
26
27 local ztact = { -- public domain 20080404 lua@ztact.com
28         get = function(parent, ...)
29                 local len = select('#', ...);
30                 for i=1,len do
31                         parent = parent[select(i, ...)];
32                         if parent == nil then break; end
33                 end
34                 return parent;
35         end;
36         set = function(parent, ...)
37                 local len = select('#', ...);
38                 local key, value = select(len-1, ...);
39                 local cutpoint, cutkey;
40
41                 for i=1,len-2 do
42                         local key = select (i, ...)
43                         local child = parent[key]
44
45                         if value == nil then
46                                 if child == nil then
47                                         return;
48                                 elseif next(child, next(child)) then
49                                         cutpoint = nil; cutkey = nil;
50                                 elseif cutpoint == nil then
51                                         cutpoint = parent; cutkey = key;
52                                 end
53                         elseif child == nil then
54                                 child = {};
55                                 parent[key] = child;
56                         end
57                         parent = child
58                 end
59
60                 if value == nil and cutpoint then
61                         cutpoint[cutkey] = nil;
62                 else
63                         parent[key] = value;
64                         return value;
65                 end
66         end;
67 };
68 local get, set = ztact.get, ztact.set;
69
70 local default_timeout = 15;
71
72 -------------------------------------------------- module dns
73 module('dns')
74 local dns = _M;
75
76
77 -- dns type & class codes ------------------------------ dns type & class codes
78
79
80 local append = table.insert
81
82
83 local function highbyte(i)    -- - - - - - - - - - - - - - - - - - -  highbyte
84         return (i-(i%0x100))/0x100;
85 end
86
87
88 local function augment (t)    -- - - - - - - - - - - - - - - - - - - -  augment
89         local a = {};
90         for i,s in pairs(t) do
91                 a[i] = s;
92                 a[s] = s;
93                 a[string.lower(s)] = s;
94         end
95         return a;
96 end
97
98
99 local function encode (t)    -- - - - - - - - - - - - - - - - - - - - -  encode
100         local code = {};
101         for i,s in pairs(t) do
102                 local word = string.char(highbyte(i), i%0x100);
103                 code[i] = word;
104                 code[s] = word;
105                 code[string.lower(s)] = word;
106         end
107         return code;
108 end
109
110
111 dns.types = {
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] = '*' };
116
117
118 dns.classes = { 'IN', 'CS', 'CH', 'HS', [255] = '*' };
119
120
121 dns.type      = augment (dns.types);
122 dns.class     = augment (dns.classes);
123 dns.typecode  = encode  (dns.types);
124 dns.classcode = encode  (dns.classes);
125
126
127
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'];
132 end
133
134
135 local function prune(rrs, time, soft)    -- - - - - - - - - - - - - - -  prune
136         time = time or socket.gettime();
137         for i,rr in ipairs(rrs) do
138                 if rr.tod then
139                         -- rr.tod = rr.tod - 50    -- accelerated decripitude
140                         rr.ttl = math.floor(rr.tod - time);
141                         if rr.ttl <= 0 then
142                                 rrs[rr[rr.type:lower()]] = nil;
143                                 table.remove(rrs, i);
144                                 return prune(rrs, time, soft); -- Re-iterate
145                         end
146                 elseif soft == 'soft' then    -- What is this?  I forget!
147                         assert(rr.ttl == 0);
148                         rrs[rr[rr.type:lower()]] = nil;
149                         table.remove(rrs, i);
150                 end
151         end
152 end
153
154
155 -- metatables & co. ------------------------------------------ metatables & co.
156
157
158 local resolver = {};
159 resolver.__index = resolver;
160
161 resolver.timeout = default_timeout;
162
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>";
167         end
168         return rr_val;
169 end
170
171 local special_tostrings = {
172         LOC = resolver.LOC_tostring;
173         MX  = function (rr)
174                 return string.format('%2i %s', rr.pref, rr.mx);
175         end;
176         SRV = function (rr)
177                 local s = rr.srv;
178                 return string.format('%5d %5d %5d %s', s.priority, s.weight, s.port, s.target);
179         end;
180 };
181
182 local rr_metatable = {};   -- - - - - - - - - - - - - - - - - - -  rr_metatable
183 function rr_metatable.__tostring(rr)
184         local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr);
185         return string.format('%2s %-5s %6i %-28s %s', rr.class, rr.type, rr.ttl, rr.name, rr_string);
186 end
187
188
189 local rrs_metatable = {};    -- - - - - - - - - - - - - - - - - -  rrs_metatable
190 function rrs_metatable.__tostring(rrs)
191         local t = {};
192         for i,rr in ipairs(rrs) do
193                 append(t, tostring(rr)..'\n');
194         end
195         return table.concat(t);
196 end
197
198
199 local cache_metatable = {};    -- - - - - - - - - - - - - - - -  cache_metatable
200 function cache_metatable.__tostring(cache)
201         local time = socket.gettime();
202         local t = {};
203         for class,types in pairs(cache) do
204                 for type,names in pairs(types) do
205                         for name,rrs in pairs(names) do
206                                 prune(rrs, time);
207                                 append(t, tostring(rrs));
208                         end
209                 end
210         end
211         return table.concat(t);
212 end
213
214
215 function resolver:new()    -- - - - - - - - - - - - - - - - - - - - - resolver
216         local r = { active = {}, cache = {}, unsorted = {} };
217         setmetatable(r, resolver);
218         setmetatable(r.cache, cache_metatable);
219         setmetatable(r.unsorted, { __mode = 'kv' });
220         return r;
221 end
222
223
224 -- packet layer -------------------------------------------------- packet layer
225
226
227 function dns.random(...)    -- - - - - - - - - - - - - - - - - - -  dns.random
228         math.randomseed(math.floor(10000*socket.gettime()) % 0x80000000);
229         dns.random = math.random;
230         return dns.random(...);
231 end
232
233
234 local function encodeHeader(o)    -- - - - - - - - - - - - - - -  encodeHeader
235         o = o or {};
236         o.id = o.id or dns.random(0, 0xffff); -- 16b    (random) id
237
238         o.rd = o.rd or 1;               --  1b  1 recursion desired
239         o.tc = o.tc or 0;               --  1b  1 truncated response
240         o.aa = o.aa or 0;               --  1b  1 authoritative response
241         o.opcode = o.opcode or 0;       --  4b  0 query
242                                 --  1 inverse query
243                                 --      2 server status request
244                                 --      3-15 reserved
245         o.qr = o.qr or 0;               --  1b  0 query, 1 response
246
247         o.rcode = o.rcode or 0; --  4b  0 no error
248                                 --      1 format error
249                                 --      2 server failure
250                                 --      3 name error
251                                 --      4 not implemented
252                                 --      5 refused
253                                 --      6-15 reserved
254         o.z = o.z  or 0;                --  3b  0 resvered
255         o.ra = o.ra or 0;               --  1b  1 recursion available
256
257         o.qdcount = o.qdcount or 1;     -- 16b  number of question RRs
258         o.ancount = o.ancount or 0;     -- 16b  number of answers RRs
259         o.nscount = o.nscount or 0;     -- 16b  number of nameservers RRs
260         o.arcount = o.arcount or 0;     -- 16b  number of additional RRs
261
262         -- string.char() rounds, so prevent roundup with -0.4999
263         local header = string.char(
264                 highbyte(o.id), o.id %0x100,
265                 o.rd + 2*o.tc + 4*o.aa + 8*o.opcode + 128*o.qr,
266                 o.rcode + 16*o.z + 128*o.ra,
267                 highbyte(o.qdcount),  o.qdcount %0x100,
268                 highbyte(o.ancount),  o.ancount %0x100,
269                 highbyte(o.nscount),  o.nscount %0x100,
270                 highbyte(o.arcount),  o.arcount %0x100
271         );
272
273         return header, o.id;
274 end
275
276
277 local function encodeName(name)    -- - - - - - - - - - - - - - - - encodeName
278         local t = {};
279         for part in string.gmatch(name, '[^.]+') do
280                 append(t, string.char(string.len(part)));
281                 append(t, part);
282         end
283         append(t, string.char(0));
284         return table.concat(t);
285 end
286
287
288 local function encodeQuestion(qname, qtype, qclass)    -- - - - encodeQuestion
289         qname  = encodeName(qname);
290         qtype  = dns.typecode[qtype or 'a'];
291         qclass = dns.classcode[qclass or 'in'];
292         return qname..qtype..qclass;
293 end
294
295
296 function resolver:byte(len)    -- - - - - - - - - - - - - - - - - - - - - byte
297         len = len or 1;
298         local offset = self.offset;
299         local last = offset + len - 1;
300         if last > #self.packet then
301                 error(string.format('out of bounds: %i>%i', last, #self.packet));
302         end
303         self.offset = offset + len;
304         return string.byte(self.packet, offset, last);
305 end
306
307
308 function resolver:word()    -- - - - - - - - - - - - - - - - - - - - - -  word
309         local b1, b2 = self:byte(2);
310         return 0x100*b1 + b2;
311 end
312
313
314 function resolver:dword ()    -- - - - - - - - - - - - - - - - - - - - -  dword
315         local b1, b2, b3, b4 = self:byte(4);
316         --print('dword', b1, b2, b3, b4);
317         return 0x1000000*b1 + 0x10000*b2 + 0x100*b3 + b4;
318 end
319
320
321 function resolver:sub(len)    -- - - - - - - - - - - - - - - - - - - - - - sub
322         len = len or 1;
323         local s = string.sub(self.packet, self.offset, self.offset + len - 1);
324         self.offset = self.offset + len;
325         return s;
326 end
327
328
329 function resolver:header(force)    -- - - - - - - - - - - - - - - - - - header
330         local id = self:word();
331         --print(string.format(':header  id  %x', id));
332         if not self.active[id] and not force then return nil; end
333
334         local h = { id = id };
335
336         local b1, b2 = self:byte(2);
337
338         h.rd      = b1 %2;
339         h.tc      = b1 /2%2;
340         h.aa      = b1 /4%2;
341         h.opcode  = b1 /8%16;
342         h.qr      = b1 /128;
343
344         h.rcode   = b2 %16;
345         h.z       = b2 /16%8;
346         h.ra      = b2 /128;
347
348         h.qdcount = self:word();
349         h.ancount = self:word();
350         h.nscount = self:word();
351         h.arcount = self:word();
352
353         for k,v in pairs(h) do h[k] = v-v%1; end
354
355         return h;
356 end
357
358
359 function resolver:name()    -- - - - - - - - - - - - - - - - - - - - - -  name
360         local remember, pointers = nil, 0;
361         local len = self:byte();
362         local n = {};
363         if len == 0 then return "." end -- Root label
364         while len > 0 do
365                 if len >= 0xc0 then    -- name is "compressed"
366                         pointers = pointers + 1;
367                         if pointers >= 20 then error('dns error: 20 pointers'); end;
368                         local offset = ((len-0xc0)*0x100) + self:byte();
369                         remember = remember or self.offset;
370                         self.offset = offset + 1;    -- +1 for lua
371                 else    -- name is not compressed
372                         append(n, self:sub(len)..'.');
373                 end
374                 len = self:byte();
375         end
376         self.offset = remember or self.offset;
377         return table.concat(n);
378 end
379
380
381 function resolver:question()    -- - - - - - - - - - - - - - - - - -  question
382         local q = {};
383         q.name  = self:name();
384         q.type  = dns.type[self:word()];
385         q.class = dns.class[self:word()];
386         return q;
387 end
388
389
390 function resolver:A(rr)    -- - - - - - - - - - - - - - - - - - - - - - - -  A
391         local b1, b2, b3, b4 = self:byte(4);
392         rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
393 end
394
395 function resolver:AAAA(rr)
396         local addr = {};
397         for i = 1, rr.rdlength, 2 do
398                 local b1, b2 = self:byte(2);
399                 table.insert(addr, ("%02x%02x"):format(b1, b2));
400         end
401         addr = table.concat(addr, ":"):gsub("%f[%x]0+(%x)","%1");
402         local zeros = {};
403         for item in addr:gmatch(":[0:]+:") do
404                 table.insert(zeros, item)
405         end
406         if #zeros == 0 then
407                 rr.aaaa = addr;
408                 return
409         elseif #zeros > 1 then
410                 table.sort(zeros, function(a, b) return #a > #b end);
411         end
412         rr.aaaa = addr:gsub(zeros[1], "::", 1):gsub("^0::", "::"):gsub("::0$", "::");
413 end
414
415 function resolver:CNAME(rr)    -- - - - - - - - - - - - - - - - - - - -  CNAME
416         rr.cname = self:name();
417 end
418
419
420 function resolver:MX(rr)    -- - - - - - - - - - - - - - - - - - - - - - -  MX
421         rr.pref = self:word();
422         rr.mx   = self:name();
423 end
424
425
426 function resolver:LOC_nibble_power()    -- - - - - - - - - -  LOC_nibble_power
427         local b = self:byte();
428         --print('nibbles', ((b-(b%0x10))/0x10), (b%0x10));
429         return ((b-(b%0x10))/0x10) * (10^(b%0x10));
430 end
431
432
433 function resolver:LOC(rr)    -- - - - - - - - - - - - - - - - - - - - - -  LOC
434         rr.version = self:byte();
435         if rr.version == 0 then
436                 rr.loc           = rr.loc or {};
437                 rr.loc.size      = self:LOC_nibble_power();
438                 rr.loc.horiz_pre = self:LOC_nibble_power();
439                 rr.loc.vert_pre  = self:LOC_nibble_power();
440                 rr.loc.latitude  = self:dword();
441                 rr.loc.longitude = self:dword();
442                 rr.loc.altitude  = self:dword();
443         end
444 end
445
446
447 local function LOC_tostring_degrees(f, pos, neg)    -- - - - - - - - - - - - -
448         f = f - 0x80000000;
449         if f < 0 then pos = neg; f = -f; end
450         local deg, min, msec;
451         msec = f%60000;
452         f    = (f-msec)/60000;
453         min  = f%60;
454         deg = (f-min)/60;
455         return string.format('%3d %2d %2.3f %s', deg, min, msec/1000, pos);
456 end
457
458
459 function resolver.LOC_tostring(rr)    -- - - - - - - - - - - - -  LOC_tostring
460         local t = {};
461
462         --[[
463         for k,name in pairs { 'size', 'horiz_pre', 'vert_pre', 'latitude', 'longitude', 'altitude' } do
464                 append(t, string.format('%4s%-10s: %12.0f\n', '', name, rr.loc[name]));
465         end
466         --]]
467
468         append(t, string.format(
469                 '%s    %s    %.2fm %.2fm %.2fm %.2fm',
470                 LOC_tostring_degrees (rr.loc.latitude, 'N', 'S'),
471                 LOC_tostring_degrees (rr.loc.longitude, 'E', 'W'),
472                 (rr.loc.altitude - 10000000) / 100,
473                 rr.loc.size / 100,
474                 rr.loc.horiz_pre / 100,
475                 rr.loc.vert_pre / 100
476         ));
477
478         return table.concat(t);
479 end
480
481
482 function resolver:NS(rr)    -- - - - - - - - - - - - - - - - - - - - - - -  NS
483         rr.ns = self:name();
484 end
485
486
487 function resolver:SOA(rr)    -- - - - - - - - - - - - - - - - - - - - - -  SOA
488 end
489
490
491 function resolver:SRV(rr)    -- - - - - - - - - - - - - - - - - - - - - -  SRV
492           rr.srv = {};
493           rr.srv.priority = self:word();
494           rr.srv.weight   = self:word();
495           rr.srv.port     = self:word();
496           rr.srv.target   = self:name();
497 end
498
499 function resolver:PTR(rr)
500         rr.ptr = self:name();
501 end
502
503 function resolver:TXT(rr)    -- - - - - - - - - - - - - - - - - - - - - -  TXT
504         rr.txt = self:sub (self:byte());
505 end
506
507
508 function resolver:rr()    -- - - - - - - - - - - - - - - - - - - - - - - -  rr
509         local rr = {};
510         setmetatable(rr, rr_metatable);
511         rr.name     = self:name(self);
512         rr.type     = dns.type[self:word()] or rr.type;
513         rr.class    = dns.class[self:word()] or rr.class;
514         rr.ttl      = 0x10000*self:word() + self:word();
515         rr.rdlength = self:word();
516
517         if rr.ttl <= 0 then
518                 rr.tod = self.time + 30;
519         else
520                 rr.tod = self.time + rr.ttl;
521         end
522
523         local remember = self.offset;
524         local rr_parser = self[dns.type[rr.type]];
525         if rr_parser then rr_parser(self, rr); end
526         self.offset = remember;
527         rr.rdata = self:sub(rr.rdlength);
528         return rr;
529 end
530
531
532 function resolver:rrs (count)    -- - - - - - - - - - - - - - - - - - - - - rrs
533         local rrs = {};
534         for i = 1,count do append(rrs, self:rr()); end
535         return rrs;
536 end
537
538
539 function resolver:decode(packet, force)    -- - - - - - - - - - - - - - decode
540         self.packet, self.offset = packet, 1;
541         local header = self:header(force);
542         if not header then return nil; end
543         local response = { header = header };
544
545         response.question = {};
546         local offset = self.offset;
547         for i = 1,response.header.qdcount do
548                 append(response.question, self:question());
549         end
550         response.question.raw = string.sub(self.packet, offset, self.offset - 1);
551
552         if not force then
553                 if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
554                         self.active[response.header.id] = nil;
555                         return nil;
556                 end
557         end
558
559         response.answer     = self:rrs(response.header.ancount);
560         response.authority  = self:rrs(response.header.nscount);
561         response.additional = self:rrs(response.header.arcount);
562
563         return response;
564 end
565
566
567 -- socket layer -------------------------------------------------- socket layer
568
569
570 resolver.delays = { 1, 3 };
571
572
573 function resolver:addnameserver(address)    -- - - - - - - - - - addnameserver
574         self.server = self.server or {};
575         append(self.server, address);
576 end
577
578
579 function resolver:setnameserver(address)    -- - - - - - - - - - setnameserver
580         self.server = {};
581         self:addnameserver(address);
582 end
583
584
585 function resolver:adddefaultnameservers()    -- - - - -  adddefaultnameservers
586         if is_windows then
587                 if windows and windows.get_nameservers then
588                         for _, server in ipairs(windows.get_nameservers()) do
589                                 self:addnameserver(server);
590                         end
591                 end
592                 if not self.server or #self.server == 0 then
593                         -- TODO log warning about no nameservers, adding opendns servers as fallback
594                         self:addnameserver("208.67.222.222");
595                         self:addnameserver("208.67.220.220");
596                 end
597         else -- posix
598                 local resolv_conf = io.open("/etc/resolv.conf");
599                 if resolv_conf then
600                         for line in resolv_conf:lines() do
601                                 line = line:gsub("#.*$", "")
602                                         :match('^%s*nameserver%s+(.*)%s*$');
603                                 if line then
604                                         line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address)
605                                                 self:addnameserver(address)
606                                         end);
607                                 end
608                         end
609                 end
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");
613                 end
614         end
615 end
616
617
618 function resolver:getsocket(servernum)    -- - - - - - - - - - - - - getsocket
619         self.socket = self.socket or {};
620         self.socketset = self.socketset or {};
621
622         local sock = self.socket[servernum];
623         if sock then return sock; end
624
625         local ok, err;
626         sock, err = socket.udp();
627         if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end
628         if not sock then
629                 return nil, err;
630         end
631         sock:settimeout(0);
632         -- todo: attempt to use a random port, fallback to 0
633         self.socket[servernum] = sock;
634         self.socketset[sock] = servernum;
635         -- set{sock,peer}name can fail, eg because of local routing table
636         -- if so, try the next server
637         ok, err = sock:setsockname('*', 0);
638         if not ok then return self:servfail(sock, err); end
639         ok, err = sock:setpeername(self.server[servernum], 53);
640         if not ok then return self:servfail(sock, err); end
641         return sock;
642 end
643
644 function resolver:voidsocket(sock)
645         if self.socket[sock] then
646                 self.socketset[self.socket[sock]] = nil;
647                 self.socket[sock] = nil;
648         elseif self.socketset[sock] then
649                 self.socket[self.socketset[sock]] = nil;
650                 self.socketset[sock] = nil;
651         end
652         sock:close();
653 end
654
655 function resolver:socket_wrapper_set(func)  -- - - - - - - socket_wrapper_set
656         self.socket_wrapper = func;
657 end
658
659
660 function resolver:closeall ()    -- - - - - - - - - - - - - - - - - -  closeall
661         for i,sock in ipairs(self.socket) do
662                 self.socket[i] = nil;
663                 self.socketset[sock] = nil;
664                 sock:close();
665         end
666 end
667
668
669 function resolver:remember(rr, type)    -- - - - - - - - - - - - - -  remember
670         --print ('remember', type, rr.class, rr.type, rr.name)
671         local qname, qtype, qclass = standardize(rr.name, rr.type, rr.class);
672
673         if type ~= '*' then
674                 type = qtype;
675                 local all = get(self.cache, qclass, '*', qname);
676                 --print('remember all', all);
677                 if all then append(all, rr); end
678         end
679
680         self.cache = self.cache or setmetatable({}, cache_metatable);
681         local rrs = get(self.cache, qclass, type, qname) or
682                 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
683         if not rrs[rr[qtype:lower()]] then
684                 rrs[rr[qtype:lower()]] = true;
685                 append(rrs, rr);
686         end
687
688         if type == 'MX' then self.unsorted[rrs] = true; end
689 end
690
691
692 local function comp_mx(a, b)    -- - - - - - - - - - - - - - - - - - - comp_mx
693         return (a.pref == b.pref) and (a.mx < b.mx) or (a.pref < b.pref);
694 end
695
696
697 function resolver:peek (qname, qtype, qclass, n)    -- - - - - - - - - - - -  peek
698         qname, qtype, qclass = standardize(qname, qtype, qclass);
699         local rrs = get(self.cache, qclass, qtype, qname);
700         if not rrs then
701                 if n then if n <= 0 then return end else n = 3 end
702                 rrs = get(self.cache, qclass, "CNAME", qname);
703                 if not (rrs and rrs[1]) then return end
704                 return self:peek(rrs[1].cname, qtype, qclass, n - 1);
705         end
706         if prune(rrs, socket.gettime()) and qtype == '*' or not next(rrs) then
707                 set(self.cache, qclass, qtype, qname, nil);
708                 return nil;
709         end
710         if self.unsorted[rrs] then table.sort (rrs, comp_mx); self.unsorted[rrs] = nil; end
711         return rrs;
712 end
713
714
715 function resolver:purge(soft)    -- - - - - - - - - - - - - - - - - - -  purge
716         if soft == 'soft' then
717                 self.time = socket.gettime();
718                 for class,types in pairs(self.cache or {}) do
719                         for type,names in pairs(types) do
720                                 for name,rrs in pairs(names) do
721                                         prune(rrs, self.time, 'soft')
722                                 end
723                         end
724                 end
725         else self.cache = setmetatable({}, cache_metatable); end
726 end
727
728
729 function resolver:query(qname, qtype, qclass)    -- - - - - - - - - - -- query
730         qname, qtype, qclass = standardize(qname, qtype, qclass)
731
732         local co = coroutine.running();
733         local q = get(self.wanted, qclass, qtype, qname);
734         if co and q then
735                 -- We are already waiting for a reply to an identical query.
736                 set(self.wanted, qclass, qtype, qname, co, true);
737                 return true;
738         end
739
740         if not self.server then self:adddefaultnameservers(); end
741
742         local question = encodeQuestion(qname, qtype, qclass);
743         local peek = self:peek (qname, qtype, qclass);
744         if peek then return peek; end
745
746         local header, id = encodeHeader();
747         --print ('query  id', id, qclass, qtype, qname)
748         local o = {
749                 packet = header..question,
750                 server = self.best_server,
751                 delay  = 1,
752                 retry  = socket.gettime() + self.delays[1]
753         };
754
755         -- remember the query
756         self.active[id] = self.active[id] or {};
757         self.active[id][question] = o;
758
759         -- remember which coroutine wants the answer
760         if co then
761                 set(self.wanted, qclass, qtype, qname, co, true);
762         end
763
764         local conn, err = self:getsocket(o.server)
765         if not conn then
766                 return nil, err;
767         end
768         conn:send (o.packet)
769         
770         if timer and self.timeout then
771                 local num_servers = #self.server;
772                 local i = 1;
773                 timer.add_task(self.timeout, function ()
774                         if get(self.wanted, qclass, qtype, qname, co) then
775                                 if i < num_servers then
776                                         i = i + 1;
777                                         self:servfail(conn);
778                                         o.server = self.best_server;
779                                         conn, err = self:getsocket(o.server);
780                                         if conn then
781                                                 conn:send(o.packet);
782                                                 return self.timeout;
783                                         end
784                                 end
785                                 -- Tried everything, failed
786                                 self:cancel(qclass, qtype, qname);
787                         end
788                 end)
789         end
790         return true;
791 end
792
793 function resolver:servfail(sock, err)
794         -- Resend all queries for this server
795
796         local num = self.socketset[sock]
797
798         -- Socket is dead now
799         sock = self:voidsocket(sock);
800
801         -- Find all requests to the down server, and retry on the next server
802         self.time = socket.gettime();
803         for id,queries in pairs(self.active) do
804                 for question,o in pairs(queries) do
805                         if o.server == num then -- This request was to the broken server
806                                 o.server = o.server + 1 -- Use next server
807                                 if o.server > #self.server then
808                                         o.server = 1;
809                                 end
810
811                                 o.retries = (o.retries or 0) + 1;
812                                 if o.retries >= #self.server then
813                                         --print('timeout');
814                                         queries[question] = nil;
815                                 else
816                                         sock, err = self:getsocket(o.server);
817                                         if sock then sock:send(o.packet); end
818                                 end
819                         end
820                 end
821                 if next(queries) == nil then
822                         self.active[id] = nil;
823                 end
824         end
825
826         if num == self.best_server then
827                 self.best_server = self.best_server + 1;
828                 if self.best_server > #self.server then
829                         -- Exhausted all servers, try first again
830                         self.best_server = 1;
831                 end
832         end
833         return sock, err;
834 end
835
836 function resolver:settimeout(seconds)
837         self.timeout = seconds;
838 end
839
840 function resolver:receive(rset)    -- - - - - - - - - - - - - - - - -  receive
841         --print('receive');  print(self.socket);
842         self.time = socket.gettime();
843         rset = rset or self.socket;
844
845         local response;
846         for i,sock in pairs(rset) do
847
848                 if self.socketset[sock] then
849                         local packet = sock:receive();
850                         if packet then
851                                 response = self:decode(packet);
852                                 if response and self.active[response.header.id]
853                                         and self.active[response.header.id][response.question.raw] then
854                                         --print('received response');
855                                         --self.print(response);
856
857                                         for j,rr in pairs(response.answer) do
858                                                 self:remember(rr, response.question[1].type)
859                                         end
860
861                                         -- retire the query
862                                         local queries = self.active[response.header.id];
863                                         queries[response.question.raw] = nil;
864                                         
865                                         if not next(queries) then self.active[response.header.id] = nil; end
866                                         if not next(self.active) then self:closeall(); end
867
868                                         -- was the query on the wanted list?
869                                         local q = response.question[1];
870                                         local cos = get(self.wanted, q.class, q.type, q.name);
871                                         if cos then
872                                                 for co in pairs(cos) do
873                                                         if coroutine.status(co) == "suspended" then coroutine.resume(co); end
874                                                 end
875                                                 set(self.wanted, q.class, q.type, q.name, nil);
876                                         end
877                                 end
878                                 
879                         end
880                 end
881         end
882
883         return response;
884 end
885
886
887 function resolver:feed(sock, packet, force)
888         --print('receive'); print(self.socket);
889         self.time = socket.gettime();
890
891         local response = self:decode(packet, force);
892         if response and self.active[response.header.id]
893                 and self.active[response.header.id][response.question.raw] then
894                 --print('received response');
895                 --self.print(response);
896
897                 for j,rr in pairs(response.answer) do
898                         self:remember(rr, response.question[1].type);
899                 end
900
901                 -- retire the query
902                 local queries = self.active[response.header.id];
903                 queries[response.question.raw] = nil;
904                 if not next(queries) then self.active[response.header.id] = nil; end
905                 if not next(self.active) then self:closeall(); end
906
907                 -- was the query on the wanted list?
908                 local q = response.question[1];
909                 if q then
910                         local cos = get(self.wanted, q.class, q.type, q.name);
911                         if cos then
912                                 for co in pairs(cos) do
913                                         if coroutine.status(co) == "suspended" then coroutine.resume(co); end
914                                 end
915                                 set(self.wanted, q.class, q.type, q.name, nil);
916                         end
917                 end
918         end
919
920         return response;
921 end
922
923 function resolver:cancel(qclass, qtype, qname)
924         local cos = get(self.wanted, qclass, qtype, qname);
925         if cos then
926                 for co in pairs(cos) do
927                         if coroutine.status(co) == "suspended" then coroutine.resume(co); end
928                 end
929                 set(self.wanted, qclass, qtype, qname, nil);
930         end
931 end
932
933 function resolver:pulse()    -- - - - - - - - - - - - - - - - - - - - -  pulse
934         --print(':pulse');
935         while self:receive() do end
936         if not next(self.active) then return nil; end
937
938         self.time = socket.gettime();
939         for id,queries in pairs(self.active) do
940                 for question,o in pairs(queries) do
941                         if self.time >= o.retry then
942
943                                 o.server = o.server + 1;
944                                 if o.server > #self.server then
945                                         o.server = 1;
946                                         o.delay = o.delay + 1;
947                                 end
948
949                                 if o.delay > #self.delays then
950                                         --print('timeout');
951                                         queries[question] = nil;
952                                         if not next(queries) then self.active[id] = nil; end
953                                         if not next(self.active) then return nil; end
954                                 else
955                                         --print('retry', o.server, o.delay);
956                                         local _a = self.socket[o.server];
957                                         if _a then _a:send(o.packet); end
958                                         o.retry = self.time + self.delays[o.delay];
959                                 end
960                         end
961                 end
962         end
963
964         if next(self.active) then return true; end
965         return nil;
966 end
967
968
969 function resolver:lookup(qname, qtype, qclass)    -- - - - - - - - - -  lookup
970         self:query (qname, qtype, qclass)
971         while self:pulse() do
972                 local recvt = {}
973                 for i, s in ipairs(self.socket) do
974                         recvt[i] = s
975                 end
976                 socket.select(recvt, nil, 4)
977         end
978         --print(self.cache);
979         return self:peek(qname, qtype, qclass);
980 end
981
982 function resolver:lookupex(handler, qname, qtype, qclass)    -- - - - - - - - - -  lookup
983         return self:peek(qname, qtype, qclass) or self:query(qname, qtype, qclass);
984 end
985
986 function resolver:tohostname(ip)
987         return dns.lookup(ip:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)", "%4.%3.%2.%1.in-addr.arpa."), "PTR");
988 end
989
990 --print ---------------------------------------------------------------- print
991
992
993 local hints = {    -- - - - - - - - - - - - - - - - - - - - - - - - - - - hints
994         qr = { [0]='query', 'response' },
995         opcode = { [0]='query', 'inverse query', 'server status request' },
996         aa = { [0]='non-authoritative', 'authoritative' },
997         tc = { [0]='complete', 'truncated' },
998         rd = { [0]='recursion not desired', 'recursion desired' },
999         ra = { [0]='recursion not available', 'recursion available' },
1000         z  = { [0]='(reserved)' },
1001         rcode = { [0]='no error', 'format error', 'server failure', 'name error', 'not implemented' },
1002
1003         type = dns.type,
1004         class = dns.class
1005 };
1006
1007
1008 local function hint(p, s)    -- - - - - - - - - - - - - - - - - - - - - - hint
1009         return (hints[s] and hints[s][p[s]]) or '';
1010 end
1011
1012
1013 function resolver.print(response)    -- - - - - - - - - - - - - resolver.print
1014         for s,s in pairs { 'id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
1015                                                 'rcode', 'qdcount', 'ancount', 'nscount', 'arcount' } do
1016                 print( string.format('%-30s', 'header.'..s), response.header[s], hint(response.header, s) );
1017         end
1018
1019         for i,question in ipairs(response.question) do
1020                 print(string.format ('question[%i].name         ', i), question.name);
1021                 print(string.format ('question[%i].type         ', i), question.type);
1022                 print(string.format ('question[%i].class        ', i), question.class);
1023         end
1024
1025         local common = { name=1, type=1, class=1, ttl=1, rdlength=1, rdata=1 };
1026         local tmp;
1027         for s,s in pairs({'answer', 'authority', 'additional'}) do
1028                 for i,rr in pairs(response[s]) do
1029                         for j,t in pairs({ 'name', 'type', 'class', 'ttl', 'rdlength' }) do
1030                                 tmp = string.format('%s[%i].%s', s, i, t);
1031                                 print(string.format('%-30s', tmp), rr[t], hint(rr, t));
1032                         end
1033                         for j,t in pairs(rr) do
1034                                 if not common[j] then
1035                                         tmp = string.format('%s[%i].%s', s, i, j);
1036                                         print(string.format('%-30s  %s', tostring(tmp), tostring(t)));
1037                                 end
1038                         end
1039                 end
1040         end
1041 end
1042
1043
1044 -- module api ------------------------------------------------------ module api
1045
1046
1047 function dns.resolver ()    -- - - - - - - - - - - - - - - - - - - - - resolver
1048         -- this function seems to be redundant with resolver.new ()
1049
1050         local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, best_server = 1 };
1051         setmetatable (r, resolver);
1052         setmetatable (r.cache, cache_metatable);
1053         setmetatable (r.unsorted, { __mode = 'kv' });
1054         return r;
1055 end
1056
1057 local _resolver = dns.resolver();
1058 dns._resolver = _resolver;
1059
1060 function dns.lookup(...)    -- - - - - - - - - - - - - - - - - - - - -  lookup
1061         return _resolver:lookup(...);
1062 end
1063
1064 function dns.tohostname(...)
1065         return _resolver:tohostname(...);
1066 end
1067
1068 function dns.purge(...)    -- - - - - - - - - - - - - - - - - - - - - -  purge
1069         return _resolver:purge(...);
1070 end
1071
1072 function dns.peek(...)    -- - - - - - - - - - - - - - - - - - - - - - -  peek
1073         return _resolver:peek(...);
1074 end
1075
1076 function dns.query(...)    -- - - - - - - - - - - - - - - - - - - - - -  query
1077         return _resolver:query(...);
1078 end
1079
1080 function dns.feed(...)    -- - - - - - - - - - - - - - - - - - - - - - -  feed
1081         return _resolver:feed(...);
1082 end
1083
1084 function dns.cancel(...)  -- - - - - - - - - - - - - - - - - - - - - -  cancel
1085         return _resolver:cancel(...);
1086 end
1087
1088 function dns.settimeout(...)
1089         return _resolver:settimeout(...);
1090 end
1091
1092 function dns.cache()
1093         return _resolver.cache;
1094 end
1095
1096 function dns.socket_wrapper_set(...)    -- - - - - - - - -  socket_wrapper_set
1097         return _resolver:socket_wrapper_set(...);
1098 end
1099
1100 return dns;