Skip to content
Snippets Groups Projects
Commit 314fe6b5 authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

using IP type instead of uint32 in NLRI

parent 83b14ef8
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
......@@ -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
......
......@@ -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},
......
......@@ -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,
},
},
......
......@@ -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)
......
......@@ -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
}
......@@ -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(),
},
}
......
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