|
1 | 1 | #include "netif.h" |
2 | 2 | #include "util/mallocHelper.h" |
3 | 3 |
|
| 4 | +#include <ws2tcpip.h> // AF_INET6, IN6_IS_ADDR_UNSPECIFIED |
4 | 5 | #include <iphlpapi.h> |
5 | 6 |
|
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) |
7 | 8 | { |
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); |
11 | 11 |
|
12 | | - FF_AUTO_FREE MIB_IPFORWARDTABLE* pIpForwardTable = (MIB_IPFORWARDTABLE*) malloc(size); |
13 | | - if (GetIpForwardTable(pIpForwardTable, &size, TRUE) != ERROR_SUCCESS) |
| 12 | + if (result != NO_ERROR) |
14 | 13 | return false; |
15 | 14 |
|
16 | | - for (uint32_t i = 0; i < pIpForwardTable->dwNumEntries; ++i) |
| 15 | + bool foundDefault = false; |
| 16 | + |
| 17 | + for (ULONG i = 0; i < pIpForwardTable->NumEntries; ++i) |
17 | 18 | { |
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)))) |
20 | 26 | { |
21 | | - *ifIndex = ipForwardRow->dwForwardIfIndex; |
| 27 | + *ifIndex = row->InterfaceIndex; |
| 28 | + foundDefault = true; |
22 | 29 | break; |
23 | 30 | } |
24 | 31 | } |
25 | 32 |
|
26 | | - return true; |
| 33 | + FreeMibTable(pIpForwardTable); |
| 34 | + |
| 35 | + return foundDefault; |
27 | 36 | } |
0 commit comments