]> git.ipfire.org Git - location/libloc.git/commitdiff
network: Make loc_network_match_country_code match special countries
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Sep 2021 10:34:09 +0000 (10:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Sep 2021 10:34:09 +0000 (10:34 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/network.c
src/test-country.c

index 66188ba31be388e1f0480c5804d3bd91c5f11cd0..de024ccfda9980bc892374f0fd6fff9fbf806e34 100644 (file)
@@ -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]);
 }
index 3e231ced63188dc4f12a7f4e81b91d88fcc51ac8..6320c367063045c36b6baf2aa6b6408389a5bb34 100644 (file)
@@ -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);