From: Aki Tuomi Date: Sun, 1 May 2022 17:50:59 +0000 (+0300) Subject: geoipbackend: Move domain loading to separate function X-Git-Tag: rec-5.0.0-alpha1~27^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4287cb762b1d3603047d60e3d0918e7cf09ddd00;p=thirdparty%2Fpdns.git geoipbackend: Move domain loading to separate function Simplifies next change --- diff --git a/modules/geoipbackend/geoipbackend.cc b/modules/geoipbackend/geoipbackend.cc index 232b2d517b..1ff316cd68 100644 --- a/modules/geoipbackend/geoipbackend.cc +++ b/modules/geoipbackend/geoipbackend.cc @@ -120,48 +120,8 @@ static bool validateMappingLookupFormats(const vector& formats) return true; } -void GeoIPBackend::initialize() +bool GeoIPBackend::loadDomain(const YAML::Node& domain, unsigned int id, GeoIPDomain& dom) { - YAML::Node config; - vector tmp_domains; - - s_geoip_files.clear(); // reset pointers - - if (getArg("database-files").empty() == false) { - vector files; - stringtok(files, getArg("database-files"), " ,\t\r\n"); - for (auto const& file : files) { - s_geoip_files.push_back(GeoIPInterface::makeInterface(file)); - } - } - - if (s_geoip_files.empty()) - g_log << Logger::Warning << "No GeoIP database files loaded!" << endl; - - if (!getArg("zones-file").empty()) { - try { - config = YAML::LoadFile(getArg("zones-file")); - } - catch (YAML::Exception& ex) { - throw PDNSException(string("Cannot read config file ") + ex.msg); - } - } - - // Global lookup formats and mapping will be used - // if none defined at the domain level. - if (YAML::Node formats = config["mapping_lookup_formats"]) { - d_global_mapping_lookup_formats = formats.as>(); - if (!validateMappingLookupFormats(d_global_mapping_lookup_formats)) - throw PDNSException(string("%mp is not allowed in mapping lookup")); - } - if (YAML::Node mapping = config["custom_mapping"]) { - d_global_custom_mapping = mapping.as>(); - } - - for (YAML::const_iterator _domain = config["domains"].begin(); _domain != config["domains"].end(); _domain++) { - const auto& domain = *_domain; - GeoIPDomain dom; - dom.id = tmp_domains.size(); dom.domain = DNSName(domain["domain"].as()); dom.ttl = domain["ttl"].as(); @@ -360,8 +320,52 @@ void GeoIPBackend::initialize() } } } + return true; +} - tmp_domains.push_back(std::move(dom)); +void GeoIPBackend::initialize() +{ + YAML::Node config; + vector tmp_domains; + + s_geoip_files.clear(); // reset pointers + + if (getArg("database-files").empty() == false) { + vector files; + stringtok(files, getArg("database-files"), " ,\t\r\n"); + for (auto const& file : files) { + s_geoip_files.push_back(GeoIPInterface::makeInterface(file)); + } + } + + if (s_geoip_files.empty()) + g_log << Logger::Warning << "No GeoIP database files loaded!" << endl; + + if (!getArg("zones-file").empty()) { + try { + config = YAML::LoadFile(getArg("zones-file")); + } + catch (YAML::Exception& ex) { + throw PDNSException(string("Cannot read config file ") + ex.msg); + } + } + + // Global lookup formats and mapping will be used + // if none defined at the domain level. + if (YAML::Node formats = config["mapping_lookup_formats"]) { + d_global_mapping_lookup_formats = formats.as>(); + if (!validateMappingLookupFormats(d_global_mapping_lookup_formats)) + throw PDNSException(string("%mp is not allowed in mapping lookup")); + } + if (YAML::Node mapping = config["custom_mapping"]) { + d_global_custom_mapping = mapping.as>(); + } + + for (YAML::const_iterator _domain = config["domains"].begin(); _domain != config["domains"].end(); _domain++) { + GeoIPDomain dom; + auto id = tmp_domains.size(); + if (loadDomain(*_domain, id, dom)) + tmp_domains.push_back(std::move(dom)); } s_domains.clear(); diff --git a/modules/geoipbackend/geoipbackend.hh b/modules/geoipbackend/geoipbackend.hh index d4138383b1..949481dfd2 100644 --- a/modules/geoipbackend/geoipbackend.hh +++ b/modules/geoipbackend/geoipbackend.hh @@ -36,6 +36,11 @@ class GeoIPInterface; +namespace YAML +{ +class Node; +}; + struct GeoIPDomain; struct GeoIPNetmask @@ -77,6 +82,7 @@ private: bool d_dnssec; bool hasDNSSECkey(const DNSName& name); bool lookup_static(const GeoIPDomain& dom, const DNSName& search, const QType& qtype, const DNSName& qdomain, const Netmask& addr, GeoIPNetmask& gl); + bool loadDomain(const YAML::Node& domain, unsigned int id, GeoIPDomain& dom); vector d_result; vector d_files; std::vector d_global_mapping_lookup_formats;