iphlpapi: Moved AllocateAndGetTcpTableFromStack implementation to ipstats.c.

This commit is contained in:
Alexandre Julliard 2009-03-02 12:51:02 +01:00
parent 95827a8237
commit bc08fb99d3
3 changed files with 44 additions and 72 deletions

View File

@ -291,67 +291,6 @@ DWORD WINAPI AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE *ppIpNetTable,
}
static int TcpTableSorter(const void *a, const void *b)
{
int ret;
if (a && b) {
const MIB_TCPROW* rowA = a;
const MIB_TCPROW* rowB = b;
ret = ntohl (rowA->dwLocalAddr) - ntohl (rowB->dwLocalAddr);
if (ret == 0) {
ret = ntohs ((unsigned short)rowA->dwLocalPort) -
ntohs ((unsigned short)rowB->dwLocalPort);
if (ret == 0) {
ret = ntohl (rowA->dwRemoteAddr) - ntohl (rowB->dwRemoteAddr);
if (ret == 0)
ret = ntohs ((unsigned short)rowA->dwRemotePort) -
ntohs ((unsigned short)rowB->dwRemotePort);
}
}
}
else
ret = 0;
return ret;
}
/******************************************************************
* AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
*
* Get the TCP connection table.
* Like GetTcpTable(), but allocate the returned table from heap.
*
* PARAMS
* ppTcpTable [Out] pointer into which the MIB_TCPTABLE is
* allocated and returned.
* bOrder [In] whether to sort the table
* heap [In] heap from which the table is allocated
* flags [In] flags to HeapAlloc
*
* RETURNS
* ERROR_INVALID_PARAMETER if ppTcpTable is NULL, whatever GetTcpTable()
* returns otherwise.
*/
DWORD WINAPI AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE *ppTcpTable,
BOOL bOrder, HANDLE heap, DWORD flags)
{
DWORD ret;
TRACE("ppTcpTable %p, bOrder %d, heap %p, flags 0x%08x\n",
ppTcpTable, bOrder, heap, flags);
*ppTcpTable = NULL;
ret = getTcpTable(ppTcpTable, heap, flags);
if (!ret && bOrder)
qsort((*ppTcpTable)->table, (*ppTcpTable)->dwNumEntries,
sizeof(MIB_TCPROW), TcpTableSorter);
TRACE("returning %d\n", ret);
return ret;
}
/******************************************************************
* CreateIpForwardEntry (IPHLPAPI.@)
*
@ -1525,7 +1464,7 @@ DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
if (!pdwSize) return ERROR_INVALID_PARAMETER;
ret = getTcpTable(&table, GetProcessHeap(), 0);
ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
if (!ret) {
DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
if (!pTcpTable || *pdwSize < size) {
@ -1535,9 +1474,6 @@ DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
else {
*pdwSize = size;
memcpy(pTcpTable, table, size);
if (bOrder)
qsort(pTcpTable->table, pTcpTable->dwNumEntries,
sizeof(MIB_TCPROW), TcpTableSorter);
}
HeapFree(GetProcessHeap(), 0, table);
}

View File

@ -1591,12 +1591,46 @@ static DWORD TCPStateToMIBState (int state)
}
DWORD getTcpTable(PMIB_TCPTABLE *ppTcpTable, HANDLE heap, DWORD flags)
static int compare_tcp_rows(const void *a, const void *b)
{
const MIB_TCPROW *rowA = a;
const MIB_TCPROW *rowB = b;
int ret;
if ((ret = ntohl (rowA->dwLocalAddr) - ntohl (rowB->dwLocalAddr)) != 0) return ret;
if ((ret = ntohs ((unsigned short)rowA->dwLocalPort) -
ntohs ((unsigned short)rowB->dwLocalPort)) != 0) return ret;
if ((ret = ntohl (rowA->dwRemoteAddr) - ntohl (rowB->dwRemoteAddr)) != 0) return ret;
return ntohs ((unsigned short)rowA->dwRemotePort) - ntohs ((unsigned short)rowB->dwRemotePort);
}
/******************************************************************
* AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
*
* Get the TCP connection table.
* Like GetTcpTable(), but allocate the returned table from heap.
*
* PARAMS
* ppTcpTable [Out] pointer into which the MIB_TCPTABLE is
* allocated and returned.
* bOrder [In] whether to sort the table
* heap [In] heap from which the table is allocated
* flags [In] flags to HeapAlloc
*
* RETURNS
* ERROR_INVALID_PARAMETER if ppTcpTable is NULL, whatever GetTcpTable()
* returns otherwise.
*/
DWORD WINAPI AllocateAndGetTcpTableFromStack( PMIB_TCPTABLE *ppTcpTable, BOOL bOrder,
HANDLE heap, DWORD flags)
{
MIB_TCPTABLE *table;
MIB_TCPROW row;
DWORD ret = NO_ERROR, count = 16;
TRACE("table %p, bOrder %d, heap %p, flags 0x%08x\n", ppTcpTable, bOrder, heap, flags);
if (!ppTcpTable) return ERROR_INVALID_PARAMETER;
if (!(table = HeapAlloc( heap, flags, FIELD_OFFSET(MIB_TCPTABLE, table[count] ))))
@ -1713,7 +1747,13 @@ DWORD getTcpTable(PMIB_TCPTABLE *ppTcpTable, HANDLE heap, DWORD flags)
#endif
if (!table) return ERROR_OUTOFMEMORY;
if (!ret) *ppTcpTable = table;
if (!ret)
{
if (bOrder && table->dwNumEntries)
qsort( table->table, table->dwNumEntries, sizeof(row), compare_tcp_rows );
*ppTcpTable = table;
}
else HeapFree( heap, flags, table );
TRACE( "returning ret %u table %p\n", ret, table );
return ret;
}

View File

@ -75,11 +75,7 @@ DWORD getNumUdpEntries(void);
/* Returns the number of entries in the TCP state table. */
DWORD getNumTcpEntries(void);
/* Allocates the TCP state table from heap and returns it to you in *ppTcpTable.
* Returns NO_ERROR on success, something else on failure.
*/
DWORD getTcpTable(PMIB_TCPTABLE *ppTcpTable, HANDLE heap, DWORD flags);
DWORD WINAPI AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE *ppUdpTable, BOOL bOrder, HANDLE heap, DWORD flags);
DWORD WINAPI AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE *ppTcpTable, BOOL bOrder, HANDLE heap, DWORD flags);
#endif /* ndef WINE_IPSTATS_H_ */