[PATCH 2/4] iphlpapi: Implement AllocateAndGetIpAddrTableFromStack() on top of GetIpAddrTable().
Huw Davies
huw at codeweavers.com
Thu Jul 22 06:50:18 CDT 2021
Signed-off-by: Huw Davies <huw at codeweavers.com>
---
dlls/iphlpapi/iphlpapi_main.c | 76 ++++++++++++++++-------------------
1 file changed, 34 insertions(+), 42 deletions(-)
diff --git a/dlls/iphlpapi/iphlpapi_main.c b/dlls/iphlpapi/iphlpapi_main.c
index 8442bc2b4a9..6e1d95ab321 100644
--- a/dlls/iphlpapi/iphlpapi_main.c
+++ b/dlls/iphlpapi/iphlpapi_main.c
@@ -139,48 +139,6 @@ DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG N
return ERROR_NOT_SUPPORTED;
}
-static int IpAddrTableNumericSorter(const void *a, const void *b)
-{
- int ret = 0;
-
- if (a && b)
- ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
- return ret;
-}
-
-/******************************************************************
- * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
- *
- * Get interface-to-IP address mapping table.
- * Like GetIpAddrTable(), but allocate the returned table from heap.
- *
- * PARAMS
- * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE 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 ppIpAddrTable is NULL, other error codes on
- * failure, NO_ERROR on success.
- */
-DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
- BOOL bOrder, HANDLE heap, DWORD flags)
-{
- DWORD ret;
-
- TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
- ppIpAddrTable, bOrder, heap, flags);
- ret = getIPAddrTable(ppIpAddrTable, heap, flags);
- if (!ret && bOrder)
- qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
- sizeof(MIB_IPADDRROW), IpAddrTableNumericSorter);
- TRACE("returning %d\n", ret);
- return ret;
-}
-
-
/******************************************************************
* CancelIPChangeNotify (IPHLPAPI.@)
*
@@ -2189,6 +2147,40 @@ err:
}
+/******************************************************************
+ * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
+ *
+ * Get interface-to-IP address mapping table.
+ * Like GetIpAddrTable(), but allocate the returned table from heap.
+ *
+ * PARAMS
+ * table [Out] pointer into which the MIB_IPADDRTABLE is
+ * allocated and returned.
+ * sort [In] whether to sort the table
+ * heap [In] heap from which the table is allocated
+ * flags [In] flags to HeapAlloc
+ *
+ */
+DWORD WINAPI AllocateAndGetIpAddrTableFromStack( MIB_IPADDRTABLE **table, BOOL sort, HANDLE heap, DWORD flags )
+{
+ DWORD err, size = FIELD_OFFSET(MIB_IPADDRTABLE, table[2]), attempt;
+
+ TRACE( "table %p, sort %d, heap %p, flags 0x%08x\n", table, sort, heap, flags );
+
+ for (attempt = 0; attempt < 5; attempt++)
+ {
+ *table = HeapAlloc( heap, flags, size );
+ if (!*table) return ERROR_NOT_ENOUGH_MEMORY;
+
+ err = GetIpAddrTable( *table, &size, sort );
+ if (!err) break;
+ HeapFree( heap, flags, *table );
+ if (err != ERROR_INSUFFICIENT_BUFFER) break;
+ }
+
+ return err;
+}
+
/******************************************************************
* GetIpForwardTable (IPHLPAPI.@)
*
--
2.25.1
More information about the wine-devel
mailing list