-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns_codec.lua
284 lines (237 loc) · 8.22 KB
/
dns_codec.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
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: dns_codec.lua $
--
-- Author: Markus Stenberg <markus [email protected]>
--
-- Copyright (c) 2012 cisco Systems, Inc.
--
-- Created: Fri Nov 30 11:15:52 2012 mstenber
-- Last modified: Mon Oct 21 12:05:20 2013 mstenber
-- Edit time: 289 min
--
-- Functionality for en-decoding various DNS structures;
-- - DNS RR
-- - DNS query
-- - DNS message
-- Only really tricky part is the message compression; we do it by
-- having a local state about the labels' locations while
-- uncompressing. When compressing, we have hash_set which contains
-- all dumped label substrings and their locations, and we pick one
-- whenever we find something that matches.
-- Internally, FQDN is stored WITHOUT the final terminating 'empty'
-- label. There is no point, as we're storing the label lists in Lua
-- arrays, and we can just assume it's there, always.
-- NOTE: Current message compression (decoding) algorithm works only
-- in RFC1035 compliant format => has to be _prior_ occurence of the
-- name. If it's subsequent one, all bets are off..
-- XXX - implement name compression for encoding too!
require 'codec'
require 'ipv4s'
require 'ipv6s'
require 'dns_const'
require 'dns_name'
require 'dns_rdata'
require 'dns_db'
module(..., package.seeall)
local abstract_base = codec.abstract_base
local abstract_data = codec.abstract_data
local cursor_has_left = codec.cursor_has_left
--- actual data classes
dns_header = abstract_data:new{class='dns_header',
format='id:u2 [2|qr:b1 opcode:u4 aa:b1 tc:b1 rd:b1 ra:b1 z:u1 ad:b1 cd:b1 rcode:u4] qdcount:u2 ancount:u2 nscount:u2 arcount:u2',
header_default={id=0,
qr=false,
opcode=0,
aa=false,
tc=false,
rd=false,
ra=false,
z=0,
-- ad, cd defined in RFC2535
ad=false,
cd=false,
rcode=0,
qdcount=0,
ancount=0,
nscount=0,
arcount=0,}
}
dns_query = abstract_data:new{class='dns_query',
format='qtype:u2 [2|qu:b1 qclass:u15]',
header_default={qtype=0,
qu=false,
qclass=dns_const.CLASS_IN},
}
function dns_query:try_decode(cur, context)
-- in query, the name is _first_ part. So we decode that, then the
-- fixed-length fields.
local r = {}
local name, err = dns_name.try_decode_name(cur, context)
if not name then return nil, err end
local o, err = abstract_data.try_decode(self, cur)
if not o then return nil, err end
-- copy the name to the record
o.name = name
return o
end
function dns_query:do_encode(o, context)
local t, err = dns_name.try_encode_name(o.name, context)
if not t then return nil, err end
table.insert(t, abstract_data.do_encode(self, o))
return table.concat(t)
end
dns_rr = abstract_data:new{class='dns_rr',
format='rtype:u2 [2|cache_flush:b1 rclass:u15] ttl:u4 rdlength:u2',
header_default={rtype=0,
cache_flush=false,
rclass=dns_const.CLASS_IN,
ttl=0,
rdlength=0,},
}
function dns_rr:try_decode(cur, context)
-- in RR, the name is _first_ part. So we decode that, then the
-- fixed-length fields, and then finally leftover rdata
local r = {}
local name, err = dns_name.try_decode_name(cur, context)
if not name then return nil, err end
r.name = name
local o, err = abstract_data.try_decode(self, cur)
if not o then return nil, err end
-- copy the header fields
mst.table_copy(o, r)
local handler = dns_rdata.rtype_map[r.rtype]
if handler and (not context or not context.disable_decode_rrs)
then
--self:d('using handler', cur)
local ok, err = handler:decode(r, cur, context)
if not ok then return nil, err end
else
local l = r.rdlength
--self:d('default rdata handling (as-is)', r.rtype, l)
if l > 0
then
if not cursor_has_left(cur, l)
then
return nil, 'not enough bytes for body'
end
r.rdata = cur:read(l)
else
r.rdata = ''
end
end
r.rdlength = nil
return r
end
function dns_rr:produce_rdata(o, context)
local handler = dns_rdata.rtype_map[o.rtype or -1]
if handler
then
--mst.d('calling encode', context.pos, o)
-- update pos s.t. the rdata encoder will have correct
-- position to start at..
if context
then
context.pos = context.pos + self.header_length
end
return handler:encode(o, context)
end
return o.rdata
end
function dns_rr:do_encode(o, context)
mst.a(type(o) == 'table')
self:a(o.name, 'no name', o)
local t, err = dns_name.try_encode_name(o.name, context)
if not t then return nil, err end
o.rdata = self:produce_rdata(o, context)
if not o.rdata then return nil, err end
o.rdlength = #o.rdata
table.insert(t, abstract_data.do_encode(self, o))
table.insert(t, o.rdata)
return table.concat(t)
end
-- dns message - container for everything
-- assumption:
-- h = header, qd = question, an = answer, ns = authority, ar = additional
dns_message = codec.abstract_base:new{class='dns_message',
lists={
{'qd', dns_query},
{'an', dns_rr},
{'ns', dns_rr},
{'ar', dns_rr},
}
}
function dns_message:repr_data()
return ''
end
function dns_message:do_encode(o)
local h = o.h or {}
for i, v in ipairs(self.lists)
do
np, cl = unpack(v)
h[np .. 'count'] = o[np] and #o[np] or 0
end
local t = mst.array:new{}
-- initially encode header
local r, err = dns_header:encode(h)
if not r
then
return nil, err
end
t:insert(r)
-- context for storing the encoded offsets of names
local pos = #r
local context = {pos=pos}
context.nt = {}
-- then, handle each sub-list
for i, v in ipairs(self.lists)
do
np, cl = unpack(v)
--self:d('considering', np, o[np])
for i, v in ipairs(o[np] or {})
do
--self:d('encoding', np, i)
-- store the current position where this stuff _starts_
local r, err = cl:encode(v, context)
if not r then return nil, err end
t:insert(r)
-- sub-encoders may have played with the position.
-- therefore, we update the 'master copy' of pos here
pos = pos + #r
context.pos = pos
end
end
-- finally concat result together and return it
return table.concat(t)
end
function dns_message:try_decode(cur, context)
local o = {}
-- grab header
local h, err = dns_header:decode(cur)
if not h then return nil, err end
o.h = h
-- used to store message compression offsets
if context
then
context = mst.table_copy(context)
else
context = {}
end
-- then handle each list
for i, v in ipairs(self.lists)
do
np, cl = unpack(v)
local l = mst.array:new{}
local cnt = h[np .. 'count']
for i=1,cnt
do
local o, err = cl:decode(cur, context)
self:d('got', np, i, o)
if not o then return nil, err end
l[#l+1] = o
end
o[np] = l
end
return o
end