Browse Source

[2387] Add NSEC3 string RDATA constructor

Mukund Sivaraman 12 years ago
parent
commit
8a91e6ec6c
2 changed files with 23 additions and 34 deletions
  1. 20 34
      src/lib/dns/rdata/generic/nsec3_50.cc
  2. 3 0
      src/lib/dns/rdata/generic/nsec3_50.h

+ 20 - 34
src/lib/dns/rdata/generic/nsec3_50.cc

@@ -67,47 +67,33 @@ struct NSEC3Impl {
 NSEC3::NSEC3(const std::string& nsec3_str) :
     impl_(NULL)
 {
-    istringstream iss(nsec3_str);
-    vector<uint8_t> salt;
-    const ParseNSEC3ParamResult params =
-        parseNSEC3ParamText("NSEC3", nsec3_str, iss, salt);
-
-    // Extract Next hash.  It must be an unpadded base32hex string.
-    string nexthash;
-    iss >> nexthash;
-    if (iss.bad() || iss.fail()) {
-        isc_throw(InvalidRdataText, "Invalid NSEC3 text: " << nsec3_str);
-    }
-    assert(!nexthash.empty());
-    if (*nexthash.rbegin() == '=') {
-        isc_throw(InvalidRdataText, "NSEC3 hash has padding: " << nsec3_str);
-    }
-    vector<uint8_t> next;
-    decodeBase32Hex(nexthash, next);
-    if (next.size() > 255) {
-        isc_throw(InvalidRdataText, "NSEC3 hash is too long: "
-                  << next.size() << " bytes");
-    }
-
-    // For NSEC3 empty bitmap is possible and allowed.
-    if (iss.eof()) {
-        impl_ = new NSEC3Impl(params.algorithm, params.flags,
-                              params.iterations, salt, next,
-                              vector<uint8_t>());
-        return;
+    try {
+        std::istringstream ss(nsec3_str);
+        MasterLexer lexer;
+        lexer.pushSource(ss);
+
+        constructFromLexer(lexer);
+
+        if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
+            isc_throw(InvalidRdataText,
+                      "Extra input text for NSEC3: " << nsec3_str);
+        }
+    } catch (const MasterLexer::LexerError& ex) {
+        isc_throw(InvalidRdataText,
+                  "Failed to construct NSEC3 from '" << nsec3_str << "': "
+                  << ex.what());
     }
-
-    vector<uint8_t> typebits;
-    buildBitmapsFromText("NSEC3", iss, typebits);
-
-    impl_ = new NSEC3Impl(params.algorithm, params.flags, params.iterations,
-                          salt, next, typebits);
 }
 
 NSEC3::NSEC3(MasterLexer& lexer, const Name*, MasterLoader::Options,
              MasterLoaderCallbacks&) :
     impl_(NULL)
 {
+    constructFromLexer(lexer);
+}
+
+void
+NSEC3::constructFromLexer(MasterLexer& lexer) {
     vector<uint8_t> salt;
     const ParseNSEC3ParamResult params =
         parseNSEC3ParamFromLexer("NSEC3", lexer, salt);

+ 3 - 0
src/lib/dns/rdata/generic/nsec3_50.h

@@ -21,6 +21,7 @@
 #include <dns/rrtype.h>
 #include <dns/rrttl.h>
 #include <dns/rdata.h>
+#include <dns/master_lexer.h>
 
 // BEGIN_HEADER_GUARD
 
@@ -47,6 +48,8 @@ public:
     const std::vector<uint8_t>& getNext() const;
 
 private:
+    void constructFromLexer(isc::dns::MasterLexer& lexer);
+
     NSEC3Impl* impl_;
 };