]> granicus.if.org Git - pdns/commitdiff
First stab at Lua pdns_features table
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 16 Aug 2019 11:30:41 +0000 (13:30 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 16 Aug 2019 14:45:54 +0000 (16:45 +0200)
pdns/lua-base4.cc
pdns/recursordist/docs/lua-scripting/features.rst [new file with mode: 0644]
pdns/recursordist/docs/lua-scripting/index.rst
regression-tests.recursor-dnssec/test_Lua.py

index a91b624b842a816087c400428c9dd8d9c49703a4..d643d4a2fe8021bae9969b6db42e4656b01b2725 100644 (file)
@@ -42,6 +42,15 @@ BaseLua4::~BaseLua4() { }
 void BaseLua4::prepareContext() {
   d_lw = std::unique_ptr<LuaContext>(new LuaContext);
 
+  // lua features available
+  d_lw->writeVariable("pdns_features",
+                      vector<pair<string, boost::variant<string,bool,int,double> > > {
+  // Add key-values pairs below. Make sure you add string values explicity converted to string.
+  // e.g. { "somekey", string("stringvalue") }
+  // Both int and double end up as a lua number type.
+      { "PR8001_devicename", true },
+    });
+  
   // dnsheader
   d_lw->registerFunction<int(dnsheader::*)()>("getID", [](dnsheader& dh) { return ntohs(dh.id); });
   d_lw->registerFunction<bool(dnsheader::*)()>("getCD", [](dnsheader& dh) { return dh.cd; });
diff --git a/pdns/recursordist/docs/lua-scripting/features.rst b/pdns/recursordist/docs/lua-scripting/features.rst
new file mode 100644 (file)
index 0000000..2000bcd
--- /dev/null
@@ -0,0 +1,16 @@
+Checking available features
+===========================
+.. versionadded:: 4.3.0
+                  
+To check if a Lua features is available, consult the global
+``pdns_features`` table. This table contains string keys with a values
+of type boolean, string or number. If a key is absent the value will
+evaluate to ``nil``, indicating the feature is not available.
+
+Currently, the following keys are defined:
+
+.. code-block:: Lua
+                
+    pdns_feature["PR8001_devicename"] = true
+
+
index 12448fc8ebb3195ed3d5a8f2f87b66d4818e4fac..45f0ddc9083ee86b0a049346ef163a299906c835 100644 (file)
@@ -26,3 +26,5 @@ For extra performance, a Just In Time compiled version of Lua called `LuaJIT <ht
     logging
     hooks
     functions
+    features
+    
index f000bcf7a9f243c9205b771b2fd70a316a068481..f31b708b24acb65c4a6da7f51a92a1cc8aed1bb3 100644 (file)
@@ -22,6 +22,7 @@ class GettagRecursorTest(RecursorTest):
       local tags = {}
       local data = {}
 
+
       -- make sure we can pass data around to the other hooks
       data['canary'] = 'from-gettag'
 
@@ -495,3 +496,33 @@ class PDNSRandomTest(RecursorTest):
         ans.add(ret.answer[0])
 
         self.assertEqual(len(ans), 2)
+
+
+class PDNSFeaturesTest(RecursorTest):
+    """Tests if pdns_features works"""
+
+    _confdir = 'pdnsfeatures'
+    _config_template = """
+    """
+    _lua_dns_script_file = """
+    function preresolve (dq)
+      dq.rcode = pdns.NOERROR
+      -- test pdns_features
+      if pdns_features['nonexistent'] ~= nil then
+        print('PDNSFeaturesTest: case 1')
+        dq.rcode = pdns.SERVFAIL
+      end
+      if not pdns_features['PR8001_devicename']  then
+        print('PDNSFeaturesTest: case 2')
+        dq.rcode = pdns.SERVFAIL
+      end
+      return true
+    end
+    """
+
+    def testFeatures(self):
+        query = dns.message.make_query('whatever.example.', 'TXT')
+        res = self.sendUDPQuery(query)
+
+        self.assertRcodeEqual(res, dns.rcode.NOERROR)
+