Skip to content

Commit

Permalink
Fix bug where I was messing with the port number
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Bart Joy committed Jul 22, 2018
1 parent 70f92d8 commit 832fbc0
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions TcpConnectionKiller/TcpConnectionKiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@ string FormatAddress(DWORD ip)
return inet_ntoa(paddr);
}

uint16_t FixPortNumber(DWORD port)
{
return ntohs(port & 0xffff);
}

void KillAll(vector<MIB_TCPROW2> 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<u_short>(row.dwLocalPort)) << " -> " << FormatAddress(row.dwRemoteAddr) << ":" << ntohs(static_cast<u_short>(row.dwRemotePort)) << endl;

DWORD result;
if ((result = SetTcpEntry(&row)) == 0)
Expand Down

0 comments on commit 832fbc0

Please sign in to comment.