Skip to content

Commit 840434e

Browse files
committed
Netif (Windows): use GetIpForwardTable2 to support IPv6
1 parent 72afa9e commit 840434e

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/common/netif/netif.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#ifndef _WIN32
44
#include <net/if.h>
55
#else
6-
#define IF_NAMESIZE 16
6+
#define IF_NAMESIZE 0
77
#endif
88

99
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex);

src/common/netif/netif_windows.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
#include "netif.h"
22
#include "util/mallocHelper.h"
33

4+
#include <ws2tcpip.h> // AF_INET6, IN6_IS_ADDR_UNSPECIFIED
45
#include <iphlpapi.h>
56

6-
bool ffNetifGetDefaultRouteImpl(FF_MAYBE_UNUSED char* iface/* unsupported */, uint32_t* ifIndex)
7+
bool ffNetifGetDefaultRouteImpl(FF_MAYBE_UNUSED char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
78
{
8-
ULONG size = 0;
9-
if (GetIpForwardTable(NULL, &size, TRUE) != ERROR_INSUFFICIENT_BUFFER)
10-
return false;
9+
PMIB_IPFORWARD_TABLE2 pIpForwardTable = NULL;
10+
DWORD result = GetIpForwardTable2(AF_UNSPEC, &pIpForwardTable);
1111

12-
FF_AUTO_FREE MIB_IPFORWARDTABLE* pIpForwardTable = (MIB_IPFORWARDTABLE*) malloc(size);
13-
if (GetIpForwardTable(pIpForwardTable, &size, TRUE) != ERROR_SUCCESS)
12+
if (result != NO_ERROR)
1413
return false;
1514

16-
for (uint32_t i = 0; i < pIpForwardTable->dwNumEntries; ++i)
15+
bool foundDefault = false;
16+
17+
for (ULONG i = 0; i < pIpForwardTable->NumEntries; ++i)
1718
{
18-
MIB_IPFORWARDROW* ipForwardRow = &pIpForwardTable->table[i];
19-
if (ipForwardRow->dwForwardDest == 0 && ipForwardRow->dwForwardMask == 0)
19+
MIB_IPFORWARD_ROW2* row = &pIpForwardTable->Table[i];
20+
21+
if ((row->DestinationPrefix.PrefixLength == 0) &&
22+
((row->DestinationPrefix.Prefix.Ipv4.sin_family == AF_INET &&
23+
row->DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr == 0) ||
24+
(row->DestinationPrefix.Prefix.Ipv6.sin6_family == AF_INET6 &&
25+
IN6_IS_ADDR_UNSPECIFIED(&row->DestinationPrefix.Prefix.Ipv6.sin6_addr))))
2026
{
21-
*ifIndex = ipForwardRow->dwForwardIfIndex;
27+
*ifIndex = row->InterfaceIndex;
28+
foundDefault = true;
2229
break;
2330
}
2431
}
2532

26-
return true;
33+
FreeMibTable(pIpForwardTable);
34+
35+
return foundDefault;
2736
}

0 commit comments

Comments
 (0)