Tor 0.4.9.0-alpha-dev
|
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. More...
#include "lib/geoip/geoip.h"
#include "lib/container/map.h"
#include "lib/container/order.h"
#include "lib/container/smartlist.h"
#include "lib/crypt_ops/crypto_digest.h"
#include "lib/ctime/di_ops.h"
#include "lib/encoding/binascii.h"
#include "lib/fs/files.h"
#include "lib/log/escape.h"
#include "lib/malloc/malloc.h"
#include "lib/net/address.h"
#include "lib/net/inaddr.h"
#include "lib/string/compat_ctype.h"
#include "lib/string/compat_string.h"
#include "lib/string/scanf.h"
#include "lib/string/util_string.h"
#include <stdio.h>
#include <string.h>
Go to the source code of this file.
Data Structures | |
struct | geoip_ipv4_entry_t |
struct | geoip_ipv6_entry_t |
Functions | |
static void | init_geoip_countries (void) |
const smartlist_t * | geoip_get_countries (void) |
country_t | geoip_get_country (const char *country) |
static void | geoip_add_entry (const tor_addr_t *low, const tor_addr_t *high, const char *country) |
STATIC int | geoip_parse_entry (const char *line, sa_family_t family) |
static int | geoip_ipv4_compare_entries_ (const void **_a, const void **_b) |
static int | geoip_ipv4_compare_key_to_entry_ (const void *_key, const void **_member) |
static int | geoip_ipv6_compare_entries_ (const void **_a, const void **_b) |
static int | geoip_ipv6_compare_key_to_entry_ (const void *_key, const void **_member) |
int | geoip_load_file (sa_family_t family, const char *filename, int severity) |
STATIC int | geoip_get_country_by_ipv4 (uint32_t ipaddr) |
STATIC int | geoip_get_country_by_ipv6 (const struct in6_addr *addr) |
int | geoip_get_country_by_addr (const tor_addr_t *addr) |
int | geoip_get_n_countries (void) |
const char * | geoip_get_country_name (country_t num) |
int | geoip_is_loaded (sa_family_t family) |
const char * | geoip_db_digest (sa_family_t family) |
STATIC void | clear_geoip_db (void) |
void | geoip_free_all (void) |
Variables | |
static smartlist_t * | geoip_countries = NULL |
static strmap_t * | country_idxplus1_by_lc_code = NULL |
static smartlist_t * | geoip_ipv4_entries = NULL |
static smartlist_t * | geoip_ipv6_entries = NULL |
static char | geoip_digest [DIGEST_LEN] |
static char | geoip6_digest [DIGEST_LEN] |
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.
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.
Definition in file geoip.c.
STATIC void clear_geoip_db | ( | void | ) |
Release all storage held by the GeoIP databases and country list.
Definition at line 484 of file geoip.c.
Referenced by geoip_free_all().
|
static |
const char * geoip_db_digest | ( | sa_family_t | family | ) |
void geoip_free_all | ( | void | ) |
const smartlist_t * geoip_get_countries | ( | void | ) |
Return a list of geoip_country_t for all known countries.
Definition at line 89 of file geoip.c.
Referenced by geoip_get_request_history().
country_t geoip_get_country | ( | const char * | country | ) |
int geoip_get_country_by_addr | ( | const tor_addr_t * | addr | ) |
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().
Definition at line 424 of file geoip.c.
Referenced by addr_is_in_cc_list(), getinfo_helper_geoip(), and routerset_contains2().
STATIC int geoip_get_country_by_ipv4 | ( | uint32_t | ipaddr | ) |
Given an IP address in host order, 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().
Definition at line 391 of file geoip.c.
Referenced by geoip_get_country_by_addr().
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().
Definition at line 407 of file geoip.c.
Referenced by geoip_get_country_by_addr().
const char * geoip_get_country_name | ( | country_t | num | ) |
Return the two-letter country code associated with the number num, or "??" for an unknown value.
Definition at line 447 of file geoip.c.
Referenced by addr_is_in_cc_list().
int geoip_get_n_countries | ( | void | ) |
Return the number of countries recognized by the GeoIP country list.
Definition at line 437 of file geoip.c.
Referenced by geoip_get_client_history().
|
static |
Sorting helper: return -1, 1, or 0 based on comparison of two geoip_ipv4_entry_t
|
static |
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_ipv4_entry_t
Definition at line 246 of file geoip.c.
Referenced by geoip_get_country_by_ipv4().
|
static |
Sorting helper: return -1, 1, or 0 based on comparison of two geoip_ipv6_entry_t
|
static |
bsearch helper: return -1, 1, or 0 based on comparison of an IPv6 (a pointer to a in6_addr) to a geoip_ipv6_entry_t
Definition at line 272 of file geoip.c.
Referenced by geoip_get_country_by_ipv6().
int geoip_is_loaded | ( | sa_family_t | family | ) |
Return true iff we have loaded a GeoIP database.
Definition at line 458 of file geoip.c.
Referenced by config_maybe_load_geoip_files_(), geoip_get_client_history(), and getinfo_helper_geoip().
int geoip_load_file | ( | sa_family_t | family, |
const char * | filename, | ||
int | severity | ||
) |
Clear appropriate GeoIP database, based on family, and reload it from the file filename. Return 0 on success, -1 on failure.
Recognized line formats for IPv4 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.
Recognized line format for IPv6 is: IPV6LOW,IPV6HIGH,CC where IPV6LOW and IPV6HIGH are IPv6 addresses and CC is a country code.
It also recognizes, and skips over, blank lines and lines that start with '#' (comments).
Definition at line 324 of file geoip.c.
Referenced by config_load_geoip_file_().
STATIC int geoip_parse_entry | ( | const char * | line, |
sa_family_t | family | ||
) |
Add an entry to the GeoIP table indicated by family, parsing it from line. The format is as for geoip_load_file().
|
static |
Set up a new list of geoip countries with no countries (yet) set in it, except for the unknown country.
Definition at line 291 of file geoip.c.
Referenced by geoip_get_countries(), geoip_get_n_countries(), geoip_load_file(), and geoip_parse_entry().
|
static |
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.
Definition at line 72 of file geoip.c.
Referenced by geoip_get_country().
|
static |
SHA1 digest of the IPv6 GeoIP file to include in extra-info descriptors.
Definition at line 85 of file geoip.c.
Referenced by geoip_db_digest(), and geoip_free_all().
|
static |
A list of geoip_country_t
Definition at line 68 of file geoip.c.
Referenced by clear_geoip_db(), geoip_get_countries(), geoip_get_country_name(), geoip_get_n_countries(), geoip_is_loaded(), geoip_load_file(), geoip_parse_entry(), and init_geoip_countries().
|
static |
SHA1 digest of the IPv4 GeoIP file to include in extra-info descriptors.
Definition at line 82 of file geoip.c.
Referenced by geoip_db_digest(), and geoip_free_all().
|
static |
List of all known geoip_ipv4_entry_t sorted by their respective ip_low values.
Definition at line 75 of file geoip.c.
Referenced by geoip_get_country_by_ipv4(), geoip_is_loaded(), geoip_load_file(), and geoip_parse_entry().
|
static |
List of all known geoip_ipv6_entry_t, sorted by their respective ip_low values.
Definition at line 78 of file geoip.c.
Referenced by geoip_get_country_by_ipv6(), geoip_is_loaded(), and geoip_parse_entry().