|
|
|
@ -22,188 +22,6 @@
|
|
|
|
|
|
|
|
|
|
namespace llarp::net
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
class Platform_Impl : public Platform_Base
|
|
|
|
|
{
|
|
|
|
|
/// 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) const
|
|
|
|
|
{
|
|
|
|
|
constexpr auto flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST
|
|
|
|
|
| GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS
|
|
|
|
|
| GAA_FLAG_INCLUDE_ALL_INTERFACES;
|
|
|
|
|
|
|
|
|
|
ULONG sz{};
|
|
|
|
|
GetAdaptersAddresses(AF_UNSPEC, flags, nullptr, nullptr, &sz);
|
|
|
|
|
auto* ptr = new uint8_t[sz];
|
|
|
|
|
auto* addrs = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(ptr);
|
|
|
|
|
|
|
|
|
|
if (auto err = GetAdaptersAddresses(AF_UNSPEC, flags, nullptr, addrs, &sz);
|
|
|
|
|
err != ERROR_SUCCESS)
|
|
|
|
|
throw llarp::win32::error{err, "GetAdaptersAddresses()"};
|
|
|
|
|
|
|
|
|
|
for (auto* addr = addrs; addr and addr->Next; addr = addr->Next)
|
|
|
|
|
visit(addr);
|
|
|
|
|
|
|
|
|
|
delete[] ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename adapter_t>
|
|
|
|
|
bool
|
|
|
|
|
adapter_has_ip(adapter_t* a, ipaddr_t ip) const
|
|
|
|
|
{
|
|
|
|
|
for (auto* addr = a->FirstUnicastAddress; addr and addr->Next; addr = addr->Next)
|
|
|
|
|
{
|
|
|
|
|
SockAddr saddr{*addr->Address.lpSockaddr};
|
|
|
|
|
if (saddr.getIP() == ip)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename adapter_t>
|
|
|
|
|
bool
|
|
|
|
|
adapter_has_fam(adapter_t* a, int af) const
|
|
|
|
|
{
|
|
|
|
|
for (auto* addr = a->FirstUnicastAddress; addr and addr->Next; addr = addr->Next)
|
|
|
|
|
{
|
|
|
|
|
SockAddr saddr{*addr->Address.lpSockaddr};
|
|
|
|
|
if (saddr.Family() == af)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
std::optional<int>
|
|
|
|
|
GetInterfaceIndex(ipaddr_t ip) const override
|
|
|
|
|
{
|
|
|
|
|
std::optional<int> found;
|
|
|
|
|
iter_adapters([&found, ip, this](auto* adapter) {
|
|
|
|
|
if (found)
|
|
|
|
|
return;
|
|
|
|
|
if (adapter_has_ip(adapter, ip))
|
|
|
|
|
found = adapter->IfIndex;
|
|
|
|
|
});
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<llarp::SockAddr>
|
|
|
|
|
GetInterfaceAddr(std::string_view name, int af) const override
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<std::string>
|
|
|
|
|
FindFreeTun() const override
|
|
|
|
|
{
|
|
|
|
|
// TODO: implement me ?
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<std::string>
|
|
|
|
|
GetBestNetIF(int) const override
|
|
|
|
|
{
|
|
|
|
|
// TODO: implement me ?
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<IPRange>
|
|
|
|
|
FindFreeRange() const override
|
|
|
|
|
{
|
|
|
|
|
std::list<IPRange> currentRanges;
|
|
|
|
|
iter_adapters([¤tRanges](auto* i) {
|
|
|
|
|
for (auto* addr = i->FirstUnicastAddress; addr and addr->Next; addr = addr->Next)
|
|
|
|
|
{
|
|
|
|
|
SockAddr saddr{*addr->Address.lpSockaddr};
|
|
|
|
|
currentRanges.emplace_back(
|
|
|
|
|
saddr.asIPv6(),
|
|
|
|
|
ipaddr_netmask_bits(addr->OnLinkPrefixLength, addr->Address.lpSockaddr->sa_family));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
auto ownsRange = [¤tRanges](const IPRange& range) -> bool {
|
|
|
|
|
for (const auto& ownRange : currentRanges)
|
|
|
|
|
{
|
|
|
|
|
if (ownRange * range)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
// generate possible ranges to in order of attempts
|
|
|
|
|
std::list<IPRange> possibleRanges;
|
|
|
|
|
for (byte_t oct = 16; oct < 32; ++oct)
|
|
|
|
|
{
|
|
|
|
|
possibleRanges.emplace_back(IPRange::FromIPv4(172, oct, 0, 1, 16));
|
|
|
|
|
}
|
|
|
|
|
for (byte_t oct = 0; oct < 255; ++oct)
|
|
|
|
|
{
|
|
|
|
|
possibleRanges.emplace_back(IPRange::FromIPv4(10, oct, 0, 1, 16));
|
|
|
|
|
}
|
|
|
|
|
for (byte_t oct = 0; oct < 255; ++oct)
|
|
|
|
|
{
|
|
|
|
|
possibleRanges.emplace_back(IPRange::FromIPv4(192, 168, oct, 1, 24));
|
|
|
|
|
}
|
|
|
|
|
// for each possible range pick the first one we don't own
|
|
|
|
|
for (const auto& range : possibleRanges)
|
|
|
|
|
{
|
|
|
|
|
if (not ownsRange(range))
|
|
|
|
|
return range;
|
|
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
std::string
|
|
|
|
|
LoopbackInterfaceName() const override
|
|
|
|
|
{
|
|
|
|
|
// todo: implement me? does windows even have a loopback?
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
bool
|
|
|
|
|
HasInterfaceAddress(ipaddr_t ip) const override
|
|
|
|
|
{
|
|
|
|
|
return GetInterfaceIndex(ip) != std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<InterfaceInfo>
|
|
|
|
|
AllNetworkInterfaces() const override
|
|
|
|
|
{
|
|
|
|
|
std::vector<InterfaceInfo> all;
|
|
|
|
|
iter_adapters([&all](auto* a) {
|
|
|
|
|
auto& cur = all.emplace_back();
|
|
|
|
|
cur.index = a->IfIndex;
|
|
|
|
|
cur.name = a->AdapterName;
|
|
|
|
|
for (auto* addr = a->FirstUnicastAddress; addr and addr->Next; addr = addr->Next)
|
|
|
|
|
{
|
|
|
|
|
SockAddr saddr{*addr->Address.lpSockaddr};
|
|
|
|
|
cur.addrs.emplace_back(
|
|
|
|
|
saddr.asIPv6(),
|
|
|
|
|
ipaddr_netmask_bits(addr->OnLinkPrefixLength, addr->Address.lpSockaddr->sa_family));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return all;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
class Platform_Impl : public Platform
|
|
|
|
|
{
|
|
|
|
@ -405,7 +223,6 @@ namespace llarp::net
|
|
|
|
|
return all;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
const Platform_Impl g_plat{};
|
|
|
|
|
|
|
|
|
|