Newer
Older
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
package packet
import (
"bytes"
"fmt"
"github.com/taktv6/tflow2/convert"
)
type BGPUpdate struct {
WithdrawnRoutesLen uint16
WithdrawnRoutes *NLRI
TotalPathAttrLen uint16
PathAttributes *PathAttribute
NLRI *NLRI
}
func (b *BGPUpdate) SerializeUpdate(opt *Options) ([]byte, error) {
budget := MaxLen - MinLen
nlriLen := 0
buf := bytes.NewBuffer(nil)
withdrawBuf := bytes.NewBuffer(nil)
for withdraw := b.WithdrawnRoutes; withdraw != nil; withdraw = withdraw.Next {
if opt.AddPathRX {
nlriLen = int(withdraw.serializeAddPath(withdrawBuf))
} else {
nlriLen = int(withdraw.serialize(withdrawBuf))
}
budget -= nlriLen
if budget < 0 {
return nil, fmt.Errorf("update too long")
}
}
pathAttributesBuf := bytes.NewBuffer(nil)
for pa := b.PathAttributes; pa != nil; pa = pa.Next {
paLen := int(pa.serialize(pathAttributesBuf, opt))
budget -= paLen
if budget < 0 {
return nil, fmt.Errorf("update too long")
}
}
nlriBuf := bytes.NewBuffer(nil)
for nlri := b.NLRI; nlri != nil; nlri = nlri.Next {
if opt.AddPathRX {
nlriLen = int(nlri.serializeAddPath(withdrawBuf))
} else {
nlriLen = int(nlri.serialize(withdrawBuf))
}
budget -= nlriLen
if budget < 0 {
return nil, fmt.Errorf("update too long")
}
}
withdrawnRoutesLen := withdrawBuf.Len()
if withdrawnRoutesLen > 65535 {
return nil, fmt.Errorf("Invalid Withdrawn Routes Length: %d", withdrawnRoutesLen)
}
totalPathAttributesLen := pathAttributesBuf.Len()
if totalPathAttributesLen > 65535 {
return nil, fmt.Errorf("Invalid Total Path Attribute Length: %d", totalPathAttributesLen)
}
totalLength := 2 + withdrawnRoutesLen + totalPathAttributesLen + 2 + nlriBuf.Len() + 19
if totalLength > 4096 {
return nil, fmt.Errorf("Update too long: %d bytes", totalLength)
}
serializeHeader(buf, uint16(totalLength), UpdateMsg)
buf.Write(convert.Uint16Byte(uint16(withdrawnRoutesLen)))
buf.Write(withdrawBuf.Bytes())
buf.Write(convert.Uint16Byte(uint16(totalPathAttributesLen)))
buf.Write(pathAttributesBuf.Bytes())
buf.Write(nlriBuf.Bytes())
return buf.Bytes(), nil
}
func (b *BGPUpdate) SerializeUpdateAddPath(opt *Options) ([]byte, error) {
budget := MaxLen - MinLen
buf := bytes.NewBuffer(nil)
withdrawBuf := bytes.NewBuffer(nil)
for withdraw := b.WithdrawnRoutes; withdraw != nil; withdraw = withdraw.Next {
nlriLen := int(withdraw.serialize(withdrawBuf))
budget -= nlriLen
if budget < 0 {
return nil, fmt.Errorf("update too long")
}
}
pathAttributesBuf := bytes.NewBuffer(nil)
for pa := b.PathAttributes; pa != nil; pa = pa.Next {
paLen := int(pa.serialize(pathAttributesBuf, opt))
budget -= paLen
if budget < 0 {
return nil, fmt.Errorf("update too long")
}
}
nlriBuf := bytes.NewBuffer(nil)
for nlri := b.NLRI; nlri != nil; nlri = nlri.Next {
nlriLen := int(nlri.serialize(nlriBuf))
budget -= nlriLen
if budget < 0 {
return nil, fmt.Errorf("update too long")
}
}
withdrawnRoutesLen := withdrawBuf.Len()
if withdrawnRoutesLen > 65535 {
return nil, fmt.Errorf("Invalid Withdrawn Routes Length: %d", withdrawnRoutesLen)
}
totalPathAttributesLen := pathAttributesBuf.Len()
if totalPathAttributesLen > 65535 {
return nil, fmt.Errorf("Invalid Total Path Attribute Length: %d", totalPathAttributesLen)
}
totalLength := 2 + withdrawnRoutesLen + totalPathAttributesLen + 2 + nlriBuf.Len() + 19
if totalLength > 4096 {
return nil, fmt.Errorf("Update too long: %d bytes", totalLength)
}
serializeHeader(buf, uint16(totalLength), UpdateMsg)
buf.Write(convert.Uint16Byte(uint16(withdrawnRoutesLen)))
buf.Write(withdrawBuf.Bytes())
buf.Write(convert.Uint16Byte(uint16(totalPathAttributesLen)))
buf.Write(pathAttributesBuf.Bytes())
buf.Write(nlriBuf.Bytes())
return buf.Bytes(), nil
}