]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
iputils.hh: Netmask: Add getBit()
authorStephan Bosch <stephan.bosch@open-xchange.com>
Mon, 30 Sep 2019 08:30:24 +0000 (10:30 +0200)
committerStephan Bosch <stephan.bosch@open-xchange.com>
Tue, 11 Feb 2020 01:01:47 +0000 (02:01 +0100)
pdns/iputils.hh

index f85355e3970e17ae5bf579e63768d19567dfc9d7..2c453071f610e4663a2eb0366c38374505b9eb4b 100644 (file)
@@ -596,6 +596,28 @@ public:
   {
     return d_network.getBits();
   }
+
+  /** Get the value of the bit at the provided bit index. When the index >= 0,
+      the index is relative to the LSB starting at index zero. When the index < 0,
+      the index is relative to the MSB starting at index -1 and counting down.
+      When the index points outside the network bits, it always yields zero.
+   */
+  bool getBit(int bit) const
+  {
+    if (bit < -d_bits)
+      return false;
+    if (bit >= 0) {
+      if(isIPv4()) {
+        if (bit >= 32 || bit < (32 - d_bits))
+          return false;
+      }
+      if(isIPv6()) {
+        if (bit >= 128 || bit < (128 - d_bits))
+          return false;
+      }
+    }
+    return d_network.getBit(bit);
+  }
 private:
   ComboAddress d_network;
   uint32_t d_mask;