From 0b27a567306f6aa62eb56ed7479765d777438850 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 30 Sep 2021 10:34:09 +0000 Subject: [PATCH] network: Make loc_network_match_country_code match special countries Signed-off-by: Michael Tremer --- src/network.c | 8 ++++++++ src/test-country.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/network.c b/src/network.c index 66188ba..de024cc 100644 --- a/src/network.c +++ b/src/network.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) { + // 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 if (!loc_country_code_is_valid(country_code)) return -EINVAL; + // Check for an exact match return (network->country_code[0] == country_code[0]) && (network->country_code[1] == country_code[1]); } diff --git a/src/test-country.c b/src/test-country.c index 3e231ce..6320c36 100644 --- a/src/test-country.c +++ b/src/test-country.c @@ -151,6 +151,42 @@ int main(int argc, char** argv) { } 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_unref(ctx); fclose(f); -- 2.39.2