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