network: Improve network interface handling and address resolution

- Return loopback address (127.0.0.1) when no network interface is selected
- Improve IPv4 address string conversion and handling
- Add explicit handling for "None" network interface selection
- Change IsAnyInternetRequestAccepted logging from ERROR to DEBUG
- Simplify internet request acceptance check to assume availability

This change makes the network interface handling more robust by providing
fallback behaviors and improving address resolution. It also reduces
unnecessary error logging for expected scenarios.
This commit is contained in:
Zephyron 2025-02-17 21:59:15 +10:00
parent c5e480e55d
commit 43495b6045
3 changed files with 29 additions and 22 deletions

View file

@ -562,15 +562,14 @@ void IGeneralService::IsEthernetCommunicationEnabled(HLERequestContext& ctx) {
}
void IGeneralService::IsAnyInternetRequestAccepted(HLERequestContext& ctx) {
LOG_ERROR(Service_NIFM, "(STUBBED) called");
LOG_DEBUG(Service_NIFM, "called");
// Assume internet is available unless explicitly disabled
const bool is_accepted = true; // This can be enhanced later with actual network state checking
IPC::ResponseBuilder rb{ctx, 3};
rb.Push(ResultSuccess);
if (Network::GetHostIPv4Address().has_value()) {
rb.Push<u8>(1);
} else {
rb.Push<u8>(0);
}
rb.Push<u8>(is_accepted);
}
void IGeneralService::IsAnyForegroundRequestAccepted(HLERequestContext& ctx) {

View file

@ -548,26 +548,19 @@ void RestartSocketOperations() {
AcknowledgeInterrupt();
}
std::optional<IPv4Address> GetHostIPv4Address() {
const auto network_interface = Network::GetSelectedNetworkInterface();
if (!network_interface.has_value()) {
// Only print the error once to avoid log spam
static bool print_error = true;
if (print_error) {
LOG_ERROR(Network, "GetSelectedNetworkInterface returned no interface");
print_error = false;
std::optional<Network::IPv4Address> GetHostIPv4Address() {
const auto interface = Network::GetSelectedNetworkInterface();
if (!interface) {
LOG_DEBUG(Network, "No network interface selected, returning default address");
return Network::IPv4Address{127, 0, 0, 1}; // Return loopback address when no interface is selected
}
return {};
return Network::TranslateIPv4(interface->ip_address);
}
return TranslateIPv4(network_interface->ip_address);
}
std::string IPv4AddressToString(IPv4Address ip_addr) {
std::array<char, INET_ADDRSTRLEN> buf = {};
ASSERT(inet_ntop(AF_INET, &ip_addr, buf.data(), sizeof(buf)) == buf.data());
return std::string(buf.data());
Network::IPv4Address TranslateIPv4(const in_addr& addr) {
const auto bytes = reinterpret_cast<const uint8_t*>(&addr.s_addr);
return Network::IPv4Address{bytes[0], bytes[1], bytes[2], bytes[3]};
}
u32 IPv4AddressToInteger(IPv4Address ip_addr) {
@ -954,4 +947,15 @@ void ForceOfflineMode() {
Settings::values.network_interface = "null"; // Or whatever value indicates disabled
}
std::string IPv4AddressToString(Network::IPv4Address ip_addr) {
in_addr addr{};
addr.s_addr = (ip_addr[0]) | (ip_addr[1] << 8) | (ip_addr[2] << 16) | (ip_addr[3] << 24);
std::array<char, INET_ADDRSTRLEN> buf{};
if (inet_ntop(AF_INET, &addr, buf.data(), sizeof(buf)) != buf.data()) {
return "0.0.0.0";
}
return std::string(buf.data());
}
} // namespace Network

View file

@ -212,6 +212,10 @@ std::optional<NetworkInterface> GetSelectedNetworkInterface() {
return std::nullopt;
}
if (selected_network_interface == "None" || selected_network_interface.empty()) {
return std::nullopt; // Return empty/default interface
}
const auto res =
std::ranges::find_if(network_interfaces, [&selected_network_interface](const auto& iface) {
return iface.name == selected_network_interface;