Kea  2.3.5-git
message.h
Go to the documentation of this file.
1 // Copyright (C) 2009-2022 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>
92 public:
93  // Aliases used to enable iterator behavior on this class
94  using iterator_category = std::input_iterator_tag;
95  using value_type = T;
96  using difference_type = std::ptrdiff_t;
97  using pointer = T*;
98  using reference = T&;
99 
100  SectionIterator() : impl_(NULL) {}
102  ~SectionIterator();
103  SectionIterator(const SectionIterator<T>& source);
104  void operator=(const SectionIterator<T>& source);
105  SectionIterator<T>& operator++();
106  SectionIterator<T> operator++(int);
107  const T& operator*() const;
108  const T* operator->() const;
109  bool operator==(const SectionIterator<T>& other) const;
110  bool operator!=(const SectionIterator<T>& other) const;
111 private:
112  SectionIteratorImpl<T>* impl_;
113 };
114 
117 
150 class Message {
151 public:
153  enum Mode {
154  PARSE = 0,
155  RENDER = 1
156  };
157 
200  enum HeaderFlag {
201  HEADERFLAG_QR = 0x8000,
202  HEADERFLAG_AA = 0x0400,
203  HEADERFLAG_TC = 0x0200,
204  HEADERFLAG_RD = 0x0100,
205  HEADERFLAG_RA = 0x0080,
206  HEADERFLAG_AD = 0x0020,
207  HEADERFLAG_CD = 0x0010
208  };
209 
240  enum Section {
241  SECTION_QUESTION = 0,
242  SECTION_ANSWER = 1,
243  SECTION_AUTHORITY = 2,
244  SECTION_ADDITIONAL = 3
245  };
246 
255 
256 public:
259  Message(Mode mode);
261  ~Message();
262 private:
263  Message(const Message& source);
264  Message& operator=(const Message& source);
266 public:
276  bool getHeaderFlag(const HeaderFlag flag) const;
277 
304  void setHeaderFlag(const HeaderFlag flag, const bool on = true);
305 
307  qid_t getQid() const;
308 
314  void setQid(qid_t qid);
315 
327  const Rcode& getRcode() const;
328 
337  void setRcode(const Rcode& rcode);
338 
345  const Opcode& getOpcode() const;
346 
352  void setOpcode(const Opcode& opcode);
353 
360  ConstEDNSPtr getEDNS() const;
361 
370  void setEDNS(ConstEDNSPtr edns);
371 
389  const TSIGRecord* getTSIGRecord() const;
390 
414  unsigned int getRRCount(const Section section) const;
415 
418  const QuestionIterator beginQuestion() const;
419 
422  const QuestionIterator endQuestion() const;
423 
429  const RRsetIterator beginSection(const Section section) const;
430 
436  const RRsetIterator endSection(const Section section) const;
437 
443  void addQuestion(QuestionPtr question);
444 
456  void addQuestion(const Question& question);
457 
472  void addRRset(const Section section, RRsetPtr rrset);
473 
482  bool hasRRset(const Section section, const Name& name,
483  const RRClass& rrclass, const RRType& rrtype) const;
484 
490  bool hasRRset(const Section section, const RRsetPtr& rrset) const;
491 
506  bool removeRRset(const Section section, RRsetIterator& iterator);
507 
517  void clearSection(const Section section);
518 
519  // The following methods are not currently implemented.
520  //void removeQuestion(QuestionPtr question);
521  // notyet:
522  //void addRR(const Section section, const RR& rr);
523  //void removeRR(const Section section, const RR& rr);
524 
527  void clear(Mode mode);
528 
534  void appendSection(const Section section, const Message& source);
535 
541  void makeResponse();
542 
549  std::string toText() const;
550 
584  void toWire(AbstractMessageRenderer& renderer,
585  TSIGContext* tsig_ctx = NULL);
586 
595  PARSE_DEFAULT = 0,
596  PRESERVE_ORDER = 1
597  };
598 
604  void parseHeader(isc::util::InputBuffer& buffer);
605 
642  void fromWire(isc::util::InputBuffer& buffer, ParseOptions options
643  = PARSE_DEFAULT);
644 
648 
649  static const uint16_t DEFAULT_MAX_UDPSIZE = 512;
654 
656  static const uint16_t DEFAULT_MAX_EDNS0_UDPSIZE = 4096;
658 
659 private:
660  MessageImpl* impl_;
661 };
662 
669 typedef boost::shared_ptr<Message> MessagePtr;
670 typedef boost::shared_ptr<const Message> ConstMessagePtr;
671 
682 std::ostream& operator<<(std::ostream& os, const Message& message);
683 
684 } // namespace dns
685 } // namespace isc
686 
687 #endif // MESSAGE_H
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:153
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:594
The Question class encapsulates the common search key of DNS lookup, consisting of owner name...
Definition: question.h:95
std::ptrdiff_t difference_type
Definition: message.h:96
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:150
SectionIterator< QuestionPtr > QuestionIterator
Definition: message.h:115
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:669
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:116
Section
Constants to specify sections of a DNS message.
Definition: message.h:240
std::input_iterator_tag iterator_category
Definition: message.h:94
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:200
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:670