mirror of https://github.com/PCSX2/pcsx2.git
DEV9: AdapterUtils formatting
This commit is contained in:
parent
fb49c71118
commit
31fa1ea21e
|
@ -45,7 +45,7 @@ using namespace PacketReader::IP;
|
||||||
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, std::unique_ptr<IP_ADAPTER_ADDRESSES[]>* 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);
|
||||||
ULONG dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
ULONG dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
||||||
|
|
||||||
PIP_ADAPTER_ADDRESSES pAdapterInfo;
|
PIP_ADAPTER_ADDRESSES pAdapterInfo;
|
||||||
|
@ -54,14 +54,14 @@ bool AdapterUtils::GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSE
|
||||||
AF_UNSPEC,
|
AF_UNSPEC,
|
||||||
GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS,
|
GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS,
|
||||||
NULL,
|
NULL,
|
||||||
AdapterInfo.get(),
|
adapterInfo.get(),
|
||||||
&dwBufLen);
|
&dwBufLen);
|
||||||
|
|
||||||
if (dwStatus == ERROR_BUFFER_OVERFLOW)
|
if (dwStatus == ERROR_BUFFER_OVERFLOW)
|
||||||
{
|
{
|
||||||
DevCon.WriteLn("DEV9: GetWin32Adapter() buffer too small, resizing");
|
DevCon.WriteLn("DEV9: GetWin32Adapter() buffer too small, resizing");
|
||||||
neededSize = dwBufLen / sizeof(IP_ADAPTER_ADDRESSES) + 1;
|
neededSize = dwBufLen / sizeof(IP_ADAPTER_ADDRESSES) + 1;
|
||||||
AdapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
|
adapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
|
||||||
dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
||||||
DevCon.WriteLn("DEV9: New size %i", neededSize);
|
DevCon.WriteLn("DEV9: New size %i", neededSize);
|
||||||
|
|
||||||
|
@ -69,31 +69,32 @@ bool AdapterUtils::GetWin32Adapter(const std::string& name, PIP_ADAPTER_ADDRESSE
|
||||||
AF_UNSPEC,
|
AF_UNSPEC,
|
||||||
GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS,
|
GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS,
|
||||||
NULL,
|
NULL,
|
||||||
AdapterInfo.get(),
|
adapterInfo.get(),
|
||||||
&dwBufLen);
|
&dwBufLen);
|
||||||
}
|
}
|
||||||
if (dwStatus != ERROR_SUCCESS)
|
if (dwStatus != ERROR_SUCCESS)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
pAdapterInfo = AdapterInfo.get();
|
pAdapterInfo = adapterInfo.get();
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (strcmp(pAdapterInfo->AdapterName, name.c_str()) == 0)
|
if (strcmp(pAdapterInfo->AdapterName, name.c_str()) == 0)
|
||||||
{
|
{
|
||||||
*adapter = *pAdapterInfo;
|
*adapter = *pAdapterInfo;
|
||||||
buffer->swap(AdapterInfo);
|
buffer->swap(adapterInfo);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pAdapterInfo = pAdapterInfo->Next;
|
pAdapterInfo = pAdapterInfo->Next;
|
||||||
} while (pAdapterInfo);
|
} while (pAdapterInfo);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, std::unique_ptr<IP_ADAPTER_ADDRESSES[]>* buffer)
|
bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, std::unique_ptr<IP_ADAPTER_ADDRESSES[]>* 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);
|
||||||
ULONG dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
ULONG dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
||||||
|
|
||||||
PIP_ADAPTER_ADDRESSES pAdapter;
|
PIP_ADAPTER_ADDRESSES pAdapter;
|
||||||
|
@ -102,7 +103,7 @@ bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, std::uniqu
|
||||||
AF_UNSPEC,
|
AF_UNSPEC,
|
||||||
GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS,
|
GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS,
|
||||||
NULL,
|
NULL,
|
||||||
AdapterInfo.get(),
|
adapterInfo.get(),
|
||||||
&dwBufLen);
|
&dwBufLen);
|
||||||
|
|
||||||
if (dwStatus == ERROR_BUFFER_OVERFLOW)
|
if (dwStatus == ERROR_BUFFER_OVERFLOW)
|
||||||
|
@ -110,7 +111,7 @@ bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, std::uniqu
|
||||||
DevCon.WriteLn("DEV9: PCAPGetWin32Adapter() buffer too small, resizing");
|
DevCon.WriteLn("DEV9: PCAPGetWin32Adapter() buffer too small, resizing");
|
||||||
//
|
//
|
||||||
neededSize = dwBufLen / sizeof(IP_ADAPTER_ADDRESSES) + 1;
|
neededSize = dwBufLen / sizeof(IP_ADAPTER_ADDRESSES) + 1;
|
||||||
AdapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
|
adapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
|
||||||
dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
||||||
DevCon.WriteLn("DEV9: New size %i", neededSize);
|
DevCon.WriteLn("DEV9: New size %i", neededSize);
|
||||||
|
|
||||||
|
@ -118,45 +119,45 @@ bool AdapterUtils::GetWin32AdapterAuto(PIP_ADAPTER_ADDRESSES adapter, std::uniqu
|
||||||
AF_UNSPEC,
|
AF_UNSPEC,
|
||||||
GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS,
|
GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS,
|
||||||
NULL,
|
NULL,
|
||||||
AdapterInfo.get(),
|
adapterInfo.get(),
|
||||||
&dwBufLen);
|
&dwBufLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dwStatus != ERROR_SUCCESS)
|
if (dwStatus != ERROR_SUCCESS)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
pAdapter = AdapterInfo.get();
|
pAdapter = adapterInfo.get();
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (pAdapter->IfType != IF_TYPE_SOFTWARE_LOOPBACK &&
|
if (pAdapter->IfType != IF_TYPE_SOFTWARE_LOOPBACK &&
|
||||||
pAdapter->OperStatus == IfOperStatusUp)
|
pAdapter->OperStatus == IfOperStatusUp)
|
||||||
{
|
{
|
||||||
//Search for an adapter with;
|
// Search for an adapter with;
|
||||||
//IPv4 Address
|
// IPv4 Address,
|
||||||
//DNS
|
// DNS,
|
||||||
//Gateway
|
// Gateway.
|
||||||
|
|
||||||
bool hasIPv4 = false;
|
bool hasIPv4 = false;
|
||||||
bool hasDNS = false;
|
bool hasDNS = false;
|
||||||
bool hasGateway = false;
|
bool hasGateway = false;
|
||||||
|
|
||||||
//IPv4
|
// IPv4.
|
||||||
if (GetAdapterIP(pAdapter).has_value())
|
if (GetAdapterIP(pAdapter).has_value())
|
||||||
hasIPv4 = true;
|
hasIPv4 = true;
|
||||||
|
|
||||||
//DNS
|
// DNS.
|
||||||
if (GetDNS(pAdapter).size() > 0)
|
if (GetDNS(pAdapter).size() > 0)
|
||||||
hasDNS = true;
|
hasDNS = true;
|
||||||
|
|
||||||
//Gateway
|
// Gateway.
|
||||||
if (GetGateways(pAdapter).size() > 0)
|
if (GetGateways(pAdapter).size() > 0)
|
||||||
hasGateway = true;
|
hasGateway = true;
|
||||||
|
|
||||||
if (hasIPv4 && hasDNS && hasGateway)
|
if (hasIPv4 && hasDNS && hasGateway)
|
||||||
{
|
{
|
||||||
*adapter = *pAdapter;
|
*adapter = *pAdapter;
|
||||||
buffer->swap(AdapterInfo);
|
buffer->swap(adapterInfo);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,9 +213,9 @@ bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, ifaddrs** buffer)
|
||||||
if ((pAdapter->ifa_flags & IFF_LOOPBACK) == 0 &&
|
if ((pAdapter->ifa_flags & IFF_LOOPBACK) == 0 &&
|
||||||
(pAdapter->ifa_flags & IFF_UP) != 0)
|
(pAdapter->ifa_flags & IFF_UP) != 0)
|
||||||
{
|
{
|
||||||
//Search for an adapter with;
|
// Search for an adapter with;
|
||||||
//IPv4 Address
|
// IPv4 Address,
|
||||||
//Gateway
|
// Gateway.
|
||||||
|
|
||||||
bool hasIPv4 = false;
|
bool hasIPv4 = false;
|
||||||
bool hasGateway = false;
|
bool hasGateway = false;
|
||||||
|
@ -241,7 +242,7 @@ bool AdapterUtils::GetIfAdapterAuto(ifaddrs* adapter, ifaddrs** buffer)
|
||||||
}
|
}
|
||||||
#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)
|
||||||
{
|
{
|
||||||
|
@ -279,7 +280,7 @@ std::optional<IP_Address> AdapterUtils::GetAdapterIP(ifaddrs* adapter)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//Gateways
|
// Gateways.
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::vector<IP_Address> AdapterUtils::GetGateways(PIP_ADAPTER_ADDRESSES adapter)
|
std::vector<IP_Address> AdapterUtils::GetGateways(PIP_ADAPTER_ADDRESSES adapter)
|
||||||
{
|
{
|
||||||
|
@ -305,8 +306,8 @@ std::vector<IP_Address> AdapterUtils::GetGateways(PIP_ADAPTER_ADDRESSES adapter)
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
|
std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* 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.
|
||||||
if (adapter == nullptr)
|
if (adapter == nullptr)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
|
@ -325,8 +326,8 @@ std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
|
||||||
routeLines.push_back(line);
|
routeLines.push_back(line);
|
||||||
route.close();
|
route.close();
|
||||||
|
|
||||||
//Columns are as follows (first-line header):
|
// Columns are as follows (first-line header):
|
||||||
//Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
// Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT.
|
||||||
for (size_t i = 1; i < routeLines.size(); i++)
|
for (size_t i = 1; i < routeLines.size(); i++)
|
||||||
{
|
{
|
||||||
std::string line = routeLines[i];
|
std::string line = routeLines[i];
|
||||||
|
@ -356,7 +357,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
|
||||||
|
|
||||||
std::vector<IP_Address> collection;
|
std::vector<IP_Address> collection;
|
||||||
|
|
||||||
//Get index for our adapter by matching the adapter name
|
// Get index for our adapter by matching the adapter name.
|
||||||
int ifIndex = -1;
|
int ifIndex = -1;
|
||||||
|
|
||||||
struct if_nameindex* ifNI;
|
struct if_nameindex* ifNI;
|
||||||
|
@ -379,14 +380,14 @@ std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
|
||||||
}
|
}
|
||||||
if_freenameindex(ifNI);
|
if_freenameindex(ifNI);
|
||||||
|
|
||||||
//Check if we found the adapter
|
// Check if we found the adapter.
|
||||||
if (ifIndex == -1)
|
if (ifIndex == -1)
|
||||||
{
|
{
|
||||||
Console.Error("DEV9: Failed to get index for adapter");
|
Console.Error("DEV9: Failed to get index for adapter");
|
||||||
return collection;
|
return collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Find the gateway by looking though the routing information
|
// Find the gateway by looking though the routing information.
|
||||||
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;
|
||||||
|
|
||||||
|
@ -396,7 +397,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
|
||||||
return collection;
|
return collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
//len 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<u8[]> buffer = std::make_unique<u8[]>(bufferLen);
|
||||||
|
|
||||||
|
@ -416,7 +417,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
|
||||||
sockaddr* sockaddrs = (sockaddr*)(hdr + 1);
|
sockaddr* sockaddrs = (sockaddr*)(hdr + 1);
|
||||||
pxAssert(sockaddrs[RTAX_DST].sa_family == AF_INET);
|
pxAssert(sockaddrs[RTAX_DST].sa_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 = (sockaddr_in*)&sockaddrs[RTAX_DST];
|
||||||
if (sockaddr->sin_addr.s_addr != 0)
|
if (sockaddr->sin_addr.s_addr != 0)
|
||||||
continue;
|
continue;
|
||||||
|
@ -437,7 +438,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(ifaddrs* adapter)
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//DNS
|
// DNS.
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::vector<IP_Address> AdapterUtils::GetDNS(PIP_ADAPTER_ADDRESSES adapter)
|
std::vector<IP_Address> AdapterUtils::GetDNS(PIP_ADAPTER_ADDRESSES adapter)
|
||||||
{
|
{
|
||||||
|
@ -462,7 +463,7 @@ std::vector<IP_Address> AdapterUtils::GetDNS(PIP_ADAPTER_ADDRESSES adapter)
|
||||||
#elif defined(__POSIX__)
|
#elif defined(__POSIX__)
|
||||||
std::vector<IP_Address> AdapterUtils::GetDNS(ifaddrs* adapter)
|
std::vector<IP_Address> AdapterUtils::GetDNS(ifaddrs* adapter)
|
||||||
{
|
{
|
||||||
//On Linux and OSX, DNS is system wide, not adapter specific, so we can ignore adapter
|
// On Linux and OSX, DNS is system wide, not adapter specific, so we can ignore the adapter parameter.
|
||||||
|
|
||||||
// Parse /etc/resolv.conf for all of the "nameserver" entries.
|
// Parse /etc/resolv.conf for all of the "nameserver" entries.
|
||||||
// These are the DNS servers the machine is configured to use.
|
// These are the DNS servers the machine is configured to use.
|
||||||
|
|
|
@ -35,7 +35,7 @@ namespace AdapterUtils
|
||||||
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::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);
|
||||||
std::vector<PacketReader::IP::IP_Address> GetDNS(PIP_ADAPTER_ADDRESSES adapter);
|
std::vector<PacketReader::IP::IP_Address> GetDNS(PIP_ADAPTER_ADDRESSES adapter);
|
||||||
#elif defined(__POSIX__)
|
#elif defined(__POSIX__)
|
||||||
|
@ -43,7 +43,7 @@ namespace AdapterUtils
|
||||||
bool GetIfAdapterAuto(ifaddrs* adapter, ifaddrs** buffer);
|
bool GetIfAdapterAuto(ifaddrs* adapter, ifaddrs** buffer);
|
||||||
|
|
||||||
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);
|
||||||
std::vector<PacketReader::IP::IP_Address> GetDNS(ifaddrs* adapter);
|
std::vector<PacketReader::IP::IP_Address> GetDNS(ifaddrs* adapter);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue