2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2019 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
21 #include <libloc/libloc.h>
22 #include <libloc/compat.h>
23 #include <libloc/country.h>
24 #include <libloc/network.h>
25 #include <libloc/private.h>
27 static const struct loc_special_country
{
29 enum loc_network_flags flags
;
30 } loc_special_countries
[] = {
31 { "A1", LOC_NETWORK_FLAG_ANONYMOUS_PROXY
},
32 { "A2", LOC_NETWORK_FLAG_SATELLITE_PROVIDER
},
33 { "A3", LOC_NETWORK_FLAG_ANYCAST
},
34 { "XD", LOC_NETWORK_FLAG_DROP
},
48 LOC_EXPORT
int loc_country_new(struct loc_ctx
* ctx
, struct loc_country
** country
, const char* country_code
) {
49 // Check of the country code is valid
50 if (!loc_country_code_is_valid(country_code
)) {
55 struct loc_country
* c
= calloc(1, sizeof(*c
));
59 c
->ctx
= loc_ref(ctx
);
62 c
->code
= strdup(country_code
);
64 DEBUG(c
->ctx
, "Country %s allocated at %p\n", c
->code
, c
);
70 LOC_EXPORT
struct loc_country
* loc_country_ref(struct loc_country
* country
) {
76 static void loc_country_free(struct loc_country
* country
) {
77 DEBUG(country
->ctx
, "Releasing country %s %p\n", country
->code
, country
);
82 if (country
->continent_code
)
83 free(country
->continent_code
);
88 loc_unref(country
->ctx
);
92 LOC_EXPORT
struct loc_country
* loc_country_unref(struct loc_country
* country
) {
93 if (--country
->refcount
> 0)
96 loc_country_free(country
);
101 LOC_EXPORT
const char* loc_country_get_code(struct loc_country
* country
) {
102 return country
->code
;
105 LOC_EXPORT
const char* loc_country_get_continent_code(struct loc_country
* country
) {
106 return country
->continent_code
;
109 LOC_EXPORT
int loc_country_set_continent_code(struct loc_country
* country
, const char* continent_code
) {
110 // XXX validate input
112 // Free previous value
113 if (country
->continent_code
)
114 free(country
->continent_code
);
116 country
->continent_code
= strdup(continent_code
);
121 LOC_EXPORT
const char* loc_country_get_name(struct loc_country
* country
) {
122 return country
->name
;
125 LOC_EXPORT
int loc_country_set_name(struct loc_country
* country
, const char* name
) {
130 country
->name
= strdup(name
);
135 LOC_EXPORT
int loc_country_cmp(struct loc_country
* country1
, struct loc_country
* country2
) {
136 return strcmp(country1
->code
, country2
->code
);
139 int loc_country_new_from_database_v1(struct loc_ctx
* ctx
, struct loc_stringpool
* pool
,
140 struct loc_country
** country
, const struct loc_database_country_v1
* dbobj
) {
144 loc_country_code_copy(buffer
, dbobj
->code
);
149 // Create a new country object
150 int r
= loc_country_new(ctx
, country
, buffer
);
155 loc_country_code_copy(buffer
, dbobj
->continent_code
);
157 r
= loc_country_set_continent_code(*country
, buffer
);
162 const char* name
= loc_stringpool_get(pool
, be32toh(dbobj
->name
));
164 r
= loc_country_set_name(*country
, name
);
172 loc_country_unref(*country
);
176 int loc_country_to_database_v1(struct loc_country
* country
,
177 struct loc_stringpool
* pool
, struct loc_database_country_v1
* dbobj
) {
179 for (unsigned int i
= 0; i
< 2; i
++) {
180 dbobj
->code
[i
] = country
->code
[i
] ? country
->code
[i
] : '\0';
183 // Add continent code
184 if (country
->continent_code
) {
185 for (unsigned int i
= 0; i
< 2; i
++) {
186 dbobj
->continent_code
[i
] = country
->continent_code
[i
] ? country
->continent_code
[i
] : '\0';
190 // Save the name string in the string pool
191 off_t name
= loc_stringpool_add(pool
, country
->name
? country
->name
: "");
192 dbobj
->name
= htobe32(name
);
197 LOC_EXPORT
int loc_country_code_is_valid(const char* cc
) {
202 // It must be 2 characters long
206 // It must only contain A-Z
207 for (unsigned int i
= 0; i
< 2; i
++) {
208 if (cc
[i
] < 'A' || cc
[i
] > 'Z')
212 // The code cannot begin with an X (those are reserved for private use)
220 LOC_EXPORT
int loc_country_special_code_to_flag(const char* cc
) {
221 // Check if we got some input
227 // Return flags for any known special country
228 for (const struct loc_special_country
* country
= loc_special_countries
;
229 country
->flags
; country
++) {
230 if (strcmp(country
->code
, cc
) == 0)
231 return country
->flags
;