]> git.ipfire.org Git - location/libloc.git/commitdiff
network: Speed up subnet check
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 20 Nov 2020 18:39:13 +0000 (18:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 20 Nov 2020 18:39:13 +0000 (18:39 +0000)
There is no point in checking different address families
with each other and we do not need to compare addresses
when the prefix of the subnet does not fit into the
network to check.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/network.c

index 4720503cef893e26a2edd1a827e0f3105cad36e9..e52b58c63af2ec4ef69cfa31a94956980c170c99 100644 (file)
@@ -501,6 +501,14 @@ LOC_EXPORT int loc_network_overlaps(struct loc_network* self, struct loc_network
 }
 
 LOC_EXPORT int loc_network_is_subnet(struct loc_network* self, struct loc_network* other) {
+       // Check family
+       if (self->family != other->family)
+               return 0;
+
+       // The prefix must be smaller (this avoids the more complex comparisons later)
+       if (self->prefix > other->prefix)
+               return 0;
+
        // If the start address of the other network is smaller than this network,
        // it cannot be a subnet.
        if (in6_addr_cmp(&self->first_address, &other->first_address) > 0)