summaryrefslogtreecommitdiffstats
path: root/src/common/impl/netif_windows.c
blob: 7531ae06405f2ac28038c452b7680e5a48bfe94a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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;
}