forked from scionproto/scion
/
types.go
396 lines (339 loc) · 8.92 KB
/
types.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
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
// Copyright 2017 ETH Zurich
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sciond
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/hostinfo"
"github.com/scionproto/scion/go/lib/util"
"github.com/scionproto/scion/go/proto"
)
type PathErrorCode uint16
const (
ErrorOk PathErrorCode = iota
ErrorNoPaths
ErrorPSTimeout
ErrorInternal
ErrorBadSrcIA
ErrorBadDstIA
)
func (c PathErrorCode) String() string {
switch c {
case ErrorOk:
return "OK"
case ErrorNoPaths:
return "No paths available"
case ErrorPSTimeout:
return "SCIOND timed out while requesting paths"
case ErrorInternal:
return "SCIOND experienced an internal error"
case ErrorBadSrcIA:
return "Bad source ISD/AS"
case ErrorBadDstIA:
return "Bad destination ISD/AS"
default:
return fmt.Sprintf("Unknown error (%v)", uint16(c))
}
}
var _ proto.Cerealizable = (*Pld)(nil)
type Pld struct {
Id uint64
Which proto.SCIONDMsg_Which
PathReq *PathReq
PathReply *PathReply
AsInfoReq *ASInfoReq
AsInfoReply *ASInfoReply
RevNotification *RevNotification
RevReply *RevReply
IfInfoRequest *IFInfoRequest
IfInfoReply *IFInfoReply
ServiceInfoRequest *ServiceInfoRequest
ServiceInfoReply *ServiceInfoReply
}
func NewPldFromRaw(b common.RawBytes) (*Pld, error) {
p := &Pld{}
return p, proto.ParseFromRaw(p, b)
}
func (p *Pld) ProtoId() proto.ProtoIdType {
return proto.SCIONDMsg_TypeID
}
func (p *Pld) String() string {
desc := []string{fmt.Sprintf("Sciond: Id: %d Union: ", p.Id)}
u1, err := p.union()
if err != nil {
desc = append(desc, err.Error())
} else {
desc = append(desc, fmt.Sprintf("%+v", u1))
}
return strings.Join(desc, "")
}
func (p *Pld) union() (interface{}, error) {
switch p.Which {
case proto.SCIONDMsg_Which_pathReq:
return p.PathReq, nil
case proto.SCIONDMsg_Which_pathReply:
return p.PathReply, nil
case proto.SCIONDMsg_Which_asInfoReq:
return p.AsInfoReq, nil
case proto.SCIONDMsg_Which_asInfoReply:
return p.AsInfoReply, nil
case proto.SCIONDMsg_Which_revNotification:
return p.RevNotification, nil
case proto.SCIONDMsg_Which_revReply:
return p.RevReply, nil
case proto.SCIONDMsg_Which_ifInfoRequest:
return p.IfInfoRequest, nil
case proto.SCIONDMsg_Which_ifInfoReply:
return p.IfInfoReply, nil
case proto.SCIONDMsg_Which_serviceInfoRequest:
return p.ServiceInfoRequest, nil
case proto.SCIONDMsg_Which_serviceInfoReply:
return p.ServiceInfoReply, nil
}
return nil, common.NewBasicError("Unsupported SCIOND union type", nil, "type", p.Which)
}
type PathReq struct {
Dst addr.IAInt
Src addr.IAInt
MaxPaths uint16
HPCfgs []*path_mgmt.HPGroupId `capnp:"hpCfgs"`
Flags PathReqFlags
}
func (pathReq *PathReq) Copy() *PathReq {
return &PathReq{
Dst: pathReq.Dst,
Src: pathReq.Src,
MaxPaths: pathReq.MaxPaths,
Flags: pathReq.Flags,
}
}
func (pathReq *PathReq) String() string {
return fmt.Sprintf("%v -> %v, maxPaths=%d, flags=%v",
pathReq.Src, pathReq.Dst, pathReq.MaxPaths, pathReq.Flags)
}
type PathReqFlags struct {
Refresh bool
Hidden bool
}
type PathReply struct {
ErrorCode PathErrorCode
Entries []PathReplyEntry
}
func (r *PathReply) String() string {
strEntries := make([]string, len(r.Entries))
for i := range r.Entries {
strEntries[i] = r.Entries[i].String()
}
return fmt.Sprintf("ErrorCode=%v\n %v", r.ErrorCode, strings.Join(strEntries, "\n "))
}
type PathReplyEntry struct {
Path *FwdPathMeta
HostInfo hostinfo.Host
}
func (e *PathReplyEntry) Copy() *PathReplyEntry {
if e == nil {
return nil
}
return &PathReplyEntry{
Path: e.Path.Copy(),
HostInfo: *e.HostInfo.Copy(),
}
}
func (e *PathReplyEntry) String() string {
return fmt.Sprintf("%v NextHop=%v", e.Path, &e.HostInfo)
}
type FwdPathMeta struct {
FwdPath []byte
Mtu uint16
Interfaces []PathInterface
ExpTime uint32
}
func (fpm *FwdPathMeta) SrcIA() addr.IA {
ifaces := fpm.Interfaces
if len(ifaces) == 0 {
return addr.IA{}
}
return ifaces[0].IA()
}
func (fpm *FwdPathMeta) DstIA() addr.IA {
ifaces := fpm.Interfaces
if len(ifaces) == 0 {
return addr.IA{}
}
return ifaces[len(ifaces)-1].IA()
}
func (fpm *FwdPathMeta) Expiry() time.Time {
return util.SecsToTime(fpm.ExpTime)
}
func (fpm *FwdPathMeta) Copy() *FwdPathMeta {
if fpm == nil {
return nil
}
res := &FwdPathMeta{Mtu: fpm.Mtu, ExpTime: fpm.ExpTime}
res.FwdPath = common.CloneByteSlice(fpm.FwdPath)
if fpm.Interfaces != nil {
res.Interfaces = make([]PathInterface, len(fpm.Interfaces))
copy(res.Interfaces, fpm.Interfaces)
}
return res
}
func (fpm *FwdPathMeta) String() string {
hops := fpm.fmtIfaces()
return fmt.Sprintf("Hops: [%s] Mtu: %d", strings.Join(hops, ">"), fpm.Mtu)
}
func (fpm *FwdPathMeta) fmtIfaces() []string {
var hops []string
if len(fpm.Interfaces) == 0 {
return hops
}
intf := fpm.Interfaces[0]
hops = append(hops, fmt.Sprintf("%s %d", intf.IA(), intf.IfID))
for i := 1; i < len(fpm.Interfaces)-1; i += 2 {
inIntf := fpm.Interfaces[i]
outIntf := fpm.Interfaces[i+1]
hops = append(hops, fmt.Sprintf("%d %s %d", inIntf.IfID, inIntf.IA(), outIntf.IfID))
}
intf = fpm.Interfaces[len(fpm.Interfaces)-1]
hops = append(hops, fmt.Sprintf("%d %s", intf.IfID, intf.IA()))
return hops
}
type PathInterface struct {
RawIsdas addr.IAInt `capnp:"isdas"`
IfID common.IFIDType
}
func NewPathInterface(str string) (PathInterface, error) {
tokens := strings.Split(str, "#")
if len(tokens) != 2 {
return PathInterface{},
common.NewBasicError("Failed to parse interface spec", nil, "value", str)
}
var iface PathInterface
ia, err := addr.IAFromString(tokens[0])
if err != nil {
return PathInterface{}, err
}
iface.RawIsdas = ia.IAInt()
ifid, err := strconv.ParseUint(tokens[1], 10, 64)
if err != nil {
return PathInterface{}, err
}
iface.IfID = common.IFIDType(ifid)
return iface, nil
}
func (iface PathInterface) IA() addr.IA {
return iface.RawIsdas.IA()
}
func (iface PathInterface) IfId() common.IFIDType {
return iface.IfID
}
func (iface *PathInterface) Equal(other *PathInterface) bool {
if iface == nil || other == nil {
return iface == other
}
return iface.RawIsdas == other.RawIsdas && iface.IfID == other.IfID
}
func (iface PathInterface) String() string {
return fmt.Sprintf("%s#%d", iface.IA(), iface.IfID)
}
type ASInfoReq struct {
Isdas addr.IAInt
}
func (r ASInfoReq) String() string {
return r.Isdas.String()
}
type ASInfoReply struct {
Entries []ASInfoReplyEntry
}
type ASInfoReplyEntry struct {
RawIsdas addr.IAInt `capnp:"isdas"`
Mtu uint16
IsCore bool
}
func (entry *ASInfoReplyEntry) ISD_AS() addr.IA {
return entry.RawIsdas.IA()
}
func (entry ASInfoReplyEntry) String() string {
return fmt.Sprintf("ia:%v, mtu:%v, core:%t", entry.ISD_AS(), entry.Mtu, entry.IsCore)
}
type RevNotification struct {
SRevInfo *path_mgmt.SignedRevInfo
}
func (rN *RevNotification) String() string {
return fmt.Sprintf("SRevInfo: %s", rN.SRevInfo)
}
type RevReply struct {
Result RevResult
}
type RevResult uint16
const (
RevValid RevResult = iota
RevStale
RevInvalid
RevUnknown
)
func (c RevResult) String() string {
switch c {
case RevValid:
return "RevValid"
case RevStale:
return "RevStale"
case RevInvalid:
return "RevInvalid"
case RevUnknown:
return "RevUnknown"
default:
return fmt.Sprintf("Unknown revocation result (%d)", c)
}
}
type IFInfoRequest struct {
IfIDs []common.IFIDType
}
func (r IFInfoRequest) String() string {
return fmt.Sprintf("%v", r.IfIDs)
}
type IFInfoReply struct {
RawEntries []IFInfoReplyEntry `capnp:"entries"`
}
// Entries maps IFIDs to their addresses and ports; the map is rebuilt each time.
func (reply *IFInfoReply) Entries() map[common.IFIDType]hostinfo.Host {
m := make(map[common.IFIDType]hostinfo.Host)
for _, entry := range reply.RawEntries {
m[entry.IfID] = entry.HostInfo
}
return m
}
type IFInfoReplyEntry struct {
IfID common.IFIDType
HostInfo hostinfo.Host
}
type ServiceInfoRequest struct {
ServiceTypes []proto.ServiceType
}
func (r ServiceInfoRequest) String() string {
return fmt.Sprintf("%v", r.ServiceTypes)
}
type ServiceInfoReply struct {
Entries []ServiceInfoReplyEntry
}
type ServiceInfoReplyEntry struct {
ServiceType proto.ServiceType
Ttl uint32
HostInfos []hostinfo.Host
}