committed by
GitHub
16 changed files with 409 additions and 103 deletions
-
2CMakeLists.txt
-
3src/common/settings.h
-
2src/core/CMakeLists.txt
-
45src/core/hle/service/nifm/nifm.cpp
-
64src/core/network/network.cpp
-
24src/core/network/network.h
-
203src/core/network/network_interface.cpp
-
29src/core/network/network_interface.h
-
6src/yuzu/CMakeLists.txt
-
6src/yuzu/configuration/config.cpp
-
2src/yuzu/configuration/config.h
-
10src/yuzu/configuration/configure.ui
-
4src/yuzu/configuration/configure_dialog.cpp
-
39src/yuzu/configuration/configure_network.cpp
-
10src/yuzu/configuration/configure_network.h
-
63src/yuzu/configuration/configure_network.ui
@ -0,0 +1,203 @@ |
|||
// Copyright 2021 yuzu emulator team
|
|||
// Licensed under GPLv2 or any later version
|
|||
// Refer to the license.txt file included.
|
|||
|
|||
#include <algorithm>
|
|||
#include <fstream>
|
|||
#include <sstream>
|
|||
#include <vector>
|
|||
|
|||
#include "common/bit_cast.h"
|
|||
#include "common/common_types.h"
|
|||
#include "common/logging/log.h"
|
|||
#include "common/settings.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 | GAA_FLAG_INCLUDE_GATEWAYS, |
|||
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; |
|||
|
|||
ULONG mask = 0; |
|||
if (ConvertLengthToIpv4Mask(current_address->FirstUnicastAddress->OnLinkPrefixLength, |
|||
&mask) != NO_ERROR) { |
|||
LOG_ERROR(Network, "Failed to convert IPv4 prefix length to subnet mask"); |
|||
continue; |
|||
} |
|||
|
|||
struct in_addr gateway = {.S_un{.S_addr{0}}}; |
|||
if (current_address->FirstGatewayAddress != nullptr && |
|||
current_address->FirstGatewayAddress->Address.lpSockaddr != nullptr) { |
|||
gateway = Common::BitCast<struct sockaddr_in>( |
|||
*current_address->FirstGatewayAddress->Address.lpSockaddr) |
|||
.sin_addr; |
|||
} |
|||
|
|||
result.push_back(NetworkInterface{ |
|||
.name{Common::UTF16ToUTF8(std::wstring{current_address->FriendlyName})}, |
|||
.ip_address{ip_addr}, |
|||
.subnet_mask = in_addr{.S_un{.S_addr{mask}}}, |
|||
.gateway = gateway}); |
|||
} |
|||
|
|||
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 || ifa->ifa_netmask == 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; |
|||
} |
|||
|
|||
std::uint32_t gateway{0}; |
|||
std::ifstream file{"/proc/net/route"}; |
|||
if (file.is_open()) { |
|||
|
|||
// ignore header
|
|||
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); |
|||
|
|||
bool gateway_found = false; |
|||
|
|||
for (std::string line; std::getline(file, line);) { |
|||
std::istringstream iss{line}; |
|||
|
|||
std::string iface_name{}; |
|||
iss >> iface_name; |
|||
if (iface_name != ifa->ifa_name) { |
|||
continue; |
|||
} |
|||
|
|||
iss >> std::hex; |
|||
|
|||
std::uint32_t dest{0}; |
|||
iss >> dest; |
|||
if (dest != 0) { |
|||
// not the default route
|
|||
continue; |
|||
} |
|||
|
|||
iss >> gateway; |
|||
|
|||
std::uint16_t flags{0}; |
|||
iss >> flags; |
|||
|
|||
// flag RTF_GATEWAY (defined in <linux/route.h>)
|
|||
if ((flags & 0x2) == 0) { |
|||
continue; |
|||
} |
|||
|
|||
gateway_found = true; |
|||
break; |
|||
} |
|||
|
|||
if (!gateway_found) { |
|||
gateway = 0; |
|||
} |
|||
} else { |
|||
LOG_ERROR(Network, "Failed to open \"/proc/net/route\""); |
|||
} |
|||
|
|||
result.push_back(NetworkInterface{ |
|||
.name{ifa->ifa_name}, |
|||
.ip_address{Common::BitCast<struct sockaddr_in>(*ifa->ifa_addr).sin_addr}, |
|||
.subnet_mask{Common::BitCast<struct sockaddr_in>(*ifa->ifa_netmask).sin_addr}, |
|||
.gateway{in_addr{.s_addr = gateway}}}); |
|||
} |
|||
|
|||
freeifaddrs(ifaddr); |
|||
|
|||
return result; |
|||
} |
|||
|
|||
#endif
|
|||
|
|||
std::optional<NetworkInterface> GetSelectedNetworkInterface() { |
|||
const std::string& selected_network_interface = Settings::values.network_interface.GetValue(); |
|||
const auto network_interfaces = Network::GetAvailableNetworkInterfaces(); |
|||
if (network_interfaces.size() == 0) { |
|||
LOG_ERROR(Network, "GetAvailableNetworkInterfaces returned no interfaces"); |
|||
return {}; |
|||
} |
|||
|
|||
const auto res = |
|||
std::ranges::find_if(network_interfaces, [&selected_network_interface](const auto& iface) { |
|||
return iface.name == selected_network_interface; |
|||
}); |
|||
|
|||
if (res != network_interfaces.end()) { |
|||
return *res; |
|||
} else { |
|||
LOG_ERROR(Network, "Couldn't find selected interface \"{}\"", selected_network_interface); |
|||
return {}; |
|||
} |
|||
} |
|||
|
|||
} // namespace Network
|
|||
@ -0,0 +1,29 @@ |
|||
// Copyright 2021 yuzu emulator team |
|||
// Licensed under GPLv2 or any later version |
|||
// Refer to the license.txt file included. |
|||
|
|||
#pragma once |
|||
|
|||
#include <optional> |
|||
#include <string> |
|||
#include <vector> |
|||
|
|||
#ifdef _WIN32 |
|||
#include <winsock2.h> |
|||
#else |
|||
#include <netinet/in.h> |
|||
#endif |
|||
|
|||
namespace Network { |
|||
|
|||
struct NetworkInterface { |
|||
std::string name; |
|||
struct in_addr ip_address; |
|||
struct in_addr subnet_mask; |
|||
struct in_addr gateway; |
|||
}; |
|||
|
|||
std::vector<NetworkInterface> GetAvailableNetworkInterfaces(); |
|||
std::optional<NetworkInterface> GetSelectedNetworkInterface(); |
|||
|
|||
} // namespace Network |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue