Parcourir la source

Add Rdata::getWireLength() method

Mukund Sivaraman il y a 11 ans
Parent
commit
4b77db4d8b
3 fichiers modifiés avec 32 ajouts et 0 suppressions
  1. 9 0
      src/lib/dns/rdata.cc
  2. 15 0
      src/lib/dns/rdata.h
  3. 8 0
      src/lib/dns/tests/rdata_unittest.cc

+ 9 - 0
src/lib/dns/rdata.cc

@@ -46,6 +46,15 @@ namespace isc {
 namespace dns {
 namespace rdata {
 
+uint16_t
+Rdata::getWireLength() const {
+    OutputBuffer obuffer(0);
+
+    toWire(obuffer);
+
+    return (obuffer.getLength());
+}
+
 // XXX: we need to specify std:: for string to help doxygen match the
 // function signature with that given in the header file.
 RdataPtr

+ 15 - 0
src/lib/dns/rdata.h

@@ -221,6 +221,21 @@ public:
     /// \return > 0 if \c this would be sorted after \c other.
     virtual int compare(const Rdata& other) const = 0;
     //@}
+
+    /// \brief Get the wire format length of an Rdata.
+    ///
+    /// IMPLEMENTATION NOTE: Currently this base class implementation is
+    /// non-optimal as it renders the wire data to a buffer and returns
+    /// the buffer's length. What would perform better is to add
+    /// implementations of \c getWireLength() method to every RDATA
+    /// type. This is why this method is virtual. Once all Rdata types
+    /// have \c getWireLength() implementations, this base class
+    /// implementation must be removed and the method should become a
+    /// pure interface.
+    ///
+    /// \return The length of the wire format representation of the
+    /// RDATA.
+    virtual uint16_t getWireLength() const;
 };
 
 namespace generic {

+ 8 - 0
src/lib/dns/tests/rdata_unittest.cc

@@ -211,6 +211,14 @@ TEST_F(RdataTest, createRdataWithLexer) {
                    "file does not end with newline");
 }
 
+TEST_F(RdataTest, getWireLength) {
+    const in::AAAA aaaa_rdata("2001:db8::1");
+    EXPECT_EQ(16, aaaa_rdata.getWireLength());
+
+    const generic::TXT txt_rdata("Hello World");
+    EXPECT_EQ(12, txt_rdata.getWireLength());
+}
+
 }
 }
 }