Kea 2.7.5
message.h
Go to the documentation of this file.
1// Copyright (C) 2009-2024 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
9
10#include <stdint.h>
11
12#include <iterator>
13#include <string>
14#include <ostream>
15
16#include <dns/exceptions.h>
17#include <util/buffer.h>
18
19#include <dns/edns.h>
20#include <dns/question.h>
21#include <dns/rrset.h>
22
23namespace isc {
24namespace dns {
25class TSIGContext;
26class TSIGRecord;
27
34public:
35 MessageTooShort(const char* file, size_t line, const char* what) :
36 isc::dns::Exception(file, line, what) {}
37};
38
45public:
46 InvalidMessageSection(const char* file, size_t line, const char* what) :
47 isc::dns::Exception(file, line, what) {}
48};
49
56public:
57 InvalidMessageOperation(const char* file, size_t line, const char* what) :
58 isc::dns::Exception(file, line, what) {}
59};
60
67public:
68 InvalidMessageUDPSize(const char* file, size_t line, const char* what) :
69 isc::dns::Exception(file, line, what) {}
70};
71
72typedef uint16_t qid_t;
73
75class Message;
76class MessageImpl;
77class Opcode;
78class Rcode;
79
80template <typename T>
82
87template <typename T>
89public:
90 // Aliases used to enable iterator behavior on this class
91 using iterator_category = std::input_iterator_tag;
92 using value_type = T;
93 using difference_type = std::ptrdiff_t;
94 using pointer = T*;
95 using reference = T&;
96
97 SectionIterator() : impl_(0) {
98 }
99 SectionIterator(const SectionIteratorImpl<T>& impl);
101 SectionIterator(const SectionIterator<T>& source);
102 void operator=(const SectionIterator<T>& source);
103 SectionIterator<T>& operator++();
104 SectionIterator<T> operator++(int);
105 const T& operator*() const;
106 const T* operator->() const;
107 bool operator==(const SectionIterator<T>& other) const;
108 bool operator!=(const SectionIterator<T>& other) const;
109private:
110 SectionIteratorImpl<T>* impl_;
111};
112
113typedef SectionIterator<QuestionPtr> QuestionIterator;
114typedef SectionIterator<RRsetPtr> RRsetIterator;
115
116class MessageImpl;
118typedef boost::shared_ptr<MessageImpl> MessageImplPtr;
119
152class Message {
153public:
155 enum Mode {
156 PARSE = 0, // Parse mode (handling an incoming message)
157 RENDER = 1 // Render mode (building an outgoing message)
158 };
159
202 enum HeaderFlag : int {
203 HEADERFLAG_QR = 0x8000, // Query (if cleared) or response (if set)
204 HEADERFLAG_AA = 0x0400, // Authoritative answer
205 HEADERFLAG_TC = 0x0200, // Truncation
206 HEADERFLAG_RD = 0x0100, // Recursion desired
207 HEADERFLAG_RA = 0x0080, // Recursion available
208 HEADERFLAG_AD = 0x0020, // Authentic %data (RFC4035)
209 HEADERFLAG_CD = 0x0010 // DNSSEC checking disabled (RFC4035)
210 };
211
242 enum Section : int {
243 SECTION_QUESTION = 0, // Question section
244 SECTION_ANSWER = 1, // Answer section
245 SECTION_AUTHORITY = 2, // Authority section
246 SECTION_ADDITIONAL = 3 // Additional section
247 };
248
257
258public:
261 Message(Mode mode);
263 ~Message() = default;
264private:
265 Message(const Message& source);
266 Message& operator=(const Message& source);
268public:
278 bool getHeaderFlag(const HeaderFlag flag) const;
279
306 void setHeaderFlag(const HeaderFlag flag, const bool on = true);
307
309 qid_t getQid() const;
310
316 void setQid(qid_t qid);
317
329 const Rcode& getRcode() const;
330
339 void setRcode(const Rcode& rcode);
340
347 const Opcode& getOpcode() const;
348
354 void setOpcode(const Opcode& opcode);
355
362 ConstEDNSPtr getEDNS() const;
363
372 void setEDNS(ConstEDNSPtr edns);
373
391 const TSIGRecord* getTSIGRecord() const;
392
416 unsigned int getRRCount(const Section section) const;
417
420 const QuestionIterator beginQuestion() const;
421
424 const QuestionIterator endQuestion() const;
425
431 const RRsetIterator beginSection(const Section section) const;
432
438 const RRsetIterator endSection(const Section section) const;
439
445 void addQuestion(QuestionPtr question);
446
458 void addQuestion(const Question& question);
459
474 void addRRset(const Section section, RRsetPtr rrset);
475
484 bool hasRRset(const Section section, const Name& name,
485 const RRClass& rrclass, const RRType& rrtype) const;
486
492 bool hasRRset(const Section section, const RRsetPtr& rrset) const;
493
508 bool removeRRset(const Section section, RRsetIterator& iterator);
509
519 void clearSection(const Section section);
520
521 // The following methods are not currently implemented.
522 //void removeQuestion(QuestionPtr question);
523 // notyet:
524 //void addRR(const Section section, const RR& rr);
525 //void removeRR(const Section section, const RR& rr);
526
529 void clear(Mode mode);
530
536 void appendSection(const Section section, const Message& source);
537
543 void makeResponse();
544
551 std::string toText() const;
552
586 void toWire(AbstractMessageRenderer& renderer,
587 TSIGContext* tsig_ctx = 0);
588
597 PARSE_DEFAULT = 0, // The default options
598 PRESERVE_ORDER = 1 // Preserve RR order and don't combine them
599 };
600
607
645
649
650
654 static const uint16_t DEFAULT_MAX_UDPSIZE = 512;
655
657 static const uint16_t DEFAULT_MAX_EDNS0_UDPSIZE = 4096;
659
660private:
661 MessageImplPtr impl_;
662};
663
670typedef boost::shared_ptr<Message> MessagePtr;
671typedef boost::shared_ptr<const Message> ConstMessagePtr;
672
683std::ostream& operator<<(std::ostream& os, const Message& message);
684
685} // namespace dns
686} // namespace isc
687
688#endif // MESSAGE_H
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
The AbstractMessageRenderer class is an abstract base class that provides common interfaces for rende...
A standard DNS module exception that is thrown if a Message class method is called that is prohibited...
Definition message.h:55
InvalidMessageOperation(const char *file, size_t line, const char *what)
Definition message.h:57
A standard DNS module exception that is thrown if a section iterator is being constructed for an inco...
Definition message.h:44
InvalidMessageSection(const char *file, size_t line, const char *what)
Definition message.h:46
A standard DNS module exception that is thrown if a UDP buffer size smaller than the standard default...
Definition message.h:66
InvalidMessageUDPSize(const char *file, size_t line, const char *what)
Definition message.h:68
A standard DNS module exception that is thrown if a wire format message parser encounters a short len...
Definition message.h:33
MessageTooShort(const char *file, size_t line, const char *what)
Definition message.h:35
The Message class encapsulates a standard DNS message.
Definition message.h:152
const QuestionIterator beginQuestion() const
Return an iterator corresponding to the beginning of the Question section of the message.
const QuestionIterator endQuestion() const
Return an iterator corresponding to the end of the Question section of the message.
static const uint16_t DEFAULT_MAX_EDNS0_UDPSIZE
The default maximum size of UDP DNS messages we can handle.
Definition message.h:657
void toWire(AbstractMessageRenderer &renderer, TSIGContext *tsig_ctx=0)
Render the message in wire formant into a message renderer object with (or without) TSIG.
const Rcode & getRcode() const
Return the Response Code of the message.
bool getHeaderFlag(const HeaderFlag flag) const
Return whether the specified header flag bit is set in the header section.
~Message()=default
The destructor.
void clear(Mode mode)
Clear the message content (if any) and reinitialize it in the specified mode.
static const uint16_t DEFAULT_MAX_UDPSIZE
The default maximum size of UDP DNS messages that don't cause truncation.
Definition message.h:654
void clearSection(const Section section)
Remove all RRSets from the given Section.
Section
Constants to specify sections of a DNS message.
Definition message.h:242
ParseOptions
Parse options.
Definition message.h:596
Mode
Constants to specify the operation mode of the Message.
Definition message.h:155
unsigned int getRRCount(const Section section) const
Returns the number of RRs contained in the given section.
void setOpcode(const Opcode &opcode)
Set the OPCODE of the header section of the message.
bool removeRRset(const Section section, RRsetIterator &iterator)
Remove RRSet from Message.
void addRRset(const Section section, RRsetPtr rrset)
Add a (pointer like object of) RRset to the given section of the message.
void fromWire(isc::util::InputBuffer &buffer, ParseOptions options=PARSE_DEFAULT)
(Re)build a Message object from wire-format data.
HeaderFlag
Constants for flag bit fields of a DNS message header.
Definition message.h:202
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 TSIGRecord * getTSIGRecord() const
Return, if any, the TSIG record contained in the received message.
std::string toText() const
Convert the Message to a string.
void makeResponse()
Prepare for making a response from a request.
void setHeaderFlag(const HeaderFlag flag, const bool on=true)
Set or clear the specified header flag bit in the header section.
ConstEDNSPtr getEDNS() const
Return, if any, the EDNS associated with the message.
const Opcode & getOpcode() const
Return the OPCODE given in the header section of the message.
void parseHeader(isc::util::InputBuffer &buffer)
Parse the header section of the Message.
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...
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...
void addQuestion(QuestionPtr question)
Add a (pointer like object of) Question to the message.
void setQid(qid_t qid)
Set the query ID of the header section of the message.
qid_t getQid() const
Return the query ID given in the header section of the message.
const RRsetIterator beginSection(const Section section) const
Return an iterator corresponding to the beginning of the given section (other than Question) of the m...
void setRcode(const Rcode &rcode)
Set the Response Code of the message.
Message(Mode mode)
The constructor.
void setEDNS(ConstEDNSPtr edns)
Set EDNS for the message.
The Name class encapsulates DNS names.
Definition name.h:219
The Opcode class objects represent standard OPCODEs of the header section of DNS messages as defined ...
Definition opcode.h:32
The Question class encapsulates the common search key of DNS lookup, consisting of owner name,...
Definition question.h:88
The RRClass class encapsulates DNS resource record classes.
Definition rrclass.h:89
The RRType class encapsulates DNS resource record types.
Definition rrtype.h:96
DNS Response Codes (RCODEs) class.
Definition rcode.h:40
SectionIterator is a templated class to provide standard-compatible iterators for Questions and RRset...
Definition message.h:88
const T & operator*() const
const T * operator->() const
bool operator!=(const SectionIterator< T > &other) const
std::ptrdiff_t difference_type
Definition message.h:93
bool operator==(const SectionIterator< T > &other) const
SectionIterator< T > & operator++()
void operator=(const SectionIterator< T > &source)
std::input_iterator_tag iterator_category
Definition message.h:91
TSIG session context.
Definition tsig.h:171
TSIG resource record.
Definition tsigrecord.h:51
The InputBuffer class is a buffer abstraction for manipulating read-only data.
Definition buffer.h:81
boost::shared_ptr< MessageImpl > MessageImplPtr
Pointer to the MessageImpl object.
boost::shared_ptr< const Message > ConstMessagePtr
Definition message.h:671
uint16_t qid_t
Definition message.h:72
boost::shared_ptr< Question > QuestionPtr
A pointer-like type pointing to an Question object.
Definition question.h:24
SectionIterator< QuestionPtr > QuestionIterator
Definition message.h:113
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition rrset.h:50
ostream & operator<<(std::ostream &os, const EDNS &edns)
Insert the EDNS as a string into stream.
Definition edns.cc:163
boost::shared_ptr< Message > MessagePtr
Pointer-like type pointing to a Message.
Definition message.h:670
boost::shared_ptr< const EDNS > ConstEDNSPtr
A pointer-like type pointing to an immutable EDNS object.
Definition edns.h:35
SectionIterator< RRsetPtr > RRsetIterator
Definition message.h:114
Defines the logger used by the top-level component of kea-lfc.
Template version of Section Iterator.