}
}
-bool GeoIPBackend::lookup_static(const GeoIPDomain &dom, const DNSName &search, const QType &qtype, const DNSName& qdomain, const std::string &ip, GeoIPLookup &gl, bool v6) {
+bool GeoIPBackend::lookup_static(const GeoIPDomain &dom, const DNSName &search, const QType &qtype, const DNSName& qdomain, const std::string &ip, GeoIPNetmask &gl, bool v6) {
const auto i = dom.records.find(search);
int cumul_probability = 0;
int probability_rnd = 1+(dns_random(1000)); // setting probability=0 means it never is used
}
if (qtype == QType::ANY || rr.qtype == qtype) {
d_result.push_back(rr);
- d_result.back().content = format2str(rr.content, ip, v6, &gl);
+ d_result.back().content = format2str(rr.content, ip, v6, gl);
d_result.back().qname = qdomain;
}
}
void GeoIPBackend::lookup(const QType &qtype, const DNSName& qdomain, DNSPacket *pkt_p, int zoneId) {
ReadLock rl(&s_state_lock);
GeoIPDomain dom;
- GeoIPLookup gl;
+ GeoIPNetmask gl;
bool found = false;
if (d_result.size()>0)
// note that this means the array format won't work with indirect
for(auto it = node->second.begin(); it != node->second.end(); it++) {
- sformat = DNSName(format2str(*it, ip, v6, &gl));
+ sformat = DNSName(format2str(*it, ip, v6, gl));
// see if the record can be found
if (this->lookup_static(dom, sformat, qtype, qdomain, ip, gl, v6))
return true;
}
-string queryGeoIP(const string &ip, bool v6, GeoIPInterface::GeoIPQueryAttribute attribute, GeoIPLookup* gl) {
+string queryGeoIP(const string &ip, bool v6, GeoIPInterface::GeoIPQueryAttribute attribute, GeoIPNetmask& gl) {
string ret = "unknown";
for(auto const& gi: s_geoip_files) {
break;
}
- if (ret == "unknown") gl->netmask = (v6?128:32); // prevent caching
+ if (ret == "unknown") gl.netmask = (v6?128:32); // prevent caching
return ret;
}
-string GeoIPBackend::format2str(string sformat, const string& ip, bool v6, GeoIPLookup* gl) {
+string GeoIPBackend::format2str(string sformat, const string& ip, bool v6, GeoIPNetmask& gl) {
string::size_type cur,last;
time_t t = time((time_t*)NULL);
- GeoIPLookup tmp_gl; // largest wins
+ GeoIPNetmask tmp_gl; // largest wins
struct tm gtm;
gmtime_r(&t, >m);
last=0;
int nrep=3;
tmp_gl.netmask = 0;
if (!sformat.compare(cur,3,"%cn")) {
- rep = queryGeoIP(ip, v6, GeoIPInterface::Continent, &tmp_gl);
+ rep = queryGeoIP(ip, v6, GeoIPInterface::Continent, tmp_gl);
} else if (!sformat.compare(cur,3,"%co")) {
- rep = queryGeoIP(ip, v6, GeoIPInterface::Country, &tmp_gl);
+ rep = queryGeoIP(ip, v6, GeoIPInterface::Country, tmp_gl);
} else if (!sformat.compare(cur,3,"%cc")) {
- rep = queryGeoIP(ip, v6, GeoIPInterface::Country2, &tmp_gl);
+ rep = queryGeoIP(ip, v6, GeoIPInterface::Country2, tmp_gl);
} else if (!sformat.compare(cur,3,"%af")) {
rep = (v6?"v6":"v4");
} else if (!sformat.compare(cur,3,"%as")) {
- rep = queryGeoIP(ip, v6, GeoIPInterface::ASn, &tmp_gl);
+ rep = queryGeoIP(ip, v6, GeoIPInterface::ASn, tmp_gl);
} else if (!sformat.compare(cur,3,"%re")) {
- rep = queryGeoIP(ip, v6, GeoIPInterface::Region, &tmp_gl);
+ rep = queryGeoIP(ip, v6, GeoIPInterface::Region, tmp_gl);
} else if (!sformat.compare(cur,3,"%na")) {
- rep = queryGeoIP(ip, v6, GeoIPInterface::Name, &tmp_gl);
+ rep = queryGeoIP(ip, v6, GeoIPInterface::Name, tmp_gl);
} else if (!sformat.compare(cur,3,"%ci")) {
- rep = queryGeoIP(ip, v6, GeoIPInterface::City, &tmp_gl);
+ rep = queryGeoIP(ip, v6, GeoIPInterface::City, tmp_gl);
} else if (!sformat.compare(cur,3,"%hh")) {
rep = boost::str(boost::format("%02d") % gtm.tm_hour);
tmp_gl.netmask = (v6?128:32);
} else {
last = cur + 1; continue;
}
- if (tmp_gl.netmask > gl->netmask) gl->netmask = tmp_gl.netmask;
+ if (tmp_gl.netmask > gl.netmask) gl.netmask = tmp_gl.netmask;
sformat.replace(cur, nrep, rep);
last = cur + rep.size(); // move to next attribute
}
d_db_type = GeoIP_database_edition(d_gi.get());
}
- bool queryCountry(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryCountry(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = gl.netmask,
+ };
if (d_db_type == GEOIP_COUNTRY_EDITION ||
d_db_type == GEOIP_LARGE_COUNTRY_EDITION) {
- ret = GeoIP_code3_by_id(GeoIP_id_by_addr_gl(d_gi.get(), ip.c_str(), gl));
+ ret = GeoIP_code3_by_id(GeoIP_id_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
+ gl.netmask = tmp_gl.netmask;
return true;
} 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(), gl);
+ GeoIPRegion* 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));
return true;
}
GeoIPRecord *gir = GeoIP_record_by_addr(d_gi.get(), ip.c_str());
if (gir) {
ret = gir->country_code3;
- gl->netmask = gir->netmask;
+ gl.netmask = gir->netmask;
return true;
}
}
return false;
}
- bool queryCountryV6(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryCountryV6(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = gl.netmask,
+ };
if (d_db_type == GEOIP_COUNTRY_EDITION_V6 ||
d_db_type == GEOIP_LARGE_COUNTRY_EDITION_V6) {
- ret = GeoIP_code3_by_id(GeoIP_id_by_addr_v6_gl(d_gi.get(), ip.c_str(), gl));
+ ret = GeoIP_code3_by_id(GeoIP_id_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
+ gl.netmask = tmp_gl.netmask;
return true;
} 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(), gl);
+ GeoIPRegion* 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));
return true;
}
GeoIPRecord *gir = GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str());
if (gir) {
ret = gir->country_code3;
- gl->netmask = gir->netmask;
+ gl.netmask = gir->netmask;
return true;
}
}
return false;
}
- bool queryCountry2(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryCountry2(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = gl.netmask,
+ };
if (d_db_type == GEOIP_COUNTRY_EDITION ||
d_db_type == GEOIP_LARGE_COUNTRY_EDITION) {
- ret = GeoIP_code_by_id(GeoIP_id_by_addr_gl(d_gi.get(), ip.c_str(), gl));
+ ret = GeoIP_code_by_id(GeoIP_id_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
+ gl.netmask = tmp_gl.netmask;
return true;
} 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(), gl);
+ GeoIPRegion* 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));
return true;
}
GeoIPRecord *gir = GeoIP_record_by_addr(d_gi.get(), ip.c_str());
if (gir) {
ret = gir->country_code;
- gl->netmask = gir->netmask;
+ gl.netmask = gir->netmask;
return true;
}
}
return false;
}
- bool queryCountry2V6(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryCountry2V6(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = gl.netmask,
+ };
if (d_db_type == GEOIP_COUNTRY_EDITION_V6 ||
d_db_type == GEOIP_LARGE_COUNTRY_EDITION_V6) {
- ret = GeoIP_code_by_id(GeoIP_id_by_addr_v6_gl(d_gi.get(), ip.c_str(), gl));
+ ret = GeoIP_code_by_id(GeoIP_id_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
+ gl.netmask = tmp_gl.netmask;
return true;
} 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(), gl);
+ GeoIPRegion* 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));
return true;
}
GeoIPRecord *gir = GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str());
if (gir) {
ret = gir->country_code;
- gl->netmask = gir->netmask;
+ gl.netmask = gir->netmask;
return true;
}
}
return false;
}
- bool queryContinent(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryContinent(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = gl.netmask,
+ };
if (d_db_type == GEOIP_COUNTRY_EDITION ||
d_db_type == GEOIP_LARGE_COUNTRY_EDITION) {
- ret = GeoIP_continent_by_id(GeoIP_id_by_addr_gl(d_gi.get(), ip.c_str(), gl));
+ ret = GeoIP_continent_by_id(GeoIP_id_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
+ gl.netmask = tmp_gl.netmask;
return true;
} 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(), gl);
+ GeoIPRegion* 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));
return true;
}
GeoIPRecord *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;
+ gl.netmask = gir->netmask;
return true;
}
}
return false;
}
- bool queryContinentV6(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryContinentV6(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = gl.netmask,
+ };
if (d_db_type == GEOIP_COUNTRY_EDITION_V6 ||
d_db_type == GEOIP_LARGE_COUNTRY_EDITION_V6) {
- ret = GeoIP_continent_by_id(GeoIP_id_by_addr_v6_gl(d_gi.get(), ip.c_str(), gl));
+ ret = GeoIP_continent_by_id(GeoIP_id_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
+ gl.netmask = tmp_gl.netmask;
return true;
} 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(), gl);
+ GeoIPRegion* 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));
return true;
}
GeoIPRecord *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;
+ gl.netmask = gir->netmask;
return true;
}
}
return false;
}
- bool queryName(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryName(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = gl.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(), gl));
+ string val = valueOrEmpty<char*,string>(GeoIP_name_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (!val.empty()) {
+ gl.netmask = tmp_gl.netmask;
// reduce space to dash
ret = boost::replace_all_copy(val, " ", "-");
return true;
return false;
}
- bool queryNameV6(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryNameV6(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = gl.netmask,
+ };
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(), gl));
+ string val = valueOrEmpty<char*,string>(GeoIP_name_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (!val.empty()) {
+ gl.netmask = tmp_gl.netmask;
// reduce space to dash
ret = boost::replace_all_copy(val, " ", "-");
return true;
return false;
}
- bool queryASnum(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryASnum(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .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(), gl));
+ string val = valueOrEmpty<char*,string>(GeoIP_name_by_addr_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (!val.empty()) {
vector<string> asnr;
stringtok(asnr, val);
if(asnr.size()>0) {
+ gl.netmask = tmp_gl.netmask;
ret = asnr[0];
return true;
}
return false;
}
- bool queryASnumV6(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryASnumV6(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .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(), gl));
+ string val = valueOrEmpty<char*,string>(GeoIP_name_by_addr_v6_gl(d_gi.get(), ip.c_str(), &tmp_gl));
if (!val.empty()) {
vector<string> asnr;
stringtok(asnr, val);
if(asnr.size()>0) {
+ gl.netmask = tmp_gl.netmask;
ret = asnr[0];
return true;
}
return false;
}
- bool queryRegion(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryRegion(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = 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(), gl);
+ GeoIPRegion *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);
return true;
}
GeoIPRecord *gir = GeoIP_record_by_addr(d_gi.get(), ip.c_str());
if (gir) {
ret = valueOrEmpty<char*,string>(gir->region);
- gl->netmask = gir->netmask;
+ gl.netmask = gir->netmask;
return true;
}
}
return false;
}
- bool queryRegionV6(string &ret, GeoIPLookup* gl, const string &ip) override {
+ bool queryRegionV6(string &ret, GeoIPNetmask& gl, const string &ip) override {
+ GeoIPLookup tmp_gl = {
+ .netmask = gl.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(), gl);
+ GeoIPRegion *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);
return true;
}
GeoIPRecord *gir = GeoIP_record_by_addr_v6(d_gi.get(), ip.c_str());
if (gir) {
ret = valueOrEmpty<char*,string>(gir->region);
- gl->netmask = gir->netmask;
+ gl.netmask = gir->netmask;
return true;
}
}
return false;
}
- bool queryCity(string &ret, GeoIPLookup* gl, const string &ip) override {
+ 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());
if (gir) {
ret = valueOrEmpty<char*,string>(gir->city);
- gl->netmask = gir->netmask;
+ gl.netmask = gir->netmask;
return true;
}
}
return false;
}
- bool queryCityV6(string &ret, GeoIPLookup* gl, const string &ip) override {
+ 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());
if (gir) {
ret = valueOrEmpty<char*,string>(gir->city);
- gl->netmask = gir->netmask;
+ gl.netmask = gir->netmask;
return true;
}
}
Region
};
- virtual bool queryCountry(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryCountryV6(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryCountry2(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryCountry2V6(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryContinent(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryContinentV6(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryName(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryNameV6(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryASnum(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryASnumV6(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryRegion(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryRegionV6(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryCity(string &ret, GeoIPLookup* gl, const string &ip) = 0;
- virtual bool queryCityV6(string &ret, GeoIPLookup* gl, const string &ip) = 0;
+ virtual bool queryCountry(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryCountryV6(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryCountry2(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryCountry2V6(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryContinent(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryContinentV6(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryName(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryNameV6(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryASnum(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryASnumV6(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryRegion(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryRegionV6(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryCity(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
+ virtual bool queryCityV6(string &ret, GeoIPNetmask& gl, const string &ip) = 0;
virtual ~GeoIPInterface() { }