Annotate

net/dns.lua @ 341:a9e02b5c58d2

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