]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5680] Add ability to fetch unescaped string from dns::Name
authorThomas Markwalder <tmark@isc.org>
Fri, 20 Jul 2018 15:21:23 +0000 (11:21 -0400)
committerTomek Mrugalski <tomasz@isc.org>
Fri, 27 Jul 2018 11:54:10 +0000 (13:54 +0200)
src/lib/dns/labelsequence.cc
src/lib/dns/labelsequence.h
    Added LabelSequence::toRawText()

src/lib/dns/name.cc
src/lib/dns/name.h
    Added Name::toRawText() and LabelSequence::toRawText()

src/lib/dns/tests/labelsequence_unittest.cc
src/lib/dns/tests/name_unittest.cc
    Added unit tests

src/lib/dns/labelsequence.cc
src/lib/dns/labelsequence.h
src/lib/dns/name.cc
src/lib/dns/name.h
src/lib/dns/tests/labelsequence_unittest.cc
src/lib/dns/tests/name_unittest.cc

index 686efa65bf9f03494db6f3aca605f5fa5d00b58b..1c63717de24c22dac601a8af76fec9ffcabf2424 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2015,2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -279,6 +279,60 @@ LabelSequence::getHash(bool case_sensitive) const {
     return (hash_val);
 }
 
+std::string
+LabelSequence::toRawText(bool omit_final_dot) const {
+    const uint8_t* np = &data_[offsets_[first_label_]];
+    const uint8_t* np_end = np + getDataLength();
+
+    // use for integrity check
+    unsigned int labels = getLabelCount();
+    // init with an impossible value to catch error cases in the end:
+    unsigned int count = Name::MAX_LABELLEN + 1;
+
+    // result string: it will roughly have the same length as the wire format
+    // label sequence data.  reserve that length to minimize reallocation.
+    std::string result;
+    result.reserve(getDataLength());
+
+    while (np != np_end) {
+        labels--;
+        count = *np++;
+
+        if (count == 0) {
+            // We've reached the "final dot".  If we've not dumped any
+            // character, the entire label sequence is the root name.
+            // In that case we don't omit the final dot.
+            if (!omit_final_dot || result.empty()) {
+                result.push_back('.');
+            }
+            break;
+        }
+
+        if (count <= Name::MAX_LABELLEN) {
+            assert(np_end - np >= count);
+
+            if (!result.empty()) {
+                // just after a non-empty label.  add a separating dot.
+                result.push_back('.');
+            }
+
+            while (count-- > 0) {
+                const uint8_t c = *np++;
+                result.push_back(c);
+            }
+        } else {
+            isc_throw(BadLabelType, "unknown label type in name data");
+        }
+    }
+
+    // We should be at the end of the data and have consumed all labels.
+    assert(np == np_end);
+    assert(labels == 0);
+
+    return (result);
+}
+
+
 std::string
 LabelSequence::toText(bool omit_final_dot) const {
     const uint8_t* np = &data_[offsets_[first_label_]];
index be5a1efeb83b10946684ef0ee404e45b4122523d..0b0b2bc4e8bbda1a241936340ba0f5f63fd013b8 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -305,6 +305,16 @@ public:
     /// \return a string representation of the <code>LabelSequence</code>.
     std::string toText() const;
 
+    /// \brief Convert the LabelSequence to a string without escape sequences.
+    ///
+    /// The string returned will contain a single character value for any
+    /// escape sequences in the label(s).
+    ///
+    /// \param omit_final_dot whether to omit the trailing dot in the output.
+    /// \return a string representation of the <code>LabelSequence</code>
+    /// that does not contain escape sequences.
+    std::string toRawText(bool omit_final_dot) const;
+
     /// \brief Extend this LabelSequence with the given labelsequence
     ///
     /// The given labels are appended to the name data, and internal
@@ -351,7 +361,6 @@ private:
     /// \param omit_final_dot whether to omit the trailing dot in the output.
     /// \return a string representation of the <code>LabelSequence</code>.
     std::string toText(bool omit_final_dot) const;
-
 public:
     /// \brief Calculate a simple hash for the label sequence.
     ///
index 94dd25e9b27a378904fe66ee5f10be117778c124..e734fb95562a57f751726797157893514a87e0f2 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -509,6 +509,12 @@ Name::toText(bool omit_final_dot) const {
     return (ls.toText(omit_final_dot));
 }
 
+std::string
+Name::toRawText(bool omit_final_dot) const {
+    LabelSequence ls(*this);
+    return (ls.toRawText(omit_final_dot));
+}
+
 NameComparisonResult
 Name::compare(const Name& other) const {
     const LabelSequence ls1(*this);
index 27a088e287bfab6d0e7605cb556d0a0b0ef11a79..92d25cbb237d062f7e75bf1c9007188f388f6fb1 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -392,6 +392,16 @@ public:
     /// \return a string representation of the <code>Name</code>.
     std::string toText(bool omit_final_dot = false) const;
 
+    /// \brief Convert the LabelSequence to a string without escape sequences.
+    ///
+    /// The string returned will contain a single character value for any
+    /// escape sequences in the label(s).
+    ///
+    /// \param omit_final_dot whether to omit the trailing dot in the output.
+    /// \return a string representation of the <code>LabelSequence</code>
+    /// that does not contain escape sequences.  Default value is false.
+    std::string toRawText(bool omit_final_dot = false) const;
+
     /// \brief Render the <code>Name</code> in the wire format with compression.
     ///
     /// This method dumps the Name in wire format with help of \c renderer,
index 69e2cb8e4e7225576acb5ec1935007c2e1c3f4b3..3cd1330e09e48f227418c9bc5e1101109264769b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -629,6 +629,17 @@ TEST_F(LabelSequenceTest, toText) {
     EXPECT_EQ("0.1", ls_long2.toText());
 }
 
+// The following verifies that toRawText() returns a string
+// actual characters in place of escape sequences.  We do not
+// bother with an exhaustive set of tests here as this is
+// not a primary use case.
+TEST_F(LabelSequenceTest, toRawText) {
+    Name n("a bc.$exa(m)ple.@org");
+    LabelSequence l(n);
+    EXPECT_EQ("a bc.$exa(m)ple.@org", l.toRawText(true));
+    EXPECT_EQ("a bc.$exa(m)ple.@org.", l.toRawText(false));
+}
+
 // The following are test data used in the getHash test below.  Normally
 // we use example/documentation domain names for testing, but in this case
 // we'd specifically like to use more realistic data, and are intentionally
index d6ef1b2782b06a2a46ad7ca8228ef89c1e6d4760..0380e59e3e07a6c55340bdba5b618df5203365f1 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -752,4 +752,17 @@ TEST_F(NameTest, LeftShiftOperator) {
     oss << example_name;
     EXPECT_EQ(example_name.toText(), oss.str());
 }
+
+// The following verifies that toRawText() returns a string
+// actual characters in place of escape sequences.  We do not
+// bother with an exhaustive set of tests here as this is
+// not a primary use case.
+TEST_F(NameTest, toRawText) {
+    Name n("a bc.$exa(m)ple.@org");
+    EXPECT_EQ("a bc.$exa(m)ple.@org", n.toRawText(true));
+    EXPECT_EQ("a bc.$exa(m)ple.@org.", n.toRawText(false));
+    // Verify default value of omit parameter is false.
+    EXPECT_EQ("a bc.$exa(m)ple.@org.", n.toRawText());
+}
+
 }