Add Coccinelle patch for replacing NULL/non-NULL tt_assert().
[tor.git] / src / or / hs_cache.h
blob2a4d2dbb2f0d0e67d1825755647fe5ccc61c98d5
1 /* Copyright (c) 2016-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file hs_cache.h
6 * \brief Header file for hs_cache.c
7 **/
9 #ifndef TOR_HS_CACHE_H
10 #define TOR_HS_CACHE_H
12 #include <stdint.h>
14 #include "crypto.h"
15 #include "crypto_ed25519.h"
16 #include "hs_common.h"
17 #include "hs_descriptor.h"
18 #include "rendcommon.h"
19 #include "torcert.h"
21 /* This is the maximum time an introduction point state object can stay in the
22 * client cache in seconds (2 mins or 120 seconds). */
23 #define HS_CACHE_CLIENT_INTRO_STATE_MAX_AGE (2 * 60)
25 /* Introduction point state. */
26 typedef struct hs_cache_intro_state_t {
27 /* When this entry was created and put in the cache. */
28 time_t created_ts;
30 /* Did it suffered a generic error? */
31 unsigned int error : 1;
33 /* Did it timed out? */
34 unsigned int timed_out : 1;
36 /* How many times we tried to reached it and it was unreachable. */
37 uint32_t unreachable_count;
38 } hs_cache_intro_state_t;
40 typedef struct hs_cache_client_intro_state_t {
41 /* Contains hs_cache_intro_state_t object indexed by introduction point
42 * authentication key. */
43 digest256map_t *intro_points;
44 } hs_cache_client_intro_state_t;
46 /* Descriptor representation on the directory side which is a subset of
47 * information that the HSDir can decode and serve it. */
48 typedef struct hs_cache_dir_descriptor_t {
49 /* This object is indexed using the blinded pubkey located in the plaintext
50 * data which is populated only once the descriptor has been successfully
51 * decoded and validated. This simply points to that pubkey. */
52 const uint8_t *key;
54 /* When does this entry has been created. Used to expire entries. */
55 time_t created_ts;
57 /* Descriptor plaintext information. Obviously, we can't decrypt the
58 * encrypted part of the descriptor. */
59 hs_desc_plaintext_data_t *plaintext_data;
61 /* Encoded descriptor which is basically in text form. It's a NUL terminated
62 * string thus safe to strlen(). */
63 char *encoded_desc;
64 } hs_cache_dir_descriptor_t;
66 /* Public API */
68 void hs_cache_init(void);
69 void hs_cache_free_all(void);
70 void hs_cache_clean_as_dir(time_t now);
71 size_t hs_cache_handle_oom(time_t now, size_t min_remove_bytes);
73 unsigned int hs_cache_get_max_descriptor_size(void);
75 /* Store and Lookup function. They are version agnostic that is depending on
76 * the requested version of the descriptor, it will be re-routed to the
77 * right function. */
78 int hs_cache_store_as_dir(const char *desc);
79 int hs_cache_lookup_as_dir(uint32_t version, const char *query,
80 const char **desc_out);
82 const hs_descriptor_t *
83 hs_cache_lookup_as_client(const ed25519_public_key_t *key);
84 int hs_cache_store_as_client(const char *desc_str,
85 const ed25519_public_key_t *identity_pk);
86 void hs_cache_clean_as_client(time_t now);
88 /* Client failure cache. */
89 void hs_cache_client_intro_state_note(const ed25519_public_key_t *service_pk,
90 const ed25519_public_key_t *auth_key,
91 rend_intro_point_failure_t failure);
92 const hs_cache_intro_state_t *hs_cache_client_intro_state_find(
93 const ed25519_public_key_t *service_pk,
94 const ed25519_public_key_t *auth_key);
95 void hs_cache_client_intro_state_clean(time_t now);
97 #ifdef HS_CACHE_PRIVATE
99 /** Represents a locally cached HS descriptor on a hidden service client. */
100 typedef struct hs_cache_client_descriptor_t {
101 /* This object is indexed using the service identity public key */
102 ed25519_public_key_t key;
104 /* When was this entry created. Used to expire entries. */
105 time_t created_ts;
107 /* The cached descriptor, this object is the owner. It can't be NULL. A
108 * cache object without a valid descriptor is not possible. */
109 hs_descriptor_t *desc;
111 /* Encoded descriptor in string form. Can't be NULL. */
112 char *encoded_desc;
113 } hs_cache_client_descriptor_t;
115 STATIC size_t cache_clean_v3_as_dir(time_t now, time_t global_cutoff);
117 STATIC hs_cache_client_descriptor_t *
118 lookup_v3_desc_as_client(const uint8_t *key);
120 #endif /* HS_CACHE_PRIVATE */
122 #endif /* TOR_HS_CACHE_H */