1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2009, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define DIRVOTE_PRIVATE
11 * \brief Functions to compute directory consensus, and schedule voting.
14 /** A consensus that we have built and are appending signatures to. Once it's
15 * time to publish it, it will become an active consensus if it accumulates
16 * enough signatures. */
17 typedef struct pending_consensus_t
{
18 /** The body of the consensus that we're currently building. Once we
19 * have it built, it goes into dirserv.c */
21 /** The parsed in-progress consensus document. */
22 networkstatus_t
*consensus
;
23 } pending_consensus_t
;
25 static int dirvote_add_signatures_to_all_pending_consensuses(
26 const char *detached_signatures_body
,
27 const char **msg_out
);
28 static int dirvote_add_signatures_to_pending_consensus(
29 pending_consensus_t
*pc
,
30 ns_detached_signatures_t
*sigs
,
31 const char **msg_out
);
32 static char *list_v3_auth_ids(void);
33 static void dirvote_fetch_missing_votes(void);
34 static void dirvote_fetch_missing_signatures(void);
35 static int dirvote_perform_vote(void);
36 static void dirvote_clear_votes(int all_votes
);
37 static int dirvote_compute_consensuses(void);
38 static int dirvote_publish_consensus(void);
39 static char *make_consensus_method_list(int low
, int high
, const char *sep
);
41 /** The highest consensus method that we currently support. */
42 #define MAX_SUPPORTED_CONSENSUS_METHOD 8
44 #define MIN_METHOD_FOR_PARAMS 7
46 /** Lowest consensus method that generates microdescriptors */
47 #define MIN_METHOD_FOR_MICRODESC 8
54 #define MICRODESC_LINE_LEN 80
56 /** Return a new string containing the string representation of the vote in
57 * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
58 * For v3 authorities. */
60 format_networkstatus_vote(crypto_pk_env_t
*private_signing_key
,
61 networkstatus_t
*v3_ns
)
65 const char *client_versions
= NULL
, *server_versions
= NULL
;
67 char fingerprint
[FINGERPRINT_LEN
+1];
68 char ipaddr
[INET_NTOA_BUF_LEN
];
69 char digest
[DIGEST_LEN
];
72 routerlist_t
*rl
= router_get_routerlist();
73 char *version_lines
= NULL
;
74 networkstatus_voter_info_t
*voter
;
76 tor_assert(private_signing_key
);
77 tor_assert(v3_ns
->type
== NS_TYPE_VOTE
|| v3_ns
->type
== NS_TYPE_OPINION
);
79 voter
= smartlist_get(v3_ns
->voters
, 0);
82 in
.s_addr
= htonl(addr
);
83 tor_inet_ntoa(&in
, ipaddr
, sizeof(ipaddr
));
85 base16_encode(fingerprint
, sizeof(fingerprint
),
86 v3_ns
->cert
->cache_info
.identity_digest
, DIGEST_LEN
);
87 client_versions
= v3_ns
->client_versions
;
88 server_versions
= v3_ns
->server_versions
;
90 if (client_versions
|| server_versions
) {
94 v_len
+= strlen(client_versions
);
96 v_len
+= strlen(server_versions
);
97 version_lines
= tor_malloc(v_len
);
99 if (client_versions
) {
100 tor_snprintf(cp
, v_len
-(cp
-version_lines
),
101 "client-versions %s\n", client_versions
);
105 tor_snprintf(cp
, v_len
-(cp
-version_lines
),
106 "server-versions %s\n", server_versions
);
108 version_lines
= tor_strdup("");
112 len
+= strlen(version_lines
);
113 len
+= (RS_ENTRY_LEN
+MICRODESC_LINE_LEN
)*smartlist_len(rl
->routers
);
114 len
+= v3_ns
->cert
->cache_info
.signed_descriptor_len
;
116 status
= tor_malloc(len
);
118 char published
[ISO_TIME_LEN
+1];
119 char va
[ISO_TIME_LEN
+1];
120 char fu
[ISO_TIME_LEN
+1];
121 char vu
[ISO_TIME_LEN
+1];
122 char *flags
= smartlist_join_strings(v3_ns
->known_flags
, " ", 0, NULL
);
124 authority_cert_t
*cert
= v3_ns
->cert
;
126 make_consensus_method_list(1, MAX_SUPPORTED_CONSENSUS_METHOD
, " ");
127 format_iso_time(published
, v3_ns
->published
);
128 format_iso_time(va
, v3_ns
->valid_after
);
129 format_iso_time(fu
, v3_ns
->fresh_until
);
130 format_iso_time(vu
, v3_ns
->valid_until
);
132 if (v3_ns
->net_params
)
133 params
= smartlist_join_strings(v3_ns
->net_params
, " ", 0, NULL
);
135 params
= tor_strdup("");
138 tor_snprintf(status
, len
,
139 "network-status-version 3\n"
141 "consensus-methods %s\n"
146 "voting-delay %d %d\n"
150 "dir-source %s %s %s %s %d %d\n"
152 v3_ns
->type
== NS_TYPE_VOTE
? "vote" : "opinion",
154 published
, va
, fu
, vu
,
155 v3_ns
->vote_seconds
, v3_ns
->dist_seconds
,
159 voter
->nickname
, fingerprint
, voter
->address
,
160 ipaddr
, voter
->dir_port
, voter
->or_port
, voter
->contact
);
165 outp
= status
+ strlen(status
);
168 if (!tor_digest_is_zero(voter
->legacy_id_digest
)) {
169 char fpbuf
[HEX_DIGEST_LEN
+1];
170 base16_encode(fpbuf
, sizeof(fpbuf
), voter
->legacy_id_digest
, DIGEST_LEN
);
171 tor_snprintf(outp
, endp
-outp
, "legacy-dir-key %s\n", fpbuf
);
172 outp
+= strlen(outp
);
175 tor_assert(outp
+ cert
->cache_info
.signed_descriptor_len
< endp
);
176 memcpy(outp
, cert
->cache_info
.signed_descriptor_body
,
177 cert
->cache_info
.signed_descriptor_len
);
179 outp
+= cert
->cache_info
.signed_descriptor_len
;
182 SMARTLIST_FOREACH_BEGIN(v3_ns
->routerstatus_list
, vote_routerstatus_t
*,
184 vote_microdesc_hash_t
*h
;
185 if (routerstatus_format_entry(outp
, endp
-outp
, &vrs
->status
,
186 vrs
->version
, NS_V3_VOTE
) < 0) {
187 log_warn(LD_BUG
, "Unable to print router status.");
190 outp
+= strlen(outp
);
192 for (h
= vrs
->microdesc
; h
; h
= h
->next
) {
193 size_t mlen
= strlen(h
->microdesc_hash_line
);
194 if (outp
+mlen
>= endp
) {
195 log_warn(LD_BUG
, "Can't fit microdesc line in vote.");
197 memcpy(outp
, h
->microdesc_hash_line
, mlen
+1);
198 outp
+= strlen(outp
);
200 } SMARTLIST_FOREACH_END(vrs
);
203 char signing_key_fingerprint
[FINGERPRINT_LEN
+1];
204 if (tor_snprintf(outp
, endp
-outp
, "directory-signature ")<0) {
205 log_warn(LD_BUG
, "Unable to start signature line.");
208 outp
+= strlen(outp
);
210 if (crypto_pk_get_fingerprint(private_signing_key
,
211 signing_key_fingerprint
, 0)<0) {
212 log_warn(LD_BUG
, "Unable to get fingerprint for signing key");
215 if (tor_snprintf(outp
, endp
-outp
, "%s %s\n", fingerprint
,
216 signing_key_fingerprint
)<0) {
217 log_warn(LD_BUG
, "Unable to end signature line.");
220 outp
+= strlen(outp
);
223 if (router_get_networkstatus_v3_hash(status
, digest
, DIGEST_SHA1
)<0)
225 note_crypto_pk_op(SIGN_DIR
);
226 if (router_append_dirobj_signature(outp
,endp
-outp
,digest
, DIGEST_LEN
,
227 private_signing_key
)<0) {
228 log_warn(LD_BUG
, "Unable to sign networkstatus vote.");
234 if (!(v
= networkstatus_parse_vote_from_string(status
, NULL
,
236 log_err(LD_BUG
,"Generated a networkstatus %s we couldn't parse: "
238 v3_ns
->type
== NS_TYPE_VOTE
? "vote" : "opinion", status
);
241 networkstatus_vote_free(v
);
249 tor_free(version_lines
);
254 * Consensus generation
257 /** Given a vote <b>vote</b> (not a consensus!), return its associated
258 * networkstatus_voter_info_t. */
259 static networkstatus_voter_info_t
*
260 get_voter(const networkstatus_t
*vote
)
263 tor_assert(vote
->type
== NS_TYPE_VOTE
);
264 tor_assert(vote
->voters
);
265 tor_assert(smartlist_len(vote
->voters
) == 1);
266 return smartlist_get(vote
->voters
, 0);
269 /** Return the signature made by <b>voter</b> using the algorithm
270 * <b>alg</b>, or NULL if none is found. */
271 document_signature_t
*
272 voter_get_sig_by_algorithm(const networkstatus_voter_info_t
*voter
,
273 digest_algorithm_t alg
)
277 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
283 /** Temporary structure used in constructing a list of dir-source entries
284 * for a consensus. One of these is generated for every vote, and one more
285 * for every legacy key in each vote. */
286 typedef struct dir_src_ent_t
{
292 /** Helper for sorting networkstatus_t votes (not consensuses) by the
293 * hash of their voters' identity digests. */
295 _compare_votes_by_authority_id(const void **_a
, const void **_b
)
297 const networkstatus_t
*a
= *_a
, *b
= *_b
;
298 return memcmp(get_voter(a
)->identity_digest
,
299 get_voter(b
)->identity_digest
, DIGEST_LEN
);
302 /** Helper: Compare the dir_src_ent_ts in *<b>_a</b> and *<b>_b</b> by
303 * their identity digests, and return -1, 0, or 1 depending on their
306 _compare_dir_src_ents_by_authority_id(const void **_a
, const void **_b
)
308 const dir_src_ent_t
*a
= *_a
, *b
= *_b
;
309 const networkstatus_voter_info_t
*a_v
= get_voter(a
->v
),
310 *b_v
= get_voter(b
->v
);
311 const char *a_id
, *b_id
;
312 a_id
= a
->is_legacy
? a_v
->legacy_id_digest
: a_v
->identity_digest
;
313 b_id
= b
->is_legacy
? b_v
->legacy_id_digest
: b_v
->identity_digest
;
315 return memcmp(a_id
, b_id
, DIGEST_LEN
);
318 /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
319 * that occurs more than <b>min</b> times. */
321 get_frequent_members(smartlist_t
*out
, smartlist_t
*in
, int min
)
325 SMARTLIST_FOREACH(in
, char *, cp
,
327 if (cur
&& !strcmp(cp
, cur
)) {
331 smartlist_add(out
, cur
);
337 smartlist_add(out
, cur
);
340 /** Given a sorted list of strings <b>lst</b>, return the member that appears
341 * most. Break ties in favor of later-occurring members. */
342 #define get_most_frequent_member(lst) \
343 smartlist_get_most_frequent_string(lst)
345 /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
346 * that come from the same routerinfo, with the same derived elements.
349 compare_vote_rs(const vote_routerstatus_t
*a
, const vote_routerstatus_t
*b
)
352 if ((r
= memcmp(a
->status
.identity_digest
, b
->status
.identity_digest
,
355 if ((r
= memcmp(a
->status
.descriptor_digest
, b
->status
.descriptor_digest
,
358 if ((r
= (int)(b
->status
.published_on
- a
->status
.published_on
)))
360 if ((r
= strcmp(b
->status
.nickname
, a
->status
.nickname
)))
362 if ((r
= (((int)b
->status
.addr
) - ((int)a
->status
.addr
))))
364 if ((r
= (((int)b
->status
.or_port
) - ((int)a
->status
.or_port
))))
366 if ((r
= (((int)b
->status
.dir_port
) - ((int)a
->status
.dir_port
))))
371 /** Helper for sorting routerlists based on compare_vote_rs. */
373 _compare_vote_rs(const void **_a
, const void **_b
)
375 const vote_routerstatus_t
*a
= *_a
, *b
= *_b
;
376 return compare_vote_rs(a
,b
);
379 /** Given a list of vote_routerstatus_t, all for the same router identity,
380 * return whichever is most frequent, breaking ties in favor of more
381 * recently published vote_routerstatus_t and in case of ties there,
382 * in favor of smaller descriptor digest.
384 static vote_routerstatus_t
*
385 compute_routerstatus_consensus(smartlist_t
*votes
, int consensus_method
,
386 char *microdesc_digest256_out
)
388 vote_routerstatus_t
*most
= NULL
, *cur
= NULL
;
389 int most_n
= 0, cur_n
= 0;
390 time_t most_published
= 0;
392 /* _compare_vote_rs() sorts the items by identity digest (all the same),
393 * then by SD digest. That way, if we have a tie that the published_on
394 * date cannot tie, we use the descriptor with the smaller digest.
396 smartlist_sort(votes
, _compare_vote_rs
);
397 SMARTLIST_FOREACH(votes
, vote_routerstatus_t
*, rs
,
399 if (cur
&& !compare_vote_rs(cur
, rs
)) {
402 if (cur_n
> most_n
||
403 (cur
&& cur_n
== most_n
&&
404 cur
->status
.published_on
> most_published
)) {
407 most_published
= cur
->status
.published_on
;
414 if (cur_n
> most_n
||
415 (cur
&& cur_n
== most_n
&& cur
->status
.published_on
> most_published
)) {
418 most_published
= cur
->status
.published_on
;
423 if (consensus_method
>= MIN_METHOD_FOR_MICRODESC
&&
424 microdesc_digest256_out
) {
425 smartlist_t
*digests
= smartlist_create();
426 const char *best_microdesc_digest
;
427 SMARTLIST_FOREACH_BEGIN(votes
, vote_routerstatus_t
*, rs
) {
428 char d
[DIGEST256_LEN
];
429 if (compare_vote_rs(rs
, most
))
431 if (!vote_routerstatus_find_microdesc_hash(d
, rs
, consensus_method
,
433 smartlist_add(digests
, tor_memdup(d
, sizeof(d
)));
434 } SMARTLIST_FOREACH_END(rs
);
435 smartlist_sort_digests256(digests
);
436 best_microdesc_digest
= smartlist_get_most_frequent_digest256(digests
);
437 if (best_microdesc_digest
)
438 memcpy(microdesc_digest256_out
, best_microdesc_digest
, DIGEST256_LEN
);
439 SMARTLIST_FOREACH(digests
, char *, cp
, tor_free(cp
));
440 smartlist_free(digests
);
446 /** Given a list of strings in <b>lst</b>, set the <b>len_out</b>-byte digest
447 * at <b>digest_out</b> to the hash of the concatenation of those strings,
448 * computed with the algorithm <b>alg</b>. */
450 hash_list_members(char *digest_out
, size_t len_out
,
451 smartlist_t
*lst
, digest_algorithm_t alg
)
453 crypto_digest_env_t
*d
;
454 if (alg
== DIGEST_SHA1
)
455 d
= crypto_new_digest_env();
457 d
= crypto_new_digest256_env(alg
);
458 SMARTLIST_FOREACH(lst
, const char *, cp
,
459 crypto_digest_add_bytes(d
, cp
, strlen(cp
)));
460 crypto_digest_get_digest(d
, digest_out
, len_out
);
461 crypto_free_digest_env(d
);
464 /** Sorting helper: compare two strings based on their values as base-ten
465 * positive integers. (Non-integers are treated as prior to all integers, and
466 * compared lexically.) */
468 _cmp_int_strings(const void **_a
, const void **_b
)
470 const char *a
= *_a
, *b
= *_b
;
471 int ai
= (int)tor_parse_long(a
, 10, 1, INT_MAX
, NULL
, NULL
);
472 int bi
= (int)tor_parse_long(b
, 10, 1, INT_MAX
, NULL
, NULL
);
476 if (ai
== 0) /* Parsing failed. */
484 /** Given a list of networkstatus_t votes, determine and return the number of
485 * the highest consensus method that is supported by 2/3 of the voters. */
487 compute_consensus_method(smartlist_t
*votes
)
489 smartlist_t
*all_methods
= smartlist_create();
490 smartlist_t
*acceptable_methods
= smartlist_create();
491 smartlist_t
*tmp
= smartlist_create();
492 int min
= (smartlist_len(votes
) * 2) / 3;
495 SMARTLIST_FOREACH(votes
, networkstatus_t
*, vote
,
497 tor_assert(vote
->supported_methods
);
498 smartlist_add_all(tmp
, vote
->supported_methods
);
499 smartlist_sort(tmp
, _cmp_int_strings
);
500 smartlist_uniq(tmp
, _cmp_int_strings
, NULL
);
501 smartlist_add_all(all_methods
, tmp
);
502 smartlist_clear(tmp
);
505 smartlist_sort(all_methods
, _cmp_int_strings
);
506 get_frequent_members(acceptable_methods
, all_methods
, min
);
507 n_ok
= smartlist_len(acceptable_methods
);
509 const char *best
= smartlist_get(acceptable_methods
, n_ok
-1);
510 result
= (int)tor_parse_long(best
, 10, 1, INT_MAX
, NULL
, NULL
);
515 smartlist_free(all_methods
);
516 smartlist_free(acceptable_methods
);
520 /** Return true iff <b>method</b> is a consensus method that we support. */
522 consensus_method_is_supported(int method
)
524 return (method
>= 1) && (method
<= MAX_SUPPORTED_CONSENSUS_METHOD
);
527 /** Return a newly allocated string holding the numbers between low and high
528 * (inclusive) that are supported consensus methods. */
530 make_consensus_method_list(int low
, int high
, const char *separator
)
537 lst
= smartlist_create();
538 for (i
= low
; i
<= high
; ++i
) {
539 if (!consensus_method_is_supported(i
))
541 tor_snprintf(b
, sizeof(b
), "%d", i
);
542 smartlist_add(lst
, tor_strdup(b
));
544 list
= smartlist_join_strings(lst
, separator
, 0, NULL
);
546 SMARTLIST_FOREACH(lst
, char *, cp
, tor_free(cp
));
551 /** Helper: given <b>lst</b>, a list of version strings such that every
552 * version appears once for every versioning voter who recommends it, return a
553 * newly allocated string holding the resulting client-versions or
554 * server-versions list. May change contents of <b>lst</b> */
556 compute_consensus_versions_list(smartlist_t
*lst
, int n_versioning
)
558 int min
= n_versioning
/ 2;
559 smartlist_t
*good
= smartlist_create();
561 sort_version_list(lst
, 0);
562 get_frequent_members(good
, lst
, min
);
563 result
= smartlist_join_strings(good
, ",", 0, NULL
);
564 smartlist_free(good
);
568 /** Helper: given a list of valid networkstatus_t, return a new string
569 * containing the contents of the consensus network parameter set.
572 dirvote_compute_params(smartlist_t
*votes
)
578 const char *cur_param
;
582 const int n_votes
= smartlist_len(votes
);
584 smartlist_t
*param_list
= smartlist_create();
586 /* We require that the parameter lists in the votes are well-formed: that
587 is, that their keywords are unique and sorted, and that their values are
588 between INT32_MIN and INT32_MAX inclusive. This should be guaranteed by
591 vals
= tor_malloc(sizeof(int)*n_votes
);
593 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
596 smartlist_add_all(param_list
, v
->net_params
);
597 } SMARTLIST_FOREACH_END(v
);
599 if (smartlist_len(param_list
) == 0) {
601 smartlist_free(param_list
);
605 smartlist_sort_strings(param_list
);
607 cur_param
= smartlist_get(param_list
, 0);
608 eq
= strchr(cur_param
, '=');
610 cur_param_len
= (int)(eq
+1 - cur_param
);
612 output
= smartlist_create();
614 SMARTLIST_FOREACH_BEGIN(param_list
, const char *, param
) {
615 const char *next_param
;
617 eq
= strchr(param
, '=');
618 tor_assert(i
<n_votes
);
619 vals
[i
++] = (int32_t)
620 tor_parse_long(eq
+1, 10, INT32_MIN
, INT32_MAX
, &ok
, NULL
);
623 if (param_sl_idx
+1 == smartlist_len(param_list
))
626 next_param
= smartlist_get(param_list
, param_sl_idx
+1);
627 if (!next_param
|| strncmp(next_param
, param
, cur_param_len
)) {
628 /* We've reached the end of a series. */
629 int32_t median
= median_int32(vals
, i
);
630 char *out_string
= tor_malloc(64+cur_param_len
);
631 memcpy(out_string
, param
, cur_param_len
);
632 tor_snprintf(out_string
+cur_param_len
,64, "%ld", (long)median
);
633 smartlist_add(output
, out_string
);
637 eq
= strchr(next_param
, '=');
638 cur_param_len
= (int)(eq
+1 - next_param
);
641 } SMARTLIST_FOREACH_END(param
);
643 result
= smartlist_join_strings(output
, " ", 0, NULL
);
644 SMARTLIST_FOREACH(output
, char *, cp
, tor_free(cp
));
645 smartlist_free(output
);
646 smartlist_free(param_list
);
651 /** Given a list of vote networkstatus_t in <b>votes</b>, our public
652 * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
653 * and the number of <b>total_authorities</b> that we believe exist in our
654 * voting quorum, generate the text of a new v3 consensus vote, and return the
655 * value in a newly allocated string.
657 * Note: this function DOES NOT check whether the votes are from
658 * recognized authorities. (dirvote_add_vote does that.) */
660 networkstatus_compute_consensus(smartlist_t
*votes
,
661 int total_authorities
,
662 crypto_pk_env_t
*identity_key
,
663 crypto_pk_env_t
*signing_key
,
664 const char *legacy_id_key_digest
,
665 crypto_pk_env_t
*legacy_signing_key
,
666 consensus_flavor_t flavor
)
670 int consensus_method
;
671 time_t valid_after
, fresh_until
, valid_until
;
672 int vote_seconds
, dist_seconds
;
673 char *client_versions
= NULL
, *server_versions
= NULL
;
675 const char *flavor_name
;
676 const routerstatus_format_type_t rs_format
=
677 flavor
== FLAV_NS
? NS_V3_CONSENSUS
: NS_V3_CONSENSUS_MICRODESC
;
679 tor_assert(flavor
== FLAV_NS
|| flavor
== FLAV_MICRODESC
);
680 tor_assert(total_authorities
>= smartlist_len(votes
));
682 flavor_name
= networkstatus_get_flavor_name(flavor
);
684 if (!smartlist_len(votes
)) {
685 log_warn(LD_DIR
, "Can't compute a consensus from no votes.");
688 flags
= smartlist_create();
690 consensus_method
= compute_consensus_method(votes
);
691 if (consensus_method_is_supported(consensus_method
)) {
692 log_info(LD_DIR
, "Generating consensus using method %d.",
695 log_warn(LD_DIR
, "The other authorities will use consensus method %d, "
696 "which I don't support. Maybe I should upgrade!",
698 consensus_method
= 1;
701 /* Compute medians of time-related things, and figure out how many
702 * routers we might need to talk about. */
704 int n_votes
= smartlist_len(votes
);
705 time_t *va_times
= tor_malloc(n_votes
* sizeof(time_t));
706 time_t *fu_times
= tor_malloc(n_votes
* sizeof(time_t));
707 time_t *vu_times
= tor_malloc(n_votes
* sizeof(time_t));
708 int *votesec_list
= tor_malloc(n_votes
* sizeof(int));
709 int *distsec_list
= tor_malloc(n_votes
* sizeof(int));
710 int n_versioning_clients
= 0, n_versioning_servers
= 0;
711 smartlist_t
*combined_client_versions
= smartlist_create();
712 smartlist_t
*combined_server_versions
= smartlist_create();
714 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
715 tor_assert(v
->type
== NS_TYPE_VOTE
);
716 va_times
[v_sl_idx
] = v
->valid_after
;
717 fu_times
[v_sl_idx
] = v
->fresh_until
;
718 vu_times
[v_sl_idx
] = v
->valid_until
;
719 votesec_list
[v_sl_idx
] = v
->vote_seconds
;
720 distsec_list
[v_sl_idx
] = v
->dist_seconds
;
721 if (v
->client_versions
) {
722 smartlist_t
*cv
= smartlist_create();
723 ++n_versioning_clients
;
724 smartlist_split_string(cv
, v
->client_versions
, ",",
725 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
726 sort_version_list(cv
, 1);
727 smartlist_add_all(combined_client_versions
, cv
);
728 smartlist_free(cv
); /* elements get freed later. */
730 if (v
->server_versions
) {
731 smartlist_t
*sv
= smartlist_create();
732 ++n_versioning_servers
;
733 smartlist_split_string(sv
, v
->server_versions
, ",",
734 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
735 sort_version_list(sv
, 1);
736 smartlist_add_all(combined_server_versions
, sv
);
737 smartlist_free(sv
); /* elements get freed later. */
739 SMARTLIST_FOREACH(v
->known_flags
, const char *, cp
,
740 smartlist_add(flags
, tor_strdup(cp
)));
741 } SMARTLIST_FOREACH_END(v
);
742 valid_after
= median_time(va_times
, n_votes
);
743 fresh_until
= median_time(fu_times
, n_votes
);
744 valid_until
= median_time(vu_times
, n_votes
);
745 vote_seconds
= median_int(votesec_list
, n_votes
);
746 dist_seconds
= median_int(distsec_list
, n_votes
);
748 tor_assert(valid_after
+MIN_VOTE_INTERVAL
<= fresh_until
);
749 tor_assert(fresh_until
+MIN_VOTE_INTERVAL
<= valid_until
);
750 tor_assert(vote_seconds
>= MIN_VOTE_SECONDS
);
751 tor_assert(dist_seconds
>= MIN_DIST_SECONDS
);
753 server_versions
= compute_consensus_versions_list(combined_server_versions
,
754 n_versioning_servers
);
755 client_versions
= compute_consensus_versions_list(combined_client_versions
,
756 n_versioning_clients
);
758 SMARTLIST_FOREACH(combined_server_versions
, char *, cp
, tor_free(cp
));
759 SMARTLIST_FOREACH(combined_client_versions
, char *, cp
, tor_free(cp
));
760 smartlist_free(combined_server_versions
);
761 smartlist_free(combined_client_versions
);
763 smartlist_sort_strings(flags
);
764 smartlist_uniq_strings(flags
);
769 tor_free(votesec_list
);
770 tor_free(distsec_list
);
773 chunks
= smartlist_create();
777 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
778 vu_buf
[ISO_TIME_LEN
+1];
780 format_iso_time(va_buf
, valid_after
);
781 format_iso_time(fu_buf
, fresh_until
);
782 format_iso_time(vu_buf
, valid_until
);
783 flaglist
= smartlist_join_strings(flags
, " ", 0, NULL
);
785 tor_snprintf(buf
, sizeof(buf
), "network-status-version 3%s%s\n"
786 "vote-status consensus\n",
787 flavor
== FLAV_NS
? "" : " ",
788 flavor
== FLAV_NS
? "" : flavor_name
);
790 smartlist_add(chunks
, tor_strdup(buf
));
792 if (consensus_method
>= 2) {
793 tor_snprintf(buf
, sizeof(buf
), "consensus-method %d\n",
795 smartlist_add(chunks
, tor_strdup(buf
));
798 tor_snprintf(buf
, sizeof(buf
),
802 "voting-delay %d %d\n"
803 "client-versions %s\n"
804 "server-versions %s\n"
806 va_buf
, fu_buf
, vu_buf
,
807 vote_seconds
, dist_seconds
,
808 client_versions
, server_versions
, flaglist
);
809 smartlist_add(chunks
, tor_strdup(buf
));
814 if (consensus_method
>= MIN_METHOD_FOR_PARAMS
) {
815 char *params
= dirvote_compute_params(votes
);
817 smartlist_add(chunks
, tor_strdup("params "));
818 smartlist_add(chunks
, params
);
819 smartlist_add(chunks
, tor_strdup("\n"));
823 /* Sort the votes. */
824 smartlist_sort(votes
, _compare_votes_by_authority_id
);
825 /* Add the authority sections. */
827 smartlist_t
*dir_sources
= smartlist_create();
828 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
829 dir_src_ent_t
*e
= tor_malloc_zero(sizeof(dir_src_ent_t
));
831 e
->digest
= get_voter(v
)->identity_digest
;
833 smartlist_add(dir_sources
, e
);
834 if (consensus_method
>= 3 &&
835 !tor_digest_is_zero(get_voter(v
)->legacy_id_digest
)) {
836 dir_src_ent_t
*e_legacy
= tor_malloc_zero(sizeof(dir_src_ent_t
));
838 e_legacy
->digest
= get_voter(v
)->legacy_id_digest
;
839 e_legacy
->is_legacy
= 1;
840 smartlist_add(dir_sources
, e_legacy
);
842 } SMARTLIST_FOREACH_END(v
);
843 smartlist_sort(dir_sources
, _compare_dir_src_ents_by_authority_id
);
845 SMARTLIST_FOREACH(dir_sources
, const dir_src_ent_t
*, e
,
849 char ip
[INET_NTOA_BUF_LEN
];
850 char fingerprint
[HEX_DIGEST_LEN
+1];
851 char votedigest
[HEX_DIGEST_LEN
+1];
852 networkstatus_t
*v
= e
->v
;
853 networkstatus_voter_info_t
*voter
= get_voter(v
);
856 tor_assert(consensus_method
>= 2);
858 in
.s_addr
= htonl(voter
->addr
);
859 tor_inet_ntoa(&in
, ip
, sizeof(ip
));
860 base16_encode(fingerprint
, sizeof(fingerprint
), e
->digest
, DIGEST_LEN
);
861 base16_encode(votedigest
, sizeof(votedigest
), voter
->vote_digest
,
864 tor_snprintf(buf
, sizeof(buf
),
865 "dir-source %s%s %s %s %s %d %d\n",
866 voter
->nickname
, e
->is_legacy
? "-legacy" : "",
867 fingerprint
, voter
->address
, ip
,
870 smartlist_add(chunks
, tor_strdup(buf
));
871 if (! e
->is_legacy
) {
872 tor_snprintf(buf
, sizeof(buf
),
877 smartlist_add(chunks
, tor_strdup(buf
));
880 SMARTLIST_FOREACH(dir_sources
, dir_src_ent_t
*, e
, tor_free(e
));
881 smartlist_free(dir_sources
);
884 /* Add the actual router entries. */
886 int *index
; /* index[j] is the current index into votes[j]. */
887 int *size
; /* size[j] is the number of routerstatuses in votes[j]. */
888 int *flag_counts
; /* The number of voters that list flag[j] for the
889 * currently considered router. */
891 smartlist_t
*matching_descs
= smartlist_create();
892 smartlist_t
*chosen_flags
= smartlist_create();
893 smartlist_t
*versions
= smartlist_create();
894 smartlist_t
*exitsummaries
= smartlist_create();
895 uint32_t *bandwidths
= tor_malloc(sizeof(uint32_t) * smartlist_len(votes
));
896 uint32_t *measured_bws
= tor_malloc(sizeof(uint32_t) *
897 smartlist_len(votes
));
901 int *n_voter_flags
; /* n_voter_flags[j] is the number of flags that
902 * votes[j] knows about. */
903 int *n_flag_voters
; /* n_flag_voters[f] is the number of votes that care
905 int **flag_map
; /* flag_map[j][b] is an index f such that flag_map[f]
906 * is the same flag as votes[j]->known_flags[b]. */
907 int *named_flag
; /* Index of the flag "Named" for votes[j] */
908 int *unnamed_flag
; /* Index of the flag "Unnamed" for votes[j] */
909 int chosen_named_idx
, chosen_unnamed_idx
;
911 strmap_t
*name_to_id_map
= strmap_new();
912 char conflict
[DIGEST_LEN
];
913 char unknown
[DIGEST_LEN
];
914 memset(conflict
, 0, sizeof(conflict
));
915 memset(unknown
, 0xff, sizeof(conflict
));
917 index
= tor_malloc_zero(sizeof(int)*smartlist_len(votes
));
918 size
= tor_malloc_zero(sizeof(int)*smartlist_len(votes
));
919 n_voter_flags
= tor_malloc_zero(sizeof(int) * smartlist_len(votes
));
920 n_flag_voters
= tor_malloc_zero(sizeof(int) * smartlist_len(flags
));
921 flag_map
= tor_malloc_zero(sizeof(int*) * smartlist_len(votes
));
922 named_flag
= tor_malloc_zero(sizeof(int) * smartlist_len(votes
));
923 unnamed_flag
= tor_malloc_zero(sizeof(int) * smartlist_len(votes
));
924 for (i
= 0; i
< smartlist_len(votes
); ++i
)
925 unnamed_flag
[i
] = named_flag
[i
] = -1;
926 chosen_named_idx
= smartlist_string_pos(flags
, "Named");
927 chosen_unnamed_idx
= smartlist_string_pos(flags
, "Unnamed");
929 /* Build the flag index. */
930 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
,
932 flag_map
[v_sl_idx
] = tor_malloc_zero(
933 sizeof(int)*smartlist_len(v
->known_flags
));
934 SMARTLIST_FOREACH(v
->known_flags
, const char *, fl
,
936 int p
= smartlist_string_pos(flags
, fl
);
938 flag_map
[v_sl_idx
][fl_sl_idx
] = p
;
940 if (!strcmp(fl
, "Named"))
941 named_flag
[v_sl_idx
] = fl_sl_idx
;
942 if (!strcmp(fl
, "Unnamed"))
943 unnamed_flag
[v_sl_idx
] = fl_sl_idx
;
945 n_voter_flags
[v_sl_idx
] = smartlist_len(v
->known_flags
);
946 size
[v_sl_idx
] = smartlist_len(v
->routerstatus_list
);
949 /* Named and Unnamed get treated specially */
950 if (consensus_method
>= 2) {
951 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
,
954 if (named_flag
[v_sl_idx
]<0)
956 nf
= U64_LITERAL(1) << named_flag
[v_sl_idx
];
957 SMARTLIST_FOREACH(v
->routerstatus_list
, vote_routerstatus_t
*, rs
,
959 if ((rs
->flags
& nf
) != 0) {
960 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
962 /* We have no name officially mapped to this digest. */
963 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
,
964 rs
->status
.identity_digest
);
965 } else if (d
!= conflict
&&
966 memcmp(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
967 /* Authorities disagree about this nickname. */
968 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
970 /* It's already a conflict, or it's already this ID. */
975 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
,
978 if (unnamed_flag
[v_sl_idx
]<0)
980 uf
= U64_LITERAL(1) << unnamed_flag
[v_sl_idx
];
981 SMARTLIST_FOREACH(v
->routerstatus_list
, vote_routerstatus_t
*, rs
,
983 if ((rs
->flags
& uf
) != 0) {
984 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
985 if (d
== conflict
|| d
== unknown
) {
986 /* Leave it alone; we know what it is. */
988 /* We have no name officially mapped to this digest. */
989 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, unknown
);
990 } else if (!memcmp(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
991 /* Authorities disagree about this nickname. */
992 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
994 /* It's mapped to a different name. */
1001 /* Now go through all the votes */
1002 flag_counts
= tor_malloc(sizeof(int) * smartlist_len(flags
));
1004 vote_routerstatus_t
*rs
;
1005 routerstatus_t rs_out
;
1006 const char *lowest_id
= NULL
;
1007 const char *chosen_version
;
1008 const char *chosen_name
= NULL
;
1009 int exitsummary_disagreement
= 0;
1010 int is_named
= 0, is_unnamed
= 0, is_running
= 0;
1011 int naming_conflict
= 0;
1015 char microdesc_digest
[DIGEST256_LEN
];
1017 /* Of the next-to-be-considered digest in each voter, which is first? */
1018 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
, {
1019 if (index
[v_sl_idx
] < size
[v_sl_idx
]) {
1020 rs
= smartlist_get(v
->routerstatus_list
, index
[v_sl_idx
]);
1022 memcmp(rs
->status
.identity_digest
, lowest_id
, DIGEST_LEN
) < 0)
1023 lowest_id
= rs
->status
.identity_digest
;
1026 if (!lowest_id
) /* we're out of routers. */
1029 memset(flag_counts
, 0, sizeof(int)*smartlist_len(flags
));
1030 smartlist_clear(matching_descs
);
1031 smartlist_clear(chosen_flags
);
1032 smartlist_clear(versions
);
1036 /* Okay, go through all the entries for this digest. */
1037 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1038 if (index
[v_sl_idx
] >= size
[v_sl_idx
])
1039 continue; /* out of entries. */
1040 rs
= smartlist_get(v
->routerstatus_list
, index
[v_sl_idx
]);
1041 if (memcmp(rs
->status
.identity_digest
, lowest_id
, DIGEST_LEN
))
1042 continue; /* doesn't include this router. */
1043 /* At this point, we know that we're looking at a routerstatus with
1044 * identity "lowest".
1049 smartlist_add(matching_descs
, rs
);
1050 if (rs
->version
&& rs
->version
[0])
1051 smartlist_add(versions
, rs
->version
);
1053 /* Tally up all the flags. */
1054 for (i
= 0; i
< n_voter_flags
[v_sl_idx
]; ++i
) {
1055 if (rs
->flags
& (U64_LITERAL(1) << i
))
1056 ++flag_counts
[flag_map
[v_sl_idx
][i
]];
1058 if (rs
->flags
& (U64_LITERAL(1) << named_flag
[v_sl_idx
])) {
1059 if (chosen_name
&& strcmp(chosen_name
, rs
->status
.nickname
)) {
1060 log_notice(LD_DIR
, "Conflict on naming for router: %s vs %s",
1061 chosen_name
, rs
->status
.nickname
);
1062 naming_conflict
= 1;
1064 chosen_name
= rs
->status
.nickname
;
1067 /* count bandwidths */
1068 if (rs
->status
.has_measured_bw
)
1069 measured_bws
[num_mbws
++] = rs
->status
.measured_bw
;
1071 if (rs
->status
.has_bandwidth
)
1072 bandwidths
[num_bandwidths
++] = rs
->status
.bandwidth
;
1073 } SMARTLIST_FOREACH_END(v
);
1075 /* We don't include this router at all unless more than half of
1076 * the authorities we believe in list it. */
1077 if (n_listing
<= total_authorities
/2)
1080 /* Figure out the most popular opinion of what the most recent
1081 * routerinfo and its contents are. */
1082 memset(microdesc_digest
, 0, sizeof(microdesc_digest
));
1083 rs
= compute_routerstatus_consensus(matching_descs
, consensus_method
,
1085 /* Copy bits of that into rs_out. */
1086 tor_assert(!memcmp(lowest_id
, rs
->status
.identity_digest
, DIGEST_LEN
));
1087 memcpy(rs_out
.identity_digest
, lowest_id
, DIGEST_LEN
);
1088 memcpy(rs_out
.descriptor_digest
, rs
->status
.descriptor_digest
,
1090 rs_out
.addr
= rs
->status
.addr
;
1091 rs_out
.published_on
= rs
->status
.published_on
;
1092 rs_out
.dir_port
= rs
->status
.dir_port
;
1093 rs_out
.or_port
= rs
->status
.or_port
;
1094 rs_out
.has_bandwidth
= 0;
1095 rs_out
.has_exitsummary
= 0;
1097 if (chosen_name
&& !naming_conflict
) {
1098 strlcpy(rs_out
.nickname
, chosen_name
, sizeof(rs_out
.nickname
));
1100 strlcpy(rs_out
.nickname
, rs
->status
.nickname
, sizeof(rs_out
.nickname
));
1103 if (consensus_method
== 1) {
1104 is_named
= chosen_named_idx
>= 0 &&
1105 (!naming_conflict
&& flag_counts
[chosen_named_idx
]);
1107 const char *d
= strmap_get_lc(name_to_id_map
, rs_out
.nickname
);
1109 is_named
= is_unnamed
= 0;
1110 } else if (!memcmp(d
, lowest_id
, DIGEST_LEN
)) {
1111 is_named
= 1; is_unnamed
= 0;
1113 is_named
= 0; is_unnamed
= 1;
1117 /* Set the flags. */
1118 smartlist_add(chosen_flags
, (char*)"s"); /* for the start of the line. */
1119 SMARTLIST_FOREACH(flags
, const char *, fl
,
1121 if (!strcmp(fl
, "Named")) {
1123 smartlist_add(chosen_flags
, (char*)fl
);
1124 } else if (!strcmp(fl
, "Unnamed") && consensus_method
>= 2) {
1126 smartlist_add(chosen_flags
, (char*)fl
);
1128 if (flag_counts
[fl_sl_idx
] > n_flag_voters
[fl_sl_idx
]/2) {
1129 smartlist_add(chosen_flags
, (char*)fl
);
1130 if (!strcmp(fl
, "Running"))
1136 /* Starting with consensus method 4 we do not list servers
1137 * that are not running in a consensus. See Proposal 138 */
1138 if (consensus_method
>= 4 && !is_running
)
1141 /* Pick the version. */
1142 if (smartlist_len(versions
)) {
1143 sort_version_list(versions
, 0);
1144 chosen_version
= get_most_frequent_member(versions
);
1146 chosen_version
= NULL
;
1149 /* Pick a bandwidth */
1150 if (consensus_method
>= 6 && num_mbws
> 2) {
1151 rs_out
.has_bandwidth
= 1;
1152 rs_out
.bandwidth
= median_uint32(measured_bws
, num_mbws
);
1153 } else if (consensus_method
>= 5 && num_bandwidths
> 0) {
1154 rs_out
.has_bandwidth
= 1;
1155 rs_out
.bandwidth
= median_uint32(bandwidths
, num_bandwidths
);
1158 /* Ok, we already picked a descriptor digest we want to list
1159 * previously. Now we want to use the exit policy summary from
1160 * that descriptor. If everybody plays nice all the voters who
1161 * listed that descriptor will have the same summary. If not then
1162 * something is fishy and we'll use the most common one (breaking
1163 * ties in favor of lexicographically larger one (only because it
1164 * lets me reuse more existing code.
1166 * The other case that can happen is that no authority that voted
1167 * for that descriptor has an exit policy summary. That's
1168 * probably quite unlikely but can happen. In that case we use
1169 * the policy that was most often listed in votes, again breaking
1170 * ties like in the previous case.
1172 if (consensus_method
>= 5) {
1173 /* Okay, go through all the votes for this router. We prepared
1174 * that list previously */
1175 const char *chosen_exitsummary
= NULL
;
1176 smartlist_clear(exitsummaries
);
1177 SMARTLIST_FOREACH(matching_descs
, vote_routerstatus_t
*, vsr
, {
1178 /* Check if the vote where this status comes from had the
1179 * proper descriptor */
1180 tor_assert(!memcmp(rs_out
.identity_digest
,
1181 vsr
->status
.identity_digest
,
1183 if (vsr
->status
.has_exitsummary
&&
1184 !memcmp(rs_out
.descriptor_digest
,
1185 vsr
->status
.descriptor_digest
,
1187 tor_assert(vsr
->status
.exitsummary
);
1188 smartlist_add(exitsummaries
, vsr
->status
.exitsummary
);
1189 if (!chosen_exitsummary
) {
1190 chosen_exitsummary
= vsr
->status
.exitsummary
;
1191 } else if (strcmp(chosen_exitsummary
, vsr
->status
.exitsummary
)) {
1192 /* Great. There's disagreement among the voters. That
1193 * really shouldn't be */
1194 exitsummary_disagreement
= 1;
1199 if (exitsummary_disagreement
) {
1200 char id
[HEX_DIGEST_LEN
+1];
1201 char dd
[HEX_DIGEST_LEN
+1];
1202 base16_encode(id
, sizeof(dd
), rs_out
.identity_digest
, DIGEST_LEN
);
1203 base16_encode(dd
, sizeof(dd
), rs_out
.descriptor_digest
, DIGEST_LEN
);
1204 log_warn(LD_DIR
, "The voters disagreed on the exit policy summary "
1205 " for router %s with descriptor %s. This really shouldn't"
1206 " have happened.", id
, dd
);
1208 smartlist_sort_strings(exitsummaries
);
1209 chosen_exitsummary
= get_most_frequent_member(exitsummaries
);
1210 } else if (!chosen_exitsummary
) {
1211 char id
[HEX_DIGEST_LEN
+1];
1212 char dd
[HEX_DIGEST_LEN
+1];
1213 base16_encode(id
, sizeof(dd
), rs_out
.identity_digest
, DIGEST_LEN
);
1214 base16_encode(dd
, sizeof(dd
), rs_out
.descriptor_digest
, DIGEST_LEN
);
1215 log_warn(LD_DIR
, "Not one of the voters that made us select"
1216 "descriptor %s for router %s had an exit policy"
1219 /* Ok, none of those voting for the digest we chose had an
1220 * exit policy for us. Well, that kinda sucks.
1222 smartlist_clear(exitsummaries
);
1223 SMARTLIST_FOREACH(matching_descs
, vote_routerstatus_t
*, vsr
, {
1224 if (vsr
->status
.has_exitsummary
)
1225 smartlist_add(exitsummaries
, vsr
->status
.exitsummary
);
1227 smartlist_sort_strings(exitsummaries
);
1228 chosen_exitsummary
= get_most_frequent_member(exitsummaries
);
1230 if (!chosen_exitsummary
)
1231 log_warn(LD_DIR
, "Wow, not one of the voters had an exit "
1232 "policy summary for %s. Wow.", id
);
1235 if (chosen_exitsummary
) {
1236 rs_out
.has_exitsummary
= 1;
1237 /* yea, discards the const */
1238 rs_out
.exitsummary
= (char *)chosen_exitsummary
;
1242 /* Okay!! Now we can write the descriptor... */
1243 /* First line goes into "buf". */
1244 routerstatus_format_entry(buf
, sizeof(buf
), &rs_out
, NULL
,
1246 smartlist_add(chunks
, tor_strdup(buf
));
1247 /* Now an m line, if applicable. */
1248 if (flavor
== FLAV_MICRODESC
&&
1249 !tor_digest256_is_zero(microdesc_digest
)) {
1250 char m
[BASE64_DIGEST256_LEN
+1], *cp
;
1251 const size_t mlen
= BASE64_DIGEST256_LEN
+5;
1252 digest256_to_base64(m
, microdesc_digest
);
1253 cp
= tor_malloc(mlen
);
1254 tor_snprintf(cp
, mlen
, "m %s\n", m
);
1255 smartlist_add(chunks
, cp
);
1257 /* Next line is all flags. The "\n" is missing. */
1258 smartlist_add(chunks
,
1259 smartlist_join_strings(chosen_flags
, " ", 0, NULL
));
1260 /* Now the version line. */
1261 if (chosen_version
) {
1262 smartlist_add(chunks
, tor_strdup("\nv "));
1263 smartlist_add(chunks
, tor_strdup(chosen_version
));
1265 smartlist_add(chunks
, tor_strdup("\n"));
1266 /* Now the weight line. */
1267 if (rs_out
.has_bandwidth
) {
1268 int r
= tor_snprintf(buf
, sizeof(buf
),
1269 "w Bandwidth=%d\n", rs_out
.bandwidth
);
1271 log_warn(LD_BUG
, "Not enough space in buffer for weight line.");
1275 smartlist_add(chunks
, tor_strdup(buf
));
1278 /* Now the exitpolicy summary line. */
1279 if (rs_out
.has_exitsummary
&& flavor
== FLAV_NS
) {
1280 char buf
[MAX_POLICY_LINE_LEN
+1];
1281 int r
= tor_snprintf(buf
, sizeof(buf
), "p %s\n", rs_out
.exitsummary
);
1283 log_warn(LD_BUG
, "Not enough space in buffer for exitpolicy line.");
1286 smartlist_add(chunks
, tor_strdup(buf
));
1289 /* And the loop is over and we move on to the next router */
1294 tor_free(n_voter_flags
);
1295 tor_free(n_flag_voters
);
1296 for (i
= 0; i
< smartlist_len(votes
); ++i
)
1297 tor_free(flag_map
[i
]);
1299 tor_free(flag_counts
);
1300 tor_free(named_flag
);
1301 tor_free(unnamed_flag
);
1302 strmap_free(name_to_id_map
, NULL
);
1303 smartlist_free(matching_descs
);
1304 smartlist_free(chosen_flags
);
1305 smartlist_free(versions
);
1306 smartlist_free(exitsummaries
);
1307 tor_free(bandwidths
);
1308 tor_free(measured_bws
);
1311 /* Add a signature. */
1313 char digest
[DIGEST256_LEN
];
1314 char fingerprint
[HEX_DIGEST_LEN
+1];
1315 char signing_key_fingerprint
[HEX_DIGEST_LEN
+1];
1316 digest_algorithm_t digest_alg
=
1317 flavor
== FLAV_NS
? DIGEST_SHA1
: DIGEST_SHA256
;
1319 flavor
== FLAV_NS
? DIGEST_LEN
: DIGEST256_LEN
;
1320 const char *algname
= crypto_digest_algorithm_get_name(digest_alg
);
1323 smartlist_add(chunks
, tor_strdup("directory-signature "));
1325 /* Compute the hash of the chunks. */
1326 hash_list_members(digest
, digest_len
, chunks
, digest_alg
);
1328 /* Get the fingerprints */
1329 crypto_pk_get_fingerprint(identity_key
, fingerprint
, 0);
1330 crypto_pk_get_fingerprint(signing_key
, signing_key_fingerprint
, 0);
1332 /* add the junk that will go at the end of the line. */
1333 if (flavor
== FLAV_NS
) {
1334 tor_snprintf(buf
, sizeof(buf
), "%s %s\n", fingerprint
,
1335 signing_key_fingerprint
);
1337 tor_snprintf(buf
, sizeof(buf
), "%s %s %s\n",
1338 algname
, fingerprint
,
1339 signing_key_fingerprint
);
1341 /* And the signature. */
1342 if (router_append_dirobj_signature(buf
, sizeof(buf
), digest
, digest_len
,
1344 log_warn(LD_BUG
, "Couldn't sign consensus networkstatus.");
1345 return NULL
; /* This leaks, but it should never happen. */
1347 smartlist_add(chunks
, tor_strdup(buf
));
1349 if (legacy_id_key_digest
&& legacy_signing_key
&& consensus_method
>= 3) {
1350 smartlist_add(chunks
, tor_strdup("directory-signature "));
1351 base16_encode(fingerprint
, sizeof(fingerprint
),
1352 legacy_id_key_digest
, DIGEST_LEN
);
1353 crypto_pk_get_fingerprint(legacy_signing_key
,
1354 signing_key_fingerprint
, 0);
1355 if (flavor
== FLAV_NS
) {
1356 tor_snprintf(buf
, sizeof(buf
), "%s %s\n", fingerprint
,
1357 signing_key_fingerprint
);
1359 tor_snprintf(buf
, sizeof(buf
), "%s %s %s\n",
1360 algname
, fingerprint
,
1361 signing_key_fingerprint
);
1363 if (router_append_dirobj_signature(buf
, sizeof(buf
), digest
, digest_len
,
1364 legacy_signing_key
)) {
1365 log_warn(LD_BUG
, "Couldn't sign consensus networkstatus.");
1366 return NULL
; /* This leaks, but it should never happen. */
1368 smartlist_add(chunks
, tor_strdup(buf
));
1372 result
= smartlist_join_strings(chunks
, "", 0, NULL
);
1374 tor_free(client_versions
);
1375 tor_free(server_versions
);
1376 SMARTLIST_FOREACH(flags
, char *, cp
, tor_free(cp
));
1377 smartlist_free(flags
);
1378 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
1379 smartlist_free(chunks
);
1383 if (!(c
= networkstatus_parse_vote_from_string(result
, NULL
,
1384 NS_TYPE_CONSENSUS
))) {
1385 log_err(LD_BUG
,"Generated a networkstatus consensus we couldn't "
1390 networkstatus_vote_free(c
);
1396 /** Given a consensus vote <b>target</b> and a set of detached signatures in
1397 * <b>sigs</b> that correspond to the same consensus, check whether there are
1398 * any new signatures in <b>src_voter_list</b> that should be added to
1399 * <b>target</b>. (A signature should be added if we have no signature for that
1400 * voter in <b>target</b> yet, or if we have no verifiable signature and the
1401 * new signature is verifiable.) Return the number of signatures added or
1402 * changed, or -1 if the document signed by <b>sigs</b> isn't the same
1403 * document as <b>target</b>. */
1405 networkstatus_add_detached_signatures(networkstatus_t
*target
,
1406 ns_detached_signatures_t
*sigs
,
1407 const char **msg_out
)
1411 smartlist_t
*siglist
;
1414 tor_assert(target
->type
== NS_TYPE_CONSENSUS
);
1416 flavor
= networkstatus_get_flavor_name(target
->flavor
);
1418 /* Do the times seem right? */
1419 if (target
->valid_after
!= sigs
->valid_after
) {
1420 *msg_out
= "Valid-After times do not match "
1421 "when adding detached signatures to consensus";
1424 if (target
->fresh_until
!= sigs
->fresh_until
) {
1425 *msg_out
= "Fresh-until times do not match "
1426 "when adding detached signatures to consensus";
1429 if (target
->valid_until
!= sigs
->valid_until
) {
1430 *msg_out
= "Valid-until times do not match "
1431 "when adding detached signatures to consensus";
1434 siglist
= strmap_get(sigs
->signatures
, flavor
);
1436 *msg_out
= "No signatures for given consensus flavor";
1440 /** Make sure all the digests we know match, and at least one matches. */
1442 digests_t
*digests
= strmap_get(sigs
->digests
, flavor
);
1444 digest_algorithm_t alg
;
1446 *msg_out
= "No digests for given consensus flavor";
1449 for (alg
= DIGEST_SHA1
; alg
< N_DIGEST_ALGORITHMS
; ++alg
) {
1450 if (!tor_mem_is_zero(digests
->d
[alg
], DIGEST256_LEN
)) {
1451 if (!memcmp(target
->digests
.d
[alg
], digests
->d
[alg
], DIGEST256_LEN
)) {
1454 *msg_out
= "Mismatched digest.";
1460 *msg_out
= "No regognized digests for given consensus flavor";
1464 /* For each voter in src... */
1465 SMARTLIST_FOREACH_BEGIN(siglist
, document_signature_t
*, sig
) {
1466 char voter_identity
[HEX_DIGEST_LEN
+1];
1467 networkstatus_voter_info_t
*target_voter
=
1468 networkstatus_get_voter_by_id(target
, sig
->identity_digest
);
1469 authority_cert_t
*cert
= NULL
;
1470 const char *algorithm
;
1471 document_signature_t
*old_sig
= NULL
;
1473 algorithm
= crypto_digest_algorithm_get_name(sig
->alg
);
1475 base16_encode(voter_identity
, sizeof(voter_identity
),
1476 sig
->identity_digest
, DIGEST_LEN
);
1477 log_info(LD_DIR
, "Looking at signature from %s using %s", voter_identity
,
1479 /* If the target doesn't know about this voter, then forget it. */
1480 if (!target_voter
) {
1481 log_info(LD_DIR
, "We do not know any voter with ID %s", voter_identity
);
1485 old_sig
= voter_get_sig_by_algorithm(target_voter
, sig
->alg
);
1487 /* If the target already has a good signature from this voter, then skip
1489 if (old_sig
&& old_sig
->good_signature
) {
1490 log_info(LD_DIR
, "We already have a good signature from %s using %s",
1491 voter_identity
, algorithm
);
1495 /* Try checking the signature if we haven't already. */
1496 if (!sig
->good_signature
&& !sig
->bad_signature
) {
1497 cert
= authority_cert_get_by_digests(sig
->identity_digest
,
1498 sig
->signing_key_digest
);
1500 networkstatus_check_document_signature(target
, sig
, cert
);
1503 /* If this signature is good, or we don't have any signature yet,
1504 * then maybe add it. */
1505 if (sig
->good_signature
|| !old_sig
|| old_sig
->bad_signature
) {
1506 log_info(LD_DIR
, "Adding signature from %s with %s", voter_identity
,
1510 smartlist_remove(target_voter
->sigs
, old_sig
);
1511 document_signature_free(old_sig
);
1513 smartlist_add(target_voter
->sigs
, document_signature_dup(sig
));
1515 log_info(LD_DIR
, "Not adding signature from %s", voter_identity
);
1517 } SMARTLIST_FOREACH_END(sig
);
1522 /** Return a newly allocated string containing all the signatures on
1523 * <b>consensus</b> by all voters. If <b>for_detached_signatures</b> is true,
1524 * then the signatures will be put in a detached signatures document, so
1525 * prefix any non-NS-flavored signatures with "additional-signature" rather
1526 * than "directory-signature". */
1528 networkstatus_format_signatures(networkstatus_t
*consensus
,
1529 int for_detached_signatures
)
1531 smartlist_t
*elements
;
1533 char *result
= NULL
;
1535 const consensus_flavor_t flavor
= consensus
->flavor
;
1536 const char *flavor_name
= networkstatus_get_flavor_name(flavor
);
1537 const char *keyword
;
1539 if (for_detached_signatures
&& flavor
!= FLAV_NS
)
1540 keyword
= "additional-signature";
1542 keyword
= "directory-signature";
1544 elements
= smartlist_create();
1546 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*, v
) {
1547 SMARTLIST_FOREACH_BEGIN(v
->sigs
, document_signature_t
*, sig
) {
1548 char sk
[HEX_DIGEST_LEN
+1];
1549 char id
[HEX_DIGEST_LEN
+1];
1550 if (!sig
->signature
|| sig
->bad_signature
)
1553 base16_encode(sk
, sizeof(sk
), sig
->signing_key_digest
, DIGEST_LEN
);
1554 base16_encode(id
, sizeof(id
), sig
->identity_digest
, DIGEST_LEN
);
1555 if (flavor
== FLAV_NS
) {
1556 tor_snprintf(buf
, sizeof(buf
),
1557 "%s %s %s\n-----BEGIN SIGNATURE-----\n",
1560 const char *digest_name
=
1561 crypto_digest_algorithm_get_name(sig
->alg
);
1562 tor_snprintf(buf
, sizeof(buf
),
1563 "%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n",
1565 for_detached_signatures
? " " : "",
1566 for_detached_signatures
? flavor_name
: "",
1567 digest_name
, id
, sk
);
1569 smartlist_add(elements
, tor_strdup(buf
));
1570 base64_encode(buf
, sizeof(buf
), sig
->signature
, sig
->signature_len
);
1571 strlcat(buf
, "-----END SIGNATURE-----\n", sizeof(buf
));
1572 smartlist_add(elements
, tor_strdup(buf
));
1573 } SMARTLIST_FOREACH_END(sig
);
1574 } SMARTLIST_FOREACH_END(v
);
1576 result
= smartlist_join_strings(elements
, "", 0, NULL
);
1577 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
1578 smartlist_free(elements
);
1584 /** Return a newly allocated string holding the detached-signatures document
1585 * corresponding to the signatures on <b>consensuses</b>, which must contain
1586 * exactly one FLAV_NS consensus, and no more than one consensus for each
1589 networkstatus_get_detached_signatures(smartlist_t
*consensuses
)
1591 smartlist_t
*elements
;
1593 char *result
= NULL
, *sigs
= NULL
;
1594 networkstatus_t
*consensus_ns
= NULL
;
1595 tor_assert(consensuses
);
1597 SMARTLIST_FOREACH(consensuses
, networkstatus_t
*, ns
, {
1599 tor_assert(ns
->type
== NS_TYPE_CONSENSUS
);
1600 if (ns
&& ns
->flavor
== FLAV_NS
)
1603 if (!consensus_ns
) {
1604 log_warn(LD_BUG
, "No NS consensus given.");
1608 elements
= smartlist_create();
1611 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
1612 vu_buf
[ISO_TIME_LEN
+1];
1613 char d
[HEX_DIGEST_LEN
+1];
1615 base16_encode(d
, sizeof(d
),
1616 consensus_ns
->digests
.d
[DIGEST_SHA1
], DIGEST_LEN
);
1617 format_iso_time(va_buf
, consensus_ns
->valid_after
);
1618 format_iso_time(fu_buf
, consensus_ns
->fresh_until
);
1619 format_iso_time(vu_buf
, consensus_ns
->valid_until
);
1621 tor_snprintf(buf
, sizeof(buf
),
1622 "consensus-digest %s\n"
1625 "valid-until %s\n", d
, va_buf
, fu_buf
, vu_buf
);
1626 smartlist_add(elements
, tor_strdup(buf
));
1629 /* Get all the digests for the non-FLAV_NS consensuses */
1630 SMARTLIST_FOREACH_BEGIN(consensuses
, networkstatus_t
*, ns
) {
1631 const char *flavor_name
= networkstatus_get_flavor_name(ns
->flavor
);
1633 if (ns
->flavor
== FLAV_NS
)
1636 /* start with SHA256; we don't include SHA1 for anything but the basic
1638 for (alg
= DIGEST_SHA256
; alg
< N_DIGEST_ALGORITHMS
; ++alg
) {
1639 char d
[HEX_DIGEST256_LEN
+1];
1640 const char *alg_name
=
1641 crypto_digest_algorithm_get_name(alg
);
1642 if (tor_mem_is_zero(ns
->digests
.d
[alg
], DIGEST256_LEN
))
1644 base16_encode(d
, sizeof(d
), ns
->digests
.d
[alg
], DIGEST256_LEN
);
1645 tor_snprintf(buf
, sizeof(buf
), "additional-digest %s %s %s\n",
1646 flavor_name
, alg_name
, d
);
1647 smartlist_add(elements
, tor_strdup(buf
));
1649 } SMARTLIST_FOREACH_END(ns
);
1651 /* Now get all the sigs for non-FLAV_NS consensuses */
1652 SMARTLIST_FOREACH_BEGIN(consensuses
, networkstatus_t
*, ns
) {
1654 if (ns
->flavor
== FLAV_NS
)
1656 sigs
= networkstatus_format_signatures(ns
, 1);
1658 log_warn(LD_DIR
, "Couldn't format signatures");
1661 smartlist_add(elements
, sigs
);
1662 } SMARTLIST_FOREACH_END(ns
);
1664 /* Now add the FLAV_NS consensus signatrures. */
1665 sigs
= networkstatus_format_signatures(consensus_ns
, 1);
1668 smartlist_add(elements
, sigs
);
1670 result
= smartlist_join_strings(elements
, "", 0, NULL
);
1672 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
1673 smartlist_free(elements
);
1677 /** Return a newly allocated string holding a detached-signatures document for
1678 * all of the in-progress consensuses in the <b>n_flavors</b>-element array at
1679 * <b>pending</b>. */
1681 get_detached_signatures_from_pending_consensuses(pending_consensus_t
*pending
,
1686 smartlist_t
*c
= smartlist_create();
1687 for (flav
= 0; flav
< n_flavors
; ++flav
) {
1688 if (pending
[flav
].consensus
)
1689 smartlist_add(c
, pending
[flav
].consensus
);
1691 signatures
= networkstatus_get_detached_signatures(c
);
1696 /** Release all storage held in <b>s</b>. */
1698 ns_detached_signatures_free(ns_detached_signatures_t
*s
)
1702 if (s
->signatures
) {
1703 STRMAP_FOREACH(s
->signatures
, flavor
, smartlist_t
*, sigs
) {
1704 SMARTLIST_FOREACH(sigs
, document_signature_t
*, sig
,
1705 document_signature_free(sig
));
1706 smartlist_free(sigs
);
1707 } STRMAP_FOREACH_END
;
1708 strmap_free(s
->signatures
, NULL
);
1709 strmap_free(s
->digests
, _tor_free
);
1716 * Certificate functions
1719 /** Allocate and return a new authority_cert_t with the same contents as
1722 authority_cert_dup(authority_cert_t
*cert
)
1724 authority_cert_t
*out
= tor_malloc(sizeof(authority_cert_t
));
1727 memcpy(out
, cert
, sizeof(authority_cert_t
));
1728 /* Now copy pointed-to things. */
1729 out
->cache_info
.signed_descriptor_body
=
1730 tor_strndup(cert
->cache_info
.signed_descriptor_body
,
1731 cert
->cache_info
.signed_descriptor_len
);
1732 out
->cache_info
.saved_location
= SAVED_NOWHERE
;
1733 out
->identity_key
= crypto_pk_dup_key(cert
->identity_key
);
1734 out
->signing_key
= crypto_pk_dup_key(cert
->signing_key
);
1743 /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
1744 * Note that these aren't the intervals we'll use to vote; they're the ones
1745 * that we'll vote to use. */
1747 dirvote_get_preferred_voting_intervals(vote_timing_t
*timing_out
)
1749 or_options_t
*options
= get_options();
1751 tor_assert(timing_out
);
1753 timing_out
->vote_interval
= options
->V3AuthVotingInterval
;
1754 timing_out
->n_intervals_valid
= options
->V3AuthNIntervalsValid
;
1755 timing_out
->vote_delay
= options
->V3AuthVoteDelay
;
1756 timing_out
->dist_delay
= options
->V3AuthDistDelay
;
1759 /** Return the start of the next interval of size <b>interval</b> (in seconds)
1760 * after <b>now</b>. Midnight always starts a fresh interval, and if the last
1761 * interval of a day would be truncated to less than half its size, it is
1762 * rolled into the previous interval. */
1764 dirvote_get_start_of_next_interval(time_t now
, int interval
)
1767 time_t midnight_today
;
1768 time_t midnight_tomorrow
;
1771 tor_gmtime_r(&now
, &tm
);
1776 midnight_today
= tor_timegm(&tm
);
1777 midnight_tomorrow
= midnight_today
+ (24*60*60);
1779 next
= midnight_today
+ ((now
-midnight_today
)/interval
+ 1)*interval
;
1781 /* Intervals never cross midnight. */
1782 if (next
> midnight_tomorrow
)
1783 next
= midnight_tomorrow
;
1785 /* If the interval would only last half as long as it's supposed to, then
1786 * skip over to the next day. */
1787 if (next
+ interval
/2 > midnight_tomorrow
)
1788 next
= midnight_tomorrow
;
1793 /** Scheduling information for a voting interval. */
1795 /** When do we generate and distribute our vote for this interval? */
1796 time_t voting_starts
;
1797 /** When do we send an HTTP request for any votes that we haven't
1798 * been posted yet?*/
1799 time_t fetch_missing_votes
;
1800 /** When do we give up on getting more votes and generate a consensus? */
1802 /** When do we send an HTTP request for any signatures we're expecting to
1803 * see on the consensus? */
1804 time_t fetch_missing_signatures
;
1805 /** When do we publish the consensus? */
1806 time_t interval_starts
;
1808 /* True iff we have generated and distributed our vote. */
1810 /* True iff we've requested missing votes. */
1811 int have_fetched_missing_votes
;
1812 /* True iff we have built a consensus and sent the signatures around. */
1813 int have_built_consensus
;
1814 /* True iff we've fetched missing signatures. */
1815 int have_fetched_missing_signatures
;
1816 /* True iff we have published our consensus. */
1817 int have_published_consensus
;
1818 } voting_schedule
= {0,0,0,0,0,0,0,0,0,0};
1820 /** Set voting_schedule to hold the timing for the next vote we should be
1823 dirvote_recalculate_timing(or_options_t
*options
, time_t now
)
1825 int interval
, vote_delay
, dist_delay
;
1828 networkstatus_t
*consensus
;
1830 if (!authdir_mode_v3(options
))
1833 consensus
= networkstatus_get_live_consensus(now
);
1835 memset(&voting_schedule
, 0, sizeof(voting_schedule
));
1838 interval
= (int)( consensus
->fresh_until
- consensus
->valid_after
);
1839 vote_delay
= consensus
->vote_seconds
;
1840 dist_delay
= consensus
->dist_seconds
;
1842 interval
= options
->TestingV3AuthInitialVotingInterval
;
1843 vote_delay
= options
->TestingV3AuthInitialVoteDelay
;
1844 dist_delay
= options
->TestingV3AuthInitialDistDelay
;
1847 tor_assert(interval
> 0);
1849 if (vote_delay
+ dist_delay
> interval
/2)
1850 vote_delay
= dist_delay
= interval
/ 4;
1852 start
= voting_schedule
.interval_starts
=
1853 dirvote_get_start_of_next_interval(now
,interval
);
1854 end
= dirvote_get_start_of_next_interval(start
+1, interval
);
1856 tor_assert(end
> start
);
1858 voting_schedule
.fetch_missing_signatures
= start
- (dist_delay
/2);
1859 voting_schedule
.voting_ends
= start
- dist_delay
;
1860 voting_schedule
.fetch_missing_votes
= start
- dist_delay
- (vote_delay
/2);
1861 voting_schedule
.voting_starts
= start
- dist_delay
- vote_delay
;
1864 char tbuf
[ISO_TIME_LEN
+1];
1865 format_iso_time(tbuf
, voting_schedule
.interval_starts
);
1866 log_notice(LD_DIR
,"Choosing expected valid-after time as %s: "
1867 "consensus_set=%d, interval=%d",
1868 tbuf
, consensus
?1:0, interval
);
1872 /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
1874 dirvote_act(or_options_t
*options
, time_t now
)
1876 if (!authdir_mode_v3(options
))
1878 if (!voting_schedule
.voting_starts
) {
1879 char *keys
= list_v3_auth_ids();
1880 authority_cert_t
*c
= get_my_v3_authority_cert();
1881 log_notice(LD_DIR
, "Scheduling voting. Known authority IDs are %s. "
1883 keys
, hex_str(c
->cache_info
.identity_digest
, DIGEST_LEN
));
1885 dirvote_recalculate_timing(options
, now
);
1887 if (voting_schedule
.voting_starts
< now
&& !voting_schedule
.have_voted
) {
1888 log_notice(LD_DIR
, "Time to vote.");
1889 dirvote_perform_vote();
1890 voting_schedule
.have_voted
= 1;
1892 if (voting_schedule
.fetch_missing_votes
< now
&&
1893 !voting_schedule
.have_fetched_missing_votes
) {
1894 log_notice(LD_DIR
, "Time to fetch any votes that we're missing.");
1895 dirvote_fetch_missing_votes();
1896 voting_schedule
.have_fetched_missing_votes
= 1;
1898 if (voting_schedule
.voting_ends
< now
&&
1899 !voting_schedule
.have_built_consensus
) {
1900 log_notice(LD_DIR
, "Time to compute a consensus.");
1901 dirvote_compute_consensuses();
1902 /* XXXX We will want to try again later if we haven't got enough
1903 * votes yet. Implement this if it turns out to ever happen. */
1904 voting_schedule
.have_built_consensus
= 1;
1906 if (voting_schedule
.fetch_missing_signatures
< now
&&
1907 !voting_schedule
.have_fetched_missing_signatures
) {
1908 log_notice(LD_DIR
, "Time to fetch any signatures that we're missing.");
1909 dirvote_fetch_missing_signatures();
1910 voting_schedule
.have_fetched_missing_signatures
= 1;
1912 if (voting_schedule
.interval_starts
< now
&&
1913 !voting_schedule
.have_published_consensus
) {
1914 log_notice(LD_DIR
, "Time to publish the consensus and discard old votes");
1915 dirvote_publish_consensus();
1916 dirvote_clear_votes(0);
1917 voting_schedule
.have_published_consensus
= 1;
1918 /* XXXX We will want to try again later if we haven't got enough
1919 * signatures yet. Implement this if it turns out to ever happen. */
1920 dirvote_recalculate_timing(options
, now
);
1924 /** A vote networkstatus_t and its unparsed body: held around so we can
1925 * use it to generate a consensus (at voting_ends) and so we can serve it to
1926 * other authorities that might want it. */
1927 typedef struct pending_vote_t
{
1928 cached_dir_t
*vote_body
;
1929 networkstatus_t
*vote
;
1932 /** List of pending_vote_t for the current vote. Before we've used them to
1933 * build a consensus, the votes go here. */
1934 static smartlist_t
*pending_vote_list
= NULL
;
1935 /** List of pending_vote_t for the previous vote. After we've used them to
1936 * build a consensus, the votes go here for the next period. */
1937 static smartlist_t
*previous_vote_list
= NULL
;
1939 static pending_consensus_t pending_consensuses
[N_CONSENSUS_FLAVORS
];
1941 /** The detached signatures for the consensus that we're currently
1943 static char *pending_consensus_signatures
= NULL
;
1945 /** List of ns_detached_signatures_t: hold signatures that get posted to us
1946 * before we have generated the consensus on our own. */
1947 static smartlist_t
*pending_consensus_signature_list
= NULL
;
1949 /** Generate a networkstatus vote and post it to all the v3 authorities.
1950 * (V3 Authority only) */
1952 dirvote_perform_vote(void)
1954 crypto_pk_env_t
*key
= get_my_v3_authority_signing_key();
1955 authority_cert_t
*cert
= get_my_v3_authority_cert();
1956 networkstatus_t
*ns
;
1958 pending_vote_t
*pending_vote
;
1959 time_t now
= time(NULL
);
1962 const char *msg
= "";
1964 if (!cert
|| !key
) {
1965 log_warn(LD_NET
, "Didn't find key/certificate to generate v3 vote");
1967 } else if (cert
->expires
< now
) {
1968 log_warn(LD_NET
, "Can't generate v3 vote with expired certificate");
1971 if (!(ns
= dirserv_generate_networkstatus_vote_obj(key
, cert
)))
1974 contents
= format_networkstatus_vote(key
, ns
);
1975 networkstatus_vote_free(ns
);
1979 pending_vote
= dirvote_add_vote(contents
, &msg
, &status
);
1981 if (!pending_vote
) {
1982 log_warn(LD_DIR
, "Couldn't store my own vote! (I told myself, '%s'.)",
1987 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE
,
1988 ROUTER_PURPOSE_GENERAL
,
1990 pending_vote
->vote_body
->dir
,
1991 pending_vote
->vote_body
->dir_len
, 0);
1992 log_notice(LD_DIR
, "Vote posted.");
1996 /** Send an HTTP request to every other v3 authority, for the votes of every
1997 * authority for which we haven't received a vote yet in this period. (V3
1998 * authority only) */
2000 dirvote_fetch_missing_votes(void)
2002 smartlist_t
*missing_fps
= smartlist_create();
2005 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
2006 trusted_dir_server_t
*, ds
,
2008 if (!(ds
->type
& V3_AUTHORITY
))
2010 if (!dirvote_get_vote(ds
->v3_identity_digest
,
2011 DGV_BY_ID
|DGV_INCLUDE_PENDING
)) {
2012 char *cp
= tor_malloc(HEX_DIGEST_LEN
+1);
2013 base16_encode(cp
, HEX_DIGEST_LEN
+1, ds
->v3_identity_digest
,
2015 smartlist_add(missing_fps
, cp
);
2019 if (!smartlist_len(missing_fps
)) {
2020 smartlist_free(missing_fps
);
2023 log_notice(LOG_NOTICE
, "We're missing votes from %d authorities. Asking "
2024 "every other authority for a copy.", smartlist_len(missing_fps
));
2025 resource
= smartlist_join_strings(missing_fps
, "+", 0, NULL
);
2026 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE
,
2029 SMARTLIST_FOREACH(missing_fps
, char *, cp
, tor_free(cp
));
2030 smartlist_free(missing_fps
);
2033 /** Send a request to every other authority for its detached signatures,
2034 * unless we have signatures from all other v3 authorities already. */
2036 dirvote_fetch_missing_signatures(void)
2040 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2041 networkstatus_t
*consensus
= pending_consensuses
[i
].consensus
;
2043 networkstatus_check_consensus_signature(consensus
, -1) == 1) {
2044 /* We have no consensus, or we have one that's signed by everybody. */
2052 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
,
2056 /** Release all storage held by pending consensuses (those waiting for
2059 dirvote_clear_pending_consensuses(void)
2062 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2063 pending_consensus_t
*pc
= &pending_consensuses
[i
];
2066 networkstatus_vote_free(pc
->consensus
);
2067 pc
->consensus
= NULL
;
2071 /** Drop all currently pending votes, consensus, and detached signatures. */
2073 dirvote_clear_votes(int all_votes
)
2075 if (!previous_vote_list
)
2076 previous_vote_list
= smartlist_create();
2077 if (!pending_vote_list
)
2078 pending_vote_list
= smartlist_create();
2080 /* All "previous" votes are now junk. */
2081 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, v
, {
2082 cached_dir_decref(v
->vote_body
);
2083 v
->vote_body
= NULL
;
2084 networkstatus_vote_free(v
->vote
);
2087 smartlist_clear(previous_vote_list
);
2090 /* If we're dumping all the votes, we delete the pending ones. */
2091 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
2092 cached_dir_decref(v
->vote_body
);
2093 v
->vote_body
= NULL
;
2094 networkstatus_vote_free(v
->vote
);
2098 /* Otherwise, we move them into "previous". */
2099 smartlist_add_all(previous_vote_list
, pending_vote_list
);
2101 smartlist_clear(pending_vote_list
);
2103 if (pending_consensus_signature_list
) {
2104 SMARTLIST_FOREACH(pending_consensus_signature_list
, char *, cp
,
2106 smartlist_clear(pending_consensus_signature_list
);
2108 tor_free(pending_consensus_signatures
);
2109 dirvote_clear_pending_consensuses();
2112 /** Return a newly allocated string containing the hex-encoded v3 authority
2113 identity digest of every recognized v3 authority. */
2115 list_v3_auth_ids(void)
2117 smartlist_t
*known_v3_keys
= smartlist_create();
2119 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
2120 trusted_dir_server_t
*, ds
,
2121 if ((ds
->type
& V3_AUTHORITY
) &&
2122 !tor_digest_is_zero(ds
->v3_identity_digest
))
2123 smartlist_add(known_v3_keys
,
2124 tor_strdup(hex_str(ds
->v3_identity_digest
, DIGEST_LEN
))));
2125 keys
= smartlist_join_strings(known_v3_keys
, ", ", 0, NULL
);
2126 SMARTLIST_FOREACH(known_v3_keys
, char *, cp
, tor_free(cp
));
2127 smartlist_free(known_v3_keys
);
2131 /** Called when we have received a networkstatus vote in <b>vote_body</b>.
2132 * Parse and validate it, and on success store it as a pending vote (which we
2133 * then return). Return NULL on failure. Sets *<b>msg_out</b> and
2134 * *<b>status_out</b> to an HTTP response and status code. (V3 authority
2137 dirvote_add_vote(const char *vote_body
, const char **msg_out
, int *status_out
)
2139 networkstatus_t
*vote
;
2140 networkstatus_voter_info_t
*vi
;
2141 trusted_dir_server_t
*ds
;
2142 pending_vote_t
*pending_vote
= NULL
;
2143 const char *end_of_vote
= NULL
;
2145 tor_assert(vote_body
);
2146 tor_assert(msg_out
);
2147 tor_assert(status_out
);
2149 if (!pending_vote_list
)
2150 pending_vote_list
= smartlist_create();
2155 vote
= networkstatus_parse_vote_from_string(vote_body
, &end_of_vote
,
2158 end_of_vote
= vote_body
+ strlen(vote_body
);
2160 log_warn(LD_DIR
, "Couldn't parse vote: length was %d",
2161 (int)strlen(vote_body
));
2162 *msg_out
= "Unable to parse vote";
2165 tor_assert(smartlist_len(vote
->voters
) == 1);
2166 vi
= get_voter(vote
);
2168 int any_sig_good
= 0;
2169 SMARTLIST_FOREACH(vi
->sigs
, document_signature_t
*, sig
,
2170 if (sig
->good_signature
)
2172 tor_assert(any_sig_good
);
2174 ds
= trusteddirserver_get_by_v3_auth_digest(vi
->identity_digest
);
2176 char *keys
= list_v3_auth_ids();
2177 log_warn(LD_DIR
, "Got a vote from an authority (nickname %s, address %s) "
2178 "with authority key ID %s. "
2179 "This key ID is not recognized. Known v3 key IDs are: %s",
2180 vi
->nickname
, vi
->address
,
2181 hex_str(vi
->identity_digest
, DIGEST_LEN
), keys
);
2183 *msg_out
= "Vote not from a recognized v3 authority";
2186 tor_assert(vote
->cert
);
2187 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
2188 vote
->cert
->signing_key_digest
)) {
2189 /* Hey, it's a new cert! */
2190 trusted_dirs_load_certs_from_string(
2191 vote
->cert
->cache_info
.signed_descriptor_body
,
2192 0 /* from_store */, 1 /*flush*/);
2193 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
2194 vote
->cert
->signing_key_digest
)) {
2195 log_warn(LD_BUG
, "We added a cert, but still couldn't find it.");
2199 /* Is it for the right period? */
2200 if (vote
->valid_after
!= voting_schedule
.interval_starts
) {
2201 char tbuf1
[ISO_TIME_LEN
+1], tbuf2
[ISO_TIME_LEN
+1];
2202 format_iso_time(tbuf1
, vote
->valid_after
);
2203 format_iso_time(tbuf2
, voting_schedule
.interval_starts
);
2204 log_warn(LD_DIR
, "Rejecting vote from %s with valid-after time of %s; "
2205 "we were expecting %s", vi
->address
, tbuf1
, tbuf2
);
2206 *msg_out
= "Bad valid-after time";
2210 /* Now see whether we already have a vote from this authority. */
2211 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
2212 if (! memcmp(v
->vote
->cert
->cache_info
.identity_digest
,
2213 vote
->cert
->cache_info
.identity_digest
,
2215 networkstatus_voter_info_t
*vi_old
= get_voter(v
->vote
);
2216 if (!memcmp(vi_old
->vote_digest
, vi
->vote_digest
, DIGEST_LEN
)) {
2217 /* Ah, it's the same vote. Not a problem. */
2218 log_info(LD_DIR
, "Discarding a vote we already have.");
2219 if (*status_out
< 200)
2222 } else if (v
->vote
->published
< vote
->published
) {
2223 log_notice(LD_DIR
, "Replacing an older pending vote from this "
2225 cached_dir_decref(v
->vote_body
);
2226 networkstatus_vote_free(v
->vote
);
2227 v
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
2228 end_of_vote
-vote_body
),
2232 !strcmpstart(end_of_vote
, "network-status-version"))
2235 if (*status_out
< 200)
2241 *msg_out
= "Already have a newer pending vote";
2247 pending_vote
= tor_malloc_zero(sizeof(pending_vote_t
));
2248 pending_vote
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
2249 end_of_vote
-vote_body
),
2251 pending_vote
->vote
= vote
;
2252 smartlist_add(pending_vote_list
, pending_vote
);
2254 if (!strcmpstart(end_of_vote
, "network-status-version ")) {
2255 vote_body
= end_of_vote
;
2264 *msg_out
= "Error adding vote";
2265 if (*status_out
< 400)
2269 networkstatus_vote_free(vote
);
2271 if (end_of_vote
&& !strcmpstart(end_of_vote
, "network-status-version ")) {
2272 vote_body
= end_of_vote
;
2278 if (*status_out
< 200)
2281 if (!any_failed
&& !pending_vote
) {
2282 *msg_out
= "Duplicate discarded";
2288 return any_failed
? NULL
: pending_vote
;
2291 /** Try to compute a v3 networkstatus consensus from the currently pending
2292 * votes. Return 0 on success, -1 on failure. Store the consensus in
2293 * pending_consensus: it won't be ready to be published until we have
2294 * everybody else's signatures collected too. (V3 Authority only) */
2296 dirvote_compute_consensuses(void)
2298 /* Have we got enough votes to try? */
2299 int n_votes
, n_voters
, n_vote_running
= 0;
2300 smartlist_t
*votes
= NULL
, *votestrings
= NULL
;
2301 char *consensus_body
= NULL
, *signatures
= NULL
, *votefile
;
2302 networkstatus_t
*consensus
= NULL
;
2303 authority_cert_t
*my_cert
;
2304 pending_consensus_t pending
[N_CONSENSUS_FLAVORS
];
2307 memset(pending
, 0, sizeof(pending
));
2309 if (!pending_vote_list
)
2310 pending_vote_list
= smartlist_create();
2312 n_voters
= get_n_authorities(V3_AUTHORITY
);
2313 n_votes
= smartlist_len(pending_vote_list
);
2314 if (n_votes
<= n_voters
/2) {
2315 log_warn(LD_DIR
, "We don't have enough votes to generate a consensus: "
2316 "%d of %d", n_votes
, n_voters
/2);
2319 tor_assert(pending_vote_list
);
2320 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
2321 if (smartlist_string_isin(v
->vote
->known_flags
, "Running"))
2324 if (!n_vote_running
) {
2325 /* See task 1066. */
2326 log_warn(LD_DIR
, "Nobody has voted on the Running flag. Generating "
2327 "and publishing a consensus without Running nodes "
2328 "would make many clients stop working. Not "
2329 "generating a consensus!");
2333 if (!(my_cert
= get_my_v3_authority_cert())) {
2334 log_warn(LD_DIR
, "Can't generate consensus without a certificate.");
2338 votes
= smartlist_create();
2339 votestrings
= smartlist_create();
2340 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
,
2342 sized_chunk_t
*c
= tor_malloc(sizeof(sized_chunk_t
));
2343 c
->bytes
= v
->vote_body
->dir
;
2344 c
->len
= v
->vote_body
->dir_len
;
2345 smartlist_add(votestrings
, c
); /* collect strings to write to disk */
2347 smartlist_add(votes
, v
->vote
); /* collect votes to compute consensus */
2350 votefile
= get_datadir_fname("v3-status-votes");
2351 write_chunks_to_file(votefile
, votestrings
, 0);
2353 SMARTLIST_FOREACH(votestrings
, sized_chunk_t
*, c
, tor_free(c
));
2354 smartlist_free(votestrings
);
2357 char legacy_dbuf
[DIGEST_LEN
];
2358 crypto_pk_env_t
*legacy_sign
=NULL
;
2359 char *legacy_id_digest
= NULL
;
2360 int n_generated
= 0;
2361 if (get_options()->V3AuthUseLegacyKey
) {
2362 authority_cert_t
*cert
= get_my_v3_legacy_cert();
2363 legacy_sign
= get_my_v3_legacy_signing_key();
2365 crypto_pk_get_digest(cert
->identity_key
, legacy_dbuf
);
2366 legacy_id_digest
= legacy_dbuf
;
2370 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
2371 const char *flavor_name
= networkstatus_get_flavor_name(flav
);
2372 consensus_body
= networkstatus_compute_consensus(
2374 my_cert
->identity_key
,
2375 get_my_v3_authority_signing_key(), legacy_id_digest
, legacy_sign
,
2378 if (!consensus_body
) {
2379 log_warn(LD_DIR
, "Couldn't generate a %s consensus at all!",
2383 consensus
= networkstatus_parse_vote_from_string(consensus_body
, NULL
,
2386 log_warn(LD_DIR
, "Couldn't parse %s consensus we generated!",
2388 tor_free(consensus_body
);
2392 /* 'Check' our own signature, to mark it valid. */
2393 networkstatus_check_consensus_signature(consensus
, -1);
2395 pending
[flav
].body
= consensus_body
;
2396 pending
[flav
].consensus
= consensus
;
2398 consensus_body
= NULL
;
2402 log_warn(LD_DIR
, "Couldn't generate any consensus flavors at all.");
2407 signatures
= get_detached_signatures_from_pending_consensuses(
2408 pending
, N_CONSENSUS_FLAVORS
);
2411 log_warn(LD_DIR
, "Couldn't extract signatures.");
2415 dirvote_clear_pending_consensuses();
2416 memcpy(pending_consensuses
, pending
, sizeof(pending
));
2418 tor_free(pending_consensus_signatures
);
2419 pending_consensus_signatures
= signatures
;
2421 if (pending_consensus_signature_list
) {
2423 /* we may have gotten signatures for this consensus before we built
2424 * it ourself. Add them now. */
2425 SMARTLIST_FOREACH(pending_consensus_signature_list
, char *, sig
,
2427 const char *msg
= NULL
;
2428 int r
= dirvote_add_signatures_to_all_pending_consensuses(sig
, &msg
);
2433 "Could not add queued signature to new consensus: %s",
2438 log_notice(LD_DIR
, "Added %d pending signatures while building "
2439 "consensus.", n_sigs
);
2440 smartlist_clear(pending_consensus_signature_list
);
2443 log_notice(LD_DIR
, "Consensus computed; uploading signature(s)");
2445 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES
,
2446 ROUTER_PURPOSE_GENERAL
,
2448 pending_consensus_signatures
,
2449 strlen(pending_consensus_signatures
), 0);
2450 log_notice(LD_DIR
, "Signature(s) posted.");
2454 smartlist_free(votes
);
2455 tor_free(consensus_body
);
2456 tor_free(signatures
);
2457 networkstatus_vote_free(consensus
);
2462 /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
2463 * signatures on the currently pending consensus. Add them to <b>pc</b>
2464 * as appropriate. Return the number of signatures added. (?) */
2466 dirvote_add_signatures_to_pending_consensus(
2467 pending_consensus_t
*pc
,
2468 ns_detached_signatures_t
*sigs
,
2469 const char **msg_out
)
2471 const char *flavor_name
;
2474 /* Only call if we have a pending consensus right now. */
2475 tor_assert(pc
->consensus
);
2476 tor_assert(pc
->body
);
2477 tor_assert(pending_consensus_signatures
);
2479 flavor_name
= networkstatus_get_flavor_name(pc
->consensus
->flavor
);
2483 smartlist_t
*sig_list
= strmap_get(sigs
->signatures
, flavor_name
);
2484 log_info(LD_DIR
, "Have %d signatures for adding to %s consensus.",
2485 sig_list
? smartlist_len(sig_list
) : 0, flavor_name
);
2487 r
= networkstatus_add_detached_signatures(pc
->consensus
, sigs
, msg_out
);
2488 log_info(LD_DIR
,"Added %d signatures to consensus.", r
);
2491 char *new_signatures
=
2492 networkstatus_format_signatures(pc
->consensus
, 0);
2493 char *dst
, *dst_end
;
2494 size_t new_consensus_len
;
2495 if (!new_signatures
) {
2496 *msg_out
= "No signatures to add";
2500 strlen(pc
->body
) + strlen(new_signatures
) + 1;
2501 pc
->body
= tor_realloc(pc
->body
, new_consensus_len
);
2502 dst_end
= pc
->body
+ new_consensus_len
;
2503 dst
= strstr(pc
->body
, "directory-signature ");
2505 strlcpy(dst
, new_signatures
, dst_end
-dst
);
2507 /* We remove this block once it has failed to crash for a while. But
2508 * unless it shows up in profiles, we're probably better leaving it in,
2509 * just in case we break detached signature processing at some point. */
2511 networkstatus_t
*v
= networkstatus_parse_vote_from_string(
2515 networkstatus_vote_free(v
);
2517 *msg_out
= "Signatures added";
2518 } else if (r
== 0) {
2519 *msg_out
= "Signatures ignored";
2527 *msg_out
= "Unrecognized error while adding detached signatures.";
2533 dirvote_add_signatures_to_all_pending_consensuses(
2534 const char *detached_signatures_body
,
2535 const char **msg_out
)
2537 int r
=0, i
, n_added
= 0, errors
= 0;
2538 ns_detached_signatures_t
*sigs
;
2539 tor_assert(detached_signatures_body
);
2540 tor_assert(msg_out
);
2541 tor_assert(pending_consensus_signatures
);
2543 if (!(sigs
= networkstatus_parse_detached_signatures(
2544 detached_signatures_body
, NULL
))) {
2545 *msg_out
= "Couldn't parse detached signatures.";
2549 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2551 pending_consensus_t
*pc
= &pending_consensuses
[i
];
2554 res
= dirvote_add_signatures_to_pending_consensus(pc
, sigs
, msg_out
);
2561 if (errors
&& !n_added
) {
2566 if (n_added
&& pending_consensuses
[FLAV_NS
].consensus
) {
2567 char *new_detached
=
2568 get_detached_signatures_from_pending_consensuses(
2569 pending_consensuses
, N_CONSENSUS_FLAVORS
);
2571 tor_free(pending_consensus_signatures
);
2572 pending_consensus_signatures
= new_detached
;
2580 *msg_out
= "Unrecognized error while adding detached signatures.";
2582 ns_detached_signatures_free(sigs
);
2583 /* XXXX NM Check how return is used. We can now have an error *and*
2584 signatures added. */
2588 /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
2589 * signatures on the currently pending consensus. Add them to the pending
2590 * consensus (if we have one); otherwise queue them until we have a
2591 * consensus. Return negative on failure, nonnegative on success. */
2593 dirvote_add_signatures(const char *detached_signatures_body
,
2597 if (pending_consensuses
[FLAV_NS
].consensus
) {
2598 log_notice(LD_DIR
, "Got a signature from %s. "
2599 "Adding it to the pending consensus.", source
);
2600 return dirvote_add_signatures_to_all_pending_consensuses(
2601 detached_signatures_body
, msg
);
2603 log_notice(LD_DIR
, "Got a signature from %s. "
2604 "Queuing it for the next consensus.", source
);
2605 if (!pending_consensus_signature_list
)
2606 pending_consensus_signature_list
= smartlist_create();
2607 smartlist_add(pending_consensus_signature_list
,
2608 tor_strdup(detached_signatures_body
));
2609 *msg
= "Signature queued";
2614 /** Replace the consensus that we're currently serving with the one that we've
2615 * been building. (V3 Authority only) */
2617 dirvote_publish_consensus(void)
2621 /* Now remember all the other consensuses as if we were a directory cache. */
2622 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2623 pending_consensus_t
*pending
= &pending_consensuses
[i
];
2625 name
= networkstatus_get_flavor_name(i
);
2627 if (!pending
->consensus
||
2628 networkstatus_check_consensus_signature(pending
->consensus
, 1)<0) {
2629 log_warn(LD_DIR
, "Not enough info to publish pending %s consensus",name
);
2633 if (networkstatus_set_current_consensus(pending
->body
, name
, 0))
2634 log_warn(LD_DIR
, "Error publishing %s consensus", name
);
2636 log_notice(LD_DIR
, "Published %s consensus", name
);
2642 /** Release all static storage held in dirvote.c */
2644 dirvote_free_all(void)
2646 dirvote_clear_votes(1);
2647 /* now empty as a result of clear_pending_votes. */
2648 smartlist_free(pending_vote_list
);
2649 pending_vote_list
= NULL
;
2650 smartlist_free(previous_vote_list
);
2651 previous_vote_list
= NULL
;
2653 dirvote_clear_pending_consensuses();
2654 tor_free(pending_consensus_signatures
);
2655 if (pending_consensus_signature_list
) {
2656 /* now empty as a result of clear_pending_votes. */
2657 smartlist_free(pending_consensus_signature_list
);
2658 pending_consensus_signature_list
= NULL
;
2663 * Access to pending items.
2666 /** Return the body of the consensus that we're currently trying to build. */
2668 dirvote_get_pending_consensus(consensus_flavor_t flav
)
2670 tor_assert(((int)flav
) >= 0 && flav
< N_CONSENSUS_FLAVORS
);
2671 return pending_consensuses
[flav
].body
;
2674 /** Return the signatures that we know for the consensus that we're currently
2675 * trying to build. */
2677 dirvote_get_pending_detached_signatures(void)
2679 return pending_consensus_signatures
;
2682 /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
2683 * vote for the authority with the v3 authority identity key digest <b>fp</b>;
2684 * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
2685 * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
2686 * false, do not consider any votes for a consensus that's already been built.
2687 * If <b>include_pending</b> is false, do not consider any votes for the
2688 * consensus that's in progress. May return NULL if we have no vote for the
2689 * authority in question. */
2690 const cached_dir_t
*
2691 dirvote_get_vote(const char *fp
, int flags
)
2693 int by_id
= flags
& DGV_BY_ID
;
2694 const int include_pending
= flags
& DGV_INCLUDE_PENDING
;
2695 const int include_previous
= flags
& DGV_INCLUDE_PREVIOUS
;
2697 if (!pending_vote_list
&& !previous_vote_list
)
2700 authority_cert_t
*c
= get_my_v3_authority_cert();
2702 fp
= c
->cache_info
.identity_digest
;
2708 if (pending_vote_list
&& include_pending
) {
2709 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
2710 if (!memcmp(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
2711 return pv
->vote_body
);
2713 if (previous_vote_list
&& include_previous
) {
2714 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
2715 if (!memcmp(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
2716 return pv
->vote_body
);
2719 if (pending_vote_list
&& include_pending
) {
2720 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
2721 if (!memcmp(pv
->vote
->digests
.d
[DIGEST_SHA1
], fp
, DIGEST_LEN
))
2722 return pv
->vote_body
);
2724 if (previous_vote_list
&& include_previous
) {
2725 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
2726 if (!memcmp(pv
->vote
->digests
.d
[DIGEST_SHA1
], fp
, DIGEST_LEN
))
2727 return pv
->vote_body
);
2733 /** Construct and return a new microdescriptor from a routerinfo <b>ri</b>.
2735 * XXX Right now, there is only one way to generate microdescriptors from
2736 * router descriptors. This may change in future consensus methods. If so,
2737 * we'll need an internal way to remember which method we used, and ask for a
2738 * particular method.
2741 dirvote_create_microdescriptor(const routerinfo_t
*ri
)
2743 microdesc_t
*result
= NULL
;
2744 char *key
= NULL
, *summary
= NULL
, *family
= NULL
;
2747 char *out
= buf
, *end
= buf
+sizeof(buf
);
2749 if (crypto_pk_write_public_key_to_string(ri
->onion_pkey
, &key
, &keylen
)<0)
2751 summary
= policy_summarize(ri
->exit_policy
);
2752 if (ri
->declared_family
)
2753 family
= smartlist_join_strings(ri
->declared_family
, " ", 0, NULL
);
2755 if (tor_snprintf(out
, end
-out
, "onion-key\n%s", key
)<0)
2759 if (tor_snprintf(out
, end
-out
, "family %s\n", family
)<0)
2763 if (summary
&& strcmp(summary
, "reject 1-65535")) {
2764 if (tor_snprintf(out
, end
-out
, "p %s\n", summary
)<0)
2768 *out
= '\0'; /* Make sure it's nul-terminated. This should be a no-op */
2771 smartlist_t
*lst
= microdescs_parse_from_string(buf
, out
, 0, 1);
2772 if (smartlist_len(lst
) != 1) {
2773 log_warn(LD_DIR
, "We generated a microdescriptor we couldn't parse.");
2774 SMARTLIST_FOREACH(lst
, microdesc_t
*, md
, microdesc_free(md
));
2775 smartlist_free(lst
);
2778 result
= smartlist_get(lst
, 0);
2779 smartlist_free(lst
);
2789 /** Cached space-separated string to hold */
2790 static char *microdesc_consensus_methods
= NULL
;
2792 /** Format the appropriate vote line to describe the microdescriptor <b>md</b>
2793 * in a consensus vote document. Write it into the <b>out_len</b>-byte buffer
2794 * in <b>out</b>. Return -1 on failure and the number of characters written
2797 dirvote_format_microdesc_vote_line(char *out
, size_t out_len
,
2798 const microdesc_t
*md
)
2800 char d64
[BASE64_DIGEST256_LEN
+1];
2801 if (!microdesc_consensus_methods
) {
2802 microdesc_consensus_methods
=
2803 make_consensus_method_list(MIN_METHOD_FOR_MICRODESC
,
2804 MAX_SUPPORTED_CONSENSUS_METHOD
,
2806 tor_assert(microdesc_consensus_methods
);
2808 if (digest256_to_base64(d64
, md
->digest
)<0)
2811 if (tor_snprintf(out
, out_len
, "m %s sha256=%s\n",
2812 microdesc_consensus_methods
, d64
)<0)
2818 /** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with
2819 * the digest algorithm <b>alg</b>, decode it and copy it into
2820 * <b>digest256_out</b> and return 0. Otherwise return -1. */
2822 vote_routerstatus_find_microdesc_hash(char *digest256_out
,
2823 const vote_routerstatus_t
*vrs
,
2825 digest_algorithm_t alg
)
2827 /* XXXX only returns the sha256 method. */
2828 const vote_microdesc_hash_t
*h
;
2833 tor_snprintf(mstr
, sizeof(mstr
), "%d", method
);
2834 mlen
= strlen(mstr
);
2835 tor_snprintf(dstr
, sizeof(dstr
), " %s=",
2836 crypto_digest_algorithm_get_name(alg
));
2838 for (h
= vrs
->microdesc
; h
; h
= h
->next
) {
2839 const char *cp
= h
->microdesc_hash_line
;
2841 /* cp looks like \d+(,\d+)* (digesttype=val )+ . Let's hunt for mstr in
2842 * the first part. */
2844 num_len
= strspn(cp
, "1234567890");
2845 if (num_len
== mlen
&& !memcmp(mstr
, cp
, mlen
)) {
2846 /* This is the line. */
2847 char buf
[BASE64_DIGEST256_LEN
+1];
2848 /* XXXX ignores extraneous stuff if the digest is too long. This
2849 * seems harmless enough, right? */
2850 cp
= strstr(cp
, dstr
);
2854 strlcpy(buf
, cp
, sizeof(buf
));
2855 return digest256_from_base64(digest256_out
, buf
);
2857 if (num_len
== 0 || cp
[num_len
] != ',')