1 /* Copyright (c) 2015-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief Header file for rendcache.c
9 #ifndef TOR_RENDCACHE_H
10 #define TOR_RENDCACHE_H
12 #include "core/or/or.h"
13 #include "feature/rend/rendcommon.h"
15 /** How old do we let hidden service descriptors get before discarding
17 #define REND_CACHE_MAX_AGE (2*24*60*60)
18 /** How wrong do we assume our clock may be when checking whether hidden
19 * services are too old or too new? */
20 #define REND_CACHE_MAX_SKEW (24*60*60)
21 /** How old do we keep an intro point failure entry in the failure cache? */
22 #define REND_CACHE_FAILURE_MAX_AGE (5*60)
24 /* Do not allow more than this many introduction points in a hidden service
26 #define MAX_INTRO_POINTS 10
28 /** A cached rendezvous descriptor. */
29 typedef struct rend_cache_entry_t
{
30 size_t len
; /**< Length of <b>desc</b> */
31 time_t last_served
; /**< When did we last write this one to somebody?
33 char *desc
; /**< Service descriptor */
34 rend_service_descriptor_t
*parsed
; /**< Parsed value of 'desc' */
37 /* Introduction point failure type. */
38 typedef struct rend_cache_failure_intro_t
{
39 /* When this intro point failure occurred thus we allocated this object and
42 rend_intro_point_failure_t failure_type
;
43 } rend_cache_failure_intro_t
;
45 /** Cache failure object indexed by service ID. */
46 typedef struct rend_cache_failure_t
{
47 /* Contains rend_cache_failure_intro_t indexed by identity digest. */
48 digestmap_t
*intro_failures
;
49 } rend_cache_failure_t
;
52 REND_CACHE_TYPE_CLIENT
= 1,
53 REND_CACHE_TYPE_SERVICE
= 2,
56 /* Return maximum lifetime in seconds of a cache entry. */
58 rend_cache_max_entry_lifetime(void)
60 return REND_CACHE_MAX_AGE
+ REND_CACHE_MAX_SKEW
;
63 void rend_cache_init(void);
64 void rend_cache_clean(time_t now
, rend_cache_type_t cache_type
);
65 void rend_cache_failure_clean(time_t now
);
66 size_t rend_cache_clean_v2_descs_as_dir(time_t cutoff
);
67 void rend_cache_purge(void);
68 void rend_cache_free_all(void);
69 int rend_cache_lookup_entry(const char *query
, int version
,
70 rend_cache_entry_t
**entry_out
);
71 int rend_cache_lookup_v2_desc_as_service(const char *query
,
72 rend_cache_entry_t
**entry_out
);
73 int rend_cache_lookup_v2_desc_as_dir(const char *query
, const char **desc
);
75 int rend_cache_store_v2_desc_as_dir(const char *desc
);
76 int rend_cache_store_v2_desc_as_service(const char *desc
);
77 int rend_cache_store_v2_desc_as_client(const char *desc
,
78 const char *desc_id_base32
,
79 const rend_data_t
*rend_query
,
80 rend_cache_entry_t
**entry
);
81 size_t rend_cache_get_total_allocation(void);
83 void rend_cache_intro_failure_note(rend_intro_point_failure_t failure
,
84 const uint8_t *identity
,
85 const char *service_id
);
86 void rend_cache_failure_purge(void);
87 void rend_cache_decrement_allocation(size_t n
);
88 void rend_cache_increment_allocation(size_t n
);
90 #ifdef RENDCACHE_PRIVATE
92 STATIC
size_t rend_cache_entry_allocation(const rend_cache_entry_t
*e
);
93 STATIC
void rend_cache_entry_free_(rend_cache_entry_t
*e
);
94 #define rend_cache_entry_free(e) \
95 FREE_AND_NULL(rend_cache_entry_t, rend_cache_entry_free_, (e))
96 STATIC
void rend_cache_failure_intro_entry_free_(rend_cache_failure_intro_t
98 #define rend_cache_failure_intro_entry_free(e) \
99 FREE_AND_NULL(rend_cache_failure_intro_t, \
100 rend_cache_failure_intro_entry_free_, (e))
101 STATIC
void rend_cache_failure_entry_free_(rend_cache_failure_t
*entry
);
102 #define rend_cache_failure_entry_free(e) \
103 FREE_AND_NULL(rend_cache_failure_t, \
104 rend_cache_failure_entry_free_, (e))
105 STATIC
int cache_failure_intro_lookup(const uint8_t *identity
,
106 const char *service_id
,
107 rend_cache_failure_intro_t
109 STATIC rend_cache_failure_intro_t
*rend_cache_failure_intro_entry_new(
110 rend_intro_point_failure_t failure
);
111 STATIC rend_cache_failure_t
*rend_cache_failure_entry_new(void);
112 STATIC
void rend_cache_failure_remove(rend_service_descriptor_t
*desc
);
113 STATIC
void cache_failure_intro_add(const uint8_t *identity
,
114 const char *service_id
,
115 rend_intro_point_failure_t failure
);
116 STATIC
void validate_intro_point_failure(const rend_service_descriptor_t
*desc
,
117 const char *service_id
);
119 STATIC
void rend_cache_failure_entry_free_void(void *entry
);
121 #ifdef TOR_UNIT_TESTS
122 extern strmap_t
*rend_cache
;
123 extern strmap_t
*rend_cache_failure
;
124 extern digestmap_t
*rend_cache_v2_dir
;
125 extern size_t rend_cache_total_allocation
;
126 #endif /* defined(TOR_UNIT_TESTS) */
127 #endif /* defined(RENDCACHE_PRIVATE) */
129 #endif /* !defined(TOR_RENDCACHE_H) */