]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Stop using random(), only place left is in dnsdist-random.cc as a fallback
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 20 Jun 2023 10:50:36 +0000 (12:50 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Thu, 6 Jul 2023 13:46:42 +0000 (15:46 +0200)
pdns/auth-main.cc
pdns/credentials.cc
pdns/dnsdist-lua-rules.cc
pdns/dnsdistdist/dnsdist-lbpolicies.cc
pdns/dnsdistdist/dnsdist-rules.hh
pdns/misc.cc
pdns/pdnsutil.cc
pdns/recursordist/rec-main.cc
pdns/unix_utility.cc
pdns/utility.hh

index 390f7188f4c02e36d6daf4351391a041024f4c53..38d81c56aaedff03a9fb45bcb9940639b60d0089 100644 (file)
@@ -685,8 +685,6 @@ static void triggerLoadOfLibraries()
 
 static void mainthread()
 {
-  Utility::srandom();
-
   gid_t newgid = 0;
   if (!::arg()["setgid"].empty())
     newgid = strToGID(::arg()["setgid"]);
index 343c1205ecac5d7e61ecc589d314da8575dd4979..5038804b6386a4758727b065c09011b7d0747afb 100644 (file)
@@ -40,6 +40,7 @@
 #include <unistd.h>
 
 #include "base64.hh"
+#include "dns_random.hh"
 #include "credentials.hh"
 #include "misc.hh"
 
@@ -373,7 +374,7 @@ CredentialsHolder::CredentialsHolder(std::string&& password, bool hashPlaintext)
   }
 
   if (!d_isHashed) {
-    d_fallbackHashPerturb = random();
+    d_fallbackHashPerturb = dns_random_uint32();
     d_fallbackHash = burtle(reinterpret_cast<const unsigned char*>(d_credentials.getString().data()), d_credentials.getString().size(), d_fallbackHashPerturb);
   }
 }
index 762e4e670db1e8ea05be98e74934be1694ed0652..8549014e72efef989069fb4d6e9289bce5b84954 100644 (file)
@@ -22,6 +22,7 @@
 #include "dnsdist.hh"
 #include "dnsdist-lua.hh"
 #include "dnsdist-rules.hh"
+#include "dns_random.hh"
 
 std::shared_ptr<DNSRule> makeRule(const luadnsrule_t& var)
 {
@@ -443,9 +444,9 @@ void setupLuaRules(LuaContext& luaCtx)
       items.reserve(1000);
       for (int n = 0; n < 1000; ++n) {
         struct item i;
-        i.ids.qname = DNSName(std::to_string(random()));
+        i.ids.qname = DNSName(std::to_string(dns_random_uint32()));
         i.ids.qname += suffix;
-        i.ids.qtype = random() % 0xff;
+        i.ids.qtype = dns_random(0xff);
         i.ids.qclass = QClass::IN;
         i.ids.protocol = dnsdist::Protocol::DoUDP;
         i.ids.origRemote = ComboAddress("127.0.0.1");
index 70fec893c88ada5f81a3153cb4f472186e5f5bbf..3aed13d54ab182653fa94ae8e504cb8ca3a566bb 100644 (file)
@@ -25,6 +25,7 @@
 #include "dnsdist-lua.hh"
 #include "dnsdist-lua-ffi.hh"
 #include "dolog.hh"
+#include "dns_random.hh"
 
 GlobalStateHolder<ServerPolicy> g_policy;
 bool g_roundrobinFailOnNoServer{false};
@@ -153,7 +154,7 @@ static shared_ptr<DownstreamState> valrandom(const unsigned int val, const Serve
 
 shared_ptr<DownstreamState> wrandom(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq)
 {
-  return valrandom(random(), servers);
+  return valrandom(dns_random_uint32(), servers);
 }
 
 uint32_t g_hashperturb;
index 651911e4f80051e46c050ff30b6964598a5a47aa..5b4e80a3947fc439d7971a3f55f2aebe9484cdc8 100644 (file)
@@ -33,6 +33,7 @@
 #include "dnsdist-lua-ffi.hh"
 #include "dolog.hh"
 #include "dnsparser.hh"
+#include "dns_random.hh"
 
 class MaxQPSIPRule : public DNSRule
 {
@@ -1055,7 +1056,7 @@ public:
   {
     if(d_proba == 1.0)
       return true;
-    double rnd = 1.0*random() / RAND_MAX;
+    double rnd = 1.0*dns_random_uint32() / UINT32_MAX;
     return rnd > (1.0 - d_proba);
   }
   string toString() const override
index 4cfe6d4196cc7f70e69afc283c7cd8930e08f7fc..5c9c81785fa43d31bd5d181f731b548f73cb0e3d 100644 (file)
@@ -56,6 +56,7 @@
 #include <boost/format.hpp>
 #include "iputils.hh"
 #include "dnsparser.hh"
+#include "dns_random.hh"
 #include <pwd.h>
 #include <grp.h>
 #include <climits>
@@ -432,7 +433,7 @@ int waitForMultiData(const set<int>& fds, const int seconds, const int useconds,
     }
   }
   set<int>::const_iterator it(pollinFDs.begin());
-  advance(it, random() % pollinFDs.size());
+  advance(it, dns_random(pollinFDs.size()));
   *fdOut = *it;
   return 1;
 }
@@ -463,7 +464,7 @@ int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int*fd)
   else if((pfds[1].revents & POLLIN) && !(pfds[0].revents & POLLIN))
     *fd = pfds[1].fd;
   else if(ret == 2) {
-    *fd = pfds[random()%2].fd;
+    *fd = pfds[dns_random_uint32()%2].fd;
   }
   else
     *fd = -1; // should never happen
index 2f6a173ca0e224b52e866f514f5a10cfcd5e8cc0..bbb840cbdf88707d5b8c64fb42c09a2442f27ffc 100644 (file)
@@ -216,7 +216,7 @@ static void dbBench(const std::string& fname)
     while(B.get(rr)) {
       hits++;
     }
-    B.lookup(QType(QType::A), DNSName(std::to_string(random()))+domain, -1);
+    B.lookup(QType(QType::A), DNSName(std::to_string(dns_random_uint32()))+domain, -1);
     while(B.get(rr)) {
     }
     misses++;
index e94401e74f18c4efd620a73aecdedfe21a9c8837..2d5dc826f5e3fc9c770496366de2f775f7f3d848 100644 (file)
@@ -3116,7 +3116,6 @@ int main(int argc, char** argv)
 {
   g_argc = argc;
   g_argv = argv;
-  Utility::srandom();
   versionSetProduct(ProductRecursor);
   reportBasicTypes();
   reportOtherTypes();
index 26c0782e1c53f379dafe91cc77f009d9a416efc3..2fb2d00e07cac50c99f98d70cf9095f96bd7126b 100644 (file)
@@ -204,14 +204,6 @@ int Utility::gettimeofday( struct timeval *tv, void * /* tz */)
   return ::gettimeofday(tv, nullptr);
 }
 
-// Sets the random seed.
-void Utility::srandom()
-{
-  struct timeval tv;
-  gettimeofday(&tv, nullptr);
-  ::srandom(tv.tv_sec ^ tv.tv_usec ^ getpid());
-}
-
 // Writes a vector.
 int Utility::writev(int socket, const iovec *vector, size_t count )
 {
index 296449e4ef5e5bb1ce67777348a1afbb05d32123..5ddf2cc0f2b09b970e6d67c48be513f1430cf093 100644 (file)
@@ -121,9 +121,6 @@ public:
   //! Writes a vector.
   static int writev( Utility::sock_t socket, const iovec *vector, size_t count );
 
-  //! Sets the random seed.
-  static void srandom(void);
-
   //! Drops the program's group privileges.
   static void dropGroupPrivs( uid_t uid, gid_t gid );