diff --git a/pcsx2/DEV9/AdapterUtils.cpp b/pcsx2/DEV9/AdapterUtils.cpp index 5b154b5995..01c0764714 100644 --- a/pcsx2/DEV9/AdapterUtils.cpp +++ b/pcsx2/DEV9/AdapterUtils.cpp @@ -46,7 +46,7 @@ using namespace PacketReader; using namespace PacketReader::IP; #ifdef _WIN32 -bool AdapterUtils::GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSES adapter, AdapterBuffer* buffer) +bool AdapterUtils::GetAdapter(const std::string& name, Adapter* adapter, AdapterBuffer* buffer) { int neededSize = 128; std::unique_ptr adapterInfo = std::make_unique(neededSize); @@ -95,7 +95,7 @@ bool AdapterUtils::GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSE return false; } -bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, AdapterBuffer* buffer) +bool AdapterUtils::GetAdapterAuto(Adapter* adapter, AdapterBuffer* buffer) { int neededSize = 128; std::unique_ptr adapterInfo = std::make_unique(neededSize); @@ -172,7 +172,7 @@ bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, AdapterBuf return false; } #elif defined(__POSIX__) -bool AdapterUtils::GetIfAdapter(const std::string& name, ifaddrs* adapter, AdapterBuffer* buffer) +bool AdapterUtils::GetAdapter(const std::string& name, Adapter* adapter, AdapterBuffer* buffer) { ifaddrs* ifa; ifaddrs* pAdapter; @@ -202,7 +202,7 @@ bool AdapterUtils::GetIfAdapter(const std::string& name, ifaddrs* adapter, Adapt return false; } -bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, AdapterBuffer* buffer) +bool AdapterUtils::GetAdapterAuto(Adapter* adapter, AdapterBuffer* buffer) { ifaddrs* ifa; ifaddrs* pAdapter; @@ -250,7 +250,7 @@ bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, AdapterBuffer* buffer) // AdapterMAC. #ifdef _WIN32 -std::optional AdapterUtils::GetAdapterMAC(PIP_ADAPTER_ADDRESSES adapter) +std::optional AdapterUtils::GetAdapterMAC(Adapter* adapter) { if (adapter != nullptr && adapter->PhysicalAddressLength == 6) return *(MAC_Address*)adapter->PhysicalAddress; @@ -259,7 +259,7 @@ std::optional AdapterUtils::GetAdapterMAC(PIP_ADAPTER_ADDRESSES ada } #elif defined(__POSIX__) #ifdef __linux__ -std::optional AdapterUtils::GetAdapterMAC(ifaddrs* adapter) +std::optional AdapterUtils::GetAdapterMAC(Adapter* adapter) { struct ifreq ifr; strcpy(ifr.ifr_name, adapter->ifa_name); @@ -274,7 +274,7 @@ std::optional AdapterUtils::GetAdapterMAC(ifaddrs* adapter) return std::nullopt; } #else -std::optional AdapterUtils::GetAdapterMAC(ifaddrs* adapter) +std::optional AdapterUtils::GetAdapterMAC(Adapter* adapter) { Console.Error("DEV9: Unsupported OS, can't get MAC address"); return std::nullopt; @@ -284,7 +284,7 @@ std::optional AdapterUtils::GetAdapterMAC(ifaddrs* adapter) // AdapterIP. #ifdef _WIN32 -std::optional AdapterUtils::GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter) +std::optional AdapterUtils::GetAdapterIP(Adapter* adapter) { PIP_ADAPTER_UNICAST_ADDRESS address = nullptr; if (adapter != nullptr) @@ -302,7 +302,7 @@ std::optional AdapterUtils::GetAdapterIP(PIP_ADAPTER_ADDRESSES adapt return std::nullopt; } #elif defined(__POSIX__) -std::optional AdapterUtils::GetAdapterIP(ifaddrs* adapter) +std::optional AdapterUtils::GetAdapterIP(Adapter* adapter) { sockaddr* address = nullptr; if (adapter != nullptr) @@ -322,7 +322,7 @@ std::optional AdapterUtils::GetAdapterIP(ifaddrs* adapter) // Gateways. #ifdef _WIN32 -std::vector AdapterUtils::GetGateways(PIP_ADAPTER_ADDRESSES adapter) +std::vector AdapterUtils::GetGateways(Adapter* adapter) { if (adapter == nullptr) return {}; @@ -344,7 +344,7 @@ std::vector AdapterUtils::GetGateways(PIP_ADAPTER_ADDRESSES adapter) } #elif defined(__POSIX__) #ifdef __linux__ -std::vector AdapterUtils::GetGateways(ifaddrs* adapter) +std::vector AdapterUtils::GetGateways(Adapter* adapter) { // /proc/net/route contains some information about gateway addresses, // and separates the information about by each interface. @@ -390,7 +390,7 @@ std::vector AdapterUtils::GetGateways(ifaddrs* adapter) return collection; } #elif defined(__FreeBSD__) || (__APPLE__) -std::vector AdapterUtils::GetGateways(ifaddrs* adapter) +std::vector AdapterUtils::GetGateways(Adapter* adapter) { if (adapter == nullptr) return {}; @@ -470,7 +470,7 @@ std::vector AdapterUtils::GetGateways(ifaddrs* adapter) return collection; } #else -std::vector AdapterUtils::GetGateways(ifaddrs* adapter) +std::vector AdapterUtils::GetGateways(Adapter* adapter) { Console.Error("DEV9: Unsupported OS, can't find Gateway"); return {}; @@ -480,7 +480,7 @@ std::vector AdapterUtils::GetGateways(ifaddrs* adapter) // DNS. #ifdef _WIN32 -std::vector AdapterUtils::GetDNS(PIP_ADAPTER_ADDRESSES adapter) +std::vector AdapterUtils::GetDNS(Adapter* adapter) { if (adapter == nullptr) return {}; @@ -501,7 +501,7 @@ std::vector AdapterUtils::GetDNS(PIP_ADAPTER_ADDRESSES adapter) return collection; } #elif defined(__POSIX__) -std::vector AdapterUtils::GetDNS(ifaddrs* adapter) +std::vector AdapterUtils::GetDNS(Adapter* adapter) { // On Linux and OSX, DNS is system wide, not adapter specific, so we can ignore the adapter parameter. diff --git a/pcsx2/DEV9/AdapterUtils.h b/pcsx2/DEV9/AdapterUtils.h index 6ab4045dfd..e2e7f70acb 100644 --- a/pcsx2/DEV9/AdapterUtils.h +++ b/pcsx2/DEV9/AdapterUtils.h @@ -32,28 +32,22 @@ namespace AdapterUtils { #ifdef _WIN32 + typedef IP_ADAPTER_ADDRESSES Adapter; typedef std::unique_ptr AdapterBuffer; - bool GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSES adapter, AdapterBuffer* buffer); - bool GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, std::unique_ptr* buffer); - - std::optional GetAdapterMAC(PIP_ADAPTER_ADDRESSES adapter); - std::optional GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter); - // Mask. - std::vector GetGateways(PIP_ADAPTER_ADDRESSES adapter); - std::vector GetDNS(PIP_ADAPTER_ADDRESSES adapter); #elif defined(__POSIX__) + typedef ifaddrs Adapter; struct IfAdaptersDeleter { void operator()(ifaddrs* buffer) const { freeifaddrs(buffer); } }; typedef std::unique_ptr AdapterBuffer; - bool GetIfAdapter(const std::string& name, ifaddrs* adapter, AdapterBuffer* buffer); - bool GetIfAdapterAuto(ifaddrs* adapter, AdapterBuffer* buffer); - - std::optional GetAdapterMAC(ifaddrs* adapter); - std::optional GetAdapterIP(ifaddrs* adapter); - // Mask. - std::vector GetGateways(ifaddrs* adapter); - std::vector GetDNS(ifaddrs* adapter); #endif + bool GetAdapter(const std::string& name, Adapter* adapter, AdapterBuffer* buffer); + bool GetAdapterAuto(Adapter* adapter, AdapterBuffer* buffer); + + std::optional GetAdapterMAC(Adapter* adapter); + std::optional GetAdapterIP(Adapter* adapter); + // Mask. + std::vector GetGateways(Adapter* adapter); + std::vector GetDNS(Adapter* adapter); }; // namespace AdapterUtils diff --git a/pcsx2/DEV9/pcap_io.cpp b/pcsx2/DEV9/pcap_io.cpp index 3b7db648f8..256eee7096 100644 --- a/pcsx2/DEV9/pcap_io.cpp +++ b/pcsx2/DEV9/pcap_io.cpp @@ -251,36 +251,34 @@ PCAPAdapter::PCAPAdapter() return; #endif - bool foundAdapter = false; - -#ifdef _WIN32 - IP_ADAPTER_ADDRESSES adapter; + AdapterUtils::Adapter adapter; AdapterUtils::AdapterBuffer buffer; - foundAdapter = AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); -#elif defined(__POSIX__) - ifaddrs adapter; - AdapterUtils::AdapterBuffer buffer; - foundAdapter = AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); -#endif - - std::optional adMAC = AdapterUtils::GetAdapterMAC(&adapter); - if (adMAC.has_value()) + if (AdapterUtils::GetAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer)) { - mac_address hostMAC = *(mac_address*)&adMAC.value(); - mac_address newMAC; - memcpy(&newMAC, &ps2MAC, 6); + std::optional adMAC = AdapterUtils::GetAdapterMAC(&adapter); + if (adMAC.has_value()) + { + mac_address hostMAC = *(mac_address*)&adMAC.value(); + mac_address newMAC; + memcpy(&newMAC, &ps2MAC, 6); - //Lets take the hosts last 2 bytes to make it unique on Xlink - newMAC.bytes[5] = hostMAC.bytes[4]; - newMAC.bytes[4] = hostMAC.bytes[5]; + //Lets take the hosts last 2 bytes to make it unique on Xlink + newMAC.bytes[5] = hostMAC.bytes[4]; + newMAC.bytes[4] = hostMAC.bytes[5]; - SetMACAddress((PacketReader::MAC_Address*)&newMAC); - host_mac = hostMAC; - ps2_mac = newMAC; //Needed outside of this class + SetMACAddress((PacketReader::MAC_Address*)&newMAC); + host_mac = hostMAC; + ps2_mac = newMAC; //Needed outside of this class + } + else + { + Console.Error("DEV9: Failed to get MAC address for adapter"); + return; + } } else { - Console.Error("DEV9: Failed to get MAC address for adapter"); + Console.Error("DEV9: Failed to get adapter information"); return; } @@ -289,14 +287,8 @@ PCAPAdapter::PCAPAdapter() Console.Error("DEV9: Can't open Device '%s'", EmuConfig.DEV9.EthDevice.c_str()); return; } - - if (foundAdapter) - InitInternalServer(&adapter); - else - { - Console.Error("DEV9: Failed to get adapter information"); - InitInternalServer(nullptr); - } + + InitInternalServer(&adapter); } AdapterOptions PCAPAdapter::GetAdapterOptions() { @@ -345,21 +337,12 @@ bool PCAPAdapter::send(NetPacket* pkt) void PCAPAdapter::reloadSettings() { -#ifdef _WIN32 - IP_ADAPTER_ADDRESSES adapter; + AdapterUtils::Adapter adapter; AdapterUtils::AdapterBuffer buffer; - if (AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer)) + if (AdapterUtils::GetAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer)) ReloadInternalServer(&adapter); else ReloadInternalServer(nullptr); -#elif defined(__POSIX__) - ifaddrs adapter; - AdapterUtils::AdapterBuffer buffer; - if (AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer)) - ReloadInternalServer(&adapter); - else - ReloadInternalServer(nullptr); -#endif } PCAPAdapter::~PCAPAdapter() @@ -401,7 +384,7 @@ std::vector PCAPAdapter::GetAdapters() IP_ADAPTER_ADDRESSES adapterInfo; std::unique_ptr buffer; - if (AdapterUtils::GetWin32Adapter(entry.guid, &adapterInfo, &buffer)) + if (AdapterUtils::GetAdapter(entry.guid, &adapterInfo, &buffer)) entry.name = StringUtil::WideStringToUTF8String(std::wstring(adapterInfo.FriendlyName)); else { diff --git a/pcsx2/DEV9/sockets.cpp b/pcsx2/DEV9/sockets.cpp index bcf6c26b64..584eeef987 100644 --- a/pcsx2/DEV9/sockets.cpp +++ b/pcsx2/DEV9/sockets.cpp @@ -156,13 +156,12 @@ SocketAdapter::SocketAdapter() { bool foundAdapter; -#ifdef _WIN32 - IP_ADAPTER_ADDRESSES adapter; + AdapterUtils::Adapter adapter; AdapterUtils::AdapterBuffer buffer; if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0) { - foundAdapter = AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); + foundAdapter = AdapterUtils::GetAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); if (!foundAdapter) { @@ -181,7 +180,7 @@ SocketAdapter::SocketAdapter() } else { - foundAdapter = AdapterUtils::GetWin32AdapterAuto(&adapter, &buffer); + foundAdapter = AdapterUtils::GetAdapterAuto(&adapter, &buffer); adapterIP = {}; if (!foundAdapter) @@ -190,41 +189,6 @@ SocketAdapter::SocketAdapter() return; } } -#elif defined(__POSIX__) - ifaddrs adapter; - AdapterUtils::AdapterBuffer buffer; - - if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0) - { - foundAdapter = AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); - - if (!foundAdapter) - { - Console.Error("DEV9: Socket: Failed to Get Adapter"); - return; - } - - std::optional adIP = AdapterUtils::GetAdapterIP(&adapter); - if (adIP.has_value()) - adapterIP = adIP.value(); - else - { - Console.Error("DEV9: Socket: Failed To Get Adapter IP"); - return; - } - } - else - { - foundAdapter = AdapterUtils::GetIfAdapterAuto(&adapter, &buffer); - adapterIP = {0}; - - if (!foundAdapter) - { - Console.Error("DEV9: Socket: Auto Selection Failed, Check You Connection or Manually Specify Adapter"); - return; - } - } -#endif //For DHCP, we need to override some settings //DNS settings as per direct adapters @@ -409,24 +373,14 @@ void SocketAdapter::reset() void SocketAdapter::reloadSettings() { bool foundAdapter = false; -#ifdef _WIN32 - IP_ADAPTER_ADDRESSES adapter; + + AdapterUtils::Adapter adapter; AdapterUtils::AdapterBuffer buffer; if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0) - foundAdapter = AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); + foundAdapter = AdapterUtils::GetAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); else - foundAdapter = AdapterUtils::GetWin32AdapterAuto(&adapter, &buffer); - -#elif defined(__POSIX__) - ifaddrs adapter; - AdapterUtils::AdapterBuffer buffer; - - if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0) - foundAdapter = AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); - else - foundAdapter = AdapterUtils::GetIfAdapterAuto(&adapter, &buffer); -#endif + foundAdapter = AdapterUtils::GetAdapterAuto(&adapter, &buffer); const IP_Address ps2IP = {{{internalIP.bytes[0], internalIP.bytes[1], internalIP.bytes[2], 100}}}; const IP_Address subnet{{{255, 255, 255, 0}}};