fix(net,linux): fix decodeaddress if Big Endian

pull/1322/head
shirou 3 years ago
parent 7a094df3f7
commit 8ae3affce0

@ -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
}

@ -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 {

Loading…
Cancel
Save