nn_ac: Add local IP getter implementation for Linux (#1839)

This commit is contained in:
Ash 2026-06-06 03:39:00 +10:00 committed by GitHub
parent 5c56bfe43b
commit 326933b248
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,9 @@
#if BOOST_OS_WINDOWS
#include <iphlpapi.h>
#elif BOOST_OS_LINUX
#include <ifaddrs.h>
#include <net/if.h>
#endif
// AC lib (manages internet connection)
@ -81,6 +84,40 @@ void _GetLocalIPAndSubnetMask(uint32& localIp, uint32& subnetMask)
cemuLog_logDebug(LogType::Force, "_GetLocalIPAndSubnetMask(): Failed to find network IP and subnet mask");
_GetLocalIPAndSubnetMaskFallback(localIp, subnetMask);
}
#elif BOOST_OS_LINUX
void _GetLocalIPAndSubnetMask(uint32& localIp, uint32& subnetMask)
{
struct ifaddrs *ifaddr;
if (getifaddrs(&ifaddr) == -1)
{
cemuLog_log(LogType::Force, "Failed to acquire local IP and subnet mask");
_GetLocalIPAndSubnetMaskFallback(localIp, subnetMask);
}
stdx::scope_exit _ifa([&]{ freeifaddrs(ifaddr); });
for (const struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == nullptr || ifa->ifa_addr->sa_family != AF_INET)
continue;
if (!(ifa->ifa_flags & IFF_UP) || !(ifa->ifa_flags & IFF_RUNNING))
continue;
if (ifa->ifa_flags & IFF_LOOPBACK || ifa->ifa_flags & IFF_POINTOPOINT)
continue;
if (boost::starts_with(ifa->ifa_name, "br-") || boost::starts_with(ifa->ifa_name, "docker"))
continue;
const auto* addr_in = reinterpret_cast<struct sockaddr_in*>(ifa->ifa_addr);
localIp = ntohl(addr_in->sin_addr.s_addr);
const auto* mask_in = reinterpret_cast<struct sockaddr_in*>(ifa->ifa_netmask);
subnetMask = ntohl(mask_in->sin_addr.s_addr);
return;
}
cemuLog_logDebug(LogType::Force, "_GetLocalIPAndSubnetMask(): Failed to find network IP and subnet mask");
_GetLocalIPAndSubnetMaskFallback(localIp, subnetMask);
}
#else
void _GetLocalIPAndSubnetMask(uint32& localIp, uint32& subnetMask)
{