-
Notifications
You must be signed in to change notification settings - Fork 44
/
mp_unreach_nlri.go
63 lines (50 loc) · 1.45 KB
/
mp_unreach_nlri.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
package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/bio-rd/util/decode"
"github.com/bio-routing/tflow2/convert"
)
// MultiProtocolUnreachNLRI represents network layer withdraw information for one prefix of an IP address family (rfc4760)
type MultiProtocolUnreachNLRI struct {
AFI uint16
SAFI uint8
NLRI *NLRI
}
func (n *MultiProtocolUnreachNLRI) serialize(buf *bytes.Buffer, opt *EncodeOptions) uint16 {
tempBuf := bytes.NewBuffer(nil)
tempBuf.Write(convert.Uint16Byte(n.AFI))
tempBuf.WriteByte(n.SAFI)
for cur := n.NLRI; cur != nil; cur = cur.Next {
cur.serialize(tempBuf, opt.UseAddPath)
}
buf.Write(tempBuf.Bytes())
return uint16(tempBuf.Len())
}
func deserializeMultiProtocolUnreachNLRI(b []byte, opt *DecodeOptions) (MultiProtocolUnreachNLRI, error) {
n := MultiProtocolUnreachNLRI{}
prefixesLength := len(b) - 3 // 3 <- AFI + SAFI
if prefixesLength < 0 {
return n, fmt.Errorf("Invalid length of MP_UNREACH_NLRI: expected more than 3 bytes but got %d", len(b))
}
nlris := make([]byte, prefixesLength)
fields := []interface{}{
&n.AFI,
&n.SAFI,
&nlris,
}
err := decode.Decode(bytes.NewBuffer(b), fields)
if err != nil {
return MultiProtocolUnreachNLRI{}, err
}
if len(nlris) == 0 {
return n, nil
}
buf := bytes.NewBuffer(nlris)
nlri, err := decodeNLRIs(buf, uint16(buf.Len()), n.AFI, opt.addPath(int(n.AFI), int(n.SAFI)))
if err != nil {
return MultiProtocolUnreachNLRI{}, err
}
n.NLRI = nlri
return n, nil
}