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