/
rib.go
321 lines (280 loc) · 10.9 KB
/
rib.go
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
/* YaNFD - Yet another NDN Forwarding Daemon
*
* Copyright (C) 2020-2021 Eric Newberry.
*
* This file is licensed under the terms of the MIT License, as found in LICENSE.md.
*/
package mgmt
import (
"strconv"
"time"
"github.com/named-data/YaNFD/core"
"github.com/named-data/YaNFD/face"
"github.com/named-data/YaNFD/ndn"
"github.com/named-data/YaNFD/ndn/mgmt"
"github.com/named-data/YaNFD/ndn/tlv"
"github.com/named-data/YaNFD/table"
)
// RIBModule is the module that handles RIB Management.
type RIBModule struct {
manager *Thread
nextRIBDatasetVersion uint64
}
func (r *RIBModule) String() string {
return "RIBMgmt"
}
func (r *RIBModule) registerManager(manager *Thread) {
r.manager = manager
}
func (r *RIBModule) getManager() *Thread {
return r.manager
}
func (r *RIBModule) handleIncomingInterest(interest *ndn.Interest, pitToken []byte, inFace uint64) {
// Dispatch by verb
verb := interest.Name().At(r.manager.prefixLength() + 1).String()
switch verb {
case "register":
r.register(interest, pitToken, inFace)
case "unregister":
r.unregister(interest, pitToken, inFace)
case "announce":
r.announce(interest, pitToken, inFace)
case "list":
r.list(interest, pitToken, inFace)
default:
core.LogWarn(r, "Received Interest for non-existent verb '", verb, "'")
response := mgmt.MakeControlResponse(501, "Unknown verb", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
}
func (r *RIBModule) register(interest *ndn.Interest, pitToken []byte, inFace uint64) {
var response *mgmt.ControlResponse
if interest.Name().Size() < r.manager.prefixLength()+3 {
// Name not long enough to contain ControlParameters
core.LogWarn(r, "Missing ControlParameters in ", interest.Name())
response = mgmt.MakeControlResponse(400, "ControlParameters is incorrect", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
params := decodeControlParameters(r, interest)
if params == nil {
response = mgmt.MakeControlResponse(400, "ControlParameters is incorrect", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
if params.Name == nil {
core.LogWarn(r, "Missing Name in ControlParameters for ", interest.Name())
response = mgmt.MakeControlResponse(400, "ControlParameters is incorrect", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
faceID := inFace
if params.FaceID != nil && *params.FaceID != 0 {
faceID = *params.FaceID
if face.FaceTable.Get(faceID) == nil {
response = mgmt.MakeControlResponse(410, "Face does not exist", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
}
origin := table.RouteOriginApp
if params.Origin != nil {
origin = *params.Origin
}
cost := uint64(0)
if params.Cost != nil {
cost = *params.Cost
}
flags := table.RouteFlagChildInherit
if params.Flags != nil {
flags = *params.Flags
}
expirationPeriod := (*time.Duration)(nil)
if params.ExpirationPeriod != nil {
expirationPeriod = new(time.Duration)
*expirationPeriod = time.Duration(*params.ExpirationPeriod) * time.Millisecond
}
table.Rib.AddRoute(params.Name, faceID, origin, cost, flags, expirationPeriod)
if expirationPeriod != nil {
core.LogInfo(r, "Created route for Prefix=", params.Name, ", FaceID=", faceID, ", Origin=", origin, ", Cost=", cost, ", Flags=0x", strconv.FormatUint(flags, 16), ", ExpirationPeriod=", expirationPeriod)
} else {
core.LogInfo(r, "Created route for Prefix=", params.Name, ", FaceID=", faceID, ", Origin=", origin, ", Cost=", cost, ", Flags=0x", strconv.FormatUint(flags, 16))
}
responseParams := mgmt.MakeControlParameters()
responseParams.Name = params.Name
responseParams.FaceID = new(uint64)
*responseParams.FaceID = faceID
responseParams.Origin = new(uint64)
*responseParams.Origin = origin
responseParams.Cost = new(uint64)
*responseParams.Cost = cost
responseParams.Flags = new(uint64)
*responseParams.Flags = flags
if expirationPeriod != nil {
responseParams.ExpirationPeriod = new(uint64)
*responseParams.ExpirationPeriod = uint64(expirationPeriod.Milliseconds())
}
responseParamsWire, err := responseParams.Encode()
if err != nil {
core.LogError(r, "Unable to encode response parameters: ", err)
response = mgmt.MakeControlResponse(500, "Internal error", nil)
} else {
response = mgmt.MakeControlResponse(200, "OK", responseParamsWire)
}
r.manager.sendResponse(response, interest, pitToken, inFace)
}
func (r *RIBModule) unregister(interest *ndn.Interest, pitToken []byte, inFace uint64) {
var response *mgmt.ControlResponse
if interest.Name().Size() < r.manager.prefixLength()+3 {
// Name not long enough to contain ControlParameters
core.LogWarn(r, "Missing ControlParameters in ", interest.Name())
response = mgmt.MakeControlResponse(400, "ControlParameters is incorrect", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
params := decodeControlParameters(r, interest)
if params == nil {
response = mgmt.MakeControlResponse(400, "ControlParameters is incorrect", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
if params.Name == nil {
core.LogWarn(r, "Missing Name in ControlParameters for ", interest.Name())
response = mgmt.MakeControlResponse(400, "ControlParameters is incorrect", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
faceID := inFace
if params.FaceID != nil && *params.FaceID != 0 {
faceID = *params.FaceID
}
origin := table.RouteOriginApp
if params.Origin != nil {
origin = *params.Origin
}
table.Rib.RemoveRoute(params.Name, faceID, origin)
core.LogInfo(r, "Removed route for Prefix=", params.Name, ", FaceID=", faceID, ", Origin=", origin)
responseParams := mgmt.MakeControlParameters()
responseParams.Name = params.Name
responseParams.FaceID = new(uint64)
*responseParams.FaceID = faceID
responseParams.Origin = new(uint64)
*responseParams.Origin = origin
responseParamsWire, err := responseParams.Encode()
if err != nil {
core.LogError(r, "Unable to encode response parameters: ", err)
response = mgmt.MakeControlResponse(500, "Internal error", nil)
} else {
response = mgmt.MakeControlResponse(200, "OK", responseParamsWire)
}
r.manager.sendResponse(response, interest, pitToken, inFace)
}
func (r *RIBModule) announce(interest *ndn.Interest, pitToken []byte, inFace uint64) {
var response *mgmt.ControlResponse
if interest.Name().Size() != r.manager.prefixLength()+3 || interest.Name().At(r.manager.prefixLength()+2).Type() != tlv.ParametersSha256DigestComponent {
// Name not long enough to contain ControlParameters
core.LogWarn(r, "Name of Interest=", interest.Name(), " is either too short or incorrectly formatted to be rib/announce")
response = mgmt.MakeControlResponse(400, "Name is incorrect", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
// Get PrefixAnnouncement
if len(interest.ApplicationParameters()) == 0 || interest.ApplicationParameters()[0].Type() != tlv.Data {
core.LogWarn(r, "PrefixAnnouncement Interest=", interest.Name(), " missing PrefixAnnouncement")
response = mgmt.MakeControlResponse(400, "PrefixAnnouncement is missing", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
prefixAnnouncement, err := ndn.DecodePrefixAnnouncement(interest.ApplicationParameters()[0].DeepCopy())
if err != nil {
core.LogWarn(r, "PrefixAnnouncement Interest=", interest.Name(), " has invalid PrefixAnnouncement")
response = mgmt.MakeControlResponse(400, "PrefixAnnouncement is invalid", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
}
prefix := prefixAnnouncement.Prefix()
faceID := inFace
origin := table.RouteOriginPrefixAnn
cost := uint64(0)
expirationPeriod := time.Duration(prefixAnnouncement.ExpirationPeriod()) * time.Millisecond
// Use more restrictive of ExpirationPeriod and ValidityPeriod
notBefore, notAfter := prefixAnnouncement.ValidityPeriod()
if notBefore.Unix() == 0 && notAfter.Unix() == 0 {
} else if notBefore.After(time.Now()) && notAfter.Before(time.Now()) {
core.LogWarn(r, "PrefixAnnouncement Interest=", interest.Name(), " is in the future")
response = mgmt.MakeControlResponse(416, "Time out of range", nil)
r.manager.sendResponse(response, interest, pitToken, inFace)
return
} else if notAfter.Before(time.Now().Add(expirationPeriod)) {
expirationPeriod = time.Until(notAfter)
}
table.Rib.AddRoute(prefix, faceID, origin, cost, 0, &expirationPeriod)
core.LogInfo(r, "Created route via PrefixAnnouncement for Prefix=", prefix, ", FaceID=", faceID, ", Origin=", origin, ", Cost=", cost, ", Flags=0x0, ExpirationPeriod=", expirationPeriod)
responseParams := mgmt.MakeControlParameters()
responseParams.Name = prefix
responseParams.FaceID = new(uint64)
*responseParams.FaceID = faceID
responseParams.Origin = new(uint64)
*responseParams.Origin = origin
responseParams.Cost = new(uint64)
*responseParams.Cost = cost
responseParams.Flags = new(uint64)
*responseParams.Flags = 0
responseParams.ExpirationPeriod = new(uint64)
*responseParams.ExpirationPeriod = uint64(expirationPeriod.Milliseconds())
responseParamsWire, err := responseParams.Encode()
if err != nil {
core.LogError(r, "Unable to encode response parameters: ", err)
response = mgmt.MakeControlResponse(500, "Internal error", nil)
} else {
response = mgmt.MakeControlResponse(200, "OK", responseParamsWire)
}
r.manager.sendResponse(response, interest, pitToken, inFace)
}
func (r *RIBModule) list(interest *ndn.Interest, pitToken []byte, inFace uint64) {
if interest.Name().Size() > r.manager.prefixLength()+2 {
// Ignore because contains version and/or segment components
return
}
// Generate new dataset
entries := table.Rib.GetAllEntries()
dataset := make([]byte, 0)
for _, entry := range entries {
ribEntry := mgmt.MakeRibEntry(entry.Name)
for _, route := range entry.GetRoutes() {
var res mgmt.Route
res.FaceID = route.FaceID
res.Origin = route.Origin
res.Cost = route.Cost
res.Flags = route.Flags
if route.ExpirationPeriod != nil {
res.ExpirationPeriod = route.ExpirationPeriod
}
ribEntry.Routes = append(ribEntry.Routes, res)
}
wire, err := ribEntry.Encode()
if err != nil {
core.LogError(r, "Cannot encode RibEntry for Name=", entry.Name, ": ", err)
continue
}
encoded, err := wire.Wire()
if err != nil {
core.LogError(r, "Cannot encode RibEntry for Name=", entry.Name, ": ", err)
continue
}
dataset = append(dataset, encoded...)
}
name, _ := ndn.NameFromString(interest.Name().Prefix(r.manager.prefixLength()).String() + "/rib/list")
segments := mgmt.MakeStatusDataset(name, r.nextRIBDatasetVersion, dataset)
for _, segment := range segments {
encoded, err := segment.Encode()
if err != nil {
core.LogError(r, "Unable to encode RIB dataset: ", err)
return
}
r.manager.transport.Send(encoded, pitToken, nil)
}
core.LogTrace(r, "Published RIB dataset version=", r.nextRIBDatasetVersion, ", containing ", len(segments), " segments")
r.nextRIBDatasetVersion++
}