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

Alexandre Julliard julliard at winehq.org
Thu Sep 19 17:21:06 CDT 2013


Module: wine
Branch: master
Commit: 8876c3ee820a7c0ef6662ef6c81f45bab0b204ae
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=8876c3ee820a7c0ef6662ef6c81f45bab0b204ae

Author: Huw Davies <huw at codeweavers.com>
Date:   Thu Sep 19 15:39:20 2013 +0100

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

---

 dlls/iphlpapi/ifenum.c        |  119 ++++++++++-------------------------------
 dlls/iphlpapi/ifenum.h        |   13 ++---
 dlls/iphlpapi/iphlpapi_main.c |   18 ++++---
 dlls/iphlpapi/ipstats.c       |    4 +-
 4 files changed, 45 insertions(+), 109 deletions(-)

diff --git a/dlls/iphlpapi/ifenum.c b/dlls/iphlpapi/ifenum.c
index d8eebca..6ad08a8 100644
--- a/dlls/iphlpapi/ifenum.c
+++ b/dlls/iphlpapi/ifenum.c
@@ -161,103 +161,42 @@ BOOL isIfIndexLoopback(ULONG idx)
   return ret;
 }
 
-DWORD getNumNonLoopbackInterfaces(void)
-{
-  DWORD numInterfaces;
-  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++;
-      if_freenameindex(indexes);
-    }
-    else
-      numInterfaces = 0;
-    close(fd);
-  }
-  else
-    numInterfaces = 0;
-  return numInterfaces;
-}
 
-DWORD getNumInterfaces(void)
+DWORD get_interface_indices( BOOL skip_loopback, InterfaceIndexTable **table )
 {
-  DWORD numInterfaces;
-  struct if_nameindex *indexes = if_nameindex();
+    DWORD count = 0, i;
+    struct if_nameindex *p, *indices = if_nameindex();
+    InterfaceIndexTable *ret;
 
-  if (indexes) {
-    struct if_nameindex *p;
+    if (table) *table = NULL;
+    if (!indices) return 0;
 
-    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;
+    for (p = indices; p->if_name; p++)
+    {
+        if (skip_loopback && isIfIndexLoopback( p->if_index )) continue;
+        count++;
     }
-    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);
+    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;
     }
-    else
-      ret = NULL;
-    close(fd);
-  }
-  else
-    ret = NULL;
-  return ret;
+
+end:
+    if_freenameindex( indices );
+    return count;
 }
 
 static DWORD getInterfaceBCastAddrByName(const char *name)
diff --git a/dlls/iphlpapi/ifenum.h b/dlls/iphlpapi/ifenum.h
index 304f0db..49b6e7b 100644
--- a/dlls/iphlpapi/ifenum.h
+++ b/dlls/iphlpapi/ifenum.h
@@ -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. */
 
diff --git a/dlls/iphlpapi/iphlpapi_main.c b/dlls/iphlpapi/iphlpapi_main.c
index e2d09eb..9d23c82 100644
--- a/dlls/iphlpapi/iphlpapi_main.c
+++ b/dlls/iphlpapi/iphlpapi_main.c
@@ -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);
diff --git a/dlls/iphlpapi/ipstats.c b/dlls/iphlpapi/ipstats.c
index 1185f08..3ff30f3 100644
--- a/dlls/iphlpapi/ipstats.c
+++ b/dlls/iphlpapi/ipstats.c
@@ -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)
     {




More information about the wine-cvs mailing list