Tor  0.4.7.0-alpha-dev
Data Structures | Macros | Functions | Variables
geoip.c File Reference

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_tgeoip_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)
 
int geoip_get_country_by_ipv4 (uint32_t ipaddr)
 
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_tgeoip_countries = NULL
 
static strmap_t * country_idxplus1_by_lc_code = NULL
 
static smartlist_tgeoip_ipv4_entries = NULL
 
static smartlist_tgeoip_ipv6_entries = NULL
 
static char geoip_digest [DIGEST_LEN]
 
static char geoip6_digest [DIGEST_LEN]
 

Detailed Description

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.

Function Documentation

◆ clear_geoip_db()

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().

◆ geoip_add_entry()

static void geoip_add_entry ( const tor_addr_t low,
const tor_addr_t high,
const char *  country 
)
static

Add an entry to a GeoIP table, mapping all IP addresses between low and high, inclusive, to the 2-letter country code country.

Definition at line 117 of file geoip.c.

◆ geoip_db_digest()

const char* geoip_db_digest ( sa_family_t  family)

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().

Definition at line 473 of file geoip.c.

◆ geoip_free_all()

void geoip_free_all ( void  )

Release all storage held in this file.

Definition at line 510 of file geoip.c.

◆ geoip_get_countries()

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().

◆ geoip_get_country()

country_t geoip_get_country ( const char *  country)

Return the index of the country's entry in the GeoIP country list if it is a valid 2-letter country code, otherwise return -1.

Definition at line 101 of file geoip.c.

◆ geoip_get_country_by_addr()

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(), and routerset_contains2().

◆ geoip_get_country_by_ipv4()

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(), and getinfo_helper_geoip().

◆ geoip_get_country_by_ipv6()

int geoip_get_country_by_ipv6 ( const struct in6_addr 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(), and getinfo_helper_geoip().

◆ geoip_get_country_name()

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().

◆ geoip_get_n_countries()

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().

◆ geoip_ipv4_compare_entries_()

static int geoip_ipv4_compare_entries_ ( const void **  _a,
const void **  _b 
)
static

Sorting helper: return -1, 1, or 0 based on comparison of two geoip_ipv4_entry_t

Definition at line 232 of file geoip.c.

◆ geoip_ipv4_compare_key_to_entry_()

static int geoip_ipv4_compare_key_to_entry_ ( const void *  _key,
const void **  _member 
)
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().

◆ geoip_ipv6_compare_entries_()

static int geoip_ipv6_compare_entries_ ( const void **  _a,
const void **  _b 
)
static

Sorting helper: return -1, 1, or 0 based on comparison of two geoip_ipv6_entry_t

Definition at line 262 of file geoip.c.

◆ geoip_ipv6_compare_key_to_entry_()

static int geoip_ipv6_compare_key_to_entry_ ( const void *  _key,
const void **  _member 
)
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().

◆ geoip_is_loaded()

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().

◆ geoip_load_file()

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_().

◆ geoip_parse_entry()

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().

Definition at line 163 of file geoip.c.

◆ init_geoip_countries()

static void init_geoip_countries ( void  )
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().

Variable Documentation

◆ country_idxplus1_by_lc_code

strmap_t* country_idxplus1_by_lc_code = NULL
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().

◆ geoip6_digest

char geoip6_digest[DIGEST_LEN]
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().

◆ geoip_countries

smartlist_t* geoip_countries = NULL
static

◆ geoip_digest

char geoip_digest[DIGEST_LEN]
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().

◆ geoip_ipv4_entries

smartlist_t* geoip_ipv4_entries = NULL
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().

◆ geoip_ipv6_entries

smartlist_t* geoip_ipv6_entries = NULL
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().