diff options
| author | sumuel <samuel@yakubos.org> | 2026-08-17 20:44:55 +0000 |
|---|---|---|
| committer | sumuel <samuel@yakubos.org> | 2026-08-17 20:44:55 +0000 |
| commit | 76424950e373d3b04ac3dd13019151bfba3e8423 (patch) | |
| tree | 4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/common/impl/netif_windows.c | |
Add the files
Diffstat (limited to 'src/common/impl/netif_windows.c')
| -rw-r--r-- | src/common/impl/netif_windows.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/common/impl/netif_windows.c b/src/common/impl/netif_windows.c new file mode 100644 index 0000000..7531ae0 --- /dev/null +++ b/src/common/impl/netif_windows.c @@ -0,0 +1,92 @@ +#include "common/netif.h" + +#include <ws2tcpip.h> // AF_INET6, IN6_IS_ADDR_UNSPECIFIED +#include <iphlpapi.h> + +bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) { + PMIB_IPFORWARD_TABLE2 pIpForwardTable = NULL; + + if (!NETIO_SUCCESS(GetIpForwardTable2(AF_INET, &pIpForwardTable))) { + return false; + } + + bool foundDefault = false; + uint32_t smallestMetric = UINT32_MAX; + + for (ULONG i = 0; i < pIpForwardTable->NumEntries; ++i) { + MIB_IPFORWARD_ROW2* row = &pIpForwardTable->Table[i]; + + if (row->DestinationPrefix.PrefixLength == 0 && + row->DestinationPrefix.Prefix.Ipv4.sin_addr.S_un.S_addr == 0) { + MIB_IF_ROW2 ifRow = { + .InterfaceIndex = row->InterfaceIndex, + }; + if (NETIO_SUCCESS(GetIfEntry2(&ifRow)) && ifRow.OperStatus == IfOperStatusUp) { + MIB_IPINTERFACE_ROW ipInterfaceRow = { + .Family = AF_INET, + .InterfaceIndex = row->InterfaceIndex, + }; + + uint32_t realMetric = row->Metric /* Metric offset */; + + if (NETIO_SUCCESS(GetIpInterfaceEntry(&ipInterfaceRow))) { + realMetric += ipInterfaceRow.Metric /* Interface metric */; + } + + if (realMetric < smallestMetric) { + smallestMetric = realMetric; + result->ifIndex = row->InterfaceIndex; + foundDefault = true; + } + } + } + } + + FreeMibTable(pIpForwardTable); + + return foundDefault; +} + +bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) { + PMIB_IPFORWARD_TABLE2 pIpForwardTable = NULL; + + if (!NETIO_SUCCESS(GetIpForwardTable2(AF_INET6, &pIpForwardTable))) { + return false; + } + + bool foundDefault = false; + uint32_t smallestMetric = UINT32_MAX; + + for (ULONG i = 0; i < pIpForwardTable->NumEntries; ++i) { + MIB_IPFORWARD_ROW2* row = &pIpForwardTable->Table[i]; + + if (row->DestinationPrefix.PrefixLength == 0 && + IN6_IS_ADDR_UNSPECIFIED(&row->DestinationPrefix.Prefix.Ipv6.sin6_addr)) { + MIB_IF_ROW2 ifRow = { + .InterfaceIndex = row->InterfaceIndex, + }; + if (NETIO_SUCCESS(GetIfEntry2(&ifRow)) && ifRow.OperStatus == IfOperStatusUp) { + MIB_IPINTERFACE_ROW ipInterfaceRow = { + .Family = AF_INET6, + .InterfaceIndex = row->InterfaceIndex, + }; + + uint32_t realMetric = row->Metric /* Metric offset */; + + if (NETIO_SUCCESS(GetIpInterfaceEntry(&ipInterfaceRow))) { + realMetric += ipInterfaceRow.Metric /* Interface metric */; + } + + if (realMetric < smallestMetric) { + smallestMetric = realMetric; + result->ifIndex = row->InterfaceIndex; + foundDefault = true; + } + } + } + } + + FreeMibTable(pIpForwardTable); + + return foundDefault; +} |