]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
make geobackend optional, but function with it, even when loaded as a .so
authorbert hubert <bert.hubert@netherlabs.nl>
Sun, 10 Dec 2017 13:12:39 +0000 (14:12 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Sun, 10 Dec 2017 13:12:39 +0000 (14:12 +0100)
modules/geoipbackend/geoipbackend.cc
modules/geoipbackend/geoipbackend.hh
pdns/lua-record.cc

index ab299da01e4f71e8d4509d240d292085a06ba825..1fdb36de21be30256712d67ab1dbb507e5cbdc92 100644 (file)
@@ -87,6 +87,27 @@ GeoIPBackend::GeoIPBackend(const string& suffix) {
   s_rc++;
 }
 
+string getGeoForLua(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa)
+{
+  try {
+    GeoIPBackend gib;
+    GeoIPLookup gl;
+    string res=gib.queryGeoIP(ip, false, qa, &gl);
+    //    cout<<"Result for "<<ip<<" lookup: "<<res<<endl;
+    if(qa==GeoIPBackend::ASn && boost::starts_with(res, "as"))
+      return res.substr(2);
+    return res;
+  }
+  catch(std::exception& e) {
+    cout<<"Error: "<<e.what()<<endl;
+  }
+  catch(PDNSException& e) {
+    cout<<"Error: "<<e.reason<<endl;
+  }
+  return "";
+}
+
+
 void GeoIPBackend::initialize() {
   YAML::Node config;
   vector<GeoIPDomain> tmp_domains;
@@ -289,6 +310,9 @@ void GeoIPBackend::initialize() {
 
   s_domains.clear();
   std::swap(s_domains, tmp_domains);
+
+  extern std::function<std::string(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute)> g_getGeo;
+  g_getGeo = getGeoForLua;
 }
 
 GeoIPBackend::~GeoIPBackend() {
@@ -1098,25 +1122,6 @@ bool GeoIPBackend::hasDNSSECkey(const DNSName& name) {
   return false;
 }
 
-string getGeo(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa)
-{
-  try {
-    GeoIPBackend gib;
-    GeoIPLookup gl;
-    string res=gib.queryGeoIP(ip, false, qa, &gl);
-    //    cout<<"Result for "<<ip<<" lookup: "<<res<<endl;
-    if(qa==GeoIPBackend::ASn && boost::starts_with(res, "as"))
-      return res.substr(2);
-    return res;
-  }
-  catch(std::exception& e) {
-    cout<<"Error: "<<e.what()<<endl;
-  }
-  catch(PDNSException& e) {
-    cout<<"Error: "<<e.reason<<endl;
-  }
-  return "";
-}
 
 
 class GeoIPFactory : public BackendFactory{
index d963a798e2ac8d4bd6827f68536661dff835d377..02a66a4e80f7af1f3cee3aa26d7cdec005fca17e 100644 (file)
@@ -112,6 +112,4 @@ private:
   vector<DNSResourceRecord> d_result;
 };
 
-std::string getGeo(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa);
-
 #endif /* PDNS_GEOIPBACKEND_HH */
index f7fff7d107a4737f5dea07f0f517269c901921fe..8a6db6d7d869fa4e0d12620f338e55d010475a38 100644 (file)
@@ -6,6 +6,7 @@
 #include "minicurl.hh"
 #include "ueberbackend.hh"
 #include <boost/format.hpp>
+// this is only for the ENUM
 #include "../../modules/geoipbackend/geoipbackend.hh"
 
 /* to do:
@@ -193,12 +194,20 @@ bool doCompare(const T& var, const std::string& res, const C& cmp)
 }
 
 
-std::string getGeo(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa) __attribute__((weak));
+std::function<std::string(const std::string&, GeoIPBackend::GeoIPQueryAttribute)> g_getGeo;
 
 std::string getGeo(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa)
 {
-  cerr<<"Weak variant called"<<endl;
-  return "unknown";
+  static bool intialized;
+  if(!g_getGeo) {
+    if(!initialized) {
+      L<<Logger::Error<<"LUA Record attempted to use GeoIPBackend functionality, but backend not launched"<<endl;
+      initialized=true;
+    }
+    return "unknown";
+  }
+  else
+    return g_getGeo(ip, qa);
 }
 
 static ComboAddress pickrandom(vector<ComboAddress>& ips)