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

View File

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

View File

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