Merge pull request #6905 from Morph1984/nifm-misc

nifm/network_interface: Cleanup and populate fields in GetCurrentNetworkProfile
This commit is contained in:
bunnei 2021-08-29 00:04:58 -07:00 committed by GitHub
commit 5f19b66189
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 164 additions and 154 deletions

View file

@ -277,13 +277,20 @@ private:
void GetCurrentNetworkProfile(Kernel::HLERequestContext& ctx) { void GetCurrentNetworkProfile(Kernel::HLERequestContext& ctx) {
LOG_WARNING(Service_NIFM, "(STUBBED) called"); LOG_WARNING(Service_NIFM, "(STUBBED) called");
const SfNetworkProfileData network_profile_data{ const auto net_iface = Network::GetSelectedNetworkInterface();
const SfNetworkProfileData network_profile_data = [&net_iface] {
if (!net_iface) {
return SfNetworkProfileData{};
}
return SfNetworkProfileData{
.ip_setting_data{ .ip_setting_data{
.ip_address_setting{ .ip_address_setting{
.is_automatic{true}, .is_automatic{true},
.current_address{192, 168, 1, 100}, .current_address{Network::TranslateIPv4(net_iface->ip_address)},
.subnet_mask{255, 255, 255, 0}, .subnet_mask{Network::TranslateIPv4(net_iface->subnet_mask)},
.gateway{192, 168, 1, 1}, .gateway{Network::TranslateIPv4(net_iface->gateway)},
}, },
.dns_setting{ .dns_setting{
.is_automatic{true}, .is_automatic{true},
@ -308,6 +315,7 @@ private:
.passphrase{"yuzupassword"}, .passphrase{"yuzupassword"},
}, },
}; };
}();
ctx.WriteBuffer(network_profile_data); ctx.WriteBuffer(network_profile_data);
@ -352,18 +360,25 @@ private:
LOG_WARNING(Service_NIFM, "(STUBBED) called"); LOG_WARNING(Service_NIFM, "(STUBBED) called");
struct IpConfigInfo { struct IpConfigInfo {
IpAddressSetting ip_address_setting; IpAddressSetting ip_address_setting{};
DnsSetting dns_setting; DnsSetting dns_setting{};
}; };
static_assert(sizeof(IpConfigInfo) == sizeof(IpAddressSetting) + sizeof(DnsSetting), static_assert(sizeof(IpConfigInfo) == sizeof(IpAddressSetting) + sizeof(DnsSetting),
"IpConfigInfo has incorrect size."); "IpConfigInfo has incorrect size.");
IpConfigInfo ip_config_info{ const auto net_iface = Network::GetSelectedNetworkInterface();
const IpConfigInfo ip_config_info = [&net_iface] {
if (!net_iface) {
return IpConfigInfo{};
}
return IpConfigInfo{
.ip_address_setting{ .ip_address_setting{
.is_automatic{true}, .is_automatic{true},
.current_address{0, 0, 0, 0}, .current_address{Network::TranslateIPv4(net_iface->ip_address)},
.subnet_mask{255, 255, 255, 0}, .subnet_mask{Network::TranslateIPv4(net_iface->subnet_mask)},
.gateway{192, 168, 1, 1}, .gateway{Network::TranslateIPv4(net_iface->gateway)},
}, },
.dns_setting{ .dns_setting{
.is_automatic{true}, .is_automatic{true},
@ -371,19 +386,7 @@ private:
.secondary_dns{1, 0, 0, 1}, .secondary_dns{1, 0, 0, 1},
}, },
}; };
}();
const auto iface = Network::GetSelectedNetworkInterface();
if (iface) {
ip_config_info.ip_address_setting =
IpAddressSetting{.is_automatic{true},
.current_address{Network::TranslateIPv4(iface->ip_address)},
.subnet_mask{Network::TranslateIPv4(iface->subnet_mask)},
.gateway{Network::TranslateIPv4(iface->gateway)}};
} else {
LOG_ERROR(Service_NIFM,
"Couldn't get host network configuration info, using default values");
}
IPC::ResponseBuilder rb{ctx, 2 + (sizeof(IpConfigInfo) + 3) / sizeof(u32)}; IPC::ResponseBuilder rb{ctx, 2 + (sizeof(IpConfigInfo) + 3) / sizeof(u32)};
rb.Push(ResultSuccess); rb.Push(ResultSuccess);

View file

@ -37,14 +37,18 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
AF_INET, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_INCLUDE_GATEWAYS, AF_INET, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_INCLUDE_GATEWAYS,
nullptr, adapter_addresses.data(), &buf_size); nullptr, adapter_addresses.data(), &buf_size);
if (ret == ERROR_BUFFER_OVERFLOW) { if (ret != ERROR_BUFFER_OVERFLOW) {
adapter_addresses.resize((buf_size / sizeof(IP_ADAPTER_ADDRESSES)) + 1);
} else {
break; break;
} }
adapter_addresses.resize((buf_size / sizeof(IP_ADAPTER_ADDRESSES)) + 1);
}
if (ret != NO_ERROR) {
LOG_ERROR(Network, "Failed to get network interfaces with GetAdaptersAddresses");
return {};
} }
if (ret == NO_ERROR) {
std::vector<NetworkInterface> result; std::vector<NetworkInterface> result;
for (auto current_address = adapter_addresses.data(); current_address != nullptr; for (auto current_address = adapter_addresses.data(); current_address != nullptr;
@ -77,7 +81,7 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
.sin_addr; .sin_addr;
} }
result.push_back(NetworkInterface{ result.emplace_back(NetworkInterface{
.name{Common::UTF16ToUTF8(std::wstring{current_address->FriendlyName})}, .name{Common::UTF16ToUTF8(std::wstring{current_address->FriendlyName})},
.ip_address{ip_addr}, .ip_address{ip_addr},
.subnet_mask = in_addr{.S_un{.S_addr{mask}}}, .subnet_mask = in_addr{.S_un{.S_addr{mask}}},
@ -85,25 +89,21 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
} }
return result; return result;
} else {
LOG_ERROR(Network, "Failed to get network interfaces with GetAdaptersAddresses");
return {};
}
} }
#else #else
std::vector<NetworkInterface> GetAvailableNetworkInterfaces() { std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
std::vector<NetworkInterface> result;
struct ifaddrs* ifaddr = nullptr; struct ifaddrs* ifaddr = nullptr;
if (getifaddrs(&ifaddr) != 0) { if (getifaddrs(&ifaddr) != 0) {
LOG_ERROR(Network, "Failed to get network interfaces with getifaddrs: {}", LOG_ERROR(Network, "Failed to get network interfaces with getifaddrs: {}",
std::strerror(errno)); std::strerror(errno));
return result; return {};
} }
std::vector<NetworkInterface> result;
for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr || ifa->ifa_netmask == nullptr) { if (ifa->ifa_addr == nullptr || ifa->ifa_netmask == nullptr) {
continue; continue;
@ -117,9 +117,19 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
continue; continue;
} }
std::uint32_t gateway{0}; u32 gateway{};
std::ifstream file{"/proc/net/route"}; std::ifstream file{"/proc/net/route"};
if (file.is_open()) { if (!file.is_open()) {
LOG_ERROR(Network, "Failed to open \"/proc/net/route\"");
result.emplace_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}}});
continue;
}
// ignore header // ignore header
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
@ -129,7 +139,7 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
for (std::string line; std::getline(file, line);) { for (std::string line; std::getline(file, line);) {
std::istringstream iss{line}; std::istringstream iss{line};
std::string iface_name{}; std::string iface_name;
iss >> iface_name; iss >> iface_name;
if (iface_name != ifa->ifa_name) { if (iface_name != ifa->ifa_name) {
continue; continue;
@ -137,7 +147,7 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
iss >> std::hex; iss >> std::hex;
std::uint32_t dest{0}; u32 dest{};
iss >> dest; iss >> dest;
if (dest != 0) { if (dest != 0) {
// not the default route // not the default route
@ -146,7 +156,7 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
iss >> gateway; iss >> gateway;
std::uint16_t flags{0}; u16 flags{};
iss >> flags; iss >> flags;
// flag RTF_GATEWAY (defined in <linux/route.h>) // flag RTF_GATEWAY (defined in <linux/route.h>)
@ -161,11 +171,8 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
if (!gateway_found) { if (!gateway_found) {
gateway = 0; gateway = 0;
} }
} else {
LOG_ERROR(Network, "Failed to open \"/proc/net/route\"");
}
result.push_back(NetworkInterface{ result.emplace_back(NetworkInterface{
.name{ifa->ifa_name}, .name{ifa->ifa_name},
.ip_address{Common::BitCast<struct sockaddr_in>(*ifa->ifa_addr).sin_addr}, .ip_address{Common::BitCast<struct sockaddr_in>(*ifa->ifa_addr).sin_addr},
.subnet_mask{Common::BitCast<struct sockaddr_in>(*ifa->ifa_netmask).sin_addr}, .subnet_mask{Common::BitCast<struct sockaddr_in>(*ifa->ifa_netmask).sin_addr},
@ -180,11 +187,11 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
#endif #endif
std::optional<NetworkInterface> GetSelectedNetworkInterface() { std::optional<NetworkInterface> GetSelectedNetworkInterface() {
const std::string& selected_network_interface = Settings::values.network_interface.GetValue(); const auto& selected_network_interface = Settings::values.network_interface.GetValue();
const auto network_interfaces = Network::GetAvailableNetworkInterfaces(); const auto network_interfaces = Network::GetAvailableNetworkInterfaces();
if (network_interfaces.size() == 0) { if (network_interfaces.size() == 0) {
LOG_ERROR(Network, "GetAvailableNetworkInterfaces returned no interfaces"); LOG_ERROR(Network, "GetAvailableNetworkInterfaces returned no interfaces");
return {}; return std::nullopt;
} }
const auto res = const auto res =
@ -192,12 +199,12 @@ std::optional<NetworkInterface> GetSelectedNetworkInterface() {
return iface.name == selected_network_interface; return iface.name == selected_network_interface;
}); });
if (res != network_interfaces.end()) { if (res == network_interfaces.end()) {
return *res;
} else {
LOG_ERROR(Network, "Couldn't find selected interface \"{}\"", selected_network_interface); LOG_ERROR(Network, "Couldn't find selected interface \"{}\"", selected_network_interface);
return {}; return std::nullopt;
} }
return *res;
} }
} // namespace Network } // namespace Network