iphlpapi: Combine the various interface enumeration functions into one function.

This commit is contained in:
Huw Davies 2013-09-19 15:39:20 +01:00 committed by Alexandre Julliard
parent 8e74c8953a
commit 8876c3ee82
4 changed files with 45 additions and 109 deletions

View File

@ -161,103 +161,42 @@ BOOL isIfIndexLoopback(ULONG idx)
return ret;
}
DWORD getNumNonLoopbackInterfaces(void)
DWORD get_interface_indices( BOOL skip_loopback, InterfaceIndexTable **table )
{
DWORD numInterfaces;
int fd = socket(PF_INET, SOCK_DGRAM, 0);
DWORD count = 0, i;
struct if_nameindex *p, *indices = if_nameindex();
InterfaceIndexTable *ret;
if (fd != -1) {
struct if_nameindex *indexes = if_nameindex();
if (table) *table = NULL;
if (!indices) return 0;
if (indexes) {
struct if_nameindex *p;
for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
if (!isLoopbackInterface(fd, p->if_name))
numInterfaces++;
if_freenameindex(indexes);
for (p = indices; p->if_name; p++)
{
if (skip_loopback && isIfIndexLoopback( p->if_index )) continue;
count++;
}
else
numInterfaces = 0;
close(fd);
}
else
numInterfaces = 0;
return numInterfaces;
}
DWORD getNumInterfaces(void)
{
DWORD numInterfaces;
struct if_nameindex *indexes = if_nameindex();
if (indexes) {
struct if_nameindex *p;
for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
numInterfaces++;
if_freenameindex(indexes);
}
else
numInterfaces = 0;
return numInterfaces;
}
InterfaceIndexTable *getInterfaceIndexTable(void)
{
DWORD numInterfaces;
InterfaceIndexTable *ret;
struct if_nameindex *indexes = if_nameindex();
if (indexes) {
struct if_nameindex *p;
for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
numInterfaces++;
ret = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceIndexTable, indexes[numInterfaces]));
if (ret) {
ret->numIndexes = 0;
for (p = indexes; p && p->if_name; p++)
ret->indexes[ret->numIndexes++] = p->if_index;
if (table)
{
ret = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(InterfaceIndexTable, indexes[count]) );
if (!ret)
{
count = 0;
goto end;
}
for (p = indices, i = 0; p->if_name && i < count; p++)
{
if (skip_loopback && isIfIndexLoopback( p->if_index )) continue;
ret->indexes[i++] = p->if_index;
}
ret->numIndexes = count = i;
*table = ret;
}
if_freenameindex(indexes);
}
else
ret = NULL;
return ret;
}
InterfaceIndexTable *getNonLoopbackInterfaceIndexTable(void)
{
DWORD numInterfaces;
InterfaceIndexTable *ret;
int fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd != -1) {
struct if_nameindex *indexes = if_nameindex();
if (indexes) {
struct if_nameindex *p;
for (p = indexes, numInterfaces = 0; p && p->if_name; p++)
if (!isLoopbackInterface(fd, p->if_name))
numInterfaces++;
ret = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceIndexTable, indexes[numInterfaces]));
if (ret) {
ret->numIndexes = 0;
for (p = indexes; p && p->if_name; p++)
if (!isLoopbackInterface(fd, p->if_name))
ret->indexes[ret->numIndexes++] = p->if_index;
}
if_freenameindex(indexes);
}
else
ret = NULL;
close(fd);
}
else
ret = NULL;
return ret;
end:
if_freenameindex( indices );
return count;
}
static DWORD getInterfaceBCastAddrByName(const char *name)

View File

@ -44,23 +44,18 @@
#define MAX_INTERFACE_PHYSADDR 8
#define MAX_INTERFACE_DESCRIPTION 256
DWORD getNumInterfaces(void) DECLSPEC_HIDDEN;
DWORD getNumNonLoopbackInterfaces(void) DECLSPEC_HIDDEN;
BOOL isIfIndexLoopback(ULONG idx) DECLSPEC_HIDDEN;
/* A table of interface indexes, see get*InterfaceTable(). */
/* A table of interface indexes, see get_interface_indices(). */
typedef struct _InterfaceIndexTable {
DWORD numIndexes;
IF_INDEX indexes[1];
} InterfaceIndexTable;
/* Returns a table with all known interface indexes, or NULL if one could not
* be allocated. HeapFree() the returned table.
/* Returns the count of all interface indexes and optionally a ptr to an interface table.
* HeapFree() the returned table. Will optionally ignore loopback devices.
*/
InterfaceIndexTable *getInterfaceIndexTable(void) DECLSPEC_HIDDEN;
/* Like getInterfaceIndexTable, but filters out loopback interfaces. */
InterfaceIndexTable *getNonLoopbackInterfaceIndexTable(void) DECLSPEC_HIDDEN;
DWORD get_interface_indices( BOOL skip_loopback, InterfaceIndexTable **table ) DECLSPEC_HIDDEN;
/* ByName/ByIndex versions of various getter functions. */

View File

@ -463,7 +463,7 @@ DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
if (!pOutBufLen)
ret = ERROR_INVALID_PARAMETER;
else {
DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
DWORD numNonLoopbackInterfaces = get_interface_indices( TRUE, NULL );
if (numNonLoopbackInterfaces > 0) {
DWORD numIPAddresses = getNumIPAddresses();
@ -489,7 +489,7 @@ DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
if (!ret)
ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
if (!ret)
table = getNonLoopbackInterfaceIndexTable();
get_interface_indices( TRUE, &table );
if (table) {
size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
@ -1129,7 +1129,7 @@ ULONG WINAPI DECLSPEC_HOTPATCH GetAdaptersAddresses(ULONG family, ULONG flags, P
if (!buflen) return ERROR_INVALID_PARAMETER;
table = getInterfaceIndexTable();
get_interface_indices( FALSE, &table );
if (!table || !table->numIndexes)
{
HeapFree(GetProcessHeap(), 0, table);
@ -1431,7 +1431,7 @@ DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
if (!pdwSize)
ret = ERROR_INVALID_PARAMETER;
else {
DWORD numInterfaces = getNumInterfaces();
DWORD numInterfaces = get_interface_indices( FALSE, NULL );
ULONG size = sizeof(MIB_IFTABLE);
if (numInterfaces > 1)
@ -1441,7 +1441,8 @@ DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
ret = ERROR_INSUFFICIENT_BUFFER;
}
else {
InterfaceIndexTable *table = getInterfaceIndexTable();
InterfaceIndexTable *table;
get_interface_indices( FALSE, &table );
if (table) {
size = sizeof(MIB_IFTABLE);
@ -1501,7 +1502,7 @@ DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
if (!dwOutBufLen)
ret = ERROR_INVALID_PARAMETER;
else {
DWORD numInterfaces = getNumInterfaces();
DWORD numInterfaces = get_interface_indices( FALSE, NULL );
ULONG size = sizeof(IP_INTERFACE_INFO);
if (numInterfaces > 1)
@ -1511,7 +1512,8 @@ DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
ret = ERROR_INSUFFICIENT_BUFFER;
}
else {
InterfaceIndexTable *table = getInterfaceIndexTable();
InterfaceIndexTable *table;
get_interface_indices( FALSE, &table );
if (table) {
size = sizeof(IP_INTERFACE_INFO);
@ -1837,7 +1839,7 @@ DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
if (!pdwNumIf)
ret = ERROR_INVALID_PARAMETER;
else {
*pdwNumIf = getNumInterfaces();
*pdwNumIf = get_interface_indices( FALSE, NULL );
ret = NO_ERROR;
}
TRACE("returning %d\n", ret);

View File

@ -763,7 +763,7 @@ DWORD WINAPI GetIpStatisticsEx(PMIB_IPSTATS stats, DWORD family)
if (family != WS_AF_INET && family != WS_AF_INET6) return ERROR_INVALID_PARAMETER;
memset( stats, 0, sizeof(*stats) );
stats->dwNumIf = stats->dwNumAddr = getNumInterfaces();
stats->dwNumIf = stats->dwNumAddr = get_interface_indices( FALSE, NULL );
if (!AllocateAndGetIpForwardTableFromStack( &fwd_table, FALSE, GetProcessHeap(), 0 ))
{
stats->dwNumRoutes = fwd_table->dwNumEntries;
@ -1174,7 +1174,7 @@ DWORD WINAPI GetUdpStatisticsEx(PMIB_UDPSTATS stats, DWORD family)
if (family != WS_AF_INET && family != WS_AF_INET6) return ERROR_INVALID_PARAMETER;
memset( stats, 0, sizeof(*stats) );
stats->dwNumAddrs = getNumInterfaces();
stats->dwNumAddrs = get_interface_indices( FALSE, NULL );
if (family == WS_AF_INET6)
{