-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhp_ospf.lua
341 lines (311 loc) · 8.73 KB
/
hp_ospf.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
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: hp_ospf.lua $
--
-- Author: Markus Stenberg <markus [email protected]>
--
-- Copyright (c) 2013 cisco Systems, Inc.
--
-- Created: Thu May 23 14:11:50 2013 mstenber
-- Last modified: Tue Nov 5 03:54:37 2013 mstenber
-- Edit time: 148 min
--
-- Auto-configured hybrid proxy code. It interacts with skv to
-- auto-configure itself, but in general hp_core base is used as-is.
require 'hp_core'
require 'elsa_pa'
require 'ssloop'
module(..., package.seeall)
local _hp = hp_core.hybrid_proxy
-- this filter function can be used in e.g. attach_skv of mcastjoiner;
-- it is also used directly here. regardless, all 3 elements of a
-- hybrid proxy (hp*, which provides dns, per_ip_server with N
-- dns_proxy instances, and mdns_client) should have _same_ idea of
-- valid laps to use. otherwise, not so happy things happen..
function valid_lap_filter(lap)
-- we're interested about _any_ if! even if
-- we're not owner, as we have to give answers
-- for our own address. however, if we're
-- deprecated, then not that good.. or external,
-- perhaps
local ext = lap.external
local dep = lap.depracate
return not ext and not dep
end
hybrid_ospf = _hp:new_subclass{name='hybrid_ospf',
-- rid isn't
-- mandatory, we get
-- it from ospf
mandatory={'domain',
'mdns_resolve_callback',
},
lap_filter=valid_lap_filter,
events={'rid_changed', 'lap_changed'},
}
function hybrid_ospf:init()
self:d('init')
_hp.init(self)
end
function hybrid_ospf:uninit()
self:d('uninit')
self:detach_skv()
self:set_timeout(false)
end
function hybrid_ospf:set_timeout(enabled)
self:d('set_timeout', enabled)
if not enabled
then
if self.timeout
then
self:d('set_timeout clearing timeout')
self.timeout:done()
self.timeout = nil
end
return
end
self:a(not self._is_done, 'post-death notification')
if self.timeout
then
return
end
-- want timeout, but none yet
self:d('set_timeout adding timeout')
-- schedule a timeout to update our state in a second
-- ~once/second should not be 'a lot'; recreate_tree side
-- effect (skv updates) is relevant 'soon', so we do this
-- even if nobody needs the tree itself
local loop = ssloop.loop()
self:d('queueing timeout')
self.timeout = loop:new_timeout_delta(1, function ()
self:d('timeout')
self:set_timeout(false)
-- get_root will lead to
-- recreate_tree (if
-- necessary)
self:get_root()
end)
self.timeout:start()
end
function hybrid_ospf:recreate_tree()
self.local_zones = {}
self.search_path = {dns_db.ll2name(self.domain)}
local root = _hp.recreate_tree(self)
self.skv:set(elsa_pa.HP_MDNS_ZONES_KEY, self.local_zones)
self.skv:set(elsa_pa.HP_SEARCH_LIST_KEY, self.search_path)
return root
end
function hybrid_ospf:create_local_forward_node(router, o)
local n = _hp.create_local_forward_node(self, router, o)
if not n then return end
local ip = self:get_ip()
if not ip then return end
local browse = self:is_owner_of_iid(o.iid) and 1 or nil
-- We don't want inactive domains to be browsed.
if not self.disable_filter_browse_active_only and
not self.active[o.ifname]
then
browse = nil
end
local o2 = {
name=n:get_fqdn(),
ip=ip,
browse=browse,
}
table.insert(self.local_zones, o2)
end
function hybrid_ospf:create_local_reverse_node(root, router, o)
local n = _hp.create_local_reverse_node(self, root, router, o)
if not n then return end
local ip = self:get_ip()
if not ip then return end
-- don't browse reverse zones..
local o2 = {
name=n:get_fqdn(),
ip=ip,
}
table.insert(self.local_zones, o2)
end
function hybrid_ospf:create_remote_zone(root, zone)
local n = _hp.create_remote_zone(self, root, zone)
self:d('create_remote_zone', zone, n)
if not n then return end
if zone.search
then
table.insert(self.search_path, n:get_fqdn())
end
end
function hybrid_ospf:attach_skv(skv)
self:detach_skv()
self.skv = skv
-- The listening to appropriate IP addresses etc is handled in
-- per_ip_server. So, what we care about is:
-- - router id
-- - usp (for usable prefix ranges)
-- - asp (for assigned prefixes)
-- - asa (for assigned addresses)
self.f = function (k, v)
self:d('skv notification', k)
if k == elsa_pa.OSPF_RID_KEY
then
self:set_rid(v)
elseif k == elsa_pa.OSPF_RNAME_KEY
then
self.rname = v
elseif k == elsa_pa.OSPF_IPV4_DNS_KEY
then
-- no need for tree invalidation (handled in :get_server per-request)
self.ospf_v4_dns = v or {}
return
elseif k == elsa_pa.OSPF_DNS_KEY
then
-- no need for tree invalidation (handled in :get_server per-request)
self.ospf_dns = v or {}
return
elseif k == elsa_pa.OSPF_USP_KEY
then
self.usp = v
elseif k == elsa_pa.OSPF_HP_DOMAIN_KEY
then
-- domain has changed (perhaps)
self.domain = v
elseif k == elsa_pa.OSPF_HP_ZONES_KEY
then
-- domain has changed (perhaps)
self.zones = v
elseif k == elsa_pa.OSPF_LAP_KEY
then
self.lap = v
self:lap_changed()
else
-- unknown key => no need to invalidate tree, hopefully ;-)
return
end
self:refresh_tree()
end
self.skv:add_change_observer(self.f)
end
function hybrid_ospf:refresh_tree()
_hp.refresh_tree(self)
self:set_timeout(true) -- schedule a timeout to redo the tree
end
function hybrid_ospf:iterate_usable_prefixes(f)
if not self.usp
then
return
end
for i, v in ipairs(self.usp)
do
f(v.prefix)
end
end
function hybrid_ospf:iterate_lap(f)
for i, lap in ipairs(self.lap or {})
do
if self.lap_filter(lap)
then
f(lap)
end
end
end
function hybrid_ospf:iterate_remote_zones(f)
for i, zone in ipairs(self.zones or {})
do
f(zone)
end
end
function hybrid_ospf:detach_skv()
if not self.skv
then
return
end
self.skv:remove_change_observer(self.f)
self.f = nil
self.skv = nil
end
function hybrid_ospf:get_ip()
-- find _one_ ip that matches us
local found
-- IPv6 address
for i, lap in ipairs(self.lap or {})
do
local ip = lap.address
if ip and not ipv6s.address_is_ipv4(ip) and self.lap_filter(lap)
then
-- strip prefix, just in case
--ip = mst.string_split(ip, '/')[1]
self:a(not ip or not string.find(ip, '/'),
'ip should not be prefix', ip)
self:d('got ip', ip)
return ip
end
end
-- any address if no IPv6 available
for i, lap in ipairs(self.lap or {})
do
local ip = lap.address
if ip and self.lap_filter(lap)
then
-- strip prefix, just in case
--ip = mst.string_split(ip, '/')[1]
self:a(not ip or not string.find(ip, '/'),
'ip should not be prefix', ip)
self:d('got ip', ip)
return ip
end
end
self:d('no ip available', self.lap)
end
function hybrid_ospf:get_server()
local l = self.ospf_dns
if l and #l > 0
then
return l[1]
end
local l = self.ospf_v4_dns
if l and #l > 0
then
return l[1]
end
return _hp.get_server(self)
end
function hybrid_ospf:rid2label(rid)
-- for our own rid only, use the rname as is
local n
if tostring(rid) == tostring(self.rid)
then
n = self.rname
end
if n then return n end
-- if no luck, fallback to parent
return _hp.rid2label(self, rid)
end
function hybrid_ospf:is_owner_of_iid(iid)
for i, lap in ipairs(self.lap or {})
do
if lap.iid == iid
then
if lap.owner
then
return true
end
end
end
end
function hybrid_ospf:iid2label(iid)
-- if no luck, fallback to parent
for i, lap in ipairs(self.lap or {})
do
if lap.iid == iid
then
local n = lap.ifname
if n
then
-- sanitize name - we don't want dots there
n = string.gsub(n, '%.', '_')
return n
end
end
end
return _hp.rid2label(self, rid)
end