diff --git a/net/net.go b/net/net.go index 0f3a62f..0f28f28 100644 --- a/net/net.go +++ b/net/net.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "net" + "unsafe" "github.com/shirou/gopsutil/v3/internal/common" ) @@ -271,3 +272,10 @@ func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) { return []IOCountersStat{r}, nil } + +// IsLittleEndian checks if the current platform uses little-endian. +// copied from https://github.com/ntrrg/ntgo/blob/v0.8.0/runtime/infrastructure.go#L16 (MIT License) +func IsLittleEndian() bool { + var x int16 = 0x0011 + return *(*byte)(unsafe.Pointer(&x)) == 0x11 +} diff --git a/net/net_linux.go b/net/net_linux.go index 81cde81..21d2a24 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -721,9 +721,13 @@ func decodeAddressWithContext(ctx context.Context, family uint32, src string) (A return Addr{}, fmt.Errorf("decode error, %w", err) } var ip net.IP - // Assumes this is little_endian + if family == syscall.AF_INET { - ip = net.IP(ReverseWithContext(ctx, decoded)) + if IsLittleEndian() { + ip = net.IP(ReverseWithContext(ctx, decoded)) + } else { + ip = net.IP(decoded) + } } else { // IPv6 ip, err = parseIPv6HexStringWithContext(ctx, decoded) if err != nil {