]> git.ipfire.org Git - location/libloc.git/commitdiff
network: Add excluded networks to to_check list
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Nov 2020 15:41:53 +0000 (15:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Nov 2020 15:41:53 +0000 (15:41 +0000)
This is now being done immediately instead of creating a new
list which is being merged later.

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

index 38d557a4fe5e04dee787576a8c5f686d1892fcf4..7ab22f86a6b430624cfcd37e4d5f5c267a683c48 100644 (file)
@@ -640,49 +640,55 @@ ERROR:
        return r;
 }
 
-LOC_EXPORT struct loc_network_list* loc_network_exclude(
-               struct loc_network* self, struct loc_network* other) {
-       struct loc_network_list* list;
-
-#ifdef ENABLE_DEBUG
-       char* n1 = loc_network_str(self);
-       char* n2 = loc_network_str(other);
-
-       DEBUG(self->ctx, "Returning %s excluding %s...\n", n1, n2);
-
-       free(n1);
-       free(n2);
-#endif
-
+static int __loc_network_exclude_to_list(struct loc_network* self,
+               struct loc_network* other, struct loc_network_list* list) {
        // Family must match
        if (self->family != other->family) {
                DEBUG(self->ctx, "Family mismatch\n");
 
-               return NULL;
+               return 1;
        }
 
        // Other must be a subnet of self
        if (!loc_network_is_subnet(self, other)) {
                DEBUG(self->ctx, "Network %p is not contained in network %p\n", other, self);
 
-               return NULL;
+               return 1;
        }
 
        // We cannot perform this operation if both networks equal
        if (loc_network_eq(self, other)) {
                DEBUG(self->ctx, "Networks %p and %p are equal\n", self, other);
 
-               return NULL;
+               return 1;
        }
 
+       return __loc_network_exclude(self, other, list);
+}
+
+LOC_EXPORT struct loc_network_list* loc_network_exclude(
+               struct loc_network* self, struct loc_network* other) {
+       struct loc_network_list* list;
+
+#ifdef ENABLE_DEBUG
+       char* n1 = loc_network_str(self);
+       char* n2 = loc_network_str(other);
+
+       DEBUG(self->ctx, "Returning %s excluding %s...\n", n1, n2);
+
+       free(n1);
+       free(n2);
+#endif
+
        // Create a new list with the result
        int r = loc_network_list_new(self->ctx, &list);
        if (r) {
                ERROR(self->ctx, "Could not create network list: %d\n", r);
+
                return NULL;
        }
 
-       r = __loc_network_exclude(self, other, list);
+       r = __loc_network_exclude_to_list(self, other, list);
        if (r) {
                loc_network_list_unref(list);
 
@@ -709,11 +715,14 @@ LOC_EXPORT struct loc_network_list* loc_network_exclude_list(
                subnet = loc_network_list_get(list, i);
 
                // Find all excluded networks
-               struct loc_network_list* excluded = loc_network_exclude(network, subnet);
-               if (excluded) {
-                       // Add them all to the "to check" list
-                       loc_network_list_merge(to_check, excluded);
-                       loc_network_list_unref(excluded);
+               if (!loc_network_list_contains(to_check, subnet)) {
+                       r = __loc_network_exclude_to_list(network, subnet, to_check);
+                       if (r) {
+                               loc_network_list_unref(to_check);
+                               loc_network_unref(subnet);
+
+                               return NULL;
+                       }
                }
 
                // Cleanup