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-2016, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
8 * \file networkstatus.c
9 * \brief Functions and structures for handling network status documents as a
13 #define NETWORKSTATUS_PRIVATE
16 #include "circuitmux.h"
17 #include "circuitmux_ewma.h"
18 #include "circuitstats.h"
20 #include "connection.h"
21 #include "connection_or.h"
23 #include "directory.h"
26 #include "entrynodes.h"
28 #include "microdesc.h"
29 #include "networkstatus.h"
34 #include "routerlist.h"
35 #include "routerparse.h"
36 #include "shared_random.h"
37 #include "transports.h"
40 /** Map from lowercase nickname to identity digest of named server, if any. */
41 static strmap_t
*named_server_map
= NULL
;
42 /** Map from lowercase nickname to (void*)1 for all names that are listed
43 * as unnamed for some server in the consensus. */
44 static strmap_t
*unnamed_server_map
= NULL
;
46 /** Most recently received and validated v3 consensus network status,
47 * of whichever type we are using for our own circuits. This will be the same
48 * as one of current_ns_consensus or current_md_consensus.
50 #define current_consensus \
51 (we_use_microdescriptors_for_circuits(get_options()) ? \
52 current_md_consensus : current_ns_consensus)
54 /** Most recently received and validated v3 "ns"-flavored consensus network
56 static networkstatus_t
*current_ns_consensus
= NULL
;
58 /** Most recently received and validated v3 "microdec"-flavored consensus
60 static networkstatus_t
*current_md_consensus
= NULL
;
62 /** A v3 consensus networkstatus that we've received, but which we don't
63 * have enough certificates to be happy about. */
64 typedef struct consensus_waiting_for_certs_t
{
65 /** The consensus itself. */
66 networkstatus_t
*consensus
;
67 /** The encoded version of the consensus, nul-terminated. */
69 /** When did we set the current value of consensus_waiting_for_certs? If
70 * this is too recent, we shouldn't try to fetch a new consensus for a
71 * little while, to give ourselves time to get certificates for this one. */
73 /** Set to 1 if we've been holding on to it for so long we should maybe
74 * treat it as being bad. */
76 } consensus_waiting_for_certs_t
;
78 /** An array, for each flavor of consensus we might want, of consensuses that
79 * we have downloaded, but which we cannot verify due to having insufficient
80 * authority certificates. */
81 static consensus_waiting_for_certs_t
82 consensus_waiting_for_certs
[N_CONSENSUS_FLAVORS
];
84 /** A time before which we shouldn't try to replace the current consensus:
85 * this will be at some point after the next consensus becomes valid, but
86 * before the current consensus becomes invalid. */
87 static time_t time_to_download_next_consensus
[N_CONSENSUS_FLAVORS
];
88 /** Download status for the current consensus networkstatus. */
89 static download_status_t consensus_dl_status
[N_CONSENSUS_FLAVORS
] =
91 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
92 DL_SCHED_INCREMENT_FAILURE
, DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 },
93 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
94 DL_SCHED_INCREMENT_FAILURE
, DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 },
97 #define N_CONSENSUS_BOOTSTRAP_SCHEDULES 2
98 #define CONSENSUS_BOOTSTRAP_SOURCE_AUTHORITY 0
99 #define CONSENSUS_BOOTSTRAP_SOURCE_ANY_DIRSERVER 1
101 /* Using DL_SCHED_INCREMENT_ATTEMPT on these schedules means that
102 * download_status_increment_failure won't increment these entries.
103 * However, any bootstrap connection failures that occur after we have
104 * a valid consensus will count against the failure counts on the non-bootstrap
105 * schedules. There should only be one of these, as all the others will have
106 * been cancelled. (This doesn't seem to be a significant issue.) */
107 static download_status_t
108 consensus_bootstrap_dl_status
[N_CONSENSUS_BOOTSTRAP_SCHEDULES
] =
110 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_AUTHORITY
,
111 DL_SCHED_INCREMENT_ATTEMPT
, DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 },
112 /* During bootstrap, DL_WANT_ANY_DIRSERVER means "use fallbacks". */
113 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
114 DL_SCHED_INCREMENT_ATTEMPT
, DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 },
117 /** True iff we have logged a warning about this OR's version being older than
118 * listed by the authorities. */
119 static int have_warned_about_old_version
= 0;
120 /** True iff we have logged a warning about this OR's version being newer than
121 * listed by the authorities. */
122 static int have_warned_about_new_version
= 0;
124 static void routerstatus_list_update_named_server_map(void);
125 static void update_consensus_bootstrap_multiple_downloads(
127 const or_options_t
*options
);
129 /** Forget that we've warned about anything networkstatus-related, so we will
130 * give fresh warnings if the same behavior happens again. */
132 networkstatus_reset_warnings(void)
134 if (current_consensus
) {
135 SMARTLIST_FOREACH(nodelist_get_list(), node_t
*, node
,
136 node
->name_lookup_warned
= 0);
139 have_warned_about_old_version
= 0;
140 have_warned_about_new_version
= 0;
143 /** Reset the descriptor download failure count on all networkstatus docs, so
144 * that we can retry any long-failed documents immediately.
147 networkstatus_reset_download_failures(void)
151 log_debug(LD_GENERAL
,
152 "In networkstatus_reset_download_failures()");
154 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
)
155 download_status_reset(&consensus_dl_status
[i
]);
157 for (i
=0; i
< N_CONSENSUS_BOOTSTRAP_SCHEDULES
; ++i
)
158 download_status_reset(&consensus_bootstrap_dl_status
[i
]);
161 /** Read every cached v3 consensus networkstatus from the disk. */
163 router_reload_consensus_networkstatus(void)
167 const unsigned int flags
= NSSET_FROM_CACHE
| NSSET_DONT_DOWNLOAD_CERTS
;
170 /* FFFF Suppress warnings if cached consensus is bad? */
171 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
173 const char *flavor
= networkstatus_get_flavor_name(flav
);
174 if (flav
== FLAV_NS
) {
175 filename
= get_datadir_fname("cached-consensus");
177 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
178 filename
= get_datadir_fname(buf
);
180 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
182 if (networkstatus_set_current_consensus(s
, flavor
, flags
, NULL
) < -1) {
183 log_warn(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
190 if (flav
== FLAV_NS
) {
191 filename
= get_datadir_fname("unverified-consensus");
193 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
194 filename
= get_datadir_fname(buf
);
197 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
199 if (networkstatus_set_current_consensus(s
, flavor
,
200 flags
|NSSET_WAS_WAITING_FOR_CERTS
,
202 log_info(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
210 if (!current_consensus
) {
211 if (!named_server_map
)
212 named_server_map
= strmap_new();
213 if (!unnamed_server_map
)
214 unnamed_server_map
= strmap_new();
217 update_certificate_downloads(time(NULL
));
219 routers_update_all_from_networkstatus(time(NULL
), 3);
220 update_microdescs_from_networkstatus(time(NULL
));
225 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
227 vote_routerstatus_free(vote_routerstatus_t
*rs
)
229 vote_microdesc_hash_t
*h
, *next
;
232 tor_free(rs
->version
);
233 tor_free(rs
->status
.exitsummary
);
234 for (h
= rs
->microdesc
; h
; h
= next
) {
235 tor_free(h
->microdesc_hash_line
);
242 /** Free all storage held by the routerstatus object <b>rs</b>. */
244 routerstatus_free(routerstatus_t
*rs
)
248 tor_free(rs
->exitsummary
);
252 /** Free all storage held in <b>sig</b> */
254 document_signature_free(document_signature_t
*sig
)
256 tor_free(sig
->signature
);
260 /** Return a newly allocated copy of <b>sig</b> */
261 document_signature_t
*
262 document_signature_dup(const document_signature_t
*sig
)
264 document_signature_t
*r
= tor_memdup(sig
, sizeof(document_signature_t
));
266 r
->signature
= tor_memdup(sig
->signature
, sig
->signature_len
);
270 /** Free all storage held in <b>ns</b>. */
272 networkstatus_vote_free(networkstatus_t
*ns
)
277 tor_free(ns
->client_versions
);
278 tor_free(ns
->server_versions
);
279 tor_free(ns
->recommended_client_protocols
);
280 tor_free(ns
->recommended_relay_protocols
);
281 tor_free(ns
->required_client_protocols
);
282 tor_free(ns
->required_relay_protocols
);
284 if (ns
->known_flags
) {
285 SMARTLIST_FOREACH(ns
->known_flags
, char *, c
, tor_free(c
));
286 smartlist_free(ns
->known_flags
);
288 if (ns
->weight_params
) {
289 SMARTLIST_FOREACH(ns
->weight_params
, char *, c
, tor_free(c
));
290 smartlist_free(ns
->weight_params
);
292 if (ns
->net_params
) {
293 SMARTLIST_FOREACH(ns
->net_params
, char *, c
, tor_free(c
));
294 smartlist_free(ns
->net_params
);
296 if (ns
->supported_methods
) {
297 SMARTLIST_FOREACH(ns
->supported_methods
, char *, c
, tor_free(c
));
298 smartlist_free(ns
->supported_methods
);
300 if (ns
->package_lines
) {
301 SMARTLIST_FOREACH(ns
->package_lines
, char *, c
, tor_free(c
));
302 smartlist_free(ns
->package_lines
);
305 SMARTLIST_FOREACH_BEGIN(ns
->voters
, networkstatus_voter_info_t
*, voter
) {
306 tor_free(voter
->nickname
);
307 tor_free(voter
->address
);
308 tor_free(voter
->contact
);
310 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
311 document_signature_free(sig
));
312 smartlist_free(voter
->sigs
);
315 } SMARTLIST_FOREACH_END(voter
);
316 smartlist_free(ns
->voters
);
318 authority_cert_free(ns
->cert
);
320 if (ns
->routerstatus_list
) {
321 if (ns
->type
== NS_TYPE_VOTE
|| ns
->type
== NS_TYPE_OPINION
) {
322 SMARTLIST_FOREACH(ns
->routerstatus_list
, vote_routerstatus_t
*, rs
,
323 vote_routerstatus_free(rs
));
325 SMARTLIST_FOREACH(ns
->routerstatus_list
, routerstatus_t
*, rs
,
326 routerstatus_free(rs
));
329 smartlist_free(ns
->routerstatus_list
);
332 digestmap_free(ns
->desc_digest_map
, NULL
);
334 if (ns
->sr_info
.commits
) {
335 SMARTLIST_FOREACH(ns
->sr_info
.commits
, sr_commit_t
*, c
,
337 smartlist_free(ns
->sr_info
.commits
);
339 tor_free(ns
->sr_info
.previous_srv
);
340 tor_free(ns
->sr_info
.current_srv
);
342 memwipe(ns
, 11, sizeof(*ns
));
346 /** Return the voter info from <b>vote</b> for the voter whose identity digest
347 * is <b>identity</b>, or NULL if no such voter is associated with
349 networkstatus_voter_info_t
*
350 networkstatus_get_voter_by_id(networkstatus_t
*vote
,
351 const char *identity
)
353 if (!vote
|| !vote
->voters
)
355 SMARTLIST_FOREACH(vote
->voters
, networkstatus_voter_info_t
*, voter
,
356 if (fast_memeq(voter
->identity_digest
, identity
, DIGEST_LEN
))
361 /** Check whether the signature <b>sig</b> is correctly signed with the
362 * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
363 * signing key; otherwise set the good_signature or bad_signature flag on
364 * <b>voter</b>, and return 0. */
366 networkstatus_check_document_signature(const networkstatus_t
*consensus
,
367 document_signature_t
*sig
,
368 const authority_cert_t
*cert
)
370 char key_digest
[DIGEST_LEN
];
371 const int dlen
= sig
->alg
== DIGEST_SHA1
? DIGEST_LEN
: DIGEST256_LEN
;
373 size_t signed_digest_len
;
375 if (crypto_pk_get_digest(cert
->signing_key
, key_digest
)<0)
377 if (tor_memneq(sig
->signing_key_digest
, key_digest
, DIGEST_LEN
) ||
378 tor_memneq(sig
->identity_digest
, cert
->cache_info
.identity_digest
,
382 if (authority_cert_is_blacklisted(cert
)) {
383 /* We implement blacklisting for authority signing keys by treating
384 * all their signatures as always bad. That way we don't get into
385 * crazy loops of dropping and re-fetching signatures. */
386 log_warn(LD_DIR
, "Ignoring a consensus signature made with deprecated"
388 hex_str(cert
->signing_key_digest
, DIGEST_LEN
));
389 sig
->bad_signature
= 1;
393 signed_digest_len
= crypto_pk_keysize(cert
->signing_key
);
394 signed_digest
= tor_malloc(signed_digest_len
);
395 if (crypto_pk_public_checksig(cert
->signing_key
,
399 sig
->signature_len
) < dlen
||
400 tor_memneq(signed_digest
, consensus
->digests
.d
[sig
->alg
], dlen
)) {
401 log_warn(LD_DIR
, "Got a bad signature on a networkstatus vote");
402 sig
->bad_signature
= 1;
404 sig
->good_signature
= 1;
406 tor_free(signed_digest
);
410 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
411 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
412 * signature from every recognized authority on it, 0 if there are
413 * enough good signatures from recognized authorities on it, -1 if we might
414 * get enough good signatures by fetching missing certificates, and -2
415 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
416 * about every problem; if warn is at least 1, warn only if we can't get
417 * enough signatures; if warn is negative, log nothing at all. */
419 networkstatus_check_consensus_signature(networkstatus_t
*consensus
,
423 int n_missing_key
= 0, n_dl_failed_key
= 0;
426 int n_no_signature
= 0;
427 int n_v3_authorities
= get_n_authorities(V3_DIRINFO
);
428 int n_required
= n_v3_authorities
/2 + 1;
429 smartlist_t
*list_good
= smartlist_new();
430 smartlist_t
*list_no_signature
= smartlist_new();
431 smartlist_t
*need_certs_from
= smartlist_new();
432 smartlist_t
*unrecognized
= smartlist_new();
433 smartlist_t
*missing_authorities
= smartlist_new();
435 time_t now
= time(NULL
);
437 tor_assert(consensus
->type
== NS_TYPE_CONSENSUS
);
439 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*,
443 int unknown_here
= 0;
444 int missing_key_here
= 0, dl_failed_key_here
= 0;
445 SMARTLIST_FOREACH_BEGIN(voter
->sigs
, document_signature_t
*, sig
) {
446 if (!sig
->good_signature
&& !sig
->bad_signature
&&
448 /* we can try to check the signature. */
449 int is_v3_auth
= trusteddirserver_get_by_v3_auth_digest(
450 sig
->identity_digest
) != NULL
;
451 authority_cert_t
*cert
=
452 authority_cert_get_by_digests(sig
->identity_digest
,
453 sig
->signing_key_digest
);
454 tor_assert(tor_memeq(sig
->identity_digest
, voter
->identity_digest
,
458 smartlist_add(unrecognized
, voter
);
461 } else if (!cert
|| cert
->expires
< now
) {
462 smartlist_add(need_certs_from
, voter
);
464 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
465 ++dl_failed_key_here
;
468 if (networkstatus_check_document_signature(consensus
, sig
, cert
) < 0) {
469 smartlist_add(need_certs_from
, voter
);
471 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
472 ++dl_failed_key_here
;
476 if (sig
->good_signature
)
478 else if (sig
->bad_signature
)
480 } SMARTLIST_FOREACH_END(sig
);
484 smartlist_add(list_good
, voter
->nickname
);
485 } else if (bad_here
) {
487 } else if (missing_key_here
) {
489 if (dl_failed_key_here
)
491 } else if (unknown_here
) {
495 smartlist_add(list_no_signature
, voter
->nickname
);
497 } SMARTLIST_FOREACH_END(voter
);
499 /* Now see whether we're missing any voters entirely. */
500 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
503 if ((ds
->type
& V3_DIRINFO
) &&
504 !networkstatus_get_voter_by_id(consensus
, ds
->v3_identity_digest
))
505 smartlist_add(missing_authorities
, ds
);
508 if (warn
> 1 || (warn
>= 0 &&
509 (n_good
+ n_missing_key
- n_dl_failed_key
< n_required
))) {
516 SMARTLIST_FOREACH(unrecognized
, networkstatus_voter_info_t
*, voter
,
518 tor_log(severity
, LD_DIR
, "Consensus includes unrecognized authority "
519 "'%s' at %s:%d (contact %s; identity %s)",
520 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
521 voter
->contact
?voter
->contact
:"n/a",
522 hex_str(voter
->identity_digest
, DIGEST_LEN
));
524 SMARTLIST_FOREACH(need_certs_from
, networkstatus_voter_info_t
*, voter
,
526 tor_log(severity
, LD_DIR
, "Looks like we need to download a new "
527 "certificate from authority '%s' at %s:%d (contact %s; "
529 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
530 voter
->contact
?voter
->contact
:"n/a",
531 hex_str(voter
->identity_digest
, DIGEST_LEN
));
533 SMARTLIST_FOREACH(missing_authorities
, dir_server_t
*, ds
,
535 tor_log(severity
, LD_DIR
, "Consensus does not include configured "
536 "authority '%s' at %s:%d (identity %s)",
537 ds
->nickname
, ds
->address
, (int)ds
->dir_port
,
538 hex_str(ds
->v3_identity_digest
, DIGEST_LEN
));
542 smartlist_t
*sl
= smartlist_new();
543 char *tmp
= smartlist_join_strings(list_good
, " ", 0, NULL
);
544 smartlist_add_asprintf(sl
,
545 "A consensus needs %d good signatures from recognized "
546 "authorities for us to accept it. This one has %d (%s).",
547 n_required
, n_good
, tmp
);
549 if (n_no_signature
) {
550 tmp
= smartlist_join_strings(list_no_signature
, " ", 0, NULL
);
551 smartlist_add_asprintf(sl
,
552 "%d (%s) of the authorities we know didn't sign it.",
553 n_no_signature
, tmp
);
557 smartlist_add_asprintf(sl
,
558 "It has %d signatures from authorities we don't "
559 "recognize.", n_unknown
);
562 smartlist_add_asprintf(sl
, "%d of the signatures on it didn't verify "
563 "correctly.", n_bad
);
566 smartlist_add_asprintf(sl
,
567 "We were unable to check %d of the signatures, "
568 "because we were missing the keys.", n_missing_key
);
570 joined
= smartlist_join_strings(sl
, " ", 0, NULL
);
571 tor_log(severity
, LD_DIR
, "%s", joined
);
573 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
578 smartlist_free(list_good
);
579 smartlist_free(list_no_signature
);
580 smartlist_free(unrecognized
);
581 smartlist_free(need_certs_from
);
582 smartlist_free(missing_authorities
);
584 if (n_good
== n_v3_authorities
)
586 else if (n_good
>= n_required
)
588 else if (n_good
+ n_missing_key
>= n_required
)
594 /** How far in the future do we allow a network-status to get before removing
596 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
598 /** Helper for bsearching a list of routerstatus_t pointers: compare a
599 * digest in the key to the identity digest of a routerstatus_t. */
601 compare_digest_to_routerstatus_entry(const void *_key
, const void **_member
)
603 const char *key
= _key
;
604 const routerstatus_t
*rs
= *_member
;
605 return tor_memcmp(key
, rs
->identity_digest
, DIGEST_LEN
);
608 /** Helper for bsearching a list of routerstatus_t pointers: compare a
609 * digest in the key to the identity digest of a routerstatus_t. */
611 compare_digest_to_vote_routerstatus_entry(const void *_key
,
612 const void **_member
)
614 const char *key
= _key
;
615 const vote_routerstatus_t
*vrs
= *_member
;
616 return tor_memcmp(key
, vrs
->status
.identity_digest
, DIGEST_LEN
);
619 /** As networkstatus_find_entry, but do not return a const pointer */
621 networkstatus_vote_find_mutable_entry(networkstatus_t
*ns
, const char *digest
)
623 return smartlist_bsearch(ns
->routerstatus_list
, digest
,
624 compare_digest_to_routerstatus_entry
);
627 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
628 * NULL if none was found. */
629 const routerstatus_t
*
630 networkstatus_vote_find_entry(networkstatus_t
*ns
, const char *digest
)
632 return networkstatus_vote_find_mutable_entry(ns
, digest
);
635 /*XXXX MOVE make this static once functions are moved into this file. */
636 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
637 * <b>digest</b>. Return value and set *<b>found_out</b> as for
638 * smartlist_bsearch_idx(). */
640 networkstatus_vote_find_entry_idx(networkstatus_t
*ns
,
641 const char *digest
, int *found_out
)
643 return smartlist_bsearch_idx(ns
->routerstatus_list
, digest
,
644 compare_digest_to_routerstatus_entry
,
648 /** As router_get_consensus_status_by_descriptor_digest, but does not return
649 * a const pointer. */
650 MOCK_IMPL(routerstatus_t
*,
651 router_get_mutable_consensus_status_by_descriptor_digest
,(
652 networkstatus_t
*consensus
,
656 consensus
= current_consensus
;
659 if (!consensus
->desc_digest_map
) {
660 digestmap_t
*m
= consensus
->desc_digest_map
= digestmap_new();
661 SMARTLIST_FOREACH(consensus
->routerstatus_list
,
662 routerstatus_t
*, rs
,
664 digestmap_set(m
, rs
->descriptor_digest
, rs
);
667 return digestmap_get(consensus
->desc_digest_map
, digest
);
670 /** Return the consensus view of the status of the router whose current
671 * <i>descriptor</i> digest in <b>consensus</b> is <b>digest</b>, or NULL if
672 * no such router is known. */
673 const routerstatus_t
*
674 router_get_consensus_status_by_descriptor_digest(networkstatus_t
*consensus
,
677 return router_get_mutable_consensus_status_by_descriptor_digest(
681 /** Return a smartlist of all router descriptor digests in a consensus */
683 router_get_descriptor_digests_in_consensus(networkstatus_t
*consensus
)
685 smartlist_t
*result
= smartlist_new();
691 for (i
= digestmap_iter_init(consensus
->desc_digest_map
);
692 !(digestmap_iter_done(i
));
693 i
= digestmap_iter_next(consensus
->desc_digest_map
, i
)) {
694 digestmap_iter_get(i
, &digest
, &rs
);
695 digest_tmp
= tor_malloc(DIGEST_LEN
);
696 memcpy(digest_tmp
, digest
, DIGEST_LEN
);
697 smartlist_add(result
, digest_tmp
);
703 /** Return a smartlist of all router descriptor digests in the current
705 MOCK_IMPL(smartlist_t
*,
706 router_get_descriptor_digests
,(void))
708 smartlist_t
*result
= NULL
;
710 if (current_ns_consensus
) {
712 router_get_descriptor_digests_in_consensus(current_ns_consensus
);
718 /** Given the digest of a router descriptor, return its current download
719 * status, or NULL if the digest is unrecognized. */
720 MOCK_IMPL(download_status_t
*,
721 router_get_dl_status_by_descriptor_digest
,(const char *d
))
724 if (!current_ns_consensus
)
726 if ((rs
= router_get_mutable_consensus_status_by_descriptor_digest(
727 current_ns_consensus
, d
)))
728 return &rs
->dl_status
;
733 /** As router_get_consensus_status_by_id, but do not return a const pointer */
735 router_get_mutable_consensus_status_by_id(const char *digest
)
737 if (!current_consensus
)
739 return smartlist_bsearch(current_consensus
->routerstatus_list
, digest
,
740 compare_digest_to_routerstatus_entry
);
743 /** Return the consensus view of the status of the router whose identity
744 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
745 const routerstatus_t
*
746 router_get_consensus_status_by_id(const char *digest
)
748 return router_get_mutable_consensus_status_by_id(digest
);
751 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
752 * the corresponding routerstatus_t, or NULL if none exists. Warn the
753 * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
754 * nickname, but the Named flag isn't set for that router. */
755 const routerstatus_t
*
756 router_get_consensus_status_by_nickname(const char *nickname
,
759 const node_t
*node
= node_get_by_nickname(nickname
, warn_if_unnamed
);
766 /** Return the identity digest that's mapped to officially by
767 * <b>nickname</b>. */
769 networkstatus_get_router_digest_by_nickname(const char *nickname
)
771 if (!named_server_map
)
773 return strmap_get_lc(named_server_map
, nickname
);
776 /** Return true iff <b>nickname</b> is disallowed from being the nickname
779 networkstatus_nickname_is_unnamed(const char *nickname
)
781 if (!unnamed_server_map
)
783 return strmap_get_lc(unnamed_server_map
, nickname
) != NULL
;
786 /** How frequently do directory authorities re-download fresh networkstatus
788 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
790 /** How frequently do non-authority directory caches re-download fresh
791 * networkstatus documents? */
792 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
794 /** Return true iff, given the options listed in <b>options</b>, <b>flavor</b>
795 * is the flavor of a consensus networkstatus that we would like to fetch. */
797 we_want_to_fetch_flavor(const or_options_t
*options
, int flavor
)
799 if (flavor
< 0 || flavor
> N_CONSENSUS_FLAVORS
) {
800 /* This flavor is crazy; we don't want it */
801 /*XXXX handle unrecognized flavors later */
804 if (authdir_mode_v3(options
) || directory_caches_dir_info(options
)) {
805 /* We want to serve all flavors to others, regardless if we would use
809 if (options
->FetchUselessDescriptors
) {
810 /* In order to get all descriptors, we need to fetch all consensuses. */
813 /* Otherwise, we want the flavor only if we want to use it to build
815 return flavor
== usable_consensus_flavor();
818 /** How long will we hang onto a possibly live consensus for which we're
819 * fetching certs before we check whether there is a better one? */
820 #define DELAY_WHILE_FETCHING_CERTS (20*60)
822 /* Check if a downloaded consensus flavor should still wait for certificates
824 * If so, return 1. If not, fail dls and return 0. */
826 check_consensus_waiting_for_certs(int flavor
, time_t now
,
827 download_status_t
*dls
)
829 consensus_waiting_for_certs_t
*waiting
;
831 /* We should always have a known flavor, because we_want_to_fetch_flavor()
832 * filters out unknown flavors. */
833 tor_assert(flavor
>= 0 && flavor
< N_CONSENSUS_FLAVORS
);
835 waiting
= &consensus_waiting_for_certs
[flavor
];
836 if (waiting
->consensus
) {
837 /* XXXX make sure this doesn't delay sane downloads. */
838 if (waiting
->set_at
+ DELAY_WHILE_FETCHING_CERTS
> now
) {
841 if (!waiting
->dl_failed
) {
842 download_status_failed(dls
, 0);
843 waiting
->dl_failed
=1;
851 /** If we want to download a fresh consensus, launch a new download as
854 update_consensus_networkstatus_downloads(time_t now
)
857 const or_options_t
*options
= get_options();
858 const int we_are_bootstrapping
= networkstatus_consensus_is_bootstrapping(
860 const int use_multi_conn
=
861 networkstatus_consensus_can_use_multiple_directories(options
);
863 if (should_delay_dir_fetches(options
, NULL
))
866 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
867 /* XXXX need some way to download unknown flavors if we are caching. */
868 const char *resource
;
870 int max_in_progress_conns
= 1;
872 if (! we_want_to_fetch_flavor(options
, i
))
875 c
= networkstatus_get_latest_consensus_by_flavor(i
);
876 if (! (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
)) {
877 /* No live consensus? Get one now!*/
878 time_to_download_next_consensus
[i
] = now
;
881 if (time_to_download_next_consensus
[i
] > now
)
882 continue; /* Wait until the current consensus is older. */
884 resource
= networkstatus_get_flavor_name(i
);
886 /* Check if we already have enough connections in progress */
887 if (we_are_bootstrapping
) {
888 max_in_progress_conns
=
889 options
->ClientBootstrapConsensusMaxInProgressTries
;
891 if (connection_dir_count_by_purpose_and_resource(
892 DIR_PURPOSE_FETCH_CONSENSUS
,
894 >= max_in_progress_conns
) {
898 /* Check if we want to launch another download for a usable consensus.
899 * Only used during bootstrap. */
900 if (we_are_bootstrapping
&& use_multi_conn
901 && i
== usable_consensus_flavor()) {
903 /* Check if we're already downloading a usable consensus */
904 if (networkstatus_consensus_is_already_downloading(resource
))
907 /* Make multiple connections for a bootstrap consensus download. */
908 update_consensus_bootstrap_multiple_downloads(now
, options
);
910 /* Check if we failed downloading a consensus too recently */
911 int max_dl_tries
= options
->TestingConsensusMaxDownloadTries
;
913 /* Let's make sure we remembered to update consensus_dl_status */
914 tor_assert(consensus_dl_status
[i
].schedule
== DL_SCHED_CONSENSUS
);
916 if (!download_status_is_ready(&consensus_dl_status
[i
],
922 /* Check if we're waiting for certificates to download */
923 if (check_consensus_waiting_for_certs(i
, now
, &consensus_dl_status
[i
]))
926 /* Try the requested attempt */
927 log_info(LD_DIR
, "Launching %s standard networkstatus consensus "
928 "download.", networkstatus_get_flavor_name(i
));
929 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
930 ROUTER_PURPOSE_GENERAL
, resource
,
931 PDS_RETRY_IF_NO_SERVERS
,
932 consensus_dl_status
[i
].want_authority
);
937 /** When we're bootstrapping, launch one or more consensus download
938 * connections, if schedule indicates connection(s) should be made after now.
939 * If is_authority, connect to an authority, otherwise, use a fallback
943 update_consensus_bootstrap_attempt_downloads(
945 const or_options_t
*options
,
946 download_status_t
*dls
,
947 download_want_authority_t want_authority
)
949 int use_fallbacks
= networkstatus_consensus_can_use_extra_fallbacks(options
);
950 int max_dl_tries
= options
->ClientBootstrapConsensusMaxDownloadTries
;
951 if (!use_fallbacks
) {
953 options
->ClientBootstrapConsensusAuthorityOnlyMaxDownloadTries
;
956 const char *resource
= networkstatus_get_flavor_name(
957 usable_consensus_flavor());
959 /* Let's make sure we remembered to update schedule */
960 tor_assert(dls
->schedule
== DL_SCHED_CONSENSUS
);
962 /* Allow for multiple connections in the same second, if the schedule value
964 while (download_status_is_ready(dls
, now
, max_dl_tries
)) {
965 log_info(LD_DIR
, "Launching %s bootstrap %s networkstatus consensus "
966 "download.", resource
, (want_authority
== DL_WANT_AUTHORITY
970 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
971 ROUTER_PURPOSE_GENERAL
, resource
,
972 PDS_RETRY_IF_NO_SERVERS
, want_authority
);
973 /* schedule the next attempt */
974 download_status_increment_attempt(dls
, resource
, now
);
978 /** If we're bootstrapping, check the connection schedules and see if we want
979 * to make additional, potentially concurrent, consensus download
981 * Only call when bootstrapping, and when we want to make additional
982 * connections. Only nodes that satisfy
983 * networkstatus_consensus_can_use_multiple_directories make additional
987 update_consensus_bootstrap_multiple_downloads(time_t now
,
988 const or_options_t
*options
)
990 const int usable_flavor
= usable_consensus_flavor();
992 /* make sure we can use multiple connections */
993 if (!networkstatus_consensus_can_use_multiple_directories(options
)) {
997 /* Launch concurrent consensus download attempt(s) based on the mirror and
998 * authority schedules. Try the mirror first - this makes it slightly more
999 * likely that we'll connect to the fallback first, and then end the
1000 * authority connection attempt. */
1002 /* If a consensus download fails because it's waiting for certificates,
1003 * we'll fail both the authority and fallback schedules. This is better than
1004 * failing only one of the schedules, and having the other continue
1008 /* If we don't have or can't use extra fallbacks, don't try them. */
1009 if (networkstatus_consensus_can_use_extra_fallbacks(options
)) {
1010 download_status_t
*dls_f
=
1011 &consensus_bootstrap_dl_status
[CONSENSUS_BOOTSTRAP_SOURCE_ANY_DIRSERVER
];
1013 if (!check_consensus_waiting_for_certs(usable_flavor
, now
, dls_f
)) {
1014 /* During bootstrap, DL_WANT_ANY_DIRSERVER means "use fallbacks". */
1015 update_consensus_bootstrap_attempt_downloads(now
, options
, dls_f
,
1016 DL_WANT_ANY_DIRSERVER
);
1020 /* Now try an authority. */
1021 download_status_t
*dls_a
=
1022 &consensus_bootstrap_dl_status
[CONSENSUS_BOOTSTRAP_SOURCE_AUTHORITY
];
1024 if (!check_consensus_waiting_for_certs(usable_flavor
, now
, dls_a
)) {
1025 update_consensus_bootstrap_attempt_downloads(now
, options
, dls_a
,
1030 /** Called when an attempt to download a consensus fails: note that the
1031 * failure occurred, and possibly retry. */
1033 networkstatus_consensus_download_failed(int status_code
, const char *flavname
)
1035 int flav
= networkstatus_parse_flavor_name(flavname
);
1037 tor_assert(flav
< N_CONSENSUS_FLAVORS
);
1038 /* XXXX handle unrecognized flavors */
1039 download_status_failed(&consensus_dl_status
[flav
], status_code
);
1040 /* Retry immediately, if appropriate. */
1041 update_consensus_networkstatus_downloads(time(NULL
));
1045 /** How long do we (as a cache) wait after a consensus becomes non-fresh
1046 * before trying to fetch another? */
1047 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
1049 /** Update the time at which we'll consider replacing the current
1050 * consensus of flavor <b>flav</b> */
1052 update_consensus_networkstatus_fetch_time_impl(time_t now
, int flav
)
1054 const or_options_t
*options
= get_options();
1055 networkstatus_t
*c
= networkstatus_get_latest_consensus_by_flavor(flav
);
1056 const char *flavor
= networkstatus_get_flavor_name(flav
);
1057 if (! we_want_to_fetch_flavor(get_options(), flav
))
1060 if (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1062 long interval
= c
->fresh_until
- c
->valid_after
;
1063 long min_sec_before_caching
= CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1066 if (min_sec_before_caching
> interval
/16) {
1067 /* Usually we allow 2-minutes slop factor in case clocks get
1068 desynchronized a little. If we're on a private network with
1069 a crazy-fast voting interval, though, 2 minutes may be too
1071 min_sec_before_caching
= interval
/16;
1072 /* make sure we always delay by at least a second before caching */
1073 if (min_sec_before_caching
== 0) {
1074 min_sec_before_caching
= 1;
1078 if (directory_fetches_dir_info_early(options
)) {
1079 /* We want to cache the next one at some point after this one
1080 * is no longer fresh... */
1081 start
= (time_t)(c
->fresh_until
+ min_sec_before_caching
);
1082 /* Some clients may need the consensus sooner than others. */
1083 if (options
->FetchDirInfoExtraEarly
|| authdir_mode_v3(options
)) {
1085 if (min_sec_before_caching
+ dl_interval
> interval
)
1086 dl_interval
= interval
/2;
1088 /* But only in the first half-interval after that. */
1089 dl_interval
= interval
/2;
1092 /* We're an ordinary client, a bridge, or a hidden service.
1093 * Give all the caches enough time to download the consensus. */
1094 start
= (time_t)(c
->fresh_until
+ (interval
*3)/4);
1095 /* But download the next one well before this one is expired. */
1096 dl_interval
= ((c
->valid_until
- start
) * 7 )/ 8;
1098 /* If we're a bridge user, make use of the numbers we just computed
1099 * to choose the rest of the interval *after* them. */
1100 if (directory_fetches_dir_info_later(options
)) {
1101 /* Give all the *clients* enough time to download the consensus. */
1102 start
= (time_t)(start
+ dl_interval
+ min_sec_before_caching
);
1103 /* But try to get it before ours actually expires. */
1104 dl_interval
= (c
->valid_until
- start
) - min_sec_before_caching
;
1107 /* catch low dl_interval in crazy-fast networks */
1108 if (dl_interval
< 1)
1110 /* catch late start in crazy-fast networks */
1111 if (start
+dl_interval
>= c
->valid_until
)
1112 start
= c
->valid_until
- dl_interval
- 1;
1114 "fresh_until: %ld start: %ld "
1115 "dl_interval: %ld valid_until: %ld ",
1116 (long)c
->fresh_until
, (long)start
, dl_interval
,
1117 (long)c
->valid_until
);
1118 /* We must not try to replace c while it's still fresh: */
1119 tor_assert(c
->fresh_until
< start
);
1120 /* We must download the next one before c is invalid: */
1121 tor_assert(start
+dl_interval
< c
->valid_until
);
1122 time_to_download_next_consensus
[flav
] =
1123 start
+ crypto_rand_int((int)dl_interval
);
1125 char tbuf1
[ISO_TIME_LEN
+1];
1126 char tbuf2
[ISO_TIME_LEN
+1];
1127 char tbuf3
[ISO_TIME_LEN
+1];
1128 format_local_iso_time(tbuf1
, c
->fresh_until
);
1129 format_local_iso_time(tbuf2
, c
->valid_until
);
1130 format_local_iso_time(tbuf3
, time_to_download_next_consensus
[flav
]);
1131 log_info(LD_DIR
, "Live %s consensus %s the most recent until %s and "
1132 "will expire at %s; fetching the next one at %s.",
1133 flavor
, (c
->fresh_until
> now
) ? "will be" : "was",
1134 tbuf1
, tbuf2
, tbuf3
);
1137 time_to_download_next_consensus
[flav
] = now
;
1138 log_info(LD_DIR
, "No live %s consensus; we should fetch one immediately.",
1143 /** Update the time at which we'll consider replacing the current
1144 * consensus of flavor 'flavor' */
1146 update_consensus_networkstatus_fetch_time(time_t now
)
1149 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1150 if (we_want_to_fetch_flavor(get_options(), i
))
1151 update_consensus_networkstatus_fetch_time_impl(now
, i
);
1155 /** Return 1 if there's a reason we shouldn't try any directory
1156 * fetches yet (e.g. we demand bridges and none are yet known).
1159 * If we return 1 and <b>msg_out</b> is provided, set <b>msg_out</b>
1160 * to an explanation of why directory fetches are delayed. (If we
1161 * return 0, we set msg_out to NULL.)
1164 should_delay_dir_fetches(const or_options_t
*options
, const char **msg_out
)
1170 if (options
->DisableNetwork
) {
1172 *msg_out
= "DisableNetwork is set.";
1174 log_info(LD_DIR
, "Delaying dir fetches (DisableNetwork is set)");
1178 if (options
->UseBridges
) {
1179 if (!any_bridge_descriptors_known()) {
1181 *msg_out
= "No running bridges";
1183 log_info(LD_DIR
, "Delaying dir fetches (no running bridges known)");
1187 if (pt_proxies_configuration_pending()) {
1189 *msg_out
= "Pluggable transport proxies still configuring";
1191 log_info(LD_DIR
, "Delaying dir fetches (pt proxies still configuring)");
1199 /** Launch requests for networkstatus documents and authority certificates as
1202 update_networkstatus_downloads(time_t now
)
1204 const or_options_t
*options
= get_options();
1205 if (should_delay_dir_fetches(options
, NULL
))
1207 update_consensus_networkstatus_downloads(now
);
1208 update_certificate_downloads(now
);
1211 /** Launch requests as appropriate for missing directory authority
1214 update_certificate_downloads(time_t now
)
1217 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1218 if (consensus_waiting_for_certs
[i
].consensus
)
1219 authority_certs_fetch_missing(consensus_waiting_for_certs
[i
].consensus
,
1223 if (current_ns_consensus
)
1224 authority_certs_fetch_missing(current_ns_consensus
, now
, NULL
);
1225 if (current_md_consensus
)
1226 authority_certs_fetch_missing(current_md_consensus
, now
, NULL
);
1229 /** Return 1 if we have a consensus but we don't have enough certificates
1230 * to start using it yet. */
1232 consensus_is_waiting_for_certs(void)
1234 return consensus_waiting_for_certs
[usable_consensus_flavor()].consensus
1238 /** Look up the currently active (depending on bootstrap status) download
1239 * status for this consensus flavor and return a pointer to it.
1241 MOCK_IMPL(download_status_t
*,
1242 networkstatus_get_dl_status_by_flavor
,(consensus_flavor_t flavor
))
1244 download_status_t
*dl
= NULL
;
1245 const int we_are_bootstrapping
=
1246 networkstatus_consensus_is_bootstrapping(time(NULL
));
1248 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1249 dl
= &((we_are_bootstrapping
?
1250 consensus_bootstrap_dl_status
: consensus_dl_status
)[flavor
]);
1256 /** Look up the bootstrap download status for this consensus flavor
1257 * and return a pointer to it. */
1258 MOCK_IMPL(download_status_t
*,
1259 networkstatus_get_dl_status_by_flavor_bootstrap
,(consensus_flavor_t flavor
))
1261 download_status_t
*dl
= NULL
;
1263 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1264 dl
= &(consensus_bootstrap_dl_status
[flavor
]);
1270 /** Look up the running (non-bootstrap) download status for this consensus
1271 * flavor and return a pointer to it. */
1272 MOCK_IMPL(download_status_t
*,
1273 networkstatus_get_dl_status_by_flavor_running
,(consensus_flavor_t flavor
))
1275 download_status_t
*dl
= NULL
;
1277 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1278 dl
= &(consensus_dl_status
[flavor
]);
1284 /** Return the most recent consensus that we have downloaded, or NULL if we
1285 * don't have one. */
1286 MOCK_IMPL(networkstatus_t
*,
1287 networkstatus_get_latest_consensus
,(void))
1289 return current_consensus
;
1292 /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
1293 * if we don't have one. */
1294 MOCK_IMPL(networkstatus_t
*,
1295 networkstatus_get_latest_consensus_by_flavor
,(consensus_flavor_t f
))
1298 return current_ns_consensus
;
1299 else if (f
== FLAV_MICRODESC
)
1300 return current_md_consensus
;
1307 /** Return the most recent consensus that we have downloaded, or NULL if it is
1308 * no longer live. */
1309 MOCK_IMPL(networkstatus_t
*,
1310 networkstatus_get_live_consensus
,(time_t now
))
1312 if (current_consensus
&&
1313 current_consensus
->valid_after
<= now
&&
1314 now
<= current_consensus
->valid_until
)
1315 return current_consensus
;
1320 /* XXXX remove this in favor of get_live_consensus. But actually,
1321 * leave something like it for bridge users, who need to not totally
1322 * lose if they spend a while fetching a new consensus. */
1323 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1326 networkstatus_get_reasonably_live_consensus(time_t now
, int flavor
)
1328 #define REASONABLY_LIVE_TIME (24*60*60)
1329 networkstatus_t
*consensus
=
1330 networkstatus_get_latest_consensus_by_flavor(flavor
);
1332 consensus
->valid_after
<= now
&&
1333 now
<= consensus
->valid_until
+REASONABLY_LIVE_TIME
)
1339 /** Check if we need to download a consensus during tor's bootstrap phase.
1340 * If we have no consensus, or our consensus is unusably old, return 1.
1341 * As soon as we have received a consensus, return 0, even if we don't have
1342 * enough certificates to validate it.
1343 * If a fallback directory gives us a consensus we can never get certs for,
1344 * check_consensus_waiting_for_certs() will wait 20 minutes before failing
1345 * the cert downloads. After that, a new consensus will be fetched from a
1346 * randomly chosen fallback. */
1348 networkstatus_consensus_is_bootstrapping
,(time_t now
))
1350 /* If we have a validated, reasonably live consensus, we're not
1351 * bootstrapping a consensus at all. */
1352 if (networkstatus_get_reasonably_live_consensus(
1354 usable_consensus_flavor())) {
1358 /* If we have a consensus, but we're waiting for certificates,
1359 * we're not waiting for a consensus download while bootstrapping. */
1360 if (consensus_is_waiting_for_certs()) {
1364 /* If we have no consensus, or our consensus is very old, we are
1365 * bootstrapping, and we need to download a consensus. */
1369 /** Check if we can use multiple directories for a consensus download.
1370 * Only clients (including bridge relays, which act like clients) benefit
1371 * from multiple simultaneous consensus downloads. */
1373 networkstatus_consensus_can_use_multiple_directories(
1374 const or_options_t
*options
)
1376 /* If we are a client, bridge, bridge client, or hidden service */
1377 return !public_server_mode(options
);
1380 /** Check if we can use fallback directory mirrors for a consensus download.
1381 * If we have fallbacks and don't want to fetch from the authorities,
1382 * we can use them. */
1384 networkstatus_consensus_can_use_extra_fallbacks
,(const or_options_t
*options
))
1386 /* The list length comparisons are a quick way to check if we have any
1387 * non-authority fallback directories. If we ever have any authorities that
1388 * aren't fallback directories, we will need to change this code. */
1389 tor_assert(smartlist_len(router_get_fallback_dir_servers())
1390 >= smartlist_len(router_get_trusted_dir_servers()));
1391 /* If we don't fetch from the authorities, and we have additional mirrors,
1392 * we can use them. */
1393 return (!directory_fetches_from_authorities(options
)
1394 && (smartlist_len(router_get_fallback_dir_servers())
1395 > smartlist_len(router_get_trusted_dir_servers())));
1398 /* Is there a consensus fetch for flavor <b>resource</b> that's far
1399 * enough along to be attached to a circuit? */
1401 networkstatus_consensus_is_already_downloading(const char *resource
)
1405 /* First, get a list of all the dir conns that are fetching a consensus,
1406 * fetching *this* consensus, and are in state "reading" (meaning they
1407 * have already flushed their request onto the socks connection). */
1408 smartlist_t
*fetching_conns
=
1409 connection_dir_list_by_purpose_resource_and_state(
1410 DIR_PURPOSE_FETCH_CONSENSUS
, resource
, DIR_CONN_STATE_CLIENT_READING
);
1412 /* Then, walk through each conn, to see if its linked socks connection
1413 * is in an attached state. We have to check this separately, since with
1414 * the optimistic data feature, fetches can send their request to the
1415 * socks connection and go into state 'reading', even before they're
1416 * attached to any circuit. */
1417 SMARTLIST_FOREACH_BEGIN(fetching_conns
, dir_connection_t
*, dirconn
) {
1418 /* Do any of these other dir conns have a linked socks conn that is
1419 * attached to a circuit already? */
1420 connection_t
*base
= TO_CONN(dirconn
);
1421 if (base
->linked_conn
&&
1422 base
->linked_conn
->type
== CONN_TYPE_AP
&&
1423 !AP_CONN_STATE_IS_UNATTACHED(base
->linked_conn
->state
)) {
1425 break; /* stop looping, because we know the answer will be yes */
1427 } SMARTLIST_FOREACH_END(dirconn
);
1428 smartlist_free(fetching_conns
);
1433 /** Given two router status entries for the same router identity, return 1 if
1434 * if the contents have changed between them. Otherwise, return 0. */
1436 routerstatus_has_changed(const routerstatus_t
*a
, const routerstatus_t
*b
)
1438 tor_assert(tor_memeq(a
->identity_digest
, b
->identity_digest
, DIGEST_LEN
));
1440 return strcmp(a
->nickname
, b
->nickname
) ||
1441 fast_memneq(a
->descriptor_digest
, b
->descriptor_digest
, DIGEST_LEN
) ||
1442 a
->addr
!= b
->addr
||
1443 a
->or_port
!= b
->or_port
||
1444 a
->dir_port
!= b
->dir_port
||
1445 a
->is_authority
!= b
->is_authority
||
1446 a
->is_exit
!= b
->is_exit
||
1447 a
->is_stable
!= b
->is_stable
||
1448 a
->is_fast
!= b
->is_fast
||
1449 a
->is_flagged_running
!= b
->is_flagged_running
||
1450 a
->is_named
!= b
->is_named
||
1451 a
->is_unnamed
!= b
->is_unnamed
||
1452 a
->is_valid
!= b
->is_valid
||
1453 a
->is_possible_guard
!= b
->is_possible_guard
||
1454 a
->is_bad_exit
!= b
->is_bad_exit
||
1455 a
->is_hs_dir
!= b
->is_hs_dir
||
1456 a
->version_known
!= b
->version_known
;
1459 /** Notify controllers of any router status entries that changed between
1460 * <b>old_c</b> and <b>new_c</b>. */
1462 notify_control_networkstatus_changed(const networkstatus_t
*old_c
,
1463 const networkstatus_t
*new_c
)
1465 smartlist_t
*changed
;
1469 /* tell the controller exactly which relays are still listed, as well
1470 * as what they're listed as */
1471 control_event_newconsensus(new_c
);
1473 if (!control_event_is_interesting(EVENT_NS
))
1477 control_event_networkstatus_changed(new_c
->routerstatus_list
);
1480 changed
= smartlist_new();
1482 SMARTLIST_FOREACH_JOIN(
1483 old_c
->routerstatus_list
, const routerstatus_t
*, rs_old
,
1484 new_c
->routerstatus_list
, const routerstatus_t
*, rs_new
,
1485 tor_memcmp(rs_old
->identity_digest
,
1486 rs_new
->identity_digest
, DIGEST_LEN
),
1487 smartlist_add(changed
, (void*) rs_new
)) {
1488 if (routerstatus_has_changed(rs_old
, rs_new
))
1489 smartlist_add(changed
, (void*)rs_new
);
1490 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1492 control_event_networkstatus_changed(changed
);
1493 smartlist_free(changed
);
1496 /** Copy all the ancillary information (like router download status and so on)
1497 * from <b>old_c</b> to <b>new_c</b>. */
1499 networkstatus_copy_old_consensus_info(networkstatus_t
*new_c
,
1500 const networkstatus_t
*old_c
)
1504 if (!old_c
|| !smartlist_len(old_c
->routerstatus_list
))
1507 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1508 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1509 tor_memcmp(rs_old
->identity_digest
,
1510 rs_new
->identity_digest
, DIGEST_LEN
),
1512 /* Okay, so we're looking at the same identity. */
1513 rs_new
->last_dir_503_at
= rs_old
->last_dir_503_at
;
1515 if (tor_memeq(rs_old
->descriptor_digest
, rs_new
->descriptor_digest
,
1517 /* And the same descriptor too! */
1518 memcpy(&rs_new
->dl_status
, &rs_old
->dl_status
,sizeof(download_status_t
));
1520 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1523 #ifdef TOR_UNIT_TESTS
1524 /**Accept a <b>flavor</b> consensus <b>c</b> without any additional
1525 * validation. This is exclusively for unit tests.
1526 * We copy any ancillary information from a pre-existing consensus
1527 * and then free the current one and replace it with the newly
1528 * provided instance. Returns -1 on unrecognized flavor, 0 otherwise.
1531 networkstatus_set_current_consensus_from_ns(networkstatus_t
*c
,
1534 int flav
= networkstatus_parse_flavor_name(flavor
);
1537 if (current_ns_consensus
) {
1538 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1539 networkstatus_vote_free(current_ns_consensus
);
1541 current_ns_consensus
= c
;
1543 case FLAV_MICRODESC
:
1544 if (current_md_consensus
) {
1545 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1546 networkstatus_vote_free(current_md_consensus
);
1548 current_md_consensus
= c
;
1551 return current_md_consensus
? 0 : -1;
1553 #endif //TOR_UNIT_TESTS
1555 /** Called when we have received a networkstatus <b>c</b>. If there are
1556 * any _required_ protocols we are missing, log an error and exit
1557 * immediately. If there are any _recommended_ protocols we are missing,
1560 handle_missing_protocol_warning(const networkstatus_t
*c
,
1561 const or_options_t
*options
)
1563 char *protocol_warning
= NULL
;
1564 int should_exit
= networkstatus_check_required_protocols(c
,
1565 !server_mode(options
),
1567 if (protocol_warning
) {
1568 tor_log(should_exit
? LOG_ERR
: LOG_WARN
,
1570 "%s", protocol_warning
);
1573 tor_assert_nonfatal(protocol_warning
);
1575 tor_free(protocol_warning
);
1580 /** Try to replace the current cached v3 networkstatus with the one in
1581 * <b>consensus</b>. If we don't have enough certificates to validate it,
1582 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1584 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1585 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1586 * already received, but we were waiting for certificates on it. If flags &
1587 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1588 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1589 * consensus, even if it comes from many days in the past.
1591 * If source_dir is non-NULL, it's the identity digest for a directory that
1592 * we've just successfully retrieved a consensus or certificates from, so try
1593 * it first to fetch any missing certificates.
1595 * Return 0 on success, <0 on failure. On failure, caller should increment
1596 * the failure count as appropriate.
1598 * We return -1 for mild failures that don't need to be reported to the
1599 * user, and -2 for more serious problems.
1602 networkstatus_set_current_consensus(const char *consensus
,
1605 const char *source_dir
)
1607 networkstatus_t
*c
=NULL
;
1609 time_t now
= time(NULL
);
1610 const or_options_t
*options
= get_options();
1611 char *unverified_fname
= NULL
, *consensus_fname
= NULL
;
1612 int flav
= networkstatus_parse_flavor_name(flavor
);
1613 const unsigned from_cache
= flags
& NSSET_FROM_CACHE
;
1614 const unsigned was_waiting_for_certs
= flags
& NSSET_WAS_WAITING_FOR_CERTS
;
1615 const unsigned dl_certs
= !(flags
& NSSET_DONT_DOWNLOAD_CERTS
);
1616 const unsigned accept_obsolete
= flags
& NSSET_ACCEPT_OBSOLETE
;
1617 const unsigned require_flavor
= flags
& NSSET_REQUIRE_FLAVOR
;
1618 const common_digests_t
*current_digests
= NULL
;
1619 consensus_waiting_for_certs_t
*waiting
= NULL
;
1620 time_t current_valid_after
= 0;
1621 int free_consensus
= 1; /* Free 'c' at the end of the function */
1622 int old_ewma_enabled
;
1623 int checked_protocols_already
= 0;
1626 /* XXXX we don't handle unrecognized flavors yet. */
1627 log_warn(LD_BUG
, "Unrecognized consensus flavor %s", flavor
);
1631 /* Make sure it's parseable. */
1632 c
= networkstatus_parse_vote_from_string(consensus
, NULL
, NS_TYPE_CONSENSUS
);
1634 log_warn(LD_DIR
, "Unable to parse networkstatus consensus");
1639 if (from_cache
&& !was_waiting_for_certs
) {
1640 /* We previously stored this; check _now_ to make sure that version-kills
1641 * really work. This happens even before we check signatures: we did so
1642 * before when we stored this to disk. This does mean an attacker who can
1643 * write to the datadir can make us not start: such an attacker could
1644 * already harm us by replacing our guards, which would be worse. */
1645 checked_protocols_already
= 1;
1646 handle_missing_protocol_warning(c
, options
);
1649 if ((int)c
->flavor
!= flav
) {
1650 /* This wasn't the flavor we thought we were getting. */
1651 if (require_flavor
) {
1652 log_warn(LD_DIR
, "Got consensus with unexpected flavor %s (wanted %s)",
1653 networkstatus_get_flavor_name(c
->flavor
), flavor
);
1657 flavor
= networkstatus_get_flavor_name(flav
);
1660 if (flav
!= usable_consensus_flavor() &&
1661 !directory_caches_dir_info(options
)) {
1662 /* This consensus is totally boring to us: we won't use it, and we won't
1663 * serve it. Drop it. */
1667 if (from_cache
&& !accept_obsolete
&&
1668 c
->valid_until
< now
-OLD_ROUTER_DESC_MAX_AGE
) {
1669 log_info(LD_DIR
, "Loaded an expired consensus. Discarding.");
1673 if (!strcmp(flavor
, "ns")) {
1674 consensus_fname
= get_datadir_fname("cached-consensus");
1675 unverified_fname
= get_datadir_fname("unverified-consensus");
1676 if (current_ns_consensus
) {
1677 current_digests
= ¤t_ns_consensus
->digests
;
1678 current_valid_after
= current_ns_consensus
->valid_after
;
1680 } else if (!strcmp(flavor
, "microdesc")) {
1681 consensus_fname
= get_datadir_fname("cached-microdesc-consensus");
1682 unverified_fname
= get_datadir_fname("unverified-microdesc-consensus");
1683 if (current_md_consensus
) {
1684 current_digests
= ¤t_md_consensus
->digests
;
1685 current_valid_after
= current_md_consensus
->valid_after
;
1690 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
1691 consensus_fname
= get_datadir_fname(buf
);
1692 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
1693 unverified_fname
= get_datadir_fname(buf
);
1694 cur
= dirserv_get_consensus(flavor
);
1696 current_digests
= &cur
->digests
;
1697 current_valid_after
= cur
->published
;
1701 if (current_digests
&&
1702 tor_memeq(&c
->digests
, current_digests
, sizeof(c
->digests
))) {
1703 /* We already have this one. That's a failure. */
1704 log_info(LD_DIR
, "Got a %s consensus we already have", flavor
);
1708 if (current_valid_after
&& c
->valid_after
<= current_valid_after
) {
1709 /* We have a newer one. There's no point in accepting this one,
1710 * even if it's great. */
1711 log_info(LD_DIR
, "Got a %s consensus at least as old as the one we have",
1716 /* Make sure it's signed enough. */
1717 if ((r
=networkstatus_check_consensus_signature(c
, 1))<0) {
1719 /* Okay, so it _might_ be signed enough if we get more certificates. */
1720 if (!was_waiting_for_certs
) {
1722 "Not enough certificates to check networkstatus consensus");
1724 if (!current_valid_after
||
1725 c
->valid_after
> current_valid_after
) {
1726 waiting
= &consensus_waiting_for_certs
[flav
];
1727 networkstatus_vote_free(waiting
->consensus
);
1728 tor_free(waiting
->body
);
1729 waiting
->consensus
= c
;
1731 waiting
->body
= tor_strdup(consensus
);
1732 waiting
->set_at
= now
;
1733 waiting
->dl_failed
= 0;
1735 write_str_to_file(unverified_fname
, consensus
, 0);
1738 authority_certs_fetch_missing(c
, now
, source_dir
);
1739 /* This case is not a success or a failure until we get the certs
1740 * or fail to get the certs. */
1743 /* Even if we had enough signatures, we'd never use this as the
1744 * latest consensus. */
1745 if (was_waiting_for_certs
&& from_cache
)
1746 if (unlink(unverified_fname
) != 0) {
1748 "Failed to unlink %s: %s",
1749 unverified_fname
, strerror(errno
));
1754 /* This can never be signed enough: Kill it. */
1755 if (!was_waiting_for_certs
) {
1756 log_warn(LD_DIR
, "Not enough good signatures on networkstatus "
1760 if (was_waiting_for_certs
&& (r
< -1) && from_cache
) {
1761 if (unlink(unverified_fname
) != 0) {
1763 "Failed to unlink %s: %s",
1764 unverified_fname
, strerror(errno
));
1771 if (!from_cache
&& flav
== usable_consensus_flavor())
1772 control_event_client_status(LOG_NOTICE
, "CONSENSUS_ARRIVED");
1774 if (!checked_protocols_already
) {
1775 handle_missing_protocol_warning(c
, options
);
1778 /* Are we missing any certificates at all? */
1779 if (r
!= 1 && dl_certs
)
1780 authority_certs_fetch_missing(c
, now
, source_dir
);
1782 if (flav
== usable_consensus_flavor()) {
1783 notify_control_networkstatus_changed(current_consensus
, c
);
1785 if (flav
== FLAV_NS
) {
1786 if (current_ns_consensus
) {
1787 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1788 networkstatus_vote_free(current_ns_consensus
);
1789 /* Defensive programming : we should set current_consensus very soon,
1790 * but we're about to call some stuff in the meantime, and leaving this
1791 * dangling pointer around has proven to be trouble. */
1792 current_ns_consensus
= NULL
;
1794 current_ns_consensus
= c
;
1795 free_consensus
= 0; /* avoid free */
1796 } else if (flav
== FLAV_MICRODESC
) {
1797 if (current_md_consensus
) {
1798 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1799 networkstatus_vote_free(current_md_consensus
);
1800 /* more defensive programming */
1801 current_md_consensus
= NULL
;
1803 current_md_consensus
= c
;
1804 free_consensus
= 0; /* avoid free */
1807 waiting
= &consensus_waiting_for_certs
[flav
];
1808 if (waiting
->consensus
&&
1809 waiting
->consensus
->valid_after
<= c
->valid_after
) {
1810 networkstatus_vote_free(waiting
->consensus
);
1811 waiting
->consensus
= NULL
;
1812 if (consensus
!= waiting
->body
)
1813 tor_free(waiting
->body
);
1815 waiting
->body
= NULL
;
1816 waiting
->set_at
= 0;
1817 waiting
->dl_failed
= 0;
1818 if (unlink(unverified_fname
) != 0) {
1820 "Failed to unlink %s: %s",
1821 unverified_fname
, strerror(errno
));
1825 /* Reset the failure count only if this consensus is actually valid. */
1826 if (c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1827 download_status_reset(&consensus_dl_status
[flav
]);
1830 download_status_failed(&consensus_dl_status
[flav
], 0);
1833 if (flav
== usable_consensus_flavor()) {
1834 /* XXXXNM Microdescs: needs a non-ns variant. ???? NM*/
1835 update_consensus_networkstatus_fetch_time(now
);
1837 nodelist_set_consensus(current_consensus
);
1839 dirvote_recalculate_timing(options
, now
);
1840 routerstatus_list_update_named_server_map();
1842 /* Update ewma and adjust policy if needed; first cache the old value */
1843 old_ewma_enabled
= cell_ewma_enabled();
1844 /* Change the cell EWMA settings */
1845 cell_ewma_set_scale_factor(options
, networkstatus_get_latest_consensus());
1846 /* If we just enabled ewma, set the cmux policy on all active channels */
1847 if (cell_ewma_enabled() && !old_ewma_enabled
) {
1848 channel_set_cmux_policy_everywhere(&ewma_policy
);
1849 } else if (!cell_ewma_enabled() && old_ewma_enabled
) {
1850 /* Turn it off everywhere */
1851 channel_set_cmux_policy_everywhere(NULL
);
1854 /* XXXX this call might be unnecessary here: can changing the
1855 * current consensus really alter our view of any OR's rate limits? */
1856 connection_or_update_token_buckets(get_connection_array(), options
);
1858 circuit_build_times_new_consensus_params(get_circuit_build_times_mutable(),
1862 if (directory_caches_dir_info(options
)) {
1863 dirserv_set_cached_consensus_networkstatus(consensus
,
1870 write_str_to_file(consensus_fname
, consensus
, 0);
1873 /** If a consensus appears more than this many seconds before its declared
1874 * valid-after time, declare that our clock is skewed. */
1875 #define EARLY_CONSENSUS_NOTICE_SKEW 60
1877 if (now
< c
->valid_after
- EARLY_CONSENSUS_NOTICE_SKEW
) {
1878 char tbuf
[ISO_TIME_LEN
+1];
1880 long delta
= now
- c
->valid_after
;
1881 format_iso_time(tbuf
, c
->valid_after
);
1882 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
1883 log_warn(LD_GENERAL
, "Our clock is %s behind the time published in the "
1884 "consensus network status document (%s UTC). Tor needs an "
1885 "accurate clock to work correctly. Please check your time and "
1886 "date settings!", dbuf
, tbuf
);
1887 control_event_general_status(LOG_WARN
,
1888 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta
);
1891 router_dir_info_changed();
1896 networkstatus_vote_free(c
);
1897 tor_free(consensus_fname
);
1898 tor_free(unverified_fname
);
1902 /** Called when we have gotten more certificates: see whether we can
1903 * now verify a pending consensus.
1905 * If source_dir is non-NULL, it's the identity digest for a directory that
1906 * we've just successfully retrieved certificates from, so try it first to
1907 * fetch any missing certificates.
1910 networkstatus_note_certs_arrived(const char *source_dir
)
1913 for (i
=0; i
<N_CONSENSUS_FLAVORS
; ++i
) {
1914 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
1915 if (!waiting
->consensus
)
1917 if (networkstatus_check_consensus_signature(waiting
->consensus
, 0)>=0) {
1918 char *waiting_body
= waiting
->body
;
1919 if (!networkstatus_set_current_consensus(
1921 networkstatus_get_flavor_name(i
),
1922 NSSET_WAS_WAITING_FOR_CERTS
,
1924 tor_free(waiting_body
);
1930 /** If the network-status list has changed since the last time we called this
1931 * function, update the status of every routerinfo from the network-status
1932 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
1933 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
1936 routers_update_all_from_networkstatus(time_t now
, int dir_version
)
1938 routerlist_t
*rl
= router_get_routerlist();
1939 networkstatus_t
*consensus
= networkstatus_get_reasonably_live_consensus(now
,
1942 if (!consensus
|| dir_version
< 3) /* nothing more we should do */
1945 /* calls router_dir_info_changed() when it's done -- more routers
1946 * might be up or down now, which might affect whether there's enough
1947 * directory info. */
1948 routers_update_status_from_consensus_networkstatus(rl
->routers
, 0);
1950 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
,
1951 ri
->cache_info
.routerlist_index
= ri_sl_idx
);
1952 if (rl
->old_routers
)
1953 signed_descs_update_status_from_consensus_networkstatus(rl
->old_routers
);
1955 if (!have_warned_about_old_version
) {
1956 int is_server
= server_mode(get_options());
1957 version_status_t status
;
1958 const char *recommended
= is_server
?
1959 consensus
->server_versions
: consensus
->client_versions
;
1960 status
= tor_version_is_obsolete(VERSION
, recommended
);
1962 if (status
== VS_RECOMMENDED
) {
1963 log_info(LD_GENERAL
, "The directory authorities say my version is ok.");
1964 } else if (status
== VS_EMPTY
) {
1965 log_info(LD_GENERAL
,
1966 "The directory authorities don't recommend any versions.");
1967 } else if (status
== VS_NEW
|| status
== VS_NEW_IN_SERIES
) {
1968 if (!have_warned_about_new_version
) {
1969 log_notice(LD_GENERAL
, "This version of Tor (%s) is newer than any "
1970 "recommended version%s, according to the directory "
1971 "authorities. Recommended versions are: %s",
1973 status
== VS_NEW_IN_SERIES
? " in its series" : "",
1975 have_warned_about_new_version
= 1;
1976 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1977 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1978 VERSION
, "NEW", recommended
);
1981 log_warn(LD_GENERAL
, "Please upgrade! "
1982 "This version of Tor (%s) is %s, according to the directory "
1983 "authorities. Recommended versions are: %s",
1985 status
== VS_OLD
? "obsolete" : "not recommended",
1987 have_warned_about_old_version
= 1;
1988 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1989 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1990 VERSION
, status
== VS_OLD
? "OBSOLETE" : "UNRECOMMENDED",
1996 /** Update our view of the list of named servers from the most recently
1997 * retrieved networkstatus consensus. */
1999 routerstatus_list_update_named_server_map(void)
2001 if (!current_consensus
)
2004 strmap_free(named_server_map
, tor_free_
);
2005 named_server_map
= strmap_new();
2006 strmap_free(unnamed_server_map
, NULL
);
2007 unnamed_server_map
= strmap_new();
2008 SMARTLIST_FOREACH_BEGIN(current_consensus
->routerstatus_list
,
2009 const routerstatus_t
*, rs
) {
2011 strmap_set_lc(named_server_map
, rs
->nickname
,
2012 tor_memdup(rs
->identity_digest
, DIGEST_LEN
));
2014 if (rs
->is_unnamed
) {
2015 strmap_set_lc(unnamed_server_map
, rs
->nickname
, (void*)1);
2017 } SMARTLIST_FOREACH_END(rs
);
2020 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
2021 * according to our current consensus networkstatus. May re-order
2022 * <b>routers</b>. */
2024 routers_update_status_from_consensus_networkstatus(smartlist_t
*routers
,
2027 const or_options_t
*options
= get_options();
2028 int authdir
= authdir_mode_v3(options
);
2029 networkstatus_t
*ns
= current_consensus
;
2030 if (!ns
|| !smartlist_len(ns
->routerstatus_list
))
2033 routers_sort_by_identity(routers
);
2035 SMARTLIST_FOREACH_JOIN(ns
->routerstatus_list
, routerstatus_t
*, rs
,
2036 routers
, routerinfo_t
*, router
,
2037 tor_memcmp(rs
->identity_digest
,
2038 router
->cache_info
.identity_digest
, DIGEST_LEN
),
2041 /* Is it the same descriptor, or only the same identity? */
2042 if (tor_memeq(router
->cache_info
.signed_descriptor_digest
,
2043 rs
->descriptor_digest
, DIGEST_LEN
)) {
2044 if (ns
->valid_until
> router
->cache_info
.last_listed_as_valid_until
)
2045 router
->cache_info
.last_listed_as_valid_until
= ns
->valid_until
;
2049 /* If we _are_ an authority, we should check whether this router
2050 * is one that will cause us to need a reachability test. */
2051 routerinfo_t
*old_router
=
2052 router_get_mutable_by_digest(router
->cache_info
.identity_digest
);
2053 if (old_router
!= router
) {
2054 router
->needs_retest_if_added
=
2055 dirserv_should_launch_reachability_test(router
, old_router
);
2058 if (reset_failures
) {
2059 download_status_reset(&rs
->dl_status
);
2061 } SMARTLIST_FOREACH_JOIN_END(rs
, router
);
2063 router_dir_info_changed();
2066 /** Given a list of signed_descriptor_t, update their fields (mainly, when
2067 * they were last listed) from the most recent consensus. */
2069 signed_descs_update_status_from_consensus_networkstatus(smartlist_t
*descs
)
2071 networkstatus_t
*ns
= current_ns_consensus
;
2075 if (!ns
->desc_digest_map
) {
2076 char dummy
[DIGEST_LEN
];
2077 /* instantiates the digest map. */
2078 memset(dummy
, 0, sizeof(dummy
));
2079 router_get_consensus_status_by_descriptor_digest(ns
, dummy
);
2081 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, d
,
2083 const routerstatus_t
*rs
= digestmap_get(ns
->desc_digest_map
,
2084 d
->signed_descriptor_digest
);
2086 if (ns
->valid_until
> d
->last_listed_as_valid_until
)
2087 d
->last_listed_as_valid_until
= ns
->valid_until
;
2092 /** Generate networkstatus lines for a single routerstatus_t object, and
2093 * return the result in a newly allocated string. Used only by controller
2094 * interface (for now.) */
2096 networkstatus_getinfo_helper_single(const routerstatus_t
*rs
)
2098 return routerstatus_format_entry(rs
, NULL
, NS_CONTROL_PORT
, NULL
);
2101 /** Alloc and return a string describing routerstatuses for the most
2102 * recent info of each router we know about that is of purpose
2103 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
2105 * Right now this function is oriented toward listing bridges (you
2106 * shouldn't use this for general-purpose routers, since those
2107 * should be listed from the consensus, not from the routers list). */
2109 networkstatus_getinfo_by_purpose(const char *purpose_string
, time_t now
)
2111 time_t cutoff
= now
- ROUTER_MAX_AGE_TO_PUBLISH
;
2113 routerlist_t
*rl
= router_get_routerlist();
2114 smartlist_t
*statuses
;
2115 uint8_t purpose
= router_purpose_from_string(purpose_string
);
2117 int bridge_auth
= authdir_mode_bridge(get_options());
2119 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
2120 log_info(LD_DIR
, "Unrecognized purpose '%s' when listing router statuses.",
2125 statuses
= smartlist_new();
2126 SMARTLIST_FOREACH_BEGIN(rl
->routers
, routerinfo_t
*, ri
) {
2127 node_t
*node
= node_get_mutable_by_id(ri
->cache_info
.identity_digest
);
2130 if (ri
->cache_info
.published_on
< cutoff
)
2132 if (ri
->purpose
!= purpose
)
2134 if (bridge_auth
&& ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
2135 dirserv_set_router_is_running(ri
, now
);
2136 /* then generate and write out status lines for each of them */
2137 set_routerstatus_from_routerinfo(&rs
, node
, ri
, now
, 0);
2138 smartlist_add(statuses
, networkstatus_getinfo_helper_single(&rs
));
2139 } SMARTLIST_FOREACH_END(ri
);
2141 answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2142 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2143 smartlist_free(statuses
);
2147 /** Write out router status entries for all our bridge descriptors. */
2149 networkstatus_dump_bridge_status_to_file(time_t now
)
2151 char *status
= networkstatus_getinfo_by_purpose("bridge", now
);
2152 const or_options_t
*options
= get_options();
2154 char *thresholds
= NULL
;
2155 char *published_thresholds_and_status
= NULL
;
2156 char published
[ISO_TIME_LEN
+1];
2158 format_iso_time(published
, now
);
2159 dirserv_compute_bridge_flag_thresholds();
2160 thresholds
= dirserv_get_flag_thresholds_line();
2161 tor_asprintf(&published_thresholds_and_status
,
2162 "published %s\nflag-thresholds %s\n%s",
2163 published
, thresholds
, status
);
2164 tor_asprintf(&fname
, "%s"PATH_SEPARATOR
"networkstatus-bridges",
2165 options
->DataDirectory
);
2166 write_str_to_file(fname
,published_thresholds_and_status
,0);
2167 tor_free(thresholds
);
2168 tor_free(published_thresholds_and_status
);
2173 /* DOCDOC get_net_param_from_list */
2175 get_net_param_from_list(smartlist_t
*net_params
, const char *param_name
,
2176 int32_t default_val
, int32_t min_val
, int32_t max_val
)
2178 int32_t res
= default_val
;
2179 size_t name_len
= strlen(param_name
);
2181 tor_assert(max_val
> min_val
);
2182 tor_assert(min_val
<= default_val
);
2183 tor_assert(max_val
>= default_val
);
2185 SMARTLIST_FOREACH_BEGIN(net_params
, const char *, p
) {
2186 if (!strcmpstart(p
, param_name
) && p
[name_len
] == '=') {
2188 long v
= tor_parse_long(p
+name_len
+1, 10, INT32_MIN
,
2189 INT32_MAX
, &ok
, NULL
);
2195 } SMARTLIST_FOREACH_END(p
);
2197 if (res
< min_val
) {
2198 log_warn(LD_DIR
, "Consensus parameter %s is too small. Got %d, raising to "
2199 "%d.", param_name
, res
, min_val
);
2201 } else if (res
> max_val
) {
2202 log_warn(LD_DIR
, "Consensus parameter %s is too large. Got %d, capping to "
2203 "%d.", param_name
, res
, max_val
);
2210 /** Return the value of a integer parameter from the networkstatus <b>ns</b>
2211 * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
2212 * latest consensus ourselves. Return <b>default_val</b> if no latest
2213 * consensus, or if it has no parameter called <b>param_name</b>.
2214 * Make sure the value parsed from the consensus is at least
2215 * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
2218 networkstatus_get_param(const networkstatus_t
*ns
, const char *param_name
,
2219 int32_t default_val
, int32_t min_val
, int32_t max_val
)
2221 if (!ns
) /* if they pass in null, go find it ourselves */
2222 ns
= networkstatus_get_latest_consensus();
2224 if (!ns
|| !ns
->net_params
)
2227 return get_net_param_from_list(ns
->net_params
, param_name
,
2228 default_val
, min_val
, max_val
);
2232 * Retrieve the consensus parameter that governs the
2233 * fixed-point precision of our network balancing 'bandwidth-weights'
2234 * (which are themselves integer consensus values). We divide them
2235 * by this value and ensure they never exceed this value.
2238 networkstatus_get_weight_scale_param(networkstatus_t
*ns
)
2240 return networkstatus_get_param(ns
, "bwweightscale",
2242 BW_MIN_WEIGHT_SCALE
,
2243 BW_MAX_WEIGHT_SCALE
);
2246 /** Return the value of a integer bw weight parameter from the networkstatus
2247 * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
2248 * loading the latest consensus ourselves. Return <b>default_val</b> if no
2249 * latest consensus, or if it has no parameter called <b>weight_name</b>. */
2251 networkstatus_get_bw_weight(networkstatus_t
*ns
, const char *weight_name
,
2252 int32_t default_val
)
2256 if (!ns
) /* if they pass in null, go find it ourselves */
2257 ns
= networkstatus_get_latest_consensus();
2259 if (!ns
|| !ns
->weight_params
)
2262 max
= networkstatus_get_weight_scale_param(ns
);
2263 param
= get_net_param_from_list(ns
->weight_params
, weight_name
,
2265 BW_MAX_WEIGHT_SCALE
);
2267 log_warn(LD_DIR
, "Value of consensus weight %s was too large, capping "
2268 "to %d", weight_name
, max
);
2274 /** Return the name of the consensus flavor <b>flav</b> as used to identify
2275 * the flavor in directory documents. */
2277 networkstatus_get_flavor_name(consensus_flavor_t flav
)
2282 case FLAV_MICRODESC
:
2285 tor_fragile_assert();
2290 /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
2291 * or -1 if the flavor is not recognized. */
2293 networkstatus_parse_flavor_name(const char *flavname
)
2295 if (!strcmp(flavname
, "ns"))
2297 else if (!strcmp(flavname
, "microdesc"))
2298 return FLAV_MICRODESC
;
2303 /** Return 0 if this routerstatus is obsolete, too new, isn't
2304 * running, or otherwise not a descriptor that we would make any
2305 * use of even if we had it. Else return 1. */
2307 client_would_use_router(const routerstatus_t
*rs
, time_t now
,
2308 const or_options_t
*options
)
2310 if (!rs
->is_flagged_running
&& !options
->FetchUselessDescriptors
) {
2311 /* If we had this router descriptor, we wouldn't even bother using it.
2312 * But, if we want to have a complete list, fetch it anyway. */
2315 if (rs
->published_on
+ options
->TestingEstimatedDescriptorPropagationTime
2317 /* Most caches probably don't have this descriptor yet. */
2320 if (rs
->published_on
+ OLD_ROUTER_DESC_MAX_AGE
< now
) {
2321 /* We'd drop it immediately for being too old. */
2324 if (!routerstatus_version_supports_ntor(rs
, 1)) {
2325 /* We'd ignore it because it doesn't support ntor.
2326 * If we don't know the version, download the descriptor so we can
2327 * check if it supports ntor. */
2333 /** If <b>question</b> is a string beginning with "ns/" in a format the
2334 * control interface expects for a GETINFO question, set *<b>answer</b> to a
2335 * newly-allocated string containing networkstatus lines for the appropriate
2336 * ORs. Return 0 on success, -1 on unrecognized question format. */
2338 getinfo_helper_networkstatus(control_connection_t
*conn
,
2339 const char *question
, char **answer
,
2340 const char **errmsg
)
2342 const routerstatus_t
*status
;
2345 if (!current_consensus
) {
2346 *answer
= tor_strdup("");
2350 if (!strcmp(question
, "ns/all")) {
2351 smartlist_t
*statuses
= smartlist_new();
2352 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
2353 const routerstatus_t
*, rs
,
2355 smartlist_add(statuses
, networkstatus_getinfo_helper_single(rs
));
2357 *answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2358 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2359 smartlist_free(statuses
);
2361 } else if (!strcmpstart(question
, "ns/id/")) {
2363 const char *q
= question
+ 6;
2367 if (base16_decode(d
, DIGEST_LEN
, q
, strlen(q
)) != DIGEST_LEN
) {
2368 *errmsg
= "Data not decodeable as hex";
2371 status
= router_get_consensus_status_by_id(d
);
2372 } else if (!strcmpstart(question
, "ns/name/")) {
2373 status
= router_get_consensus_status_by_nickname(question
+8, 0);
2374 } else if (!strcmpstart(question
, "ns/purpose/")) {
2375 *answer
= networkstatus_getinfo_by_purpose(question
+11, time(NULL
));
2376 return *answer
? 0 : -1;
2377 } else if (!strcmp(question
, "consensus/packages")) {
2378 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2379 if (ns
&& ns
->package_lines
)
2380 *answer
= smartlist_join_strings(ns
->package_lines
, "\n", 0, NULL
);
2382 *errmsg
= "No consensus available";
2383 return *answer
? 0 : -1;
2384 } else if (!strcmp(question
, "consensus/valid-after") ||
2385 !strcmp(question
, "consensus/fresh-until") ||
2386 !strcmp(question
, "consensus/valid-until")) {
2387 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2390 if (!strcmp(question
, "consensus/valid-after"))
2391 t
= ns
->valid_after
;
2392 else if (!strcmp(question
, "consensus/fresh-until"))
2393 t
= ns
->fresh_until
;
2395 t
= ns
->valid_until
;
2397 char tbuf
[ISO_TIME_LEN
+1];
2398 format_iso_time(tbuf
, t
);
2399 *answer
= tor_strdup(tbuf
);
2401 *errmsg
= "No consensus available";
2403 return *answer
? 0 : -1;
2409 *answer
= networkstatus_getinfo_helper_single(status
);
2413 /** Check whether the networkstatus <b>ns</b> lists any protocol
2414 * versions as "required" or "recommended" that we do not support. If
2415 * so, set *<b>warning_out</b> to a newly allocated string describing
2418 * Return 1 if we should exit, 0 if we should not. */
2420 networkstatus_check_required_protocols(const networkstatus_t
*ns
,
2424 const char *func
= client_mode
? "client" : "relay";
2425 const char *required
, *recommended
;
2426 char *missing
= NULL
;
2428 tor_assert(warning_out
);
2431 required
= ns
->required_client_protocols
;
2432 recommended
= ns
->recommended_client_protocols
;
2434 required
= ns
->required_relay_protocols
;
2435 recommended
= ns
->recommended_relay_protocols
;
2438 if (!protover_all_supported(required
, &missing
)) {
2439 tor_asprintf(warning_out
, "At least one protocol listed as required in "
2440 "the consensus is not supported by this version of Tor. "
2441 "You should upgrade. This version of Tor will not work as a "
2442 "%s on the Tor network. The missing protocols are: %s",
2448 if (! protover_all_supported(recommended
, &missing
)) {
2449 tor_asprintf(warning_out
, "At least one protocol listed as recommended in "
2450 "the consensus is not supported by this version of Tor. "
2451 "You should upgrade. This version of Tor will eventually "
2452 "stop working as a %s on the Tor network. The missing "
2453 "protocols are: %s",
2458 tor_assert_nonfatal(missing
== NULL
);
2463 /** Free all storage held locally in this module. */
2465 networkstatus_free_all(void)
2468 networkstatus_vote_free(current_ns_consensus
);
2469 networkstatus_vote_free(current_md_consensus
);
2470 current_md_consensus
= current_ns_consensus
= NULL
;
2472 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2473 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2474 if (waiting
->consensus
) {
2475 networkstatus_vote_free(waiting
->consensus
);
2476 waiting
->consensus
= NULL
;
2478 tor_free(waiting
->body
);
2481 strmap_free(named_server_map
, tor_free_
);
2482 strmap_free(unnamed_server_map
, NULL
);