diff --git a/pcsx2/DEV9/AdapterUtils.cpp b/pcsx2/DEV9/AdapterUtils.cpp index 906c4f7f9e..5b154b5995 100644 --- a/pcsx2/DEV9/AdapterUtils.cpp +++ b/pcsx2/DEV9/AdapterUtils.cpp @@ -15,21 +15,24 @@ #include "PrecompiledHeader.h" -#include #ifdef __POSIX__ #include #include #include +#include #include #include #include "common/StringUtil.h" +#ifdef __linux__ +#include +#include +#endif + #if defined(__FreeBSD__) || (__APPLE__) #include #include -#include -#include #include #include "common/Assertions.h" @@ -39,6 +42,7 @@ #include "AdapterUtils.h" +using namespace PacketReader; using namespace PacketReader::IP; #ifdef _WIN32 @@ -244,6 +248,40 @@ bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, AdapterBuffer* buffer) } #endif +// AdapterMAC. +#ifdef _WIN32 +std::optional AdapterUtils::GetAdapterMAC(PIP_ADAPTER_ADDRESSES adapter) +{ + if (adapter != nullptr && adapter->PhysicalAddressLength == 6) + return *(MAC_Address*)adapter->PhysicalAddress; + + return std::nullopt; +} +#elif defined(__POSIX__) +#ifdef __linux__ +std::optional AdapterUtils::GetAdapterMAC(ifaddrs* adapter) +{ + struct ifreq ifr; + strcpy(ifr.ifr_name, adapter->ifa_name); + + int fd = socket(AF_INET, SOCK_DGRAM, 0); + int ret = ioctl(fd, SIOCGIFHWADDR, &ifr); + close(fd); + + if (ret == 0) + return *(MAC_Address*)ifr.ifr_hwaddr.sa_data; + + return std::nullopt; +} +#else +std::optional AdapterUtils::GetAdapterMAC(ifaddrs* adapter) +{ + Console.Error("DEV9: Unsupported OS, can't get MAC address"); + return std::nullopt; +} +#endif +#endif + // AdapterIP. #ifdef _WIN32 std::optional AdapterUtils::GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter) diff --git a/pcsx2/DEV9/AdapterUtils.h b/pcsx2/DEV9/AdapterUtils.h index 3a57af84c1..6ab4045dfd 100644 --- a/pcsx2/DEV9/AdapterUtils.h +++ b/pcsx2/DEV9/AdapterUtils.h @@ -26,6 +26,7 @@ #include #include +#include "DEV9/PacketReader/MAC_Address.h" #include "DEV9/PacketReader/IP/IP_Address.h" namespace AdapterUtils @@ -35,6 +36,7 @@ namespace AdapterUtils 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); @@ -48,6 +50,7 @@ namespace AdapterUtils 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); diff --git a/pcsx2/DEV9/pcap_io.cpp b/pcsx2/DEV9/pcap_io.cpp index 4685066bd8..3b7db648f8 100644 --- a/pcsx2/DEV9/pcap_io.cpp +++ b/pcsx2/DEV9/pcap_io.cpp @@ -23,14 +23,9 @@ #include "common/StringUtil.h" #include #include -#elif defined(__linux__) -#include -#include -#include #elif defined(__POSIX__) #include #include -#include #endif #include @@ -64,40 +59,6 @@ ip_address ps2_ip; mac_address ps2_mac; mac_address host_mac; -// Fetches the MAC address and prints it -int GetMACAddress(const std::string& adapter, mac_address* addr) -{ - int retval = 0; -#ifdef _WIN32 - IP_ADAPTER_ADDRESSES adapterInfo; - std::unique_ptr buffer; - - if (AdapterUtils::GetWin32Adapter(adapter, &adapterInfo, &buffer)) - { - memcpy(addr, adapterInfo.PhysicalAddress, 6); - retval = 1; - } - -#elif defined(__linux__) - struct ifreq ifr; - int fd = socket(AF_INET, SOCK_DGRAM, 0); - strcpy(ifr.ifr_name, adapter.c_str()); - if (0 == ioctl(fd, SIOCGIFHWADDR, &ifr)) - { - retval = 1; - memcpy(addr, ifr.ifr_hwaddr.sa_data, 6); - } - else - { - Console.Error("Could not get MAC address for adapter: %s", adapter.c_str()); - } - close(fd); -#else - Console.Error("Could not get MAC address for adapter, OS not supported"); -#endif - return retval; -} - int pcap_io_init(const std::string& adapter, bool switched, mac_address virtual_mac) { struct bpf_program fp; @@ -290,48 +251,52 @@ PCAPAdapter::PCAPAdapter() return; #endif - mac_address hostMAC; - mac_address newMAC; - - GetMACAddress(EmuConfig.DEV9.EthDevice, &hostMAC); - 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]; - - SetMACAddress((PacketReader::MAC_Address*)&newMAC); - host_mac = hostMAC; - ps2_mac = newMAC; //Needed outside of this class - - if (pcap_io_init(EmuConfig.DEV9.EthDevice, EmuConfig.DEV9.EthApi == Pcsx2Config::DEV9Options::NetApi::PCAP_Switched, newMAC) == -1) - { - Console.Error("DEV9: Can't open Device '%s'", EmuConfig.DEV9.EthDevice.c_str()); - return; - } + bool foundAdapter = false; #ifdef _WIN32 IP_ADAPTER_ADDRESSES adapter; AdapterUtils::AdapterBuffer buffer; - if (AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer)) - InitInternalServer(&adapter); - else - { - Console.Error("DEV9: Failed to get adapter information"); - InitInternalServer(nullptr); - } - + foundAdapter = AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); #elif defined(__POSIX__) ifaddrs adapter; AdapterUtils::AdapterBuffer buffer; - if (AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer)) + foundAdapter = AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); +#endif + + 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]; + + 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; + } + + if (pcap_io_init(EmuConfig.DEV9.EthDevice, EmuConfig.DEV9.EthApi == Pcsx2Config::DEV9Options::NetApi::PCAP_Switched, ps2_mac) == -1) + { + 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); } -#endif } AdapterOptions PCAPAdapter::GetAdapterOptions() { diff --git a/pcsx2/DEV9/sockets.cpp b/pcsx2/DEV9/sockets.cpp index da2fa2afc6..bcf6c26b64 100644 --- a/pcsx2/DEV9/sockets.cpp +++ b/pcsx2/DEV9/sockets.cpp @@ -27,10 +27,6 @@ #include #include #include -#include -#ifdef __linux__ -#include -#endif #endif #include "sockets.h" @@ -239,34 +235,20 @@ SocketAdapter::SocketAdapter() InitInternalServer(&adapter, true, ps2IP, subnet, gateway); - MAC_Address hostMAC; - MAC_Address newMAC; - -#ifdef _WIN32 - hostMAC = *(MAC_Address*)adapter.PhysicalAddress; -#elif defined(__linux__) - struct ifreq ifr; - int fd = socket(AF_INET, SOCK_DGRAM, 0); - strcpy(ifr.ifr_name, adapter.ifa_name); - if (0 == ioctl(fd, SIOCGIFHWADDR, &ifr)) - hostMAC = *(MAC_Address*)ifr.ifr_hwaddr.sa_data; - else + std::optional adMAC = AdapterUtils::GetAdapterMAC(&adapter); + if (adMAC.has_value()) { - hostMAC = ps2MAC; - Console.Error("Could not get MAC address for adapter: %s", adapter.ifa_name); + MAC_Address hostMAC = adMAC.value(); + MAC_Address newMAC = ps2MAC; + + //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(&newMAC); } - ::close(fd); -#else - hostMAC = ps2MAC; - Console.Error("Could not get MAC address for adapter, OS not supported"); -#endif - newMAC = ps2MAC; - - //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(&newMAC); + else + Console.Error("DEV9: Socket: Failed to get MAC address for adapter"); #ifdef _WIN32 /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */