From 8ae3affce071e10243334b32d8398c732608f97e Mon Sep 17 00:00:00 2001 From: shirou Date: Sat, 2 Jul 2022 13:39:32 +0000 Subject: [PATCH 1/3] fix(net,linux): fix decodeaddress if Big Endian --- net/net.go | 8 ++++++++ net/net_linux.go | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/net/net.go b/net/net.go index 0f3a62f39..0f28f28cd 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 81cde8133..21d2a2403 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 { From e4e6ed2efa2743cc02c9b352ae75b3ffd5c983f1 Mon Sep 17 00:00:00 2001 From: shirou Date: Sun, 3 Jul 2022 02:44:35 +0000 Subject: [PATCH 2/3] fix(net,linux): fix BigEndian test --- net/net_linux_test.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/net/net_linux_test.go b/net/net_linux_test.go index bec091c54..05ff0236c 100644 --- a/net/net_linux_test.go +++ b/net/net_linux_test.go @@ -137,14 +137,6 @@ func TestDecodeAddress(t *testing.T) { assert := assert.New(t) addr := map[string]AddrTest{ - "0500000A:0016": { - IP: "10.0.0.5", - Port: 22, - }, - "0100007F:D1C2": { - IP: "127.0.0.1", - Port: 53698, - }, "11111:0035": { Error: true, }, @@ -159,6 +151,25 @@ func TestDecodeAddress(t *testing.T) { Error: true, }, } + if IsLittleEndian() { + addr["0500000A:0016"] = AddrTest{ + IP: "10.0.0.5", + Port: 22, + } + addr["0100007F:D1C2"] = AddrTest{ + IP: "127.0.0.1", + Port: 53698, + } + } else { + addr["0A000005:0016"] = AddrTest{ + IP: "10.0.0.5", + Port: 22, + } + addr["7F000001:D1C2"] = AddrTest{ + IP: "127.0.0.1", + Port: 53698, + } + } for src, dst := range addr { family := syscall.AF_INET From f11e3ba120f70c03914e845ecf7f79c33d5ebf30 Mon Sep 17 00:00:00 2001 From: shirou Date: Mon, 4 Jul 2022 08:41:25 +0000 Subject: [PATCH 3/3] fix(net,linux): move IsLittleEndian to internal --- internal/common/endian.go | 10 ++++++++++ net/net.go | 8 -------- net/net_linux.go | 2 +- net/net_linux_test.go | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 internal/common/endian.go diff --git a/internal/common/endian.go b/internal/common/endian.go new file mode 100644 index 000000000..147cfdc4b --- /dev/null +++ b/internal/common/endian.go @@ -0,0 +1,10 @@ +package common + +import "unsafe" + +// 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.go b/net/net.go index 0f28f28cd..0f3a62f39 100644 --- a/net/net.go +++ b/net/net.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "net" - "unsafe" "github.com/shirou/gopsutil/v3/internal/common" ) @@ -272,10 +271,3 @@ 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 21d2a2403..c08997196 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -723,7 +723,7 @@ func decodeAddressWithContext(ctx context.Context, family uint32, src string) (A var ip net.IP if family == syscall.AF_INET { - if IsLittleEndian() { + if common.IsLittleEndian() { ip = net.IP(ReverseWithContext(ctx, decoded)) } else { ip = net.IP(decoded) diff --git a/net/net_linux_test.go b/net/net_linux_test.go index 05ff0236c..d5d6252a8 100644 --- a/net/net_linux_test.go +++ b/net/net_linux_test.go @@ -151,7 +151,7 @@ func TestDecodeAddress(t *testing.T) { Error: true, }, } - if IsLittleEndian() { + if common.IsLittleEndian() { addr["0500000A:0016"] = AddrTest{ IP: "10.0.0.5", Port: 22,