Skip to content

Commit

Permalink
Refactor serialize/wireFormatContent as suggested by @rgacogne
Browse files Browse the repository at this point in the history
  • Loading branch information
omoerbeek committed Oct 25, 2024
1 parent 2e9888c commit 080d63b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 25 deletions.
28 changes: 7 additions & 21 deletions pdns/dnsparser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> 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<uint8_t> packet;
DNSPacketWriter packetWriter(packet, g_rootdnsname, QType::A);
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions pdns/dnswriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@ template <typename Container> void GenericDNSPacketWriter<Container>::getRecordP
}

// call __before commit__
template <typename Container> void GenericDNSPacketWriter<Container>::getContentWireFormat(string& records)
template <typename Container> void GenericDNSPacketWriter<Container>::getWireFormatContent(string& record)
{
records.assign(d_content.begin() + d_rollbackmarker, d_content.end());
record.assign(d_content.begin() + d_rollbackmarker, d_content.end());
}

template <typename Container> uint32_t GenericDNSPacketWriter<Container>::size() const
Expand Down
2 changes: 1 addition & 1 deletion pdns/dnswriter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion pdns/shuffle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ unsigned int pdns::dedupRecords(vector<DNSRecord>& 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;
Expand Down

0 comments on commit 080d63b

Please sign in to comment.