From: Miod Vallat Date: Fri, 25 Jul 2025 07:05:25 +0000 (+0200) Subject: Simplify some DNSName label processing. X-Git-Tag: auth-5.1.0-alpha0^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=65105e6555db1f8131ec5fc33d4a4c44ca1c94d2;p=thirdparty%2Fpdns.git Simplify some DNSName label processing. Instead of checking countLabels() against zero, introduce a faster hasLabels() which does not need to actually count them. Also replace getRawLabels()[n] with getRawLabel(n), the only difference being that getRawLabel() will raise an exception if n is out of bounds, instead of returning garbage. Signed-off-by: Miod Vallat --- diff --git a/pdns/dnsname.hh b/pdns/dnsname.hh index 4949d0465..a20c0f1d0 100644 --- a/pdns/dnsname.hh +++ b/pdns/dnsname.hh @@ -152,6 +152,7 @@ public: size_t wirelength() const; //!< Number of total bytes in the name bool empty() const { return d_storage.empty(); } bool isRoot() const { return d_storage.size()==1 && d_storage[0]==0; } + bool hasLabels() const { return !empty() && !isRoot(); } void clear() { d_storage.clear(); } void trimToLabels(unsigned int); size_t hash(size_t init=0) const diff --git a/pdns/dnssecsigner.cc b/pdns/dnssecsigner.cc index 9b93a4135..880c1b0b2 100644 --- a/pdns/dnssecsigner.cc +++ b/pdns/dnssecsigner.cc @@ -161,7 +161,7 @@ static void addSignature(DNSSECKeeper& dsk, UeberBackend& ueber, const ZoneName& dsk.getPreRRSIGs(ueber, outsigned, origTTL, packet); // does it all } else { - if(getRRSIGsForRRSET(dsk, signer, wildcardname.countLabels() != 0 ? wildcardname : signQName, signQType, signTTL, toSign, rrcs) < 0) { + if(getRRSIGsForRRSET(dsk, signer, wildcardname.hasLabels() ? wildcardname : signQName, signQType, signTTL, toSign, rrcs) < 0) { // cerr<<"Error signing a record!"<& bool PacketHandler::tryAuthSignal(DNSPacket& p, std::unique_ptr& r, DNSName &target) // NOLINT(readability-identifier-length) { DLOG(g_log<& r, D } // Check for prefix mismatch - if(target.countLabels() == 0 || !pdns_iequals(target.getRawLabel(0), "_dsboot")) { + if(!target.hasLabels() || !pdns_iequals(target.getRawLabel(0), "_dsboot")) { makeNOError(p, r, target, DNSName(), 0); // could be ENT return true; } diff --git a/pdns/pdnsutil.cc b/pdns/pdnsutil.cc index 1f4517c9c..3f4f9c552 100644 --- a/pdns/pdnsutil.cc +++ b/pdns/pdnsutil.cc @@ -1107,7 +1107,7 @@ static int checkZone(DNSSECKeeper &dk, UeberBackend &B, const ZoneName& zone, co continue; } - if (isSecure && isOptOut && (rr.qname.countLabels() != 0 && rr.qname.getRawLabels()[0] == "*")) { + if (isSecure && isOptOut && (rr.qname.hasLabels() && rr.qname.getRawLabel(0) == "*")) { cout<<"[Warning] wildcard record '"<& cmds, const std::string_view synopsi ZoneName zone(cmds.at(0)); - if(zone.operator const DNSName&().countLabels() == 0 || !pdns_iequals(zone.operator const DNSName&().getRawLabel(0), "_signal")) { + if(!zone.operator const DNSName&().hasLabels() || !pdns_iequals(zone.operator const DNSName&().getRawLabel(0), "_signal")) { cerr << "Signaling zone's first label must be '_signal': " << zone << endl; return 1; } diff --git a/pdns/validate.cc b/pdns/validate.cc index b36f7e6f0..6f7aa17b2 100644 --- a/pdns/validate.cc +++ b/pdns/validate.cc @@ -224,7 +224,7 @@ bool denialProvesNoDelegation(const DNSName& zone, const std::vector& return false; } - const string beginHash = fromBase32Hex(record.d_name.getRawLabels()[0]); + const string beginHash = fromBase32Hex(record.d_name.getRawLabel(0)); if (beginHash == hash) { return !nsec3->isSet(QType::NS); } @@ -436,7 +436,7 @@ static bool provesNSEC3NoWildCard(const DNSName& closestEncloser, uint16_t const return false; } VLOG(log, closestEncloser << ":\tWildcard hash: "< "<d_nexthash)< "<d_nexthash)<d_nexthash)) {