2021-03-09 22:24:35 +00:00
|
|
|
#include "name.hpp"
|
|
|
|
#include <llarp/net/net.hpp>
|
|
|
|
#include <llarp/net/ip.hpp>
|
|
|
|
#include <llarp/util/str.hpp>
|
2022-07-18 20:05:39 +00:00
|
|
|
#include <oxenc/hex.h>
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dns
|
|
|
|
{
|
2022-07-18 20:05:39 +00:00
|
|
|
std::optional<std::string>
|
|
|
|
DecodeName(llarp_buffer_t* buf, bool trimTrailingDot)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buf->size_left() < 1)
|
2022-07-18 20:05:39 +00:00
|
|
|
return std::nullopt;
|
|
|
|
auto result = std::make_optional<std::string>();
|
|
|
|
auto& name = *result;
|
2018-12-03 22:22:59 +00:00
|
|
|
size_t l;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
l = *buf->cur;
|
|
|
|
buf->cur++;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (l)
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buf->size_left() < l)
|
2022-07-18 20:05:39 +00:00
|
|
|
return std::nullopt;
|
2018-12-04 16:16:43 +00:00
|
|
|
|
2022-07-18 20:05:39 +00:00
|
|
|
name.append((const char*)buf->cur, l);
|
|
|
|
name += '.';
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
|
|
|
buf->cur = buf->cur + l;
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (l);
|
2019-04-26 12:11:34 +00:00
|
|
|
/// trim off last dot
|
2020-04-07 18:38:56 +00:00
|
|
|
if (trimTrailingDot)
|
2022-07-18 20:05:39 +00:00
|
|
|
name.pop_back();
|
|
|
|
return result;
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2022-07-18 20:05:39 +00:00
|
|
|
EncodeNameTo(llarp_buffer_t* buf, std::string_view name)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2022-07-18 20:05:39 +00:00
|
|
|
if (name.size() && name.back() == '.')
|
|
|
|
name.remove_suffix(1);
|
2018-12-04 16:16:43 +00:00
|
|
|
|
2022-07-18 20:05:39 +00:00
|
|
|
for (auto part : llarp::split(name, "."))
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2018-12-04 16:16:43 +00:00
|
|
|
size_t l = part.length();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (l > 63)
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
*(buf->cur) = l;
|
2018-12-03 22:22:59 +00:00
|
|
|
buf->cur++;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buf->size_left() < l)
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (l)
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
2022-07-18 20:05:39 +00:00
|
|
|
std::memcpy(buf->cur, part.data(), l);
|
2018-12-04 16:16:43 +00:00
|
|
|
buf->cur += l;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
2018-12-04 16:16:43 +00:00
|
|
|
*buf->cur = 0;
|
|
|
|
buf->cur++;
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-18 20:05:39 +00:00
|
|
|
std::optional<huint128_t>
|
|
|
|
DecodePTR(std::string_view name)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-06-12 13:48:14 +00:00
|
|
|
bool isV6 = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
auto pos = name.find(".in-addr.arpa");
|
|
|
|
if (pos == std::string::npos)
|
2019-06-12 13:48:14 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
pos = name.find(".ip6.arpa");
|
2019-06-12 13:48:14 +00:00
|
|
|
isV6 = true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (pos == std::string::npos)
|
2022-07-18 20:05:39 +00:00
|
|
|
return std::nullopt;
|
|
|
|
name = name.substr(0, pos + 1);
|
|
|
|
const auto numdots = std::count(name.begin(), name.end(), '.');
|
2020-04-07 18:38:56 +00:00
|
|
|
if (numdots == 4 && !isV6)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2022-07-18 20:05:39 +00:00
|
|
|
std::array<uint8_t, 4> q;
|
|
|
|
for (int i = 3; i >= 0; i--)
|
|
|
|
{
|
|
|
|
pos = name.find('.');
|
|
|
|
if (!llarp::parse_int(name.substr(0, pos), q[i]))
|
|
|
|
return std::nullopt;
|
|
|
|
name.remove_prefix(pos + 1);
|
|
|
|
}
|
|
|
|
return net::ExpandV4(llarp::ipaddr_ipv4_bits(q[0], q[1], q[2], q[3]));
|
2019-06-12 13:48:14 +00:00
|
|
|
}
|
2022-07-18 20:05:39 +00:00
|
|
|
if (numdots == 32 && name.size() == 64 && isV6)
|
2019-06-12 13:48:14 +00:00
|
|
|
{
|
2022-07-18 20:05:39 +00:00
|
|
|
// 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
|
|
|
{
|
2022-07-18 20:05:39 +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;
|
|
|
|
|
|
|
|
// Flip the nybbles because the smallest one is first
|
|
|
|
*in_pos++ = name[i + 2];
|
|
|
|
*in_pos++ = name[i];
|
2019-06-12 13:48:14 +00:00
|
|
|
}
|
2022-07-18 20:05:39 +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));
|
2019-07-06 17:03:40 +00:00
|
|
|
|
2022-07-18 20:05:39 +00:00
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
bool
|
2022-07-18 20:05:39 +00:00
|
|
|
NameIsReserved(std::string_view name)
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
|
|
|
const std::vector<std::string_view> reserved_names = {
|
2022-07-18 20:05:39 +00:00
|
|
|
".snode.loki"sv, ".loki.loki"sv, ".snode.loki."sv, ".loki.loki."sv};
|
2020-09-17 19:18:08 +00:00
|
|
|
for (const auto& reserved : reserved_names)
|
|
|
|
{
|
2022-07-18 20:05:39 +00:00
|
|
|
if (ends_with(name, reserved)) // subdomain foo.loki.loki
|
|
|
|
return true;
|
|
|
|
if (name == reserved.substr(1)) // loki.loki itself
|
2020-09-17 19:18:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
} // namespace dns
|
|
|
|
} // namespace llarp
|