Newer
Older
)
func decodePathAttrs(buf *bytes.Buffer, tpal uint16) (*PathAttribute, error) {
var ret *PathAttribute
var eol *PathAttribute
var pa *PathAttribute
var err error
var consumed uint16
p := uint16(0)
for p < tpal {
pa, consumed, err = decodePathAttr(buf)
if err != nil {
return nil, fmt.Errorf("Unable to decode path attr: %v", err)
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
if ret == nil {
ret = pa
eol = pa
} else {
eol.Next = pa
eol = pa
}
}
return ret, nil
}
func decodePathAttr(buf *bytes.Buffer) (pa *PathAttribute, consumed uint16, err error) {
pa = &PathAttribute{}
err = decodePathAttrFlags(buf, pa)
if err != nil {
return nil, consumed, fmt.Errorf("Unable to get path attribute flags: %v", err)
}
consumed++
err = decode(buf, []interface{}{&pa.TypeCode})
if err != nil {
return nil, consumed, err
}
consumed++
n, err := pa.setLength(buf)
if err != nil {
return nil, consumed, err
}
consumed += uint16(n)
switch pa.TypeCode {
case OriginAttr:
if err := pa.decodeOrigin(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to decode Origin: %v", err)
}
case ASPathAttr:
if err := pa.decodeASPath(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to decode AS Path: %v", err)
}
case NextHopAttr:
if err := pa.decodeNextHop(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to decode Next-Hop: %v", err)
}
case MEDAttr:
if err := pa.decodeMED(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to decode MED: %v", err)
}
case LocalPrefAttr:
if err := pa.decodeLocalPref(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to decode local pref: %v", err)
}
case AggregatorAttr:
if err := pa.decodeAggregator(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to decode Aggregator: %v", err)
}
case AtomicAggrAttr:
// Nothing to do for 0 octet long attribute
case CommunitiesAttr:
if err := pa.decodeCommunities(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to decode Community: %v", err)
}
case AS4PathAttr:
if err := pa.decodeAS4Path(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to skip not supported AS4Path: %v", err)
}
case AS4AggregatorAttr:
if err := pa.decodeAS4Aggregator(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to skip not supported AS4Aggregator: %v", err)
}
if err := pa.decodeUnknown(buf); err != nil {
return nil, consumed, fmt.Errorf("Failed to decode unknown attribute: %v", err)
}
func (pa *PathAttribute) decodeUnknown(buf *bytes.Buffer) error {
u := make([]byte, pa.Length)
p := uint16(0)
err := decode(buf, []interface{}{&u})
if err != nil {
return fmt.Errorf("Unable to decode: %v", err)
}
pa.Value = u
p += pa.Length
return nil
}
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
func (pa *PathAttribute) decodeOrigin(buf *bytes.Buffer) error {
origin := uint8(0)
p := uint16(0)
err := decode(buf, []interface{}{&origin})
if err != nil {
return fmt.Errorf("Unable to decode: %v", err)
}
pa.Value = origin
p++
return dumpNBytes(buf, pa.Length-p)
}
func (pa *PathAttribute) decodeASPath(buf *bytes.Buffer) error {
pa.Value = make(ASPath, 0)
p := uint16(0)
for p < pa.Length {
segment := ASPathSegment{
ASNs: make([]uint32, 0),
}
err := decode(buf, []interface{}{&segment.Type, &segment.Count})
if err != nil {
return err
}
p += 2
if segment.Type != ASSet && segment.Type != ASSequence {
return fmt.Errorf("Invalid AS Path segment type: %d", segment.Type)
}
if segment.Count == 0 {
return fmt.Errorf("Invalid AS Path segment length: %d", segment.Count)
}
for i := uint8(0); i < segment.Count; i++ {
asn := uint16(0)
err := decode(buf, []interface{}{&asn})
if err != nil {
return err
}
p += 2
segment.ASNs = append(segment.ASNs, uint32(asn))
}
pa.Value = append(pa.Value.(ASPath), segment)
}
return nil
}
func (pa *PathAttribute) decodeNextHop(buf *bytes.Buffer) error {
addr := [4]byte{}
p := uint16(0)
n, err := buf.Read(addr[:])
if err != nil {
return err
}
if n != 4 {
return fmt.Errorf("Unable to read next hop: buf.Read read %d bytes", n)
}
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
p += 4
return dumpNBytes(buf, pa.Length-p)
}
func (pa *PathAttribute) decodeMED(buf *bytes.Buffer) error {
med, err := pa.decodeUint32(buf)
if err != nil {
return fmt.Errorf("Unable to recode local pref: %v", err)
}
pa.Value = uint32(med)
return nil
}
func (pa *PathAttribute) decodeLocalPref(buf *bytes.Buffer) error {
lpref, err := pa.decodeUint32(buf)
if err != nil {
return fmt.Errorf("Unable to recode local pref: %v", err)
}
pa.Value = uint32(lpref)
return nil
}
func (pa *PathAttribute) decodeAggregator(buf *bytes.Buffer) error {
aggr := Aggretator{}
p := uint16(0)
err := decode(buf, []interface{}{&aggr.ASN})
if err != nil {
return err
}
p += 2
addr := [4]byte{}
n, err := buf.Read(addr[:])
return fmt.Errorf("Unable to read next hop: buf.Read read %d bytes", n)
aggr.Addr = fourBytesToUint32(addr)
p += 4
func (pa *PathAttribute) decodeCommunities(buf *bytes.Buffer) error {
if pa.Length%4 != 0 {
return fmt.Errorf("Unable to read community path attribute length %d is not divisible by 4", pa.Length)
}
comNumber := pa.Length / 4
var com = make([]uint32, comNumber)
for i := uint16(0); i < comNumber; i++ {
c := [4]byte{}
n, err := buf.Read(c[:])
if err != nil {
return err
}
if n != 4 {
return fmt.Errorf("Unable to read next hop: buf.Read read %d bytes", n)
}
com[i] = fourBytesToUint32(c)
}
pa.Value = com
return nil
}
func (pa *PathAttribute) decodeAS4Path(buf *bytes.Buffer) error {
as4Path, err := pa.decodeUint32(buf)
if err != nil {
return fmt.Errorf("Unable to decode AS4Path: %v", err)
}
pa.Value = as4Path
return nil
}
func (pa *PathAttribute) decodeAS4Aggregator(buf *bytes.Buffer) error {
as4Aggregator, err := pa.decodeUint32(buf)
if err != nil {
return fmt.Errorf("Unable to decode AS4Aggregator: %v", err)
}
pa.Value = as4Aggregator
return nil
}
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
func (pa *PathAttribute) setLength(buf *bytes.Buffer) (int, error) {
bytesRead := 0
if pa.ExtendedLength {
err := decode(buf, []interface{}{&pa.Length})
if err != nil {
return 0, err
}
bytesRead = 2
} else {
x := uint8(0)
err := decode(buf, []interface{}{&x})
if err != nil {
return 0, err
}
pa.Length = uint16(x)
bytesRead = 1
}
return bytesRead, nil
}
func (pa *PathAttribute) decodeUint32(buf *bytes.Buffer) (uint32, error) {
var v uint32
p := uint16(0)
err := decode(buf, []interface{}{&v})
if err != nil {
return 0, err
}
p += 4
err = dumpNBytes(buf, pa.Length-p)
if err != nil {
return 0, fmt.Errorf("dumpNBytes failed: %v", err)
}
return v, nil
}
func (pa *PathAttribute) ASPathString() (ret string) {
if p.Type == ASSet {
ret += " ("
}
n := len(p.ASNs)
for i, asn := range p.ASNs {
if i < n-1 {
ret += fmt.Sprintf("%d ", asn)
continue
}
ret += fmt.Sprintf("%d", asn)
}
if p.Type == ASSet {
ret += ")"
}
}
return
}
func (pa *PathAttribute) ASPathLen() (ret uint16) {
if p.Type == ASSet {
ret++
continue
}
ret += uint16(len(p.ASNs))
}
return
}
// dumpNBytes is used to dump n bytes of buf. This is useful in case an path attributes
// length doesn't match a fixed length's attributes length (e.g. ORIGIN is always an octet)
func dumpNBytes(buf *bytes.Buffer, n uint16) error {
if n <= 0 {
return nil
}
dump := make([]byte, n)
err := decode(buf, []interface{}{&dump})
if err != nil {
return err
}
return nil
}
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
func (pa *PathAttribute) serialize(buf *bytes.Buffer) uint8 {
pathAttrLen := uint8(0)
switch pa.TypeCode {
case OriginAttr:
pathAttrLen = pa.serializeOrigin(buf)
case ASPathAttr:
pathAttrLen = pa.serializeASPath(buf)
case NextHopAttr:
pathAttrLen = pa.serializeNextHop(buf)
case MEDAttr:
pathAttrLen = pa.serializeMED(buf)
case LocalPrefAttr:
pathAttrLen = pa.serializeLocalpref(buf)
case AtomicAggrAttr:
pathAttrLen = pa.serializeAtomicAggregate(buf)
case AggregatorAttr:
pathAttrLen = pa.serializeAggregator(buf)
}
return pathAttrLen
}
func (pa *PathAttribute) serializeOrigin(buf *bytes.Buffer) uint8 {
attrFlags := uint8(0)
attrFlags = setTransitive(attrFlags)
buf.WriteByte(attrFlags)
buf.WriteByte(OriginAttr)
length := uint8(1)
buf.WriteByte(length)
buf.WriteByte(pa.Value.(uint8))
return 4
}
func (pa *PathAttribute) serializeASPath(buf *bytes.Buffer) uint8 {
attrFlags := uint8(0)
attrFlags = setTransitive(attrFlags)
buf.WriteByte(attrFlags)
buf.WriteByte(ASPathAttr)
length := uint8(0)
segmentsBuf := bytes.NewBuffer(nil)
for _, segment := range pa.Value.(ASPath) {
segmentsBuf.WriteByte(segment.Type)
segmentsBuf.WriteByte(uint8(len(segment.ASNs)))
}
length += 2 + uint8(len(segment.ASNs))*2
}
buf.WriteByte(length)
buf.Write(segmentsBuf.Bytes())
return length + 2
}
func (pa *PathAttribute) serializeNextHop(buf *bytes.Buffer) uint8 {
attrFlags := uint8(0)
attrFlags = setTransitive(attrFlags)
buf.WriteByte(attrFlags)
buf.WriteByte(NextHopAttr)
length := uint8(4)
buf.WriteByte(length)
addr := pa.Value.(uint32)
buf.Write(convert.Uint32Byte(addr))
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
return 7
}
func (pa *PathAttribute) serializeMED(buf *bytes.Buffer) uint8 {
attrFlags := uint8(0)
attrFlags = setOptional(attrFlags)
buf.WriteByte(attrFlags)
buf.WriteByte(MEDAttr)
length := uint8(4)
buf.WriteByte(length)
buf.Write(convert.Uint32Byte(pa.Value.(uint32)))
return 7
}
func (pa *PathAttribute) serializeLocalpref(buf *bytes.Buffer) uint8 {
attrFlags := uint8(0)
attrFlags = setTransitive(attrFlags)
buf.WriteByte(attrFlags)
buf.WriteByte(LocalPrefAttr)
length := uint8(4)
buf.WriteByte(length)
buf.Write(convert.Uint32Byte(pa.Value.(uint32)))
return 7
}
func (pa *PathAttribute) serializeAtomicAggregate(buf *bytes.Buffer) uint8 {
attrFlags := uint8(0)
attrFlags = setTransitive(attrFlags)
buf.WriteByte(attrFlags)
buf.WriteByte(AtomicAggrAttr)
length := uint8(0)
buf.WriteByte(length)
return 3
}
func (pa *PathAttribute) serializeAggregator(buf *bytes.Buffer) uint8 {
attrFlags := uint8(0)
attrFlags = setOptional(attrFlags)
attrFlags = setTransitive(attrFlags)
buf.WriteByte(attrFlags)
buf.WriteByte(AggregatorAttr)
length := uint8(2)
buf.WriteByte(length)
buf.Write(convert.Uint16Byte(pa.Value.(uint16)))
return 5
}
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
/*func (pa *PathAttribute) PrependASPath(prepend []uint32) {
if pa.TypeCode != ASPathAttr {
return
}
asPath := pa.Value.(ASPath)
asPathSegementCount := len(asPath)
currentSegment := asPathSegementCount - 1
newSegmentNeeded := false
if asPath[asPathSegementCount-1].Type == ASSequence {
newSegmentNeeded = true
} else {
if len(asPath[asPathSegementCount-1].ASNs) >= MaxASNsSegment {
newSegmentNeeded = true
}
}
for _, asn := range prepend {
if newSegmentNeeded {
segment := ASPathSegment{
Type: ASSequence,
ASNs: make([]uint32, 0),
},
}
asPath[currentSegment].ASNs = append(asPath[currentSegment].ASNs, asn)
if len(asPath[asPathSegementCount-1].ASNs) >= MaxASNsSegment {
newSegmentNeeded = true
}
}
}*/
// ParseASPathStr converts an AS path from string representation info an PathAttribute object
func ParseASPathStr(asPathString string) (*PathAttribute, error) {
asPath := ASPath{}
currentType := ASSequence
newSegmentNeeded := true
currentSegment := -1
for _, asn := range strings.Split(asPathString, " ") {
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
if isBeginOfASSet(asn) {
currentType = ASSet
newSegmentNeeded = true
asn = strings.Replace(asn, "(", "", 1)
}
if newSegmentNeeded {
seg := ASPathSegment{
Type: uint8(currentType),
ASNs: make([]uint32, 0),
}
asPath = append(asPath, seg)
currentSegment++
newSegmentNeeded = false
}
if isEndOfASSset(asn) {
currentType = ASSequence
newSegmentNeeded = true
asn = strings.Replace(asn, ")", "", 1)
}
numericASN, err := strconv.Atoi(asn)
if err != nil {
return nil, fmt.Errorf("Unable to convert ASN: %v", err)
}
asPath[currentSegment].ASNs = append(asPath[currentSegment].ASNs, uint32(numericASN))
if len(asPath[currentSegment].ASNs) == MaxASNsSegment {
newSegmentNeeded = true
}
}
return &PathAttribute{
TypeCode: ASPathAttr,
Value: asPath,
}, nil
}
func isBeginOfASSet(asPathPart string) bool {
return strings.Contains(asPathPart, "(")
}
func isEndOfASSset(asPathPart string) bool {
return strings.Contains(asPathPart, ")")
}
func fourBytesToUint32(address [4]byte) uint32 {
return uint32(address[0])<<24 + uint32(address[1])<<16 + uint32(address[2])<<8 + uint32(address[3])
}