Skip to content
Snippets Groups Projects
Unverified Commit 9be746a5 authored by Annika Wickert's avatar Annika Wickert Committed by GitHub
Browse files

Merge pull request #248 from bio-routing/fix/bmp32asns

Fix 32bit ASN handling in BMP
parents 68c3468b 6f42a8b1
No related branches found
No related tags found
No related merge requests found
......@@ -281,23 +281,23 @@ func (pa *PathAttribute) decodeASPath(buf *bytes.Buffer, asnLength uint8) error
func (pa *PathAttribute) decodeASN(buf *bytes.Buffer, asnSize uint8) (asn uint32, err error) {
if asnSize == 4 {
return pa.decode4ByteASN(buf)
return decode4ByteASN(buf)
}
return pa.decode2ByteASN(buf)
return decode2ByteASN(buf)
}
func (pa *PathAttribute) decode4ByteASN(buf *bytes.Buffer) (asn uint32, err error) {
func decode4ByteASN(buf *bytes.Buffer) (asn uint32, err error) {
asn4 := uint32(0)
err = decode.DecodeUint32(buf, &asn4)
if err != nil {
return 0, err
}
return uint32(asn4), nil
return asn4, nil
}
func (pa *PathAttribute) decode2ByteASN(buf *bytes.Buffer) (asn uint32, err error) {
func decode2ByteASN(buf *bytes.Buffer) (asn uint32, err error) {
asn2 := uint16(0)
err = decode.DecodeUint16(buf, &asn2)
if err != nil {
......
......@@ -2336,3 +2336,27 @@ func TestFourBytesToUint32(t *testing.T) {
}
}
}
func TestDecode4ByteASN(t *testing.T) {
tests := []struct {
name string
input *bytes.Buffer
expected uint32
}{
{
name: "Test #1",
input: bytes.NewBuffer([]byte{0b00000000, 0b00000011, 0b00010011, 0b11100101}),
expected: 201701,
},
}
for _, test := range tests {
res, err := decode4ByteASN(test.input)
if err != nil {
t.Errorf("error in test %q: %v", test.name, err)
continue
}
assert.Equal(t, test.expected, res, test.name)
}
}
......@@ -158,7 +158,9 @@ func (r *Router) processRouteMonitoringMsg(msg *bmppkt.RouteMonitoringMsg) {
}
s := n.fsm.state.(*establishedState)
s.msgReceived(msg.BGPUpdate, s.fsm.decodeOptions())
opt := s.fsm.decodeOptions()
opt.Use32BitASN = !msg.PerPeerHeader.GetAFlag()
s.msgReceived(msg.BGPUpdate, opt)
}
func (r *Router) processInitiationMsg(msg *bmppkt.InitiationMessage) {
......@@ -385,9 +387,6 @@ func (p *peer) configureBySentOpen(msg *packet.BGPOpen) {
MaxPaths: 10,
}
}
case packet.ASN4CapabilityCode:
asn4Cap := cap.Value.(packet.ASN4Capability)
p.localASN = asn4Cap.ASN4
}
}
}
......
......@@ -224,7 +224,7 @@ func TestBMPServer(t *testing.T) {
0, // Msg Type (route monitoring)
0, // Peer Type (global instance peer)
0, // Peer Flags
0b00100000, // Peer Flags
0, 0, 0, 0, 0, 0, 0, 123, // Peer Distinguisher
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 1, 1, 1, // Peer Address (10.1.1.1)
0, 0, 0, 200, // Peer AS = 200
......@@ -315,7 +315,7 @@ func TestBMPServer(t *testing.T) {
0, // Msg Type (route monitoring)
0, // Peer Type (global instance peer)
0, // Peer Flags
0b00100000, // Peer Flags
0, 0, 0, 0, 0, 0, 0, 123, // Peer Distinguisher
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 1, 2, 1, // Peer Address (10.1.2.1)
0, 0, 0, 222, // Peer AS = 222
......
......@@ -60,8 +60,14 @@ func decodePerPeerHeader(buf *bytes.Buffer) (*PerPeerHeader, error) {
// GetIPVersion gets the IP version of the BGP session
func (p *PerPeerHeader) GetIPVersion() uint8 {
if p.PeerFlags>>7 == 1 {
if p.PeerFlags&0b10000000 == 0b10000000 {
return 6
}
return 4
}
// GetAFlag checks if the A flag is set
func (p *PerPeerHeader) GetAFlag() bool {
return p.PeerFlags&0b00100000 == 0b00100000
}
......@@ -125,28 +125,28 @@ func TestGetIPVersion(t *testing.T) {
{
name: "IPv4",
p: &PerPeerHeader{
PeerFlags: 0,
PeerFlags: 0b00000000,
},
expected: 4,
},
{
name: "IPv4 #2",
p: &PerPeerHeader{
PeerFlags: 127,
PeerFlags: 0b01000000,
},
expected: 4,
},
{
name: "IPv6",
p: &PerPeerHeader{
PeerFlags: 128,
PeerFlags: 0b10000000,
},
expected: 6,
},
{
name: "IPv6 #2",
p: &PerPeerHeader{
PeerFlags: 129,
PeerFlags: 0b11000000,
},
expected: 6,
},
......@@ -154,6 +154,33 @@ func TestGetIPVersion(t *testing.T) {
for _, test := range tests {
v := test.p.GetIPVersion()
assert.Equal(t, test.expected, v)
assert.Equal(t, test.expected, v, test.name)
}
}
func TestGetAFlag(t *testing.T) {
tests := []struct {
name string
input *PerPeerHeader
expected bool
}{
{
name: "Test #1",
input: &PerPeerHeader{
PeerFlags: 0b11011111,
},
expected: false,
},
{
name: "Test #2",
input: &PerPeerHeader{
PeerFlags: 0b00100000,
},
expected: true,
},
}
for _, test := range tests {
assert.Equal(t, test.expected, test.input.GetAFlag())
}
}
......@@ -19,6 +19,7 @@ func Decode(buf *bytes.Buffer, fields []interface{}) error {
return nil
}
// DecodeUint8 decodes an uint8
func DecodeUint8(buf *bytes.Buffer, x *uint8) error {
y, err := buf.ReadByte()
if err != nil {
......@@ -29,6 +30,7 @@ func DecodeUint8(buf *bytes.Buffer, x *uint8) error {
return nil
}
// DecodeUint16 decodes an uint16
func DecodeUint16(buf *bytes.Buffer, x *uint16) error {
a, err := buf.ReadByte()
if err != nil {
......@@ -40,10 +42,11 @@ func DecodeUint16(buf *bytes.Buffer, x *uint16) error {
return err
}
*x = uint16(a)*256 + uint16(b)
*x = uint16(a)<<8 + uint16(b)
return nil
}
// DecodeUint32 decodes an uint32
func DecodeUint32(buf *bytes.Buffer, x *uint32) error {
a, err := buf.ReadByte()
if err != nil {
......@@ -65,6 +68,6 @@ func DecodeUint32(buf *bytes.Buffer, x *uint32) error {
return err
}
*x = uint32(a)*256*256*256 + uint32(b)*256*256*256 + uint32(c)*256 + uint32(d)
*x = uint32(a)<<24 + uint32(b)<<16 + uint32(c)<<8 + uint32(d)
return nil
}
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