2016-02-27 18:48:19 +01:00
|
|
|
/* Copyright (c) 2007-2016, 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
|
2010-08-13 23:18:45 +02:00
|
|
|
* \brief Functions related to maintaining an IP-to-country database;
|
|
|
|
* to summarizing client connections by country to entry guards, bridges,
|
|
|
|
* and directory servers; and for statistics on answering network status
|
|
|
|
* requests.
|
2016-10-15 02:08:51 +02:00
|
|
|
*
|
|
|
|
* There are two main kinds of functions in this module: geoip functions,
|
|
|
|
* which map groups of IPv4 and IPv6 addresses to country codes, and
|
|
|
|
* statistical functions, which collect statistics about different kinds of
|
|
|
|
* per-country usage.
|
|
|
|
*
|
|
|
|
* The geoip lookup tables are implemented as sorted lists of disjoint address
|
|
|
|
* ranges, each mapping to a singleton geoip_country_t. These country objects
|
|
|
|
* are also indexed by their names in a hashtable.
|
|
|
|
*
|
|
|
|
* The tables are populated from disk at startup by the geoip_load_file()
|
|
|
|
* function. For more information on the file format they read, see that
|
|
|
|
* function. See the scripts and the README file in src/config for more
|
|
|
|
* information about how those files are generated.
|
|
|
|
*
|
|
|
|
* Tor uses GeoIP information in order to implement user requests (such as
|
|
|
|
* ExcludeNodes {cc}), and to keep track of how much usage relays are getting
|
|
|
|
* for each country.
|
2007-12-18 22:27:08 +01:00
|
|
|
*/
|
|
|
|
|
2007-12-17 23:44:11 +01:00
|
|
|
#define GEOIP_PRIVATE
|
|
|
|
#include "or.h"
|
|
|
|
#include "ht.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "control.h"
|
2010-07-21 12:26:18 +02:00
|
|
|
#include "dnsserv.h"
|
2018-01-19 19:15:07 +01:00
|
|
|
#include "dos.h"
|
2010-07-21 14:38:52 +02:00
|
|
|
#include "geoip.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2007-12-17 23:44:11 +01:00
|
|
|
|
2010-07-31 21:14:48 +02:00
|
|
|
static void init_geoip_countries(void);
|
2007-12-17 23:44:11 +01:00
|
|
|
|
2012-10-31 16:38:07 +01:00
|
|
|
/** An entry from the GeoIP IPv4 file: maps an IPv4 range to a country. */
|
2012-03-01 01:42:59 +01:00
|
|
|
typedef struct geoip_ipv4_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 */
|
2012-03-01 01:42:59 +01:00
|
|
|
} geoip_ipv4_entry_t;
|
2007-12-17 23:44:11 +01:00
|
|
|
|
2012-03-01 02:04:45 +01:00
|
|
|
/** An entry from the GeoIP IPv6 file: maps an IPv6 range to a country. */
|
|
|
|
typedef struct geoip_ipv6_entry_t {
|
|
|
|
struct in6_addr ip_low; /**< The lowest IP in the range, in host order */
|
|
|
|
struct in6_addr ip_high; /**< The highest IP in the range, in host order */
|
|
|
|
intptr_t country; /**< An index into geoip_countries */
|
|
|
|
} geoip_ipv6_entry_t;
|
2007-12-17 23:44:11 +01: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];
|
2010-06-21 18:52:46 +02:00
|
|
|
uint32_t n_v3_ns_requests;
|
2008-06-10 20:08:56 +02:00
|
|
|
} 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;
|
2012-03-28 15:52:33 +02:00
|
|
|
/** Lists of all known geoip_ipv4_entry_t and geoip_ipv6_entry_t, sorted
|
|
|
|
* by their respective ip_low. */
|
2012-03-01 02:04:45 +01:00
|
|
|
static smartlist_t *geoip_ipv4_entries = NULL, *geoip_ipv6_entries = NULL;
|
2007-12-17 23:44:11 +01:00
|
|
|
|
2012-10-20 18:48:58 +02:00
|
|
|
/** SHA1 digest of the GeoIP files to include in extra-info descriptors. */
|
2010-11-09 09:13:45 +01:00
|
|
|
static char geoip_digest[DIGEST_LEN];
|
2012-10-20 18:48:58 +02:00
|
|
|
static char geoip6_digest[DIGEST_LEN];
|
2010-11-09 09:13:45 +01:00
|
|
|
|
2018-02-02 16:15:28 +01:00
|
|
|
/* Total size in bytes of the geoip client history cache. Used by the OOM
|
|
|
|
* handler. */
|
|
|
|
static size_t geoip_client_history_cache_size;
|
|
|
|
|
2018-02-02 19:14:50 +01:00
|
|
|
/* Increment the geoip client history cache size counter with the given bytes.
|
|
|
|
* This prevents an overflow and set it to its maximum in that case. */
|
|
|
|
static inline void
|
|
|
|
geoip_increment_client_history_cache_size(size_t bytes)
|
|
|
|
{
|
|
|
|
/* This is shockingly high, lets log it so it can be reported. */
|
|
|
|
IF_BUG_ONCE(geoip_client_history_cache_size > (SIZE_MAX - bytes)) {
|
|
|
|
geoip_client_history_cache_size = SIZE_MAX;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
geoip_client_history_cache_size += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the geoip client history cache size counter with the given bytes.
|
|
|
|
* This prevents an underflow and set it to 0 in that case. */
|
|
|
|
static inline void
|
|
|
|
geoip_decrement_client_history_cache_size(size_t bytes)
|
|
|
|
{
|
|
|
|
/* Going below 0 means that we either allocated an entry without
|
|
|
|
* incrementing the counter or we have different sizes when allocating and
|
|
|
|
* freeing. It shouldn't happened so log it. */
|
|
|
|
IF_BUG_ONCE(geoip_client_history_cache_size < bytes) {
|
|
|
|
geoip_client_history_cache_size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
geoip_client_history_cache_size -= bytes;
|
|
|
|
}
|
|
|
|
|
2012-10-31 16:38:07 +01:00
|
|
|
/** Return the index of the <b>country</b>'s entry in the GeoIP
|
|
|
|
* country list if it is a valid 2-letter country code, otherwise
|
|
|
|
* return -1. */
|
2014-08-27 12:41:25 +02:00
|
|
|
MOCK_IMPL(country_t,
|
|
|
|
geoip_get_country,(const char *country))
|
2008-09-25 22:21:35 +02:00
|
|
|
{
|
2012-10-12 18:22:13 +02:00
|
|
|
void *idxplus1_;
|
2008-09-25 22:21:35 +02:00
|
|
|
intptr_t idx;
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
idxplus1_ = strmap_get_lc(country_idxplus1_by_lc_code, country);
|
|
|
|
if (!idxplus1_)
|
2008-09-25 22:21:35 +02:00
|
|
|
return -1;
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
idx = ((uintptr_t)idxplus1_)-1;
|
2008-09-25 22:21:35 +02:00
|
|
|
return (country_t)idx;
|
|
|
|
}
|
|
|
|
|
2012-11-05 03:52:28 +01:00
|
|
|
/** Add an entry to a GeoIP table, mapping all IP addresses between <b>low</b>
|
|
|
|
* and <b>high</b>, inclusive, to the 2-letter country code <b>country</b>. */
|
2007-12-18 22:27:08 +01:00
|
|
|
static void
|
2012-10-31 13:58:55 +01:00
|
|
|
geoip_add_entry(const tor_addr_t *low, const tor_addr_t *high,
|
|
|
|
const char *country)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2008-02-22 20:09:45 +01:00
|
|
|
intptr_t idx;
|
2012-10-12 18:22:13 +02:00
|
|
|
void *idxplus1_;
|
2007-12-18 22:27:08 +01:00
|
|
|
|
2016-05-30 17:12:58 +02:00
|
|
|
IF_BUG_ONCE(tor_addr_family(low) != tor_addr_family(high))
|
2012-10-31 13:58:55 +01:00
|
|
|
return;
|
2016-05-30 17:12:58 +02:00
|
|
|
IF_BUG_ONCE(tor_addr_compare(high, low, CMP_EXACT) < 0)
|
2007-12-18 22:27:08 +01:00
|
|
|
return;
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
idxplus1_ = strmap_get_lc(country_idxplus1_by_lc_code, country);
|
2007-12-17 23:44:11 +01:00
|
|
|
|
2012-10-12 18:22:13 +02: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 {
|
2012-10-12 18:22:13 +02:00
|
|
|
idx = ((uintptr_t)idxplus1_)-1;
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
2008-06-10 20:08:56 +02:00
|
|
|
{
|
|
|
|
geoip_country_t *c = smartlist_get(geoip_countries, idx);
|
|
|
|
tor_assert(!strcasecmp(c->countrycode, country));
|
|
|
|
}
|
2012-10-31 13:58:55 +01:00
|
|
|
|
|
|
|
if (tor_addr_family(low) == AF_INET) {
|
|
|
|
geoip_ipv4_entry_t *ent = tor_malloc_zero(sizeof(geoip_ipv4_entry_t));
|
|
|
|
ent->ip_low = tor_addr_to_ipv4h(low);
|
|
|
|
ent->ip_high = tor_addr_to_ipv4h(high);
|
|
|
|
ent->country = idx;
|
|
|
|
smartlist_add(geoip_ipv4_entries, ent);
|
|
|
|
} else if (tor_addr_family(low) == AF_INET6) {
|
|
|
|
geoip_ipv6_entry_t *ent = tor_malloc_zero(sizeof(geoip_ipv6_entry_t));
|
2016-05-30 17:12:58 +02:00
|
|
|
ent->ip_low = *tor_addr_to_in6_assert(low);
|
|
|
|
ent->ip_high = *tor_addr_to_in6_assert(high);
|
2012-10-31 13:58:55 +01:00
|
|
|
ent->country = idx;
|
|
|
|
smartlist_add(geoip_ipv6_entries, ent);
|
|
|
|
}
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2012-10-31 13:58:55 +01:00
|
|
|
/** Add an entry to the GeoIP table indicated by <b>family</b>,
|
|
|
|
* parsing it from <b>line</b>. The format is as for geoip_load_file(). */
|
2013-06-06 23:58:28 +02:00
|
|
|
STATIC int
|
2012-10-31 13:58:55 +01:00
|
|
|
geoip_parse_entry(const char *line, sa_family_t family)
|
2007-12-18 22:27:08 +01:00
|
|
|
{
|
2012-10-31 13:58:55 +01:00
|
|
|
tor_addr_t low_addr, high_addr;
|
|
|
|
char c[3];
|
|
|
|
char *country = NULL;
|
|
|
|
|
2010-07-31 21:14:48 +02:00
|
|
|
if (!geoip_countries)
|
|
|
|
init_geoip_countries();
|
2012-10-31 13:58:55 +01:00
|
|
|
if (family == AF_INET) {
|
|
|
|
if (!geoip_ipv4_entries)
|
|
|
|
geoip_ipv4_entries = smartlist_new();
|
|
|
|
} else if (family == AF_INET6) {
|
|
|
|
if (!geoip_ipv6_entries)
|
|
|
|
geoip_ipv6_entries = smartlist_new();
|
|
|
|
} else {
|
|
|
|
log_warn(LD_GENERAL, "Unsupported family: %d", family);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-31 21:14:48 +02:00
|
|
|
|
2008-05-28 20:31:57 +02:00
|
|
|
while (TOR_ISSPACE(*line))
|
|
|
|
++line;
|
|
|
|
if (*line == '#')
|
|
|
|
return 0;
|
2012-10-31 13:58:55 +01:00
|
|
|
|
2017-06-05 16:09:39 +02:00
|
|
|
char buf[512];
|
2012-10-31 13:58:55 +01:00
|
|
|
if (family == AF_INET) {
|
|
|
|
unsigned int low, high;
|
|
|
|
if (tor_sscanf(line,"%u,%u,%2s", &low, &high, c) == 3 ||
|
|
|
|
tor_sscanf(line,"\"%u\",\"%u\",\"%2s\",", &low, &high, c) == 3) {
|
|
|
|
tor_addr_from_ipv4h(&low_addr, low);
|
|
|
|
tor_addr_from_ipv4h(&high_addr, high);
|
|
|
|
} else
|
|
|
|
goto fail;
|
|
|
|
country = c;
|
|
|
|
} else { /* AF_INET6 */
|
|
|
|
char *low_str, *high_str;
|
|
|
|
struct in6_addr low, high;
|
|
|
|
char *strtok_state;
|
|
|
|
strlcpy(buf, line, sizeof(buf));
|
|
|
|
low_str = tor_strtok_r(buf, ",", &strtok_state);
|
|
|
|
if (!low_str)
|
|
|
|
goto fail;
|
|
|
|
high_str = tor_strtok_r(NULL, ",", &strtok_state);
|
|
|
|
if (!high_str)
|
|
|
|
goto fail;
|
|
|
|
country = tor_strtok_r(NULL, "\n", &strtok_state);
|
|
|
|
if (!country)
|
|
|
|
goto fail;
|
|
|
|
if (strlen(country) != 2)
|
|
|
|
goto fail;
|
|
|
|
if (tor_inet_pton(AF_INET6, low_str, &low) <= 0)
|
|
|
|
goto fail;
|
|
|
|
tor_addr_from_in6(&low_addr, &low);
|
|
|
|
if (tor_inet_pton(AF_INET6, high_str, &high) <= 0)
|
|
|
|
goto fail;
|
|
|
|
tor_addr_from_in6(&high_addr, &high);
|
2007-12-18 22:27:08 +01:00
|
|
|
}
|
2012-10-31 13:58:55 +01:00
|
|
|
geoip_add_entry(&low_addr, &high_addr, country);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
log_warn(LD_GENERAL, "Unable to parse line from GEOIP %s file: %s",
|
|
|
|
family == AF_INET ? "IPv4" : "IPv6", escaped(line));
|
|
|
|
return -1;
|
2007-12-18 22:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sorting helper: return -1, 1, or 0 based on comparison of two
|
2012-03-01 01:42:59 +01:00
|
|
|
* geoip_ipv4_entry_t */
|
2007-12-17 23:44:11 +01:00
|
|
|
static int
|
2012-10-17 10:38:03 +02:00
|
|
|
geoip_ipv4_compare_entries_(const void **_a, const void **_b)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2012-03-01 01:42:59 +01:00
|
|
|
const geoip_ipv4_entry_t *a = *_a, *b = *_b;
|
2007-12-17 23:44:11 +01:00
|
|
|
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
|
2012-03-01 01:42:59 +01:00
|
|
|
* to a uint32_t in host order) to a geoip_ipv4_entry_t */
|
2007-12-17 23:44:11 +01:00
|
|
|
static int
|
2012-10-17 10:38:03 +02:00
|
|
|
geoip_ipv4_compare_key_to_entry_(const void *_key, const void **_member)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2010-09-21 00:40:32 +02:00
|
|
|
/* No alignment issue here, since _key really is a pointer to uint32_t */
|
2007-12-17 23:44:11 +01:00
|
|
|
const uint32_t addr = *(uint32_t *)_key;
|
2012-03-01 01:42:59 +01:00
|
|
|
const geoip_ipv4_entry_t *entry = *_member;
|
2007-12-17 23:44:11 +01:00
|
|
|
if (addr < entry->ip_low)
|
|
|
|
return -1;
|
|
|
|
else if (addr > entry->ip_high)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-01 02:04:45 +01:00
|
|
|
/** Sorting helper: return -1, 1, or 0 based on comparison of two
|
|
|
|
* geoip_ipv6_entry_t */
|
|
|
|
static int
|
2012-10-17 10:38:03 +02:00
|
|
|
geoip_ipv6_compare_entries_(const void **_a, const void **_b)
|
2012-03-01 02:04:45 +01:00
|
|
|
{
|
|
|
|
const geoip_ipv6_entry_t *a = *_a, *b = *_b;
|
2012-12-13 23:34:05 +01:00
|
|
|
return fast_memcmp(a->ip_low.s6_addr, b->ip_low.s6_addr,
|
|
|
|
sizeof(struct in6_addr));
|
2012-03-01 02:04:45 +01:00
|
|
|
}
|
|
|
|
|
2012-10-17 14:01:02 +02:00
|
|
|
/** bsearch helper: return -1, 1, or 0 based on comparison of an IPv6
|
2012-10-31 15:52:56 +01:00
|
|
|
* (a pointer to a in6_addr) to a geoip_ipv6_entry_t */
|
2012-03-01 02:04:45 +01:00
|
|
|
static int
|
2012-10-17 10:38:03 +02:00
|
|
|
geoip_ipv6_compare_key_to_entry_(const void *_key, const void **_member)
|
2012-03-01 02:04:45 +01:00
|
|
|
{
|
|
|
|
const struct in6_addr *addr = (struct in6_addr *)_key;
|
|
|
|
const geoip_ipv6_entry_t *entry = *_member;
|
|
|
|
|
2012-12-13 23:34:05 +01:00
|
|
|
if (fast_memcmp(addr->s6_addr, entry->ip_low.s6_addr,
|
2012-10-31 15:52:56 +01:00
|
|
|
sizeof(struct in6_addr)) < 0)
|
2012-03-01 02:04:45 +01:00
|
|
|
return -1;
|
2012-12-13 23:34:05 +01:00
|
|
|
else if (fast_memcmp(addr->s6_addr, entry->ip_high.s6_addr,
|
2012-10-31 15:52:56 +01:00
|
|
|
sizeof(struct in6_addr)) > 0)
|
2012-03-01 02:04:45 +01:00
|
|
|
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
|
2011-06-14 19:01:38 +02:00
|
|
|
should_record_bridge_info(const or_options_t *options)
|
2008-06-04 11:02:25 +02:00
|
|
|
{
|
|
|
|
return options->BridgeRelay && options->BridgeRecordUsageByCountry;
|
|
|
|
}
|
|
|
|
|
2010-07-31 21:14:48 +02:00
|
|
|
/** Set up a new list of geoip countries with no countries (yet) set in it,
|
|
|
|
* except for the unknown country.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_geoip_countries(void)
|
|
|
|
{
|
|
|
|
geoip_country_t *geoip_unresolved;
|
2012-01-18 21:53:30 +01:00
|
|
|
geoip_countries = smartlist_new();
|
2010-07-31 21:14:48 +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);
|
|
|
|
country_idxplus1_by_lc_code = strmap_new();
|
|
|
|
strmap_set_lc(country_idxplus1_by_lc_code, "??", (void*)(1));
|
|
|
|
}
|
|
|
|
|
2012-10-31 13:58:55 +01:00
|
|
|
/** Clear appropriate GeoIP database, based on <b>family</b>, and
|
|
|
|
* reload it from the file <b>filename</b>. Return 0 on success, -1 on
|
|
|
|
* failure.
|
2007-12-18 22:27:08 +01:00
|
|
|
*
|
2012-10-31 13:58:55 +01:00
|
|
|
* Recognized line formats for IPv4 are:
|
2007-12-18 22:27:08 +01:00
|
|
|
* 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
|
|
|
*
|
2012-10-31 13:58:55 +01:00
|
|
|
* Recognized line format for IPv6 is:
|
|
|
|
* IPV6LOW,IPV6HIGH,CC
|
|
|
|
* where IPV6LOW and IPV6HIGH are IPv6 addresses and CC is a country code.
|
|
|
|
*
|
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
|
2012-10-20 19:35:00 +02:00
|
|
|
geoip_load_file(sa_family_t family, const char *filename)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
|
|
|
FILE *f;
|
2008-09-25 23:06:32 +02:00
|
|
|
const char *msg = "";
|
2012-10-20 19:35:00 +02:00
|
|
|
const or_options_t *options = get_options();
|
2008-09-25 23:06:32 +02:00
|
|
|
int severity = options_need_geoip_info(options, &msg) ? LOG_WARN : LOG_INFO;
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t *geoip_digest_env = NULL;
|
2012-03-01 02:04:45 +01:00
|
|
|
|
|
|
|
tor_assert(family == AF_INET || family == AF_INET6);
|
|
|
|
|
2010-11-20 07:16:29 +01:00
|
|
|
if (!(f = tor_fopen_cloexec(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;
|
|
|
|
}
|
2010-07-31 21:14:48 +02:00
|
|
|
if (!geoip_countries)
|
|
|
|
init_geoip_countries();
|
2012-03-01 02:04:45 +01:00
|
|
|
|
|
|
|
if (family == AF_INET) {
|
|
|
|
if (geoip_ipv4_entries) {
|
2012-10-17 14:01:02 +02:00
|
|
|
SMARTLIST_FOREACH(geoip_ipv4_entries, geoip_ipv4_entry_t *, e,
|
|
|
|
tor_free(e));
|
2012-03-01 02:04:45 +01:00
|
|
|
smartlist_free(geoip_ipv4_entries);
|
|
|
|
}
|
|
|
|
geoip_ipv4_entries = smartlist_new();
|
|
|
|
} else { /* AF_INET6 */
|
|
|
|
if (geoip_ipv6_entries) {
|
2012-10-17 14:01:02 +02:00
|
|
|
SMARTLIST_FOREACH(geoip_ipv6_entries, geoip_ipv6_entry_t *, e,
|
|
|
|
tor_free(e));
|
2012-03-01 02:04:45 +01:00
|
|
|
smartlist_free(geoip_ipv6_entries);
|
|
|
|
}
|
|
|
|
geoip_ipv6_entries = smartlist_new();
|
2008-09-25 22:21:35 +02:00
|
|
|
}
|
2012-01-18 21:53:30 +01:00
|
|
|
geoip_digest_env = crypto_digest_new();
|
2012-10-17 14:01:02 +02:00
|
|
|
|
|
|
|
log_notice(LD_GENERAL, "Parsing GEOIP %s file %s.",
|
2012-10-31 13:58:55 +01:00
|
|
|
(family == AF_INET) ? "IPv4" : "IPv6", filename);
|
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;
|
2010-11-09 09:13:45 +01:00
|
|
|
crypto_digest_add_bytes(geoip_digest_env, buf, strlen(buf));
|
2008-02-05 22:39:44 +01:00
|
|
|
/* FFFF track full country name. */
|
2012-10-31 13:58:55 +01:00
|
|
|
geoip_parse_entry(buf, family);
|
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);
|
|
|
|
|
2012-10-20 18:48:58 +02:00
|
|
|
/* Sort list and remember file digests so that we can include it in
|
|
|
|
* our extra-info descriptors. */
|
|
|
|
if (family == AF_INET) {
|
2012-10-17 10:38:03 +02:00
|
|
|
smartlist_sort(geoip_ipv4_entries, geoip_ipv4_compare_entries_);
|
2012-10-20 18:48:58 +02:00
|
|
|
/* Okay, now we need to maybe change our mind about what is in
|
|
|
|
* which country. We do this for IPv4 only since that's what we
|
|
|
|
* store in node->country. */
|
|
|
|
refresh_all_country_info();
|
|
|
|
crypto_digest_get_digest(geoip_digest_env, geoip_digest, DIGEST_LEN);
|
2012-11-05 03:44:31 +01:00
|
|
|
} else {
|
|
|
|
/* AF_INET6 */
|
2012-10-17 10:38:03 +02:00
|
|
|
smartlist_sort(geoip_ipv6_entries, geoip_ipv6_compare_entries_);
|
2012-10-20 18:48:58 +02:00
|
|
|
crypto_digest_get_digest(geoip_digest_env, geoip6_digest, DIGEST_LEN);
|
|
|
|
}
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_free(geoip_digest_env);
|
2010-11-09 09:13:45 +01:00
|
|
|
|
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
|
2010-07-31 21:06:55 +02:00
|
|
|
* country to which that address belongs, -1 for "No geoip information
|
|
|
|
* available", or 0 for the 'unknown country'. The return value will always
|
|
|
|
* be less than geoip_get_n_countries(). To decode it, call
|
|
|
|
* geoip_get_country_name().
|
2007-12-18 22:27:08 +01:00
|
|
|
*/
|
2013-06-06 23:58:28 +02:00
|
|
|
STATIC int
|
2012-03-01 01:42:59 +01:00
|
|
|
geoip_get_country_by_ipv4(uint32_t ipaddr)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2012-03-01 01:42:59 +01:00
|
|
|
geoip_ipv4_entry_t *ent;
|
|
|
|
if (!geoip_ipv4_entries)
|
2007-12-17 23:44:11 +01:00
|
|
|
return -1;
|
2012-10-17 14:01:02 +02:00
|
|
|
ent = smartlist_bsearch(geoip_ipv4_entries, &ipaddr,
|
|
|
|
geoip_ipv4_compare_key_to_entry_);
|
2010-07-31 21:06:55 +02:00
|
|
|
return ent ? (int)ent->country : 0;
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2012-03-28 15:52:33 +02:00
|
|
|
/** Given an IPv6 address, return a number representing the country to
|
|
|
|
* which that address belongs, -1 for "No geoip information available", or
|
|
|
|
* 0 for the 'unknown country'. The return value will always be less than
|
|
|
|
* geoip_get_n_countries(). To decode it, call geoip_get_country_name().
|
|
|
|
*/
|
2013-06-06 23:58:28 +02:00
|
|
|
STATIC int
|
2012-03-01 02:04:45 +01:00
|
|
|
geoip_get_country_by_ipv6(const struct in6_addr *addr)
|
|
|
|
{
|
|
|
|
geoip_ipv6_entry_t *ent;
|
|
|
|
|
|
|
|
if (!geoip_ipv6_entries)
|
|
|
|
return -1;
|
2012-10-17 14:01:02 +02:00
|
|
|
ent = smartlist_bsearch(geoip_ipv6_entries, addr,
|
|
|
|
geoip_ipv6_compare_key_to_entry_);
|
2010-07-31 21:06:55 +02:00
|
|
|
return ent ? (int)ent->country : 0;
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 11:12:30 +01:00
|
|
|
/** Given an IP address, return a number representing the country to which
|
|
|
|
* that address belongs, -1 for "No geoip information available", or 0 for
|
|
|
|
* the 'unknown country'. The return value will always be less than
|
|
|
|
* geoip_get_n_countries(). To decode it, call geoip_get_country_name().
|
|
|
|
*/
|
2014-08-27 12:41:25 +02:00
|
|
|
MOCK_IMPL(int,
|
|
|
|
geoip_get_country_by_addr,(const tor_addr_t *addr))
|
2012-02-09 11:12:30 +01:00
|
|
|
{
|
2012-03-01 02:04:45 +01:00
|
|
|
if (tor_addr_family(addr) == AF_INET) {
|
|
|
|
return geoip_get_country_by_ipv4(tor_addr_to_ipv4h(addr));
|
|
|
|
} else if (tor_addr_family(addr) == AF_INET6) {
|
|
|
|
return geoip_get_country_by_ipv6(tor_addr_to_in6(addr));
|
|
|
|
} else {
|
2012-02-09 11:12:30 +01:00
|
|
|
return -1;
|
2012-02-09 11:25:48 +01:00
|
|
|
}
|
2012-02-09 11:12:30 +01:00
|
|
|
}
|
|
|
|
|
2012-10-31 16:38:07 +01:00
|
|
|
/** Return the number of countries recognized by the GeoIP country list. */
|
2014-08-27 12:41:25 +02:00
|
|
|
MOCK_IMPL(int,
|
|
|
|
geoip_get_n_countries,(void))
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2010-09-27 23:15:43 +02:00
|
|
|
if (!geoip_countries)
|
|
|
|
init_geoip_countries();
|
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.*/
|
2014-08-27 12:41:25 +02:00
|
|
|
MOCK_IMPL(int,
|
|
|
|
geoip_is_loaded,(sa_family_t family))
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2012-10-20 18:48:58 +02:00
|
|
|
tor_assert(family == AF_INET || family == AF_INET6);
|
|
|
|
if (geoip_countries == NULL)
|
|
|
|
return 0;
|
|
|
|
if (family == AF_INET)
|
|
|
|
return geoip_ipv4_entries != NULL;
|
|
|
|
else /* AF_INET6 */
|
|
|
|
return geoip_ipv6_entries != NULL;
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:06:27 +01:00
|
|
|
/** Return the hex-encoded SHA1 digest of the loaded GeoIP file. The
|
|
|
|
* result does not need to be deallocated, but will be overwritten by the
|
|
|
|
* next call of hex_str(). */
|
2010-11-09 09:13:45 +01:00
|
|
|
const char *
|
2012-10-20 18:48:58 +02:00
|
|
|
geoip_db_digest(sa_family_t family)
|
2010-11-09 09:13:45 +01:00
|
|
|
{
|
2012-10-20 18:48:58 +02:00
|
|
|
tor_assert(family == AF_INET || family == AF_INET6);
|
|
|
|
if (family == AF_INET)
|
|
|
|
return hex_str(geoip_digest, DIGEST_LEN);
|
|
|
|
else /* AF_INET6 */
|
|
|
|
return hex_str(geoip6_digest, DIGEST_LEN);
|
2010-11-09 09:13:45 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 19:06:04 +01:00
|
|
|
/** Largest allowable value for last_seen_in_minutes. (It's a 30-bit field,
|
|
|
|
* so it can hold up to (1u<<30)-1, or 0x3fffffffu.
|
|
|
|
*/
|
|
|
|
#define MAX_LAST_SEEN_IN_MINUTES 0X3FFFFFFFu
|
2008-05-29 04:29:35 +02:00
|
|
|
|
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
|
|
|
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Hashtable helper: compute a hash of a clientmap_entry_t. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline unsigned
|
2007-12-17 23:44:11 +01:00
|
|
|
clientmap_entry_hash(const clientmap_entry_t *a)
|
|
|
|
{
|
2014-02-07 23:38:16 +01:00
|
|
|
unsigned h = (unsigned) tor_addr_hash(&a->addr);
|
|
|
|
|
2013-07-16 19:59:31 +02:00
|
|
|
if (a->transport_name)
|
2014-02-07 23:38:16 +01:00
|
|
|
h += (unsigned) siphash24g(a->transport_name, strlen(a->transport_name));
|
|
|
|
|
|
|
|
return h;
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
2007-12-18 22:27:08 +01:00
|
|
|
/** Hashtable helper: compare two clientmap_entry_t values for equality. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline int
|
2007-12-17 23:44:11 +01:00
|
|
|
clientmap_entries_eq(const clientmap_entry_t *a, const clientmap_entry_t *b)
|
|
|
|
{
|
2013-07-16 19:59:31 +02:00
|
|
|
if (strcmp_opt(a->transport_name, b->transport_name))
|
2013-06-27 17:27:44 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-02-09 11:12:30 +01:00
|
|
|
return !tor_addr_compare(&a->addr, &b->addr, CMP_EXACT) &&
|
|
|
|
a->action == b->action;
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HT_PROTOTYPE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
|
2016-06-02 15:09:37 +02:00
|
|
|
clientmap_entries_eq)
|
2014-09-02 18:48:34 +02:00
|
|
|
HT_GENERATE2(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
|
|
|
|
clientmap_entries_eq, 0.6, tor_reallocarray_, tor_free_)
|
2007-12-17 23:44:11 +01:00
|
|
|
|
2018-02-02 16:15:28 +01:00
|
|
|
/** Return the size of a client map entry. */
|
|
|
|
static inline size_t
|
|
|
|
clientmap_entry_size(const clientmap_entry_t *ent)
|
|
|
|
{
|
|
|
|
tor_assert(ent);
|
|
|
|
return (sizeof(clientmap_entry_t) +
|
|
|
|
(ent->transport_name ? strlen(ent->transport_name) : 0));
|
|
|
|
}
|
|
|
|
|
2013-07-16 20:04:36 +02:00
|
|
|
/** Free all storage held by <b>ent</b>. */
|
|
|
|
static void
|
|
|
|
clientmap_entry_free(clientmap_entry_t *ent)
|
|
|
|
{
|
|
|
|
if (!ent)
|
|
|
|
return;
|
|
|
|
|
2018-01-25 15:44:21 +01:00
|
|
|
/* This entry is about to be freed so pass it to the DoS subsystem to see if
|
|
|
|
* any actions can be taken about it. */
|
|
|
|
dos_geoip_entry_about_to_free(ent);
|
2018-02-02 19:14:50 +01:00
|
|
|
geoip_decrement_client_history_cache_size(clientmap_entry_size(ent));
|
2018-01-25 15:44:21 +01:00
|
|
|
|
2013-07-16 20:04:36 +02:00
|
|
|
tor_free(ent->transport_name);
|
|
|
|
tor_free(ent);
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:24:37 +01:00
|
|
|
/* Return a newly allocated clientmap entry with the given action and address
|
|
|
|
* that are mandatory. The transport_name can be optional. This can't fail. */
|
|
|
|
static clientmap_entry_t *
|
|
|
|
clientmap_entry_new(geoip_client_action_t action, const tor_addr_t *addr,
|
|
|
|
const char *transport_name)
|
|
|
|
{
|
|
|
|
clientmap_entry_t *entry;
|
|
|
|
|
|
|
|
tor_assert(action == GEOIP_CLIENT_CONNECT ||
|
|
|
|
action == GEOIP_CLIENT_NETWORKSTATUS);
|
|
|
|
tor_assert(addr);
|
|
|
|
|
|
|
|
entry = tor_malloc_zero(sizeof(clientmap_entry_t));
|
|
|
|
entry->action = action;
|
|
|
|
tor_addr_copy(&entry->addr, addr);
|
|
|
|
if (transport_name) {
|
|
|
|
entry->transport_name = tor_strdup(transport_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocated and initialized, note down its size for the OOM handler. */
|
|
|
|
geoip_increment_client_history_cache_size(clientmap_entry_size(entry));
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
/** Clear history of connecting clients used by entry and bridge stats. */
|
|
|
|
static void
|
|
|
|
client_history_clear(void)
|
|
|
|
{
|
|
|
|
clientmap_entry_t **ent, **next, *this;
|
|
|
|
for (ent = HT_START(clientmap, &client_history); ent != NULL;
|
|
|
|
ent = next) {
|
|
|
|
if ((*ent)->action == GEOIP_CLIENT_CONNECT) {
|
|
|
|
this = *ent;
|
|
|
|
next = HT_NEXT_RMV(clientmap, &client_history, ent);
|
2013-07-16 20:04:36 +02:00
|
|
|
clientmap_entry_free(this);
|
2010-06-21 18:52:46 +02:00
|
|
|
} else {
|
|
|
|
next = HT_NEXT(clientmap, &client_history, ent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 11:12:30 +01:00
|
|
|
/** Note that we've seen a client connect from the IP <b>addr</b>
|
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,
|
2013-02-11 20:51:41 +01:00
|
|
|
const tor_addr_t *addr,
|
|
|
|
const char *transport_name,
|
|
|
|
time_t now)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2018-01-25 21:44:48 +01:00
|
|
|
clientmap_entry_t *ent;
|
2013-06-27 17:27:44 +02:00
|
|
|
|
2008-05-29 04:29:35 +02:00
|
|
|
if (action == GEOIP_CLIENT_CONNECT) {
|
2018-01-19 19:15:07 +01:00
|
|
|
/* Only remember statistics if the DoS mitigation subsystem is enabled. If
|
|
|
|
* not, only if as entry guard or as bridge. */
|
|
|
|
if (!dos_enabled()) {
|
|
|
|
if (!options->EntryStatistics &&
|
|
|
|
(!(options->BridgeRelay && options->BridgeRecordUsageByCountry))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-05-29 04:29:35 +02:00
|
|
|
} else {
|
2013-12-18 18:02:10 +01:00
|
|
|
/* Only gather directory-request statistics if configured, and
|
|
|
|
* forcibly disable them on bridge authorities. */
|
|
|
|
if (!options->DirReqStatistics || options->BridgeAuthoritativeDir)
|
2008-05-29 04:29:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-11 20:51:41 +01:00
|
|
|
log_debug(LD_GENERAL, "Seen client from '%s' with transport '%s'.",
|
|
|
|
safe_str_client(fmt_addr((addr))),
|
|
|
|
transport_name ? transport_name : "<no transport>");
|
|
|
|
|
2018-01-25 21:44:48 +01:00
|
|
|
ent = geoip_lookup_client(addr, transport_name, action);
|
2011-03-17 19:06:04 +01:00
|
|
|
if (! ent) {
|
2018-02-02 19:24:37 +01:00
|
|
|
ent = clientmap_entry_new(action, addr, transport_name);
|
2007-12-17 23:44:11 +01:00
|
|
|
HT_INSERT(clientmap, &client_history, ent);
|
|
|
|
}
|
2011-03-18 17:42:00 +01:00
|
|
|
if (now / 60 <= (int)MAX_LAST_SEEN_IN_MINUTES && now >= 0)
|
2011-03-17 19:06:04 +01:00
|
|
|
ent->last_seen_in_minutes = (unsigned)(now/60);
|
|
|
|
else
|
|
|
|
ent->last_seen_in_minutes = 0;
|
2008-06-10 20:08:56 +02:00
|
|
|
|
2013-01-17 10:45:19 +01:00
|
|
|
if (action == GEOIP_CLIENT_NETWORKSTATUS) {
|
2012-02-09 11:12:30 +01:00
|
|
|
int country_idx = geoip_get_country_by_addr(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);
|
2013-01-17 10:45:19 +01:00
|
|
|
++country->n_v3_ns_requests;
|
2008-06-10 20:08:56 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
2012-10-12 18:22:13 +02:00
|
|
|
remove_old_client_helper_(struct clientmap_entry_t *ent, void *_cutoff)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2009-05-26 21:32:56 +02:00
|
|
|
time_t cutoff = *(time_t*)_cutoff / 60;
|
|
|
|
if (ent->last_seen_in_minutes < cutoff) {
|
2013-07-16 20:04:36 +02:00
|
|
|
clientmap_entry_free(ent);
|
2007-12-17 23:44:11 +01:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
/** Forget about all clients that haven't connected since <b>cutoff</b>. */
|
2007-12-17 23:44:11 +01:00
|
|
|
void
|
|
|
|
geoip_remove_old_clients(time_t cutoff)
|
|
|
|
{
|
|
|
|
clientmap_HT_FOREACH_FN(&client_history,
|
2012-10-12 18:22:13 +02:00
|
|
|
remove_old_client_helper_,
|
2007-12-17 23:44:11 +01:00
|
|
|
&cutoff);
|
|
|
|
}
|
|
|
|
|
2018-01-25 21:44:48 +01:00
|
|
|
/* Return a client entry object matching the given address, transport name and
|
|
|
|
* geoip action from the clientmap. NULL if not found. The transport_name can
|
|
|
|
* be NULL. */
|
|
|
|
clientmap_entry_t *
|
|
|
|
geoip_lookup_client(const tor_addr_t *addr, const char *transport_name,
|
|
|
|
geoip_client_action_t action)
|
|
|
|
{
|
|
|
|
clientmap_entry_t lookup;
|
|
|
|
|
|
|
|
tor_assert(addr);
|
|
|
|
|
|
|
|
/* We always look for a client connection with no transport. */
|
|
|
|
tor_addr_copy(&lookup.addr, addr);
|
|
|
|
lookup.action = action;
|
|
|
|
lookup.transport_name = (char *) transport_name;
|
|
|
|
|
|
|
|
return HT_FIND(clientmap, &client_history, &lookup);
|
|
|
|
}
|
|
|
|
|
2018-02-02 16:15:28 +01:00
|
|
|
/* Cleanup client entries older than the cutoff. Used for the OOM. Return the
|
|
|
|
* number of bytes freed. If 0 is returned, nothing was freed. */
|
|
|
|
static size_t
|
|
|
|
oom_clean_client_entries(time_t cutoff)
|
|
|
|
{
|
|
|
|
size_t bytes = 0;
|
|
|
|
clientmap_entry_t **ent, **ent_next;
|
|
|
|
|
|
|
|
for (ent = HT_START(clientmap, &client_history); ent; ent = ent_next) {
|
|
|
|
clientmap_entry_t *entry = *ent;
|
|
|
|
if (entry->last_seen_in_minutes < (cutoff / 60)) {
|
|
|
|
ent_next = HT_NEXT_RMV(clientmap, &client_history, ent);
|
|
|
|
bytes += clientmap_entry_size(entry);
|
|
|
|
clientmap_entry_free(entry);
|
|
|
|
} else {
|
|
|
|
ent_next = HT_NEXT(clientmap, &client_history, ent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Below this minimum lifetime, the OOM won't cleanup any entries. */
|
|
|
|
#define GEOIP_CLIENT_CACHE_OOM_MIN_CUTOFF (4 * 60 * 60)
|
|
|
|
/* The OOM moves the cutoff by that much every run. */
|
|
|
|
#define GEOIP_CLIENT_CACHE_OOM_STEP (15 * 50)
|
|
|
|
|
|
|
|
/* Cleanup the geoip client history cache called from the OOM handler. Return
|
|
|
|
* the amount of bytes removed. This can return a value below or above
|
|
|
|
* min_remove_bytes but will stop as oon as the min_remove_bytes has been
|
|
|
|
* reached. */
|
|
|
|
size_t
|
|
|
|
geoip_client_cache_handle_oom(time_t now, size_t min_remove_bytes)
|
|
|
|
{
|
|
|
|
time_t k;
|
|
|
|
size_t bytes_removed = 0;
|
|
|
|
|
|
|
|
/* Our OOM handler called with 0 bytes to remove is a code flow error. */
|
|
|
|
tor_assert(min_remove_bytes != 0);
|
|
|
|
|
|
|
|
/* Set k to the initial cutoff of an entry. We then going to move it by step
|
|
|
|
* to try to remove as much as we can. */
|
|
|
|
k = WRITE_STATS_INTERVAL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
time_t cutoff;
|
|
|
|
|
|
|
|
/* If k has reached the minimum lifetime, we have to stop else we might
|
|
|
|
* remove every single entries which would be pretty bad for the DoS
|
|
|
|
* mitigation subsystem if by just filling the geoip cache, it was enough
|
|
|
|
* to trigger the OOM and clean every single entries. */
|
|
|
|
if (k <= GEOIP_CLIENT_CACHE_OOM_MIN_CUTOFF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cutoff = now - k;
|
|
|
|
bytes_removed += oom_clean_client_entries(cutoff);
|
|
|
|
k -= GEOIP_CLIENT_CACHE_OOM_STEP;
|
|
|
|
} while (bytes_removed < min_remove_bytes);
|
|
|
|
|
|
|
|
return bytes_removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the total size in bytes of the client history cache. */
|
|
|
|
size_t
|
|
|
|
geoip_client_cache_total_allocation(void)
|
|
|
|
{
|
2018-02-05 16:39:10 +01:00
|
|
|
return geoip_client_history_cache_size;
|
2018-02-02 16:15:28 +01:00
|
|
|
}
|
|
|
|
|
2009-07-10 13:37:25 +02:00
|
|
|
/** How many responses are we giving to clients requesting v3 network
|
|
|
|
* statuses? */
|
|
|
|
static uint32_t ns_v3_responses[GEOIP_NS_RESPONSE_NUM];
|
|
|
|
|
2013-01-17 10:45:19 +01:00
|
|
|
/** Note that we've rejected a client's request for a v3 network status
|
|
|
|
* for reason <b>reason</b> at time <b>now</b>. */
|
2009-07-10 13:37:25 +02:00
|
|
|
void
|
2013-01-17 10:45:19 +01:00
|
|
|
geoip_note_ns_response(geoip_ns_response_t response)
|
2009-07-10 13:37:25 +02:00
|
|
|
{
|
|
|
|
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_v3_responses, 0, sizeof(ns_v3_responses));
|
|
|
|
arrays_initialized = 1;
|
|
|
|
}
|
|
|
|
tor_assert(response < GEOIP_NS_RESPONSE_NUM);
|
2013-01-17 10:45:19 +01:00
|
|
|
ns_v3_responses[response]++;
|
2009-07-10 13:37:25 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
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
|
2012-03-01 01:42:59 +01:00
|
|
|
* geoip_ipv4_entry_t. Sort in descending order of total, and then by country
|
2007-12-19 00:45:19 +01:00
|
|
|
* code. */
|
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
c_hist_compare_(const void **_a, const void **_b)
|
2007-12-19 00:45:19 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
|
2012-09-04 09:33:16 +02:00
|
|
|
/** Entry in a map from either chan->global_identifier for direct requests
|
2009-07-12 16:33:31 +02:00
|
|
|
* 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
|
2012-09-04 09:33:16 +02:00
|
|
|
* chan->global_identifier of the dir channel (direct request) or a new
|
2009-07-12 16:33:31 +02:00
|
|
|
* 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? */
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-05 01:51:00 +02:00
|
|
|
/* DOCDOC dirreq_map_ent_hash */
|
2009-07-15 18:24:02 +02:00
|
|
|
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,
|
2016-06-02 15:09:37 +02:00
|
|
|
dirreq_map_ent_eq)
|
2014-09-02 18:48:34 +02:00
|
|
|
HT_GENERATE2(dirreqmap, dirreq_map_entry_t, node, dirreq_map_ent_hash,
|
|
|
|
dirreq_map_ent_eq, 0.6, tor_reallocarray_, tor_free_)
|
2009-07-12 16:33:31 +02:00
|
|
|
|
|
|
|
/** Helper: Put <b>entry</b> into map of directory requests using
|
2010-06-21 18:52:46 +02:00
|
|
|
* <b>type</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
|
2012-10-12 18:22:13 +02:00
|
|
|
dirreq_map_put_(dirreq_map_entry_t *entry, dirreq_type_t type,
|
2009-07-14 22:24:50 +02:00
|
|
|
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);
|
|
|
|
|
2011-03-25 21:01:16 +01:00
|
|
|
/* XXXX we could switch this to HT_INSERT some time, since it seems that
|
|
|
|
* this bug doesn't happen. But since this function doesn't seem to be
|
|
|
|
* critical-path, it's sane to leave it alone. */
|
2009-07-15 18:24:02 +02:00
|
|
|
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
|
2010-06-21 18:52:46 +02:00
|
|
|
* using <b>type</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 *
|
2012-10-12 18:22:13 +02:00
|
|
|
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
|
2013-01-17 10:45:19 +01:00
|
|
|
* request for a v3 network status with unique ID <b>dirreq_id</b> of size
|
|
|
|
* <b>response_size</b> has started. */
|
2009-07-12 16:33:31 +02:00
|
|
|
void
|
2009-07-14 22:24:50 +02:00
|
|
|
geoip_start_dirreq(uint64_t dirreq_id, size_t response_size,
|
2013-01-17 10:45:19 +01:00
|
|
|
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->type = type;
|
2012-10-12 18:22:13 +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;
|
2012-10-12 18:22:13 +02:00
|
|
|
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 &&
|
2012-09-04 09:33:16 +02:00
|
|
|
new_state == DIRREQ_CHANNEL_BUFFER_FLUSHED)) {
|
2009-07-12 16:33:31 +02:00
|
|
|
tor_gettimeofday(&ent->completion_time);
|
|
|
|
ent->completed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-11 20:52:28 +01:00
|
|
|
/** Return the bridge-ip-transports string that should be inserted in
|
|
|
|
* our extra-info descriptor. Return NULL if the bridge-ip-transports
|
|
|
|
* line should be empty. */
|
|
|
|
char *
|
|
|
|
geoip_get_transport_history(void)
|
|
|
|
{
|
|
|
|
unsigned granularity = IP_GRANULARITY;
|
2014-03-25 16:16:18 +01:00
|
|
|
/** String hash table (name of transport) -> (number of users). */
|
2013-02-11 20:52:28 +01:00
|
|
|
strmap_t *transport_counts = strmap_new();
|
|
|
|
|
|
|
|
/** Smartlist that contains copies of the names of the transports
|
|
|
|
that have been used. */
|
|
|
|
smartlist_t *transports_used = smartlist_new();
|
|
|
|
|
|
|
|
/* Special string to signify that no transport was used for this
|
|
|
|
connection. Pluggable transport names can't have symbols in their
|
|
|
|
names, so this string will never collide with a real transport. */
|
|
|
|
static const char* no_transport_str = "<OR>";
|
|
|
|
|
|
|
|
clientmap_entry_t **ent;
|
|
|
|
smartlist_t *string_chunks = smartlist_new();
|
|
|
|
char *the_string = NULL;
|
|
|
|
|
|
|
|
/* If we haven't seen any clients yet, return NULL. */
|
|
|
|
if (HT_EMPTY(&client_history))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/** We do the following steps to form the transport history string:
|
|
|
|
* a) Foreach client that uses a pluggable transport, we increase the
|
|
|
|
* times that transport was used by one. If the client did not use
|
|
|
|
* a transport, we increase the number of times someone connected
|
|
|
|
* without obfuscation.
|
|
|
|
* b) Foreach transport we observed, we write its transport history
|
|
|
|
* string and push it to string_chunks. So, for example, if we've
|
|
|
|
* seen 665 obfs2 clients, we write "obfs2=665".
|
|
|
|
* c) We concatenate string_chunks to form the final string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
log_debug(LD_GENERAL,"Starting iteration for transport history. %d clients.",
|
|
|
|
HT_SIZE(&client_history));
|
|
|
|
|
|
|
|
/* Loop through all clients. */
|
|
|
|
HT_FOREACH(ent, clientmap, &client_history) {
|
2013-07-16 19:14:44 +02:00
|
|
|
uintptr_t val;
|
|
|
|
void *ptr;
|
2016-07-05 20:19:31 +02:00
|
|
|
const char *transport_name = (*ent)->transport_name;
|
2013-02-11 20:52:28 +01:00
|
|
|
if (!transport_name)
|
|
|
|
transport_name = no_transport_str;
|
|
|
|
|
|
|
|
/* Increase the count for this transport name. */
|
|
|
|
ptr = strmap_get(transport_counts, transport_name);
|
2013-07-16 19:14:44 +02:00
|
|
|
val = (uintptr_t)ptr;
|
2013-02-11 20:52:28 +01:00
|
|
|
val++;
|
|
|
|
ptr = (void*)val;
|
|
|
|
strmap_set(transport_counts, transport_name, ptr);
|
|
|
|
|
|
|
|
/* If it's the first time we see this transport, note it. */
|
2013-06-27 16:50:56 +02:00
|
|
|
if (val == 1)
|
2013-02-11 20:52:28 +01:00
|
|
|
smartlist_add(transports_used, tor_strdup(transport_name));
|
|
|
|
|
|
|
|
log_debug(LD_GENERAL, "Client from '%s' with transport '%s'. "
|
|
|
|
"I've now seen %d clients.",
|
|
|
|
safe_str_client(fmt_addr(&(*ent)->addr)),
|
|
|
|
transport_name ? transport_name : "<no transport>",
|
|
|
|
(int)val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort the transport names (helps with unit testing). */
|
|
|
|
smartlist_sort_strings(transports_used);
|
|
|
|
|
|
|
|
/* Loop through all seen transports. */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(transports_used, const char *, transport_name) {
|
|
|
|
void *transport_count_ptr = strmap_get(transport_counts, transport_name);
|
2013-07-16 19:14:44 +02:00
|
|
|
uintptr_t transport_count = (uintptr_t) transport_count_ptr;
|
2013-02-11 20:52:28 +01:00
|
|
|
|
2013-07-16 19:14:44 +02:00
|
|
|
log_debug(LD_GENERAL, "We got "U64_FORMAT" clients with transport '%s'.",
|
|
|
|
U64_PRINTF_ARG((uint64_t)transport_count), transport_name);
|
2013-02-11 20:52:28 +01:00
|
|
|
|
2013-07-16 19:14:44 +02:00
|
|
|
smartlist_add_asprintf(string_chunks, "%s="U64_FORMAT,
|
2013-02-11 20:52:28 +01:00
|
|
|
transport_name,
|
2013-07-16 19:14:44 +02:00
|
|
|
U64_PRINTF_ARG(round_uint64_to_next_multiple_of(
|
|
|
|
(uint64_t)transport_count,
|
|
|
|
granularity)));
|
2013-02-11 20:52:28 +01:00
|
|
|
} SMARTLIST_FOREACH_END(transport_name);
|
|
|
|
|
2013-06-27 16:50:56 +02:00
|
|
|
the_string = smartlist_join_strings(string_chunks, ",", 0, NULL);
|
2013-02-11 20:52:28 +01:00
|
|
|
|
|
|
|
log_debug(LD_GENERAL, "Final bridge-ip-transports string: '%s'", the_string);
|
|
|
|
|
|
|
|
done:
|
|
|
|
strmap_free(transport_counts, NULL);
|
|
|
|
SMARTLIST_FOREACH(transports_used, char *, s, tor_free(s));
|
|
|
|
smartlist_free(transports_used);
|
|
|
|
SMARTLIST_FOREACH(string_chunks, char *, s, tor_free(s));
|
|
|
|
smartlist_free(string_chunks);
|
|
|
|
|
|
|
|
return the_string;
|
|
|
|
}
|
|
|
|
|
2009-07-12 16:33:31 +02:00
|
|
|
/** 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 *
|
2013-01-17 10:45:19 +01:00
|
|
|
geoip_get_dirreq_history(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;
|
2016-07-05 20:19:31 +02:00
|
|
|
dirreq_map_entry_t **ptr, **next;
|
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);
|
2012-01-18 21:53:30 +01:00
|
|
|
dirreq_completed = smartlist_new();
|
2009-07-15 18:24:02 +02:00
|
|
|
for (ptr = HT_START(dirreqmap, &dirreq_map); ptr; ptr = next) {
|
2016-07-05 20:19:31 +02:00
|
|
|
dirreq_map_entry_t *ent = *ptr;
|
2013-01-17 10:45:19 +01:00
|
|
|
if (ent->type != type) {
|
2009-07-15 18:24:02 +02:00
|
|
|
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);
|
2014-11-02 17:56:02 +01:00
|
|
|
dltimes = tor_calloc(complete, sizeof(uint32_t));
|
2009-08-21 19:35:47 +02:00
|
|
|
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
|
2015-11-23 13:40:13 +01:00
|
|
|
* by law of nature or something, but a millisecond
|
2009-08-21 19:35:47 +02:00
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2012-10-19 23:23:04 +02:00
|
|
|
/** Store a newly allocated comma-separated string in
|
|
|
|
* *<a>country_str</a> containing entries for all the countries from
|
|
|
|
* which we've seen enough clients connect as a bridge, directory
|
|
|
|
* server, or entry guard. 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. *<a>country_str</a> is set to NULL if
|
|
|
|
* we're not ready to export per country data yet.
|
|
|
|
*
|
|
|
|
* Store a newly allocated comma-separated string in <a>ipver_str</a>
|
|
|
|
* containing entries for clients connecting over IPv4 and IPv6. The
|
|
|
|
* format is family=num where num is the nubmer of IPs we've seen
|
|
|
|
* connecting over that protocol family, and family is 'v4' or 'v6'.
|
|
|
|
*
|
|
|
|
* Return 0 on success and -1 if we're missing geoip data. */
|
|
|
|
int
|
|
|
|
geoip_get_client_history(geoip_client_action_t action,
|
|
|
|
char **country_str, char **ipver_str)
|
2007-12-17 23:44:11 +01:00
|
|
|
{
|
2010-06-21 18:52:46 +02:00
|
|
|
unsigned granularity = IP_GRANULARITY;
|
|
|
|
smartlist_t *entries = NULL;
|
|
|
|
int n_countries = geoip_get_n_countries();
|
|
|
|
int i;
|
2016-07-05 20:19:31 +02:00
|
|
|
clientmap_entry_t **cm_ent;
|
2010-06-21 18:52:46 +02:00
|
|
|
unsigned *counts = NULL;
|
|
|
|
unsigned total = 0;
|
2012-02-24 22:21:48 +01:00
|
|
|
unsigned ipv4_count = 0, ipv6_count = 0;
|
2010-06-21 18:52:46 +02:00
|
|
|
|
2012-10-20 18:48:58 +02:00
|
|
|
if (!geoip_is_loaded(AF_INET) && !geoip_is_loaded(AF_INET6))
|
2012-10-19 23:23:04 +02:00
|
|
|
return -1;
|
2010-06-21 18:52:46 +02:00
|
|
|
|
2014-11-02 17:56:02 +01:00
|
|
|
counts = tor_calloc(n_countries, sizeof(unsigned));
|
2016-07-05 20:19:31 +02:00
|
|
|
HT_FOREACH(cm_ent, clientmap, &client_history) {
|
2010-06-21 18:52:46 +02:00
|
|
|
int country;
|
2016-07-05 20:19:31 +02:00
|
|
|
if ((*cm_ent)->action != (int)action)
|
2010-06-21 18:52:46 +02:00
|
|
|
continue;
|
2016-07-05 20:19:31 +02:00
|
|
|
country = geoip_get_country_by_addr(&(*cm_ent)->addr);
|
2010-06-21 18:52:46 +02:00
|
|
|
if (country < 0)
|
|
|
|
country = 0; /** unresolved requests are stored at index 0. */
|
|
|
|
tor_assert(0 <= country && country < n_countries);
|
|
|
|
++counts[country];
|
|
|
|
++total;
|
2016-07-05 20:19:31 +02:00
|
|
|
switch (tor_addr_family(&(*cm_ent)->addr)) {
|
2012-02-24 22:21:48 +01:00
|
|
|
case AF_INET:
|
|
|
|
ipv4_count++;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
ipv6_count++;
|
|
|
|
break;
|
|
|
|
}
|
2010-06-21 18:52:46 +02:00
|
|
|
}
|
2012-10-19 23:23:04 +02:00
|
|
|
if (ipver_str) {
|
|
|
|
smartlist_t *chunks = smartlist_new();
|
|
|
|
smartlist_add_asprintf(chunks, "v4=%u",
|
|
|
|
round_to_next_multiple_of(ipv4_count, granularity));
|
|
|
|
smartlist_add_asprintf(chunks, "v6=%u",
|
|
|
|
round_to_next_multiple_of(ipv6_count, granularity));
|
|
|
|
*ipver_str = smartlist_join_strings(chunks, ",", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
|
|
|
|
smartlist_free(chunks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't record per country data if we haven't seen enough IPs. */
|
|
|
|
if (total < MIN_IPS_TO_NOTE_ANYTHING) {
|
|
|
|
tor_free(counts);
|
|
|
|
if (country_str)
|
|
|
|
*country_str = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
/* Make a list of c_hist_t */
|
2012-01-18 21:53:30 +01:00
|
|
|
entries = smartlist_new();
|
2010-06-21 18:52:46 +02:00
|
|
|
for (i = 0; i < n_countries; ++i) {
|
|
|
|
unsigned c = counts[i];
|
|
|
|
const char *countrycode;
|
|
|
|
c_hist_t *ent;
|
|
|
|
/* Only report a country if it has a minimum number of IPs. */
|
|
|
|
if (c >= MIN_IPS_TO_NOTE_COUNTRY) {
|
|
|
|
c = round_to_next_multiple_of(c, granularity);
|
|
|
|
countrycode = geoip_get_country_name(i);
|
|
|
|
ent = tor_malloc(sizeof(c_hist_t));
|
|
|
|
strlcpy(ent->country, countrycode, sizeof(ent->country));
|
|
|
|
ent->total = c;
|
|
|
|
smartlist_add(entries, ent);
|
2007-12-19 00:45:19 +01:00
|
|
|
}
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
2010-06-21 18:52:46 +02:00
|
|
|
/* Sort entries. Note that we must do this _AFTER_ rounding, or else
|
|
|
|
* the sort order could leak info. */
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(entries, c_hist_compare_);
|
2009-08-14 14:45:53 +02:00
|
|
|
|
2012-10-19 23:23:04 +02:00
|
|
|
if (country_str) {
|
|
|
|
smartlist_t *chunks = smartlist_new();
|
|
|
|
SMARTLIST_FOREACH(entries, c_hist_t *, ch, {
|
|
|
|
smartlist_add_asprintf(chunks, "%s=%u", ch->country, ch->total);
|
|
|
|
});
|
|
|
|
*country_str = smartlist_join_strings(chunks, ",", 0, NULL);
|
2010-06-21 18:52:46 +02:00
|
|
|
SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
|
|
|
|
smartlist_free(chunks);
|
|
|
|
}
|
2012-10-19 23:23:04 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(entries, c_hist_t *, c, tor_free(c));
|
|
|
|
smartlist_free(entries);
|
|
|
|
tor_free(counts);
|
|
|
|
|
|
|
|
return 0;
|
2009-08-14 14:45:53 +02:00
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** Return a newly allocated string holding the per-country request history
|
2013-01-17 10:45:19 +01:00
|
|
|
* for v3 network statuses in a format suitable for an extra-info document,
|
|
|
|
* or NULL on failure. */
|
2008-06-10 20:08:56 +02:00
|
|
|
char *
|
2013-01-17 10:45:19 +01:00
|
|
|
geoip_get_request_history(void)
|
2008-06-10 20:08:56 +02:00
|
|
|
{
|
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;
|
|
|
|
|
2008-06-10 20:08:56 +02:00
|
|
|
if (!geoip_countries)
|
|
|
|
return NULL;
|
2008-06-18 06:34:52 +02:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
entries = smartlist_new();
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(geoip_countries, geoip_country_t *, c) {
|
2008-06-10 20:08:56 +02:00
|
|
|
uint32_t tot = 0;
|
2008-06-18 06:34:52 +02:00
|
|
|
c_hist_t *ent;
|
2013-01-17 10:45:19 +01:00
|
|
|
tot = c->n_v3_ns_requests;
|
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);
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(c);
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(entries, c_hist_compare_);
|
2008-06-18 06:34:52 +02:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
strings = smartlist_new();
|
2008-06-18 06:34:52 +02:00
|
|
|
SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(strings, "%s=%u", ent->country, ent->total);
|
|
|
|
});
|
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;
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
/** Start time of directory request stats or 0 if we're not collecting
|
|
|
|
* directory request statistics. */
|
2009-08-19 15:41:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-04 12:28:12 +02:00
|
|
|
/** Reset counters for dirreq stats. */
|
2009-08-19 15:41:12 +02:00
|
|
|
void
|
2011-08-04 12:28:12 +02:00
|
|
|
geoip_reset_dirreq_stats(time_t now)
|
2010-06-21 18:52:46 +02:00
|
|
|
{
|
|
|
|
SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
|
2013-01-17 10:45:19 +01:00
|
|
|
c->n_v3_ns_requests = 0;
|
2010-06-21 18:52:46 +02:00
|
|
|
});
|
|
|
|
{
|
|
|
|
clientmap_entry_t **ent, **next, *this;
|
|
|
|
for (ent = HT_START(clientmap, &client_history); ent != NULL;
|
|
|
|
ent = next) {
|
2013-01-17 10:45:19 +01:00
|
|
|
if ((*ent)->action == GEOIP_CLIENT_NETWORKSTATUS) {
|
2010-06-21 18:52:46 +02:00
|
|
|
this = *ent;
|
|
|
|
next = HT_NEXT_RMV(clientmap, &client_history, ent);
|
2013-07-16 20:04:36 +02:00
|
|
|
clientmap_entry_free(this);
|
2010-06-21 18:52:46 +02:00
|
|
|
} else {
|
|
|
|
next = HT_NEXT(clientmap, &client_history, ent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(ns_v3_responses, 0, sizeof(ns_v3_responses));
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2011-08-04 12:28:12 +02:00
|
|
|
start_of_dirreq_stats_interval = now;
|
2010-06-21 18:52:46 +02:00
|
|
|
}
|
|
|
|
|
2011-08-04 12:28:12 +02:00
|
|
|
/** Stop collecting directory request stats in a way that we can re-start
|
|
|
|
* doing so in geoip_dirreq_stats_init(). */
|
|
|
|
void
|
|
|
|
geoip_dirreq_stats_term(void)
|
|
|
|
{
|
|
|
|
geoip_reset_dirreq_stats(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a newly allocated string containing the dirreq statistics
|
2011-10-21 17:01:38 +02:00
|
|
|
* until <b>now</b>, or NULL if we're not collecting dirreq stats. Caller
|
|
|
|
* must ensure start_of_dirreq_stats_interval is in the past. */
|
2011-08-04 12:28:12 +02:00
|
|
|
char *
|
|
|
|
geoip_format_dirreq_stats(time_t now)
|
2008-05-29 04:29:35 +02:00
|
|
|
{
|
2011-08-04 11:52:52 +02:00
|
|
|
char t[ISO_TIME_LEN+1];
|
2009-07-10 13:37:25 +02:00
|
|
|
int i;
|
2015-09-07 19:22:33 +02:00
|
|
|
char *v3_ips_string = NULL, *v3_reqs_string = NULL,
|
|
|
|
*v3_direct_dl_string = NULL, *v3_tunneled_dl_string = NULL;
|
|
|
|
char *result = NULL;
|
2008-05-29 04:29:35 +02:00
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
if (!start_of_dirreq_stats_interval)
|
2011-08-04 12:28:12 +02:00
|
|
|
return NULL; /* Not initialized. */
|
2009-08-19 15:41:12 +02:00
|
|
|
|
2011-10-21 17:01:38 +02:00
|
|
|
tor_assert(now >= start_of_dirreq_stats_interval);
|
|
|
|
|
2011-08-04 11:52:52 +02:00
|
|
|
format_iso_time(t, now);
|
2012-10-19 23:23:04 +02:00
|
|
|
geoip_get_client_history(GEOIP_CLIENT_NETWORKSTATUS, &v3_ips_string, NULL);
|
2013-01-17 10:45:19 +01:00
|
|
|
v3_reqs_string = geoip_get_request_history();
|
2008-06-10 20:08:56 +02:00
|
|
|
|
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_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
|
2011-08-04 11:52:52 +02:00
|
|
|
|
2013-01-17 10:45:19 +01:00
|
|
|
v3_direct_dl_string = geoip_get_dirreq_history(DIRREQ_DIRECT);
|
|
|
|
v3_tunneled_dl_string = geoip_get_dirreq_history(DIRREQ_TUNNELED);
|
2011-08-04 11:52:52 +02:00
|
|
|
|
|
|
|
/* Put everything together into a single string. */
|
|
|
|
tor_asprintf(&result, "dirreq-stats-end %s (%d s)\n"
|
|
|
|
"dirreq-v3-ips %s\n"
|
|
|
|
"dirreq-v3-reqs %s\n"
|
|
|
|
"dirreq-v3-resp ok=%u,not-enough-sigs=%u,unavailable=%u,"
|
|
|
|
"not-found=%u,not-modified=%u,busy=%u\n"
|
|
|
|
"dirreq-v3-direct-dl %s\n"
|
2013-01-17 10:45:19 +01:00
|
|
|
"dirreq-v3-tunneled-dl %s\n",
|
2011-08-04 11:52:52 +02:00
|
|
|
t,
|
|
|
|
(unsigned) (now - start_of_dirreq_stats_interval),
|
|
|
|
v3_ips_string ? v3_ips_string : "",
|
|
|
|
v3_reqs_string ? v3_reqs_string : "",
|
|
|
|
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],
|
|
|
|
v3_direct_dl_string ? v3_direct_dl_string : "",
|
2013-01-17 10:45:19 +01:00
|
|
|
v3_tunneled_dl_string ? v3_tunneled_dl_string : "");
|
2011-08-04 11:52:52 +02:00
|
|
|
|
|
|
|
/* Free partial strings. */
|
|
|
|
tor_free(v3_ips_string);
|
|
|
|
tor_free(v3_reqs_string);
|
|
|
|
tor_free(v3_direct_dl_string);
|
|
|
|
tor_free(v3_tunneled_dl_string);
|
|
|
|
|
2011-08-04 12:28:12 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** If 24 hours have passed since the beginning of the current dirreq
|
|
|
|
* stats period, write dirreq stats to $DATADIR/stats/dirreq-stats
|
|
|
|
* (possibly overwriting an existing file) and reset counters. Return
|
|
|
|
* when we would next want to write dirreq stats or 0 if we never want to
|
|
|
|
* write. */
|
|
|
|
time_t
|
|
|
|
geoip_dirreq_stats_write(time_t now)
|
|
|
|
{
|
2013-01-25 11:49:33 +01:00
|
|
|
char *str = NULL;
|
2011-08-04 12:28:12 +02:00
|
|
|
|
|
|
|
if (!start_of_dirreq_stats_interval)
|
|
|
|
return 0; /* Not initialized. */
|
|
|
|
if (start_of_dirreq_stats_interval + WRITE_STATS_INTERVAL > now)
|
|
|
|
goto done; /* Not ready to write. */
|
|
|
|
|
|
|
|
/* Discard all items in the client history that are too old. */
|
|
|
|
geoip_remove_old_clients(start_of_dirreq_stats_interval);
|
|
|
|
|
|
|
|
/* Generate history string .*/
|
|
|
|
str = geoip_format_dirreq_stats(now);
|
2015-12-18 16:00:44 +01:00
|
|
|
if (! str)
|
|
|
|
goto done;
|
2011-08-04 11:52:52 +02:00
|
|
|
|
|
|
|
/* Write dirreq-stats string to disk. */
|
2013-05-24 19:36:15 +02:00
|
|
|
if (!check_or_create_data_subdir("stats")) {
|
2013-01-25 11:49:33 +01:00
|
|
|
write_to_data_subdir("stats", "dirreq-stats", str, "dirreq statistics");
|
|
|
|
/* Reset measurement interval start. */
|
|
|
|
geoip_reset_dirreq_stats(now);
|
2011-08-04 12:28:12 +02:00
|
|
|
}
|
2009-08-19 15:41:12 +02:00
|
|
|
|
2008-05-29 04:29:35 +02:00
|
|
|
done:
|
2011-08-04 12:28:12 +02:00
|
|
|
tor_free(str);
|
2010-06-21 18:52:46 +02:00
|
|
|
return start_of_dirreq_stats_interval + WRITE_STATS_INTERVAL;
|
2008-05-29 04:29:35 +02:00
|
|
|
}
|
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
/** Start time of bridge stats or 0 if we're not collecting bridge
|
|
|
|
* statistics. */
|
2009-12-12 08:32:46 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
/** Stop collecting bridge stats in a way that we can re-start doing so in
|
|
|
|
* geoip_bridge_stats_init(). */
|
|
|
|
void
|
|
|
|
geoip_bridge_stats_term(void)
|
|
|
|
{
|
|
|
|
client_history_clear();
|
|
|
|
start_of_bridge_stats_interval = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-16 03:39:22 +01:00
|
|
|
/** Validate a bridge statistics string as it would be written to a
|
|
|
|
* current extra-info descriptor. Return 1 if the string is valid and
|
|
|
|
* recent enough, or 0 otherwise. */
|
|
|
|
static int
|
|
|
|
validate_bridge_stats(const char *stats_str, time_t now)
|
2009-12-12 08:32:46 +01:00
|
|
|
{
|
|
|
|
char stats_end_str[ISO_TIME_LEN+1], stats_start_str[ISO_TIME_LEN+1],
|
2011-03-16 03:39:22 +01:00
|
|
|
*eos;
|
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";
|
2013-02-11 20:52:28 +01:00
|
|
|
const char *BRIDGE_TRANSPORTS = "bridge-ip-transports ";
|
|
|
|
const char *BRIDGE_TRANSPORTS_EMPTY_LINE = "bridge-ip-transports\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)
|
2011-03-16 03:39:22 +01:00
|
|
|
return 0;
|
2009-12-18 00:29:37 +01:00
|
|
|
tmp += strlen(BRIDGE_STATS_END);
|
|
|
|
|
2009-12-17 11:20:31 +01:00
|
|
|
if (strlen(tmp) < ISO_TIME_LEN + 6)
|
2011-03-16 03:39:22 +01:00
|
|
|
return 0;
|
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)
|
2011-03-16 03:39:22 +01:00
|
|
|
return 0;
|
2009-12-12 08:32:46 +01:00
|
|
|
if (stats_end_time < now - (25*60*60) ||
|
|
|
|
stats_end_time > now + (1*60*60))
|
2011-03-16 03:39:22 +01:00
|
|
|
return 0;
|
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)
|
2011-03-16 03:39:22 +01:00
|
|
|
return 0;
|
2009-12-12 08:32:46 +01:00
|
|
|
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);
|
2011-03-16 03:39:22 +01:00
|
|
|
if (!tmp) {
|
2009-12-18 13:29:51 +01:00
|
|
|
/* Look if there is an empty "bridge-ips" line */
|
|
|
|
tmp = find_str_at_start_of_line(stats_str, BRIDGE_IPS_EMPTY_LINE);
|
|
|
|
if (!tmp)
|
2011-03-16 03:39:22 +01:00
|
|
|
return 0;
|
2009-12-18 13:29:51 +01:00
|
|
|
}
|
2009-12-18 00:29:37 +01:00
|
|
|
|
2013-02-11 20:52:28 +01:00
|
|
|
/* Parse: "bridge-ip-transports PT=N,PT=N,..." */
|
|
|
|
tmp = find_str_at_start_of_line(stats_str, BRIDGE_TRANSPORTS);
|
|
|
|
if (!tmp) {
|
|
|
|
/* Look if there is an empty "bridge-ip-transports" line */
|
|
|
|
tmp = find_str_at_start_of_line(stats_str, BRIDGE_TRANSPORTS_EMPTY_LINE);
|
|
|
|
if (!tmp)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-16 03:39:22 +01:00
|
|
|
return 1;
|
2009-12-12 08:32:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Most recent bridge statistics formatted to be written to extra-info
|
|
|
|
* descriptors. */
|
|
|
|
static char *bridge_stats_extrainfo = NULL;
|
|
|
|
|
2011-03-16 03:39:22 +01:00
|
|
|
/** Return a newly allocated string holding our bridge usage stats by country
|
|
|
|
* in a format suitable for inclusion in an extrainfo document. Return NULL on
|
|
|
|
* failure. */
|
2011-08-04 14:45:24 +02:00
|
|
|
char *
|
|
|
|
geoip_format_bridge_stats(time_t now)
|
2011-03-16 03:39:22 +01:00
|
|
|
{
|
2013-02-11 20:52:28 +01:00
|
|
|
char *out = NULL;
|
|
|
|
char *country_data = NULL, *ipver_data = NULL, *transport_data = NULL;
|
2011-03-16 03:39:22 +01:00
|
|
|
long duration = now - start_of_bridge_stats_interval;
|
|
|
|
char written[ISO_TIME_LEN+1];
|
|
|
|
|
|
|
|
if (duration < 0)
|
|
|
|
return NULL;
|
2011-08-04 14:45:24 +02:00
|
|
|
if (!start_of_bridge_stats_interval)
|
|
|
|
return NULL; /* Not initialized. */
|
2011-03-16 03:39:22 +01:00
|
|
|
|
|
|
|
format_iso_time(written, now);
|
2012-10-19 23:23:04 +02:00
|
|
|
geoip_get_client_history(GEOIP_CLIENT_CONNECT, &country_data, &ipver_data);
|
2013-02-11 20:52:28 +01:00
|
|
|
transport_data = geoip_get_transport_history();
|
2011-03-16 03:39:22 +01:00
|
|
|
|
|
|
|
tor_asprintf(&out,
|
|
|
|
"bridge-stats-end %s (%ld s)\n"
|
2012-02-24 22:21:48 +01:00
|
|
|
"bridge-ips %s\n"
|
2013-02-11 20:52:28 +01:00
|
|
|
"bridge-ip-versions %s\n"
|
|
|
|
"bridge-ip-transports %s\n",
|
2011-03-16 03:39:22 +01:00
|
|
|
written, duration,
|
2012-10-19 23:23:04 +02:00
|
|
|
country_data ? country_data : "",
|
2013-02-11 20:52:28 +01:00
|
|
|
ipver_data ? ipver_data : "",
|
|
|
|
transport_data ? transport_data : "");
|
2012-10-19 23:23:04 +02:00
|
|
|
tor_free(country_data);
|
|
|
|
tor_free(ipver_data);
|
2013-02-11 20:52:28 +01:00
|
|
|
tor_free(transport_data);
|
2011-03-16 03:39:22 +01:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a newly allocated string holding our bridge usage stats by country
|
|
|
|
* in a format suitable for the answer to a controller request. Return NULL on
|
|
|
|
* failure. */
|
|
|
|
static char *
|
|
|
|
format_bridge_stats_controller(time_t now)
|
|
|
|
{
|
2012-10-19 23:23:04 +02:00
|
|
|
char *out = NULL, *country_data = NULL, *ipver_data = NULL;
|
2011-03-16 03:39:22 +01:00
|
|
|
char started[ISO_TIME_LEN+1];
|
|
|
|
(void) now;
|
|
|
|
|
|
|
|
format_iso_time(started, start_of_bridge_stats_interval);
|
2012-10-19 23:23:04 +02:00
|
|
|
geoip_get_client_history(GEOIP_CLIENT_CONNECT, &country_data, &ipver_data);
|
2011-03-16 03:39:22 +01:00
|
|
|
|
|
|
|
tor_asprintf(&out,
|
2012-10-19 23:23:04 +02:00
|
|
|
"TimeStarted=\"%s\" CountrySummary=%s IPVersions=%s",
|
|
|
|
started,
|
|
|
|
country_data ? country_data : "",
|
|
|
|
ipver_data ? ipver_data : "");
|
|
|
|
tor_free(country_data);
|
|
|
|
tor_free(ipver_data);
|
2011-03-16 03:39:22 +01:00
|
|
|
return out;
|
|
|
|
}
|
2009-12-12 08:32:46 +01:00
|
|
|
|
2013-02-26 11:43:53 +01:00
|
|
|
/** Return a newly allocated string holding our bridge usage stats by
|
|
|
|
* country in a format suitable for inclusion in our heartbeat
|
|
|
|
* message. Return NULL on failure. */
|
|
|
|
char *
|
|
|
|
format_client_stats_heartbeat(time_t now)
|
|
|
|
{
|
2015-02-03 19:02:22 +01:00
|
|
|
const int n_hours = 6;
|
2013-02-26 11:43:53 +01:00
|
|
|
char *out = NULL;
|
|
|
|
int n_clients = 0;
|
|
|
|
clientmap_entry_t **ent;
|
2015-02-03 19:02:22 +01:00
|
|
|
unsigned cutoff = (unsigned)( (now-n_hours*3600)/60 );
|
2013-02-26 11:43:53 +01:00
|
|
|
|
|
|
|
if (!start_of_bridge_stats_interval)
|
|
|
|
return NULL; /* Not initialized. */
|
|
|
|
|
|
|
|
/* count unique IPs */
|
|
|
|
HT_FOREACH(ent, clientmap, &client_history) {
|
|
|
|
/* only count directly connecting clients */
|
|
|
|
if ((*ent)->action != GEOIP_CLIENT_CONNECT)
|
|
|
|
continue;
|
2015-02-03 19:02:22 +01:00
|
|
|
if ((*ent)->last_seen_in_minutes < cutoff)
|
|
|
|
continue;
|
2013-02-26 11:43:53 +01:00
|
|
|
n_clients++;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_asprintf(&out, "Heartbeat: "
|
2015-02-03 19:02:22 +01:00
|
|
|
"In the last %d hours, I have seen %d unique clients.",
|
|
|
|
n_hours,
|
2013-02-26 11:43:53 +01:00
|
|
|
n_clients);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-12-12 08:32:46 +01:00
|
|
|
/** 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)
|
|
|
|
{
|
2013-01-25 11:49:33 +01:00
|
|
|
char *val = NULL;
|
2009-12-12 08:32:46 +01:00
|
|
|
|
|
|
|
/* Check if 24 hours have passed since starting measurements. */
|
2010-06-21 18:52:46 +02:00
|
|
|
if (now < start_of_bridge_stats_interval + WRITE_STATS_INTERVAL)
|
|
|
|
return start_of_bridge_stats_interval + WRITE_STATS_INTERVAL;
|
2009-12-12 08:32:46 +01:00
|
|
|
|
|
|
|
/* Discard all items in the client history that are too old. */
|
|
|
|
geoip_remove_old_clients(start_of_bridge_stats_interval);
|
|
|
|
|
2011-03-16 03:39:22 +01:00
|
|
|
/* Generate formatted string */
|
2011-08-04 14:45:24 +02:00
|
|
|
val = geoip_format_bridge_stats(now);
|
2011-03-16 03:39:22 +01:00
|
|
|
if (val == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Update the stored value. */
|
|
|
|
tor_free(bridge_stats_extrainfo);
|
|
|
|
bridge_stats_extrainfo = val;
|
|
|
|
start_of_bridge_stats_interval = now;
|
|
|
|
|
|
|
|
/* Write it to disk. */
|
2013-01-25 11:49:33 +01:00
|
|
|
if (!check_or_create_data_subdir("stats")) {
|
|
|
|
write_to_data_subdir("stats", "bridge-stats",
|
2013-05-24 19:37:14 +02:00
|
|
|
bridge_stats_extrainfo, "bridge statistics");
|
2013-01-25 11:49:33 +01:00
|
|
|
|
|
|
|
/* Tell the controller, "hey, there are clients!" */
|
|
|
|
{
|
|
|
|
char *controller_str = format_bridge_stats_controller(now);
|
|
|
|
if (controller_str)
|
|
|
|
control_event_clients_seen(controller_str);
|
|
|
|
tor_free(controller_str);
|
|
|
|
}
|
2011-03-16 03:39:22 +01:00
|
|
|
}
|
|
|
|
|
2013-01-25 11:49:33 +01:00
|
|
|
done:
|
2011-03-16 03:39:22 +01:00
|
|
|
return start_of_bridge_stats_interval + WRITE_STATS_INTERVAL;
|
2009-12-12 08:32:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Try to load the most recent bridge statistics from disk, unless we
|
2011-03-16 03:39:22 +01:00
|
|
|
* have finished a measurement interval lately, and check whether they
|
|
|
|
* are still recent enough. */
|
2009-12-12 08:32:46 +01:00
|
|
|
static void
|
|
|
|
load_bridge_stats(time_t now)
|
|
|
|
{
|
2011-03-16 03:39:22 +01:00
|
|
|
char *fname, *contents;
|
2009-12-12 08:32:46 +01:00
|
|
|
if (bridge_stats_extrainfo)
|
|
|
|
return;
|
2011-03-16 03:39:22 +01:00
|
|
|
|
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);
|
2012-12-29 04:49:32 +01:00
|
|
|
if (contents && validate_bridge_stats(contents, now)) {
|
2011-03-16 03:39:22 +01:00
|
|
|
bridge_stats_extrainfo = contents;
|
2012-12-29 04:49:32 +01:00
|
|
|
} else {
|
|
|
|
tor_free(contents);
|
|
|
|
}
|
2011-03-16 03:39:22 +01:00
|
|
|
|
2009-12-12 08:32:46 +01:00
|
|
|
tor_free(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
|
2011-03-16 03:39:22 +01:00
|
|
|
/** Return a new string containing the recent bridge statistics to be returned
|
|
|
|
* to controller clients, or NULL if we don't have any bridge statistics. */
|
|
|
|
char *
|
2009-12-12 08:32:46 +01:00
|
|
|
geoip_get_bridge_stats_controller(time_t now)
|
|
|
|
{
|
2011-03-16 03:39:22 +01:00
|
|
|
return format_bridge_stats_controller(now);
|
2009-12-12 08:32:46 +01:00
|
|
|
}
|
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
/** Start time of entry stats or 0 if we're not collecting entry
|
|
|
|
* statistics. */
|
2009-08-19 15:41:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-04 14:14:01 +02:00
|
|
|
/** Reset counters for entry stats. */
|
|
|
|
void
|
|
|
|
geoip_reset_entry_stats(time_t now)
|
|
|
|
{
|
|
|
|
client_history_clear();
|
|
|
|
start_of_entry_stats_interval = now;
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
/** Stop collecting entry stats in a way that we can re-start doing so in
|
|
|
|
* geoip_entry_stats_init(). */
|
2009-08-19 15:41:12 +02:00
|
|
|
void
|
2010-06-21 18:52:46 +02:00
|
|
|
geoip_entry_stats_term(void)
|
|
|
|
{
|
2011-08-04 14:14:01 +02:00
|
|
|
geoip_reset_entry_stats(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a newly allocated string containing the entry statistics
|
2011-10-21 17:01:38 +02:00
|
|
|
* until <b>now</b>, or NULL if we're not collecting entry stats. Caller
|
|
|
|
* must ensure start_of_entry_stats_interval lies in the past. */
|
2011-08-04 14:14:01 +02:00
|
|
|
char *
|
|
|
|
geoip_format_entry_stats(time_t now)
|
|
|
|
{
|
|
|
|
char t[ISO_TIME_LEN+1];
|
|
|
|
char *data = NULL;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if (!start_of_entry_stats_interval)
|
|
|
|
return NULL; /* Not initialized. */
|
|
|
|
|
2011-10-21 17:01:38 +02:00
|
|
|
tor_assert(now >= start_of_entry_stats_interval);
|
|
|
|
|
2012-10-19 23:23:04 +02:00
|
|
|
geoip_get_client_history(GEOIP_CLIENT_CONNECT, &data, NULL);
|
2011-08-04 14:14:01 +02:00
|
|
|
format_iso_time(t, now);
|
2012-02-24 22:21:48 +01:00
|
|
|
tor_asprintf(&result,
|
2012-03-28 16:02:19 +02:00
|
|
|
"entry-stats-end %s (%u s)\n"
|
|
|
|
"entry-ips %s\n",
|
|
|
|
t, (unsigned) (now - start_of_entry_stats_interval),
|
|
|
|
data ? data : "");
|
2011-08-04 14:14:01 +02:00
|
|
|
tor_free(data);
|
|
|
|
return result;
|
2010-06-21 18:52:46 +02:00
|
|
|
}
|
|
|
|
|
2011-08-04 14:14:01 +02:00
|
|
|
/** If 24 hours have passed since the beginning of the current entry stats
|
|
|
|
* period, write entry stats to $DATADIR/stats/entry-stats (possibly
|
|
|
|
* overwriting an existing file) and reset counters. Return when we would
|
|
|
|
* next want to write entry stats or 0 if we never want to write. */
|
2010-06-21 18:52:46 +02:00
|
|
|
time_t
|
2009-08-19 15:41:12 +02:00
|
|
|
geoip_entry_stats_write(time_t now)
|
2009-07-05 20:48:16 +02:00
|
|
|
{
|
2013-01-25 11:49:33 +01:00
|
|
|
char *str = NULL;
|
2009-07-05 20:48:16 +02:00
|
|
|
|
2010-06-21 18:52:46 +02:00
|
|
|
if (!start_of_entry_stats_interval)
|
|
|
|
return 0; /* Not initialized. */
|
|
|
|
if (start_of_entry_stats_interval + WRITE_STATS_INTERVAL > now)
|
|
|
|
goto done; /* Not ready to write. */
|
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_entry_stats_interval);
|
|
|
|
|
2011-08-04 14:14:01 +02:00
|
|
|
/* Generate history string .*/
|
|
|
|
str = geoip_format_entry_stats(now);
|
|
|
|
|
|
|
|
/* Write entry-stats string to disk. */
|
2013-01-25 11:49:33 +01:00
|
|
|
if (!check_or_create_data_subdir("stats")) {
|
2013-05-24 19:37:14 +02:00
|
|
|
write_to_data_subdir("stats", "entry-stats", str, "entry statistics");
|
2009-07-05 20:48:16 +02:00
|
|
|
|
2013-05-24 19:37:14 +02:00
|
|
|
/* Reset measurement interval start. */
|
|
|
|
geoip_reset_entry_stats(now);
|
2013-01-25 11:49:33 +01:00
|
|
|
}
|
2009-08-19 15:41:12 +02:00
|
|
|
|
2009-07-05 20:48:16 +02:00
|
|
|
done:
|
2011-08-04 14:14:01 +02:00
|
|
|
tor_free(str);
|
2010-06-21 18:52:46 +02:00
|
|
|
return start_of_entry_stats_interval + WRITE_STATS_INTERVAL;
|
2009-07-05 20:48:16 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2010-07-18 17:05:58 +02:00
|
|
|
const char *question, char **answer,
|
|
|
|
const char **errmsg)
|
2007-12-17 23:44:18 +01:00
|
|
|
{
|
|
|
|
(void)control_conn;
|
2010-07-18 17:05:58 +02:00
|
|
|
if (!strcmpstart(question, "ip-to-country/")) {
|
2007-12-17 23:44:18 +01:00
|
|
|
int c;
|
2012-10-20 18:48:58 +02:00
|
|
|
sa_family_t family;
|
|
|
|
tor_addr_t addr;
|
2007-12-17 23:44:18 +01:00
|
|
|
question += strlen("ip-to-country/");
|
2012-10-20 18:48:58 +02:00
|
|
|
family = tor_addr_parse(&addr, question);
|
|
|
|
if (family != AF_INET && family != AF_INET6) {
|
|
|
|
*errmsg = "Invalid address family";
|
|
|
|
return -1;
|
2007-12-17 23:44:18 +01:00
|
|
|
}
|
2012-10-20 18:48:58 +02:00
|
|
|
if (!geoip_is_loaded(family)) {
|
|
|
|
*errmsg = "GeoIP data not loaded";
|
|
|
|
return -1;
|
2007-12-17 23:44:18 +01:00
|
|
|
}
|
2012-10-20 18:48:58 +02:00
|
|
|
if (family == AF_INET)
|
|
|
|
c = geoip_get_country_by_ipv4(tor_addr_to_ipv4h(&addr));
|
|
|
|
else /* AF_INET6 */
|
|
|
|
c = geoip_get_country_by_ipv6(tor_addr_to_in6(&addr));
|
|
|
|
*answer = tor_strdup(geoip_get_country_name(c));
|
2007-12-17 23:44:18 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-17 23:44:11 +01:00
|
|
|
|
2012-10-31 16:38:07 +01:00
|
|
|
/** Release all storage held by the GeoIP databases and country list. */
|
2015-09-07 19:22:33 +02:00
|
|
|
STATIC void
|
2007-12-18 22:27:08 +01:00
|
|
|
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);
|
2012-03-01 01:42:59 +01:00
|
|
|
if (geoip_ipv4_entries) {
|
2012-10-17 14:01:02 +02:00
|
|
|
SMARTLIST_FOREACH(geoip_ipv4_entries, geoip_ipv4_entry_t *, ent,
|
|
|
|
tor_free(ent));
|
2012-03-01 01:42:59 +01:00
|
|
|
smartlist_free(geoip_ipv4_entries);
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
2012-03-01 02:04:45 +01:00
|
|
|
if (geoip_ipv6_entries) {
|
2012-10-17 14:01:02 +02:00
|
|
|
SMARTLIST_FOREACH(geoip_ipv6_entries, geoip_ipv6_entry_t *, ent,
|
|
|
|
tor_free(ent));
|
2012-03-01 02:04:45 +01:00
|
|
|
smartlist_free(geoip_ipv6_entries);
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
|
|
|
geoip_countries = NULL;
|
|
|
|
country_idxplus1_by_lc_code = NULL;
|
2012-03-01 01:42:59 +01:00
|
|
|
geoip_ipv4_entries = NULL;
|
2012-03-01 02:04:45 +01:00
|
|
|
geoip_ipv6_entries = NULL;
|
2007-12-17 23:44:11 +01:00
|
|
|
}
|
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);
|
2013-07-16 20:04:36 +02:00
|
|
|
clientmap_entry_free(this);
|
2009-07-15 18:24:02 +02:00
|
|
|
}
|
|
|
|
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();
|
2013-02-11 20:52:28 +01:00
|
|
|
tor_free(bridge_stats_extrainfo);
|
2007-12-18 22:27:08 +01:00
|
|
|
}
|
|
|
|
|