tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

geoip_stats.h (5718B)


      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  /** An incoming ORPort connection */
     24  GEOIP_CLIENT_CONNECT = 0,
     25  /** We've served a networkstatus consensus as a directory server. */
     26  GEOIP_CLIENT_NETWORKSTATUS = 1,
     27 } geoip_client_action_t;
     28 /** Indicates either a positive reply or a reason for rejecting a network
     29 * status request that will be included in geoip statistics. */
     30 typedef enum {
     31  /** Request is answered successfully. */
     32  GEOIP_SUCCESS = 0,
     33  /** V3 network status is not signed by a sufficient number of requested
     34   * authorities. */
     35  GEOIP_REJECT_NOT_ENOUGH_SIGS = 1,
     36  /** Requested network status object is unavailable. */
     37  GEOIP_REJECT_UNAVAILABLE = 2,
     38  /** Requested network status not found. */
     39  GEOIP_REJECT_NOT_FOUND = 3,
     40  /** Network status has not been modified since If-Modified-Since time. */
     41  GEOIP_REJECT_NOT_MODIFIED = 4,
     42  /** Directory is busy. */
     43  GEOIP_REJECT_BUSY = 5,
     44  /** We began to serve the request, and when we feel we have finished
     45   * serving it we will note this with a GEOIP_SUCCESS call too. */
     46  GEOIP_SERVED = 6,
     47 } geoip_ns_response_t;
     48 #define GEOIP_NS_RESPONSE_NUM 7
     49 
     50 /** Directory requests that we are measuring can be either direct or
     51 * tunneled. */
     52 typedef enum {
     53  DIRREQ_DIRECT = 0,
     54  DIRREQ_TUNNELED = 1,
     55 } dirreq_type_t;
     56 
     57 /** Possible states for either direct or tunneled directory requests that
     58 * are relevant for determining network status download times. */
     59 typedef enum {
     60  /** Found that the client requests a network status; applies to both
     61   * direct and tunneled requests; initial state of a request that we are
     62   * measuring. */
     63  DIRREQ_IS_FOR_NETWORK_STATUS = 0,
     64  /** Finished writing a network status to the directory connection;
     65   * applies to both direct and tunneled requests; completes a direct
     66   * request. */
     67  DIRREQ_FLUSHING_DIR_CONN_FINISHED = 1,
     68  /** END cell sent to circuit that initiated a tunneled request. */
     69  DIRREQ_END_CELL_SENT = 2,
     70  /** Flushed last cell from queue of the circuit that initiated a
     71    * tunneled request to the outbuf of the OR connection. */
     72  DIRREQ_CIRC_QUEUE_FLUSHED = 3,
     73  /** Flushed last byte from buffer of the channel belonging to the
     74    * circuit that initiated a tunneled request; completes a tunneled
     75    * request. */
     76  DIRREQ_CHANNEL_BUFFER_FLUSHED = 4
     77 } dirreq_state_t;
     78 
     79 /** Entry in a map from IP address to the last time we've seen an incoming
     80 * connection from that IP address. Used by bridges only to track which
     81 * countries have them blocked, or the DoS mitigation subsystem if enabled. */
     82 typedef struct clientmap_entry_t {
     83  HT_ENTRY(clientmap_entry_t) node;
     84  tor_addr_t addr;
     85  /* Name of pluggable transport used by this client. NULL if no
     86     pluggable transport was used. */
     87  char *transport_name;
     88 
     89  /** Time when we last saw this IP address, in MINUTES since the epoch.
     90   *
     91   * (This will run out of space around 4011 CE.  If Tor is still in use around
     92   * 4000 CE, please remember to add more bits to last_seen_in_minutes.) */
     93  unsigned int last_seen_in_minutes:30;
     94  unsigned int action:2;
     95 
     96  /* This object is used to keep some statistics per client address for the
     97   * DoS mitigation subsystem. */
     98  dos_client_stats_t dos_stats;
     99 } clientmap_entry_t;
    100 
    101 int should_record_bridge_info(const or_options_t *options);
    102 
    103 void geoip_note_client_seen(geoip_client_action_t action,
    104                            const tor_addr_t *addr, const char *transport_name,
    105                            time_t now);
    106 void geoip_remove_old_clients(time_t cutoff);
    107 clientmap_entry_t *geoip_lookup_client(const tor_addr_t *addr,
    108                                       const char *transport_name,
    109                                       geoip_client_action_t action);
    110 size_t geoip_client_cache_total_allocation(void);
    111 size_t geoip_client_cache_handle_oom(time_t now, size_t min_remove_bytes);
    112 
    113 void geoip_note_ns_response(geoip_ns_response_t response);
    114 char *geoip_get_transport_history(void);
    115 int geoip_get_client_history(geoip_client_action_t action,
    116                             char **country_str, char **ipver_str);
    117 char *geoip_get_request_history(void);
    118 void geoip_stats_free_all(void);
    119 
    120 void geoip_start_dirreq(uint64_t dirreq_id, size_t response_size,
    121                        dirreq_type_t type);
    122 void geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type,
    123                               dirreq_state_t new_state);
    124 
    125 void geoip_dirreq_stats_init(time_t now);
    126 void geoip_reset_dirreq_stats(time_t now);
    127 char *geoip_format_dirreq_stats(time_t now);
    128 time_t geoip_dirreq_stats_write(time_t now);
    129 void geoip_dirreq_stats_term(void);
    130 void geoip_entry_stats_init(time_t now);
    131 time_t geoip_entry_stats_write(time_t now);
    132 void geoip_entry_stats_term(void);
    133 void geoip_reset_entry_stats(time_t now);
    134 char *geoip_format_entry_stats(time_t now);
    135 void geoip_bridge_stats_init(time_t now);
    136 char *geoip_format_bridge_stats(time_t now);
    137 time_t geoip_bridge_stats_write(time_t now);
    138 void geoip_bridge_stats_term(void);
    139 const char *geoip_get_bridge_stats_extrainfo(time_t);
    140 char *geoip_get_bridge_stats_controller(time_t);
    141 char *format_client_stats_heartbeat(time_t now);
    142 
    143 #endif /* !defined(TOR_GEOIP_STATS_H) */