From 832fbc0700fd770356b7e756c7130dfc0e61e92c Mon Sep 17 00:00:00 2001 From: Bart Joy Date: Mon, 23 Jul 2018 10:59:15 +1200 Subject: [PATCH] Fix bug where I was messing with the port number The docs say that only the lower 16 bits are relevant, but I shouldn't have been converting to host order before passing to SetTcpEntry. That should only be done for formatting purposes. --- TcpConnectionKiller/TcpConnectionKiller.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/TcpConnectionKiller/TcpConnectionKiller.cpp b/TcpConnectionKiller/TcpConnectionKiller.cpp index ff23a1f..4cf5702 100644 --- a/TcpConnectionKiller/TcpConnectionKiller.cpp +++ b/TcpConnectionKiller/TcpConnectionKiller.cpp @@ -11,23 +11,18 @@ string FormatAddress(DWORD ip) return inet_ntoa(paddr); } -uint16_t FixPortNumber(DWORD port) -{ - return ntohs(port & 0xffff); -} - void KillAll(vector const& toKill) { for (auto con : toKill) { MIB_TCPROW row; row.dwLocalAddr = con.dwLocalAddr; - row.dwLocalPort = FixPortNumber(con.dwLocalPort); + row.dwLocalPort = con.dwLocalPort & 0xffff; row.dwRemoteAddr = con.dwRemoteAddr; - row.dwRemotePort = FixPortNumber(con.dwRemotePort); + row.dwRemotePort = con.dwRemotePort & 0xffff; row.dwState = MIB_TCP_STATE_DELETE_TCB; - cout << "Killing " << FormatAddress(row.dwLocalAddr) << ":" << row.dwLocalPort << " -> " << FormatAddress(row.dwRemoteAddr) << ":" << row.dwRemotePort << endl; + cout << "Killing " << FormatAddress(row.dwLocalAddr) << ":" << ntohs(static_cast(row.dwLocalPort)) << " -> " << FormatAddress(row.dwRemoteAddr) << ":" << ntohs(static_cast(row.dwRemotePort)) << endl; DWORD result; if ((result = SetTcpEntry(&row)) == 0)