From: Otto Moerbeek Date: Mon, 16 Dec 2024 10:19:17 +0000 (+0100) Subject: rec: if the full CNAME chain leading to the answer is cached, indicate that X-Git-Tag: dnsdist-2.0.0-alpha1~203^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f58b74b096559655ce205cdc4d12299d96a4dd97;p=thirdparty%2Fpdns.git rec: if the full CNAME chain leading to the answer is cached, indicate that Alternative approach to #14918 --- diff --git a/pdns/recursordist/syncres.cc b/pdns/recursordist/syncres.cc index 9a318dfc46..a3591e9311 100644 --- a/pdns/recursordist/syncres.cc +++ b/pdns/recursordist/syncres.cc @@ -1885,6 +1885,32 @@ unsigned int SyncRes::getAdjustedRecursionBound() const return bound; } +static bool haveFinalAnswer(const DNSName& qname, QType qtype, int res, const vector& ret) +{ + if (res != RCode::NoError) { + return false; + } + + // This loop assumes the CNAME's records are in-order + DNSName target(qname); + for (const auto& record : ret) { + if (record.d_place == DNSResourceRecord::ANSWER && record.d_name == target) { + if (record.d_type == qtype) { + return true; + } + if (record.d_type == QType::CNAME) { + if (auto ptr = getRR(record)) { + target = ptr->getTarget(); + } + else { + return false; + } + } + } + } + return false; +} + /*! This function will check the cache and go out to the internet if the answer is not in cache * * \param qname The name we need an answer for @@ -1986,6 +2012,11 @@ int SyncRes::doResolveNoQNameMinimization(const DNSName& qname, const QType qtyp } } } + // This handles the case mentioned above: if the full CNAME chain leading to the answer was + // constructed from the cache, indicate that. + if (fromCache != nullptr && haveFinalAnswer(qname, qtype, res, ret)) { + *fromCache = true; + } return res; } @@ -2035,7 +2066,9 @@ int SyncRes::doResolveNoQNameMinimization(const DNSName& qname, const QType qtyp } } } - + if (fromCache != nullptr && haveFinalAnswer(qname, qtype, res, ret)) { + *fromCache = true; + } return res; } }