E-MailRelay
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
GNet::DnsMessage Class Reference

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< Addressaddresses () 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
 

Detailed Description

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.

See also
RFC-1035

Definition at line 55 of file gdnsmessage.h.

Member Typedef Documentation

◆ Question

Definition at line 59 of file gdnsmessage.h.

◆ RR

Definition at line 60 of file gdnsmessage.h.

Constructor & Destructor Documentation

◆ DnsMessage() [1/2]

GNet::DnsMessage::DnsMessage ( const std::vector< char > &  buffer)
explicit

Constructor. Check with valid().

Definition at line 95 of file gdnsmessage.cpp.

◆ DnsMessage() [2/2]

GNet::DnsMessage::DnsMessage ( const char *  p,
std::size_t  n 
)

Constructor. Check with valid().

Definition at line 100 of file gdnsmessage.cpp.

Member Function Documentation

◆ AA()

bool GNet::DnsMessage::AA ( ) const

Returns the header AA flag (authorative).

Definition at line 178 of file gdnsmessage.cpp.

◆ addresses()

std::vector< GNet::Address > GNet::DnsMessage::addresses ( ) const

Returns the Answer addresses.

Definition at line 133 of file gdnsmessage.cpp.

◆ ANCOUNT()

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.

◆ ARCOUNT()

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.

◆ byte()

unsigned int GNet::DnsMessage::byte ( unsigned int  byte_index) const

Returns byte at the given offset.

Definition at line 143 of file gdnsmessage.cpp.

◆ empty()

GNet::DnsMessage GNet::DnsMessage::empty ( )
static

Factory function for an unusable object.

Most methods will throw, except n() will return zero.

Definition at line 121 of file gdnsmessage.cpp.

◆ ID()

unsigned int GNet::DnsMessage::ID ( ) const

Returns the header ID.

Definition at line 163 of file gdnsmessage.cpp.

◆ n()

std::size_t GNet::DnsMessage::n ( ) const
noexcept

Returns the raw data size.

Definition at line 115 of file gdnsmessage.cpp.

◆ NSCOUNT()

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.

◆ OPCODE()

unsigned int GNet::DnsMessage::OPCODE ( ) const

Returns the header OPCODE.

Definition at line 173 of file gdnsmessage.cpp.

◆ p()

const char * GNet::DnsMessage::p ( ) const
noexcept

Returns the raw data.

Definition at line 110 of file gdnsmessage.cpp.

◆ QDCOUNT()

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.

◆ QR()

bool GNet::DnsMessage::QR ( ) const

Returns the header QR (query/response).

Definition at line 168 of file gdnsmessage.cpp.

◆ question()

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.

◆ RA()

bool GNet::DnsMessage::RA ( ) const

Returns the header RA (recursion available).

Definition at line 196 of file gdnsmessage.cpp.

◆ RCODE()

unsigned int GNet::DnsMessage::RCODE ( ) const

Returns the header RCODE.

Definition at line 209 of file gdnsmessage.cpp.

◆ RD()

bool GNet::DnsMessage::RD ( ) const

Returns the header RD (recursion desired).

Definition at line 189 of file gdnsmessage.cpp.

◆ recordCount()

unsigned int GNet::DnsMessage::recordCount ( ) const
inline

Returns QDCOUNT()+ANCOUNT()+NSCOUNT()+ARCOUNT().

Definition at line 430 of file gdnsmessage.h.

◆ rejection()

GNet::DnsMessage GNet::DnsMessage::rejection ( const DnsMessage request,
unsigned int  rcode 
)
static

Factory function for a failure response based on the given request message.

Definition at line 238 of file gdnsmessage.cpp.

◆ request()

GNet::DnsMessage GNet::DnsMessage::request ( const std::string &  type,
const std::string &  hostname,
unsigned int  id = 0U 
)
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.

◆ rr()

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.

◆ rrAddress()

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.

◆ span()

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.

◆ TC()

bool GNet::DnsMessage::TC ( ) const

Returns the header TC flag (truncated).

Definition at line 183 of file gdnsmessage.cpp.

◆ valid()

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.

◆ word()

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.

◆ Z()

unsigned int GNet::DnsMessage::Z ( ) const

Returns the header Z value (zero).

Definition at line 203 of file gdnsmessage.cpp.

Friends And Related Function Documentation

◆ DnsMessageBuilder

friend class DnsMessageBuilder
friend

Definition at line 171 of file gdnsmessage.h.

◆ DnsMessageDumper

friend class DnsMessageDumper
friend

Definition at line 169 of file gdnsmessage.h.


The documentation for this class was generated from the following files: