-
Notifications
You must be signed in to change notification settings - Fork 11
/
example_update_decoder_test.go
158 lines (150 loc) · 4.37 KB
/
example_update_decoder_test.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
package corebgp_test
import (
"fmt"
"net/netip"
"github.com/jwhited/corebgp"
)
type updateMessage struct {
withdrawn []netip.Prefix
origin uint8
asPath []uint32
nextHop netip.Addr
communities []uint32
nlri []netip.Prefix
ipv6NextHops []netip.Addr
ipv6NLRI []netip.Prefix
ipv6Withdrawn []netip.Prefix
}
func newPathAttrsDecodeFn() func(m *updateMessage, code uint8, flags corebgp.PathAttrFlags, b []byte) error {
reachDecodeFn := corebgp.NewMPReachNLRIDecodeFn[*updateMessage](
func(m *updateMessage, afi uint16, safi uint8, nh, nlri []byte) error {
if afi == corebgp.AFI_IPV6 && safi == corebgp.SAFI_UNICAST {
nhs, err := corebgp.DecodeMPReachIPv6NextHops(nh)
if err != nil {
return err
}
prefixes, err := corebgp.DecodeMPIPv6Prefixes(nlri)
if err != nil {
return err
}
m.ipv6NextHops = nhs
m.ipv6NLRI = prefixes
}
return nil
},
)
unreachDecodeFn := corebgp.NewMPUnreachNLRIDecodeFn[*updateMessage](
func(m *updateMessage, afi uint16, safi uint8, withdrawn []byte) error {
if afi == corebgp.AFI_IPV6 && safi == corebgp.SAFI_UNICAST {
prefixes, err := corebgp.DecodeMPIPv6Prefixes(withdrawn)
if err != nil {
return err
}
m.ipv6Withdrawn = prefixes
}
return nil
},
)
return func(m *updateMessage, code uint8, flags corebgp.PathAttrFlags, b []byte) error {
switch code {
case corebgp.PATH_ATTR_ORIGIN:
var o corebgp.OriginPathAttr
err := o.Decode(flags, b)
if err != nil {
return err
}
m.origin = uint8(o)
return nil
case corebgp.PATH_ATTR_AS_PATH:
var a corebgp.ASPathAttr
err := a.Decode(flags, b)
if err != nil {
return err
}
m.asPath = a.ASSequence
return nil
case corebgp.PATH_ATTR_NEXT_HOP:
var nh corebgp.NextHopPathAttr
err := nh.Decode(flags, b)
if err != nil {
return err
}
m.nextHop = netip.Addr(nh)
return nil
case corebgp.PATH_ATTR_COMMUNITY:
var comms corebgp.CommunitiesPathAttr
err := comms.Decode(flags, b)
if err != nil {
return err
}
m.communities = comms
case corebgp.PATH_ATTR_MP_REACH_NLRI:
return reachDecodeFn(m, flags, b)
case corebgp.PATH_ATTR_MP_UNREACH_NLRI:
return unreachDecodeFn(m, flags, b)
}
return nil
}
}
// ExampleUpdateDecoder demonstrates an UpdateDecoder that decodes UPDATE
// messages containing IPv4 and IPv6 routes.
func ExampleUpdateDecoder() {
ud := corebgp.NewUpdateDecoder[*updateMessage](
corebgp.NewWithdrawnRoutesDecodeFn[*updateMessage](func(u *updateMessage, withdrawn []netip.Prefix) error {
u.withdrawn = withdrawn
return nil
}),
newPathAttrsDecodeFn(),
corebgp.NewNLRIDecodeFn[*updateMessage](func(u *updateMessage, nlri []netip.Prefix) error {
u.nlri = nlri
return nil
}),
)
m := &updateMessage{}
fmt.Println("=== ipv4 ===")
fmt.Println(ud.Decode(m, []byte{
0x00, 0x03, // withdrawn routes length
0x10, 0x0a, 0x00, // withdrawn 10.0.0.0/16
0x00, 0x1b, // total path attr len
0x40, 0x01, 0x01, 0x01, // origin egp
0x40, 0x02, 0x06, 0x02, 0x01, 0x00, 0x00, 0xfd, 0xea, // as path 65002
0x40, 0x03, 0x04, 0xc0, 0x00, 0x02, 0x02, // next hop 192.0.2.2
0xc0, 0x08, 0x04, 0xfd, 0xea, 0xff, 0xff, // communities 65002:65535
0x18, 0xc0, 0x00, 0x02, // nlri 192.0.2.0/24
}))
fmt.Println(m.withdrawn)
fmt.Println(m.origin)
fmt.Println(m.asPath)
fmt.Println(m.nextHop)
fmt.Println(m.nlri)
fmt.Println(m.communities)
m = &updateMessage{}
fmt.Println("=== ipv6 ===")
fmt.Println(ud.Decode(m, []byte{
0x00, 0x00, // withdrawn routes length
0x00, 0x3f, // total path attr len
// extended len MP_REACH_NLRI 2001:db8::/64 nhs 2001:db8::2 & fe80::42:c0ff:fe00:202
0x90, 0x0e, 0x00, 0x2e, 0x00, 0x02, 0x01, 0x20, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0xc0, 0xff, 0xfe, 0x00, 0x02, 0x02, 0x00, 0x40, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
0x40, 0x01, 0x01, 0x01, // origin egp
0x40, 0x02, 0x06, 0x02, 0x01, 0x00, 0x00, 0xfd, 0xea, // as path 65002
}))
fmt.Println(m.origin)
fmt.Println(m.asPath)
fmt.Println(m.ipv6NextHops)
fmt.Println(m.ipv6NLRI)
// Output:
// === ipv4 ===
// <nil>
// [10.0.0.0/16]
// 1
// [65002]
// 192.0.2.2
// [192.0.2.0/24]
// [4260036607]
// === ipv6 ===
// <nil>
// 1
// [65002]
// [2001:db8::2 fe80::42:c0ff:fe00:202]
// [2001:db8::/64]
}