From 080d63ba53db3a834b3943d1ced0584dfb9095b0 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Fri, 25 Oct 2024 11:33:17 +0200 Subject: [PATCH] Refactor serialize/wireFormatContent as suggested by @rgacogne --- pdns/dnsparser.hh | 28 +++++++--------------------- pdns/dnswriter.cc | 4 ++-- pdns/dnswriter.hh | 2 +- pdns/shuffle.cc | 2 +- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/pdns/dnsparser.hh b/pdns/dnsparser.hh index 757caf6da36b5..5f59ded73183d 100644 --- a/pdns/dnsparser.hh +++ b/pdns/dnsparser.hh @@ -200,26 +200,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); @@ -235,7 +217,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 1cdc5776beb24..96a66a1a8bbe5 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 c6ed4b3f04fd4..adccd8365b6ee 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 fae7a4a493ddd..c9987b5633424 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;