Commit 88a3db10 authored by Michael Tremer's avatar Michael Tremer

network-list: Cap prefix length based on family

Signed-off-by: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent 9306c68d
......@@ -103,6 +103,19 @@ static inline int loc_address_family(const struct in6_addr* address) {
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;
......
......@@ -307,10 +307,11 @@ int loc_network_list_summarize(struct loc_ctx* ctx,
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;
......@@ -326,11 +327,20 @@ int loc_network_list_summarize(struct loc_ctx* ctx,
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?)
......
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