DEV9: Move logic for getting MAC address into AdapterUtils

This commit is contained in:
TheLastRar 2023-01-19 12:01:46 +00:00 committed by refractionpcsx2
parent df674d4056
commit 2db1e8fb81
4 changed files with 89 additions and 101 deletions

View File

@ -15,21 +15,24 @@
#include "PrecompiledHeader.h"
#include <algorithm>
#ifdef __POSIX__
#include <vector>
#include <fstream>
#include <net/if.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "common/StringUtil.h"
#ifdef __linux__
#include <unistd.h>
#include <sys/ioctl.h>
#endif
#if defined(__FreeBSD__) || (__APPLE__)
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/route.h>
#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<MAC_Address> 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<MAC_Address> 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<MAC_Address> AdapterUtils::GetAdapterMAC(ifaddrs* adapter)
{
Console.Error("DEV9: Unsupported OS, can't get MAC address");
return std::nullopt;
}
#endif
#endif
// AdapterIP.
#ifdef _WIN32
std::optional<IP_Address> AdapterUtils::GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter)

View File

@ -26,6 +26,7 @@
#include <string>
#include <optional>
#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<IP_ADAPTER_ADDRESSES[]>* buffer);
std::optional<PacketReader::MAC_Address> GetAdapterMAC(PIP_ADAPTER_ADDRESSES adapter);
std::optional<PacketReader::IP::IP_Address> GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter);
// Mask.
std::vector<PacketReader::IP::IP_Address> 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<PacketReader::MAC_Address> GetAdapterMAC(ifaddrs* adapter);
std::optional<PacketReader::IP::IP_Address> GetAdapterIP(ifaddrs* adapter);
// Mask.
std::vector<PacketReader::IP::IP_Address> GetGateways(ifaddrs* adapter);

View File

@ -23,14 +23,9 @@
#include "common/StringUtil.h"
#include <WinSock2.h>
#include <iphlpapi.h>
#elif defined(__linux__)
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#elif defined(__POSIX__)
#include <sys/types.h>
#include <ifaddrs.h>
#include <unistd.h>
#endif
#include <stdio.h>
@ -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<IP_ADAPTER_ADDRESSES[]> 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<PacketReader::MAC_Address> 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()
{

View File

@ -27,10 +27,6 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
#ifdef __linux__
#include <sys/ioctl.h>
#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<MAC_Address> 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 */