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-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
8 * \file networkstatus.c
9 * \brief Functions and structures for handling networkstatus documents as a
10 * client or as a directory cache.
12 * A consensus networkstatus object is created by the directory
13 * authorities. It authenticates a set of network parameters--most
14 * importantly, the list of all the relays in the network. This list
15 * of relays is represented as an array of routerstatus_t objects.
17 * There are currently two flavors of consensus. With the older "NS"
18 * flavor, each relay is associated with a digest of its router
19 * descriptor. Tor instances that use this consensus keep the list of
20 * router descriptors as routerinfo_t objects stored and managed in
21 * routerlist.c. With the newer "microdesc" flavor, each relay is
22 * associated with a digest of the microdescriptor that the authorities
23 * made for it. These are stored and managed in microdesc.c. Information
24 * about the router is divided between the the networkstatus and the
25 * microdescriptor according to the general rule that microdescriptors
26 * should hold information that changes much less frequently than the
27 * information in the networkstatus.
29 * Modern clients use microdescriptor networkstatuses. Directory caches
30 * need to keep both kinds of networkstatus document, so they can serve them.
32 * This module manages fetching, holding, storing, updating, and
33 * validating networkstatus objects. The download-and-validate process
34 * is slightly complicated by the fact that the keys you need to
35 * validate a consensus are stored in the authority certificates, which
36 * you might not have yet when you download the consensus.
39 #define NETWORKSTATUS_PRIVATE
40 #include "core/or/or.h"
41 #include "app/config/config.h"
42 #include "core/mainloop/connection.h"
43 #include "core/mainloop/mainloop.h"
44 #include "core/mainloop/netstatus.h"
45 #include "core/or/channel.h"
46 #include "core/or/channelpadding.h"
47 #include "core/or/circuitpadding.h"
48 #include "core/or/circuitmux.h"
49 #include "core/or/circuitmux_ewma.h"
50 #include "core/or/circuitstats.h"
51 #include "core/or/connection_edge.h"
52 #include "core/or/connection_or.h"
53 #include "core/or/dos.h"
54 #include "core/or/protover.h"
55 #include "core/or/relay.h"
56 #include "core/or/scheduler.h"
57 #include "core/or/versions.h"
58 #include "feature/client/bridges.h"
59 #include "feature/client/entrynodes.h"
60 #include "feature/client/transports.h"
61 #include "feature/control/control_events.h"
62 #include "feature/dirauth/reachability.h"
63 #include "feature/dircache/consdiffmgr.h"
64 #include "feature/dircache/dirserv.h"
65 #include "feature/dirclient/dirclient.h"
66 #include "feature/dirclient/dlstatus.h"
67 #include "feature/dircommon/directory.h"
68 #include "feature/dircommon/voting_schedule.h"
69 #include "feature/dirparse/ns_parse.h"
70 #include "feature/hibernate/hibernate.h"
71 #include "feature/hs/hs_dos.h"
72 #include "feature/nodelist/authcert.h"
73 #include "feature/nodelist/dirlist.h"
74 #include "feature/nodelist/fmt_routerstatus.h"
75 #include "feature/nodelist/microdesc.h"
76 #include "feature/nodelist/networkstatus.h"
77 #include "feature/nodelist/node_select.h"
78 #include "feature/nodelist/nodelist.h"
79 #include "feature/nodelist/routerinfo.h"
80 #include "feature/nodelist/routerlist.h"
81 #include "feature/nodelist/torcert.h"
82 #include "feature/relay/routermode.h"
83 #include "lib/crypt_ops/crypto_rand.h"
84 #include "lib/crypt_ops/crypto_util.h"
86 #include "feature/dirauth/dirauth_periodic.h"
87 #include "feature/dirauth/dirvote.h"
88 #include "feature/dirauth/authmode.h"
89 #include "feature/dirauth/shared_random.h"
90 #include "feature/dirauth/voteflags.h"
92 #include "feature/nodelist/authority_cert_st.h"
93 #include "feature/dircommon/dir_connection_st.h"
94 #include "feature/dirclient/dir_server_st.h"
95 #include "feature/nodelist/document_signature_st.h"
96 #include "feature/nodelist/networkstatus_st.h"
97 #include "feature/nodelist/networkstatus_voter_info_st.h"
98 #include "feature/dirauth/ns_detached_signatures_st.h"
99 #include "feature/nodelist/node_st.h"
100 #include "feature/nodelist/routerinfo_st.h"
101 #include "feature/nodelist/routerlist_st.h"
102 #include "feature/dirauth/vote_microdesc_hash_st.h"
103 #include "feature/nodelist/vote_routerstatus_st.h"
104 #include "routerstatus_st.h"
110 /** Most recently received and validated v3 "ns"-flavored consensus network
112 STATIC networkstatus_t
*current_ns_consensus
= NULL
;
114 /** Most recently received and validated v3 "microdesc"-flavored consensus
116 STATIC networkstatus_t
*current_md_consensus
= NULL
;
118 /** A v3 consensus networkstatus that we've received, but which we don't
119 * have enough certificates to be happy about. */
120 typedef struct consensus_waiting_for_certs_t
{
121 /** The consensus itself. */
122 networkstatus_t
*consensus
;
123 /** When did we set the current value of consensus_waiting_for_certs? If
124 * this is too recent, we shouldn't try to fetch a new consensus for a
125 * little while, to give ourselves time to get certificates for this one. */
127 /** Set to 1 if we've been holding on to it for so long we should maybe
128 * treat it as being bad. */
130 } consensus_waiting_for_certs_t
;
132 /** An array, for each flavor of consensus we might want, of consensuses that
133 * we have downloaded, but which we cannot verify due to having insufficient
134 * authority certificates. */
135 static consensus_waiting_for_certs_t
136 consensus_waiting_for_certs
[N_CONSENSUS_FLAVORS
];
138 /** A time before which we shouldn't try to replace the current consensus:
139 * this will be at some point after the next consensus becomes valid, but
140 * before the current consensus becomes invalid. */
141 static time_t time_to_download_next_consensus
[N_CONSENSUS_FLAVORS
];
142 /** Download status for the current consensus networkstatus. */
143 static download_status_t consensus_dl_status
[N_CONSENSUS_FLAVORS
] =
145 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
146 DL_SCHED_INCREMENT_FAILURE
, 0, 0 },
147 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
148 DL_SCHED_INCREMENT_FAILURE
, 0, 0 },
151 #define N_CONSENSUS_BOOTSTRAP_SCHEDULES 2
152 #define CONSENSUS_BOOTSTRAP_SOURCE_AUTHORITY 0
153 #define CONSENSUS_BOOTSTRAP_SOURCE_ANY_DIRSERVER 1
155 /* Using DL_SCHED_INCREMENT_ATTEMPT on these schedules means that
156 * download_status_increment_failure won't increment these entries.
157 * However, any bootstrap connection failures that occur after we have
158 * a valid consensus will count against the failure counts on the non-bootstrap
159 * schedules. There should only be one of these, as all the others will have
160 * been cancelled. (This doesn't seem to be a significant issue.) */
161 static download_status_t
162 consensus_bootstrap_dl_status
[N_CONSENSUS_BOOTSTRAP_SCHEDULES
] =
164 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_AUTHORITY
,
165 DL_SCHED_INCREMENT_ATTEMPT
, 0, 0 },
166 /* During bootstrap, DL_WANT_ANY_DIRSERVER means "use fallbacks". */
167 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
168 DL_SCHED_INCREMENT_ATTEMPT
, 0, 0 },
171 /** True iff we have logged a warning about this OR's version being older than
172 * listed by the authorities. */
173 static int have_warned_about_old_version
= 0;
174 /** True iff we have logged a warning about this OR's version being newer than
175 * listed by the authorities. */
176 static int have_warned_about_new_version
= 0;
178 static void update_consensus_bootstrap_multiple_downloads(
180 const or_options_t
*options
);
181 static int networkstatus_check_required_protocols(const networkstatus_t
*ns
,
184 static int reload_consensus_from_file(const char *fname
,
187 const char *source_dir
);
189 /** Forget that we've warned about anything networkstatus-related, so we will
190 * give fresh warnings if the same behavior happens again. */
192 networkstatus_reset_warnings(void)
194 SMARTLIST_FOREACH(nodelist_get_list(), node_t
*, node
,
195 node
->name_lookup_warned
= 0);
197 have_warned_about_old_version
= 0;
198 have_warned_about_new_version
= 0;
201 /** Reset the descriptor download failure count on all networkstatus docs, so
202 * that we can retry any long-failed documents immediately.
205 networkstatus_reset_download_failures(void)
209 log_debug(LD_GENERAL
,
210 "In networkstatus_reset_download_failures()");
212 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
)
213 download_status_reset(&consensus_dl_status
[i
]);
215 for (i
=0; i
< N_CONSENSUS_BOOTSTRAP_SCHEDULES
; ++i
)
216 download_status_reset(&consensus_bootstrap_dl_status
[i
]);
219 /** Return the filename used to cache the consensus of a given flavor */
221 networkstatus_get_cache_fname
,(int flav
,
222 const char *flavorname
,
223 int unverified_consensus
))
227 if (unverified_consensus
) {
228 prefix
= "unverified";
232 if (flav
== FLAV_NS
) {
233 tor_snprintf(buf
, sizeof(buf
), "%s-consensus", prefix
);
235 tor_snprintf(buf
, sizeof(buf
), "%s-%s-consensus", prefix
, flavorname
);
238 return get_cachedir_fname(buf
);
242 * Read and and return the cached consensus of type <b>flavorname</b>. If
243 * <b>unverified</b> is false, get the one we haven't verified. Return NULL if
244 * the file isn't there. */
246 networkstatus_map_cached_consensus_impl(int flav
,
247 const char *flavorname
,
248 int unverified_consensus
)
250 char *filename
= networkstatus_get_cache_fname(flav
,
252 unverified_consensus
);
253 tor_mmap_t
*result
= tor_mmap_file(filename
);
258 /** Map the file containing the current cached consensus of flavor
259 * <b>flavorname</b> */
261 networkstatus_map_cached_consensus(const char *flavorname
)
263 int flav
= networkstatus_parse_flavor_name(flavorname
);
266 return networkstatus_map_cached_consensus_impl(flav
, flavorname
, 0);
269 /** Read every cached v3 consensus networkstatus from the disk. */
271 router_reload_consensus_networkstatus(void)
273 const unsigned int flags
= NSSET_FROM_CACHE
| NSSET_DONT_DOWNLOAD_CERTS
;
276 /* FFFF Suppress warnings if cached consensus is bad? */
277 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
278 const char *flavor
= networkstatus_get_flavor_name(flav
);
279 char *fname
= networkstatus_get_cache_fname(flav
, flavor
, 0);
280 reload_consensus_from_file(fname
, flavor
, flags
, NULL
);
283 fname
= networkstatus_get_cache_fname(flav
, flavor
, 1);
284 reload_consensus_from_file(fname
, flavor
,
285 flags
| NSSET_WAS_WAITING_FOR_CERTS
,
290 update_certificate_downloads(time(NULL
));
292 routers_update_all_from_networkstatus(time(NULL
), 3);
293 update_microdescs_from_networkstatus(time(NULL
));
298 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
300 vote_routerstatus_free_(vote_routerstatus_t
*rs
)
302 vote_microdesc_hash_t
*h
, *next
;
305 tor_free(rs
->version
);
306 tor_free(rs
->protocols
);
307 tor_free(rs
->status
.exitsummary
);
308 for (h
= rs
->microdesc
; h
; h
= next
) {
309 tor_free(h
->microdesc_hash_line
);
316 /** Free all storage held by the routerstatus object <b>rs</b>. */
318 routerstatus_free_(routerstatus_t
*rs
)
322 tor_free(rs
->exitsummary
);
326 /** Free all storage held in <b>sig</b> */
328 document_signature_free_(document_signature_t
*sig
)
330 tor_free(sig
->signature
);
334 /** Return a newly allocated copy of <b>sig</b> */
335 document_signature_t
*
336 document_signature_dup(const document_signature_t
*sig
)
338 document_signature_t
*r
= tor_memdup(sig
, sizeof(document_signature_t
));
340 r
->signature
= tor_memdup(sig
->signature
, sig
->signature_len
);
344 /** Free all storage held in <b>ns</b>. */
346 networkstatus_vote_free_(networkstatus_t
*ns
)
351 tor_free(ns
->client_versions
);
352 tor_free(ns
->server_versions
);
353 tor_free(ns
->recommended_client_protocols
);
354 tor_free(ns
->recommended_relay_protocols
);
355 tor_free(ns
->required_client_protocols
);
356 tor_free(ns
->required_relay_protocols
);
358 if (ns
->known_flags
) {
359 SMARTLIST_FOREACH(ns
->known_flags
, char *, c
, tor_free(c
));
360 smartlist_free(ns
->known_flags
);
362 if (ns
->weight_params
) {
363 SMARTLIST_FOREACH(ns
->weight_params
, char *, c
, tor_free(c
));
364 smartlist_free(ns
->weight_params
);
366 if (ns
->net_params
) {
367 SMARTLIST_FOREACH(ns
->net_params
, char *, c
, tor_free(c
));
368 smartlist_free(ns
->net_params
);
370 if (ns
->supported_methods
) {
371 SMARTLIST_FOREACH(ns
->supported_methods
, char *, c
, tor_free(c
));
372 smartlist_free(ns
->supported_methods
);
374 if (ns
->package_lines
) {
375 SMARTLIST_FOREACH(ns
->package_lines
, char *, c
, tor_free(c
));
376 smartlist_free(ns
->package_lines
);
379 SMARTLIST_FOREACH_BEGIN(ns
->voters
, networkstatus_voter_info_t
*, voter
) {
380 tor_free(voter
->nickname
);
381 tor_free(voter
->address
);
382 tor_free(voter
->contact
);
384 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
385 document_signature_free(sig
));
386 smartlist_free(voter
->sigs
);
389 } SMARTLIST_FOREACH_END(voter
);
390 smartlist_free(ns
->voters
);
392 authority_cert_free(ns
->cert
);
394 if (ns
->routerstatus_list
) {
395 if (ns
->type
== NS_TYPE_VOTE
|| ns
->type
== NS_TYPE_OPINION
) {
396 SMARTLIST_FOREACH(ns
->routerstatus_list
, vote_routerstatus_t
*, rs
,
397 vote_routerstatus_free(rs
));
399 SMARTLIST_FOREACH(ns
->routerstatus_list
, routerstatus_t
*, rs
,
400 routerstatus_free(rs
));
403 smartlist_free(ns
->routerstatus_list
);
406 if (ns
->bw_file_headers
) {
407 SMARTLIST_FOREACH(ns
->bw_file_headers
, char *, c
, tor_free(c
));
408 smartlist_free(ns
->bw_file_headers
);
411 digestmap_free(ns
->desc_digest_map
, NULL
);
413 if (ns
->sr_info
.commits
) {
414 dirvote_clear_commits(ns
);
416 tor_free(ns
->sr_info
.previous_srv
);
417 tor_free(ns
->sr_info
.current_srv
);
419 memwipe(ns
, 11, sizeof(*ns
));
423 /** Return the voter info from <b>vote</b> for the voter whose identity digest
424 * is <b>identity</b>, or NULL if no such voter is associated with
426 networkstatus_voter_info_t
*
427 networkstatus_get_voter_by_id(networkstatus_t
*vote
,
428 const char *identity
)
430 if (!vote
|| !vote
->voters
)
432 SMARTLIST_FOREACH(vote
->voters
, networkstatus_voter_info_t
*, voter
,
433 if (fast_memeq(voter
->identity_digest
, identity
, DIGEST_LEN
))
438 /** Return the signature made by <b>voter</b> using the algorithm
439 * <b>alg</b>, or NULL if none is found. */
440 document_signature_t
*
441 networkstatus_get_voter_sig_by_alg(const networkstatus_voter_info_t
*voter
,
442 digest_algorithm_t alg
)
446 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
452 /** Check whether the signature <b>sig</b> is correctly signed with the
453 * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
454 * signing key; otherwise set the good_signature or bad_signature flag on
455 * <b>voter</b>, and return 0. */
457 networkstatus_check_document_signature(const networkstatus_t
*consensus
,
458 document_signature_t
*sig
,
459 const authority_cert_t
*cert
)
461 char key_digest
[DIGEST_LEN
];
462 const int dlen
= sig
->alg
== DIGEST_SHA1
? DIGEST_LEN
: DIGEST256_LEN
;
464 size_t signed_digest_len
;
466 if (crypto_pk_get_digest(cert
->signing_key
, key_digest
)<0)
468 if (tor_memneq(sig
->signing_key_digest
, key_digest
, DIGEST_LEN
) ||
469 tor_memneq(sig
->identity_digest
, cert
->cache_info
.identity_digest
,
473 if (authority_cert_is_blacklisted(cert
)) {
474 /* We implement blacklisting for authority signing keys by treating
475 * all their signatures as always bad. That way we don't get into
476 * crazy loops of dropping and re-fetching signatures. */
477 log_warn(LD_DIR
, "Ignoring a consensus signature made with deprecated"
479 hex_str(cert
->signing_key_digest
, DIGEST_LEN
));
480 sig
->bad_signature
= 1;
484 signed_digest_len
= crypto_pk_keysize(cert
->signing_key
);
485 signed_digest
= tor_malloc(signed_digest_len
);
486 if (crypto_pk_public_checksig(cert
->signing_key
,
490 sig
->signature_len
) < dlen
||
491 tor_memneq(signed_digest
, consensus
->digests
.d
[sig
->alg
], dlen
)) {
492 log_warn(LD_DIR
, "Got a bad signature on a networkstatus vote");
493 sig
->bad_signature
= 1;
495 sig
->good_signature
= 1;
497 tor_free(signed_digest
);
501 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
502 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
503 * signature from every recognized authority on it, 0 if there are
504 * enough good signatures from recognized authorities on it, -1 if we might
505 * get enough good signatures by fetching missing certificates, and -2
506 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
507 * about every problem; if warn is at least 1, warn only if we can't get
508 * enough signatures; if warn is negative, log nothing at all. */
510 networkstatus_check_consensus_signature(networkstatus_t
*consensus
,
514 int n_missing_key
= 0, n_dl_failed_key
= 0;
517 int n_no_signature
= 0;
518 int n_v3_authorities
= get_n_authorities(V3_DIRINFO
);
519 int n_required
= n_v3_authorities
/2 + 1;
520 smartlist_t
*list_good
= smartlist_new();
521 smartlist_t
*list_no_signature
= smartlist_new();
522 smartlist_t
*need_certs_from
= smartlist_new();
523 smartlist_t
*unrecognized
= smartlist_new();
524 smartlist_t
*missing_authorities
= smartlist_new();
526 time_t now
= time(NULL
);
528 tor_assert(consensus
->type
== NS_TYPE_CONSENSUS
);
530 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*,
534 int unknown_here
= 0;
535 int missing_key_here
= 0, dl_failed_key_here
= 0;
536 SMARTLIST_FOREACH_BEGIN(voter
->sigs
, document_signature_t
*, sig
) {
537 if (!sig
->good_signature
&& !sig
->bad_signature
&&
539 /* we can try to check the signature. */
540 int is_v3_auth
= trusteddirserver_get_by_v3_auth_digest(
541 sig
->identity_digest
) != NULL
;
542 authority_cert_t
*cert
=
543 authority_cert_get_by_digests(sig
->identity_digest
,
544 sig
->signing_key_digest
);
545 tor_assert(tor_memeq(sig
->identity_digest
, voter
->identity_digest
,
549 smartlist_add(unrecognized
, voter
);
552 } else if (!cert
|| cert
->expires
< now
) {
553 smartlist_add(need_certs_from
, voter
);
555 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
556 ++dl_failed_key_here
;
559 if (networkstatus_check_document_signature(consensus
, sig
, cert
) < 0) {
560 smartlist_add(need_certs_from
, voter
);
562 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
563 ++dl_failed_key_here
;
567 if (sig
->good_signature
)
569 else if (sig
->bad_signature
)
571 } SMARTLIST_FOREACH_END(sig
);
575 smartlist_add(list_good
, voter
->nickname
);
576 } else if (bad_here
) {
578 } else if (missing_key_here
) {
580 if (dl_failed_key_here
)
582 } else if (unknown_here
) {
586 smartlist_add(list_no_signature
, voter
->nickname
);
588 } SMARTLIST_FOREACH_END(voter
);
590 /* Now see whether we're missing any voters entirely. */
591 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
594 if ((ds
->type
& V3_DIRINFO
) &&
595 !networkstatus_get_voter_by_id(consensus
, ds
->v3_identity_digest
))
596 smartlist_add(missing_authorities
, ds
);
599 if (warn
> 1 || (warn
>= 0 &&
600 (n_good
+ n_missing_key
- n_dl_failed_key
< n_required
))) {
607 SMARTLIST_FOREACH(unrecognized
, networkstatus_voter_info_t
*, voter
,
609 tor_log(severity
, LD_DIR
, "Consensus includes unrecognized authority "
610 "'%s' at %s:%d (contact %s; identity %s)",
611 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
612 voter
->contact
?voter
->contact
:"n/a",
613 hex_str(voter
->identity_digest
, DIGEST_LEN
));
615 SMARTLIST_FOREACH(need_certs_from
, networkstatus_voter_info_t
*, voter
,
617 tor_log(severity
, LD_DIR
, "Looks like we need to download a new "
618 "certificate from authority '%s' at %s:%d (contact %s; "
620 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
621 voter
->contact
?voter
->contact
:"n/a",
622 hex_str(voter
->identity_digest
, DIGEST_LEN
));
624 SMARTLIST_FOREACH(missing_authorities
, dir_server_t
*, ds
,
626 tor_log(severity
, LD_DIR
, "Consensus does not include configured "
627 "authority '%s' at %s:%d (identity %s)",
628 ds
->nickname
, ds
->address
, (int)ds
->dir_port
,
629 hex_str(ds
->v3_identity_digest
, DIGEST_LEN
));
633 smartlist_t
*sl
= smartlist_new();
634 char *tmp
= smartlist_join_strings(list_good
, " ", 0, NULL
);
635 smartlist_add_asprintf(sl
,
636 "A consensus needs %d good signatures from recognized "
637 "authorities for us to accept it. "
638 "This %s one has %d (%s).",
640 networkstatus_get_flavor_name(consensus
->flavor
),
643 if (n_no_signature
) {
644 tmp
= smartlist_join_strings(list_no_signature
, " ", 0, NULL
);
645 smartlist_add_asprintf(sl
,
646 "%d (%s) of the authorities we know didn't sign it.",
647 n_no_signature
, tmp
);
651 smartlist_add_asprintf(sl
,
652 "It has %d signatures from authorities we don't "
653 "recognize.", n_unknown
);
656 smartlist_add_asprintf(sl
, "%d of the signatures on it didn't verify "
657 "correctly.", n_bad
);
660 smartlist_add_asprintf(sl
,
661 "We were unable to check %d of the signatures, "
662 "because we were missing the keys.", n_missing_key
);
664 joined
= smartlist_join_strings(sl
, " ", 0, NULL
);
665 tor_log(severity
, LD_DIR
, "%s", joined
);
667 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
672 smartlist_free(list_good
);
673 smartlist_free(list_no_signature
);
674 smartlist_free(unrecognized
);
675 smartlist_free(need_certs_from
);
676 smartlist_free(missing_authorities
);
678 if (n_good
== n_v3_authorities
)
680 else if (n_good
>= n_required
)
682 else if (n_good
+ n_missing_key
>= n_required
)
688 /** How far in the future do we allow a network-status to get before removing
690 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
692 /** Helper for bsearching a list of routerstatus_t pointers: compare a
693 * digest in the key to the identity digest of a routerstatus_t. */
695 compare_digest_to_routerstatus_entry(const void *_key
, const void **_member
)
697 const char *key
= _key
;
698 const routerstatus_t
*rs
= *_member
;
699 return tor_memcmp(key
, rs
->identity_digest
, DIGEST_LEN
);
702 /** Helper for bsearching a list of routerstatus_t pointers: compare a
703 * digest in the key to the identity digest of a routerstatus_t. */
705 compare_digest_to_vote_routerstatus_entry(const void *_key
,
706 const void **_member
)
708 const char *key
= _key
;
709 const vote_routerstatus_t
*vrs
= *_member
;
710 return tor_memcmp(key
, vrs
->status
.identity_digest
, DIGEST_LEN
);
713 /** As networkstatus_find_entry, but do not return a const pointer */
715 networkstatus_vote_find_mutable_entry(networkstatus_t
*ns
, const char *digest
)
717 return smartlist_bsearch(ns
->routerstatus_list
, digest
,
718 compare_digest_to_routerstatus_entry
);
721 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
722 * NULL if none was found. */
723 MOCK_IMPL(const routerstatus_t
*,
724 networkstatus_vote_find_entry
,(networkstatus_t
*ns
, const char *digest
))
726 return networkstatus_vote_find_mutable_entry(ns
, digest
);
729 /*XXXX MOVE make this static once functions are moved into this file. */
730 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
731 * <b>digest</b>. Return value and set *<b>found_out</b> as for
732 * smartlist_bsearch_idx(). */
734 networkstatus_vote_find_entry_idx(networkstatus_t
*ns
,
735 const char *digest
, int *found_out
)
737 return smartlist_bsearch_idx(ns
->routerstatus_list
, digest
,
738 compare_digest_to_routerstatus_entry
,
742 /** As router_get_consensus_status_by_descriptor_digest, but does not return
743 * a const pointer. */
744 MOCK_IMPL(routerstatus_t
*,
745 router_get_mutable_consensus_status_by_descriptor_digest
,(
746 networkstatus_t
*consensus
,
750 consensus
= networkstatus_get_latest_consensus();
753 if (!consensus
->desc_digest_map
) {
754 digestmap_t
*m
= consensus
->desc_digest_map
= digestmap_new();
755 SMARTLIST_FOREACH(consensus
->routerstatus_list
,
756 routerstatus_t
*, rs
,
758 digestmap_set(m
, rs
->descriptor_digest
, rs
);
761 return digestmap_get(consensus
->desc_digest_map
, digest
);
764 /** Return the consensus view of the status of the router whose current
765 * <i>descriptor</i> digest in <b>consensus</b> is <b>digest</b>, or NULL if
766 * no such router is known. */
767 const routerstatus_t
*
768 router_get_consensus_status_by_descriptor_digest(networkstatus_t
*consensus
,
771 return router_get_mutable_consensus_status_by_descriptor_digest(
775 /** Return a smartlist of all router descriptor digests in a consensus */
777 router_get_descriptor_digests_in_consensus(networkstatus_t
*consensus
)
779 smartlist_t
*result
= smartlist_new();
785 for (i
= digestmap_iter_init(consensus
->desc_digest_map
);
786 !(digestmap_iter_done(i
));
787 i
= digestmap_iter_next(consensus
->desc_digest_map
, i
)) {
788 digestmap_iter_get(i
, &digest
, &rs
);
789 digest_tmp
= tor_malloc(DIGEST_LEN
);
790 memcpy(digest_tmp
, digest
, DIGEST_LEN
);
791 smartlist_add(result
, digest_tmp
);
797 /** Return a smartlist of all router descriptor digests in the current
799 MOCK_IMPL(smartlist_t
*,
800 router_get_descriptor_digests
,(void))
802 smartlist_t
*result
= NULL
;
804 if (current_ns_consensus
) {
806 router_get_descriptor_digests_in_consensus(current_ns_consensus
);
812 /** Given the digest of a router descriptor, return its current download
813 * status, or NULL if the digest is unrecognized. */
814 MOCK_IMPL(download_status_t
*,
815 router_get_dl_status_by_descriptor_digest
,(const char *d
))
818 if (!current_ns_consensus
)
820 if ((rs
= router_get_mutable_consensus_status_by_descriptor_digest(
821 current_ns_consensus
, d
)))
822 return &rs
->dl_status
;
827 /** As router_get_consensus_status_by_id, but do not return a const pointer */
829 router_get_mutable_consensus_status_by_id(const char *digest
)
831 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
834 smartlist_t
*rslist
= ns
->routerstatus_list
;
835 return smartlist_bsearch(rslist
, digest
,
836 compare_digest_to_routerstatus_entry
);
839 /** Return the consensus view of the status of the router whose identity
840 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
841 const routerstatus_t
*
842 router_get_consensus_status_by_id(const char *digest
)
844 return router_get_mutable_consensus_status_by_id(digest
);
847 /** How frequently do directory authorities re-download fresh networkstatus
849 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
851 /** How frequently do non-authority directory caches re-download fresh
852 * networkstatus documents? */
853 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
855 /** Return true iff, given the options listed in <b>options</b>, <b>flavor</b>
856 * is the flavor of a consensus networkstatus that we would like to fetch.
858 * For certificate fetches, use we_want_to_fetch_unknown_auth_certs, and
859 * for serving fetched documents, use directory_caches_dir_info. */
861 we_want_to_fetch_flavor(const or_options_t
*options
, int flavor
)
863 if (flavor
< 0 || flavor
> N_CONSENSUS_FLAVORS
) {
864 /* This flavor is crazy; we don't want it */
865 /*XXXX handle unrecognized flavors later */
868 if (authdir_mode_v3(options
) || directory_caches_dir_info(options
)) {
869 /* We want to serve all flavors to others, regardless if we would use
873 if (options
->FetchUselessDescriptors
) {
874 /* In order to get all descriptors, we need to fetch all consensuses. */
877 /* Otherwise, we want the flavor only if we want to use it to build
879 return flavor
== usable_consensus_flavor();
882 /** Return true iff, given the options listed in <b>options</b>, we would like
883 * to fetch and store unknown authority certificates.
885 * For consensus and descriptor fetches, use we_want_to_fetch_flavor, and
886 * for serving fetched certificates, use directory_caches_unknown_auth_certs.
889 we_want_to_fetch_unknown_auth_certs(const or_options_t
*options
)
891 if (authdir_mode_v3(options
) ||
892 directory_caches_unknown_auth_certs((options
))) {
893 /* We want to serve all certs to others, regardless if we would use
897 if (options
->FetchUselessDescriptors
) {
898 /* Unknown certificates are definitely useless. */
901 /* Otherwise, don't fetch unknown certificates. */
905 /** How long will we hang onto a possibly live consensus for which we're
906 * fetching certs before we check whether there is a better one? */
907 #define DELAY_WHILE_FETCHING_CERTS (20*60)
909 /** What is the minimum time we need to have waited fetching certs, before we
910 * increment the consensus download schedule on failure? */
911 #define MIN_DELAY_FOR_FETCH_CERT_STATUS_FAILURE (1*60)
913 /* Check if a downloaded consensus flavor should still wait for certificates
914 * to download now. If we decide not to wait, check if enough time has passed
915 * to consider the certificate download failure a separate failure. If so,
917 * If waiting for certificates to download, return 1. If not, return 0. */
919 check_consensus_waiting_for_certs(int flavor
, time_t now
,
920 download_status_t
*dls
)
922 consensus_waiting_for_certs_t
*waiting
;
924 /* We should always have a known flavor, because we_want_to_fetch_flavor()
925 * filters out unknown flavors. */
926 tor_assert(flavor
>= 0 && flavor
< N_CONSENSUS_FLAVORS
);
928 waiting
= &consensus_waiting_for_certs
[flavor
];
929 if (waiting
->consensus
) {
930 /* XXXX make sure this doesn't delay sane downloads. */
931 if (waiting
->set_at
+ DELAY_WHILE_FETCHING_CERTS
> now
&&
932 waiting
->consensus
->valid_until
> now
) {
935 if (!waiting
->dl_failed
) {
936 if (waiting
->set_at
+ MIN_DELAY_FOR_FETCH_CERT_STATUS_FAILURE
> now
) {
937 download_status_failed(dls
, 0);
939 waiting
->dl_failed
=1;
947 /** If we want to download a fresh consensus, launch a new download as
950 update_consensus_networkstatus_downloads(time_t now
)
953 const or_options_t
*options
= get_options();
954 const int we_are_bootstrapping
= networkstatus_consensus_is_bootstrapping(
956 const int use_multi_conn
=
957 networkstatus_consensus_can_use_multiple_directories(options
);
959 if (should_delay_dir_fetches(options
, NULL
))
962 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
963 /* XXXX need some way to download unknown flavors if we are caching. */
964 const char *resource
;
966 int max_in_progress_conns
= 1;
968 if (! we_want_to_fetch_flavor(options
, i
))
971 c
= networkstatus_get_latest_consensus_by_flavor(i
);
972 if (! (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
)) {
973 /* No live consensus? Get one now!*/
974 time_to_download_next_consensus
[i
] = now
;
977 if (time_to_download_next_consensus
[i
] > now
)
978 continue; /* Wait until the current consensus is older. */
980 resource
= networkstatus_get_flavor_name(i
);
982 /* Check if we already have enough connections in progress */
983 if (we_are_bootstrapping
&& use_multi_conn
) {
984 max_in_progress_conns
=
985 options
->ClientBootstrapConsensusMaxInProgressTries
;
987 if (connection_dir_count_by_purpose_and_resource(
988 DIR_PURPOSE_FETCH_CONSENSUS
,
990 >= max_in_progress_conns
) {
994 /* Check if we want to launch another download for a usable consensus.
995 * Only used during bootstrap. */
996 if (we_are_bootstrapping
&& use_multi_conn
997 && i
== usable_consensus_flavor()) {
999 /* Check if we're already downloading a usable consensus */
1000 if (networkstatus_consensus_is_already_downloading(resource
))
1003 /* Make multiple connections for a bootstrap consensus download. */
1004 update_consensus_bootstrap_multiple_downloads(now
, options
);
1006 /* Check if we failed downloading a consensus too recently */
1008 /* Let's make sure we remembered to update consensus_dl_status */
1009 tor_assert(consensus_dl_status
[i
].schedule
== DL_SCHED_CONSENSUS
);
1011 if (!download_status_is_ready(&consensus_dl_status
[i
], now
)) {
1015 /** Check if we're waiting for certificates to download. If we are,
1016 * launch download for missing directory authority certificates. */
1017 if (check_consensus_waiting_for_certs(i
, now
, &consensus_dl_status
[i
])) {
1018 update_certificate_downloads(now
);
1022 /* Try the requested attempt */
1023 log_info(LD_DIR
, "Launching %s standard networkstatus consensus "
1024 "download.", networkstatus_get_flavor_name(i
));
1025 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
1026 ROUTER_PURPOSE_GENERAL
, resource
,
1027 PDS_RETRY_IF_NO_SERVERS
,
1028 consensus_dl_status
[i
].want_authority
);
1033 /** When we're bootstrapping, launch one or more consensus download
1034 * connections, if schedule indicates connection(s) should be made after now.
1035 * If is_authority, connect to an authority, otherwise, use a fallback
1039 update_consensus_bootstrap_attempt_downloads(
1041 download_status_t
*dls
,
1042 download_want_authority_t want_authority
)
1044 const char *resource
= networkstatus_get_flavor_name(
1045 usable_consensus_flavor());
1047 /* Let's make sure we remembered to update schedule */
1048 tor_assert(dls
->schedule
== DL_SCHED_CONSENSUS
);
1050 /* Allow for multiple connections in the same second, if the schedule value
1052 while (download_status_is_ready(dls
, now
)) {
1053 log_info(LD_DIR
, "Launching %s bootstrap %s networkstatus consensus "
1054 "download.", resource
, (want_authority
== DL_WANT_AUTHORITY
1058 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
1059 ROUTER_PURPOSE_GENERAL
, resource
,
1060 PDS_RETRY_IF_NO_SERVERS
, want_authority
);
1061 /* schedule the next attempt */
1062 download_status_increment_attempt(dls
, resource
, now
);
1066 /** If we're bootstrapping, check the connection schedules and see if we want
1067 * to make additional, potentially concurrent, consensus download
1069 * Only call when bootstrapping, and when we want to make additional
1070 * connections. Only nodes that satisfy
1071 * networkstatus_consensus_can_use_multiple_directories make additional
1075 update_consensus_bootstrap_multiple_downloads(time_t now
,
1076 const or_options_t
*options
)
1078 const int usable_flavor
= usable_consensus_flavor();
1080 /* make sure we can use multiple connections */
1081 if (!networkstatus_consensus_can_use_multiple_directories(options
)) {
1085 /* Launch concurrent consensus download attempt(s) based on the mirror and
1086 * authority schedules. Try the mirror first - this makes it slightly more
1087 * likely that we'll connect to the fallback first, and then end the
1088 * authority connection attempt. */
1090 /* If a consensus download fails because it's waiting for certificates,
1091 * we'll fail both the authority and fallback schedules. This is better than
1092 * failing only one of the schedules, and having the other continue
1096 /* If we don't have or can't use extra fallbacks, don't try them. */
1097 if (networkstatus_consensus_can_use_extra_fallbacks(options
)) {
1098 download_status_t
*dls_f
=
1099 &consensus_bootstrap_dl_status
[CONSENSUS_BOOTSTRAP_SOURCE_ANY_DIRSERVER
];
1101 if (!check_consensus_waiting_for_certs(usable_flavor
, now
, dls_f
)) {
1102 /* During bootstrap, DL_WANT_ANY_DIRSERVER means "use fallbacks". */
1103 update_consensus_bootstrap_attempt_downloads(now
, dls_f
,
1104 DL_WANT_ANY_DIRSERVER
);
1108 /* Now try an authority. */
1109 download_status_t
*dls_a
=
1110 &consensus_bootstrap_dl_status
[CONSENSUS_BOOTSTRAP_SOURCE_AUTHORITY
];
1112 if (!check_consensus_waiting_for_certs(usable_flavor
, now
, dls_a
)) {
1113 update_consensus_bootstrap_attempt_downloads(now
, dls_a
,
1118 /** Called when an attempt to download a consensus fails: note that the
1119 * failure occurred, and possibly retry. */
1121 networkstatus_consensus_download_failed(int status_code
, const char *flavname
)
1123 int flav
= networkstatus_parse_flavor_name(flavname
);
1125 tor_assert(flav
< N_CONSENSUS_FLAVORS
);
1126 /* XXXX handle unrecognized flavors */
1127 download_status_failed(&consensus_dl_status
[flav
], status_code
);
1128 /* Retry immediately, if appropriate. */
1129 update_consensus_networkstatus_downloads(time(NULL
));
1133 /** How long do we (as a cache) wait after a consensus becomes non-fresh
1134 * before trying to fetch another? */
1135 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
1137 /** Update the time at which we'll consider replacing the current
1138 * consensus of flavor <b>flav</b> */
1140 update_consensus_networkstatus_fetch_time_impl(time_t now
, int flav
)
1142 const or_options_t
*options
= get_options();
1143 networkstatus_t
*c
= networkstatus_get_latest_consensus_by_flavor(flav
);
1144 const char *flavor
= networkstatus_get_flavor_name(flav
);
1145 if (! we_want_to_fetch_flavor(get_options(), flav
))
1148 if (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1150 long interval
= c
->fresh_until
- c
->valid_after
;
1151 long min_sec_before_caching
= CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1154 if (min_sec_before_caching
> interval
/16) {
1155 /* Usually we allow 2-minutes slop factor in case clocks get
1156 desynchronized a little. If we're on a private network with
1157 a crazy-fast voting interval, though, 2 minutes may be too
1159 min_sec_before_caching
= interval
/16;
1160 /* make sure we always delay by at least a second before caching */
1161 if (min_sec_before_caching
== 0) {
1162 min_sec_before_caching
= 1;
1166 if (directory_fetches_dir_info_early(options
)) {
1167 /* We want to cache the next one at some point after this one
1168 * is no longer fresh... */
1169 start
= (time_t)(c
->fresh_until
+ min_sec_before_caching
);
1170 /* Some clients may need the consensus sooner than others. */
1171 if (options
->FetchDirInfoExtraEarly
|| authdir_mode_v3(options
)) {
1173 if (min_sec_before_caching
+ dl_interval
> interval
)
1174 dl_interval
= interval
/2;
1176 /* But only in the first half-interval after that. */
1177 dl_interval
= interval
/2;
1180 /* We're an ordinary client, a bridge, or a hidden service.
1181 * Give all the caches enough time to download the consensus. */
1182 start
= (time_t)(c
->fresh_until
+ (interval
*3)/4);
1183 /* But download the next one well before this one is expired. */
1184 dl_interval
= ((c
->valid_until
- start
) * 7 )/ 8;
1186 /* If we're a bridge user, make use of the numbers we just computed
1187 * to choose the rest of the interval *after* them. */
1188 if (directory_fetches_dir_info_later(options
)) {
1189 /* Give all the *clients* enough time to download the consensus. */
1190 start
= (time_t)(start
+ dl_interval
+ min_sec_before_caching
);
1191 /* But try to get it before ours actually expires. */
1192 dl_interval
= (c
->valid_until
- start
) - min_sec_before_caching
;
1195 /* catch low dl_interval in crazy-fast networks */
1196 if (dl_interval
< 1)
1198 /* catch late start in crazy-fast networks */
1199 if (start
+dl_interval
>= c
->valid_until
)
1200 start
= c
->valid_until
- dl_interval
- 1;
1202 "fresh_until: %ld start: %ld "
1203 "dl_interval: %ld valid_until: %ld ",
1204 (long)c
->fresh_until
, (long)start
, dl_interval
,
1205 (long)c
->valid_until
);
1206 /* We must not try to replace c while it's still fresh: */
1207 tor_assert(c
->fresh_until
< start
);
1208 /* We must download the next one before c is invalid: */
1209 tor_assert(start
+dl_interval
< c
->valid_until
);
1210 time_to_download_next_consensus
[flav
] =
1211 start
+ crypto_rand_int((int)dl_interval
);
1213 char tbuf1
[ISO_TIME_LEN
+1];
1214 char tbuf2
[ISO_TIME_LEN
+1];
1215 char tbuf3
[ISO_TIME_LEN
+1];
1216 format_local_iso_time(tbuf1
, c
->fresh_until
);
1217 format_local_iso_time(tbuf2
, c
->valid_until
);
1218 format_local_iso_time(tbuf3
, time_to_download_next_consensus
[flav
]);
1219 log_info(LD_DIR
, "Live %s consensus %s the most recent until %s and "
1220 "will expire at %s; fetching the next one at %s.",
1221 flavor
, (c
->fresh_until
> now
) ? "will be" : "was",
1222 tbuf1
, tbuf2
, tbuf3
);
1225 time_to_download_next_consensus
[flav
] = now
;
1226 log_info(LD_DIR
, "No live %s consensus; we should fetch one immediately.",
1231 /** Update the time at which we'll consider replacing the current
1232 * consensus of flavor 'flavor' */
1234 update_consensus_networkstatus_fetch_time(time_t now
)
1237 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1238 if (we_want_to_fetch_flavor(get_options(), i
))
1239 update_consensus_networkstatus_fetch_time_impl(now
, i
);
1243 /** Return 1 if there's a reason we shouldn't try any directory
1244 * fetches yet (e.g. we demand bridges and none are yet known).
1247 * If we return 1 and <b>msg_out</b> is provided, set <b>msg_out</b>
1248 * to an explanation of why directory fetches are delayed. (If we
1249 * return 0, we set msg_out to NULL.)
1252 should_delay_dir_fetches(const or_options_t
*options
, const char **msg_out
)
1258 if (options
->DisableNetwork
) {
1260 *msg_out
= "DisableNetwork is set.";
1262 log_info(LD_DIR
, "Delaying dir fetches (DisableNetwork is set)");
1266 if (we_are_hibernating()) {
1268 *msg_out
= "We are hibernating or shutting down.";
1270 log_info(LD_DIR
, "Delaying dir fetches (Hibernating or shutting down)");
1274 if (options
->UseBridges
) {
1275 /* If we know that none of our bridges can possibly work, avoid fetching
1276 * directory documents. But if some of them might work, try again. */
1277 if (num_bridges_usable(1) == 0) {
1279 *msg_out
= "No running bridges";
1281 log_info(LD_DIR
, "Delaying dir fetches (no running bridges known)");
1285 if (pt_proxies_configuration_pending()) {
1287 *msg_out
= "Pluggable transport proxies still configuring";
1289 log_info(LD_DIR
, "Delaying dir fetches (pt proxies still configuring)");
1297 /** Launch requests for networkstatus documents as appropriate. This is called
1298 * when we retry all the connections on a SIGHUP and periodically by a Periodic
1299 * event which checks whether we want to download any networkstatus documents.
1302 update_networkstatus_downloads(time_t now
)
1304 const or_options_t
*options
= get_options();
1305 if (should_delay_dir_fetches(options
, NULL
))
1307 /** Launch a consensus download request, we will wait for the consensus to
1308 * download and when it completes we will launch a certificate download
1310 update_consensus_networkstatus_downloads(now
);
1313 /** Launch requests as appropriate for missing directory authority
1316 update_certificate_downloads(time_t now
)
1319 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1320 if (consensus_waiting_for_certs
[i
].consensus
)
1321 authority_certs_fetch_missing(consensus_waiting_for_certs
[i
].consensus
,
1325 if (current_ns_consensus
)
1326 authority_certs_fetch_missing(current_ns_consensus
, now
, NULL
);
1327 if (current_md_consensus
)
1328 authority_certs_fetch_missing(current_md_consensus
, now
, NULL
);
1331 /** Return 1 if we have a consensus but we don't have enough certificates
1332 * to start using it yet. */
1334 consensus_is_waiting_for_certs(void)
1336 return consensus_waiting_for_certs
[usable_consensus_flavor()].consensus
1340 /** Look up the currently active (depending on bootstrap status) download
1341 * status for this consensus flavor and return a pointer to it.
1343 MOCK_IMPL(download_status_t
*,
1344 networkstatus_get_dl_status_by_flavor
,(consensus_flavor_t flavor
))
1346 download_status_t
*dl
= NULL
;
1347 const int we_are_bootstrapping
=
1348 networkstatus_consensus_is_bootstrapping(time(NULL
));
1350 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1351 dl
= &((we_are_bootstrapping
?
1352 consensus_bootstrap_dl_status
: consensus_dl_status
)[flavor
]);
1358 /** Look up the bootstrap download status for this consensus flavor
1359 * and return a pointer to it. */
1360 MOCK_IMPL(download_status_t
*,
1361 networkstatus_get_dl_status_by_flavor_bootstrap
,(consensus_flavor_t flavor
))
1363 download_status_t
*dl
= NULL
;
1365 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1366 dl
= &(consensus_bootstrap_dl_status
[flavor
]);
1372 /** Look up the running (non-bootstrap) download status for this consensus
1373 * flavor and return a pointer to it. */
1374 MOCK_IMPL(download_status_t
*,
1375 networkstatus_get_dl_status_by_flavor_running
,(consensus_flavor_t flavor
))
1377 download_status_t
*dl
= NULL
;
1379 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1380 dl
= &(consensus_dl_status
[flavor
]);
1386 /** Return the most recent consensus that we have downloaded, or NULL if we
1387 * don't have one. May return future or expired consensuses. */
1388 MOCK_IMPL(networkstatus_t
*,
1389 networkstatus_get_latest_consensus
,(void))
1391 if (we_use_microdescriptors_for_circuits(get_options()))
1392 return current_md_consensus
;
1394 return current_ns_consensus
;
1397 /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
1398 * if we don't have one. May return future or expired consensuses. */
1399 MOCK_IMPL(networkstatus_t
*,
1400 networkstatus_get_latest_consensus_by_flavor
,(consensus_flavor_t f
))
1403 return current_ns_consensus
;
1404 else if (f
== FLAV_MICRODESC
)
1405 return current_md_consensus
;
1412 /** Return the most recent consensus that we have downloaded, or NULL if it is
1413 * no longer live. */
1414 MOCK_IMPL(networkstatus_t
*,
1415 networkstatus_get_live_consensus
,(time_t now
))
1417 networkstatus_t
*ns
= networkstatus_get_latest_consensus();
1418 if (ns
&& networkstatus_is_live(ns
, now
))
1424 /** Given a consensus in <b>ns</b>, return true iff currently live and
1427 networkstatus_is_live(const networkstatus_t
*ns
, time_t now
)
1429 return (ns
->valid_after
<= now
&& now
<= ns
->valid_until
);
1432 /** Determine if <b>consensus</b> is valid, or expired recently enough, or not
1433 * too far in the future, so that we can still use it.
1435 * Return 1 if the consensus is reasonably live, or 0 if it is too old or
1439 networkstatus_consensus_reasonably_live(const networkstatus_t
*consensus
,
1442 if (BUG(!consensus
))
1445 return networkstatus_valid_after_is_reasonably_live(consensus
->valid_after
,
1447 networkstatus_valid_until_is_reasonably_live(consensus
->valid_until
,
1451 #define REASONABLY_LIVE_TIME (24*60*60)
1453 /** As networkstatus_consensus_reasonably_live, but takes a valid_after
1454 * time, and checks to see if it is in the past, or not too far in the future.
1457 networkstatus_valid_after_is_reasonably_live(time_t valid_after
,
1460 return (now
>= valid_after
- REASONABLY_LIVE_TIME
);
1463 /** As networkstatus_consensus_reasonably_live, but takes a valid_until
1464 * time, and checks to see if it is in the future, or not too far in the past.
1467 networkstatus_valid_until_is_reasonably_live(time_t valid_until
,
1470 return (now
<= valid_until
+ REASONABLY_LIVE_TIME
);
1473 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1474 * and future consensuses. */
1475 MOCK_IMPL(networkstatus_t
*,
1476 networkstatus_get_reasonably_live_consensus
,(time_t now
, int flavor
))
1478 networkstatus_t
*consensus
=
1479 networkstatus_get_latest_consensus_by_flavor(flavor
);
1481 networkstatus_consensus_reasonably_live(consensus
, now
))
1487 /** Check if we need to download a consensus during tor's bootstrap phase.
1488 * If we have no consensus, or our consensus is unusably old, return 1.
1489 * As soon as we have received a consensus, return 0, even if we don't have
1490 * enough certificates to validate it.
1491 * If a fallback directory gives us a consensus we can never get certs for,
1492 * check_consensus_waiting_for_certs() will wait 20 minutes before failing
1493 * the cert downloads. After that, a new consensus will be fetched from a
1494 * randomly chosen fallback. */
1496 networkstatus_consensus_is_bootstrapping
,(time_t now
))
1498 /* If we have a validated, reasonably live consensus, we're not
1499 * bootstrapping a consensus at all. */
1500 if (networkstatus_get_reasonably_live_consensus(
1502 usable_consensus_flavor())) {
1506 /* If we have a consensus, but we're waiting for certificates,
1507 * we're not waiting for a consensus download while bootstrapping. */
1508 if (consensus_is_waiting_for_certs()) {
1512 /* If we have no consensus, or our consensus is very old, we are
1513 * bootstrapping, and we need to download a consensus. */
1517 /** Check if we can use multiple directories for a consensus download.
1518 * Only clients (including bridge relays, which act like clients) benefit
1519 * from multiple simultaneous consensus downloads. */
1521 networkstatus_consensus_can_use_multiple_directories(
1522 const or_options_t
*options
)
1524 /* If we are a client, bridge, bridge client, or hidden service */
1525 return !public_server_mode(options
);
1528 /** Check if we can use fallback directory mirrors for a consensus download.
1529 * If we have fallbacks and don't want to fetch from the authorities,
1530 * we can use them. */
1532 networkstatus_consensus_can_use_extra_fallbacks
,(const or_options_t
*options
))
1534 /* The list length comparisons are a quick way to check if we have any
1535 * non-authority fallback directories. If we ever have any authorities that
1536 * aren't fallback directories, we will need to change this code. */
1537 tor_assert(smartlist_len(router_get_fallback_dir_servers())
1538 >= smartlist_len(router_get_trusted_dir_servers()));
1539 /* If we don't fetch from the authorities, and we have additional mirrors,
1540 * we can use them. */
1541 return (!directory_fetches_from_authorities(options
)
1542 && (smartlist_len(router_get_fallback_dir_servers())
1543 > smartlist_len(router_get_trusted_dir_servers())));
1546 /* Is there a consensus fetch for flavor <b>resource</b> that's far
1547 * enough along to be attached to a circuit? */
1549 networkstatus_consensus_is_already_downloading(const char *resource
)
1553 /* First, get a list of all the dir conns that are fetching a consensus,
1554 * fetching *this* consensus, and are in state "reading" (meaning they
1555 * have already flushed their request onto the socks connection). */
1556 smartlist_t
*fetching_conns
=
1557 connection_dir_list_by_purpose_resource_and_state(
1558 DIR_PURPOSE_FETCH_CONSENSUS
, resource
, DIR_CONN_STATE_CLIENT_READING
);
1560 /* Then, walk through each conn, to see if its linked socks connection
1561 * is in an attached state. We have to check this separately, since with
1562 * the optimistic data feature, fetches can send their request to the
1563 * socks connection and go into state 'reading', even before they're
1564 * attached to any circuit. */
1565 SMARTLIST_FOREACH_BEGIN(fetching_conns
, dir_connection_t
*, dirconn
) {
1566 /* Do any of these other dir conns have a linked socks conn that is
1567 * attached to a circuit already? */
1568 connection_t
*base
= TO_CONN(dirconn
);
1569 if (base
->linked_conn
&&
1570 base
->linked_conn
->type
== CONN_TYPE_AP
&&
1571 !AP_CONN_STATE_IS_UNATTACHED(base
->linked_conn
->state
)) {
1573 break; /* stop looping, because we know the answer will be yes */
1575 } SMARTLIST_FOREACH_END(dirconn
);
1576 smartlist_free(fetching_conns
);
1581 /* Does the current, reasonably live consensus have IPv6 addresses?
1582 * Returns 1 if there is a reasonably live consensus and its consensus method
1583 * includes IPv6 addresses in the consensus.
1584 * Otherwise, if there is no consensus, or the method does not include IPv6
1585 * addresses, returns 0. */
1587 networkstatus_consensus_has_ipv6(const or_options_t
* options
)
1589 const networkstatus_t
*cons
= networkstatus_get_reasonably_live_consensus(
1591 usable_consensus_flavor());
1593 /* If we have no consensus, we have no IPv6 in it */
1598 /* Different flavours of consensus gained IPv6 at different times */
1599 if (we_use_microdescriptors_for_circuits(options
)) {
1601 cons
->consensus_method
>= MIN_METHOD_FOR_A_LINES_IN_MICRODESC_CONSENSUS
;
1607 /** Given two router status entries for the same router identity, return 1
1608 * if the contents have changed between them. Otherwise, return 0.
1609 * It only checks for fields that are output by control port.
1610 * This should be kept in sync with the struct routerstatus_t
1611 * and the printing function routerstatus_format_entry in
1612 * NS_CONTROL_PORT mode.
1615 routerstatus_has_changed(const routerstatus_t
*a
, const routerstatus_t
*b
)
1617 tor_assert(tor_memeq(a
->identity_digest
, b
->identity_digest
, DIGEST_LEN
));
1619 return strcmp(a
->nickname
, b
->nickname
) ||
1620 fast_memneq(a
->descriptor_digest
, b
->descriptor_digest
, DIGEST_LEN
) ||
1621 a
->addr
!= b
->addr
||
1622 a
->or_port
!= b
->or_port
||
1623 a
->dir_port
!= b
->dir_port
||
1624 a
->is_authority
!= b
->is_authority
||
1625 a
->is_exit
!= b
->is_exit
||
1626 a
->is_stable
!= b
->is_stable
||
1627 a
->is_fast
!= b
->is_fast
||
1628 a
->is_flagged_running
!= b
->is_flagged_running
||
1629 a
->is_named
!= b
->is_named
||
1630 a
->is_unnamed
!= b
->is_unnamed
||
1631 a
->is_valid
!= b
->is_valid
||
1632 a
->is_possible_guard
!= b
->is_possible_guard
||
1633 a
->is_bad_exit
!= b
->is_bad_exit
||
1634 a
->is_hs_dir
!= b
->is_hs_dir
||
1635 a
->is_staledesc
!= b
->is_staledesc
||
1636 a
->has_bandwidth
!= b
->has_bandwidth
||
1637 a
->published_on
!= b
->published_on
||
1638 a
->ipv6_orport
!= b
->ipv6_orport
||
1639 a
->is_v2_dir
!= b
->is_v2_dir
||
1640 a
->bandwidth_kb
!= b
->bandwidth_kb
||
1641 tor_addr_compare(&a
->ipv6_addr
, &b
->ipv6_addr
, CMP_EXACT
);
1644 /** Notify controllers of any router status entries that changed between
1645 * <b>old_c</b> and <b>new_c</b>. */
1647 notify_control_networkstatus_changed(const networkstatus_t
*old_c
,
1648 const networkstatus_t
*new_c
)
1650 smartlist_t
*changed
;
1654 /* tell the controller exactly which relays are still listed, as well
1655 * as what they're listed as */
1656 control_event_newconsensus(new_c
);
1658 if (!control_event_is_interesting(EVENT_NS
))
1662 control_event_networkstatus_changed(new_c
->routerstatus_list
);
1665 changed
= smartlist_new();
1667 SMARTLIST_FOREACH_JOIN(
1668 old_c
->routerstatus_list
, const routerstatus_t
*, rs_old
,
1669 new_c
->routerstatus_list
, const routerstatus_t
*, rs_new
,
1670 tor_memcmp(rs_old
->identity_digest
,
1671 rs_new
->identity_digest
, DIGEST_LEN
),
1672 smartlist_add(changed
, (void*) rs_new
)) {
1673 if (routerstatus_has_changed(rs_old
, rs_new
))
1674 smartlist_add(changed
, (void*)rs_new
);
1675 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1677 control_event_networkstatus_changed(changed
);
1678 smartlist_free(changed
);
1681 /* Called before the consensus changes from old_c to new_c. */
1683 notify_before_networkstatus_changes(const networkstatus_t
*old_c
,
1684 const networkstatus_t
*new_c
)
1686 notify_control_networkstatus_changed(old_c
, new_c
);
1687 dos_consensus_has_changed(new_c
);
1688 relay_consensus_has_changed(new_c
);
1689 hs_dos_consensus_has_changed(new_c
);
1692 /* Called after a new consensus has been put in the global state. It is safe
1693 * to use the consensus getters in this function. */
1695 notify_after_networkstatus_changes(void)
1697 scheduler_notify_networkstatus_changed();
1700 /** Copy all the ancillary information (like router download status and so on)
1701 * from <b>old_c</b> to <b>new_c</b>. */
1703 networkstatus_copy_old_consensus_info(networkstatus_t
*new_c
,
1704 const networkstatus_t
*old_c
)
1708 if (!old_c
|| !smartlist_len(old_c
->routerstatus_list
))
1711 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1712 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1713 tor_memcmp(rs_old
->identity_digest
,
1714 rs_new
->identity_digest
, DIGEST_LEN
),
1716 /* Okay, so we're looking at the same identity. */
1717 rs_new
->last_dir_503_at
= rs_old
->last_dir_503_at
;
1719 if (tor_memeq(rs_old
->descriptor_digest
, rs_new
->descriptor_digest
,
1721 /* And the same descriptor too! */
1722 memcpy(&rs_new
->dl_status
, &rs_old
->dl_status
,sizeof(download_status_t
));
1724 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1727 #ifdef TOR_UNIT_TESTS
1728 /**Accept a <b>flavor</b> consensus <b>c</b> without any additional
1729 * validation. This is exclusively for unit tests.
1730 * We copy any ancillary information from a pre-existing consensus
1731 * and then free the current one and replace it with the newly
1732 * provided instance. Returns -1 on unrecognized flavor, 0 otherwise.
1735 networkstatus_set_current_consensus_from_ns(networkstatus_t
*c
,
1738 int flav
= networkstatus_parse_flavor_name(flavor
);
1741 if (current_ns_consensus
) {
1742 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1743 networkstatus_vote_free(current_ns_consensus
);
1745 current_ns_consensus
= c
;
1747 case FLAV_MICRODESC
:
1748 if (current_md_consensus
) {
1749 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1750 networkstatus_vote_free(current_md_consensus
);
1752 current_md_consensus
= c
;
1755 return current_md_consensus
? 0 : -1;
1757 #endif /* defined(TOR_UNIT_TESTS) */
1760 * Helper: Read the current consensus of type <b>flavor</b> from
1761 * <b>fname</b>. Flags and return values are as for
1762 * networkstatus_set_current_consensus().
1765 reload_consensus_from_file(const char *fname
,
1768 const char *source_dir
)
1770 tor_mmap_t
*map
= tor_mmap_file(fname
);
1774 int rv
= networkstatus_set_current_consensus(map
->data
, map
->size
,
1775 flavor
, flags
, source_dir
);
1777 if (rv
< 0 && tor_memstr(map
->data
, map
->size
, "\r\n")) {
1778 log_notice(LD_GENERAL
, "Looks like the above failures are probably "
1779 "because of a CRLF in consensus file %s; falling back to "
1780 "read_file_to_string. Nothing to worry about: this file "
1781 "was probably saved by an earlier version of Tor.",
1783 char *content
= read_file_to_str(fname
, RFTS_IGNORE_MISSING
, NULL
);
1784 rv
= networkstatus_set_current_consensus(content
, strlen(content
),
1785 flavor
, flags
, source_dir
);
1788 #endif /* defined(_WIN32) */
1790 log_warn(LD_GENERAL
, "Couldn't set consensus from cache file %s",
1793 tor_munmap_file(map
);
1798 * Helper for handle_missing_protocol_warning: handles either the
1799 * client case (if <b>is_client</b> is set) or the server case otherwise.
1802 handle_missing_protocol_warning_impl(const networkstatus_t
*c
,
1805 char *protocol_warning
= NULL
;
1807 int should_exit
= networkstatus_check_required_protocols(c
,
1810 if (protocol_warning
) {
1811 tor_log(should_exit
? LOG_ERR
: LOG_WARN
,
1813 "%s", protocol_warning
);
1816 tor_assert_nonfatal(protocol_warning
);
1818 tor_free(protocol_warning
);
1820 exit(1); // XXXX bad exit: should return from main.
1823 /** Called when we have received a networkstatus <b>c</b>. If there are
1824 * any _required_ protocols we are missing, log an error and exit
1825 * immediately. If there are any _recommended_ protocols we are missing,
1828 handle_missing_protocol_warning(const networkstatus_t
*c
,
1829 const or_options_t
*options
)
1831 const int is_server
= server_mode(options
);
1832 const int is_client
= options_any_client_port_set(options
) || !is_server
;
1835 handle_missing_protocol_warning_impl(c
, 0);
1837 handle_missing_protocol_warning_impl(c
, 1);
1841 * Check whether we received a consensus that appears to be coming
1842 * from the future. Because we implicitly trust the directory
1843 * authorities' idea of the current time, we produce a warning if we
1844 * get an early consensus.
1846 * If we got a consensus that is time stamped far in the past, that
1847 * could simply have come from a stale cache. Possible ways to get a
1848 * consensus from the future can include:
1850 * - enough directory authorities have wrong clocks
1851 * - directory authorities collude to produce misleading time stamps
1852 * - our own clock is wrong (this is by far the most likely)
1854 * We neglect highly improbable scenarios that involve actual time
1858 warn_early_consensus(const networkstatus_t
*c
, const char *flavor
,
1861 char tbuf
[ISO_TIME_LEN
+1];
1863 long delta
= now
- c
->valid_after
;
1864 char *flavormsg
= NULL
;
1866 /** If a consensus appears more than this many seconds before it could
1867 * possibly be a sufficiently-signed consensus, declare that our clock
1869 #define EARLY_CONSENSUS_NOTICE_SKEW 60
1871 /* We assume that if a majority of dirauths have accurate clocks,
1872 * the earliest that a dirauth with a skewed clock could possibly
1873 * publish a sufficiently-signed consensus is (valid_after -
1874 * dist_seconds). Before that time, the skewed dirauth would be
1875 * unable to obtain enough authority signatures for the consensus to
1877 if (now
>= c
->valid_after
- c
->dist_seconds
- EARLY_CONSENSUS_NOTICE_SKEW
)
1880 format_iso_time(tbuf
, c
->valid_after
);
1881 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
1882 log_warn(LD_GENERAL
, "Our clock is %s behind the time published in the "
1883 "consensus network status document (%s UTC). Tor needs an "
1884 "accurate clock to work correctly. Please check your time and "
1885 "date settings!", dbuf
, tbuf
);
1886 tor_asprintf(&flavormsg
, "%s flavor consensus", flavor
);
1887 clock_skew_warning(NULL
, delta
, 1, LD_GENERAL
, flavormsg
, "CONSENSUS");
1888 tor_free(flavormsg
);
1891 /** Try to replace the current cached v3 networkstatus with the one in
1892 * <b>consensus</b>. If we don't have enough certificates to validate it,
1893 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1895 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1896 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1897 * already received, but we were waiting for certificates on it. If flags &
1898 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1899 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1900 * consensus, even if it comes from many days in the past.
1902 * If source_dir is non-NULL, it's the identity digest for a directory that
1903 * we've just successfully retrieved a consensus or certificates from, so try
1904 * it first to fetch any missing certificates.
1906 * Return 0 on success, <0 on failure. On failure, caller should increment
1907 * the failure count as appropriate.
1909 * We return -1 for mild failures that don't need to be reported to the
1910 * user, and -2 for more serious problems.
1913 networkstatus_set_current_consensus(const char *consensus
,
1914 size_t consensus_len
,
1917 const char *source_dir
)
1919 networkstatus_t
*c
=NULL
;
1921 time_t now
= approx_time();
1922 const or_options_t
*options
= get_options();
1923 char *unverified_fname
= NULL
, *consensus_fname
= NULL
;
1924 int flav
= networkstatus_parse_flavor_name(flavor
);
1925 const unsigned from_cache
= flags
& NSSET_FROM_CACHE
;
1926 const unsigned was_waiting_for_certs
= flags
& NSSET_WAS_WAITING_FOR_CERTS
;
1927 const unsigned dl_certs
= !(flags
& NSSET_DONT_DOWNLOAD_CERTS
);
1928 const unsigned accept_obsolete
= flags
& NSSET_ACCEPT_OBSOLETE
;
1929 const unsigned require_flavor
= flags
& NSSET_REQUIRE_FLAVOR
;
1930 const common_digests_t
*current_digests
= NULL
;
1931 consensus_waiting_for_certs_t
*waiting
= NULL
;
1932 time_t current_valid_after
= 0;
1933 int free_consensus
= 1; /* Free 'c' at the end of the function */
1934 int checked_protocols_already
= 0;
1937 /* XXXX we don't handle unrecognized flavors yet. */
1938 log_warn(LD_BUG
, "Unrecognized consensus flavor %s", flavor
);
1942 /* Make sure it's parseable. */
1943 c
= networkstatus_parse_vote_from_string(consensus
,
1945 NULL
, NS_TYPE_CONSENSUS
);
1947 log_warn(LD_DIR
, "Unable to parse networkstatus consensus");
1952 if (from_cache
&& !was_waiting_for_certs
) {
1953 /* We previously stored this; check _now_ to make sure that version-kills
1954 * really work. This happens even before we check signatures: we did so
1955 * before when we stored this to disk. This does mean an attacker who can
1956 * write to the datadir can make us not start: such an attacker could
1957 * already harm us by replacing our guards, which would be worse. */
1958 checked_protocols_already
= 1;
1959 handle_missing_protocol_warning(c
, options
);
1962 if ((int)c
->flavor
!= flav
) {
1963 /* This wasn't the flavor we thought we were getting. */
1964 if (require_flavor
) {
1965 log_warn(LD_DIR
, "Got consensus with unexpected flavor %s (wanted %s)",
1966 networkstatus_get_flavor_name(c
->flavor
), flavor
);
1970 flavor
= networkstatus_get_flavor_name(flav
);
1973 if (flav
!= usable_consensus_flavor() &&
1974 !we_want_to_fetch_flavor(options
, flav
)) {
1975 /* This consensus is totally boring to us: we won't use it, we didn't want
1976 * it, and we won't serve it. Drop it. */
1980 if (from_cache
&& !accept_obsolete
&&
1981 c
->valid_until
< now
-OLD_ROUTER_DESC_MAX_AGE
) {
1982 log_info(LD_DIR
, "Loaded an expired consensus. Discarding.");
1986 if (!strcmp(flavor
, "ns")) {
1987 consensus_fname
= get_cachedir_fname("cached-consensus");
1988 unverified_fname
= get_cachedir_fname("unverified-consensus");
1989 if (current_ns_consensus
) {
1990 current_digests
= ¤t_ns_consensus
->digests
;
1991 current_valid_after
= current_ns_consensus
->valid_after
;
1993 } else if (!strcmp(flavor
, "microdesc")) {
1994 consensus_fname
= get_cachedir_fname("cached-microdesc-consensus");
1995 unverified_fname
= get_cachedir_fname("unverified-microdesc-consensus");
1996 if (current_md_consensus
) {
1997 current_digests
= ¤t_md_consensus
->digests
;
1998 current_valid_after
= current_md_consensus
->valid_after
;
2001 tor_assert_nonfatal_unreached();
2006 if (current_digests
&&
2007 tor_memeq(&c
->digests
, current_digests
, sizeof(c
->digests
))) {
2008 /* We already have this one. That's a failure. */
2009 log_info(LD_DIR
, "Got a %s consensus we already have", flavor
);
2013 if (current_valid_after
&& c
->valid_after
<= current_valid_after
) {
2014 /* We have a newer one. There's no point in accepting this one,
2015 * even if it's great. */
2016 log_info(LD_DIR
, "Got a %s consensus at least as old as the one we have",
2021 /* Make sure it's signed enough. */
2022 if ((r
=networkstatus_check_consensus_signature(c
, 1))<0) {
2024 /* Okay, so it _might_ be signed enough if we get more certificates. */
2025 if (!was_waiting_for_certs
) {
2027 "Not enough certificates to check networkstatus consensus");
2029 if (!current_valid_after
||
2030 c
->valid_after
> current_valid_after
) {
2031 waiting
= &consensus_waiting_for_certs
[flav
];
2032 networkstatus_vote_free(waiting
->consensus
);
2033 waiting
->consensus
= c
;
2035 waiting
->set_at
= now
;
2036 waiting
->dl_failed
= 0;
2038 write_bytes_to_file(unverified_fname
, consensus
, consensus_len
, 1);
2041 authority_certs_fetch_missing(c
, now
, source_dir
);
2042 /* This case is not a success or a failure until we get the certs
2043 * or fail to get the certs. */
2046 /* Even if we had enough signatures, we'd never use this as the
2047 * latest consensus. */
2048 if (was_waiting_for_certs
&& from_cache
)
2049 if (unlink(unverified_fname
) != 0) {
2051 "Failed to unlink %s: %s",
2052 unverified_fname
, strerror(errno
));
2057 /* This can never be signed enough: Kill it. */
2058 if (!was_waiting_for_certs
) {
2059 log_warn(LD_DIR
, "Not enough good signatures on networkstatus "
2063 if (was_waiting_for_certs
&& (r
< -1) && from_cache
) {
2064 if (unlink(unverified_fname
) != 0) {
2066 "Failed to unlink %s: %s",
2067 unverified_fname
, strerror(errno
));
2074 /* Signatures from the consensus are verified */
2075 if (from_cache
&& was_waiting_for_certs
) {
2076 /* We check if the consensus is loaded from disk cache and that it
2077 * it is an unverified consensus. If it is unverified, rename it to
2078 * cached-*-consensus since it has been verified. */
2079 log_info(LD_DIR
, "Unverified consensus signatures verified.");
2080 tor_rename(unverified_fname
, consensus_fname
);
2083 if (!from_cache
&& flav
== usable_consensus_flavor())
2084 control_event_client_status(LOG_NOTICE
, "CONSENSUS_ARRIVED");
2086 if (!checked_protocols_already
) {
2087 handle_missing_protocol_warning(c
, options
);
2090 /* Are we missing any certificates at all? */
2091 if (r
!= 1 && dl_certs
)
2092 authority_certs_fetch_missing(c
, now
, source_dir
);
2094 const int is_usable_flavor
= flav
== usable_consensus_flavor();
2096 /* Before we switch to the new consensus, notify that we are about to change
2097 * it using the old consensus and the new one. */
2098 if (is_usable_flavor
) {
2099 notify_before_networkstatus_changes(networkstatus_get_latest_consensus(),
2102 if (flav
== FLAV_NS
) {
2103 if (current_ns_consensus
) {
2104 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
2105 networkstatus_vote_free(current_ns_consensus
);
2106 /* Defensive programming : we should set current_ns_consensus very soon
2107 * but we're about to call some stuff in the meantime, and leaving this
2108 * dangling pointer around has proven to be trouble. */
2109 current_ns_consensus
= NULL
;
2111 current_ns_consensus
= c
;
2112 free_consensus
= 0; /* avoid free */
2113 } else if (flav
== FLAV_MICRODESC
) {
2114 if (current_md_consensus
) {
2115 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
2116 networkstatus_vote_free(current_md_consensus
);
2117 /* more defensive programming */
2118 current_md_consensus
= NULL
;
2120 current_md_consensus
= c
;
2121 free_consensus
= 0; /* avoid free */
2124 waiting
= &consensus_waiting_for_certs
[flav
];
2125 if (waiting
->consensus
&&
2126 waiting
->consensus
->valid_after
<= c
->valid_after
) {
2127 networkstatus_vote_free(waiting
->consensus
);
2128 waiting
->consensus
= NULL
;
2129 waiting
->set_at
= 0;
2130 waiting
->dl_failed
= 0;
2131 if (unlink(unverified_fname
) != 0) {
2133 "Failed to unlink %s: %s",
2134 unverified_fname
, strerror(errno
));
2138 if (is_usable_flavor
) {
2139 /* Notify that we just changed the consensus so the current global value
2140 * can be looked at. */
2141 notify_after_networkstatus_changes();
2143 /* The "current" consensus has just been set and it is a usable flavor so
2144 * the first thing we need to do is recalculate the voting schedule static
2145 * object so we can use the timings in there needed by some subsystems
2146 * such as hidden service and shared random. */
2147 voting_schedule_recalculate_timing(options
, now
);
2148 reschedule_dirvote(options
);
2150 nodelist_set_consensus(c
);
2152 update_consensus_networkstatus_fetch_time(now
);
2154 /* Change the cell EWMA settings */
2155 cmux_ewma_set_options(options
, c
);
2157 /* XXXX this call might be unnecessary here: can changing the
2158 * current consensus really alter our view of any OR's rate limits? */
2159 connection_or_update_token_buckets(get_connection_array(), options
);
2161 circuit_build_times_new_consensus_params(
2162 get_circuit_build_times_mutable(), c
);
2163 channelpadding_new_consensus_params(c
);
2164 circpad_new_consensus_params(c
);
2167 /* Reset the failure count only if this consensus is actually valid. */
2168 if (c
->valid_after
<= now
&& now
<= c
->valid_until
) {
2169 download_status_reset(&consensus_dl_status
[flav
]);
2172 download_status_failed(&consensus_dl_status
[flav
], 0);
2175 if (we_want_to_fetch_flavor(options
, flav
)) {
2176 if (dir_server_mode(get_options())) {
2177 dirserv_set_cached_consensus_networkstatus(consensus
,
2181 c
->digest_sha3_as_signed
,
2184 consdiffmgr_add_consensus(consensus
, consensus_len
, c
);
2189 write_bytes_to_file(consensus_fname
, consensus
, consensus_len
, 1);
2192 warn_early_consensus(c
, flavor
, now
);
2194 /* We got a new consesus. Reset our md fetch fail cache */
2195 microdesc_reset_outdated_dirservers_list();
2197 router_dir_info_changed();
2202 networkstatus_vote_free(c
);
2203 tor_free(consensus_fname
);
2204 tor_free(unverified_fname
);
2208 /** Called when we have gotten more certificates: see whether we can
2209 * now verify a pending consensus.
2211 * If source_dir is non-NULL, it's the identity digest for a directory that
2212 * we've just successfully retrieved certificates from, so try it first to
2213 * fetch any missing certificates.
2216 networkstatus_note_certs_arrived(const char *source_dir
)
2219 for (i
=0; i
<N_CONSENSUS_FLAVORS
; ++i
) {
2220 const char *flavor_name
= networkstatus_get_flavor_name(i
);
2221 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2222 if (!waiting
->consensus
)
2224 if (networkstatus_check_consensus_signature(waiting
->consensus
, 0)>=0) {
2225 char *fname
= networkstatus_get_cache_fname(i
, flavor_name
, 1);
2226 reload_consensus_from_file(fname
, flavor_name
,
2227 NSSET_WAS_WAITING_FOR_CERTS
, source_dir
);
2233 /** If the network-status list has changed since the last time we called this
2234 * function, update the status of every routerinfo from the network-status
2235 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
2236 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
2239 routers_update_all_from_networkstatus(time_t now
, int dir_version
)
2241 routerlist_t
*rl
= router_get_routerlist();
2242 networkstatus_t
*consensus
= networkstatus_get_reasonably_live_consensus(now
,
2245 if (!consensus
|| dir_version
< 3) /* nothing more we should do */
2248 /* calls router_dir_info_changed() when it's done -- more routers
2249 * might be up or down now, which might affect whether there's enough
2250 * directory info. */
2251 routers_update_status_from_consensus_networkstatus(rl
->routers
, 0);
2253 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
,
2254 ri
->cache_info
.routerlist_index
= ri_sl_idx
);
2255 if (rl
->old_routers
)
2256 signed_descs_update_status_from_consensus_networkstatus(rl
->old_routers
);
2258 if (!have_warned_about_old_version
) {
2259 int is_server
= server_mode(get_options());
2260 version_status_t status
;
2261 const char *recommended
= is_server
?
2262 consensus
->server_versions
: consensus
->client_versions
;
2263 status
= tor_version_is_obsolete(VERSION
, recommended
);
2265 if (status
== VS_RECOMMENDED
) {
2266 log_info(LD_GENERAL
, "The directory authorities say my version is ok.");
2267 } else if (status
== VS_EMPTY
) {
2268 log_info(LD_GENERAL
,
2269 "The directory authorities don't recommend any versions.");
2270 } else if (status
== VS_NEW
|| status
== VS_NEW_IN_SERIES
) {
2271 if (!have_warned_about_new_version
) {
2272 log_notice(LD_GENERAL
, "This version of Tor (%s) is newer than any "
2273 "recommended version%s, according to the directory "
2274 "authorities. Recommended versions are: %s",
2276 status
== VS_NEW_IN_SERIES
? " in its series" : "",
2278 have_warned_about_new_version
= 1;
2279 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
2280 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
2281 VERSION
, "NEW", recommended
);
2284 log_warn(LD_GENERAL
, "Please upgrade! "
2285 "This version of Tor (%s) is %s, according to the directory "
2286 "authorities. Recommended versions are: %s",
2288 status
== VS_OLD
? "obsolete" : "not recommended",
2290 have_warned_about_old_version
= 1;
2291 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
2292 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
2293 VERSION
, status
== VS_OLD
? "OBSOLETE" : "UNRECOMMENDED",
2299 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
2300 * according to our current consensus networkstatus. May re-order
2301 * <b>routers</b>. */
2303 routers_update_status_from_consensus_networkstatus(smartlist_t
*routers
,
2306 const or_options_t
*options
= get_options();
2307 int authdir
= authdir_mode_v3(options
);
2308 networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2309 if (!ns
|| !smartlist_len(ns
->routerstatus_list
))
2312 routers_sort_by_identity(routers
);
2314 SMARTLIST_FOREACH_JOIN(ns
->routerstatus_list
, routerstatus_t
*, rs
,
2315 routers
, routerinfo_t
*, router
,
2316 tor_memcmp(rs
->identity_digest
,
2317 router
->cache_info
.identity_digest
, DIGEST_LEN
),
2320 /* Is it the same descriptor, or only the same identity? */
2321 if (tor_memeq(router
->cache_info
.signed_descriptor_digest
,
2322 rs
->descriptor_digest
, DIGEST_LEN
)) {
2323 if (ns
->valid_until
> router
->cache_info
.last_listed_as_valid_until
)
2324 router
->cache_info
.last_listed_as_valid_until
= ns
->valid_until
;
2328 /* If we _are_ an authority, we should check whether this router
2329 * is one that will cause us to need a reachability test. */
2330 routerinfo_t
*old_router
=
2331 router_get_mutable_by_digest(router
->cache_info
.identity_digest
);
2332 if (old_router
!= router
) {
2333 router
->needs_retest_if_added
=
2334 dirserv_should_launch_reachability_test(router
, old_router
);
2337 if (reset_failures
) {
2338 download_status_reset(&rs
->dl_status
);
2340 } SMARTLIST_FOREACH_JOIN_END(rs
, router
);
2342 router_dir_info_changed();
2345 /** Given a list of signed_descriptor_t, update their fields (mainly, when
2346 * they were last listed) from the most recent consensus. */
2348 signed_descs_update_status_from_consensus_networkstatus(smartlist_t
*descs
)
2350 networkstatus_t
*ns
= current_ns_consensus
;
2354 if (!ns
->desc_digest_map
) {
2355 char dummy
[DIGEST_LEN
];
2356 /* instantiates the digest map. */
2357 memset(dummy
, 0, sizeof(dummy
));
2358 router_get_consensus_status_by_descriptor_digest(ns
, dummy
);
2360 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, d
,
2362 const routerstatus_t
*rs
= digestmap_get(ns
->desc_digest_map
,
2363 d
->signed_descriptor_digest
);
2365 if (ns
->valid_until
> d
->last_listed_as_valid_until
)
2366 d
->last_listed_as_valid_until
= ns
->valid_until
;
2371 /** Generate networkstatus lines for a single routerstatus_t object, and
2372 * return the result in a newly allocated string. Used only by controller
2373 * interface (for now.) */
2375 networkstatus_getinfo_helper_single(const routerstatus_t
*rs
)
2377 return routerstatus_format_entry(rs
, NULL
, NULL
, NS_CONTROL_PORT
,
2378 ROUTERSTATUS_FORMAT_NO_CONSENSUS_METHOD
,
2383 * Extract status information from <b>ri</b> and from other authority
2384 * functions and store it in <b>rs</b>. <b>rs</b> is zeroed out before it is
2387 * We assume that node-\>is_running has already been set, e.g. by
2388 * dirserv_set_router_is_running(ri, now);
2391 set_routerstatus_from_routerinfo(routerstatus_t
*rs
,
2393 const routerinfo_t
*ri
)
2395 memset(rs
, 0, sizeof(routerstatus_t
));
2398 router_digest_is_trusted_dir(ri
->cache_info
.identity_digest
);
2400 /* Set by compute_performance_thresholds or from consensus */
2401 rs
->is_exit
= node
->is_exit
;
2402 rs
->is_stable
= node
->is_stable
;
2403 rs
->is_fast
= node
->is_fast
;
2404 rs
->is_flagged_running
= node
->is_running
;
2405 rs
->is_valid
= node
->is_valid
;
2406 rs
->is_possible_guard
= node
->is_possible_guard
;
2407 rs
->is_bad_exit
= node
->is_bad_exit
;
2408 rs
->is_hs_dir
= node
->is_hs_dir
;
2409 rs
->is_named
= rs
->is_unnamed
= 0;
2411 rs
->published_on
= ri
->cache_info
.published_on
;
2412 memcpy(rs
->identity_digest
, node
->identity
, DIGEST_LEN
);
2413 memcpy(rs
->descriptor_digest
, ri
->cache_info
.signed_descriptor_digest
,
2415 rs
->addr
= ri
->addr
;
2416 strlcpy(rs
->nickname
, ri
->nickname
, sizeof(rs
->nickname
));
2417 rs
->or_port
= ri
->or_port
;
2418 rs
->dir_port
= ri
->dir_port
;
2419 rs
->is_v2_dir
= ri
->supports_tunnelled_dir_requests
;
2421 tor_addr_copy(&rs
->ipv6_addr
, &ri
->ipv6_addr
);
2422 rs
->ipv6_orport
= ri
->ipv6_orport
;
2425 /** Alloc and return a string describing routerstatuses for the most
2426 * recent info of each router we know about that is of purpose
2427 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
2429 * Right now this function is oriented toward listing bridges (you
2430 * shouldn't use this for general-purpose routers, since those
2431 * should be listed from the consensus, not from the routers list). */
2433 networkstatus_getinfo_by_purpose(const char *purpose_string
, time_t now
)
2435 const time_t cutoff
= now
- ROUTER_MAX_AGE_TO_PUBLISH
;
2437 routerlist_t
*rl
= router_get_routerlist();
2438 smartlist_t
*statuses
;
2439 const uint8_t purpose
= router_purpose_from_string(purpose_string
);
2442 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
2443 log_info(LD_DIR
, "Unrecognized purpose '%s' when listing router statuses.",
2448 statuses
= smartlist_new();
2449 SMARTLIST_FOREACH_BEGIN(rl
->routers
, routerinfo_t
*, ri
) {
2450 node_t
*node
= node_get_mutable_by_id(ri
->cache_info
.identity_digest
);
2453 if (ri
->cache_info
.published_on
< cutoff
)
2455 if (ri
->purpose
!= purpose
)
2457 set_routerstatus_from_routerinfo(&rs
, node
, ri
);
2458 smartlist_add(statuses
, networkstatus_getinfo_helper_single(&rs
));
2459 } SMARTLIST_FOREACH_END(ri
);
2461 answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2462 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2463 smartlist_free(statuses
);
2467 /* DOCDOC get_net_param_from_list */
2469 get_net_param_from_list(smartlist_t
*net_params
, const char *param_name
,
2470 int32_t default_val
, int32_t min_val
, int32_t max_val
)
2472 int32_t res
= default_val
;
2473 size_t name_len
= strlen(param_name
);
2475 tor_assert(max_val
> min_val
);
2476 tor_assert(min_val
<= default_val
);
2477 tor_assert(max_val
>= default_val
);
2479 SMARTLIST_FOREACH_BEGIN(net_params
, const char *, p
) {
2480 if (!strcmpstart(p
, param_name
) && p
[name_len
] == '=') {
2482 long v
= tor_parse_long(p
+name_len
+1, 10, INT32_MIN
,
2483 INT32_MAX
, &ok
, NULL
);
2489 } SMARTLIST_FOREACH_END(p
);
2491 if (res
< min_val
) {
2492 log_warn(LD_DIR
, "Consensus parameter %s is too small. Got %d, raising to "
2493 "%d.", param_name
, res
, min_val
);
2495 } else if (res
> max_val
) {
2496 log_warn(LD_DIR
, "Consensus parameter %s is too large. Got %d, capping to "
2497 "%d.", param_name
, res
, max_val
);
2501 tor_assert(res
>= min_val
);
2502 tor_assert(res
<= max_val
);
2506 /** Return the value of a integer parameter from the networkstatus <b>ns</b>
2507 * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
2508 * latest consensus ourselves. Return <b>default_val</b> if no latest
2509 * consensus, or if it has no parameter called <b>param_name</b>.
2510 * Make sure the value parsed from the consensus is at least
2511 * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
2514 networkstatus_get_param
, (const networkstatus_t
*ns
, const char *param_name
,
2515 int32_t default_val
, int32_t min_val
, int32_t max_val
))
2517 if (!ns
) /* if they pass in null, go find it ourselves */
2518 ns
= networkstatus_get_latest_consensus();
2520 if (!ns
|| !ns
->net_params
)
2523 return get_net_param_from_list(ns
->net_params
, param_name
,
2524 default_val
, min_val
, max_val
);
2528 * As networkstatus_get_param(), but check torrc_value before checking the
2529 * consensus. If torrc_value is in-range, then return it instead of the
2530 * value from the consensus.
2533 networkstatus_get_overridable_param(const networkstatus_t
*ns
,
2534 int32_t torrc_value
,
2535 const char *param_name
,
2536 int32_t default_val
,
2537 int32_t min_val
, int32_t max_val
)
2539 if (torrc_value
>= min_val
&& torrc_value
<= max_val
)
2542 return networkstatus_get_param(
2543 ns
, param_name
, default_val
, min_val
, max_val
);
2547 * Retrieve the consensus parameter that governs the
2548 * fixed-point precision of our network balancing 'bandwidth-weights'
2549 * (which are themselves integer consensus values). We divide them
2550 * by this value and ensure they never exceed this value.
2553 networkstatus_get_weight_scale_param(networkstatus_t
*ns
)
2555 return networkstatus_get_param(ns
, "bwweightscale",
2557 BW_MIN_WEIGHT_SCALE
,
2558 BW_MAX_WEIGHT_SCALE
);
2561 /** Return the value of a integer bw weight parameter from the networkstatus
2562 * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
2563 * loading the latest consensus ourselves. Return <b>default_val</b> if no
2564 * latest consensus, or if it has no parameter called <b>weight_name</b>. */
2566 networkstatus_get_bw_weight(networkstatus_t
*ns
, const char *weight_name
,
2567 int32_t default_val
)
2571 if (!ns
) /* if they pass in null, go find it ourselves */
2572 ns
= networkstatus_get_latest_consensus();
2574 if (!ns
|| !ns
->weight_params
)
2577 max
= networkstatus_get_weight_scale_param(ns
);
2578 param
= get_net_param_from_list(ns
->weight_params
, weight_name
,
2580 BW_MAX_WEIGHT_SCALE
);
2582 log_warn(LD_DIR
, "Value of consensus weight %s was too large, capping "
2583 "to %d", weight_name
, max
);
2589 /** Return the name of the consensus flavor <b>flav</b> as used to identify
2590 * the flavor in directory documents. */
2592 networkstatus_get_flavor_name(consensus_flavor_t flav
)
2597 case FLAV_MICRODESC
:
2600 tor_fragile_assert();
2605 /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
2606 * or -1 if the flavor is not recognized. */
2608 networkstatus_parse_flavor_name(const char *flavname
)
2610 if (!strcmp(flavname
, "ns"))
2612 else if (!strcmp(flavname
, "microdesc"))
2613 return FLAV_MICRODESC
;
2618 /** Return 0 if this routerstatus is obsolete, too new, isn't
2619 * running, or otherwise not a descriptor that we would make any
2620 * use of even if we had it. Else return 1. */
2622 client_would_use_router(const routerstatus_t
*rs
, time_t now
)
2624 if (!rs
->is_flagged_running
) {
2625 /* If we had this router descriptor, we wouldn't even bother using it.
2626 * (Fetching and storing depends on by we_want_to_fetch_flavor().) */
2629 if (rs
->published_on
+ OLD_ROUTER_DESC_MAX_AGE
< now
) {
2630 /* We'd drop it immediately for being too old. */
2633 if (!routerstatus_version_supports_extend2_cells(rs
, 1)) {
2634 /* We'd ignore it because it doesn't support EXTEND2 cells.
2635 * If we don't know the version, download the descriptor so we can
2636 * check if it supports EXTEND2 cells and ntor. */
2642 /** If <b>question</b> is a string beginning with "ns/" in a format the
2643 * control interface expects for a GETINFO question, set *<b>answer</b> to a
2644 * newly-allocated string containing networkstatus lines for the appropriate
2645 * ORs. Return 0 on success, -1 on unrecognized question format. */
2647 getinfo_helper_networkstatus(control_connection_t
*conn
,
2648 const char *question
, char **answer
,
2649 const char **errmsg
)
2651 const routerstatus_t
*status
;
2654 if (!networkstatus_get_latest_consensus()) {
2655 *answer
= tor_strdup("");
2659 if (!strcmp(question
, "ns/all")) {
2660 smartlist_t
*statuses
= smartlist_new();
2661 SMARTLIST_FOREACH(networkstatus_get_latest_consensus()->routerstatus_list
,
2662 const routerstatus_t
*, rs
,
2664 smartlist_add(statuses
, networkstatus_getinfo_helper_single(rs
));
2666 *answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2667 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2668 smartlist_free(statuses
);
2670 } else if (!strcmpstart(question
, "ns/id/")) {
2672 const char *q
= question
+ 6;
2676 if (base16_decode(d
, DIGEST_LEN
, q
, strlen(q
)) != DIGEST_LEN
) {
2677 *errmsg
= "Data not decodeable as hex";
2680 status
= router_get_consensus_status_by_id(d
);
2681 } else if (!strcmpstart(question
, "ns/name/")) {
2682 const node_t
*n
= node_get_by_nickname(question
+8, 0);
2683 status
= n
? n
->rs
: NULL
;
2684 } else if (!strcmpstart(question
, "ns/purpose/")) {
2685 *answer
= networkstatus_getinfo_by_purpose(question
+11, time(NULL
));
2686 return *answer
? 0 : -1;
2687 } else if (!strcmp(question
, "consensus/packages")) {
2688 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2689 if (ns
&& ns
->package_lines
)
2690 *answer
= smartlist_join_strings(ns
->package_lines
, "\n", 0, NULL
);
2692 *errmsg
= "No consensus available";
2693 return *answer
? 0 : -1;
2694 } else if (!strcmp(question
, "consensus/valid-after") ||
2695 !strcmp(question
, "consensus/fresh-until") ||
2696 !strcmp(question
, "consensus/valid-until")) {
2697 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2700 if (!strcmp(question
, "consensus/valid-after"))
2701 t
= ns
->valid_after
;
2702 else if (!strcmp(question
, "consensus/fresh-until"))
2703 t
= ns
->fresh_until
;
2705 t
= ns
->valid_until
;
2707 char tbuf
[ISO_TIME_LEN
+1];
2708 format_iso_time(tbuf
, t
);
2709 *answer
= tor_strdup(tbuf
);
2711 *errmsg
= "No consensus available";
2713 return *answer
? 0 : -1;
2719 *answer
= networkstatus_getinfo_helper_single(status
);
2723 /** Check whether the networkstatus <b>ns</b> lists any protocol
2724 * versions as "required" or "recommended" that we do not support. If
2725 * so, set *<b>warning_out</b> to a newly allocated string describing
2728 * Return 1 if we should exit, 0 if we should not. */
2730 networkstatus_check_required_protocols(const networkstatus_t
*ns
,
2734 const char *func
= client_mode
? "client" : "relay";
2735 const char *required
, *recommended
;
2736 char *missing
= NULL
;
2738 const bool consensus_postdates_this_release
=
2739 ns
->valid_after
>= tor_get_approx_release_date();
2741 tor_assert(warning_out
);
2744 required
= ns
->required_client_protocols
;
2745 recommended
= ns
->recommended_client_protocols
;
2747 required
= ns
->required_relay_protocols
;
2748 recommended
= ns
->recommended_relay_protocols
;
2751 if (!protover_all_supported(required
, &missing
)) {
2752 tor_asprintf(warning_out
, "At least one protocol listed as required in "
2753 "the consensus is not supported by this version of Tor. "
2754 "You should upgrade. This version of Tor will not work as a "
2755 "%s on the Tor network. The missing protocols are: %s",
2758 return consensus_postdates_this_release
? 1 : 0;
2761 if (! protover_all_supported(recommended
, &missing
)) {
2762 tor_asprintf(warning_out
, "At least one protocol listed as recommended in "
2763 "the consensus is not supported by this version of Tor. "
2764 "You should upgrade. This version of Tor will eventually "
2765 "stop working as a %s on the Tor network. The missing "
2766 "protocols are: %s",
2771 tor_assert_nonfatal(missing
== NULL
);
2776 /** Free all storage held locally in this module. */
2778 networkstatus_free_all(void)
2781 networkstatus_vote_free(current_ns_consensus
);
2782 networkstatus_vote_free(current_md_consensus
);
2783 current_md_consensus
= current_ns_consensus
= NULL
;
2785 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2786 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2787 if (waiting
->consensus
) {
2788 networkstatus_vote_free(waiting
->consensus
);
2789 waiting
->consensus
= NULL
;