E-MailRelay
|
A DNS message parser, with static factory functions for message composition. More...
#include <gdnsmessage.h>
Public Types | |
using | Question = DnsMessageQuestion |
using | RR = DnsMessageRR |
Public Member Functions | |
DnsMessage (const std::vector< char > &buffer) | |
Constructor. Check with valid(). More... | |
DnsMessage (const char *, std::size_t) | |
Constructor. Check with valid(). More... | |
bool | valid () const |
Returns true if the message data is big enough for a header and its TC() flag is false. More... | |
std::vector< Address > | addresses () const |
Returns the Answer addresses. More... | |
unsigned int | ID () const |
Returns the header ID. More... | |
bool | QR () const |
Returns the header QR (query/response). More... | |
unsigned int | OPCODE () const |
Returns the header OPCODE. More... | |
bool | AA () const |
Returns the header AA flag (authorative). More... | |
bool | TC () const |
Returns the header TC flag (truncated). More... | |
bool | RD () const |
Returns the header RD (recursion desired). More... | |
bool | RA () const |
Returns the header RA (recursion available). More... | |
unsigned int | Z () const |
Returns the header Z value (zero). More... | |
unsigned int | RCODE () const |
Returns the header RCODE. More... | |
unsigned int | QDCOUNT () const |
Returns the header QDCOUNT field, ie. More... | |
unsigned int | ANCOUNT () const |
Returns the header ANCOUNT field, ie. More... | |
unsigned int | NSCOUNT () const |
Returns the header NSCOUNT field, ie. More... | |
unsigned int | ARCOUNT () const |
Returns the header ARCOUNT field, ie. More... | |
unsigned int | recordCount () const |
Returns QDCOUNT()+ANCOUNT()+NSCOUNT()+ARCOUNT(). More... | |
Question | question (unsigned int n) const |
Returns the n'th record as a Question record. More... | |
RR | rr (unsigned int n) const |
Returns the n'th record as a RR record. More... | |
Address | rrAddress (unsigned int n) const |
Returns the address in the n'th record. More... | |
const char * | p () const noexcept |
Returns the raw data. More... | |
std::size_t | n () const noexcept |
Returns the raw data size. More... | |
unsigned int | byte (unsigned int byte_index) const |
Returns byte at the given offset. More... | |
unsigned int | word (unsigned int byte_index) const |
Returns word at the given byte offset. More... | |
std::string | span (unsigned int begin, unsigned int end) const |
Returns the data in the given half-open byte range. More... | |
Static Public Member Functions | |
static DnsMessage | request (const std::string &type, const std::string &hostname, unsigned int id=0U) |
Factory function for a request message of the give type ("A", "AAAA", etc). More... | |
static DnsMessage | rejection (const DnsMessage &request, unsigned int rcode) |
Factory function for a failure response based on the given request message. More... | |
static DnsMessage | response (const DnsMessage &request, const Address &address) |
Factory function for an answer response based on the given request message. | |
static DnsMessage | empty () |
Factory function for an unusable object. More... | |
Friends | |
class | DnsMessageDumper |
class | DnsMessageBuilder |
A DNS message parser, with static factory functions for message composition.
A DnsMessage contains a Header and four sections: Question, Answer, Authority and Additional. The Question section contains Question records (DnsMessageQuestion) while the Answer, Authority and Additional sections contain RR records (DnsMessageRR). Each RR has a standard header followed by RDATA.
Definition at line 55 of file gdnsmessage.h.
Definition at line 59 of file gdnsmessage.h.
using GNet::DnsMessage::RR = DnsMessageRR |
Definition at line 60 of file gdnsmessage.h.
|
explicit |
Constructor. Check with valid().
Definition at line 95 of file gdnsmessage.cpp.
GNet::DnsMessage::DnsMessage | ( | const char * | p, |
std::size_t | n | ||
) |
Constructor. Check with valid().
Definition at line 100 of file gdnsmessage.cpp.
bool GNet::DnsMessage::AA | ( | ) | const |
Returns the header AA flag (authorative).
Definition at line 178 of file gdnsmessage.cpp.
std::vector< GNet::Address > GNet::DnsMessage::addresses | ( | ) | const |
Returns the Answer addresses.
Definition at line 133 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::ANCOUNT | ( | ) | const |
Returns the header ANCOUNT field, ie.
the number of RR records in the Answer section.
Definition at line 219 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::ARCOUNT | ( | ) | const |
Returns the header ARCOUNT field, ie.
the number of RR records in the Additional section.
Definition at line 232 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::byte | ( | unsigned int | byte_index | ) | const |
Returns byte at the given offset.
Definition at line 143 of file gdnsmessage.cpp.
|
static |
Factory function for an unusable object.
Most methods will throw, except n() will return zero.
Definition at line 121 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::ID | ( | ) | const |
Returns the header ID.
Definition at line 163 of file gdnsmessage.cpp.
|
noexcept |
Returns the raw data size.
Definition at line 115 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::NSCOUNT | ( | ) | const |
Returns the header NSCOUNT field, ie.
the number of RR records in the Authority section.
Definition at line 225 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::OPCODE | ( | ) | const |
Returns the header OPCODE.
Definition at line 173 of file gdnsmessage.cpp.
|
noexcept |
Returns the raw data.
Definition at line 110 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::QDCOUNT | ( | ) | const |
Returns the header QDCOUNT field, ie.
the number of records in the Question section.
Definition at line 214 of file gdnsmessage.cpp.
bool GNet::DnsMessage::QR | ( | ) | const |
Returns the header QR (query/response).
Definition at line 168 of file gdnsmessage.cpp.
GNet::DnsMessageQuestion GNet::DnsMessage::question | ( | unsigned int | n | ) | const |
Returns the n'th record as a Question record.
Precondition: n < QDCOUNT()
Definition at line 279 of file gdnsmessage.cpp.
bool GNet::DnsMessage::RA | ( | ) | const |
Returns the header RA (recursion available).
Definition at line 196 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::RCODE | ( | ) | const |
Returns the header RCODE.
Definition at line 209 of file gdnsmessage.cpp.
bool GNet::DnsMessage::RD | ( | ) | const |
Returns the header RD (recursion desired).
Definition at line 189 of file gdnsmessage.cpp.
|
inline |
|
static |
Factory function for a failure response based on the given request message.
Definition at line 238 of file gdnsmessage.cpp.
|
static |
Factory function for a request message of the give type ("A", "AAAA", etc).
The type name is interpreted by DnsMessageRecordType::value().
Definition at line 127 of file gdnsmessage.cpp.
GNet::DnsMessageRR GNet::DnsMessage::rr | ( | unsigned int | n | ) | const |
Returns the n'th record as a RR record.
The returned object retains a reference to this DnsMessage, so prefer rrAddress(). Precondition: n >= QDCOUNT() && n < recordCount()
Definition at line 290 of file gdnsmessage.cpp.
GNet::Address GNet::DnsMessage::rrAddress | ( | unsigned int | n | ) | const |
Returns the address in the n'th record.
Throws if not A or AAAA. Precondition: n >= QDCOUNT()
Definition at line 306 of file gdnsmessage.cpp.
std::string GNet::DnsMessage::span | ( | unsigned int | begin, |
unsigned int | end | ||
) | const |
Returns the data in the given half-open byte range.
Definition at line 156 of file gdnsmessage.cpp.
bool GNet::DnsMessage::TC | ( | ) | const |
Returns the header TC flag (truncated).
Definition at line 183 of file gdnsmessage.cpp.
bool GNet::DnsMessage::valid | ( | ) | const |
Returns true if the message data is big enough for a header and its TC() flag is false.
Definition at line 105 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::word | ( | unsigned int | byte_index | ) | const |
Returns word at the given byte offset.
Definition at line 151 of file gdnsmessage.cpp.
unsigned int GNet::DnsMessage::Z | ( | ) | const |
Returns the header Z value (zero).
Definition at line 203 of file gdnsmessage.cpp.
|
friend |
Definition at line 171 of file gdnsmessage.h.
|
friend |
Definition at line 169 of file gdnsmessage.h.