geoip.c 21.7 KB
Newer Older
1
/* Copyright (c) 2007-2008, The Tor Project, Inc. */
2
3
4
5
6
/* See LICENSE for licensing information */
/* $Id: /tor/trunk/src/or/networkstatus.c 15493 2007-12-16T18:33:25.055570Z nickm  $ */
const char geoip_c_id[] =
  "$Id: /tor/trunk/src/or/networkstatus.c 15493 2007-12-16T18:33:25.055570Z nickm  $";

7
8
9
10
11
12
/**
 * \file geoip.c
 * \brief Functions related to maintaining an IP-to-country database and to
 *    summarizing client connections by country.
 */

13
14
15
16
#define GEOIP_PRIVATE
#include "or.h"
#include "ht.h"

17
static void clear_geoip_db(void);
18

19
/** An entry from the GeoIP file: maps an IP range to a country. */
20
typedef struct geoip_entry_t {
21
22
  uint32_t ip_low; /**< The lowest IP in the range, in host order */
  uint32_t ip_high; /**< The highest IP in the range, in host order */
23
  intptr_t country; /**< An index into geoip_countries */
24
25
} geoip_entry_t;

Nick Mathewson's avatar
Nick Mathewson committed
26
/** For how many periods should we remember per-country request history? */
27
#define REQUEST_HIST_LEN 3
Nick Mathewson's avatar
Nick Mathewson committed
28
/** How long are the periods for which we should remember request history? */
29
30
#define REQUEST_HIST_PERIOD (8*60*60)

Nick Mathewson's avatar
Nick Mathewson committed
31
/** A per-country record for GeoIP request history */
32
33
34
35
36
37
38
typedef struct geoip_country_t {
  char countrycode[3];
  uint32_t n_v2_ns_requests[REQUEST_HIST_LEN];
  uint32_t n_v3_ns_requests[REQUEST_HIST_LEN];
} geoip_country_t;

/** A list of geoip_country_t */
39
static smartlist_t *geoip_countries = NULL;
40
41
42
/** A map from lowercased country codes to their position in geoip_countries.
 * The index is encoded in the pointer, and 1 is added so that NULL can mean
 * not found. */
43
static strmap_t *country_idxplus1_by_lc_code = NULL;
44
/** A list of all known geoip_entry_t, sorted by ip_low. */
45
46
static smartlist_t *geoip_entries = NULL;

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/** Return the index of the <b>country</b>'s entry in the GeoIP DB
 * if it is a valid 2-letter country code, otherwise return zero.
 */
country_t
geoip_get_country(const char *country)
{
  void *_idxplus1;
  intptr_t idx;

  _idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
  if (!_idxplus1)
    return -1;

  idx = ((uintptr_t)_idxplus1)-1;
  return (country_t)idx;
}

64
65
66
67
/** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and
 * <b>high</b>, inclusive, to the 2-letter country code <b>country</b>.
 */
static void
68
69
geoip_add_entry(uint32_t low, uint32_t high, const char *country)
{
70
  intptr_t idx;
71
  geoip_entry_t *ent;
72
73
74
75
76
77
  void *_idxplus1;

  if (high < low)
    return;

  _idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
78
79

  if (!_idxplus1) {
80
81
82
    geoip_country_t *c = tor_malloc_zero(sizeof(geoip_country_t));
    strlcpy(c->countrycode, country, sizeof(c->countrycode));
    tor_strlower(c->countrycode);
83
    smartlist_add(geoip_countries, c);
84
    idx = smartlist_len(geoip_countries) - 1;
85
86
87
88
    strmap_set_lc(country_idxplus1_by_lc_code, country, (void*)(idx+1));
  } else {
    idx = ((uintptr_t)_idxplus1)-1;
  }
89
90
91
92
  {
    geoip_country_t *c = smartlist_get(geoip_countries, idx);
    tor_assert(!strcasecmp(c->countrycode, country));
  }
93
94
95
96
97
98
99
  ent = tor_malloc_zero(sizeof(geoip_entry_t));
  ent->ip_low = low;
  ent->ip_high = high;
  ent->country = idx;
  smartlist_add(geoip_entries, ent);
}

100
/** Add an entry to the GeoIP table, parsing it from <b>line</b>.  The
101
 * format is as for geoip_load_file(). */
102
103
104
105
106
107
108
109
110
111
/*private*/ int
geoip_parse_entry(const char *line)
{
  unsigned int low, high;
  char b[3];
  if (!geoip_countries) {
    geoip_countries = smartlist_create();
    geoip_entries = smartlist_create();
    country_idxplus1_by_lc_code = strmap_new();
  }
112
113
114
115
  while (TOR_ISSPACE(*line))
    ++line;
  if (*line == '#')
    return 0;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  if (sscanf(line,"%u,%u,%2s", &low, &high, b) == 3) {
    geoip_add_entry(low, high, b);
    return 0;
  } else if (sscanf(line,"\"%u\",\"%u\",\"%2s\",", &low, &high, b) == 3) {
    geoip_add_entry(low, high, b);
    return 0;
  } else {
    log_warn(LD_GENERAL, "Unable to parse line from GEOIP file: %s",
             escaped(line));
    return -1;
  }
}

/** Sorting helper: return -1, 1, or 0 based on comparison of two
 * geoip_entry_t */
131
132
133
134
135
136
137
138
139
140
141
142
static int
_geoip_compare_entries(const void **_a, const void **_b)
{
  const geoip_entry_t *a = *_a, *b = *_b;
  if (a->ip_low < b->ip_low)
    return -1;
  else if (a->ip_low > b->ip_low)
    return 1;
  else
    return 0;
}

143
144
/** bsearch helper: return -1, 1, or 0 based on comparison of an IP (a pointer
 * to a uint32_t in host order) to a geoip_entry_t */
145
146
147
148
149
150
151
152
153
154
155
156
157
static int
_geoip_compare_key_to_entry(const void *_key, const void **_member)
{
  const uint32_t addr = *(uint32_t *)_key;
  const geoip_entry_t *entry = *_member;
  if (addr < entry->ip_low)
    return -1;
  else if (addr > entry->ip_high)
    return 1;
  else
    return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
158
159
160
161
162
163
164
165
/** Return 1 if we should collect geoip stats on bridge users, and
 * include them in our extrainfo descriptor. Else return 0. */
int
should_record_bridge_info(or_options_t *options)
{
  return options->BridgeRelay && options->BridgeRecordUsageByCountry;
}

166
167
168
169
170
171
172
173
174
/** Clear the GeoIP database and reload it from the file
 * <b>filename</b>. Return 0 on success, -1 on failure.
 *
 * Recognized line formats are:
 *   INTIPLOW,INTIPHIGH,CC
 * and
 *   "INTIPLOW","INTIPHIGH","CC","CC3","COUNTRY NAME"
 * where INTIPLOW and INTIPHIGH are IPv4 addresses encoded as 4-byte unsigned
 * integers, and CC is a country code.
175
 *
176
177
 * It also recognizes, and skips over, blank lines and lines that start
 * with '#' (comments).
178
 */
179
int
Roger Dingledine's avatar
Roger Dingledine committed
180
geoip_load_file(const char *filename, or_options_t *options)
181
182
{
  FILE *f;
183
184
  const char *msg = "";
  int severity = options_need_geoip_info(options, &msg) ? LOG_WARN : LOG_INFO;
185
  clear_geoip_db();
186
  if (!(f = fopen(filename, "r"))) {
187
188
    log_fn(severity, LD_GENERAL, "Failed to open GEOIP file %s.  %s",
           filename, msg);
189
190
    return -1;
  }
191
192
193
194
195
196
197
198
  if (!geoip_countries) {
    geoip_countries = smartlist_create();
    country_idxplus1_by_lc_code = strmap_new();
  }
  if (geoip_entries) {
    SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, e, tor_free(e));
    smartlist_free(geoip_entries);
  }
199
  geoip_entries = smartlist_create();
200
  log_notice(LD_GENERAL, "Parsing GEOIP file.");
201
  while (!feof(f)) {
202
    char buf[512];
203
    if (fgets(buf, (int)sizeof(buf), f) == NULL)
204
      break;
205
    /* FFFF track full country name. */
206
    geoip_parse_entry(buf);
207
  }
208
  /*XXXX abort and return -1 if no entries/illformed?*/
209
210
211
212
213
214
  fclose(f);

  smartlist_sort(geoip_entries, _geoip_compare_entries);
  return 0;
}

215
216
217
218
219
/** Given an IP address in host order, return a number representing the
 * country to which that address belongs, or -1 for unknown.  The return value
 * will always be less than geoip_get_n_countries().  To decode it,
 * call geoip_get_country_name().
 */
220
221
222
223
224
225
226
int
geoip_get_country_by_ip(uint32_t ipaddr)
{
  geoip_entry_t *ent;
  if (!geoip_entries)
    return -1;
  ent = smartlist_bsearch(geoip_entries, &ipaddr, _geoip_compare_key_to_entry);
227
  return ent ? (int)ent->country : -1;
228
229
}

Roger Dingledine's avatar
tweaks    
Roger Dingledine committed
230
/** Return the number of countries recognized by the GeoIP database. */
231
232
233
int
geoip_get_n_countries(void)
{
234
  return (int) smartlist_len(geoip_countries);
235
236
}

237
238
/** Return the two-letter country code associated with the number <b>num</b>,
 * or "??" for an unknown value. */
239
const char *
240
geoip_get_country_name(country_t num)
241
{
242
243
244
245
  if (geoip_countries && num >= 0 && num < smartlist_len(geoip_countries)) {
    geoip_country_t *c = smartlist_get(geoip_countries, num);
    return c->countrycode;
  } else
246
247
248
    return "??";
}

249
/** Return true iff we have loaded a GeoIP database.*/
250
251
252
253
254
255
int
geoip_is_loaded(void)
{
  return geoip_countries != NULL && geoip_entries != NULL;
}

256
257
258
/** Entry in a map from IP address to the last time we've seen an incoming
 * connection from that IP address. Used by bridges only, to track which
 * countries have them blocked. */
259
260
261
typedef struct clientmap_entry_t {
  HT_ENTRY(clientmap_entry_t) node;
  uint32_t ipaddr;
262
263
  time_t last_seen; /* The last 2 bits of this value hold the client
                     * operation. */
264
265
} clientmap_entry_t;

266
267
#define ACTION_MASK 3

268
/** Map from client IP address to last time seen. */
269
270
static HT_HEAD(clientmap, clientmap_entry_t) client_history =
     HT_INITIALIZER();
271
/** Time at which we started tracking client IP history. */
272
273
static time_t client_history_starts = 0;

Nick Mathewson's avatar
Nick Mathewson committed
274
275
/** When did the current period of checking per-country request history
 * start? */
276
static time_t current_request_period_starts = 0;
Nick Mathewson's avatar
Nick Mathewson committed
277
/** How many older request periods are we remembering? */
278
279
static int n_old_request_periods = 0;

280
/** Hashtable helper: compute a hash of a clientmap_entry_t. */
281
282
283
284
285
static INLINE unsigned
clientmap_entry_hash(const clientmap_entry_t *a)
{
  return ht_improve_hash((unsigned) a->ipaddr);
}
286
/** Hashtable helper: compare two clientmap_entry_t values for equality. */
287
288
289
290
291
292
293
294
295
296
297
static INLINE int
clientmap_entries_eq(const clientmap_entry_t *a, const clientmap_entry_t *b)
{
  return a->ipaddr == b->ipaddr;
}

HT_PROTOTYPE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
             clientmap_entries_eq);
HT_GENERATE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
            clientmap_entries_eq, 0.6, malloc, realloc, free);

298
299
/** Note that we've seen a client connect from the IP <b>addr</b> (host order)
 * at time <b>now</b>. Ignored by all but bridges. */
300
void
301
302
geoip_note_client_seen(geoip_client_action_t action,
                       uint32_t addr, time_t now)
303
304
305
{
  or_options_t *options = get_options();
  clientmap_entry_t lookup, *ent;
306
307
308
309
310
  if (action == GEOIP_CLIENT_CONNECT) {
    if (!(options->BridgeRelay && options->BridgeRecordUsageByCountry))
      return;
  } else {
#ifndef ENABLE_GEOIP_STATS
311
    return;
312
313
314
315
316
317
318
#else
    if (options->BridgeRelay || options->BridgeAuthoritativeDir ||
        !options->DirRecordUsageByCountry)
      return;
#endif
  }

Nick Mathewson's avatar
Nick Mathewson committed
319
  /* Rotate the current request period. */
320
  while (current_request_period_starts + REQUEST_HIST_PERIOD < now) {
321
322
    if (!geoip_countries)
      geoip_countries = smartlist_create();
Nick Mathewson's avatar
Nick Mathewson committed
323
324
325
326
    if (!current_request_period_starts) {
      current_request_period_starts = now;
      break;
    }
327
328
329
330
331
332
333
334
335
    SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
        memmove(&c->n_v2_ns_requests[0], &c->n_v2_ns_requests[1],
                sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
        memmove(&c->n_v3_ns_requests[0], &c->n_v3_ns_requests[1],
                sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
        c->n_v2_ns_requests[REQUEST_HIST_LEN-1] = 0;
        c->n_v3_ns_requests[REQUEST_HIST_LEN-1] = 0;
      });
    current_request_period_starts += REQUEST_HIST_PERIOD;
Nick Mathewson's avatar
Nick Mathewson committed
336
    if (n_old_request_periods < REQUEST_HIST_LEN-1)
337
      ++n_old_request_periods;
Nick Mathewson's avatar
Nick Mathewson committed
338
   }
339

340
  /* We use the low 3 bits of the time to encode the action. Since we're
341
   * potentially remembering tons of clients, we don't want to make
342
343
   * clientmap_entry_t larger than it has to be. */
  now = (now & ~ACTION_MASK) | (((int)action) & ACTION_MASK);
344
345
346
347
348
349
350
351
352
353
  lookup.ipaddr = addr;
  ent = HT_FIND(clientmap, &client_history, &lookup);
  if (ent) {
    ent->last_seen = now;
  } else {
    ent = tor_malloc_zero(sizeof(clientmap_entry_t));
    ent->ipaddr = addr;
    ent->last_seen = now;
    HT_INSERT(clientmap, &client_history, ent);
  }
354
355
356
357
358
359
360
361
362
363
364
365
366
367

  if (action == GEOIP_CLIENT_NETWORKSTATUS ||
      action == GEOIP_CLIENT_NETWORKSTATUS_V2) {
    int country_idx = geoip_get_country_by_ip(addr);
    if (country_idx >= 0 && country_idx < smartlist_len(geoip_countries)) {
      geoip_country_t *country = smartlist_get(geoip_countries, country_idx);
      if (action == GEOIP_CLIENT_NETWORKSTATUS)
        ++country->n_v3_ns_requests[REQUEST_HIST_LEN-1];
      else
        ++country->n_v2_ns_requests[REQUEST_HIST_LEN-1];
    }
  }

  if (!client_history_starts) {
368
    client_history_starts = now;
369
370
    current_request_period_starts = now;
  }
371
372
}

373
374
/** HT_FOREACH helper: remove a clientmap_entry_t from the hashtable if it's
 * older than a certain time. */
375
376
377
378
379
380
381
382
383
384
385
386
static int
_remove_old_client_helper(struct clientmap_entry_t *ent, void *_cutoff)
{
  time_t cutoff = *(time_t*)_cutoff;
  if (ent->last_seen < cutoff) {
    tor_free(ent);
    return 1;
  } else {
    return 0;
  }
}

387
/** Forget about all clients that haven't connected since <b>cutoff</b>. */
388
389
390
391
392
393
394
395
396
397
void
geoip_remove_old_clients(time_t cutoff)
{
  clientmap_HT_FOREACH_FN(&client_history,
                          _remove_old_client_helper,
                          &cutoff);
  if (client_history_starts < cutoff)
    client_history_starts = cutoff;
}

398
/** Do not mention any country from which fewer than this number of IPs have
399
400
401
 * connected.  This conceivably avoids reporting information that could
 * deanonymize users, though analysis is lacking. */
#define MIN_IPS_TO_NOTE_COUNTRY 0
402
403
/** Do not report any geoip data at all if we have fewer than this number of
 * IPs to report about. */
404
#define MIN_IPS_TO_NOTE_ANYTHING 0
405
/** When reporting geoip data about countries, round up to the nearest
406
 * multiple of this value. */
407
408
#define IP_GRANULARITY 8

409
/** Return the time at which we started recording geoip data. */
410
411
412
413
414
415
time_t
geoip_get_history_start(void)
{
  return client_history_starts;
}

416
/** Helper type: used to sort per-country totals by value. */
417
typedef struct c_hist_t {
418
419
  char country[3]; /**< Two-letter country code. */
  unsigned total; /**< Total IP addresses seen in this country. */
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
} c_hist_t;

/** Sorting helper: return -1, 1, or 0 based on comparison of two
 * geoip_entry_t.  Sort in descending order of total, and then by country
 * code. */
static int
_c_hist_compare(const void **_a, const void **_b)
{
  const c_hist_t *a = *_a, *b = *_b;
  if (a->total > b->total)
    return -1;
  else if (a->total < b->total)
    return 1;
  else
    return strcmp(a->country, b->country);
}

Nick Mathewson's avatar
Nick Mathewson committed
437
438
/** How long do we have to have observed per-country request history before we
 * are willing to talk about it? */
439
#define GEOIP_MIN_OBSERVATION_TIME (12*60*60)
440

441
442
/** Return the lowest x such that x is at least <b>number</b>, and x modulo
 * <b>divisor</b> == 0. */
443
444
445
446
447
448
449
450
static INLINE unsigned
round_to_next_multiple_of(unsigned number, unsigned divisor)
{
  number += divisor - 1;
  number -= number % divisor;
  return number;
}

451
452
453
454
455
/** Return a newly allocated comma-separated string containing entries for all
 * the countries from which we've seen enough clients connect. The entry
 * format is cc=num where num is the number of IPs we've seen connecting from
 * that country, and cc is a lowercased country code.  Returns NULL if we don't
 * want to export geoip data yet. */
456
char *
457
geoip_get_client_history(time_t now, geoip_client_action_t action)
458
459
460
461
{
  char *result = NULL;
  if (!geoip_is_loaded())
    return NULL;
462
  if (client_history_starts < (now - GEOIP_MIN_OBSERVATION_TIME)) {
463
464
    char buf[32];
    smartlist_t *chunks = NULL;
465
    smartlist_t *entries = NULL;
466
467
468
469
470
    int n_countries = geoip_get_n_countries();
    int i;
    clientmap_entry_t **ent;
    unsigned *counts = tor_malloc_zero(sizeof(unsigned)*n_countries);
    unsigned total = 0;
471
472
473
474
475
    unsigned granularity = IP_GRANULARITY;
#ifdef ENABLE_GEOIP_STATS
    if (get_options()->DirRecordUsageByCountry)
      granularity = get_options()->DirRecordUsageGranularity;
#endif
476
    HT_FOREACH(ent, clientmap, &client_history) {
477
      int country;
478
      if (((*ent)->last_seen & ACTION_MASK) != (int)action)
479
480
        continue;
      country = geoip_get_country_by_ip((*ent)->ipaddr);
481
482
483
484
485
486
      if (country < 0)
        continue;
      tor_assert(0 <= country && country < n_countries);
      ++counts[country];
      ++total;
    }
487
    /* Don't record anything if we haven't seen enough IPs. */
Nick Mathewson's avatar
Nick Mathewson committed
488
#if (MIN_IPS_TO_NOTE_ANYTHING > 0)
489
490
    if (total < MIN_IPS_TO_NOTE_ANYTHING)
      goto done;
Nick Mathewson's avatar
Nick Mathewson committed
491
#endif
492
493
    /* Make a list of c_hist_t */
    entries = smartlist_create();
494
495
496
    for (i = 0; i < n_countries; ++i) {
      unsigned c = counts[i];
      const char *countrycode;
497
498
      c_hist_t *ent;
      /* Only report a country if it has a minimum number of IPs. */
Nick Mathewson's avatar
Nick Mathewson committed
499
#if (MIN_IPS_TO_NOTE_COUNTRY > 0)
500
      if (c >= MIN_IPS_TO_NOTE_COUNTRY) {
Nick Mathewson's avatar
Nick Mathewson committed
501
#else
Roger Dingledine's avatar
Roger Dingledine committed
502
      if (c > 0) {
Nick Mathewson's avatar
Nick Mathewson committed
503
#endif
504
        c = round_to_next_multiple_of(c, granularity);
505
        countrycode = geoip_get_country_name(i);
506
507
508
509
        ent = tor_malloc(sizeof(c_hist_t));
        strlcpy(ent->country, countrycode, sizeof(ent->country));
        ent->total = c;
        smartlist_add(entries, ent);
510
511
      }
    }
512
513
514
515
516
517
518
519
520
521
    /* Sort entries. Note that we must do this _AFTER_ rounding, or else
     * the sort order could leak info. */
    smartlist_sort(entries, _c_hist_compare);

    /* Build the result. */
    chunks = smartlist_create();
    SMARTLIST_FOREACH(entries, c_hist_t *, ch, {
        tor_snprintf(buf, sizeof(buf), "%s=%u", ch->country, ch->total);
        smartlist_add(chunks, tor_strdup(buf));
      });
522
    result = smartlist_join_strings(chunks, ",", 0, NULL);
Nick Mathewson's avatar
Nick Mathewson committed
523
#if (MIN_IPS_TO_NOTE_ANYTHING > 0)
524
  done:
Nick Mathewson's avatar
Nick Mathewson committed
525
#endif
526
527
528
529
530
    tor_free(counts);
    if (chunks) {
      SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
      smartlist_free(chunks);
    }
531
532
533
534
    if (entries) {
      SMARTLIST_FOREACH(entries, c_hist_t *, c, tor_free(c));
      smartlist_free(entries);
    }
535
536
537
538
  }
  return result;
}

Nick Mathewson's avatar
Nick Mathewson committed
539
540
541
/** Return a newly allocated string holding the per-country request history
 * for <b>action</b> in a format suitable for an extra-info document, or NULL
 * on failure. */
542
543
544
char *
geoip_get_request_history(time_t now, geoip_client_action_t action)
{
545
  smartlist_t *entries, *strings;
546
  char *result;
547
548
549
550
551
552
  unsigned granularity = IP_GRANULARITY;
#ifdef ENABLE_GEOIP_STATS
  if (get_options()->DirRecordUsageByCountry)
    granularity = get_options()->DirRecordUsageGranularity;
#endif

553
554
555
556
557
558
559
  if (client_history_starts >= (now - GEOIP_MIN_OBSERVATION_TIME))
    return NULL;
  if (action != GEOIP_CLIENT_NETWORKSTATUS &&
      action != GEOIP_CLIENT_NETWORKSTATUS_V2)
    return NULL;
  if (!geoip_countries)
    return NULL;
560

561
562
563
564
565
566
  entries = smartlist_create();
  SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
      uint32_t *n = (action == GEOIP_CLIENT_NETWORKSTATUS)
        ? c->n_v3_ns_requests : c->n_v2_ns_requests;
      uint32_t tot = 0;
      int i;
567
      c_hist_t *ent;
568
569
      for (i=0; i < REQUEST_HIST_LEN; ++i)
        tot += n[i];
570
571
      if (!tot)
        continue;
572
573
574
575
      ent = tor_malloc_zero(sizeof(c_hist_t));
      strlcpy(ent->country, c->countrycode, sizeof(ent->country));
      ent->total = round_to_next_multiple_of(tot, granularity);
      smartlist_add(entries, ent);
576
  });
577
578
579
580
581
582
583
584
585
586
587
588
589
  smartlist_sort(entries, _c_hist_compare);

  strings = smartlist_create();
  SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
      char buf[32];
      tor_snprintf(buf, sizeof(buf), "%s=%u", ent->country, ent->total);
      smartlist_add(strings, tor_strdup(buf));
    });
  result = smartlist_join_strings(strings, ",", 0, NULL);
  SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp));
  SMARTLIST_FOREACH(entries, c_hist_t *, ent, tor_free(ent));
  smartlist_free(strings);
  smartlist_free(entries);
590
591
592
  return result;
}

593
/** Store all our geoip statistics into $DATADIR/geoip-stats. */
594
595
596
597
598
void
dump_geoip_stats(void)
{
#ifdef ENABLE_GEOIP_STATS
  time_t now = time(NULL);
599
  time_t request_start;
600
601
602
603
  char *filename = get_datadir_fname("geoip-stats");
  char *data_v2 = NULL, *data_v3 = NULL;
  char since[ISO_TIME_LEN+1], written[ISO_TIME_LEN+1];
  open_file_t *open_file = NULL;
604
  double v2_share = 0.0, v3_share = 0.0;
605
606
607
608
609
610
  FILE *out;

  data_v2 = geoip_get_client_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
  data_v3 = geoip_get_client_history(now, GEOIP_CLIENT_NETWORKSTATUS);
  format_iso_time(since, geoip_get_history_start());
  format_iso_time(written, now);
611
612
  out = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE,
                                    0600, &open_file);
613
614
  if (!out)
    goto done;
615
  if (fprintf(out, "written %s\nstarted-at %s\nns-ips %s\nns-v2-ips %s\n",
616
617
618
619
620
621
622
623
624
625
626
              written, since,
              data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
    goto done;
  tor_free(data_v2);
  tor_free(data_v3);

  request_start = current_request_period_starts -
    (n_old_request_periods * REQUEST_HIST_PERIOD);
  format_iso_time(since, request_start);
  data_v2 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
  data_v3 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS);
Nick Mathewson's avatar
Nick Mathewson committed
627
  if (fprintf(out, "requests-start %s\nn-ns-reqs %s\nn-v2-ns-reqs %s\n",
628
629
              since,
              data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
630
    goto done;
631
632
633
634
635
636
  if (!router_get_my_share_of_directory_requests(&v2_share, &v3_share)) {
    if (fprintf(out, "v2-ns-share %0.2lf%%\n", v2_share*100) < 0)
      goto done;
    if (fprintf(out, "v3-ns-share %0.2lf%%\n", v3_share*100) < 0)
      goto done;
  }
637
638
639
640
641
642
643
644
645
646
647
648

  finish_writing_to_file(open_file);
  open_file = NULL;
 done:
  if (open_file)
    abort_writing_to_file(open_file);
  tor_free(filename);
  tor_free(data_v2);
  tor_free(data_v3);
#endif
}

649
/** Helper used to implement GETINFO ip-to-country/... controller command. */
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
int
getinfo_helper_geoip(control_connection_t *control_conn,
                     const char *question, char **answer)
{
  (void)control_conn;
  if (geoip_is_loaded() && !strcmpstart(question, "ip-to-country/")) {
    int c;
    uint32_t ip;
    struct in_addr in;
    question += strlen("ip-to-country/");
    if (tor_inet_aton(question, &in) != 0) {
      ip = ntohl(in.s_addr);
      c = geoip_get_country_by_ip(ip);
      *answer = tor_strdup(geoip_get_country_name(c));
    }
  }
  return 0;
}
668

669
670
671
/** Release all storage held by the GeoIP database. */
static void
clear_geoip_db(void)
672
673
{
  if (geoip_countries) {
674
    SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, tor_free(c));
675
676
677
678
679
680
681
682
683
684
685
686
    smartlist_free(geoip_countries);
  }
  if (country_idxplus1_by_lc_code)
    strmap_free(country_idxplus1_by_lc_code, NULL);
  if (geoip_entries) {
    SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, ent, tor_free(ent));
    smartlist_free(geoip_entries);
  }
  geoip_countries = NULL;
  country_idxplus1_by_lc_code = NULL;
  geoip_entries = NULL;
}
687

688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/** Release all storage held in this file. */
void
geoip_free_all(void)
{
  clientmap_entry_t **ent, **next, *this;
  for (ent = HT_START(clientmap, &client_history); ent != NULL; ent = next) {
    this = *ent;
    next = HT_NEXT_RMV(clientmap, &client_history, ent);
    tor_free(this);
  }
  HT_CLEAR(clientmap, &client_history);

  clear_geoip_db();
}