]> git.ipfire.org Git - location/libloc.git/commitdiff
network-list: Cap prefix length based on family
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Mar 2022 11:27:11 +0000 (11:27 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Mar 2022 11:27:11 +0000 (11:27 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libloc/network.h
src/network-list.c

index 857767f88cdff3005e4ea0c543f3e76163c6d421..c937b745472c88759bb46dc445be10ec3cf4e917 100644 (file)
@@ -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;
 
index b8a485db873c45b821a667237559c4e9ab31873c..0157d44a4fdcbd231ebbc7765c800dbe8246c720 100644 (file)
@@ -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?)