[NETSTAT] Add support for displaying TCP connections owning process

This commit is contained in:
Pierre Schweitzer 2018-11-24 21:04:20 +01:00
parent 73c87d5c14
commit 2b55073360
No known key found for this signature in database
GPG key ID: 7545556C3D585B0B
2 changed files with 38 additions and 10 deletions

View file

@ -134,6 +134,9 @@ BOOL ParseCmdline(int argc, char* argv[])
case 's' : case 's' :
bDoShowProtoStats = TRUE; bDoShowProtoStats = TRUE;
break; break;
case 'o' :
bDoShowProcessId = TRUE;
break;
case 'v' : case 'v' :
_tprintf(_T("got v\n")); _tprintf(_T("got v\n"));
bDoDispSeqComp = TRUE; bDoDispSeqComp = TRUE;
@ -161,6 +164,19 @@ BOOL ParseCmdline(int argc, char* argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/*
* Display table header
*/
VOID DisplayTableHeader()
{
_tprintf(_T("\n Proto Local Address Foreign Address State"));
if (bDoShowProcessId)
_tprintf(_T(" Process\n"));
else
_tprintf(_T("\n"));
}
/* /*
* Simulate Microsofts netstat utility output * Simulate Microsofts netstat utility output
*/ */
@ -168,7 +184,7 @@ BOOL DisplayOutput()
{ {
if (bNoOptions) if (bNoOptions)
{ {
_tprintf(_T("\n Proto Local Address Foreign Address State\n")); DisplayTableHeader();
ShowTcpTable(); ShowTcpTable();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -212,14 +228,14 @@ BOOL DisplayOutput()
if (bDoShowProtoStats) if (bDoShowProtoStats)
ShowTcpStatistics(); ShowTcpStatistics();
_tprintf(_T("\nActive Connections\n")); _tprintf(_T("\nActive Connections\n"));
_tprintf(_T("\n Proto Local Address Foreign Address State\n")); DisplayTableHeader();
ShowTcpTable(); ShowTcpTable();
break; break;
case UDP : case UDP :
if (bDoShowProtoStats) if (bDoShowProtoStats)
ShowUdpStatistics(); ShowUdpStatistics();
_tprintf(_T("\nActive Connections\n")); _tprintf(_T("\nActive Connections\n"));
_tprintf(_T("\n Proto Local Address Foreign Address State\n")); DisplayTableHeader();
ShowUdpTable(); ShowUdpTable();
break; break;
default : default :
@ -237,7 +253,7 @@ BOOL DisplayOutput()
else else
{ {
_tprintf(_T("\nActive Connections\n")); _tprintf(_T("\nActive Connections\n"));
_tprintf(_T("\n Proto Local Address Foreign Address State\n")); DisplayTableHeader();
ShowTcpTable(); ShowTcpTable();
if (bDoShowAllCons) if (bDoShowAllCons)
ShowUdpTable(); ShowUdpTable();
@ -408,22 +424,23 @@ VOID ShowEthernetStatistics()
VOID ShowTcpTable() VOID ShowTcpTable()
{ {
PMIB_TCPTABLE tcpTable; PMIB_TCPTABLE_OWNER_PID tcpTable;
DWORD error, dwSize; DWORD error, dwSize;
DWORD i; DWORD i;
CHAR HostIp[HOSTNAMELEN], HostPort[PORTNAMELEN]; CHAR HostIp[HOSTNAMELEN], HostPort[PORTNAMELEN];
CHAR RemoteIp[HOSTNAMELEN], RemotePort[PORTNAMELEN]; CHAR RemoteIp[HOSTNAMELEN], RemotePort[PORTNAMELEN];
CHAR Host[ADDRESSLEN]; CHAR Host[ADDRESSLEN];
CHAR Remote[ADDRESSLEN]; CHAR Remote[ADDRESSLEN];
CHAR PID[64];
/* Get the table of TCP endpoints */ /* Get the table of TCP endpoints */
dwSize = sizeof (MIB_TCPTABLE); dwSize = sizeof (MIB_TCPTABLE_OWNER_PID);
/* Should also work when we get new connections between 2 GetTcpTable() /* Should also work when we get new connections between 2 GetTcpTable()
* calls: */ * calls: */
do do
{ {
tcpTable = (PMIB_TCPTABLE) HeapAlloc(GetProcessHeap(), 0, dwSize); tcpTable = (PMIB_TCPTABLE_OWNER_PID) HeapAlloc(GetProcessHeap(), 0, dwSize);
error = GetTcpTable(tcpTable, &dwSize, TRUE); error = GetExtendedTcpTable(tcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
if ( error != NO_ERROR ) if ( error != NO_ERROR )
HeapFree(GetProcessHeap(), 0, tcpTable); HeapFree(GetProcessHeap(), 0, tcpTable);
} }
@ -461,8 +478,17 @@ VOID ShowTcpTable()
sprintf(Remote, "%s:%s", RemoteIp, RemotePort); sprintf(Remote, "%s:%s", RemoteIp, RemotePort);
} }
_tprintf(_T(" %-6s %-22s %-22s %s\n"), _T("TCP"), if (bDoShowProcessId)
Host, Remote, TcpState[tcpTable->table[i].dwState]); {
sprintf(PID, "%ld", tcpTable->table[i].dwOwningPid);
}
else
{
PID[0] = 0;
}
_tprintf(_T(" %-6s %-22s %-22s %-11s %s\n"), _T("TCP"),
Host, Remote, TcpState[tcpTable->table[i].dwState], PID);
} }
} }
HeapFree(GetProcessHeap(), 0, tcpTable); HeapFree(GetProcessHeap(), 0, tcpTable);
@ -600,6 +626,7 @@ VOID Usage()
" -s Displays per-protocol statistics. By default, Statistics are\n" " -s Displays per-protocol statistics. By default, Statistics are\n"
" shown for IP, ICMP, TCP and UDP;\n" " shown for IP, ICMP, TCP and UDP;\n"
" the -p option may be used to specify a subset of the default.\n" " the -p option may be used to specify a subset of the default.\n"
" -o Displays the process ID for each connection.\n"
" interval Redisplays selected statistics every 'interval' seconds.\n" " interval Redisplays selected statistics every 'interval' seconds.\n"
" Press CTRL+C to stop redisplaying. By default netstat will\n" " Press CTRL+C to stop redisplaying. By default netstat will\n"
" print the current information only once.\n")); " print the current information only once.\n"));

View file

@ -13,6 +13,7 @@ BOOL bDoShowNumbers = FALSE; // -n
BOOL bDoShowProtoCons = FALSE; // -p BOOL bDoShowProtoCons = FALSE; // -p
BOOL bDoShowRouteTable = FALSE; // -r BOOL bDoShowRouteTable = FALSE; // -r
BOOL bDoShowProtoStats = FALSE; // -s BOOL bDoShowProtoStats = FALSE; // -s
BOOL bDoShowProcessId = FALSE; // -o
BOOL bDoDispSeqComp = FALSE; // -v BOOL bDoDispSeqComp = FALSE; // -v
BOOL bLoopOutput = FALSE; // interval BOOL bLoopOutput = FALSE; // interval