Report only the top 10 ports in exit-port stats.
[tor/rransom.git] / src / or / geoip.c
blobee8d72ee1def05e69cfeb6a21d73fa707881cd71
1 /* Copyright (c) 2007-2010, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file geoip.c
6 * \brief Functions related to maintaining an IP-to-country database;
7 * to summarizing client connections by country to entry guards, bridges,
8 * and directory servers; and for statistics on answering network status
9 * requests.
12 #define GEOIP_PRIVATE
13 #include "or.h"
14 #include "ht.h"
15 #include "config.h"
16 #include "control.h"
17 #include "dnsserv.h"
18 #include "geoip.h"
19 #include "routerlist.h"
21 static void clear_geoip_db(void);
22 static void init_geoip_countries(void);
24 /** An entry from the GeoIP file: maps an IP range to a country. */
25 typedef struct geoip_entry_t {
26 uint32_t ip_low; /**< The lowest IP in the range, in host order */
27 uint32_t ip_high; /**< The highest IP in the range, in host order */
28 intptr_t country; /**< An index into geoip_countries */
29 } geoip_entry_t;
31 /** A per-country record for GeoIP request history. */
32 typedef struct geoip_country_t {
33 char countrycode[3];
34 uint32_t n_v2_ns_requests;
35 uint32_t n_v3_ns_requests;
36 } geoip_country_t;
38 /** A list of geoip_country_t */
39 static smartlist_t *geoip_countries = NULL;
40 /** A map from lowercased country codes to their position in geoip_countries.
41 * The index is encoded in the pointer, and 1 is added so that NULL can mean
42 * 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 static smartlist_t *geoip_entries = NULL;
47 /** Return the index of the <b>country</b>'s entry in the GeoIP DB
48 * if it is a valid 2-letter country code, otherwise return -1.
50 country_t
51 geoip_get_country(const char *country)
53 void *_idxplus1;
54 intptr_t idx;
56 _idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
57 if (!_idxplus1)
58 return -1;
60 idx = ((uintptr_t)_idxplus1)-1;
61 return (country_t)idx;
64 /** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and
65 * <b>high</b>, inclusive, to the 2-letter country code <b>country</b>.
67 static void
68 geoip_add_entry(uint32_t low, uint32_t high, const char *country)
70 intptr_t idx;
71 geoip_entry_t *ent;
72 void *_idxplus1;
74 if (high < low)
75 return;
77 _idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
79 if (!_idxplus1) {
80 geoip_country_t *c = tor_malloc_zero(sizeof(geoip_country_t));
81 strlcpy(c->countrycode, country, sizeof(c->countrycode));
82 tor_strlower(c->countrycode);
83 smartlist_add(geoip_countries, c);
84 idx = smartlist_len(geoip_countries) - 1;
85 strmap_set_lc(country_idxplus1_by_lc_code, country, (void*)(idx+1));
86 } else {
87 idx = ((uintptr_t)_idxplus1)-1;
90 geoip_country_t *c = smartlist_get(geoip_countries, idx);
91 tor_assert(!strcasecmp(c->countrycode, country));
93 ent = tor_malloc_zero(sizeof(geoip_entry_t));
94 ent->ip_low = low;
95 ent->ip_high = high;
96 ent->country = idx;
97 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 /*private*/ int
103 geoip_parse_entry(const char *line)
105 unsigned int low, high;
106 char b[3];
107 if (!geoip_countries)
108 init_geoip_countries();
109 if (!geoip_entries)
110 geoip_entries = smartlist_create();
112 while (TOR_ISSPACE(*line))
113 ++line;
114 if (*line == '#')
115 return 0;
116 if (sscanf(line,"%u,%u,%2s", &low, &high, b) == 3) {
117 geoip_add_entry(low, high, b);
118 return 0;
119 } else if (sscanf(line,"\"%u\",\"%u\",\"%2s\",", &low, &high, b) == 3) {
120 geoip_add_entry(low, high, b);
121 return 0;
122 } else {
123 log_warn(LD_GENERAL, "Unable to parse line from GEOIP file: %s",
124 escaped(line));
125 return -1;
129 /** Sorting helper: return -1, 1, or 0 based on comparison of two
130 * geoip_entry_t */
131 static int
132 _geoip_compare_entries(const void **_a, const void **_b)
134 const geoip_entry_t *a = *_a, *b = *_b;
135 if (a->ip_low < b->ip_low)
136 return -1;
137 else if (a->ip_low > b->ip_low)
138 return 1;
139 else
140 return 0;
143 /** bsearch helper: return -1, 1, or 0 based on comparison of an IP (a pointer
144 * to a uint32_t in host order) to a geoip_entry_t */
145 static int
146 _geoip_compare_key_to_entry(const void *_key, const void **_member)
148 /* No alignment issue here, since _key really is a pointer to uint32_t */
149 const uint32_t addr = *(uint32_t *)_key;
150 const geoip_entry_t *entry = *_member;
151 if (addr < entry->ip_low)
152 return -1;
153 else if (addr > entry->ip_high)
154 return 1;
155 else
156 return 0;
159 /** Return 1 if we should collect geoip stats on bridge users, and
160 * include them in our extrainfo descriptor. Else return 0. */
162 should_record_bridge_info(or_options_t *options)
164 return options->BridgeRelay && options->BridgeRecordUsageByCountry;
167 /** Set up a new list of geoip countries with no countries (yet) set in it,
168 * except for the unknown country.
170 static void
171 init_geoip_countries(void)
173 geoip_country_t *geoip_unresolved;
174 geoip_countries = smartlist_create();
175 /* Add a geoip_country_t for requests that could not be resolved to a
176 * country as first element (index 0) to geoip_countries. */
177 geoip_unresolved = tor_malloc_zero(sizeof(geoip_country_t));
178 strlcpy(geoip_unresolved->countrycode, "??",
179 sizeof(geoip_unresolved->countrycode));
180 smartlist_add(geoip_countries, geoip_unresolved);
181 country_idxplus1_by_lc_code = strmap_new();
182 strmap_set_lc(country_idxplus1_by_lc_code, "??", (void*)(1));
185 /** Clear the GeoIP database and reload it from the file
186 * <b>filename</b>. Return 0 on success, -1 on failure.
188 * Recognized line formats are:
189 * INTIPLOW,INTIPHIGH,CC
190 * and
191 * "INTIPLOW","INTIPHIGH","CC","CC3","COUNTRY NAME"
192 * where INTIPLOW and INTIPHIGH are IPv4 addresses encoded as 4-byte unsigned
193 * integers, and CC is a country code.
195 * It also recognizes, and skips over, blank lines and lines that start
196 * with '#' (comments).
199 geoip_load_file(const char *filename, or_options_t *options)
201 FILE *f;
202 const char *msg = "";
203 int severity = options_need_geoip_info(options, &msg) ? LOG_WARN : LOG_INFO;
204 clear_geoip_db();
205 if (!(f = fopen(filename, "r"))) {
206 log_fn(severity, LD_GENERAL, "Failed to open GEOIP file %s. %s",
207 filename, msg);
208 return -1;
210 if (!geoip_countries)
211 init_geoip_countries();
212 if (geoip_entries) {
213 SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, e, tor_free(e));
214 smartlist_free(geoip_entries);
216 geoip_entries = smartlist_create();
217 log_notice(LD_GENERAL, "Parsing GEOIP file.");
218 while (!feof(f)) {
219 char buf[512];
220 if (fgets(buf, (int)sizeof(buf), f) == NULL)
221 break;
222 /* FFFF track full country name. */
223 geoip_parse_entry(buf);
225 /*XXXX abort and return -1 if no entries/illformed?*/
226 fclose(f);
228 smartlist_sort(geoip_entries, _geoip_compare_entries);
230 /* Okay, now we need to maybe change our mind about what is in which
231 * country. */
232 refresh_all_country_info();
234 return 0;
237 /** Given an IP address in host order, return a number representing the
238 * country to which that address belongs, -1 for "No geoip information
239 * available", or 0 for the 'unknown country'. The return value will always
240 * be less than geoip_get_n_countries(). To decode it, call
241 * geoip_get_country_name().
244 geoip_get_country_by_ip(uint32_t ipaddr)
246 geoip_entry_t *ent;
247 if (!geoip_entries)
248 return -1;
249 ent = smartlist_bsearch(geoip_entries, &ipaddr, _geoip_compare_key_to_entry);
250 return ent ? (int)ent->country : 0;
253 /** Return the number of countries recognized by the GeoIP database. */
255 geoip_get_n_countries(void)
257 if (!geoip_countries)
258 init_geoip_countries();
259 return (int) smartlist_len(geoip_countries);
262 /** Return the two-letter country code associated with the number <b>num</b>,
263 * or "??" for an unknown value. */
264 const char *
265 geoip_get_country_name(country_t num)
267 if (geoip_countries && num >= 0 && num < smartlist_len(geoip_countries)) {
268 geoip_country_t *c = smartlist_get(geoip_countries, num);
269 return c->countrycode;
270 } else
271 return "??";
274 /** Return true iff we have loaded a GeoIP database.*/
276 geoip_is_loaded(void)
278 return geoip_countries != NULL && geoip_entries != NULL;
281 /** Entry in a map from IP address to the last time we've seen an incoming
282 * connection from that IP address. Used by bridges only, to track which
283 * countries have them blocked. */
284 typedef struct clientmap_entry_t {
285 HT_ENTRY(clientmap_entry_t) node;
286 uint32_t ipaddr;
287 unsigned int last_seen_in_minutes:30;
288 unsigned int action:2;
289 } clientmap_entry_t;
291 #define ACTION_MASK 3
293 /** Map from client IP address to last time seen. */
294 static HT_HEAD(clientmap, clientmap_entry_t) client_history =
295 HT_INITIALIZER();
297 /** Hashtable helper: compute a hash of a clientmap_entry_t. */
298 static INLINE unsigned
299 clientmap_entry_hash(const clientmap_entry_t *a)
301 return ht_improve_hash((unsigned) a->ipaddr);
303 /** Hashtable helper: compare two clientmap_entry_t values for equality. */
304 static INLINE int
305 clientmap_entries_eq(const clientmap_entry_t *a, const clientmap_entry_t *b)
307 return a->ipaddr == b->ipaddr && a->action == b->action;
310 HT_PROTOTYPE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
311 clientmap_entries_eq);
312 HT_GENERATE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
313 clientmap_entries_eq, 0.6, malloc, realloc, free);
315 /** Clear history of connecting clients used by entry and bridge stats. */
316 static void
317 client_history_clear(void)
319 clientmap_entry_t **ent, **next, *this;
320 for (ent = HT_START(clientmap, &client_history); ent != NULL;
321 ent = next) {
322 if ((*ent)->action == GEOIP_CLIENT_CONNECT) {
323 this = *ent;
324 next = HT_NEXT_RMV(clientmap, &client_history, ent);
325 tor_free(this);
326 } else {
327 next = HT_NEXT(clientmap, &client_history, ent);
332 /** How often do we update our estimate which share of v2 and v3 directory
333 * requests is sent to us? We could as well trigger updates of shares from
334 * network status updates, but that means adding a lot of calls into code
335 * that is independent from geoip stats (and keeping them up-to-date). We
336 * are perfectly fine with an approximation of 15-minute granularity. */
337 #define REQUEST_SHARE_INTERVAL (15 * 60)
339 /** When did we last determine which share of v2 and v3 directory requests
340 * is sent to us? */
341 static time_t last_time_determined_shares = 0;
343 /** Sum of products of v2 shares times the number of seconds for which we
344 * consider these shares as valid. */
345 static double v2_share_times_seconds;
347 /** Sum of products of v3 shares times the number of seconds for which we
348 * consider these shares as valid. */
349 static double v3_share_times_seconds;
351 /** Number of seconds we are determining v2 and v3 shares. */
352 static int share_seconds;
354 /** Try to determine which fraction of v2 and v3 directory requests aimed at
355 * caches will be sent to us at time <b>now</b> and store that value in
356 * order to take a mean value later on. */
357 static void
358 geoip_determine_shares(time_t now)
360 double v2_share = 0.0, v3_share = 0.0;
361 if (router_get_my_share_of_directory_requests(&v2_share, &v3_share) < 0)
362 return;
363 if (last_time_determined_shares) {
364 v2_share_times_seconds += v2_share *
365 ((double) (now - last_time_determined_shares));
366 v3_share_times_seconds += v3_share *
367 ((double) (now - last_time_determined_shares));
368 share_seconds += (int)(now - last_time_determined_shares);
370 last_time_determined_shares = now;
373 /** Calculate which fraction of v2 and v3 directory requests aimed at caches
374 * have been sent to us since the last call of this function up to time
375 * <b>now</b>. Set *<b>v2_share_out</b> and *<b>v3_share_out</b> to the
376 * fractions of v2 and v3 protocol shares we expect to have seen. Reset
377 * counters afterwards. Return 0 on success, -1 on failure (e.g. when zero
378 * seconds have passed since the last call).*/
379 static int
380 geoip_get_mean_shares(time_t now, double *v2_share_out,
381 double *v3_share_out)
383 geoip_determine_shares(now);
384 if (!share_seconds)
385 return -1;
386 *v2_share_out = v2_share_times_seconds / ((double) share_seconds);
387 *v3_share_out = v3_share_times_seconds / ((double) share_seconds);
388 v2_share_times_seconds = v3_share_times_seconds = 0.0;
389 share_seconds = 0;
390 return 0;
393 /** Note that we've seen a client connect from the IP <b>addr</b> (host order)
394 * at time <b>now</b>. Ignored by all but bridges and directories if
395 * configured accordingly. */
396 void
397 geoip_note_client_seen(geoip_client_action_t action,
398 uint32_t addr, time_t now)
400 or_options_t *options = get_options();
401 clientmap_entry_t lookup, *ent;
402 if (action == GEOIP_CLIENT_CONNECT) {
403 /* Only remember statistics as entry guard or as bridge. */
404 if (!options->EntryStatistics &&
405 (!(options->BridgeRelay && options->BridgeRecordUsageByCountry)))
406 return;
407 } else {
408 if (options->BridgeRelay || options->BridgeAuthoritativeDir ||
409 !options->DirReqStatistics)
410 return;
413 lookup.ipaddr = addr;
414 lookup.action = (int)action;
415 ent = HT_FIND(clientmap, &client_history, &lookup);
416 if (ent) {
417 ent->last_seen_in_minutes = now / 60;
418 } else {
419 ent = tor_malloc_zero(sizeof(clientmap_entry_t));
420 ent->ipaddr = addr;
421 ent->last_seen_in_minutes = now / 60;
422 ent->action = (int)action;
423 HT_INSERT(clientmap, &client_history, ent);
426 if (action == GEOIP_CLIENT_NETWORKSTATUS ||
427 action == GEOIP_CLIENT_NETWORKSTATUS_V2) {
428 int country_idx = geoip_get_country_by_ip(addr);
429 if (country_idx < 0)
430 country_idx = 0; /** unresolved requests are stored at index 0. */
431 if (country_idx >= 0 && country_idx < smartlist_len(geoip_countries)) {
432 geoip_country_t *country = smartlist_get(geoip_countries, country_idx);
433 if (action == GEOIP_CLIENT_NETWORKSTATUS)
434 ++country->n_v3_ns_requests;
435 else
436 ++country->n_v2_ns_requests;
439 /* Periodically determine share of requests that we should see */
440 if (last_time_determined_shares + REQUEST_SHARE_INTERVAL < now)
441 geoip_determine_shares(now);
445 /** HT_FOREACH helper: remove a clientmap_entry_t from the hashtable if it's
446 * older than a certain time. */
447 static int
448 _remove_old_client_helper(struct clientmap_entry_t *ent, void *_cutoff)
450 time_t cutoff = *(time_t*)_cutoff / 60;
451 if (ent->last_seen_in_minutes < cutoff) {
452 tor_free(ent);
453 return 1;
454 } else {
455 return 0;
459 /** Forget about all clients that haven't connected since <b>cutoff</b>. */
460 void
461 geoip_remove_old_clients(time_t cutoff)
463 clientmap_HT_FOREACH_FN(&client_history,
464 _remove_old_client_helper,
465 &cutoff);
468 /** How many responses are we giving to clients requesting v2 network
469 * statuses? */
470 static uint32_t ns_v2_responses[GEOIP_NS_RESPONSE_NUM];
472 /** How many responses are we giving to clients requesting v3 network
473 * statuses? */
474 static uint32_t ns_v3_responses[GEOIP_NS_RESPONSE_NUM];
476 /** Note that we've rejected a client's request for a v2 or v3 network
477 * status, encoded in <b>action</b> for reason <b>reason</b> at time
478 * <b>now</b>. */
479 void
480 geoip_note_ns_response(geoip_client_action_t action,
481 geoip_ns_response_t response)
483 static int arrays_initialized = 0;
484 if (!get_options()->DirReqStatistics)
485 return;
486 if (!arrays_initialized) {
487 memset(ns_v2_responses, 0, sizeof(ns_v2_responses));
488 memset(ns_v3_responses, 0, sizeof(ns_v3_responses));
489 arrays_initialized = 1;
491 tor_assert(action == GEOIP_CLIENT_NETWORKSTATUS ||
492 action == GEOIP_CLIENT_NETWORKSTATUS_V2);
493 tor_assert(response < GEOIP_NS_RESPONSE_NUM);
494 if (action == GEOIP_CLIENT_NETWORKSTATUS)
495 ns_v3_responses[response]++;
496 else
497 ns_v2_responses[response]++;
500 /** Do not mention any country from which fewer than this number of IPs have
501 * connected. This conceivably avoids reporting information that could
502 * deanonymize users, though analysis is lacking. */
503 #define MIN_IPS_TO_NOTE_COUNTRY 1
504 /** Do not report any geoip data at all if we have fewer than this number of
505 * IPs to report about. */
506 #define MIN_IPS_TO_NOTE_ANYTHING 1
507 /** When reporting geoip data about countries, round up to the nearest
508 * multiple of this value. */
509 #define IP_GRANULARITY 8
511 /** Helper type: used to sort per-country totals by value. */
512 typedef struct c_hist_t {
513 char country[3]; /**< Two-letter country code. */
514 unsigned total; /**< Total IP addresses seen in this country. */
515 } c_hist_t;
517 /** Sorting helper: return -1, 1, or 0 based on comparison of two
518 * geoip_entry_t. Sort in descending order of total, and then by country
519 * code. */
520 static int
521 _c_hist_compare(const void **_a, const void **_b)
523 const c_hist_t *a = *_a, *b = *_b;
524 if (a->total > b->total)
525 return -1;
526 else if (a->total < b->total)
527 return 1;
528 else
529 return strcmp(a->country, b->country);
532 /** When there are incomplete directory requests at the end of a 24-hour
533 * period, consider those requests running for longer than this timeout as
534 * failed, the others as still running. */
535 #define DIRREQ_TIMEOUT (10*60)
537 /** Entry in a map from either conn->global_identifier for direct requests
538 * or a unique circuit identifier for tunneled requests to request time,
539 * response size, and completion time of a network status request. Used to
540 * measure download times of requests to derive average client
541 * bandwidths. */
542 typedef struct dirreq_map_entry_t {
543 HT_ENTRY(dirreq_map_entry_t) node;
544 /** Unique identifier for this network status request; this is either the
545 * conn->global_identifier of the dir conn (direct request) or a new
546 * locally unique identifier of a circuit (tunneled request). This ID is
547 * only unique among other direct or tunneled requests, respectively. */
548 uint64_t dirreq_id;
549 unsigned int state:3; /**< State of this directory request. */
550 unsigned int type:1; /**< Is this a direct or a tunneled request? */
551 unsigned int completed:1; /**< Is this request complete? */
552 unsigned int action:2; /**< Is this a v2 or v3 request? */
553 /** When did we receive the request and started sending the response? */
554 struct timeval request_time;
555 size_t response_size; /**< What is the size of the response in bytes? */
556 struct timeval completion_time; /**< When did the request succeed? */
557 } dirreq_map_entry_t;
559 /** Map of all directory requests asking for v2 or v3 network statuses in
560 * the current geoip-stats interval. Values are
561 * of type *<b>dirreq_map_entry_t</b>. */
562 static HT_HEAD(dirreqmap, dirreq_map_entry_t) dirreq_map =
563 HT_INITIALIZER();
565 static int
566 dirreq_map_ent_eq(const dirreq_map_entry_t *a,
567 const dirreq_map_entry_t *b)
569 return a->dirreq_id == b->dirreq_id && a->type == b->type;
572 static unsigned
573 dirreq_map_ent_hash(const dirreq_map_entry_t *entry)
575 unsigned u = (unsigned) entry->dirreq_id;
576 u += entry->type << 20;
577 return u;
580 HT_PROTOTYPE(dirreqmap, dirreq_map_entry_t, node, dirreq_map_ent_hash,
581 dirreq_map_ent_eq);
582 HT_GENERATE(dirreqmap, dirreq_map_entry_t, node, dirreq_map_ent_hash,
583 dirreq_map_ent_eq, 0.6, malloc, realloc, free);
585 /** Helper: Put <b>entry</b> into map of directory requests using
586 * <b>type</b> and <b>dirreq_id</b> as key parts. If there is
587 * already an entry for that key, print out a BUG warning and return. */
588 static void
589 _dirreq_map_put(dirreq_map_entry_t *entry, dirreq_type_t type,
590 uint64_t dirreq_id)
592 dirreq_map_entry_t *old_ent;
593 tor_assert(entry->type == type);
594 tor_assert(entry->dirreq_id == dirreq_id);
596 /* XXXX022 once we're sure the bug case never happens, we can switch
597 * to HT_INSERT */
598 old_ent = HT_REPLACE(dirreqmap, &dirreq_map, entry);
599 if (old_ent && old_ent != entry) {
600 log_warn(LD_BUG, "Error when putting directory request into local "
601 "map. There was already an entry for the same identifier.");
602 return;
606 /** Helper: Look up and return an entry in the map of directory requests
607 * using <b>type</b> and <b>dirreq_id</b> as key parts. If there
608 * is no such entry, return NULL. */
609 static dirreq_map_entry_t *
610 _dirreq_map_get(dirreq_type_t type, uint64_t dirreq_id)
612 dirreq_map_entry_t lookup;
613 lookup.type = type;
614 lookup.dirreq_id = dirreq_id;
615 return HT_FIND(dirreqmap, &dirreq_map, &lookup);
618 /** Note that an either direct or tunneled (see <b>type</b>) directory
619 * request for a network status with unique ID <b>dirreq_id</b> of size
620 * <b>response_size</b> and action <b>action</b> (either v2 or v3) has
621 * started. */
622 void
623 geoip_start_dirreq(uint64_t dirreq_id, size_t response_size,
624 geoip_client_action_t action, dirreq_type_t type)
626 dirreq_map_entry_t *ent;
627 if (!get_options()->DirReqStatistics)
628 return;
629 ent = tor_malloc_zero(sizeof(dirreq_map_entry_t));
630 ent->dirreq_id = dirreq_id;
631 tor_gettimeofday(&ent->request_time);
632 ent->response_size = response_size;
633 ent->action = action;
634 ent->type = type;
635 _dirreq_map_put(ent, type, dirreq_id);
638 /** Change the state of the either direct or tunneled (see <b>type</b>)
639 * directory request with <b>dirreq_id</b> to <b>new_state</b> and
640 * possibly mark it as completed. If no entry can be found for the given
641 * key parts (e.g., if this is a directory request that we are not
642 * measuring, or one that was started in the previous measurement period),
643 * or if the state cannot be advanced to <b>new_state</b>, do nothing. */
644 void
645 geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type,
646 dirreq_state_t new_state)
648 dirreq_map_entry_t *ent;
649 if (!get_options()->DirReqStatistics)
650 return;
651 ent = _dirreq_map_get(type, dirreq_id);
652 if (!ent)
653 return;
654 if (new_state == DIRREQ_IS_FOR_NETWORK_STATUS)
655 return;
656 if (new_state - 1 != ent->state)
657 return;
658 ent->state = new_state;
659 if ((type == DIRREQ_DIRECT &&
660 new_state == DIRREQ_FLUSHING_DIR_CONN_FINISHED) ||
661 (type == DIRREQ_TUNNELED &&
662 new_state == DIRREQ_OR_CONN_BUFFER_FLUSHED)) {
663 tor_gettimeofday(&ent->completion_time);
664 ent->completed = 1;
668 /** Return a newly allocated comma-separated string containing statistics
669 * on network status downloads. The string contains the number of completed
670 * requests, timeouts, and still running requests as well as the download
671 * times by deciles and quartiles. Return NULL if we have not observed
672 * requests for long enough. */
673 static char *
674 geoip_get_dirreq_history(geoip_client_action_t action,
675 dirreq_type_t type)
677 char *result = NULL;
678 smartlist_t *dirreq_completed = NULL;
679 uint32_t complete = 0, timeouts = 0, running = 0;
680 int bufsize = 1024, written;
681 dirreq_map_entry_t **ptr, **next, *ent;
682 struct timeval now;
684 tor_gettimeofday(&now);
685 if (action != GEOIP_CLIENT_NETWORKSTATUS &&
686 action != GEOIP_CLIENT_NETWORKSTATUS_V2)
687 return NULL;
688 dirreq_completed = smartlist_create();
689 for (ptr = HT_START(dirreqmap, &dirreq_map); ptr; ptr = next) {
690 ent = *ptr;
691 if (ent->action != action || ent->type != type) {
692 next = HT_NEXT(dirreqmap, &dirreq_map, ptr);
693 continue;
694 } else {
695 if (ent->completed) {
696 smartlist_add(dirreq_completed, ent);
697 complete++;
698 next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ptr);
699 } else {
700 if (tv_mdiff(&ent->request_time, &now) / 1000 > DIRREQ_TIMEOUT)
701 timeouts++;
702 else
703 running++;
704 next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ptr);
705 tor_free(ent);
709 #define DIR_REQ_GRANULARITY 4
710 complete = round_uint32_to_next_multiple_of(complete,
711 DIR_REQ_GRANULARITY);
712 timeouts = round_uint32_to_next_multiple_of(timeouts,
713 DIR_REQ_GRANULARITY);
714 running = round_uint32_to_next_multiple_of(running,
715 DIR_REQ_GRANULARITY);
716 result = tor_malloc_zero(bufsize);
717 written = tor_snprintf(result, bufsize, "complete=%u,timeout=%u,"
718 "running=%u", complete, timeouts, running);
719 if (written < 0) {
720 tor_free(result);
721 goto done;
724 #define MIN_DIR_REQ_RESPONSES 16
725 if (complete >= MIN_DIR_REQ_RESPONSES) {
726 uint32_t *dltimes;
727 /* We may have rounded 'completed' up. Here we want to use the
728 * real value. */
729 complete = smartlist_len(dirreq_completed);
730 dltimes = tor_malloc_zero(sizeof(uint32_t) * complete);
731 SMARTLIST_FOREACH_BEGIN(dirreq_completed, dirreq_map_entry_t *, ent) {
732 uint32_t bytes_per_second;
733 uint32_t time_diff = (uint32_t) tv_mdiff(&ent->request_time,
734 &ent->completion_time);
735 if (time_diff == 0)
736 time_diff = 1; /* Avoid DIV/0; "instant" answers are impossible
737 * by law of nature or something, but a milisecond
738 * is a bit greater than "instantly" */
739 bytes_per_second = (uint32_t)(1000 * ent->response_size / time_diff);
740 dltimes[ent_sl_idx] = bytes_per_second;
741 } SMARTLIST_FOREACH_END(ent);
742 median_uint32(dltimes, complete); /* sorts as a side effect. */
743 written = tor_snprintf(result + written, bufsize - written,
744 ",min=%u,d1=%u,d2=%u,q1=%u,d3=%u,d4=%u,md=%u,"
745 "d6=%u,d7=%u,q3=%u,d8=%u,d9=%u,max=%u",
746 dltimes[0],
747 dltimes[1*complete/10-1],
748 dltimes[2*complete/10-1],
749 dltimes[1*complete/4-1],
750 dltimes[3*complete/10-1],
751 dltimes[4*complete/10-1],
752 dltimes[5*complete/10-1],
753 dltimes[6*complete/10-1],
754 dltimes[7*complete/10-1],
755 dltimes[3*complete/4-1],
756 dltimes[8*complete/10-1],
757 dltimes[9*complete/10-1],
758 dltimes[complete-1]);
759 if (written<0)
760 tor_free(result);
761 tor_free(dltimes);
763 done:
764 SMARTLIST_FOREACH(dirreq_completed, dirreq_map_entry_t *, ent,
765 tor_free(ent));
766 smartlist_free(dirreq_completed);
767 return result;
770 /** Return a newly allocated comma-separated string containing entries for
771 * all the countries from which we've seen enough clients connect as a
772 * bridge, directory server, or entry guard. The entry format is cc=num
773 * where num is the number of IPs we've seen connecting from that country,
774 * and cc is a lowercased country code. Returns NULL if we don't want
775 * to export geoip data yet. */
776 char *
777 geoip_get_client_history(geoip_client_action_t action)
779 char *result = NULL;
780 unsigned granularity = IP_GRANULARITY;
781 smartlist_t *chunks = NULL;
782 smartlist_t *entries = NULL;
783 int n_countries = geoip_get_n_countries();
784 int i;
785 clientmap_entry_t **ent;
786 unsigned *counts = NULL;
787 unsigned total = 0;
789 if (!geoip_is_loaded())
790 return NULL;
792 counts = tor_malloc_zero(sizeof(unsigned)*n_countries);
793 HT_FOREACH(ent, clientmap, &client_history) {
794 int country;
795 if ((*ent)->action != (int)action)
796 continue;
797 country = geoip_get_country_by_ip((*ent)->ipaddr);
798 if (country < 0)
799 country = 0; /** unresolved requests are stored at index 0. */
800 tor_assert(0 <= country && country < n_countries);
801 ++counts[country];
802 ++total;
804 /* Don't record anything if we haven't seen enough IPs. */
805 if (total < MIN_IPS_TO_NOTE_ANYTHING)
806 goto done;
807 /* Make a list of c_hist_t */
808 entries = smartlist_create();
809 for (i = 0; i < n_countries; ++i) {
810 unsigned c = counts[i];
811 const char *countrycode;
812 c_hist_t *ent;
813 /* Only report a country if it has a minimum number of IPs. */
814 if (c >= MIN_IPS_TO_NOTE_COUNTRY) {
815 c = round_to_next_multiple_of(c, granularity);
816 countrycode = geoip_get_country_name(i);
817 ent = tor_malloc(sizeof(c_hist_t));
818 strlcpy(ent->country, countrycode, sizeof(ent->country));
819 ent->total = c;
820 smartlist_add(entries, ent);
823 /* Sort entries. Note that we must do this _AFTER_ rounding, or else
824 * the sort order could leak info. */
825 smartlist_sort(entries, _c_hist_compare);
827 /* Build the result. */
828 chunks = smartlist_create();
829 SMARTLIST_FOREACH(entries, c_hist_t *, ch, {
830 char *buf=NULL;
831 tor_asprintf(&buf, "%s=%u", ch->country, ch->total);
832 smartlist_add(chunks, buf);
834 result = smartlist_join_strings(chunks, ",", 0, NULL);
835 done:
836 tor_free(counts);
837 if (chunks) {
838 SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
839 smartlist_free(chunks);
841 if (entries) {
842 SMARTLIST_FOREACH(entries, c_hist_t *, c, tor_free(c));
843 smartlist_free(entries);
845 return result;
848 /** Return a newly allocated string holding the per-country request history
849 * for <b>action</b> in a format suitable for an extra-info document, or NULL
850 * on failure. */
851 char *
852 geoip_get_request_history(geoip_client_action_t action)
854 smartlist_t *entries, *strings;
855 char *result;
856 unsigned granularity = IP_GRANULARITY;
858 if (action != GEOIP_CLIENT_NETWORKSTATUS &&
859 action != GEOIP_CLIENT_NETWORKSTATUS_V2)
860 return NULL;
861 if (!geoip_countries)
862 return NULL;
864 entries = smartlist_create();
865 SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
866 uint32_t tot = 0;
867 c_hist_t *ent;
868 tot = (action == GEOIP_CLIENT_NETWORKSTATUS) ?
869 c->n_v3_ns_requests : c->n_v2_ns_requests;
870 if (!tot)
871 continue;
872 ent = tor_malloc_zero(sizeof(c_hist_t));
873 strlcpy(ent->country, c->countrycode, sizeof(ent->country));
874 ent->total = round_to_next_multiple_of(tot, granularity);
875 smartlist_add(entries, ent);
877 smartlist_sort(entries, _c_hist_compare);
879 strings = smartlist_create();
880 SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
881 char *buf = NULL;
882 tor_asprintf(&buf, "%s=%u", ent->country, ent->total);
883 smartlist_add(strings, buf);
885 result = smartlist_join_strings(strings, ",", 0, NULL);
886 SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp));
887 SMARTLIST_FOREACH(entries, c_hist_t *, ent, tor_free(ent));
888 smartlist_free(strings);
889 smartlist_free(entries);
890 return result;
893 /** Start time of directory request stats or 0 if we're not collecting
894 * directory request statistics. */
895 static time_t start_of_dirreq_stats_interval;
897 /** Initialize directory request stats. */
898 void
899 geoip_dirreq_stats_init(time_t now)
901 start_of_dirreq_stats_interval = now;
904 /** Stop collecting directory request stats in a way that we can re-start
905 * doing so in geoip_dirreq_stats_init(). */
906 void
907 geoip_dirreq_stats_term(void)
909 SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
910 c->n_v2_ns_requests = c->n_v3_ns_requests = 0;
913 clientmap_entry_t **ent, **next, *this;
914 for (ent = HT_START(clientmap, &client_history); ent != NULL;
915 ent = next) {
916 if ((*ent)->action == GEOIP_CLIENT_NETWORKSTATUS ||
917 (*ent)->action == GEOIP_CLIENT_NETWORKSTATUS_V2) {
918 this = *ent;
919 next = HT_NEXT_RMV(clientmap, &client_history, ent);
920 tor_free(this);
921 } else {
922 next = HT_NEXT(clientmap, &client_history, ent);
926 v2_share_times_seconds = v3_share_times_seconds = 0.0;
927 last_time_determined_shares = 0;
928 share_seconds = 0;
929 memset(ns_v2_responses, 0, sizeof(ns_v2_responses));
930 memset(ns_v3_responses, 0, sizeof(ns_v3_responses));
932 dirreq_map_entry_t **ent, **next, *this;
933 for (ent = HT_START(dirreqmap, &dirreq_map); ent != NULL; ent = next) {
934 this = *ent;
935 next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ent);
936 tor_free(this);
939 start_of_dirreq_stats_interval = 0;
942 /** Write dirreq statistics to $DATADIR/stats/dirreq-stats and return when
943 * we would next want to write. */
944 time_t
945 geoip_dirreq_stats_write(time_t now)
947 char *statsdir = NULL, *filename = NULL;
948 char *data_v2 = NULL, *data_v3 = NULL;
949 char written[ISO_TIME_LEN+1];
950 open_file_t *open_file = NULL;
951 double v2_share = 0.0, v3_share = 0.0;
952 FILE *out;
953 int i;
955 if (!start_of_dirreq_stats_interval)
956 return 0; /* Not initialized. */
957 if (start_of_dirreq_stats_interval + WRITE_STATS_INTERVAL > now)
958 goto done; /* Not ready to write. */
960 /* Discard all items in the client history that are too old. */
961 geoip_remove_old_clients(start_of_dirreq_stats_interval);
963 statsdir = get_datadir_fname("stats");
964 if (check_private_dir(statsdir, CPD_CREATE) < 0)
965 goto done;
966 filename = get_datadir_fname2("stats", "dirreq-stats");
967 data_v2 = geoip_get_client_history(GEOIP_CLIENT_NETWORKSTATUS_V2);
968 data_v3 = geoip_get_client_history(GEOIP_CLIENT_NETWORKSTATUS);
969 format_iso_time(written, now);
970 out = start_writing_to_stdio_file(filename, OPEN_FLAGS_APPEND,
971 0600, &open_file);
972 if (!out)
973 goto done;
974 if (fprintf(out, "dirreq-stats-end %s (%d s)\ndirreq-v3-ips %s\n"
975 "dirreq-v2-ips %s\n", written,
976 (unsigned) (now - start_of_dirreq_stats_interval),
977 data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
978 goto done;
979 tor_free(data_v2);
980 tor_free(data_v3);
982 data_v2 = geoip_get_request_history(GEOIP_CLIENT_NETWORKSTATUS_V2);
983 data_v3 = geoip_get_request_history(GEOIP_CLIENT_NETWORKSTATUS);
984 if (fprintf(out, "dirreq-v3-reqs %s\ndirreq-v2-reqs %s\n",
985 data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
986 goto done;
987 tor_free(data_v2);
988 tor_free(data_v3);
989 SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
990 c->n_v2_ns_requests = c->n_v3_ns_requests = 0;
992 #define RESPONSE_GRANULARITY 8
993 for (i = 0; i < GEOIP_NS_RESPONSE_NUM; i++) {
994 ns_v2_responses[i] = round_uint32_to_next_multiple_of(
995 ns_v2_responses[i], RESPONSE_GRANULARITY);
996 ns_v3_responses[i] = round_uint32_to_next_multiple_of(
997 ns_v3_responses[i], RESPONSE_GRANULARITY);
999 #undef RESPONSE_GRANULARITY
1000 if (fprintf(out, "dirreq-v3-resp ok=%u,not-enough-sigs=%u,unavailable=%u,"
1001 "not-found=%u,not-modified=%u,busy=%u\n",
1002 ns_v3_responses[GEOIP_SUCCESS],
1003 ns_v3_responses[GEOIP_REJECT_NOT_ENOUGH_SIGS],
1004 ns_v3_responses[GEOIP_REJECT_UNAVAILABLE],
1005 ns_v3_responses[GEOIP_REJECT_NOT_FOUND],
1006 ns_v3_responses[GEOIP_REJECT_NOT_MODIFIED],
1007 ns_v3_responses[GEOIP_REJECT_BUSY]) < 0)
1008 goto done;
1009 if (fprintf(out, "dirreq-v2-resp ok=%u,unavailable=%u,"
1010 "not-found=%u,not-modified=%u,busy=%u\n",
1011 ns_v2_responses[GEOIP_SUCCESS],
1012 ns_v2_responses[GEOIP_REJECT_UNAVAILABLE],
1013 ns_v2_responses[GEOIP_REJECT_NOT_FOUND],
1014 ns_v2_responses[GEOIP_REJECT_NOT_MODIFIED],
1015 ns_v2_responses[GEOIP_REJECT_BUSY]) < 0)
1016 goto done;
1017 memset(ns_v2_responses, 0, sizeof(ns_v2_responses));
1018 memset(ns_v3_responses, 0, sizeof(ns_v3_responses));
1019 if (!geoip_get_mean_shares(now, &v2_share, &v3_share)) {
1020 if (fprintf(out, "dirreq-v2-share %0.2lf%%\n", v2_share*100) < 0)
1021 goto done;
1022 if (fprintf(out, "dirreq-v3-share %0.2lf%%\n", v3_share*100) < 0)
1023 goto done;
1026 data_v2 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS_V2,
1027 DIRREQ_DIRECT);
1028 data_v3 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS,
1029 DIRREQ_DIRECT);
1030 if (fprintf(out, "dirreq-v3-direct-dl %s\ndirreq-v2-direct-dl %s\n",
1031 data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
1032 goto done;
1033 tor_free(data_v2);
1034 tor_free(data_v3);
1035 data_v2 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS_V2,
1036 DIRREQ_TUNNELED);
1037 data_v3 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS,
1038 DIRREQ_TUNNELED);
1039 if (fprintf(out, "dirreq-v3-tunneled-dl %s\ndirreq-v2-tunneled-dl %s\n",
1040 data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
1041 goto done;
1043 finish_writing_to_file(open_file);
1044 open_file = NULL;
1046 start_of_dirreq_stats_interval = now;
1048 done:
1049 if (open_file)
1050 abort_writing_to_file(open_file);
1051 tor_free(filename);
1052 tor_free(statsdir);
1053 tor_free(data_v2);
1054 tor_free(data_v3);
1055 return start_of_dirreq_stats_interval + WRITE_STATS_INTERVAL;
1058 /** Start time of bridge stats or 0 if we're not collecting bridge
1059 * statistics. */
1060 static time_t start_of_bridge_stats_interval;
1062 /** Initialize bridge stats. */
1063 void
1064 geoip_bridge_stats_init(time_t now)
1066 start_of_bridge_stats_interval = now;
1069 /** Stop collecting bridge stats in a way that we can re-start doing so in
1070 * geoip_bridge_stats_init(). */
1071 void
1072 geoip_bridge_stats_term(void)
1074 client_history_clear();
1075 start_of_bridge_stats_interval = 0;
1078 /** Parse the bridge statistics as they are written to extra-info
1079 * descriptors for being returned to controller clients. Return the
1080 * controller string if successful, or NULL otherwise. */
1081 static char *
1082 parse_bridge_stats_controller(const char *stats_str, time_t now)
1084 char stats_end_str[ISO_TIME_LEN+1], stats_start_str[ISO_TIME_LEN+1],
1085 *controller_str, *eos, *eol, *summary;
1087 const char *BRIDGE_STATS_END = "bridge-stats-end ";
1088 const char *BRIDGE_IPS = "bridge-ips ";
1089 const char *BRIDGE_IPS_EMPTY_LINE = "bridge-ips\n";
1090 const char *tmp;
1091 time_t stats_end_time;
1092 int seconds;
1093 tor_assert(stats_str);
1095 /* Parse timestamp and number of seconds from
1096 "bridge-stats-end YYYY-MM-DD HH:MM:SS (N s)" */
1097 tmp = find_str_at_start_of_line(stats_str, BRIDGE_STATS_END);
1098 if (!tmp)
1099 return NULL;
1100 tmp += strlen(BRIDGE_STATS_END);
1102 if (strlen(tmp) < ISO_TIME_LEN + 6)
1103 return NULL;
1104 strlcpy(stats_end_str, tmp, sizeof(stats_end_str));
1105 if (parse_iso_time(stats_end_str, &stats_end_time) < 0)
1106 return NULL;
1107 if (stats_end_time < now - (25*60*60) ||
1108 stats_end_time > now + (1*60*60))
1109 return NULL;
1110 seconds = (int)strtol(tmp + ISO_TIME_LEN + 2, &eos, 10);
1111 if (!eos || seconds < 23*60*60)
1112 return NULL;
1113 format_iso_time(stats_start_str, stats_end_time - seconds);
1115 /* Parse: "bridge-ips CC=N,CC=N,..." */
1116 tmp = find_str_at_start_of_line(stats_str, BRIDGE_IPS);
1117 if (tmp) {
1118 tmp += strlen(BRIDGE_IPS);
1119 tmp = eat_whitespace_no_nl(tmp);
1120 eol = strchr(tmp, '\n');
1121 if (eol)
1122 summary = tor_strndup(tmp, eol-tmp);
1123 else
1124 summary = tor_strdup(tmp);
1125 } else {
1126 /* Look if there is an empty "bridge-ips" line */
1127 tmp = find_str_at_start_of_line(stats_str, BRIDGE_IPS_EMPTY_LINE);
1128 if (!tmp)
1129 return NULL;
1130 summary = tor_strdup("");
1133 tor_asprintf(&controller_str,
1134 "TimeStarted=\"%s\" CountrySummary=%s",
1135 stats_start_str, summary);
1136 tor_free(summary);
1137 return controller_str;
1140 /** Most recent bridge statistics formatted to be written to extra-info
1141 * descriptors. */
1142 static char *bridge_stats_extrainfo = NULL;
1144 /** Most recent bridge statistics formatted to be returned to controller
1145 * clients. */
1146 static char *bridge_stats_controller = NULL;
1148 /** Write bridge statistics to $DATADIR/stats/bridge-stats and return
1149 * when we should next try to write statistics. */
1150 time_t
1151 geoip_bridge_stats_write(time_t now)
1153 char *statsdir = NULL, *filename = NULL, *data = NULL,
1154 written[ISO_TIME_LEN+1], *out = NULL, *controller_str;
1155 size_t len;
1157 /* Check if 24 hours have passed since starting measurements. */
1158 if (now < start_of_bridge_stats_interval + WRITE_STATS_INTERVAL)
1159 return start_of_bridge_stats_interval + WRITE_STATS_INTERVAL;
1161 /* Discard all items in the client history that are too old. */
1162 geoip_remove_old_clients(start_of_bridge_stats_interval);
1164 statsdir = get_datadir_fname("stats");
1165 if (check_private_dir(statsdir, CPD_CREATE) < 0)
1166 goto done;
1167 filename = get_datadir_fname2("stats", "bridge-stats");
1168 data = geoip_get_client_history(GEOIP_CLIENT_CONNECT);
1169 format_iso_time(written, now);
1170 len = strlen("bridge-stats-end (999999 s)\nbridge-ips \n") +
1171 ISO_TIME_LEN + (data ? strlen(data) : 0) + 42;
1172 out = tor_malloc(len);
1173 if (tor_snprintf(out, len, "bridge-stats-end %s (%u s)\nbridge-ips %s\n",
1174 written, (unsigned) (now - start_of_bridge_stats_interval),
1175 data ? data : "") < 0)
1176 goto done;
1177 write_str_to_file(filename, out, 0);
1178 controller_str = parse_bridge_stats_controller(out, now);
1179 if (!controller_str)
1180 goto done;
1181 start_of_bridge_stats_interval = now;
1182 tor_free(bridge_stats_extrainfo);
1183 tor_free(bridge_stats_controller);
1184 bridge_stats_extrainfo = out;
1185 out = NULL;
1186 bridge_stats_controller = controller_str;
1187 control_event_clients_seen(controller_str);
1188 done:
1189 tor_free(filename);
1190 tor_free(statsdir);
1191 tor_free(data);
1192 tor_free(out);
1193 return start_of_bridge_stats_interval +
1194 WRITE_STATS_INTERVAL;
1197 /** Try to load the most recent bridge statistics from disk, unless we
1198 * have finished a measurement interval lately. */
1199 static void
1200 load_bridge_stats(time_t now)
1202 char *statsdir, *fname=NULL, *contents, *controller_str;
1203 if (bridge_stats_extrainfo)
1204 return;
1205 statsdir = get_datadir_fname("stats");
1206 if (check_private_dir(statsdir, CPD_CREATE) < 0)
1207 goto done;
1208 fname = get_datadir_fname2("stats", "bridge-stats");
1209 contents = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
1210 if (contents) {
1211 controller_str = parse_bridge_stats_controller(contents, now);
1212 if (controller_str) {
1213 bridge_stats_extrainfo = contents;
1214 bridge_stats_controller = controller_str;
1215 } else {
1216 tor_free(contents);
1219 done:
1220 tor_free(fname);
1221 tor_free(statsdir);
1224 /** Return most recent bridge statistics for inclusion in extra-info
1225 * descriptors, or NULL if we don't have recent bridge statistics. */
1226 const char *
1227 geoip_get_bridge_stats_extrainfo(time_t now)
1229 load_bridge_stats(now);
1230 return bridge_stats_extrainfo;
1233 /** Return most recent bridge statistics to be returned to controller
1234 * clients, or NULL if we don't have recent bridge statistics. */
1235 const char *
1236 geoip_get_bridge_stats_controller(time_t now)
1238 load_bridge_stats(now);
1239 return bridge_stats_controller;
1242 /** Start time of entry stats or 0 if we're not collecting entry
1243 * statistics. */
1244 static time_t start_of_entry_stats_interval;
1246 /** Initialize entry stats. */
1247 void
1248 geoip_entry_stats_init(time_t now)
1250 start_of_entry_stats_interval = now;
1253 /** Stop collecting entry stats in a way that we can re-start doing so in
1254 * geoip_entry_stats_init(). */
1255 void
1256 geoip_entry_stats_term(void)
1258 client_history_clear();
1259 start_of_entry_stats_interval = 0;
1262 /** Write entry statistics to $DATADIR/stats/entry-stats and return time
1263 * when we would next want to write. */
1264 time_t
1265 geoip_entry_stats_write(time_t now)
1267 char *statsdir = NULL, *filename = NULL;
1268 char *data = NULL;
1269 char written[ISO_TIME_LEN+1];
1270 open_file_t *open_file = NULL;
1271 FILE *out;
1273 if (!start_of_entry_stats_interval)
1274 return 0; /* Not initialized. */
1275 if (start_of_entry_stats_interval + WRITE_STATS_INTERVAL > now)
1276 goto done; /* Not ready to write. */
1278 /* Discard all items in the client history that are too old. */
1279 geoip_remove_old_clients(start_of_entry_stats_interval);
1281 statsdir = get_datadir_fname("stats");
1282 if (check_private_dir(statsdir, CPD_CREATE) < 0)
1283 goto done;
1284 filename = get_datadir_fname2("stats", "entry-stats");
1285 data = geoip_get_client_history(GEOIP_CLIENT_CONNECT);
1286 format_iso_time(written, now);
1287 out = start_writing_to_stdio_file(filename, OPEN_FLAGS_APPEND,
1288 0600, &open_file);
1289 if (!out)
1290 goto done;
1291 if (fprintf(out, "entry-stats-end %s (%u s)\nentry-ips %s\n",
1292 written, (unsigned) (now - start_of_entry_stats_interval),
1293 data ? data : "") < 0)
1294 goto done;
1296 start_of_entry_stats_interval = now;
1298 finish_writing_to_file(open_file);
1299 open_file = NULL;
1300 done:
1301 if (open_file)
1302 abort_writing_to_file(open_file);
1303 tor_free(filename);
1304 tor_free(statsdir);
1305 tor_free(data);
1306 return start_of_entry_stats_interval + WRITE_STATS_INTERVAL;
1309 /** Helper used to implement GETINFO ip-to-country/... controller command. */
1311 getinfo_helper_geoip(control_connection_t *control_conn,
1312 const char *question, char **answer,
1313 const char **errmsg)
1315 (void)control_conn;
1316 if (!geoip_is_loaded()) {
1317 *errmsg = "GeoIP data not loaded";
1318 return -1;
1320 if (!strcmpstart(question, "ip-to-country/")) {
1321 int c;
1322 uint32_t ip;
1323 struct in_addr in;
1324 question += strlen("ip-to-country/");
1325 if (tor_inet_aton(question, &in) != 0) {
1326 ip = ntohl(in.s_addr);
1327 c = geoip_get_country_by_ip(ip);
1328 *answer = tor_strdup(geoip_get_country_name(c));
1331 return 0;
1334 /** Release all storage held by the GeoIP database. */
1335 static void
1336 clear_geoip_db(void)
1338 if (geoip_countries) {
1339 SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, tor_free(c));
1340 smartlist_free(geoip_countries);
1343 strmap_free(country_idxplus1_by_lc_code, NULL);
1344 if (geoip_entries) {
1345 SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, ent, tor_free(ent));
1346 smartlist_free(geoip_entries);
1348 geoip_countries = NULL;
1349 country_idxplus1_by_lc_code = NULL;
1350 geoip_entries = NULL;
1353 /** Release all storage held in this file. */
1354 void
1355 geoip_free_all(void)
1358 clientmap_entry_t **ent, **next, *this;
1359 for (ent = HT_START(clientmap, &client_history); ent != NULL; ent = next) {
1360 this = *ent;
1361 next = HT_NEXT_RMV(clientmap, &client_history, ent);
1362 tor_free(this);
1364 HT_CLEAR(clientmap, &client_history);
1367 dirreq_map_entry_t **ent, **next, *this;
1368 for (ent = HT_START(dirreqmap, &dirreq_map); ent != NULL; ent = next) {
1369 this = *ent;
1370 next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ent);
1371 tor_free(this);
1373 HT_CLEAR(dirreqmap, &dirreq_map);
1376 clear_geoip_db();