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

removed unused func, cleanup

parent 4c3a2174
No related branches found
No related tags found
No related merge requests found
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
}
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)
}
}
......@@ -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)
......
......@@ -12,6 +12,8 @@ const (
MaxLen = 4096
MinUpdateLen = 4
NLRIMaxLen = 5
AFILen = 2
SAFILen = 1
CommunityLen = 4
LargeCommunityLen = 12
......
......@@ -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))
}
......@@ -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
......
......@@ -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
......
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