Browse Source

introduced Message::parseHeader() so that we can perform minimal level
sanity check on incoming messages first. Message::fromWire() internall
calls parseHeader().


git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@1350 e5f2f494-b856-4b98-b285-d166d9295462

JINMEI Tatuya 15 years ago
parent
commit
c64dbdb7b7
2 changed files with 25 additions and 3 deletions
  1. 24 3
      src/lib/dns/message.cc
  2. 1 0
      src/lib/dns/message.h

+ 24 - 3
src/lib/dns/message.cc

@@ -201,6 +201,7 @@ public:
     flags_t flags_;
     bool dnssec_ok_;
 
+    bool header_parsed_;
     static const unsigned int SECTION_MAX = 4; // TODO: revisit this design
     int counts_[SECTION_MAX];   // TODO: revisit this definition
     vector<QuestionPtr> questions_;
@@ -243,6 +244,7 @@ MessageImpl::init()
         counts_[i] = 0;
     }
 
+    header_parsed_ = false;
     questions_.clear();
     rrsets_[sectionCodeToId(Section::ANSWER())].clear();
     rrsets_[sectionCodeToId(Section::AUTHORITY())].clear();
@@ -524,10 +526,15 @@ Message::toWire(MessageRenderer& renderer)
 }
 
 void
-Message::fromWire(InputBuffer& buffer)
-{
+Message::parseHeader(InputBuffer& buffer) {
+    if (impl_->mode_ != Message::PARSE) {
+        isc_throw(InvalidMessageOperation,
+                  "Message parse attempted in non parse mode");
+    }
+
     if ((buffer.getLength() - buffer.getPosition()) < HEADERLEN) {
-        isc_throw(MessageTooShort, "");
+        isc_throw(MessageTooShort, "Malformed DNS message (short length): "
+                  << buffer.getLength() - buffer.getPosition());
     }
 
     impl_->qid_ = buffer.readUint16();
@@ -540,6 +547,20 @@ Message::fromWire(InputBuffer& buffer)
     impl_->counts_[Section::AUTHORITY().getCode()] = buffer.readUint16();
     impl_->counts_[Section::ADDITIONAL().getCode()] = buffer.readUint16();
 
+    impl_->header_parsed_ = true;
+}
+
+void
+Message::fromWire(InputBuffer& buffer) {
+    if (impl_->mode_ != Message::PARSE) {
+        isc_throw(InvalidMessageOperation,
+                  "Message parse attempted in non parse mode");
+    }
+
+    if (!impl_->header_parsed_) {
+        parseHeader(buffer);
+    }
+
     impl_->counts_[Section::QUESTION().getCode()] =
         impl_->parseQuestion(buffer);
     impl_->counts_[Section::ANSWER().getCode()] =

+ 1 - 0
src/lib/dns/message.h

@@ -557,6 +557,7 @@ public:
     void toWire(MessageRenderer& renderer);
 
     /// \brief Parse a DNS message.
+    void parseHeader(InputBuffer& buffer);
     void fromWire(InputBuffer& buffer);
 
     ///