Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
- // Country codes must be two characters
- if (strlen(country_code) != 2)
- return -EINVAL;
-
// Treat A1, A2, A3 as special country codes,
// but perform search for flags instead
if (strcmp(country_code, "A1") == 0) {
// Treat A1, A2, A3 as special country codes,
// but perform search for flags instead
if (strcmp(country_code, "A1") == 0) {
LOC_NETWORK_FLAG_ANYCAST);
}
LOC_NETWORK_FLAG_ANYCAST);
}
+ // Country codes must be two characters
+ if (!loc_country_code_is_valid(country_code))
+ return -EINVAL;
+
for (unsigned int i = 0; i < 3; i++) {
enumerator->country_code[i] = country_code[i];
}
for (unsigned int i = 0; i < 3; i++) {
enumerator->country_code[i] = country_code[i];
}
int loc_country_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool,
struct loc_country** country, const struct loc_database_country_v0* dbobj);
int loc_country_to_database_v0(struct loc_country* country,
struct loc_stringpool* pool, struct loc_database_country_v0* dbobj);
int loc_country_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool,
struct loc_country** country, const struct loc_database_country_v0* dbobj);
int loc_country_to_database_v0(struct loc_country* country,
struct loc_stringpool* pool, struct loc_database_country_v0* dbobj);
+static inline int loc_country_code_is_valid(const char* cc) {
+ // It cannot be NULL
+ if (!cc || !*cc)
+ return 0;
+
+ // It must be 2 characters long
+ if (strlen(cc) != 2)
+ return 0;
+
+ // It must only contain A-Z
+ for (unsigned int i = 0; i < 2; i++) {
+ if (cc[i] < 'A' || cc[i] > 'Z')
+ return 0;
+ }
+
+ // Looks valid
+ return 1;
+}
+
static inline void loc_country_copy_code(char* dst, const char* src) {
for (unsigned int i = 0; i < 2; i++) {
dst[i] = src[i];
static inline void loc_country_copy_code(char* dst, const char* src) {
for (unsigned int i = 0; i < 2; i++) {
dst[i] = src[i];
#include <string.h>
#include <loc/libloc.h>
#include <string.h>
#include <loc/libloc.h>
+#include <loc/country.h>
#include <loc/network.h>
#include <loc/private.h>
#include <loc/network.h>
#include <loc/private.h>
- // Country codes must be two characters
- if (strlen(country_code) != 2)
+ // Check country code
+ if (!loc_country_code_is_valid(country_code))
return -EINVAL;
for (unsigned int i = 0; i < 3; i++) {
return -EINVAL;
for (unsigned int i = 0; i < 3; i++) {
}
LOC_EXPORT int loc_network_match_country_code(struct loc_network* network, const char* country_code) {
}
LOC_EXPORT int loc_network_match_country_code(struct loc_network* network, const char* country_code) {
- // Country codes must be two characters
- if (strlen(country_code) != 2)
+ // Check country code
+ if (!loc_country_code_is_valid(country_code))
return -EINVAL;
return (network->country_code[0] == country_code[0])
return -EINVAL;
return (network->country_code[0] == country_code[0])
#include <string.h>
#include <loc/libloc.h>
#include <string.h>
#include <loc/libloc.h>
+#include <loc/country.h>
#include <loc/database.h>
#include <loc/network.h>
#include <loc/writer.h>
#include <loc/database.h>
#include <loc/network.h>
#include <loc/writer.h>
struct loc_country* country;
int err;
struct loc_country* country;
int err;
+ // Check some valid country codes
+ if (!loc_country_code_is_valid("XX")) {
+ fprintf(stderr, "Valid country code detected as invalid: %s\n", "XX");
+ exit(EXIT_FAILURE);
+ }
+
+ // Check some invalid country codes
+ if (loc_country_code_is_valid("X1")) {
+ fprintf(stderr, "Invalid country code detected as valid: %s\n", "X1");
+ exit(EXIT_FAILURE);
+ }
+
struct loc_ctx* ctx;
err = loc_new(&ctx);
if (err < 0)
struct loc_ctx* ctx;
err = loc_new(&ctx);
if (err < 0)
exit(EXIT_FAILURE);
// Create a country
exit(EXIT_FAILURE);
// Create a country
- err = loc_writer_add_country(writer, &country, "T1");
+ err = loc_writer_add_country(writer, &country, "XX");
if (err) {
fprintf(stderr, "Could not create country\n");
exit(EXIT_FAILURE);
if (err) {
fprintf(stderr, "Could not create country\n");
exit(EXIT_FAILURE);
// Set name & continent
loc_country_set_name(country, "Testistan");
// Set name & continent
loc_country_set_name(country, "Testistan");
- loc_country_set_continent_code(country, "XX");
+ loc_country_set_continent_code(country, "YY");
// Free country
loc_country_unref(country);
// Free country
loc_country_unref(country);
}
// Lookup an address in the subnet
}
// Lookup an address in the subnet
- err = loc_database_get_country(db, &country, "T1");
+ err = loc_database_get_country(db, &country, "XX");
- fprintf(stderr, "Could not find country T1\n");
+ fprintf(stderr, "Could not find country XX\n");
exit(EXIT_FAILURE);
}
loc_country_unref(country);
exit(EXIT_FAILURE);
}
loc_country_unref(country);