lokinet/llarp/dns/name.cpp

139 lines
3.6 KiB
C++
Raw Normal View History

#include "name.hpp"
2023-10-19 21:59:57 +00:00
#include <llarp/net/ip.hpp>
2023-10-19 21:59:57 +00:00
#include <llarp/net/net_bits.hpp>
#include <llarp/util/str.hpp>
#include <oxenc/hex.h>
2018-12-03 22:22:59 +00:00
2023-10-19 21:59:57 +00:00
namespace llarp::dns
2018-12-03 22:22:59 +00:00
{
2023-10-19 21:59:57 +00:00
std::optional<std::string>
DecodeName(llarp_buffer_t* buf, bool trimTrailingDot)
2018-12-03 22:22:59 +00:00
{
2023-10-19 21:59:57 +00:00
if (buf->size_left() < 1)
return std::nullopt;
auto result = std::make_optional<std::string>();
auto& name = *result;
size_t l;
do
2018-12-03 22:22:59 +00:00
{
2023-10-19 21:59:57 +00:00
l = *buf->cur;
buf->cur++;
if (l)
2018-12-03 22:22:59 +00:00
{
2023-10-19 21:59:57 +00:00
if (buf->size_left() < l)
return std::nullopt;
2018-12-04 16:16:43 +00:00
2023-10-19 21:59:57 +00:00
name.append((const char*)buf->cur, l);
name += '.';
}
buf->cur = buf->cur + l;
} while (l);
/// trim off last dot
if (trimTrailingDot)
name.pop_back();
return result;
}
2018-12-03 22:22:59 +00:00
2023-10-19 21:59:57 +00:00
bool
EncodeNameTo(llarp_buffer_t* buf, std::string_view name)
{
if (name.size() && name.back() == '.')
name.remove_suffix(1);
2018-12-04 16:16:43 +00:00
2023-10-19 21:59:57 +00:00
for (auto part : llarp::split(name, "."))
{
size_t l = part.length();
if (l > 63)
return false;
*(buf->cur) = l;
buf->cur++;
if (buf->size_left() < l)
return false;
if (l)
2018-12-03 22:22:59 +00:00
{
2023-10-19 21:59:57 +00:00
std::memcpy(buf->cur, part.data(), l);
buf->cur += l;
2018-12-03 22:22:59 +00:00
}
2023-10-19 21:59:57 +00:00
else
break;
2018-12-03 22:22:59 +00:00
}
2023-10-19 21:59:57 +00:00
*buf->cur = 0;
buf->cur++;
return true;
}
2018-12-03 22:22:59 +00:00
2023-10-19 21:59:57 +00:00
std::optional<huint128_t>
DecodePTR(std::string_view name)
{
bool isV6 = false;
auto pos = name.find(".in-addr.arpa");
if (pos == std::string::npos)
2018-12-03 22:22:59 +00:00
{
2023-10-19 21:59:57 +00:00
pos = name.find(".ip6.arpa");
isV6 = true;
}
if (pos == std::string::npos)
return std::nullopt;
name = name.substr(0, pos + 1);
const auto numdots = std::count(name.begin(), name.end(), '.');
if (numdots == 4 && !isV6)
{
std::array<uint8_t, 4> q;
for (int i = 3; i >= 0; i--)
2018-12-03 22:22:59 +00:00
{
2023-10-19 21:59:57 +00:00
pos = name.find('.');
if (!llarp::parse_int(name.substr(0, pos), q[i]))
return std::nullopt;
name.remove_prefix(pos + 1);
2019-06-12 13:48:14 +00:00
}
2023-10-19 21:59:57 +00:00
return net::ExpandV4(llarp::ipaddr_ipv4_bits(q[0], q[1], q[2], q[3]));
}
if (numdots == 32 && name.size() == 64 && isV6)
{
// We're going to convert from nybbles a.b.c.d.e.f.0.1.2.3.[...] into hex string
// "badcfe1032...", then decode the hex string to bytes.
std::array<char, 32> in;
auto in_pos = in.data();
for (size_t i = 0; i < 64; i += 4)
2019-06-12 13:48:14 +00:00
{
2023-10-19 21:59:57 +00:00
if (not(oxenc::is_hex_digit(name[i]) and name[i + 1] == '.'
and oxenc::is_hex_digit(name[i + 2]) and name[i + 3] == '.'))
return std::nullopt;
2023-10-19 21:59:57 +00:00
// Flip the nybbles because the smallest one is first
*in_pos++ = name[i + 2];
*in_pos++ = name[i];
}
2023-10-19 21:59:57 +00:00
assert(in_pos == in.data() + in.size());
huint128_t ip;
static_assert(in.size() == 2 * sizeof(ip.h));
// our string right now is the little endian representation, so load it as such on little
// endian, or in reverse on big endian.
if constexpr (oxenc::little_endian)
oxenc::from_hex(in.begin(), in.end(), reinterpret_cast<uint8_t*>(&ip.h));
else
oxenc::from_hex(in.rbegin(), in.rend(), reinterpret_cast<uint8_t*>(&ip.h));
return ip;
2018-12-03 22:22:59 +00:00
}
2023-10-19 21:59:57 +00:00
return std::nullopt;
}
2018-12-03 22:22:59 +00:00
2023-10-19 21:59:57 +00:00
bool
NameIsReserved(std::string_view name)
{
const std::vector<std::string_view> reserved_names = {
".snode.loki"sv, ".loki.loki"sv, ".snode.loki."sv, ".loki.loki."sv};
for (const auto& reserved : reserved_names)
{
2023-10-19 21:59:57 +00:00
if (ends_with(name, reserved)) // subdomain foo.loki.loki
return true;
if (name == reserved.substr(1)) // loki.loki itself
return true;
}
2023-10-19 21:59:57 +00:00
return false;
}
} // namespace llarp::dns