Add weight feature to GeoIP backend #3338

Merged
merged 2 commits into from Feb 9, 2016

Projects

None yet

3 participants

@cmouse
Contributor
cmouse commented Feb 5, 2016

This allows administrator to define weights on records so that they are returned approximately weight% times on queries.

@ahupowerdns ahupowerdns commented on an outdated diff Feb 5, 2016
docs/markdown/authoritative/backend-geoip.md
- ns: ns2.example.com
- mx: 10 mx.example.com
fin.eu.service.geo.example.com:
- a: 198.51.100.221
- txt: hello world
- aaaa: 2001:DB8::12:34DE:3
+ swe.eu.service.geo.example.com
+ - a:
+ content: 1.2.3.4
+ weight: 50
+ - a: 2.3.4.5
@ahupowerdns
ahupowerdns Feb 5, 2016 Member

what is the weight of 2.3.4.5?

@Habbie
Member
Habbie commented Feb 5, 2016

(Please use documentation IP space like 192.0.2.0/24)

@ahupowerdns ahupowerdns and 1 other commented on an outdated diff Feb 5, 2016
modules/geoipbackend/geoipbackend.cc
@@ -123,9 +128,24 @@ void GeoIPBackend::initialize() {
}
if (rec->second.IsNull()) {
rr.content = "";
- } else {
+ } else if (rec->second.IsMap()) {
+ for(YAML::const_iterator iter = rec->second.begin(); iter != rec->second.end(); iter++) {
+ string attr = iter->first.as<string>();
+ if (attr == "content") {
+ string content = iter->second.as<string>();
+ rr.content = content;
+ } else if (attr == "weight") {
+ rr.weight = iter->second.as<int>();
+ } else if (attr == "ttl") {
+ rr.ttl = iter->second.as<int>();
+ } else {
+ L<<Logger::Warning<<"Unsupported record attribute " << attr << " for " << rr.qname << endl;
@ahupowerdns
ahupowerdns Feb 5, 2016 Member

just kill it at this point. Will only lead to silently wrong config files.

@cmouse
cmouse Feb 5, 2016 Contributor

well, it does log it. =)

@ahupowerdns ahupowerdns commented on an outdated diff Feb 5, 2016
modules/geoipbackend/geoipbackend.cc
@@ -208,6 +230,39 @@ void GeoIPBackend::initialize() {
}
}
+ // finally fix weights
+ for(auto &item: dom.records) {
+ int weight=-1;
+ int rem, count;
+ rem = count = 0;
+ // first we look for used weight
+ for(const auto &rr: item.second) {
+ if (rr.weight > -1) {
+ if (weight==-1)
@ahupowerdns
ahupowerdns Feb 5, 2016 Member

-1 means it won't be -1

@cmouse
Contributor
cmouse commented Feb 5, 2016

Rewrote to use weight "as usual". Please review again, sorry for rebase.

@ahupowerdns ahupowerdns merged commit f2772ba into PowerDNS:master Feb 9, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@cmouse cmouse deleted the cmouse:geoip-weights branch Feb 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment