Kea  1.9.9-git
message.h
Go to the documentation of this file.
1 // Copyright (C) 2009-2017 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef MESSAGE_H
8 #define MESSAGE_H 1
9 
10 #include <stdint.h>
11 
12 #include <iterator>
13 #include <string>
14 #include <ostream>
15 
16 #include <dns/exceptions.h>
17 
18 #include <dns/edns.h>
19 #include <dns/question.h>
20 #include <dns/rrset.h>
21 
22 namespace isc {
23 namespace util {
24 class InputBuffer;
25 }
26 
27 namespace dns {
28 class TSIGContext;
29 class TSIGRecord;
30 
37 public:
38  MessageTooShort(const char* file, size_t line, const char* what) :
39  isc::dns::Exception(file, line, what) {}
40 };
41 
48 public:
49  InvalidMessageSection(const char* file, size_t line, const char* what) :
50  isc::dns::Exception(file, line, what) {}
51 };
52 
59 public:
60  InvalidMessageOperation(const char* file, size_t line, const char* what) :
61  isc::dns::Exception(file, line, what) {}
62 };
63 
70 public:
71  InvalidMessageUDPSize(const char* file, size_t line, const char* what) :
72  isc::dns::Exception(file, line, what) {}
73 };
74 
75 typedef uint16_t qid_t;
76 
78 class Message;
79 class MessageImpl;
80 class Opcode;
81 class Rcode;
82 
83 template <typename T>
84 struct SectionIteratorImpl;
85 
90 template <typename T>
91 class SectionIterator : public std::iterator<std::input_iterator_tag, T> {
92 public:
93  SectionIterator() : impl_(NULL) {}
96  SectionIterator(const SectionIterator<T>& source);
97  void operator=(const SectionIterator<T>& source);
100  const T& operator*() const;
101  const T* operator->() const;
102  bool operator==(const SectionIterator<T>& other) const;
103  bool operator!=(const SectionIterator<T>& other) const;
104 private:
105  SectionIteratorImpl<T>* impl_;
106 };
107 
110 
143 class Message {
144 public:
146  enum Mode {
147  PARSE = 0,
148  RENDER = 1
149  };
150 
193  enum HeaderFlag {
194  HEADERFLAG_QR = 0x8000,
195  HEADERFLAG_AA = 0x0400,
196  HEADERFLAG_TC = 0x0200,
197  HEADERFLAG_RD = 0x0100,
198  HEADERFLAG_RA = 0x0080,
199  HEADERFLAG_AD = 0x0020,
200  HEADERFLAG_CD = 0x0010
201  };
202 
233  enum Section {
238  };
239 
248 
249 public:
252  Message(Mode mode);
254  ~Message();
255 private:
256  Message(const Message& source);
257  Message& operator=(const Message& source);
259 public:
269  bool getHeaderFlag(const HeaderFlag flag) const;
270 
297  void setHeaderFlag(const HeaderFlag flag, const bool on = true);
298 
300  qid_t getQid() const;
301 
307  void setQid(qid_t qid);
308 
320  const Rcode& getRcode() const;
321 
330  void setRcode(const Rcode& rcode);
331 
338  const Opcode& getOpcode() const;
339 
345  void setOpcode(const Opcode& opcode);
346 
353  ConstEDNSPtr getEDNS() const;
354 
363  void setEDNS(ConstEDNSPtr edns);
364 
382  const TSIGRecord* getTSIGRecord() const;
383 
407  unsigned int getRRCount(const Section section) const;
408 
411  const QuestionIterator beginQuestion() const;
412 
415  const QuestionIterator endQuestion() const;
416 
422  const RRsetIterator beginSection(const Section section) const;
423 
429  const RRsetIterator endSection(const Section section) const;
430 
436  void addQuestion(QuestionPtr question);
437 
449  void addQuestion(const Question& question);
450 
465  void addRRset(const Section section, RRsetPtr rrset);
466 
475  bool hasRRset(const Section section, const Name& name,
476  const RRClass& rrclass, const RRType& rrtype) const;
477 
483  bool hasRRset(const Section section, const RRsetPtr& rrset) const;
484 
499  bool removeRRset(const Section section, RRsetIterator& iterator);
500 
510  void clearSection(const Section section);
511 
512  // The following methods are not currently implemented.
513  //void removeQuestion(QuestionPtr question);
514  // notyet:
515  //void addRR(const Section section, const RR& rr);
516  //void removeRR(const Section section, const RR& rr);
517 
520  void clear(Mode mode);
521 
527  void appendSection(const Section section, const Message& source);
528 
534  void makeResponse();
535 
542  std::string toText() const;
543 
577  void toWire(AbstractMessageRenderer& renderer,
578  TSIGContext* tsig_ctx = NULL);
579 
590  };
591 
597  void parseHeader(isc::util::InputBuffer& buffer);
598 
635  void fromWire(isc::util::InputBuffer& buffer, ParseOptions options
636  = PARSE_DEFAULT);
637 
641 
642  static const uint16_t DEFAULT_MAX_UDPSIZE = 512;
647 
649  static const uint16_t DEFAULT_MAX_EDNS0_UDPSIZE = 4096;
651 
652 private:
653  MessageImpl* impl_;
654 };
655 
662 typedef boost::shared_ptr<Message> MessagePtr;
663 typedef boost::shared_ptr<const Message> ConstMessagePtr;
664 
675 std::ostream& operator<<(std::ostream& os, const Message& message);
676 }
677 }
678 #endif // MESSAGE_H
679 
680 // Local Variables:
681 // mode: c++
682 // End:
The Name class encapsulates DNS names.
Definition: name.h:223
uint16_t qid_t
Definition: message.h:75
const RRsetIterator endSection(const Section section) const
Return an iterator corresponding to the end of the given section (other than Question) of the message...
const Rcode & getRcode() const
Return the Response Code of the message.
Definition: dns/message.cc:433
void toWire(AbstractMessageRenderer &renderer, TSIGContext *tsig_ctx=NULL)
Render the message in wire formant into a message renderer object with (or without) TSIG...
Definition: dns/message.cc:601
The default options.
Definition: message.h:588
Render mode (building an outgoing message)
Definition: message.h:148
void clear(Mode mode)
Clear the message content (if any) and reinitialize it in the specified mode.
Definition: dns/message.cc:981
void setEDNS(ConstEDNSPtr edns)
Set EDNS for the message.
Definition: dns/message.cc:472
Mode
Constants to specify the operation mode of the Message.
Definition: message.h:146
ostream & operator<<(std::ostream &os, const EDNS &edns)
Insert the EDNS as a string into stream.
Definition: edns.cc:172
TSIG session context.
Definition: tsig.h:171
A standard DNS module exception that is thrown if a Message class method is called that is prohibited...
Definition: message.h:58
bool operator==(const SectionIterator< T > &other) const
ParseOptions
Parse options.
Definition: message.h:587
const QuestionIterator beginQuestion() const
Return an iterator corresponding to the beginning of the Question section of the message.
The Question class encapsulates the common search key of DNS lookup, consisting of owner name...
Definition: question.h:95
bool hasRRset(const Section section, const Name &name, const RRClass &rrclass, const RRType &rrtype) const
Determine whether the given section already has an RRset matching the given name, RR class and RR typ...
Definition: dns/message.cc:518
boost::shared_ptr< const EDNS > ConstEDNSPtr
A pointer-like type pointing to an immutable EDNS object.
Definition: edns.h:37
~Message()
The destructor.
Definition: dns/message.cc:386
qid_t getQid() const
Return the query ID given in the header section of the message.
Definition: dns/message.cc:419
Recursion available.
Definition: message.h:198
TSIG resource record.
Definition: tsigrecord.h:54
void clearSection(const Section section)
Remove all RRSets from the given Section.
Definition: dns/message.cc:568
InvalidMessageUDPSize(const char *file, size_t line, const char *what)
Definition: message.h:71
void setRcode(const Rcode &rcode)
Set the Response Code of the message.
Definition: dns/message.cc:441
DNS Response Codes (RCODEs) class.
Definition: rcode.h:40
void setHeaderFlag(const HeaderFlag flag, const bool on=true)
Set or clear the specified header flag bit in the header section.
Definition: dns/message.cc:401
SectionIterator< T > & operator++()
Authentic data (RFC4035)
Definition: message.h:199
const T & operator*() const
const Opcode & getOpcode() const
Return the OPCODE given in the header section of the message.
Definition: dns/message.cc:450
const T * operator->() const
The Message class encapsulates a standard DNS message.
Definition: message.h:143
SectionIterator< QuestionPtr > QuestionIterator
Definition: message.h:108
The RRClass class encapsulates DNS resource record classes.
Definition: rrclass.h:98
boost::shared_ptr< Message > MessagePtr
Pointer-like type pointing to a Message.
Definition: message.h:662
Template version of Section Iterator.
void operator=(const SectionIterator< T > &source)
ConstEDNSPtr getEDNS() const
Return, if any, the EDNS associated with the message.
Definition: dns/message.cc:467
A standard DNS module exception that is thrown if a section iterator is being constructed for an inco...
Definition: message.h:47
void fromWire(isc::util::InputBuffer &buffer, ParseOptions options=PARSE_DEFAULT)
(Re)build a Message object from wire-format data.
Definition: dns/message.cc:635
The AbstractMessageRenderer class is an abstract base class that provides common interfaces for rende...
InvalidMessageSection(const char *file, size_t line, const char *what)
Definition: message.h:49
void addQuestion(QuestionPtr question)
Add a (pointer like object of) Question to the message.
Definition: dns/message.cc:585
const QuestionIterator endQuestion() const
Return an iterator corresponding to the end of the Question section of the message.
bool operator!=(const SectionIterator< T > &other) const
bool getHeaderFlag(const HeaderFlag flag) const
Return whether the specified header flag bit is set in the header section.
Definition: dns/message.cc:391
Parse mode (handling an incoming message)
Definition: message.h:147
Authoritative answer.
Definition: message.h:195
Query (if cleared) or response (if set)
Definition: message.h:194
DNSSEC checking disabled (RFC4035)
Definition: message.h:200
A standard DNS module exception that is thrown if a wire format message parser encounters a short len...
Definition: message.h:36
MessageTooShort(const char *file, size_t line, const char *what)
Definition: message.h:38
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
std::string toText() const
Convert the Message to a string.
Definition: dns/message.cc:882
A standard DNS module exception that is thrown if a UDP buffer size smaller than the standard default...
Definition: message.h:69
Defines the logger used by the top-level component of kea-dhcp-ddns.
const RRsetIterator beginSection(const Section section) const
Return an iterator corresponding to the beginning of the given section (other than Question) of the m...
SectionIterator is a templated class to provide standard-compatible iterators for Questions and RRset...
Definition: message.h:91
SectionIterator< RRsetPtr > RRsetIterator
Definition: message.h:109
unsigned int getRRCount(const Section section) const
Returns the number of RRs contained in the given section.
Definition: dns/message.cc:491
void setOpcode(const Opcode &opcode)
Set the OPCODE of the header section of the message.
Definition: dns/message.cc:458
Section
Constants to specify sections of a DNS message.
Definition: message.h:233
bool removeRRset(const Section section, RRsetIterator &iterator)
Remove RRSet from Message.
Definition: dns/message.cc:543
FlexOptionImplPtr impl
void appendSection(const Section section, const Message &source)
Adds all rrsets from the source the given section in the source message to the same section of this m...
Definition: dns/message.cc:987
The RRType class encapsulates DNS resource record types.
Definition: rrtype.h:106
InvalidMessageOperation(const char *file, size_t line, const char *what)
Definition: message.h:60
Recursion desired.
Definition: message.h:197
const TSIGRecord * getTSIGRecord() const
Return, if any, the TSIG record contained in the received message.
Definition: dns/message.cc:481
void parseHeader(isc::util::InputBuffer &buffer)
Parse the header section of the Message.
Definition: dns/message.cc:606
The InputBuffer class is a buffer abstraction for manipulating read-only data.
Definition: buffer.h:81
void setQid(qid_t qid)
Set the query ID of the header section of the message.
Definition: dns/message.cc:424
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:47
HeaderFlag
Constants for flag bit fields of a DNS message header.
Definition: message.h:193
void makeResponse()
Prepare for making a response from a request.
static const uint16_t DEFAULT_MAX_UDPSIZE
The default maximum size of UDP DNS messages that don't cause truncation.
Definition: message.h:646
The Opcode class objects represent standard OPCODEs of the header section of DNS messages as defined ...
Definition: opcode.h:32
boost::shared_ptr< Question > QuestionPtr
A pointer-like type pointing to an Question object.
Definition: question.h:28
boost::shared_ptr< const Message > ConstMessagePtr
Definition: message.h:663
void addRRset(const Section section, RRsetPtr rrset)
Add a (pointer like object of) RRset to the given section of the message.
Definition: dns/message.cc:499
Preserve RR order and don't combine them.
Definition: message.h:589
Message(Mode mode)
The constructor.
Definition: dns/message.cc:382
static const uint16_t DEFAULT_MAX_EDNS0_UDPSIZE
The default maximum size of UDP DNS messages we can handle.
Definition: message.h:649