Commit 3eb1eed6 authored by Michael Tremer's avatar Michael Tremer

country: Add function that returns flags for special country

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: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent 843ad331
......@@ -21,8 +21,20 @@
#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;
}
......@@ -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
......
......@@ -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
......
......@@ -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)
......
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