-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdns_core.lua
577 lines (496 loc) · 15.8 KB
/
mdns_core.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: mdns_core.lua $
--
-- Author: Markus Stenberg <markus [email protected]>
--
-- Copyright (c) 2012 cisco Systems, Inc.
--
-- Created: Mon Dec 17 15:07:49 2012 mstenber
-- Last modified: Mon Nov 4 14:08:34 2013 mstenber
-- Edit time: 994 min
--
-- This module contains the main mdns algorithm; it is not tied
-- directly to socket, event loop, or time functions. Instead,
-- bidirectional API is assumed to address those.
-- API from outside => mdns:
-- - run() - perform one iteration of whatever it does
-- - next_time() - when to run next time
-- - recvfrom(data, from, fromport)
-- additionally within mdns_ospf subclass
-- - skv
-- API from mdns => outside:
-- - time() => current timestamp
-- - sendto(data, to, toport)
-- additionally within mdns_ospf subclass
--=> skv
-- Internally, implementation has two different data structures per if:
-- - cache (=what someone has sent to us, following the usual TTL rules)
-- - own (=what we want to publish on the link, following the state
-- machine for each record)
-- TODO:
-- - noticing already sent responses on link (should be unlikely, but
-- you never know)
-- - filtering of RRs we pass along (linklocals aren't very useful,
-- for example)
-- - ton more, see SHOULD/MUST list in mdns_test.txt
require 'mst'
require 'dns_db'
require 'mdns_if'
require 'mdns_const'
require 'linux_if'
require 'ssloop'
local _mcj = require 'mcastjoiner'.mcj
local _eventful = require 'mst_eventful'.eventful
IF_INFO_VALIDITY_PERIOD=60
module(..., package.seeall)
-- global mdns structure, which mostly just deals with different
-- mdns_if instances
mdns = mst.create_class({class='mdns',
ifclass=mdns_if.mdns_if,
time=ssloop.time,
mandatory={'sendto', 'shell'},
events={'if_active'}, -- ifname + true/false
},
_mcj,
_eventful)
function mdns:init()
_mcj.init(self)
_eventful.init(self)
self.ifname2if = {}
end
function mdns:uninit()
-- intentionally not calling _mcj.uninit
-- (it would try to detach skv, while not attached - we play with
-- skv from superclass, not in mcj)
_eventful.uninit(self)
-- call uninit for each interface object (intentionally after us,
-- as we are connected _to_ interface object)
for ifname, ifo in pairs(self.ifname2if)
do
ifo:done()
end
end
function mdns:get_ipv4_map()
local now = self.time()
local was = self.ipv4map_refresh
local refreshed
if not was or (was + IF_INFO_VALIDITY_PERIOD) < now
then
-- TODO - consider if it is worth storing this if_table;
-- for the time being, we save memory by not keeping it around..
local if_table = linux_if.if_table:new{shell=self.shell}
self.ipv4map = if_table:read_ip_ipv4()
self.ipv4map_refresh = now
end
return self.ipv4map, self.ipv4map_refresh
end
function mdns:get_ipv6_map()
local now = self.time()
local was = self.ipv6map_refresh
local refreshed
if not was or (was + IF_INFO_VALIDITY_PERIOD) < now
then
-- TODO - consider if it is worth storing this if_table;
-- for the time being, we save memory by not keeping it around..
local if_table = linux_if.if_table:new{shell=self.shell}
self.ipv6map = if_table:read_ip_ipv6()
self.ipv6map_refresh = now
end
return self.ipv6map, self.ipv6map_refresh
end
function mdns:calculate_local_binary_prefix_set()
local map = self:get_ipv6_map()
local m = {}
for ifname, ifo in pairs(map)
do
for i, prefix in ipairs(ifo.ipv6 or {})
do
local p = ipv6s.new_prefix_from_ascii(prefix)
local b = p:get_binary()
local v = m[b]
-- intentionally produce always consistent mapping to interfaces
-- here - that's why we do this check
if not v or v > ifname
then
m[b] = ifname
end
end
end
return m
end
function mdns:get_local_binary_prefix_set()
local map, gen = self:get_ipv6_map()
if gen ~= self.local_binary2ifname_gen
then
local m = self:calculate_local_binary_prefix_set()
self.local_binary2ifname = m
self.local_binary2ifname_gen = gen
end
self:a(self.local_binary2ifname,
'no local_binary2ifname',
map, gen)
return self.local_binary2ifname
end
function mdns:is_local_binary_prefix(b)
return self:get_local_binary_prefix_set()[b]
end
function mdns:get_local_interface(addr)
-- it better be IPv6; IPv4 we don't care about for the time being
local r = ipv4s.address_to_binary_address(addr)
if r
then
return
end
-- ok, looks like IPv6 address
local b = ipv6s.address_to_binary_address(addr)
mst.a(#b == 16)
-- just use the prefix as key
b = string.sub(b, 1, 8)
return self:is_local_binary_prefix(b)
end
function mdns:get_if(ifname)
mst.a(type(ifname) == 'string', 'non-string ifname', ifname)
local o = self.ifname2if[ifname]
if not o
then
o = self.ifclass:new{ifname=ifname, parent=self}
self.ifname2if[ifname] = o
self:connect(o.queue_check_propagate_rr,
function (rr)
self:queue_check_propagate_if_rr(ifname, rr)
end)
self:connect(o.cache.is_not_empty,
function ()
self.if_active(ifname, true)
end)
self:connect(o.cache.is_empty,
function ()
self.if_active(ifname, false)
end)
end
return o
end
function mdns:iterate_ifs_ns(is_own, f)
self:a(f, 'nil function')
for ifname, o in pairs(self.ifname2if)
do
local ns = is_own and o.own or o.cache
f(ns, o)
end
end
function mdns:iterate_ifs_matching_q(is_own, q, f)
for ifname, o in pairs(self.ifname2if)
do
-- nil = no kas
o:iterate_matching_query(is_own, q, nil, f)
end
end
function mdns:repr_data()
return mst.repr{rid=self.rid}
end
function mdns:run()
self:run_propagate_check()
self.now = self.time()
-- expire items
for ifname, ifo in pairs(self.ifname2if)
do
ifo:run()
end
self.now = nil
end
function mdns:should_run()
local nt = self:next_time()
if not nt then return end
local now = self.time()
return nt <= now
end
function mdns:next_time()
local best
if self.pending_propagate_check
then
best = 0
else
for ifname, ifo in pairs(self.ifname2if)
do
local b = ifo:next_time()
if not best or (b and b < best)
then
best = b
end
end
end
mst.d('next_time returning', best)
return best
end
function mdns:recvfrom(data, src, srcport)
self:a(type(data) == 'string', 'non-string data',
data, src, srcport)
self:a(type(src) == 'string', 'non-string src',
data, src, srcport)
-- n/a - comes actually as a string, sigh
--self:a(type(srcport) == 'number', 'non-number srcport', data, src, srcport)
local l = mst.string_split(src, '%')
local addr, ifname
if #l < 2
then
addr = src
ifname = self:get_local_interface(src)
if not ifname
then
self:d('global? query received, ignoring', src)
return
end
else
mst.a(#l == 2, 'invalid src', src)
addr, ifname = unpack(l)
end
local ifo = self:get_if(ifname)
ifo:handle_recvfrom(data, addr, srcport)
end
local function nsh_count(nsh)
mst.a(nsh)
local c = 0
for k, ns in pairs(nsh)
do
c = c + ns:count()
end
return c
end
function mdns:own_count()
local c = 0
self:iterate_ifs_ns(true,
function (ns)
c = c + ns:count()
end)
return c
end
function mdns:cache_count()
local c = 0
self:iterate_ifs_ns(false,
function (ns)
c = c + ns:count()
end)
return c
end
-- related things
function mdns:query(ifname, ...)
local ifo = self:get_if(ifname)
ifo:query(...)
end
function mdns:start_query(ifname, ...)
local ifo = self:get_if(ifname)
ifo:start_query(...)
end
function mdns:stop_query(ifname, ...)
local ifo = self:get_if(ifname)
ifo:stop_query(...)
end
function mdns:insert_if_own_rr(ifname, rr)
local ifo = self:get_if(ifname)
ifo:insert_own_rr(rr)
end
function mdns:is_forwardable_rr(rr)
-- we don't want to publish NSEC entries
if rr.rtype == dns_const.TYPE_NSEC
then
return false
end
if rr.rtype == dns_const.TYPE_AAAA
then
-- nor do we want to publish linklocal AAAA records
return not ipv6s.address_is_linklocal(rr.rdata_aaaa)
end
return true
end
function mdns:queue_check_propagate_if_rr(ifname, rr)
local p = self.pending_propagate_check
-- we don't propagate NSEC records, instead we produce them
if not self:is_forwardable_rr(rr)
then
return
end
if not p
then
p = dns_db.ns:new{}
self.pending_propagate_check = p
end
local o = p:insert_rr{name=rr.name,
rtype=rr.rtype,
rclass=dns_const.CLASS_IN,
ifname=ifname}
if o.ifname and o.ifname ~= ifname
then
o.ifname = nil
end
end
function mdns:queue_check_propagate_all()
-- this is rather brute-force; for EVERY cache interface, check
-- _everything_. hopefully configuration doesn't change that often..
-- (we take entries both from cache + own on all interfaces to be
-- thorough)
self:iterate_ifs_ns(false,
function (ns, ifo)
ns:iterate_rrs(function (rr)
local n = ifo and ifo.ifname
self:queue_check_propagate_if_rr(n, rr)
end)
end)
self:iterate_ifs_ns(true,
function (ns, ifo)
ns:iterate_rrs(function (rr)
local n = ifo and ifo.ifname
self:queue_check_propagate_if_rr(n, rr)
end)
end)
end
function mdns:run_propagate_check()
if not self.pending_propagate_check
then
return
end
local p = self.pending_propagate_check
self.pending_propagate_check = nil
p:iterate_rrs(function (rr)
self:run_propagate_check_o(rr)
end)
end
function mdns:valid_propagate_src_ifo(ifo)
return true
end
function mdns:valid_propagate_dst_ifo(ifo)
return true
end
function mdns:run_propagate_check_o(o)
-- o is rr-ish, but cache_flush, rdata, and so forth are not set
-- first off, detect if there's conflict for this name+rtype
-- across _all_ caches; if there is, all we need to do is
-- just remove all related own entries
local c = 0
local foundifo
o.cache_flush = true
self:iterate_ifs_ns(false, function (ns, ifo)
if not self:valid_propagate_src_ifo(ifo)
then
return
end
-- see if we can find cache_flush
local r = ns:find_rr_list(o)
if r
then
for _, rr in ipairs(r)
do
if rr.cache_flush
then
foundifo = ifo
c = c + 1
return
end
end
end
-- similarly, if it has been probed
-- for, it is CF name+rtype
if ifo and ifo.probe
then
r = ifo.probe:find_rr_list(o)
if r
then
foundifo = ifo
c = c + 1
end
end
end)
self:d('run_propagate_check_o', o, c, foundifo)
if c > 1
then
-- XXX - we should probably ensure that if the content seen
-- on both interfaces is same, we can still propagate it;
-- however, for the time being, we choose not to do that
self:stop_propagate_unique_o(o)
return
end
if c == 1
then
self:propagate_unique_ifo_o(foundifo, o)
return
end
-- nothing cache flush found in the whole system; so we can
-- do the more peaceful 'let all live' kind of propagation occur
o.cache_flush = false
self:propagate_shared_o(o)
end
function mdns:stop_propagate_unique_o(o)
-- go through all interfaces, and propagate empty set for the given o
self:propagate_o_l_ifo_to_ifs(o)
end
function mdns:propagate_unique_ifo_o(ifo, o)
-- rather simple - get the set, and propagate that to each
-- interface
local r = ifo and ifo.own:find_rr_list(o)
or self:find_all_cache_rr_matching_o(o)
self:propagate_o_l_ifo_to_ifs(o, r, ifo)
end
function mdns:iterate_all_cache_rr_matching_o(o, f)
self:iterate_ifs_ns(false, function (ns, ifo)
ns:iterate_rrs_for_ll(o.name,
function (rr)
if rr.rtype == o.rtype
then
f(rr)
end
end)
end)
end
function mdns:find_all_cache_rr_matching_o(o)
-- not super lightweight operation, but oh well..
-- basic idea:
-- first, gather each entry from caches, taking the one with longest
-- ttl if same entry encountered multiple times
local rns = dns_db.ns:new{}
self:iterate_all_cache_rr_matching_o(o, function (rr)
if rr.rtype ~= o.rtype
then
return
end
local orr =
rns:insert_rr(rr, true)
if not rr.ttl
or (orr.ttl
and orr.ttl < rr.ttl)
then
orr.ttl = rr.ttl
end
end)
return rns:values()
end
function mdns:propagate_shared_o(o)
local l = self:find_all_cache_rr_matching_o(o)
-- then, for each target interface, propagate that set to that interface
self:propagate_o_l_ifo_to_ifs(o, l)
end
function mdns:propagate_o_l_ifo_to_ifs(o, l, skip_ifo)
for ifname, ifo in pairs(self.ifname2if)
do
if ifo ~= skip_ifo and self:valid_propagate_dst_ifo(ifo)
then
-- upsert entries
ifo:propagate_o_l(o, l)
else
-- clear propagation of that stuff on this interface (if any)
ifo:propagate_o_l(o)
end
end
end
-- this implements timeout API, to make sure the MDNS
-- gets all execution time it needs within ssloop
mdns_runner = mst.create_class{class='mdns_runner', mandatory={'mdns'}}
function mdns_runner:run_timeout()
-- just call run - timeouts are handled on per-iteration basis
-- using the get_timeout
mst.d('calling mdns:run()')
self.mdns:run()
end
function mdns_runner:get_timeout()
return self.mdns:next_time()
end