2018-12-03 23:22:59 +01:00
|
|
|
#ifndef LLARP_DNS_MESSAGE_HPP
|
|
|
|
#define LLARP_DNS_MESSAGE_HPP
|
2018-12-12 01:58:08 +01:00
|
|
|
#include <dns/serialize.hpp>
|
|
|
|
#include <dns/rr.hpp>
|
|
|
|
#include <dns/question.hpp>
|
2018-12-03 23:22:59 +01:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dns
|
|
|
|
{
|
|
|
|
using MsgID_t = uint16_t;
|
|
|
|
using Fields_t = uint16_t;
|
|
|
|
using Count_t = uint16_t;
|
|
|
|
|
|
|
|
struct MessageHeader : public Serialize
|
|
|
|
{
|
2018-12-04 17:16:43 +01:00
|
|
|
const static size_t Size = 12;
|
|
|
|
|
|
|
|
MessageHeader() = default;
|
|
|
|
|
2018-12-03 23:22:59 +01:00
|
|
|
MsgID_t id;
|
|
|
|
Fields_t fields;
|
|
|
|
Count_t qd_count;
|
|
|
|
Count_t an_count;
|
|
|
|
Count_t ns_count;
|
|
|
|
Count_t ar_count;
|
|
|
|
|
|
|
|
bool
|
|
|
|
Encode(llarp_buffer_t* buf) const override;
|
|
|
|
|
|
|
|
bool
|
|
|
|
Decode(llarp_buffer_t* buf) override;
|
2018-12-04 17:16:43 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const MessageHeader& other) const
|
|
|
|
{
|
|
|
|
return id == other.id && fields == other.fields
|
|
|
|
&& qd_count == other.qd_count && an_count == other.an_count
|
|
|
|
&& ns_count == other.ns_count && ar_count == other.ar_count;
|
|
|
|
}
|
2018-12-03 23:22:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Message : public Serialize
|
|
|
|
{
|
2018-12-04 17:16:43 +01:00
|
|
|
Message(const MessageHeader& hdr);
|
|
|
|
|
|
|
|
Message(Message&& other);
|
|
|
|
Message(const Message& other);
|
|
|
|
|
2018-12-03 23:22:59 +01:00
|
|
|
void
|
|
|
|
UpdateHeader();
|
|
|
|
|
2018-12-04 17:16:43 +01:00
|
|
|
void
|
2018-12-03 23:22:59 +01:00
|
|
|
AddNXReply();
|
|
|
|
|
2018-12-07 22:52:19 +01:00
|
|
|
void
|
|
|
|
AddMXReply(std::string name, uint16_t priority);
|
|
|
|
|
2018-12-13 01:03:19 +01:00
|
|
|
void
|
|
|
|
AddCNAMEReply(std::string name, uint32_t ttl);
|
|
|
|
|
2018-12-04 17:16:43 +01:00
|
|
|
void
|
2018-12-03 23:22:59 +01:00
|
|
|
AddINReply(llarp::huint32_t addr);
|
|
|
|
|
2018-12-04 17:16:43 +01:00
|
|
|
void
|
2018-12-03 23:22:59 +01:00
|
|
|
AddAReply(std::string name);
|
|
|
|
|
|
|
|
bool
|
|
|
|
Encode(llarp_buffer_t* buf) const override;
|
|
|
|
|
|
|
|
bool
|
|
|
|
Decode(llarp_buffer_t* buf) override;
|
|
|
|
|
2018-12-04 17:16:43 +01:00
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const Message& msg)
|
|
|
|
{
|
|
|
|
out << "[dns message id=" << std::hex << msg.hdr_id
|
|
|
|
<< " fields=" << msg.hdr_fields << " questions=[ ";
|
|
|
|
for(const auto& qd : msg.questions)
|
|
|
|
out << qd << ", ";
|
|
|
|
out << "] answers=[ ";
|
|
|
|
for(const auto& an : msg.answers)
|
|
|
|
out << an << ", ";
|
|
|
|
out << "] nameserver=[ ";
|
|
|
|
for(const auto& ns : msg.authorities)
|
|
|
|
out << ns << ", ";
|
|
|
|
out << "] additional=[ ";
|
|
|
|
for(const auto& ar : msg.additional)
|
|
|
|
out << ar << ", ";
|
|
|
|
return out << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
MsgID_t hdr_id;
|
|
|
|
Fields_t hdr_fields;
|
2018-12-03 23:22:59 +01:00
|
|
|
std::vector< Question > questions;
|
|
|
|
std::vector< ResourceRecord > answers;
|
|
|
|
std::vector< ResourceRecord > authorities;
|
|
|
|
std::vector< ResourceRecord > additional;
|
|
|
|
};
|
|
|
|
} // namespace dns
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|