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[] = {
147 rcode_placeholder_(
Rcode(0)),
148 opcode_placeholder_(
Opcode(0)) {
202struct RenderSection {
208 void operator()(
const T& entry) {
213 const size_t pos0 =
renderer_.getLength();
224 unsigned int getTotalCount() {
242 "Message rendering attempted in non render mode");
246 "Message rendering attempted without Rcode set");
250 "Message rendering attempted without Opcode set");
257 const size_t tsig_len = (tsig_ctx != NULL) ? tsig_ctx->
getTSIGLength() : 0;
269 if (tsig_len > orig_msg_len_limit) {
271 "too small limit for a TSIG (" <<
272 orig_msg_len_limit <<
")");
282 "too small limit for a Header");
284 renderer.
skip(HEADERLEN);
288 RenderSection<QuestionPtr>(renderer,
false)).getTotalCount();
291 uint16_t ancount = 0;
296 RenderSection<RRsetPtr>(renderer,
true)).getTotalCount();
298 uint16_t nscount = 0;
303 RenderSection<RRsetPtr>(renderer,
true)).getTotalCount();
305 uint16_t arcount = 0;
312 RenderSection<RRsetPtr>(renderer,
false)).getTotalCount();
335 renderer.
skip(HEADERLEN);
337 RenderSection<QuestionPtr>(renderer,
338 false)).getTotalCount();
355 size_t header_pos = 0;
357 header_pos +=
sizeof(uint16_t);
359 uint16_t codes_and_flags =
362 codes_and_flags |= (
flags_ & HEADERFLAG_MASK);
364 header_pos +=
sizeof(uint16_t);
367 header_pos +=
sizeof(uint16_t);
369 header_pos +=
sizeof(uint16_t);
371 header_pos +=
sizeof(uint16_t);
375 if (tsig_ctx != NULL) {
379 const int tsig_count =
382 if (tsig_count != 1) {
398 if (flag == 0 || (flag & ~HEADERFLAG_MASK) != 0) {
400 "Message::getHeaderFlag:: Invalid flag is specified: " <<
401 "0x" << std::hex << flag);
403 return ((impl_->flags_ & flag) != 0);
410 "setHeaderFlag performed in non-render mode");
412 if (flag == 0 || (flag & ~HEADERFLAG_MASK) != 0) {
414 "Message::getHeaderFlag:: Invalid flag is specified: " <<
415 "0x" << std::hex <<
static_cast<int>(flag));
418 impl_->flags_ |= flag;
420 impl_->flags_ &= ~flag;
426 return (impl_->qid_);
433 "setQid performed in non-render mode");
440 if (impl_->rcode_ == NULL) {
443 return (*impl_->rcode_);
450 "setRcode performed in non-render mode");
452 impl_->setRcode(rcode);
457 if (impl_->opcode_ == NULL) {
460 return (*impl_->opcode_);
467 "setOpcode performed in non-render mode");
469 impl_->setOpcode(opcode);
474 return (impl_->edns_);
481 "setEDNS performed in non-render mode");
490 "getTSIGRecord performed in non-parse mode");
493 return (impl_->tsig_rr_.get());
501 return (impl_->counts_[section]);
508 "NULL RRset is given to Message::addRRset");
512 "addRRset performed in non-render mode");
518 impl_->rrsets_[section].push_back(rrset);
519 impl_->counts_[section] += rrset->getRdataCount();
520 impl_->counts_[section] += rrset->getRRsigDataCount();
531 if (r->getClass() == rrclass &&
532 r->getType() == rrtype &&
533 r->getName() == name) {
543 return (
hasRRset(section, rrset->getName(),
544 rrset->getClass(), rrset->getType()));
553 bool removed =
false;
554 for (vector<RRsetPtr>::iterator i = impl_->rrsets_[section].begin();
555 i != impl_->rrsets_[section].end(); ++i) {
556 if (((*i)->getName() == (*iterator)->getName()) &&
557 ((*i)->getClass() == (*iterator)->getClass()) &&
558 ((*i)->getType() == (*iterator)->getType())) {
561 impl_->counts_[section] -= (*iterator)->getRdataCount();
562 impl_->counts_[section] -= (*iterator)->getRRsigDataCount();
563 impl_->rrsets_[section].erase(i);
576 "clearSection performed in non-render mode");
582 impl_->questions_.clear();
584 impl_->rrsets_[section].clear();
586 impl_->counts_[section] = 0;
593 "addQuestion performed in non-render mode");
596 impl_->questions_.push_back(question);
607 impl_->toWire(renderer, tsig_ctx);
614 "Message parse attempted in non parse mode");
617 if (impl_->header_parsed_) {
627 const uint16_t codes_and_flags = buffer.
readUint16();
628 impl_->setOpcode(
Opcode((codes_and_flags & OPCODE_MASK) >> OPCODE_SHIFT));
629 impl_->setRcode(
Rcode(codes_and_flags & RCODE_MASK));
630 impl_->flags_ = (codes_and_flags & HEADERFLAG_MASK);
636 impl_->header_parsed_ =
true;
643 "Message parse attempted in non parse mode");
663 unsigned int added = 0;
665 for (
unsigned int count = 0;
668 const Name name(buffer);
671 2 *
sizeof(uint16_t)) {
695 bool operator()(
const RRsetPtr& rrset)
const {
696 return (rrset->getType() ==
rrtype_ &&
698 rrset->getName() ==
name_);
744 unsigned int added = 0;
746 for (
unsigned int count = 0; count <
counts_[section]; ++count) {
748 const size_t start_position = buffer.
getPosition();
750 const Name name(buffer);
754 3 *
sizeof(uint16_t) +
sizeof(uint32_t)) {
756 " section too short: " <<
771 addRR(section, name, rrclass, rrtype, ttl, options);
778 addEDNS(section, name, rrclass, rrtype, ttl, *rdata);
780 addTSIG(section, count, buffer, start_position, name, rrclass, ttl,
783 addRR(section, name, rrclass, rrtype, ttl, rdata, options);
797 vector<RRsetPtr>::iterator it =
799 MatchRR(name, rrtype, rrclass));
800 if (it !=
rrsets_[section].end()) {
801 (*it)->setTTL(min((*it)->getTTL(), ttl));
802 (*it)->addRdata(rdata);
807 rrset->addRdata(rdata);
808 rrsets_[section].push_back(rrset);
816 vector<RRsetPtr>::iterator it =
818 MatchRR(name, rrtype, rrclass));
819 if (it !=
rrsets_[section].end()) {
820 (*it)->setTTL(min((*it)->getTTL(), ttl));
825 rrsets_[section].push_back(rrset);
834 "EDNS OPT RR found in an invalid section");
840 uint8_t extended_rcode;
853 "TSIG RR found in an invalid section");
855 if (count !=
counts_[section] - 1) {
871struct SectionFormatter {
876 void operator()(
const T& entry) {
894 if (impl_->rcode_ == NULL) {
896 "Message::toText() attempted without Rcode set");
898 if (impl_->opcode_ == NULL) {
900 "Message::toText() attempted without Opcode set");
905 s +=
";; ->>HEADER<<- opcode: " + impl_->opcode_->toText();
907 s +=
", status: " + impl_->rcode_->toText();
908 s +=
", id: " + boost::lexical_cast<string>(impl_->qid_);
937 s +=
", AUTHORITY: " +
941 if (impl_->edns_ != NULL) {
944 if (impl_->tsig_rr_ != NULL) {
947 s +=
", ADDITIONAL: " + lexical_cast<string>(arcount) +
"\n";
949 if (impl_->edns_ != NULL) {
950 s +=
"\n;; OPT PSEUDOSECTION:\n";
951 s += impl_->edns_->toText();
954 if (!impl_->questions_.empty()) {
957 for_each(impl_->questions_.begin(), impl_->questions_.end(),
983 if (impl_->tsig_rr_ != NULL) {
984 s +=
"\n;; TSIG PSEUDOSECTION:\n";
985 s += impl_->tsig_rr_->toText();
1022 "makeResponse() is performed in non-parse mode");
1028 impl_->flags_ &= MESSAGE_REPLYPRESERVE;
1042template <
typename T>
1048 typename vector<T>::const_iterator
it_;
1051template <
typename T>
1056template <
typename T>
1061template <
typename T>
1066template <
typename T>
1069 if (impl_ == source.impl_) {
1078template <
typename T>
1085template <
typename T>
1093template <
typename T>
1096 return (*(impl_->it_));
1099template <
typename T>
1102 return (&(
operator*()));
1105template <
typename T>
1108 return (impl_->it_ == other.impl_->it_);
1111template <
typename T>
1114 return (impl_->it_ != other.impl_->it_);
1134 return (
QuestionIterator(QuestionIteratorImpl(impl_->questions_.begin())));
1152 "RRset iterator is requested for question");
1155 return (
RRsetIterator(RRsetIteratorImpl(impl_->rrsets_[section].begin())));
1165 "RRset iterator is requested for question");
1168 return (
RRsetIterator(RRsetIteratorImpl(impl_->rrsets_[section].end())));
1173 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.
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 ...
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.
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< MessageImpl > MessageImplPtr
Pointer to the MessageImpl object.
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)