DEV9: Unify GetAdapter method definitions

by providing a typedef for Adapter
This commit is contained in:
TheLastRar 2023-01-19 15:28:11 +00:00 committed by refractionpcsx2
parent 2db1e8fb81
commit e68d507659
4 changed files with 58 additions and 127 deletions

View File

@ -46,7 +46,7 @@ using namespace PacketReader;
using namespace PacketReader::IP; using namespace PacketReader::IP;
#ifdef _WIN32 #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; int neededSize = 128;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> adapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize); std::unique_ptr<IP_ADAPTER_ADDRESSES[]> adapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
@ -95,7 +95,7 @@ bool AdapterUtils::GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSE
return false; return false;
} }
bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, AdapterBuffer* buffer) bool AdapterUtils::GetAdapterAuto(Adapter* adapter, AdapterBuffer* buffer)
{ {
int neededSize = 128; int neededSize = 128;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> adapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize); std::unique_ptr<IP_ADAPTER_ADDRESSES[]> adapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
@ -172,7 +172,7 @@ bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, AdapterBuf
return false; return false;
} }
#elif defined(__POSIX__) #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* ifa;
ifaddrs* pAdapter; ifaddrs* pAdapter;
@ -202,7 +202,7 @@ bool AdapterUtils::GetIfAdapter(const std::string& name, ifaddrs* adapter, Adapt
return false; return false;
} }
bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, AdapterBuffer* buffer) bool AdapterUtils::GetAdapterAuto(Adapter* adapter, AdapterBuffer* buffer)
{ {
ifaddrs* ifa; ifaddrs* ifa;
ifaddrs* pAdapter; ifaddrs* pAdapter;
@ -250,7 +250,7 @@ bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, AdapterBuffer* buffer)
// AdapterMAC. // AdapterMAC.
#ifdef _WIN32 #ifdef _WIN32
std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(PIP_ADAPTER_ADDRESSES adapter) std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* adapter)
{ {
if (adapter != nullptr && adapter->PhysicalAddressLength == 6) if (adapter != nullptr && adapter->PhysicalAddressLength == 6)
return *(MAC_Address*)adapter->PhysicalAddress; return *(MAC_Address*)adapter->PhysicalAddress;
@ -259,7 +259,7 @@ std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(PIP_ADAPTER_ADDRESSES ada
} }
#elif defined(__POSIX__) #elif defined(__POSIX__)
#ifdef __linux__ #ifdef __linux__
std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(ifaddrs* adapter) std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* adapter)
{ {
struct ifreq ifr; struct ifreq ifr;
strcpy(ifr.ifr_name, adapter->ifa_name); strcpy(ifr.ifr_name, adapter->ifa_name);
@ -274,7 +274,7 @@ std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(ifaddrs* adapter)
return std::nullopt; return std::nullopt;
} }
#else #else
std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(ifaddrs* adapter) std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* adapter)
{ {
Console.Error("DEV9: Unsupported OS, can't get MAC address"); Console.Error("DEV9: Unsupported OS, can't get MAC address");
return std::nullopt; return std::nullopt;
@ -284,7 +284,7 @@ std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(ifaddrs* adapter)
// AdapterIP. // AdapterIP.
#ifdef _WIN32 #ifdef _WIN32
std::optional<IP_Address> AdapterUtils::GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter) std::optional<IP_Address> AdapterUtils::GetAdapterIP(Adapter* adapter)
{ {
PIP_ADAPTER_UNICAST_ADDRESS address = nullptr; PIP_ADAPTER_UNICAST_ADDRESS address = nullptr;
if (adapter != nullptr) if (adapter != nullptr)
@ -302,7 +302,7 @@ std::optional<IP_Address> AdapterUtils::GetAdapterIP(PIP_ADAPTER_ADDRESSES adapt
return std::nullopt; return std::nullopt;
} }
#elif defined(__POSIX__) #elif defined(__POSIX__)
std::optional<IP_Address> AdapterUtils::GetAdapterIP(ifaddrs* adapter) std::optional<IP_Address> AdapterUtils::GetAdapterIP(Adapter* adapter)
{ {
sockaddr* address = nullptr; sockaddr* address = nullptr;
if (adapter != nullptr) if (adapter != nullptr)
@ -322,7 +322,7 @@ std::optional<IP_Address> AdapterUtils::GetAdapterIP(ifaddrs* adapter)
// Gateways. // Gateways.
#ifdef _WIN32 #ifdef _WIN32
std::vector<IP_Address> AdapterUtils::GetGateways(PIP_ADAPTER_ADDRESSES adapter) std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
{ {
if (adapter == nullptr) if (adapter == nullptr)
return {}; return {};
@ -344,7 +344,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(PIP_ADAPTER_ADDRESSES adapter)
} }
#elif defined(__POSIX__) #elif defined(__POSIX__)
#ifdef __linux__ #ifdef __linux__
std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter) std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
{ {
// /proc/net/route contains some information about gateway addresses, // /proc/net/route contains some information about gateway addresses,
// and separates the information about by each interface. // and separates the information about by each interface.
@ -390,7 +390,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
return collection; return collection;
} }
#elif defined(__FreeBSD__) || (__APPLE__) #elif defined(__FreeBSD__) || (__APPLE__)
std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter) std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
{ {
if (adapter == nullptr) if (adapter == nullptr)
return {}; return {};
@ -470,7 +470,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
return collection; return collection;
} }
#else #else
std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter) std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
{ {
Console.Error("DEV9: Unsupported OS, can't find Gateway"); Console.Error("DEV9: Unsupported OS, can't find Gateway");
return {}; return {};
@ -480,7 +480,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
// DNS. // DNS.
#ifdef _WIN32 #ifdef _WIN32
std::vector<IP_Address> AdapterUtils::GetDNS(PIP_ADAPTER_ADDRESSES adapter) std::vector<IP_Address> AdapterUtils::GetDNS(Adapter* adapter)
{ {
if (adapter == nullptr) if (adapter == nullptr)
return {}; return {};
@ -501,7 +501,7 @@ std::vector<IP_Address> AdapterUtils::GetDNS(PIP_ADAPTER_ADDRESSES adapter)
return collection; return collection;
} }
#elif defined(__POSIX__) #elif defined(__POSIX__)
std::vector<IP_Address> AdapterUtils::GetDNS(ifaddrs* adapter) std::vector<IP_Address> AdapterUtils::GetDNS(Adapter* adapter)
{ {
// On Linux and OSX, DNS is system wide, not adapter specific, so we can ignore the adapter parameter. // On Linux and OSX, DNS is system wide, not adapter specific, so we can ignore the adapter parameter.

View File

@ -32,28 +32,22 @@
namespace AdapterUtils namespace AdapterUtils
{ {
#ifdef _WIN32 #ifdef _WIN32
typedef IP_ADAPTER_ADDRESSES Adapter;
typedef std::unique_ptr<IP_ADAPTER_ADDRESSES[]> AdapterBuffer; typedef std::unique_ptr<IP_ADAPTER_ADDRESSES[]> AdapterBuffer;
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);
std::vector<PacketReader::IP::IP_Address> GetDNS(PIP_ADAPTER_ADDRESSES adapter);
#elif defined(__POSIX__) #elif defined(__POSIX__)
typedef ifaddrs Adapter;
struct IfAdaptersDeleter struct IfAdaptersDeleter
{ {
void operator()(ifaddrs* buffer) const { freeifaddrs(buffer); } void operator()(ifaddrs* buffer) const { freeifaddrs(buffer); }
}; };
typedef std::unique_ptr<ifaddrs, IfAdaptersDeleter> AdapterBuffer; typedef std::unique_ptr<ifaddrs, IfAdaptersDeleter> AdapterBuffer;
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);
std::vector<PacketReader::IP::IP_Address> GetDNS(ifaddrs* adapter);
#endif #endif
bool GetAdapter(const std::string& name, Adapter* adapter, AdapterBuffer* buffer);
bool GetAdapterAuto(Adapter* adapter, AdapterBuffer* buffer);
std::optional<PacketReader::MAC_Address> GetAdapterMAC(Adapter* adapter);
std::optional<PacketReader::IP::IP_Address> GetAdapterIP(Adapter* adapter);
// Mask.
std::vector<PacketReader::IP::IP_Address> GetGateways(Adapter* adapter);
std::vector<PacketReader::IP::IP_Address> GetDNS(Adapter* adapter);
}; // namespace AdapterUtils }; // namespace AdapterUtils

View File

@ -251,36 +251,34 @@ PCAPAdapter::PCAPAdapter()
return; return;
#endif #endif
bool foundAdapter = false; AdapterUtils::Adapter adapter;
#ifdef _WIN32
IP_ADAPTER_ADDRESSES adapter;
AdapterUtils::AdapterBuffer buffer; AdapterUtils::AdapterBuffer buffer;
foundAdapter = AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer); if (AdapterUtils::GetAdapter(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(); std::optional<PacketReader::MAC_Address> adMAC = AdapterUtils::GetAdapterMAC(&adapter);
mac_address newMAC; if (adMAC.has_value())
memcpy(&newMAC, &ps2MAC, 6); {
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 //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((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;
}
} }
else else
{ {
Console.Error("DEV9: Failed to get MAC address for adapter"); Console.Error("DEV9: Failed to get adapter information");
return; return;
} }
@ -289,14 +287,8 @@ PCAPAdapter::PCAPAdapter()
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;
} }
if (foundAdapter) InitInternalServer(&adapter);
InitInternalServer(&adapter);
else
{
Console.Error("DEV9: Failed to get adapter information");
InitInternalServer(nullptr);
}
} }
AdapterOptions PCAPAdapter::GetAdapterOptions() AdapterOptions PCAPAdapter::GetAdapterOptions()
{ {
@ -345,21 +337,12 @@ bool PCAPAdapter::send(NetPacket* pkt)
void PCAPAdapter::reloadSettings() void PCAPAdapter::reloadSettings()
{ {
#ifdef _WIN32 AdapterUtils::Adapter adapter;
IP_ADAPTER_ADDRESSES adapter;
AdapterUtils::AdapterBuffer buffer; AdapterUtils::AdapterBuffer buffer;
if (AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer)) if (AdapterUtils::GetAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer))
ReloadInternalServer(&adapter); ReloadInternalServer(&adapter);
else else
ReloadInternalServer(nullptr); 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() PCAPAdapter::~PCAPAdapter()
@ -401,7 +384,7 @@ std::vector<AdapterEntry> PCAPAdapter::GetAdapters()
IP_ADAPTER_ADDRESSES adapterInfo; IP_ADAPTER_ADDRESSES adapterInfo;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer; std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
if (AdapterUtils::GetWin32Adapter(entry.guid, &adapterInfo, &buffer)) if (AdapterUtils::GetAdapter(entry.guid, &adapterInfo, &buffer))
entry.name = StringUtil::WideStringToUTF8String(std::wstring(adapterInfo.FriendlyName)); entry.name = StringUtil::WideStringToUTF8String(std::wstring(adapterInfo.FriendlyName));
else else
{ {

View File

@ -156,13 +156,12 @@ SocketAdapter::SocketAdapter()
{ {
bool foundAdapter; bool foundAdapter;
#ifdef _WIN32 AdapterUtils::Adapter adapter;
IP_ADAPTER_ADDRESSES adapter;
AdapterUtils::AdapterBuffer buffer; AdapterUtils::AdapterBuffer buffer;
if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0) 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) if (!foundAdapter)
{ {
@ -181,7 +180,7 @@ SocketAdapter::SocketAdapter()
} }
else else
{ {
foundAdapter = AdapterUtils::GetWin32AdapterAuto(&adapter, &buffer); foundAdapter = AdapterUtils::GetAdapterAuto(&adapter, &buffer);
adapterIP = {}; adapterIP = {};
if (!foundAdapter) if (!foundAdapter)
@ -190,41 +189,6 @@ SocketAdapter::SocketAdapter()
return; 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<IP_Address> 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 //For DHCP, we need to override some settings
//DNS settings as per direct adapters //DNS settings as per direct adapters
@ -409,24 +373,14 @@ void SocketAdapter::reset()
void SocketAdapter::reloadSettings() void SocketAdapter::reloadSettings()
{ {
bool foundAdapter = false; bool foundAdapter = false;
#ifdef _WIN32
IP_ADAPTER_ADDRESSES adapter; AdapterUtils::Adapter adapter;
AdapterUtils::AdapterBuffer buffer; AdapterUtils::AdapterBuffer buffer;
if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0) 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 else
foundAdapter = AdapterUtils::GetWin32AdapterAuto(&adapter, &buffer); foundAdapter = AdapterUtils::GetAdapterAuto(&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
const IP_Address ps2IP = {{{internalIP.bytes[0], internalIP.bytes[1], internalIP.bytes[2], 100}}}; const IP_Address ps2IP = {{{internalIP.bytes[0], internalIP.bytes[1], internalIP.bytes[2], 100}}};
const IP_Address subnet{{{255, 255, 255, 0}}}; const IP_Address subnet{{{255, 255, 255, 0}}};