mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-02-20 07:13:49 -07:00
Common/Network: Add GetSubnetMask function.
This commit is contained in:
parent
9bc96893fb
commit
930af97d95
@ -9,17 +9,21 @@
|
||||
#include <vector>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <arpa/inet.h>
|
||||
#include <cstring>
|
||||
#include <ifaddrs.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#else
|
||||
#include <WinSock2.h>
|
||||
#include <iphlpapi.h>
|
||||
#endif
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "Common/BitUtils.h"
|
||||
#include "Common/Buffer.h"
|
||||
#include "Common/CommonFuncs.h"
|
||||
#include "Common/Random.h"
|
||||
#include "Common/StringUtil.h"
|
||||
@ -101,6 +105,65 @@ std::optional<BluetoothAddress> StringToBluetoothAddress(std::string_view str)
|
||||
return std::bit_cast<BluetoothAddress>(*result);
|
||||
}
|
||||
|
||||
std::optional<IPAddress> GetSubnetMask(const IPAddress& address)
|
||||
{
|
||||
std::optional<IPAddress> result;
|
||||
|
||||
// Android apparently does not provide getifaddrs.
|
||||
// TODO: Provide an android implementation.
|
||||
#if !defined(ANDROID)
|
||||
const auto target_in_addr = std::bit_cast<in_addr>(address);
|
||||
|
||||
#if defined(_WIN32)
|
||||
static constexpr ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
|
||||
|
||||
// This is basically what Microsoft recommends.
|
||||
Common::UniqueBuffer<IP_ADAPTER_ADDRESSES> buffer(15000 / sizeof(IP_ADAPTER_ADDRESSES));
|
||||
auto buffer_size = ULONG(buffer.size() * sizeof(IP_ADAPTER_ADDRESSES));
|
||||
|
||||
if (GetAdaptersAddresses(AF_INET, flags, nullptr, buffer.data(), &buffer_size) != NO_ERROR)
|
||||
return result;
|
||||
|
||||
for (auto* addr = buffer.data(); addr != nullptr; addr = addr->Next)
|
||||
{
|
||||
for (auto* ua = addr->FirstUnicastAddress; ua != nullptr; ua = ua->Next)
|
||||
{
|
||||
auto* const sa = reinterpret_cast<SOCKADDR_IN*>(ua->Address.lpSockaddr);
|
||||
if (sa->sin_addr.s_addr != target_in_addr.s_addr)
|
||||
continue;
|
||||
|
||||
result = std::bit_cast<IPAddress>(htonl(u32(-1) << (32 - ua->OnLinkPrefixLength)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
#else
|
||||
ifaddrs* ifaddrs{};
|
||||
if (getifaddrs(&ifaddrs) == -1)
|
||||
return result;
|
||||
|
||||
for (auto* ifa = ifaddrs; ifa != nullptr; ifa = ifa->ifa_next)
|
||||
{
|
||||
if (ifa->ifa_addr->sa_family != AF_INET || ifa->ifa_addr == nullptr ||
|
||||
ifa->ifa_netmask == nullptr)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
auto* const ifaddr = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
|
||||
if (ifaddr->sin_addr.s_addr != target_in_addr.s_addr)
|
||||
continue;
|
||||
|
||||
auto* const mask = reinterpret_cast<sockaddr_in*>(ifa->ifa_netmask);
|
||||
result = std::bit_cast<IPAddress>(mask->sin_addr.s_addr);
|
||||
break;
|
||||
}
|
||||
|
||||
freeifaddrs(ifaddrs);
|
||||
#endif
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
EthernetHeader::EthernetHeader() = default;
|
||||
|
||||
EthernetHeader::EthernetHeader(u16 ether_type) : ethertype(htons(ether_type))
|
||||
|
||||
@ -281,6 +281,7 @@ std::optional<MACAddress> StringToMacAddress(std::string_view mac_string);
|
||||
std::string BluetoothAddressToString(BluetoothAddress bdaddr);
|
||||
std::optional<BluetoothAddress> StringToBluetoothAddress(std::string_view str);
|
||||
|
||||
std::optional<IPAddress> GetSubnetMask(const IPAddress& address);
|
||||
u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value = 0);
|
||||
u16 ComputeTCPNetworkChecksum(const IPAddress& from, const IPAddress& to, const void* data,
|
||||
u16 length, u8 protocol);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user