Commit d069e498 authored by Alexandre Julliard's avatar Alexandre Julliard

iphlpapi: Reimplement GetUdpTable to avoid parsing the same information three times.

parent 9b309b01
...@@ -1685,31 +1685,17 @@ DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats) ...@@ -1685,31 +1685,17 @@ DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats)
*/ */
DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder) DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
{ {
DWORD ret; DWORD ret;
PMIB_UDPTABLE table;
TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
(DWORD)bOrder);
if (!pdwSize)
ret = ERROR_INVALID_PARAMETER;
else {
DWORD numEntries = getNumUdpEntries();
DWORD size = sizeof(MIB_UDPTABLE);
if (numEntries > 1) if (!pdwSize) return ERROR_INVALID_PARAMETER;
size += (numEntries - 1) * sizeof(MIB_UDPROW);
if (!pUdpTable || *pdwSize < size) {
*pdwSize = size;
ret = ERROR_INSUFFICIENT_BUFFER;
}
else {
PMIB_UDPTABLE table;
ret = getUdpTable(&table, GetProcessHeap(), 0); ret = getUdpTable(&table, GetProcessHeap(), 0);
if (!ret) { if (!ret) {
size = sizeof(MIB_UDPTABLE); DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
if (table->dwNumEntries > 1) if (!pUdpTable || *pdwSize < size) {
size += (table->dwNumEntries - 1) * sizeof(MIB_UDPROW);
if (*pdwSize < size) {
*pdwSize = size; *pdwSize = size;
ret = ERROR_INSUFFICIENT_BUFFER; ret = ERROR_INSUFFICIENT_BUFFER;
} }
...@@ -1719,16 +1705,11 @@ DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder) ...@@ -1719,16 +1705,11 @@ DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
if (bOrder) if (bOrder)
qsort(pUdpTable->table, pUdpTable->dwNumEntries, qsort(pUdpTable->table, pUdpTable->dwNumEntries,
sizeof(MIB_UDPROW), UdpTableSorter); sizeof(MIB_UDPROW), UdpTableSorter);
ret = NO_ERROR;
} }
HeapFree(GetProcessHeap(), 0, table); HeapFree(GetProcessHeap(), 0, table);
}
else
ret = ERROR_OUTOFMEMORY;
} }
} TRACE("returning %d\n", ret);
TRACE("returning %d\n", ret); return ret;
return ret;
} }
......
/* Copyright (C) 2003,2006 Juan Lang /*
* Copyright (C) 2003,2006 Juan Lang
* Copyright (C) 2007 TransGaming Technologies Inc. * Copyright (C) 2007 TransGaming Technologies Inc.
* Copyright (C) 2009 Alexandre Julliard
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -14,9 +16,6 @@ ...@@ -14,9 +16,6 @@
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* This file implements statistics getting using the /proc filesystem exported
* by Linux, and maybe other OSes.
*/ */
#include "config.h" #include "config.h"
...@@ -1480,72 +1479,71 @@ DWORD getNumUdpEntries(void) ...@@ -1480,72 +1479,71 @@ DWORD getNumUdpEntries(void)
#endif #endif
} }
DWORD getUdpTable(PMIB_UDPTABLE *ppUdpTable, HANDLE heap, DWORD flags) static MIB_UDPTABLE *append_udp_row( HANDLE heap, DWORD flags, MIB_UDPTABLE *table,
DWORD *count, const MIB_UDPROW *row )
{ {
DWORD ret; if (table->dwNumEntries >= *count)
{
MIB_UDPTABLE *new_table;
DWORD new_count = table->dwNumEntries * 2;
#if defined(HAVE_SYS_SYSCTL_H) && defined(NET_RT_DUMP) if (!(new_table = HeapReAlloc( heap, flags, table, FIELD_OFFSET(MIB_UDPTABLE, table[new_count] ))))
ERR ("unimplemented!\n"); {
return ERROR_NOT_SUPPORTED; HeapFree( heap, 0, table );
#endif return NULL;
}
*count = new_count;
table = new_table;
}
memcpy( &table->table[table->dwNumEntries++], row, sizeof(*row) );
return table;
}
if (!ppUdpTable) DWORD getUdpTable(PMIB_UDPTABLE *ppUdpTable, HANDLE heap, DWORD flags)
ret = ERROR_INVALID_PARAMETER; {
else { MIB_UDPTABLE *table;
DWORD numEntries = getNumUdpEntries(); MIB_UDPROW row;
DWORD size = sizeof(MIB_UDPTABLE); DWORD ret = NO_ERROR, count = 16;
PMIB_UDPTABLE table;
if (numEntries > 1) if (!ppUdpTable) return ERROR_INVALID_PARAMETER;
size += (numEntries - 1) * sizeof(MIB_UDPROW);
table = HeapAlloc(heap, flags, size);
if (table) {
FILE *fp;
ret = NO_ERROR; if (!(table = HeapAlloc( heap, flags, FIELD_OFFSET(MIB_UDPTABLE, table[count] ))))
*ppUdpTable = table; return ERROR_OUTOFMEMORY;
table->dwNumEntries = 0;
/* get from /proc/net/udp, no error if can't */
fp = fopen("/proc/net/udp", "r");
if (fp) {
char buf[512] = { 0 }, *ptr;
/* skip header line */ table->dwNumEntries = 0;
ptr = fgets(buf, sizeof(buf), fp);
while (ptr && table->dwNumEntries < numEntries) {
memset(&table->table[table->dwNumEntries], 0, sizeof(MIB_UDPROW));
ptr = fgets(buf, sizeof(buf), fp);
if (ptr) {
char *endPtr;
if (ptr && *ptr) { #ifdef __linux__
strtoul(ptr, &endPtr, 16); /* skip */ {
ptr = endPtr; FILE *fp;
}
if (ptr && *ptr) { if ((fp = fopen("/proc/net/udp", "r")))
ptr++; {
table->table[table->dwNumEntries].dwLocalAddr = strtoul(ptr, char buf[512], *ptr;
&endPtr, 16); DWORD dummy;
ptr = endPtr;
} /* skip header line */
if (ptr && *ptr) { ptr = fgets(buf, sizeof(buf), fp);
ptr++; while ((ptr = fgets(buf, sizeof(buf), fp)))
table->table[table->dwNumEntries].dwLocalPort = strtoul(ptr, {
&endPtr, 16); if (sscanf( ptr, "%u: %x:%x", &dummy, &row.dwLocalAddr, &row.dwLocalPort ) != 3)
ptr = endPtr; continue;
row.dwLocalPort = htons( row.dwLocalPort );
if (!(table = append_udp_row( heap, flags, table, &count, &row )))
break;
} }
table->dwNumEntries++; fclose(fp);
}
} }
fclose(fp); else ret = ERROR_NOT_SUPPORTED;
}
else
ret = ERROR_NOT_SUPPORTED;
} }
else #else
ret = ERROR_OUTOFMEMORY; FIXME( "not implemented\n" );
} ret = ERROR_NOT_SUPPORTED;
return ret; #endif
if (!table) return ERROR_OUTOFMEMORY;
if (!ret) *ppUdpTable = table;
else HeapFree( heap, flags, table );
return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment