-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns_db.lua
410 lines (366 loc) · 9.81 KB
/
dns_db.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: dns_db.lua $
--
-- Author: Markus Stenberg <markus [email protected]>
--
-- Copyright (c) 2012 cisco Systems, Inc.
--
-- Created: Mon Dec 17 14:09:58 2012 mstenber
-- Last modified: Mon Nov 4 14:47:40 2013 mstenber
-- Edit time: 222 min
--
-- This is a datastructure used for storing the (m)DNS
-- information. Typical example usage case is to have NS per local
-- interface, and yet another one based on data from OSPF.
-- The data is stored in a multimap based on hash of the name;
-- collisions are handled by the multimap lists.
-- What is _unique_?
-- name + rtype + rclass + rdata (=~ whole rr, -ttl)
-- (cache_flush is used to clear contents of cache based on name+rtype+rclass,
-- but it should not affect storage in dns_db itself, just how it's called)
require 'mst'
require 'dns_const'
require 'dns_rdata'
local _eventful = require 'mst_eventful'.eventful
module(..., package.seeall)
function name2ll(name)
mst.a(name, 'name not set?!?')
local ntype = type(name)
if ntype == 'table'
then
return name
end
mst.a(ntype == 'string', 'unsupported type', ntype, name)
-- use '.' as separator, and pray hard that there isn't . within the labels
local t = mst.string_split(name, '.')
-- we don't include the last empty label in the label lists we use
--table.insert(t, '')
-- eliminate the last empty one, if any (dns name can validly end
-- with '.', it just means it's globally scoped (but we ignore that
-- distinction for most part))
if #t>0 and #t[#t] == 0
then
t[#t] = nil
end
return t
end
function ll2name(ll)
if type(ll) == 'string'
then
return ll
end
return table.concat(ll, '.')
end
function ll2key(ll)
-- just in case, make sure it's ll
ll = name2ll(ll)
local lowercase_ll = mst.array_map(ll, string.lower)
-- space-efficient, but perhaps painful to calculate?
--local s = mst.repr(lowercase_ll)
--return mst.create_hash(s)
-- computationally efficient (all we do is just concatenate lengths + data)
local t = dns_name.encode_name(lowercase_ll)
return table.concat(t)
end
function ll2nameish(ll)
local ltype = type(ll)
if ltype ~= 'table'
then
return ll
end
-- ok, it's table, let's see if we can actually convert it
for i, v in ipairs(ll)
do
if string.find(v, '[.]')
then
return ll
end
end
-- we can! so convert it to a name
return ll2name(ll)
end
function prefix2ll(s)
-- We do this in inverse order, and then reverse just in the end
local p = ipv6s.new_prefix_from_ascii(s)
local b = p:get_binary()
local bits = p:get_binary_bits()
local a
if p:is_ipv4()
then
-- IPv4 is of format
-- <reverse-ip>.in-addr.arpa
a = mst.array:new(mst.table_copy(dns_const.REVERSE_LL_IPV4_INVERSE))
for i=13,bits/8
do
a:insert(tostring(string.byte(string.sub(b, i, i))))
end
else
-- IPv6 is of format
-- <reverse-ip6-addr-per-hex-octet>.ip6.arpa
a = mst.array:new(mst.table_copy(dns_const.REVERSE_LL_IPV6_INVERSE))
-- just whole bytes?
for i=1,bits/8
do
local v = string.byte(string.sub(b, i, i))
a:insert(string.format('%x', math.floor(v / 16)))
a:insert(string.format('%x', v % 16))
end
end
a:reverse()
return a
end
-- extend dns_codec's dns_rr
function ll_equal(ll1, ll2)
if #ll1 ~= #ll2
then
return false
end
for i=1,#ll1
do
local s1 = string.lower(ll1[i])
local s2 = string.lower(ll2[i])
if s1 ~= s2 then return false end
end
return true
end
-- class used for handling single RR record (whether it's from
-- dns_codec, or synthetic created by us)
rr = mst.create_class{class='rr'}
function rr:repr_data()
local d = {name=self.name,
state=self.state,
wait_until=self.wait_until,
valid=self.valid,
next=self.next,
ttl=self.ttl,
rtype=self.rtype,
cache_flush=self.cache_flush}
local m = dns_rdata.rtype_map[self.rtype]
local f = m and m.field
if f and self[f]
then
d[f] = self[f]
else
d.rdata = self.rdata
end
return mst.repr(d)
end
function rr:get_rdata()
return dns_codec.dns_rr:produce_rdata(self)
end
function rr:rdata_equals(o)
--mst.a(o ~= rr, "can't compare with class")
local m = dns_rdata.rtype_map[self.rtype]
if m and o[m.field]
then
local f = m.field
local r = m:field_equal(self[f], o[f])
--mst.d(' fallback field match?', r)
return r
end
--self:a(o.rdata, 'no rdata', o)
if o.rdata
then
local r = o.rdata == self.rdata
--mst.d(' rdata match?', r)
return r
end
--mst.d(' fallback field not set, no rdata -> match')
return true
end
function rr:equals(o)
mst.a(o.rtype and o.name, 'mandatory bits missing')
return self.rtype == o.rtype
and self.rclass == (o.rclass or dns_const.CLASS_IN)
and ll_equal(self.name, o.name)
and rr.rdata_equals(self, o)
and not self.cache_flush == not o.cache_flush
end
-- namespace of RR records; it has ~fast access to RRs by name
ns = _eventful:new_subclass{class='ns',
events={'inserted', -- per entry
'removed',
'is_not_empty', -- when emptiness changes
'is_empty',
}}
function ns:init()
_eventful.init(self)
self.nh2rr = mst.multimap:new{}
end
function ns:repr_data()
return mst.repr({count=self:count()})
end
function ns:iterate_rrs(f)
self:a(f, 'nil function')
self.nh2rr:foreach(function (k, v)
f(v)
end)
end
function ns:iterate_rrs_safe(f)
self:a(f, 'nil function')
local r = {}
self:iterate_rrs(function (rr)
table.insert(r, rr)
end)
for i, rr in ipairs(r)
do
f(rr)
end
end
function ns:iterate_rrs_for_ll(ll, f)
-- just in case, make sure it's ll
ll = name2ll(ll)
local key = ll2key(ll)
local l = self.nh2rr[key]
for i, v in ipairs(l or {})
do
if ll_equal(v.name, ll)
then
if f(v)
then
break
end
end
end
end
function ns:iterate_rrs_for_ll_safe(ll, f)
local r
self:iterate_rrs_for_ll(ll, function (rr)
r = r or {}
table.insert(r, rr)
end)
if r
then
for i, rr in ipairs(r)
do
f(rr)
end
end
end
function ns:find_rr_list_for_ll(ll)
local r = {}
self:iterate_rrs_for_ll(ll, function (rr)
table.insert(r, rr)
end)
return r
end
function ns:find_rr_list(o)
local r
self:a(o.name, 'missing name', o)
self:a(o.rtype, 'missing rtype', o)
self:iterate_rrs_for_ll(o.name, function (rr)
if rr:equals(o)
then
r = r or {}
table.insert(r, rr)
end
end)
return r
end
function ns:find_rr(o)
self:a(o.name, 'missing name', o)
self:a(o.rtype, 'missing rtype', o)
local found
self:iterate_rrs_for_ll(o.name, function (rr)
if rr:equals(o)
then
found = rr
return 1
end
end)
return found
end
-- transactionally correct multi-rr insert
-- initially, it checks based on cache_flush whether or not
-- the matching rrs should be zapped
function ns:insert_rrs(l, do_copy)
local all = {}
local fresh = {}
-- first off, handle cache_flush bit
for i, rr in ipairs(l)
do
if rr.cache_flush
then
-- get rid of anything matching it
-- (regardless of rdata)
local o = {name=rr.name, rtype=rr.rtype, rclass=rr.rclass}
while self:remove_rr(o)
do
self:d('insert_rrs removed one matching', o)
end
end
end
-- then, insert all
for i, rr in ipairs(l)
do
local o, is_new = self:insert_rr(rr, do_copy)
all[rr] = o
if is_new
then
fresh[rr] = o
end
end
return all, fresh
end
function ns:insert_rr(o, do_copy)
-- these fields have to be set
self:a(o.name and o.rtype and o.rclass,
'one of mandatory fields is missing (name/rtype/rclass)', o)
-- let's see if we have _exactly_ same rr already
local old_rr = self:find_rr(o)
if old_rr
then
self:d('insert_rr reused old rr', old_rr)
return old_rr, false
end
if self.enable_copy or do_copy == true
then
o = mst.table_copy(o)
o = rr:new(o)
elseif getmetatable(o) ~= rr
then
o = rr:new(o)
end
self:insert_raw(o)
return o, true
end
function ns:insert_raw(o)
-- not found - have to add
local ll = o.name
local key = ll2key(ll)
local was_empty = self.nh2rr:is_empty()
self.nh2rr:insert(key, o)
self:d('calling inserted', o)
self.inserted(o)
if was_empty
then
self.is_not_empty()
end
return o, true
end
function ns:remove_rr(o)
local old_rr = self:find_rr(o)
if not old_rr then return end
--self:d('remove_rr', old_rr)
local ll = o.name
local key = ll2key(ll)
self.nh2rr:remove(key, old_rr)
self:d('calling removed', old_rr)
self.removed(old_rr)
if self.nh2rr:is_empty()
then
self.is_empty()
end
return old_rr
end
function ns:count()
return self.nh2rr:count()
end
function ns:values()
return self.nh2rr:values()
end
function ns:foreach(f)
return self.nh2rr:foreach_values(f)
end