Commit 964633d9 authored by Michael Tremer's avatar Michael Tremer

export: Fix filtering logic

It is possible to filter for what kind of network should be exported.

This worked well when the filter list only contained country codes, or
when it only contained ASNs. If there was a mix, only networks that
match both (i.e. virtually nothing) matched.

This patch fixes that we will use for either of them.
Signed-off-by: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent 2ad36187
...@@ -1160,41 +1160,41 @@ static int loc_database_enumerator_stack_push_node( ...@@ -1160,41 +1160,41 @@ static int loc_database_enumerator_stack_push_node(
return 0; return 0;
} }
static int loc_database_enumerator_filter_network( static int loc_database_enumerator_match_network(
struct loc_database_enumerator* enumerator, struct loc_network* network) { struct loc_database_enumerator* enumerator, struct loc_network* network) {
// Skip if the family does not match // If family is set, it must match
if (enumerator->family && loc_network_address_family(network) != enumerator->family) { if (enumerator->family && loc_network_address_family(network) != enumerator->family) {
DEBUG(enumerator->ctx, "Filtered network %p because of family not matching\n", network); DEBUG(enumerator->ctx, "Filtered network %p because of family not matching\n", network);
return 1; return 0;
} }
// Skip if the country code does not match // Check if the country code matches
if (enumerator->countries && !loc_country_list_empty(enumerator->countries)) { if (enumerator->countries && !loc_country_list_empty(enumerator->countries)) {
const char* country_code = loc_network_get_country_code(network); const char* country_code = loc_network_get_country_code(network);
if (!loc_country_list_contains_code(enumerator->countries, country_code)) { if (loc_country_list_contains_code(enumerator->countries, country_code)) {
DEBUG(enumerator->ctx, "Filtered network %p because of country code not matching\n", network); DEBUG(enumerator->ctx, "Matched network %p because of its country code\n", network);
return 1; return 1;
} }
} }
// Skip if the ASN does not match // Check if the ASN matches
if (enumerator->asns && !loc_as_list_empty(enumerator->asns)) { if (enumerator->asns && !loc_as_list_empty(enumerator->asns)) {
uint32_t asn = loc_network_get_asn(network); uint32_t asn = loc_network_get_asn(network);
if (!loc_as_list_contains_number(enumerator->asns, asn)) { if (loc_as_list_contains_number(enumerator->asns, asn)) {
DEBUG(enumerator->ctx, "Filtered network %p because of ASN not matching\n", network); DEBUG(enumerator->ctx, "Matched network %p because of its ASN\n", network);
return 1; return 1;
} }
} }
// Skip if flags do not match // Check if flags match
if (enumerator->flags && !loc_network_has_flag(network, enumerator->flags)) { if (enumerator->flags && loc_network_has_flag(network, enumerator->flags)) {
DEBUG(enumerator->ctx, "Filtered network %p because of flags not matching\n", network); DEBUG(enumerator->ctx, "Matched network %p because of its flags\n", network);
return 1; return 1;
} }
// Do not filter // Not a match
return 0; return 0;
} }
...@@ -1208,15 +1208,13 @@ static int __loc_database_enumerator_next_network( ...@@ -1208,15 +1208,13 @@ static int __loc_database_enumerator_next_network(
if (!*network) if (!*network)
break; break;
// Throw away any networks by filter // Return everything if filter isn't enabled, or only return matches
if (filter && loc_database_enumerator_filter_network(enumerator, *network)) { if (!filter || loc_database_enumerator_match_network(enumerator, *network))
loc_network_unref(*network); return 0;
*network = NULL;
continue;
}
// Return result // Throw away anything that doesn't match
return 0; loc_network_unref(*network);
*network = NULL;
} }
DEBUG(enumerator->ctx, "Called with a stack of %u nodes\n", DEBUG(enumerator->ctx, "Called with a stack of %u nodes\n",
...@@ -1273,19 +1271,13 @@ static int __loc_database_enumerator_next_network( ...@@ -1273,19 +1271,13 @@ static int __loc_database_enumerator_next_network(
if (r) if (r)
return r; return r;
// Return all networks when the filter is disabled // Return all networks when the filter is disabled, or check for match
if (!filter) if (!filter || loc_database_enumerator_match_network(enumerator, *network))
return 0; return 0;
// Check if we are interested in this network // Does not seem to be a match, so we cleanup and move on
if (loc_database_enumerator_filter_network(enumerator, *network)) { loc_network_unref(*network);
loc_network_unref(*network); *network = NULL;
*network = NULL;
continue;
}
return 0;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment