From: Pieter Lexis Date: Mon, 18 Feb 2019 12:29:52 +0000 (+0100) Subject: Lua: expose `dns_random` as `pdnsrandom` X-Git-Tag: auth-4.2.0-beta1~13^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2c41983154a2c0c89345a69692c1df02f64416a8;p=thirdparty%2Fpdns.git Lua: expose `dns_random` as `pdnsrandom` Fixes the usecase in #6853 --- diff --git a/docs/lua-records/reference/misc.rst b/docs/lua-records/reference/misc.rst index 247f25049f..c1029c9526 100644 --- a/docs/lua-records/reference/misc.rst +++ b/docs/lua-records/reference/misc.rst @@ -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. diff --git a/pdns/lua-base4.cc b/pdns/lua-base4.cc index 34be790dad..39dbb391c3 100644 --- a/pdns/lua-base4.cc +++ b/pdns/lua-base4.cc @@ -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 loglevel) { g_log << (Logger::Urgency)loglevel.get_value_or(Logger::Warning) << msg<writeFunction("pdnsrandom", [](boost::optional maximum) { return dns_random(maximum.get_value_or(0xffffffff)); }); // certain constants d_pd.push_back({"PASS", (int)PolicyDecision::PASS}); diff --git a/pdns/recursordist/docs/lua-scripting/functions.rst b/pdns/recursordist/docs/lua-scripting/functions.rst index 356b2f34d9..df6ca553c5 100644 --- a/pdns/recursordist/docs/lua-scripting/functions.rst +++ b/pdns/recursordist/docs/lua-scripting/functions.rst @@ -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. diff --git a/regression-tests.recursor-dnssec/test_Lua.py b/regression-tests.recursor-dnssec/test_Lua.py index e126d91d02..f000bcf7a9 100644 --- a/regression-tests.recursor-dnssec/test_Lua.py +++ b/regression-tests.recursor-dnssec/test_Lua.py @@ -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)