Created
February 5, 2019 11:13
-
-
Save Barakat/d2bc3dae51c2fe0060482beec85faa4a to your computer and use it in GitHub Desktop.
TCPView like example. It only displays IP4 TCP connections but you can extend it easily.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <iphlpapi.h> | |
#include <cstdio> | |
#pragma comment(lib, "iphlpapi.lib") | |
static void Ip4ToString(DWORD ip4, char ip4_string[16]) | |
{ | |
if (ip4 == 0) | |
{ | |
std::snprintf(ip4_string, 16, "0.0.0.0"); | |
} | |
else | |
{ | |
const int n0 = (ip4) & 0xff; | |
const int n1 = (ip4 >> 8) & 0xff; | |
const int n2 = (ip4 >> 16) & 0xff; | |
const int n3 = (ip4 >> 24) & 0xff; | |
std::snprintf(ip4_string, 16, "%d.%d.%d.%d", n0, n1, n2, n3); | |
} | |
} | |
static const char* TcpStatusToString(DWORD status) | |
{ | |
switch (status) | |
{ | |
case MIB_TCP_STATE_CLOSED: | |
return "CLOSED"; | |
case MIB_TCP_STATE_LISTEN: | |
return "LISTEN"; | |
case MIB_TCP_STATE_SYN_SENT: | |
return "SYN_SENT"; | |
case MIB_TCP_STATE_SYN_RCVD: | |
return "SYN_RECEIVED"; | |
case MIB_TCP_STATE_ESTAB: | |
return "ESTABLISHED"; | |
case MIB_TCP_STATE_FIN_WAIT1: | |
case MIB_TCP_STATE_FIN_WAIT2: | |
return "FIN_WAIT1"; | |
case MIB_TCP_STATE_CLOSE_WAIT: | |
return "CLOSE_WAIT"; | |
case MIB_TCP_STATE_CLOSING: | |
return "CLOSING"; | |
case MIB_TCP_STATE_LAST_ACK: | |
return "LAST_ACK"; | |
case MIB_TCP_STATE_TIME_WAIT: | |
return "TIME_WAIT"; | |
case MIB_TCP_STATE_DELETE_TCB: | |
return "DELETE_TCB"; | |
default: | |
break; | |
} | |
return ""; | |
} | |
int main() | |
{ | |
const auto heap = GetProcessHeap(); | |
DWORD buffer_size = sizeof(MIB_TCPTABLE_OWNER_PID); | |
auto buffer = HeapAlloc(heap, HEAP_GENERATE_EXCEPTIONS, buffer_size); | |
DWORD result; | |
while ((result = GetExtendedTcpTable(buffer, &buffer_size, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) == | |
ERROR_INSUFFICIENT_BUFFER) | |
{ | |
buffer = HeapReAlloc(heap, 0, buffer, buffer_size); | |
} | |
if (result == NO_ERROR) | |
{ | |
const auto mib_tcp_table = reinterpret_cast<const MIB_TCPTABLE_OWNER_PID *>(buffer); | |
for (DWORD i = 0; i < mib_tcp_table->dwNumEntries; ++i) | |
{ | |
const auto mib_tcp_table_row = &mib_tcp_table->table[i]; | |
char ip4_address_string[16]{0}; | |
Ip4ToString(mib_tcp_table_row->dwLocalAddr, &ip4_address_string[0]); | |
std::printf("%-10lu TCP %12s %16s:%-5d", | |
mib_tcp_table_row->dwOwningPid, | |
TcpStatusToString(mib_tcp_table_row->dwState), | |
ip4_address_string, | |
mib_tcp_table_row->dwLocalPort); | |
if (mib_tcp_table_row->dwState != MIB_TCP_STATE_LISTEN) | |
{ | |
Ip4ToString(mib_tcp_table_row->dwRemoteAddr, &ip4_address_string[0]); | |
std::printf(" <-> %s:%-5d", ip4_address_string, mib_tcp_table_row->dwRemotePort); | |
} | |
std::printf("\n"); | |
} | |
} | |
HeapFree(heap, 0, buffer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
_byteswap_ushort(mib_tcp_table_row->dwLocalPort);
_byteswap_ushort(mib_tcp_table_row->dwRemotePort);