Kea: message.h Source File

| Kea 2.2.0

message.h
Go to the documentation of this file.
1// Copyright (C) 2009-2017 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
36 class MessageTooShort : public isc::dns::Exception {
37public:
38 MessageTooShort(const char* file, size_t line, const char* what) :
39 isc::dns::Exception(file, line, what) {}
40};
41
47 class InvalidMessageSection : public isc::dns::Exception {
48public:
49 InvalidMessageSection(const char* file, size_t line, const char* what) :
50 isc::dns::Exception(file, line, what) {}
51};
52
58 class InvalidMessageOperation : public isc::dns::Exception {
59public:
60 InvalidMessageOperation(const char* file, size_t line, const char* what) :
61 isc::dns::Exception(file, line, what) {}
62};
63
69 class InvalidMessageUDPSize : public isc::dns::Exception {
70public:
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
77class AbstractMessageRenderer;
78class Message;
79class MessageImpl;
80class Opcode;
81class Rcode;
82
83template <typename T>
84struct SectionIteratorImpl;
85
90template <typename T>
91 class SectionIterator : public std::iterator<std::input_iterator_tag, T> {
92public:
93 SectionIterator() : impl_(NULL) {}
94 SectionIterator(const SectionIteratorImpl<T>& impl);
95 ~SectionIterator();
96 SectionIterator(const SectionIterator<T>& source);
97 void operator=(const SectionIterator<T>& source);
98 SectionIterator<T>& operator++();
99 SectionIterator<T> operator++(int);
100 const T& operator*() const;
101 const T* operator->() const;
102 bool operator==(const SectionIterator<T>& other) const;
103 bool operator!=(const SectionIterator<T>& other) const;
104private:
105 SectionIteratorImpl<T>* impl_;
106};
107
108 typedef SectionIterator<QuestionPtr> QuestionIterator;
109 typedef SectionIterator<RRsetPtr> RRsetIterator;
110
143 class Message {
144public:
146 enum Mode {
147 PARSE = 0,
148 RENDER = 1
149 };
150
193 enum HeaderFlag {
194 HEADERFLAG_QR = 0x8000,
195 HEADERFLAG_AA = 0x0400,
196 HEADERFLAG_TC = 0x0200,
197 HEADERFLAG_RD = 0x0100,
198 HEADERFLAG_RA = 0x0080,
199 HEADERFLAG_AD = 0x0020,
200 HEADERFLAG_CD = 0x0010
201 };
202
233 enum Section {
234 SECTION_QUESTION = 0,
235 SECTION_ANSWER = 1,
236 SECTION_AUTHORITY = 2,
237 SECTION_ADDITIONAL = 3
238 };
239
248
249public:
252 Message(Mode mode);
254 ~Message();
255private:
256 Message(const Message& source);
257 Message& operator=(const Message& source);
259public:
269 bool getHeaderFlag(const HeaderFlag flag) const;
270
297 void setHeaderFlag(const HeaderFlag flag, const bool on = true);
298
300 qid_t getQid() const;
301
307 void setQid(qid_t qid);
308
320 const Rcode& getRcode() const;
321
330 void setRcode(const Rcode& rcode);
331
338 const Opcode& getOpcode() const;
339
345 void setOpcode(const Opcode& opcode);
346
353 ConstEDNSPtr getEDNS() const;
354
363 void setEDNS(ConstEDNSPtr edns);
364
382 const TSIGRecord* getTSIGRecord() const;
383
407 unsigned int getRRCount(const Section section) const;
408
411 const QuestionIterator beginQuestion() const;
412
415 const QuestionIterator endQuestion() const;
416
422 const RRsetIterator beginSection(const Section section) const;
423
429 const RRsetIterator endSection(const Section section) const;
430
436 void addQuestion(QuestionPtr question);
437
449 void addQuestion(const Question& question);
450
465 void addRRset(const Section section, RRsetPtr rrset);
466
475 bool hasRRset(const Section section, const Name& name,
476 const RRClass& rrclass, const RRType& rrtype) const;
477
483 bool hasRRset(const Section section, const RRsetPtr& rrset) const;
484
499 bool removeRRset(const Section section, RRsetIterator& iterator);
500
510 void clearSection(const Section section);
511
512 // The following methods are not currently implemented.
513 //void removeQuestion(QuestionPtr question);
514 // notyet:
515 //void addRR(const Section section, const RR& rr);
516 //void removeRR(const Section section, const RR& rr);
517
520 void clear(Mode mode);
521
527 void appendSection(const Section section, const Message& source);
528
534 void makeResponse();
535
542 std::string toText() const;
543
577 void toWire(AbstractMessageRenderer& renderer,
578 TSIGContext* tsig_ctx = NULL);
579
587 enum ParseOptions {
588 PARSE_DEFAULT = 0,
589 PRESERVE_ORDER = 1
590 };
591
597 void parseHeader(isc::util::InputBuffer& buffer);
598
635 void fromWire(isc::util::InputBuffer& buffer, ParseOptions options
636 = PARSE_DEFAULT);
637
641
642
646 static const uint16_t DEFAULT_MAX_UDPSIZE = 512;
647
649 static const uint16_t DEFAULT_MAX_EDNS0_UDPSIZE = 4096;
651
652private:
653 MessageImpl* impl_;
654};
655
662 typedef boost::shared_ptr<Message> MessagePtr;
663 typedef boost::shared_ptr<const Message> ConstMessagePtr;
664
675std::ostream& operator<<(std::ostream& os, const Message& message);
676}
677}
678#endif // MESSAGE_H
679
680// Local Variables:
681// mode: c++
682// End:
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:68
isc::dns::Exception
Definition: dns/exceptions.h:25
isc::dns::InvalidMessageOperation
A standard DNS module exception that is thrown if a Message class method is called that is prohibited...
Definition: message.h:58
isc::dns::InvalidMessageOperation::InvalidMessageOperation
InvalidMessageOperation(const char *file, size_t line, const char *what)
Definition: message.h:60
isc::dns::InvalidMessageSection
A standard DNS module exception that is thrown if a section iterator is being constructed for an inco...
Definition: message.h:47
isc::dns::InvalidMessageSection::InvalidMessageSection
InvalidMessageSection(const char *file, size_t line, const char *what)
Definition: message.h:49
isc::dns::InvalidMessageUDPSize
A standard DNS module exception that is thrown if a UDP buffer size smaller than the standard default...
Definition: message.h:69
isc::dns::InvalidMessageUDPSize::InvalidMessageUDPSize
InvalidMessageUDPSize(const char *file, size_t line, const char *what)
Definition: message.h:71
isc::dns::MessageImpl
Definition: dns/message.cc:88
isc::dns::MessageTooShort
A standard DNS module exception that is thrown if a wire format message parser encounters a short len...
Definition: message.h:36
isc::dns::MessageTooShort::MessageTooShort
MessageTooShort(const char *file, size_t line, const char *what)
Definition: message.h:38
isc::dns::Message
The Message class encapsulates a standard DNS message.
Definition: message.h:143
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:1120
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:1125
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:649
isc::dns::Message::HeaderFlag
HeaderFlag
Constants for flag bit fields of a DNS message header.
Definition: message.h:193
isc::dns::Message::HEADERFLAG_RD
@ HEADERFLAG_RD
Recursion desired.
Definition: message.h:197
isc::dns::Message::HEADERFLAG_TC
@ HEADERFLAG_TC
Truncation.
Definition: message.h:196
isc::dns::Message::HEADERFLAG_AA
@ HEADERFLAG_AA
Authoritative answer.
Definition: message.h:195
isc::dns::Message::HEADERFLAG_RA
@ HEADERFLAG_RA
Recursion available.
Definition: message.h:198
isc::dns::Message::HEADERFLAG_CD
@ HEADERFLAG_CD
DNSSEC checking disabled (RFC4035)
Definition: message.h:200
isc::dns::Message::HEADERFLAG_QR
@ HEADERFLAG_QR
Query (if cleared) or response (if set)
Definition: message.h:194
isc::dns::Message::HEADERFLAG_AD
@ HEADERFLAG_AD
Authentic data (RFC4035)
Definition: message.h:199
isc::dns::Message::getRcode
const Rcode & getRcode() const
Return the Response Code of the message.
Definition: dns/message.cc:433
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:391
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:981
isc::dns::Message::Section
Section
Constants to specify sections of a DNS message.
Definition: message.h:233
isc::dns::Message::SECTION_ADDITIONAL
@ SECTION_ADDITIONAL
Additional section.
Definition: message.h:237
isc::dns::Message::SECTION_AUTHORITY
@ SECTION_AUTHORITY
Authority section.
Definition: message.h:236
isc::dns::Message::SECTION_ANSWER
@ SECTION_ANSWER
Answer section.
Definition: message.h:235
isc::dns::Message::SECTION_QUESTION
@ SECTION_QUESTION
Question section
Definition: message.h:234
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:646
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:587
isc::dns::Message::PARSE_DEFAULT
@ PARSE_DEFAULT
The default options.
Definition: message.h:588
isc::dns::Message::PRESERVE_ORDER
@ PRESERVE_ORDER
Preserve RR order and don't combine them.
Definition: message.h:589
isc::dns::Message::Mode
Mode
Constants to specify the operation mode of the Message.
Definition: message.h:146
isc::dns::Message::RENDER
@ RENDER
Render mode (building an outgoing message)
Definition: message.h:148
isc::dns::Message::PARSE
@ PARSE
Parse mode (handling an incoming message)
Definition: message.h:147
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:491
isc::dns::Message::setOpcode
void setOpcode(const Opcode &opcode)
Set the OPCODE of the header section of the message.
Definition: dns/message.cc:458
isc::dns::Message::removeRRset
bool removeRRset(const Section section, RRsetIterator &iterator)
Remove RRSet from Message.
Definition: dns/message.cc:543
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:499
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::~Message
~Message()
The destructor.
Definition: dns/message.cc:386
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:1146
isc::dns::Message::getTSIGRecord
const TSIGRecord * getTSIGRecord() const
Return, if any, the TSIG record contained in the received message.
Definition: dns/message.cc:481
isc::dns::Message::toText
std::string toText() const
Convert the Message to a string.
Definition: dns/message.cc:882
isc::dns::Message::makeResponse
void makeResponse()
Prepare for making a response from a request.
Definition: dns/message.cc:1008
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:401
isc::dns::Message::getEDNS
ConstEDNSPtr getEDNS() const
Return, if any, the EDNS associated with the message.
Definition: dns/message.cc:467
isc::dns::Message::getOpcode
const Opcode & getOpcode() const
Return the OPCODE given in the header section of the message.
Definition: dns/message.cc:450
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:518
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:987
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:424
isc::dns::Message::getQid
qid_t getQid() const
Return the query ID given in the header section of the message.
Definition: dns/message.cc:419
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:1133
isc::dns::Message::setRcode
void setRcode(const Rcode &rcode)
Set the Response Code of the message.
Definition: dns/message.cc:441
isc::dns::Message::Message
Message(Mode mode)
The constructor.
Definition: dns/message.cc:382
isc::dns::Message::toWire
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:601
isc::dns::Message::setEDNS
void setEDNS(ConstEDNSPtr edns)
Set EDNS for the message.
Definition: dns/message.cc:472
isc::dns::Name
The Name class encapsulates DNS names.
Definition: name.h:223
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:95
isc::dns::RRClass
The RRClass class encapsulates DNS resource record classes.
Definition: rrclass.h:98
isc::dns::RRType
The RRType class encapsulates DNS resource record types.
Definition: rrtype.h:106
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:91
isc::dns::SectionIterator::~SectionIterator
~SectionIterator()
Definition: dns/message.cc:1044
isc::dns::SectionIterator::SectionIterator
SectionIterator()
Definition: message.h:93
isc::dns::SectionIterator::operator*
const T & operator*() const
Definition: dns/message.cc:1082
isc::dns::SectionIterator::operator->
const T * operator->() const
Definition: dns/message.cc:1088
isc::dns::SectionIterator::operator!=
bool operator!=(const SectionIterator< T > &other) const
Definition: dns/message.cc:1100
isc::dns::SectionIterator::operator==
bool operator==(const SectionIterator< T > &other) const
Definition: dns/message.cc:1094
isc::dns::SectionIterator::operator++
SectionIterator< T > & operator++()
Definition: dns/message.cc:1067
isc::dns::SectionIterator::operator=
void operator=(const SectionIterator< T > &source)
Definition: dns/message.cc:1055
isc::dns::TSIGContext
TSIG session context.
Definition: tsig.h:171
isc::dns::TSIGRecord
TSIG resource record.
Definition: tsigrecord.h:54
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::ConstMessagePtr
boost::shared_ptr< const Message > ConstMessagePtr
Definition: message.h:663
isc::dns::qid_t
uint16_t qid_t
Definition: message.h:75
isc::dns::QuestionPtr
boost::shared_ptr< Question > QuestionPtr
A pointer-like type pointing to an Question object.
Definition: question.h:28
isc::dns::QuestionIterator
SectionIterator< QuestionPtr > QuestionIterator
Definition: message.h:108
isc::dns::RRsetPtr
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:47
isc::dns::operator<<
ostream & operator<<(std::ostream &os, const EDNS &edns)
Insert the EDNS as a string into stream.
Definition: edns.cc:172
isc::dns::MessagePtr
boost::shared_ptr< Message > MessagePtr
Pointer-like type pointing to a Message.
Definition: message.h:662
isc::dns::ConstEDNSPtr
boost::shared_ptr< const EDNS > ConstEDNSPtr
A pointer-like type pointing to an immutable EDNS object.
Definition: edns.h:37
isc::dns::RRsetIterator
SectionIterator< RRsetPtr > RRsetIterator
Definition: message.h:109
isc::flex_option::impl
FlexOptionImplPtr impl
Definition: flex_option_callouts.cc:19
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
question.h
rrset.h
isc::dns::SectionIteratorImpl
Template version of Section Iterator.
Definition: dns/message.cc:1032