Browse Source

[trac1140] txt and spf rr types derived from common template base

Dima Volodin 14 years ago
parent
commit
21850ab947

+ 168 - 0
src/lib/dns/rdata/generic/detail/txt_like.h

@@ -0,0 +1,168 @@
+// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __TXT_LIKE_H
+#define __TXT_LIKE_H 1
+
+#include <stdint.h>
+
+#include <string>
+#include <vector>
+
+using namespace std;
+using namespace isc::util;
+
+template<class RTYPE, uint16_t typeCode>class TXT_LIKE : public Rdata {
+public:
+    TXT_LIKE(InputBuffer& buffer, size_t rdata_len) {
+	if (rdata_len > MAX_RDLENGTH) {
+	    isc_throw(InvalidRdataLength, "RDLENGTH too large: " << rdata_len);
+	}
+
+	if (rdata_len == 0) {       // note that this couldn't happen in the loop.
+	    isc_throw(DNSMessageFORMERR,
+		      "Error in parsing " + RRParamRegistry::getRegistry().codeToTypeText(typeCode)
+		     + " RDATA: 0-length character string");
+	}
+
+	do {
+	    const uint8_t len = buffer.readUint8();
+	    if (rdata_len < len + 1) {
+		isc_throw(DNSMessageFORMERR,
+			  "Error in parsing " + RRParamRegistry::getRegistry().codeToTypeText(typeCode)
+			 + " RDATA: character string length is too large: " << static_cast<int>(len));
+	    }
+	    vector<uint8_t> data(len + 1);
+	    data[0] = len;
+	    buffer.readData(&data[0] + 1, len);
+	    string_list_.push_back(data);
+
+	    rdata_len -= (len + 1);
+	} while (rdata_len > 0);
+    }
+
+    explicit TXT_LIKE(const std::string& txtstr) {
+	// TBD: this is a simple, incomplete implementation that only supports
+	// a single character-string.
+
+	size_t length = txtstr.size();
+	size_t pos_begin = 0;
+
+	if (length > 1 && txtstr[0] == '"' && txtstr[length - 1] == '"') {
+	    pos_begin = 1;
+	    length -= 2;
+	}
+
+	if (length > MAX_CHARSTRING_LEN) {
+	    isc_throw(CharStringTooLong, RRParamRegistry::getRegistry().codeToTypeText(typeCode)
+			 + " RDATA construction from text: string length is too long: " << length);
+	}
+
+	// TBD: right now, we don't support escaped characters
+	if (txtstr.find('\\') != string::npos) {
+	    isc_throw(InvalidRdataText, RRParamRegistry::getRegistry().codeToTypeText(typeCode)
+			+ " RDATA from text: escaped character is currently not supported: " << txtstr);
+	}
+
+	vector<uint8_t> data;
+	data.reserve(length + 1);
+	data.push_back(length);
+	data.insert(data.end(), txtstr.begin() + pos_begin,
+		    txtstr.begin() + pos_begin + length);
+	string_list_.push_back(data);
+    }
+
+    TXT_LIKE(const RTYPE& other) :
+	Rdata(), string_list_(other.string_list_)
+    {}
+
+    void
+    toWire(OutputBuffer& buffer) const {
+	for (vector<vector<uint8_t> >::const_iterator it = string_list_.begin();
+	     it != string_list_.end();
+	     ++it)
+	{
+	    buffer.writeData(&(*it)[0], (*it).size());
+	}
+    }
+
+    void
+    toWire(AbstractMessageRenderer& renderer) const {
+	for (vector<vector<uint8_t> >::const_iterator it = string_list_.begin();
+	     it != string_list_.end();
+	     ++it)
+	{
+	    renderer.writeData(&(*it)[0], (*it).size());
+	}
+    }
+
+    string
+    toText() const {
+	string s;
+
+	// XXX: this implementation is not entirely correct.  for example, it
+	// should escape double-quotes if they appear in the character string.
+	for (vector<vector<uint8_t> >::const_iterator it = string_list_.begin();
+	     it != string_list_.end();
+	     ++it)
+	{
+	    if (!s.empty()) {
+		s.push_back(' ');
+	    }
+	    s.push_back('"');
+	    s.insert(s.end(), (*it).begin() + 1, (*it).end());
+	    s.push_back('"');
+	}
+
+	return (s);
+    }
+
+    int
+    compare(const Rdata& other) const {
+	const RTYPE& other_txt = dynamic_cast<const RTYPE&>(other);
+
+	// This implementation is not efficient.  Revisit this (TBD).
+	OutputBuffer this_buffer(0);
+	toWire(this_buffer);
+	size_t this_len = this_buffer.getLength();
+
+	OutputBuffer other_buffer(0);
+	other_txt.toWire(other_buffer);
+	const size_t other_len = other_buffer.getLength();
+
+	const size_t cmplen = min(this_len, other_len);
+	const int cmp = memcmp(this_buffer.getData(), other_buffer.getData(),
+			       cmplen);
+	if (cmp != 0) {
+	    return (cmp);
+	} else {
+	    return ((this_len == other_len) ? 0 :
+		    (this_len < other_len) ? -1 : 1);
+	}
+    }
+
+private:
+    /// Note: this is a prototype version; we may reconsider
+    /// this representation later.
+    std::vector<std::vector<uint8_t> > string_list_;
+};
+
+// END_RDATA_NAMESPACE
+// END_ISC_NAMESPACE
+
+#endif //  __TXT_LIKE_H
+
+// Local Variables: 
+// mode: c++
+// End: 

+ 19 - 0
src/lib/dns/rdata/generic/spf_99.cc

@@ -0,0 +1,19 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// BEGIN_ISC_NAMESPACE
+// BEGIN_RDATA_NAMESPACE
+
+// END_RDATA_NAMESPACE
+// END_ISC_NAMESPACE

+ 85 - 0
src/lib/dns/rdata/generic/spf_99.h

@@ -0,0 +1,85 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// BEGIN_HEADER_GUARD
+
+#include <string>
+
+#include <dns/name.h>
+#include <dns/rdata.h>
+
+#include <util/buffer.h>
+#include <dns/exceptions.h>
+#include <dns/messagerenderer.h>
+#include <dns/rrparamregistry.h>
+
+// BEGIN_ISC_NAMESPACE
+
+// BEGIN_COMMON_DECLARATIONS
+// END_COMMON_DECLARATIONS
+
+// BEGIN_RDATA_NAMESPACE
+
+#include <dns/rdata/generic/detail/txt_like.h>
+
+class SPF : public TXT_LIKE<SPF, 99> {
+    friend class TXT_LIKE<SPF, 99>;
+    static string const id;
+
+public:
+    // BEGIN_COMMON_MEMBERS
+    // END_COMMON_MEMBERS
+
+};
+
+///    explicit SPF(const std::string& type_str);
+inline SPF::SPF(const std::string& type_str) : TXT_LIKE<SPF, 99>(type_str) {}
+
+///    SPF(isc::util::InputBuffer& buffer, size_t rdata_len);
+inline SPF::SPF(isc::util::InputBuffer& buffer, size_t rdata_len) : TXT_LIKE<SPF, 99>(buffer, rdata_len) {}
+
+///    SPF(const SPF& other);
+inline SPF::SPF(const SPF& other) : TXT_LIKE<SPF, 99>(other) {}
+
+///    virtual std::string toText() const;
+inline std::string SPF::toText() const
+{
+    return TXT_LIKE<SPF, 99>::toText();
+}
+
+///    virtual void toWire(isc::util::OutputBuffer& buffer) const;
+inline void SPF::toWire(isc::util::OutputBuffer& buffer) const
+{
+    TXT_LIKE<SPF, 99>::toWire(buffer);
+}
+
+///    virtual void toWire(AbstractMessageRenderer& renderer) const;
+inline void SPF::toWire(AbstractMessageRenderer& renderer) const
+{
+    TXT_LIKE<SPF, 99>::toWire(renderer);
+}
+
+///    virtual int compare(const Rdata& other) const;
+inline int SPF::compare(const Rdata& other) const
+{
+    return TXT_LIKE<SPF, 99>::compare(other);
+}
+
+// END_RDATA_NAMESPACE
+// END_ISC_NAMESPACE
+// END_HEADER_GUARD
+
+// Local Variables: 
+// mode: c++
+// End: 

+ 0 - 141
src/lib/dns/rdata/generic/txt_16.cc

@@ -12,149 +12,8 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
-#include <stdint.h>
-#include <string.h>
-
-#include <string>
-#include <vector>
-
-#include <util/buffer.h>
-#include <dns/exceptions.h>
-#include <dns/messagerenderer.h>
-#include <dns/rdata.h>
-#include <dns/rdataclass.h>
-
-using namespace std;
-using namespace isc::util;
-
 // BEGIN_ISC_NAMESPACE
 // BEGIN_RDATA_NAMESPACE
 
-TXT::TXT(InputBuffer& buffer, size_t rdata_len) {
-    if (rdata_len > MAX_RDLENGTH) {
-        isc_throw(InvalidRdataLength, "RDLENGTH too large: " << rdata_len);
-    }
-
-    if (rdata_len == 0) {       // note that this couldn't happen in the loop.
-        isc_throw(DNSMessageFORMERR,
-                  "Error in parsing TXT RDATA: 0-length character string");
-    }
-
-    do {
-        const uint8_t len = buffer.readUint8();
-        if (rdata_len < len + 1) {
-            isc_throw(DNSMessageFORMERR,
-                      "Error in parsing TXT RDATA: character string length "
-                      "is too large: " << static_cast<int>(len));
-        }
-        vector<uint8_t> data(len + 1);
-        data[0] = len;
-        buffer.readData(&data[0] + 1, len);
-        string_list_.push_back(data);
-
-        rdata_len -= (len + 1);
-    } while (rdata_len > 0);
-}
-
-TXT::TXT(const std::string& txtstr) {
-    // TBD: this is a simple, incomplete implementation that only supports
-    // a single character-string.
-
-    size_t length = txtstr.size();
-    size_t pos_begin = 0;
-
-    if (length > 1 && txtstr[0] == '"' && txtstr[length - 1] == '"') {
-        pos_begin = 1;
-        length -= 2;
-    }
-
-    if (length > MAX_CHARSTRING_LEN) {
-        isc_throw(CharStringTooLong, "TXT RDATA construction from text: "
-                  "string length is too long: " << length);
-    }
-
-    // TBD: right now, we don't support escaped characters
-    if (txtstr.find('\\') != string::npos) {
-        isc_throw(InvalidRdataText, "TXT RDATA from text: "
-                  "escaped character is currently not supported: " << txtstr);
-    }
-
-    vector<uint8_t> data;
-    data.reserve(length + 1);
-    data.push_back(length);
-    data.insert(data.end(), txtstr.begin() + pos_begin,
-                txtstr.begin() + pos_begin + length);
-    string_list_.push_back(data);
-}
-
-TXT::TXT(const TXT& other) :
-    Rdata(), string_list_(other.string_list_)
-{}
-
-void
-TXT::toWire(OutputBuffer& buffer) const {
-    for (vector<vector<uint8_t> >::const_iterator it = string_list_.begin();
-         it != string_list_.end();
-         ++it)
-    {
-        buffer.writeData(&(*it)[0], (*it).size());
-    }
-}
-
-void
-TXT::toWire(AbstractMessageRenderer& renderer) const {
-    for (vector<vector<uint8_t> >::const_iterator it = string_list_.begin();
-         it != string_list_.end();
-         ++it)
-    {
-        renderer.writeData(&(*it)[0], (*it).size());
-    }
-}
-
-string
-TXT::toText() const {
-    string s;
-
-    // XXX: this implementation is not entirely correct.  for example, it
-    // should escape double-quotes if they appear in the character string.
-    for (vector<vector<uint8_t> >::const_iterator it = string_list_.begin();
-         it != string_list_.end();
-         ++it)
-    {
-        if (!s.empty()) {
-            s.push_back(' ');
-        }
-        s.push_back('"');
-        s.insert(s.end(), (*it).begin() + 1, (*it).end());
-        s.push_back('"');
-    }
-
-    return (s);
-}
-
-int
-TXT::compare(const Rdata& other) const {
-    const TXT& other_txt = dynamic_cast<const TXT&>(other);
-
-    // This implementation is not efficient.  Revisit this (TBD).
-    OutputBuffer this_buffer(0);
-    toWire(this_buffer);
-    size_t this_len = this_buffer.getLength();
-
-    OutputBuffer other_buffer(0);
-    other_txt.toWire(other_buffer);
-    const size_t other_len = other_buffer.getLength();
-
-    const size_t cmplen = min(this_len, other_len);
-    const int cmp = memcmp(this_buffer.getData(), other_buffer.getData(),
-                           cmplen);
-    if (cmp != 0) {
-        return (cmp);
-    } else {
-        return ((this_len == other_len) ? 0 :
-                (this_len < other_len) ? -1 : 1);
-    }
-}
-
 // END_RDATA_NAMESPACE
 // END_ISC_NAMESPACE

+ 46 - 8
src/lib/dns/rdata/generic/txt_16.h

@@ -14,13 +14,16 @@
 
 // BEGIN_HEADER_GUARD
 
-#include <stdint.h>
-
 #include <string>
-#include <vector>
 
+#include <dns/name.h>
 #include <dns/rdata.h>
 
+#include <util/buffer.h>
+#include <dns/exceptions.h>
+#include <dns/messagerenderer.h>
+#include <dns/rrparamregistry.h>
+
 // BEGIN_ISC_NAMESPACE
 
 // BEGIN_COMMON_DECLARATIONS
@@ -28,16 +31,51 @@
 
 // BEGIN_RDATA_NAMESPACE
 
-class TXT : public Rdata {
+#include <dns/rdata/generic/detail/txt_like.h>
+
+class TXT : public TXT_LIKE<TXT, 16> {
+    friend class TXT_LIKE<TXT, 16>;
+    static string const id;
+
 public:
     // BEGIN_COMMON_MEMBERS
     // END_COMMON_MEMBERS
-private:
-    /// Note: this is a prototype version; we may reconsider
-    /// this representation later.
-    std::vector<std::vector<uint8_t> > string_list_;
+
 };
 
+///    explicit TXT(const std::string& type_str);
+inline TXT::TXT(const std::string& type_str) : TXT_LIKE<TXT, 16>(type_str) {}
+
+///    TXT(isc::util::InputBuffer& buffer, size_t rdata_len);
+inline TXT::TXT(isc::util::InputBuffer& buffer, size_t rdata_len) : TXT_LIKE<TXT, 16>(buffer, rdata_len) {}
+
+///    TXT(const TXT& other);
+inline TXT::TXT(const TXT& other) : TXT_LIKE<TXT, 16>(other) {}
+
+///    virtual std::string toText() const;
+inline std::string TXT::toText() const
+{
+    return TXT_LIKE<TXT, 16>::toText();
+}
+
+///    virtual void toWire(isc::util::OutputBuffer& buffer) const;
+inline void TXT::toWire(isc::util::OutputBuffer& buffer) const
+{
+    TXT_LIKE<TXT, 16>::toWire(buffer);
+}
+
+///    virtual void toWire(AbstractMessageRenderer& renderer) const;
+inline void TXT::toWire(AbstractMessageRenderer& renderer) const
+{
+    TXT_LIKE<TXT, 16>::toWire(renderer);
+}
+
+///    virtual int compare(const Rdata& other) const;
+inline int TXT::compare(const Rdata& other) const
+{
+    return TXT_LIKE<TXT, 16>::compare(other);
+}
+
 // END_RDATA_NAMESPACE
 // END_ISC_NAMESPACE
 // END_HEADER_GUARD