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 ;
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 ;
254 unsigned int type()
const ;
257 unsigned int class_()
const ;
260 unsigned int size()
const ;
263 std::string
name()
const ;
280 std::string rdataDname(
unsigned int rdata_offset )
const ;
281 std::string rdataDname(
unsigned int * rdata_offset_inout_p )
const ;
282 std::string rdataSpan(
unsigned int begin )
const ;
283 std::string rdataSpan(
unsigned int begin ,
unsigned int end )
const ;
284 unsigned int rdataOffset()
const ;
285 unsigned int rdataSize()
const ;
286 unsigned int rdataByte(
unsigned int offset )
const ;
287 unsigned int rdataWord(
unsigned int offset )
const ;
288 GNet::Address addressImp(
unsigned int port ,
bool & ok )
const ;
292 unsigned int m_offset ;
293 unsigned int m_size ;
294 unsigned int m_type ;
295 unsigned int m_class ;
296 unsigned int m_rdata_offset ;
297 unsigned int m_rdata_size ;
310 unsigned int size()
const ;
313 unsigned int qtype()
const ;
316 unsigned int qclass()
const ;
319 std::string
qname()
const ;
323 unsigned int m_size ;
324 unsigned int m_qtype ;
325 unsigned int m_qclass ;
326 std::string m_qname ;
336 static unsigned int size(
const DnsMessage & msg ,
unsigned int ) ;
339 static std::string
read(
const DnsMessage & msg ,
unsigned int ) ;
358 const char *
p()
const ;
361 std::size_t
n()
const ;
365 void addDomainName(
const std::string & domain ,
char sep ) ;
367 void addWord(
unsigned int ) ;
368 void addByte(
unsigned int ) ;
383 return static_cast<const DnsMessageRR *
>(
this)->rdataDname( offset ) ;
389 return static_cast<const DnsMessageRR *
>(
this)->rdataDname( offset_inout_p ) ;
395 return static_cast<const DnsMessageRR *
>(
this)->rdataSpan( begin ) ;
401 return static_cast<const DnsMessageRR *
>(
this)->rdataSpan( begin , end ) ;
407 return static_cast<const DnsMessageRR *
>(
this)->rdataSize() ;
413 return static_cast<const DnsMessageRR *
>(
this)->rdataByte( offset ) ;
419 return static_cast<const DnsMessageRR *
>(
this)->rdataWord( offset ) ;
425 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.
std::string name() const
Returns the RR NAME.
bool isa(G::string_view) const noexcept
Returns true if the type() has the given name().
A static class for mapping between a RR type name, such as "AAAA", and its corresponding numeric valu...
static unsigned int value(G::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.
A class like c++17's std::string_view.
std::string hostname()
Returns the hostname.
constexpr const char * tx(const char *p)
A briefer alternative to G::gettext_noop().