mirror of https://github.com/PCSX2/pcsx2.git
DEV9: Move logic for getting MAC address into AdapterUtils
This commit is contained in:
parent
df674d4056
commit
2db1e8fb81
|
@ -15,21 +15,24 @@
|
||||||
|
|
||||||
#include "PrecompiledHeader.h"
|
#include "PrecompiledHeader.h"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#ifdef __POSIX__
|
#ifdef __POSIX__
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
#include "common/StringUtil.h"
|
#include "common/StringUtil.h"
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(__FreeBSD__) || (__APPLE__)
|
#if defined(__FreeBSD__) || (__APPLE__)
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <net/if.h>
|
|
||||||
#include <net/route.h>
|
#include <net/route.h>
|
||||||
|
|
||||||
#include "common/Assertions.h"
|
#include "common/Assertions.h"
|
||||||
|
@ -39,6 +42,7 @@
|
||||||
|
|
||||||
#include "AdapterUtils.h"
|
#include "AdapterUtils.h"
|
||||||
|
|
||||||
|
using namespace PacketReader;
|
||||||
using namespace PacketReader::IP;
|
using namespace PacketReader::IP;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -244,6 +248,40 @@ bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, AdapterBuffer* buffer)
|
||||||
}
|
}
|
||||||
#endif
|
#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.
|
// AdapterIP.
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::optional<IP_Address> AdapterUtils::GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter)
|
std::optional<IP_Address> AdapterUtils::GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter)
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
#include "DEV9/PacketReader/MAC_Address.h"
|
||||||
#include "DEV9/PacketReader/IP/IP_Address.h"
|
#include "DEV9/PacketReader/IP/IP_Address.h"
|
||||||
|
|
||||||
namespace AdapterUtils
|
namespace AdapterUtils
|
||||||
|
@ -35,6 +36,7 @@ namespace AdapterUtils
|
||||||
bool GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSES adapter, AdapterBuffer* buffer);
|
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);
|
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);
|
std::optional<PacketReader::IP::IP_Address> GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter);
|
||||||
// Mask.
|
// Mask.
|
||||||
std::vector<PacketReader::IP::IP_Address> GetGateways(PIP_ADAPTER_ADDRESSES adapter);
|
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 GetIfAdapter(const std::string& name, ifaddrs* adapter, AdapterBuffer* buffer);
|
||||||
bool GetIfAdapterAuto(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);
|
std::optional<PacketReader::IP::IP_Address> GetAdapterIP(ifaddrs* adapter);
|
||||||
// Mask.
|
// Mask.
|
||||||
std::vector<PacketReader::IP::IP_Address> GetGateways(ifaddrs* adapter);
|
std::vector<PacketReader::IP::IP_Address> GetGateways(ifaddrs* adapter);
|
||||||
|
|
|
@ -23,14 +23,9 @@
|
||||||
#include "common/StringUtil.h"
|
#include "common/StringUtil.h"
|
||||||
#include <WinSock2.h>
|
#include <WinSock2.h>
|
||||||
#include <iphlpapi.h>
|
#include <iphlpapi.h>
|
||||||
#elif defined(__linux__)
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <net/if.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#elif defined(__POSIX__)
|
#elif defined(__POSIX__)
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <ifaddrs.h>
|
#include <ifaddrs.h>
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -64,40 +59,6 @@ ip_address ps2_ip;
|
||||||
mac_address ps2_mac;
|
mac_address ps2_mac;
|
||||||
mac_address host_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)
|
int pcap_io_init(const std::string& adapter, bool switched, mac_address virtual_mac)
|
||||||
{
|
{
|
||||||
struct bpf_program fp;
|
struct bpf_program fp;
|
||||||
|
@ -290,10 +251,23 @@ PCAPAdapter::PCAPAdapter()
|
||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
mac_address hostMAC;
|
bool foundAdapter = false;
|
||||||
mac_address newMAC;
|
|
||||||
|
|
||||||
GetMACAddress(EmuConfig.DEV9.EthDevice, &hostMAC);
|
#ifdef _WIN32
|
||||||
|
IP_ADAPTER_ADDRESSES 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<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);
|
memcpy(&newMAC, &ps2MAC, 6);
|
||||||
|
|
||||||
//Lets take the hosts last 2 bytes to make it unique on Xlink
|
//Lets take the hosts last 2 bytes to make it unique on Xlink
|
||||||
|
@ -303,35 +277,26 @@ PCAPAdapter::PCAPAdapter()
|
||||||
SetMACAddress((PacketReader::MAC_Address*)&newMAC);
|
SetMACAddress((PacketReader::MAC_Address*)&newMAC);
|
||||||
host_mac = hostMAC;
|
host_mac = hostMAC;
|
||||||
ps2_mac = newMAC; //Needed outside of this class
|
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, newMAC) == -1)
|
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());
|
Console.Error("DEV9: Can't open Device '%s'", EmuConfig.DEV9.EthDevice.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
if (foundAdapter)
|
||||||
IP_ADAPTER_ADDRESSES adapter;
|
|
||||||
AdapterUtils::AdapterBuffer buffer;
|
|
||||||
if (AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer))
|
|
||||||
InitInternalServer(&adapter);
|
InitInternalServer(&adapter);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Console.Error("DEV9: Failed to get adapter information");
|
Console.Error("DEV9: Failed to get adapter information");
|
||||||
InitInternalServer(nullptr);
|
InitInternalServer(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(__POSIX__)
|
|
||||||
ifaddrs adapter;
|
|
||||||
AdapterUtils::AdapterBuffer buffer;
|
|
||||||
if (AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer))
|
|
||||||
InitInternalServer(&adapter);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.Error("DEV9: Failed to get adapter information");
|
|
||||||
InitInternalServer(nullptr);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
AdapterOptions PCAPAdapter::GetAdapterOptions()
|
AdapterOptions PCAPAdapter::GetAdapterOptions()
|
||||||
{
|
{
|
||||||
|
|
|
@ -27,10 +27,6 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <unistd.h>
|
|
||||||
#ifdef __linux__
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "sockets.h"
|
#include "sockets.h"
|
||||||
|
@ -239,34 +235,20 @@ SocketAdapter::SocketAdapter()
|
||||||
|
|
||||||
InitInternalServer(&adapter, true, ps2IP, subnet, gateway);
|
InitInternalServer(&adapter, true, ps2IP, subnet, gateway);
|
||||||
|
|
||||||
MAC_Address hostMAC;
|
std::optional<MAC_Address> adMAC = AdapterUtils::GetAdapterMAC(&adapter);
|
||||||
MAC_Address newMAC;
|
if (adMAC.has_value())
|
||||||
|
|
||||||
#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
|
|
||||||
{
|
{
|
||||||
hostMAC = ps2MAC;
|
MAC_Address hostMAC = adMAC.value();
|
||||||
Console.Error("Could not get MAC address for adapter: %s", adapter.ifa_name);
|
MAC_Address newMAC = ps2MAC;
|
||||||
}
|
|
||||||
::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
|
//Lets take the hosts last 2 bytes to make it unique on Xlink
|
||||||
newMAC.bytes[5] = hostMAC.bytes[4];
|
newMAC.bytes[5] = hostMAC.bytes[4];
|
||||||
newMAC.bytes[4] = hostMAC.bytes[5];
|
newMAC.bytes[4] = hostMAC.bytes[5];
|
||||||
|
|
||||||
SetMACAddress(&newMAC);
|
SetMACAddress(&newMAC);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
Console.Error("DEV9: Socket: Failed to get MAC address for adapter");
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
|
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
|
||||||
|
|
Loading…
Reference in New Issue