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