Kea 2.5.5
message.h
Go to the documentation of this file.
1// Copyright (C) 2009-2023 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
22namespace isc {
23namespace util {
24class InputBuffer;
25}
26
27namespace dns {
28class TSIGContext;
29class TSIGRecord;
30
37public:
38 MessageTooShort(const char* file, size_t line, const char* what) :
39 isc::dns::Exception(file, line, what) {}
40};
41
48public:
49 InvalidMessageSection(const char* file, size_t line, const char* what) :
50 isc::dns::Exception(file, line, what) {}
51};
52
59public:
60 InvalidMessageOperation(const char* file, size_t line, const char* what) :
61 isc::dns::Exception(file, line, what) {}
62};
63
70public:
71 InvalidMessageUDPSize(const char* file, size_t line, const char* what) :
72 isc::dns::Exception(file, line, what) {}
73};
74
75typedef uint16_t qid_t;
76
78class Message;
79class MessageImpl;
80class Opcode;
81class Rcode;
82
83template <typename T>
85
90template <typename T>
92public:
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) {}
103 SectionIterator(const SectionIterator<T>& source);
104 void operator=(const SectionIterator<T>& source);
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;
111private:
113};
114
117
118class MessageImpl;
120typedef boost::shared_ptr<MessageImpl> MessageImplPtr;
121
154class Message {
155public:
157 enum Mode {
158 PARSE = 0,
159 RENDER = 1
160 };
161
205 HEADERFLAG_QR = 0x8000,
206 HEADERFLAG_AA = 0x0400,
207 HEADERFLAG_TC = 0x0200,
208 HEADERFLAG_RD = 0x0100,
209 HEADERFLAG_RA = 0x0080,
210 HEADERFLAG_AD = 0x0020,
211 HEADERFLAG_CD = 0x0010
212 };
213
244 enum Section {
249 };
250
259
260public:
263 Message(Mode mode);
265 ~Message() = default;
266private:
267 Message(const Message& source);
268 Message& operator=(const Message& source);
270public:
280 bool getHeaderFlag(const HeaderFlag flag) const;
281
308 void setHeaderFlag(const HeaderFlag flag, const bool on = true);
309
311 qid_t getQid() const;
312
318 void setQid(qid_t qid);
319
331 const Rcode& getRcode() const;
332
341 void setRcode(const Rcode& rcode);
342
349 const Opcode& getOpcode() const;
350
356 void setOpcode(const Opcode& opcode);
357
364 ConstEDNSPtr getEDNS() const;
365
374 void setEDNS(ConstEDNSPtr edns);
375
393 const TSIGRecord* getTSIGRecord() const;
394
418 unsigned int getRRCount(const Section section) const;
419
422 const QuestionIterator beginQuestion() const;
423
426 const QuestionIterator endQuestion() const;
427
433 const RRsetIterator beginSection(const Section section) const;
434
440 const RRsetIterator endSection(const Section section) const;
441
447 void addQuestion(QuestionPtr question);
448
460 void addQuestion(const Question& question);
461
476 void addRRset(const Section section, RRsetPtr rrset);
477
486 bool hasRRset(const Section section, const Name& name,
487 const RRClass& rrclass, const RRType& rrtype) const;
488
494 bool hasRRset(const Section section, const RRsetPtr& rrset) const;
495
510 bool removeRRset(const Section section, RRsetIterator& iterator);
511
521 void clearSection(const Section section);
522
523 // The following methods are not currently implemented.
524 //void removeQuestion(QuestionPtr question);
525 // notyet:
526 //void addRR(const Section section, const RR& rr);
527 //void removeRR(const Section section, const RR& rr);
528
531 void clear(Mode mode);
532
538 void appendSection(const Section section, const Message& source);
539
545 void makeResponse();
546
553 std::string toText() const;
554
588 void toWire(AbstractMessageRenderer& renderer,
589 TSIGContext* tsig_ctx = NULL);
590
600 PRESERVE_ORDER = 1
601 };
602
609
646 void fromWire(isc::util::InputBuffer& buffer, ParseOptions options
647 = PARSE_DEFAULT);
648
652
653
657 static const uint16_t DEFAULT_MAX_UDPSIZE = 512;
658
660 static const uint16_t DEFAULT_MAX_EDNS0_UDPSIZE = 4096;
662
663private:
664 MessageImplPtr impl_;
665};
666
673typedef boost::shared_ptr<Message> MessagePtr;
674typedef boost::shared_ptr<const Message> ConstMessagePtr;
675
686std::ostream& operator<<(std::ostream& os, const Message& message);
687
688} // namespace dns
689} // namespace isc
690
691#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:58
InvalidMessageOperation(const char *file, size_t line, const char *what)
Definition: message.h:60
A standard DNS module exception that is thrown if a section iterator is being constructed for an inco...
Definition: message.h:47
InvalidMessageSection(const char *file, size_t line, const char *what)
Definition: message.h:49
A standard DNS module exception that is thrown if a UDP buffer size smaller than the standard default...
Definition: message.h:69
InvalidMessageUDPSize(const char *file, size_t line, const char *what)
Definition: message.h:71
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
The Message class encapsulates a standard DNS message.
Definition: message.h:154
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:660
HeaderFlag
Constants for flag bit fields of a DNS message header.
Definition: message.h:204
@ HEADERFLAG_RD
Recursion desired.
Definition: message.h:208
@ HEADERFLAG_TC
Truncation.
Definition: message.h:207
@ HEADERFLAG_AA
Authoritative answer.
Definition: message.h:206
@ HEADERFLAG_RA
Recursion available.
Definition: message.h:209
@ HEADERFLAG_CD
DNSSEC checking disabled (RFC4035)
Definition: message.h:211
@ HEADERFLAG_QR
Query (if cleared) or response (if set)
Definition: message.h:205
@ HEADERFLAG_AD
Authentic data (RFC4035)
Definition: message.h:210
const Rcode & getRcode() const
Return the Response Code of the message.
Definition: dns/message.cc:439
bool getHeaderFlag(const HeaderFlag flag) const
Return whether the specified header flag bit is set in the header section.
Definition: dns/message.cc:397
~Message()=default
The destructor.
void clear(Mode mode)
Clear the message content (if any) and reinitialize it in the specified mode.
Definition: dns/message.cc:992
Section
Constants to specify sections of a DNS message.
Definition: message.h:244
@ SECTION_ADDITIONAL
Additional section.
Definition: message.h:248
@ SECTION_AUTHORITY
Authority section.
Definition: message.h:247
@ SECTION_ANSWER
Answer section.
Definition: message.h:246
@ SECTION_QUESTION
Question section
Definition: message.h:245
static const uint16_t DEFAULT_MAX_UDPSIZE
The default maximum size of UDP DNS messages that don't cause truncation.
Definition: message.h:657
void clearSection(const Section section)
Remove all RRSets from the given Section.
Definition: dns/message.cc:573
ParseOptions
Parse options.
Definition: message.h:598
@ PARSE_DEFAULT
The default options.
Definition: message.h:599
@ PRESERVE_ORDER
Preserve RR order and don't combine them.
Definition: message.h:600
Mode
Constants to specify the operation mode of the Message.
Definition: message.h:157
@ RENDER
Render mode (building an outgoing message)
Definition: message.h:159
@ PARSE
Parse mode (handling an incoming message)
Definition: message.h:158
unsigned int getRRCount(const Section section) const
Returns the number of RRs contained in the given section.
Definition: dns/message.cc:497
void setOpcode(const Opcode &opcode)
Set the OPCODE of the header section of the message.
Definition: dns/message.cc:464
bool removeRRset(const Section section, RRsetIterator &iterator)
Remove RRSet from Message.
Definition: dns/message.cc:548
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:505
void fromWire(isc::util::InputBuffer &buffer, ParseOptions options=PARSE_DEFAULT)
(Re)build a Message object from wire-format data.
Definition: dns/message.cc:640
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.
Definition: dns/message.cc:487
std::string toText() const
Convert the Message to a string.
Definition: dns/message.cc:893
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.
Definition: dns/message.cc:407
ConstEDNSPtr getEDNS() const
Return, if any, the EDNS associated with the message.
Definition: dns/message.cc:473
const Opcode & getOpcode() const
Return the OPCODE given in the header section of the message.
Definition: dns/message.cc:456
void parseHeader(isc::util::InputBuffer &buffer)
Parse the header section of the Message.
Definition: dns/message.cc:611
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:524
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:998
void addQuestion(QuestionPtr question)
Add a (pointer like object of) Question to the message.
Definition: dns/message.cc:590
void setQid(qid_t qid)
Set the query ID of the header section of the message.
Definition: dns/message.cc:430
qid_t getQid() const
Return the query ID given in the header section of the message.
Definition: dns/message.cc:425
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.
Definition: dns/message.cc:447
Message(Mode mode)
The constructor.
Definition: dns/message.cc:392
void toWire(AbstractMessageRenderer &renderer, TSIGContext *tsig_ctx=NULL)
Render the message in wire formant into a message renderer object with (or without) TSIG.
Definition: dns/message.cc:606
void setEDNS(ConstEDNSPtr edns)
Set EDNS for the message.
Definition: dns/message.cc:478
The Name class encapsulates DNS names.
Definition: name.h:223
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:95
The RRClass class encapsulates DNS resource record classes.
Definition: rrclass.h:98
The RRType class encapsulates DNS resource record types.
Definition: rrtype.h:106
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:91
const T & operator*() const
const T * operator->() const
bool operator!=(const SectionIterator< T > &other) const
std::ptrdiff_t difference_type
Definition: message.h:96
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:94
TSIG session context.
Definition: tsig.h:171
TSIG resource record.
Definition: tsigrecord.h:54
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.
Definition: dns/message.cc:143
boost::shared_ptr< const Message > ConstMessagePtr
Definition: message.h:674
uint16_t qid_t
Definition: message.h:75
boost::shared_ptr< Question > QuestionPtr
A pointer-like type pointing to an Question object.
Definition: question.h:31
SectionIterator< QuestionPtr > QuestionIterator
Definition: message.h:115
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:53
ostream & operator<<(std::ostream &os, const EDNS &edns)
Insert the EDNS as a string into stream.
Definition: edns.cc:172
boost::shared_ptr< Message > MessagePtr
Pointer-like type pointing to a Message.
Definition: message.h:673
boost::shared_ptr< const EDNS > ConstEDNSPtr
A pointer-like type pointing to an immutable EDNS object.
Definition: edns.h:37
SectionIterator< RRsetPtr > RRsetIterator
Definition: message.h:116
FlexOptionImplPtr impl
Defines the logger used by the top-level component of kea-lfc.
Template version of Section Iterator.