]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
geoipbackend: Move domain loading to separate function
authorAki Tuomi <cmouse@cmouse.fi>
Sun, 1 May 2022 17:50:59 +0000 (20:50 +0300)
committerAki Tuomi <cmouse@cmouse.fi>
Mon, 28 Aug 2023 12:44:07 +0000 (15:44 +0300)
Simplifies next change

modules/geoipbackend/geoipbackend.cc
modules/geoipbackend/geoipbackend.hh

index 232b2d517bb15a1a019b32e4d16372ad4c9c621c..1ff316cd686dc0cb966295f3035eb5f4618cdb03 100644 (file)
@@ -120,48 +120,8 @@ static bool validateMappingLookupFormats(const vector<string>& formats)
   return true;
 }
 
-void GeoIPBackend::initialize()
+bool GeoIPBackend::loadDomain(const YAML::Node& domain, unsigned int id, GeoIPDomain& dom)
 {
-  YAML::Node config;
-  vector<GeoIPDomain> tmp_domains;
-
-  s_geoip_files.clear(); // reset pointers
-
-  if (getArg("database-files").empty() == false) {
-    vector<string> 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<vector<string>>();
-    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<map<std::string, std::string>>();
-  }
-
-  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<string>());
     dom.ttl = domain["ttl"].as<int>();
 
@@ -360,8 +320,52 @@ void GeoIPBackend::initialize()
         }
       }
     }
+  return true;
+}
 
-    tmp_domains.push_back(std::move(dom));
+void GeoIPBackend::initialize()
+{
+  YAML::Node config;
+  vector<GeoIPDomain> tmp_domains;
+
+  s_geoip_files.clear(); // reset pointers
+
+  if (getArg("database-files").empty() == false) {
+    vector<string> 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<vector<string>>();
+    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<map<std::string, std::string>>();
+  }
+
+  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();
index d4138383b1b1e584bc9a7cc99efff8646d7ab942..949481dfd2afb6b9ae51bffa87d9403589a710cd 100644 (file)
 
 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<DNSResourceRecord> d_result;
   vector<GeoIPInterface> d_files;
   std::vector<std::string> d_global_mapping_lookup_formats;