2018-12-12 00:58:08 +00:00
|
|
|
#include <dns/rr.hpp>
|
2020-02-12 20:43:37 +00:00
|
|
|
#include <dns/dns.hpp>
|
|
|
|
#include <util/mem.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-02-24 23:46:37 +00:00
|
|
|
#include <util/printer.hpp>
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dns
|
|
|
|
{
|
2018-12-04 16:16:43 +00:00
|
|
|
ResourceRecord::ResourceRecord(const ResourceRecord& other)
|
|
|
|
: rr_name(other.rr_name)
|
|
|
|
, rr_type(other.rr_type)
|
|
|
|
, rr_class(other.rr_class)
|
|
|
|
, ttl(other.ttl)
|
|
|
|
, rData(other.rData)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceRecord::ResourceRecord(ResourceRecord&& other)
|
|
|
|
: rr_name(std::move(other.rr_name))
|
|
|
|
, rr_type(std::move(other.rr_type))
|
|
|
|
, rr_class(std::move(other.rr_class))
|
|
|
|
, ttl(std::move(other.ttl))
|
|
|
|
, rData(std::move(other.rData))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
bool
|
|
|
|
ResourceRecord::Encode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not EncodeName(buf, rr_name))
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buf->put_uint16(rr_type))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buf->put_uint16(rr_class))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buf->put_uint32(ttl))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!EncodeRData(buf, rData))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ResourceRecord::Decode(llarp_buffer_t* buf)
|
|
|
|
{
|
2020-02-12 20:43:37 +00:00
|
|
|
uint16_t discard;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buf->read_uint16(discard))
|
2018-12-04 16:16:43 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buf->read_uint16(rr_type))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
2020-03-09 20:20:27 +00:00
|
|
|
llarp::LogDebug("failed to decode rr type");
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buf->read_uint16(rr_class))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
2020-03-09 20:20:27 +00:00
|
|
|
llarp::LogDebug("failed to decode rr class");
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buf->read_uint32(ttl))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
2020-03-09 20:20:27 +00:00
|
|
|
llarp::LogDebug("failed to decode ttl");
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!DecodeRData(buf, rData))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
2020-03-09 20:20:27 +00:00
|
|
|
llarp::LogDebug("failed to decode rr rdata ", *this);
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
2019-02-24 23:46:37 +00:00
|
|
|
|
|
|
|
std::ostream&
|
|
|
|
ResourceRecord::print(std::ostream& stream, int level, int spaces) const
|
|
|
|
{
|
|
|
|
Printer printer(stream, level, spaces);
|
2020-02-12 20:43:37 +00:00
|
|
|
printer.printAttribute("name", rr_name);
|
2019-02-24 23:46:37 +00:00
|
|
|
printer.printAttribute("type", rr_type);
|
|
|
|
printer.printAttribute("class", rr_class);
|
|
|
|
printer.printAttribute("ttl", ttl);
|
|
|
|
printer.printAttribute("rdata", rData.size());
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
2020-02-12 20:43:37 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
ResourceRecord::HasCNameForTLD(const std::string& tld) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (rr_type != qTypeCNAME)
|
2020-02-12 20:43:37 +00:00
|
|
|
return false;
|
|
|
|
Name_t name;
|
|
|
|
llarp_buffer_t buf(rData);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not DecodeName(&buf, name))
|
2020-02-12 20:43:37 +00:00
|
|
|
return false;
|
|
|
|
return name.find(tld) != std::string::npos
|
|
|
|
&& name.rfind(tld) == (name.size() - tld.size()) - 1;
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
} // namespace dns
|
|
|
|
} // namespace llarp
|