geoip_stats.h 5.4 KB

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