Tor  0.4.7.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-2021, 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) */
time_t geoip_entry_stats_write(time_t now)
Definition: geoip_stats.c:1373
int geoip_get_client_history(geoip_client_action_t action, char **country_str, char **ipver_str)
Definition: geoip_stats.c:784
void geoip_reset_entry_stats(time_t now)
Definition: geoip_stats.c:1328
char * geoip_format_bridge_stats(time_t now)
Definition: geoip_stats.c:1148
dirreq_type_t
Definition: geoip_stats.h:49
time_t geoip_dirreq_stats_write(time_t now)
Definition: geoip_stats.c:1031
dirreq_state_t
Definition: geoip_stats.h:56
@ DIRREQ_END_CELL_SENT
Definition: geoip_stats.h:66
@ DIRREQ_CIRC_QUEUE_FLUSHED
Definition: geoip_stats.h:69
@ DIRREQ_CHANNEL_BUFFER_FLUSHED
Definition: geoip_stats.h:73
@ DIRREQ_IS_FOR_NETWORK_STATUS
Definition: geoip_stats.h:60
@ DIRREQ_FLUSHING_DIR_CONN_FINISHED
Definition: geoip_stats.h:64
void geoip_dirreq_stats_init(time_t now)
Definition: geoip_stats.c:920
void geoip_bridge_stats_init(time_t now)
Definition: geoip_stats.c:1066
char * geoip_get_request_history(void)
Definition: geoip_stats.c:879
void geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type, dirreq_state_t new_state)
Definition: geoip_stats.c:552
void geoip_note_ns_response(geoip_ns_response_t response)
Definition: geoip_stats.c:395
void geoip_remove_old_clients(time_t cutoff)
Definition: geoip_stats.c:293
void geoip_bridge_stats_term(void)
Definition: geoip_stats.c:1074
void geoip_entry_stats_init(time_t now)
Definition: geoip_stats.c:1321
void geoip_dirreq_stats_term(void)
Definition: geoip_stats.c:959
geoip_client_action_t
Definition: geoip_stats.h:22
@ GEOIP_CLIENT_NETWORKSTATUS
Definition: geoip_stats.h:26
@ GEOIP_CLIENT_CONNECT
Definition: geoip_stats.h:24
char * geoip_format_dirreq_stats(time_t now)
Definition: geoip_stats.c:968
char * geoip_get_bridge_stats_controller(time_t)
Definition: geoip_stats.c:1310
void geoip_start_dirreq(uint64_t dirreq_id, size_t response_size, dirreq_type_t type)
Definition: geoip_stats.c:531
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:229
char * format_client_stats_heartbeat(time_t now)
Definition: geoip_stats.c:1207
char * geoip_get_transport_history(void)
Definition: geoip_stats.c:579
void geoip_reset_dirreq_stats(time_t now)
Definition: geoip_stats.c:927
char * geoip_format_entry_stats(time_t now)
Definition: geoip_stats.c:1346
const char * geoip_get_bridge_stats_extrainfo(time_t)
Definition: geoip_stats.c:1301
void geoip_stats_free_all(void)
Definition: geoip_stats.c:1403
void geoip_entry_stats_term(void)
Definition: geoip_stats.c:1337
int should_record_bridge_info(const or_options_t *options)
Definition: geoip_stats.c:112
time_t geoip_bridge_stats_write(time_t now)
Definition: geoip_stats.c:1238
geoip_ns_response_t
Definition: geoip_stats.h:30
@ GEOIP_REJECT_BUSY
Definition: geoip_stats.h:43
@ GEOIP_SUCCESS
Definition: geoip_stats.h:32
@ GEOIP_REJECT_NOT_FOUND
Definition: geoip_stats.h:39
@ GEOIP_REJECT_NOT_MODIFIED
Definition: geoip_stats.h:41
@ GEOIP_REJECT_NOT_ENOUGH_SIGS
Definition: geoip_stats.h:35
@ GEOIP_REJECT_UNAVAILABLE
Definition: geoip_stats.h:37
Definition: geoip_stats.h:79
unsigned int last_seen_in_minutes
Definition: geoip_stats.h:90