From: bert hubert Date: Mon, 12 Sep 2016 21:48:41 +0000 (+0200) Subject: make it possible to compare pointers/references to DNSRecordContent. There is a gener... X-Git-Tag: dnsdist-1.1.0-beta2~123^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f18e430ff1b6fa216b08e29205071a60c0cb316a;p=thirdparty%2Fpdns.git make it possible to compare pointers/references to DNSRecordContent. There is a generic comparison function based on getZoneRepresentation(), and specializations for A, AAAA, MX and NS. Rest to follow. Also make DNSPacket::addRecord() use this infrastructure --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 89fae83fc7..ee4ea9126f 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -1136,6 +1136,7 @@ testrunner_SOURCES = \ test-base64_cc.cc \ test-bindparser_cc.cc \ test-delaypipe_hh.cc \ + test-dnsrecordcontent.cc \ test-distributor_hh.cc \ test-dns_random_hh.cc \ test-dnsname_cc.cc \ diff --git a/pdns/dnsparser.hh b/pdns/dnsparser.hh index a3b0771f44..e64985f62b 100644 --- a/pdns/dnsparser.hh +++ b/pdns/dnsparser.hh @@ -192,6 +192,11 @@ public: return record; } + virtual bool operator==(const DNSRecordContent& rhs) const + { + return typeid(*this)==typeid(rhs) && this->getZoneRepresentation() == rhs.getZoneRepresentation(); + } + static shared_ptr unserialize(const DNSName& qname, uint16_t qtype, const string& serialized); void doRecordCheck(const struct DNSRecord&){} @@ -318,13 +323,7 @@ struct DNSRecord if(d_type != rhs.d_type || d_class != rhs.d_class || d_name != rhs.d_name) return false; - string lzrp, rzrp; - if(d_content) - lzrp=toLower(d_content->getZoneRepresentation()); - if(rhs.d_content) - rzrp=toLower(rhs.d_content->getZoneRepresentation()); - - return lzrp == rzrp; + return *d_content == *rhs.d_content; } }; diff --git a/pdns/dnsrecords.hh b/pdns/dnsrecords.hh index a597162fb6..97c66d0052 100644 --- a/pdns/dnsrecords.hh +++ b/pdns/dnsrecords.hh @@ -62,6 +62,12 @@ public: includeboilerplate(A); void doRecordCheck(const DNSRecord& dr); ComboAddress getCA(int port=0) const; + bool operator==(const DNSRecordContent& rhs) const override + { + if(typeid(*this) != typeid(rhs)) + return false; + return d_ip == dynamic_cast(rhs).d_ip; + } private: uint32_t d_ip; }; @@ -73,6 +79,12 @@ public: explicit AAAARecordContent(const ComboAddress& ca); includeboilerplate(AAAA); ComboAddress getCA(int port=0) const; + bool operator==(const DNSRecordContent& rhs) const override + { + if(typeid(*this) != typeid(rhs)) + return false; + return d_ip6 == dynamic_cast(&rhs)->d_ip6; + } private: string d_ip6; // why?? }; @@ -86,6 +98,15 @@ public: uint16_t d_preference; DNSName d_mxname; + + bool operator==(const DNSRecordContent& rhs) const override + { + if(typeid(*this) != typeid(rhs)) + return false; + auto rrhs =dynamic_cast(&rhs); + return std::tie(d_preference, d_mxname) == std::tie(rrhs->d_preference, rrhs->d_mxname); + } + }; class KXRecordContent : public DNSRecordContent @@ -185,7 +206,15 @@ class NSRecordContent : public DNSRecordContent public: includeboilerplate(NS) explicit NSRecordContent(const DNSName& content) : d_content(content){} - const DNSName& getNS() const { return d_content; } + const DNSName& getNS() const { return d_content; } + bool operator==(const DNSRecordContent& rhs) const override + { + if(typeid(*this) != typeid(rhs)) + return false; + auto rrhs =dynamic_cast(&rhs); + return d_content == rrhs->d_content; + } + private: DNSName d_content; }; diff --git a/pdns/test-dnsrecordcontent.cc b/pdns/test-dnsrecordcontent.cc new file mode 100644 index 0000000000..9eb6829b3c --- /dev/null +++ b/pdns/test-dnsrecordcontent.cc @@ -0,0 +1,46 @@ +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_NO_MAIN +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include +#include "dnsrecords.hh" +#include "iputils.hh" + +BOOST_AUTO_TEST_SUITE(test_dnsrecordcontent) + +BOOST_AUTO_TEST_CASE(test_equality) { + reportAllTypes(); + ComboAddress ip("1.2.3.4"), ip2("10.0.0.1"), ip6("::1"); + ARecordContent a1(ip), a2(ip), a3(ip2); + AAAARecordContent aaaa(ip6), aaaa1(ip6); + + BOOST_CHECK(a1==a2); + BOOST_CHECK(!(a1==a3)); + + BOOST_CHECK(aaaa == aaaa1); + + + auto rec1=DNSRecordContent::makeunique(QType::A, 1, "192.168.0.1"); + auto rec2=DNSRecordContent::makeunique(QType::A, 1, "192.168.222.222"); + auto rec3=DNSRecordContent::makeunique(QType::AAAA, 1, "::1"); + auto recMX=DNSRecordContent::makeunique(QType::MX, 1, "25 smtp.powerdns.com"); + auto recMX2=DNSRecordContent::makeunique(QType::MX, 1, "26 smtp.powerdns.com"); + auto recMX3=DNSRecordContent::makeunique(QType::MX, 1, "26 SMTP.powerdns.com"); + BOOST_CHECK(!(*rec1==*rec2)); + BOOST_CHECK(*rec1==*rec1); + BOOST_CHECK(*rec3==*rec3); + + BOOST_CHECK(*recMX==*recMX); + BOOST_CHECK(*recMX2==*recMX3); + BOOST_CHECK(!(*recMX==*recMX3)); + + + BOOST_CHECK(!(*rec1==*rec3)); + + NSRecordContent ns1(DNSName("ns1.powerdns.com")), ns2(DNSName("NS1.powerdns.COM")), ns3(DNSName("powerdns.net")); + BOOST_CHECK(ns1==ns2); + BOOST_CHECK(!(ns1==ns3)); +} + +BOOST_AUTO_TEST_SUITE_END()