From: Stephan Bosch Date: Mon, 30 Sep 2019 08:30:24 +0000 (+0200) Subject: iputils.hh: Netmask: Add getBit() X-Git-Tag: auth-4.3.0-beta2~20^2~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d8c1d77bfd62353824748713e4f30c866213f89;p=thirdparty%2Fpdns.git iputils.hh: Netmask: Add getBit() --- diff --git a/pdns/iputils.hh b/pdns/iputils.hh index f85355e397..2c453071f6 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -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;