]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
network: Add new subnet function
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 12 Nov 2020 20:00:09 +0000 (20:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 12 Nov 2020 20:00:09 +0000 (20:00 +0000)
The old one is too difficult to use in terms of order
of input parameters and return value.

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

index c0b6b1f1bcc4a79ba80912903fa89b7ae4364806..5392437dd2300a066ce84a5e0cbb08d08e63a515 100644 (file)
@@ -87,6 +87,7 @@ global:
        loc_network_get_asn;
        loc_network_get_country_code;
        loc_network_has_flag;
+       loc_network_is_subnet;
        loc_network_is_subnet_of;
        loc_network_match_asn;
        loc_network_match_country_code;
index e30d91c9cc4a171edf38dc0dae1a6b705cdbff63..2154cdc9b3ebca7b99b0767571906e5ae62990c4 100644 (file)
@@ -56,6 +56,7 @@ int loc_network_match_flag(struct loc_network* network, uint32_t flag);
 
 int loc_network_eq(struct loc_network* self, struct loc_network* other);
 int loc_network_overlaps(struct loc_network* self, struct loc_network* other);
+int loc_network_is_subnet(struct loc_network* self, struct loc_network* other);
 int loc_network_is_subnet_of(struct loc_network* self, struct loc_network* other);
 struct loc_network_list* loc_network_subnets(struct loc_network* network);
 struct loc_network_list* loc_network_exclude(
index d41e87300acf5f75b155972a966e3b6140d99133..5719111252e31ad2f6e417dfbddedab4c3f58241 100644 (file)
@@ -479,6 +479,21 @@ LOC_EXPORT int loc_network_overlaps(struct loc_network* self, struct loc_network
        return 0;
 }
 
+LOC_EXPORT int loc_network_is_subnet(struct loc_network* self, struct loc_network* other) {
+       // 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)
+               return 0;
+
+       // If the end address of the other network is greater than this network,
+       // it cannot be a subnet.
+       if (in6_addr_cmp(&self->last_address, &other->last_address) > 0)
+               return 0;
+
+       return 1;
+}
+
+// XXX DEPRECATED - I find this too difficult to use
 LOC_EXPORT int loc_network_is_subnet_of(struct loc_network* self, struct loc_network* other) {
        // If the start address of the other network is smaller than this network,
        // it cannot be a subnet.