Tor  0.4.5.0-alpha-dev
geoip_stats.h
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file geoip_stats.h
9  * \brief Header file for geoip_stats.c.
10  **/
11 
12 #ifndef TOR_GEOIP_STATS_H
13 #define TOR_GEOIP_STATS_H
14 
15 #include "core/or/dos.h"
16 #include "ext/ht.h"
17 
18 /** Indicates an action that we might be noting geoip statistics on.
19  * Note that if we're noticing CONNECT, we're a bridge, and if we're noticing
20  * the others, we're not.
21  */
22 typedef enum {
23  /** We've noticed a connection as a bridge relay or entry guard. */
25  /** We've served a networkstatus consensus as a directory server. */
28 /** Indicates either a positive reply or a reason for rejectng a network
29  * status request that will be included in geoip statistics. */
30 typedef enum {
31  /** Request is answered successfully. */
33  /** V3 network status is not signed by a sufficient number of requested
34  * authorities. */
36  /** Requested network status object is unavailable. */
38  /** Requested network status not found. */
40  /** Network status has not been modified since If-Modified-Since time. */
42  /** Directory is busy. */
45 #define GEOIP_NS_RESPONSE_NUM 6
46 
47 /** Directory requests that we are measuring can be either direct or
48  * tunneled. */
49 typedef enum {
50  DIRREQ_DIRECT = 0,
51  DIRREQ_TUNNELED = 1,
53 
54 /** Possible states for either direct or tunneled directory requests that
55  * are relevant for determining network status download times. */
56 typedef enum {
57  /** Found that the client requests a network status; applies to both
58  * direct and tunneled requests; initial state of a request that we are
59  * measuring. */
61  /** Finished writing a network status to the directory connection;
62  * applies to both direct and tunneled requests; completes a direct
63  * request. */
65  /** END cell sent to circuit that initiated a tunneled request. */
67  /** Flushed last cell from queue of the circuit that initiated a
68  * tunneled request to the outbuf of the OR connection. */
70  /** Flushed last byte from buffer of the channel belonging to the
71  * circuit that initiated a tunneled request; completes a tunneled
72  * request. */
75 
76 /** Entry in a map from IP address to the last time we've seen an incoming
77  * connection from that IP address. Used by bridges only to track which
78  * countries have them blocked, or the DoS mitigation subsystem if enabled. */
79 typedef struct clientmap_entry_t {
80  HT_ENTRY(clientmap_entry_t) node;
81  tor_addr_t addr;
82  /* Name of pluggable transport used by this client. NULL if no
83  pluggable transport was used. */
84  char *transport_name;
85 
86  /** Time when we last saw this IP address, in MINUTES since the epoch.
87  *
88  * (This will run out of space around 4011 CE. If Tor is still in use around
89  * 4000 CE, please remember to add more bits to last_seen_in_minutes.) */
90  unsigned int last_seen_in_minutes:30;
91  unsigned int action:2;
92 
93  /* This object is used to keep some statistics per client address for the
94  * DoS mitigation subsystem. */
95  dos_client_stats_t dos_stats;
97 
98 int should_record_bridge_info(const or_options_t *options);
99 
101  const tor_addr_t *addr, const char *transport_name,
102  time_t now);
103 void geoip_remove_old_clients(time_t cutoff);
104 clientmap_entry_t *geoip_lookup_client(const tor_addr_t *addr,
105  const char *transport_name,
106  geoip_client_action_t action);
107 size_t geoip_client_cache_total_allocation(void);
108 size_t geoip_client_cache_handle_oom(time_t now, size_t min_remove_bytes);
109 
111 char *geoip_get_transport_history(void);
113  char **country_str, char **ipver_str);
114 char *geoip_get_request_history(void);
115 void geoip_stats_free_all(void);
116 
117 void geoip_start_dirreq(uint64_t dirreq_id, size_t response_size,
118  dirreq_type_t type);
119 void geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type,
120  dirreq_state_t new_state);
121 
122 void geoip_dirreq_stats_init(time_t now);
123 void geoip_reset_dirreq_stats(time_t now);
124 char *geoip_format_dirreq_stats(time_t now);
125 time_t geoip_dirreq_stats_write(time_t now);
126 void geoip_dirreq_stats_term(void);
127 void geoip_entry_stats_init(time_t now);
128 time_t geoip_entry_stats_write(time_t now);
129 void geoip_entry_stats_term(void);
130 void geoip_reset_entry_stats(time_t now);
131 char *geoip_format_entry_stats(time_t now);
132 void geoip_bridge_stats_init(time_t now);
133 char *geoip_format_bridge_stats(time_t now);
134 time_t geoip_bridge_stats_write(time_t now);
135 void geoip_bridge_stats_term(void);
136 const char *geoip_get_bridge_stats_extrainfo(time_t);
138 char *format_client_stats_heartbeat(time_t now);
139 
140 #endif /* !defined(TOR_GEOIP_STATS_H) */
tor_addr_t
Definition: address.h:69
geoip_get_transport_history
char * geoip_get_transport_history(void)
Definition: geoip_stats.c:577
geoip_bridge_stats_term
void geoip_bridge_stats_term(void)
Definition: geoip_stats.c:1072
DIRREQ_CIRC_QUEUE_FLUSHED
@ DIRREQ_CIRC_QUEUE_FLUSHED
Definition: geoip_stats.h:69
geoip_dirreq_stats_term
void geoip_dirreq_stats_term(void)
Definition: geoip_stats.c:957
geoip_format_entry_stats
char * geoip_format_entry_stats(time_t now)
Definition: geoip_stats.c:1345
geoip_client_action_t
geoip_client_action_t
Definition: geoip_stats.h:22
geoip_note_ns_response
void geoip_note_ns_response(geoip_ns_response_t response)
Definition: geoip_stats.c:393
geoip_format_dirreq_stats
char * geoip_format_dirreq_stats(time_t now)
Definition: geoip_stats.c:966
GEOIP_REJECT_NOT_MODIFIED
@ GEOIP_REJECT_NOT_MODIFIED
Definition: geoip_stats.h:41
dirreq_state_t
dirreq_state_t
Definition: geoip_stats.h:56
GEOIP_SUCCESS
@ GEOIP_SUCCESS
Definition: geoip_stats.h:32
DIRREQ_END_CELL_SENT
@ DIRREQ_END_CELL_SENT
Definition: geoip_stats.h:66
GEOIP_REJECT_NOT_FOUND
@ GEOIP_REJECT_NOT_FOUND
Definition: geoip_stats.h:39
GEOIP_REJECT_BUSY
@ GEOIP_REJECT_BUSY
Definition: geoip_stats.h:43
geoip_dirreq_stats_init
void geoip_dirreq_stats_init(time_t now)
Definition: geoip_stats.c:918
clientmap_entry_t
Definition: geoip_stats.h:79
GEOIP_REJECT_NOT_ENOUGH_SIGS
@ GEOIP_REJECT_NOT_ENOUGH_SIGS
Definition: geoip_stats.h:35
geoip_bridge_stats_write
time_t geoip_bridge_stats_write(time_t now)
Definition: geoip_stats.c:1237
should_record_bridge_info
int should_record_bridge_info(const or_options_t *options)
Definition: geoip_stats.c:112
geoip_get_bridge_stats_controller
char * geoip_get_bridge_stats_controller(time_t)
Definition: geoip_stats.c:1309
geoip_start_dirreq
void geoip_start_dirreq(uint64_t dirreq_id, size_t response_size, dirreq_type_t type)
Definition: geoip_stats.c:529
geoip_reset_entry_stats
void geoip_reset_entry_stats(time_t now)
Definition: geoip_stats.c:1327
geoip_format_bridge_stats
char * geoip_format_bridge_stats(time_t now)
Definition: geoip_stats.c:1146
GEOIP_CLIENT_CONNECT
@ GEOIP_CLIENT_CONNECT
Definition: geoip_stats.h:24
geoip_remove_old_clients
void geoip_remove_old_clients(time_t cutoff)
Definition: geoip_stats.c:291
GEOIP_REJECT_UNAVAILABLE
@ GEOIP_REJECT_UNAVAILABLE
Definition: geoip_stats.h:37
DIRREQ_IS_FOR_NETWORK_STATUS
@ DIRREQ_IS_FOR_NETWORK_STATUS
Definition: geoip_stats.h:60
DIRREQ_FLUSHING_DIR_CONN_FINISHED
@ DIRREQ_FLUSHING_DIR_CONN_FINISHED
Definition: geoip_stats.h:64
geoip_entry_stats_write
time_t geoip_entry_stats_write(time_t now)
Definition: geoip_stats.c:1372
geoip_note_client_seen
void geoip_note_client_seen(geoip_client_action_t action, const tor_addr_t *addr, const char *transport_name, time_t now)
Definition: geoip_stats.c:227
geoip_bridge_stats_init
void geoip_bridge_stats_init(time_t now)
Definition: geoip_stats.c:1064
geoip_reset_dirreq_stats
void geoip_reset_dirreq_stats(time_t now)
Definition: geoip_stats.c:925
geoip_stats_free_all
void geoip_stats_free_all(void)
Definition: geoip_stats.c:1402
geoip_change_dirreq_state
void geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type, dirreq_state_t new_state)
Definition: geoip_stats.c:550
geoip_get_bridge_stats_extrainfo
const char * geoip_get_bridge_stats_extrainfo(time_t)
Definition: geoip_stats.c:1300
GEOIP_CLIENT_NETWORKSTATUS
@ GEOIP_CLIENT_NETWORKSTATUS
Definition: geoip_stats.h:26
format_client_stats_heartbeat
char * format_client_stats_heartbeat(time_t now)
Definition: geoip_stats.c:1205
geoip_entry_stats_init
void geoip_entry_stats_init(time_t now)
Definition: geoip_stats.c:1320
geoip_ns_response_t
geoip_ns_response_t
Definition: geoip_stats.h:30
geoip_get_client_history
int geoip_get_client_history(geoip_client_action_t action, char **country_str, char **ipver_str)
Definition: geoip_stats.c:782
geoip_entry_stats_term
void geoip_entry_stats_term(void)
Definition: geoip_stats.c:1336
geoip_get_request_history
char * geoip_get_request_history(void)
Definition: geoip_stats.c:877
or_options_t
Definition: or_options_st.h:45
geoip_dirreq_stats_write
time_t geoip_dirreq_stats_write(time_t now)
Definition: geoip_stats.c:1029
DIRREQ_CHANNEL_BUFFER_FLUSHED
@ DIRREQ_CHANNEL_BUFFER_FLUSHED
Definition: geoip_stats.h:73
dirreq_type_t
dirreq_type_t
Definition: geoip_stats.h:49
dos_client_stats_t
Definition: dos.h:34
clientmap_entry_t::last_seen_in_minutes
unsigned int last_seen_in_minutes
Definition: geoip_stats.h:90