Skip to content
Snippets Groups Projects
Commit 6e7b27cf authored by Christoph Petrausch's avatar Christoph Petrausch
Browse files

Removed duplicate code and fix bug in update_sender. UpdateSenderAddPath send...

Removed duplicate code and fix bug in update_sender. UpdateSenderAddPath send nonAddpath Update. UpdateSender send BGPUpdateAddpath
parent ca0496d9
No related branches found
No related tags found
No related merge requests found
...@@ -2,10 +2,12 @@ package server ...@@ -2,10 +2,12 @@ package server
import ( import (
"fmt" "fmt"
"io"
"strings" "strings"
"github.com/bio-routing/bio-rd/protocols/bgp/packet" "github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/route" "github.com/bio-routing/bio-rd/route"
log "github.com/sirupsen/logrus"
) )
func pathAttribues(p *route.Path, fsm *FSM) (*packet.PathAttribute, error) { func pathAttribues(p *route.Path, fsm *FSM) (*packet.PathAttribute, error) {
...@@ -68,3 +70,21 @@ func addOptionalPathAttribues(p *route.Path, parent *packet.PathAttribute) error ...@@ -68,3 +70,21 @@ func addOptionalPathAttribues(p *route.Path, parent *packet.PathAttribute) error
return nil return nil
} }
type serializeAbleUpdate interface {
SerializeUpdate() ([]byte, error)
}
func serializeAndSendUpdate(out io.Writer, update serializeAbleUpdate) error {
updateBytes, err := update.SerializeUpdate()
if err != nil {
log.Errorf("Unable to serialize BGP Update: %v", err)
return nil
}
_, err = out.Write(updateBytes)
if err != nil {
return fmt.Errorf("Failed sending Update: %v", err)
}
return nil
}
package server
import (
"testing"
"bytes"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/net"
"github.com/stretchr/testify/assert"
)
func TestSerializeAndSendUpdate(t *testing.T) {
tests := []struct {
name string
err error
testUpdate serializeAbleUpdate
expected []byte
}{
{
name: "normal bgp update",
err: nil,
testUpdate: &packet.BGPUpdate{
WithdrawnRoutesLen: 5,
WithdrawnRoutes: &packet.NLRI{
IP: strAddr("10.0.0.0"),
Pfxlen: 8,
Next: &packet.NLRI{
IP: strAddr("192.168.0.0"),
Pfxlen: 16,
},
},
},
expected: []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // Marker
0, 28, // Length
2, // Type = Update
0, 5, 8, 10, 16, 192, 168, 0, 0, // 2 withdraws
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
err := serializeAndSendUpdate(buf, test.testUpdate)
assert.Equal(t, test.err, err)
assert.Equal(t, test.expected, buf.Bytes())
})
}
}
func strAddr(s string) uint32 {
ret, _ := net.StrToAddr(s)
return ret
}
...@@ -34,26 +34,15 @@ func (u *UpdateSender) AddPath(pfx net.Prefix, p *route.Path) error { ...@@ -34,26 +34,15 @@ func (u *UpdateSender) AddPath(pfx net.Prefix, p *route.Path) error {
return nil return nil
} }
update := &packet.BGPUpdateAddPath{ update := &packet.BGPUpdate{
PathAttributes: pathAttrs, PathAttributes: pathAttrs,
NLRI: &packet.NLRIAddPath{ NLRI: &packet.NLRI{
PathIdentifier: p.BGPPath.PathIdentifier, IP: pfx.Addr(),
IP: pfx.Addr(), Pfxlen: pfx.Pfxlen(),
Pfxlen: pfx.Pfxlen(),
}, },
} }
updateBytes, err := update.SerializeUpdate() return serializeAndSendUpdate(u.fsm.con, update)
if err != nil {
log.Errorf("Unable to serialize BGP Update: %v", err)
return nil
}
_, err = u.fsm.con.Write(updateBytes)
if err != nil {
return fmt.Errorf("Failed sending Update: %v", err)
}
return nil
} }
// RemovePath withdraws prefix `pfx` from a peer // RemovePath withdraws prefix `pfx` from a peer
......
package server package server
import ( import (
"fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/bio-routing/bio-rd/net" "github.com/bio-routing/bio-rd/net"
...@@ -32,26 +30,15 @@ func (u *UpdateSenderAddPath) AddPath(pfx net.Prefix, p *route.Path) error { ...@@ -32,26 +30,15 @@ func (u *UpdateSenderAddPath) AddPath(pfx net.Prefix, p *route.Path) error {
log.Errorf("Unable to create BGP Update: %v", err) log.Errorf("Unable to create BGP Update: %v", err)
return nil return nil
} }
update := &packet.BGPUpdateAddPath{
update := &packet.BGPUpdate{
PathAttributes: pathAttrs, PathAttributes: pathAttrs,
NLRI: &packet.NLRI{ NLRI: &packet.NLRIAddPath{
IP: pfx.Addr(), PathIdentifier: p.BGPPath.PathIdentifier,
Pfxlen: pfx.Pfxlen(), IP: pfx.Addr(),
Pfxlen: pfx.Pfxlen(),
}, },
} }
return serializeAndSendUpdate(u.fsm.con, update)
updateBytes, err := update.SerializeUpdate()
if err != nil {
log.Errorf("Unable to serialize BGP Update: %v", err)
return nil
}
_, err = u.fsm.con.Write(updateBytes)
if err != nil {
return fmt.Errorf("Failed sending Update: %v", err)
}
return nil
} }
// RemovePath withdraws prefix `pfx` from a peer // RemovePath withdraws prefix `pfx` from a peer
......
...@@ -35,12 +35,8 @@ func withDrawPrefixes(out io.Writer, prefixes ...net.Prefix) error { ...@@ -35,12 +35,8 @@ func withDrawPrefixes(out io.Writer, prefixes ...net.Prefix) error {
update := &packet.BGPUpdate{ update := &packet.BGPUpdate{
WithdrawnRoutes: rootNLRI, WithdrawnRoutes: rootNLRI,
} }
data, err := update.SerializeUpdate() return serializeAndSendUpdate(out, update)
if err != nil {
return err
}
_, err = out.Write(data)
return err
} }
// withDrawPrefixesAddPath generates a BGPUpdateAddPath message and write it to the given // withDrawPrefixesAddPath generates a BGPUpdateAddPath message and write it to the given
...@@ -59,10 +55,5 @@ func withDrawPrefixesAddPath(out io.Writer, pfx net.Prefix, p *route.Path) error ...@@ -59,10 +55,5 @@ func withDrawPrefixesAddPath(out io.Writer, pfx net.Prefix, p *route.Path) error
Pfxlen: pfx.Pfxlen(), Pfxlen: pfx.Pfxlen(),
}, },
} }
data, err := update.SerializeUpdate() return serializeAndSendUpdate(out, update)
if err != nil {
return err
}
_, err = out.Write(data)
return err
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment