Move the "nowhereland" logic into geoip.c
[tor.git] / src / or / geoip.c
blobadbad8af738beb0699cddbcf711afa0e20670bfa
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 and to
7 * summarizing client connections by country.
8 */
10 #define GEOIP_PRIVATE
11 #include "or.h"
12 #include "ht.h"
13 #include "config.h"
14 #include "control.h"
15 #include "dnsserv.h"
16 #include "geoip.h"
17 #include "routerlist.h"
19 static void clear_geoip_db(void);
21 /** An entry from the GeoIP file: maps an IP range to a country. */
22 typedef struct geoip_entry_t {
23 uint32_t ip_low; /**< The lowest IP in the range, in host order */
24 uint32_t ip_high; /**< The highest IP in the range, in host order */
25 intptr_t country; /**< An index into geoip_countries */
26 } geoip_entry_t;
28 /** For how many periods should we remember per-country request history? */
29 #define REQUEST_HIST_LEN 1
30 /** How long are the periods for which we should remember request history? */
31 #define REQUEST_HIST_PERIOD (24*60*60)
33 /** A per-country record for GeoIP request history. */
34 typedef struct geoip_country_t {
35 char countrycode[3];
36 uint32_t n_v2_ns_requests[REQUEST_HIST_LEN];
37 uint32_t n_v3_ns_requests[REQUEST_HIST_LEN];
38 } geoip_country_t;
40 /** A list of geoip_country_t */
41 static smartlist_t *geoip_countries = NULL;
42 /** A map from lowercased country codes to their position in geoip_countries.
43 * The index is encoded in the pointer, and 1 is added so that NULL can mean
44 * not found. */
45 static strmap_t *country_idxplus1_by_lc_code = NULL;
46 /** A list of all known geoip_entry_t, sorted by ip_low. */
47 static smartlist_t *geoip_entries = NULL;
49 /** Return the index of the <b>country</b>'s entry in the GeoIP DB
50 * if it is a valid 2-letter country code, otherwise return -1.
52 country_t
53 geoip_get_country(const char *country)
55 void *_idxplus1;
56 intptr_t idx;
58 _idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
59 if (!_idxplus1)
60 return -1;
62 idx = ((uintptr_t)_idxplus1)-1;
63 return (country_t)idx;
66 /** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and
67 * <b>high</b>, inclusive, to the 2-letter country code <b>country</b>.
69 static void
70 geoip_add_entry(uint32_t low, uint32_t high, const char *country)
72 intptr_t idx;
73 geoip_entry_t *ent;
74 void *_idxplus1;
76 if (high < low)
77 return;
79 _idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
81 if (!_idxplus1) {
82 geoip_country_t *c = tor_malloc_zero(sizeof(geoip_country_t));
83 strlcpy(c->countrycode, country, sizeof(c->countrycode));
84 tor_strlower(c->countrycode);
85 smartlist_add(geoip_countries, c);
86 idx = smartlist_len(geoip_countries) - 1;
87 strmap_set_lc(country_idxplus1_by_lc_code, country, (void*)(idx+1));
88 } else {
89 idx = ((uintptr_t)_idxplus1)-1;
92 geoip_country_t *c = smartlist_get(geoip_countries, idx);
93 tor_assert(!strcasecmp(c->countrycode, country));
95 ent = tor_malloc_zero(sizeof(geoip_entry_t));
96 ent->ip_low = low;
97 ent->ip_high = high;
98 ent->country = idx;
99 smartlist_add(geoip_entries, ent);
102 /** Add an entry to the GeoIP table, parsing it from <b>line</b>. The
103 * format is as for geoip_load_file(). */
104 /*private*/ int
105 geoip_parse_entry(const char *line)
107 unsigned int low, high;
108 char b[3];
109 if (!geoip_countries) {
110 geoip_countries = smartlist_create();
111 geoip_entries = smartlist_create();
112 country_idxplus1_by_lc_code = strmap_new();
114 while (TOR_ISSPACE(*line))
115 ++line;
116 if (*line == '#')
117 return 0;
118 if (sscanf(line,"%u,%u,%2s", &low, &high, b) == 3) {
119 geoip_add_entry(low, high, b);
120 return 0;
121 } else if (sscanf(line,"\"%u\",\"%u\",\"%2s\",", &low, &high, b) == 3) {
122 geoip_add_entry(low, high, b);
123 return 0;
124 } else {
125 log_warn(LD_GENERAL, "Unable to parse line from GEOIP file: %s",
126 escaped(line));
127 return -1;
131 /** Sorting helper: return -1, 1, or 0 based on comparison of two
132 * geoip_entry_t */
133 static int
134 _geoip_compare_entries(const void **_a, const void **_b)
136 const geoip_entry_t *a = *_a, *b = *_b;
137 if (a->ip_low < b->ip_low)
138 return -1;
139 else if (a->ip_low > b->ip_low)
140 return 1;
141 else
142 return 0;
145 /** bsearch helper: return -1, 1, or 0 based on comparison of an IP (a pointer
146 * to a uint32_t in host order) to a geoip_entry_t */
147 static int
148 _geoip_compare_key_to_entry(const void *_key, const void **_member)
150 const uint32_t addr = *(uint32_t *)_key;
151 const geoip_entry_t *entry = *_member;
152 if (addr < entry->ip_low)
153 return -1;
154 else if (addr > entry->ip_high)
155 return 1;
156 else
157 return 0;
160 /** Return 1 if we should collect geoip stats on bridge users, and
161 * include them in our extrainfo descriptor. Else return 0. */
163 should_record_bridge_info(or_options_t *options)
165 return options->BridgeRelay && options->BridgeRecordUsageByCountry;
168 /** Clear the GeoIP database and reload it from the file
169 * <b>filename</b>. Return 0 on success, -1 on failure.
171 * Recognized line formats are:
172 * INTIPLOW,INTIPHIGH,CC
173 * and
174 * "INTIPLOW","INTIPHIGH","CC","CC3","COUNTRY NAME"
175 * where INTIPLOW and INTIPHIGH are IPv4 addresses encoded as 4-byte unsigned
176 * integers, and CC is a country code.
178 * It also recognizes, and skips over, blank lines and lines that start
179 * with '#' (comments).
182 geoip_load_file(const char *filename, or_options_t *options)
184 FILE *f;
185 const char *msg = "";
186 int severity = options_need_geoip_info(options, &msg) ? LOG_WARN : LOG_INFO;
187 clear_geoip_db();
188 if (!(f = fopen(filename, "r"))) {
189 log_fn(severity, LD_GENERAL, "Failed to open GEOIP file %s. %s",
190 filename, msg);
191 return -1;
193 if (!geoip_countries) {
194 geoip_country_t *geoip_unresolved;
195 geoip_countries = smartlist_create();
196 /* Add a geoip_country_t for requests that could not be resolved to a
197 * country as first element (index 0) to geoip_countries. */
198 geoip_unresolved = tor_malloc_zero(sizeof(geoip_country_t));
199 strlcpy(geoip_unresolved->countrycode, "??",
200 sizeof(geoip_unresolved->countrycode));
201 smartlist_add(geoip_countries, geoip_unresolved);
202 country_idxplus1_by_lc_code = strmap_new();
203 strmap_set_lc(country_idxplus1_by_lc_code, "??", (void*)(1));
205 if (geoip_entries) {
206 SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, e, tor_free(e));
207 smartlist_free(geoip_entries);
209 geoip_entries = smartlist_create();
210 log_notice(LD_GENERAL, "Parsing GEOIP file.");
211 while (!feof(f)) {
212 char buf[512];
213 if (fgets(buf, (int)sizeof(buf), f) == NULL)
214 break;
215 /* FFFF track full country name. */
216 geoip_parse_entry(buf);
218 /*XXXX abort and return -1 if no entries/illformed?*/
219 fclose(f);
221 smartlist_sort(geoip_entries, _geoip_compare_entries);
223 /* Okay, now we need to maybe change our mind about what is in which
224 * country. */
225 refresh_all_country_info();
227 return 0;
230 /** Given an IP address in host order, return a number representing the
231 * country to which that address belongs, -1 for "No geoip information
232 * available", or 0 for the 'unknown country'. The return value will always
233 * be less than geoip_get_n_countries(). To decode it, call
234 * geoip_get_country_name().
237 geoip_get_country_by_ip(uint32_t ipaddr)
239 geoip_entry_t *ent;
240 if (!geoip_entries)
241 return -1;
242 ent = smartlist_bsearch(geoip_entries, &ipaddr, _geoip_compare_key_to_entry);
243 return ent ? (int)ent->country : 0;
246 /** Return the number of countries recognized by the GeoIP database. */
248 geoip_get_n_countries(void)
250 return (int) smartlist_len(geoip_countries);
253 /** Return the two-letter country code associated with the number <b>num</b>,
254 * or "??" for an unknown value. */
255 const char *
256 geoip_get_country_name(country_t num)
258 if (geoip_countries && num >= 0 && num < smartlist_len(geoip_countries)) {
259 geoip_country_t *c = smartlist_get(geoip_countries, num);
260 return c->countrycode;
261 } else
262 return "??";
265 /** Return true iff we have loaded a GeoIP database.*/
267 geoip_is_loaded(void)
269 return geoip_countries != NULL && geoip_entries != NULL;
272 /** Entry in a map from IP address to the last time we've seen an incoming
273 * connection from that IP address. Used by bridges only, to track which
274 * countries have them blocked. */
275 typedef struct clientmap_entry_t {
276 HT_ENTRY(clientmap_entry_t) node;
277 uint32_t ipaddr;
278 unsigned int last_seen_in_minutes:30;
279 unsigned int action:2;
280 } clientmap_entry_t;
282 #define ACTION_MASK 3
284 /** Map from client IP address to last time seen. */
285 static HT_HEAD(clientmap, clientmap_entry_t) client_history =
286 HT_INITIALIZER();
287 /** Time at which we started tracking client IP history. */
288 static time_t client_history_starts = 0;
290 /** When did the current period of checking per-country request history
291 * start? */
292 static time_t current_request_period_starts = 0;
293 /** How many older request periods are we remembering? */
294 static int n_old_request_periods = 0;
296 /** Hashtable helper: compute a hash of a clientmap_entry_t. */
297 static INLINE unsigned
298 clientmap_entry_hash(const clientmap_entry_t *a)
300 return ht_improve_hash((unsigned) a->ipaddr);
302 /** Hashtable helper: compare two clientmap_entry_t values for equality. */
303 static INLINE int
304 clientmap_entries_eq(const clientmap_entry_t *a, const clientmap_entry_t *b)
306 return a->ipaddr == b->ipaddr && a->action == b->action;
309 HT_PROTOTYPE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
310 clientmap_entries_eq);
311 HT_GENERATE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
312 clientmap_entries_eq, 0.6, malloc, realloc, free);
314 /** How often do we update our estimate which share of v2 and v3 directory
315 * requests is sent to us? We could as well trigger updates of shares from
316 * network status updates, but that means adding a lot of calls into code
317 * that is independent from geoip stats (and keeping them up-to-date). We
318 * are perfectly fine with an approximation of 15-minute granularity. */
319 #define REQUEST_SHARE_INTERVAL (15 * 60)
321 /** When did we last determine which share of v2 and v3 directory requests
322 * is sent to us? */
323 static time_t last_time_determined_shares = 0;
325 /** Sum of products of v2 shares times the number of seconds for which we
326 * consider these shares as valid. */
327 static double v2_share_times_seconds;
329 /** Sum of products of v3 shares times the number of seconds for which we
330 * consider these shares as valid. */
331 static double v3_share_times_seconds;
333 /** Number of seconds we are determining v2 and v3 shares. */
334 static int share_seconds;
336 /** Try to determine which fraction of v2 and v3 directory requests aimed at
337 * caches will be sent to us at time <b>now</b> and store that value in
338 * order to take a mean value later on. */
339 static void
340 geoip_determine_shares(time_t now)
342 double v2_share = 0.0, v3_share = 0.0;
343 if (router_get_my_share_of_directory_requests(&v2_share, &v3_share) < 0)
344 return;
345 if (last_time_determined_shares) {
346 v2_share_times_seconds += v2_share *
347 ((double) (now - last_time_determined_shares));
348 v3_share_times_seconds += v3_share *
349 ((double) (now - last_time_determined_shares));
350 share_seconds += (int)(now - last_time_determined_shares);
352 last_time_determined_shares = now;
355 /** Calculate which fraction of v2 and v3 directory requests aimed at caches
356 * have been sent to us since the last call of this function up to time
357 * <b>now</b>. Set *<b>v2_share_out</b> and *<b>v3_share_out</b> to the
358 * fractions of v2 and v3 protocol shares we expect to have seen. Reset
359 * counters afterwards. Return 0 on success, -1 on failure (e.g. when zero
360 * seconds have passed since the last call).*/
361 static int
362 geoip_get_mean_shares(time_t now, double *v2_share_out,
363 double *v3_share_out)
365 geoip_determine_shares(now);
366 if (!share_seconds)
367 return -1;
368 *v2_share_out = v2_share_times_seconds / ((double) share_seconds);
369 *v3_share_out = v3_share_times_seconds / ((double) share_seconds);
370 v2_share_times_seconds = v3_share_times_seconds = 0.0;
371 share_seconds = 0;
372 return 0;
375 /* Rotate period of v2 and v3 network status requests. */
376 static void
377 rotate_request_period(void)
379 SMARTLIST_FOREACH_BEGIN(geoip_countries, geoip_country_t *, c) {
380 #if REQUEST_HIST_LEN > 1
381 memmove(&c->n_v2_ns_requests[0], &c->n_v2_ns_requests[1],
382 sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
383 memmove(&c->n_v3_ns_requests[0], &c->n_v3_ns_requests[1],
384 sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
385 #endif
386 c->n_v2_ns_requests[REQUEST_HIST_LEN-1] = 0;
387 c->n_v3_ns_requests[REQUEST_HIST_LEN-1] = 0;
388 } SMARTLIST_FOREACH_END(c);
389 current_request_period_starts += REQUEST_HIST_PERIOD;
390 if (n_old_request_periods < REQUEST_HIST_LEN-1)
391 ++n_old_request_periods;
394 /** Note that we've seen a client connect from the IP <b>addr</b> (host order)
395 * at time <b>now</b>. Ignored by all but bridges and directories if
396 * configured accordingly. */
397 void
398 geoip_note_client_seen(geoip_client_action_t action,
399 uint32_t addr, time_t now)
401 or_options_t *options = get_options();
402 clientmap_entry_t lookup, *ent;
403 if (action == GEOIP_CLIENT_CONNECT) {
404 /* Only remember statistics as entry guard or as bridge. */
405 if (!options->EntryStatistics &&
406 (!(options->BridgeRelay && options->BridgeRecordUsageByCountry)))
407 return;
408 /* Did we recently switch from bridge to relay or back? */
409 if (client_history_starts > now)
410 return;
411 } else {
412 if (options->BridgeRelay || options->BridgeAuthoritativeDir ||
413 !options->DirReqStatistics)
414 return;
417 /* As a bridge that doesn't rotate request periods every 24 hours,
418 * possibly rotate now. */
419 if (options->BridgeRelay) {
420 while (current_request_period_starts + REQUEST_HIST_PERIOD < now) {
421 if (!geoip_countries)
422 geoip_countries = smartlist_create();
423 if (!current_request_period_starts) {
424 current_request_period_starts = now;
425 break;
427 /* Also discard all items in the client history that are too old.
428 * (This only works here because bridge and directory stats are
429 * independent. Otherwise, we'd only want to discard those items
430 * with action GEOIP_CLIENT_NETWORKSTATUS{_V2}.) */
431 geoip_remove_old_clients(current_request_period_starts);
432 /* Now rotate request period */
433 rotate_request_period();
437 lookup.ipaddr = addr;
438 lookup.action = (int)action;
439 ent = HT_FIND(clientmap, &client_history, &lookup);
440 if (ent) {
441 ent->last_seen_in_minutes = now / 60;
442 } else {
443 ent = tor_malloc_zero(sizeof(clientmap_entry_t));
444 ent->ipaddr = addr;
445 ent->last_seen_in_minutes = now / 60;
446 ent->action = (int)action;
447 HT_INSERT(clientmap, &client_history, ent);
450 if (action == GEOIP_CLIENT_NETWORKSTATUS ||
451 action == GEOIP_CLIENT_NETWORKSTATUS_V2) {
452 int country_idx = geoip_get_country_by_ip(addr);
453 if (country_idx < 0)
454 country_idx = 0; /** unresolved requests are stored at index 0. */
455 if (country_idx >= 0 && country_idx < smartlist_len(geoip_countries)) {
456 geoip_country_t *country = smartlist_get(geoip_countries, country_idx);
457 if (action == GEOIP_CLIENT_NETWORKSTATUS)
458 ++country->n_v3_ns_requests[REQUEST_HIST_LEN-1];
459 else
460 ++country->n_v2_ns_requests[REQUEST_HIST_LEN-1];
463 /* Periodically determine share of requests that we should see */
464 if (last_time_determined_shares + REQUEST_SHARE_INTERVAL < now)
465 geoip_determine_shares(now);
468 if (!client_history_starts) {
469 client_history_starts = now;
470 current_request_period_starts = now;
474 /** HT_FOREACH helper: remove a clientmap_entry_t from the hashtable if it's
475 * older than a certain time. */
476 static int
477 _remove_old_client_helper(struct clientmap_entry_t *ent, void *_cutoff)
479 time_t cutoff = *(time_t*)_cutoff / 60;
480 if (ent->last_seen_in_minutes < cutoff) {
481 tor_free(ent);
482 return 1;
483 } else {
484 return 0;
488 /** Forget about all clients that haven't connected since <b>cutoff</b>.
489 * If <b>cutoff</b> is in the future, clients won't be added to the history
490 * until this time is reached. This is useful to prevent relays that switch
491 * to bridges from reporting unbelievable numbers of clients. */
492 void
493 geoip_remove_old_clients(time_t cutoff)
495 clientmap_HT_FOREACH_FN(&client_history,
496 _remove_old_client_helper,
497 &cutoff);
498 if (client_history_starts < cutoff)
499 client_history_starts = cutoff;
502 /** How many responses are we giving to clients requesting v2 network
503 * statuses? */
504 static uint32_t ns_v2_responses[GEOIP_NS_RESPONSE_NUM];
506 /** How many responses are we giving to clients requesting v3 network
507 * statuses? */
508 static uint32_t ns_v3_responses[GEOIP_NS_RESPONSE_NUM];
510 /** Note that we've rejected a client's request for a v2 or v3 network
511 * status, encoded in <b>action</b> for reason <b>reason</b> at time
512 * <b>now</b>. */
513 void
514 geoip_note_ns_response(geoip_client_action_t action,
515 geoip_ns_response_t response)
517 static int arrays_initialized = 0;
518 if (!get_options()->DirReqStatistics)
519 return;
520 if (!arrays_initialized) {
521 memset(ns_v2_responses, 0, sizeof(ns_v2_responses));
522 memset(ns_v3_responses, 0, sizeof(ns_v3_responses));
523 arrays_initialized = 1;
525 tor_assert(action == GEOIP_CLIENT_NETWORKSTATUS ||
526 action == GEOIP_CLIENT_NETWORKSTATUS_V2);
527 tor_assert(response < GEOIP_NS_RESPONSE_NUM);
528 if (action == GEOIP_CLIENT_NETWORKSTATUS)
529 ns_v3_responses[response]++;
530 else
531 ns_v2_responses[response]++;
534 /** Do not mention any country from which fewer than this number of IPs have
535 * connected. This conceivably avoids reporting information that could
536 * deanonymize users, though analysis is lacking. */
537 #define MIN_IPS_TO_NOTE_COUNTRY 1
538 /** Do not report any geoip data at all if we have fewer than this number of
539 * IPs to report about. */
540 #define MIN_IPS_TO_NOTE_ANYTHING 1
541 /** When reporting geoip data about countries, round up to the nearest
542 * multiple of this value. */
543 #define IP_GRANULARITY 8
545 /** Return the time at which we started recording geoip data. */
546 time_t
547 geoip_get_history_start(void)
549 return client_history_starts;
552 /** Helper type: used to sort per-country totals by value. */
553 typedef struct c_hist_t {
554 char country[3]; /**< Two-letter country code. */
555 unsigned total; /**< Total IP addresses seen in this country. */
556 } c_hist_t;
558 /** Sorting helper: return -1, 1, or 0 based on comparison of two
559 * geoip_entry_t. Sort in descending order of total, and then by country
560 * code. */
561 static int
562 _c_hist_compare(const void **_a, const void **_b)
564 const c_hist_t *a = *_a, *b = *_b;
565 if (a->total > b->total)
566 return -1;
567 else if (a->total < b->total)
568 return 1;
569 else
570 return strcmp(a->country, b->country);
573 /** When there are incomplete directory requests at the end of a 24-hour
574 * period, consider those requests running for longer than this timeout as
575 * failed, the others as still running. */
576 #define DIRREQ_TIMEOUT (10*60)
578 /** Entry in a map from either conn->global_identifier for direct requests
579 * or a unique circuit identifier for tunneled requests to request time,
580 * response size, and completion time of a network status request. Used to
581 * measure download times of requests to derive average client
582 * bandwidths. */
583 typedef struct dirreq_map_entry_t {
584 HT_ENTRY(dirreq_map_entry_t) node;
585 /** Unique identifier for this network status request; this is either the
586 * conn->global_identifier of the dir conn (direct request) or a new
587 * locally unique identifier of a circuit (tunneled request). This ID is
588 * only unique among other direct or tunneled requests, respectively. */
589 uint64_t dirreq_id;
590 unsigned int state:3; /**< State of this directory request. */
591 unsigned int type:1; /**< Is this a direct or a tunneled request? */
592 unsigned int completed:1; /**< Is this request complete? */
593 unsigned int action:2; /**< Is this a v2 or v3 request? */
594 /** When did we receive the request and started sending the response? */
595 struct timeval request_time;
596 size_t response_size; /**< What is the size of the response in bytes? */
597 struct timeval completion_time; /**< When did the request succeed? */
598 } dirreq_map_entry_t;
600 /** Map of all directory requests asking for v2 or v3 network statuses in
601 * the current geoip-stats interval. Values are
602 * of type *<b>dirreq_map_entry_t</b>. */
603 static HT_HEAD(dirreqmap, dirreq_map_entry_t) dirreq_map =
604 HT_INITIALIZER();
606 static int
607 dirreq_map_ent_eq(const dirreq_map_entry_t *a,
608 const dirreq_map_entry_t *b)
610 return a->dirreq_id == b->dirreq_id && a->type == b->type;
613 static unsigned
614 dirreq_map_ent_hash(const dirreq_map_entry_t *entry)
616 unsigned u = (unsigned) entry->dirreq_id;
617 u += entry->type << 20;
618 return u;
621 HT_PROTOTYPE(dirreqmap, dirreq_map_entry_t, node, dirreq_map_ent_hash,
622 dirreq_map_ent_eq);
623 HT_GENERATE(dirreqmap, dirreq_map_entry_t, node, dirreq_map_ent_hash,
624 dirreq_map_ent_eq, 0.6, malloc, realloc, free);
626 /** Helper: Put <b>entry</b> into map of directory requests using
627 * <b>tunneled</b> and <b>dirreq_id</b> as key parts. If there is
628 * already an entry for that key, print out a BUG warning and return. */
629 static void
630 _dirreq_map_put(dirreq_map_entry_t *entry, dirreq_type_t type,
631 uint64_t dirreq_id)
633 dirreq_map_entry_t *old_ent;
634 tor_assert(entry->type == type);
635 tor_assert(entry->dirreq_id == dirreq_id);
637 /* XXXX022 once we're sure the bug case never happens, we can switch
638 * to HT_INSERT */
639 old_ent = HT_REPLACE(dirreqmap, &dirreq_map, entry);
640 if (old_ent && old_ent != entry) {
641 log_warn(LD_BUG, "Error when putting directory request into local "
642 "map. There was already an entry for the same identifier.");
643 return;
647 /** Helper: Look up and return an entry in the map of directory requests
648 * using <b>tunneled</b> and <b>dirreq_id</b> as key parts. If there
649 * is no such entry, return NULL. */
650 static dirreq_map_entry_t *
651 _dirreq_map_get(dirreq_type_t type, uint64_t dirreq_id)
653 dirreq_map_entry_t lookup;
654 lookup.type = type;
655 lookup.dirreq_id = dirreq_id;
656 return HT_FIND(dirreqmap, &dirreq_map, &lookup);
659 /** Note that an either direct or tunneled (see <b>type</b>) directory
660 * request for a network status with unique ID <b>dirreq_id</b> of size
661 * <b>response_size</b> and action <b>action</b> (either v2 or v3) has
662 * started. */
663 void
664 geoip_start_dirreq(uint64_t dirreq_id, size_t response_size,
665 geoip_client_action_t action, dirreq_type_t type)
667 dirreq_map_entry_t *ent;
668 if (!get_options()->DirReqStatistics)
669 return;
670 ent = tor_malloc_zero(sizeof(dirreq_map_entry_t));
671 ent->dirreq_id = dirreq_id;
672 tor_gettimeofday(&ent->request_time);
673 ent->response_size = response_size;
674 ent->action = action;
675 ent->type = type;
676 _dirreq_map_put(ent, type, dirreq_id);
679 /** Change the state of the either direct or tunneled (see <b>type</b>)
680 * directory request with <b>dirreq_id</b> to <b>new_state</b> and
681 * possibly mark it as completed. If no entry can be found for the given
682 * key parts (e.g., if this is a directory request that we are not
683 * measuring, or one that was started in the previous measurement period),
684 * or if the state cannot be advanced to <b>new_state</b>, do nothing. */
685 void
686 geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type,
687 dirreq_state_t new_state)
689 dirreq_map_entry_t *ent;
690 if (!get_options()->DirReqStatistics)
691 return;
692 ent = _dirreq_map_get(type, dirreq_id);
693 if (!ent)
694 return;
695 if (new_state == DIRREQ_IS_FOR_NETWORK_STATUS)
696 return;
697 if (new_state - 1 != ent->state)
698 return;
699 ent->state = new_state;
700 if ((type == DIRREQ_DIRECT &&
701 new_state == DIRREQ_FLUSHING_DIR_CONN_FINISHED) ||
702 (type == DIRREQ_TUNNELED &&
703 new_state == DIRREQ_OR_CONN_BUFFER_FLUSHED)) {
704 tor_gettimeofday(&ent->completion_time);
705 ent->completed = 1;
709 /** Return a newly allocated comma-separated string containing statistics
710 * on network status downloads. The string contains the number of completed
711 * requests, timeouts, and still running requests as well as the download
712 * times by deciles and quartiles. Return NULL if we have not observed
713 * requests for long enough. */
714 static char *
715 geoip_get_dirreq_history(geoip_client_action_t action,
716 dirreq_type_t type)
718 char *result = NULL;
719 smartlist_t *dirreq_completed = NULL;
720 uint32_t complete = 0, timeouts = 0, running = 0;
721 int bufsize = 1024, written;
722 dirreq_map_entry_t **ptr, **next, *ent;
723 struct timeval now;
725 tor_gettimeofday(&now);
726 if (action != GEOIP_CLIENT_NETWORKSTATUS &&
727 action != GEOIP_CLIENT_NETWORKSTATUS_V2)
728 return NULL;
729 dirreq_completed = smartlist_create();
730 for (ptr = HT_START(dirreqmap, &dirreq_map); ptr; ptr = next) {
731 ent = *ptr;
732 if (ent->action != action || ent->type != type) {
733 next = HT_NEXT(dirreqmap, &dirreq_map, ptr);
734 continue;
735 } else {
736 if (ent->completed) {
737 smartlist_add(dirreq_completed, ent);
738 complete++;
739 next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ptr);
740 } else {
741 if (tv_mdiff(&ent->request_time, &now) / 1000 > DIRREQ_TIMEOUT)
742 timeouts++;
743 else
744 running++;
745 next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ptr);
746 tor_free(ent);
750 #define DIR_REQ_GRANULARITY 4
751 complete = round_uint32_to_next_multiple_of(complete,
752 DIR_REQ_GRANULARITY);
753 timeouts = round_uint32_to_next_multiple_of(timeouts,
754 DIR_REQ_GRANULARITY);
755 running = round_uint32_to_next_multiple_of(running,
756 DIR_REQ_GRANULARITY);
757 result = tor_malloc_zero(bufsize);
758 written = tor_snprintf(result, bufsize, "complete=%u,timeout=%u,"
759 "running=%u", complete, timeouts, running);
760 if (written < 0) {
761 tor_free(result);
762 goto done;
765 #define MIN_DIR_REQ_RESPONSES 16
766 if (complete >= MIN_DIR_REQ_RESPONSES) {
767 uint32_t *dltimes;
768 /* We may have rounded 'completed' up. Here we want to use the
769 * real value. */
770 complete = smartlist_len(dirreq_completed);
771 dltimes = tor_malloc_zero(sizeof(uint32_t) * complete);
772 SMARTLIST_FOREACH_BEGIN(dirreq_completed, dirreq_map_entry_t *, ent) {
773 uint32_t bytes_per_second;
774 uint32_t time_diff = (uint32_t) tv_mdiff(&ent->request_time,
775 &ent->completion_time);
776 if (time_diff == 0)
777 time_diff = 1; /* Avoid DIV/0; "instant" answers are impossible
778 * by law of nature or something, but a milisecond
779 * is a bit greater than "instantly" */
780 bytes_per_second = (uint32_t)(1000 * ent->response_size / time_diff);
781 dltimes[ent_sl_idx] = bytes_per_second;
782 } SMARTLIST_FOREACH_END(ent);
783 median_uint32(dltimes, complete); /* sorts as a side effect. */
784 written = tor_snprintf(result + written, bufsize - written,
785 ",min=%u,d1=%u,d2=%u,q1=%u,d3=%u,d4=%u,md=%u,"
786 "d6=%u,d7=%u,q3=%u,d8=%u,d9=%u,max=%u",
787 dltimes[0],
788 dltimes[1*complete/10-1],
789 dltimes[2*complete/10-1],
790 dltimes[1*complete/4-1],
791 dltimes[3*complete/10-1],
792 dltimes[4*complete/10-1],
793 dltimes[5*complete/10-1],
794 dltimes[6*complete/10-1],
795 dltimes[7*complete/10-1],
796 dltimes[3*complete/4-1],
797 dltimes[8*complete/10-1],
798 dltimes[9*complete/10-1],
799 dltimes[complete-1]);
800 if (written<0)
801 tor_free(result);
802 tor_free(dltimes);
804 done:
805 SMARTLIST_FOREACH(dirreq_completed, dirreq_map_entry_t *, ent,
806 tor_free(ent));
807 smartlist_free(dirreq_completed);
808 return result;
811 /** How long do we have to have observed per-country request history before we
812 * are willing to talk about it? */
813 #define GEOIP_MIN_OBSERVATION_TIME (12*60*60)
815 /** Helper for geoip_get_client_history_dirreq() and
816 * geoip_get_client_history_bridge(). */
817 static char *
818 geoip_get_client_history(time_t now, geoip_client_action_t action,
819 int min_observation_time, unsigned granularity)
821 char *result = NULL;
822 if (!geoip_is_loaded())
823 return NULL;
824 if (client_history_starts < (now - min_observation_time)) {
825 smartlist_t *chunks = NULL;
826 smartlist_t *entries = NULL;
827 int n_countries = geoip_get_n_countries();
828 int i;
829 clientmap_entry_t **ent;
830 unsigned *counts = tor_malloc_zero(sizeof(unsigned)*n_countries);
831 unsigned total = 0;
832 HT_FOREACH(ent, clientmap, &client_history) {
833 int country;
834 if ((*ent)->action != (int)action)
835 continue;
836 country = geoip_get_country_by_ip((*ent)->ipaddr);
837 if (country < 0)
838 country = 0; /** unresolved requests are stored at index 0. */
839 tor_assert(0 <= country && country < n_countries);
840 ++counts[country];
841 ++total;
843 /* Don't record anything if we haven't seen enough IPs. */
844 if (total < MIN_IPS_TO_NOTE_ANYTHING)
845 goto done;
846 /* Make a list of c_hist_t */
847 entries = smartlist_create();
848 for (i = 0; i < n_countries; ++i) {
849 unsigned c = counts[i];
850 const char *countrycode;
851 c_hist_t *ent;
852 /* Only report a country if it has a minimum number of IPs. */
853 if (c >= MIN_IPS_TO_NOTE_COUNTRY) {
854 c = round_to_next_multiple_of(c, granularity);
855 countrycode = geoip_get_country_name(i);
856 ent = tor_malloc(sizeof(c_hist_t));
857 strlcpy(ent->country, countrycode, sizeof(ent->country));
858 ent->total = c;
859 smartlist_add(entries, ent);
862 /* Sort entries. Note that we must do this _AFTER_ rounding, or else
863 * the sort order could leak info. */
864 smartlist_sort(entries, _c_hist_compare);
866 /* Build the result. */
867 chunks = smartlist_create();
868 SMARTLIST_FOREACH(entries, c_hist_t *, ch, {
869 char *buf=NULL;
870 tor_asprintf(&buf, "%s=%u", ch->country, ch->total);
871 smartlist_add(chunks, buf);
873 result = smartlist_join_strings(chunks, ",", 0, NULL);
874 done:
875 tor_free(counts);
876 if (chunks) {
877 SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
878 smartlist_free(chunks);
880 if (entries) {
881 SMARTLIST_FOREACH(entries, c_hist_t *, c, tor_free(c));
882 smartlist_free(entries);
885 return result;
888 /** Return a newly allocated comma-separated string containing entries for
889 * all the countries from which we've seen enough clients connect as a
890 * directory. The entry format is cc=num where num is the number of IPs
891 * we've seen connecting from that country, and cc is a lowercased country
892 * code. Returns NULL if we don't want to export geoip data yet. */
893 char *
894 geoip_get_client_history_dirreq(time_t now,
895 geoip_client_action_t action)
897 return geoip_get_client_history(now, action,
898 DIR_RECORD_USAGE_MIN_OBSERVATION_TIME,
899 DIR_RECORD_USAGE_GRANULARITY);
902 /** Return a newly allocated comma-separated string containing entries for
903 * all the countries from which we've seen enough clients connect as a
904 * bridge. The entry format is cc=num where num is the number of IPs
905 * we've seen connecting from that country, and cc is a lowercased country
906 * code. Returns NULL if we don't want to export geoip data yet. */
907 char *
908 geoip_get_client_history_bridge(time_t now,
909 geoip_client_action_t action)
911 return geoip_get_client_history(now, action,
912 GEOIP_MIN_OBSERVATION_TIME,
913 IP_GRANULARITY);
916 /** Return a newly allocated string holding the per-country request history
917 * for <b>action</b> in a format suitable for an extra-info document, or NULL
918 * on failure. */
919 char *
920 geoip_get_request_history(time_t now, geoip_client_action_t action)
922 smartlist_t *entries, *strings;
923 char *result;
924 unsigned granularity = IP_GRANULARITY;
925 int min_observation_time = GEOIP_MIN_OBSERVATION_TIME;
927 if (client_history_starts >= (now - min_observation_time))
928 return NULL;
929 if (action != GEOIP_CLIENT_NETWORKSTATUS &&
930 action != GEOIP_CLIENT_NETWORKSTATUS_V2)
931 return NULL;
932 if (!geoip_countries)
933 return NULL;
935 entries = smartlist_create();
936 SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
937 uint32_t *n = (action == GEOIP_CLIENT_NETWORKSTATUS)
938 ? c->n_v3_ns_requests : c->n_v2_ns_requests;
939 uint32_t tot = 0;
940 int i;
941 c_hist_t *ent;
942 for (i=0; i < REQUEST_HIST_LEN; ++i)
943 tot += n[i];
944 if (!tot)
945 continue;
946 ent = tor_malloc_zero(sizeof(c_hist_t));
947 strlcpy(ent->country, c->countrycode, sizeof(ent->country));
948 ent->total = round_to_next_multiple_of(tot, granularity);
949 smartlist_add(entries, ent);
951 smartlist_sort(entries, _c_hist_compare);
953 strings = smartlist_create();
954 SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
955 char *buf = NULL;
956 tor_asprintf(&buf, "%s=%u", ent->country, ent->total);
957 smartlist_add(strings, buf);
959 result = smartlist_join_strings(strings, ",", 0, NULL);
960 SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp));
961 SMARTLIST_FOREACH(entries, c_hist_t *, ent, tor_free(ent));
962 smartlist_free(strings);
963 smartlist_free(entries);
964 return result;
967 /** Start time of directory request stats. */
968 static time_t start_of_dirreq_stats_interval;
970 /** Initialize directory request stats. */
971 void
972 geoip_dirreq_stats_init(time_t now)
974 start_of_dirreq_stats_interval = now;
977 /** Write dirreq statistics to $DATADIR/stats/dirreq-stats. */
978 void
979 geoip_dirreq_stats_write(time_t now)
981 char *statsdir = NULL, *filename = NULL;
982 char *data_v2 = NULL, *data_v3 = NULL;
983 char written[ISO_TIME_LEN+1];
984 open_file_t *open_file = NULL;
985 double v2_share = 0.0, v3_share = 0.0;
986 FILE *out;
987 int i;
989 if (!get_options()->DirReqStatistics)
990 goto done;
992 /* Discard all items in the client history that are too old. */
993 geoip_remove_old_clients(start_of_dirreq_stats_interval);
995 statsdir = get_datadir_fname("stats");
996 if (check_private_dir(statsdir, CPD_CREATE) < 0)
997 goto done;
998 filename = get_datadir_fname2("stats", "dirreq-stats");
999 data_v2 = geoip_get_client_history_dirreq(now,
1000 GEOIP_CLIENT_NETWORKSTATUS_V2);
1001 data_v3 = geoip_get_client_history_dirreq(now,
1002 GEOIP_CLIENT_NETWORKSTATUS);
1003 format_iso_time(written, now);
1004 out = start_writing_to_stdio_file(filename, OPEN_FLAGS_APPEND,
1005 0600, &open_file);
1006 if (!out)
1007 goto done;
1008 if (fprintf(out, "dirreq-stats-end %s (%d s)\ndirreq-v3-ips %s\n"
1009 "dirreq-v2-ips %s\n", written,
1010 (unsigned) (now - start_of_dirreq_stats_interval),
1011 data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
1012 goto done;
1013 tor_free(data_v2);
1014 tor_free(data_v3);
1016 data_v2 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
1017 data_v3 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS);
1018 if (fprintf(out, "dirreq-v3-reqs %s\ndirreq-v2-reqs %s\n",
1019 data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
1020 goto done;
1021 tor_free(data_v2);
1022 tor_free(data_v3);
1023 #define RESPONSE_GRANULARITY 8
1024 for (i = 0; i < GEOIP_NS_RESPONSE_NUM; i++) {
1025 ns_v2_responses[i] = round_uint32_to_next_multiple_of(
1026 ns_v2_responses[i], RESPONSE_GRANULARITY);
1027 ns_v3_responses[i] = round_uint32_to_next_multiple_of(
1028 ns_v3_responses[i], RESPONSE_GRANULARITY);
1030 #undef RESPONSE_GRANULARITY
1031 if (fprintf(out, "dirreq-v3-resp ok=%u,not-enough-sigs=%u,unavailable=%u,"
1032 "not-found=%u,not-modified=%u,busy=%u\n",
1033 ns_v3_responses[GEOIP_SUCCESS],
1034 ns_v3_responses[GEOIP_REJECT_NOT_ENOUGH_SIGS],
1035 ns_v3_responses[GEOIP_REJECT_UNAVAILABLE],
1036 ns_v3_responses[GEOIP_REJECT_NOT_FOUND],
1037 ns_v3_responses[GEOIP_REJECT_NOT_MODIFIED],
1038 ns_v3_responses[GEOIP_REJECT_BUSY]) < 0)
1039 goto done;
1040 if (fprintf(out, "dirreq-v2-resp ok=%u,unavailable=%u,"
1041 "not-found=%u,not-modified=%u,busy=%u\n",
1042 ns_v2_responses[GEOIP_SUCCESS],
1043 ns_v2_responses[GEOIP_REJECT_UNAVAILABLE],
1044 ns_v2_responses[GEOIP_REJECT_NOT_FOUND],
1045 ns_v2_responses[GEOIP_REJECT_NOT_MODIFIED],
1046 ns_v2_responses[GEOIP_REJECT_BUSY]) < 0)
1047 goto done;
1048 memset(ns_v2_responses, 0, sizeof(ns_v2_responses));
1049 memset(ns_v3_responses, 0, sizeof(ns_v3_responses));
1050 if (!geoip_get_mean_shares(now, &v2_share, &v3_share)) {
1051 if (fprintf(out, "dirreq-v2-share %0.2lf%%\n", v2_share*100) < 0)
1052 goto done;
1053 if (fprintf(out, "dirreq-v3-share %0.2lf%%\n", v3_share*100) < 0)
1054 goto done;
1057 data_v2 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS_V2,
1058 DIRREQ_DIRECT);
1059 data_v3 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS,
1060 DIRREQ_DIRECT);
1061 if (fprintf(out, "dirreq-v3-direct-dl %s\ndirreq-v2-direct-dl %s\n",
1062 data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
1063 goto done;
1064 tor_free(data_v2);
1065 tor_free(data_v3);
1066 data_v2 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS_V2,
1067 DIRREQ_TUNNELED);
1068 data_v3 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS,
1069 DIRREQ_TUNNELED);
1070 if (fprintf(out, "dirreq-v3-tunneled-dl %s\ndirreq-v2-tunneled-dl %s\n",
1071 data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
1072 goto done;
1074 finish_writing_to_file(open_file);
1075 open_file = NULL;
1077 /* Rotate request period */
1078 rotate_request_period();
1080 start_of_dirreq_stats_interval = now;
1082 done:
1083 if (open_file)
1084 abort_writing_to_file(open_file);
1085 tor_free(filename);
1086 tor_free(statsdir);
1087 tor_free(data_v2);
1088 tor_free(data_v3);
1091 /** Start time of bridge stats. */
1092 static time_t start_of_bridge_stats_interval;
1094 /** Initialize bridge stats. */
1095 void
1096 geoip_bridge_stats_init(time_t now)
1098 start_of_bridge_stats_interval = now;
1101 /** Parse the bridge statistics as they are written to extra-info
1102 * descriptors for being returned to controller clients. Return the
1103 * controller string if successful, or NULL otherwise. */
1104 static char *
1105 parse_bridge_stats_controller(const char *stats_str, time_t now)
1107 char stats_end_str[ISO_TIME_LEN+1], stats_start_str[ISO_TIME_LEN+1],
1108 *controller_str, *eos, *eol, *summary;
1110 const char *BRIDGE_STATS_END = "bridge-stats-end ";
1111 const char *BRIDGE_IPS = "bridge-ips ";
1112 const char *BRIDGE_IPS_EMPTY_LINE = "bridge-ips\n";
1113 const char *tmp;
1114 time_t stats_end_time;
1115 int seconds;
1116 tor_assert(stats_str);
1118 /* Parse timestamp and number of seconds from
1119 "bridge-stats-end YYYY-MM-DD HH:MM:SS (N s)" */
1120 tmp = find_str_at_start_of_line(stats_str, BRIDGE_STATS_END);
1121 if (!tmp)
1122 return NULL;
1123 tmp += strlen(BRIDGE_STATS_END);
1125 if (strlen(tmp) < ISO_TIME_LEN + 6)
1126 return NULL;
1127 strlcpy(stats_end_str, tmp, sizeof(stats_end_str));
1128 if (parse_iso_time(stats_end_str, &stats_end_time) < 0)
1129 return NULL;
1130 if (stats_end_time < now - (25*60*60) ||
1131 stats_end_time > now + (1*60*60))
1132 return NULL;
1133 seconds = (int)strtol(tmp + ISO_TIME_LEN + 2, &eos, 10);
1134 if (!eos || seconds < 23*60*60)
1135 return NULL;
1136 format_iso_time(stats_start_str, stats_end_time - seconds);
1138 /* Parse: "bridge-ips CC=N,CC=N,..." */
1139 tmp = find_str_at_start_of_line(stats_str, BRIDGE_IPS);
1140 if (tmp) {
1141 tmp += strlen(BRIDGE_IPS);
1142 tmp = eat_whitespace_no_nl(tmp);
1143 eol = strchr(tmp, '\n');
1144 if (eol)
1145 summary = tor_strndup(tmp, eol-tmp);
1146 else
1147 summary = tor_strdup(tmp);
1148 } else {
1149 /* Look if there is an empty "bridge-ips" line */
1150 tmp = find_str_at_start_of_line(stats_str, BRIDGE_IPS_EMPTY_LINE);
1151 if (!tmp)
1152 return NULL;
1153 summary = tor_strdup("");
1156 tor_asprintf(&controller_str,
1157 "TimeStarted=\"%s\" CountrySummary=%s",
1158 stats_start_str, summary);
1159 tor_free(summary);
1160 return controller_str;
1163 /** Most recent bridge statistics formatted to be written to extra-info
1164 * descriptors. */
1165 static char *bridge_stats_extrainfo = NULL;
1167 /** Most recent bridge statistics formatted to be returned to controller
1168 * clients. */
1169 static char *bridge_stats_controller = NULL;
1171 /** Write bridge statistics to $DATADIR/stats/bridge-stats and return
1172 * when we should next try to write statistics. */
1173 time_t
1174 geoip_bridge_stats_write(time_t now)
1176 char *statsdir = NULL, *filename = NULL, *data = NULL,
1177 written[ISO_TIME_LEN+1], *out = NULL, *controller_str;
1178 size_t len;
1180 /* If we changed from relay to bridge recently, adapt starting time
1181 * of current measurements. */
1182 if (start_of_bridge_stats_interval < client_history_starts)
1183 start_of_bridge_stats_interval = client_history_starts;
1185 /* Check if 24 hours have passed since starting measurements. */
1186 if (now < start_of_bridge_stats_interval +
1187 DIR_ENTRY_RECORD_USAGE_RETAIN_IPS)
1188 return start_of_bridge_stats_interval +
1189 DIR_ENTRY_RECORD_USAGE_RETAIN_IPS;
1191 /* Discard all items in the client history that are too old. */
1192 geoip_remove_old_clients(start_of_bridge_stats_interval);
1194 statsdir = get_datadir_fname("stats");
1195 if (check_private_dir(statsdir, CPD_CREATE) < 0)
1196 goto done;
1197 filename = get_datadir_fname2("stats", "bridge-stats");
1198 data = geoip_get_client_history_bridge(now, GEOIP_CLIENT_CONNECT);
1199 format_iso_time(written, now);
1200 len = strlen("bridge-stats-end (999999 s)\nbridge-ips \n") +
1201 ISO_TIME_LEN + (data ? strlen(data) : 0) + 42;
1202 out = tor_malloc(len);
1203 if (tor_snprintf(out, len, "bridge-stats-end %s (%u s)\nbridge-ips %s\n",
1204 written, (unsigned) (now - start_of_bridge_stats_interval),
1205 data ? data : "") < 0)
1206 goto done;
1207 write_str_to_file(filename, out, 0);
1208 controller_str = parse_bridge_stats_controller(out, now);
1209 if (!controller_str)
1210 goto done;
1211 start_of_bridge_stats_interval = now;
1212 tor_free(bridge_stats_extrainfo);
1213 tor_free(bridge_stats_controller);
1214 bridge_stats_extrainfo = out;
1215 out = NULL;
1216 bridge_stats_controller = controller_str;
1217 control_event_clients_seen(controller_str);
1218 done:
1219 tor_free(filename);
1220 tor_free(statsdir);
1221 tor_free(data);
1222 tor_free(out);
1223 return start_of_bridge_stats_interval +
1224 DIR_ENTRY_RECORD_USAGE_RETAIN_IPS;
1227 /** Try to load the most recent bridge statistics from disk, unless we
1228 * have finished a measurement interval lately. */
1229 static void
1230 load_bridge_stats(time_t now)
1232 char *statsdir, *fname=NULL, *contents, *controller_str;
1233 if (bridge_stats_extrainfo)
1234 return;
1235 statsdir = get_datadir_fname("stats");
1236 if (check_private_dir(statsdir, CPD_CREATE) < 0)
1237 goto done;
1238 fname = get_datadir_fname2("stats", "bridge-stats");
1239 contents = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
1240 if (contents) {
1241 controller_str = parse_bridge_stats_controller(contents, now);
1242 if (controller_str) {
1243 bridge_stats_extrainfo = contents;
1244 bridge_stats_controller = controller_str;
1245 } else {
1246 tor_free(contents);
1249 done:
1250 tor_free(fname);
1251 tor_free(statsdir);
1254 /** Return most recent bridge statistics for inclusion in extra-info
1255 * descriptors, or NULL if we don't have recent bridge statistics. */
1256 const char *
1257 geoip_get_bridge_stats_extrainfo(time_t now)
1259 load_bridge_stats(now);
1260 return bridge_stats_extrainfo;
1263 /** Return most recent bridge statistics to be returned to controller
1264 * clients, or NULL if we don't have recent bridge statistics. */
1265 const char *
1266 geoip_get_bridge_stats_controller(time_t now)
1268 load_bridge_stats(now);
1269 return bridge_stats_controller;
1272 /** Start time of entry stats. */
1273 static time_t start_of_entry_stats_interval;
1275 /** Initialize entry stats. */
1276 void
1277 geoip_entry_stats_init(time_t now)
1279 start_of_entry_stats_interval = now;
1282 /** Write entry statistics to $DATADIR/stats/entry-stats. */
1283 void
1284 geoip_entry_stats_write(time_t now)
1286 char *statsdir = NULL, *filename = NULL;
1287 char *data = NULL;
1288 char written[ISO_TIME_LEN+1];
1289 open_file_t *open_file = NULL;
1290 FILE *out;
1292 if (!get_options()->EntryStatistics)
1293 goto done;
1295 /* Discard all items in the client history that are too old. */
1296 geoip_remove_old_clients(start_of_entry_stats_interval);
1298 statsdir = get_datadir_fname("stats");
1299 if (check_private_dir(statsdir, CPD_CREATE) < 0)
1300 goto done;
1301 filename = get_datadir_fname2("stats", "entry-stats");
1302 data = geoip_get_client_history_dirreq(now, GEOIP_CLIENT_CONNECT);
1303 format_iso_time(written, now);
1304 out = start_writing_to_stdio_file(filename, OPEN_FLAGS_APPEND,
1305 0600, &open_file);
1306 if (!out)
1307 goto done;
1308 if (fprintf(out, "entry-stats-end %s (%u s)\nentry-ips %s\n",
1309 written, (unsigned) (now - start_of_entry_stats_interval),
1310 data ? data : "") < 0)
1311 goto done;
1313 start_of_entry_stats_interval = now;
1315 finish_writing_to_file(open_file);
1316 open_file = NULL;
1317 done:
1318 if (open_file)
1319 abort_writing_to_file(open_file);
1320 tor_free(filename);
1321 tor_free(statsdir);
1322 tor_free(data);
1325 /** Helper used to implement GETINFO ip-to-country/... controller command. */
1327 getinfo_helper_geoip(control_connection_t *control_conn,
1328 const char *question, char **answer,
1329 const char **errmsg)
1331 (void)control_conn;
1332 if (!geoip_is_loaded()) {
1333 *errmsg = "GeoIP data not loaded";
1334 return -1;
1336 if (!strcmpstart(question, "ip-to-country/")) {
1337 int c;
1338 uint32_t ip;
1339 struct in_addr in;
1340 question += strlen("ip-to-country/");
1341 if (tor_inet_aton(question, &in) != 0) {
1342 ip = ntohl(in.s_addr);
1343 c = geoip_get_country_by_ip(ip);
1344 *answer = tor_strdup(geoip_get_country_name(c));
1347 return 0;
1350 /** Release all storage held by the GeoIP database. */
1351 static void
1352 clear_geoip_db(void)
1354 if (geoip_countries) {
1355 SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, tor_free(c));
1356 smartlist_free(geoip_countries);
1359 strmap_free(country_idxplus1_by_lc_code, NULL);
1360 if (geoip_entries) {
1361 SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, ent, tor_free(ent));
1362 smartlist_free(geoip_entries);
1364 geoip_countries = NULL;
1365 country_idxplus1_by_lc_code = NULL;
1366 geoip_entries = NULL;
1369 /** Release all storage held in this file. */
1370 void
1371 geoip_free_all(void)
1374 clientmap_entry_t **ent, **next, *this;
1375 for (ent = HT_START(clientmap, &client_history); ent != NULL; ent = next) {
1376 this = *ent;
1377 next = HT_NEXT_RMV(clientmap, &client_history, ent);
1378 tor_free(this);
1380 HT_CLEAR(clientmap, &client_history);
1383 dirreq_map_entry_t **ent, **next, *this;
1384 for (ent = HT_START(dirreqmap, &dirreq_map); ent != NULL; ent = next) {
1385 this = *ent;
1386 next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ent);
1387 tor_free(this);
1389 HT_CLEAR(dirreqmap, &dirreq_map);
1392 clear_geoip_db();