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