Fix: Keep IPv4 address bytes in network order for connection list
iphlpapi stores the address in network byte order inside the memory of the DWORD, it is not a host-order integer. For 192.168.0.55 the DWORD value on x86 is 0x3700A8C0 while its bytes are C0 A8 00 37, so shifting the value by 24/16/8/0 produced 37 00 A8 C0 and the dialog rendered "55.0.168.192" for both the local and remote address. Copy the DWORD with memcpy instead, which preserves the memory layout that inet_ntop and SetTcpEntry both expect. GetAddr4 is changed the same way, otherwise a forced disconnect would rebuild a wrong address and miss the target connection. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
ca35e6e26f
commit
15a50db0a7
1 file changed
+8
-7
@@ -60,19 +60,20 @@ static void ResolveProcName(DWORD pid, char* out, int outLen)
|
|||||||
out[outLen - 1] = 0;
|
out[outLen - 1] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPv4 地址:iphlpapi 的 dwLocalAddr/dwRemoteAddr 是网络序 DWORD(值 0xC0A80101 即 192.168.1.1),
|
// IPv4 地址:iphlpapi 的 dwLocalAddr/dwRemoteAddr 是把网络序地址直接放在 DWORD 的**内存**里,
|
||||||
// 转成 16 字节字段前 4 字节的网络序字节后 inet_ntop 可直接展示、断连时再拼回 DWORD。
|
// 不是主机序数值(192.168.0.55 在 x86 上 DWORD 数值是 0x3700A8C0、内存字节是 C0 A8 00 37)。
|
||||||
|
// 所以只能按内存拷贝:这样得到的正是 inet_ntop 需要的网络序字节,也能原样拼回 SetTcpEntry 的 DWORD。
|
||||||
|
// 按数值移位会把字节序弄反(显示成 55.0.168.192)。
|
||||||
static void PutAddr4(uint8_t* out, DWORD addrNet)
|
static void PutAddr4(uint8_t* out, DWORD addrNet)
|
||||||
{
|
{
|
||||||
out[0] = (uint8_t)(addrNet >> 24);
|
memcpy(out, &addrNet, 4);
|
||||||
out[1] = (uint8_t)(addrNet >> 16);
|
|
||||||
out[2] = (uint8_t)(addrNet >> 8);
|
|
||||||
out[3] = (uint8_t)(addrNet);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static DWORD GetAddr4(const uint8_t* addr)
|
static DWORD GetAddr4(const uint8_t* addr)
|
||||||
{
|
{
|
||||||
return ((DWORD)addr[0] << 24) | ((DWORD)addr[1] << 16) | ((DWORD)addr[2] << 8) | (DWORD)addr[3];
|
DWORD addrNet = 0;
|
||||||
|
memcpy(&addrNet, addr, 4);
|
||||||
|
return addrNet;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void AppendTcp4(std::vector<ConnectionInfo>& rows)
|
static void AppendTcp4(std::vector<ConnectionInfo>& rows)
|
||||||
|
|||||||
Reference in new issue
Block a user