rendcache.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (c) 2015-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file rendcache.h
  5. * \brief Header file for rendcache.c
  6. **/
  7. #ifndef TOR_RENDCACHE_H
  8. #define TOR_RENDCACHE_H
  9. #include "or.h"
  10. #include "rendcommon.h"
  11. /** How old do we let hidden service descriptors get before discarding
  12. * them as too old? */
  13. #define REND_CACHE_MAX_AGE (2*24*60*60)
  14. /** How wrong do we assume our clock may be when checking whether hidden
  15. * services are too old or too new? */
  16. #define REND_CACHE_MAX_SKEW (24*60*60)
  17. /** How old do we keep an intro point failure entry in the failure cache? */
  18. #define REND_CACHE_FAILURE_MAX_AGE (5*60)
  19. /* Do not allow more than this many introduction points in a hidden service
  20. * descriptor */
  21. #define MAX_INTRO_POINTS 10
  22. /** A cached rendezvous descriptor. */
  23. typedef struct rend_cache_entry_t {
  24. size_t len; /**< Length of <b>desc</b> */
  25. time_t last_served; /**< When did we last write this one to somebody?
  26. * (HSDir only) */
  27. char *desc; /**< Service descriptor */
  28. rend_service_descriptor_t *parsed; /**< Parsed value of 'desc' */
  29. } rend_cache_entry_t;
  30. /* Introduction point failure type. */
  31. typedef struct rend_cache_failure_intro_t {
  32. /* When this intro point failure occured thus we allocated this object and
  33. * cache it. */
  34. time_t created_ts;
  35. rend_intro_point_failure_t failure_type;
  36. } rend_cache_failure_intro_t;
  37. /** Cache failure object indexed by service ID. */
  38. typedef struct rend_cache_failure_t {
  39. /* Contains rend_cache_failure_intro_t indexed by identity digest. */
  40. digestmap_t *intro_failures;
  41. } rend_cache_failure_t;
  42. typedef enum {
  43. REND_CACHE_TYPE_CLIENT = 1,
  44. REND_CACHE_TYPE_SERVICE = 2,
  45. } rend_cache_type_t;
  46. /* Return maximum lifetime in seconds of a cache entry. */
  47. static inline time_t
  48. rend_cache_max_entry_lifetime(void)
  49. {
  50. return REND_CACHE_MAX_AGE + REND_CACHE_MAX_SKEW;
  51. }
  52. void rend_cache_init(void);
  53. void rend_cache_clean(time_t now, rend_cache_type_t cache_type);
  54. void rend_cache_failure_clean(time_t now);
  55. size_t rend_cache_clean_v2_descs_as_dir(time_t cutoff);
  56. void rend_cache_purge(void);
  57. void rend_cache_free_all(void);
  58. int rend_cache_lookup_entry(const char *query, int version,
  59. rend_cache_entry_t **entry_out);
  60. int rend_cache_lookup_v2_desc_as_service(const char *query,
  61. rend_cache_entry_t **entry_out);
  62. int rend_cache_lookup_v2_desc_as_dir(const char *query, const char **desc);
  63. int rend_cache_store_v2_desc_as_dir(const char *desc);
  64. int rend_cache_store_v2_desc_as_service(const char *desc);
  65. int rend_cache_store_v2_desc_as_client(const char *desc,
  66. const char *desc_id_base32,
  67. const rend_data_t *rend_query,
  68. rend_cache_entry_t **entry);
  69. size_t rend_cache_get_total_allocation(void);
  70. void rend_cache_intro_failure_note(rend_intro_point_failure_t failure,
  71. const uint8_t *identity,
  72. const char *service_id);
  73. void rend_cache_failure_purge(void);
  74. void rend_cache_decrement_allocation(size_t n);
  75. void rend_cache_increment_allocation(size_t n);
  76. #ifdef RENDCACHE_PRIVATE
  77. STATIC size_t rend_cache_entry_allocation(const rend_cache_entry_t *e);
  78. STATIC void rend_cache_entry_free(rend_cache_entry_t *e);
  79. STATIC void rend_cache_failure_intro_entry_free(rend_cache_failure_intro_t
  80. *entry);
  81. STATIC void rend_cache_failure_entry_free(rend_cache_failure_t *entry);
  82. STATIC int cache_failure_intro_lookup(const uint8_t *identity,
  83. const char *service_id,
  84. rend_cache_failure_intro_t
  85. **intro_entry);
  86. STATIC rend_cache_failure_intro_t *rend_cache_failure_intro_entry_new(
  87. rend_intro_point_failure_t failure);
  88. STATIC rend_cache_failure_t *rend_cache_failure_entry_new(void);
  89. STATIC void rend_cache_failure_remove(rend_service_descriptor_t *desc);
  90. STATIC void cache_failure_intro_add(const uint8_t *identity,
  91. const char *service_id,
  92. rend_intro_point_failure_t failure);
  93. STATIC void validate_intro_point_failure(const rend_service_descriptor_t *desc,
  94. const char *service_id);
  95. STATIC void rend_cache_failure_entry_free_(void *entry);
  96. #ifdef TOR_UNIT_TESTS
  97. extern strmap_t *rend_cache;
  98. extern strmap_t *rend_cache_failure;
  99. extern digestmap_t *rend_cache_v2_dir;
  100. extern size_t rend_cache_total_allocation;
  101. #endif
  102. #endif
  103. #endif /* TOR_RENDCACHE_H */