@@ -16,11 +16,8 @@ package bgpserver
16
16
17
17
import (
18
18
"context"
19
- "errors"
20
19
"fmt"
21
20
"log/slog"
22
- "strconv"
23
- "strings"
24
21
25
22
gobgpapi "github.com/osrg/gobgp/v3/api"
26
23
gobgpbgp "github.com/osrg/gobgp/v3/pkg/packet/bgp"
@@ -30,13 +27,13 @@ import (
30
27
31
28
type Route struct {
32
29
Prefix string
33
- Community string
30
+ Community uint32
34
31
}
35
32
36
33
type Connector interface {
37
34
Start (ctx context.Context ) error
38
35
AddPeer (neighbor string , remoteAS uint32 , remotePort uint32 , keepaliveIntervalSec uint64 ) error
39
- AddPath (prefix string , prefixLen uint32 , nexthop string , community string ) error
36
+ AddPath (prefix string , prefixLen uint32 , nexthop string , community uint32 ) error
40
37
ListPath () ([]Route , error )
41
38
}
42
39
@@ -132,7 +129,7 @@ func (bs *bgpServerConnector) AddPeer(neighbor string, remoteAS uint32, remotePo
132
129
)
133
130
}
134
131
135
- func (bs * bgpServerConnector ) AddPath (prefix string , prefixLen uint32 , nexthop string , community string ) error {
132
+ func (bs * bgpServerConnector ) AddPath (prefix string , prefixLen uint32 , nexthop string , community uint32 ) error {
136
133
nlri1 , err := apb .New (& gobgpapi.IPAddressPrefix {
137
134
Prefix : prefix ,
138
135
PrefixLen : prefixLen ,
@@ -149,12 +146,8 @@ func (bs *bgpServerConnector) AddPath(prefix string, prefixLen uint32, nexthop s
149
146
attrNextHop , _ := apb .New (& gobgpapi.NextHopAttribute {
150
147
NextHop : nexthop ,
151
148
})
152
- encodedCommunity , err := encodeCommunity (community )
153
- if err != nil {
154
- return err
155
- }
156
149
attrCommunities , _ := apb .New (& gobgpapi.CommunitiesAttribute {
157
- Communities : []uint32 {encodedCommunity },
150
+ Communities : []uint32 {community },
158
151
})
159
152
attrs = []* apb.Any {attrOrigin , attrNextHop , attrCommunities }
160
153
}
@@ -199,11 +192,10 @@ func (bs *bgpServerConnector) ListPath() ([]Route, error) {
199
192
}
200
193
201
194
for _ , comm := range ca .Communities {
202
- decodedCommunity := decodeCommunity (comm )
203
- bs .logger .Debug ("ListPath: found community" , "community" , decodedCommunity )
195
+ bs .logger .Debug ("ListPath: found community" , "community" , EncodeCommunity (comm ))
204
196
routes = append (routes , Route {
205
197
Prefix : d .Prefix ,
206
- Community : decodedCommunity ,
198
+ Community : comm ,
207
199
})
208
200
}
209
201
}
@@ -213,29 +205,8 @@ func (bs *bgpServerConnector) ListPath() ([]Route, error) {
213
205
return routes , err
214
206
}
215
207
216
- // encodeCommunity converts plain community value to human readable notation(for example 65001:10)
217
- func encodeCommunity (comm string ) (uint32 , error ) {
218
- nodes := strings .Split (comm , ":" )
219
- if len (nodes ) != 2 {
220
- return 0 , errors .New ("invaild community" )
221
- }
222
-
223
- upeer , err := strconv .Atoi (nodes [0 ])
224
- if err != nil {
225
- return 0 , err
226
- }
227
-
228
- lower , err := strconv .Atoi (nodes [1 ])
229
- if err != nil {
230
- return 0 , err
231
- }
232
- commEncoded := uint32 (upeer << 16 | lower )
233
-
234
- return commEncoded , nil
235
- }
236
-
237
- // decodeCommunity converts human readable notation to plain community value
238
- func decodeCommunity (comm uint32 ) string {
208
+ // EncodeCommunity converts plain community value to human readable notation(for example 65001:10)
209
+ func EncodeCommunity (comm uint32 ) string {
239
210
upper := comm >> 16
240
211
lower := comm & 0xffff
241
212
commDecoded := fmt .Sprintf ("%d:%d" , upper , lower )
0 commit comments