return AF_INET6;
}
+static inline int loc_address_family_bit_length(const int family) {
+ switch (family) {
+ case AF_INET6:
+ return 128;
+
+ case AF_INET:
+ return 32;
+
+ default:
+ return -1;
+ }
+}
+
static inline int loc_address_all_zeroes(const struct in6_addr* address) {
struct in6_addr all_zeroes = IN6ADDR_ANY_INIT;
return 1;
}
- int family = loc_address_family(first);
+ const int family1 = loc_address_family(first);
+ const int family2 = loc_address_family(last);
- // Families must match
- if (family != loc_address_family(last)) {
+ // Check if address families match
+ if (family1 != family2) {
ERROR(ctx, "Address families do not match\n");
errno = EINVAL;
return 1;
struct loc_network* network = NULL;
struct in6_addr start = *first;
+ const int family_bit_length = loc_address_family_bit_length(family1);
+
while (in6_addr_cmp(&start, last) <= 0) {
struct in6_addr num;
+ int bits1;
// Find the number of trailing zeroes of the start address
- int bits1 = loc_address_count_trailing_zero_bits(&start);
+ if (loc_address_all_zeroes(&start))
+ bits1 = family_bit_length;
+ else {
+ bits1 = loc_address_count_trailing_zero_bits(&start);
+ if (bits1 > family_bit_length)
+ bits1 = family_bit_length;
+ }
// Subtract the start address from the last address and add one
// (i.e. how many addresses are in this network?)