From: Otto Moerbeek Date: Fri, 25 Oct 2024 09:33:17 +0000 (+0200) Subject: Refactor serialize/wireFormatContent as suggested by @rgacogne X-Git-Tag: dnsdist-2.0.0-alpha1~182^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f3d1ab14413e604740885688d283c0b326609f09;p=thirdparty%2Fpdns.git Refactor serialize/wireFormatContent as suggested by @rgacogne --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index dcdfada549..a5e9cbfe70 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -949,8 +949,8 @@ speedtest_SOURCES = \ nsecrecords.cc \ qtype.cc \ rcpgenerator.cc rcpgenerator.hh \ - sillyrecords.cc \ shuffle.cc shuffle.hh \ + sillyrecords.cc \ speedtest.cc \ statbag.cc \ svc-records.cc svc-records.hh \ diff --git a/pdns/dnsparser.hh b/pdns/dnsparser.hh index 5d9889e20d..bc818b9100 100644 --- a/pdns/dnsparser.hh +++ b/pdns/dnsparser.hh @@ -201,26 +201,8 @@ public: virtual std::string getZoneRepresentation(bool noDot=false) const = 0; virtual ~DNSRecordContent() = default; virtual void toPacket(DNSPacketWriter& pw) const = 0; - // returns the wire format of the content, possibly including compressed pointers pointing to the owner name (unless canonic or lowerCase are set) - string serialize(const DNSName& qname, bool canonic=false, bool lowerCase=false) const - { - vector packet; - DNSPacketWriter pw(packet, g_rootdnsname, 1); - if(canonic) - pw.setCanonic(true); - - if(lowerCase) - pw.setLowercase(true); - - pw.startRecord(qname, this->getType()); - this->toPacket(pw); - - string record; - pw.getRecordPayload(record); // needs to be called before commit() - return record; - } - - [[nodiscard]] string wireFormatContent(const DNSName& qname, bool canonic = false, bool lowerCase = false) const + // returns the wire format of the content or the full record, possibly including compressed pointers pointing to the owner name (unless canonic or lowerCase are set) + [[nodiscard]] string serialize(const DNSName& qname, bool canonic = false, bool lowerCase = false, bool full = false) const { vector packet; DNSPacketWriter packetWriter(packet, g_rootdnsname, QType::A); @@ -236,7 +218,11 @@ public: toPacket(packetWriter); string record; - packetWriter.getContentWireFormat(record); // needs to be called before commit() + if (full) { + packetWriter.getWireFormatContent(record); // needs to be called before commit() + } else { + packetWriter.getRecordPayload(record); // needs to be called before commit() + } return record; } diff --git a/pdns/dnswriter.cc b/pdns/dnswriter.cc index 1cdc5776be..96a66a1a8b 100644 --- a/pdns/dnswriter.cc +++ b/pdns/dnswriter.cc @@ -458,9 +458,9 @@ template void GenericDNSPacketWriter::getRecordP } // call __before commit__ -template void GenericDNSPacketWriter::getContentWireFormat(string& records) +template void GenericDNSPacketWriter::getWireFormatContent(string& record) { - records.assign(d_content.begin() + d_rollbackmarker, d_content.end()); + record.assign(d_content.begin() + d_rollbackmarker, d_content.end()); } template uint32_t GenericDNSPacketWriter::size() const diff --git a/pdns/dnswriter.hh b/pdns/dnswriter.hh index c6ed4b3f04..adccd8365b 100644 --- a/pdns/dnswriter.hh +++ b/pdns/dnswriter.hh @@ -138,7 +138,7 @@ public: dnsheader* getHeader(); void getRecordPayload(string& records); // call __before commit__ - void getContentWireFormat(string& records); // call __before commit__ + void getWireFormatContent(string& record); // call __before commit__ void setCanonic(bool val) { diff --git a/pdns/shuffle.cc b/pdns/shuffle.cc index fae7a4a493..c9987b5633 100644 --- a/pdns/shuffle.cc +++ b/pdns/shuffle.cc @@ -159,7 +159,7 @@ unsigned int pdns::dedupRecords(vector& rrs) seen.reserve(rrs.size()); for (const auto& rec : rrs) { - auto key = rec.getContent()->wireFormatContent(rec.d_name, true, true); + auto key = rec.getContent()->serialize(rec.d_name, true, true, true); // This ignores class, ttl and place by using constants for those if (!seen.emplace(std::move(key)).second) { dups[counter] = true;