From: Michael Tremer Date: Tue, 24 Nov 2020 15:41:53 +0000 (+0000) Subject: network: Add excluded networks to to_check list X-Git-Tag: 0.9.5~31 X-Git-Url: http://git.ipfire.org/?p=location%2Flibloc.git;a=commitdiff_plain;h=c650008e2dd9af5fd1eadba6354aa0b615047f84 network: Add excluded networks to to_check list This is now being done immediately instead of creating a new list which is being merged later. Signed-off-by: Michael Tremer --- diff --git a/src/network.c b/src/network.c index 38d557a..7ab22f8 100644 --- a/src/network.c +++ b/src/network.c @@ -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