Commit 0b27a567 authored by Michael Tremer's avatar Michael Tremer

network: Make loc_network_match_country_code match special countries

Signed-off-by: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent e8ebd079
...@@ -387,10 +387,18 @@ LOC_EXPORT int loc_network_set_country_code(struct loc_network* network, const c ...@@ -387,10 +387,18 @@ LOC_EXPORT int loc_network_set_country_code(struct loc_network* network, const c
} }
LOC_EXPORT int loc_network_match_country_code(struct loc_network* network, const char* country_code) { LOC_EXPORT int loc_network_match_country_code(struct loc_network* network, const char* country_code) {
// Search for any special flags
const int flag = loc_country_special_code_to_flag(country_code);
// If we found a flag, we will return whether it is set or not
if (flag)
return loc_network_has_flag(network, flag);
// Check country code // Check country code
if (!loc_country_code_is_valid(country_code)) if (!loc_country_code_is_valid(country_code))
return -EINVAL; return -EINVAL;
// Check for an exact match
return (network->country_code[0] == country_code[0]) return (network->country_code[0] == country_code[0])
&& (network->country_code[1] == country_code[1]); && (network->country_code[1] == country_code[1]);
} }
......
...@@ -151,6 +151,42 @@ int main(int argc, char** argv) { ...@@ -151,6 +151,42 @@ int main(int argc, char** argv) {
} }
loc_country_unref(country); loc_country_unref(country);
struct loc_network* network = NULL;
// Create a test network
err = loc_network_new_from_string(ctx, &network, "2001:db8::/64");
if (err) {
fprintf(stderr, "Could not create network: %m\n");
exit(EXIT_FAILURE);
}
// Set country code & flag
loc_network_set_country_code(network, "YY");
loc_network_set_flag(network, LOC_NETWORK_FLAG_ANONYMOUS_PROXY);
// Check if this network matches its own country code
err = loc_network_match_country_code(network, "YY");
if (!err) {
fprintf(stderr, "Network does not match its own country code\n");
exit(EXIT_FAILURE);
}
// Check if this network matches the special country code
err = loc_network_match_country_code(network, "A1");
if (!err) {
fprintf(stderr, "Network does not match the special country code A1\n");
exit(EXIT_FAILURE);
}
// Check if this network does not match another special country code
err = loc_network_match_country_code(network, "A2");
if (err) {
fprintf(stderr, "Network matches another special country code A2\n");
exit(EXIT_FAILURE);
}
loc_network_unref(network);
loc_database_unref(db); loc_database_unref(db);
loc_unref(ctx); loc_unref(ctx);
fclose(f); fclose(f);
......
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