summaryrefslogtreecommitdiffstats
path: root/src/core/network/network_interface.cpp
blob: 75f4dc54fbbb843658c31d757e40cd30bedc4c0d (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2021 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <vector>

#include "common/bit_cast.h"
#include "common/common_types.h"
#include "common/logging/log.h"
#include "common/string_util.h"
#include "core/network/network_interface.h"

#ifdef _WIN32
#include <iphlpapi.h>
#else
#include <cerrno>
#include <ifaddrs.h>
#include <net/if.h>
#endif

namespace Network {

#ifdef _WIN32

std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
    std::vector<IP_ADAPTER_ADDRESSES> adapter_addresses;
    DWORD ret = ERROR_BUFFER_OVERFLOW;
    DWORD buf_size = 0;

    // retry up to 5 times
    for (int i = 0; i < 5 && ret == ERROR_BUFFER_OVERFLOW; i++) {
        ret = GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
                                   nullptr, adapter_addresses.data(), &buf_size);

        if (ret == ERROR_BUFFER_OVERFLOW) {
            adapter_addresses.resize((buf_size / sizeof(IP_ADAPTER_ADDRESSES)) + 1);
        } else {
            break;
        }
    }

    if (ret == NO_ERROR) {
        std::vector<NetworkInterface> result;

        for (auto current_address = adapter_addresses.data(); current_address != nullptr;
             current_address = current_address->Next) {
            if (current_address->FirstUnicastAddress == nullptr ||
                current_address->FirstUnicastAddress->Address.lpSockaddr == nullptr) {
                continue;
            }

            if (current_address->OperStatus != IfOperStatusUp) {
                continue;
            }

            const auto ip_addr = Common::BitCast<struct sockaddr_in>(
                                     *current_address->FirstUnicastAddress->Address.lpSockaddr)
                                     .sin_addr;

            result.push_back(NetworkInterface{
                .name{Common::UTF16ToUTF8(std::wstring{current_address->FriendlyName})},
                .ip_address{ip_addr}});
        }

        return result;
    } else {
        LOG_ERROR(Network, "Failed to get network interfaces with GetAdaptersAddresses");
        return {};
    }
}

#else

std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
    std::vector<NetworkInterface> result;

    struct ifaddrs* ifaddr = nullptr;

    if (getifaddrs(&ifaddr) != 0) {
        LOG_ERROR(Network, "Failed to get network interfaces with getifaddrs: {}",
                  std::strerror(errno));
        return result;
    }

    for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
        if (ifa->ifa_addr == nullptr) {
            continue;
        }

        if (ifa->ifa_addr->sa_family != AF_INET) {
            continue;
        }

        if ((ifa->ifa_flags & IFF_UP) == 0 || (ifa->ifa_flags & IFF_LOOPBACK) != 0) {
            continue;
        }

        result.push_back(NetworkInterface{
            .name{ifa->ifa_name},
            .ip_address{Common::BitCast<struct sockaddr_in>(*ifa->ifa_addr).sin_addr}});
    }

    freeifaddrs(ifaddr);

    return result;
}

#endif

} // namespace Network