From: Michael Tremer Date: Fri, 20 Nov 2020 18:39:13 +0000 (+0000) Subject: network: Speed up subnet check X-Git-Tag: 0.9.5~37 X-Git-Url: http://git.ipfire.org/?p=location%2Flibloc.git;a=commitdiff_plain;h=cf8d3c6454843943e3bc81eb85522779d1c11f9b network: Speed up subnet check 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 --- diff --git a/src/network.c b/src/network.c index 4720503..e52b58c 100644 --- a/src/network.c +++ b/src/network.c @@ -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)