diff --git a/net/helper.go b/net/helper.go deleted file mode 100644 index 4b82b2425fdc199023c675c904e92cd6bcce27b3..0000000000000000000000000000000000000000 --- a/net/helper.go +++ /dev/null @@ -1,9 +0,0 @@ -package net - -import "net" - -// IPv4ToUint32 converts an `net.IP` to an uint32 interpretation -func IPv4ToUint32(ip net.IP) uint32 { - ip = ip.To4() - return uint32(ip[3]) + uint32(ip[2])<<8 + uint32(ip[1])<<16 + uint32(ip[0])<<24 -} diff --git a/net/helper_test.go b/net/helper_test.go deleted file mode 100644 index d5a038cbb6bd2683b50ba50c658fae797e46d39c..0000000000000000000000000000000000000000 --- a/net/helper_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package net - -import ( - "testing" - - "net" - - "github.com/stretchr/testify/assert" -) - -func TestIPv4ToUint32(t *testing.T) { - tests := []struct { - input []byte - expected uint32 - }{ - { - input: []byte{192, 168, 1, 5}, - expected: 3232235781, - }, - { - input: []byte{10, 0, 0, 0}, - expected: 167772160, - }, - { - input: []byte{172, 24, 5, 1}, - expected: 2887255297, - }, - { - input: net.ParseIP("172.24.5.1"), - expected: 2887255297, - }, - } - - for _, test := range tests { - res := IPv4ToUint32(test.input) - assert.Equal(t, test.expected, res) - } -} diff --git a/net/ip.go b/net/ip.go index 8a532a2c09f36033fbb27d0e8aa69a9e10942492..616658ca14977195a4d3b81a3db73f64d61f3399 100644 --- a/net/ip.go +++ b/net/ip.go @@ -133,6 +133,20 @@ func (ip IP) bytesIPv4() []byte { } } +// IsIPv4 returns if the `IP` is of address family IPv4 +func (ip IP) IsIPv4() bool { + return ip.ipVersion == 4 +} + +// SizeBytes returns the number of bytes required to represent the `IP` +func (ip IP) SizeBytes() uint8 { + if ip.ipVersion == 4 { + return 4 + } + + return 16 +} + // ToUint32 return the rightmost 32 bits of an 'IP' func (ip IP) ToUint32() uint32 { return uint32(^uint64(0) >> 32 & ip.lower) diff --git a/protocols/bgp/packet/bgp.go b/protocols/bgp/packet/bgp.go index e008205a2ad95e97872c0a691a479efe42888c24..b827dc7135984cd88b476ca474487ecca0e2c74f 100644 --- a/protocols/bgp/packet/bgp.go +++ b/protocols/bgp/packet/bgp.go @@ -12,6 +12,8 @@ const ( MaxLen = 4096 MinUpdateLen = 4 NLRIMaxLen = 5 + AFILen = 2 + SAFILen = 1 CommunityLen = 4 LargeCommunityLen = 12 diff --git a/protocols/bgp/packet/helper.go b/protocols/bgp/packet/helper.go index 991af5a5eb9410c6875400c3b88ed3ee2e7c8de4..d2e7d23a29af30ffe7d30451bb9f3b45bc66420e 100644 --- a/protocols/bgp/packet/helper.go +++ b/protocols/bgp/packet/helper.go @@ -2,7 +2,6 @@ package packet import ( "fmt" - "math" bnet "github.com/bio-routing/bio-rd/net" ) @@ -12,7 +11,7 @@ func serializePrefix(pfx bnet.Prefix) []byte { return []byte{} } - numBytes := numberOfBytesForPrefixLength(pfx.Pfxlen()) + numBytes := BytesInAddr(pfx.Pfxlen()) b := make([]byte, numBytes+1) b[0] = pfx.Pfxlen() @@ -22,7 +21,7 @@ func serializePrefix(pfx bnet.Prefix) []byte { } func deserializePrefix(b []byte, pfxLen uint8, afi uint16) (bnet.Prefix, error) { - numBytes := numberOfBytesForPrefixLength(pfxLen) + numBytes := BytesInAddr(pfxLen) if numBytes != uint8(len(b)) { return bnet.Prefix{}, fmt.Errorf("could not parse prefix of length %d. Expected %d bytes, got %d", pfxLen, numBytes, len(b)) @@ -38,7 +37,3 @@ func deserializePrefix(b []byte, pfxLen uint8, afi uint16) (bnet.Prefix, error) return bnet.NewPfx(ip, pfxLen), nil } - -func numberOfBytesForPrefixLength(pfxLen uint8) uint8 { - return uint8(math.Ceil(float64(pfxLen) / 8)) -} diff --git a/protocols/bgp/packet/mp_reach_nlri.go b/protocols/bgp/packet/mp_reach_nlri.go index ec18df05c242e714951ff433fad194e8e24db80b..8b15eaca5fdb4a34ab3325ecd59e5f0170154a91 100644 --- a/protocols/bgp/packet/mp_reach_nlri.go +++ b/protocols/bgp/packet/mp_reach_nlri.go @@ -67,7 +67,7 @@ func deserializeMultiProtocolReachNLRI(b []byte) (MultiProtocolReachNLRI, error) idx := uint16(0) for idx < uint16(len(variable)) { pfxLen := variable[idx] - numBytes := uint16(numberOfBytesForPrefixLength(pfxLen)) + numBytes := uint16(BytesInAddr(pfxLen)) idx++ r := uint16(len(variable)) - idx diff --git a/protocols/bgp/packet/mp_unreach_nlri.go b/protocols/bgp/packet/mp_unreach_nlri.go index b7835cd11a24da70d811c1653f3c93cb13b824a3..bd1a28280efd7d24850d1f2eee00ad0600bad632 100644 --- a/protocols/bgp/packet/mp_unreach_nlri.go +++ b/protocols/bgp/packet/mp_unreach_nlri.go @@ -49,7 +49,7 @@ func deserializeMultiProtocolUnreachNLRI(b []byte) (MultiProtocolUnreachNLRI, er idx := uint16(0) for idx < uint16(len(prefix)) { pfxLen := prefix[idx] - numBytes := uint16(numberOfBytesForPrefixLength(pfxLen)) + numBytes := uint16(BytesInAddr(pfxLen)) idx++ r := uint16(len(prefix)) - idx