]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Lua: expose `dns_random` as `pdnsrandom`
authorPieter Lexis <pieter.lexis@powerdns.com>
Mon, 18 Feb 2019 12:29:52 +0000 (13:29 +0100)
committerPieter Lexis <pieter.lexis@powerdns.com>
Mon, 18 Feb 2019 12:29:52 +0000 (13:29 +0100)
Fixes the usecase in #6853

docs/lua-records/reference/misc.rst
pdns/lua-base4.cc
pdns/recursordist/docs/lua-scripting/functions.rst
regression-tests.recursor-dnssec/test_Lua.py

index 247f25049f3f6c4fa6365edc8dfe0947d20f6fb6..c1029c9526a442487bedaa00993e4b0418e4608a 100644 (file)
@@ -20,3 +20,9 @@ Other functions
    - `pdns.loglevels.Notice`
    - `pdns.loglevels.Warning`
    - `pdns.loglevels.Error`
+
+.. function:: pdnsrandom([maximum])
+
+  Get a random number.
+
+  :param int maximum: The largest number to return. This is 2^32 by default.
index 34be790dad9d03554059b42258485a67077f6457..39dbb391c3abdcad7f65e15dbd12bb6e32459f25 100644 (file)
@@ -10,6 +10,7 @@
 #include "namespaces.hh"
 #include "ednssubnet.hh"
 #include "lua-base4.hh"
+#include "dns_random.hh"
 
 BaseLua4::BaseLua4() {
 }
@@ -190,6 +191,7 @@ void BaseLua4::prepareContext() {
 
   // pdnsload
   d_lw->writeFunction("pdnslog", [](const std::string& msg, boost::optional<int> loglevel) { g_log << (Logger::Urgency)loglevel.get_value_or(Logger::Warning) << msg<<endl; });
+  d_lw->writeFunction("pdnsrandom", [](boost::optional<uint32_t> maximum) { return dns_random(maximum.get_value_or(0xffffffff)); });
 
   // certain constants
   d_pd.push_back({"PASS", (int)PolicyDecision::PASS});
index 356b2f34d93ad1474e91c44e860711c058e1fd63..df6ca553c554371dd2b2ee4ebb8b327d7f0a3ace 100644 (file)
@@ -15,3 +15,9 @@ These are some functions that don't really have a place in one of the other cate
 .. function:: getRecursorThreadId() -> int
 
   returns an unsigned integer identifying the thread handling the current request.
+
+.. function:: pdnsrandom([maximum])
+
+  Get a random number.
+
+  :param int maximum: The largest number to return. This is 2^32 by default.
index e126d91d02cf4272a8e4cade6c922e2d980b9176..f000bcf7a9f243c9205b771b2fd70a316a068481 100644 (file)
@@ -468,3 +468,30 @@ class DNS64Test(RecursorTest):
             self.assertEqual(len(res.answer), 2)
             self.assertEqual(len(res.authority), 0)
             self.assertResponseMatches(query, expected, res)
+
+
+class PDNSRandomTest(RecursorTest):
+    """Tests if pdnsrandom works"""
+
+    _confdir = 'pdnsrandom'
+    _config_template = """
+    """
+    _lua_dns_script_file = """
+    function preresolve (dq)
+      dq.rcode = pdns.NOERROR
+      dq:addAnswer(pdns.TXT, pdnsrandom())
+      return true
+    end
+    """
+
+    def testRandom(self):
+        query = dns.message.make_query('whatever.example.', 'TXT')
+
+        ans = set()
+
+        ret = self.sendUDPQuery(query)
+        ans.add(ret.answer[0])
+        ret = self.sendUDPQuery(query)
+        ans.add(ret.answer[0])
+
+        self.assertEqual(len(ans), 2)