21#ifndef G_NET_DNS_MESSAGE_H
22#define G_NET_DNS_MESSAGE_H
38 class DnsMessageRecordType ;
39 class DnsMessageQuestion ;
40 class DnsMessageNameParser ;
41 class DnsMessageRequest ;
42 class DnsMessageRRData ;
43 class DnsMessageDumper ;
44 class DnsMessageBuilder ;
58 G_EXCEPTION( Error ,
tx(
"dns message error") )
62 explicit DnsMessage(
const std::vector<char> & buffer ) ;
75 unsigned int ID()
const ;
81 unsigned int OPCODE()
const ;
96 unsigned int Z()
const ;
99 unsigned int RCODE()
const ;
125 RR rr(
unsigned int n )
const ;
136 const char *
p()
const noexcept ;
139 std::size_t
n()
const noexcept ;
142 unsigned int byte(
unsigned int byte_index )
const ;
145 unsigned int word(
unsigned int byte_index )
const ;
148 std::string
span(
unsigned int begin ,
unsigned int end )
const ;
169 friend class DnsMessageDumper ;
171 friend class DnsMessageBuilder ;
172 void convertToResponse(
unsigned int rcode ,
bool authoritative ) ;
173 void addByte(
unsigned int ) ;
174 void addWord(
unsigned int ) ;
177 std::vector<char> m_buffer ;
187 static unsigned int value( std::string_view type_name ) ;
191 static unsigned int value( std::string_view type_name , std::nothrow_t ) noexcept ;
195 static std::string
name(
unsigned int type_value ) ;
209 unsigned int byte(
unsigned int offset )
const ;
212 unsigned int word(
unsigned int offset )
const ;
215 std::string
span(
unsigned int begin ,
unsigned int end )
const ;
218 std::string
span(
unsigned int begin )
const ;
221 std::string
dname(
unsigned int rdata_offset )
const ;
224 std::string
dname(
unsigned int * rdata_offset_inout_p )
const ;
230 unsigned int size()
const ;
251 bool isa( std::string_view )
const noexcept ;
254 unsigned int type()
const ;
257 unsigned int class_()
const ;
260 unsigned int size()
const ;
263 std::string
name()
const ;
287 std::string rdataDname(
unsigned int rdata_offset )
const ;
288 std::string rdataDname(
unsigned int * rdata_offset_inout_p )
const ;
289 std::string rdataSpan(
unsigned int begin )
const ;
290 std::string rdataSpan(
unsigned int begin ,
unsigned int end )
const ;
291 unsigned int rdataOffset()
const ;
292 unsigned int rdataSize()
const ;
293 unsigned int rdataByte(
unsigned int offset )
const ;
294 unsigned int rdataWord(
unsigned int offset )
const ;
295 GNet::Address addressImp(
unsigned int port ,
bool & ok )
const ;
299 unsigned int m_offset {0U} ;
300 unsigned int m_size {0U} ;
301 unsigned int m_type {0U} ;
302 unsigned int m_class {0U} ;
303 unsigned int m_rdata_offset {0U} ;
304 unsigned int m_rdata_size {0U} ;
317 unsigned int size()
const ;
320 unsigned int qtype()
const ;
323 unsigned int qclass()
const ;
326 std::string
qname()
const ;
330 unsigned int m_size {0U} ;
331 unsigned int m_qtype {0U} ;
332 unsigned int m_qclass {0U} ;
333 std::string m_qname ;
343 static unsigned int size(
const DnsMessage & msg ,
unsigned int ) ;
346 static std::string
read(
const DnsMessage & msg ,
unsigned int ) ;
365 const char *
p()
const ;
368 std::size_t
n()
const ;
372 void addDomainName(
const std::string & domain ,
char sep ) ;
373 void addLabel( std::string_view ) ;
374 void addWord(
unsigned int ) ;
375 void addByte(
unsigned int ) ;
390 return static_cast<const DnsMessageRR *
>(
this)->rdataDname( offset ) ;
396 return static_cast<const DnsMessageRR *
>(
this)->rdataDname( offset_inout_p ) ;
402 return static_cast<const DnsMessageRR *
>(
this)->rdataSpan( begin ) ;
408 return static_cast<const DnsMessageRR *
>(
this)->rdataSpan( begin , end ) ;
414 return static_cast<const DnsMessageRR *
>(
this)->rdataSize() ;
420 return static_cast<const DnsMessageRR *
>(
this)->rdataByte( offset ) ;
426 return static_cast<const DnsMessageRR *
>(
this)->rdataWord( offset ) ;
432 return QDCOUNT() + ANCOUNT() + NSCOUNT() + ARCOUNT() ;
The GNet::Address class encapsulates a TCP/UDP transport address.
An implementation class used by GNet::DnsMessage to parse compressed domain names.
static unsigned int size(const DnsMessage &msg, unsigned int)
Returns the size of the compressed name at the given offset.
static std::string read(const DnsMessage &msg, unsigned int)
Returns the decompressed domain name at the given offset, made up of the labels with dots inbetween.
Represents DNS question record.
DnsMessageQuestion(const DnsMessage &, unsigned int offset)
Constructor.
unsigned int qtype() const
Returns the question QTYPE value.
std::string qname() const
Returns the question domain name (QNAME).
unsigned int size() const
Returns the record size.
unsigned int qclass() const
Returns the question QCLASS value.
A trivial mix-in base class that simplifies method names when accessing data from a DnsMessageRR deri...
unsigned int byte(unsigned int offset) const
Calls rdataByte().
std::string dname(unsigned int rdata_offset) const
Calls rdataDname().
unsigned int offset() const
Calls rdataOffset().
std::string span(unsigned int begin, unsigned int end) const
Calls rdataSpan().
unsigned int size() const
Calls rdataSize().
unsigned int word(unsigned int offset) const
Calls rdataWord().
Represents DNS response record.
unsigned int type() const
Returns the RR TYPE value().
unsigned int class_() const
Returns the RR CLASS value().
DnsMessageRR(const DnsMessage &, unsigned int offset)
Constructor from DnsMessage data.
unsigned int size() const
Returns the size of the RR.
Address address(unsigned int port=0U) const
Returns the Address if isa(A) or isa(AAAA).
const DnsMessageRRData & rdata() const
Provides access to the message RDATA.
bool isa(std::string_view) const noexcept
Returns true if the type() has the given name().
std::string name() const
Returns the RR NAME.
A static class for mapping between a RR type name, such as "AAAA", and its corresponding numeric valu...
static unsigned int value(std::string_view type_name)
Returns the type value for the given type name.
static std::string name(unsigned int type_value)
Returns the type name for the given type value.
Represents a DNS query message.
std::size_t n() const
Returns message size.
const char * p() const
Returns a pointer to the message data.
DnsMessageRequest(const std::string &type, const std::string &hostname, unsigned int id=0U)
Constructor.
A DNS message parser, with static factory functions for message composition.
Address rrAddress(unsigned int n) const
Returns the address in the n'th record.
unsigned int ANCOUNT() const
Returns the header ANCOUNT field, ie.
const char * p() const noexcept
Returns the raw data.
Question question(unsigned int n) const
Returns the n'th record as a Question record.
unsigned int RCODE() const
Returns the header RCODE.
bool QR() const
Returns the header QR (query/response).
bool valid() const
Returns true if the message data is big enough for a header and its TC() flag is false.
unsigned int Z() const
Returns the header Z value (zero).
static DnsMessage response(const DnsMessage &request, const Address &address)
Factory function for an answer response based on the given request message.
unsigned int QDCOUNT() const
Returns the header QDCOUNT field, ie.
unsigned int NSCOUNT() const
Returns the header NSCOUNT field, ie.
unsigned int ARCOUNT() const
Returns the header ARCOUNT field, ie.
bool AA() const
Returns the header AA flag (authorative).
unsigned int OPCODE() const
Returns the header OPCODE.
unsigned int recordCount() const
Returns QDCOUNT()+ANCOUNT()+NSCOUNT()+ARCOUNT().
static DnsMessage rejection(const DnsMessage &request, unsigned int rcode)
Factory function for a failure response based on the given request message.
bool TC() const
Returns the header TC flag (truncated).
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).
std::string span(unsigned int begin, unsigned int end) const
Returns the data in the given half-open byte range.
unsigned int byte(unsigned int byte_index) const
Returns byte at the given offset.
std::vector< Address > addresses() const
Returns the Answer addresses.
bool RD() const
Returns the header RD (recursion desired).
static DnsMessage empty()
Factory function for an unusable object.
unsigned int ID() const
Returns the header ID.
bool RA() const
Returns the header RA (recursion available).
RR rr(unsigned int n) const
Returns the n'th record as a RR record.
unsigned int word(unsigned int byte_index) const
Returns word at the given byte offset.
std::size_t n() const noexcept
Returns the raw data size.
std::string hostname()
Returns the hostname.
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().