geoip.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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-2016, 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 "testsupport.h"
  13. #include "dos.h"
  14. #ifdef GEOIP_PRIVATE
  15. STATIC int geoip_parse_entry(const char *line, sa_family_t family);
  16. STATIC int geoip_get_country_by_ipv4(uint32_t ipaddr);
  17. STATIC int geoip_get_country_by_ipv6(const struct in6_addr *addr);
  18. STATIC void clear_geoip_db(void);
  19. #endif
  20. /** Entry in a map from IP address to the last time we've seen an incoming
  21. * connection from that IP address. Used by bridges only to track which
  22. * countries have them blocked, or the DoS mitigation subsystem if enabled. */
  23. typedef struct clientmap_entry_t {
  24. HT_ENTRY(clientmap_entry_t) node;
  25. tor_addr_t addr;
  26. /* Name of pluggable transport used by this client. NULL if no
  27. pluggable transport was used. */
  28. char *transport_name;
  29. /** Time when we last saw this IP address, in MINUTES since the epoch.
  30. *
  31. * (This will run out of space around 4011 CE. If Tor is still in use around
  32. * 4000 CE, please remember to add more bits to last_seen_in_minutes.) */
  33. unsigned int last_seen_in_minutes:30;
  34. unsigned int action:2;
  35. /* This object is used to keep some statistics per client address for the
  36. * DoS mitigation subsystem. */
  37. dos_client_stats_t dos_stats;
  38. } clientmap_entry_t;
  39. int should_record_bridge_info(const or_options_t *options);
  40. int geoip_load_file(sa_family_t family, const char *filename);
  41. MOCK_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
  42. MOCK_DECL(int, geoip_get_n_countries, (void));
  43. const char *geoip_get_country_name(country_t num);
  44. MOCK_DECL(int, geoip_is_loaded, (sa_family_t family));
  45. const char *geoip_db_digest(sa_family_t family);
  46. MOCK_DECL(country_t, geoip_get_country, (const char *countrycode));
  47. void geoip_note_client_seen(geoip_client_action_t action,
  48. const tor_addr_t *addr, const char *transport_name,
  49. time_t now);
  50. void geoip_remove_old_clients(time_t cutoff);
  51. clientmap_entry_t *geoip_lookup_client(const tor_addr_t *addr,
  52. const char *transport_name,
  53. geoip_client_action_t action);
  54. size_t geoip_client_cache_total_allocation(void);
  55. size_t geoip_client_cache_handle_oom(time_t now, size_t min_remove_bytes);
  56. void geoip_note_ns_response(geoip_ns_response_t response);
  57. char *geoip_get_transport_history(void);
  58. int geoip_get_client_history(geoip_client_action_t action,
  59. char **country_str, char **ipver_str);
  60. char *geoip_get_request_history(void);
  61. int getinfo_helper_geoip(control_connection_t *control_conn,
  62. const char *question, char **answer,
  63. const char **errmsg);
  64. void geoip_free_all(void);
  65. void geoip_start_dirreq(uint64_t dirreq_id, size_t response_size,
  66. dirreq_type_t type);
  67. void geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type,
  68. dirreq_state_t new_state);
  69. void geoip_dirreq_stats_init(time_t now);
  70. void geoip_reset_dirreq_stats(time_t now);
  71. char *geoip_format_dirreq_stats(time_t now);
  72. time_t geoip_dirreq_stats_write(time_t now);
  73. void geoip_dirreq_stats_term(void);
  74. void geoip_entry_stats_init(time_t now);
  75. time_t geoip_entry_stats_write(time_t now);
  76. void geoip_entry_stats_term(void);
  77. void geoip_reset_entry_stats(time_t now);
  78. char *geoip_format_entry_stats(time_t now);
  79. void geoip_bridge_stats_init(time_t now);
  80. char *geoip_format_bridge_stats(time_t now);
  81. time_t geoip_bridge_stats_write(time_t now);
  82. void geoip_bridge_stats_term(void);
  83. const char *geoip_get_bridge_stats_extrainfo(time_t);
  84. char *geoip_get_bridge_stats_controller(time_t);
  85. char *format_client_stats_heartbeat(time_t now);
  86. #endif