2020-05-20 19:14:05 +00:00
|
|
|
#pragma once
|
2022-10-06 18:45:42 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "ip.hpp"
|
|
|
|
#include "net_bits.hpp"
|
2023-08-31 16:28:02 +00:00
|
|
|
#include <oxen/log/catlogger.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/bits.hpp>
|
2021-04-14 15:07:06 +00:00
|
|
|
#include <llarp/util/buffer.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/types.hpp>
|
2023-08-31 16:28:02 +00:00
|
|
|
#include <llarp/util/logging.hpp>
|
|
|
|
#include <oxenc/bt.h>
|
2022-10-06 18:45:42 +00:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <optional>
|
2023-01-26 17:51:55 +00:00
|
|
|
#include <stdexcept>
|
2020-05-20 19:14:05 +00:00
|
|
|
#include <string>
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
static auto net_cat = llarp::log::Cat("lokinet.net");
|
|
|
|
} // namespace
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct IPRange
|
|
|
|
{
|
|
|
|
using Addr_t = huint128_t;
|
|
|
|
huint128_t addr = {0};
|
|
|
|
huint128_t netmask_bits = {0};
|
|
|
|
|
2021-02-18 17:20:58 +00:00
|
|
|
constexpr IPRange()
|
|
|
|
{}
|
|
|
|
constexpr IPRange(huint128_t address, huint128_t netmask)
|
|
|
|
: addr{std::move(address)}, netmask_bits{std::move(netmask)}
|
|
|
|
{}
|
|
|
|
|
2023-01-26 17:51:55 +00:00
|
|
|
explicit IPRange(std::string _range)
|
|
|
|
{
|
|
|
|
if (not FromString(_range))
|
|
|
|
throw std::invalid_argument{"IP string '{}' cannot be parsed as IP range"_format(_range)};
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
static constexpr IPRange
|
|
|
|
V4MappedRange()
|
|
|
|
{
|
|
|
|
return IPRange{huint128_t{0x0000'ffff'0000'0000UL}, netmask_ipv6_bits(96)};
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
static constexpr IPRange
|
|
|
|
FromIPv4(byte_t a, byte_t b, byte_t c, byte_t d, byte_t mask)
|
|
|
|
{
|
2020-05-20 22:48:13 +00:00
|
|
|
return IPRange{net::ExpandV4(ipaddr_ipv4_bits(a, b, c, d)), netmask_ipv6_bits(mask + 96)};
|
2020-05-20 19:14:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
static inline IPRange
|
|
|
|
FromIPv4(net::ipv4addr_t addr, net::ipv4addr_t netmask)
|
|
|
|
{
|
|
|
|
return IPRange{
|
2023-01-27 23:08:43 +00:00
|
|
|
net::ExpandV4(llarp::net::ToHost(addr)),
|
|
|
|
netmask_ipv6_bits(bits::count_bits(netmask) + 96)};
|
2022-07-09 15:05:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 23:34:42 +00:00
|
|
|
/// return true if this iprange is in the IPv4 mapping range for containing ipv4 addresses
|
2020-06-24 13:24:07 +00:00
|
|
|
constexpr bool
|
|
|
|
IsV4() const
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
return V4MappedRange().Contains(addr);
|
2020-06-24 13:24:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
/// get address family
|
|
|
|
constexpr int
|
|
|
|
Family() const
|
|
|
|
{
|
|
|
|
if (IsV4())
|
|
|
|
return AF_INET;
|
|
|
|
return AF_INET6;
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:24:07 +00:00
|
|
|
/// return the number of bits set in the hostmask
|
|
|
|
constexpr int
|
|
|
|
HostmaskBits() const
|
|
|
|
{
|
|
|
|
if (IsV4())
|
|
|
|
{
|
|
|
|
return bits::count_bits(net::TruncateV6(netmask_bits));
|
|
|
|
}
|
|
|
|
return bits::count_bits(netmask_bits);
|
|
|
|
}
|
|
|
|
|
2021-10-22 19:53:19 +00:00
|
|
|
/// return true if our range and other intersect
|
|
|
|
constexpr bool
|
|
|
|
operator*(const IPRange& other) const
|
|
|
|
{
|
|
|
|
return Contains(other) or other.Contains(*this);
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:24:07 +00:00
|
|
|
/// return true if the other range is inside our range
|
|
|
|
constexpr bool
|
|
|
|
Contains(const IPRange& other) const
|
|
|
|
{
|
|
|
|
return Contains(other.addr) and Contains(other.HighestAddr());
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
/// return true if ip is contained in this ip range
|
|
|
|
constexpr bool
|
|
|
|
Contains(const Addr_t& ip) const
|
|
|
|
{
|
|
|
|
return (addr & netmask_bits) == (ip & netmask_bits);
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:24:07 +00:00
|
|
|
/// return true if we are a ipv4 range and contains this ip
|
|
|
|
constexpr bool
|
|
|
|
Contains(const huint32_t& ip) const
|
|
|
|
{
|
|
|
|
if (not IsV4())
|
|
|
|
return false;
|
|
|
|
return Contains(net::ExpandV4(ip));
|
|
|
|
}
|
2020-05-20 19:14:05 +00:00
|
|
|
|
2022-09-14 19:08:59 +00:00
|
|
|
inline bool
|
|
|
|
Contains(const net::ipaddr_t& ip) const
|
|
|
|
{
|
2023-01-27 23:08:43 +00:00
|
|
|
return var::visit([this](auto&& ip) { return Contains(llarp::net::ToHost(ip)); }, ip);
|
2022-09-14 19:08:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
/// get the highest address on this range
|
2020-06-24 13:24:07 +00:00
|
|
|
constexpr huint128_t
|
2020-05-20 19:14:05 +00:00
|
|
|
HighestAddr() const
|
|
|
|
{
|
|
|
|
return (addr & netmask_bits) + (huint128_t{1} << (128 - bits::count_bits_128(netmask_bits.h)))
|
|
|
|
- huint128_t{1};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const IPRange& other) const
|
|
|
|
{
|
2022-10-13 17:19:25 +00:00
|
|
|
auto maskedA = addr & netmask_bits, maskedB = other.addr & other.netmask_bits;
|
|
|
|
return std::tie(maskedA, netmask_bits) < std::tie(maskedB, other.netmask_bits);
|
2020-05-20 19:14:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
bool
|
|
|
|
operator==(const IPRange& other) const
|
|
|
|
{
|
|
|
|
return addr == other.addr and netmask_bits == other.netmask_bits;
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
std::string
|
2020-06-24 13:24:07 +00:00
|
|
|
ToString() const
|
|
|
|
{
|
|
|
|
return BaseAddressString() + "/" + std::to_string(HostmaskBits());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
BaseAddressString() const;
|
2020-05-20 19:14:05 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
std::string
|
|
|
|
NetmaskString() const;
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
bool
|
|
|
|
FromString(std::string str);
|
2021-04-14 15:07:06 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
void
|
|
|
|
bt_encode(oxenc::bt_list_producer& btlc) const;
|
2021-04-14 15:07:06 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf);
|
2022-10-06 18:45:42 +00:00
|
|
|
|
|
|
|
/// Finds a free private use range not overlapping the given ranges.
|
|
|
|
static std::optional<IPRange>
|
|
|
|
FindPrivateRange(const std::list<IPRange>& excluding);
|
2020-05-20 19:14:05 +00:00
|
|
|
};
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
template <>
|
|
|
|
constexpr inline bool IsToStringFormattable<IPRange> = true;
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
} // namespace llarp
|
2021-01-11 23:13:22 +00:00
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct hash<llarp::IPRange>
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const llarp::IPRange& range) const
|
|
|
|
{
|
|
|
|
const auto str = range.ToString();
|
|
|
|
return std::hash<std::string>{}(str);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|