tor  0.4.0.0-alpha-dev
geoip.c
Go to the documentation of this file.
1 /* Copyright (c) 2007-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
30 #define GEOIP_PRIVATE
31 #include "lib/geoip/geoip.h"
32 #include "lib/container/map.h"
33 #include "lib/container/order.h"
36 #include "lib/ctime/di_ops.h"
37 #include "lib/encoding/binascii.h"
38 #include "lib/fs/files.h"
39 #include "lib/log/escape.h"
40 #include "lib/malloc/malloc.h"
41 #include "lib/net/address.h" //????
42 #include "lib/net/inaddr.h"
45 #include "lib/string/scanf.h"
46 #include "lib/string/util_string.h"
47 
48 #include <stdio.h>
49 #include <string.h>
50 
51 static void init_geoip_countries(void);
52 
54 typedef struct geoip_ipv4_entry_t {
55  uint32_t ip_low;
56  uint32_t ip_high;
57  intptr_t country;
59 
61 typedef struct geoip_ipv6_entry_t {
62  struct in6_addr ip_low;
63  struct in6_addr ip_high;
64  intptr_t country;
66 
72 static strmap_t *country_idxplus1_by_lc_code = NULL;
75 static smartlist_t *geoip_ipv4_entries = NULL, *geoip_ipv6_entries = NULL;
76 
78 static char geoip_digest[DIGEST_LEN];
79 static char geoip6_digest[DIGEST_LEN];
80 
82 const smartlist_t *
84 {
85  if (geoip_countries == NULL) {
87  }
88  return geoip_countries;
89 }
90 
94 MOCK_IMPL(country_t,
95 geoip_get_country,(const char *country))
96 {
97  void *idxplus1_;
98  intptr_t idx;
99 
100  idxplus1_ = strmap_get_lc(country_idxplus1_by_lc_code, country);
101  if (!idxplus1_)
102  return -1;
103 
104  idx = ((uintptr_t)idxplus1_)-1;
105  return (country_t)idx;
106 }
107 
110 static void
111 geoip_add_entry(const tor_addr_t *low, const tor_addr_t *high,
112  const char *country)
113 {
114  intptr_t idx;
115  void *idxplus1_;
116 
118  return;
119  IF_BUG_ONCE(tor_addr_compare(high, low, CMP_EXACT) < 0)
120  return;
121 
122  idxplus1_ = strmap_get_lc(country_idxplus1_by_lc_code, country);
123 
124  if (!idxplus1_) {
125  geoip_country_t *c = tor_malloc_zero(sizeof(geoip_country_t));
126  strlcpy(c->countrycode, country, sizeof(c->countrycode));
127  tor_strlower(c->countrycode);
129  idx = smartlist_len(geoip_countries) - 1;
130  strmap_set_lc(country_idxplus1_by_lc_code, country, (void*)(idx+1));
131  } else {
132  idx = ((uintptr_t)idxplus1_)-1;
133  }
134  {
135  geoip_country_t *c = smartlist_get(geoip_countries, (int)idx);
136  tor_assert(!strcasecmp(c->countrycode, country));
137  }
138 
139  if (tor_addr_family(low) == AF_INET) {
140  geoip_ipv4_entry_t *ent = tor_malloc_zero(sizeof(geoip_ipv4_entry_t));
141  ent->ip_low = tor_addr_to_ipv4h(low);
142  ent->ip_high = tor_addr_to_ipv4h(high);
143  ent->country = idx;
145  } else if (tor_addr_family(low) == AF_INET6) {
146  geoip_ipv6_entry_t *ent = tor_malloc_zero(sizeof(geoip_ipv6_entry_t));
147  ent->ip_low = *tor_addr_to_in6_assert(low);
148  ent->ip_high = *tor_addr_to_in6_assert(high);
149  ent->country = idx;
150  smartlist_add(geoip_ipv6_entries, ent);
151  }
152 }
153 
156 STATIC int
157 geoip_parse_entry(const char *line, sa_family_t family)
158 {
159  tor_addr_t low_addr, high_addr;
160  char c[3];
161  char *country = NULL;
162 
163  if (!geoip_countries)
165  if (family == AF_INET) {
166  if (!geoip_ipv4_entries)
167  geoip_ipv4_entries = smartlist_new();
168  } else if (family == AF_INET6) {
169  if (!geoip_ipv6_entries)
170  geoip_ipv6_entries = smartlist_new();
171  } else {
172  log_warn(LD_GENERAL, "Unsupported family: %d", family);
173  return -1;
174  }
175 
176  while (TOR_ISSPACE(*line))
177  ++line;
178  if (*line == '#')
179  return 0;
180 
181  char buf[512];
182  if (family == AF_INET) {
183  unsigned int low, high;
184  if (tor_sscanf(line,"%u,%u,%2s", &low, &high, c) == 3 ||
185  tor_sscanf(line,"\"%u\",\"%u\",\"%2s\",", &low, &high, c) == 3) {
186  tor_addr_from_ipv4h(&low_addr, low);
187  tor_addr_from_ipv4h(&high_addr, high);
188  } else
189  goto fail;
190  country = c;
191  } else { /* AF_INET6 */
192  char *low_str, *high_str;
193  struct in6_addr low, high;
194  char *strtok_state;
195  strlcpy(buf, line, sizeof(buf));
196  low_str = tor_strtok_r(buf, ",", &strtok_state);
197  if (!low_str)
198  goto fail;
199  high_str = tor_strtok_r(NULL, ",", &strtok_state);
200  if (!high_str)
201  goto fail;
202  country = tor_strtok_r(NULL, "\n", &strtok_state);
203  if (!country)
204  goto fail;
205  if (strlen(country) != 2)
206  goto fail;
207  if (tor_inet_pton(AF_INET6, low_str, &low) <= 0)
208  goto fail;
209  tor_addr_from_in6(&low_addr, &low);
210  if (tor_inet_pton(AF_INET6, high_str, &high) <= 0)
211  goto fail;
212  tor_addr_from_in6(&high_addr, &high);
213  }
214  geoip_add_entry(&low_addr, &high_addr, country);
215  return 0;
216 
217  fail:
218  log_warn(LD_GENERAL, "Unable to parse line from GEOIP %s file: %s",
219  family == AF_INET ? "IPv4" : "IPv6", escaped(line));
220  return -1;
221 }
222 
225 static int
226 geoip_ipv4_compare_entries_(const void **_a, const void **_b)
227 {
228  const geoip_ipv4_entry_t *a = *_a, *b = *_b;
229  if (a->ip_low < b->ip_low)
230  return -1;
231  else if (a->ip_low > b->ip_low)
232  return 1;
233  else
234  return 0;
235 }
236 
239 static int
240 geoip_ipv4_compare_key_to_entry_(const void *_key, const void **_member)
241 {
242  /* No alignment issue here, since _key really is a pointer to uint32_t */
243  const uint32_t addr = *(uint32_t *)_key;
244  const geoip_ipv4_entry_t *entry = *_member;
245  if (addr < entry->ip_low)
246  return -1;
247  else if (addr > entry->ip_high)
248  return 1;
249  else
250  return 0;
251 }
252 
255 static int
256 geoip_ipv6_compare_entries_(const void **_a, const void **_b)
257 {
258  const geoip_ipv6_entry_t *a = *_a, *b = *_b;
259  return fast_memcmp(a->ip_low.s6_addr, b->ip_low.s6_addr,
260  sizeof(struct in6_addr));
261 }
262 
265 static int
266 geoip_ipv6_compare_key_to_entry_(const void *_key, const void **_member)
267 {
268  const struct in6_addr *addr = (struct in6_addr *)_key;
269  const geoip_ipv6_entry_t *entry = *_member;
270 
271  if (fast_memcmp(addr->s6_addr, entry->ip_low.s6_addr,
272  sizeof(struct in6_addr)) < 0)
273  return -1;
274  else if (fast_memcmp(addr->s6_addr, entry->ip_high.s6_addr,
275  sizeof(struct in6_addr)) > 0)
276  return 1;
277  else
278  return 0;
279 }
280 
284 static void
286 {
287  geoip_country_t *geoip_unresolved;
288  geoip_countries = smartlist_new();
289  /* Add a geoip_country_t for requests that could not be resolved to a
290  * country as first element (index 0) to geoip_countries. */
291  geoip_unresolved = tor_malloc_zero(sizeof(geoip_country_t));
292  strlcpy(geoip_unresolved->countrycode, "??",
293  sizeof(geoip_unresolved->countrycode));
294  smartlist_add(geoip_countries, geoip_unresolved);
295  country_idxplus1_by_lc_code = strmap_new();
296  strmap_set_lc(country_idxplus1_by_lc_code, "??", (void*)(1));
297 }
298 
317 int
318 geoip_load_file(sa_family_t family, const char *filename, int severity)
319 {
320  FILE *f;
321  crypto_digest_t *geoip_digest_env = NULL;
322 
323  tor_assert(family == AF_INET || family == AF_INET6);
324 
325  if (!(f = tor_fopen_cloexec(filename, "r"))) {
326  log_fn(severity, LD_GENERAL, "Failed to open GEOIP file %s.",
327  filename);
328  return -1;
329  }
330  if (!geoip_countries)
332 
333  if (family == AF_INET) {
334  if (geoip_ipv4_entries) {
336  tor_free(e));
337  smartlist_free(geoip_ipv4_entries);
338  }
339  geoip_ipv4_entries = smartlist_new();
340  } else { /* AF_INET6 */
341  if (geoip_ipv6_entries) {
342  SMARTLIST_FOREACH(geoip_ipv6_entries, geoip_ipv6_entry_t *, e,
343  tor_free(e));
344  smartlist_free(geoip_ipv6_entries);
345  }
346  geoip_ipv6_entries = smartlist_new();
347  }
348  geoip_digest_env = crypto_digest_new();
349 
350  log_notice(LD_GENERAL, "Parsing GEOIP %s file %s.",
351  (family == AF_INET) ? "IPv4" : "IPv6", filename);
352  while (!feof(f)) {
353  char buf[512];
354  if (fgets(buf, (int)sizeof(buf), f) == NULL)
355  break;
356  crypto_digest_add_bytes(geoip_digest_env, buf, strlen(buf));
357  /* FFFF track full country name. */
358  geoip_parse_entry(buf, family);
359  }
360  /*XXXX abort and return -1 if no entries/illformed?*/
361  fclose(f);
362 
363  /* Sort list and remember file digests so that we can include it in
364  * our extra-info descriptors. */
365  if (family == AF_INET) {
368  } else {
369  /* AF_INET6 */
370  smartlist_sort(geoip_ipv6_entries, geoip_ipv6_compare_entries_);
371  crypto_digest_get_digest(geoip_digest_env, geoip6_digest, DIGEST_LEN);
372  }
373  crypto_digest_free(geoip_digest_env);
374 
375  return 0;
376 }
377 
384 int
386 {
387  geoip_ipv4_entry_t *ent;
388  if (!geoip_ipv4_entries)
389  return -1;
390  ent = smartlist_bsearch(geoip_ipv4_entries, &ipaddr,
392  return ent ? (int)ent->country : 0;
393 }
394 
400 int
402 {
403  geoip_ipv6_entry_t *ent;
404 
405  if (!geoip_ipv6_entries)
406  return -1;
407  ent = smartlist_bsearch(geoip_ipv6_entries, addr,
409  return ent ? (int)ent->country : 0;
410 }
411 
418 geoip_get_country_by_addr,(const tor_addr_t *addr))
419 {
420  if (tor_addr_family(addr) == AF_INET) {
422  } else if (tor_addr_family(addr) == AF_INET6) {
424  } else {
425  return -1;
426  }
427 }
428 
431 geoip_get_n_countries,(void))
432 {
433  if (!geoip_countries)
435  return (int) smartlist_len(geoip_countries);
436 }
437 
440 const char *
442 {
443  if (geoip_countries && num >= 0 && num < smartlist_len(geoip_countries)) {
444  geoip_country_t *c = smartlist_get(geoip_countries, num);
445  return c->countrycode;
446  } else
447  return "??";
448 }
449 
452 geoip_is_loaded,(sa_family_t family))
453 {
454  tor_assert(family == AF_INET || family == AF_INET6);
455  if (geoip_countries == NULL)
456  return 0;
457  if (family == AF_INET)
458  return geoip_ipv4_entries != NULL;
459  else /* AF_INET6 */
460  return geoip_ipv6_entries != NULL;
461 }
462 
466 const char *
468 {
469  tor_assert(family == AF_INET || family == AF_INET6);
470  if (family == AF_INET)
472  else /* AF_INET6 */
473  return hex_str(geoip6_digest, DIGEST_LEN);
474 }
475 
477 STATIC void
479 {
480  if (geoip_countries) {
482  smartlist_free(geoip_countries);
483  }
484 
485  strmap_free(country_idxplus1_by_lc_code, NULL);
486  if (geoip_ipv4_entries) {
488  tor_free(ent));
489  smartlist_free(geoip_ipv4_entries);
490  }
491  if (geoip_ipv6_entries) {
492  SMARTLIST_FOREACH(geoip_ipv6_entries, geoip_ipv6_entry_t *, ent,
493  tor_free(ent));
494  smartlist_free(geoip_ipv6_entries);
495  }
496  geoip_countries = NULL;
498  geoip_ipv4_entries = NULL;
499  geoip_ipv6_entries = NULL;
500 }
501 
503 void
505 {
506  clear_geoip_db();
507 
508  memset(geoip_digest, 0, sizeof(geoip_digest));
509  memset(geoip6_digest, 0, sizeof(geoip6_digest));
510 }
int tor_sscanf(const char *buf, const char *pattern,...)
Definition: scanf.c:309
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len)
int tor_inet_pton(int af, const char *src, void *dst)
Definition: inaddr.c:166
void * strmap_get_lc(const strmap_t *map, const char *key)
Definition: map.c:393
Header for smartlist.c.
uint16_t sa_family_t
Definition: inaddr_st.h:77
Headers for di_ops.c.
uint32_t ip_low
Definition: geoip.c:55
#define LD_GENERAL
Definition: log.h:58
int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2, tor_addr_comparison_t how)
Definition: address.c:942
void smartlist_add(smartlist_t *sl, void *element)
crypto_digest_t * crypto_digest_new(void)
static uint32_t tor_addr_to_ipv4h(const tor_addr_t *a)
Definition: address.h:152
#define tor_free(p)
Definition: malloc.h:52
Header for util_string.c.
Header for inaddr.c.
Headers for util_malloc.c.
void * strmap_set_lc(strmap_t *map, const char *key, void *val)
Definition: map.c:379
static int geoip_ipv4_compare_key_to_entry_(const void *_key, const void **_member)
Definition: geoip.c:240
Header file for geoip.c.
struct geoip_ipv6_entry_t geoip_ipv6_entry_t
static void init_geoip_countries(void)
Definition: geoip.c:285
static int geoip_ipv6_compare_key_to_entry_(const void *_key, const void **_member)
Definition: geoip.c:266
intptr_t country
Definition: geoip.c:57
int geoip_get_country_by_ipv6(const struct in6_addr *addr)
Definition: geoip.c:401
tor_assert(buffer)
#define tor_addr_from_ipv4h(dest, v4addr)
Definition: address.h:287
STATIC void clear_geoip_db(void)
Definition: geoip.c:478
#define DIGEST_LEN
Definition: digest_sizes.h:20
Headers for address.h.
void crypto_digest_get_digest(crypto_digest_t *digest, char *out, size_t out_len)
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
Header for binascii.c.
Header for scanf.c.
void tor_strlower(char *s)
Definition: util_string.c:130
struct geoip_ipv4_entry_t geoip_ipv4_entry_t
void * smartlist_bsearch(const smartlist_t *sl, const void *key, int(*compare)(const void *key, const void **member))
Definition: smartlist.c:411
static smartlist_t * geoip_ipv4_entries
Definition: geoip.c:75
Header for order.c.
Headers for crypto_digest.c.
const smartlist_t * geoip_get_countries(void)
Definition: geoip.c:83
const char * geoip_db_digest(sa_family_t family)
Definition: geoip.c:467
const char * geoip_get_country_name(country_t num)
Definition: geoip.c:441
uint32_t ip_high
Definition: geoip.c:56
#define IF_BUG_ONCE(cond)
Definition: util_bug.h:214
void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6)
Definition: address.c:884
#define SMARTLIST_FOREACH(sl, type, var, cmd)
const char * escaped(const char *s)
Definition: escape.c:126
static int geoip_ipv4_compare_entries_(const void **_a, const void **_b)
Definition: geoip.c:226
Locale-independent character-type inspection (header)
static const struct in6_addr * tor_addr_to_in6(const tor_addr_t *a)
Definition: address.h:111
static int geoip_ipv6_compare_entries_(const void **_a, const void **_b)
Definition: geoip.c:256
#define log_fn(severity, domain, args,...)
Definition: log.h:255
Headers for map.c.
Definition: geoip.c:54
Header for compat_string.c.
Header for escape.c.
#define fast_memcmp(a, b, c)
Definition: di_ops.h:26
struct in6_addr ip_high
Definition: geoip.c:63
Wrappers for reading and writing data to files on disk.
static strmap_t * country_idxplus1_by_lc_code
Definition: geoip.c:72
int geoip_get_country_by_ipv4(uint32_t ipaddr)
Definition: geoip.c:385
static const struct in6_addr * tor_addr_to_in6_assert(const tor_addr_t *a)
Definition: address.h:119
STATIC int geoip_parse_entry(const char *line, sa_family_t family)
Definition: geoip.c:157
MOCK_IMPL(country_t, geoip_get_country,(const char *country))
Definition: geoip.c:94
intptr_t country
Definition: geoip.c:64
static void geoip_add_entry(const tor_addr_t *low, const tor_addr_t *high, const char *country)
Definition: geoip.c:111
static smartlist_t * geoip_countries
Definition: geoip.c:68
FILE * tor_fopen_cloexec(const char *path, const char *mode)
Definition: files.c:86
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
int geoip_load_file(sa_family_t family, const char *filename, int severity)
Definition: geoip.c:318
static char geoip_digest[DIGEST_LEN]
Definition: geoip.c:78
Definition: geoip.c:61
struct in6_addr ip_low
Definition: geoip.c:62
void geoip_free_all(void)
Definition: geoip.c:504
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:179