#include "geoipinterface.hh"
#ifdef HAVE_GEOIP
#include "GeoIPCity.h"
+#include "GeoIP.h"
struct geoip_deleter {
void operator()(GeoIP* ptr) {
- if (ptr) GeoIP_delete(ptr);
+ if (ptr) {
+ GeoIP_delete(ptr);
+ }
};
};
+struct geoiprecord_deleter {
+ void operator()(GeoIPRecord* ptr) {
+ if (ptr) {
+ GeoIPRecord_delete(ptr);
+ }
+ }
+};
+
+struct geoipregion_deleter {
+ void operator()(GeoIPRegion* ptr) {
+ if (ptr) {
+ GeoIPRegion_delete(ptr);
+ }
+ }
+};
+
class GeoIPInterfaceDAT : public GeoIPInterface {
public:
GeoIPInterfaceDAT(const string &fname, const string &modeStr) {
flags = GEOIP_MEMORY_CACHE;
else if (modeStr == "index")
flags = GEOIP_INDEX_CACHE;
- #ifdef HAVE_MMAP
+#ifdef HAVE_MMAP
else if (modeStr == "mmap")
flags = GEOIP_MMAP_CACHE;
- #endif
+#endif
else
throw PDNSException("Invalid cache mode " + modeStr + " for GeoIP backend");
- d_gi = std::unique_ptr<GeoIP,geoip_deleter>(GeoIP_open(fname.c_str(), flags));
- if (d_gi.get() == NULL)
+ d_gi = std::unique_ptr<GeoIP, geoip_deleter>(GeoIP_open(fname.c_str(), flags));
+ if (d_gi.get() == nullptr)
throw PDNSException("Cannot open GeoIP database " + fname);
d_db_type = GeoIP_database_edition(d_gi.get());
}
}
} else if (d_db_type == GEOIP_REGION_EDITION_REV0 ||
d_db_type == GEOIP_REGION_EDITION_REV1) {
- GeoIPRegion* gir = GeoIP_region_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ std::unique_ptr<GeoIPRegion, geoipregion_deleter> gir(GeoIP_region_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (gir) {
gl.netmask = tmp_gl.netmask;
ret = GeoIP_code3_by_id(GeoIP_id_by_code(gir->country_code));
}
} else if (d_db_type == GEOIP_CITY_EDITION_REV0 ||
d_db_type == GEOIP_CITY_EDITION_REV1) {
- GeoIPRecord *gir = GeoIP_record_by_addr(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr(d_gi.get(), ip.c_str()));
if (gir) {
ret = gir->country_code3;
gl.netmask = gir->netmask;
}
} else if (d_db_type == GEOIP_REGION_EDITION_REV0 ||
d_db_type == GEOIP_REGION_EDITION_REV1) {
- GeoIPRegion* gir = GeoIP_region_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ std::unique_ptr<GeoIPRegion, geoipregion_deleter> gir(GeoIP_region_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (gir) {
gl.netmask = tmp_gl.netmask;
ret = GeoIP_code3_by_id(GeoIP_id_by_code(gir->country_code));
}
} else if (d_db_type == GEOIP_CITY_EDITION_REV0_V6 ||
d_db_type == GEOIP_CITY_EDITION_REV1_V6) {
- GeoIPRecord *gir = GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str()));
if (gir) {
ret = gir->country_code3;
gl.netmask = gir->netmask;
}
} else if (d_db_type == GEOIP_REGION_EDITION_REV0 ||
d_db_type == GEOIP_REGION_EDITION_REV1) {
- GeoIPRegion* gir = GeoIP_region_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ std::unique_ptr<GeoIPRegion,geoipregion_deleter> gir(GeoIP_region_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (gir) {
gl.netmask = tmp_gl.netmask;
ret = GeoIP_code_by_id(GeoIP_id_by_code(gir->country_code));
}
} else if (d_db_type == GEOIP_CITY_EDITION_REV0 ||
d_db_type == GEOIP_CITY_EDITION_REV1) {
- GeoIPRecord *gir = GeoIP_record_by_addr(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr(d_gi.get(), ip.c_str()));
if (gir) {
ret = gir->country_code;
gl.netmask = gir->netmask;
}
} else if (d_db_type == GEOIP_REGION_EDITION_REV0 ||
d_db_type == GEOIP_REGION_EDITION_REV1) {
- GeoIPRegion* gir = GeoIP_region_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ std::unique_ptr<GeoIPRegion, geoipregion_deleter> gir(GeoIP_region_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (gir) {
gl.netmask = tmp_gl.netmask;
ret = GeoIP_code_by_id(GeoIP_id_by_code(gir->country_code));
}
} else if (d_db_type == GEOIP_CITY_EDITION_REV0_V6 ||
d_db_type == GEOIP_CITY_EDITION_REV1_V6) {
- GeoIPRecord *gir = GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str()));
if (gir) {
ret = gir->country_code;
gl.netmask = gir->netmask;
}
} else if (d_db_type == GEOIP_REGION_EDITION_REV0 ||
d_db_type == GEOIP_REGION_EDITION_REV1) {
- GeoIPRegion* gir = GeoIP_region_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ std::unique_ptr<GeoIPRegion, geoipregion_deleter> gir(GeoIP_region_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (gir) {
gl.netmask = tmp_gl.netmask;
ret = GeoIP_continent_by_id(GeoIP_id_by_code(gir->country_code));
}
} else if (d_db_type == GEOIP_CITY_EDITION_REV0 ||
d_db_type == GEOIP_CITY_EDITION_REV1) {
- GeoIPRecord *gir = GeoIP_record_by_addr(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr(d_gi.get(), ip.c_str()));
if (gir) {
ret = ret = GeoIP_continent_by_id(GeoIP_id_by_code(gir->country_code));
gl.netmask = gir->netmask;
}
} else if (d_db_type == GEOIP_REGION_EDITION_REV0 ||
d_db_type == GEOIP_REGION_EDITION_REV1) {
- GeoIPRegion* gir = GeoIP_region_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ std::unique_ptr<GeoIPRegion, geoipregion_deleter> gir(GeoIP_region_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (gir) {
gl.netmask = tmp_gl.netmask;
ret = GeoIP_continent_by_id(GeoIP_id_by_code(gir->country_code));
}
} else if (d_db_type == GEOIP_CITY_EDITION_REV0_V6 ||
d_db_type == GEOIP_CITY_EDITION_REV1_V6) {
- GeoIPRecord *gir = GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str()));
if (gir) {
ret = GeoIP_continent_by_id(GeoIP_id_by_code(gir->country_code));
gl.netmask = gir->netmask;
};
if (d_db_type == GEOIP_ISP_EDITION ||
d_db_type == GEOIP_ORG_EDITION) {
- string val = valueOrEmpty<char*,string>(GeoIP_name_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
- if (!val.empty()) {
+ char* result = GeoIP_name_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ if (result != nullptr) {
+ ret = result;
+ free(result);
gl.netmask = tmp_gl.netmask;
// reduce space to dash
- ret = boost::replace_all_copy(val, " ", "-");
+ ret = boost::replace_all_copy(ret, " ", "-");
return true;
}
+
}
return false;
}
};
if (d_db_type == GEOIP_ISP_EDITION_V6 ||
d_db_type == GEOIP_ORG_EDITION_V6) {
- string val = valueOrEmpty<char*,string>(GeoIP_name_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
- if (!val.empty()) {
+ char* result = GeoIP_name_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ if (result != nullptr) {
+ ret = result;
+ free(result);
gl.netmask = tmp_gl.netmask;
// reduce space to dash
- ret = boost::replace_all_copy(val, " ", "-");
+ ret = boost::replace_all_copy(ret, " ", "-");
return true;
}
}
.netmask = gl.netmask,
};
if (d_db_type == GEOIP_ASNUM_EDITION) {
- string val = valueOrEmpty<char*,string>(GeoIP_name_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
- if (!val.empty()) {
+ char* result = GeoIP_name_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ if (result != nullptr) {
+ std::string val(result);
vector<string> asnr;
+ free(result);
stringtok(asnr, val);
if(asnr.size()>0) {
gl.netmask = tmp_gl.netmask;
.netmask = gl.netmask,
};
if (d_db_type == GEOIP_ASNUM_EDITION_V6) {
- string val = valueOrEmpty<char*,string>(GeoIP_name_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
- if (!val.empty()) {
+ char* result = GeoIP_name_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ if (result != nullptr) {
+ std::string val(result);
vector<string> asnr;
+ free(result);
stringtok(asnr, val);
if(asnr.size()>0) {
gl.netmask = tmp_gl.netmask;
};
if (d_db_type == GEOIP_REGION_EDITION_REV0 ||
d_db_type == GEOIP_REGION_EDITION_REV1) {
- GeoIPRegion *gir = GeoIP_region_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ std::unique_ptr<GeoIPRegion, geoipregion_deleter> gir(GeoIP_region_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (gir) {
gl.netmask = tmp_gl.netmask;
ret = valueOrEmpty<char*,string>(gir->region);
}
} else if (d_db_type == GEOIP_CITY_EDITION_REV0 ||
d_db_type == GEOIP_CITY_EDITION_REV1) {
- GeoIPRecord *gir = GeoIP_record_by_addr(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr(d_gi.get(), ip.c_str()));
if (gir) {
ret = valueOrEmpty<char*,string>(gir->region);
gl.netmask = gir->netmask;
};
if (d_db_type == GEOIP_REGION_EDITION_REV0 ||
d_db_type == GEOIP_REGION_EDITION_REV1) {
- GeoIPRegion *gir = GeoIP_region_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl);
+ std::unique_ptr<GeoIPRegion, geoipregion_deleter> gir(GeoIP_region_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (gir) {
gl.netmask = tmp_gl.netmask;
ret = valueOrEmpty<char*,string>(gir->region);
}
} else if (d_db_type == GEOIP_CITY_EDITION_REV0_V6 ||
d_db_type == GEOIP_CITY_EDITION_REV1_V6) {
- GeoIPRecord *gir = GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str()));
if (gir) {
ret = valueOrEmpty<char*,string>(gir->region);
gl.netmask = gir->netmask;
bool queryCity(string &ret, GeoIPNetmask& gl, const string &ip) override {
if (d_db_type == GEOIP_CITY_EDITION_REV0 ||
d_db_type == GEOIP_CITY_EDITION_REV1) {
- GeoIPRecord *gir = GeoIP_record_by_addr(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr(d_gi.get(), ip.c_str()));
if (gir) {
ret = valueOrEmpty<char*,string>(gir->city);
gl.netmask = gir->netmask;
bool queryCityV6(string &ret, GeoIPNetmask& gl, const string &ip) override {
if (d_db_type == GEOIP_CITY_EDITION_REV0_V6 ||
d_db_type == GEOIP_CITY_EDITION_REV1_V6) {
- GeoIPRecord *gir = GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str()));
if (gir) {
ret = valueOrEmpty<char*,string>(gir->city);
gl.netmask = gir->netmask;
d_db_type == GEOIP_REGION_EDITION_REV1 ||
d_db_type == GEOIP_CITY_EDITION_REV0_V6 ||
d_db_type == GEOIP_CITY_EDITION_REV1_V6) {
- GeoIPRecord *gir = GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str()));
if (gir) {
latitude = gir->latitude;
longitude = gir->longitude;
d_db_type == GEOIP_REGION_EDITION_REV1 ||
d_db_type == GEOIP_CITY_EDITION_REV0 ||
d_db_type == GEOIP_CITY_EDITION_REV1) {
- GeoIPRecord *gir = GeoIP_record_by_addr(d_gi.get(), ip.c_str());
+ std::unique_ptr<GeoIPRecord, geoiprecord_deleter> gir(GeoIP_record_by_addr(d_gi.get(), ip.c_str()));
if (gir) {
latitude = gir->latitude;
longitude = gir->longitude;
~GeoIPInterfaceDAT() { }
private:
unsigned int d_db_type;
- unique_ptr<GeoIP,geoip_deleter> d_gi;
+ unique_ptr<GeoIP, geoip_deleter> d_gi;
};
unique_ptr<GeoIPInterface> GeoIPInterface::makeDATInterface(const string &fname, const map<string, string>& opts) {