diff --git a/protocols/bgp/packet/decoder_test.go b/protocols/bgp/packet/decoder_test.go index 74e5b84f978f084c38360b85b72030fe79fe945e..3db3b4888742093b9d29e971e7a44e1fb2fca0c5 100644 --- a/protocols/bgp/packet/decoder_test.go +++ b/protocols/bgp/packet/decoder_test.go @@ -6,12 +6,10 @@ import ( "strconv" "testing" - "github.com/bio-routing/bio-rd/net" + bnet "github.com/bio-routing/bio-rd/net" "github.com/bio-routing/bio-rd/protocols/bgp/types" "github.com/stretchr/testify/assert" "github.com/taktv6/tflow2/convert" - - bnet "github.com/bio-routing/bio-rd/net" ) type test struct { @@ -211,10 +209,10 @@ func TestDecode(t *testing.T) { Body: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -431,10 +429,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -457,10 +455,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -499,10 +497,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -602,10 +600,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -677,10 +675,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -768,10 +766,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -872,10 +870,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -988,10 +986,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -1120,10 +1118,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -1199,7 +1197,7 @@ func TestDecodeUpdateMsg(t *testing.T) { TypeCode: 7, Value: types.Aggregator{ ASN: uint16(258), - Address: strAddr("10.11.12.13"), + Address: bnet.IPv4FromOctets(10, 11, 12, 13).ToUint32(), }, }, }, @@ -1210,7 +1208,7 @@ func TestDecodeUpdateMsg(t *testing.T) { }, NLRI: &NLRI{ Pfxlen: 8, - IP: strAddr("11.0.0.0"), + IP: bnet.IPv4FromOctets(11, 0, 0, 0), }, }, }, @@ -1292,10 +1290,10 @@ func TestDecodeUpdateMsg(t *testing.T) { expected: &BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -1820,8 +1818,3 @@ func TestDecodeAddPathCapability(t *testing.T) { assert.Equal(t, test.expected, cap) } } - -func strAddr(s string) uint32 { - ret, _ := net.StrToAddr(s) - return ret -} diff --git a/protocols/bgp/packet/nlri.go b/protocols/bgp/packet/nlri.go index c11b47bf8191a2f57b595cc6b6e9b78ff3d3fe6e..bc404cc347b2672c2da711d5dfae2d0174f6dc98 100644 --- a/protocols/bgp/packet/nlri.go +++ b/protocols/bgp/packet/nlri.go @@ -6,13 +6,14 @@ import ( "math" "net" + bnet "github.com/bio-routing/bio-rd/net" "github.com/bio-routing/bio-rd/util/decode" "github.com/taktv6/tflow2/convert" ) type NLRI struct { PathIdentifier uint32 - IP uint32 + IP bnet.IP Pfxlen uint8 Next *NLRI } @@ -46,7 +47,7 @@ func decodeNLRIs(buf *bytes.Buffer, length uint16) (*NLRI, error) { } func decodeNLRI(buf *bytes.Buffer) (*NLRI, uint8, error) { - var addr [4]byte + addr := make([]byte, 4) nlri := &NLRI{} err := decode.Decode(buf, []interface{}{&nlri.Pfxlen}) @@ -65,33 +66,28 @@ func decodeNLRI(buf *bytes.Buffer) (*NLRI, uint8, error) { addr[i] = 0 } } - nlri.IP = fourBytesToUint32(addr) + nlri.IP, err = bnet.IPFromBytes(addr) + if err != nil { + return nil, 0, err + } + return nlri, toCopy + 1, nil } func (n *NLRI) serialize(buf *bytes.Buffer) uint8 { - a := convert.Uint32Byte(n.IP) + buf.WriteByte(n.Pfxlen) + b := n.IP.Bytes() - addr := [4]byte{a[0], a[1], a[2], a[3]} nBytes := BytesInAddr(n.Pfxlen) - - buf.WriteByte(n.Pfxlen) - buf.Write(addr[:nBytes]) + buf.Write(b[:nBytes]) return nBytes + 1 } func (n *NLRI) serializeAddPath(buf *bytes.Buffer) uint8 { - a := convert.Uint32Byte(n.IP) - - addr := [4]byte{a[0], a[1], a[2], a[3]} - nBytes := BytesInAddr(n.Pfxlen) - buf.Write(convert.Uint32Byte(n.PathIdentifier)) - buf.WriteByte(n.Pfxlen) - buf.Write(addr[:nBytes]) - return nBytes + 4 + return uint8(n.serialize(buf) + 4) } // BytesInAddr gets the amount of bytes needed to encode an NLRI of prefix length pfxlen diff --git a/protocols/bgp/packet/nlri_test.go b/protocols/bgp/packet/nlri_test.go index 4cefc5adb3a664564ac201a81afc5efaebe015a8..b109fe052de7526307b499422b8abd7a6378dcb0 100644 --- a/protocols/bgp/packet/nlri_test.go +++ b/protocols/bgp/packet/nlri_test.go @@ -4,6 +4,7 @@ import ( "bytes" "testing" + bnet "github.com/bio-routing/bio-rd/net" "github.com/stretchr/testify/assert" ) @@ -23,13 +24,13 @@ func TestDecodeNLRIs(t *testing.T) { }, wantFail: false, expected: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 24, Next: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("172.16.0.0"), + IP: bnet.IPv4FromOctets(172, 16, 0, 0), Pfxlen: 17, }, }, @@ -76,7 +77,7 @@ func TestDecodeNLRI(t *testing.T) { }, wantFail: false, expected: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 24, }, }, @@ -87,7 +88,7 @@ func TestDecodeNLRI(t *testing.T) { }, wantFail: false, expected: &NLRI{ - IP: strAddr("192.168.0.128"), + IP: bnet.IPv4FromOctets(192, 168, 0, 128), Pfxlen: 25, }, }, @@ -178,7 +179,7 @@ func TestNLRISerialize(t *testing.T) { { name: "Test #1", nlri: &NLRI{ - IP: strAddr("1.2.3.0"), + IP: bnet.IPv4FromOctets(1, 2, 3, 0), Pfxlen: 25, }, expected: []byte{25, 1, 2, 3, 0}, @@ -186,7 +187,7 @@ func TestNLRISerialize(t *testing.T) { { name: "Test #2", nlri: &NLRI{ - IP: strAddr("1.2.3.0"), + IP: bnet.IPv4FromOctets(1, 2, 3, 0), Pfxlen: 24, }, expected: []byte{24, 1, 2, 3}, @@ -194,7 +195,7 @@ func TestNLRISerialize(t *testing.T) { { name: "Test #3", nlri: &NLRI{ - IP: strAddr("100.200.128.0"), + IP: bnet.IPv4FromOctets(100, 200, 128, 0), Pfxlen: 17, }, expected: []byte{17, 100, 200, 128}, @@ -219,7 +220,7 @@ func TestNLRIAddPathSerialize(t *testing.T) { name: "Test #1", nlri: &NLRI{ PathIdentifier: 100, - IP: strAddr("1.2.3.0"), + IP: bnet.IPv4FromOctets(1, 2, 3, 0), Pfxlen: 25, }, expected: []byte{0, 0, 0, 100, 25, 1, 2, 3, 0}, @@ -228,7 +229,7 @@ func TestNLRIAddPathSerialize(t *testing.T) { name: "Test #2", nlri: &NLRI{ PathIdentifier: 100, - IP: strAddr("1.2.3.0"), + IP: bnet.IPv4FromOctets(1, 2, 3, 0), Pfxlen: 24, }, expected: []byte{0, 0, 0, 100, 24, 1, 2, 3}, @@ -237,7 +238,7 @@ func TestNLRIAddPathSerialize(t *testing.T) { name: "Test #3", nlri: &NLRI{ PathIdentifier: 100, - IP: strAddr("100.200.128.0"), + IP: bnet.IPv4FromOctets(100, 200, 128, 0), Pfxlen: 17, }, expected: []byte{0, 0, 0, 100, 17, 100, 200, 128}, diff --git a/protocols/bgp/packet/path_attributes_test.go b/protocols/bgp/packet/path_attributes_test.go index 958da99b783d20121e89fe4b85c4d213891bf550..1b7cce5be4170738d58ef90574b3ed7d573f4b43 100644 --- a/protocols/bgp/packet/path_attributes_test.go +++ b/protocols/bgp/packet/path_attributes_test.go @@ -640,7 +640,7 @@ func TestDecodeAggregator(t *testing.T) { Length: 6, Value: types.Aggregator{ ASN: 222, - Address: strAddr("10.20.30.40"), + Address: bnet.IPv4FromOctets(10, 20, 30, 40).ToUint32(), }, }, }, @@ -1485,7 +1485,7 @@ func TestSerializeAggregator(t *testing.T) { TypeCode: AggregatorAttr, Value: types.Aggregator{ ASN: 174, - Address: strAddr("10.20.30.40"), + Address: bnet.IPv4FromOctets(10, 20, 30, 40).ToUint32(), }, }, expected: []byte{ @@ -1869,7 +1869,7 @@ func TestSerialize(t *testing.T) { name: "Withdraw only", msg: &BGPUpdate{ WithdrawnRoutes: &NLRI{ - IP: strAddr("100.110.120.0"), + IP: bnet.IPv4FromOctets(100, 110, 120, 0), Pfxlen: 24, }, }, @@ -1886,7 +1886,7 @@ func TestSerialize(t *testing.T) { name: "NLRI only", msg: &BGPUpdate{ NLRI: &NLRI{ - IP: strAddr("100.110.128.0"), + IP: bnet.IPv4FromOctets(100, 110, 128, 0), Pfxlen: 17, }, }, @@ -1925,10 +1925,10 @@ func TestSerialize(t *testing.T) { name: "Full test", msg: &BGPUpdate{ WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -1962,7 +1962,7 @@ func TestSerialize(t *testing.T) { TypeCode: AggregatorAttr, Value: types.Aggregator{ ASN: 200, - Address: strAddr("10.20.30.40"), + Address: bnet.IPv4FromOctets(10, 20, 30, 40).ToUint32(), }, }, }, @@ -1972,10 +1972,10 @@ func TestSerialize(t *testing.T) { }, }, NLRI: &NLRI{ - IP: strAddr("8.8.8.0"), + IP: bnet.IPv4FromOctets(8, 8, 8, 0), Pfxlen: 24, Next: &NLRI{ - IP: strAddr("185.65.240.0"), + IP: bnet.IPv4FromOctets(185, 65, 240, 0), Pfxlen: 22, }, }, @@ -2042,7 +2042,7 @@ func TestSerialize(t *testing.T) { name: "Reflected NLRI", msg: &BGPUpdate{ NLRI: &NLRI{ - IP: strAddr("100.110.128.0"), + IP: bnet.IPv4FromOctets(100, 110, 128, 0), Pfxlen: 17, }, PathAttributes: &PathAttribute{ @@ -2112,7 +2112,7 @@ func TestSerializeAddPath(t *testing.T) { msg: &BGPUpdate{ WithdrawnRoutes: &NLRI{ PathIdentifier: 257, - IP: strAddr("100.110.120.0"), + IP: bnet.IPv4FromOctets(100, 110, 120, 0), Pfxlen: 24, }, }, @@ -2131,7 +2131,7 @@ func TestSerializeAddPath(t *testing.T) { msg: &BGPUpdate{ NLRI: &NLRI{ PathIdentifier: 257, - IP: strAddr("100.110.128.0"), + IP: bnet.IPv4FromOctets(100, 110, 128, 0), Pfxlen: 17, }, }, @@ -2171,10 +2171,10 @@ func TestSerializeAddPath(t *testing.T) { name: "Full test", msg: &BGPUpdate{ WithdrawnRoutes: &NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -2208,7 +2208,7 @@ func TestSerializeAddPath(t *testing.T) { TypeCode: AggregatorAttr, Value: types.Aggregator{ ASN: 200, - Address: strAddr("10.20.30.40"), + Address: bnet.IPv4FromOctets(10, 20, 30, 40).ToUint32(), }, }, }, @@ -2218,10 +2218,10 @@ func TestSerializeAddPath(t *testing.T) { }, }, NLRI: &NLRI{ - IP: strAddr("8.8.8.0"), + IP: bnet.IPv4FromOctets(8, 8, 8, 0), Pfxlen: 24, Next: &NLRI{ - IP: strAddr("185.65.240.0"), + IP: bnet.IPv4FromOctets(185, 65, 240, 0), Pfxlen: 22, }, }, diff --git a/protocols/bgp/server/fsm_address_family.go b/protocols/bgp/server/fsm_address_family.go index 092572c50463ff4109efa669f798e484d218e5ee..8eac38966eab6533ed42a3a44207c1cfcbdb0f42 100644 --- a/protocols/bgp/server/fsm_address_family.go +++ b/protocols/bgp/server/fsm_address_family.go @@ -107,14 +107,14 @@ func (f *fsmAddressFamily) processUpdate(u *packet.BGPUpdate) { func (f *fsmAddressFamily) withdraws(u *packet.BGPUpdate) { for r := u.WithdrawnRoutes; r != nil; r = r.Next { - pfx := bnet.NewPfx(bnet.IPv4(r.IP), r.Pfxlen) + pfx := bnet.NewPfx(r.IP, r.Pfxlen) f.adjRIBIn.RemovePath(pfx, nil) } } func (f *fsmAddressFamily) updates(u *packet.BGPUpdate) { for r := u.NLRI; r != nil; r = r.Next { - pfx := bnet.NewPfx(bnet.IPv4(r.IP), r.Pfxlen) + pfx := bnet.NewPfx(r.IP, r.Pfxlen) path := f.newRoutePath() f.processAttributes(u.PathAttributes, path) diff --git a/protocols/bgp/server/update_helper_test.go b/protocols/bgp/server/update_helper_test.go index 86a70f64d5626c4cbb38fb4eca5b793d7a548093..a7f45b5b39ee7279c44168395303ac6480cceff0 100644 --- a/protocols/bgp/server/update_helper_test.go +++ b/protocols/bgp/server/update_helper_test.go @@ -8,7 +8,7 @@ import ( "github.com/bio-routing/bio-rd/protocols/bgp/packet" - "github.com/bio-routing/bio-rd/net" + bnet "github.com/bio-routing/bio-rd/net" "github.com/stretchr/testify/assert" ) @@ -49,10 +49,10 @@ func TestSerializeAndSendUpdate(t *testing.T) { testUpdate: &packet.BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &packet.NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &packet.NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -79,10 +79,10 @@ func TestSerializeAndSendUpdate(t *testing.T) { testUpdate: &packet.BGPUpdate{ WithdrawnRoutesLen: 5, WithdrawnRoutes: &packet.NLRI{ - IP: strAddr("10.0.0.0"), + IP: bnet.IPv4FromOctets(10, 0, 0, 0), Pfxlen: 8, Next: &packet.NLRI{ - IP: strAddr("192.168.0.0"), + IP: bnet.IPv4FromOctets(192, 168, 0, 0), Pfxlen: 16, }, }, @@ -100,8 +100,3 @@ func TestSerializeAndSendUpdate(t *testing.T) { }) } } - -func strAddr(s string) uint32 { - ret, _ := net.StrToAddr(s) - return ret -} diff --git a/protocols/bgp/server/update_sender.go b/protocols/bgp/server/update_sender.go index 9e40303fc4e52aa36b3678026bf855be8992b931..ca7a11d5f9738d07c82c55591830652ab4ec6b11 100644 --- a/protocols/bgp/server/update_sender.go +++ b/protocols/bgp/server/update_sender.go @@ -191,7 +191,7 @@ func (u *UpdateSender) bgpUpdate(pfxs []bnet.Prefix, pa *packet.PathAttribute, p for _, pfx := range pfxs { nlri = &packet.NLRI{ PathIdentifier: pathID, - IP: pfx.Addr().ToUint32(), + IP: pfx.Addr(), Pfxlen: pfx.Pfxlen(), Next: update.NLRI, } @@ -276,7 +276,7 @@ func (u *UpdateSender) withdrawPrefixIPv4(out io.Writer, pfx bnet.Prefix, p *rou update := &packet.BGPUpdate{ WithdrawnRoutes: &packet.NLRI{ PathIdentifier: p.BGPPath.PathIdentifier, - IP: pfx.Addr().ToUint32(), + IP: pfx.Addr(), Pfxlen: pfx.Pfxlen(), }, }