]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Add Lua Netmask bindings
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 3 Jan 2020 17:03:31 +0000 (18:03 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 11 Feb 2020 10:49:58 +0000 (11:49 +0100)
pdns/dnsdist-lua-bindings.cc
pdns/dnsdistdist/docs/reference/config.rst
pdns/dnsdistdist/docs/reference/netmask.rst [new file with mode: 0644]

index a24df185bfa72dd8f2c3557f3498c9a92df56dd1..60717e174dfa891dfa3f14c6b378ae18e9efd732 100644 (file)
@@ -258,6 +258,34 @@ void setupLuaBindings(bool client)
   });
   g_lua.registerFunction("check",(bool (SuffixMatchNode::*)(const DNSName&) const) &SuffixMatchNode::check);
 
+  /* Netmask */
+  g_lua.writeFunction("newNetmask", [](boost::variant<std::string,ComboAddress> s, boost::optional<uint8_t> bits) {
+    if (s.type() == typeid(ComboAddress)) {
+      auto ca = boost::get<ComboAddress>(s);
+      if (bits) {
+        return Netmask(ca, *bits);
+      }
+      return Netmask(ca);
+    }
+    else if (s.type() == typeid(std::string)) {
+      auto str = boost::get<std::string>(s);
+      return Netmask(str);
+    }
+    throw std::runtime_error("Invalid parameter passed to 'newNetmask()'");
+  });
+  g_lua.registerFunction("empty", &Netmask::empty);
+  g_lua.registerFunction("getBits", &Netmask::getBits);
+  g_lua.registerFunction<ComboAddress(Netmask::*)()>("getNetwork", [](const Netmask& nm) { return nm.getNetwork(); } ); // const reference makes this necessary
+  g_lua.registerFunction<ComboAddress(Netmask::*)()>("getMaskedNetwork", [](const Netmask& nm) { return nm.getMaskedNetwork(); } );
+  g_lua.registerFunction("isIpv4", &Netmask::isIPv4);
+  g_lua.registerFunction("isIPv4", &Netmask::isIPv4);
+  g_lua.registerFunction("isIpv6", &Netmask::isIPv6);
+  g_lua.registerFunction("isIPv6", &Netmask::isIPv6);
+  g_lua.registerFunction("match", (bool (Netmask::*)(const string&) const)&Netmask::match);
+  g_lua.registerFunction("toString", &Netmask::toString);
+  g_lua.registerEqFunction(&Netmask::operator==);
+  g_lua.registerToStringFunction(&Netmask::toString);
+
   /* NetmaskGroup */
   g_lua.writeFunction("newNMG", []() { return NetmaskGroup(); });
   g_lua.registerFunction<void(NetmaskGroup::*)(const std::string&mask)>("addMask", [](NetmaskGroup&nmg, const std::string& mask)
index a0cdeb9baba262c84b1c2b72c4d8df0cc76f4ec0..ff139f48bf8fc5c89a93d626d0afa39bb4987866 100644 (file)
@@ -18,6 +18,7 @@ Within dnsdist several core object types exist:
 * :class:`Server`: generated with :func:`newServer`, represents a downstream server
 * :class:`ComboAddress`: represents an IP address and port
 * :class:`DNSName`: represents a domain name
+* :class:`Netmask`: represents a Netmask
 * :class:`NetmaskGroup`: represents a group of netmasks
 * :class:`QPSLimiter`: implements a QPS-based filter
 * :class:`SuffixMatchNode`: represents a group of domain suffixes for rapid testing of membership
diff --git a/pdns/dnsdistdist/docs/reference/netmask.rst b/pdns/dnsdistdist/docs/reference/netmask.rst
new file mode 100644 (file)
index 0000000..411d0a7
--- /dev/null
@@ -0,0 +1,53 @@
+Netmask
+=======
+
+.. function:: newNetmask(str) -> Netmask
+              newNetmask(ca, bits) -> Netmask
+
+  .. versionadded:: 1.5.0
+
+  Returns a Netmask
+
+  :param string str: A netmask, like ``192.0.2.0/24``.
+  :param ComboAddress ca: A :class:`ComboAddress`.
+  :param int bits: The number of bits in this netmask.
+
+.. class:: Netmask
+
+  .. versionadded:: 1.5.0
+
+   Represents a netmask.
+
+  .. method:: Netmask:getBits() -> int
+
+    Return the number of bits of this netmask, for example ``24`` for ``192.0.2.0/24``.
+
+  .. method:: Netmask:getMaskedNetwork() -> ComboAddress
+
+    Return a :class:`ComboAddress` object representing the base network of this netmask object after masking any additional bits if necessary (for example ``192.0.2.0`` if the netmask was constructed with ``newNetmask('192.0.2.1/24')).
+
+  .. method:: Netmask:isEmpty() -> bool
+
+    Return true if the netmask is empty, meaning that the netmask has not been set to a proper value.
+
+  .. method:: Netmask:isIPv4() -> bool
+
+    Return true if the netmask is an IPv4 one.
+
+  .. method:: Netmask:isIPv6() -> bool
+
+    Return true if the netmask is an IPv6 one.
+
+  .. method:: Netmask:getNetwork() -> ComboAddress
+
+    Return a :class:`ComboAddress` object representing the base network of this netmask object.
+
+  .. method:: Netmask:match(str) -> bool
+
+    Return true if the address passed in the ``str`` parameter belongs to this netmask.
+
+    :param string str: A network address, like ``192.0.2.0``.
+
+  .. method:: Netmask:toString() -> string
+
+    Return a string representation of the netmask, for example ``192.0.2.0/24``.