Avoid crashing if we call num_usable_bridges() when bridges are not enabled
[tor/appveyor.git] / src / or / dirvote.h
blob72a35fea6d34d722dc8bcde4c5e2779aa5648e3d
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-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file dirvote.h
9 * \brief Header file for dirvote.c.
10 **/
12 #ifndef TOR_DIRVOTE_H
13 #define TOR_DIRVOTE_H
15 #include "testsupport.h"
18 * Ideally, assuming synced clocks, we should only need 1 second for each of:
19 * - Vote
20 * - Distribute
21 * - Consensus Publication
22 * As we can gather descriptors continuously.
23 * (Could we even go as far as publishing the previous consensus,
24 * in the same second that we vote for the next one?)
25 * But we're not there yet: these are the lowest working values at this time.
28 /** Lowest allowable value for VoteSeconds. */
29 #define MIN_VOTE_SECONDS 2
30 /** Lowest allowable value for VoteSeconds when TestingTorNetwork is 1 */
31 #define MIN_VOTE_SECONDS_TESTING 2
33 /** Lowest allowable value for DistSeconds. */
34 #define MIN_DIST_SECONDS 2
35 /** Lowest allowable value for DistSeconds when TestingTorNetwork is 1 */
36 #define MIN_DIST_SECONDS_TESTING 2
38 /** Lowest allowable voting interval. */
39 #define MIN_VOTE_INTERVAL 300
40 /** Lowest allowable voting interval when TestingTorNetwork is 1:
41 * Voting Interval can be:
42 * 10, 12, 15, 18, 20, 24, 25, 30, 36, 40, 45, 50, 60, ...
43 * Testing Initial Voting Interval can be:
44 * 5, 6, 8, 9, or any of the possible values for Voting Interval,
45 * as they both need to evenly divide 30 minutes.
46 * If clock desynchronisation is an issue, use an interval of at least:
47 * 18 * drift in seconds, to allow for a clock slop factor */
48 #define MIN_VOTE_INTERVAL_TESTING \
49 (((MIN_VOTE_SECONDS_TESTING)+(MIN_DIST_SECONDS_TESTING)+1)*2)
51 #define MIN_VOTE_INTERVAL_TESTING_INITIAL \
52 ((MIN_VOTE_SECONDS_TESTING)+(MIN_DIST_SECONDS_TESTING)+1)
54 /** The lowest consensus method that we currently support. */
55 #define MIN_SUPPORTED_CONSENSUS_METHOD 13
57 /** The highest consensus method that we currently support. */
58 #define MAX_SUPPORTED_CONSENSUS_METHOD 26
60 /** Lowest consensus method where microdesc consensuses omit any entry
61 * with no microdesc. */
62 #define MIN_METHOD_FOR_MANDATORY_MICRODESC 13
64 /** Lowest consensus method that contains "a" lines. */
65 #define MIN_METHOD_FOR_A_LINES 14
67 /** Lowest consensus method where microdescs may include a "p6" line. */
68 #define MIN_METHOD_FOR_P6_LINES 15
70 /** Lowest consensus method where microdescs may include an onion-key-ntor
71 * line */
72 #define MIN_METHOD_FOR_NTOR_KEY 16
74 /** Lowest consensus method that ensures that authorities output an
75 * Unmeasured=1 flag for unmeasured bandwidths */
76 #define MIN_METHOD_TO_CLIP_UNMEASURED_BW 17
78 /** Lowest consensus method where authorities may include an "id" line in
79 * microdescriptors. */
80 #define MIN_METHOD_FOR_ID_HASH_IN_MD 18
82 /** Lowest consensus method where we include "package" lines*/
83 #define MIN_METHOD_FOR_PACKAGE_LINES 19
85 /** Lowest consensus method where authorities may include
86 * GuardFraction information in microdescriptors. */
87 #define MIN_METHOD_FOR_GUARDFRACTION 20
89 /** Lowest consensus method where authorities may include an "id" line for
90 * ed25519 identities in microdescriptors. (Broken; see
91 * consensus_method_is_supported() for more info.) */
92 #define MIN_METHOD_FOR_ED25519_ID_IN_MD 21
94 /** Lowest consensus method where authorities vote on ed25519 ids and ensure
95 * ed25519 id consistency. */
96 #define MIN_METHOD_FOR_ED25519_ID_VOTING 22
98 /** Lowest consensus method where authorities may include a shared random
99 * value(s). */
100 #define MIN_METHOD_FOR_SHARED_RANDOM 23
102 /** Lowest consensus method where authorities drop all nodes that don't get
103 * the Valid flag. */
104 #define MIN_METHOD_FOR_EXCLUDING_INVALID_NODES 24
106 /** Lowest consensus method where authorities vote on required/recommended
107 * protocols. */
108 #define MIN_METHOD_FOR_RECOMMENDED_PROTOCOLS 25
110 /** Lowest consensus method where authorities add protocols to routerstatus
111 * entries. */
112 #define MIN_METHOD_FOR_RS_PROTOCOLS 25
114 /** Lowest consensus method where authorities initialize bandwidth weights to 1
115 * instead of 0. See #14881 */
116 #define MIN_METHOD_FOR_INIT_BW_WEIGHTS_ONE 26
118 /** Default bandwidth to clip unmeasured bandwidths to using method >=
119 * MIN_METHOD_TO_CLIP_UNMEASURED_BW. (This is not a consensus method; do not
120 * get confused with the above macros.) */
121 #define DEFAULT_MAX_UNMEASURED_BW_KB 20
123 void dirvote_free_all(void);
125 /* vote manipulation */
126 char *networkstatus_compute_consensus(smartlist_t *votes,
127 int total_authorities,
128 crypto_pk_t *identity_key,
129 crypto_pk_t *signing_key,
130 const char *legacy_identity_key_digest,
131 crypto_pk_t *legacy_signing_key,
132 consensus_flavor_t flavor);
133 int networkstatus_add_detached_signatures(networkstatus_t *target,
134 ns_detached_signatures_t *sigs,
135 const char *source,
136 int severity,
137 const char **msg_out);
138 char *networkstatus_get_detached_signatures(smartlist_t *consensuses);
139 void ns_detached_signatures_free(ns_detached_signatures_t *s);
141 /* cert manipulation */
142 authority_cert_t *authority_cert_dup(authority_cert_t *cert);
144 /* vote scheduling */
146 /** Scheduling information for a voting interval. */
147 typedef struct {
148 /** When do we generate and distribute our vote for this interval? */
149 time_t voting_starts;
150 /** When do we send an HTTP request for any votes that we haven't
151 * been posted yet?*/
152 time_t fetch_missing_votes;
153 /** When do we give up on getting more votes and generate a consensus? */
154 time_t voting_ends;
155 /** When do we send an HTTP request for any signatures we're expecting to
156 * see on the consensus? */
157 time_t fetch_missing_signatures;
158 /** When do we publish the consensus? */
159 time_t interval_starts;
161 /* True iff we have generated and distributed our vote. */
162 int have_voted;
163 /* True iff we've requested missing votes. */
164 int have_fetched_missing_votes;
165 /* True iff we have built a consensus and sent the signatures around. */
166 int have_built_consensus;
167 /* True iff we've fetched missing signatures. */
168 int have_fetched_missing_signatures;
169 /* True iff we have published our consensus. */
170 int have_published_consensus;
172 /* True iff this voting schedule was set on demand meaning not through the
173 * normal vote operation of a dirauth or when a consensus is set. This only
174 * applies to a directory authority that needs to recalculate the voting
175 * timings only for the first vote even though this object was initilized
176 * prior to voting. */
177 int created_on_demand;
178 } voting_schedule_t;
180 void dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out);
181 time_t dirvote_get_start_of_next_interval(time_t now,
182 int interval,
183 int offset);
184 void dirvote_recalculate_timing(const or_options_t *options, time_t now);
185 void dirvote_act(const or_options_t *options, time_t now);
186 time_t dirvote_get_next_valid_after_time(void);
188 /* invoked on timers and by outside triggers. */
189 struct pending_vote_t * dirvote_add_vote(const char *vote_body,
190 const char **msg_out,
191 int *status_out);
192 int dirvote_add_signatures(const char *detached_signatures_body,
193 const char *source,
194 const char **msg_out);
196 /* Item access */
197 MOCK_DECL(const char*, dirvote_get_pending_consensus,
198 (consensus_flavor_t flav));
199 MOCK_DECL(const char*, dirvote_get_pending_detached_signatures, (void));
201 #define DGV_BY_ID 1
202 #define DGV_INCLUDE_PENDING 2
203 #define DGV_INCLUDE_PREVIOUS 4
204 const cached_dir_t *dirvote_get_vote(const char *fp, int flags);
205 void set_routerstatus_from_routerinfo(routerstatus_t *rs,
206 node_t *node,
207 routerinfo_t *ri, time_t now,
208 int listbadexits);
209 networkstatus_t *
210 dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
211 authority_cert_t *cert);
213 microdesc_t *dirvote_create_microdescriptor(const routerinfo_t *ri,
214 int consensus_method);
215 ssize_t dirvote_format_microdesc_vote_line(char *out, size_t out_len,
216 const microdesc_t *md,
217 int consensus_method_low,
218 int consensus_method_high);
219 vote_microdesc_hash_t *dirvote_format_all_microdesc_vote_lines(
220 const routerinfo_t *ri,
221 time_t now,
222 smartlist_t *microdescriptors_out);
224 int vote_routerstatus_find_microdesc_hash(char *digest256_out,
225 const vote_routerstatus_t *vrs,
226 int method,
227 digest_algorithm_t alg);
228 document_signature_t *voter_get_sig_by_algorithm(
229 const networkstatus_voter_info_t *voter,
230 digest_algorithm_t alg);
232 #ifdef DIRVOTE_PRIVATE
233 STATIC int32_t dirvote_get_intermediate_param_value(
234 const smartlist_t *param_list,
235 const char *keyword,
236 int32_t default_val);
237 STATIC char *format_networkstatus_vote(crypto_pk_t *private_key,
238 networkstatus_t *v3_ns);
239 STATIC smartlist_t *dirvote_compute_params(smartlist_t *votes, int method,
240 int total_authorities);
241 STATIC char *compute_consensus_package_lines(smartlist_t *votes);
242 STATIC char *make_consensus_method_list(int low, int high, const char *sep);
243 STATIC int
244 networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G,
245 int64_t M, int64_t E, int64_t D,
246 int64_t T, int64_t weight_scale);
247 #endif /* defined(DIRVOTE_PRIVATE) */
249 #endif /* !defined(TOR_DIRVOTE_H) */