17#include <boost/foreach.hpp>
18#include <boost/lexical_cast.hpp>
19#include <boost/shared_ptr.hpp>
41using boost::lexical_cast;
50const size_t HEADERLEN = 12;
52const unsigned int OPCODE_MASK = 0x7800;
53const unsigned int OPCODE_SHIFT = 11;
54const unsigned int RCODE_MASK = 0x000f;
70const unsigned int HEADERFLAG_MASK = 0x87b0;
80const char*
const sectiontext[] = {
144 rcode_placeholder_(
Rcode(0)),
145 opcode_placeholder_(
Opcode(0))
200struct RenderSection {
205 void operator()(
const T& entry) {
210 const size_t pos0 =
renderer_.getLength();
220 unsigned int getTotalCount() {
return (
counter_); }
232 "Message rendering attempted in non render mode");
236 "Message rendering attempted without Rcode set");
240 "Message rendering attempted without Opcode set");
247 const size_t tsig_len = (tsig_ctx != NULL) ? tsig_ctx->
getTSIGLength() : 0;
259 if (tsig_len > orig_msg_len_limit) {
261 "too small limit for a TSIG (" <<
262 orig_msg_len_limit <<
")");
272 "too small limit for a Header");
274 renderer.
skip(HEADERLEN);
278 RenderSection<QuestionPtr>(renderer,
false)).getTotalCount();
281 uint16_t ancount = 0;
286 RenderSection<RRsetPtr>(renderer,
true)).getTotalCount();
288 uint16_t nscount = 0;
293 RenderSection<RRsetPtr>(renderer,
true)).getTotalCount();
295 uint16_t arcount = 0;
302 RenderSection<RRsetPtr>(renderer,
false)).getTotalCount();
325 renderer.
skip(HEADERLEN);
327 RenderSection<QuestionPtr>(renderer,
328 false)).getTotalCount();
345 size_t header_pos = 0;
347 header_pos +=
sizeof(uint16_t);
349 uint16_t codes_and_flags =
352 codes_and_flags |= (
flags_ & HEADERFLAG_MASK);
354 header_pos +=
sizeof(uint16_t);
357 header_pos +=
sizeof(uint16_t);
359 header_pos +=
sizeof(uint16_t);
361 header_pos +=
sizeof(uint16_t);
365 if (tsig_ctx != NULL) {
369 const int tsig_count =
372 if (tsig_count != 1) {
392 if (flag == 0 || (flag & ~HEADERFLAG_MASK) != 0) {
394 "Message::getHeaderFlag:: Invalid flag is specified: " <<
395 "0x" << std::hex << flag);
397 return ((impl_->
flags_ & flag) != 0);
404 "setHeaderFlag performed in non-render mode");
406 if (flag == 0 || (flag & ~HEADERFLAG_MASK) != 0) {
408 "Message::getHeaderFlag:: Invalid flag is specified: " <<
409 "0x" << std::hex << flag);
420 return (impl_->
qid_);
427 "setQid performed in non-render mode");
434 if (impl_->
rcode_ == NULL) {
444 "setRcode performed in non-render mode");
461 "setOpcode performed in non-render mode");
468 return (impl_->
edns_);
475 "setEDNS performed in non-render mode");
484 "getTSIGRecord performed in non-parse mode");
495 return (impl_->
counts_[section]);
502 "NULL RRset is given to Message::addRRset");
506 "addRRset performed in non-render mode");
512 impl_->
rrsets_[section].push_back(rrset);
513 impl_->
counts_[section] += rrset->getRdataCount();
514 impl_->
counts_[section] += rrset->getRRsigDataCount();
526 if (r->getClass() == rrclass &&
527 r->getType() == rrtype &&
528 r->getName() == name) {
538 return (
hasRRset(section, rrset->getName(),
539 rrset->getClass(), rrset->getType()));
548 bool removed =
false;
549 for (vector<RRsetPtr>::iterator i = impl_->
rrsets_[section].begin();
550 i != impl_->
rrsets_[section].end(); ++i) {
551 if (((*i)->getName() == (*iterator)->getName()) &&
552 ((*i)->getClass() == (*iterator)->getClass()) &&
553 ((*i)->getType() == (*iterator)->getType())) {
556 impl_->
counts_[section] -= (*iterator)->getRdataCount();
557 impl_->
counts_[section] -= (*iterator)->getRRsigDataCount();
558 impl_->
rrsets_[section].erase(i);
571 "clearSection performed in non-render mode");
579 impl_->
rrsets_[section].clear();
588 "addQuestion performed in non-render mode");
602 impl_->
toWire(renderer, tsig_ctx);
609 "Message parse attempted in non parse mode");
622 const uint16_t codes_and_flags = buffer.
readUint16();
623 impl_->
setOpcode(
Opcode((codes_and_flags & OPCODE_MASK) >> OPCODE_SHIFT));
625 impl_->
flags_ = (codes_and_flags & HEADERFLAG_MASK);
638 "Message parse attempted in non parse mode");
658 unsigned int added = 0;
660 for (
unsigned int count = 0;
663 const Name name(buffer);
666 2 *
sizeof(uint16_t)) {
688 bool operator()(
const RRsetPtr& rrset)
const {
689 return (rrset->getType() ==
rrtype_ &&
691 rrset->getName() ==
name_);
733 unsigned int added = 0;
735 for (
unsigned int count = 0; count <
counts_[section]; ++count) {
737 const size_t start_position = buffer.
getPosition();
739 const Name name(buffer);
743 3 *
sizeof(uint16_t) +
sizeof(uint32_t)) {
745 " section too short: " <<
760 addRR(section, name, rrclass, rrtype, ttl, options);
767 addEDNS(section, name, rrclass, rrtype, ttl, *rdata);
769 addTSIG(section, count, buffer, start_position, name, rrclass, ttl,
772 addRR(section, name, rrclass, rrtype, ttl, rdata, options);
787 vector<RRsetPtr>::iterator it =
789 MatchRR(name, rrtype, rrclass));
790 if (it !=
rrsets_[section].end()) {
791 (*it)->setTTL(min((*it)->getTTL(), ttl));
792 (*it)->addRdata(rdata);
797 rrset->addRdata(rdata);
798 rrsets_[section].push_back(rrset);
807 vector<RRsetPtr>::iterator it =
809 MatchRR(name, rrtype, rrclass));
810 if (it !=
rrsets_[section].end()) {
811 (*it)->setTTL(min((*it)->getTTL(), ttl));
816 rrsets_[section].push_back(rrset);
826 "EDNS OPT RR found in an invalid section");
832 uint8_t extended_rcode;
846 "TSIG RR found in an invalid section");
848 if (count !=
counts_[section] - 1) {
864struct SectionFormatter {
867 void operator()(
const T& entry) {
883 if (impl_->
rcode_ == NULL) {
885 "Message::toText() attempted without Rcode set");
889 "Message::toText() attempted without Opcode set");
897 s +=
", id: " + boost::lexical_cast<string>(impl_->
qid_);
926 s +=
", AUTHORITY: " +
930 if (impl_->
edns_ != NULL) {
936 s +=
", ADDITIONAL: " + lexical_cast<string>(arcount) +
"\n";
938 if (impl_->
edns_ != NULL) {
939 s +=
"\n;; OPT PSEUDOSECTION:\n";
940 s += impl_->
edns_->toText();
973 s +=
"\n;; TSIG PSEUDOSECTION:\n";
1011 "makeResponse() is performed in non-parse mode");
1017 impl_->
flags_ &= MESSAGE_REPLYPRESERVE;
1031template <
typename T>
1035 typename vector<T>::const_iterator
it_;
1038template <
typename T>
1043template <
typename T>
1048template <
typename T>
1053template <
typename T>
1056 if (impl_ == source.impl_) {
1065template <
typename T>
1072template <
typename T>
1080template <
typename T>
1083 return (*(impl_->it_));
1086template <
typename T>
1089 return (&(
operator*()));
1092template <
typename T>
1095 return (impl_->it_ == other.impl_->it_);
1098template <
typename T>
1101 return (impl_->it_ != other.impl_->it_);
1139 "RRset iterator is requested for question");
1152 "RRset iterator is requested for question");
1160 return (os << message.
toText());
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
A generic exception that is thrown when an unexpected error condition occurs.
The AbstractMessageRenderer class is an abstract base class that provides common interfaces for rende...
virtual void setLengthLimit(size_t len)=0
Set the maximum length of rendered data that can fit in the corresponding DNS message without truncat...
size_t getLength() const
Return the length of data written in the internal buffer.
virtual void clear()
Clear the internal buffer and other internal resources.
virtual size_t getLengthLimit() const =0
Return the maximum length of rendered data that can fit in the corresponding DNS message without trun...
virtual void setCompressMode(CompressMode mode)=0
Set the compression mode of the renderer class object.
const void * getData() const
Return a pointer to the head of the data stored in the internal buffer.
virtual CompressMode getCompressMode() const =0
Return the compression mode of the renderer class object.
virtual bool isTruncated() const =0
Return whether truncation has occurred while rendering.
void skip(size_t len)
Insert a specified length of gap at the end of the buffer.
void writeUint16At(uint16_t data, size_t pos)
Write an unsigned 16-bit integer in host byte order at the specified position of the internal buffer ...
CompressMode
Compression mode constants.
The EDNS class represents the EDNS OPT RR defined in RFC2671.
A standard DNS module exception that is thrown if a Message class method is called that is prohibited...
A standard DNS module exception that is thrown if a section iterator is being constructed for an inco...
void toWire(AbstractMessageRenderer &renderer, TSIGContext *tsig_ctx)
void addRR(Message::Section section, const Name &name, const RRClass &rrclass, const RRType &rrtype, const RRTTL &ttl, ConstRdataPtr rdata, Message::ParseOptions options)
static const unsigned int NUM_SECTIONS
vector< QuestionPtr > questions_
vector< RRsetPtr > rrsets_[NUM_SECTIONS]
int parseSection(const Message::Section section, InputBuffer &buffer, Message::ParseOptions options)
Opcode opcode_placeholder_
MessageImpl(Message::Mode mode)
void setOpcode(const Opcode &opcode)
void addTSIG(Message::Section section, unsigned int count, const InputBuffer &buffer, size_t start_position, const Name &name, const RRClass &rrclass, const RRTTL &ttl, const Rdata &rdata)
void addEDNS(Message::Section section, const Name &name, const RRClass &rrclass, const RRType &rrtype, const RRTTL &ttl, const Rdata &rdata)
ConstTSIGRecordPtr tsig_rr_
int parseQuestion(InputBuffer &buffer)
void setRcode(const Rcode &rcode)
int counts_[NUM_SECTIONS]
A standard DNS module exception that is thrown if a wire format message parser encounters a short len...
The Message class encapsulates a standard DNS message.
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.
HeaderFlag
Constants for flag bit fields of a DNS message header.
@ HEADERFLAG_RD
Recursion desired.
@ HEADERFLAG_TC
Truncation.
@ HEADERFLAG_AA
Authoritative answer.
@ HEADERFLAG_RA
Recursion available.
@ HEADERFLAG_CD
DNSSEC checking disabled (RFC4035)
@ HEADERFLAG_QR
Query (if cleared) or response (if set)
@ HEADERFLAG_AD
Authentic data (RFC4035)
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.
void clear(Mode mode)
Clear the message content (if any) and reinitialize it in the specified mode.
Section
Constants to specify sections of a DNS message.
@ SECTION_ADDITIONAL
Additional section.
@ SECTION_AUTHORITY
Authority section.
@ SECTION_ANSWER
Answer section.
@ SECTION_QUESTION
Question section
void clearSection(const Section section)
Remove all RRSets from the given Section.
ParseOptions
Parse options.
@ PRESERVE_ORDER
Preserve RR order and don't combine them.
Mode
Constants to specify the operation mode of the Message.
@ RENDER
Render mode (building an outgoing message)
@ PARSE
Parse mode (handling an incoming message)
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.
~Message()
The destructor.
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.
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.
void toWire(AbstractMessageRenderer &renderer, TSIGContext *tsig_ctx=NULL)
Render the message in wire formant into a message renderer object with (or without) TSIG.
void setEDNS(ConstEDNSPtr edns)
Set EDNS for the message.
The Name class encapsulates DNS names.
The Opcode class objects represent standard OPCODEs of the header section of DNS messages as defined ...
std::string toText() const
Convert the Opcode to a string.
CodeValue getCode() const
Returns the Opcode code value.
The Question class encapsulates the common search key of DNS lookup, consisting of owner name,...
The RRClass class encapsulates DNS resource record classes.
static const RRClass & ANY()
static const RRClass & NONE()
The RRTTL class encapsulates TTLs used in DNS resource records.
The RRType class encapsulates DNS resource record types.
static const RRType & TSIG()
static const RRType & OPT()
The RRset class is a concrete derived class of BasicRRset which contains a pointer to an additional R...
DNS Response Codes (RCODEs) class.
std::string toText() const
Convert the Rcode to a string.
uint16_t getCode() const
Returns the Rcode code value.
uint8_t getExtendedCode() const
Returns the upper 8-bit of the Rcode code value.
SectionIterator is a templated class to provide standard-compatible iterators for Questions and RRset...
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)
virtual ConstTSIGRecordPtr sign(const uint16_t qid, const void *const data, const size_t data_len)
Sign a DNS message.
virtual size_t getTSIGLength() const
Return the expected length of TSIG RR after sign()
The Rdata class is an abstract base class that provides a set of common interfaces to manipulate conc...
AbstractMessageRenderer & renderer_
const Message::Section section_
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Rdata > ConstRdataPtr
RdataPtr createRdata(const RRType &rrtype, const RRClass &rrclass, const std::string &rdata_string)
Create RDATA of a given pair of RR type and class from a string.
boost::shared_ptr< const TSIGRecord > ConstTSIGRecordPtr
A pointer-like type pointing to an immutable TSIGRecord object.
boost::shared_ptr< Question > QuestionPtr
A pointer-like type pointing to an Question object.
SectionIterator< QuestionPtr > QuestionIterator
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
ostream & operator<<(std::ostream &os, const EDNS &edns)
Insert the EDNS as a string into stream.
EDNS * createEDNSFromRR(const Name &name, const RRClass &rrclass, const RRType &rrtype, const RRTTL &ttl, const Rdata &rdata, uint8_t &extended_rcode)
Create a new EDNS object from a set of RR parameters, also providing the extended RCODE value.
boost::shared_ptr< const EDNS > ConstEDNSPtr
A pointer-like type pointing to an immutable EDNS object.
boost::shared_ptr< EDNS > EDNSPtr
A pointer-like type pointing to an EDNS object.
SectionIterator< RRsetPtr > RRsetIterator
boost::shared_ptr< const AbstractRRset > ConstRRsetPtr
A pointer-like type pointing to an (immutable) RRset object.
Defines the logger used by the top-level component of kea-lfc.
Template version of Section Iterator.
vector< T >::const_iterator it_
SectionIteratorImpl(const typename vector< T >::const_iterator &it)