[PATCH 2/6] iphlpapi: Implement GetAdapterIndex() on top of GetIfTable().
Huw Davies
huw at codeweavers.com
Fri Jul 9 03:09:24 CDT 2021
Eventually this should just parse the Guid in the name. This is an
intermediate step to keep the interface names in sync.
Signed-off-by: Huw Davies <huw at codeweavers.com>
---
dlls/iphlpapi/iphlpapi_main.c | 37 ++++++++++++++++++++--------------
dlls/iphlpapi/tests/iphlpapi.c | 36 ++++++++++++++++++---------------
2 files changed, 42 insertions(+), 31 deletions(-)
diff --git a/dlls/iphlpapi/iphlpapi_main.c b/dlls/iphlpapi/iphlpapi_main.c
index 7c537dc3f27..ff534afa0f9 100644
--- a/dlls/iphlpapi/iphlpapi_main.c
+++ b/dlls/iphlpapi/iphlpapi_main.c
@@ -628,28 +628,35 @@ void WINAPI FreeMibTable(void *ptr)
* Get interface index from its name.
*
* PARAMS
- * AdapterName [In] unicode string with the adapter name
- * IfIndex [Out] returns found interface index
+ * adapter_name [In] unicode string with the adapter name
+ * index [Out] returns found interface index
*
* RETURNS
* Success: NO_ERROR
* Failure: error code from winerror.h
*/
-DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
+DWORD WINAPI GetAdapterIndex( WCHAR *adapter_name, ULONG *index )
{
- char adapterName[MAX_ADAPTER_NAME];
- unsigned int i;
- DWORD ret;
+ MIB_IFTABLE *if_table;
+ DWORD err, i;
- TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
- /* The adapter name is guaranteed not to have any unicode characters, so
- * this translation is never lossy */
- for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
- adapterName[i] = (char)AdapterName[i];
- adapterName[i] = '\0';
- ret = getInterfaceIndexByName(adapterName, IfIndex);
- TRACE("returning %d\n", ret);
- return ret;
+ TRACE( "name %s, index %p\n", debugstr_w( adapter_name ), index );
+
+ err = AllocateAndGetIfTableFromStack( &if_table, 0, GetProcessHeap(), 0 );
+ if (err) return err;
+
+ err = ERROR_INVALID_PARAMETER;
+ for (i = 0; i < if_table->dwNumEntries; i++)
+ {
+ if (!strcmpW( adapter_name, if_table->table[i].wszName ))
+ {
+ *index = if_table->table[i].dwIndex;
+ err = ERROR_SUCCESS;
+ break;
+ }
+ }
+ heap_free( if_table );
+ return err;
}
diff --git a/dlls/iphlpapi/tests/iphlpapi.c b/dlls/iphlpapi/tests/iphlpapi.c
index 61406090839..784eafbd1ef 100644
--- a/dlls/iphlpapi/tests/iphlpapi.c
+++ b/dlls/iphlpapi/tests/iphlpapi.c
@@ -236,27 +236,31 @@ static void testGetIfTable(void)
"GetIfTable(buf, &dwSize, FALSE) returned %d, expected NO_ERROR\n\n",
apiReturn);
- if (apiReturn == NO_ERROR && winetest_debug > 1)
+ if (apiReturn == NO_ERROR)
{
- DWORD i, j;
- char name[MAX_INTERFACE_NAME_LEN];
+ DWORD i, index;
- trace( "interface table: %u entries\n", buf->dwNumEntries );
+ if (winetest_debug > 1) trace( "interface table: %u entries\n", buf->dwNumEntries );
for (i = 0; i < buf->dwNumEntries; i++)
{
MIB_IFROW *row = &buf->table[i];
- WideCharToMultiByte( CP_ACP, 0, row->wszName, -1, name, MAX_INTERFACE_NAME_LEN, NULL, NULL );
- trace( "%u: '%s' type %u mtu %u speed %u phys",
- row->dwIndex, name, row->dwType, row->dwMtu, row->dwSpeed );
- for (j = 0; j < row->dwPhysAddrLen; j++)
- printf( " %02x", row->bPhysAddr[j] );
- printf( "\n" );
- trace( " in: bytes %u upkts %u nupkts %u disc %u err %u unk %u\n",
- row->dwInOctets, row->dwInUcastPkts, row->dwInNUcastPkts,
- row->dwInDiscards, row->dwInErrors, row->dwInUnknownProtos );
- trace( " out: bytes %u upkts %u nupkts %u disc %u err %u\n",
- row->dwOutOctets, row->dwOutUcastPkts, row->dwOutNUcastPkts,
- row->dwOutDiscards, row->dwOutErrors );
+
+ if (winetest_debug > 1)
+ {
+ trace( "%u: '%s' type %u mtu %u speed %u\n",
+ row->dwIndex, debugstr_w(row->wszName), row->dwType, row->dwMtu, row->dwSpeed );
+ trace( " in: bytes %u upkts %u nupkts %u disc %u err %u unk %u\n",
+ row->dwInOctets, row->dwInUcastPkts, row->dwInNUcastPkts,
+ row->dwInDiscards, row->dwInErrors, row->dwInUnknownProtos );
+ trace( " out: bytes %u upkts %u nupkts %u disc %u err %u\n",
+ row->dwOutOctets, row->dwOutUcastPkts, row->dwOutNUcastPkts,
+ row->dwOutDiscards, row->dwOutErrors );
+ }
+ apiReturn = GetAdapterIndex( row->wszName, &index );
+ ok( !apiReturn, "got %d\n", apiReturn );
+ ok( index == row->dwIndex ||
+ broken( index != row->dwIndex && index ), /* Win8 can have identical guids for two different ifaces */
+ "got %d vs %d\n", index, row->dwIndex );
}
}
HeapFree(GetProcessHeap(), 0, buf);
--
2.23.0
More information about the wine-devel
mailing list