Skip to content

Commit

Permalink
fix(#139): Do not check for host endianness.
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Klemenski <[email protected]>
  • Loading branch information
rectified95 committed Jun 5, 2024
1 parent 9f455ca commit 9c627d0
Showing 1 changed file with 2 additions and 31 deletions.
33 changes: 2 additions & 31 deletions pkg/utils/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,15 @@ func htons(i uint16) uint16 {
// https://gist.github.com/ammario/649d4c0da650162efd404af23e25b86b
func Int2ip(nn uint32) net.IP {
ip := make(net.IP, 4)
switch determineEndian() {
case binary.BigEndian:
binary.BigEndian.PutUint32(ip, nn)
default:
// default is little endian
binary.LittleEndian.PutUint32(ip, nn)
}
binary.LittleEndian.PutUint32(ip, nn)
return ip
}

func Ip2int(ip []byte) (res uint32, err error) {
if len(ip) == 16 {
return res, errors.New("IPv6 not supported")
}
switch determineEndian() {
case binary.BigEndian:
res = binary.BigEndian.Uint32(ip)
default:
// default is little endian.
res = binary.LittleEndian.Uint32(ip)
}
return res, nil
return binary.LittleEndian.Uint32(ip), nil
}

// HostToNetShort converts a 16-bit integer from host to network byte order, aka "htons"
Expand All @@ -80,22 +67,6 @@ func HostToNetShort(i uint16) uint16 {
return binary.BigEndian.Uint16(b)
}

func determineEndian() binary.ByteOrder {
var endian binary.ByteOrder
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xABCD)

switch buf {
case [2]byte{0xCD, 0xAB}:
endian = binary.LittleEndian
case [2]byte{0xAB, 0xCD}:
endian = binary.BigEndian
default:
fmt.Println("Couldn't determine endianness")
}
return endian
}

// GetDefaultOutgoingLinks gets the outgoing interface by executing an equivalent to `ip route show default 0.0.0.0/0`
func GetDefaultOutgoingLinks() ([]netlink.Link, error) {
routes, err := netlink.RouteList(nil, netlink.FAMILY_ALL)
Expand Down

0 comments on commit 9c627d0

Please sign in to comment.