lokinet/llarp/dns/rr.cpp

99 lines
2.2 KiB
C++
Raw Normal View History

2018-12-12 00:58:08 +00:00
#include <dns/rr.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(!EncodeName(buf, rr_name))
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_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))
{
return false;
}
return true;
2018-12-03 22:22:59 +00:00
}
bool
ResourceRecord::Decode(llarp_buffer_t* buf)
{
if(!DecodeName(buf, rr_name))
2018-12-04 16:16:43 +00:00
{
llarp::LogError("failed to decode rr name");
return false;
}
if(!buf->read_uint16(rr_type))
2018-12-04 16:16:43 +00:00
{
llarp::LogError("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::LogError("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::LogError("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))
{
llarp::LogError("failed to decode rr rdata");
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("RR name", rr_name);
printer.printAttribute("type", rr_type);
printer.printAttribute("class", rr_class);
printer.printAttribute("ttl", ttl);
printer.printAttribute("rdata", rData.size());
return stream;
}
2018-12-03 22:22:59 +00:00
} // namespace dns
} // namespace llarp