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