lokinet/llarp/dns/message.cpp

237 lines
5.9 KiB
C++
Raw Normal View History

2018-12-03 22:22:59 +00:00
#include <llarp/dns/message.hpp>
#include <llarp/endian.hpp>
2018-12-04 16:16:43 +00:00
#include <llarp/logger.hpp>
2018-12-04 17:02:13 +00:00
#include <llarp/buffer.hpp>
2018-12-04 16:16:43 +00:00
2018-12-03 22:22:59 +00:00
namespace llarp
{
namespace dns
{
bool
MessageHeader::Encode(llarp_buffer_t* buf) const
{
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_put_uint16(buf, id))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_put_uint16(buf, fields))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_put_uint16(buf, qd_count))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_put_uint16(buf, an_count))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_put_uint16(buf, ns_count))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
return llarp_buffer_put_uint16(buf, ar_count);
2018-12-03 22:22:59 +00:00
}
bool
MessageHeader::Decode(llarp_buffer_t* buf)
{
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_read_uint16(buf, &id))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_read_uint16(buf, &fields))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_read_uint16(buf, &qd_count))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_read_uint16(buf, &an_count))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
if(!llarp_buffer_read_uint16(buf, &ns_count))
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
return llarp_buffer_read_uint16(buf, &ar_count);
}
Message::Message(Message&& other)
: hdr_id(std::move(other.hdr_id))
, hdr_fields(std::move(other.hdr_fields))
, questions(std::move(other.questions))
, answers(std::move(other.answers))
, authorities(std::move(other.authorities))
, additional(std::move(other.additional))
{
}
Message::Message(const Message& other)
: hdr_id(other.hdr_id)
, hdr_fields(other.hdr_fields)
, questions(other.questions)
, answers(other.answers)
, authorities(other.authorities)
, additional(other.additional)
{
}
Message::Message(const MessageHeader& hdr)
: hdr_id(hdr.id)
, hdr_fields(hdr.fields)
, questions(size_t(hdr.qd_count))
, answers(size_t(hdr.an_count))
, authorities(size_t(hdr.ns_count))
, additional(size_t(hdr.ar_count))
{
2018-12-03 22:22:59 +00:00
}
bool
Message::Encode(llarp_buffer_t* buf) const
{
2018-12-04 16:16:43 +00:00
MessageHeader hdr;
hdr.id = hdr_id;
hdr.fields = hdr_fields;
hdr.qd_count = questions.size();
hdr.an_count = answers.size();
hdr.ns_count = authorities.size();
hdr.ar_count = additional.size();
2018-12-03 22:22:59 +00:00
if(!hdr.Encode(buf))
return false;
for(const auto& question : questions)
if(!question.Encode(buf))
return false;
for(const auto& answer : answers)
if(!answer.Encode(buf))
return false;
for(const auto& auth : authorities)
if(!auth.Encode(buf))
return false;
for(const auto& rr : additional)
if(!rr.Encode(buf))
return false;
return true;
}
bool
Message::Decode(llarp_buffer_t* buf)
{
for(auto& qd : questions)
2018-12-04 16:16:43 +00:00
{
2018-12-03 22:22:59 +00:00
if(!qd.Decode(buf))
2018-12-04 16:16:43 +00:00
{
llarp::LogError("failed to decode question");
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
}
llarp::LogDebug(qd);
}
2018-12-03 22:22:59 +00:00
for(auto& an : answers)
2018-12-04 16:16:43 +00:00
{
2018-12-03 22:22:59 +00:00
if(!an.Decode(buf))
2018-12-04 16:16:43 +00:00
{
llarp::LogError("failed to decode answer");
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
}
llarp::LogDebug(an);
}
2018-12-03 22:22:59 +00:00
for(auto& ns : authorities)
2018-12-04 16:16:43 +00:00
{
2018-12-03 22:22:59 +00:00
if(!ns.Decode(buf))
2018-12-04 16:16:43 +00:00
{
llarp::LogError("failed to decode authority");
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
}
llarp::LogDebug(ns);
}
2018-12-03 22:22:59 +00:00
for(auto& ar : additional)
2018-12-04 16:16:43 +00:00
{
2018-12-03 22:22:59 +00:00
if(!ar.Decode(buf))
2018-12-04 16:16:43 +00:00
{
llarp::LogError("failed to decode additonal");
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
}
llarp::LogDebug(ar);
}
2018-12-03 22:22:59 +00:00
return true;
}
void
Message::AddINReply(llarp::huint32_t ip)
{
if(questions.size())
{
2018-12-04 16:16:43 +00:00
hdr_fields |= (1 << 15);
2018-12-03 22:22:59 +00:00
const auto& question = questions[0];
2018-12-04 16:16:43 +00:00
ResourceRecord rec;
rec.rr_name = question.qname;
rec.rr_type = 1;
rec.rr_class = 1;
rec.ttl = 1;
2018-12-03 22:22:59 +00:00
rec.rData.resize(4);
htobe32buf(rec.rData.data(), ip.h);
2018-12-04 16:16:43 +00:00
answers.emplace_back(std::move(rec));
2018-12-03 22:22:59 +00:00
}
}
2018-12-04 16:16:43 +00:00
void
2018-12-03 22:22:59 +00:00
Message::AddAReply(std::string name)
{
if(questions.size())
{
2018-12-04 16:16:43 +00:00
hdr_fields |= (1 << 15);
2018-12-03 22:22:59 +00:00
const auto& question = questions[0];
answers.emplace_back();
2018-12-04 17:02:13 +00:00
auto& rec = answers.back();
rec.rr_name = question.qname;
rec.rr_type = question.qtype;
rec.rr_class = 1;
rec.ttl = 1;
byte_t tmp[512] = {0};
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(EncodeName(&buf, name))
{
buf.sz = buf.cur - buf.base;
rec.rData.resize(buf.sz);
memcpy(rec.rData.data(), buf.base, buf.sz);
}
2018-12-03 22:22:59 +00:00
}
}
2018-12-07 21:52:19 +00:00
void
Message::AddMXReply(std::string name, uint16_t priority)
{
if(questions.size())
{
hdr_fields |= (1 << 15);
const auto& question = questions[0];
answers.emplace_back();
auto& rec = answers.back();
rec.rr_name = question.qname;
rec.rr_type = question.qtype;
2018-12-07 22:13:42 +00:00
rec.rr_class = 1;
2018-12-07 21:52:19 +00:00
rec.ttl = 1;
byte_t tmp[512] = {0};
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
llarp_buffer_put_uint16(&buf, priority);
if(EncodeName(&buf, name))
{
buf.sz = buf.cur - buf.base;
rec.rData.resize(buf.sz);
memcpy(rec.rData.data(), buf.base, buf.sz);
}
}
}
2018-12-04 16:16:43 +00:00
void
2018-12-03 22:22:59 +00:00
Message::AddNXReply()
{
if(questions.size())
{
2018-12-04 16:16:43 +00:00
hdr_fields |= (1 << 15) | (1 << 3);
2018-12-03 22:22:59 +00:00
const auto& question = questions[0];
answers.emplace_back();
auto& nx = answers.back();
nx.rr_name = question.qname;
nx.rr_type = question.qtype;
nx.rr_class = question.qclass;
2018-12-04 16:16:43 +00:00
nx.ttl = 1;
2018-12-03 22:22:59 +00:00
nx.rData.resize(1);
nx.rData.data()[0] = 0;
}
}
} // namespace dns
} // namespace llarp