lokinet/llarp/dns/rr.cpp

110 lines
2.6 KiB
C++
Raw Normal View History

2018-12-12 00:58:08 +00:00
#include <dns/rr.hpp>
#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
{
if (not EncodeName(buf, rr_name))
2018-12-03 22:22:59 +00:00
return false;
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
}
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
}
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
}
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)
{
uint16_t discard;
if (!buf->read_uint16(discard))
2018-12-04 16:16:43 +00:00
return false;
if (!buf->read_uint16(rr_type))
2018-12-04 16:16:43 +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
}
if (!buf->read_uint16(rr_class))
2018-12-04 16:16:43 +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
}
if (!buf->read_uint32(ttl))
2018-12-04 16:16:43 +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
}
if (!DecodeRData(buf, rData))
2018-12-04 16:16:43 +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);
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;
}
bool
ResourceRecord::HasCNameForTLD(const std::string& tld) const
{
if (rr_type != qTypeCNAME)
return false;
Name_t name;
llarp_buffer_t buf(rData);
if (not DecodeName(&buf, name))
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