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
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>
91class SectionIterator : public std::iterator<std::input_iterator_tag, T> {
92public:
93 SectionIterator() : impl_(NULL) {}
97 void operator=(const SectionIterator<T>& source);
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:
106};
107
110
143class Message {
144public:
146 enum Mode {
147 PARSE = 0,
148 RENDER = 1
149 };
150
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 {
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
589 PRESERVE_ORDER = 1
590 };
591
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
662typedef boost::shared_ptr<Message> MessagePtr;
663typedef 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:
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:143
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:649
HeaderFlag
Constants for flag bit fields of a DNS message header.
Definition: message.h:193
@ HEADERFLAG_RD
Recursion desired.
Definition: message.h:197
@ HEADERFLAG_TC
Truncation.
Definition: message.h:196
@ HEADERFLAG_AA
Authoritative answer.
Definition: message.h:195
@ HEADERFLAG_RA
Recursion available.
Definition: message.h:198
@ HEADERFLAG_CD
DNSSEC checking disabled (RFC4035)
Definition: message.h:200
@ HEADERFLAG_QR
Query (if cleared) or response (if set)
Definition: message.h:194
@ HEADERFLAG_AD
Authentic data (RFC4035)
Definition: message.h:199
const Rcode & getRcode() const
Return the Response Code of the message.
Definition: dns/message.cc:433
bool getHeaderFlag(const HeaderFlag flag) const
Return whether the specified header flag bit is set in the header section.
Definition: dns/message.cc:391
void clear(Mode mode)
Clear the message content (if any) and reinitialize it in the specified mode.
Definition: dns/message.cc:981
Section
Constants to specify sections of a DNS message.
Definition: message.h:233
@ SECTION_ADDITIONAL
Additional section.
Definition: message.h:237
@ SECTION_AUTHORITY
Authority section.
Definition: message.h:236
@ SECTION_ANSWER
Answer section.
Definition: message.h:235
@ SECTION_QUESTION
Question section
Definition: message.h:234
static const uint16_t DEFAULT_MAX_UDPSIZE
The default maximum size of UDP DNS messages that don't cause truncation.
Definition: message.h:646
void clearSection(const Section section)
Remove all RRSets from the given Section.
Definition: dns/message.cc:568
ParseOptions
Parse options.
Definition: message.h:587
@ PARSE_DEFAULT
The default options.
Definition: message.h:588
@ PRESERVE_ORDER
Preserve RR order and don't combine them.
Definition: message.h:589
Mode
Constants to specify the operation mode of the Message.
Definition: message.h:146
@ RENDER
Render mode (building an outgoing message)
Definition: message.h:148
@ PARSE
Parse mode (handling an incoming message)
Definition: message.h:147
unsigned int getRRCount(const Section section) const
Returns the number of RRs contained in the given section.
Definition: dns/message.cc:491
void setOpcode(const Opcode &opcode)
Set the OPCODE of the header section of the message.
Definition: dns/message.cc:458
bool removeRRset(const Section section, RRsetIterator &iterator)
Remove RRSet from Message.
Definition: dns/message.cc:543
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
void fromWire(isc::util::InputBuffer &buffer, ParseOptions options=PARSE_DEFAULT)
(Re)build a Message object from wire-format data.
Definition: dns/message.cc:635
~Message()
The destructor.
Definition: dns/message.cc:386
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:481
std::string toText() const
Convert the Message to a string.
Definition: dns/message.cc:882
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:401
ConstEDNSPtr getEDNS() const
Return, if any, the EDNS associated with the message.
Definition: dns/message.cc:467
const Opcode & getOpcode() const
Return the OPCODE given in the header section of the message.
Definition: dns/message.cc:450
void parseHeader(isc::util::InputBuffer &buffer)
Parse the header section of the Message.
Definition: dns/message.cc:606
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
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
void addQuestion(QuestionPtr question)
Add a (pointer like object of) Question to the message.
Definition: dns/message.cc:585
void setQid(qid_t qid)
Set the query ID of the header section of the message.
Definition: dns/message.cc:424
qid_t getQid() const
Return the query ID given in the header section of the message.
Definition: dns/message.cc:419
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:441
Message(Mode mode)
The constructor.
Definition: dns/message.cc:382
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
void setEDNS(ConstEDNSPtr edns)
Set EDNS for the message.
Definition: dns/message.cc:472
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
bool operator==(const SectionIterator< T > &other) const
SectionIterator< T > & operator++()
void operator=(const SectionIterator< T > &source)
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< const Message > ConstMessagePtr
Definition: message.h:663
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:28
SectionIterator< QuestionPtr > QuestionIterator
Definition: message.h:108
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:47
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:662
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:109
FlexOptionImplPtr impl
Defines the logger used by the top-level component of kea-lfc.
Template version of Section Iterator.