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"
33 #include "routerlist.h"
34 #include "routerparse.h"
35 #include "shared_random.h"
36 #include "transports.h"
39 /** Map from lowercase nickname to identity digest of named server, if any. */
40 static strmap_t
*named_server_map
= NULL
;
41 /** Map from lowercase nickname to (void*)1 for all names that are listed
42 * as unnamed for some server in the consensus. */
43 static strmap_t
*unnamed_server_map
= NULL
;
45 /** Most recently received and validated v3 "ns"-flavored consensus network
47 static networkstatus_t
*current_ns_consensus
= NULL
;
49 /** Most recently received and validated v3 "microdec"-flavored consensus
51 static networkstatus_t
*current_md_consensus
= NULL
;
53 /** A v3 consensus networkstatus that we've received, but which we don't
54 * have enough certificates to be happy about. */
55 typedef struct consensus_waiting_for_certs_t
{
56 /** The consensus itself. */
57 networkstatus_t
*consensus
;
58 /** The encoded version of the consensus, nul-terminated. */
60 /** When did we set the current value of consensus_waiting_for_certs? If
61 * this is too recent, we shouldn't try to fetch a new consensus for a
62 * little while, to give ourselves time to get certificates for this one. */
64 /** Set to 1 if we've been holding on to it for so long we should maybe
65 * treat it as being bad. */
67 } consensus_waiting_for_certs_t
;
69 /** An array, for each flavor of consensus we might want, of consensuses that
70 * we have downloaded, but which we cannot verify due to having insufficient
71 * authority certificates. */
72 static consensus_waiting_for_certs_t
73 consensus_waiting_for_certs
[N_CONSENSUS_FLAVORS
];
75 /** A time before which we shouldn't try to replace the current consensus:
76 * this will be at some point after the next consensus becomes valid, but
77 * before the current consensus becomes invalid. */
78 static time_t time_to_download_next_consensus
[N_CONSENSUS_FLAVORS
];
79 /** Download status for the current consensus networkstatus. */
80 static download_status_t consensus_dl_status
[N_CONSENSUS_FLAVORS
] =
82 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
83 DL_SCHED_INCREMENT_FAILURE
, DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 },
84 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
85 DL_SCHED_INCREMENT_FAILURE
, DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 },
88 #define N_CONSENSUS_BOOTSTRAP_SCHEDULES 2
89 #define CONSENSUS_BOOTSTRAP_SOURCE_AUTHORITY 0
90 #define CONSENSUS_BOOTSTRAP_SOURCE_ANY_DIRSERVER 1
92 /* Using DL_SCHED_INCREMENT_ATTEMPT on these schedules means that
93 * download_status_increment_failure won't increment these entries.
94 * However, any bootstrap connection failures that occur after we have
95 * a valid consensus will count against the failure counts on the non-bootstrap
96 * schedules. There should only be one of these, as all the others will have
97 * been cancelled. (This doesn't seem to be a significant issue.) */
98 static download_status_t
99 consensus_bootstrap_dl_status
[N_CONSENSUS_BOOTSTRAP_SCHEDULES
] =
101 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_AUTHORITY
,
102 DL_SCHED_INCREMENT_ATTEMPT
, DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 },
103 /* During bootstrap, DL_WANT_ANY_DIRSERVER means "use fallbacks". */
104 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
105 DL_SCHED_INCREMENT_ATTEMPT
, DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 },
108 /** True iff we have logged a warning about this OR's version being older than
109 * listed by the authorities. */
110 static int have_warned_about_old_version
= 0;
111 /** True iff we have logged a warning about this OR's version being newer than
112 * listed by the authorities. */
113 static int have_warned_about_new_version
= 0;
115 static void routerstatus_list_update_named_server_map(void);
116 static void update_consensus_bootstrap_multiple_downloads(
118 const or_options_t
*options
);
120 /** Forget that we've warned about anything networkstatus-related, so we will
121 * give fresh warnings if the same behavior happens again. */
123 networkstatus_reset_warnings(void)
125 if (networkstatus_get_latest_consensus()) {
126 SMARTLIST_FOREACH(nodelist_get_list(), node_t
*, node
,
127 node
->name_lookup_warned
= 0);
130 have_warned_about_old_version
= 0;
131 have_warned_about_new_version
= 0;
134 /** Reset the descriptor download failure count on all networkstatus docs, so
135 * that we can retry any long-failed documents immediately.
138 networkstatus_reset_download_failures(void)
142 log_debug(LD_GENERAL
,
143 "In networkstatus_reset_download_failures()");
145 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
)
146 download_status_reset(&consensus_dl_status
[i
]);
148 for (i
=0; i
< N_CONSENSUS_BOOTSTRAP_SCHEDULES
; ++i
)
149 download_status_reset(&consensus_bootstrap_dl_status
[i
]);
152 /** Read every cached v3 consensus networkstatus from the disk. */
154 router_reload_consensus_networkstatus(void)
158 const unsigned int flags
= NSSET_FROM_CACHE
| NSSET_DONT_DOWNLOAD_CERTS
;
161 /* FFFF Suppress warnings if cached consensus is bad? */
162 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
164 const char *flavor
= networkstatus_get_flavor_name(flav
);
165 if (flav
== FLAV_NS
) {
166 filename
= get_datadir_fname("cached-consensus");
168 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
169 filename
= get_datadir_fname(buf
);
171 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
173 if (networkstatus_set_current_consensus(s
, flavor
, flags
, NULL
) < -1) {
174 log_warn(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
181 if (flav
== FLAV_NS
) {
182 filename
= get_datadir_fname("unverified-consensus");
184 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
185 filename
= get_datadir_fname(buf
);
188 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
190 if (networkstatus_set_current_consensus(s
, flavor
,
191 flags
|NSSET_WAS_WAITING_FOR_CERTS
,
193 log_info(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
201 if (!networkstatus_get_latest_consensus()) {
202 if (!named_server_map
)
203 named_server_map
= strmap_new();
204 if (!unnamed_server_map
)
205 unnamed_server_map
= strmap_new();
208 update_certificate_downloads(time(NULL
));
210 routers_update_all_from_networkstatus(time(NULL
), 3);
211 update_microdescs_from_networkstatus(time(NULL
));
216 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
218 vote_routerstatus_free(vote_routerstatus_t
*rs
)
220 vote_microdesc_hash_t
*h
, *next
;
223 tor_free(rs
->version
);
224 tor_free(rs
->status
.exitsummary
);
225 for (h
= rs
->microdesc
; h
; h
= next
) {
226 tor_free(h
->microdesc_hash_line
);
233 /** Free all storage held by the routerstatus object <b>rs</b>. */
235 routerstatus_free(routerstatus_t
*rs
)
239 tor_free(rs
->exitsummary
);
243 /** Free all storage held in <b>sig</b> */
245 document_signature_free(document_signature_t
*sig
)
247 tor_free(sig
->signature
);
251 /** Return a newly allocated copy of <b>sig</b> */
252 document_signature_t
*
253 document_signature_dup(const document_signature_t
*sig
)
255 document_signature_t
*r
= tor_memdup(sig
, sizeof(document_signature_t
));
257 r
->signature
= tor_memdup(sig
->signature
, sig
->signature_len
);
261 /** Free all storage held in <b>ns</b>. */
263 networkstatus_vote_free(networkstatus_t
*ns
)
268 tor_free(ns
->client_versions
);
269 tor_free(ns
->server_versions
);
270 if (ns
->known_flags
) {
271 SMARTLIST_FOREACH(ns
->known_flags
, char *, c
, tor_free(c
));
272 smartlist_free(ns
->known_flags
);
274 if (ns
->weight_params
) {
275 SMARTLIST_FOREACH(ns
->weight_params
, char *, c
, tor_free(c
));
276 smartlist_free(ns
->weight_params
);
278 if (ns
->net_params
) {
279 SMARTLIST_FOREACH(ns
->net_params
, char *, c
, tor_free(c
));
280 smartlist_free(ns
->net_params
);
282 if (ns
->supported_methods
) {
283 SMARTLIST_FOREACH(ns
->supported_methods
, char *, c
, tor_free(c
));
284 smartlist_free(ns
->supported_methods
);
286 if (ns
->package_lines
) {
287 SMARTLIST_FOREACH(ns
->package_lines
, char *, c
, tor_free(c
));
288 smartlist_free(ns
->package_lines
);
291 SMARTLIST_FOREACH_BEGIN(ns
->voters
, networkstatus_voter_info_t
*, voter
) {
292 tor_free(voter
->nickname
);
293 tor_free(voter
->address
);
294 tor_free(voter
->contact
);
296 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
297 document_signature_free(sig
));
298 smartlist_free(voter
->sigs
);
301 } SMARTLIST_FOREACH_END(voter
);
302 smartlist_free(ns
->voters
);
304 authority_cert_free(ns
->cert
);
306 if (ns
->routerstatus_list
) {
307 if (ns
->type
== NS_TYPE_VOTE
|| ns
->type
== NS_TYPE_OPINION
) {
308 SMARTLIST_FOREACH(ns
->routerstatus_list
, vote_routerstatus_t
*, rs
,
309 vote_routerstatus_free(rs
));
311 SMARTLIST_FOREACH(ns
->routerstatus_list
, routerstatus_t
*, rs
,
312 routerstatus_free(rs
));
315 smartlist_free(ns
->routerstatus_list
);
318 digestmap_free(ns
->desc_digest_map
, NULL
);
320 if (ns
->sr_info
.commits
) {
321 SMARTLIST_FOREACH(ns
->sr_info
.commits
, sr_commit_t
*, c
,
323 smartlist_free(ns
->sr_info
.commits
);
325 tor_free(ns
->sr_info
.previous_srv
);
326 tor_free(ns
->sr_info
.current_srv
);
328 memwipe(ns
, 11, sizeof(*ns
));
332 /** Return the voter info from <b>vote</b> for the voter whose identity digest
333 * is <b>identity</b>, or NULL if no such voter is associated with
335 networkstatus_voter_info_t
*
336 networkstatus_get_voter_by_id(networkstatus_t
*vote
,
337 const char *identity
)
339 if (!vote
|| !vote
->voters
)
341 SMARTLIST_FOREACH(vote
->voters
, networkstatus_voter_info_t
*, voter
,
342 if (fast_memeq(voter
->identity_digest
, identity
, DIGEST_LEN
))
347 /** Check whether the signature <b>sig</b> is correctly signed with the
348 * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
349 * signing key; otherwise set the good_signature or bad_signature flag on
350 * <b>voter</b>, and return 0. */
352 networkstatus_check_document_signature(const networkstatus_t
*consensus
,
353 document_signature_t
*sig
,
354 const authority_cert_t
*cert
)
356 char key_digest
[DIGEST_LEN
];
357 const int dlen
= sig
->alg
== DIGEST_SHA1
? DIGEST_LEN
: DIGEST256_LEN
;
359 size_t signed_digest_len
;
361 if (crypto_pk_get_digest(cert
->signing_key
, key_digest
)<0)
363 if (tor_memneq(sig
->signing_key_digest
, key_digest
, DIGEST_LEN
) ||
364 tor_memneq(sig
->identity_digest
, cert
->cache_info
.identity_digest
,
368 if (authority_cert_is_blacklisted(cert
)) {
369 /* We implement blacklisting for authority signing keys by treating
370 * all their signatures as always bad. That way we don't get into
371 * crazy loops of dropping and re-fetching signatures. */
372 log_warn(LD_DIR
, "Ignoring a consensus signature made with deprecated"
374 hex_str(cert
->signing_key_digest
, DIGEST_LEN
));
375 sig
->bad_signature
= 1;
379 signed_digest_len
= crypto_pk_keysize(cert
->signing_key
);
380 signed_digest
= tor_malloc(signed_digest_len
);
381 if (crypto_pk_public_checksig(cert
->signing_key
,
385 sig
->signature_len
) < dlen
||
386 tor_memneq(signed_digest
, consensus
->digests
.d
[sig
->alg
], dlen
)) {
387 log_warn(LD_DIR
, "Got a bad signature on a networkstatus vote");
388 sig
->bad_signature
= 1;
390 sig
->good_signature
= 1;
392 tor_free(signed_digest
);
396 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
397 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
398 * signature from every recognized authority on it, 0 if there are
399 * enough good signatures from recognized authorities on it, -1 if we might
400 * get enough good signatures by fetching missing certificates, and -2
401 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
402 * about every problem; if warn is at least 1, warn only if we can't get
403 * enough signatures; if warn is negative, log nothing at all. */
405 networkstatus_check_consensus_signature(networkstatus_t
*consensus
,
409 int n_missing_key
= 0, n_dl_failed_key
= 0;
412 int n_no_signature
= 0;
413 int n_v3_authorities
= get_n_authorities(V3_DIRINFO
);
414 int n_required
= n_v3_authorities
/2 + 1;
415 smartlist_t
*list_good
= smartlist_new();
416 smartlist_t
*list_no_signature
= smartlist_new();
417 smartlist_t
*need_certs_from
= smartlist_new();
418 smartlist_t
*unrecognized
= smartlist_new();
419 smartlist_t
*missing_authorities
= smartlist_new();
421 time_t now
= time(NULL
);
423 tor_assert(consensus
->type
== NS_TYPE_CONSENSUS
);
425 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*,
429 int unknown_here
= 0;
430 int missing_key_here
= 0, dl_failed_key_here
= 0;
431 SMARTLIST_FOREACH_BEGIN(voter
->sigs
, document_signature_t
*, sig
) {
432 if (!sig
->good_signature
&& !sig
->bad_signature
&&
434 /* we can try to check the signature. */
435 int is_v3_auth
= trusteddirserver_get_by_v3_auth_digest(
436 sig
->identity_digest
) != NULL
;
437 authority_cert_t
*cert
=
438 authority_cert_get_by_digests(sig
->identity_digest
,
439 sig
->signing_key_digest
);
440 tor_assert(tor_memeq(sig
->identity_digest
, voter
->identity_digest
,
444 smartlist_add(unrecognized
, voter
);
447 } else if (!cert
|| cert
->expires
< now
) {
448 smartlist_add(need_certs_from
, voter
);
450 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
451 ++dl_failed_key_here
;
454 if (networkstatus_check_document_signature(consensus
, sig
, cert
) < 0) {
455 smartlist_add(need_certs_from
, voter
);
457 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
458 ++dl_failed_key_here
;
462 if (sig
->good_signature
)
464 else if (sig
->bad_signature
)
466 } SMARTLIST_FOREACH_END(sig
);
470 smartlist_add(list_good
, voter
->nickname
);
471 } else if (bad_here
) {
473 } else if (missing_key_here
) {
475 if (dl_failed_key_here
)
477 } else if (unknown_here
) {
481 smartlist_add(list_no_signature
, voter
->nickname
);
483 } SMARTLIST_FOREACH_END(voter
);
485 /* Now see whether we're missing any voters entirely. */
486 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
489 if ((ds
->type
& V3_DIRINFO
) &&
490 !networkstatus_get_voter_by_id(consensus
, ds
->v3_identity_digest
))
491 smartlist_add(missing_authorities
, ds
);
494 if (warn
> 1 || (warn
>= 0 &&
495 (n_good
+ n_missing_key
- n_dl_failed_key
< n_required
))) {
502 SMARTLIST_FOREACH(unrecognized
, networkstatus_voter_info_t
*, voter
,
504 tor_log(severity
, LD_DIR
, "Consensus includes unrecognized authority "
505 "'%s' at %s:%d (contact %s; identity %s)",
506 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
507 voter
->contact
?voter
->contact
:"n/a",
508 hex_str(voter
->identity_digest
, DIGEST_LEN
));
510 SMARTLIST_FOREACH(need_certs_from
, networkstatus_voter_info_t
*, voter
,
512 tor_log(severity
, LD_DIR
, "Looks like we need to download a new "
513 "certificate from authority '%s' at %s:%d (contact %s; "
515 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
516 voter
->contact
?voter
->contact
:"n/a",
517 hex_str(voter
->identity_digest
, DIGEST_LEN
));
519 SMARTLIST_FOREACH(missing_authorities
, dir_server_t
*, ds
,
521 tor_log(severity
, LD_DIR
, "Consensus does not include configured "
522 "authority '%s' at %s:%d (identity %s)",
523 ds
->nickname
, ds
->address
, (int)ds
->dir_port
,
524 hex_str(ds
->v3_identity_digest
, DIGEST_LEN
));
528 smartlist_t
*sl
= smartlist_new();
529 char *tmp
= smartlist_join_strings(list_good
, " ", 0, NULL
);
530 smartlist_add_asprintf(sl
,
531 "A consensus needs %d good signatures from recognized "
532 "authorities for us to accept it. This one has %d (%s).",
533 n_required
, n_good
, tmp
);
535 if (n_no_signature
) {
536 tmp
= smartlist_join_strings(list_no_signature
, " ", 0, NULL
);
537 smartlist_add_asprintf(sl
,
538 "%d (%s) of the authorities we know didn't sign it.",
539 n_no_signature
, tmp
);
543 smartlist_add_asprintf(sl
,
544 "It has %d signatures from authorities we don't "
545 "recognize.", n_unknown
);
548 smartlist_add_asprintf(sl
, "%d of the signatures on it didn't verify "
549 "correctly.", n_bad
);
552 smartlist_add_asprintf(sl
,
553 "We were unable to check %d of the signatures, "
554 "because we were missing the keys.", n_missing_key
);
556 joined
= smartlist_join_strings(sl
, " ", 0, NULL
);
557 tor_log(severity
, LD_DIR
, "%s", joined
);
559 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
564 smartlist_free(list_good
);
565 smartlist_free(list_no_signature
);
566 smartlist_free(unrecognized
);
567 smartlist_free(need_certs_from
);
568 smartlist_free(missing_authorities
);
570 if (n_good
== n_v3_authorities
)
572 else if (n_good
>= n_required
)
574 else if (n_good
+ n_missing_key
>= n_required
)
580 /** How far in the future do we allow a network-status to get before removing
582 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
584 /** Helper for bsearching a list of routerstatus_t pointers: compare a
585 * digest in the key to the identity digest of a routerstatus_t. */
587 compare_digest_to_routerstatus_entry(const void *_key
, const void **_member
)
589 const char *key
= _key
;
590 const routerstatus_t
*rs
= *_member
;
591 return tor_memcmp(key
, rs
->identity_digest
, DIGEST_LEN
);
594 /** Helper for bsearching a list of routerstatus_t pointers: compare a
595 * digest in the key to the identity digest of a routerstatus_t. */
597 compare_digest_to_vote_routerstatus_entry(const void *_key
,
598 const void **_member
)
600 const char *key
= _key
;
601 const vote_routerstatus_t
*vrs
= *_member
;
602 return tor_memcmp(key
, vrs
->status
.identity_digest
, DIGEST_LEN
);
605 /** As networkstatus_find_entry, but do not return a const pointer */
607 networkstatus_vote_find_mutable_entry(networkstatus_t
*ns
, const char *digest
)
609 return smartlist_bsearch(ns
->routerstatus_list
, digest
,
610 compare_digest_to_routerstatus_entry
);
613 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
614 * NULL if none was found. */
615 const routerstatus_t
*
616 networkstatus_vote_find_entry(networkstatus_t
*ns
, const char *digest
)
618 return networkstatus_vote_find_mutable_entry(ns
, digest
);
621 /*XXXX MOVE make this static once functions are moved into this file. */
622 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
623 * <b>digest</b>. Return value and set *<b>found_out</b> as for
624 * smartlist_bsearch_idx(). */
626 networkstatus_vote_find_entry_idx(networkstatus_t
*ns
,
627 const char *digest
, int *found_out
)
629 return smartlist_bsearch_idx(ns
->routerstatus_list
, digest
,
630 compare_digest_to_routerstatus_entry
,
634 /** As router_get_consensus_status_by_descriptor_digest, but does not return
635 * a const pointer. */
636 MOCK_IMPL(routerstatus_t
*,
637 router_get_mutable_consensus_status_by_descriptor_digest
,(
638 networkstatus_t
*consensus
,
642 consensus
= networkstatus_get_latest_consensus();
645 if (!consensus
->desc_digest_map
) {
646 digestmap_t
*m
= consensus
->desc_digest_map
= digestmap_new();
647 SMARTLIST_FOREACH(consensus
->routerstatus_list
,
648 routerstatus_t
*, rs
,
650 digestmap_set(m
, rs
->descriptor_digest
, rs
);
653 return digestmap_get(consensus
->desc_digest_map
, digest
);
656 /** Return the consensus view of the status of the router whose current
657 * <i>descriptor</i> digest in <b>consensus</b> is <b>digest</b>, or NULL if
658 * no such router is known. */
659 const routerstatus_t
*
660 router_get_consensus_status_by_descriptor_digest(networkstatus_t
*consensus
,
663 return router_get_mutable_consensus_status_by_descriptor_digest(
667 /** Return a smartlist of all router descriptor digests in a consensus */
669 router_get_descriptor_digests_in_consensus(networkstatus_t
*consensus
)
671 smartlist_t
*result
= smartlist_new();
677 for (i
= digestmap_iter_init(consensus
->desc_digest_map
);
678 !(digestmap_iter_done(i
));
679 i
= digestmap_iter_next(consensus
->desc_digest_map
, i
)) {
680 digestmap_iter_get(i
, &digest
, &rs
);
681 digest_tmp
= tor_malloc(DIGEST_LEN
);
682 memcpy(digest_tmp
, digest
, DIGEST_LEN
);
683 smartlist_add(result
, digest_tmp
);
689 /** Return a smartlist of all router descriptor digests in the current
691 MOCK_IMPL(smartlist_t
*,
692 router_get_descriptor_digests
,(void))
694 smartlist_t
*result
= NULL
;
696 if (current_ns_consensus
) {
698 router_get_descriptor_digests_in_consensus(current_ns_consensus
);
704 /** Given the digest of a router descriptor, return its current download
705 * status, or NULL if the digest is unrecognized. */
706 MOCK_IMPL(download_status_t
*,
707 router_get_dl_status_by_descriptor_digest
,(const char *d
))
710 if (!current_ns_consensus
)
712 if ((rs
= router_get_mutable_consensus_status_by_descriptor_digest(
713 current_ns_consensus
, d
)))
714 return &rs
->dl_status
;
719 /** As router_get_consensus_status_by_id, but do not return a const pointer */
721 router_get_mutable_consensus_status_by_id(const char *digest
)
723 if (!networkstatus_get_latest_consensus())
725 const smartlist_t
*rslist
;
726 rslist
= networkstatus_get_latest_consensus()->routerstatus_list
;
727 return smartlist_bsearch(rslist
, digest
,
728 compare_digest_to_routerstatus_entry
);
731 /** Return the consensus view of the status of the router whose identity
732 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
733 const routerstatus_t
*
734 router_get_consensus_status_by_id(const char *digest
)
736 return router_get_mutable_consensus_status_by_id(digest
);
739 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
740 * the corresponding routerstatus_t, or NULL if none exists. Warn the
741 * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
742 * nickname, but the Named flag isn't set for that router. */
743 const routerstatus_t
*
744 router_get_consensus_status_by_nickname(const char *nickname
,
747 const node_t
*node
= node_get_by_nickname(nickname
, warn_if_unnamed
);
754 /** Return the identity digest that's mapped to officially by
755 * <b>nickname</b>. */
757 networkstatus_get_router_digest_by_nickname(const char *nickname
)
759 if (!named_server_map
)
761 return strmap_get_lc(named_server_map
, nickname
);
764 /** Return true iff <b>nickname</b> is disallowed from being the nickname
767 networkstatus_nickname_is_unnamed(const char *nickname
)
769 if (!unnamed_server_map
)
771 return strmap_get_lc(unnamed_server_map
, nickname
) != NULL
;
774 /** How frequently do directory authorities re-download fresh networkstatus
776 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
778 /** How frequently do non-authority directory caches re-download fresh
779 * networkstatus documents? */
780 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
782 /** Return true iff, given the options listed in <b>options</b>, <b>flavor</b>
783 * is the flavor of a consensus networkstatus that we would like to fetch. */
785 we_want_to_fetch_flavor(const or_options_t
*options
, int flavor
)
787 if (flavor
< 0 || flavor
> N_CONSENSUS_FLAVORS
) {
788 /* This flavor is crazy; we don't want it */
789 /*XXXX handle unrecognized flavors later */
792 if (authdir_mode_v3(options
) || directory_caches_dir_info(options
)) {
793 /* We want to serve all flavors to others, regardless if we would use
797 if (options
->FetchUselessDescriptors
) {
798 /* In order to get all descriptors, we need to fetch all consensuses. */
801 /* Otherwise, we want the flavor only if we want to use it to build
803 return flavor
== usable_consensus_flavor();
806 /** How long will we hang onto a possibly live consensus for which we're
807 * fetching certs before we check whether there is a better one? */
808 #define DELAY_WHILE_FETCHING_CERTS (20*60)
810 /* Check if a downloaded consensus flavor should still wait for certificates
812 * If so, return 1. If not, fail dls and return 0. */
814 check_consensus_waiting_for_certs(int flavor
, time_t now
,
815 download_status_t
*dls
)
817 consensus_waiting_for_certs_t
*waiting
;
819 /* We should always have a known flavor, because we_want_to_fetch_flavor()
820 * filters out unknown flavors. */
821 tor_assert(flavor
>= 0 && flavor
< N_CONSENSUS_FLAVORS
);
823 waiting
= &consensus_waiting_for_certs
[flavor
];
824 if (waiting
->consensus
) {
825 /* XXXX make sure this doesn't delay sane downloads. */
826 if (waiting
->set_at
+ DELAY_WHILE_FETCHING_CERTS
> now
) {
829 if (!waiting
->dl_failed
) {
830 download_status_failed(dls
, 0);
831 waiting
->dl_failed
=1;
839 /** If we want to download a fresh consensus, launch a new download as
842 update_consensus_networkstatus_downloads(time_t now
)
845 const or_options_t
*options
= get_options();
846 const int we_are_bootstrapping
= networkstatus_consensus_is_bootstrapping(
848 const int use_multi_conn
=
849 networkstatus_consensus_can_use_multiple_directories(options
);
851 if (should_delay_dir_fetches(options
, NULL
))
854 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
855 /* XXXX need some way to download unknown flavors if we are caching. */
856 const char *resource
;
858 int max_in_progress_conns
= 1;
860 if (! we_want_to_fetch_flavor(options
, i
))
863 c
= networkstatus_get_latest_consensus_by_flavor(i
);
864 if (! (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
)) {
865 /* No live consensus? Get one now!*/
866 time_to_download_next_consensus
[i
] = now
;
869 if (time_to_download_next_consensus
[i
] > now
)
870 continue; /* Wait until the current consensus is older. */
872 resource
= networkstatus_get_flavor_name(i
);
874 /* Check if we already have enough connections in progress */
875 if (we_are_bootstrapping
) {
876 max_in_progress_conns
=
877 options
->ClientBootstrapConsensusMaxInProgressTries
;
879 if (connection_dir_count_by_purpose_and_resource(
880 DIR_PURPOSE_FETCH_CONSENSUS
,
882 >= max_in_progress_conns
) {
886 /* Check if we want to launch another download for a usable consensus.
887 * Only used during bootstrap. */
888 if (we_are_bootstrapping
&& use_multi_conn
889 && i
== usable_consensus_flavor()) {
891 /* Check if we're already downloading a usable consensus */
892 if (networkstatus_consensus_is_already_downloading(resource
))
895 /* Make multiple connections for a bootstrap consensus download. */
896 update_consensus_bootstrap_multiple_downloads(now
, options
);
898 /* Check if we failed downloading a consensus too recently */
899 int max_dl_tries
= options
->TestingConsensusMaxDownloadTries
;
901 /* Let's make sure we remembered to update consensus_dl_status */
902 tor_assert(consensus_dl_status
[i
].schedule
== DL_SCHED_CONSENSUS
);
904 if (!download_status_is_ready(&consensus_dl_status
[i
],
910 /* Check if we're waiting for certificates to download */
911 if (check_consensus_waiting_for_certs(i
, now
, &consensus_dl_status
[i
]))
914 /* Try the requested attempt */
915 log_info(LD_DIR
, "Launching %s standard networkstatus consensus "
916 "download.", networkstatus_get_flavor_name(i
));
917 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
918 ROUTER_PURPOSE_GENERAL
, resource
,
919 PDS_RETRY_IF_NO_SERVERS
,
920 consensus_dl_status
[i
].want_authority
);
925 /** When we're bootstrapping, launch one or more consensus download
926 * connections, if schedule indicates connection(s) should be made after now.
927 * If is_authority, connect to an authority, otherwise, use a fallback
931 update_consensus_bootstrap_attempt_downloads(
933 const or_options_t
*options
,
934 download_status_t
*dls
,
935 download_want_authority_t want_authority
)
937 int use_fallbacks
= networkstatus_consensus_can_use_extra_fallbacks(options
);
938 int max_dl_tries
= options
->ClientBootstrapConsensusMaxDownloadTries
;
939 if (!use_fallbacks
) {
941 options
->ClientBootstrapConsensusAuthorityOnlyMaxDownloadTries
;
944 const char *resource
= networkstatus_get_flavor_name(
945 usable_consensus_flavor());
947 /* Let's make sure we remembered to update schedule */
948 tor_assert(dls
->schedule
== DL_SCHED_CONSENSUS
);
950 /* Allow for multiple connections in the same second, if the schedule value
952 while (download_status_is_ready(dls
, now
, max_dl_tries
)) {
953 log_info(LD_DIR
, "Launching %s bootstrap %s networkstatus consensus "
954 "download.", resource
, (want_authority
== DL_WANT_AUTHORITY
958 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
959 ROUTER_PURPOSE_GENERAL
, resource
,
960 PDS_RETRY_IF_NO_SERVERS
, want_authority
);
961 /* schedule the next attempt */
962 download_status_increment_attempt(dls
, resource
, now
);
966 /** If we're bootstrapping, check the connection schedules and see if we want
967 * to make additional, potentially concurrent, consensus download
969 * Only call when bootstrapping, and when we want to make additional
970 * connections. Only nodes that satisfy
971 * networkstatus_consensus_can_use_multiple_directories make additional
975 update_consensus_bootstrap_multiple_downloads(time_t now
,
976 const or_options_t
*options
)
978 const int usable_flavor
= usable_consensus_flavor();
980 /* make sure we can use multiple connections */
981 if (!networkstatus_consensus_can_use_multiple_directories(options
)) {
985 /* Launch concurrent consensus download attempt(s) based on the mirror and
986 * authority schedules. Try the mirror first - this makes it slightly more
987 * likely that we'll connect to the fallback first, and then end the
988 * authority connection attempt. */
990 /* If a consensus download fails because it's waiting for certificates,
991 * we'll fail both the authority and fallback schedules. This is better than
992 * failing only one of the schedules, and having the other continue
996 /* If we don't have or can't use extra fallbacks, don't try them. */
997 if (networkstatus_consensus_can_use_extra_fallbacks(options
)) {
998 download_status_t
*dls_f
=
999 &consensus_bootstrap_dl_status
[CONSENSUS_BOOTSTRAP_SOURCE_ANY_DIRSERVER
];
1001 if (!check_consensus_waiting_for_certs(usable_flavor
, now
, dls_f
)) {
1002 /* During bootstrap, DL_WANT_ANY_DIRSERVER means "use fallbacks". */
1003 update_consensus_bootstrap_attempt_downloads(now
, options
, dls_f
,
1004 DL_WANT_ANY_DIRSERVER
);
1008 /* Now try an authority. */
1009 download_status_t
*dls_a
=
1010 &consensus_bootstrap_dl_status
[CONSENSUS_BOOTSTRAP_SOURCE_AUTHORITY
];
1012 if (!check_consensus_waiting_for_certs(usable_flavor
, now
, dls_a
)) {
1013 update_consensus_bootstrap_attempt_downloads(now
, options
, dls_a
,
1018 /** Called when an attempt to download a consensus fails: note that the
1019 * failure occurred, and possibly retry. */
1021 networkstatus_consensus_download_failed(int status_code
, const char *flavname
)
1023 int flav
= networkstatus_parse_flavor_name(flavname
);
1025 tor_assert(flav
< N_CONSENSUS_FLAVORS
);
1026 /* XXXX handle unrecognized flavors */
1027 download_status_failed(&consensus_dl_status
[flav
], status_code
);
1028 /* Retry immediately, if appropriate. */
1029 update_consensus_networkstatus_downloads(time(NULL
));
1033 /** How long do we (as a cache) wait after a consensus becomes non-fresh
1034 * before trying to fetch another? */
1035 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
1037 /** Update the time at which we'll consider replacing the current
1038 * consensus of flavor <b>flav</b> */
1040 update_consensus_networkstatus_fetch_time_impl(time_t now
, int flav
)
1042 const or_options_t
*options
= get_options();
1043 networkstatus_t
*c
= networkstatus_get_latest_consensus_by_flavor(flav
);
1044 const char *flavor
= networkstatus_get_flavor_name(flav
);
1045 if (! we_want_to_fetch_flavor(get_options(), flav
))
1048 if (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1050 long interval
= c
->fresh_until
- c
->valid_after
;
1051 long min_sec_before_caching
= CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1054 if (min_sec_before_caching
> interval
/16) {
1055 /* Usually we allow 2-minutes slop factor in case clocks get
1056 desynchronized a little. If we're on a private network with
1057 a crazy-fast voting interval, though, 2 minutes may be too
1059 min_sec_before_caching
= interval
/16;
1060 /* make sure we always delay by at least a second before caching */
1061 if (min_sec_before_caching
== 0) {
1062 min_sec_before_caching
= 1;
1066 if (directory_fetches_dir_info_early(options
)) {
1067 /* We want to cache the next one at some point after this one
1068 * is no longer fresh... */
1069 start
= (time_t)(c
->fresh_until
+ min_sec_before_caching
);
1070 /* Some clients may need the consensus sooner than others. */
1071 if (options
->FetchDirInfoExtraEarly
|| authdir_mode_v3(options
)) {
1073 if (min_sec_before_caching
+ dl_interval
> interval
)
1074 dl_interval
= interval
/2;
1076 /* But only in the first half-interval after that. */
1077 dl_interval
= interval
/2;
1080 /* We're an ordinary client, a bridge, or a hidden service.
1081 * Give all the caches enough time to download the consensus. */
1082 start
= (time_t)(c
->fresh_until
+ (interval
*3)/4);
1083 /* But download the next one well before this one is expired. */
1084 dl_interval
= ((c
->valid_until
- start
) * 7 )/ 8;
1086 /* If we're a bridge user, make use of the numbers we just computed
1087 * to choose the rest of the interval *after* them. */
1088 if (directory_fetches_dir_info_later(options
)) {
1089 /* Give all the *clients* enough time to download the consensus. */
1090 start
= (time_t)(start
+ dl_interval
+ min_sec_before_caching
);
1091 /* But try to get it before ours actually expires. */
1092 dl_interval
= (c
->valid_until
- start
) - min_sec_before_caching
;
1095 /* catch low dl_interval in crazy-fast networks */
1096 if (dl_interval
< 1)
1098 /* catch late start in crazy-fast networks */
1099 if (start
+dl_interval
>= c
->valid_until
)
1100 start
= c
->valid_until
- dl_interval
- 1;
1102 "fresh_until: %ld start: %ld "
1103 "dl_interval: %ld valid_until: %ld ",
1104 (long)c
->fresh_until
, (long)start
, dl_interval
,
1105 (long)c
->valid_until
);
1106 /* We must not try to replace c while it's still fresh: */
1107 tor_assert(c
->fresh_until
< start
);
1108 /* We must download the next one before c is invalid: */
1109 tor_assert(start
+dl_interval
< c
->valid_until
);
1110 time_to_download_next_consensus
[flav
] =
1111 start
+ crypto_rand_int((int)dl_interval
);
1113 char tbuf1
[ISO_TIME_LEN
+1];
1114 char tbuf2
[ISO_TIME_LEN
+1];
1115 char tbuf3
[ISO_TIME_LEN
+1];
1116 format_local_iso_time(tbuf1
, c
->fresh_until
);
1117 format_local_iso_time(tbuf2
, c
->valid_until
);
1118 format_local_iso_time(tbuf3
, time_to_download_next_consensus
[flav
]);
1119 log_info(LD_DIR
, "Live %s consensus %s the most recent until %s and "
1120 "will expire at %s; fetching the next one at %s.",
1121 flavor
, (c
->fresh_until
> now
) ? "will be" : "was",
1122 tbuf1
, tbuf2
, tbuf3
);
1125 time_to_download_next_consensus
[flav
] = now
;
1126 log_info(LD_DIR
, "No live %s consensus; we should fetch one immediately.",
1131 /** Update the time at which we'll consider replacing the current
1132 * consensus of flavor 'flavor' */
1134 update_consensus_networkstatus_fetch_time(time_t now
)
1137 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1138 if (we_want_to_fetch_flavor(get_options(), i
))
1139 update_consensus_networkstatus_fetch_time_impl(now
, i
);
1143 /** Return 1 if there's a reason we shouldn't try any directory
1144 * fetches yet (e.g. we demand bridges and none are yet known).
1147 * If we return 1 and <b>msg_out</b> is provided, set <b>msg_out</b>
1148 * to an explanation of why directory fetches are delayed. (If we
1149 * return 0, we set msg_out to NULL.)
1152 should_delay_dir_fetches(const or_options_t
*options
, const char **msg_out
)
1158 if (options
->DisableNetwork
) {
1160 *msg_out
= "DisableNetwork is set.";
1162 log_info(LD_DIR
, "Delaying dir fetches (DisableNetwork is set)");
1166 if (options
->UseBridges
) {
1167 if (!any_bridge_descriptors_known()) {
1169 *msg_out
= "No running bridges";
1171 log_info(LD_DIR
, "Delaying dir fetches (no running bridges known)");
1175 if (pt_proxies_configuration_pending()) {
1177 *msg_out
= "Pluggable transport proxies still configuring";
1179 log_info(LD_DIR
, "Delaying dir fetches (pt proxies still configuring)");
1187 /** Launch requests for networkstatus documents and authority certificates as
1190 update_networkstatus_downloads(time_t now
)
1192 const or_options_t
*options
= get_options();
1193 if (should_delay_dir_fetches(options
, NULL
))
1195 update_consensus_networkstatus_downloads(now
);
1196 update_certificate_downloads(now
);
1199 /** Launch requests as appropriate for missing directory authority
1202 update_certificate_downloads(time_t now
)
1205 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1206 if (consensus_waiting_for_certs
[i
].consensus
)
1207 authority_certs_fetch_missing(consensus_waiting_for_certs
[i
].consensus
,
1211 if (current_ns_consensus
)
1212 authority_certs_fetch_missing(current_ns_consensus
, now
, NULL
);
1213 if (current_md_consensus
)
1214 authority_certs_fetch_missing(current_md_consensus
, now
, NULL
);
1217 /** Return 1 if we have a consensus but we don't have enough certificates
1218 * to start using it yet. */
1220 consensus_is_waiting_for_certs(void)
1222 return consensus_waiting_for_certs
[usable_consensus_flavor()].consensus
1226 /** Look up the currently active (depending on bootstrap status) download
1227 * status for this consensus flavor and return a pointer to it.
1229 MOCK_IMPL(download_status_t
*,
1230 networkstatus_get_dl_status_by_flavor
,(consensus_flavor_t flavor
))
1232 download_status_t
*dl
= NULL
;
1233 const int we_are_bootstrapping
=
1234 networkstatus_consensus_is_bootstrapping(time(NULL
));
1236 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1237 dl
= &((we_are_bootstrapping
?
1238 consensus_bootstrap_dl_status
: consensus_dl_status
)[flavor
]);
1244 /** Look up the bootstrap download status for this consensus flavor
1245 * and return a pointer to it. */
1246 MOCK_IMPL(download_status_t
*,
1247 networkstatus_get_dl_status_by_flavor_bootstrap
,(consensus_flavor_t flavor
))
1249 download_status_t
*dl
= NULL
;
1251 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1252 dl
= &(consensus_bootstrap_dl_status
[flavor
]);
1258 /** Look up the running (non-bootstrap) download status for this consensus
1259 * flavor and return a pointer to it. */
1260 MOCK_IMPL(download_status_t
*,
1261 networkstatus_get_dl_status_by_flavor_running
,(consensus_flavor_t flavor
))
1263 download_status_t
*dl
= NULL
;
1265 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1266 dl
= &(consensus_dl_status
[flavor
]);
1272 /** Return the most recent consensus that we have downloaded, or NULL if we
1273 * don't have one. */
1274 MOCK_IMPL(networkstatus_t
*,
1275 networkstatus_get_latest_consensus
,(void))
1277 if (we_use_microdescriptors_for_circuits(get_options()))
1278 return current_md_consensus
;
1280 return current_ns_consensus
;
1283 /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
1284 * if we don't have one. */
1285 MOCK_IMPL(networkstatus_t
*,
1286 networkstatus_get_latest_consensus_by_flavor
,(consensus_flavor_t f
))
1289 return current_ns_consensus
;
1290 else if (f
== FLAV_MICRODESC
)
1291 return current_md_consensus
;
1298 /** Return the most recent consensus that we have downloaded, or NULL if it is
1299 * no longer live. */
1300 MOCK_IMPL(networkstatus_t
*,
1301 networkstatus_get_live_consensus
,(time_t now
))
1303 if (networkstatus_get_latest_consensus() &&
1304 networkstatus_get_latest_consensus()->valid_after
<= now
&&
1305 now
<= networkstatus_get_latest_consensus()->valid_until
)
1306 return networkstatus_get_latest_consensus();
1311 /* XXXX remove this in favor of get_live_consensus. But actually,
1312 * leave something like it for bridge users, who need to not totally
1313 * lose if they spend a while fetching a new consensus. */
1314 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1317 networkstatus_get_reasonably_live_consensus(time_t now
, int flavor
)
1319 #define REASONABLY_LIVE_TIME (24*60*60)
1320 networkstatus_t
*consensus
=
1321 networkstatus_get_latest_consensus_by_flavor(flavor
);
1323 consensus
->valid_after
<= now
&&
1324 now
<= consensus
->valid_until
+REASONABLY_LIVE_TIME
)
1330 /** Check if we need to download a consensus during tor's bootstrap phase.
1331 * If we have no consensus, or our consensus is unusably old, return 1.
1332 * As soon as we have received a consensus, return 0, even if we don't have
1333 * enough certificates to validate it.
1334 * If a fallback directory gives us a consensus we can never get certs for,
1335 * check_consensus_waiting_for_certs() will wait 20 minutes before failing
1336 * the cert downloads. After that, a new consensus will be fetched from a
1337 * randomly chosen fallback. */
1339 networkstatus_consensus_is_bootstrapping
,(time_t now
))
1341 /* If we have a validated, reasonably live consensus, we're not
1342 * bootstrapping a consensus at all. */
1343 if (networkstatus_get_reasonably_live_consensus(
1345 usable_consensus_flavor())) {
1349 /* If we have a consensus, but we're waiting for certificates,
1350 * we're not waiting for a consensus download while bootstrapping. */
1351 if (consensus_is_waiting_for_certs()) {
1355 /* If we have no consensus, or our consensus is very old, we are
1356 * bootstrapping, and we need to download a consensus. */
1360 /** Check if we can use multiple directories for a consensus download.
1361 * Only clients (including bridge relays, which act like clients) benefit
1362 * from multiple simultaneous consensus downloads. */
1364 networkstatus_consensus_can_use_multiple_directories(
1365 const or_options_t
*options
)
1367 /* If we are a client, bridge, bridge client, or hidden service */
1368 return !public_server_mode(options
);
1371 /** Check if we can use fallback directory mirrors for a consensus download.
1372 * If we have fallbacks and don't want to fetch from the authorities,
1373 * we can use them. */
1375 networkstatus_consensus_can_use_extra_fallbacks
,(const or_options_t
*options
))
1377 /* The list length comparisons are a quick way to check if we have any
1378 * non-authority fallback directories. If we ever have any authorities that
1379 * aren't fallback directories, we will need to change this code. */
1380 tor_assert(smartlist_len(router_get_fallback_dir_servers())
1381 >= smartlist_len(router_get_trusted_dir_servers()));
1382 /* If we don't fetch from the authorities, and we have additional mirrors,
1383 * we can use them. */
1384 return (!directory_fetches_from_authorities(options
)
1385 && (smartlist_len(router_get_fallback_dir_servers())
1386 > smartlist_len(router_get_trusted_dir_servers())));
1389 /* Is there a consensus fetch for flavor <b>resource</b> that's far
1390 * enough along to be attached to a circuit? */
1392 networkstatus_consensus_is_already_downloading(const char *resource
)
1396 /* First, get a list of all the dir conns that are fetching a consensus,
1397 * fetching *this* consensus, and are in state "reading" (meaning they
1398 * have already flushed their request onto the socks connection). */
1399 smartlist_t
*fetching_conns
=
1400 connection_dir_list_by_purpose_resource_and_state(
1401 DIR_PURPOSE_FETCH_CONSENSUS
, resource
, DIR_CONN_STATE_CLIENT_READING
);
1403 /* Then, walk through each conn, to see if its linked socks connection
1404 * is in an attached state. We have to check this separately, since with
1405 * the optimistic data feature, fetches can send their request to the
1406 * socks connection and go into state 'reading', even before they're
1407 * attached to any circuit. */
1408 SMARTLIST_FOREACH_BEGIN(fetching_conns
, dir_connection_t
*, dirconn
) {
1409 /* Do any of these other dir conns have a linked socks conn that is
1410 * attached to a circuit already? */
1411 connection_t
*base
= TO_CONN(dirconn
);
1412 if (base
->linked_conn
&&
1413 base
->linked_conn
->type
== CONN_TYPE_AP
&&
1414 !AP_CONN_STATE_IS_UNATTACHED(base
->linked_conn
->state
)) {
1416 break; /* stop looping, because we know the answer will be yes */
1418 } SMARTLIST_FOREACH_END(dirconn
);
1419 smartlist_free(fetching_conns
);
1424 /** Given two router status entries for the same router identity, return 1 if
1425 * if the contents have changed between them. Otherwise, return 0. */
1427 routerstatus_has_changed(const routerstatus_t
*a
, const routerstatus_t
*b
)
1429 tor_assert(tor_memeq(a
->identity_digest
, b
->identity_digest
, DIGEST_LEN
));
1431 return strcmp(a
->nickname
, b
->nickname
) ||
1432 fast_memneq(a
->descriptor_digest
, b
->descriptor_digest
, DIGEST_LEN
) ||
1433 a
->addr
!= b
->addr
||
1434 a
->or_port
!= b
->or_port
||
1435 a
->dir_port
!= b
->dir_port
||
1436 a
->is_authority
!= b
->is_authority
||
1437 a
->is_exit
!= b
->is_exit
||
1438 a
->is_stable
!= b
->is_stable
||
1439 a
->is_fast
!= b
->is_fast
||
1440 a
->is_flagged_running
!= b
->is_flagged_running
||
1441 a
->is_named
!= b
->is_named
||
1442 a
->is_unnamed
!= b
->is_unnamed
||
1443 a
->is_valid
!= b
->is_valid
||
1444 a
->is_possible_guard
!= b
->is_possible_guard
||
1445 a
->is_bad_exit
!= b
->is_bad_exit
||
1446 a
->is_hs_dir
!= b
->is_hs_dir
||
1447 a
->version_known
!= b
->version_known
;
1450 /** Notify controllers of any router status entries that changed between
1451 * <b>old_c</b> and <b>new_c</b>. */
1453 notify_control_networkstatus_changed(const networkstatus_t
*old_c
,
1454 const networkstatus_t
*new_c
)
1456 smartlist_t
*changed
;
1460 /* tell the controller exactly which relays are still listed, as well
1461 * as what they're listed as */
1462 control_event_newconsensus(new_c
);
1464 if (!control_event_is_interesting(EVENT_NS
))
1468 control_event_networkstatus_changed(new_c
->routerstatus_list
);
1471 changed
= smartlist_new();
1473 SMARTLIST_FOREACH_JOIN(
1474 old_c
->routerstatus_list
, const routerstatus_t
*, rs_old
,
1475 new_c
->routerstatus_list
, const routerstatus_t
*, rs_new
,
1476 tor_memcmp(rs_old
->identity_digest
,
1477 rs_new
->identity_digest
, DIGEST_LEN
),
1478 smartlist_add(changed
, (void*) rs_new
)) {
1479 if (routerstatus_has_changed(rs_old
, rs_new
))
1480 smartlist_add(changed
, (void*)rs_new
);
1481 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1483 control_event_networkstatus_changed(changed
);
1484 smartlist_free(changed
);
1487 /** Copy all the ancillary information (like router download status and so on)
1488 * from <b>old_c</b> to <b>new_c</b>. */
1490 networkstatus_copy_old_consensus_info(networkstatus_t
*new_c
,
1491 const networkstatus_t
*old_c
)
1495 if (!old_c
|| !smartlist_len(old_c
->routerstatus_list
))
1498 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1499 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1500 tor_memcmp(rs_old
->identity_digest
,
1501 rs_new
->identity_digest
, DIGEST_LEN
),
1503 /* Okay, so we're looking at the same identity. */
1504 rs_new
->last_dir_503_at
= rs_old
->last_dir_503_at
;
1506 if (tor_memeq(rs_old
->descriptor_digest
, rs_new
->descriptor_digest
,
1508 /* And the same descriptor too! */
1509 memcpy(&rs_new
->dl_status
, &rs_old
->dl_status
,sizeof(download_status_t
));
1511 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1514 #ifdef TOR_UNIT_TESTS
1515 /**Accept a <b>flavor</b> consensus <b>c</b> without any additional
1516 * validation. This is exclusively for unit tests.
1517 * We copy any ancillary information from a pre-existing consensus
1518 * and then free the current one and replace it with the newly
1519 * provided instance. Returns -1 on unrecognized flavor, 0 otherwise.
1522 networkstatus_set_current_consensus_from_ns(networkstatus_t
*c
,
1525 int flav
= networkstatus_parse_flavor_name(flavor
);
1528 if (current_ns_consensus
) {
1529 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1530 networkstatus_vote_free(current_ns_consensus
);
1532 current_ns_consensus
= c
;
1534 case FLAV_MICRODESC
:
1535 if (current_md_consensus
) {
1536 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1537 networkstatus_vote_free(current_md_consensus
);
1539 current_md_consensus
= c
;
1542 return current_md_consensus
? 0 : -1;
1544 #endif //TOR_UNIT_TESTS
1546 /** Try to replace the current cached v3 networkstatus with the one in
1547 * <b>consensus</b>. If we don't have enough certificates to validate it,
1548 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1550 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1551 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1552 * already received, but we were waiting for certificates on it. If flags &
1553 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1554 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1555 * consensus, even if it comes from many days in the past.
1557 * If source_dir is non-NULL, it's the identity digest for a directory that
1558 * we've just successfully retrieved a consensus or certificates from, so try
1559 * it first to fetch any missing certificates.
1561 * Return 0 on success, <0 on failure. On failure, caller should increment
1562 * the failure count as appropriate.
1564 * We return -1 for mild failures that don't need to be reported to the
1565 * user, and -2 for more serious problems.
1568 networkstatus_set_current_consensus(const char *consensus
,
1571 const char *source_dir
)
1573 networkstatus_t
*c
=NULL
;
1575 time_t now
= time(NULL
);
1576 const or_options_t
*options
= get_options();
1577 char *unverified_fname
= NULL
, *consensus_fname
= NULL
;
1578 int flav
= networkstatus_parse_flavor_name(flavor
);
1579 const unsigned from_cache
= flags
& NSSET_FROM_CACHE
;
1580 const unsigned was_waiting_for_certs
= flags
& NSSET_WAS_WAITING_FOR_CERTS
;
1581 const unsigned dl_certs
= !(flags
& NSSET_DONT_DOWNLOAD_CERTS
);
1582 const unsigned accept_obsolete
= flags
& NSSET_ACCEPT_OBSOLETE
;
1583 const unsigned require_flavor
= flags
& NSSET_REQUIRE_FLAVOR
;
1584 const common_digests_t
*current_digests
= NULL
;
1585 consensus_waiting_for_certs_t
*waiting
= NULL
;
1586 time_t current_valid_after
= 0;
1587 int free_consensus
= 1; /* Free 'c' at the end of the function */
1588 int old_ewma_enabled
;
1591 /* XXXX we don't handle unrecognized flavors yet. */
1592 log_warn(LD_BUG
, "Unrecognized consensus flavor %s", flavor
);
1596 /* Make sure it's parseable. */
1597 c
= networkstatus_parse_vote_from_string(consensus
, NULL
, NS_TYPE_CONSENSUS
);
1599 log_warn(LD_DIR
, "Unable to parse networkstatus consensus");
1604 if ((int)c
->flavor
!= flav
) {
1605 /* This wasn't the flavor we thought we were getting. */
1606 if (require_flavor
) {
1607 log_warn(LD_DIR
, "Got consensus with unexpected flavor %s (wanted %s)",
1608 networkstatus_get_flavor_name(c
->flavor
), flavor
);
1612 flavor
= networkstatus_get_flavor_name(flav
);
1615 if (flav
!= usable_consensus_flavor() &&
1616 !directory_caches_dir_info(options
)) {
1617 /* This consensus is totally boring to us: we won't use it, and we won't
1618 * serve it. Drop it. */
1622 if (from_cache
&& !accept_obsolete
&&
1623 c
->valid_until
< now
-OLD_ROUTER_DESC_MAX_AGE
) {
1624 log_info(LD_DIR
, "Loaded an expired consensus. Discarding.");
1628 if (!strcmp(flavor
, "ns")) {
1629 consensus_fname
= get_datadir_fname("cached-consensus");
1630 unverified_fname
= get_datadir_fname("unverified-consensus");
1631 if (current_ns_consensus
) {
1632 current_digests
= ¤t_ns_consensus
->digests
;
1633 current_valid_after
= current_ns_consensus
->valid_after
;
1635 } else if (!strcmp(flavor
, "microdesc")) {
1636 consensus_fname
= get_datadir_fname("cached-microdesc-consensus");
1637 unverified_fname
= get_datadir_fname("unverified-microdesc-consensus");
1638 if (current_md_consensus
) {
1639 current_digests
= ¤t_md_consensus
->digests
;
1640 current_valid_after
= current_md_consensus
->valid_after
;
1645 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
1646 consensus_fname
= get_datadir_fname(buf
);
1647 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
1648 unverified_fname
= get_datadir_fname(buf
);
1649 cur
= dirserv_get_consensus(flavor
);
1651 current_digests
= &cur
->digests
;
1652 current_valid_after
= cur
->published
;
1656 if (current_digests
&&
1657 tor_memeq(&c
->digests
, current_digests
, sizeof(c
->digests
))) {
1658 /* We already have this one. That's a failure. */
1659 log_info(LD_DIR
, "Got a %s consensus we already have", flavor
);
1663 if (current_valid_after
&& c
->valid_after
<= current_valid_after
) {
1664 /* We have a newer one. There's no point in accepting this one,
1665 * even if it's great. */
1666 log_info(LD_DIR
, "Got a %s consensus at least as old as the one we have",
1671 /* Make sure it's signed enough. */
1672 if ((r
=networkstatus_check_consensus_signature(c
, 1))<0) {
1674 /* Okay, so it _might_ be signed enough if we get more certificates. */
1675 if (!was_waiting_for_certs
) {
1677 "Not enough certificates to check networkstatus consensus");
1679 if (!current_valid_after
||
1680 c
->valid_after
> current_valid_after
) {
1681 waiting
= &consensus_waiting_for_certs
[flav
];
1682 networkstatus_vote_free(waiting
->consensus
);
1683 tor_free(waiting
->body
);
1684 waiting
->consensus
= c
;
1686 waiting
->body
= tor_strdup(consensus
);
1687 waiting
->set_at
= now
;
1688 waiting
->dl_failed
= 0;
1690 write_str_to_file(unverified_fname
, consensus
, 0);
1693 authority_certs_fetch_missing(c
, now
, source_dir
);
1694 /* This case is not a success or a failure until we get the certs
1695 * or fail to get the certs. */
1698 /* Even if we had enough signatures, we'd never use this as the
1699 * latest consensus. */
1700 if (was_waiting_for_certs
&& from_cache
)
1701 if (unlink(unverified_fname
) != 0) {
1703 "Failed to unlink %s: %s",
1704 unverified_fname
, strerror(errno
));
1709 /* This can never be signed enough: Kill it. */
1710 if (!was_waiting_for_certs
) {
1711 log_warn(LD_DIR
, "Not enough good signatures on networkstatus "
1715 if (was_waiting_for_certs
&& (r
< -1) && from_cache
) {
1716 if (unlink(unverified_fname
) != 0) {
1718 "Failed to unlink %s: %s",
1719 unverified_fname
, strerror(errno
));
1726 if (!from_cache
&& flav
== usable_consensus_flavor())
1727 control_event_client_status(LOG_NOTICE
, "CONSENSUS_ARRIVED");
1729 /* Are we missing any certificates at all? */
1730 if (r
!= 1 && dl_certs
)
1731 authority_certs_fetch_missing(c
, now
, source_dir
);
1733 const int is_usable_flavor
= flav
== usable_consensus_flavor();
1735 if (is_usable_flavor
) {
1736 notify_control_networkstatus_changed(
1737 networkstatus_get_latest_consensus(), c
);
1739 if (flav
== FLAV_NS
) {
1740 if (current_ns_consensus
) {
1741 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1742 networkstatus_vote_free(current_ns_consensus
);
1743 /* Defensive programming : we should set current_ns_consensus very soon
1744 * but we're about to call some stuff in the meantime, and leaving this
1745 * dangling pointer around has proven to be trouble. */
1746 current_ns_consensus
= NULL
;
1748 current_ns_consensus
= c
;
1749 free_consensus
= 0; /* avoid free */
1750 } else if (flav
== FLAV_MICRODESC
) {
1751 if (current_md_consensus
) {
1752 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1753 networkstatus_vote_free(current_md_consensus
);
1754 /* more defensive programming */
1755 current_md_consensus
= NULL
;
1757 current_md_consensus
= c
;
1758 free_consensus
= 0; /* avoid free */
1761 waiting
= &consensus_waiting_for_certs
[flav
];
1762 if (waiting
->consensus
&&
1763 waiting
->consensus
->valid_after
<= c
->valid_after
) {
1764 networkstatus_vote_free(waiting
->consensus
);
1765 waiting
->consensus
= NULL
;
1766 if (consensus
!= waiting
->body
)
1767 tor_free(waiting
->body
);
1769 waiting
->body
= NULL
;
1770 waiting
->set_at
= 0;
1771 waiting
->dl_failed
= 0;
1772 if (unlink(unverified_fname
) != 0) {
1774 "Failed to unlink %s: %s",
1775 unverified_fname
, strerror(errno
));
1779 if (is_usable_flavor
) {
1780 nodelist_set_consensus(c
);
1782 /* XXXXNM Microdescs: needs a non-ns variant. ???? NM*/
1783 update_consensus_networkstatus_fetch_time(now
);
1785 dirvote_recalculate_timing(options
, now
);
1786 routerstatus_list_update_named_server_map();
1788 /* Update ewma and adjust policy if needed; first cache the old value */
1789 old_ewma_enabled
= cell_ewma_enabled();
1790 /* Change the cell EWMA settings */
1791 cell_ewma_set_scale_factor(options
, networkstatus_get_latest_consensus());
1792 /* If we just enabled ewma, set the cmux policy on all active channels */
1793 if (cell_ewma_enabled() && !old_ewma_enabled
) {
1794 channel_set_cmux_policy_everywhere(&ewma_policy
);
1795 } else if (!cell_ewma_enabled() && old_ewma_enabled
) {
1796 /* Turn it off everywhere */
1797 channel_set_cmux_policy_everywhere(NULL
);
1800 /* XXXX this call might be unnecessary here: can changing the
1801 * current consensus really alter our view of any OR's rate limits? */
1802 connection_or_update_token_buckets(get_connection_array(), options
);
1804 circuit_build_times_new_consensus_params(get_circuit_build_times_mutable(),
1805 networkstatus_get_latest_consensus());
1808 /* Reset the failure count only if this consensus is actually valid. */
1809 if (c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1810 download_status_reset(&consensus_dl_status
[flav
]);
1813 download_status_failed(&consensus_dl_status
[flav
], 0);
1816 if (directory_caches_dir_info(options
)) {
1817 dirserv_set_cached_consensus_networkstatus(consensus
,
1824 write_str_to_file(consensus_fname
, consensus
, 0);
1827 /** If a consensus appears more than this many seconds before its declared
1828 * valid-after time, declare that our clock is skewed. */
1829 #define EARLY_CONSENSUS_NOTICE_SKEW 60
1831 if (now
< c
->valid_after
- EARLY_CONSENSUS_NOTICE_SKEW
) {
1832 char tbuf
[ISO_TIME_LEN
+1];
1834 long delta
= now
- c
->valid_after
;
1835 format_iso_time(tbuf
, c
->valid_after
);
1836 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
1837 log_warn(LD_GENERAL
, "Our clock is %s behind the time published in the "
1838 "consensus network status document (%s UTC). Tor needs an "
1839 "accurate clock to work correctly. Please check your time and "
1840 "date settings!", dbuf
, tbuf
);
1841 control_event_general_status(LOG_WARN
,
1842 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta
);
1845 router_dir_info_changed();
1850 networkstatus_vote_free(c
);
1851 tor_free(consensus_fname
);
1852 tor_free(unverified_fname
);
1856 /** Called when we have gotten more certificates: see whether we can
1857 * now verify a pending consensus.
1859 * If source_dir is non-NULL, it's the identity digest for a directory that
1860 * we've just successfully retrieved certificates from, so try it first to
1861 * fetch any missing certificates.
1864 networkstatus_note_certs_arrived(const char *source_dir
)
1867 for (i
=0; i
<N_CONSENSUS_FLAVORS
; ++i
) {
1868 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
1869 if (!waiting
->consensus
)
1871 if (networkstatus_check_consensus_signature(waiting
->consensus
, 0)>=0) {
1872 char *waiting_body
= waiting
->body
;
1873 if (!networkstatus_set_current_consensus(
1875 networkstatus_get_flavor_name(i
),
1876 NSSET_WAS_WAITING_FOR_CERTS
,
1878 tor_free(waiting_body
);
1884 /** If the network-status list has changed since the last time we called this
1885 * function, update the status of every routerinfo from the network-status
1886 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
1887 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
1890 routers_update_all_from_networkstatus(time_t now
, int dir_version
)
1892 routerlist_t
*rl
= router_get_routerlist();
1893 networkstatus_t
*consensus
= networkstatus_get_reasonably_live_consensus(now
,
1896 if (!consensus
|| dir_version
< 3) /* nothing more we should do */
1899 /* calls router_dir_info_changed() when it's done -- more routers
1900 * might be up or down now, which might affect whether there's enough
1901 * directory info. */
1902 routers_update_status_from_consensus_networkstatus(rl
->routers
, 0);
1904 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
,
1905 ri
->cache_info
.routerlist_index
= ri_sl_idx
);
1906 if (rl
->old_routers
)
1907 signed_descs_update_status_from_consensus_networkstatus(rl
->old_routers
);
1909 if (!have_warned_about_old_version
) {
1910 int is_server
= server_mode(get_options());
1911 version_status_t status
;
1912 const char *recommended
= is_server
?
1913 consensus
->server_versions
: consensus
->client_versions
;
1914 status
= tor_version_is_obsolete(VERSION
, recommended
);
1916 if (status
== VS_RECOMMENDED
) {
1917 log_info(LD_GENERAL
, "The directory authorities say my version is ok.");
1918 } else if (status
== VS_EMPTY
) {
1919 log_info(LD_GENERAL
,
1920 "The directory authorities don't recommend any versions.");
1921 } else if (status
== VS_NEW
|| status
== VS_NEW_IN_SERIES
) {
1922 if (!have_warned_about_new_version
) {
1923 log_notice(LD_GENERAL
, "This version of Tor (%s) is newer than any "
1924 "recommended version%s, according to the directory "
1925 "authorities. Recommended versions are: %s",
1927 status
== VS_NEW_IN_SERIES
? " in its series" : "",
1929 have_warned_about_new_version
= 1;
1930 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1931 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1932 VERSION
, "NEW", recommended
);
1935 log_warn(LD_GENERAL
, "Please upgrade! "
1936 "This version of Tor (%s) is %s, according to the directory "
1937 "authorities. Recommended versions are: %s",
1939 status
== VS_OLD
? "obsolete" : "not recommended",
1941 have_warned_about_old_version
= 1;
1942 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1943 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1944 VERSION
, status
== VS_OLD
? "OBSOLETE" : "UNRECOMMENDED",
1950 /** Update our view of the list of named servers from the most recently
1951 * retrieved networkstatus consensus. */
1953 routerstatus_list_update_named_server_map(void)
1955 if (!networkstatus_get_latest_consensus())
1958 strmap_free(named_server_map
, tor_free_
);
1959 named_server_map
= strmap_new();
1960 strmap_free(unnamed_server_map
, NULL
);
1961 unnamed_server_map
= strmap_new();
1962 const smartlist_t
*rslist
;
1963 rslist
= networkstatus_get_latest_consensus()->routerstatus_list
;
1964 SMARTLIST_FOREACH_BEGIN(rslist
,
1965 const routerstatus_t
*, rs
) {
1967 strmap_set_lc(named_server_map
, rs
->nickname
,
1968 tor_memdup(rs
->identity_digest
, DIGEST_LEN
));
1970 if (rs
->is_unnamed
) {
1971 strmap_set_lc(unnamed_server_map
, rs
->nickname
, (void*)1);
1973 } SMARTLIST_FOREACH_END(rs
);
1976 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
1977 * according to our current consensus networkstatus. May re-order
1978 * <b>routers</b>. */
1980 routers_update_status_from_consensus_networkstatus(smartlist_t
*routers
,
1983 const or_options_t
*options
= get_options();
1984 int authdir
= authdir_mode_v3(options
);
1985 networkstatus_t
*ns
= networkstatus_get_latest_consensus();
1986 if (!ns
|| !smartlist_len(ns
->routerstatus_list
))
1989 routers_sort_by_identity(routers
);
1991 SMARTLIST_FOREACH_JOIN(ns
->routerstatus_list
, routerstatus_t
*, rs
,
1992 routers
, routerinfo_t
*, router
,
1993 tor_memcmp(rs
->identity_digest
,
1994 router
->cache_info
.identity_digest
, DIGEST_LEN
),
1997 /* Is it the same descriptor, or only the same identity? */
1998 if (tor_memeq(router
->cache_info
.signed_descriptor_digest
,
1999 rs
->descriptor_digest
, DIGEST_LEN
)) {
2000 if (ns
->valid_until
> router
->cache_info
.last_listed_as_valid_until
)
2001 router
->cache_info
.last_listed_as_valid_until
= ns
->valid_until
;
2005 /* If we _are_ an authority, we should check whether this router
2006 * is one that will cause us to need a reachability test. */
2007 routerinfo_t
*old_router
=
2008 router_get_mutable_by_digest(router
->cache_info
.identity_digest
);
2009 if (old_router
!= router
) {
2010 router
->needs_retest_if_added
=
2011 dirserv_should_launch_reachability_test(router
, old_router
);
2014 if (reset_failures
) {
2015 download_status_reset(&rs
->dl_status
);
2017 } SMARTLIST_FOREACH_JOIN_END(rs
, router
);
2019 router_dir_info_changed();
2022 /** Given a list of signed_descriptor_t, update their fields (mainly, when
2023 * they were last listed) from the most recent consensus. */
2025 signed_descs_update_status_from_consensus_networkstatus(smartlist_t
*descs
)
2027 networkstatus_t
*ns
= current_ns_consensus
;
2031 if (!ns
->desc_digest_map
) {
2032 char dummy
[DIGEST_LEN
];
2033 /* instantiates the digest map. */
2034 memset(dummy
, 0, sizeof(dummy
));
2035 router_get_consensus_status_by_descriptor_digest(ns
, dummy
);
2037 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, d
,
2039 const routerstatus_t
*rs
= digestmap_get(ns
->desc_digest_map
,
2040 d
->signed_descriptor_digest
);
2042 if (ns
->valid_until
> d
->last_listed_as_valid_until
)
2043 d
->last_listed_as_valid_until
= ns
->valid_until
;
2048 /** Generate networkstatus lines for a single routerstatus_t object, and
2049 * return the result in a newly allocated string. Used only by controller
2050 * interface (for now.) */
2052 networkstatus_getinfo_helper_single(const routerstatus_t
*rs
)
2054 return routerstatus_format_entry(rs
, NULL
, NS_CONTROL_PORT
, NULL
);
2057 /** Alloc and return a string describing routerstatuses for the most
2058 * recent info of each router we know about that is of purpose
2059 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
2061 * Right now this function is oriented toward listing bridges (you
2062 * shouldn't use this for general-purpose routers, since those
2063 * should be listed from the consensus, not from the routers list). */
2065 networkstatus_getinfo_by_purpose(const char *purpose_string
, time_t now
)
2067 time_t cutoff
= now
- ROUTER_MAX_AGE_TO_PUBLISH
;
2069 routerlist_t
*rl
= router_get_routerlist();
2070 smartlist_t
*statuses
;
2071 uint8_t purpose
= router_purpose_from_string(purpose_string
);
2073 int bridge_auth
= authdir_mode_bridge(get_options());
2075 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
2076 log_info(LD_DIR
, "Unrecognized purpose '%s' when listing router statuses.",
2081 statuses
= smartlist_new();
2082 SMARTLIST_FOREACH_BEGIN(rl
->routers
, routerinfo_t
*, ri
) {
2083 node_t
*node
= node_get_mutable_by_id(ri
->cache_info
.identity_digest
);
2086 if (ri
->cache_info
.published_on
< cutoff
)
2088 if (ri
->purpose
!= purpose
)
2090 if (bridge_auth
&& ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
2091 dirserv_set_router_is_running(ri
, now
);
2092 /* then generate and write out status lines for each of them */
2093 set_routerstatus_from_routerinfo(&rs
, node
, ri
, now
, 0);
2094 smartlist_add(statuses
, networkstatus_getinfo_helper_single(&rs
));
2095 } SMARTLIST_FOREACH_END(ri
);
2097 answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2098 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2099 smartlist_free(statuses
);
2103 /** Write out router status entries for all our bridge descriptors. */
2105 networkstatus_dump_bridge_status_to_file(time_t now
)
2107 char *status
= networkstatus_getinfo_by_purpose("bridge", now
);
2108 const or_options_t
*options
= get_options();
2110 char *thresholds
= NULL
;
2111 char *published_thresholds_and_status
= NULL
;
2112 char published
[ISO_TIME_LEN
+1];
2114 format_iso_time(published
, now
);
2115 dirserv_compute_bridge_flag_thresholds();
2116 thresholds
= dirserv_get_flag_thresholds_line();
2117 tor_asprintf(&published_thresholds_and_status
,
2118 "published %s\nflag-thresholds %s\n%s",
2119 published
, thresholds
, status
);
2120 tor_asprintf(&fname
, "%s"PATH_SEPARATOR
"networkstatus-bridges",
2121 options
->DataDirectory
);
2122 write_str_to_file(fname
,published_thresholds_and_status
,0);
2123 tor_free(thresholds
);
2124 tor_free(published_thresholds_and_status
);
2129 /* DOCDOC get_net_param_from_list */
2131 get_net_param_from_list(smartlist_t
*net_params
, const char *param_name
,
2132 int32_t default_val
, int32_t min_val
, int32_t max_val
)
2134 int32_t res
= default_val
;
2135 size_t name_len
= strlen(param_name
);
2137 tor_assert(max_val
> min_val
);
2138 tor_assert(min_val
<= default_val
);
2139 tor_assert(max_val
>= default_val
);
2141 SMARTLIST_FOREACH_BEGIN(net_params
, const char *, p
) {
2142 if (!strcmpstart(p
, param_name
) && p
[name_len
] == '=') {
2144 long v
= tor_parse_long(p
+name_len
+1, 10, INT32_MIN
,
2145 INT32_MAX
, &ok
, NULL
);
2151 } SMARTLIST_FOREACH_END(p
);
2153 if (res
< min_val
) {
2154 log_warn(LD_DIR
, "Consensus parameter %s is too small. Got %d, raising to "
2155 "%d.", param_name
, res
, min_val
);
2157 } else if (res
> max_val
) {
2158 log_warn(LD_DIR
, "Consensus parameter %s is too large. Got %d, capping to "
2159 "%d.", param_name
, res
, max_val
);
2166 /** Return the value of a integer parameter from the networkstatus <b>ns</b>
2167 * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
2168 * latest consensus ourselves. Return <b>default_val</b> if no latest
2169 * consensus, or if it has no parameter called <b>param_name</b>.
2170 * Make sure the value parsed from the consensus is at least
2171 * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
2174 networkstatus_get_param(const networkstatus_t
*ns
, const char *param_name
,
2175 int32_t default_val
, int32_t min_val
, int32_t max_val
)
2177 if (!ns
) /* if they pass in null, go find it ourselves */
2178 ns
= networkstatus_get_latest_consensus();
2180 if (!ns
|| !ns
->net_params
)
2183 return get_net_param_from_list(ns
->net_params
, param_name
,
2184 default_val
, min_val
, max_val
);
2188 * Retrieve the consensus parameter that governs the
2189 * fixed-point precision of our network balancing 'bandwidth-weights'
2190 * (which are themselves integer consensus values). We divide them
2191 * by this value and ensure they never exceed this value.
2194 networkstatus_get_weight_scale_param(networkstatus_t
*ns
)
2196 return networkstatus_get_param(ns
, "bwweightscale",
2198 BW_MIN_WEIGHT_SCALE
,
2199 BW_MAX_WEIGHT_SCALE
);
2202 /** Return the value of a integer bw weight parameter from the networkstatus
2203 * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
2204 * loading the latest consensus ourselves. Return <b>default_val</b> if no
2205 * latest consensus, or if it has no parameter called <b>weight_name</b>. */
2207 networkstatus_get_bw_weight(networkstatus_t
*ns
, const char *weight_name
,
2208 int32_t default_val
)
2212 if (!ns
) /* if they pass in null, go find it ourselves */
2213 ns
= networkstatus_get_latest_consensus();
2215 if (!ns
|| !ns
->weight_params
)
2218 max
= networkstatus_get_weight_scale_param(ns
);
2219 param
= get_net_param_from_list(ns
->weight_params
, weight_name
,
2221 BW_MAX_WEIGHT_SCALE
);
2223 log_warn(LD_DIR
, "Value of consensus weight %s was too large, capping "
2224 "to %d", weight_name
, max
);
2230 /** Return the name of the consensus flavor <b>flav</b> as used to identify
2231 * the flavor in directory documents. */
2233 networkstatus_get_flavor_name(consensus_flavor_t flav
)
2238 case FLAV_MICRODESC
:
2241 tor_fragile_assert();
2246 /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
2247 * or -1 if the flavor is not recognized. */
2249 networkstatus_parse_flavor_name(const char *flavname
)
2251 if (!strcmp(flavname
, "ns"))
2253 else if (!strcmp(flavname
, "microdesc"))
2254 return FLAV_MICRODESC
;
2259 /** Return 0 if this routerstatus is obsolete, too new, isn't
2260 * running, or otherwise not a descriptor that we would make any
2261 * use of even if we had it. Else return 1. */
2263 client_would_use_router(const routerstatus_t
*rs
, time_t now
,
2264 const or_options_t
*options
)
2266 if (!rs
->is_flagged_running
&& !options
->FetchUselessDescriptors
) {
2267 /* If we had this router descriptor, we wouldn't even bother using it.
2268 * But, if we want to have a complete list, fetch it anyway. */
2271 if (rs
->published_on
+ options
->TestingEstimatedDescriptorPropagationTime
2273 /* Most caches probably don't have this descriptor yet. */
2276 if (rs
->published_on
+ OLD_ROUTER_DESC_MAX_AGE
< now
) {
2277 /* We'd drop it immediately for being too old. */
2280 if (!routerstatus_version_supports_ntor(rs
, 1)) {
2281 /* We'd ignore it because it doesn't support ntor.
2282 * If we don't know the version, download the descriptor so we can
2283 * check if it supports ntor. */
2289 /** If <b>question</b> is a string beginning with "ns/" in a format the
2290 * control interface expects for a GETINFO question, set *<b>answer</b> to a
2291 * newly-allocated string containing networkstatus lines for the appropriate
2292 * ORs. Return 0 on success, -1 on unrecognized question format. */
2294 getinfo_helper_networkstatus(control_connection_t
*conn
,
2295 const char *question
, char **answer
,
2296 const char **errmsg
)
2298 const routerstatus_t
*status
;
2301 if (!networkstatus_get_latest_consensus()) {
2302 *answer
= tor_strdup("");
2306 if (!strcmp(question
, "ns/all")) {
2307 smartlist_t
*statuses
= smartlist_new();
2308 SMARTLIST_FOREACH(networkstatus_get_latest_consensus()->routerstatus_list
,
2309 const routerstatus_t
*, rs
,
2311 smartlist_add(statuses
, networkstatus_getinfo_helper_single(rs
));
2313 *answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2314 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2315 smartlist_free(statuses
);
2317 } else if (!strcmpstart(question
, "ns/id/")) {
2319 const char *q
= question
+ 6;
2323 if (base16_decode(d
, DIGEST_LEN
, q
, strlen(q
)) != DIGEST_LEN
) {
2324 *errmsg
= "Data not decodeable as hex";
2327 status
= router_get_consensus_status_by_id(d
);
2328 } else if (!strcmpstart(question
, "ns/name/")) {
2329 status
= router_get_consensus_status_by_nickname(question
+8, 0);
2330 } else if (!strcmpstart(question
, "ns/purpose/")) {
2331 *answer
= networkstatus_getinfo_by_purpose(question
+11, time(NULL
));
2332 return *answer
? 0 : -1;
2333 } else if (!strcmp(question
, "consensus/packages")) {
2334 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2335 if (ns
&& ns
->package_lines
)
2336 *answer
= smartlist_join_strings(ns
->package_lines
, "\n", 0, NULL
);
2338 *errmsg
= "No consensus available";
2339 return *answer
? 0 : -1;
2340 } else if (!strcmp(question
, "consensus/valid-after") ||
2341 !strcmp(question
, "consensus/fresh-until") ||
2342 !strcmp(question
, "consensus/valid-until")) {
2343 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2346 if (!strcmp(question
, "consensus/valid-after"))
2347 t
= ns
->valid_after
;
2348 else if (!strcmp(question
, "consensus/fresh-until"))
2349 t
= ns
->fresh_until
;
2351 t
= ns
->valid_until
;
2353 char tbuf
[ISO_TIME_LEN
+1];
2354 format_iso_time(tbuf
, t
);
2355 *answer
= tor_strdup(tbuf
);
2357 *errmsg
= "No consensus available";
2359 return *answer
? 0 : -1;
2365 *answer
= networkstatus_getinfo_helper_single(status
);
2369 /** Free all storage held locally in this module. */
2371 networkstatus_free_all(void)
2374 networkstatus_vote_free(current_ns_consensus
);
2375 networkstatus_vote_free(current_md_consensus
);
2376 current_md_consensus
= current_ns_consensus
= NULL
;
2378 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2379 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2380 if (waiting
->consensus
) {
2381 networkstatus_vote_free(waiting
->consensus
);
2382 waiting
->consensus
= NULL
;
2384 tor_free(waiting
->body
);
2387 strmap_free(named_server_map
, tor_free_
);
2388 strmap_free(unnamed_server_map
, NULL
);