1 /* Copyright (c) 2007-2008, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief Functions related to maintaining an IP-to-country database and to
7 * summarizing client connections by country.
14 static void clear_geoip_db(void);
16 /** An entry from the GeoIP file: maps an IP range to a country. */
17 typedef struct geoip_entry_t
{
18 uint32_t ip_low
; /**< The lowest IP in the range, in host order */
19 uint32_t ip_high
; /**< The highest IP in the range, in host order */
20 intptr_t country
; /**< An index into geoip_countries */
23 /** For how many periods should we remember per-country request history? */
24 #define REQUEST_HIST_LEN 3
25 /** How long are the periods for which we should remember request history? */
26 #define REQUEST_HIST_PERIOD (8*60*60)
28 /** A per-country record for GeoIP request history. */
29 typedef struct geoip_country_t
{
31 uint32_t n_v2_ns_requests
[REQUEST_HIST_LEN
];
32 uint32_t n_v3_ns_requests
[REQUEST_HIST_LEN
];
35 /** A list of geoip_country_t */
36 static smartlist_t
*geoip_countries
= NULL
;
37 /** A map from lowercased country codes to their position in geoip_countries.
38 * The index is encoded in the pointer, and 1 is added so that NULL can mean
40 static strmap_t
*country_idxplus1_by_lc_code
= NULL
;
41 /** A list of all known geoip_entry_t, sorted by ip_low. */
42 static smartlist_t
*geoip_entries
= NULL
;
44 /** Return the index of the <b>country</b>'s entry in the GeoIP DB
45 * if it is a valid 2-letter country code, otherwise return zero.
48 geoip_get_country(const char *country
)
53 _idxplus1
= strmap_get_lc(country_idxplus1_by_lc_code
, country
);
57 idx
= ((uintptr_t)_idxplus1
)-1;
58 return (country_t
)idx
;
61 /** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and
62 * <b>high</b>, inclusive, to the 2-letter country code <b>country</b>.
65 geoip_add_entry(uint32_t low
, uint32_t high
, const char *country
)
74 _idxplus1
= strmap_get_lc(country_idxplus1_by_lc_code
, country
);
77 geoip_country_t
*c
= tor_malloc_zero(sizeof(geoip_country_t
));
78 strlcpy(c
->countrycode
, country
, sizeof(c
->countrycode
));
79 tor_strlower(c
->countrycode
);
80 smartlist_add(geoip_countries
, c
);
81 idx
= smartlist_len(geoip_countries
) - 1;
82 strmap_set_lc(country_idxplus1_by_lc_code
, country
, (void*)(idx
+1));
84 idx
= ((uintptr_t)_idxplus1
)-1;
87 geoip_country_t
*c
= smartlist_get(geoip_countries
, idx
);
88 tor_assert(!strcasecmp(c
->countrycode
, country
));
90 ent
= tor_malloc_zero(sizeof(geoip_entry_t
));
94 smartlist_add(geoip_entries
, ent
);
97 /** Add an entry to the GeoIP table, parsing it from <b>line</b>. The
98 * format is as for geoip_load_file(). */
100 geoip_parse_entry(const char *line
)
102 unsigned int low
, high
;
104 if (!geoip_countries
) {
105 geoip_countries
= smartlist_create();
106 geoip_entries
= smartlist_create();
107 country_idxplus1_by_lc_code
= strmap_new();
109 while (TOR_ISSPACE(*line
))
113 if (sscanf(line
,"%u,%u,%2s", &low
, &high
, b
) == 3) {
114 geoip_add_entry(low
, high
, b
);
116 } else if (sscanf(line
,"\"%u\",\"%u\",\"%2s\",", &low
, &high
, b
) == 3) {
117 geoip_add_entry(low
, high
, b
);
120 log_warn(LD_GENERAL
, "Unable to parse line from GEOIP file: %s",
126 /** Sorting helper: return -1, 1, or 0 based on comparison of two
129 _geoip_compare_entries(const void **_a
, const void **_b
)
131 const geoip_entry_t
*a
= *_a
, *b
= *_b
;
132 if (a
->ip_low
< b
->ip_low
)
134 else if (a
->ip_low
> b
->ip_low
)
140 /** bsearch helper: return -1, 1, or 0 based on comparison of an IP (a pointer
141 * to a uint32_t in host order) to a geoip_entry_t */
143 _geoip_compare_key_to_entry(const void *_key
, const void **_member
)
145 const uint32_t addr
= *(uint32_t *)_key
;
146 const geoip_entry_t
*entry
= *_member
;
147 if (addr
< entry
->ip_low
)
149 else if (addr
> entry
->ip_high
)
155 /** Return 1 if we should collect geoip stats on bridge users, and
156 * include them in our extrainfo descriptor. Else return 0. */
158 should_record_bridge_info(or_options_t
*options
)
160 return options
->BridgeRelay
&& options
->BridgeRecordUsageByCountry
;
163 /** Clear the GeoIP database and reload it from the file
164 * <b>filename</b>. Return 0 on success, -1 on failure.
166 * Recognized line formats are:
167 * INTIPLOW,INTIPHIGH,CC
169 * "INTIPLOW","INTIPHIGH","CC","CC3","COUNTRY NAME"
170 * where INTIPLOW and INTIPHIGH are IPv4 addresses encoded as 4-byte unsigned
171 * integers, and CC is a country code.
173 * It also recognizes, and skips over, blank lines and lines that start
174 * with '#' (comments).
177 geoip_load_file(const char *filename
, or_options_t
*options
)
180 const char *msg
= "";
181 int severity
= options_need_geoip_info(options
, &msg
) ? LOG_WARN
: LOG_INFO
;
183 if (!(f
= fopen(filename
, "r"))) {
184 log_fn(severity
, LD_GENERAL
, "Failed to open GEOIP file %s. %s",
188 if (!geoip_countries
) {
189 geoip_countries
= smartlist_create();
190 country_idxplus1_by_lc_code
= strmap_new();
193 SMARTLIST_FOREACH(geoip_entries
, geoip_entry_t
*, e
, tor_free(e
));
194 smartlist_free(geoip_entries
);
196 geoip_entries
= smartlist_create();
197 log_notice(LD_GENERAL
, "Parsing GEOIP file.");
200 if (fgets(buf
, (int)sizeof(buf
), f
) == NULL
)
202 /* FFFF track full country name. */
203 geoip_parse_entry(buf
);
205 /*XXXX abort and return -1 if no entries/illformed?*/
208 smartlist_sort(geoip_entries
, _geoip_compare_entries
);
212 /** Given an IP address in host order, return a number representing the
213 * country to which that address belongs, or -1 for unknown. The return value
214 * will always be less than geoip_get_n_countries(). To decode it,
215 * call geoip_get_country_name().
218 geoip_get_country_by_ip(uint32_t ipaddr
)
223 ent
= smartlist_bsearch(geoip_entries
, &ipaddr
, _geoip_compare_key_to_entry
);
224 return ent
? (int)ent
->country
: -1;
227 /** Return the number of countries recognized by the GeoIP database. */
229 geoip_get_n_countries(void)
231 return (int) smartlist_len(geoip_countries
);
234 /** Return the two-letter country code associated with the number <b>num</b>,
235 * or "??" for an unknown value. */
237 geoip_get_country_name(country_t num
)
239 if (geoip_countries
&& num
>= 0 && num
< smartlist_len(geoip_countries
)) {
240 geoip_country_t
*c
= smartlist_get(geoip_countries
, num
);
241 return c
->countrycode
;
246 /** Return true iff we have loaded a GeoIP database.*/
248 geoip_is_loaded(void)
250 return geoip_countries
!= NULL
&& geoip_entries
!= NULL
;
253 /** Entry in a map from IP address to the last time we've seen an incoming
254 * connection from that IP address. Used by bridges only, to track which
255 * countries have them blocked. */
256 typedef struct clientmap_entry_t
{
257 HT_ENTRY(clientmap_entry_t
) node
;
259 time_t last_seen
; /* The last 2 bits of this value hold the client
263 #define ACTION_MASK 3
265 /** Map from client IP address to last time seen. */
266 static HT_HEAD(clientmap
, clientmap_entry_t
) client_history
=
268 /** Time at which we started tracking client IP history. */
269 static time_t client_history_starts
= 0;
271 /** When did the current period of checking per-country request history
273 static time_t current_request_period_starts
= 0;
274 /** How many older request periods are we remembering? */
275 static int n_old_request_periods
= 0;
277 /** Hashtable helper: compute a hash of a clientmap_entry_t. */
278 static INLINE
unsigned
279 clientmap_entry_hash(const clientmap_entry_t
*a
)
281 return ht_improve_hash((unsigned) a
->ipaddr
);
283 /** Hashtable helper: compare two clientmap_entry_t values for equality. */
285 clientmap_entries_eq(const clientmap_entry_t
*a
, const clientmap_entry_t
*b
)
287 return a
->ipaddr
== b
->ipaddr
;
290 HT_PROTOTYPE(clientmap
, clientmap_entry_t
, node
, clientmap_entry_hash
,
291 clientmap_entries_eq
);
292 HT_GENERATE(clientmap
, clientmap_entry_t
, node
, clientmap_entry_hash
,
293 clientmap_entries_eq
, 0.6, malloc
, realloc
, free
);
295 /** Note that we've seen a client connect from the IP <b>addr</b> (host order)
296 * at time <b>now</b>. Ignored by all but bridges. */
298 geoip_note_client_seen(geoip_client_action_t action
,
299 uint32_t addr
, time_t now
)
301 or_options_t
*options
= get_options();
302 clientmap_entry_t lookup
, *ent
;
303 if (action
== GEOIP_CLIENT_CONNECT
) {
304 if (!(options
->BridgeRelay
&& options
->BridgeRecordUsageByCountry
))
307 #ifndef ENABLE_GEOIP_STATS
310 if (options
->BridgeRelay
|| options
->BridgeAuthoritativeDir
||
311 !options
->DirRecordUsageByCountry
)
316 /* Rotate the current request period. */
317 while (current_request_period_starts
+ REQUEST_HIST_PERIOD
< now
) {
318 if (!geoip_countries
)
319 geoip_countries
= smartlist_create();
320 if (!current_request_period_starts
) {
321 current_request_period_starts
= now
;
324 SMARTLIST_FOREACH(geoip_countries
, geoip_country_t
*, c
, {
325 memmove(&c
->n_v2_ns_requests
[0], &c
->n_v2_ns_requests
[1],
326 sizeof(uint32_t)*(REQUEST_HIST_LEN
-1));
327 memmove(&c
->n_v3_ns_requests
[0], &c
->n_v3_ns_requests
[1],
328 sizeof(uint32_t)*(REQUEST_HIST_LEN
-1));
329 c
->n_v2_ns_requests
[REQUEST_HIST_LEN
-1] = 0;
330 c
->n_v3_ns_requests
[REQUEST_HIST_LEN
-1] = 0;
332 current_request_period_starts
+= REQUEST_HIST_PERIOD
;
333 if (n_old_request_periods
< REQUEST_HIST_LEN
-1)
334 ++n_old_request_periods
;
337 /* We use the low 3 bits of the time to encode the action. Since we're
338 * potentially remembering tons of clients, we don't want to make
339 * clientmap_entry_t larger than it has to be. */
340 now
= (now
& ~ACTION_MASK
) | (((int)action
) & ACTION_MASK
);
341 lookup
.ipaddr
= addr
;
342 ent
= HT_FIND(clientmap
, &client_history
, &lookup
);
344 ent
->last_seen
= now
;
346 ent
= tor_malloc_zero(sizeof(clientmap_entry_t
));
348 ent
->last_seen
= now
;
349 HT_INSERT(clientmap
, &client_history
, ent
);
352 if (action
== GEOIP_CLIENT_NETWORKSTATUS
||
353 action
== GEOIP_CLIENT_NETWORKSTATUS_V2
) {
354 int country_idx
= geoip_get_country_by_ip(addr
);
355 if (country_idx
>= 0 && country_idx
< smartlist_len(geoip_countries
)) {
356 geoip_country_t
*country
= smartlist_get(geoip_countries
, country_idx
);
357 if (action
== GEOIP_CLIENT_NETWORKSTATUS
)
358 ++country
->n_v3_ns_requests
[REQUEST_HIST_LEN
-1];
360 ++country
->n_v2_ns_requests
[REQUEST_HIST_LEN
-1];
364 if (!client_history_starts
) {
365 client_history_starts
= now
;
366 current_request_period_starts
= now
;
370 /** HT_FOREACH helper: remove a clientmap_entry_t from the hashtable if it's
371 * older than a certain time. */
373 _remove_old_client_helper(struct clientmap_entry_t
*ent
, void *_cutoff
)
375 time_t cutoff
= *(time_t*)_cutoff
;
376 if (ent
->last_seen
< cutoff
) {
384 /** Forget about all clients that haven't connected since <b>cutoff</b>. */
386 geoip_remove_old_clients(time_t cutoff
)
388 clientmap_HT_FOREACH_FN(&client_history
,
389 _remove_old_client_helper
,
391 if (client_history_starts
< cutoff
)
392 client_history_starts
= cutoff
;
395 /** Do not mention any country from which fewer than this number of IPs have
396 * connected. This conceivably avoids reporting information that could
397 * deanonymize users, though analysis is lacking. */
398 #define MIN_IPS_TO_NOTE_COUNTRY 1
399 /** Do not report any geoip data at all if we have fewer than this number of
400 * IPs to report about. */
401 #define MIN_IPS_TO_NOTE_ANYTHING 1
402 /** When reporting geoip data about countries, round up to the nearest
403 * multiple of this value. */
404 #define IP_GRANULARITY 8
406 /** Return the time at which we started recording geoip data. */
408 geoip_get_history_start(void)
410 return client_history_starts
;
413 /** Helper type: used to sort per-country totals by value. */
414 typedef struct c_hist_t
{
415 char country
[3]; /**< Two-letter country code. */
416 unsigned total
; /**< Total IP addresses seen in this country. */
419 /** Sorting helper: return -1, 1, or 0 based on comparison of two
420 * geoip_entry_t. Sort in descending order of total, and then by country
423 _c_hist_compare(const void **_a
, const void **_b
)
425 const c_hist_t
*a
= *_a
, *b
= *_b
;
426 if (a
->total
> b
->total
)
428 else if (a
->total
< b
->total
)
431 return strcmp(a
->country
, b
->country
);
434 /** How long do we have to have observed per-country request history before we
435 * are willing to talk about it? */
436 #define GEOIP_MIN_OBSERVATION_TIME (12*60*60)
438 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
439 * <b>divisor</b> == 0. */
440 static INLINE
unsigned
441 round_to_next_multiple_of(unsigned number
, unsigned divisor
)
443 number
+= divisor
- 1;
444 number
-= number
% divisor
;
448 /** Return a newly allocated comma-separated string containing entries for all
449 * the countries from which we've seen enough clients connect. The entry
450 * format is cc=num where num is the number of IPs we've seen connecting from
451 * that country, and cc is a lowercased country code. Returns NULL if we don't
452 * want to export geoip data yet. */
454 geoip_get_client_history(time_t now
, geoip_client_action_t action
)
457 if (!geoip_is_loaded())
459 if (client_history_starts
< (now
- GEOIP_MIN_OBSERVATION_TIME
)) {
461 smartlist_t
*chunks
= NULL
;
462 smartlist_t
*entries
= NULL
;
463 int n_countries
= geoip_get_n_countries();
465 clientmap_entry_t
**ent
;
466 unsigned *counts
= tor_malloc_zero(sizeof(unsigned)*n_countries
);
468 unsigned granularity
= IP_GRANULARITY
;
469 #ifdef ENABLE_GEOIP_STATS
470 if (get_options()->DirRecordUsageByCountry
)
471 granularity
= get_options()->DirRecordUsageGranularity
;
473 HT_FOREACH(ent
, clientmap
, &client_history
) {
475 if (((*ent
)->last_seen
& ACTION_MASK
) != (int)action
)
477 country
= geoip_get_country_by_ip((*ent
)->ipaddr
);
480 tor_assert(0 <= country
&& country
< n_countries
);
484 /* Don't record anything if we haven't seen enough IPs. */
485 if (total
< MIN_IPS_TO_NOTE_ANYTHING
)
487 /* Make a list of c_hist_t */
488 entries
= smartlist_create();
489 for (i
= 0; i
< n_countries
; ++i
) {
490 unsigned c
= counts
[i
];
491 const char *countrycode
;
493 /* Only report a country if it has a minimum number of IPs. */
494 if (c
>= MIN_IPS_TO_NOTE_COUNTRY
) {
495 c
= round_to_next_multiple_of(c
, granularity
);
496 countrycode
= geoip_get_country_name(i
);
497 ent
= tor_malloc(sizeof(c_hist_t
));
498 strlcpy(ent
->country
, countrycode
, sizeof(ent
->country
));
500 smartlist_add(entries
, ent
);
503 /* Sort entries. Note that we must do this _AFTER_ rounding, or else
504 * the sort order could leak info. */
505 smartlist_sort(entries
, _c_hist_compare
);
507 /* Build the result. */
508 chunks
= smartlist_create();
509 SMARTLIST_FOREACH(entries
, c_hist_t
*, ch
, {
510 tor_snprintf(buf
, sizeof(buf
), "%s=%u", ch
->country
, ch
->total
);
511 smartlist_add(chunks
, tor_strdup(buf
));
513 result
= smartlist_join_strings(chunks
, ",", 0, NULL
);
517 SMARTLIST_FOREACH(chunks
, char *, c
, tor_free(c
));
518 smartlist_free(chunks
);
521 SMARTLIST_FOREACH(entries
, c_hist_t
*, c
, tor_free(c
));
522 smartlist_free(entries
);
528 /** Return a newly allocated string holding the per-country request history
529 * for <b>action</b> in a format suitable for an extra-info document, or NULL
532 geoip_get_request_history(time_t now
, geoip_client_action_t action
)
534 smartlist_t
*entries
, *strings
;
536 unsigned granularity
= IP_GRANULARITY
;
537 #ifdef ENABLE_GEOIP_STATS
538 if (get_options()->DirRecordUsageByCountry
)
539 granularity
= get_options()->DirRecordUsageGranularity
;
542 if (client_history_starts
>= (now
- GEOIP_MIN_OBSERVATION_TIME
))
544 if (action
!= GEOIP_CLIENT_NETWORKSTATUS
&&
545 action
!= GEOIP_CLIENT_NETWORKSTATUS_V2
)
547 if (!geoip_countries
)
550 entries
= smartlist_create();
551 SMARTLIST_FOREACH(geoip_countries
, geoip_country_t
*, c
, {
552 uint32_t *n
= (action
== GEOIP_CLIENT_NETWORKSTATUS
)
553 ? c
->n_v3_ns_requests
: c
->n_v2_ns_requests
;
557 for (i
=0; i
< REQUEST_HIST_LEN
; ++i
)
561 ent
= tor_malloc_zero(sizeof(c_hist_t
));
562 strlcpy(ent
->country
, c
->countrycode
, sizeof(ent
->country
));
563 ent
->total
= round_to_next_multiple_of(tot
, granularity
);
564 smartlist_add(entries
, ent
);
566 smartlist_sort(entries
, _c_hist_compare
);
568 strings
= smartlist_create();
569 SMARTLIST_FOREACH(entries
, c_hist_t
*, ent
, {
571 tor_snprintf(buf
, sizeof(buf
), "%s=%u", ent
->country
, ent
->total
);
572 smartlist_add(strings
, tor_strdup(buf
));
574 result
= smartlist_join_strings(strings
, ",", 0, NULL
);
575 SMARTLIST_FOREACH(strings
, char *, cp
, tor_free(cp
));
576 SMARTLIST_FOREACH(entries
, c_hist_t
*, ent
, tor_free(ent
));
577 smartlist_free(strings
);
578 smartlist_free(entries
);
582 /** Store all our geoip statistics into $DATADIR/geoip-stats. */
584 dump_geoip_stats(void)
586 #ifdef ENABLE_GEOIP_STATS
587 time_t now
= time(NULL
);
588 time_t request_start
;
589 char *filename
= get_datadir_fname("geoip-stats");
590 char *data_v2
= NULL
, *data_v3
= NULL
;
591 char since
[ISO_TIME_LEN
+1], written
[ISO_TIME_LEN
+1];
592 open_file_t
*open_file
= NULL
;
593 double v2_share
= 0.0, v3_share
= 0.0;
596 data_v2
= geoip_get_client_history(now
, GEOIP_CLIENT_NETWORKSTATUS_V2
);
597 data_v3
= geoip_get_client_history(now
, GEOIP_CLIENT_NETWORKSTATUS
);
598 format_iso_time(since
, geoip_get_history_start());
599 format_iso_time(written
, now
);
600 out
= start_writing_to_stdio_file(filename
, OPEN_FLAGS_REPLACE
,
604 if (fprintf(out
, "written %s\nstarted-at %s\nns-ips %s\nns-v2-ips %s\n",
606 data_v3
? data_v3
: "", data_v2
? data_v2
: "") < 0)
611 request_start
= current_request_period_starts
-
612 (n_old_request_periods
* REQUEST_HIST_PERIOD
);
613 format_iso_time(since
, request_start
);
614 data_v2
= geoip_get_request_history(now
, GEOIP_CLIENT_NETWORKSTATUS_V2
);
615 data_v3
= geoip_get_request_history(now
, GEOIP_CLIENT_NETWORKSTATUS
);
616 if (fprintf(out
, "requests-start %s\nn-ns-reqs %s\nn-v2-ns-reqs %s\n",
618 data_v3
? data_v3
: "", data_v2
? data_v2
: "") < 0)
620 if (!router_get_my_share_of_directory_requests(&v2_share
, &v3_share
)) {
621 if (fprintf(out
, "v2-ns-share %0.2lf%%\n", v2_share
*100) < 0)
623 if (fprintf(out
, "v3-ns-share %0.2lf%%\n", v3_share
*100) < 0)
627 finish_writing_to_file(open_file
);
631 abort_writing_to_file(open_file
);
638 /** Helper used to implement GETINFO ip-to-country/... controller command. */
640 getinfo_helper_geoip(control_connection_t
*control_conn
,
641 const char *question
, char **answer
)
644 if (geoip_is_loaded() && !strcmpstart(question
, "ip-to-country/")) {
648 question
+= strlen("ip-to-country/");
649 if (tor_inet_aton(question
, &in
) != 0) {
650 ip
= ntohl(in
.s_addr
);
651 c
= geoip_get_country_by_ip(ip
);
652 *answer
= tor_strdup(geoip_get_country_name(c
));
658 /** Release all storage held by the GeoIP database. */
662 if (geoip_countries
) {
663 SMARTLIST_FOREACH(geoip_countries
, geoip_country_t
*, c
, tor_free(c
));
664 smartlist_free(geoip_countries
);
666 if (country_idxplus1_by_lc_code
)
667 strmap_free(country_idxplus1_by_lc_code
, NULL
);
669 SMARTLIST_FOREACH(geoip_entries
, geoip_entry_t
*, ent
, tor_free(ent
));
670 smartlist_free(geoip_entries
);
672 geoip_countries
= NULL
;
673 country_idxplus1_by_lc_code
= NULL
;
674 geoip_entries
= NULL
;
677 /** Release all storage held in this file. */
681 clientmap_entry_t
**ent
, **next
, *this;
682 for (ent
= HT_START(clientmap
, &client_history
); ent
!= NULL
; ent
= next
) {
684 next
= HT_NEXT_RMV(clientmap
, &client_history
, ent
);
687 HT_CLEAR(clientmap
, &client_history
);