DEV9: Use unique_ptr via typedef for ifadddrs buffer

This commit is contained in:
TheLastRar 2023-01-17 13:29:57 +00:00 committed by lightningterror
parent 31fa1ea21e
commit 41a47f99f7
4 changed files with 34 additions and 40 deletions

View File

@ -42,7 +42,7 @@
using namespace PacketReader::IP;
#ifdef _WIN32
bool AdapterUtils::GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSES adapter, std::unique_ptr<IP_ADAPTER_ADDRESSES[]>* buffer)
bool AdapterUtils::GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSES adapter, AdapterBuffer* buffer)
{
int neededSize = 128;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> adapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
@ -91,7 +91,7 @@ bool AdapterUtils::GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSE
return false;
}
bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, std::unique_ptr<IP_ADAPTER_ADDRESSES[]>* buffer)
bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, AdapterBuffer* buffer)
{
int neededSize = 128;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> adapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
@ -168,16 +168,18 @@ bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, std::uniqu
return false;
}
#elif defined(__POSIX__)
bool AdapterUtils::GetIfAdapter(const std::string& name, ifaddrs* adapter, ifaddrs** buffer)
bool AdapterUtils::GetIfAdapter(const std::string& name, ifaddrs* adapter, AdapterBuffer* buffer)
{
ifaddrs* adapterInfo;
ifaddrs* ifa;
ifaddrs* pAdapter;
int error = getifaddrs(&adapterInfo);
int error = getifaddrs(&ifa);
if (error)
return false;
pAdapter = adapterInfo;
std::unique_ptr<ifaddrs, IfAdaptersDeleter> adapterInfo(ifa, IfAdaptersDeleter());
pAdapter = adapterInfo.get();
do
{
@ -190,23 +192,24 @@ bool AdapterUtils::GetIfAdapter(const std::string& name, ifaddrs* adapter, ifadd
if (pAdapter != nullptr)
{
*adapter = *pAdapter;
*buffer = adapterInfo;
buffer->swap(adapterInfo);
return true;
}
freeifaddrs(adapterInfo);
return false;
}
bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, ifaddrs** buffer)
bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, AdapterBuffer* buffer)
{
ifaddrs* adapterInfo;
ifaddrs* ifa;
ifaddrs* pAdapter;
int error = getifaddrs(&adapterInfo);
int error = getifaddrs(&ifa);
if (error)
return false;
pAdapter = adapterInfo;
std::unique_ptr<ifaddrs, IfAdaptersDeleter> adapterInfo(ifa, IfAdaptersDeleter());
pAdapter = adapterInfo.get();
do
{
@ -229,7 +232,7 @@ bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, ifaddrs** buffer)
if (hasIPv4 && hasGateway)
{
*adapter = *pAdapter;
*buffer = adapterInfo;
buffer->swap(adapterInfo);
return true;
}
}
@ -237,7 +240,6 @@ bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, ifaddrs** buffer)
pAdapter = pAdapter->ifa_next;
} while (pAdapter);
freeifaddrs(adapterInfo);
return false;
}
#endif

View File

@ -31,7 +31,8 @@
namespace AdapterUtils
{
#ifdef _WIN32
bool GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSES adapter, std::unique_ptr<IP_ADAPTER_ADDRESSES[]>* buffer);
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::IP::IP_Address> GetAdapterIP(PIP_ADAPTER_ADDRESSES adapter);
@ -39,8 +40,13 @@ namespace AdapterUtils
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__)
bool GetIfAdapter(const std::string& name, ifaddrs* adapter, ifaddrs** buffer);
bool GetIfAdapterAuto(ifaddrs* adapter, ifaddrs** buffer);
struct IfAdaptersDeleter
{
void operator()(ifaddrs* buffer) const { freeifaddrs(buffer); }
};
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::IP::IP_Address> GetAdapterIP(ifaddrs* adapter);
// Mask.

View File

@ -311,7 +311,7 @@ PCAPAdapter::PCAPAdapter()
#ifdef _WIN32
IP_ADAPTER_ADDRESSES adapter;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
AdapterUtils::AdapterBuffer buffer;
if (AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer))
InitInternalServer(&adapter);
else
@ -319,14 +319,12 @@ PCAPAdapter::PCAPAdapter()
Console.Error("DEV9: Failed to get adapter information");
InitInternalServer(nullptr);
}
#elif defined(__POSIX__)
ifaddrs adapter;
ifaddrs* buffer;
AdapterUtils::AdapterBuffer buffer;
if (AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer))
{
InitInternalServer(&adapter);
freeifaddrs(buffer);
}
else
{
Console.Error("DEV9: Failed to get adapter information");
@ -383,19 +381,16 @@ void PCAPAdapter::reloadSettings()
{
#ifdef _WIN32
IP_ADAPTER_ADDRESSES adapter;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
AdapterUtils::AdapterBuffer buffer;
if (AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer))
ReloadInternalServer(&adapter);
else
ReloadInternalServer(nullptr);
#elif defined(__POSIX__)
ifaddrs adapter;
ifaddrs* buffer;
AdapterUtils::AdapterBuffer buffer;
if (AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer))
{
ReloadInternalServer(&adapter);
freeifaddrs(buffer);
}
else
ReloadInternalServer(nullptr);
#endif

View File

@ -162,7 +162,7 @@ SocketAdapter::SocketAdapter()
#ifdef _WIN32
IP_ADAPTER_ADDRESSES adapter;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
AdapterUtils::AdapterBuffer buffer;
if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0)
{
@ -196,7 +196,7 @@ SocketAdapter::SocketAdapter()
}
#elif defined(__POSIX__)
ifaddrs adapter;
ifaddrs* buffer;
AdapterUtils::AdapterBuffer buffer;
if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0)
{
@ -214,7 +214,6 @@ SocketAdapter::SocketAdapter()
else
{
Console.Error("DEV9: Socket: Failed To Get Adapter IP");
freeifaddrs(buffer);
return;
}
}
@ -239,9 +238,6 @@ SocketAdapter::SocketAdapter()
const IP_Address gateway = internalIP;
InitInternalServer(&adapter, true, ps2IP, subnet, gateway);
#ifdef __POSIX__
freeifaddrs(buffer);
#endif
u8 hostMAC[6];
u8 newMAC[6];
@ -433,7 +429,7 @@ void SocketAdapter::reloadSettings()
bool foundAdapter = false;
#ifdef _WIN32
IP_ADAPTER_ADDRESSES adapter;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
AdapterUtils::AdapterBuffer buffer;
if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0)
foundAdapter = AdapterUtils::GetWin32Adapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer);
@ -442,7 +438,7 @@ void SocketAdapter::reloadSettings()
#elif defined(__POSIX__)
ifaddrs adapter;
ifaddrs* buffer;
AdapterUtils::AdapterBuffer buffer;
if (strcmp(EmuConfig.DEV9.EthDevice.c_str(), "Auto") != 0)
foundAdapter = AdapterUtils::GetIfAdapter(EmuConfig.DEV9.EthDevice, &adapter, &buffer);
@ -455,12 +451,7 @@ void SocketAdapter::reloadSettings()
const IP_Address gateway = internalIP;
if (foundAdapter)
{
ReloadInternalServer(&adapter, true, ps2IP, subnet, gateway);
#ifdef __POSIX__
freeifaddrs(buffer);
#endif
}
else
{
pxAssert(false);