mirror of https://github.com/PCSX2/pcsx2.git
DEV9: Eliminate c-style casts in AdapterUtils
This commit is contained in:
parent
a6ddbdb879
commit
38f02de318
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#include "AdapterUtils.h"
|
#include "AdapterUtils.h"
|
||||||
|
|
||||||
|
#include <bit>
|
||||||
|
|
||||||
#include "common/Assertions.h"
|
#include "common/Assertions.h"
|
||||||
#include "common/Console.h"
|
#include "common/Console.h"
|
||||||
#include "common/ScopedGuard.h"
|
#include "common/ScopedGuard.h"
|
||||||
|
@ -246,14 +248,18 @@ bool AdapterUtils::GetAdapterAuto(Adapter* adapter, AdapterBuffer* buffer)
|
||||||
std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* 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;
|
{
|
||||||
|
MAC_Address macAddr{};
|
||||||
|
std::memcpy(&macAddr, adapter->PhysicalAddress, sizeof(macAddr));
|
||||||
|
return macAddr;
|
||||||
|
}
|
||||||
|
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* adapter)
|
std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* adapter)
|
||||||
{
|
{
|
||||||
MAC_Address macAddr = {};
|
MAC_Address macAddr{};
|
||||||
#if defined(AF_LINK)
|
#if defined(AF_LINK)
|
||||||
ifaddrs* adapterInfo;
|
ifaddrs* adapterInfo;
|
||||||
const int error = getifaddrs(&adapterInfo);
|
const int error = getifaddrs(&adapterInfo);
|
||||||
|
@ -290,7 +296,7 @@ std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* adapter)
|
||||||
const ScopedGuard sd_guard = [&sd]() {
|
const ScopedGuard sd_guard = [&sd]() {
|
||||||
close(sd);
|
close(sd);
|
||||||
};
|
};
|
||||||
struct ifreq ifr = {};
|
struct ifreq ifr{};
|
||||||
StringUtil::Strlcpy(ifr.ifr_name, adapter->ifa_name, std::size(ifr.ifr_name));
|
StringUtil::Strlcpy(ifr.ifr_name, adapter->ifa_name, std::size(ifr.ifr_name));
|
||||||
#if defined(SIOCGIFHWADDR)
|
#if defined(SIOCGIFHWADDR)
|
||||||
if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
|
if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
|
||||||
|
@ -329,26 +335,24 @@ std::optional<IP_Address> AdapterUtils::GetAdapterIP(Adapter* adapter)
|
||||||
|
|
||||||
if (address != nullptr)
|
if (address != nullptr)
|
||||||
{
|
{
|
||||||
sockaddr_in* sockaddr = (sockaddr_in*)address->Address.lpSockaddr;
|
sockaddr_in* sockaddr = reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr);
|
||||||
return *(IP_Address*)&sockaddr->sin_addr;
|
return std::bit_cast<IP_Address>(sockaddr->sin_addr);
|
||||||
}
|
}
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
#elif defined(__POSIX__)
|
#elif defined(__POSIX__)
|
||||||
std::optional<IP_Address> AdapterUtils::GetAdapterIP(Adapter* adapter)
|
std::optional<IP_Address> AdapterUtils::GetAdapterIP(Adapter* adapter)
|
||||||
{
|
{
|
||||||
sockaddr* address = nullptr;
|
sockaddr_in* address = nullptr;
|
||||||
if (adapter != nullptr)
|
if (adapter != nullptr)
|
||||||
{
|
{
|
||||||
if (adapter->ifa_addr != nullptr && adapter->ifa_addr->sa_family == AF_INET)
|
if (adapter->ifa_addr != nullptr && adapter->ifa_addr->sa_family == AF_INET)
|
||||||
address = adapter->ifa_addr;
|
address = reinterpret_cast<sockaddr_in*>(adapter->ifa_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (address != nullptr)
|
if (address != nullptr)
|
||||||
{
|
return std::bit_cast<IP_Address>(address->sin_addr);
|
||||||
sockaddr_in* sockaddr = (sockaddr_in*)address;
|
|
||||||
return *(IP_Address*)&sockaddr->sin_addr;
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -367,8 +371,8 @@ std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
|
||||||
{
|
{
|
||||||
if (address->Address.lpSockaddr->sa_family == AF_INET)
|
if (address->Address.lpSockaddr->sa_family == AF_INET)
|
||||||
{
|
{
|
||||||
sockaddr_in* sockaddr = (sockaddr_in*)address->Address.lpSockaddr;
|
sockaddr_in* sockaddr = reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr);
|
||||||
collection.push_back(*(IP_Address*)&sockaddr->sin_addr);
|
collection.push_back(std::bit_cast<IP_Address>(sockaddr->sin_addr));
|
||||||
}
|
}
|
||||||
address = address->Next;
|
address = address->Next;
|
||||||
}
|
}
|
||||||
|
@ -414,10 +418,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
|
||||||
u32 addressValue = static_cast<u32>(std::stoul(gatewayIPHex, 0, 16));
|
u32 addressValue = static_cast<u32>(std::stoul(gatewayIPHex, 0, 16));
|
||||||
// Skip device routes without valid NextHop IP address.
|
// Skip device routes without valid NextHop IP address.
|
||||||
if (addressValue != 0)
|
if (addressValue != 0)
|
||||||
{
|
collection.push_back(std::bit_cast<IP_Address>(addressValue));
|
||||||
IP_Address gwIP = *(IP_Address*)&addressValue;
|
|
||||||
collection.push_back(gwIP);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return collection;
|
return collection;
|
||||||
|
@ -461,6 +462,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the gateway by looking though the routing information.
|
// Find the gateway by looking though the routing information.
|
||||||
|
// Ask only for AF_NET, so we can assume any given sockaddr is a sockaddr_in
|
||||||
int name[] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_DUMP, 0};
|
int name[] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_DUMP, 0};
|
||||||
size_t bufferLen = 0;
|
size_t bufferLen = 0;
|
||||||
|
|
||||||
|
@ -472,7 +474,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
|
||||||
|
|
||||||
// bufferLen is an estimate, double it to be safe.
|
// bufferLen is an estimate, double it to be safe.
|
||||||
bufferLen *= 2;
|
bufferLen *= 2;
|
||||||
std::unique_ptr<u8[]> buffer = std::make_unique<u8[]>(bufferLen);
|
std::unique_ptr<std::byte[]> buffer = std::make_unique<std::byte[]>(bufferLen);
|
||||||
|
|
||||||
if (sysctl(name, 6, buffer.get(), &bufferLen, NULL, 0) != 0)
|
if (sysctl(name, 6, buffer.get(), &bufferLen, NULL, 0) != 0)
|
||||||
{
|
{
|
||||||
|
@ -483,21 +485,21 @@ std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
|
||||||
rt_msghdr* hdr;
|
rt_msghdr* hdr;
|
||||||
for (size_t i = 0; i < bufferLen; i += hdr->rtm_msglen)
|
for (size_t i = 0; i < bufferLen; i += hdr->rtm_msglen)
|
||||||
{
|
{
|
||||||
hdr = (rt_msghdr*)&buffer[i];
|
// Relying on implicit object creation for following code
|
||||||
|
hdr = reinterpret_cast<rt_msghdr*>(&buffer[i]);
|
||||||
|
|
||||||
if (hdr->rtm_flags & RTF_GATEWAY && hdr->rtm_addrs & RTA_GATEWAY && (hdr->rtm_index == ifIndex))
|
if (hdr->rtm_flags & RTF_GATEWAY && hdr->rtm_addrs & RTA_GATEWAY && (hdr->rtm_index == ifIndex))
|
||||||
{
|
{
|
||||||
sockaddr* sockaddrs = (sockaddr*)(hdr + 1);
|
sockaddr_in* sockaddrs = reinterpret_cast<sockaddr_in*>(hdr + 1);
|
||||||
pxAssert(sockaddrs[RTAX_DST].sa_family == AF_INET);
|
pxAssert(sockaddrs[RTAX_DST].sin_family == AF_INET);
|
||||||
|
|
||||||
// Default gateway has no destination address.
|
// Default gateway has no destination address.
|
||||||
sockaddr_in* sockaddr = (sockaddr_in*)&sockaddrs[RTAX_DST];
|
sockaddr_in* sockaddr = &sockaddrs[RTAX_DST];
|
||||||
if (sockaddr->sin_addr.s_addr != 0)
|
if (sockaddr->sin_addr.s_addr != 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
sockaddr = (sockaddr_in*)&sockaddrs[RTAX_GATEWAY];
|
sockaddr = &sockaddrs[RTAX_GATEWAY];
|
||||||
IP_Address gwIP = *(IP_Address*)&sockaddr->sin_addr;
|
collection.push_back(std::bit_cast<IP_Address>(sockaddr->sin_addr));
|
||||||
collection.push_back(gwIP);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return collection;
|
return collection;
|
||||||
|
@ -525,8 +527,8 @@ std::vector<IP_Address> AdapterUtils::GetDNS(Adapter* adapter)
|
||||||
{
|
{
|
||||||
if (address->Address.lpSockaddr->sa_family == AF_INET)
|
if (address->Address.lpSockaddr->sa_family == AF_INET)
|
||||||
{
|
{
|
||||||
sockaddr_in* sockaddr = (sockaddr_in*)address->Address.lpSockaddr;
|
sockaddr_in* sockaddr = reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr);
|
||||||
collection.push_back(*(IP_Address*)&sockaddr->sin_addr);
|
collection.push_back(std::bit_cast<IP_Address>(sockaddr->sin_addr));
|
||||||
}
|
}
|
||||||
address = address->Next;
|
address = address->Next;
|
||||||
}
|
}
|
||||||
|
@ -562,7 +564,7 @@ std::vector<IP_Address> AdapterUtils::GetDNS(Adapter* adapter)
|
||||||
serversLines.push_back(line);
|
serversLines.push_back(line);
|
||||||
servers.close();
|
servers.close();
|
||||||
|
|
||||||
const IP_Address systemdDNS{127, 0, 0, 53};
|
const IP_Address systemdDNS{{{127, 0, 0, 53}}};
|
||||||
for (size_t i = 1; i < serversLines.size(); i++)
|
for (size_t i = 1; i < serversLines.size(); i++)
|
||||||
{
|
{
|
||||||
std::string line = serversLines[i];
|
std::string line = serversLines[i];
|
||||||
|
|
Loading…
Reference in New Issue