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