From: Otto Moerbeek Date: Thu, 14 Feb 2019 15:15:03 +0000 (+0100) Subject: Move from random() to dns_random() X-Git-Tag: dnsdist-1.4.0-alpha1~54^2~4 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=45d4e670f20d866afe3ea2fb11f434ae942cfe6e;p=pdns Move from random() to dns_random() An alternative would be to make sure srandom() is always called. But since dns_random() has the nice uniform upperbound API, I have chosen to use that one. --- diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 429069e4e..ce9327d53 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -7,6 +7,7 @@ #include "ueberbackend.hh" #include #include "dnsrecords.hh" +#include "dns_random.hh" #include "../modules/geoipbackend/geoipinterface.hh" // only for the enum @@ -249,7 +250,7 @@ static ComboAddress pickrandom(const vector& ips) if (ips.empty()) { throw std::invalid_argument("The IP list cannot be empty"); } - return ips[random() % ips.size()]; + return ips[dns_random(ips.size())]; } static ComboAddress hashed(const ComboAddress& who, const vector& ips) @@ -273,7 +274,7 @@ static ComboAddress pickwrandom(const vector >& wips) sum += i.first; pick.push_back({sum, i.second}); } - int r = random() % sum; + int r = dns_random(sum); auto p = upper_bound(pick.begin(), pick.end(),r, [](int r, const decltype(pick)::value_type& a) { return r < a.first;}); return p->second; } @@ -379,7 +380,7 @@ static ComboAddress pickclosest(const ComboAddress& bestwho, const vectorsecond[random() % ranked.begin()->second.size()]; + return ranked.begin()->second[dns_random(ranked.begin()->second.size())]; } static std::vector lookup(const DNSName& name, uint16_t qtype, int zoneid)