2021-03-09 22:24:35 +00:00
|
|
|
#include "ip_range.hpp"
|
2020-05-20 19:14:05 +00:00
|
|
|
|
2022-02-17 18:44:31 +00:00
|
|
|
#include "oxenc/bt_serialize.h"
|
2021-04-14 15:07:06 +00:00
|
|
|
|
|
|
|
#include "llarp/util/bencode.h"
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2021-04-14 15:07:06 +00:00
|
|
|
bool
|
|
|
|
IPRange::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2022-02-17 18:44:31 +00:00
|
|
|
const auto str = oxenc::bt_serialize(ToString());
|
2021-04-14 15:07:06 +00:00
|
|
|
return buf->write(str.begin(), str.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IPRange::BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
const auto* start = buf->cur;
|
|
|
|
if (not bencode_discard(buf))
|
|
|
|
return false;
|
|
|
|
std::string_view data{
|
|
|
|
reinterpret_cast<const char*>(start), static_cast<size_t>(buf->cur - start)};
|
|
|
|
std::string str;
|
|
|
|
try
|
|
|
|
{
|
2022-02-17 18:44:31 +00:00
|
|
|
oxenc::bt_deserialize(data, str);
|
2021-04-14 15:07:06 +00:00
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return FromString(str);
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
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())
|
|
|
|
{
|
2020-06-24 13:24:07 +00:00
|
|
|
const auto bits = stoi(bitsstr);
|
2020-05-20 19:14:05 +00:00
|
|
|
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
|
2020-06-24 13:24:07 +00:00
|
|
|
IPRange::BaseAddressString() const
|
2020-05-20 19:14:05 +00:00
|
|
|
{
|
2020-06-24 13:24:07 +00:00
|
|
|
if (IsV4())
|
2020-05-20 19:14:05 +00:00
|
|
|
{
|
2020-06-24 13:24:07 +00:00
|
|
|
const huint32_t addr4 = net::TruncateV6(addr);
|
|
|
|
return addr4.ToString();
|
2020-05-20 19:14:05 +00:00
|
|
|
}
|
2020-06-24 13:24:07 +00:00
|
|
|
return addr.ToString();
|
2020-05-20 19:14:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
std::string
|
|
|
|
IPRange::NetmaskString() const
|
|
|
|
{
|
|
|
|
if (IsV4())
|
|
|
|
{
|
|
|
|
const huint32_t mask = net::TruncateV6(netmask_bits);
|
|
|
|
return mask.ToString();
|
|
|
|
}
|
|
|
|
return netmask_bits.ToString();
|
|
|
|
}
|
|
|
|
|
2022-10-06 18:45:42 +00:00
|
|
|
std::optional<IPRange>
|
|
|
|
IPRange::FindPrivateRange(const std::list<IPRange>& excluding)
|
|
|
|
{
|
|
|
|
auto good = [&excluding](const IPRange& range) -> bool {
|
|
|
|
for (const auto& ex : excluding)
|
|
|
|
if (ex * range)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
for (int oct = 16; oct <= 31; ++oct)
|
|
|
|
if (auto range = IPRange::FromIPv4(172, oct, 0, 1, 16); good(range))
|
|
|
|
return range;
|
|
|
|
for (int oct = 0; oct <= 255; ++oct)
|
|
|
|
if (auto range = IPRange::FromIPv4(10, oct, 0, 1, 16); good(range))
|
|
|
|
return range;
|
|
|
|
for (int oct = 0; oct <= 255; ++oct)
|
|
|
|
if (auto range = IPRange::FromIPv4(192, 168, oct, 1, 24); good(range))
|
|
|
|
return range;
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:14:05 +00:00
|
|
|
} // namespace llarp
|