geoip.h 6.3 KB

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