2023-10-24 13:18:03 +00:00
|
|
|
#include "ip.hpp"
|
|
|
|
#include "ip_range.hpp"
|
2022-07-28 16:07:38 +00:00
|
|
|
#include "net.hpp"
|
|
|
|
#include "net_if.hpp"
|
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/constants/platform.hpp>
|
2022-07-28 16:07:38 +00:00
|
|
|
#include <llarp/util/logging.hpp>
|
|
|
|
#include <llarp/util/str.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/win32/exception.hpp>
|
2022-07-28 16:07:38 +00:00
|
|
|
|
|
|
|
#include <iphlpapi.h>
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <list>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <stdexcept>
|
2022-07-28 16:07:38 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace llarp::net
|
|
|
|
{
|
2024-02-01 12:43:43 +00:00
|
|
|
class Platform_Impl : public Platform
|
2022-07-28 16:07:38 +00:00
|
|
|
{
|
2024-02-01 12:43:43 +00:00
|
|
|
/// visit all adapters (not addresses). windows serves net info per adapter unlink posix
|
|
|
|
/// which gives a list of all distinct addresses.
|
|
|
|
template <typename Visit_t>
|
|
|
|
void iter_adapters(Visit_t&& visit, int af = AF_UNSPEC) const
|
|
|
|
{
|
|
|
|
ULONG err;
|
|
|
|
ULONG sz = 15000; // MS-recommended so that it "never fails", but often fails with a
|
|
|
|
// too large error.
|
2024-02-05 13:46:53 +00:00
|
|
|
std::unique_ptr<uint8_t[]> ptr;
|
2024-02-01 12:43:43 +00:00
|
|
|
PIP_ADAPTER_ADDRESSES addr;
|
|
|
|
int tries = 0;
|
|
|
|
do
|
|
|
|
{
|
2024-02-05 13:46:53 +00:00
|
|
|
ptr = std::make_unique<uint8_t[]>(sz);
|
2024-02-01 12:43:43 +00:00
|
|
|
addr = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(ptr.get());
|
2024-02-02 20:50:48 +00:00
|
|
|
err = GetAdaptersAddresses(af, GAA_FLAG_INCLUDE_GATEWAYS | GAA_FLAG_INCLUDE_PREFIX, nullptr, addr, &sz);
|
2024-02-01 12:43:43 +00:00
|
|
|
} while (err == ERROR_BUFFER_OVERFLOW and ++tries < 4);
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
throw llarp::win32::error{err, "GetAdaptersAddresses()"};
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
for (; addr; addr = addr->Next)
|
|
|
|
visit(addr);
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
template <typename adapter_t>
|
|
|
|
bool adapter_has_ip(adapter_t* a, ipaddr_t ip) const
|
|
|
|
{
|
|
|
|
for (auto* addr = a->FirstUnicastAddress; addr; addr = addr->Next)
|
2022-07-28 16:07:38 +00:00
|
|
|
{
|
2024-02-01 12:43:43 +00:00
|
|
|
SockAddr saddr{*addr->Address.lpSockaddr};
|
|
|
|
LogDebug(fmt::format("'{}' has address '{}'", a->AdapterName, saddr));
|
|
|
|
if (saddr.getIP() == ip)
|
|
|
|
return true;
|
2022-07-28 16:07:38 +00:00
|
|
|
}
|
2024-02-01 12:43:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
template <typename adapter_t>
|
|
|
|
bool adapter_has_fam(adapter_t* a, int af) const
|
|
|
|
{
|
|
|
|
for (auto* addr = a->FirstUnicastAddress; addr; addr = addr->Next)
|
|
|
|
{
|
|
|
|
SockAddr saddr{*addr->Address.lpSockaddr};
|
|
|
|
if (saddr.Family() == af)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
public:
|
|
|
|
std::optional<int> GetInterfaceIndex(ipaddr_t ip) const override
|
|
|
|
{
|
|
|
|
std::optional<int> found;
|
|
|
|
int af{AF_INET};
|
|
|
|
if (std::holds_alternative<ipv6addr_t>(ip))
|
|
|
|
af = AF_INET6;
|
|
|
|
iter_adapters(
|
|
|
|
[&found, ip, this](auto* adapter) {
|
|
|
|
if (found)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LogDebug(fmt::format(
|
2024-02-02 20:50:48 +00:00
|
|
|
"visit adapter looking for '{}': '{}' idx={}", ip, adapter->AdapterName, adapter->IfIndex));
|
2024-02-01 12:43:43 +00:00
|
|
|
if (adapter_has_ip(adapter, ip))
|
|
|
|
{
|
|
|
|
found = adapter->IfIndex;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
af);
|
|
|
|
return found;
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-02 20:50:48 +00:00
|
|
|
std::optional<llarp::SockAddr> GetInterfaceAddr(std::string_view name, int af) const override
|
2024-02-01 12:43:43 +00:00
|
|
|
{
|
|
|
|
std::optional<SockAddr> found;
|
|
|
|
iter_adapters([name = std::string{name}, af, &found, this](auto* a) {
|
|
|
|
if (found)
|
|
|
|
return;
|
|
|
|
if (std::string{a->AdapterName} != name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (adapter_has_fam(a, af))
|
|
|
|
found = SockAddr{*a->FirstUnicastAddress->Address.lpSockaddr};
|
|
|
|
});
|
|
|
|
return found;
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
std::optional<SockAddr> AllInterfaces(SockAddr fallback) const override
|
|
|
|
{
|
|
|
|
// windows seems to not give a shit about source address
|
|
|
|
return fallback.isIPv6() ? SockAddr{"[::]"} : SockAddr{"0.0.0.0"};
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
std::optional<std::string> FindFreeTun() const override
|
2022-07-28 16:07:38 +00:00
|
|
|
{
|
2024-02-01 12:43:43 +00:00
|
|
|
return "lokitun0";
|
2022-07-28 16:07:38 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
std::optional<oxen::quic::Address> get_best_public_address(bool, uint16_t) const override
|
|
|
|
{
|
|
|
|
// TODO: implement me ?
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
std::optional<IPRange> FindFreeRange() const override
|
|
|
|
{
|
|
|
|
std::list<IPRange> currentRanges;
|
|
|
|
iter_adapters([¤tRanges](auto* i) {
|
|
|
|
for (auto* addr = i->FirstUnicastAddress; addr; addr = addr->Next)
|
|
|
|
{
|
|
|
|
SockAddr saddr{*addr->Address.lpSockaddr};
|
|
|
|
currentRanges.emplace_back(
|
|
|
|
saddr.asIPv6(),
|
2024-02-02 20:50:48 +00:00
|
|
|
ipaddr_netmask_bits(addr->OnLinkPrefixLength, addr->Address.lpSockaddr->sa_family));
|
2024-02-01 12:43:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return IPRange::FindPrivateRange(currentRanges);
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
std::string LoopbackInterfaceName() const override
|
|
|
|
{
|
|
|
|
// todo: implement me? does windows even have a loopback?
|
|
|
|
return "";
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
bool HasInterfaceAddress(ipaddr_t ip) const override
|
|
|
|
{
|
|
|
|
return GetInterfaceIndex(ip) != std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<InterfaceInfo> AllNetworkInterfaces() const override
|
|
|
|
{
|
|
|
|
std::vector<InterfaceInfo> all;
|
|
|
|
for (int af : {AF_INET, AF_INET6})
|
|
|
|
iter_adapters(
|
|
|
|
[&all](auto* a) {
|
|
|
|
auto& cur = all.emplace_back();
|
|
|
|
cur.index = a->IfIndex;
|
|
|
|
cur.name = a->AdapterName;
|
|
|
|
for (auto* addr = a->FirstUnicastAddress; addr; addr = addr->Next)
|
|
|
|
{
|
|
|
|
SockAddr saddr{*addr->Address.lpSockaddr};
|
|
|
|
cur.addrs.emplace_back(
|
|
|
|
saddr.asIPv6(),
|
2024-02-02 20:50:48 +00:00
|
|
|
ipaddr_netmask_bits(addr->OnLinkPrefixLength, addr->Address.lpSockaddr->sa_family));
|
2024-02-01 12:43:43 +00:00
|
|
|
}
|
|
|
|
if (auto* addr = a->FirstGatewayAddress)
|
|
|
|
{
|
|
|
|
SockAddr gw{*addr->Address.lpSockaddr};
|
|
|
|
cur.gateway = gw.getIP();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
af);
|
|
|
|
return all;
|
|
|
|
}
|
|
|
|
};
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
const Platform_Impl g_plat{};
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2024-02-01 12:43:43 +00:00
|
|
|
const Platform* Platform::Default_ptr()
|
|
|
|
{
|
|
|
|
return &g_plat;
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
} // namespace llarp::net
|