Merge branch 'maint-0.4.8' into release-0.4.8
[tor.git] / src / feature / dircache / dirserv.h
blobcde38ff1b21c189e728c9a2556b20534e0377c1c
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-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file dirserv.h
9 * \brief Header file for dirserv.c.
10 **/
12 #ifndef TOR_DIRSERV_H
13 #define TOR_DIRSERV_H
15 struct ed25519_public_key_t;
17 #include "lib/testsupport/testsupport.h"
19 /** Ways to convert a spoolable_resource_t to a bunch of bytes. */
20 typedef enum dir_spool_source_t {
21 DIR_SPOOL_SERVER_BY_DIGEST=1, DIR_SPOOL_SERVER_BY_FP,
22 DIR_SPOOL_EXTRA_BY_DIGEST, DIR_SPOOL_EXTRA_BY_FP,
23 DIR_SPOOL_MICRODESC,
24 DIR_SPOOL_NETWORKSTATUS,
25 DIR_SPOOL_CONSENSUS_CACHE_ENTRY,
26 } dir_spool_source_t;
27 #define dir_spool_source_bitfield_t ENUM_BF(dir_spool_source_t)
29 /** Object to remember the identity of an object that we are spooling,
30 * or about to spool, in response to a directory request.
32 * (Why do we spool? Because some directory responses are very large,
33 * and we don't want to just shove the complete answer into the output
34 * buffer: that would take a ridiculous amount of RAM.)
36 * If the spooled resource is relatively small (like microdescriptors,
37 * descriptors, etc), we look them up by ID as needed, and add the whole
38 * thing onto the output buffer at once. If the spooled reseource is
39 * big (like networkstatus documents), we reference-count it, and add it
40 * a few K at a time.
42 typedef struct spooled_resource_t {
43 /**
44 * If true, we add the entire object to the outbuf. If false,
45 * we spool the object a few K at a time.
47 unsigned spool_eagerly : 1;
48 /**
49 * Tells us what kind of object to get, and how to look it up.
51 dir_spool_source_bitfield_t spool_source : 7;
52 /**
53 * Tells us the specific object to spool.
55 uint8_t digest[DIGEST256_LEN];
56 /**
57 * A large object that we're spooling. Holds a reference count. Only
58 * used when spool_eagerly is false.
60 struct cached_dir_t *cached_dir_ref;
61 /**
62 * A different kind of large object that we might be spooling. Also
63 * reference-counted. Also only used when spool_eagerly is false.
65 struct consensus_cache_entry_t *consensus_cache_entry;
66 const uint8_t *cce_body;
67 size_t cce_len;
68 /**
69 * The current offset into cached_dir or cce_body. Only used when
70 * spool_eagerly is false */
71 off_t cached_dir_offset;
72 } spooled_resource_t;
74 int connection_dirserv_flushed_some(dir_connection_t *conn);
76 enum dir_spool_source_t;
77 int dir_split_resource_into_spoolable(const char *resource,
78 enum dir_spool_source_t source,
79 smartlist_t *spool_out,
80 int *compressed_out,
81 int flags);
83 #ifdef HAVE_MODULE_DIRCACHE
84 /** Is the dircache module enabled? */
85 #define have_module_dircache() (1)
86 int directory_caches_unknown_auth_certs(const or_options_t *options);
87 int directory_caches_dir_info(const or_options_t *options);
88 int directory_permits_begindir_requests(const or_options_t *options);
89 MOCK_DECL(cached_dir_t *, dirserv_get_consensus, (const char *flavor_name));
90 void dirserv_set_cached_consensus_networkstatus(const char *consensus,
91 size_t consensus_len,
92 const char *flavor_name,
93 const common_digests_t *digests,
94 const uint8_t *sha3_as_signed,
95 time_t published);
96 #else /* !defined(HAVE_MODULE_DIRCACHE) */
97 #define have_module_dircache() (0)
98 #define directory_caches_unknown_auth_certs(opt) \
99 ((void)(opt), 0)
100 #define directory_caches_dir_info(opt) \
101 ((void)(opt), 0)
102 #define directory_permits_begindir_requests(opt) \
103 ((void)(opt), 0)
104 #define dirserv_get_consensus(flav) \
105 ((void)(flav), NULL)
106 #define dirserv_set_cached_consensus_networkstatus(a,b,c,d,e,f) \
107 STMT_BEGIN { \
108 (void)(a); \
109 (void)(b); \
110 (void)(c); \
111 (void)(d); \
112 (void)(e); \
113 (void)(f); \
114 } STMT_END
115 #endif /* defined(HAVE_MODULE_DIRCACHE) */
117 void dirserv_clear_old_networkstatuses(time_t cutoff);
118 int dirserv_get_routerdesc_spool(smartlist_t *spools_out, const char *key,
119 dir_spool_source_t source,
120 int conn_is_encrypted,
121 const char **msg_out);
123 void dirserv_free_all(void);
124 void cached_dir_decref(cached_dir_t *d);
125 cached_dir_t *new_cached_dir(char *s, time_t published);
127 spooled_resource_t *spooled_resource_new(dir_spool_source_t source,
128 const uint8_t *digest,
129 size_t digestlen);
130 spooled_resource_t *spooled_resource_new_from_cache_entry(
131 struct consensus_cache_entry_t *entry);
132 void spooled_resource_free_(spooled_resource_t *spooled);
133 #define spooled_resource_free(sp) \
134 FREE_AND_NULL(spooled_resource_t, spooled_resource_free_, (sp))
135 void dirserv_spool_remove_missing_and_guess_size(dir_connection_t *conn,
136 time_t cutoff,
137 int compression,
138 size_t *size_out,
139 int *n_expired_out);
140 void dirserv_spool_sort(dir_connection_t *conn);
141 void dir_conn_clear_spool(dir_connection_t *conn);
143 #endif /* !defined(TOR_DIRSERV_H) */