Tor  0.4.7.0-alpha-dev
dirserv.h
Go to the documentation of this file.
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 */
6 
7 /**
8  * \file dirserv.h
9  * \brief Header file for dirserv.c.
10  **/
11 
12 #ifndef TOR_DIRSERV_H
13 #define TOR_DIRSERV_H
14 
16 
18 
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,
27 #define dir_spool_source_bitfield_t ENUM_BF(dir_spool_source_t)
28 
29 /** Object to remember the identity of an object that we are spooling,
30  * or about to spool, in response to a directory request.
31  *
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.)
35  *
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.
41  */
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.
46  */
47  unsigned spool_eagerly : 1;
48  /**
49  * Tells us what kind of object to get, and how to look it up.
50  */
51  dir_spool_source_bitfield_t spool_source : 7;
52  /**
53  * Tells us the specific object to spool.
54  */
56  /**
57  * A large object that we're spooling. Holds a reference count. Only
58  * used when spool_eagerly is false.
59  */
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.
64  */
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 */
73 
75 
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);
82 
83 #ifdef HAVE_MODULE_DIRCACHE
84 /** Is the dircache module enabled? */
85 #define have_module_dircache() (1)
87 int directory_caches_dir_info(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) */
116 
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);
122 
123 void dirserv_free_all(void);
125 cached_dir_t *new_cached_dir(char *s, time_t published);
126 
127 spooled_resource_t *spooled_resource_new(dir_spool_source_t source,
128  const uint8_t *digest,
129  size_t digestlen);
131  struct consensus_cache_entry_t *entry);
133 #define spooled_resource_free(sp) \
134  FREE_AND_NULL(spooled_resource_t, spooled_resource_free_, (sp))
136  time_t cutoff,
137  int compression,
138  size_t *size_out,
139  int *n_expired_out);
142 
143 #endif /* !defined(TOR_DIRSERV_H) */
#define DIGEST256_LEN
Definition: digest_sizes.h:23
cached_dir_t * dirserv_get_consensus(const char *flavor_name)
Definition: dirserv.c:201
int directory_caches_dir_info(const or_options_t *options)
Definition: dirserv.c:94
int directory_caches_unknown_auth_certs(const or_options_t *options)
Definition: dirserv.c:79
int directory_permits_begindir_requests(const or_options_t *options)
Definition: dirserv.c:110
void dirserv_set_cached_consensus_networkstatus(const char *networkstatus, size_t networkstatus_len, const char *flavor_name, const common_digests_t *digests, const uint8_t *sha3_as_signed, time_t published)
Definition: dirserv.c:174
void cached_dir_decref(cached_dir_t *d)
Definition: dirserv.c:124
void dirserv_spool_sort(dir_connection_t *conn)
Definition: dirserv.c:697
cached_dir_t * new_cached_dir(char *s, time_t published)
Definition: dirserv.c:135
int dirserv_get_routerdesc_spool(smartlist_t *spools_out, const char *key, dir_spool_source_t source, int conn_is_encrypted, const char **msg_out)
Definition: dirserv.c:245
void dirserv_spool_remove_missing_and_guess_size(dir_connection_t *conn, time_t cutoff, int compression, size_t *size_out, int *n_expired_out)
Definition: dirserv.c:644
void spooled_resource_free_(spooled_resource_t *spooled)
Definition: dirserv.c:366
spooled_resource_t * spooled_resource_new_from_cache_entry(struct consensus_cache_entry_t *entry)
Definition: dirserv.c:345
void dir_conn_clear_spool(dir_connection_t *conn)
Definition: dircache_stub.c:36
void dirserv_free_all(void)
Definition: dircache_stub.c:78
dir_spool_source_t
Definition: dirserv.h:20
int connection_dirserv_flushed_some(dir_connection_t *conn)
Definition: dircache_stub.c:27
int dir_split_resource_into_spoolable(const char *resource, enum dir_spool_source_t source, smartlist_t *spool_out, int *compressed_out, int flags)
Definition: dirserv.c:212
Definition: conscache.c:32
dir_spool_source_bitfield_t spool_source
Definition: dirserv.h:51
unsigned spool_eagerly
Definition: dirserv.h:47
uint8_t digest[DIGEST256_LEN]
Definition: dirserv.h:55
off_t cached_dir_offset
Definition: dirserv.h:71
struct consensus_cache_entry_t * consensus_cache_entry
Definition: dirserv.h:65
struct cached_dir_t * cached_dir_ref
Definition: dirserv.h:60
Macros to implement mocking and selective exposure for the test code.
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127