110 idx = ((uintptr_t)idxplus1_)-1;
138 idx = ((uintptr_t)idxplus1_)-1;
167 char *country = NULL;
171 if (family == AF_INET) {
174 }
else if (family == AF_INET6) {
178 log_warn(
LD_GENERAL,
"Unsupported family: %d", family);
182 while (TOR_ISSPACE(*line))
188 if (family == AF_INET) {
189 unsigned int low, high;
190 if (
tor_sscanf(line,
"%u,%u,%2s", &low, &high, c) == 3 ||
191 tor_sscanf(line,
"\"%u\",\"%u\",\"%2s\",", &low, &high, c) == 3) {
198 char *low_str, *high_str;
201 strlcpy(buf, line,
sizeof(buf));
202 low_str = tor_strtok_r(buf,
",", &strtok_state);
205 high_str = tor_strtok_r(NULL,
",", &strtok_state);
208 country = tor_strtok_r(NULL,
"\n", &strtok_state);
211 if (strlen(country) != 2)
224 log_warn(
LD_GENERAL,
"Unable to parse line from GEOIP %s file: %s",
225 family == AF_INET ?
"IPv4" :
"IPv6",
escaped(line));
235 if (a->
ip_low < b->ip_low)
237 else if (a->
ip_low > b->ip_low)
249 const uint32_t addr = *(uint32_t *)_key;
251 if (addr < entry->ip_low)
253 else if (addr > entry->
ip_high)
329 tor_assert(family == AF_INET || family == AF_INET6);
339 if (family == AF_INET) {
356 log_notice(
LD_GENERAL,
"Parsing GEOIP %s file %s.",
357 (family == AF_INET) ?
"IPv4" :
"IPv6", filename);
360 if (fgets(buf, (
int)
sizeof(buf), f) == NULL)
371 if (family == AF_INET) {
398 return ent ? (int)ent->
country : 0;
415 return ent ? (int)ent->
country : 0;
460 tor_assert(family == AF_INET || family == AF_INET6);
463 if (family == AF_INET)
475 tor_assert(family == AF_INET || family == AF_INET6);
476 if (family == AF_INET)
int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2, tor_addr_comparison_t how)
void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6)
static sa_family_t tor_addr_family(const tor_addr_t *a)
static uint32_t tor_addr_to_ipv4h(const tor_addr_t *a)
static const struct in6_addr * tor_addr_to_in6(const tor_addr_t *a)
#define tor_addr_from_ipv4h(dest, v4addr)
static const struct in6_addr * tor_addr_to_in6_assert(const tor_addr_t *a)
const char * hex_str(const char *from, size_t fromlen)
Locale-independent character-type inspection (header)
Header for compat_string.c.
Headers for crypto_digest.c.
void crypto_digest_get_digest(crypto_digest_t *digest, char *out, size_t out_len)
#define crypto_digest_free(d)
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len)
crypto_digest_t * crypto_digest_new(void)
#define fast_memcmp(a, b, c)
const char * escaped(const char *s)
Wrappers for reading and writing data to files on disk.
FILE * tor_fopen_cloexec(const char *path, const char *mode)
STATIC int geoip_get_country_by_ipv6(const struct in6_addr *addr)
static strmap_t * country_idxplus1_by_lc_code
static smartlist_t * geoip_ipv4_entries
static smartlist_t * geoip_ipv6_entries
int geoip_load_file(sa_family_t family, const char *filename, int severity)
const char * geoip_db_digest(sa_family_t family)
int geoip_is_loaded(sa_family_t family)
static void init_geoip_countries(void)
country_t geoip_get_country(const char *country)
void geoip_free_all(void)
int geoip_get_country_by_addr(const tor_addr_t *addr)
static int geoip_ipv6_compare_entries_(const void **_a, const void **_b)
STATIC int geoip_get_country_by_ipv4(uint32_t ipaddr)
static smartlist_t * geoip_countries
STATIC int geoip_parse_entry(const char *line, sa_family_t family)
static int geoip_ipv6_compare_key_to_entry_(const void *_key, const void **_member)
const smartlist_t * geoip_get_countries(void)
static char geoip_digest[DIGEST_LEN]
static void geoip_add_entry(const tor_addr_t *low, const tor_addr_t *high, const char *country)
STATIC void clear_geoip_db(void)
static int geoip_ipv4_compare_key_to_entry_(const void *_key, const void **_member)
static int geoip_ipv4_compare_entries_(const void **_a, const void **_b)
int geoip_get_n_countries(void)
const char * geoip_get_country_name(country_t num)
static char geoip6_digest[DIGEST_LEN]
int tor_inet_pton(int af, const char *src, void *dst)
#define log_fn(severity, domain, args,...)
Headers for util_malloc.c.
void * strmap_get_lc(const strmap_t *map, const char *key)
void * strmap_set_lc(strmap_t *map, const char *key, void *val)
int tor_sscanf(const char *buf, const char *pattern,...)
void * smartlist_bsearch(const smartlist_t *sl, const void *key, int(*compare)(const void *key, const void **member))
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
#define MOCK_IMPL(rv, funcname, arglist)
#define IF_BUG_ONCE(cond)
void tor_strlower(char *s)
Header for util_string.c.