Kea: message.h Source File

| Kea 3.2.0-git

message.h
Go to the documentation of this file.
1// Copyright (C) 2009-2025 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
33 class MessageTooShort : public isc::dns::Exception {
34public:
35 MessageTooShort(const char* file, size_t line, const char* what) :
36 isc::dns::Exception(file, line, what) {}
37};
38
44 class InvalidMessageSection : public isc::dns::Exception {
45public:
46 InvalidMessageSection(const char* file, size_t line, const char* what) :
47 isc::dns::Exception(file, line, what) {}
48};
49
55 class InvalidMessageOperation : public isc::dns::Exception {
56public:
57 InvalidMessageOperation(const char* file, size_t line, const char* what) :
58 isc::dns::Exception(file, line, what) {}
59};
60
66 class InvalidMessageUDPSize : public isc::dns::Exception {
67public:
68 InvalidMessageUDPSize(const char* file, size_t line, const char* what) :
69 isc::dns::Exception(file, line, what) {}
70};
71
72 typedef uint16_t qid_t;
73
74class AbstractMessageRenderer;
75class Message;
76class MessageImpl;
77class Opcode;
78class Rcode;
79
80template <typename T>
81struct SectionIteratorImpl;
82
87template <typename T>
88 class SectionIterator {
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);
100 ~SectionIterator();
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
113 typedef SectionIterator<QuestionPtr> QuestionIterator;
114 typedef SectionIterator<RRsetPtr> RRsetIterator;
115
116class MessageImpl;
118typedef boost::shared_ptr<MessageImpl> MessageImplPtr;
119
152 class 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 : uint16_t {
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 : uint16_t {
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
596 enum ParseOptions {
597 PARSE_DEFAULT = 0, // The default options
598 PRESERVE_ORDER = 1 // Preserve RR order and don't combine them
599 };
600
606 void parseHeader(isc::util::InputBuffer& buffer);
607
644 void fromWire(isc::util::InputBuffer& buffer, ParseOptions options = PARSE_DEFAULT);
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
670 typedef boost::shared_ptr<Message> MessagePtr;
671 typedef 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
buffer.h
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::dns::AbstractMessageRenderer
The AbstractMessageRenderer class is an abstract base class that provides common interfaces for rende...
Definition messagerenderer.h:69
isc::dns::Exception
Definition dns/exceptions.h:25
isc::dns::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Definition dns/exceptions.h:27
isc::dns::InvalidMessageOperation::InvalidMessageOperation
InvalidMessageOperation(const char *file, size_t line, const char *what)
Definition message.h:57
isc::dns::InvalidMessageSection::InvalidMessageSection
InvalidMessageSection(const char *file, size_t line, const char *what)
Definition message.h:46
isc::dns::InvalidMessageUDPSize::InvalidMessageUDPSize
InvalidMessageUDPSize(const char *file, size_t line, const char *what)
Definition message.h:68
isc::dns::MessageImpl
Definition dns/message.cc:84
isc::dns::MessageTooShort::MessageTooShort
MessageTooShort(const char *file, size_t line, const char *what)
Definition message.h:35
isc::dns::Message
The Message class encapsulates a standard DNS message.
Definition message.h:152
isc::dns::Message::beginQuestion
const QuestionIterator beginQuestion() const
Return an iterator corresponding to the beginning of the Question section of the message.
Definition dns/message.cc:1126
isc::dns::Message::endQuestion
const QuestionIterator endQuestion() const
Return an iterator corresponding to the end of the Question section of the message.
Definition dns/message.cc:1131
isc::dns::Message::DEFAULT_MAX_EDNS0_UDPSIZE
static const uint16_t DEFAULT_MAX_EDNS0_UDPSIZE
The default maximum size of UDP DNS messages we can handle.
Definition message.h:657
isc::dns::Message::toWire
void toWire(AbstractMessageRenderer &renderer, TSIGContext *tsig_ctx=0)
Render the message in wire formant into a message renderer object with (or without) TSIG.
Definition dns/message.cc:601
isc::dns::Message::getRcode
const Rcode & getRcode() const
Return the Response Code of the message.
Definition dns/message.cc:435
isc::dns::Message::getHeaderFlag
bool getHeaderFlag(const HeaderFlag flag) const
Return whether the specified header flag bit is set in the header section.
Definition dns/message.cc:393
isc::dns::Message::HeaderFlag
HeaderFlag
Constants for flag bit fields of a DNS message header.
Definition message.h:202
isc::dns::Message::HEADERFLAG_RD
@ HEADERFLAG_RD
Definition message.h:206
isc::dns::Message::HEADERFLAG_TC
@ HEADERFLAG_TC
Definition message.h:205
isc::dns::Message::HEADERFLAG_AA
@ HEADERFLAG_AA
Definition message.h:204
isc::dns::Message::HEADERFLAG_RA
@ HEADERFLAG_RA
Definition message.h:207
isc::dns::Message::HEADERFLAG_CD
@ HEADERFLAG_CD
Definition message.h:209
isc::dns::Message::HEADERFLAG_QR
@ HEADERFLAG_QR
Definition message.h:203
isc::dns::Message::HEADERFLAG_AD
@ HEADERFLAG_AD
Definition message.h:208
isc::dns::Message::~Message
~Message()=default
The destructor.
isc::dns::Message::clear
void clear(Mode mode)
Clear the message content (if any) and reinitialize it in the specified mode.
Definition dns/message.cc:989
isc::dns::Message::DEFAULT_MAX_UDPSIZE
static const uint16_t DEFAULT_MAX_UDPSIZE
The default maximum size of UDP DNS messages that don't cause truncation.
Definition message.h:654
isc::dns::Message::clearSection
void clearSection(const Section section)
Remove all RRSets from the given Section.
Definition dns/message.cc:568
isc::dns::Message::ParseOptions
ParseOptions
Parse options.
Definition message.h:596
isc::dns::Message::PARSE_DEFAULT
@ PARSE_DEFAULT
Definition message.h:597
isc::dns::Message::PRESERVE_ORDER
@ PRESERVE_ORDER
Definition message.h:598
isc::dns::Message::Mode
Mode
Constants to specify the operation mode of the Message.
Definition message.h:155
isc::dns::Message::RENDER
@ RENDER
Definition message.h:157
isc::dns::Message::PARSE
@ PARSE
Definition message.h:156
isc::dns::Message::getRRCount
unsigned int getRRCount(const Section section) const
Returns the number of RRs contained in the given section.
Definition dns/message.cc:493
isc::dns::Message::setOpcode
void setOpcode(const Opcode &opcode)
Set the OPCODE of the header section of the message.
Definition dns/message.cc:460
isc::dns::Message::removeRRset
bool removeRRset(const Section section, RRsetIterator &iterator)
Remove RRSet from Message.
Definition dns/message.cc:544
isc::dns::Message::addRRset
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:501
isc::dns::Message::fromWire
void fromWire(isc::util::InputBuffer &buffer, ParseOptions options=PARSE_DEFAULT)
(Re)build a Message object from wire-format data.
Definition dns/message.cc:635
isc::dns::Message::endSection
const RRsetIterator endSection(const Section section) const
Return an iterator corresponding to the end of the given section (other than Question) of the message...
Definition dns/message.cc:1152
isc::dns::Message::getTSIGRecord
const TSIGRecord * getTSIGRecord() const
Return, if any, the TSIG record contained in the received message.
Definition dns/message.cc:483
isc::dns::Message::toText
std::string toText() const
Convert the Message to a string.
Definition dns/message.cc:890
isc::dns::Message::makeResponse
void makeResponse()
Prepare for making a response from a request.
Definition dns/message.cc:1012
isc::dns::Message::Section
Section
Constants to specify sections of a DNS message.
Definition message.h:242
isc::dns::Message::SECTION_ADDITIONAL
@ SECTION_ADDITIONAL
Definition message.h:246
isc::dns::Message::SECTION_AUTHORITY
@ SECTION_AUTHORITY
Definition message.h:245
isc::dns::Message::SECTION_ANSWER
@ SECTION_ANSWER
Definition message.h:244
isc::dns::Message::SECTION_QUESTION
@ SECTION_QUESTION
Definition message.h:243
isc::dns::Message::setHeaderFlag
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:403
isc::dns::Message::getEDNS
ConstEDNSPtr getEDNS() const
Return, if any, the EDNS associated with the message.
Definition dns/message.cc:469
isc::dns::Message::getOpcode
const Opcode & getOpcode() const
Return the OPCODE given in the header section of the message.
Definition dns/message.cc:452
isc::dns::Message::parseHeader
void parseHeader(isc::util::InputBuffer &buffer)
Parse the header section of the Message.
Definition dns/message.cc:606
isc::dns::Message::hasRRset
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:520
isc::dns::Message::appendSection
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:995
isc::dns::Message::addQuestion
void addQuestion(QuestionPtr question)
Add a (pointer like object of) Question to the message.
Definition dns/message.cc:585
isc::dns::Message::setQid
void setQid(qid_t qid)
Set the query ID of the header section of the message.
Definition dns/message.cc:426
isc::dns::Message::getQid
qid_t getQid() const
Return the query ID given in the header section of the message.
Definition dns/message.cc:421
isc::dns::Message::beginSection
const RRsetIterator beginSection(const Section section) const
Return an iterator corresponding to the beginning of the given section (other than Question) of the m...
Definition dns/message.cc:1139
isc::dns::Message::setRcode
void setRcode(const Rcode &rcode)
Set the Response Code of the message.
Definition dns/message.cc:443
isc::dns::Message::Message
Message(Mode mode)
The constructor.
Definition dns/message.cc:388
isc::dns::Message::setEDNS
void setEDNS(ConstEDNSPtr edns)
Set EDNS for the message.
Definition dns/message.cc:474
isc::dns::Name
The Name class encapsulates DNS names.
Definition name.h:219
isc::dns::Opcode
The Opcode class objects represent standard OPCODEs of the header section of DNS messages as defined ...
Definition opcode.h:32
isc::dns::Question
The Question class encapsulates the common search key of DNS lookup, consisting of owner name,...
Definition question.h:88
isc::dns::RRClass
The RRClass class encapsulates DNS resource record classes.
Definition rrclass.h:89
isc::dns::RRType
The RRType class encapsulates DNS resource record types.
Definition rrtype.h:96
isc::dns::Rcode
DNS Response Codes (RCODEs) class.
Definition rcode.h:40
isc::dns::SectionIterator
SectionIterator is a templated class to provide standard-compatible iterators for Questions and RRset...
Definition message.h:88
isc::dns::SectionIterator::~SectionIterator
~SectionIterator()
Definition dns/message.cc:1050
isc::dns::SectionIterator::SectionIterator
SectionIterator()
Definition message.h:97
isc::dns::SectionIterator::operator*
const T & operator*() const
Definition dns/message.cc:1088
isc::dns::SectionIterator::reference
T & reference
Definition message.h:95
isc::dns::SectionIterator::operator->
const T * operator->() const
Definition dns/message.cc:1094
isc::dns::SectionIterator::operator!=
bool operator!=(const SectionIterator< T > &other) const
Definition dns/message.cc:1106
isc::dns::SectionIterator::SectionIterator
SectionIterator(const SectionIterator< T > &source)
Definition dns/message.cc:1055
isc::dns::SectionIterator::difference_type
std::ptrdiff_t difference_type
Definition message.h:93
isc::dns::SectionIterator::pointer
T * pointer
Definition message.h:94
isc::dns::SectionIterator::operator==
bool operator==(const SectionIterator< T > &other) const
Definition dns/message.cc:1100
isc::dns::SectionIterator::operator++
SectionIterator< T > & operator++()
Definition dns/message.cc:1073
isc::dns::SectionIterator::operator=
void operator=(const SectionIterator< T > &source)
Definition dns/message.cc:1061
isc::dns::SectionIterator::value_type
T value_type
Definition message.h:92
isc::dns::SectionIterator::SectionIterator
SectionIterator(const SectionIteratorImpl< T > &impl)
Definition dns/message.cc:1045
isc::dns::SectionIterator::iterator_category
std::input_iterator_tag iterator_category
Definition message.h:91
isc::dns::SectionIterator::operator++
SectionIterator< T > operator++(int)
Definition dns/message.cc:1080
isc::dns::TSIGContext
TSIG session context.
Definition tsig.h:171
isc::dns::TSIGRecord
TSIG resource record.
Definition tsigrecord.h:51
isc::util::InputBuffer
The InputBuffer class is a buffer abstraction for manipulating read-only data.
Definition buffer.h:81
exceptions.h
edns.h
isc::dns::name
Definition name.cc:69
isc::dns
Definition char_string.cc:26
isc::dns::MessageImplPtr
boost::shared_ptr< MessageImpl > MessageImplPtr
Pointer to the MessageImpl object.
Definition dns/message.cc:139
isc::dns::ConstMessagePtr
boost::shared_ptr< const Message > ConstMessagePtr
Definition message.h:671
isc::dns::qid_t
uint16_t qid_t
Definition message.h:72
isc::dns::QuestionPtr
boost::shared_ptr< Question > QuestionPtr
A pointer-like type pointing to an Question object.
Definition question.h:24
isc::dns::QuestionIterator
SectionIterator< QuestionPtr > QuestionIterator
Definition message.h:113
isc::dns::RRsetPtr
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition rrset.h:50
isc::dns::operator<<
ostream & operator<<(std::ostream &os, const EDNS &edns)
Insert the EDNS as a string into stream.
Definition edns.cc:163
isc::dns::MessagePtr
boost::shared_ptr< Message > MessagePtr
Pointer-like type pointing to a Message.
Definition message.h:670
isc::dns::ConstEDNSPtr
boost::shared_ptr< const EDNS > ConstEDNSPtr
A pointer-like type pointing to an immutable EDNS object.
Definition edns.h:35
isc::dns::RRsetIterator
SectionIterator< RRsetPtr > RRsetIterator
Definition message.h:114
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
question.h
rrset.h
isc::dns::SectionIteratorImpl
Template version of Section Iterator.
Definition dns/message.cc:1036