Skip to content

Commit

Permalink
pkg/packet/mrt: restore compatibility
Browse files Browse the repository at this point in the history
restore the API breakage due to c801e18.

However, this is a hack. Should be removed with the major version updated.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
  • Loading branch information
fujita committed Jul 1, 2023
1 parent c801e18 commit d4d97da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
27 changes: 23 additions & 4 deletions pkg/packet/mrt/mrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ type RibEntry struct {

var errNotAllRibEntryBytesAvailable = errors.New("not all RibEntry bytes are available")

func (e *RibEntry) DecodeFromBytes(data []byte, prefix bgp.AddrPrefixInterface) ([]byte, error) {
func (e *RibEntry) DecodeFromBytes(data []byte, prefix ...bgp.AddrPrefixInterface) ([]byte, error) {
if len(data) < 8 {
return nil, errNotAllRibEntryBytesAvailable
}
Expand All @@ -384,7 +384,16 @@ func (e *RibEntry) DecodeFromBytes(data []byte, prefix bgp.AddrPrefixInterface)
if err != nil {
return nil, err
}
err = p.DecodeFromBytes(data, &bgp.MarshallingOption{ImplicitPrefix: prefix})

// HACK: keeps compatibility
switch len(prefix) {
case 0:
err = p.DecodeFromBytes(data)
case 1:
err = p.DecodeFromBytes(data, &bgp.MarshallingOption{ImplicitPrefix: prefix[0]})
default:
return nil, fmt.Errorf("only one prefix should be used")
}
if err != nil {
return nil, err
}
Expand All @@ -398,11 +407,21 @@ func (e *RibEntry) DecodeFromBytes(data []byte, prefix bgp.AddrPrefixInterface)
return data, nil
}

func (e *RibEntry) Serialize(prefix bgp.AddrPrefixInterface) ([]byte, error) {
func (e *RibEntry) Serialize(prefix ...bgp.AddrPrefixInterface) ([]byte, error) {
pbuf := make([]byte, 0)
totalLen := 0
for _, pattr := range e.PathAttributes {
pb, err := pattr.Serialize(&bgp.MarshallingOption{ImplicitPrefix: prefix})
var pb []byte
var err error
// HACK: keeps compatibility
switch len(prefix) {
case 0:
pb, err = pattr.Serialize()
case 1:
pb, err = pattr.Serialize(&bgp.MarshallingOption{ImplicitPrefix: prefix[0]})
default:
return nil, fmt.Errorf("only one prefix should be used")
}
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/packet/mrt/mrt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ func TestMrtRibEntry(t *testing.T) {
}

e1 := NewRibEntry(1, uint32(time.Now().Unix()), 0, p, false)
b1, err := e1.Serialize(nil)
b1, err := e1.Serialize()
if err != nil {
t.Fatal(err)
}

e2 := &RibEntry{}
rest, err := e2.DecodeFromBytes(b1, nil)
rest, err := e2.DecodeFromBytes(b1)
if err != nil {
t.Fatal(err)
}
Expand All @@ -145,13 +145,13 @@ func TestMrtRibEntryWithAddPath(t *testing.T) {
bgp.NewPathAttributeLocalPref(1 << 22),
}
e1 := NewRibEntry(1, uint32(time.Now().Unix()), 200, p, true)
b1, err := e1.Serialize(nil)
b1, err := e1.Serialize()
if err != nil {
t.Fatal(err)
}

e2 := &RibEntry{isAddPath: true}
rest, err := e2.DecodeFromBytes(b1, nil)
rest, err := e2.DecodeFromBytes(b1)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit d4d97da

Please sign in to comment.