Kea  2.1.7-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) {}
95  ~SectionIterator();
96  SectionIterator(const SectionIterator<T>& source);
97  void operator=(const SectionIterator<T>& source);
98  SectionIterator<T>& operator++();
99  SectionIterator<T> operator++(int);
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 {
234  SECTION_QUESTION = 0,
235  SECTION_ANSWER = 1,
236  SECTION_AUTHORITY = 2,
237  SECTION_ADDITIONAL = 3
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 
588  PARSE_DEFAULT = 0,
589  PRESERVE_ORDER = 1
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
Mode
Constants to specify the operation mode of the Message.
Definition: message.h:146
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
ParseOptions
Parse options.
Definition: message.h:587
The Question class encapsulates the common search key of DNS lookup, consisting of owner name...
Definition: question.h:95
boost::shared_ptr< const EDNS > ConstEDNSPtr
A pointer-like type pointing to an immutable EDNS object.
Definition: edns.h:37
bool operator!=(const Element &a, const Element &b)
Definition: data.cc:214
TSIG resource record.
Definition: tsigrecord.h:54
std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Overrides standard output stream operator for CSVRow object.
Definition: csv_file.cc:100
InvalidMessageUDPSize(const char *file, size_t line, const char *what)
Definition: message.h:71
DNS Response Codes (RCODEs) class.
Definition: rcode.h:40
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.
bool operator==(const Element &a, const Element &b)
Definition: data.cc:210
A standard DNS module exception that is thrown if a section iterator is being constructed for an inco...
Definition: message.h:47
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
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
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-lfc.
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
Section
Constants to specify sections of a DNS message.
Definition: message.h:233
FlexOptionImplPtr impl
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
The InputBuffer class is a buffer abstraction for manipulating read-only data.
Definition: buffer.h:81
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
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