]> git.ipfire.org Git - location/libloc.git/blobdiff - src/network.c
networks: Add function to check if network is part of a list
[location/libloc.git] / src / network.c
index 6c08070830c9aa36cf5adecc3b5b59969d68cc58..fcbdc5931b6fe2c23814e495e93e1405fb313716 100644 (file)
@@ -483,6 +483,22 @@ static int loc_network_gt(struct loc_network* self, struct loc_network* other) {
        return 0;
 }
 
+LOC_EXPORT int loc_network_overlaps(struct loc_network* self, struct loc_network* other) {
+       if (loc_network_match_address(self, &other->first_address) == 0)
+               return 1;
+
+       if (loc_network_match_address(self, &other->last_address) == 0)
+               return 1;
+
+       if (loc_network_match_address(other, &self->first_address) == 0)
+               return 1;
+
+       if (loc_network_match_address(other, &self->last_address) == 0)
+               return 1;
+
+       return 0;
+}
+
 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.
@@ -1104,6 +1120,15 @@ LOC_EXPORT struct loc_network* loc_network_list_pop(struct loc_network_list* lis
        return list->list[--list->size];
 }
 
+LOC_EXPORT int loc_network_list_contains(struct loc_network_list* list, struct loc_network* network) {
+       for (unsigned int i = 0; i < list->size; i++) {
+               if (loc_network_eq(list->list[i], network))
+                       return 1;
+       }
+
+       return 0;
+}
+
 static void loc_network_list_swap(struct loc_network_list* list, unsigned int i1, unsigned int i2) {
        // Do nothing for invalid indices
        if (i1 >= list->size || i2 >= list->size)