2010-02-27 23:13:37 +01:00
|
|
|
/* Copyright (c) 2007-2010, The Tor Project, Inc. */
|
2007-12-17 23:44:11 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/**
|
|
|
|
* \file geoip.c
|
|
|
|
* \brief Functions related to maintaining an IP-to-country database and to
|
|
|
|
* summarizing client connections by country.
|
|
|
|
*/
|
|
|
|
|
2007-12-17 23:44:11 +01:00
|
|
|
#define GEOIP_PRIVATE
|
|
|
|
#include "or.h"
|
|
|
|
#include "ht.h"
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
static void clear_geoip_db(void);
|
2007-12-17 23:44:11 +01:00
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** An entry from the GeoIP file: maps an IP range to a country. */
|
2007-12-17 23:44:11 +01:00
|
|
|
typedef struct geoip_entry_t {
|
2007-12-18 22:27:08 +01:00
|
|
|
uint32_t ip_low; /**< The lowest IP in the range, in host order */
|
|
|
|
uint32_t ip_high; /**< The highest IP in the range, in host order */
|
2008-02-22 20:09:45 +01:00
|
|
|
intptr_t country; /**< An index into geoip_countries */
|
2007-12-17 23:44:11 +01:00
|
|
|
} geoip_entry_t;
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** For how many periods should we remember per-country request history? */
|
2009-05-27 17:52:46 +02:00
|
|
|
#define REQUEST_HIST_LEN 1
|
2008-12-17 23:58:20 +01:00
|
|
|
/** How long are the periods for which we should remember request history? */
|
2009-05-27 17:52:46 +02:00
|
|
|
#define REQUEST_HIST_PERIOD (24*60*60)
|
2008-06-10 20:08:56 +02:00
|
|
|
|
2008-12-25 16:37:47 +01:00
|
|
|
/** A per-country record for GeoIP request history. */
|
2008-06-10 20:08:56 +02:00
|
|
|
typedef struct geoip_country_t {
|
|
|
|
char countrycode[3];
|
|
|
|
uint32_t n_v2_ns_requests[REQUEST_HIST_LEN];
|
|
|
|
uint32_t n_v3_ns_requests[REQUEST_HIST_LEN];
|
|
|
|
} geoip_country_t;
|
|
|
|
|
|
|
|
/** A list of geoip_country_t */
|
2007-12-17 23:44:11 +01:00
|
|
|
static smartlist_t *geoip_countries = NULL;
|
2007-12-18 22:27:08 +01:00
|
|
|
/** A map from lowercased country codes to their position in geoip_countries.
|
|
|
|
* The index is encoded in the pointer, and 1 is added so that NULL can mean
|
|
|
|
* not found. */
|
2007-12-17 23:44:11 +01:00
|
|
|
static strmap_t *country_idxplus1_by_lc_code = NULL;
|
2007-12-18 22:27:08 +01:00
|
|
|
/** A list of all known geoip_entry_t, sorted by ip_low. */
|
2007-12-17 23:44:11 +01:00
|
|
|
static smartlist_t *geoip_entries = NULL;
|
|
|
|
|
2008-09-25 22:21:35 +02:00
|
|
|
/** Return the index of the <b>country</b>'s entry in the GeoIP DB
|
2009-05-26 21:32:56 +02:00
|
|
|
* if it is a valid 2-letter country code, otherwise return -1.
|
2008-09-25 22:21:35 +02:00
|
|
|
*/
|
|
|
|
country_t
|
|
|
|
geoip_get_country(const char *country)
|
|
|
|
{
|
|
|
|
void *_idxplus1;
|
|
|
|
intptr_t idx;
|
|
|
|
|
|
|
|
_idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
|
|
|
|
if (!_idxplus1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
idx = ((uintptr_t)_idxplus1)-1;
|
|
|
|
return (country_t)idx;
|
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and
|
|
|
|
* <b>high</b>, inclusive, to the 2-letter country code <b>country</b>.
|
|
|
|
*/
|
|
|
|
static void
|
2007-12-17 23:44:11 +01:00
|
|
|
geoip_add_entry(uint32_t low, uint32_t high, const char *country)
|
|
|
|
{
|
2008-02-22 20:09:45 +01:00
|
|
|
intptr_t idx;
|
2007-12-17 23:44:11 +01:00
|
|
|
geoip_entry_t *ent;
|
2007-12-18 22:27:08 +01:00
|
|
|
void *_idxplus1;
|
|
|
|
|
|
|
|
if (high < low)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
|
2007-12-17 23:44:11 +01:00
|
|
|
|
|
|
|
if (!_idxplus1) {
|
2008-06-10 20:08:56 +02:00
|
|
|
geoip_country_t *c = tor_malloc_zero(sizeof(geoip_country_t));
|
|
|
|
strlcpy(c->countrycode, country, sizeof(c->countrycode));
|
|
|
|
tor_strlower(c->countrycode);
|
2007-12-17 23:44:11 +01:00
|
|
|
smartlist_add(geoip_countries, c);
|
2007-12-17 23:44:18 +01:00
|
|
|
idx = smartlist_len(geoip_countries) - 1;
|
2007-12-17 23:44:11 +01:00
|
|
|
strmap_set_lc(country_idxplus1_by_lc_code, country, (void*)(idx+1));
|
|
|
|
} else {
|
|
|
|
idx = ((uintptr_t)_idxplus1)-1;
|
|
|
|
}
|
2008-06-10 20:08:56 +02:00
|
|
|
{
|
|
|
|
geoip_country_t *c = smartlist_get(geoip_countries, idx);
|
|
|
|
tor_assert(!strcasecmp(c->countrycode, country));
|
|
|
|
}
|
2007-12-17 23:44:11 +01:00
|
|
|
ent = tor_malloc_zero(sizeof(geoip_entry_t));
|
|
|
|
ent->ip_low = low;
|
|
|
|
ent->ip_high = high;
|
|
|
|
ent->country = idx;
|
|
|
|
smartlist_add(geoip_entries, ent);
|
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Add an entry to the GeoIP table, parsing it from <b>line</b>. The
|
2008-04-16 02:12:44 +02:00
|
|
|
* format is as for geoip_load_file(). */
|
2007-12-18 22:27:08 +01:00
|
|
|
/*private*/ int
|
|
|
|
geoip_parse_entry(const char *line)
|
|
|
|
{
|
|
|
|
unsigned int low, high;
|
|
|
|
char b[3];
|
|
|
|
if (!geoip_countries) {
|
|
|
|
geoip_countries = smartlist_create();
|
|
|
|
geoip_entries = smartlist_create();
|
|
|
|
country_idxplus1_by_lc_code = strmap_new();
|
|
|
|
}
|
2008-05-28 20:31:57 +02:00
|
|
|
while (TOR_ISSPACE(*line))
|
|
|
|
++line;
|
|
|
|
if (*line == '#')
|
|
|
|
return 0;
|
2007-12-18 22:27:08 +01:00
|
|
|
if (sscanf(line,"%u,%u,%2s", &low, &high, b) == 3) {
|
|
|
|
geoip_add_entry(low, high, b);
|
|
|
|
return 0;
|
|
|
|
} else if (sscanf(line,"\"%u\",\"%u\",\"%2s\",", &low, &high, b) == 3) {
|
|
|
|
geoip_add_entry(low, high, b);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
log_warn(LD_GENERAL, "Unable to parse line from GEOIP file: %s",
|
|
|
|
escaped(line));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sorting helper: return -1, 1, or 0 based on comparison of two
|
|
|
|
* geoip_entry_t */
|
2007-12-17 23:44:11 +01:00
|
|
|
static int
|
|
|
|
_geoip_compare_entries(const void **_a, const void **_b)
|
|
|
|
{
|
|
|
|
const geoip_entry_t *a = *_a, *b = *_b;
|
|
|
|
if (a->ip_low < b->ip_low)
|
|
|
|
return -1;
|
|
|
|
else if (a->ip_low > b->ip_low)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** bsearch helper: return -1, 1, or 0 based on comparison of an IP (a pointer
|
|
|
|
* to a uint32_t in host order) to a geoip_entry_t */
|
2007-12-17 23:44:11 +01:00
|
|
|
static int
|
|
|
|
_geoip_compare_key_to_entry(const void *_key, const void **_member)
|
|
|
|
{
|
|
|
|
const uint32_t addr = *(uint32_t *)_key;
|
|
|
|
const geoip_entry_t *entry = *_member;
|
|
|
|
if (addr < entry->ip_low)
|
|
|
|
return -1;
|
|
|
|
else if (addr > entry->ip_high)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-04 11:02:25 +02:00
|
|
|
/** Return 1 if we should collect geoip stats on bridge users, and
|
|
|
|
* include them in our extrainfo descriptor. Else return 0. */
|
|
|
|
int
|
|
|
|
should_record_bridge_info(or_options_t *options)
|
|
|
|
{
|
|
|
|
return options->BridgeRelay && options->BridgeRecordUsageByCountry;
|
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Clear the GeoIP database and reload it from the file
|
|
|
|
* <b>filename</b>. Return 0 on success, -1 on failure.
|
|
|
|
*
|
|
|
|
* Recognized line formats are:
|
|
|
|
* INTIPLOW,INTIPHIGH,CC
|
|
|
|
* and
|
|
|
|
* "INTIPLOW","INTIPHIGH","CC","CC3","COUNTRY NAME"
|
|
|
|
* where INTIPLOW and INTIPHIGH are IPv4 addresses encoded as 4-byte unsigned
|
|
|
|
* integers, and CC is a country code.
|
2008-05-29 04:29:35 +02:00
|
|
|
*
|
2008-05-28 23:10:46 +02:00
|
|
|
* It also recognizes, and skips over, blank lines and lines that start
|
|
|
|
* with '#' (comments).
|
2007-12-18 22:27:08 +01:00
|
|
|
*/
|
2007-12-17 23:44:11 +01:00
|
|
|
int
|
2008-06-04 11:02:25 +02:00
|
|
|
geoip_load_file(const char *filename, or_options_t *options)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
|
|
|
FILE *f;
|
2008-09-25 23:06:32 +02:00
|
|
|
const char *msg = "";
|
|
|
|
int severity = options_need_geoip_info(options, &msg) ? LOG_WARN : LOG_INFO;
|
2007-12-18 22:27:08 +01:00
|
|
|
clear_geoip_db();
|
2007-12-17 23:44:11 +01:00
|
|
|
if (!(f = fopen(filename, "r"))) {
|
2008-09-25 23:06:32 +02:00
|
|
|
log_fn(severity, LD_GENERAL, "Failed to open GEOIP file %s. %s",
|
|
|
|
filename, msg);
|
2007-12-17 23:44:11 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2008-09-25 22:21:35 +02:00
|
|
|
if (!geoip_countries) {
|
2009-07-09 15:34:53 +02:00
|
|
|
geoip_country_t *geoip_unresolved;
|
2008-09-25 22:21:35 +02:00
|
|
|
geoip_countries = smartlist_create();
|
2009-07-09 15:34:53 +02:00
|
|
|
/* Add a geoip_country_t for requests that could not be resolved to a
|
|
|
|
* country as first element (index 0) to geoip_countries. */
|
|
|
|
geoip_unresolved = tor_malloc_zero(sizeof(geoip_country_t));
|
|
|
|
strlcpy(geoip_unresolved->countrycode, "??",
|
|
|
|
sizeof(geoip_unresolved->countrycode));
|
|
|
|
smartlist_add(geoip_countries, geoip_unresolved);
|
2008-09-25 22:21:35 +02:00
|
|
|
country_idxplus1_by_lc_code = strmap_new();
|
|
|
|
}
|
|
|
|
if (geoip_entries) {
|
|
|
|
SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, e, tor_free(e));
|
|
|
|
smartlist_free(geoip_entries);
|
|
|
|
}
|
2007-12-17 23:44:11 +01:00
|
|
|
geoip_entries = smartlist_create();
|
2008-09-25 23:06:32 +02:00
|
|
|
log_notice(LD_GENERAL, "Parsing GEOIP file.");
|
2007-12-17 23:44:11 +01:00
|
|
|
while (!feof(f)) {
|
2007-12-17 23:44:16 +01:00
|
|
|
char buf[512];
|
2008-02-22 20:09:45 +01:00
|
|
|
if (fgets(buf, (int)sizeof(buf), f) == NULL)
|
2007-12-17 23:44:16 +01:00
|
|
|
break;
|
2008-02-05 22:39:44 +01:00
|
|
|
/* FFFF track full country name. */
|
2007-12-18 22:27:08 +01:00
|
|
|
geoip_parse_entry(buf);
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
2008-12-18 17:11:03 +01:00
|
|
|
/*XXXX abort and return -1 if no entries/illformed?*/
|
2007-12-17 23:44:11 +01:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
smartlist_sort(geoip_entries, _geoip_compare_entries);
|
2009-02-16 16:15:06 +01:00
|
|
|
|
|
|
|
/* Okay, now we need to maybe change our mind about what is in which
|
|
|
|
* country. */
|
|
|
|
refresh_all_country_info();
|
|
|
|
|
2007-12-17 23:44:11 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Given an IP address in host order, return a number representing the
|
|
|
|
* country to which that address belongs, or -1 for unknown. The return value
|
|
|
|
* will always be less than geoip_get_n_countries(). To decode it,
|
|
|
|
* call geoip_get_country_name().
|
|
|
|
*/
|
2007-12-17 23:44:11 +01:00
|
|
|
int
|
|
|
|
geoip_get_country_by_ip(uint32_t ipaddr)
|
|
|
|
{
|
|
|
|
geoip_entry_t *ent;
|
|
|
|
if (!geoip_entries)
|
|
|
|
return -1;
|
|
|
|
ent = smartlist_bsearch(geoip_entries, &ipaddr, _geoip_compare_key_to_entry);
|
2008-02-22 20:09:45 +01:00
|
|
|
return ent ? (int)ent->country : -1;
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2007-12-25 00:51:22 +01:00
|
|
|
/** Return the number of countries recognized by the GeoIP database. */
|
2007-12-17 23:44:11 +01:00
|
|
|
int
|
|
|
|
geoip_get_n_countries(void)
|
|
|
|
{
|
2008-02-22 20:09:45 +01:00
|
|
|
return (int) smartlist_len(geoip_countries);
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Return the two-letter country code associated with the number <b>num</b>,
|
|
|
|
* or "??" for an unknown value. */
|
2007-12-17 23:44:11 +01:00
|
|
|
const char *
|
2008-09-25 22:21:35 +02:00
|
|
|
geoip_get_country_name(country_t num)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2008-06-10 20:08:56 +02:00
|
|
|
if (geoip_countries && num >= 0 && num < smartlist_len(geoip_countries)) {
|
|
|
|
geoip_country_t *c = smartlist_get(geoip_countries, num);
|
|
|
|
return c->countrycode;
|
|
|
|
} else
|
2007-12-17 23:44:11 +01:00
|
|
|
return "??";
|
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Return true iff we have loaded a GeoIP database.*/
|
2007-12-17 23:44:11 +01:00
|
|
|
int
|
|
|
|
geoip_is_loaded(void)
|
|
|
|
{
|
|
|
|
return geoip_countries != NULL && geoip_entries != NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Entry in a map from IP address to the last time we've seen an incoming
|
|
|
|
* connection from that IP address. Used by bridges only, to track which
|
|
|
|
* countries have them blocked. */
|
2007-12-17 23:44:11 +01:00
|
|
|
typedef struct clientmap_entry_t {
|
|
|
|
HT_ENTRY(clientmap_entry_t) node;
|
|
|
|
uint32_t ipaddr;
|
2009-05-26 21:32:56 +02:00
|
|
|
unsigned int last_seen_in_minutes:30;
|
|
|
|
unsigned int action:2;
|
2007-12-17 23:44:11 +01:00
|
|
|
} clientmap_entry_t;
|
|
|
|
|
2008-05-29 04:29:35 +02:00
|
|
|
#define ACTION_MASK 3
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Map from client IP address to last time seen. */
|
2007-12-17 23:44:11 +01:00
|
|
|
static HT_HEAD(clientmap, clientmap_entry_t) client_history =
|
|
|
|
HT_INITIALIZER();
|
2008-06-10 20:08:56 +02:00
|
|
|
/** Time at which we started tracking client IP history. */
|
2007-12-17 23:44:11 +01:00
|
|
|
static time_t client_history_starts = 0;
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** When did the current period of checking per-country request history
|
|
|
|
* start? */
|
2008-06-10 20:08:56 +02:00
|
|
|
static time_t current_request_period_starts = 0;
|
2008-12-17 23:58:20 +01:00
|
|
|
/** How many older request periods are we remembering? */
|
2008-06-10 20:08:56 +02:00
|
|
|
static int n_old_request_periods = 0;
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Hashtable helper: compute a hash of a clientmap_entry_t. */
|
2007-12-17 23:44:11 +01:00
|
|
|
static INLINE unsigned
|
|
|
|
clientmap_entry_hash(const clientmap_entry_t *a)
|
|
|
|
{
|
|
|
|
return ht_improve_hash((unsigned) a->ipaddr);
|
|
|
|
}
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Hashtable helper: compare two clientmap_entry_t values for equality. */
|
2007-12-17 23:44:11 +01:00
|
|
|
static INLINE int
|
|
|
|
clientmap_entries_eq(const clientmap_entry_t *a, const clientmap_entry_t *b)
|
|
|
|
{
|
2009-05-26 21:32:56 +02:00
|
|
|
return a->ipaddr == b->ipaddr && a->action == b->action;
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HT_PROTOTYPE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
|
|
|
|
clientmap_entries_eq);
|
|
|
|
HT_GENERATE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
|
|
|
|
clientmap_entries_eq, 0.6, malloc, realloc, free);
|
|
|
|
|
2009-07-04 22:17:08 +02:00
|
|
|
/** How often do we update our estimate which share of v2 and v3 directory
|
|
|
|
* requests is sent to us? We could as well trigger updates of shares from
|
|
|
|
* network status updates, but that means adding a lot of calls into code
|
|
|
|
* that is independent from geoip stats (and keeping them up-to-date). We
|
|
|
|
* are perfectly fine with an approximation of 15-minute granularity. */
|
|
|
|
#define REQUEST_SHARE_INTERVAL (15 * 60)
|
|
|
|
|
|
|
|
/** When did we last determine which share of v2 and v3 directory requests
|
|
|
|
* is sent to us? */
|
|
|
|
static time_t last_time_determined_shares = 0;
|
|
|
|
|
|
|
|
/** Sum of products of v2 shares times the number of seconds for which we
|
|
|
|
* consider these shares as valid. */
|
|
|
|
static double v2_share_times_seconds;
|
|
|
|
|
|
|
|
/** Sum of products of v3 shares times the number of seconds for which we
|
|
|
|
* consider these shares as valid. */
|
|
|
|
static double v3_share_times_seconds;
|
|
|
|
|
|
|
|
/** Number of seconds we are determining v2 and v3 shares. */
|
|
|
|
static int share_seconds;
|
|
|
|
|
|
|
|
/** Try to determine which fraction of v2 and v3 directory requests aimed at
|
|
|
|
* caches will be sent to us at time <b>now</b> and store that value in
|
|
|
|
* order to take a mean value later on. */
|
|
|
|
static void
|
|
|
|
geoip_determine_shares(time_t now)
|
|
|
|
{
|
|
|
|
double v2_share = 0.0, v3_share = 0.0;
|
|
|
|
if (router_get_my_share_of_directory_requests(&v2_share, &v3_share) < 0)
|
|
|
|
return;
|
|
|
|
if (last_time_determined_shares) {
|
|
|
|
v2_share_times_seconds += v2_share *
|
|
|
|
((double) (now - last_time_determined_shares));
|
|
|
|
v3_share_times_seconds += v3_share *
|
|
|
|
((double) (now - last_time_determined_shares));
|
2009-09-01 05:23:47 +02:00
|
|
|
share_seconds += (int)(now - last_time_determined_shares);
|
2009-07-04 22:17:08 +02:00
|
|
|
}
|
|
|
|
last_time_determined_shares = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Calculate which fraction of v2 and v3 directory requests aimed at caches
|
|
|
|
* have been sent to us since the last call of this function up to time
|
|
|
|
* <b>now</b>. Set *<b>v2_share_out</b> and *<b>v3_share_out</b> to the
|
|
|
|
* fractions of v2 and v3 protocol shares we expect to have seen. Reset
|
|
|
|
* counters afterwards. Return 0 on success, -1 on failure (e.g. when zero
|
|
|
|
* seconds have passed since the last call).*/
|
|
|
|
static int
|
|
|
|
geoip_get_mean_shares(time_t now, double *v2_share_out,
|
|
|
|
double *v3_share_out)
|
|
|
|
{
|
|
|
|
geoip_determine_shares(now);
|
|
|
|
if (!share_seconds)
|
|
|
|
return -1;
|
|
|
|
*v2_share_out = v2_share_times_seconds / ((double) share_seconds);
|
|
|
|
*v3_share_out = v3_share_times_seconds / ((double) share_seconds);
|
|
|
|
v2_share_times_seconds = v3_share_times_seconds = 0.0;
|
|
|
|
share_seconds = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 23:02:36 +02:00
|
|
|
/* Rotate period of v2 and v3 network status requests. */
|
|
|
|
static void
|
|
|
|
rotate_request_period(void)
|
|
|
|
{
|
|
|
|
SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
|
2009-09-24 21:58:56 +02:00
|
|
|
#if REQUEST_HIST_LEN > 1
|
2009-08-21 23:02:36 +02:00
|
|
|
memmove(&c->n_v2_ns_requests[0], &c->n_v2_ns_requests[1],
|
|
|
|
sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
|
|
|
|
memmove(&c->n_v3_ns_requests[0], &c->n_v3_ns_requests[1],
|
|
|
|
sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
|
2009-09-24 21:58:56 +02:00
|
|
|
#endif
|
2009-08-21 23:02:36 +02:00
|
|
|
c->n_v2_ns_requests[REQUEST_HIST_LEN-1] = 0;
|
|
|
|
c->n_v3_ns_requests[REQUEST_HIST_LEN-1] = 0;
|
|
|
|
});
|
|
|
|
current_request_period_starts += REQUEST_HIST_PERIOD;
|
|
|
|
if (n_old_request_periods < REQUEST_HIST_LEN-1)
|
|
|
|
++n_old_request_periods;
|
|
|
|
}
|
2009-07-04 22:17:08 +02:00
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Note that we've seen a client connect from the IP <b>addr</b> (host order)
|
2009-05-26 21:32:56 +02:00
|
|
|
* at time <b>now</b>. Ignored by all but bridges and directories if
|
|
|
|
* configured accordingly. */
|
2007-12-17 23:44:11 +01:00
|
|
|
void
|
2008-05-29 04:29:35 +02:00
|
|
|
geoip_note_client_seen(geoip_client_action_t action,
|
|
|
|
uint32_t addr, time_t now)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
clientmap_entry_t lookup, *ent;
|
2008-05-29 04:29:35 +02:00
|
|
|
if (action == GEOIP_CLIENT_CONNECT) {
|
2009-08-14 14:45:53 +02:00
|
|
|
/* Only remember statistics as entry guard or as bridge. */
|
2009-09-24 21:58:56 +02:00
|
|
|
if (!options->EntryStatistics &&
|
2009-08-14 14:45:53 +02:00
|
|
|
(!(options->BridgeRelay && options->BridgeRecordUsageByCountry)))
|
2009-07-05 20:48:16 +02:00
|
|
|
return;
|
2009-05-26 21:41:42 +02:00
|
|
|
/* Did we recently switch from bridge to relay or back? */
|
|
|
|
if (client_history_starts > now)
|
|
|
|
return;
|
2008-05-29 04:29:35 +02:00
|
|
|
} else {
|
2009-07-15 16:32:40 +02:00
|
|
|
if (options->BridgeRelay || options->BridgeAuthoritativeDir ||
|
|
|
|
!options->DirReqStatistics)
|
2008-05-29 04:29:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-19 23:36:27 +02:00
|
|
|
/* As a bridge that doesn't rotate request periods every 24 hours,
|
|
|
|
* possibly rotate now. */
|
|
|
|
if (options->BridgeRelay) {
|
|
|
|
while (current_request_period_starts + REQUEST_HIST_PERIOD < now) {
|
|
|
|
if (!geoip_countries)
|
|
|
|
geoip_countries = smartlist_create();
|
|
|
|
if (!current_request_period_starts) {
|
|
|
|
current_request_period_starts = now;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Also discard all items in the client history that are too old.
|
|
|
|
* (This only works here because bridge and directory stats are
|
|
|
|
* independent. Otherwise, we'd only want to discard those items
|
|
|
|
* with action GEOIP_CLIENT_NETWORKSTATUS{_V2}.) */
|
|
|
|
geoip_remove_old_clients(current_request_period_starts);
|
|
|
|
/* Now rotate request period */
|
2009-08-21 23:02:36 +02:00
|
|
|
rotate_request_period();
|
2008-06-10 21:51:48 +02:00
|
|
|
}
|
2009-05-26 21:32:56 +02:00
|
|
|
}
|
2008-06-10 20:08:56 +02:00
|
|
|
|
2007-12-17 23:44:11 +01:00
|
|
|
lookup.ipaddr = addr;
|
2009-05-26 21:32:56 +02:00
|
|
|
lookup.action = (int)action;
|
2007-12-17 23:44:11 +01:00
|
|
|
ent = HT_FIND(clientmap, &client_history, &lookup);
|
|
|
|
if (ent) {
|
2009-05-26 21:32:56 +02:00
|
|
|
ent->last_seen_in_minutes = now / 60;
|
2007-12-17 23:44:11 +01:00
|
|
|
} else {
|
|
|
|
ent = tor_malloc_zero(sizeof(clientmap_entry_t));
|
|
|
|
ent->ipaddr = addr;
|
2009-05-26 21:32:56 +02:00
|
|
|
ent->last_seen_in_minutes = now / 60;
|
|
|
|
ent->action = (int)action;
|
2007-12-17 23:44:11 +01:00
|
|
|
HT_INSERT(clientmap, &client_history, ent);
|
|
|
|
}
|
2008-06-10 20:08:56 +02:00
|
|
|
|
|
|
|
if (action == GEOIP_CLIENT_NETWORKSTATUS ||
|
|
|
|
action == GEOIP_CLIENT_NETWORKSTATUS_V2) {
|
|
|
|
int country_idx = geoip_get_country_by_ip(addr);
|
2009-07-09 15:34:53 +02:00
|
|
|
if (country_idx < 0)
|
|
|
|
country_idx = 0; /** unresolved requests are stored at index 0. */
|
2008-06-10 20:08:56 +02:00
|
|
|
if (country_idx >= 0 && country_idx < smartlist_len(geoip_countries)) {
|
|
|
|
geoip_country_t *country = smartlist_get(geoip_countries, country_idx);
|
|
|
|
if (action == GEOIP_CLIENT_NETWORKSTATUS)
|
|
|
|
++country->n_v3_ns_requests[REQUEST_HIST_LEN-1];
|
|
|
|
else
|
|
|
|
++country->n_v2_ns_requests[REQUEST_HIST_LEN-1];
|
|
|
|
}
|
2009-07-04 22:17:08 +02:00
|
|
|
|
|
|
|
/* Periodically determine share of requests that we should see */
|
|
|
|
if (last_time_determined_shares + REQUEST_SHARE_INTERVAL < now)
|
|
|
|
geoip_determine_shares(now);
|
2008-06-10 20:08:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!client_history_starts) {
|
2007-12-17 23:44:11 +01:00
|
|
|
client_history_starts = now;
|
2008-06-10 20:08:56 +02:00
|
|
|
current_request_period_starts = now;
|
|
|
|
}
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** HT_FOREACH helper: remove a clientmap_entry_t from the hashtable if it's
|
|
|
|
* older than a certain time. */
|
2007-12-17 23:44:11 +01:00
|
|
|
static int
|
|
|
|
_remove_old_client_helper(struct clientmap_entry_t *ent, void *_cutoff)
|
|
|
|
{
|
2009-05-26 21:32:56 +02:00
|
|
|
time_t cutoff = *(time_t*)_cutoff / 60;
|
|
|
|
if (ent->last_seen_in_minutes < cutoff) {
|
2007-12-17 23:44:11 +01:00
|
|
|
tor_free(ent);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-26 22:02:52 +02:00
|
|
|
/** Forget about all clients that haven't connected since <b>cutoff</b>.
|
|
|
|
* If <b>cutoff</b> is in the future, clients won't be added to the history
|
|
|
|
* until this time is reached. This is useful to prevent relays that switch
|
|
|
|
* to bridges from reporting unbelievable numbers of clients. */
|
2007-12-17 23:44:11 +01:00
|
|
|
void
|
|
|
|
geoip_remove_old_clients(time_t cutoff)
|
|
|
|
{
|
|
|
|
clientmap_HT_FOREACH_FN(&client_history,
|
|
|
|
_remove_old_client_helper,
|
|
|
|
&cutoff);
|
|
|
|
if (client_history_starts < cutoff)
|
|
|
|
client_history_starts = cutoff;
|
|
|
|
}
|
|
|
|
|
2009-07-10 13:37:25 +02:00
|
|
|
/** How many responses are we giving to clients requesting v2 network
|
|
|
|
* statuses? */
|
|
|
|
static uint32_t ns_v2_responses[GEOIP_NS_RESPONSE_NUM];
|
|
|
|
|
|
|
|
/** How many responses are we giving to clients requesting v3 network
|
|
|
|
* statuses? */
|
|
|
|
static uint32_t ns_v3_responses[GEOIP_NS_RESPONSE_NUM];
|
|
|
|
|
|
|
|
/** Note that we've rejected a client's request for a v2 or v3 network
|
|
|
|
* status, encoded in <b>action</b> for reason <b>reason</b> at time
|
|
|
|
* <b>now</b>. */
|
|
|
|
void
|
|
|
|
geoip_note_ns_response(geoip_client_action_t action,
|
|
|
|
geoip_ns_response_t response)
|
|
|
|
{
|
|
|
|
static int arrays_initialized = 0;
|
2009-07-15 16:32:40 +02:00
|
|
|
if (!get_options()->DirReqStatistics)
|
|
|
|
return;
|
2009-07-10 13:37:25 +02:00
|
|
|
if (!arrays_initialized) {
|
|
|
|
memset(ns_v2_responses, 0, sizeof(ns_v2_responses));
|
|
|
|
memset(ns_v3_responses, 0, sizeof(ns_v3_responses));
|
|
|
|
arrays_initialized = 1;
|
|
|
|
}
|
|
|
|
tor_assert(action == GEOIP_CLIENT_NETWORKSTATUS ||
|
|
|
|
action == GEOIP_CLIENT_NETWORKSTATUS_V2);
|
|
|
|
tor_assert(response < GEOIP_NS_RESPONSE_NUM);
|
|
|
|
if (action == GEOIP_CLIENT_NETWORKSTATUS)
|
|
|
|
ns_v3_responses[response]++;
|
|
|
|
else
|
|
|
|
ns_v2_responses[response]++;
|
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Do not mention any country from which fewer than this number of IPs have
|
2008-05-28 20:31:57 +02:00
|
|
|
* connected. This conceivably avoids reporting information that could
|
|
|
|
* deanonymize users, though analysis is lacking. */
|
2009-01-14 07:20:57 +01:00
|
|
|
#define MIN_IPS_TO_NOTE_COUNTRY 1
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Do not report any geoip data at all if we have fewer than this number of
|
|
|
|
* IPs to report about. */
|
2009-01-14 07:20:57 +01:00
|
|
|
#define MIN_IPS_TO_NOTE_ANYTHING 1
|
2008-04-16 02:12:44 +02:00
|
|
|
/** When reporting geoip data about countries, round up to the nearest
|
2007-12-18 22:27:08 +01:00
|
|
|
* multiple of this value. */
|
2007-12-17 23:44:11 +01:00
|
|
|
#define IP_GRANULARITY 8
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Return the time at which we started recording geoip data. */
|
2007-12-17 23:44:16 +01:00
|
|
|
time_t
|
|
|
|
geoip_get_history_start(void)
|
|
|
|
{
|
|
|
|
return client_history_starts;
|
|
|
|
}
|
|
|
|
|
2008-02-12 21:20:52 +01:00
|
|
|
/** Helper type: used to sort per-country totals by value. */
|
2007-12-19 00:45:19 +01:00
|
|
|
typedef struct c_hist_t {
|
2008-02-21 10:01:32 +01:00
|
|
|
char country[3]; /**< Two-letter country code. */
|
|
|
|
unsigned total; /**< Total IP addresses seen in this country. */
|
2007-12-19 00:45:19 +01:00
|
|
|
} c_hist_t;
|
|
|
|
|
|
|
|
/** Sorting helper: return -1, 1, or 0 based on comparison of two
|
|
|
|
* geoip_entry_t. Sort in descending order of total, and then by country
|
|
|
|
* code. */
|
|
|
|
static int
|
|
|
|
_c_hist_compare(const void **_a, const void **_b)
|
|
|
|
{
|
|
|
|
const c_hist_t *a = *_a, *b = *_b;
|
|
|
|
if (a->total > b->total)
|
|
|
|
return -1;
|
|
|
|
else if (a->total < b->total)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return strcmp(a->country, b->country);
|
|
|
|
}
|
|
|
|
|
2009-07-12 16:33:31 +02:00
|
|
|
/** When there are incomplete directory requests at the end of a 24-hour
|
|
|
|
* period, consider those requests running for longer than this timeout as
|
|
|
|
* failed, the others as still running. */
|
|
|
|
#define DIRREQ_TIMEOUT (10*60)
|
|
|
|
|
|
|
|
/** Entry in a map from either conn->global_identifier for direct requests
|
|
|
|
* or a unique circuit identifier for tunneled requests to request time,
|
|
|
|
* response size, and completion time of a network status request. Used to
|
|
|
|
* measure download times of requests to derive average client
|
|
|
|
* bandwidths. */
|
2009-07-14 22:24:50 +02:00
|
|
|
typedef struct dirreq_map_entry_t {
|
2009-07-15 18:24:02 +02:00
|
|
|
HT_ENTRY(dirreq_map_entry_t) node;
|
2009-07-12 16:33:31 +02:00
|
|
|
/** Unique identifier for this network status request; this is either the
|
|
|
|
* conn->global_identifier of the dir conn (direct request) or a new
|
|
|
|
* locally unique identifier of a circuit (tunneled request). This ID is
|
|
|
|
* only unique among other direct or tunneled requests, respectively. */
|
2009-07-14 22:24:50 +02:00
|
|
|
uint64_t dirreq_id;
|
|
|
|
unsigned int state:3; /**< State of this directory request. */
|
|
|
|
unsigned int type:1; /**< Is this a direct or a tunneled request? */
|
|
|
|
unsigned int completed:1; /**< Is this request complete? */
|
|
|
|
unsigned int action:2; /**< Is this a v2 or v3 request? */
|
2009-07-12 16:33:31 +02:00
|
|
|
/** When did we receive the request and started sending the response? */
|
|
|
|
struct timeval request_time;
|
|
|
|
size_t response_size; /**< What is the size of the response in bytes? */
|
|
|
|
struct timeval completion_time; /**< When did the request succeed? */
|
2009-07-14 22:24:50 +02:00
|
|
|
} dirreq_map_entry_t;
|
2009-07-12 16:33:31 +02:00
|
|
|
|
|
|
|
/** Map of all directory requests asking for v2 or v3 network statuses in
|
2009-07-15 18:24:02 +02:00
|
|
|
* the current geoip-stats interval. Values are
|
2009-07-14 22:24:50 +02:00
|
|
|
* of type *<b>dirreq_map_entry_t</b>. */
|
2009-07-15 18:24:02 +02:00
|
|
|
static HT_HEAD(dirreqmap, dirreq_map_entry_t) dirreq_map =
|
|
|
|
HT_INITIALIZER();
|
|
|
|
|
|
|
|
static int
|
|
|
|
dirreq_map_ent_eq(const dirreq_map_entry_t *a,
|
|
|
|
const dirreq_map_entry_t *b)
|
|
|
|
{
|
|
|
|
return a->dirreq_id == b->dirreq_id && a->type == b->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
dirreq_map_ent_hash(const dirreq_map_entry_t *entry)
|
|
|
|
{
|
|
|
|
unsigned u = (unsigned) entry->dirreq_id;
|
|
|
|
u += entry->type << 20;
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
HT_PROTOTYPE(dirreqmap, dirreq_map_entry_t, node, dirreq_map_ent_hash,
|
|
|
|
dirreq_map_ent_eq);
|
|
|
|
HT_GENERATE(dirreqmap, dirreq_map_entry_t, node, dirreq_map_ent_hash,
|
|
|
|
dirreq_map_ent_eq, 0.6, malloc, realloc, free);
|
2009-07-12 16:33:31 +02:00
|
|
|
|
|
|
|
/** Helper: Put <b>entry</b> into map of directory requests using
|
2009-07-14 22:24:50 +02:00
|
|
|
* <b>tunneled</b> and <b>dirreq_id</b> as key parts. If there is
|
2009-07-12 16:33:31 +02:00
|
|
|
* already an entry for that key, print out a BUG warning and return. */
|
|
|
|
static void
|
2009-07-14 22:24:50 +02:00
|
|
|
_dirreq_map_put(dirreq_map_entry_t *entry, dirreq_type_t type,
|
|
|
|
uint64_t dirreq_id)
|
2009-07-12 16:33:31 +02:00
|
|
|
{
|
2009-07-15 18:24:02 +02:00
|
|
|
dirreq_map_entry_t *old_ent;
|
|
|
|
tor_assert(entry->type == type);
|
|
|
|
tor_assert(entry->dirreq_id == dirreq_id);
|
|
|
|
|
|
|
|
/* XXXX022 once we're sure the bug case never happens, we can switch
|
|
|
|
* to HT_INSERT */
|
|
|
|
old_ent = HT_REPLACE(dirreqmap, &dirreq_map, entry);
|
|
|
|
if (old_ent && old_ent != entry) {
|
2009-07-12 16:33:31 +02:00
|
|
|
log_warn(LD_BUG, "Error when putting directory request into local "
|
2009-07-15 18:24:02 +02:00
|
|
|
"map. There was already an entry for the same identifier.");
|
2009-07-12 16:33:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: Look up and return an entry in the map of directory requests
|
2009-07-14 22:24:50 +02:00
|
|
|
* using <b>tunneled</b> and <b>dirreq_id</b> as key parts. If there
|
2009-07-12 16:33:31 +02:00
|
|
|
* is no such entry, return NULL. */
|
2009-07-14 22:24:50 +02:00
|
|
|
static dirreq_map_entry_t *
|
|
|
|
_dirreq_map_get(dirreq_type_t type, uint64_t dirreq_id)
|
2009-07-12 16:33:31 +02:00
|
|
|
{
|
2009-07-15 18:24:02 +02:00
|
|
|
dirreq_map_entry_t lookup;
|
|
|
|
lookup.type = type;
|
|
|
|
lookup.dirreq_id = dirreq_id;
|
|
|
|
return HT_FIND(dirreqmap, &dirreq_map, &lookup);
|
2009-07-12 16:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Note that an either direct or tunneled (see <b>type</b>) directory
|
2009-07-14 22:24:50 +02:00
|
|
|
* request for a network status with unique ID <b>dirreq_id</b> of size
|
2009-07-12 16:33:31 +02:00
|
|
|
* <b>response_size</b> and action <b>action</b> (either v2 or v3) has
|
|
|
|
* started. */
|
|
|
|
void
|
2009-07-14 22:24:50 +02:00
|
|
|
geoip_start_dirreq(uint64_t dirreq_id, size_t response_size,
|
|
|
|
geoip_client_action_t action, dirreq_type_t type)
|
2009-07-12 16:33:31 +02:00
|
|
|
{
|
2009-07-15 16:32:40 +02:00
|
|
|
dirreq_map_entry_t *ent;
|
|
|
|
if (!get_options()->DirReqStatistics)
|
|
|
|
return;
|
|
|
|
ent = tor_malloc_zero(sizeof(dirreq_map_entry_t));
|
2009-07-14 22:24:50 +02:00
|
|
|
ent->dirreq_id = dirreq_id;
|
2009-07-12 16:33:31 +02:00
|
|
|
tor_gettimeofday(&ent->request_time);
|
|
|
|
ent->response_size = response_size;
|
|
|
|
ent->action = action;
|
|
|
|
ent->type = type;
|
2009-07-14 22:24:50 +02:00
|
|
|
_dirreq_map_put(ent, type, dirreq_id);
|
2009-07-12 16:33:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Change the state of the either direct or tunneled (see <b>type</b>)
|
2009-07-14 22:24:50 +02:00
|
|
|
* directory request with <b>dirreq_id</b> to <b>new_state</b> and
|
2009-07-12 16:33:31 +02:00
|
|
|
* possibly mark it as completed. If no entry can be found for the given
|
|
|
|
* key parts (e.g., if this is a directory request that we are not
|
|
|
|
* measuring, or one that was started in the previous measurement period),
|
|
|
|
* or if the state cannot be advanced to <b>new_state</b>, do nothing. */
|
|
|
|
void
|
2009-07-14 22:24:50 +02:00
|
|
|
geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type,
|
|
|
|
dirreq_state_t new_state)
|
2009-07-12 16:33:31 +02:00
|
|
|
{
|
2009-07-15 16:32:40 +02:00
|
|
|
dirreq_map_entry_t *ent;
|
|
|
|
if (!get_options()->DirReqStatistics)
|
|
|
|
return;
|
|
|
|
ent = _dirreq_map_get(type, dirreq_id);
|
2009-07-12 16:33:31 +02:00
|
|
|
if (!ent)
|
|
|
|
return;
|
2009-07-14 22:24:50 +02:00
|
|
|
if (new_state == DIRREQ_IS_FOR_NETWORK_STATUS)
|
2009-07-12 16:33:31 +02:00
|
|
|
return;
|
|
|
|
if (new_state - 1 != ent->state)
|
|
|
|
return;
|
|
|
|
ent->state = new_state;
|
2009-07-14 22:24:50 +02:00
|
|
|
if ((type == DIRREQ_DIRECT &&
|
|
|
|
new_state == DIRREQ_FLUSHING_DIR_CONN_FINISHED) ||
|
|
|
|
(type == DIRREQ_TUNNELED &&
|
|
|
|
new_state == DIRREQ_OR_CONN_BUFFER_FLUSHED)) {
|
2009-07-12 16:33:31 +02:00
|
|
|
tor_gettimeofday(&ent->completion_time);
|
|
|
|
ent->completed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a newly allocated comma-separated string containing statistics
|
|
|
|
* on network status downloads. The string contains the number of completed
|
|
|
|
* requests, timeouts, and still running requests as well as the download
|
|
|
|
* times by deciles and quartiles. Return NULL if we have not observed
|
|
|
|
* requests for long enough. */
|
|
|
|
static char *
|
2009-07-14 22:24:50 +02:00
|
|
|
geoip_get_dirreq_history(geoip_client_action_t action,
|
|
|
|
dirreq_type_t type)
|
2009-07-12 16:33:31 +02:00
|
|
|
{
|
|
|
|
char *result = NULL;
|
2009-08-21 19:35:47 +02:00
|
|
|
smartlist_t *dirreq_completed = NULL;
|
2009-07-12 16:33:31 +02:00
|
|
|
uint32_t complete = 0, timeouts = 0, running = 0;
|
2009-08-21 19:35:47 +02:00
|
|
|
int bufsize = 1024, written;
|
2009-07-15 18:24:02 +02:00
|
|
|
dirreq_map_entry_t **ptr, **next, *ent;
|
2009-07-12 16:33:31 +02:00
|
|
|
struct timeval now;
|
2009-07-15 18:24:02 +02:00
|
|
|
|
2009-07-12 16:33:31 +02:00
|
|
|
tor_gettimeofday(&now);
|
|
|
|
if (action != GEOIP_CLIENT_NETWORKSTATUS &&
|
|
|
|
action != GEOIP_CLIENT_NETWORKSTATUS_V2)
|
|
|
|
return NULL;
|
2009-08-21 19:35:47 +02:00
|
|
|
dirreq_completed = smartlist_create();
|
2009-07-15 18:24:02 +02:00
|
|
|
for (ptr = HT_START(dirreqmap, &dirreq_map); ptr; ptr = next) {
|
|
|
|
ent = *ptr;
|
|
|
|
if (ent->action != action || ent->type != type) {
|
|
|
|
next = HT_NEXT(dirreqmap, &dirreq_map, ptr);
|
|
|
|
continue;
|
|
|
|
} else {
|
2009-07-12 16:33:31 +02:00
|
|
|
if (ent->completed) {
|
2009-08-21 19:35:47 +02:00
|
|
|
smartlist_add(dirreq_completed, ent);
|
2009-07-12 16:33:31 +02:00
|
|
|
complete++;
|
2009-08-21 19:35:47 +02:00
|
|
|
next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ptr);
|
2009-07-12 16:33:31 +02:00
|
|
|
} else {
|
2009-07-27 16:23:53 +02:00
|
|
|
if (tv_mdiff(&ent->request_time, &now) / 1000 > DIRREQ_TIMEOUT)
|
2009-07-12 16:33:31 +02:00
|
|
|
timeouts++;
|
|
|
|
else
|
|
|
|
running++;
|
2009-08-21 19:35:47 +02:00
|
|
|
next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ptr);
|
|
|
|
tor_free(ent);
|
2009-07-12 16:33:31 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-15 18:24:02 +02:00
|
|
|
}
|
2009-07-14 02:04:20 +02:00
|
|
|
#define DIR_REQ_GRANULARITY 4
|
|
|
|
complete = round_uint32_to_next_multiple_of(complete,
|
|
|
|
DIR_REQ_GRANULARITY);
|
|
|
|
timeouts = round_uint32_to_next_multiple_of(timeouts,
|
|
|
|
DIR_REQ_GRANULARITY);
|
|
|
|
running = round_uint32_to_next_multiple_of(running,
|
|
|
|
DIR_REQ_GRANULARITY);
|
2009-07-12 16:33:31 +02:00
|
|
|
result = tor_malloc_zero(bufsize);
|
2009-07-13 01:30:30 +02:00
|
|
|
written = tor_snprintf(result, bufsize, "complete=%u,timeout=%u,"
|
|
|
|
"running=%u", complete, timeouts, running);
|
2009-08-21 19:17:50 +02:00
|
|
|
if (written < 0) {
|
2009-08-21 19:35:47 +02:00
|
|
|
tor_free(result);
|
|
|
|
goto done;
|
2009-08-21 19:17:50 +02:00
|
|
|
}
|
2009-08-21 19:35:47 +02:00
|
|
|
|
2009-07-12 16:33:31 +02:00
|
|
|
#define MIN_DIR_REQ_RESPONSES 16
|
|
|
|
if (complete >= MIN_DIR_REQ_RESPONSES) {
|
2009-08-21 19:35:47 +02:00
|
|
|
uint32_t *dltimes;
|
|
|
|
/* We may have rounded 'completed' up. Here we want to use the
|
|
|
|
* real value. */
|
|
|
|
complete = smartlist_len(dirreq_completed);
|
|
|
|
dltimes = tor_malloc_zero(sizeof(uint32_t) * complete);
|
|
|
|
SMARTLIST_FOREACH_BEGIN(dirreq_completed, dirreq_map_entry_t *, ent) {
|
|
|
|
uint32_t bytes_per_second;
|
|
|
|
uint32_t time_diff = (uint32_t) tv_mdiff(&ent->request_time,
|
|
|
|
&ent->completion_time);
|
|
|
|
if (time_diff == 0)
|
|
|
|
time_diff = 1; /* Avoid DIV/0; "instant" answers are impossible
|
|
|
|
* by law of nature or something, but a milisecond
|
|
|
|
* is a bit greater than "instantly" */
|
2009-09-01 05:23:47 +02:00
|
|
|
bytes_per_second = (uint32_t)(1000 * ent->response_size / time_diff);
|
2009-08-21 19:35:47 +02:00
|
|
|
dltimes[ent_sl_idx] = bytes_per_second;
|
|
|
|
} SMARTLIST_FOREACH_END(ent);
|
|
|
|
median_uint32(dltimes, complete); /* sorts as a side effect. */
|
2009-07-12 16:33:31 +02:00
|
|
|
written = tor_snprintf(result + written, bufsize - written,
|
2009-07-13 01:30:30 +02:00
|
|
|
",min=%u,d1=%u,d2=%u,q1=%u,d3=%u,d4=%u,md=%u,"
|
|
|
|
"d6=%u,d7=%u,q3=%u,d8=%u,d9=%u,max=%u",
|
2009-07-12 16:33:31 +02:00
|
|
|
dltimes[0],
|
|
|
|
dltimes[1*complete/10-1],
|
|
|
|
dltimes[2*complete/10-1],
|
|
|
|
dltimes[1*complete/4-1],
|
|
|
|
dltimes[3*complete/10-1],
|
|
|
|
dltimes[4*complete/10-1],
|
|
|
|
dltimes[5*complete/10-1],
|
|
|
|
dltimes[6*complete/10-1],
|
|
|
|
dltimes[7*complete/10-1],
|
|
|
|
dltimes[3*complete/4-1],
|
|
|
|
dltimes[8*complete/10-1],
|
|
|
|
dltimes[9*complete/10-1],
|
|
|
|
dltimes[complete-1]);
|
2009-08-21 19:35:47 +02:00
|
|
|
if (written<0)
|
|
|
|
tor_free(result);
|
2009-07-12 16:33:31 +02:00
|
|
|
tor_free(dltimes);
|
|
|
|
}
|
2009-08-21 19:35:47 +02:00
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(dirreq_completed, dirreq_map_entry_t *, ent,
|
|
|
|
tor_free(ent));
|
|
|
|
smartlist_free(dirreq_completed);
|
2009-07-12 16:33:31 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** How long do we have to have observed per-country request history before we
|
|
|
|
* are willing to talk about it? */
|
2008-08-29 19:02:26 +02:00
|
|
|
#define GEOIP_MIN_OBSERVATION_TIME (12*60*60)
|
2008-06-10 20:08:56 +02:00
|
|
|
|
2009-08-14 14:45:53 +02:00
|
|
|
/** Helper for geoip_get_client_history_dirreq() and
|
|
|
|
* geoip_get_client_history_bridge(). */
|
|
|
|
static char *
|
|
|
|
geoip_get_client_history(time_t now, geoip_client_action_t action,
|
|
|
|
int min_observation_time, unsigned granularity)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
|
|
|
char *result = NULL;
|
|
|
|
if (!geoip_is_loaded())
|
|
|
|
return NULL;
|
2009-05-27 17:52:46 +02:00
|
|
|
if (client_history_starts < (now - min_observation_time)) {
|
2007-12-17 23:44:11 +01:00
|
|
|
smartlist_t *chunks = NULL;
|
2007-12-19 00:45:19 +01:00
|
|
|
smartlist_t *entries = NULL;
|
2007-12-17 23:44:11 +01:00
|
|
|
int n_countries = geoip_get_n_countries();
|
|
|
|
int i;
|
|
|
|
clientmap_entry_t **ent;
|
|
|
|
unsigned *counts = tor_malloc_zero(sizeof(unsigned)*n_countries);
|
|
|
|
unsigned total = 0;
|
|
|
|
HT_FOREACH(ent, clientmap, &client_history) {
|
2008-05-29 04:29:35 +02:00
|
|
|
int country;
|
2009-05-26 21:32:56 +02:00
|
|
|
if ((*ent)->action != (int)action)
|
2008-05-29 04:29:35 +02:00
|
|
|
continue;
|
|
|
|
country = geoip_get_country_by_ip((*ent)->ipaddr);
|
2007-12-17 23:44:11 +01:00
|
|
|
if (country < 0)
|
2009-07-09 15:34:53 +02:00
|
|
|
country = 0; /** unresolved requests are stored at index 0. */
|
2007-12-17 23:44:11 +01:00
|
|
|
tor_assert(0 <= country && country < n_countries);
|
|
|
|
++counts[country];
|
|
|
|
++total;
|
|
|
|
}
|
2007-12-19 00:45:19 +01:00
|
|
|
/* Don't record anything if we haven't seen enough IPs. */
|
2007-12-17 23:44:11 +01:00
|
|
|
if (total < MIN_IPS_TO_NOTE_ANYTHING)
|
|
|
|
goto done;
|
2007-12-19 00:45:19 +01:00
|
|
|
/* Make a list of c_hist_t */
|
|
|
|
entries = smartlist_create();
|
2007-12-17 23:44:11 +01:00
|
|
|
for (i = 0; i < n_countries; ++i) {
|
|
|
|
unsigned c = counts[i];
|
|
|
|
const char *countrycode;
|
2007-12-19 00:45:19 +01:00
|
|
|
c_hist_t *ent;
|
|
|
|
/* Only report a country if it has a minimum number of IPs. */
|
2007-12-17 23:44:11 +01:00
|
|
|
if (c >= MIN_IPS_TO_NOTE_COUNTRY) {
|
2008-06-18 06:34:52 +02:00
|
|
|
c = round_to_next_multiple_of(c, granularity);
|
2007-12-17 23:44:11 +01:00
|
|
|
countrycode = geoip_get_country_name(i);
|
2007-12-19 00:45:19 +01:00
|
|
|
ent = tor_malloc(sizeof(c_hist_t));
|
|
|
|
strlcpy(ent->country, countrycode, sizeof(ent->country));
|
|
|
|
ent->total = c;
|
|
|
|
smartlist_add(entries, ent);
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
}
|
2007-12-19 00:45:19 +01:00
|
|
|
/* Sort entries. Note that we must do this _AFTER_ rounding, or else
|
|
|
|
* the sort order could leak info. */
|
|
|
|
smartlist_sort(entries, _c_hist_compare);
|
|
|
|
|
|
|
|
/* Build the result. */
|
|
|
|
chunks = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(entries, c_hist_t *, ch, {
|
2010-03-01 03:46:50 +01:00
|
|
|
char *buf=NULL;
|
|
|
|
tor_asprintf(&buf, "%s=%u", ch->country, ch->total);
|
|
|
|
smartlist_add(chunks, buf);
|
|
|
|
});
|
2007-12-17 23:44:11 +01:00
|
|
|
result = smartlist_join_strings(chunks, ",", 0, NULL);
|
|
|
|
done:
|
|
|
|
tor_free(counts);
|
|
|
|
if (chunks) {
|
|
|
|
SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
|
|
|
|
smartlist_free(chunks);
|
|
|
|
}
|
2007-12-19 00:45:19 +01:00
|
|
|
if (entries) {
|
|
|
|
SMARTLIST_FOREACH(entries, c_hist_t *, c, tor_free(c));
|
|
|
|
smartlist_free(entries);
|
|
|
|
}
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-08-14 14:45:53 +02:00
|
|
|
/** Return a newly allocated comma-separated string containing entries for
|
|
|
|
* all the countries from which we've seen enough clients connect as a
|
|
|
|
* directory. The entry format is cc=num where num is the number of IPs
|
|
|
|
* we've seen connecting from that country, and cc is a lowercased country
|
|
|
|
* code. Returns NULL if we don't want to export geoip data yet. */
|
|
|
|
char *
|
|
|
|
geoip_get_client_history_dirreq(time_t now,
|
|
|
|
geoip_client_action_t action)
|
|
|
|
{
|
|
|
|
return geoip_get_client_history(now, action,
|
|
|
|
DIR_RECORD_USAGE_MIN_OBSERVATION_TIME,
|
|
|
|
DIR_RECORD_USAGE_GRANULARITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a newly allocated comma-separated string containing entries for
|
|
|
|
* all the countries from which we've seen enough clients connect as a
|
|
|
|
* bridge. The entry format is cc=num where num is the number of IPs
|
|
|
|
* we've seen connecting from that country, and cc is a lowercased country
|
|
|
|
* code. Returns NULL if we don't want to export geoip data yet. */
|
|
|
|
char *
|
|
|
|
geoip_get_client_history_bridge(time_t now,
|
|
|
|
geoip_client_action_t action)
|
|
|
|
{
|
|
|
|
return geoip_get_client_history(now, action,
|
|
|
|
GEOIP_MIN_OBSERVATION_TIME,
|
|
|
|
IP_GRANULARITY);
|
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** Return a newly allocated string holding the per-country request history
|
|
|
|
* for <b>action</b> in a format suitable for an extra-info document, or NULL
|
|
|
|
* on failure. */
|
2008-06-10 20:08:56 +02:00
|
|
|
char *
|
|
|
|
geoip_get_request_history(time_t now, geoip_client_action_t action)
|
|
|
|
{
|
2008-06-18 06:34:52 +02:00
|
|
|
smartlist_t *entries, *strings;
|
2008-06-10 20:08:56 +02:00
|
|
|
char *result;
|
2008-06-18 06:34:52 +02:00
|
|
|
unsigned granularity = IP_GRANULARITY;
|
2009-05-27 20:19:24 +02:00
|
|
|
int min_observation_time = GEOIP_MIN_OBSERVATION_TIME;
|
2008-06-18 06:34:52 +02:00
|
|
|
|
2009-05-27 17:52:46 +02:00
|
|
|
if (client_history_starts >= (now - min_observation_time))
|
2008-06-10 20:08:56 +02:00
|
|
|
return NULL;
|
|
|
|
if (action != GEOIP_CLIENT_NETWORKSTATUS &&
|
|
|
|
action != GEOIP_CLIENT_NETWORKSTATUS_V2)
|
|
|
|
return NULL;
|
|
|
|
if (!geoip_countries)
|
|
|
|
return NULL;
|
2008-06-18 06:34:52 +02:00
|
|
|
|
2008-06-10 20:08:56 +02:00
|
|
|
entries = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
|
|
|
|
uint32_t *n = (action == GEOIP_CLIENT_NETWORKSTATUS)
|
|
|
|
? c->n_v3_ns_requests : c->n_v2_ns_requests;
|
|
|
|
uint32_t tot = 0;
|
|
|
|
int i;
|
2008-06-18 06:34:52 +02:00
|
|
|
c_hist_t *ent;
|
2008-06-10 20:08:56 +02:00
|
|
|
for (i=0; i < REQUEST_HIST_LEN; ++i)
|
|
|
|
tot += n[i];
|
2008-06-10 21:25:25 +02:00
|
|
|
if (!tot)
|
|
|
|
continue;
|
2008-06-18 06:34:52 +02:00
|
|
|
ent = tor_malloc_zero(sizeof(c_hist_t));
|
|
|
|
strlcpy(ent->country, c->countrycode, sizeof(ent->country));
|
|
|
|
ent->total = round_to_next_multiple_of(tot, granularity);
|
|
|
|
smartlist_add(entries, ent);
|
2008-06-10 20:08:56 +02:00
|
|
|
});
|
2008-06-18 06:34:52 +02:00
|
|
|
smartlist_sort(entries, _c_hist_compare);
|
|
|
|
|
|
|
|
strings = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
|
2010-04-20 07:09:40 +02:00
|
|
|
char *buf = NULL;
|
|
|
|
tor_asprintf(&buf, "%s=%u", ent->country, ent->total);
|
2010-03-01 03:46:50 +01:00
|
|
|
smartlist_add(strings, buf);
|
2008-06-18 06:34:52 +02:00
|
|
|
});
|
|
|
|
result = smartlist_join_strings(strings, ",", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp));
|
|
|
|
SMARTLIST_FOREACH(entries, c_hist_t *, ent, tor_free(ent));
|
|
|
|
smartlist_free(strings);
|
|
|
|
smartlist_free(entries);
|
2008-06-10 20:08:56 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-08-19 15:41:12 +02:00
|
|
|
/** Start time of directory request stats. */
|
|
|
|
static time_t start_of_dirreq_stats_interval;
|
|
|
|
|
|
|
|
/** Initialize directory request stats. */
|
|
|
|
void
|
|
|
|
geoip_dirreq_stats_init(time_t now)
|
|
|
|
{
|
|
|
|
start_of_dirreq_stats_interval = now;
|
|
|
|
}
|
|
|
|
|
2009-08-19 17:10:40 +02:00
|
|
|
/** Write dirreq statistics to $DATADIR/stats/dirreq-stats. */
|
2009-08-19 15:41:12 +02:00
|
|
|
void
|
|
|
|
geoip_dirreq_stats_write(time_t now)
|
2008-05-29 04:29:35 +02:00
|
|
|
{
|
2009-08-19 17:10:40 +02:00
|
|
|
char *statsdir = NULL, *filename = NULL;
|
2008-05-29 04:29:35 +02:00
|
|
|
char *data_v2 = NULL, *data_v3 = NULL;
|
2009-08-14 15:30:24 +02:00
|
|
|
char written[ISO_TIME_LEN+1];
|
2008-05-29 04:29:35 +02:00
|
|
|
open_file_t *open_file = NULL;
|
2008-06-10 20:28:10 +02:00
|
|
|
double v2_share = 0.0, v3_share = 0.0;
|
2008-05-29 04:29:35 +02:00
|
|
|
FILE *out;
|
2009-07-10 13:37:25 +02:00
|
|
|
int i;
|
2008-05-29 04:29:35 +02:00
|
|
|
|
2009-07-15 16:32:40 +02:00
|
|
|
if (!get_options()->DirReqStatistics)
|
|
|
|
goto done;
|
|
|
|
|
2009-08-19 15:41:12 +02:00
|
|
|
/* Discard all items in the client history that are too old. */
|
|
|
|
geoip_remove_old_clients(start_of_dirreq_stats_interval);
|
|
|
|
|
2009-08-19 17:10:40 +02:00
|
|
|
statsdir = get_datadir_fname("stats");
|
|
|
|
if (check_private_dir(statsdir, CPD_CREATE) < 0)
|
|
|
|
goto done;
|
2009-12-17 11:20:31 +01:00
|
|
|
filename = get_datadir_fname2("stats", "dirreq-stats");
|
2009-08-14 14:45:53 +02:00
|
|
|
data_v2 = geoip_get_client_history_dirreq(now,
|
|
|
|
GEOIP_CLIENT_NETWORKSTATUS_V2);
|
|
|
|
data_v3 = geoip_get_client_history_dirreq(now,
|
|
|
|
GEOIP_CLIENT_NETWORKSTATUS);
|
2008-05-29 04:29:35 +02:00
|
|
|
format_iso_time(written, now);
|
2009-05-27 17:52:46 +02:00
|
|
|
out = start_writing_to_stdio_file(filename, OPEN_FLAGS_APPEND,
|
2008-06-10 20:08:56 +02:00
|
|
|
0600, &open_file);
|
2008-05-29 04:29:35 +02:00
|
|
|
if (!out)
|
|
|
|
goto done;
|
2009-08-14 15:30:24 +02:00
|
|
|
if (fprintf(out, "dirreq-stats-end %s (%d s)\ndirreq-v3-ips %s\n"
|
2009-08-19 23:36:27 +02:00
|
|
|
"dirreq-v2-ips %s\n", written,
|
|
|
|
(unsigned) (now - start_of_dirreq_stats_interval),
|
2008-06-10 20:08:56 +02:00
|
|
|
data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
|
|
|
|
goto done;
|
|
|
|
tor_free(data_v2);
|
|
|
|
tor_free(data_v3);
|
|
|
|
|
|
|
|
data_v2 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
|
|
|
|
data_v3 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS);
|
2009-08-14 15:30:24 +02:00
|
|
|
if (fprintf(out, "dirreq-v3-reqs %s\ndirreq-v2-reqs %s\n",
|
2008-06-10 20:08:56 +02:00
|
|
|
data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
|
2008-05-29 04:29:35 +02:00
|
|
|
goto done;
|
2009-09-24 21:58:56 +02:00
|
|
|
tor_free(data_v2);
|
|
|
|
tor_free(data_v3);
|
2009-07-10 13:37:25 +02:00
|
|
|
#define RESPONSE_GRANULARITY 8
|
|
|
|
for (i = 0; i < GEOIP_NS_RESPONSE_NUM; i++) {
|
2009-07-14 01:50:55 +02:00
|
|
|
ns_v2_responses[i] = round_uint32_to_next_multiple_of(
|
|
|
|
ns_v2_responses[i], RESPONSE_GRANULARITY);
|
|
|
|
ns_v3_responses[i] = round_uint32_to_next_multiple_of(
|
|
|
|
ns_v3_responses[i], RESPONSE_GRANULARITY);
|
2009-07-10 13:37:25 +02:00
|
|
|
}
|
|
|
|
#undef RESPONSE_GRANULARITY
|
2009-08-14 15:30:24 +02:00
|
|
|
if (fprintf(out, "dirreq-v3-resp ok=%u,not-enough-sigs=%u,unavailable=%u,"
|
2009-07-14 01:50:55 +02:00
|
|
|
"not-found=%u,not-modified=%u,busy=%u\n",
|
2009-07-10 13:37:25 +02:00
|
|
|
ns_v3_responses[GEOIP_SUCCESS],
|
|
|
|
ns_v3_responses[GEOIP_REJECT_NOT_ENOUGH_SIGS],
|
|
|
|
ns_v3_responses[GEOIP_REJECT_UNAVAILABLE],
|
|
|
|
ns_v3_responses[GEOIP_REJECT_NOT_FOUND],
|
|
|
|
ns_v3_responses[GEOIP_REJECT_NOT_MODIFIED],
|
|
|
|
ns_v3_responses[GEOIP_REJECT_BUSY]) < 0)
|
|
|
|
goto done;
|
2009-08-14 15:30:24 +02:00
|
|
|
if (fprintf(out, "dirreq-v2-resp ok=%u,unavailable=%u,"
|
2009-07-14 01:50:55 +02:00
|
|
|
"not-found=%u,not-modified=%u,busy=%u\n",
|
2009-07-10 13:37:25 +02:00
|
|
|
ns_v2_responses[GEOIP_SUCCESS],
|
|
|
|
ns_v2_responses[GEOIP_REJECT_UNAVAILABLE],
|
|
|
|
ns_v2_responses[GEOIP_REJECT_NOT_FOUND],
|
|
|
|
ns_v2_responses[GEOIP_REJECT_NOT_MODIFIED],
|
|
|
|
ns_v2_responses[GEOIP_REJECT_BUSY]) < 0)
|
|
|
|
goto done;
|
|
|
|
memset(ns_v2_responses, 0, sizeof(ns_v2_responses));
|
|
|
|
memset(ns_v3_responses, 0, sizeof(ns_v3_responses));
|
2009-07-04 22:17:08 +02:00
|
|
|
if (!geoip_get_mean_shares(now, &v2_share, &v3_share)) {
|
2009-08-14 15:30:24 +02:00
|
|
|
if (fprintf(out, "dirreq-v2-share %0.2lf%%\n", v2_share*100) < 0)
|
2008-06-10 20:28:10 +02:00
|
|
|
goto done;
|
2009-08-14 15:30:24 +02:00
|
|
|
if (fprintf(out, "dirreq-v3-share %0.2lf%%\n", v3_share*100) < 0)
|
2008-06-10 20:28:10 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2008-05-29 04:29:35 +02:00
|
|
|
|
2009-07-14 22:24:50 +02:00
|
|
|
data_v2 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS_V2,
|
|
|
|
DIRREQ_DIRECT);
|
|
|
|
data_v3 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS,
|
|
|
|
DIRREQ_DIRECT);
|
2009-08-14 15:30:24 +02:00
|
|
|
if (fprintf(out, "dirreq-v3-direct-dl %s\ndirreq-v2-direct-dl %s\n",
|
2009-07-12 16:33:31 +02:00
|
|
|
data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
|
|
|
|
goto done;
|
|
|
|
tor_free(data_v2);
|
|
|
|
tor_free(data_v3);
|
2009-07-14 22:24:50 +02:00
|
|
|
data_v2 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS_V2,
|
|
|
|
DIRREQ_TUNNELED);
|
|
|
|
data_v3 = geoip_get_dirreq_history(GEOIP_CLIENT_NETWORKSTATUS,
|
|
|
|
DIRREQ_TUNNELED);
|
2009-08-14 15:30:24 +02:00
|
|
|
if (fprintf(out, "dirreq-v3-tunneled-dl %s\ndirreq-v2-tunneled-dl %s\n",
|
2009-07-12 16:33:31 +02:00
|
|
|
data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
|
|
|
|
goto done;
|
|
|
|
|
2008-05-29 04:29:35 +02:00
|
|
|
finish_writing_to_file(open_file);
|
|
|
|
open_file = NULL;
|
2009-08-19 15:41:12 +02:00
|
|
|
|
|
|
|
/* Rotate request period */
|
2009-08-21 23:02:36 +02:00
|
|
|
rotate_request_period();
|
2009-08-19 15:41:12 +02:00
|
|
|
|
|
|
|
start_of_dirreq_stats_interval = now;
|
|
|
|
|
2008-05-29 04:29:35 +02:00
|
|
|
done:
|
|
|
|
if (open_file)
|
|
|
|
abort_writing_to_file(open_file);
|
|
|
|
tor_free(filename);
|
2009-08-19 17:10:40 +02:00
|
|
|
tor_free(statsdir);
|
2008-05-29 04:29:35 +02:00
|
|
|
tor_free(data_v2);
|
|
|
|
tor_free(data_v3);
|
|
|
|
}
|
|
|
|
|
2009-12-12 08:32:46 +01:00
|
|
|
/** Start time of bridge stats. */
|
|
|
|
static time_t start_of_bridge_stats_interval;
|
|
|
|
|
|
|
|
/** Initialize bridge stats. */
|
|
|
|
void
|
|
|
|
geoip_bridge_stats_init(time_t now)
|
|
|
|
{
|
|
|
|
start_of_bridge_stats_interval = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse the bridge statistics as they are written to extra-info
|
|
|
|
* descriptors for being returned to controller clients. Return the
|
|
|
|
* controller string if successful, or NULL otherwise. */
|
|
|
|
static char *
|
|
|
|
parse_bridge_stats_controller(const char *stats_str, time_t now)
|
|
|
|
{
|
|
|
|
char stats_end_str[ISO_TIME_LEN+1], stats_start_str[ISO_TIME_LEN+1],
|
2009-12-17 11:20:31 +01:00
|
|
|
*controller_str, *eos, *eol, *summary;
|
2009-12-18 00:29:37 +01:00
|
|
|
|
|
|
|
const char *BRIDGE_STATS_END = "bridge-stats-end ";
|
|
|
|
const char *BRIDGE_IPS = "bridge-ips ";
|
2009-12-18 13:29:51 +01:00
|
|
|
const char *BRIDGE_IPS_EMPTY_LINE = "bridge-ips\n";
|
2009-12-18 00:29:37 +01:00
|
|
|
const char *tmp;
|
2009-12-12 08:32:46 +01:00
|
|
|
time_t stats_end_time;
|
|
|
|
int seconds;
|
|
|
|
tor_assert(stats_str);
|
|
|
|
|
|
|
|
/* Parse timestamp and number of seconds from
|
|
|
|
"bridge-stats-end YYYY-MM-DD HH:MM:SS (N s)" */
|
2009-12-18 00:29:37 +01:00
|
|
|
tmp = find_str_at_start_of_line(stats_str, BRIDGE_STATS_END);
|
|
|
|
if (!tmp)
|
|
|
|
return NULL;
|
|
|
|
tmp += strlen(BRIDGE_STATS_END);
|
|
|
|
|
2009-12-17 11:20:31 +01:00
|
|
|
if (strlen(tmp) < ISO_TIME_LEN + 6)
|
2009-12-12 08:32:46 +01:00
|
|
|
return NULL;
|
2009-12-17 11:20:31 +01:00
|
|
|
strlcpy(stats_end_str, tmp, sizeof(stats_end_str));
|
2009-12-12 08:32:46 +01:00
|
|
|
if (parse_iso_time(stats_end_str, &stats_end_time) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (stats_end_time < now - (25*60*60) ||
|
|
|
|
stats_end_time > now + (1*60*60))
|
|
|
|
return NULL;
|
2009-12-17 11:20:31 +01:00
|
|
|
seconds = (int)strtol(tmp + ISO_TIME_LEN + 2, &eos, 10);
|
2009-12-12 08:32:46 +01:00
|
|
|
if (!eos || seconds < 23*60*60)
|
|
|
|
return NULL;
|
|
|
|
format_iso_time(stats_start_str, stats_end_time - seconds);
|
|
|
|
|
|
|
|
/* Parse: "bridge-ips CC=N,CC=N,..." */
|
2009-12-18 00:29:37 +01:00
|
|
|
tmp = find_str_at_start_of_line(stats_str, BRIDGE_IPS);
|
2009-12-18 13:29:51 +01:00
|
|
|
if (tmp) {
|
|
|
|
tmp += strlen(BRIDGE_IPS);
|
|
|
|
tmp = eat_whitespace_no_nl(tmp);
|
|
|
|
eol = strchr(tmp, '\n');
|
|
|
|
if (eol)
|
|
|
|
summary = tor_strndup(tmp, eol-tmp);
|
|
|
|
else
|
|
|
|
summary = tor_strdup(tmp);
|
|
|
|
} else {
|
|
|
|
/* Look if there is an empty "bridge-ips" line */
|
|
|
|
tmp = find_str_at_start_of_line(stats_str, BRIDGE_IPS_EMPTY_LINE);
|
|
|
|
if (!tmp)
|
|
|
|
return NULL;
|
|
|
|
summary = tor_strdup("");
|
|
|
|
}
|
2009-12-18 00:29:37 +01:00
|
|
|
|
2010-03-01 03:46:50 +01:00
|
|
|
tor_asprintf(&controller_str,
|
|
|
|
"TimeStarted=\"%s\" CountrySummary=%s",
|
|
|
|
stats_start_str, summary);
|
2009-12-17 11:20:31 +01:00
|
|
|
tor_free(summary);
|
2009-12-12 08:32:46 +01:00
|
|
|
return controller_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Most recent bridge statistics formatted to be written to extra-info
|
|
|
|
* descriptors. */
|
|
|
|
static char *bridge_stats_extrainfo = NULL;
|
|
|
|
|
|
|
|
/** Most recent bridge statistics formatted to be returned to controller
|
|
|
|
* clients. */
|
|
|
|
static char *bridge_stats_controller = NULL;
|
|
|
|
|
|
|
|
/** Write bridge statistics to $DATADIR/stats/bridge-stats and return
|
|
|
|
* when we should next try to write statistics. */
|
|
|
|
time_t
|
|
|
|
geoip_bridge_stats_write(time_t now)
|
|
|
|
{
|
|
|
|
char *statsdir = NULL, *filename = NULL, *data = NULL,
|
|
|
|
written[ISO_TIME_LEN+1], *out = NULL, *controller_str;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* If we changed from relay to bridge recently, adapt starting time
|
|
|
|
* of current measurements. */
|
|
|
|
if (start_of_bridge_stats_interval < client_history_starts)
|
|
|
|
start_of_bridge_stats_interval = client_history_starts;
|
|
|
|
|
|
|
|
/* Check if 24 hours have passed since starting measurements. */
|
|
|
|
if (now < start_of_bridge_stats_interval +
|
|
|
|
DIR_ENTRY_RECORD_USAGE_RETAIN_IPS)
|
|
|
|
return start_of_bridge_stats_interval +
|
|
|
|
DIR_ENTRY_RECORD_USAGE_RETAIN_IPS;
|
|
|
|
|
|
|
|
/* Discard all items in the client history that are too old. */
|
|
|
|
geoip_remove_old_clients(start_of_bridge_stats_interval);
|
|
|
|
|
|
|
|
statsdir = get_datadir_fname("stats");
|
|
|
|
if (check_private_dir(statsdir, CPD_CREATE) < 0)
|
|
|
|
goto done;
|
2009-12-17 11:20:31 +01:00
|
|
|
filename = get_datadir_fname2("stats", "bridge-stats");
|
2009-12-12 08:32:46 +01:00
|
|
|
data = geoip_get_client_history_bridge(now, GEOIP_CLIENT_CONNECT);
|
|
|
|
format_iso_time(written, now);
|
|
|
|
len = strlen("bridge-stats-end (999999 s)\nbridge-ips \n") +
|
2009-12-17 11:20:31 +01:00
|
|
|
ISO_TIME_LEN + (data ? strlen(data) : 0) + 42;
|
2009-12-12 08:32:46 +01:00
|
|
|
out = tor_malloc(len);
|
|
|
|
if (tor_snprintf(out, len, "bridge-stats-end %s (%u s)\nbridge-ips %s\n",
|
|
|
|
written, (unsigned) (now - start_of_bridge_stats_interval),
|
|
|
|
data ? data : "") < 0)
|
|
|
|
goto done;
|
|
|
|
write_str_to_file(filename, out, 0);
|
|
|
|
controller_str = parse_bridge_stats_controller(out, now);
|
|
|
|
if (!controller_str)
|
|
|
|
goto done;
|
|
|
|
start_of_bridge_stats_interval = now;
|
|
|
|
tor_free(bridge_stats_extrainfo);
|
|
|
|
tor_free(bridge_stats_controller);
|
|
|
|
bridge_stats_extrainfo = out;
|
|
|
|
out = NULL;
|
|
|
|
bridge_stats_controller = controller_str;
|
|
|
|
control_event_clients_seen(controller_str);
|
|
|
|
done:
|
|
|
|
tor_free(filename);
|
|
|
|
tor_free(statsdir);
|
|
|
|
tor_free(data);
|
|
|
|
tor_free(out);
|
|
|
|
return start_of_bridge_stats_interval +
|
|
|
|
DIR_ENTRY_RECORD_USAGE_RETAIN_IPS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Try to load the most recent bridge statistics from disk, unless we
|
|
|
|
* have finished a measurement interval lately. */
|
|
|
|
static void
|
|
|
|
load_bridge_stats(time_t now)
|
|
|
|
{
|
2010-01-02 15:28:19 +01:00
|
|
|
char *statsdir, *fname=NULL, *contents, *controller_str;
|
2009-12-12 08:32:46 +01:00
|
|
|
if (bridge_stats_extrainfo)
|
|
|
|
return;
|
2010-01-02 15:28:19 +01:00
|
|
|
statsdir = get_datadir_fname("stats");
|
|
|
|
if (check_private_dir(statsdir, CPD_CREATE) < 0)
|
|
|
|
goto done;
|
2009-12-17 11:20:31 +01:00
|
|
|
fname = get_datadir_fname2("stats", "bridge-stats");
|
2010-01-12 18:18:08 +01:00
|
|
|
contents = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
|
2009-12-12 08:32:46 +01:00
|
|
|
if (contents) {
|
|
|
|
controller_str = parse_bridge_stats_controller(contents, now);
|
|
|
|
if (controller_str) {
|
|
|
|
bridge_stats_extrainfo = contents;
|
|
|
|
bridge_stats_controller = controller_str;
|
|
|
|
} else {
|
|
|
|
tor_free(contents);
|
|
|
|
}
|
|
|
|
}
|
2010-01-02 15:28:19 +01:00
|
|
|
done:
|
2009-12-12 08:32:46 +01:00
|
|
|
tor_free(fname);
|
2010-01-02 15:28:19 +01:00
|
|
|
tor_free(statsdir);
|
2009-12-12 08:32:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return most recent bridge statistics for inclusion in extra-info
|
|
|
|
* descriptors, or NULL if we don't have recent bridge statistics. */
|
2010-01-25 19:44:17 +01:00
|
|
|
const char *
|
2009-12-12 08:32:46 +01:00
|
|
|
geoip_get_bridge_stats_extrainfo(time_t now)
|
|
|
|
{
|
|
|
|
load_bridge_stats(now);
|
|
|
|
return bridge_stats_extrainfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return most recent bridge statistics to be returned to controller
|
|
|
|
* clients, or NULL if we don't have recent bridge statistics. */
|
2010-01-25 19:44:17 +01:00
|
|
|
const char *
|
2009-12-12 08:32:46 +01:00
|
|
|
geoip_get_bridge_stats_controller(time_t now)
|
|
|
|
{
|
|
|
|
load_bridge_stats(now);
|
|
|
|
return bridge_stats_controller;
|
|
|
|
}
|
|
|
|
|
2009-08-19 15:41:12 +02:00
|
|
|
/** Start time of entry stats. */
|
|
|
|
static time_t start_of_entry_stats_interval;
|
|
|
|
|
|
|
|
/** Initialize entry stats. */
|
|
|
|
void
|
|
|
|
geoip_entry_stats_init(time_t now)
|
|
|
|
{
|
|
|
|
start_of_entry_stats_interval = now;
|
|
|
|
}
|
|
|
|
|
2009-08-19 17:10:40 +02:00
|
|
|
/** Write entry statistics to $DATADIR/stats/entry-stats. */
|
2009-08-19 15:41:12 +02:00
|
|
|
void
|
|
|
|
geoip_entry_stats_write(time_t now)
|
2009-07-05 20:48:16 +02:00
|
|
|
{
|
2009-08-19 17:10:40 +02:00
|
|
|
char *statsdir = NULL, *filename = NULL;
|
2009-07-05 20:48:16 +02:00
|
|
|
char *data = NULL;
|
2009-08-14 15:30:24 +02:00
|
|
|
char written[ISO_TIME_LEN+1];
|
2009-07-05 20:48:16 +02:00
|
|
|
open_file_t *open_file = NULL;
|
|
|
|
FILE *out;
|
|
|
|
|
2009-08-19 15:41:12 +02:00
|
|
|
if (!get_options()->EntryStatistics)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Discard all items in the client history that are too old. */
|
|
|
|
geoip_remove_old_clients(start_of_entry_stats_interval);
|
|
|
|
|
2009-08-19 17:10:40 +02:00
|
|
|
statsdir = get_datadir_fname("stats");
|
|
|
|
if (check_private_dir(statsdir, CPD_CREATE) < 0)
|
|
|
|
goto done;
|
2009-12-17 11:20:31 +01:00
|
|
|
filename = get_datadir_fname2("stats", "entry-stats");
|
2009-08-19 15:41:12 +02:00
|
|
|
data = geoip_get_client_history_dirreq(now, GEOIP_CLIENT_CONNECT);
|
2009-07-05 20:48:16 +02:00
|
|
|
format_iso_time(written, now);
|
|
|
|
out = start_writing_to_stdio_file(filename, OPEN_FLAGS_APPEND,
|
|
|
|
0600, &open_file);
|
|
|
|
if (!out)
|
|
|
|
goto done;
|
2009-08-19 15:41:12 +02:00
|
|
|
if (fprintf(out, "entry-stats-end %s (%u s)\nentry-ips %s\n",
|
|
|
|
written, (unsigned) (now - start_of_entry_stats_interval),
|
|
|
|
data ? data : "") < 0)
|
2009-07-05 20:48:16 +02:00
|
|
|
goto done;
|
|
|
|
|
2009-08-19 15:41:12 +02:00
|
|
|
start_of_entry_stats_interval = now;
|
|
|
|
|
2009-07-05 20:48:16 +02:00
|
|
|
finish_writing_to_file(open_file);
|
|
|
|
open_file = NULL;
|
|
|
|
done:
|
|
|
|
if (open_file)
|
|
|
|
abort_writing_to_file(open_file);
|
|
|
|
tor_free(filename);
|
2009-08-19 17:10:40 +02:00
|
|
|
tor_free(statsdir);
|
2009-07-05 20:48:16 +02:00
|
|
|
tor_free(data);
|
|
|
|
}
|
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Helper used to implement GETINFO ip-to-country/... controller command. */
|
2007-12-17 23:44:18 +01:00
|
|
|
int
|
|
|
|
getinfo_helper_geoip(control_connection_t *control_conn,
|
|
|
|
const char *question, char **answer)
|
|
|
|
{
|
|
|
|
(void)control_conn;
|
|
|
|
if (geoip_is_loaded() && !strcmpstart(question, "ip-to-country/")) {
|
|
|
|
int c;
|
|
|
|
uint32_t ip;
|
|
|
|
struct in_addr in;
|
|
|
|
question += strlen("ip-to-country/");
|
|
|
|
if (tor_inet_aton(question, &in) != 0) {
|
|
|
|
ip = ntohl(in.s_addr);
|
|
|
|
c = geoip_get_country_by_ip(ip);
|
|
|
|
*answer = tor_strdup(geoip_get_country_name(c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-17 23:44:11 +01:00
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Release all storage held by the GeoIP database. */
|
|
|
|
static void
|
|
|
|
clear_geoip_db(void)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
|
|
|
if (geoip_countries) {
|
2008-06-10 20:08:56 +02:00
|
|
|
SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, tor_free(c));
|
2007-12-17 23:44:11 +01:00
|
|
|
smartlist_free(geoip_countries);
|
|
|
|
}
|
2009-12-12 08:07:59 +01:00
|
|
|
|
|
|
|
strmap_free(country_idxplus1_by_lc_code, NULL);
|
2007-12-17 23:44:11 +01:00
|
|
|
if (geoip_entries) {
|
|
|
|
SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, ent, tor_free(ent));
|
|
|
|
smartlist_free(geoip_entries);
|
|
|
|
}
|
|
|
|
geoip_countries = NULL;
|
|
|
|
country_idxplus1_by_lc_code = NULL;
|
|
|
|
geoip_entries = NULL;
|
|
|
|
}
|
2007-12-17 23:44:18 +01:00
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Release all storage held in this file. */
|
|
|
|
void
|
|
|
|
geoip_free_all(void)
|
|
|
|
{
|
2009-07-15 18:24:02 +02:00
|
|
|
{
|
|
|
|
clientmap_entry_t **ent, **next, *this;
|
|
|
|
for (ent = HT_START(clientmap, &client_history); ent != NULL; ent = next) {
|
|
|
|
this = *ent;
|
|
|
|
next = HT_NEXT_RMV(clientmap, &client_history, ent);
|
|
|
|
tor_free(this);
|
|
|
|
}
|
|
|
|
HT_CLEAR(clientmap, &client_history);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
dirreq_map_entry_t **ent, **next, *this;
|
|
|
|
for (ent = HT_START(dirreqmap, &dirreq_map); ent != NULL; ent = next) {
|
|
|
|
this = *ent;
|
|
|
|
next = HT_NEXT_RMV(dirreqmap, &dirreq_map, ent);
|
|
|
|
tor_free(this);
|
|
|
|
}
|
|
|
|
HT_CLEAR(dirreqmap, &dirreq_map);
|
2007-12-18 22:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
clear_geoip_db();
|
|
|
|
}
|
|
|
|
|