From: Stephan Bosch Date: Mon, 30 Sep 2019 08:30:24 +0000 (+0200) Subject: iputils.hh: ComboAddress: Add getBit() X-Git-Tag: auth-4.3.0-beta2~20^2~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cdd23120567e97b83c7646ea81e03f2bd272067b;p=thirdparty%2Fpdns.git iputils.hh: ComboAddress: Add getBit() --- diff --git a/pdns/iputils.hh b/pdns/iputils.hh index f0c360bcb4..245beccab7 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -326,6 +326,42 @@ union ComboAddress { return 128; return 0; } + /** 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. + */ + bool getBit(int index) const + { + if(isIPv4()) { + if (index >= 32) + return false; + if (index < 0) { + if (index < -32) + return false; + index = 32 + index; + } + + uint32_t s_addr = ntohl(sin4.sin_addr.s_addr); + + return ((s_addr & (1<= 128) + return false; + if (index < 0) { + if (index < -128) + return false; + index = 128 + index; + } + + uint8_t *s_addr = (uint8_t*)sin6.sin6_addr.s6_addr; + uint8_t byte_idx = index / 8; + uint8_t bit_idx = index % 8; + + return ((s_addr[15-byte_idx] & (1 << bit_idx)) != 0x00); + } + return false; + } }; /** This exception is thrown by the Netmask class and by extension by the NetmaskGroup class */