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