From 33b162df07fa6883a97265cad8a713511e601b5a Mon Sep 17 00:00:00 2001 From: Daniel Czerwonk <daniel@dan-nrw.de> Date: Mon, 2 Jul 2018 22:03:26 +0200 Subject: [PATCH] removed unused func, cleanup --- net/helper.go | 9 ------ net/helper_test.go | 38 ------------------------- net/ip.go | 14 +++++++++ protocols/bgp/packet/bgp.go | 2 ++ protocols/bgp/packet/helper.go | 9 ++---- protocols/bgp/packet/mp_reach_nlri.go | 2 +- protocols/bgp/packet/mp_unreach_nlri.go | 2 +- 7 files changed, 20 insertions(+), 56 deletions(-) delete mode 100644 net/helper.go delete mode 100644 net/helper_test.go diff --git a/net/helper.go b/net/helper.go deleted file mode 100644 index 4b82b242..00000000 --- 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 d5a038cb..00000000 --- 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 8a532a2c..616658ca 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 e008205a..b827dc71 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 991af5a5..d2e7d23a 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 ec18df05..8b15eaca 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 b7835cd1..bd1a2828 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 -- GitLab