mirror of https://github.com/oxen-io/lokinet
Move IPRange out of net.hpp; free up TruncateV6 etc.
- Move IPRange into its own net/ip_range.hpp - Move the static net::IPPacket::TruncateV6, etc. functions to free net::TruncateV6, etc. functions (now from net/ip.hpp instead of net/ip_packet.hpp). - Make net::TruncateV6 and net::ExpandV4 constexpr. - Add IPRange::FromIPv4 factory function (to replace the iprange_ipv4 free function)pull/1282/head
parent
72bf215da4
commit
bdc9c7bfa8
@ -0,0 +1,28 @@
|
||||
#include <net/ip.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace llarp::net {
|
||||
|
||||
huint128_t
|
||||
In6ToHUInt(in6_addr addr)
|
||||
{
|
||||
uint8_t* ptr = reinterpret_cast<uint8_t*>(addr.s6_addr);
|
||||
uint128_t x{0};
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
x <<= 8;
|
||||
x |= ptr[i];
|
||||
}
|
||||
return huint128_t{x};
|
||||
}
|
||||
|
||||
in6_addr
|
||||
HUIntToIn6(huint128_t x)
|
||||
{
|
||||
in6_addr addr;
|
||||
auto i = ntoh128(x.h);
|
||||
std::memcpy(&addr, &i, 16);
|
||||
return addr;
|
||||
}
|
||||
|
||||
} // namespace llarp::net
|
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include <net/net_int.hpp>
|
||||
#include <net/net.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
namespace llarp::net
|
||||
{
|
||||
huint128_t
|
||||
In6ToHUInt(in6_addr addr);
|
||||
|
||||
in6_addr
|
||||
HUIntToIn6(huint128_t x);
|
||||
|
||||
constexpr huint128_t
|
||||
ExpandV4(huint32_t x)
|
||||
{
|
||||
return huint128_t{0xffff'0000'0000UL} | huint128_t{x.h};
|
||||
}
|
||||
|
||||
constexpr huint32_t
|
||||
TruncateV6(huint128_t x)
|
||||
{
|
||||
huint32_t ret = {0};
|
||||
ret.h = (uint32_t)(x.h & 0x0000'0000'ffff'ffffUL);
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace llarp::net
|
@ -0,0 +1,76 @@
|
||||
#include <net/ip_range.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
|
||||
bool
|
||||
IPRange::ContainsV4(const huint32_t& ip) const
|
||||
{
|
||||
return Contains(net::ExpandV4(ip));
|
||||
}
|
||||
|
||||
bool
|
||||
IPRange::FromString(std::string str)
|
||||
{
|
||||
const auto colinpos = str.find(":");
|
||||
const auto slashpos = str.find("/");
|
||||
std::string bitsstr;
|
||||
if (slashpos != std::string::npos)
|
||||
{
|
||||
bitsstr = str.substr(slashpos + 1);
|
||||
str = str.substr(0, slashpos);
|
||||
}
|
||||
if (colinpos == std::string::npos)
|
||||
{
|
||||
huint32_t ip;
|
||||
if (!ip.FromString(str))
|
||||
return false;
|
||||
addr = net::ExpandV4(ip);
|
||||
if (!bitsstr.empty())
|
||||
{
|
||||
auto bits = atoi(bitsstr.c_str());
|
||||
if (bits < 0 || bits > 32)
|
||||
return false;
|
||||
netmask_bits = netmask_ipv6_bits(96 + bits);
|
||||
}
|
||||
else
|
||||
netmask_bits = netmask_ipv6_bits(128);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!addr.FromString(str))
|
||||
return false;
|
||||
if (!bitsstr.empty())
|
||||
{
|
||||
auto bits = atoi(bitsstr.c_str());
|
||||
if (bits < 0 || bits > 128)
|
||||
return false;
|
||||
netmask_bits = netmask_ipv6_bits(bits);
|
||||
}
|
||||
else
|
||||
{
|
||||
netmask_bits = netmask_ipv6_bits(128);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string
|
||||
IPRange::ToString() const
|
||||
{
|
||||
char buf[INET6_ADDRSTRLEN + 1] = {0};
|
||||
std::string str;
|
||||
in6_addr inaddr = {};
|
||||
size_t numset = 0;
|
||||
uint128_t bits = netmask_bits.h;
|
||||
while (bits)
|
||||
{
|
||||
if (bits & 1)
|
||||
numset++;
|
||||
bits >>= 1;
|
||||
}
|
||||
str += inet_ntop(AF_INET6, &inaddr, buf, sizeof(buf));
|
||||
return str + "/" + std::to_string(numset);
|
||||
}
|
||||
|
||||
} // namespace llarp
|
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
#include <ostream>
|
||||
#include <net/ip.hpp>
|
||||
#include <util/bits.hpp>
|
||||
#include <util/types.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
struct IPRange
|
||||
{
|
||||
using Addr_t = huint128_t;
|
||||
huint128_t addr = {0};
|
||||
huint128_t netmask_bits = {0};
|
||||
|
||||
static constexpr IPRange
|
||||
FromIPv4(byte_t a, byte_t b, byte_t c, byte_t d, byte_t mask)
|
||||
{
|
||||
return IPRange{net::ExpandV4(ipaddr_ipv4_bits(a, b, c, d)),
|
||||
netmask_ipv6_bits(mask + 96)};
|
||||
}
|
||||
|
||||
/// 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);
|
||||
}
|
||||
|
||||
bool
|
||||
ContainsV4(const huint32_t& ip) const;
|
||||
|
||||
friend std::ostream&
|
||||
operator<<(std::ostream& out, const IPRange& a)
|
||||
{
|
||||
return out << a.ToString();
|
||||
}
|
||||
|
||||
/// get the highest address on this range
|
||||
huint128_t
|
||||
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
|
||||
{
|
||||
return (this->addr & this->netmask_bits) < (other.addr & other.netmask_bits)
|
||||
|| this->netmask_bits < other.netmask_bits;
|
||||
}
|
||||
|
||||
std::string
|
||||
ToString() const;
|
||||
|
||||
bool
|
||||
FromString(std::string str);
|
||||
};
|
||||
|
||||
} // namespace llarp
|
Loading…
Reference in New Issue