[IPHLPAPI] Implement getOwnerTcpTable(), to get TCP connections with owner PID

This commit is contained in:
Pierre Schweitzer 2018-11-24 20:23:29 +01:00
parent 411504b5f4
commit dca7e5689f
No known key found for this signature in database
GPG key ID: 7545556C3D585B0B

View 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;
}