Commit 95298b44 authored by Bruno Jesus's avatar Bruno Jesus Committed by Alexandre Julliard

iphlpapi: Ensure GetIpAddrTable returns loopback addresses in the end.

parent 61cb24f4
...@@ -134,17 +134,27 @@ DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable, ...@@ -134,17 +134,27 @@ DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
} }
static int IpAddrTableSorter(const void *a, const void *b) static int IpAddrTableNumericSorter(const void *a, const void *b)
{ {
int ret; int ret = 0;
if (a && b) if (a && b)
ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr; ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
else
ret = 0;
return ret; return ret;
} }
static int IpAddrTableLoopbackSorter(const void *a, const void *b)
{
const MIB_IPADDRROW *left = a, *right = b;
int ret = 0;
if (isIfIndexLoopback(left->dwIndex))
ret = 1;
else if (isIfIndexLoopback(right->dwIndex))
ret = -1;
return ret;
}
/****************************************************************** /******************************************************************
* AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@) * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
...@@ -173,7 +183,7 @@ DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable, ...@@ -173,7 +183,7 @@ DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
ret = getIPAddrTable(ppIpAddrTable, heap, flags); ret = getIPAddrTable(ppIpAddrTable, heap, flags);
if (!ret && bOrder) if (!ret && bOrder)
qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries, qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
sizeof(MIB_IPADDRROW), IpAddrTableSorter); sizeof(MIB_IPADDRROW), IpAddrTableNumericSorter);
TRACE("returning %d\n", ret); TRACE("returning %d\n", ret);
return ret; return ret;
} }
...@@ -1988,9 +1998,14 @@ DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL ...@@ -1988,9 +1998,14 @@ DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL
else { else {
*pdwSize = size; *pdwSize = size;
memcpy(pIpAddrTable, table, size); memcpy(pIpAddrTable, table, size);
/* sort by numeric IP value */
if (bOrder) if (bOrder)
qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries, qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
sizeof(MIB_IPADDRROW), IpAddrTableSorter); sizeof(MIB_IPADDRROW), IpAddrTableNumericSorter);
/* sort ensuring loopback interfaces are in the end */
else
qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
sizeof(MIB_IPADDRROW), IpAddrTableLoopbackSorter);
ret = NO_ERROR; ret = NO_ERROR;
} }
HeapFree(GetProcessHeap(), 0, table); HeapFree(GetProcessHeap(), 0, table);
......
...@@ -261,6 +261,9 @@ static void testGetIpAddrTable(void) ...@@ -261,6 +261,9 @@ static void testGetIpAddrTable(void)
ok (buf->table[i].wType != 0, "Test[%d]: expected wType > 0\n", i); ok (buf->table[i].wType != 0, "Test[%d]: expected wType > 0\n", i);
trace("Entry[%d]: addr %s, dwIndex %u, wType 0x%x\n", i, trace("Entry[%d]: addr %s, dwIndex %u, wType 0x%x\n", i,
ntoa(buf->table[i].dwAddr), buf->table[i].dwIndex, buf->table[i].wType); ntoa(buf->table[i].dwAddr), buf->table[i].dwIndex, buf->table[i].wType);
/* loopback must never be the first when more than one interface is found */
if (buf->table[i].dwAddr == htonl(INADDR_LOOPBACK))
ok(buf->dwNumEntries == 1 || i, "Loopback interface in wrong first position\n");
} }
} }
HeapFree(GetProcessHeap(), 0, buf); HeapFree(GetProcessHeap(), 0, buf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment