[NETSTAT] Add support for displaying TCP connections owning process
authorPierre Schweitzer <pierre@reactos.org>
Sat, 24 Nov 2018 20:04:20 +0000 (21:04 +0100)
committerPierre Schweitzer <pierre@reactos.org>
Sat, 24 Nov 2018 20:41:10 +0000 (21:41 +0100)
base/applications/network/netstat/netstat.c
base/applications/network/netstat/netstat.h

index 296e6df..6695777 100644 (file)
@@ -134,6 +134,9 @@ BOOL ParseCmdline(int argc, char* argv[])
                     case 's' :
                         bDoShowProtoStats = TRUE;
                         break;
+                    case 'o' :
+                        bDoShowProcessId = TRUE;
+                        break;
                     case 'v' :
                         _tprintf(_T("got v\n"));
                         bDoDispSeqComp = TRUE;
@@ -161,6 +164,19 @@ BOOL ParseCmdline(int argc, char* argv[])
     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
  */
@@ -168,7 +184,7 @@ BOOL DisplayOutput()
 {
     if (bNoOptions)
     {
-        _tprintf(_T("\n  Proto  Local Address          Foreign Address        State\n"));
+        DisplayTableHeader();
         ShowTcpTable();
         return EXIT_SUCCESS;
     }
@@ -212,14 +228,14 @@ BOOL DisplayOutput()
                     if (bDoShowProtoStats)
                         ShowTcpStatistics();
                     _tprintf(_T("\nActive Connections\n"));
-                    _tprintf(_T("\n  Proto  Local Address          Foreign Address        State\n"));
+                    DisplayTableHeader();
                     ShowTcpTable();
                     break;
                 case UDP :
                     if (bDoShowProtoStats)
                         ShowUdpStatistics();
                     _tprintf(_T("\nActive Connections\n"));
-                    _tprintf(_T("\n  Proto  Local Address          Foreign Address        State\n"));
+                    DisplayTableHeader();
                     ShowUdpTable();
                     break;
                 default :
@@ -237,7 +253,7 @@ BOOL DisplayOutput()
     else
     {
         _tprintf(_T("\nActive Connections\n"));
-        _tprintf(_T("\n  Proto  Local Address          Foreign Address        State\n"));
+        DisplayTableHeader();
         ShowTcpTable();
         if (bDoShowAllCons)
             ShowUdpTable();
@@ -408,22 +424,23 @@ VOID ShowEthernetStatistics()
 
 VOID ShowTcpTable()
 {
-    PMIB_TCPTABLE tcpTable;
+    PMIB_TCPTABLE_OWNER_PID tcpTable;
     DWORD error, dwSize;
     DWORD i;
     CHAR HostIp[HOSTNAMELEN], HostPort[PORTNAMELEN];
     CHAR RemoteIp[HOSTNAMELEN], RemotePort[PORTNAMELEN];
     CHAR Host[ADDRESSLEN];
     CHAR Remote[ADDRESSLEN];
+    CHAR PID[64];
 
     /* 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()
      * calls: */
     do
     {
-        tcpTable = (PMIB_TCPTABLE) HeapAlloc(GetProcessHeap(), 0, dwSize);
-        error = GetTcpTable(tcpTable, &dwSize, TRUE);
+        tcpTable = (PMIB_TCPTABLE_OWNER_PID) HeapAlloc(GetProcessHeap(), 0, dwSize);
+        error = GetExtendedTcpTable(tcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
         if ( error != NO_ERROR )
             HeapFree(GetProcessHeap(), 0, tcpTable);
     }
@@ -461,8 +478,17 @@ VOID ShowTcpTable()
                 sprintf(Remote, "%s:%s", RemoteIp, RemotePort);
             }
 
-            _tprintf(_T("  %-6s %-22s %-22s %s\n"), _T("TCP"),
-            Host, Remote, TcpState[tcpTable->table[i].dwState]);
+            if (bDoShowProcessId)
+            {
+                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);
@@ -600,6 +626,7 @@ VOID Usage()
     "  -s            Displays per-protocol statistics. By default, Statistics are\n"
     "                shown for IP, ICMP, TCP and UDP;\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"
     "                Press CTRL+C to stop redisplaying. By default netstat will\n"
     "                print the current information only once.\n"));
index e57b89d..0f3a94d 100644 (file)
@@ -13,6 +13,7 @@ BOOL bDoShowNumbers    = FALSE; // -n
 BOOL bDoShowProtoCons  = FALSE; // -p
 BOOL bDoShowRouteTable = FALSE; // -r
 BOOL bDoShowProtoStats = FALSE; // -s
+BOOL bDoShowProcessId  = FALSE; // -o
 BOOL bDoDispSeqComp    = FALSE; // -v
 BOOL bLoopOutput       = FALSE; // interval