Commit b074be0b authored by Michael Tremer's avatar Michael Tremer

network: Implement bit length function for IPv4

Signed-off-by: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent 5559e086
...@@ -94,7 +94,7 @@ static inline struct in6_addr loc_prefix_to_bitmask(const unsigned int prefix) { ...@@ -94,7 +94,7 @@ static inline struct in6_addr loc_prefix_to_bitmask(const unsigned int prefix) {
return bitmask; return bitmask;
} }
static inline unsigned int loc_address_bit_length(const struct in6_addr* address) { static inline unsigned int __loc_address6_bit_length(const struct in6_addr* address) {
unsigned int length = 128; unsigned int length = 128;
for (int octet = 0; octet <= 15; octet++) { for (int octet = 0; octet <= 15; octet++) {
...@@ -108,6 +108,27 @@ static inline unsigned int loc_address_bit_length(const struct in6_addr* address ...@@ -108,6 +108,27 @@ static inline unsigned int loc_address_bit_length(const struct in6_addr* address
return length; return length;
} }
static inline unsigned int __loc_address4_bit_length(const struct in6_addr* address) {
unsigned int length = 32;
for (int octet = 12; octet <= 15; octet++) {
if (address->s6_addr[octet]) {
length -= __builtin_clz(address->s6_addr[octet]) - 24;
break;
} else
length -= 8;
}
return length;
}
static inline unsigned int loc_address_bit_length(const struct in6_addr* address) {
if (IN6_IS_ADDR_V4MAPPED(address))
return __loc_address4_bit_length(address);
else
return __loc_address6_bit_length(address);
}
static inline struct in6_addr loc_address_and( static inline struct in6_addr loc_address_and(
const struct in6_addr* address, const struct in6_addr* bitmask) { const struct in6_addr* address, const struct in6_addr* bitmask) {
struct in6_addr a; struct in6_addr a;
......
...@@ -311,6 +311,8 @@ int main(int argc, char** argv) { ...@@ -311,6 +311,8 @@ int main(int argc, char** argv) {
} bit_length_tests[] = { } bit_length_tests[] = {
{ "::/0", 0 }, { "::/0", 0 },
{ "2001::/128", 126 }, { "2001::/128", 126 },
{ "1.0.0.0/32", 25 },
{ "255.255.255.255/32", 32 },
{ NULL, 0, }, { NULL, 0, },
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment