1 /* Copyright (c) 2007-2008, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 /* $Id: /tor/trunk/src/or/networkstatus.c 15493 2007-12-16T18:33:25.055570Z nickm $ */
4 const char geoip_c_id
[] =
5 "$Id: /tor/trunk/src/or/networkstatus.c 15493 2007-12-16T18:33:25.055570Z nickm $";
9 * \brief Functions related to maintaining an IP-to-country database and to
10 * summarizing client connections by country.
17 static void clear_geoip_db(void);
19 /** An entry from the GeoIP file: maps an IP range to a country. */
20 typedef struct geoip_entry_t
{
21 uint32_t ip_low
; /**< The lowest IP in the range, in host order */
22 uint32_t ip_high
; /**< The highest IP in the range, in host order */
23 intptr_t country
; /**< An index into geoip_countries */
27 #define REQUEST_HIST_LEN 3
28 #define REQUEST_HIST_PERIOD (8*60*60)
30 typedef struct geoip_country_t
{
32 uint32_t n_v2_ns_requests
[REQUEST_HIST_LEN
];
33 uint32_t n_v3_ns_requests
[REQUEST_HIST_LEN
];
36 /** A list of geoip_country_t */
37 static smartlist_t
*geoip_countries
= NULL
;
38 /** A map from lowercased country codes to their position in geoip_countries.
39 * The index is encoded in the pointer, and 1 is added so that NULL can mean
41 static strmap_t
*country_idxplus1_by_lc_code
= NULL
;
42 /** A list of all known geoip_entry_t, sorted by ip_low. */
43 static smartlist_t
*geoip_entries
= NULL
;
45 /** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and
46 * <b>high</b>, inclusive, to the 2-letter country code <b>country</b>.
49 geoip_add_entry(uint32_t low
, uint32_t high
, const char *country
)
58 _idxplus1
= strmap_get_lc(country_idxplus1_by_lc_code
, country
);
61 geoip_country_t
*c
= tor_malloc_zero(sizeof(geoip_country_t
));
62 strlcpy(c
->countrycode
, country
, sizeof(c
->countrycode
));
63 tor_strlower(c
->countrycode
);
64 smartlist_add(geoip_countries
, c
);
65 idx
= smartlist_len(geoip_countries
) - 1;
66 strmap_set_lc(country_idxplus1_by_lc_code
, country
, (void*)(idx
+1));
68 idx
= ((uintptr_t)_idxplus1
)-1;
71 geoip_country_t
*c
= smartlist_get(geoip_countries
, idx
);
72 tor_assert(!strcasecmp(c
->countrycode
, country
));
74 ent
= tor_malloc_zero(sizeof(geoip_entry_t
));
78 smartlist_add(geoip_entries
, ent
);
81 /** Add an entry to the GeoIP table, parsing it from <b>line</b>. The
82 * format is as for geoip_load_file(). */
84 geoip_parse_entry(const char *line
)
86 unsigned int low
, high
;
88 if (!geoip_countries
) {
89 geoip_countries
= smartlist_create();
90 geoip_entries
= smartlist_create();
91 country_idxplus1_by_lc_code
= strmap_new();
93 while (TOR_ISSPACE(*line
))
97 if (sscanf(line
,"%u,%u,%2s", &low
, &high
, b
) == 3) {
98 geoip_add_entry(low
, high
, b
);
100 } else if (sscanf(line
,"\"%u\",\"%u\",\"%2s\",", &low
, &high
, b
) == 3) {
101 geoip_add_entry(low
, high
, b
);
104 log_warn(LD_GENERAL
, "Unable to parse line from GEOIP file: %s",
110 /** Sorting helper: return -1, 1, or 0 based on comparison of two
113 _geoip_compare_entries(const void **_a
, const void **_b
)
115 const geoip_entry_t
*a
= *_a
, *b
= *_b
;
116 if (a
->ip_low
< b
->ip_low
)
118 else if (a
->ip_low
> b
->ip_low
)
124 /** bsearch helper: return -1, 1, or 0 based on comparison of an IP (a pointer
125 * to a uint32_t in host order) to a geoip_entry_t */
127 _geoip_compare_key_to_entry(const void *_key
, const void **_member
)
129 const uint32_t addr
= *(uint32_t *)_key
;
130 const geoip_entry_t
*entry
= *_member
;
131 if (addr
< entry
->ip_low
)
133 else if (addr
> entry
->ip_high
)
139 /** Return 1 if we should collect geoip stats on bridge users, and
140 * include them in our extrainfo descriptor. Else return 0. */
142 should_record_bridge_info(or_options_t
*options
)
144 return options
->BridgeRelay
&& options
->BridgeRecordUsageByCountry
;
147 /** Clear the GeoIP database and reload it from the file
148 * <b>filename</b>. Return 0 on success, -1 on failure.
150 * Recognized line formats are:
151 * INTIPLOW,INTIPHIGH,CC
153 * "INTIPLOW","INTIPHIGH","CC","CC3","COUNTRY NAME"
154 * where INTIPLOW and INTIPHIGH are IPv4 addresses encoded as 4-byte unsigned
155 * integers, and CC is a country code.
157 * It also recognizes, and skips over, blank lines and lines that start
158 * with '#' (comments).
161 geoip_load_file(const char *filename
, or_options_t
*options
)
164 int severity
= should_record_bridge_info(options
) ? LOG_WARN
: LOG_INFO
;
166 if (!(f
= fopen(filename
, "r"))) {
167 log_fn(severity
, LD_GENERAL
, "Failed to open GEOIP file %s.", filename
);
170 geoip_countries
= smartlist_create();
171 geoip_entries
= smartlist_create();
172 country_idxplus1_by_lc_code
= strmap_new();
173 log_info(LD_GENERAL
, "Parsing GEOIP file.");
176 if (fgets(buf
, (int)sizeof(buf
), f
) == NULL
)
178 /* FFFF track full country name. */
179 geoip_parse_entry(buf
);
181 /*XXXX020 abort and return -1 if no entries/illformed?*/
184 smartlist_sort(geoip_entries
, _geoip_compare_entries
);
188 /** Given an IP address in host order, return a number representing the
189 * country to which that address belongs, or -1 for unknown. The return value
190 * will always be less than geoip_get_n_countries(). To decode it,
191 * call geoip_get_country_name().
194 geoip_get_country_by_ip(uint32_t ipaddr
)
199 ent
= smartlist_bsearch(geoip_entries
, &ipaddr
, _geoip_compare_key_to_entry
);
200 return ent
? (int)ent
->country
: -1;
203 /** Return the number of countries recognized by the GeoIP database. */
205 geoip_get_n_countries(void)
207 return (int) smartlist_len(geoip_countries
);
210 /** Return the two-letter country code associated with the number <b>num</b>,
211 * or "??" for an unknown value. */
213 geoip_get_country_name(int num
)
215 if (geoip_countries
&& num
>= 0 && num
< smartlist_len(geoip_countries
)) {
216 geoip_country_t
*c
= smartlist_get(geoip_countries
, num
);
217 return c
->countrycode
;
222 /** Return true iff we have loaded a GeoIP database.*/
224 geoip_is_loaded(void)
226 return geoip_countries
!= NULL
&& geoip_entries
!= NULL
;
229 /** Entry in a map from IP address to the last time we've seen an incoming
230 * connection from that IP address. Used by bridges only, to track which
231 * countries have them blocked. */
232 typedef struct clientmap_entry_t
{
233 HT_ENTRY(clientmap_entry_t
) node
;
235 time_t last_seen
; /* The last 2 bits of this value hold the client
239 #define ACTION_MASK 3
241 /** Map from client IP address to last time seen. */
242 static HT_HEAD(clientmap
, clientmap_entry_t
) client_history
=
244 /** Time at which we started tracking client IP history. */
245 static time_t client_history_starts
= 0;
248 static time_t current_request_period_starts
= 0;
249 static int n_old_request_periods
= 0;
251 /** Hashtable helper: compute a hash of a clientmap_entry_t. */
252 static INLINE
unsigned
253 clientmap_entry_hash(const clientmap_entry_t
*a
)
255 return ht_improve_hash((unsigned) a
->ipaddr
);
257 /** Hashtable helper: compare two clientmap_entry_t values for equality. */
259 clientmap_entries_eq(const clientmap_entry_t
*a
, const clientmap_entry_t
*b
)
261 return a
->ipaddr
== b
->ipaddr
;
264 HT_PROTOTYPE(clientmap
, clientmap_entry_t
, node
, clientmap_entry_hash
,
265 clientmap_entries_eq
);
266 HT_GENERATE(clientmap
, clientmap_entry_t
, node
, clientmap_entry_hash
,
267 clientmap_entries_eq
, 0.6, malloc
, realloc
, free
);
269 /** Note that we've seen a client connect from the IP <b>addr</b> (host order)
270 * at time <b>now</b>. Ignored by all but bridges. */
272 geoip_note_client_seen(geoip_client_action_t action
,
273 uint32_t addr
, time_t now
)
275 or_options_t
*options
= get_options();
276 clientmap_entry_t lookup
, *ent
;
277 if (action
== GEOIP_CLIENT_CONNECT
) {
278 if (!(options
->BridgeRelay
&& options
->BridgeRecordUsageByCountry
))
281 #ifndef ENABLE_GEOIP_STATS
284 if (options
->BridgeRelay
|| options
->BridgeAuthoritativeDir
||
285 !options
->DirRecordUsageByCountry
)
291 while (current_request_period_starts
+ REQUEST_HIST_PERIOD
< now
) {
292 if (!geoip_countries
)
293 geoip_countries
= smartlist_create();
294 if (!current_request_period_starts
) {
295 current_request_period_starts
= now
;
298 SMARTLIST_FOREACH(geoip_countries
, geoip_country_t
*, c
, {
299 memmove(&c
->n_v2_ns_requests
[0], &c
->n_v2_ns_requests
[1],
300 sizeof(uint32_t)*(REQUEST_HIST_LEN
-1));
301 memmove(&c
->n_v3_ns_requests
[0], &c
->n_v3_ns_requests
[1],
302 sizeof(uint32_t)*(REQUEST_HIST_LEN
-1));
303 c
->n_v2_ns_requests
[REQUEST_HIST_LEN
-1] = 0;
304 c
->n_v3_ns_requests
[REQUEST_HIST_LEN
-1] = 0;
306 current_request_period_starts
+= REQUEST_HIST_PERIOD
;
307 if (n_old_request_periods
< REQUEST_HIST_LEN
-1)
308 ++n_old_request_periods
;
311 /* We use the low 3 bits of the time to encode the action. Since we're
312 * potentially remembering tons of clients, we don't want to make
313 * clientmap_entry_t larger than it has to be. */
314 now
= (now
& ~ACTION_MASK
) | (((int)action
) & ACTION_MASK
);
315 lookup
.ipaddr
= addr
;
316 ent
= HT_FIND(clientmap
, &client_history
, &lookup
);
318 ent
->last_seen
= now
;
320 ent
= tor_malloc_zero(sizeof(clientmap_entry_t
));
322 ent
->last_seen
= now
;
323 HT_INSERT(clientmap
, &client_history
, ent
);
326 if (action
== GEOIP_CLIENT_NETWORKSTATUS
||
327 action
== GEOIP_CLIENT_NETWORKSTATUS_V2
) {
328 int country_idx
= geoip_get_country_by_ip(addr
);
329 if (country_idx
>= 0 && country_idx
< smartlist_len(geoip_countries
)) {
330 geoip_country_t
*country
= smartlist_get(geoip_countries
, country_idx
);
331 if (action
== GEOIP_CLIENT_NETWORKSTATUS
)
332 ++country
->n_v3_ns_requests
[REQUEST_HIST_LEN
-1];
334 ++country
->n_v2_ns_requests
[REQUEST_HIST_LEN
-1];
338 if (!client_history_starts
) {
339 client_history_starts
= now
;
340 current_request_period_starts
= now
;
344 /** HT_FOREACH helper: remove a clientmap_entry_t from the hashtable if it's
345 * older than a certain time. */
347 _remove_old_client_helper(struct clientmap_entry_t
*ent
, void *_cutoff
)
349 time_t cutoff
= *(time_t*)_cutoff
;
350 if (ent
->last_seen
< cutoff
) {
358 /** Forget about all clients that haven't connected since <b>cutoff</b>. */
360 geoip_remove_old_clients(time_t cutoff
)
362 clientmap_HT_FOREACH_FN(&client_history
,
363 _remove_old_client_helper
,
365 if (client_history_starts
< cutoff
)
366 client_history_starts
= cutoff
;
369 /** Do not mention any country from which fewer than this number of IPs have
370 * connected. This conceivably avoids reporting information that could
371 * deanonymize users, though analysis is lacking. */
372 #define MIN_IPS_TO_NOTE_COUNTRY 0
373 /** Do not report any geoip data at all if we have fewer than this number of
374 * IPs to report about. */
375 #define MIN_IPS_TO_NOTE_ANYTHING 0
376 /** When reporting geoip data about countries, round up to the nearest
377 * multiple of this value. */
378 #define IP_GRANULARITY 8
380 /** Return the time at which we started recording geoip data. */
382 geoip_get_history_start(void)
384 return client_history_starts
;
387 /** Helper type: used to sort per-country totals by value. */
388 typedef struct c_hist_t
{
389 char country
[3]; /**< Two-letter country code. */
390 unsigned total
; /**< Total IP addresses seen in this country. */
393 /** Sorting helper: return -1, 1, or 0 based on comparison of two
394 * geoip_entry_t. Sort in descending order of total, and then by country
397 _c_hist_compare(const void **_a
, const void **_b
)
399 const c_hist_t
*a
= *_a
, *b
= *_b
;
400 if (a
->total
> b
->total
)
402 else if (a
->total
< b
->total
)
405 return strcmp(a
->country
, b
->country
);
409 #define GEOIP_MIN_OBSERVATION_TIME (12*60*60)
411 static INLINE
unsigned
412 round_to_next_multiple_of(unsigned number
, unsigned divisor
)
414 number
+= divisor
- 1;
415 number
-= number
% divisor
;
419 /** Return a newly allocated comma-separated string containing entries for all
420 * the countries from which we've seen enough clients connect. The entry
421 * format is cc=num where num is the number of IPs we've seen connecting from
422 * that country, and cc is a lowercased country code. Returns NULL if we don't
423 * want to export geoip data yet. */
425 geoip_get_client_history(time_t now
, geoip_client_action_t action
)
428 if (!geoip_is_loaded())
430 if (client_history_starts
< (now
- GEOIP_MIN_OBSERVATION_TIME
)) {
432 smartlist_t
*chunks
= NULL
;
433 smartlist_t
*entries
= NULL
;
434 int n_countries
= geoip_get_n_countries();
436 clientmap_entry_t
**ent
;
437 unsigned *counts
= tor_malloc_zero(sizeof(unsigned)*n_countries
);
439 unsigned granularity
= IP_GRANULARITY
;
440 #ifdef ENABLE_GEOIP_STATS
441 if (get_options()->DirRecordUsageByCountry
)
442 granularity
= get_options()->DirRecordUsageGranularity
;
444 HT_FOREACH(ent
, clientmap
, &client_history
) {
446 if (((*ent
)->last_seen
& ACTION_MASK
) != action
)
448 country
= geoip_get_country_by_ip((*ent
)->ipaddr
);
451 tor_assert(0 <= country
&& country
< n_countries
);
455 /* Don't record anything if we haven't seen enough IPs. */
456 #if (MIN_IPS_TO_NOTE_ANYTHING > 0)
457 if (total
< MIN_IPS_TO_NOTE_ANYTHING
)
460 /* Make a list of c_hist_t */
461 entries
= smartlist_create();
462 for (i
= 0; i
< n_countries
; ++i
) {
463 unsigned c
= counts
[i
];
464 const char *countrycode
;
466 /* Only report a country if it has a minimum number of IPs. */
467 #if (MIN_IPS_TO_NOTE_COUNTRY > 0)
468 if (c
>= MIN_IPS_TO_NOTE_COUNTRY
) {
472 c
= round_to_next_multiple_of(c
, granularity
);
473 countrycode
= geoip_get_country_name(i
);
474 ent
= tor_malloc(sizeof(c_hist_t
));
475 strlcpy(ent
->country
, countrycode
, sizeof(ent
->country
));
477 smartlist_add(entries
, ent
);
480 /* Sort entries. Note that we must do this _AFTER_ rounding, or else
481 * the sort order could leak info. */
482 smartlist_sort(entries
, _c_hist_compare
);
484 /* Build the result. */
485 chunks
= smartlist_create();
486 SMARTLIST_FOREACH(entries
, c_hist_t
*, ch
, {
487 tor_snprintf(buf
, sizeof(buf
), "%s=%u", ch
->country
, ch
->total
);
488 smartlist_add(chunks
, tor_strdup(buf
));
490 result
= smartlist_join_strings(chunks
, ",", 0, NULL
);
491 #if (MIN_IPS_TO_NOTE_ANYTHING > 0)
496 SMARTLIST_FOREACH(chunks
, char *, c
, tor_free(c
));
497 smartlist_free(chunks
);
500 SMARTLIST_FOREACH(entries
, c_hist_t
*, c
, tor_free(c
));
501 smartlist_free(entries
);
509 geoip_get_request_history(time_t now
, geoip_client_action_t action
)
511 smartlist_t
*entries
, *strings
;
513 unsigned granularity
= IP_GRANULARITY
;
514 #ifdef ENABLE_GEOIP_STATS
515 if (get_options()->DirRecordUsageByCountry
)
516 granularity
= get_options()->DirRecordUsageGranularity
;
519 if (client_history_starts
>= (now
- GEOIP_MIN_OBSERVATION_TIME
))
521 if (action
!= GEOIP_CLIENT_NETWORKSTATUS
&&
522 action
!= GEOIP_CLIENT_NETWORKSTATUS_V2
)
524 if (!geoip_countries
)
527 entries
= smartlist_create();
528 SMARTLIST_FOREACH(geoip_countries
, geoip_country_t
*, c
, {
529 uint32_t *n
= (action
== GEOIP_CLIENT_NETWORKSTATUS
)
530 ? c
->n_v3_ns_requests
: c
->n_v2_ns_requests
;
534 for (i
=0; i
< REQUEST_HIST_LEN
; ++i
)
538 ent
= tor_malloc_zero(sizeof(c_hist_t
));
539 strlcpy(ent
->country
, c
->countrycode
, sizeof(ent
->country
));
540 ent
->total
= round_to_next_multiple_of(tot
, granularity
);
541 smartlist_add(entries
, ent
);
543 smartlist_sort(entries
, _c_hist_compare
);
545 strings
= smartlist_create();
546 SMARTLIST_FOREACH(entries
, c_hist_t
*, ent
, {
548 tor_snprintf(buf
, sizeof(buf
), "%s=%u", ent
->country
, ent
->total
);
549 smartlist_add(strings
, tor_strdup(buf
));
551 result
= smartlist_join_strings(strings
, ",", 0, NULL
);
552 SMARTLIST_FOREACH(strings
, char *, cp
, tor_free(cp
));
553 SMARTLIST_FOREACH(entries
, c_hist_t
*, ent
, tor_free(ent
));
554 smartlist_free(strings
);
555 smartlist_free(entries
);
560 dump_geoip_stats(void)
562 #ifdef ENABLE_GEOIP_STATS
563 time_t now
= time(NULL
);
564 time_t request_start
;
565 char *filename
= get_datadir_fname("geoip-stats");
566 char *data_v2
= NULL
, *data_v3
= NULL
;
567 char since
[ISO_TIME_LEN
+1], written
[ISO_TIME_LEN
+1];
568 open_file_t
*open_file
= NULL
;
569 double v2_share
= 0.0, v3_share
= 0.0;
572 data_v2
= geoip_get_client_history(now
, GEOIP_CLIENT_NETWORKSTATUS_V2
);
573 data_v3
= geoip_get_client_history(now
, GEOIP_CLIENT_NETWORKSTATUS
);
574 format_iso_time(since
, geoip_get_history_start());
575 format_iso_time(written
, now
);
576 out
= start_writing_to_stdio_file(filename
, OPEN_FLAGS_REPLACE
,
580 if (fprintf(out
, "written %s\nstarted-at %s\nns-ips %s\nns-v2-ips %s\n",
582 data_v3
? data_v3
: "", data_v2
? data_v2
: "") < 0)
587 request_start
= current_request_period_starts
-
588 (n_old_request_periods
* REQUEST_HIST_PERIOD
);
589 format_iso_time(since
, request_start
);
590 data_v2
= geoip_get_request_history(now
, GEOIP_CLIENT_NETWORKSTATUS_V2
);
591 data_v3
= geoip_get_request_history(now
, GEOIP_CLIENT_NETWORKSTATUS
);
592 if (fprintf(out
, "requests-start %s\nn-ns-reqs %s\nn-v2-ns-reqs %s\n",
594 data_v3
? data_v3
: "", data_v2
? data_v2
: "") < 0)
596 if (!router_get_my_share_of_directory_requests(&v2_share
, &v3_share
)) {
597 if (fprintf(out
, "v2-ns-share %0.2lf%%\n", v2_share
*100) < 0)
599 if (fprintf(out
, "v3-ns-share %0.2lf%%\n", v3_share
*100) < 0)
603 finish_writing_to_file(open_file
);
607 abort_writing_to_file(open_file
);
614 /** Helper used to implement GETINFO ip-to-country/... controller command. */
616 getinfo_helper_geoip(control_connection_t
*control_conn
,
617 const char *question
, char **answer
)
620 if (geoip_is_loaded() && !strcmpstart(question
, "ip-to-country/")) {
624 question
+= strlen("ip-to-country/");
625 if (tor_inet_aton(question
, &in
) != 0) {
626 ip
= ntohl(in
.s_addr
);
627 c
= geoip_get_country_by_ip(ip
);
628 *answer
= tor_strdup(geoip_get_country_name(c
));
634 /** Release all storage held by the GeoIP database. */
638 if (geoip_countries
) {
639 SMARTLIST_FOREACH(geoip_countries
, geoip_country_t
*, c
, tor_free(c
));
640 smartlist_free(geoip_countries
);
642 if (country_idxplus1_by_lc_code
)
643 strmap_free(country_idxplus1_by_lc_code
, NULL
);
645 SMARTLIST_FOREACH(geoip_entries
, geoip_entry_t
*, ent
, tor_free(ent
));
646 smartlist_free(geoip_entries
);
648 geoip_countries
= NULL
;
649 country_idxplus1_by_lc_code
= NULL
;
650 geoip_entries
= NULL
;
653 /** Release all storage held in this file. */
657 clientmap_entry_t
**ent
, **next
, *this;
658 for (ent
= HT_START(clientmap
, &client_history
); ent
!= NULL
; ent
= next
) {
660 next
= HT_NEXT_RMV(clientmap
, &client_history
, ent
);
663 HT_CLEAR(clientmap
, &client_history
);