]> git.ipfire.org Git - location/libloc.git/commitdiff
country: Add function that returns flags for special country
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Sep 2021 10:19:46 +0000 (10:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Sep 2021 10:19:46 +0000 (10:19 +0000)
This allows for libloc to be better integrated into third-party
software.

If we would add extra flags, we would be able to extend it in libloc
without touching any third-party software.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/country.c
src/libloc.sym
src/libloc/country.h
src/test-country.c

index 957ce2e3f5643acaccfb34e96074f763c6b0e044..3ea04cc45c2f32656dc53cca2ab52e2b9299858b 100644 (file)
 #include <libloc/libloc.h>
 #include <libloc/compat.h>
 #include <libloc/country.h>
+#include <libloc/network.h>
 #include <libloc/private.h>
 
+static const struct loc_special_country {
+       const char code[3];
+       enum loc_network_flags flags;
+} loc_special_countries[] = {
+       { "A1", LOC_NETWORK_FLAG_ANONYMOUS_PROXY },
+       { "A2", LOC_NETWORK_FLAG_SATELLITE_PROVIDER },
+       { "A3", LOC_NETWORK_FLAG_ANYCAST },
+       { "XD", LOC_NETWORK_FLAG_DROP },
+       { "", 0 },
+};
+
 struct loc_country {
        struct loc_ctx* ctx;
        int refcount;
@@ -200,3 +212,20 @@ LOC_EXPORT int loc_country_code_is_valid(const char* cc) {
        // Looks valid
        return 1;
 }
+
+LOC_EXPORT int loc_country_special_code_to_flag(const char* cc) {
+       // Check if we got some input
+       if (!cc || !*cc) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       // Return flags for any known special country
+       for (const struct loc_special_country* country = loc_special_countries;
+                       country->flags; country++) {
+               if (strcmp(country->code, cc) == 0)
+                       return country->flags;
+       }
+
+       return 0;
+}
index a5b42ad49608e9d46ddf29d3fd1a6ef4245c5122..df6f4d804bcdf53b8cb684cfc576535bb8e0a0b6 100644 (file)
@@ -39,6 +39,7 @@ global:
        loc_country_ref;
        loc_country_set_continent_code;
        loc_country_set_name;
+       loc_country_special_code_to_flag;
        loc_country_unref;
 
        # Country List
index 4cc3a059e1bfcf6754ef819df7794e96a41e1fb6..98ee8cb3b8e5857ce5bd3da474701cd3aae3438f 100644 (file)
@@ -37,6 +37,7 @@ int loc_country_set_name(struct loc_country* country, const char* name);
 int loc_country_cmp(struct loc_country* country1, struct loc_country* country2);
 
 int loc_country_code_is_valid(const char* cc);
+int loc_country_special_code_to_flag(const char* cc);
 
 #ifdef LIBLOC_PRIVATE
 
index 25e41651340a894c8214b50f1e95cbb1045cd0e7..fabdbc02698b1fc35d2a164b34417c9e2037cb32 100644 (file)
@@ -29,6 +29,7 @@
 
 int main(int argc, char** argv) {
        struct loc_country* country;
+       int flag;
        int err;
 
        // Check some valid country codes
@@ -43,6 +44,48 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
+       // Test special country codes
+       flag = loc_country_special_code_to_flag("XX");
+       if (flag) {
+               fprintf(stderr, "Unexpectedly received a flag for XX: %d\n", flag);
+               exit(EXIT_FAILURE);
+       }
+
+       // A1
+       flag = loc_country_special_code_to_flag("A1");
+       if (flag != LOC_NETWORK_FLAG_ANONYMOUS_PROXY) {
+               fprintf(stderr, "Got a wrong flag for A1: %d\n", flag);
+               exit(EXIT_FAILURE);
+       }
+
+       // A2
+       flag = loc_country_special_code_to_flag("A2");
+       if (flag != LOC_NETWORK_FLAG_SATELLITE_PROVIDER) {
+               fprintf(stderr, "Got a wrong flag for A2: %d\n", flag);
+               exit(EXIT_FAILURE);
+       }
+
+       // A3
+       flag = loc_country_special_code_to_flag("A3");
+       if (flag != LOC_NETWORK_FLAG_ANYCAST) {
+               fprintf(stderr, "Got a wrong flag for A3: %d\n", flag);
+               exit(EXIT_FAILURE);
+       }
+
+       // XD
+       flag = loc_country_special_code_to_flag("XD");
+       if (flag != LOC_NETWORK_FLAG_DROP) {
+               fprintf(stderr, "Got a wrong flag for XD: %d\n", flag);
+               exit(EXIT_FAILURE);
+       }
+
+       // NULL input
+       flag = loc_country_special_code_to_flag(NULL);
+       if (flag >= 0) {
+               fprintf(stderr, "loc_country_special_code_to_flag didn't throw an error for NULL\n");
+               exit(EXIT_FAILURE);
+       }
+
        struct loc_ctx* ctx;
        err = loc_new(&ctx);
        if (err < 0)