[IPHLPAPI] Implement getOwnerTcpTable(), to get TCP connections with owner PID
authorPierre Schweitzer <pierre@reactos.org>
Sat, 24 Nov 2018 19:23:29 +0000 (20:23 +0100)
committerPierre Schweitzer <pierre@reactos.org>
Sat, 24 Nov 2018 20:41:09 +0000 (21:41 +0100)
dll/win32/iphlpapi/ipstats_reactos.c

index d8ec38f..d117a5f 100644 (file)
@@ -809,3 +809,64 @@ PMIB_TCPTABLE getTcpTable(void)
 
     return IpTcpTable;
 }
+
+PMIB_TCPTABLE_OWNER_PID getOwnerTcpTable(void)
+{
+    DWORD numEntities, returnSize;
+    TDIEntityID *entitySet;
+    HANDLE tcpFile;
+    int i, totalNumber, TmpIdx, CurrIdx = 0;
+    NTSTATUS status;
+    PMIB_TCPTABLE_OWNER_PID IpOwnerTcpTable = NULL;
+    PMIB_TCPROW_OWNER_PID AdapterOwnerTcpTable = NULL;
+
+    TRACE("called.\n");
+
+    totalNumber = getNumTcpEntries();
+
+    status = openTcpFile( &tcpFile, FILE_READ_DATA );
+    if( !NT_SUCCESS(status) ) {
+        ERR("openTcpFile returned 0x%08lx\n", status);
+        return 0;
+    }
+
+    IpOwnerTcpTable = HeapAlloc
+       ( GetProcessHeap(), 0,
+         sizeof(DWORD) + (sizeof(MIB_TCPROW_OWNER_PID) * totalNumber) );
+    if (!IpOwnerTcpTable) {
+        closeTcpFile(tcpFile);
+        return NULL;
+    }
+
+    status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
+
+    for( i = 0; i < numEntities; i++ ) {
+        if( entitySet[i].tei_entity == CO_TL_ENTITY &&
+           hasArp( tcpFile, &entitySet[i] ) ) {
+
+           status = tdiGetSetOfThings( tcpFile,
+                                       INFO_CLASS_PROTOCOL,
+                                       INFO_TYPE_PROVIDER,
+                                       IP_MIB_ADDRTABLE_ENTRY_ID,
+                                       CO_TL_ENTITY,
+                                       entitySet[i].tei_instance,
+                                       0,
+                                       sizeof(MIB_TCPROW_OWNER_PID),
+                                       (PVOID *)&AdapterOwnerTcpTable,
+                                       &returnSize );
+
+            if( status == STATUS_SUCCESS ) {
+                for( TmpIdx = 0; TmpIdx < returnSize; TmpIdx++, CurrIdx++ )
+                    IpOwnerTcpTable->table[CurrIdx] = AdapterOwnerTcpTable[TmpIdx];
+                tdiFreeThingSet( AdapterOwnerTcpTable );
+            }
+        }
+    }
+
+    closeTcpFile( tcpFile );
+
+    tdiFreeThingSet( entitySet );
+    IpOwnerTcpTable->dwNumEntries = CurrIdx;
+
+    return IpOwnerTcpTable;
+}