Browse Source

[2656] Update std::string SRV constructor to use the MasterLexer

Mukund Sivaraman 12 years ago
parent
commit
17a9c25450
2 changed files with 31 additions and 14 deletions
  1. 30 13
      src/lib/dns/rdata/in_1/srv_33.cc
  2. 1 1
      src/lib/dns/tests/rdata_srv_unittest.cc

+ 30 - 13
src/lib/dns/rdata/in_1/srv_33.cc

@@ -73,23 +73,40 @@ struct SRVImpl {
 SRV::SRV(const std::string& srv_str) :
     impl_(NULL)
 {
-    istringstream iss(srv_str);
-
     try {
-        const int32_t priority = tokenToNum<int32_t, 16>(getToken(iss));
-        const int32_t weight = tokenToNum<int32_t, 16>(getToken(iss));
-        const int32_t port = tokenToNum<int32_t, 16>(getToken(iss));
-        const Name targetname(getToken(iss));
-
-        if (!iss.eof()) {
-            isc_throw(InvalidRdataText, "Unexpected input for SRV RDATA: " <<
-                    srv_str);
+        std::istringstream ss(srv_str);
+        MasterLexer lexer;
+        lexer.pushSource(ss);
+
+        uint32_t num = lexer.getNextToken(MasterToken::NUMBER).getNumber();
+        if (num > 65535) {
+            isc_throw(InvalidRdataText, "Invalid SRV priority");
+        }
+        const uint16_t priority = static_cast<uint16_t>(num);
+
+        num = lexer.getNextToken(MasterToken::NUMBER).getNumber();
+        if (num > 65535) {
+            isc_throw(InvalidRdataText, "Invalid SRV weight");
+        }
+        const uint16_t weight = static_cast<uint16_t>(num);
+
+        num = lexer.getNextToken(MasterToken::NUMBER).getNumber();
+        if (num > 65535) {
+            isc_throw(InvalidRdataText, "Invalid SRV port");
+        }
+        const uint16_t port = static_cast<uint16_t>(num);
+
+        const Name targetname = createNameFromLexer(lexer, NULL);
+
+        if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
+            isc_throw(InvalidRdataText, "extra input text for SRV: "
+                      << srv_str);
         }
 
         impl_ = new SRVImpl(priority, weight, port, targetname);
-    } catch (const StringTokenError& ste) {
-        isc_throw(InvalidRdataText, "Invalid SRV text: " <<
-                  ste.what() << ": " << srv_str);
+    } catch (const MasterLexer::LexerError& ex) {
+        isc_throw(InvalidRdataText, "Failed to construct SRV from '" <<
+                  srv_str << "': " << ex.what());
     }
 }
 

+ 1 - 1
src/lib/dns/tests/rdata_srv_unittest.cc

@@ -39,7 +39,7 @@ class Rdata_SRV_Test : public RdataTest {
 string srv_txt("1 5 1500 a.example.com.");
 string srv_txt2("1 5 1400 example.com.");
 string too_long_label("012345678901234567890123456789"
-    "0123456789012345678901234567890123");
+    "0123456789012345678901234567890123.");
 
 // 1 5 1500 a.example.com.
 const uint8_t wiredata_srv[] = {