-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns_rdata.lua
346 lines (310 loc) · 10.9 KB
/
dns_rdata.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: dns_rdata.lua $
--
-- Author: Markus Stenberg <markus stenberg@iki.fi>
--
-- Copyright (c) 2013 cisco Systems, Inc.
--
-- Created: Mon Jan 14 13:08:00 2013 mstenber
-- Last modified: Tue Jun 18 17:14:55 2013 mstenber
-- Edit time: 21 min
--
require 'dns_const'
require 'dns_name'
require 'mdns_const'
module(..., package.seeall)
local abstract_base = codec.abstract_base
local abstract_data = codec.abstract_data
local cursor_has_left = codec.cursor_has_left
local try_encode_name = dns_name.try_encode_name
local try_decode_name = dns_name.try_decode_name
local function simple_equal(self, v1, v2)
local r = (not v1 or not v2) or v1 == v2
--mst.d('simple_equal', v1, v2, r)
return r
end
local function repr_equal(self, v1, v2)
local r = (not v1 or not v2) or mst.repr_equal(v1, v2)
--mst.d('repr_equal', v1, v2, r)
return r
end
local function encode_ll_field(self, o, context)
local v = o[self.field]
mst.a(v, 'missing', self.field, o)
local t, err = try_encode_name(v, context)
if not t then return nil, err end
return table.concat(t)
end
local function decode_ll_field(self, o, cur, context)
local name, err = try_decode_name(cur, context)
if not name then return nil, err end
o[self.field] = name
return true
end
rtype_map = {[dns_const.TYPE_PTR]={
field='rdata_ptr',
encode=encode_ll_field,
decode=decode_ll_field,
field_equal=repr_equal,
},
[dns_const.TYPE_NS]={
field='rdata_ns',
encode=encode_ll_field,
decode=decode_ll_field,
field_equal=repr_equal,
},
[dns_const.TYPE_CNAME]={
field='rdata_cname',
encode=encode_ll_field,
decode=decode_ll_field,
field_equal=repr_equal,
},
[dns_const.TYPE_A]={
field='rdata_a',
encode=function (self, o, context)
local v = o[self.field]
mst.a(v, 'no value for', self.field)
local b = ipv4s.address_to_binary_address(v)
mst.a(#b == 4, 'encode error')
return b
end,
decode=function (self, o, cur, context)
if not rdata_cursor_has_left(cur, 4)
then
return nil, 'not enough rdata (4)'
end
local b = cur:read(4)
o[self.field] = ipv4s.binary_address_to_address(b)
return true
end,
field_equal=simple_equal,
default_ttl=mdns_const.DEFAULT_NAME_TTL,
},
[dns_const.TYPE_AAAA]={
field='rdata_aaaa',
encode=function (self, o, context)
local v = o[self.field]
mst.a(v)
local b = ipv6s.address_to_binary_address(v)
mst.a(#b == 16, 'encode error', o)
return b
end,
decode=function (self, o, cur, context)
if not rdata_cursor_has_left(cur, 16)
then
return nil, 'not enough rdata (16)'
end
local b = cur:read(16)
local s = ipv6s.binary_address_to_address(b)
o[self.field] = s
return s
end,
field_equal=simple_equal,
default_ttl=mdns_const.DEFAULT_NAME_TTL,
},
}
rdata_srv = abstract_data:new{class='rdata_srv',
format='priority:u2 weight:u2 port:u2',
header_default={priority=0,
weight=0,
port=0}}
function rdata_srv:try_decode(cur, context)
-- first off, get the base struct
local o, err = abstract_data.try_decode(self, cur)
if not o then return nil, err end
-- and then 'target', which is FQDN
local n, err = try_decode_name(cur, context)
if not n then return nil, err end
o.target = n
return o
end
function rdata_srv:do_encode(o, context)
mst.a(type(o) == 'table')
mst.a(o.target, 'missing targetin ', mst.repr(o))
local r = abstract_data.do_encode(self, o)
if context
then
context.pos = context.pos + #r
end
local t, err = try_encode_name(o.target, context)
if not t then return nil, err end
-- ugh, but oh well :p
return r .. table.concat(t)
end
local _hr=3600
rdata_soa = abstract_data:new{class='rdata_soa',
format='serial:u4 refresh:u4 retry:u4 expire:u4 minimum:u4',
header_default={serial=1,
refresh=_hr,
retry=_hr,
expire=_hr,
minimum=_hr}}
function rdata_soa:try_decode(cur, context)
-- mname = domain-name of server that was original info for this zone
local mname, err = try_decode_name(cur, context)
if not mname then return nil, err end
-- rname = mailbox address of responsible person
local rname, err = try_decode_name(cur, context)
if not rname then return nil, err end
-- then 'base struct' which follows after
local o, err = abstract_data.try_decode(self, cur)
if not o then return nil, err end
o.mname = mname
o.rname = rname
return o
end
function rdata_soa:do_encode(o, context)
local t1, err = try_encode_name(o.mname, context)
if not t1 then return nil, err end
local r1 = table.concat(t1)
if context
then
context.pos = context.pos + #r1
end
local t2, err = try_encode_name(o.rname, context)
if not t2 then return nil, err end
local r2 = table.concat(t2)
if context
then
context.pos = context.pos + #r2
end
local r = abstract_data.do_encode(self, o)
if context
then
context.pos = context.pos + #r
end
return r1 .. r2 .. r
end
rdata_nsec = abstract_base:new{class='rdata_nsec'}
function rdata_cursor_has_left(cur, n)
if cur.endpos
then
return cur.pos + n <= cur.endpos
end
return cursor_has_left(cur, n)
end
function rdata_nsec:try_decode(cur, context)
-- two things - next domain name (ndn)
local n, err = try_decode_name(cur, context)
if not n then return nil, err end
-- and bitmap
--local t = mst.set:new{}
local t = mst.array:new{}
while rdata_cursor_has_left(cur, 2)
do
-- block #
local block_offset = string.byte(cur:read(1))
-- bytes
local block_bytes = string.byte(cur:read(1))
self:d('reading', block_offset, block_bytes)
if not rdata_cursor_has_left(cur, block_bytes)
then
return nil, string.format('decode ended mid-way (no %d bytes left)',
block_bytes)
end
for i=1,block_bytes
do
local c = cur:read(1)
local b = string.byte(c)
-- XXX - some day would be nice to have network order
-- independent decode here. oh well.
for j=8,1,-1
do
if mst.bitv_is_set_bit(b, j)
then
t:insert(256 * block_offset +
8 * (i - 1) +
(8 - j))
end
end
end
end
return {ndn=n, bits=t}
end
function iterate_modulo_ranges(bits, modulo, f, st, en)
local st = st or 1
local nbits = #bits
local en = en or nbits
local i = st
while i <= en
do
local j = i
local v = bits[i]
local mr = math.floor(v / modulo)
--mst.d('finding subblock', i, en, modulo, v, mr)
while (j+1) <= en and math.floor(bits[j+1] / modulo) == mr
do
j = j + 1
end
f(mr, i, j)
i = j + 1
--mst.d('i now', i)
end
end
function rdata_nsec:do_encode(o, context)
mst.a(o, 'no object given to encode?!?')
mst.a(o.ndn)
local n, err = try_encode_name(o.ndn, context)
if not n then return nil, err end
local t = {}
-- encoding of the bits is bit more complex
local bits = o.bits
table.sort(bits)
-- first off, try to get the 256-bit blocks, and deal with them
iterate_modulo_ranges(bits, 256,
function (mr, i1, j1)
--mst.d('handling', mr)
local t0 = {}
iterate_modulo_ranges(bits, 8,
function (br, i2, j2)
br = br % 32
while #t0 < br
do
table.insert(t0, 0)
end
local v = 0
for i=i2,j2
do
local b=bits[i]
v = mst.bitv_set_bit(v, 8-b%8)
end
--mst.d('produced', v)
table.insert(t0, v)
end, i1, j1)
table.insert(t, string.char(mr, #t0, unpack(t0)))
end)
mst.array_extend(n, t)
--mst.d('concatting', n)
return table.concat(n)
end
local function add_rtype_decoder(type, cl, o)
rtype_map[type] = o
function o.encode(self, o, context)
return cl:encode(o[self.field], context)
end
function o.decode(self, o, cur, context)
cur.endpos = cur.pos + o.rdlength
local r, err = cl:decode(cur, context)
cur.endpos = nil
if not r then return nil, err end
o[self.field] = r
return true
end
end
add_rtype_decoder(dns_const.TYPE_SRV, rdata_srv, {
field='rdata_srv',
field_equal=repr_equal,
default_ttl=mdns_const.DEFAULT_NAME_TTL,
})
add_rtype_decoder(dns_const.TYPE_NSEC, rdata_nsec, {
field='rdata_nsec',
field_equal=repr_equal,
default_ttl=mdns_const.DEFAULT_NAME_TTL,
})
add_rtype_decoder(dns_const.TYPE_SOA, rdata_soa, {
field='rdata_soa',
field_equal=repr_equal,
default_ttl=mdns_const.DEFAULT_NAME_TTL,
})