#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;
// 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;
+}
loc_country_ref;
loc_country_set_continent_code;
loc_country_set_name;
+ loc_country_special_code_to_flag;
loc_country_unref;
# Country List
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
int main(int argc, char** argv) {
struct loc_country* country;
+ int flag;
int err;
// Check some valid country codes
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)