From: Fred Morcos Date: Thu, 26 Oct 2023 12:09:33 +0000 (+0200) Subject: Refactor UeberBackend::getAuth: UeberBackend::fillSOAFromZoneRecord X-Git-Tag: rec-5.0.0-beta1~16^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=591e66f443043151ca383f87a6263d2c3e3decaf;p=thirdparty%2Fpdns.git Refactor UeberBackend::getAuth: UeberBackend::fillSOAFromZoneRecord --- diff --git a/pdns/ueberbackend.cc b/pdns/ueberbackend.cc index e03c9016c3..da29c062a8 100644 --- a/pdns/ueberbackend.cc +++ b/pdns/ueberbackend.cc @@ -332,6 +332,47 @@ bool UeberBackend::inTransaction() return false; } +bool UeberBackend::fillSOAFromZoneRecord(DNSName& shorter, const int zoneId, SOAData* const soaData) +{ + // Zone exists in zone cache, directly look up SOA. + lookup(QType(QType::SOA), shorter, zoneId, nullptr); + + DNSZoneRecord zoneRecord; + if (!get(zoneRecord)) { + DLOG(g_log << Logger::Info << "Backend returned no SOA for zone '" << shorter.toLogString() << "', which it reported as existing " << endl); + return false; + } + + if (zoneRecord.dr.d_name != shorter) { + throw PDNSException("getAuth() returned an SOA for the wrong zone. Zone '" + zoneRecord.dr.d_name.toLogString() + "' is not equal to looked up zone '" + shorter.toLogString() + "'"); + } + + // Fill soaData. + soaData->qname = zoneRecord.dr.d_name; + + try { + fillSOAData(zoneRecord, *soaData); + } + catch (...) { + g_log << Logger::Warning << "Backend returned a broken SOA for zone '" << shorter.toLogString() << "'" << endl; + + while (get(zoneRecord)) { + ; + } + + return false; + } + + soaData->db = backends.size() == 1 ? *backends.begin() : nullptr; + + // Leave database handle in a consistent state. + while (get(zoneRecord)) { + ; + } + + return true; +} + bool UeberBackend::getAuth(const DNSName& target, const QType& qtype, SOAData* soaData, bool cachedOk) { // A backend can respond to our authority request with the 'best' match it @@ -351,39 +392,12 @@ bool UeberBackend::getAuth(const DNSName& target, const QType& qtype, SOAData* s if (cachedOk && g_zoneCache.isEnabled()) { if (g_zoneCache.getEntry(shorter, zoneId)) { - // Zone exists in zone cache, directly look up SOA. - DNSZoneRecord zoneRecord; - lookup(QType(QType::SOA), shorter, zoneId, nullptr); - if (!get(zoneRecord)) { - DLOG(g_log << Logger::Info << "Backend returned no SOA for zone '" << shorter.toLogString() << "', which it reported as existing " << endl); - continue; - } - if (zoneRecord.dr.d_name != shorter) { - throw PDNSException("getAuth() returned an SOA for the wrong zone. Zone '" + zoneRecord.dr.d_name.toLogString() + "' is not equal to looked up zone '" + shorter.toLogString() + "'"); - } - // fill soaData - soaData->qname = zoneRecord.dr.d_name; - try { - fillSOAData(zoneRecord, *soaData); - } - catch (...) { - g_log << Logger::Warning << "Backend returned a broken SOA for zone '" << shorter.toLogString() << "'" << endl; - while (get(zoneRecord)) { - ; - } - continue; - } - if (backends.size() == 1) { - soaData->db = *backends.begin(); + if (fillSOAFromZoneRecord(shorter, zoneId, soaData)) { + goto found; } else { - soaData->db = nullptr; - } - // leave database handle in a consistent state - while (get(zoneRecord)) { - ; + continue; } - goto found; } // Zone does not exist, try again with a shorter name. diff --git a/pdns/ueberbackend.hh b/pdns/ueberbackend.hh index 241751c91d..be54670357 100644 --- a/pdns/ueberbackend.hh +++ b/pdns/ueberbackend.hh @@ -168,4 +168,6 @@ private: int cacheHas(const Question& q, vector& rrs) const; void addNegCache(const Question& q) const; void addCache(const Question& q, vector&& rrs) const; + + bool fillSOAFromZoneRecord(DNSName& shorter, int zoneId, SOAData* soaData); };