Kea 3.3.3
message.h
Go to the documentation of this file.
1// Copyright (C) 2009-2026 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 <dns/edns.h>
11#include <dns/exceptions.h>
12#include <dns/question.h>
13#include <dns/rrset.h>
14#include <util/buffer.h>
15
16#include <cstdint>
17#include <iterator>
18#include <ostream>
19#include <string>
20
21namespace isc {
22namespace dns {
23class TSIGContext;
24class TSIGRecord;
25
32public:
33 MessageTooShort(const char* file, size_t line, const char* what) :
34 isc::dns::Exception(file, line, what) {}
35};
36
43public:
44 InvalidMessageSection(const char* file, size_t line, const char* what) :
45 isc::dns::Exception(file, line, what) {}
46};
47
54public:
55 InvalidMessageOperation(const char* file, size_t line, const char* what) :
56 isc::dns::Exception(file, line, what) {}
57};
58
65public:
66 InvalidMessageUDPSize(const char* file, size_t line, const char* what) :
67 isc::dns::Exception(file, line, what) {}
68};
69
70typedef uint16_t qid_t;
71
73class Message;
74class MessageImpl;
75class Opcode;
76class Rcode;
77
78template <typename T>
80
85template <typename T>
87public:
88 // Aliases used to enable iterator behavior on this class
89 using iterator_category = std::input_iterator_tag;
90 using value_type = T;
91 using difference_type = std::ptrdiff_t;
92 using pointer = T*;
93 using reference = T&;
94
95 SectionIterator() : impl_(0) {
96 }
100 void operator=(const SectionIterator<T>& source);
103 const T& operator*() const;
104 const T* operator->() const;
105 bool operator==(const SectionIterator<T>& other) const;
106 bool operator!=(const SectionIterator<T>& other) const;
107private:
109};
110
113
114class MessageImpl;
116typedef boost::shared_ptr<MessageImpl> MessageImplPtr;
117
150class Message {
151public:
153 enum Mode : uint16_t {
154 PARSE = 0, // Parse mode (handling an incoming message)
155 RENDER = 1 // Render mode (building an outgoing message)
156 };
157
200 enum HeaderFlag : uint16_t {
201 HEADERFLAG_QR = 0x8000, // Query (if cleared) or response (if set)
202 HEADERFLAG_AA = 0x0400, // Authoritative answer
203 HEADERFLAG_TC = 0x0200, // Truncation
204 HEADERFLAG_RD = 0x0100, // Recursion desired
205 HEADERFLAG_RA = 0x0080, // Recursion available
206 HEADERFLAG_AD = 0x0020, // Authentic %data (RFC4035)
207 HEADERFLAG_CD = 0x0010 // DNSSEC checking disabled (RFC4035)
208 };
209
240 enum Section : uint16_t {
241 SECTION_QUESTION = 0, // Question section
242 SECTION_ANSWER = 1, // Answer section
243 SECTION_AUTHORITY = 2, // Authority section
244 SECTION_ADDITIONAL = 3 // Additional section
245 };
246
255
256public:
259 Message(Mode mode);
261 ~Message() = default;
262private:
263 Message(const Message& source);
264 Message& operator=(const Message& source);
266public:
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 = 0);
586
594 enum ParseOptions : uint16_t {
595 PARSE_DEFAULT = 0, // The default options
596 PRESERVE_ORDER = 1 // Preserve RR order and don't combine them
597 };
598
605
643
647
648
652 static const uint16_t DEFAULT_MAX_UDPSIZE = 512;
653
655 static const uint16_t DEFAULT_MAX_EDNS0_UDPSIZE = 4096;
657
658private:
659 MessageImplPtr impl_;
660};
661
668typedef boost::shared_ptr<Message> MessagePtr;
669typedef boost::shared_ptr<const Message> ConstMessagePtr;
670
681std::ostream& operator<<(std::ostream& os, const Message& message);
682
683} // namespace dns
684} // namespace isc
685
686#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...
Exception(const char *file, size_t line, const char *what)
InvalidMessageOperation(const char *file, size_t line, const char *what)
Definition message.h:55
InvalidMessageSection(const char *file, size_t line, const char *what)
Definition message.h:44
InvalidMessageUDPSize(const char *file, size_t line, const char *what)
Definition message.h:66
MessageTooShort(const char *file, size_t line, const char *what)
Definition message.h:33
The Message class encapsulates a standard DNS message.
Definition message.h:150
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:655
void toWire(AbstractMessageRenderer &renderer, TSIGContext *tsig_ctx=0)
Render the message in wire formant into a message renderer object with (or without) TSIG.
Mode
Constants to specify the operation mode of the Message.
Definition message.h:153
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.
HeaderFlag
Constants for flag bit fields of a DNS message header.
Definition message.h:200
~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:652
void clearSection(const Section section)
Remove all RRSets from the given Section.
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.
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.
Section
Constants to specify sections of a DNS message.
Definition message.h:240
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.
ParseOptions
Parse options.
Definition message.h:594
void setEDNS(ConstEDNSPtr edns)
Set EDNS for the message.
The Name class encapsulates DNS names.
Definition name.h:217
The Opcode class objects represent standard OPCODEs of the header section of DNS messages as defined ...
Definition opcode.h:31
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:39
SectionIterator is a templated class to provide standard-compatible iterators for Questions and RRset...
Definition message.h:86
const T & operator*() const
const T * operator->() const
bool operator!=(const SectionIterator< T > &other) const
SectionIterator(const SectionIterator< T > &source)
std::ptrdiff_t difference_type
Definition message.h:91
bool operator==(const SectionIterator< T > &other) const
SectionIterator< T > & operator++()
void operator=(const SectionIterator< T > &source)
SectionIterator(const SectionIteratorImpl< T > &impl)
std::input_iterator_tag iterator_category
Definition message.h:89
SectionIterator< T > operator++(int)
TSIG session context.
Definition tsig.h:172
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:669
uint16_t qid_t
Definition message.h:70
boost::shared_ptr< Question > QuestionPtr
A pointer-like type pointing to an Question object.
Definition question.h:24
SectionIterator< QuestionPtr > QuestionIterator
Definition message.h:111
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:668
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:112
Defines the logger used by the top-level component of kea-lfc.
Template version of Section Iterator.