2019-03-05 16:21:56 +00:00
|
|
|
#define __USE_MINGW_ANSI_STDIO 1
|
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>
|
2018-12-12 00:58:08 +00:00
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
#include <algorithm>
|
2018-12-12 00:58:08 +00:00
|
|
|
#include <sstream>
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dns
|
|
|
|
{
|
|
|
|
bool
|
2019-04-26 12:11:34 +00:00
|
|
|
DecodeName(llarp_buffer_t* buf, Name_t& name, bool trimTrailingDot)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buf->size_left() < 1)
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
|
|
|
std::stringstream ss;
|
|
|
|
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)
|
2018-12-04 16:16:43 +00:00
|
|
|
return false;
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
ss << Name_t((const char*)buf->cur, l);
|
2018-12-04 16:16:43 +00:00
|
|
|
ss << ".";
|
|
|
|
}
|
|
|
|
buf->cur = buf->cur + l;
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (l);
|
2018-12-03 22:22:59 +00:00
|
|
|
name = ss.str();
|
2019-04-26 12:11:34 +00:00
|
|
|
/// trim off last dot
|
2020-04-07 18:38:56 +00:00
|
|
|
if (trimTrailingDot)
|
2019-04-26 12:11:34 +00:00
|
|
|
name = name.substr(0, name.find_last_of('.'));
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-09-17 19:18:08 +00:00
|
|
|
EncodeName(llarp_buffer_t* buf, Name_t name)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2018-12-04 16:16:43 +00:00
|
|
|
std::stringstream ss;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (name.size() && name[name.size() - 1] == '.')
|
2018-12-04 16:16:43 +00:00
|
|
|
ss << name.substr(0, name.size() - 1);
|
|
|
|
else
|
|
|
|
ss << name;
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
std::string part;
|
2020-04-07 18:38:56 +00:00
|
|
|
while (std::getline(ss, part, '.'))
|
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
|
|
|
{
|
|
|
|
memcpy(buf->cur, part.data(), l);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-09-17 19:18:08 +00:00
|
|
|
DecodePTR(Name_t name, huint128_t& ip)
|
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)
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
std::string sub = name.substr(0, pos + 1);
|
2019-06-12 13:48:14 +00:00
|
|
|
const auto numdots = std::count(sub.begin(), sub.end(), '.');
|
2020-04-07 18:38:56 +00:00
|
|
|
if (numdots == 4 && !isV6)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
|
|
|
uint8_t a, b, c, d;
|
|
|
|
pos = sub.find('.');
|
2020-04-07 18:38:56 +00:00
|
|
|
d = atoi(sub.substr(0, pos).c_str());
|
2018-12-04 16:52:48 +00:00
|
|
|
sub = sub.substr(pos + 1);
|
2018-12-03 22:22:59 +00:00
|
|
|
pos = sub.find('.');
|
2020-04-07 18:38:56 +00:00
|
|
|
c = atoi(sub.substr(0, pos).c_str());
|
2018-12-04 16:52:48 +00:00
|
|
|
sub = sub.substr(pos + 1);
|
2018-12-03 22:22:59 +00:00
|
|
|
pos = sub.find('.');
|
2020-04-07 18:38:56 +00:00
|
|
|
b = atoi(sub.substr(0, pos).c_str());
|
2018-12-04 16:52:48 +00:00
|
|
|
sub = sub.substr(pos + 1);
|
2018-12-03 22:22:59 +00:00
|
|
|
pos = sub.find('.');
|
2020-04-07 18:38:56 +00:00
|
|
|
a = atoi(sub.substr(0, pos).c_str());
|
2020-05-20 19:14:05 +00:00
|
|
|
ip = net::ExpandV4(llarp::ipaddr_ipv4_bits(a, b, c, d));
|
2019-06-12 13:48:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (numdots == 32 && isV6)
|
2019-06-12 13:48:14 +00:00
|
|
|
{
|
|
|
|
size_t idx = 0;
|
|
|
|
uint8_t lo, hi;
|
2019-07-30 23:42:13 +00:00
|
|
|
auto* ptr = (uint8_t*)&ip.h;
|
2020-04-07 18:38:56 +00:00
|
|
|
while (idx < 16)
|
2019-06-12 13:48:14 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
pos = sub.find('.');
|
|
|
|
lo = (*sub.substr(0, pos).c_str()) - 'a';
|
|
|
|
sub = sub.substr(pos + 1);
|
|
|
|
pos = sub.find('.');
|
|
|
|
hi = (*sub.substr(0, pos).c_str()) - 'a';
|
|
|
|
sub = sub.substr(pos + 1);
|
2019-06-12 13:48:14 +00:00
|
|
|
ptr[idx] = lo | (hi << 4);
|
|
|
|
++idx;
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
return false;
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
bool
|
|
|
|
NameIsReserved(Name_t 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)
|
|
|
|
{
|
|
|
|
if (ends_with(name, reserved))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
} // namespace dns
|
|
|
|
} // namespace llarp
|