Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Cafe/OS/libs/nn_ac/nn_ac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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)
Expand Down Expand Up @@ -80,6 +83,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)
{
Expand Down