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-2012, 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
14 #include "circuitbuild.h"
16 #include "connection.h"
17 #include "connection_or.h"
19 #include "directory.h"
23 #include "microdesc.h"
24 #include "networkstatus.h"
28 #include "routerlist.h"
29 #include "routerparse.h"
31 /* For tracking v2 networkstatus documents. Only caches do this now. */
33 /** Map from descriptor digest of routers listed in the v2 networkstatus
34 * documents to download_status_t* */
35 static digestmap_t
*v2_download_status_map
= NULL
;
36 /** Global list of all of the current v2 network_status documents that we know
37 * about. This list is kept sorted by published_on. */
38 static smartlist_t
*networkstatus_v2_list
= NULL
;
39 /** True iff any member of networkstatus_v2_list has changed since the last
40 * time we called download_status_map_update_from_v2_networkstatus() */
41 static int networkstatus_v2_list_has_changed
= 0;
43 /** Map from lowercase nickname to identity digest of named server, if any. */
44 static strmap_t
*named_server_map
= NULL
;
45 /** Map from lowercase nickname to (void*)1 for all names that are listed
46 * as unnamed for some server in the consensus. */
47 static strmap_t
*unnamed_server_map
= NULL
;
49 /** Most recently received and validated v3 consensus network status,
50 * of whichever type we are using for our own circuits. This will be the same
51 * as one of current_ns_consensus or current_md_consensus.
53 #define current_consensus \
54 (we_use_microdescriptors_for_circuits(get_options()) ? \
55 current_md_consensus : current_ns_consensus)
57 /** Most recently received and validated v3 "ns"-flavored consensus network
59 static networkstatus_t
*current_ns_consensus
= NULL
;
61 /** Most recently received and validated v3 "microdec"-flavored consensus
63 static networkstatus_t
*current_md_consensus
= NULL
;
65 /** A v3 consensus networkstatus that we've received, but which we don't
66 * have enough certificates to be happy about. */
67 typedef struct consensus_waiting_for_certs_t
{
68 /** The consensus itself. */
69 networkstatus_t
*consensus
;
70 /** The encoded version of the consensus, nul-terminated. */
72 /** When did we set the current value of consensus_waiting_for_certs? If
73 * this is too recent, we shouldn't try to fetch a new consensus for a
74 * little while, to give ourselves time to get certificates for this one. */
76 /** Set to 1 if we've been holding on to it for so long we should maybe
77 * treat it as being bad. */
79 } consensus_waiting_for_certs_t
;
81 /** An array, for each flavor of consensus we might want, of consensuses that
82 * we have downloaded, but which we cannot verify due to having insufficient
83 * authority certificates. */
84 static consensus_waiting_for_certs_t
85 consensus_waiting_for_certs
[N_CONSENSUS_FLAVORS
];
87 /** The last time we tried to download a networkstatus, or 0 for "never". We
88 * use this to rate-limit download attempts for directory caches (including
89 * mirrors). Clients don't use this now. */
90 static time_t last_networkstatus_download_attempted
= 0;
92 /** A time before which we shouldn't try to replace the current consensus:
93 * this will be at some point after the next consensus becomes valid, but
94 * before the current consensus becomes invalid. */
95 static time_t time_to_download_next_consensus
[N_CONSENSUS_FLAVORS
];
96 /** Download status for the current consensus networkstatus. */
97 static download_status_t consensus_dl_status
[N_CONSENSUS_FLAVORS
];
99 /** True iff we have logged a warning about this OR's version being older than
100 * listed by the authorities. */
101 static int have_warned_about_old_version
= 0;
102 /** True iff we have logged a warning about this OR's version being newer than
103 * listed by the authorities. */
104 static int have_warned_about_new_version
= 0;
106 static void download_status_map_update_from_v2_networkstatus(void);
107 static void routerstatus_list_update_named_server_map(void);
109 /** Forget that we've warned about anything networkstatus-related, so we will
110 * give fresh warnings if the same behavior happens again. */
112 networkstatus_reset_warnings(void)
114 if (current_consensus
) {
115 SMARTLIST_FOREACH(nodelist_get_list(), node_t
*, node
,
116 node
->name_lookup_warned
= 0);
119 have_warned_about_old_version
= 0;
120 have_warned_about_new_version
= 0;
123 /** Reset the descriptor download failure count on all networkstatus docs, so
124 * that we can retry any long-failed documents immediately.
127 networkstatus_reset_download_failures(void)
130 const smartlist_t
*networkstatus_v2_list
= networkstatus_get_v2_list();
131 SMARTLIST_FOREACH_BEGIN(networkstatus_v2_list
, networkstatus_v2_t
*, ns
) {
132 SMARTLIST_FOREACH_BEGIN(ns
->entries
, routerstatus_t
*, rs
) {
133 if (!router_get_by_descriptor_digest(rs
->descriptor_digest
))
134 rs
->need_to_mirror
= 1;
135 } SMARTLIST_FOREACH_END(rs
);
136 } SMARTLIST_FOREACH_END(ns
);
138 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
)
139 download_status_reset(&consensus_dl_status
[i
]);
140 if (v2_download_status_map
) {
141 digestmap_iter_t
*iter
;
142 digestmap_t
*map
= v2_download_status_map
;
145 download_status_t
*dls
;
146 for (iter
= digestmap_iter_init(map
); !digestmap_iter_done(iter
);
147 iter
= digestmap_iter_next(map
, iter
) ) {
148 digestmap_iter_get(iter
, &key
, &val
);
150 download_status_reset(dls
);
155 /** Repopulate our list of network_status_t objects from the list cached on
156 * disk. Return 0 on success, -1 on failure. */
158 router_reload_v2_networkstatus(void)
160 smartlist_t
*entries
;
163 char *filename
= get_datadir_fname("cached-status");
164 int maybe_delete
= !directory_caches_v2_dir_info(get_options());
165 time_t now
= time(NULL
);
166 if (!networkstatus_v2_list
)
167 networkstatus_v2_list
= smartlist_new();
169 entries
= tor_listdir(filename
);
170 if (!entries
) { /* dir doesn't exist */
173 } else if (!smartlist_len(entries
) && maybe_delete
) {
176 smartlist_free(entries
);
180 SMARTLIST_FOREACH_BEGIN(entries
, const char *, fn
) {
181 char buf
[DIGEST_LEN
];
183 filename
= get_datadir_fname2("cached-status", fn
);
184 remove_file_if_very_old(filename
, now
);
188 if (strlen(fn
) != HEX_DIGEST_LEN
||
189 base16_decode(buf
, sizeof(buf
), fn
, strlen(fn
))) {
191 "Skipping cached-status file with unexpected name \"%s\"",fn
);
194 filename
= get_datadir_fname2("cached-status", fn
);
195 s
= read_file_to_str(filename
, 0, &st
);
197 if (router_set_networkstatus_v2(s
, st
.st_mtime
, NS_FROM_CACHE
,
199 log_warn(LD_FS
, "Couldn't load networkstatus from \"%s\"",filename
);
204 } SMARTLIST_FOREACH_END(fn
);
205 SMARTLIST_FOREACH(entries
, char *, fn
, tor_free(fn
));
206 smartlist_free(entries
);
207 networkstatus_v2_list_clean(time(NULL
));
208 routers_update_all_from_networkstatus(time(NULL
), 2);
212 /** Read every cached v3 consensus networkstatus from the disk. */
214 router_reload_consensus_networkstatus(void)
219 const or_options_t
*options
= get_options();
220 const unsigned int flags
= NSSET_FROM_CACHE
| NSSET_DONT_DOWNLOAD_CERTS
;
223 /* FFFF Suppress warnings if cached consensus is bad? */
224 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
226 const char *flavor
= networkstatus_get_flavor_name(flav
);
227 if (flav
== FLAV_NS
) {
228 filename
= get_datadir_fname("cached-consensus");
230 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
231 filename
= get_datadir_fname(buf
);
233 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
235 if (networkstatus_set_current_consensus(s
, flavor
, flags
) < -1) {
236 log_warn(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
243 if (flav
== FLAV_NS
) {
244 filename
= get_datadir_fname("unverified-consensus");
246 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
247 filename
= get_datadir_fname(buf
);
250 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
252 if (networkstatus_set_current_consensus(s
, flavor
,
253 flags
|NSSET_WAS_WAITING_FOR_CERTS
)) {
254 log_info(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
262 if (!current_consensus
||
263 (stat(options
->FallbackNetworkstatusFile
, &st
)==0 &&
264 st
.st_mtime
> current_consensus
->valid_after
)) {
265 s
= read_file_to_str(options
->FallbackNetworkstatusFile
,
266 RFTS_IGNORE_MISSING
, NULL
);
268 if (networkstatus_set_current_consensus(s
, "ns",
269 flags
|NSSET_ACCEPT_OBSOLETE
)) {
270 log_info(LD_FS
, "Couldn't load consensus networkstatus from \"%s\"",
271 options
->FallbackNetworkstatusFile
);
274 "Loaded fallback consensus networkstatus from \"%s\"",
275 options
->FallbackNetworkstatusFile
);
281 if (!current_consensus
) {
282 if (!named_server_map
)
283 named_server_map
= strmap_new();
284 if (!unnamed_server_map
)
285 unnamed_server_map
= strmap_new();
288 update_certificate_downloads(time(NULL
));
290 routers_update_all_from_networkstatus(time(NULL
), 3);
291 update_microdescs_from_networkstatus(time(NULL
));
296 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
298 vote_routerstatus_free(vote_routerstatus_t
*rs
)
300 vote_microdesc_hash_t
*h
, *next
;
303 tor_free(rs
->version
);
304 tor_free(rs
->status
.exitsummary
);
305 for (h
= rs
->microdesc
; h
; h
= next
) {
306 tor_free(h
->microdesc_hash_line
);
313 /** Free all storage held by the routerstatus object <b>rs</b>. */
315 routerstatus_free(routerstatus_t
*rs
)
319 tor_free(rs
->exitsummary
);
323 /** Free all storage held by the networkstatus object <b>ns</b>. */
325 networkstatus_v2_free(networkstatus_v2_t
*ns
)
329 tor_free(ns
->source_address
);
330 tor_free(ns
->contact
);
332 crypto_pk_free(ns
->signing_key
);
333 tor_free(ns
->client_versions
);
334 tor_free(ns
->server_versions
);
336 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
337 routerstatus_free(rs
));
338 smartlist_free(ns
->entries
);
343 /** Free all storage held in <b>sig</b> */
345 document_signature_free(document_signature_t
*sig
)
347 tor_free(sig
->signature
);
351 /** Return a newly allocated copy of <b>sig</b> */
352 document_signature_t
*
353 document_signature_dup(const document_signature_t
*sig
)
355 document_signature_t
*r
= tor_memdup(sig
, sizeof(document_signature_t
));
357 r
->signature
= tor_memdup(sig
->signature
, sig
->signature_len
);
361 /** Free all storage held in <b>ns</b>. */
363 networkstatus_vote_free(networkstatus_t
*ns
)
368 tor_free(ns
->client_versions
);
369 tor_free(ns
->server_versions
);
370 if (ns
->known_flags
) {
371 SMARTLIST_FOREACH(ns
->known_flags
, char *, c
, tor_free(c
));
372 smartlist_free(ns
->known_flags
);
374 if (ns
->weight_params
) {
375 SMARTLIST_FOREACH(ns
->weight_params
, char *, c
, tor_free(c
));
376 smartlist_free(ns
->weight_params
);
378 if (ns
->net_params
) {
379 SMARTLIST_FOREACH(ns
->net_params
, char *, c
, tor_free(c
));
380 smartlist_free(ns
->net_params
);
382 if (ns
->supported_methods
) {
383 SMARTLIST_FOREACH(ns
->supported_methods
, char *, c
, tor_free(c
));
384 smartlist_free(ns
->supported_methods
);
387 SMARTLIST_FOREACH_BEGIN(ns
->voters
, networkstatus_voter_info_t
*, voter
) {
388 tor_free(voter
->nickname
);
389 tor_free(voter
->address
);
390 tor_free(voter
->contact
);
392 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
393 document_signature_free(sig
));
394 smartlist_free(voter
->sigs
);
397 } SMARTLIST_FOREACH_END(voter
);
398 smartlist_free(ns
->voters
);
400 authority_cert_free(ns
->cert
);
402 if (ns
->routerstatus_list
) {
403 if (ns
->type
== NS_TYPE_VOTE
|| ns
->type
== NS_TYPE_OPINION
) {
404 SMARTLIST_FOREACH(ns
->routerstatus_list
, vote_routerstatus_t
*, rs
,
405 vote_routerstatus_free(rs
));
407 SMARTLIST_FOREACH(ns
->routerstatus_list
, routerstatus_t
*, rs
,
408 routerstatus_free(rs
));
411 smartlist_free(ns
->routerstatus_list
);
414 digestmap_free(ns
->desc_digest_map
, NULL
);
416 memset(ns
, 11, sizeof(*ns
));
420 /** Return the voter info from <b>vote</b> for the voter whose identity digest
421 * is <b>identity</b>, or NULL if no such voter is associated with
423 networkstatus_voter_info_t
*
424 networkstatus_get_voter_by_id(networkstatus_t
*vote
,
425 const char *identity
)
427 if (!vote
|| !vote
->voters
)
429 SMARTLIST_FOREACH(vote
->voters
, networkstatus_voter_info_t
*, voter
,
430 if (fast_memeq(voter
->identity_digest
, identity
, DIGEST_LEN
))
435 /** Check whether the signature <b>sig</b> is correctly signed with the
436 * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
437 * signing key; otherwise set the good_signature or bad_signature flag on
438 * <b>voter</b>, and return 0. */
440 networkstatus_check_document_signature(const networkstatus_t
*consensus
,
441 document_signature_t
*sig
,
442 const authority_cert_t
*cert
)
444 char key_digest
[DIGEST_LEN
];
445 const int dlen
= sig
->alg
== DIGEST_SHA1
? DIGEST_LEN
: DIGEST256_LEN
;
447 size_t signed_digest_len
;
449 if (crypto_pk_get_digest(cert
->signing_key
, key_digest
)<0)
451 if (tor_memneq(sig
->signing_key_digest
, key_digest
, DIGEST_LEN
) ||
452 tor_memneq(sig
->identity_digest
, cert
->cache_info
.identity_digest
,
456 signed_digest_len
= crypto_pk_keysize(cert
->signing_key
);
457 signed_digest
= tor_malloc(signed_digest_len
);
458 if (crypto_pk_public_checksig(cert
->signing_key
,
462 sig
->signature_len
) < dlen
||
463 tor_memneq(signed_digest
, consensus
->digests
.d
[sig
->alg
], dlen
)) {
464 log_warn(LD_DIR
, "Got a bad signature on a networkstatus vote");
465 sig
->bad_signature
= 1;
467 sig
->good_signature
= 1;
469 tor_free(signed_digest
);
473 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
474 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
475 * signature from every recognized authority on it, 0 if there are
476 * enough good signatures from recognized authorities on it, -1 if we might
477 * get enough good signatures by fetching missing certificates, and -2
478 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
479 * about every problem; if warn is at least 1, warn only if we can't get
480 * enough signatures; if warn is negative, log nothing at all. */
482 networkstatus_check_consensus_signature(networkstatus_t
*consensus
,
486 int n_missing_key
= 0, n_dl_failed_key
= 0;
489 int n_no_signature
= 0;
490 int n_v3_authorities
= get_n_authorities(V3_DIRINFO
);
491 int n_required
= n_v3_authorities
/2 + 1;
492 smartlist_t
*list_good
= smartlist_new();
493 smartlist_t
*list_no_signature
= smartlist_new();
494 smartlist_t
*need_certs_from
= smartlist_new();
495 smartlist_t
*unrecognized
= smartlist_new();
496 smartlist_t
*missing_authorities
= smartlist_new();
498 time_t now
= time(NULL
);
500 tor_assert(consensus
->type
== NS_TYPE_CONSENSUS
);
502 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*,
506 int unknown_here
= 0;
507 int missing_key_here
= 0, dl_failed_key_here
= 0;
508 SMARTLIST_FOREACH_BEGIN(voter
->sigs
, document_signature_t
*, sig
) {
509 if (!sig
->good_signature
&& !sig
->bad_signature
&&
511 /* we can try to check the signature. */
512 int is_v3_auth
= trusteddirserver_get_by_v3_auth_digest(
513 sig
->identity_digest
) != NULL
;
514 authority_cert_t
*cert
=
515 authority_cert_get_by_digests(sig
->identity_digest
,
516 sig
->signing_key_digest
);
517 tor_assert(tor_memeq(sig
->identity_digest
, voter
->identity_digest
,
521 smartlist_add(unrecognized
, voter
);
524 } else if (!cert
|| cert
->expires
< now
) {
525 smartlist_add(need_certs_from
, voter
);
527 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
528 ++dl_failed_key_here
;
531 if (networkstatus_check_document_signature(consensus
, sig
, cert
) < 0) {
532 smartlist_add(need_certs_from
, voter
);
534 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
535 ++dl_failed_key_here
;
539 if (sig
->good_signature
)
541 else if (sig
->bad_signature
)
543 } SMARTLIST_FOREACH_END(sig
);
547 smartlist_add(list_good
, voter
->nickname
);
548 } else if (bad_here
) {
550 } else if (missing_key_here
) {
552 if (dl_failed_key_here
)
554 } else if (unknown_here
) {
558 smartlist_add(list_no_signature
, voter
->nickname
);
560 } SMARTLIST_FOREACH_END(voter
);
562 /* Now see whether we're missing any voters entirely. */
563 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
564 trusted_dir_server_t
*, ds
,
566 if ((ds
->type
& V3_DIRINFO
) &&
567 !networkstatus_get_voter_by_id(consensus
, ds
->v3_identity_digest
))
568 smartlist_add(missing_authorities
, ds
);
571 if (warn
> 1 || (warn
>= 0 &&
572 (n_good
+ n_missing_key
- n_dl_failed_key
< n_required
))) {
579 SMARTLIST_FOREACH(unrecognized
, networkstatus_voter_info_t
*, voter
,
581 log(severity
, LD_DIR
, "Consensus includes unrecognized authority "
582 "'%s' at %s:%d (contact %s; identity %s)",
583 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
584 voter
->contact
?voter
->contact
:"n/a",
585 hex_str(voter
->identity_digest
, DIGEST_LEN
));
587 SMARTLIST_FOREACH(need_certs_from
, networkstatus_voter_info_t
*, voter
,
589 log(severity
, LD_DIR
, "Looks like we need to download a new "
590 "certificate from authority '%s' at %s:%d (contact %s; "
592 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
593 voter
->contact
?voter
->contact
:"n/a",
594 hex_str(voter
->identity_digest
, DIGEST_LEN
));
596 SMARTLIST_FOREACH(missing_authorities
, trusted_dir_server_t
*, ds
,
598 log(severity
, LD_DIR
, "Consensus does not include configured "
599 "authority '%s' at %s:%d (identity %s)",
600 ds
->nickname
, ds
->address
, (int)ds
->dir_port
,
601 hex_str(ds
->v3_identity_digest
, DIGEST_LEN
));
605 smartlist_t
*sl
= smartlist_new();
606 char *tmp
= smartlist_join_strings(list_good
, " ", 0, NULL
);
607 smartlist_add_asprintf(sl
,
608 "A consensus needs %d good signatures from recognized "
609 "authorities for us to accept it. This one has %d (%s).",
610 n_required
, n_good
, tmp
);
612 if (n_no_signature
) {
613 tmp
= smartlist_join_strings(list_no_signature
, " ", 0, NULL
);
614 smartlist_add_asprintf(sl
,
615 "%d (%s) of the authorities we know didn't sign it.",
616 n_no_signature
, tmp
);
620 smartlist_add_asprintf(sl
,
621 "It has %d signatures from authorities we don't "
622 "recognize.", n_unknown
);
625 smartlist_add_asprintf(sl
, "%d of the signatures on it didn't verify "
626 "correctly.", n_bad
);
629 smartlist_add_asprintf(sl
,
630 "We were unable to check %d of the signatures, "
631 "because we were missing the keys.", n_missing_key
);
633 joined
= smartlist_join_strings(sl
, " ", 0, NULL
);
634 log(severity
, LD_DIR
, "%s", joined
);
636 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
641 smartlist_free(list_good
);
642 smartlist_free(list_no_signature
);
643 smartlist_free(unrecognized
);
644 smartlist_free(need_certs_from
);
645 smartlist_free(missing_authorities
);
647 if (n_good
== n_v3_authorities
)
649 else if (n_good
>= n_required
)
651 else if (n_good
+ n_missing_key
>= n_required
)
657 /** Helper: return a newly allocated string containing the name of the filename
658 * where we plan to cache the network status with the given identity digest. */
660 networkstatus_get_cache_filename(const char *identity_digest
)
662 char fp
[HEX_DIGEST_LEN
+1];
663 base16_encode(fp
, HEX_DIGEST_LEN
+1, identity_digest
, DIGEST_LEN
);
664 return get_datadir_fname2("cached-status", fp
);
667 /** Helper for smartlist_sort: Compare two networkstatus objects by
668 * publication date. */
670 _compare_networkstatus_v2_published_on(const void **_a
, const void **_b
)
672 const networkstatus_v2_t
*a
= *_a
, *b
= *_b
;
673 if (a
->published_on
< b
->published_on
)
675 else if (a
->published_on
> b
->published_on
)
681 /** Add the parsed v2 networkstatus in <b>ns</b> (with original document in
682 * <b>s</b>) to the disk cache (and the in-memory directory server cache) as
685 add_networkstatus_to_cache(const char *s
,
686 v2_networkstatus_source_t source
,
687 networkstatus_v2_t
*ns
)
689 if (source
!= NS_FROM_CACHE
) {
690 char *fn
= networkstatus_get_cache_filename(ns
->identity_digest
);
691 if (write_str_to_file(fn
, s
, 0)<0) {
692 log_notice(LD_FS
, "Couldn't write cached network status to \"%s\"", fn
);
697 if (directory_caches_v2_dir_info(get_options()))
698 dirserv_set_cached_networkstatus_v2(s
,
705 /** How far in the future do we allow a network-status to get before removing
707 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
709 /** Given a string <b>s</b> containing a network status that we received at
710 * <b>arrived_at</b> from <b>source</b>, try to parse it, see if we want to
711 * store it, and put it into our cache as necessary.
713 * If <b>source</b> is NS_FROM_DIR or NS_FROM_CACHE, do not replace our
714 * own networkstatus_t (if we're an authoritative directory server).
716 * If <b>source</b> is NS_FROM_CACHE, do not write our networkstatus_t to the
719 * If <b>requested_fingerprints</b> is provided, it must contain a list of
720 * uppercased identity fingerprints. Do not update any networkstatus whose
721 * fingerprint is not on the list; after updating a networkstatus, remove its
722 * fingerprint from the list.
724 * Return 0 on success, -1 on failure.
726 * Callers should make sure that routers_update_all_from_networkstatus() is
727 * invoked after this function succeeds.
730 router_set_networkstatus_v2(const char *s
, time_t arrived_at
,
731 v2_networkstatus_source_t source
,
732 smartlist_t
*requested_fingerprints
)
734 networkstatus_v2_t
*ns
;
738 trusted_dir_server_t
*trusted_dir
= NULL
;
739 const char *source_desc
= NULL
;
740 char fp
[HEX_DIGEST_LEN
+1];
741 char published
[ISO_TIME_LEN
+1];
743 if (!directory_caches_v2_dir_info(get_options()))
744 return 0; /* Don't bother storing it. */
746 ns
= networkstatus_v2_parse_from_string(s
);
748 log_warn(LD_DIR
, "Couldn't parse network status.");
751 base16_encode(fp
, HEX_DIGEST_LEN
+1, ns
->identity_digest
, DIGEST_LEN
);
753 router_get_trusteddirserver_by_digest(ns
->identity_digest
)) ||
754 !(trusted_dir
->type
& V2_DIRINFO
)) {
755 log_info(LD_DIR
, "Network status was signed, but not by an authoritative "
756 "directory we recognize.");
759 source_desc
= trusted_dir
->description
;
762 if (arrived_at
> now
)
765 ns
->received_on
= arrived_at
;
767 format_iso_time(published
, ns
->published_on
);
769 if (ns
->published_on
> now
+ NETWORKSTATUS_ALLOW_SKEW
) {
771 long delta
= now
- ns
->published_on
;
772 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
773 log_warn(LD_GENERAL
, "Network status from %s was published %s in the "
774 "future (%s GMT). Check your time and date settings! "
776 source_desc
, dbuf
, published
);
777 control_event_general_status(LOG_WARN
,
778 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=NETWORKSTATUS:%s:%d",
779 delta
, ns
->source_address
, ns
->source_dirport
);
783 if (!networkstatus_v2_list
)
784 networkstatus_v2_list
= smartlist_new();
786 if ( (source
== NS_FROM_DIR_BY_FP
|| source
== NS_FROM_DIR_ALL
) &&
787 router_digest_is_me(ns
->identity_digest
)) {
788 /* Don't replace our own networkstatus when we get it from somebody else.*/
789 networkstatus_v2_free(ns
);
793 if (requested_fingerprints
) {
794 if (smartlist_string_isin(requested_fingerprints
, fp
)) {
795 smartlist_string_remove(requested_fingerprints
, fp
);
797 if (source
!= NS_FROM_DIR_ALL
) {
799 smartlist_join_strings(requested_fingerprints
," ",0,NULL
);
801 "We received a network status with a fingerprint (%s) that we "
802 "never requested. (We asked for: %s.) Dropping.",
812 /* We got a non-trusted networkstatus, and we're a directory cache.
813 * This means that we asked an authority, and it told us about another
814 * authority we didn't recognize. */
816 "We do not recognize authority (%s) but we are willing "
818 add_networkstatus_to_cache(s
, source
, ns
);
819 networkstatus_v2_free(ns
);
825 for (i
=0; i
< smartlist_len(networkstatus_v2_list
); ++i
) {
826 networkstatus_v2_t
*old_ns
= smartlist_get(networkstatus_v2_list
, i
);
828 if (tor_memeq(old_ns
->identity_digest
, ns
->identity_digest
, DIGEST_LEN
)) {
829 if (tor_memeq(old_ns
->networkstatus_digest
,
830 ns
->networkstatus_digest
, DIGEST_LEN
)) {
831 /* Same one we had before. */
832 networkstatus_v2_free(ns
);
833 tor_assert(trusted_dir
);
835 "Not replacing network-status from %s (published %s); "
836 "we already have it.",
837 trusted_dir
->description
, published
);
838 if (old_ns
->received_on
< arrived_at
) {
839 if (source
!= NS_FROM_CACHE
) {
841 fn
= networkstatus_get_cache_filename(old_ns
->identity_digest
);
842 /* We use mtime to tell when it arrived, so update that. */
846 old_ns
->received_on
= arrived_at
;
848 download_status_failed(&trusted_dir
->v2_ns_dl_status
, 0);
850 } else if (old_ns
->published_on
>= ns
->published_on
) {
851 char old_published
[ISO_TIME_LEN
+1];
852 format_iso_time(old_published
, old_ns
->published_on
);
853 tor_assert(trusted_dir
);
855 "Not replacing network-status from %s (published %s);"
856 " we have a newer one (published %s) for this authority.",
857 trusted_dir
->description
, published
,
859 networkstatus_v2_free(ns
);
860 download_status_failed(&trusted_dir
->v2_ns_dl_status
, 0);
863 networkstatus_v2_free(old_ns
);
864 smartlist_set(networkstatus_v2_list
, i
, ns
);
871 if (source
!= NS_FROM_CACHE
&& trusted_dir
) {
872 download_status_reset(&trusted_dir
->v2_ns_dl_status
);
876 smartlist_add(networkstatus_v2_list
, ns
);
878 /** Retain any routerinfo mentioned in a V2 networkstatus for at least this
880 #define V2_NETWORKSTATUS_ROUTER_LIFETIME (3*60*60)
883 time_t live_until
= ns
->published_on
+ V2_NETWORKSTATUS_ROUTER_LIFETIME
;
884 SMARTLIST_FOREACH_BEGIN(ns
->entries
, routerstatus_t
*, rs
) {
885 signed_descriptor_t
*sd
=
886 router_get_by_descriptor_digest(rs
->descriptor_digest
);
888 if (sd
->last_listed_as_valid_until
< live_until
)
889 sd
->last_listed_as_valid_until
= live_until
;
891 rs
->need_to_mirror
= 1;
893 } SMARTLIST_FOREACH_END(rs
);
896 log_info(LD_DIR
, "Setting networkstatus %s %s (published %s)",
897 source
== NS_FROM_CACHE
?"cached from":
898 ((source
== NS_FROM_DIR_BY_FP
|| source
== NS_FROM_DIR_ALL
) ?
899 "downloaded from":"generated for"),
900 trusted_dir
->description
, published
);
901 networkstatus_v2_list_has_changed
= 1;
903 smartlist_sort(networkstatus_v2_list
,
904 _compare_networkstatus_v2_published_on
);
907 add_networkstatus_to_cache(s
, source
, ns
);
912 /** Remove all very-old network_status_t objects from memory and from the
915 networkstatus_v2_list_clean(time_t now
)
918 if (!networkstatus_v2_list
)
921 for (i
= 0; i
< smartlist_len(networkstatus_v2_list
); ++i
) {
922 networkstatus_v2_t
*ns
= smartlist_get(networkstatus_v2_list
, i
);
924 if (ns
->published_on
+ MAX_NETWORKSTATUS_AGE
> now
)
926 /* Okay, this one is too old. Remove it from the list, and delete it
928 smartlist_del(networkstatus_v2_list
, i
--);
929 fname
= networkstatus_get_cache_filename(ns
->identity_digest
);
930 if (file_status(fname
) == FN_FILE
) {
931 log_info(LD_DIR
, "Removing too-old networkstatus in %s", fname
);
935 if (directory_caches_v2_dir_info(get_options())) {
936 dirserv_set_cached_networkstatus_v2(NULL
, ns
->identity_digest
, 0);
938 networkstatus_v2_free(ns
);
941 /* And now go through the directory cache for any cached untrusted
942 * networkstatuses and other network info. */
943 dirserv_clear_old_networkstatuses(now
- MAX_NETWORKSTATUS_AGE
);
944 dirserv_clear_old_v1_info(now
);
947 /** Helper for bsearching a list of routerstatus_t pointers: compare a
948 * digest in the key to the identity digest of a routerstatus_t. */
950 compare_digest_to_routerstatus_entry(const void *_key
, const void **_member
)
952 const char *key
= _key
;
953 const routerstatus_t
*rs
= *_member
;
954 return tor_memcmp(key
, rs
->identity_digest
, DIGEST_LEN
);
957 /** As networkstatus_v2_find_entry, but do not return a const pointer */
959 networkstatus_v2_find_mutable_entry(networkstatus_v2_t
*ns
, const char *digest
)
961 return smartlist_bsearch(ns
->entries
, digest
,
962 compare_digest_to_routerstatus_entry
);
965 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
966 * NULL if none was found. */
967 const routerstatus_t
*
968 networkstatus_v2_find_entry(networkstatus_v2_t
*ns
, const char *digest
)
970 return networkstatus_v2_find_mutable_entry(ns
, digest
);
973 /** As networkstatus_find_entry, but do not return a const pointer */
975 networkstatus_vote_find_mutable_entry(networkstatus_t
*ns
, const char *digest
)
977 return smartlist_bsearch(ns
->routerstatus_list
, digest
,
978 compare_digest_to_routerstatus_entry
);
981 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
982 * NULL if none was found. */
983 const routerstatus_t
*
984 networkstatus_vote_find_entry(networkstatus_t
*ns
, const char *digest
)
986 return networkstatus_vote_find_mutable_entry(ns
, digest
);
989 /*XXXX MOVE make this static once functions are moved into this file. */
990 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
991 * <b>digest</b>. Return value and set *<b>found_out</b> as for
992 * smartlist_bsearch_idx(). */
994 networkstatus_vote_find_entry_idx(networkstatus_t
*ns
,
995 const char *digest
, int *found_out
)
997 return smartlist_bsearch_idx(ns
->routerstatus_list
, digest
,
998 compare_digest_to_routerstatus_entry
,
1002 /** Return a list of the v2 networkstatus documents. */
1004 networkstatus_get_v2_list(void)
1006 if (!networkstatus_v2_list
)
1007 networkstatus_v2_list
= smartlist_new();
1008 return networkstatus_v2_list
;
1011 /** As router_get_consensus_status_by_descriptor_digest, but does not return
1012 * a const pointer. */
1014 router_get_mutable_consensus_status_by_descriptor_digest(
1015 networkstatus_t
*consensus
,
1019 consensus
= current_consensus
;
1022 if (!consensus
->desc_digest_map
) {
1023 digestmap_t
*m
= consensus
->desc_digest_map
= digestmap_new();
1024 SMARTLIST_FOREACH(consensus
->routerstatus_list
,
1025 routerstatus_t
*, rs
,
1027 digestmap_set(m
, rs
->descriptor_digest
, rs
);
1030 return digestmap_get(consensus
->desc_digest_map
, digest
);
1033 /** Return the consensus view of the status of the router whose current
1034 * <i>descriptor</i> digest in <b>consensus</b> is <b>digest</b>, or NULL if
1035 * no such router is known. */
1036 const routerstatus_t
*
1037 router_get_consensus_status_by_descriptor_digest(networkstatus_t
*consensus
,
1040 return router_get_mutable_consensus_status_by_descriptor_digest(
1044 /** Given the digest of a router descriptor, return its current download
1045 * status, or NULL if the digest is unrecognized. */
1047 router_get_dl_status_by_descriptor_digest(const char *d
)
1050 if (!current_ns_consensus
)
1052 if ((rs
= router_get_mutable_consensus_status_by_descriptor_digest(
1053 current_ns_consensus
, d
)))
1054 return &rs
->dl_status
;
1055 if (v2_download_status_map
)
1056 return digestmap_get(v2_download_status_map
, d
);
1061 /** As router_get_consensus_status_by_id, but do not return a const pointer */
1063 router_get_mutable_consensus_status_by_id(const char *digest
)
1065 if (!current_consensus
)
1067 return smartlist_bsearch(current_consensus
->routerstatus_list
, digest
,
1068 compare_digest_to_routerstatus_entry
);
1071 /** Return the consensus view of the status of the router whose identity
1072 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
1073 const routerstatus_t
*
1074 router_get_consensus_status_by_id(const char *digest
)
1076 return router_get_mutable_consensus_status_by_id(digest
);
1079 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
1080 * the corresponding routerstatus_t, or NULL if none exists. Warn the
1081 * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
1082 * nickname, but the Named flag isn't set for that router. */
1083 const routerstatus_t
*
1084 router_get_consensus_status_by_nickname(const char *nickname
,
1085 int warn_if_unnamed
)
1087 const node_t
*node
= node_get_by_nickname(nickname
, warn_if_unnamed
);
1094 /** Return the identity digest that's mapped to officially by
1095 * <b>nickname</b>. */
1097 networkstatus_get_router_digest_by_nickname(const char *nickname
)
1099 if (!named_server_map
)
1101 return strmap_get_lc(named_server_map
, nickname
);
1104 /** Return true iff <b>nickname</b> is disallowed from being the nickname
1107 networkstatus_nickname_is_unnamed(const char *nickname
)
1109 if (!unnamed_server_map
)
1111 return strmap_get_lc(unnamed_server_map
, nickname
) != NULL
;
1114 /** How frequently do directory authorities re-download fresh networkstatus
1116 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
1118 /** How frequently do non-authority directory caches re-download fresh
1119 * networkstatus documents? */
1120 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
1122 /** We are a directory server, and so cache network_status documents.
1123 * Initiate downloads as needed to update them. For v2 authorities,
1124 * this means asking each trusted directory for its network-status.
1125 * For caches, this means asking a random v2 authority for all
1129 update_v2_networkstatus_cache_downloads(time_t now
)
1131 int authority
= authdir_mode_v2(get_options());
1133 authority
? AUTHORITY_NS_CACHE_INTERVAL
: NONAUTHORITY_NS_CACHE_INTERVAL
;
1134 const smartlist_t
*trusted_dir_servers
= router_get_trusted_dir_servers();
1136 if (last_networkstatus_download_attempted
+ interval
>= now
)
1139 last_networkstatus_download_attempted
= now
;
1142 /* An authority launches a separate connection for everybody. */
1143 SMARTLIST_FOREACH_BEGIN(trusted_dir_servers
, trusted_dir_server_t
*, ds
)
1145 char resource
[HEX_DIGEST_LEN
+6]; /* fp/hexdigit.z\0 */
1147 if (!(ds
->type
& V2_DIRINFO
))
1149 if (router_digest_is_me(ds
->digest
))
1151 tor_addr_from_ipv4h(&addr
, ds
->addr
);
1152 /* Is this quite sensible with IPv6 or multiple addresses? */
1153 if (connection_get_by_type_addr_port_purpose(
1154 CONN_TYPE_DIR
, &addr
, ds
->dir_port
,
1155 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
)) {
1156 /* XXX the above dir_port won't be accurate if we're
1157 * doing a tunneled conn. In that case it should be or_port.
1158 * How to guess from here? Maybe make the function less general
1159 * and have it know that it's looking for dir conns. -RD */
1160 /* Only directory caches download v2 networkstatuses, and they
1161 * don't use tunneled connections. I think it's okay to ignore
1165 strlcpy(resource
, "fp/", sizeof(resource
));
1166 base16_encode(resource
+3, sizeof(resource
)-3, ds
->digest
, DIGEST_LEN
);
1167 strlcat(resource
, ".z", sizeof(resource
));
1168 directory_initiate_command_routerstatus(
1169 &ds
->fake_status
, DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
,
1170 ROUTER_PURPOSE_GENERAL
,
1171 0, /* Not private */
1173 NULL
, 0 /* No payload. */,
1176 SMARTLIST_FOREACH_END(ds
);
1178 /* A non-authority cache launches one connection to a random authority. */
1179 /* (Check whether we're currently fetching network-status objects.) */
1180 if (!connection_get_by_type_purpose(CONN_TYPE_DIR
,
1181 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
))
1182 directory_get_from_dirserver(DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
,
1183 ROUTER_PURPOSE_GENERAL
, "all.z",
1184 PDS_RETRY_IF_NO_SERVERS
);
1188 /** Return true iff, given the options listed in <b>options</b>, <b>flavor</b>
1189 * is the flavor of a consensus networkstatus that we would like to fetch. */
1191 we_want_to_fetch_flavor(const or_options_t
*options
, int flavor
)
1193 if (flavor
< 0 || flavor
> N_CONSENSUS_FLAVORS
) {
1194 /* This flavor is crazy; we don't want it */
1195 /*XXXX handle unrecognized flavors later */
1198 if (authdir_mode_v3(options
) || directory_caches_dir_info(options
)) {
1199 /* We want to serve all flavors to others, regardless if we would use
1203 if (options
->FetchUselessDescriptors
) {
1204 /* In order to get all descriptors, we need to fetch all consensuses. */
1207 /* Otherwise, we want the flavor only if we want to use it to build
1209 return flavor
== usable_consensus_flavor();
1212 /** How many times will we try to fetch a consensus before we give up? */
1213 #define CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES 8
1214 /** How long will we hang onto a possibly live consensus for which we're
1215 * fetching certs before we check whether there is a better one? */
1216 #define DELAY_WHILE_FETCHING_CERTS (20*60)
1218 /** If we want to download a fresh consensus, launch a new download as
1221 update_consensus_networkstatus_downloads(time_t now
)
1224 const or_options_t
*options
= get_options();
1226 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1227 /* XXXX need some way to download unknown flavors if we are caching. */
1228 const char *resource
;
1229 consensus_waiting_for_certs_t
*waiting
;
1232 if (! we_want_to_fetch_flavor(options
, i
))
1235 c
= networkstatus_get_latest_consensus_by_flavor(i
);
1236 if (! (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
)) {
1237 /* No live consensus? Get one now!*/
1238 time_to_download_next_consensus
[i
] = now
;
1241 if (time_to_download_next_consensus
[i
] > now
)
1242 return; /* Wait until the current consensus is older. */
1244 resource
= networkstatus_get_flavor_name(i
);
1246 if (!download_status_is_ready(&consensus_dl_status
[i
], now
,
1247 CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES
))
1248 continue; /* We failed downloading a consensus too recently. */
1249 if (connection_dir_get_by_purpose_and_resource(
1250 DIR_PURPOSE_FETCH_CONSENSUS
, resource
))
1251 continue; /* There's an in-progress download.*/
1253 waiting
= &consensus_waiting_for_certs
[i
];
1254 if (waiting
->consensus
) {
1255 /* XXXX make sure this doesn't delay sane downloads. */
1256 if (waiting
->set_at
+ DELAY_WHILE_FETCHING_CERTS
> now
) {
1257 continue; /* We're still getting certs for this one. */
1259 if (!waiting
->dl_failed
) {
1260 download_status_failed(&consensus_dl_status
[i
], 0);
1261 waiting
->dl_failed
=1;
1266 log_info(LD_DIR
, "Launching %s networkstatus consensus download.",
1267 networkstatus_get_flavor_name(i
));
1269 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
1270 ROUTER_PURPOSE_GENERAL
, resource
,
1271 PDS_RETRY_IF_NO_SERVERS
);
1275 /** Called when an attempt to download a consensus fails: note that the
1276 * failure occurred, and possibly retry. */
1278 networkstatus_consensus_download_failed(int status_code
, const char *flavname
)
1280 int flav
= networkstatus_parse_flavor_name(flavname
);
1282 tor_assert(flav
< N_CONSENSUS_FLAVORS
);
1283 /* XXXX handle unrecognized flavors */
1284 download_status_failed(&consensus_dl_status
[flav
], status_code
);
1285 /* Retry immediately, if appropriate. */
1286 update_consensus_networkstatus_downloads(time(NULL
));
1290 /** How long do we (as a cache) wait after a consensus becomes non-fresh
1291 * before trying to fetch another? */
1292 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
1294 /** Update the time at which we'll consider replacing the current
1295 * consensus of flavor <b>flav</b> */
1297 update_consensus_networkstatus_fetch_time_impl(time_t now
, int flav
)
1299 const or_options_t
*options
= get_options();
1300 networkstatus_t
*c
= networkstatus_get_latest_consensus_by_flavor(flav
);
1301 const char *flavor
= networkstatus_get_flavor_name(flav
);
1302 if (! we_want_to_fetch_flavor(get_options(), flav
))
1305 if (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1307 long interval
= c
->fresh_until
- c
->valid_after
;
1308 long min_sec_before_caching
= CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1311 if (min_sec_before_caching
> interval
/16) {
1312 /* Usually we allow 2-minutes slop factor in case clocks get
1313 desynchronized a little. If we're on a private network with
1314 a crazy-fast voting interval, though, 2 minutes may be too
1316 min_sec_before_caching
= interval
/16;
1319 if (directory_fetches_dir_info_early(options
)) {
1320 /* We want to cache the next one at some point after this one
1321 * is no longer fresh... */
1322 start
= c
->fresh_until
+ min_sec_before_caching
;
1323 /* Some clients may need the consensus sooner than others. */
1324 if (options
->FetchDirInfoExtraEarly
|| authdir_mode_v3(options
)) {
1326 if (min_sec_before_caching
+ dl_interval
> interval
)
1327 dl_interval
= interval
/2;
1329 /* But only in the first half-interval after that. */
1330 dl_interval
= interval
/2;
1333 /* We're an ordinary client or a bridge. Give all the caches enough
1334 * time to download the consensus. */
1335 start
= c
->fresh_until
+ (interval
*3)/4;
1336 /* But download the next one well before this one is expired. */
1337 dl_interval
= ((c
->valid_until
- start
) * 7 )/ 8;
1339 /* If we're a bridge user, make use of the numbers we just computed
1340 * to choose the rest of the interval *after* them. */
1341 if (directory_fetches_dir_info_later(options
)) {
1342 /* Give all the *clients* enough time to download the consensus. */
1343 start
= start
+ dl_interval
+ min_sec_before_caching
;
1344 /* But try to get it before ours actually expires. */
1345 dl_interval
= (c
->valid_until
- start
) - min_sec_before_caching
;
1348 if (dl_interval
< 1)
1350 /* We must not try to replace c while it's still fresh: */
1351 tor_assert(c
->fresh_until
< start
);
1352 /* We must download the next one before c is invalid: */
1353 tor_assert(start
+dl_interval
< c
->valid_until
);
1354 time_to_download_next_consensus
[flav
] =
1355 start
+ crypto_rand_int((int)dl_interval
);
1357 char tbuf1
[ISO_TIME_LEN
+1];
1358 char tbuf2
[ISO_TIME_LEN
+1];
1359 char tbuf3
[ISO_TIME_LEN
+1];
1360 format_local_iso_time(tbuf1
, c
->fresh_until
);
1361 format_local_iso_time(tbuf2
, c
->valid_until
);
1362 format_local_iso_time(tbuf3
, time_to_download_next_consensus
[flav
]);
1363 log_info(LD_DIR
, "Live %s consensus %s the most recent until %s and "
1364 "will expire at %s; fetching the next one at %s.",
1365 flavor
, (c
->fresh_until
> now
) ? "will be" : "was",
1366 tbuf1
, tbuf2
, tbuf3
);
1369 time_to_download_next_consensus
[flav
] = now
;
1370 log_info(LD_DIR
, "No live %s consensus; we should fetch one immediately.",
1375 /** Update the time at which we'll consider replacing the current
1376 * consensus of flavor 'flavor' */
1378 update_consensus_networkstatus_fetch_time(time_t now
)
1381 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1382 if (we_want_to_fetch_flavor(get_options(), i
))
1383 update_consensus_networkstatus_fetch_time_impl(now
, i
);
1387 /** Return 1 if there's a reason we shouldn't try any directory
1388 * fetches yet (e.g. we demand bridges and none are yet known).
1391 should_delay_dir_fetches(const or_options_t
*options
)
1393 if (options
->UseBridges
&& !any_bridge_descriptors_known()) {
1394 log_info(LD_DIR
, "delaying dir fetches (no running bridges known)");
1400 /** Launch requests for networkstatus documents and authority certificates as
1403 update_networkstatus_downloads(time_t now
)
1405 const or_options_t
*options
= get_options();
1406 if (should_delay_dir_fetches(options
))
1408 if (authdir_mode_any_main(options
) || options
->FetchV2Networkstatus
)
1409 update_v2_networkstatus_cache_downloads(now
);
1410 update_consensus_networkstatus_downloads(now
);
1411 update_certificate_downloads(now
);
1414 /** Launch requests as appropriate for missing directory authority
1417 update_certificate_downloads(time_t now
)
1420 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1421 if (consensus_waiting_for_certs
[i
].consensus
)
1422 authority_certs_fetch_missing(consensus_waiting_for_certs
[i
].consensus
,
1426 if (current_ns_consensus
)
1427 authority_certs_fetch_missing(current_ns_consensus
, now
);
1428 if (current_md_consensus
)
1429 authority_certs_fetch_missing(current_md_consensus
, now
);
1432 /** Return 1 if we have a consensus but we don't have enough certificates
1433 * to start using it yet. */
1435 consensus_is_waiting_for_certs(void)
1437 return consensus_waiting_for_certs
[usable_consensus_flavor()].consensus
1441 /** Return the network status with a given identity digest. */
1442 networkstatus_v2_t
*
1443 networkstatus_v2_get_by_digest(const char *digest
)
1445 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
1447 if (tor_memeq(ns
->identity_digest
, digest
, DIGEST_LEN
))
1453 /** Return the most recent consensus that we have downloaded, or NULL if we
1454 * don't have one. */
1456 networkstatus_get_latest_consensus(void)
1458 return current_consensus
;
1461 /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
1462 * if we don't have one. */
1464 networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f
)
1467 return current_ns_consensus
;
1468 else if (f
== FLAV_MICRODESC
)
1469 return current_md_consensus
;
1476 /** Return the most recent consensus that we have downloaded, or NULL if it is
1477 * no longer live. */
1479 networkstatus_get_live_consensus(time_t now
)
1481 if (current_consensus
&&
1482 current_consensus
->valid_after
<= now
&&
1483 now
<= current_consensus
->valid_until
)
1484 return current_consensus
;
1489 /* XXXX remove this in favor of get_live_consensus. But actually,
1490 * leave something like it for bridge users, who need to not totally
1491 * lose if they spend a while fetching a new consensus. */
1492 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1495 networkstatus_get_reasonably_live_consensus(time_t now
, int flavor
)
1497 #define REASONABLY_LIVE_TIME (24*60*60)
1498 networkstatus_t
*consensus
=
1499 networkstatus_get_latest_consensus_by_flavor(flavor
);
1501 consensus
->valid_after
<= now
&&
1502 now
<= consensus
->valid_until
+REASONABLY_LIVE_TIME
)
1508 /** Given two router status entries for the same router identity, return 1 if
1509 * if the contents have changed between them. Otherwise, return 0. */
1511 routerstatus_has_changed(const routerstatus_t
*a
, const routerstatus_t
*b
)
1513 tor_assert(tor_memeq(a
->identity_digest
, b
->identity_digest
, DIGEST_LEN
));
1515 return strcmp(a
->nickname
, b
->nickname
) ||
1516 fast_memneq(a
->descriptor_digest
, b
->descriptor_digest
, DIGEST_LEN
) ||
1517 a
->addr
!= b
->addr
||
1518 a
->or_port
!= b
->or_port
||
1519 a
->dir_port
!= b
->dir_port
||
1520 a
->is_authority
!= b
->is_authority
||
1521 a
->is_exit
!= b
->is_exit
||
1522 a
->is_stable
!= b
->is_stable
||
1523 a
->is_fast
!= b
->is_fast
||
1524 a
->is_flagged_running
!= b
->is_flagged_running
||
1525 a
->is_named
!= b
->is_named
||
1526 a
->is_unnamed
!= b
->is_unnamed
||
1527 a
->is_valid
!= b
->is_valid
||
1528 a
->is_v2_dir
!= b
->is_v2_dir
||
1529 a
->is_possible_guard
!= b
->is_possible_guard
||
1530 a
->is_bad_exit
!= b
->is_bad_exit
||
1531 a
->is_bad_directory
!= b
->is_bad_directory
||
1532 a
->is_hs_dir
!= b
->is_hs_dir
||
1533 a
->version_known
!= b
->version_known
||
1534 a
->version_supports_begindir
!= b
->version_supports_begindir
||
1535 a
->version_supports_extrainfo_upload
!=
1536 b
->version_supports_extrainfo_upload
||
1537 a
->version_supports_conditional_consensus
!=
1538 b
->version_supports_conditional_consensus
||
1539 a
->version_supports_v3_dir
!= b
->version_supports_v3_dir
;
1542 /** Notify controllers of any router status entries that changed between
1543 * <b>old_c</b> and <b>new_c</b>. */
1545 notify_control_networkstatus_changed(const networkstatus_t
*old_c
,
1546 const networkstatus_t
*new_c
)
1548 smartlist_t
*changed
;
1552 /* tell the controller exactly which relays are still listed, as well
1553 * as what they're listed as */
1554 control_event_newconsensus(new_c
);
1556 if (!control_event_is_interesting(EVENT_NS
))
1560 control_event_networkstatus_changed(new_c
->routerstatus_list
);
1563 changed
= smartlist_new();
1565 SMARTLIST_FOREACH_JOIN(
1566 old_c
->routerstatus_list
, const routerstatus_t
*, rs_old
,
1567 new_c
->routerstatus_list
, const routerstatus_t
*, rs_new
,
1568 tor_memcmp(rs_old
->identity_digest
,
1569 rs_new
->identity_digest
, DIGEST_LEN
),
1570 smartlist_add(changed
, (void*) rs_new
)) {
1571 if (routerstatus_has_changed(rs_old
, rs_new
))
1572 smartlist_add(changed
, (void*)rs_new
);
1573 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1575 control_event_networkstatus_changed(changed
);
1576 smartlist_free(changed
);
1579 /** Copy all the ancillary information (like router download status and so on)
1580 * from <b>old_c</b> to <b>new_c</b>. */
1582 networkstatus_copy_old_consensus_info(networkstatus_t
*new_c
,
1583 const networkstatus_t
*old_c
)
1587 if (!old_c
|| !smartlist_len(old_c
->routerstatus_list
))
1590 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1591 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1592 tor_memcmp(rs_old
->identity_digest
,
1593 rs_new
->identity_digest
, DIGEST_LEN
),
1595 /* Okay, so we're looking at the same identity. */
1596 rs_new
->last_dir_503_at
= rs_old
->last_dir_503_at
;
1598 if (tor_memeq(rs_old
->descriptor_digest
, rs_new
->descriptor_digest
,
1600 /* And the same descriptor too! */
1601 memcpy(&rs_new
->dl_status
, &rs_old
->dl_status
,sizeof(download_status_t
));
1603 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1606 /** Try to replace the current cached v3 networkstatus with the one in
1607 * <b>consensus</b>. If we don't have enough certificates to validate it,
1608 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1610 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1611 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1612 * already received, but we were waiting for certificates on it. If flags &
1613 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1614 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1615 * consensus, even if it comes from many days in the past.
1617 * Return 0 on success, <0 on failure. On failure, caller should increment
1618 * the failure count as appropriate.
1620 * We return -1 for mild failures that don't need to be reported to the
1621 * user, and -2 for more serious problems.
1624 networkstatus_set_current_consensus(const char *consensus
,
1628 networkstatus_t
*c
=NULL
;
1630 time_t now
= time(NULL
);
1631 const or_options_t
*options
= get_options();
1632 char *unverified_fname
= NULL
, *consensus_fname
= NULL
;
1633 int flav
= networkstatus_parse_flavor_name(flavor
);
1634 const unsigned from_cache
= flags
& NSSET_FROM_CACHE
;
1635 const unsigned was_waiting_for_certs
= flags
& NSSET_WAS_WAITING_FOR_CERTS
;
1636 const unsigned dl_certs
= !(flags
& NSSET_DONT_DOWNLOAD_CERTS
);
1637 const unsigned accept_obsolete
= flags
& NSSET_ACCEPT_OBSOLETE
;
1638 const unsigned require_flavor
= flags
& NSSET_REQUIRE_FLAVOR
;
1639 const digests_t
*current_digests
= NULL
;
1640 consensus_waiting_for_certs_t
*waiting
= NULL
;
1641 time_t current_valid_after
= 0;
1642 int free_consensus
= 1; /* Free 'c' at the end of the function */
1645 /* XXXX we don't handle unrecognized flavors yet. */
1646 log_warn(LD_BUG
, "Unrecognized consensus flavor %s", flavor
);
1650 /* Make sure it's parseable. */
1651 c
= networkstatus_parse_vote_from_string(consensus
, NULL
, NS_TYPE_CONSENSUS
);
1653 log_warn(LD_DIR
, "Unable to parse networkstatus consensus");
1658 if ((int)c
->flavor
!= flav
) {
1659 /* This wasn't the flavor we thought we were getting. */
1660 if (require_flavor
) {
1661 log_warn(LD_DIR
, "Got consensus with unexpected flavor %s (wanted %s)",
1662 networkstatus_get_flavor_name(c
->flavor
), flavor
);
1666 flavor
= networkstatus_get_flavor_name(flav
);
1669 if (flav
!= usable_consensus_flavor() &&
1670 !directory_caches_dir_info(options
)) {
1671 /* This consensus is totally boring to us: we won't use it, and we won't
1672 * serve it. Drop it. */
1676 if (from_cache
&& !accept_obsolete
&&
1677 c
->valid_until
< now
-OLD_ROUTER_DESC_MAX_AGE
) {
1678 /* XXXX If we try to make fallbackconsensus work again, we should
1679 * consider taking this out. Until then, believing obsolete consensuses
1680 * is causing more harm than good. See also bug 887. */
1681 log_info(LD_DIR
, "Loaded an expired consensus. Discarding.");
1685 if (!strcmp(flavor
, "ns")) {
1686 consensus_fname
= get_datadir_fname("cached-consensus");
1687 unverified_fname
= get_datadir_fname("unverified-consensus");
1688 if (current_ns_consensus
) {
1689 current_digests
= ¤t_ns_consensus
->digests
;
1690 current_valid_after
= current_ns_consensus
->valid_after
;
1692 } else if (!strcmp(flavor
, "microdesc")) {
1693 consensus_fname
= get_datadir_fname("cached-microdesc-consensus");
1694 unverified_fname
= get_datadir_fname("unverified-microdesc-consensus");
1695 if (current_md_consensus
) {
1696 current_digests
= ¤t_md_consensus
->digests
;
1697 current_valid_after
= current_md_consensus
->valid_after
;
1702 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
1703 consensus_fname
= get_datadir_fname(buf
);
1704 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
1705 unverified_fname
= get_datadir_fname(buf
);
1706 cur
= dirserv_get_consensus(flavor
);
1708 current_digests
= &cur
->digests
;
1709 current_valid_after
= cur
->published
;
1713 if (current_digests
&&
1714 tor_memeq(&c
->digests
, current_digests
, sizeof(c
->digests
))) {
1715 /* We already have this one. That's a failure. */
1716 log_info(LD_DIR
, "Got a %s consensus we already have", flavor
);
1720 if (current_valid_after
&& c
->valid_after
<= current_valid_after
) {
1721 /* We have a newer one. There's no point in accepting this one,
1722 * even if it's great. */
1723 log_info(LD_DIR
, "Got a %s consensus at least as old as the one we have",
1728 /* Make sure it's signed enough. */
1729 if ((r
=networkstatus_check_consensus_signature(c
, 1))<0) {
1731 /* Okay, so it _might_ be signed enough if we get more certificates. */
1732 if (!was_waiting_for_certs
) {
1734 "Not enough certificates to check networkstatus consensus");
1736 if (!current_valid_after
||
1737 c
->valid_after
> current_valid_after
) {
1738 waiting
= &consensus_waiting_for_certs
[flav
];
1739 networkstatus_vote_free(waiting
->consensus
);
1740 tor_free(waiting
->body
);
1741 waiting
->consensus
= c
;
1743 waiting
->body
= tor_strdup(consensus
);
1744 waiting
->set_at
= now
;
1745 waiting
->dl_failed
= 0;
1747 write_str_to_file(unverified_fname
, consensus
, 0);
1750 authority_certs_fetch_missing(c
, now
);
1751 /* This case is not a success or a failure until we get the certs
1752 * or fail to get the certs. */
1755 /* Even if we had enough signatures, we'd never use this as the
1756 * latest consensus. */
1757 if (was_waiting_for_certs
&& from_cache
)
1758 unlink(unverified_fname
);
1762 /* This can never be signed enough: Kill it. */
1763 if (!was_waiting_for_certs
) {
1764 log_warn(LD_DIR
, "Not enough good signatures on networkstatus "
1768 if (was_waiting_for_certs
&& (r
< -1) && from_cache
)
1769 unlink(unverified_fname
);
1774 if (!from_cache
&& flav
== usable_consensus_flavor())
1775 control_event_client_status(LOG_NOTICE
, "CONSENSUS_ARRIVED");
1777 /* Are we missing any certificates at all? */
1778 if (r
!= 1 && dl_certs
)
1779 authority_certs_fetch_missing(c
, now
);
1781 if (flav
== usable_consensus_flavor()) {
1782 notify_control_networkstatus_changed(current_consensus
, c
);
1784 if (flav
== FLAV_NS
) {
1785 if (current_ns_consensus
) {
1786 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1787 networkstatus_vote_free(current_ns_consensus
);
1788 /* Defensive programming : we should set current_consensus very soon,
1789 * but we're about to call some stuff in the meantime, and leaving this
1790 * dangling pointer around has proven to be trouble. */
1791 current_ns_consensus
= NULL
;
1793 current_ns_consensus
= c
;
1794 free_consensus
= 0; /* avoid free */
1795 } else if (flav
== FLAV_MICRODESC
) {
1796 if (current_md_consensus
) {
1797 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1798 networkstatus_vote_free(current_md_consensus
);
1799 /* more defensive programming */
1800 current_md_consensus
= NULL
;
1802 current_md_consensus
= c
;
1803 free_consensus
= 0; /* avoid free */
1806 waiting
= &consensus_waiting_for_certs
[flav
];
1807 if (waiting
->consensus
&&
1808 waiting
->consensus
->valid_after
<= c
->valid_after
) {
1809 networkstatus_vote_free(waiting
->consensus
);
1810 waiting
->consensus
= NULL
;
1811 if (consensus
!= waiting
->body
)
1812 tor_free(waiting
->body
);
1814 waiting
->body
= NULL
;
1815 waiting
->set_at
= 0;
1816 waiting
->dl_failed
= 0;
1817 unlink(unverified_fname
);
1820 /* Reset the failure count only if this consensus is actually valid. */
1821 if (c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1822 download_status_reset(&consensus_dl_status
[flav
]);
1825 download_status_failed(&consensus_dl_status
[flav
], 0);
1828 if (flav
== usable_consensus_flavor()) {
1829 /* XXXXNM Microdescs: needs a non-ns variant. ???? NM*/
1830 update_consensus_networkstatus_fetch_time(now
);
1832 nodelist_set_consensus(current_consensus
);
1834 dirvote_recalculate_timing(options
, now
);
1835 routerstatus_list_update_named_server_map();
1836 cell_ewma_set_scale_factor(options
, current_consensus
);
1838 /* XXXX024 this call might be unnecessary here: can changing the
1839 * current consensus really alter our view of any OR's rate limits? */
1840 connection_or_update_token_buckets(get_connection_array(), options
);
1842 circuit_build_times_new_consensus_params(&circ_times
, current_consensus
);
1845 if (directory_caches_dir_info(options
)) {
1846 dirserv_set_cached_consensus_networkstatus(consensus
,
1853 write_str_to_file(consensus_fname
, consensus
, 0);
1856 /** If a consensus appears more than this many seconds before its declared
1857 * valid-after time, declare that our clock is skewed. */
1858 #define EARLY_CONSENSUS_NOTICE_SKEW 60
1860 if (now
< c
->valid_after
- EARLY_CONSENSUS_NOTICE_SKEW
) {
1861 char tbuf
[ISO_TIME_LEN
+1];
1863 long delta
= now
- c
->valid_after
;
1864 format_iso_time(tbuf
, c
->valid_after
);
1865 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
1866 log_warn(LD_GENERAL
, "Our clock is %s behind the time published in the "
1867 "consensus network status document (%s GMT). Tor needs an "
1868 "accurate clock to work correctly. Please check your time and "
1869 "date settings!", dbuf
, tbuf
);
1870 control_event_general_status(LOG_WARN
,
1871 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta
);
1874 router_dir_info_changed();
1879 networkstatus_vote_free(c
);
1880 tor_free(consensus_fname
);
1881 tor_free(unverified_fname
);
1885 /** Called when we have gotten more certificates: see whether we can
1886 * now verify a pending consensus. */
1888 networkstatus_note_certs_arrived(void)
1891 for (i
=0; i
<N_CONSENSUS_FLAVORS
; ++i
) {
1892 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
1893 if (!waiting
->consensus
)
1895 if (networkstatus_check_consensus_signature(waiting
->consensus
, 0)>=0) {
1896 if (!networkstatus_set_current_consensus(
1898 networkstatus_get_flavor_name(i
),
1899 NSSET_WAS_WAITING_FOR_CERTS
)) {
1900 tor_free(waiting
->body
);
1906 /** If the network-status list has changed since the last time we called this
1907 * function, update the status of every routerinfo from the network-status
1908 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
1909 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
1912 routers_update_all_from_networkstatus(time_t now
, int dir_version
)
1914 routerlist_t
*rl
= router_get_routerlist();
1915 networkstatus_t
*consensus
= networkstatus_get_reasonably_live_consensus(now
,
1918 if (networkstatus_v2_list_has_changed
)
1919 download_status_map_update_from_v2_networkstatus();
1921 if (!consensus
|| dir_version
< 3) /* nothing more we should do */
1924 /* calls router_dir_info_changed() when it's done -- more routers
1925 * might be up or down now, which might affect whether there's enough
1926 * directory info. */
1927 routers_update_status_from_consensus_networkstatus(rl
->routers
, 0);
1929 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
,
1930 ri
->cache_info
.routerlist_index
= ri_sl_idx
);
1931 if (rl
->old_routers
)
1932 signed_descs_update_status_from_consensus_networkstatus(rl
->old_routers
);
1934 if (!have_warned_about_old_version
) {
1935 int is_server
= server_mode(get_options());
1936 version_status_t status
;
1937 const char *recommended
= is_server
?
1938 consensus
->server_versions
: consensus
->client_versions
;
1939 status
= tor_version_is_obsolete(VERSION
, recommended
);
1941 if (status
== VS_RECOMMENDED
) {
1942 log_info(LD_GENERAL
, "The directory authorities say my version is ok.");
1943 } else if (status
== VS_EMPTY
) {
1944 log_info(LD_GENERAL
,
1945 "The directory authorities don't recommend any versions.");
1946 } else if (status
== VS_NEW
|| status
== VS_NEW_IN_SERIES
) {
1947 if (!have_warned_about_new_version
) {
1948 log_notice(LD_GENERAL
, "This version of Tor (%s) is newer than any "
1949 "recommended version%s, according to the directory "
1950 "authorities. Recommended versions are: %s",
1952 status
== VS_NEW_IN_SERIES
? " in its series" : "",
1954 have_warned_about_new_version
= 1;
1955 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1956 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1957 VERSION
, "NEW", recommended
);
1960 log_warn(LD_GENERAL
, "Please upgrade! "
1961 "This version of Tor (%s) is %s, according to the directory "
1962 "authorities. Recommended versions are: %s",
1964 status
== VS_OLD
? "obsolete" : "not recommended",
1966 have_warned_about_old_version
= 1;
1967 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1968 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1969 VERSION
, status
== VS_OLD
? "OBSOLETE" : "UNRECOMMENDED",
1975 /** Update v2_download_status_map to contain an entry for every router
1976 * descriptor listed in the v2 networkstatuses. */
1978 download_status_map_update_from_v2_networkstatus(void)
1980 digestmap_t
*dl_status
;
1981 if (!networkstatus_v2_list
)
1983 if (!v2_download_status_map
)
1984 v2_download_status_map
= digestmap_new();
1986 dl_status
= digestmap_new();
1987 SMARTLIST_FOREACH_BEGIN(networkstatus_v2_list
, networkstatus_v2_t
*, ns
) {
1988 SMARTLIST_FOREACH_BEGIN(ns
->entries
, const routerstatus_t
*, rs
) {
1989 const char *d
= rs
->descriptor_digest
;
1990 download_status_t
*s
;
1991 if (digestmap_get(dl_status
, d
))
1993 if (!(s
= digestmap_remove(v2_download_status_map
, d
))) {
1994 s
= tor_malloc_zero(sizeof(download_status_t
));
1996 digestmap_set(dl_status
, d
, s
);
1997 } SMARTLIST_FOREACH_END(rs
);
1998 } SMARTLIST_FOREACH_END(ns
);
1999 digestmap_free(v2_download_status_map
, _tor_free
);
2000 v2_download_status_map
= dl_status
;
2001 networkstatus_v2_list_has_changed
= 0;
2004 /** Update our view of the list of named servers from the most recently
2005 * retrieved networkstatus consensus. */
2007 routerstatus_list_update_named_server_map(void)
2009 if (!current_consensus
)
2012 strmap_free(named_server_map
, _tor_free
);
2013 named_server_map
= strmap_new();
2014 strmap_free(unnamed_server_map
, NULL
);
2015 unnamed_server_map
= strmap_new();
2016 SMARTLIST_FOREACH_BEGIN(current_consensus
->routerstatus_list
,
2017 const routerstatus_t
*, rs
) {
2019 strmap_set_lc(named_server_map
, rs
->nickname
,
2020 tor_memdup(rs
->identity_digest
, DIGEST_LEN
));
2022 if (rs
->is_unnamed
) {
2023 strmap_set_lc(unnamed_server_map
, rs
->nickname
, (void*)1);
2025 } SMARTLIST_FOREACH_END(rs
);
2028 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
2029 * according to our current consensus networkstatus. May re-order
2030 * <b>routers</b>. */
2032 routers_update_status_from_consensus_networkstatus(smartlist_t
*routers
,
2035 trusted_dir_server_t
*ds
;
2036 const or_options_t
*options
= get_options();
2037 int authdir
= authdir_mode_v2(options
) || authdir_mode_v3(options
);
2038 networkstatus_t
*ns
= current_consensus
;
2039 if (!ns
|| !smartlist_len(ns
->routerstatus_list
))
2041 if (!networkstatus_v2_list
)
2042 networkstatus_v2_list
= smartlist_new();
2044 routers_sort_by_identity(routers
);
2046 SMARTLIST_FOREACH_JOIN(ns
->routerstatus_list
, routerstatus_t
*, rs
,
2047 routers
, routerinfo_t
*, router
,
2048 tor_memcmp(rs
->identity_digest
,
2049 router
->cache_info
.identity_digest
, DIGEST_LEN
),
2052 /* We have a routerstatus for this router. */
2053 const char *digest
= router
->cache_info
.identity_digest
;
2055 ds
= router_get_trusteddirserver_by_digest(digest
);
2057 /* Is it the same descriptor, or only the same identity? */
2058 if (tor_memeq(router
->cache_info
.signed_descriptor_digest
,
2059 rs
->descriptor_digest
, DIGEST_LEN
)) {
2060 if (ns
->valid_until
> router
->cache_info
.last_listed_as_valid_until
)
2061 router
->cache_info
.last_listed_as_valid_until
= ns
->valid_until
;
2065 /* If we _are_ an authority, we should check whether this router
2066 * is one that will cause us to need a reachability test. */
2067 routerinfo_t
*old_router
=
2068 router_get_mutable_by_digest(router
->cache_info
.identity_digest
);
2069 if (old_router
!= router
) {
2070 router
->needs_retest_if_added
=
2071 dirserv_should_launch_reachability_test(router
, old_router
);
2074 if (rs
->is_flagged_running
&& ds
) {
2075 download_status_reset(&ds
->v2_ns_dl_status
);
2077 if (reset_failures
) {
2078 download_status_reset(&rs
->dl_status
);
2080 } SMARTLIST_FOREACH_JOIN_END(rs
, router
);
2082 /* Now update last_listed_as_valid_until from v2 networkstatuses. */
2083 SMARTLIST_FOREACH_BEGIN(networkstatus_v2_list
, networkstatus_v2_t
*, ns
) {
2084 time_t live_until
= ns
->published_on
+ V2_NETWORKSTATUS_ROUTER_LIFETIME
;
2085 SMARTLIST_FOREACH_JOIN(ns
->entries
, const routerstatus_t
*, rs
,
2086 routers
, routerinfo_t
*, ri
,
2087 tor_memcmp(rs
->identity_digest
,
2088 ri
->cache_info
.identity_digest
, DIGEST_LEN
),
2090 if (tor_memeq(ri
->cache_info
.signed_descriptor_digest
,
2091 rs
->descriptor_digest
, DIGEST_LEN
)) {
2092 if (live_until
> ri
->cache_info
.last_listed_as_valid_until
)
2093 ri
->cache_info
.last_listed_as_valid_until
= live_until
;
2095 } SMARTLIST_FOREACH_JOIN_END(rs
, ri
);
2096 } SMARTLIST_FOREACH_END(ns
);
2098 router_dir_info_changed();
2101 /** Given a list of signed_descriptor_t, update their fields (mainly, when
2102 * they were last listed) from the most recent consensus. */
2104 signed_descs_update_status_from_consensus_networkstatus(smartlist_t
*descs
)
2106 networkstatus_t
*ns
= current_ns_consensus
;
2110 if (!ns
->desc_digest_map
) {
2111 char dummy
[DIGEST_LEN
];
2112 /* instantiates the digest map. */
2113 memset(dummy
, 0, sizeof(dummy
));
2114 router_get_consensus_status_by_descriptor_digest(ns
, dummy
);
2116 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, d
,
2118 const routerstatus_t
*rs
= digestmap_get(ns
->desc_digest_map
,
2119 d
->signed_descriptor_digest
);
2121 if (ns
->valid_until
> d
->last_listed_as_valid_until
)
2122 d
->last_listed_as_valid_until
= ns
->valid_until
;
2127 /** Generate networkstatus lines for a single routerstatus_t object, and
2128 * return the result in a newly allocated string. Used only by controller
2129 * interface (for now.) */
2131 networkstatus_getinfo_helper_single(const routerstatus_t
*rs
)
2133 char buf
[RS_ENTRY_LEN
+1];
2134 routerstatus_format_entry(buf
, sizeof(buf
), rs
, NULL
, NS_CONTROL_PORT
);
2135 return tor_strdup(buf
);
2138 /** Alloc and return a string describing routerstatuses for the most
2139 * recent info of each router we know about that is of purpose
2140 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
2142 * Right now this function is oriented toward listing bridges (you
2143 * shouldn't use this for general-purpose routers, since those
2144 * should be listed from the consensus, not from the routers list). */
2146 networkstatus_getinfo_by_purpose(const char *purpose_string
, time_t now
)
2148 time_t cutoff
= now
- ROUTER_MAX_AGE_TO_PUBLISH
;
2150 routerlist_t
*rl
= router_get_routerlist();
2151 smartlist_t
*statuses
;
2152 uint8_t purpose
= router_purpose_from_string(purpose_string
);
2154 int bridge_auth
= authdir_mode_bridge(get_options());
2156 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
2157 log_info(LD_DIR
, "Unrecognized purpose '%s' when listing router statuses.",
2162 statuses
= smartlist_new();
2163 SMARTLIST_FOREACH_BEGIN(rl
->routers
, routerinfo_t
*, ri
) {
2164 node_t
*node
= node_get_mutable_by_id(ri
->cache_info
.identity_digest
);
2167 if (ri
->cache_info
.published_on
< cutoff
)
2169 if (ri
->purpose
!= purpose
)
2171 if (bridge_auth
&& ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
2172 dirserv_set_router_is_running(ri
, now
);
2173 /* then generate and write out status lines for each of them */
2174 set_routerstatus_from_routerinfo(&rs
, node
, ri
, now
, 0, 0, 0, 0);
2175 smartlist_add(statuses
, networkstatus_getinfo_helper_single(&rs
));
2176 } SMARTLIST_FOREACH_END(ri
);
2178 answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2179 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2180 smartlist_free(statuses
);
2184 /** Write out router status entries for all our bridge descriptors. */
2186 networkstatus_dump_bridge_status_to_file(time_t now
)
2188 char *status
= networkstatus_getinfo_by_purpose("bridge", now
);
2189 const or_options_t
*options
= get_options();
2191 tor_asprintf(&fname
, "%s"PATH_SEPARATOR
"networkstatus-bridges",
2192 options
->DataDirectory
);
2193 write_str_to_file(fname
,status
,0);
2198 /* DOCDOC get_net_param_from_list */
2200 get_net_param_from_list(smartlist_t
*net_params
, const char *param_name
,
2201 int32_t default_val
, int32_t min_val
, int32_t max_val
)
2203 int32_t res
= default_val
;
2204 size_t name_len
= strlen(param_name
);
2206 tor_assert(max_val
> min_val
);
2207 tor_assert(min_val
<= default_val
);
2208 tor_assert(max_val
>= default_val
);
2210 SMARTLIST_FOREACH_BEGIN(net_params
, const char *, p
) {
2211 if (!strcmpstart(p
, param_name
) && p
[name_len
] == '=') {
2213 long v
= tor_parse_long(p
+name_len
+1, 10, INT32_MIN
,
2214 INT32_MAX
, &ok
, NULL
);
2220 } SMARTLIST_FOREACH_END(p
);
2222 if (res
< min_val
) {
2223 log_warn(LD_DIR
, "Consensus parameter %s is too small. Got %d, raising to "
2224 "%d.", param_name
, res
, min_val
);
2226 } else if (res
> max_val
) {
2227 log_warn(LD_DIR
, "Consensus parameter %s is too large. Got %d, capping to "
2228 "%d.", param_name
, res
, max_val
);
2235 /** Return the value of a integer parameter from the networkstatus <b>ns</b>
2236 * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
2237 * latest consensus ourselves. Return <b>default_val</b> if no latest
2238 * consensus, or if it has no parameter called <b>param_name</b>.
2239 * Make sure the value parsed from the consensus is at least
2240 * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
2243 networkstatus_get_param(const networkstatus_t
*ns
, const char *param_name
,
2244 int32_t default_val
, int32_t min_val
, int32_t max_val
)
2246 if (!ns
) /* if they pass in null, go find it ourselves */
2247 ns
= networkstatus_get_latest_consensus();
2249 if (!ns
|| !ns
->net_params
)
2252 return get_net_param_from_list(ns
->net_params
, param_name
,
2253 default_val
, min_val
, max_val
);
2256 /** Return the value of a integer bw weight parameter from the networkstatus
2257 * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
2258 * loading the latest consensus ourselves. Return <b>default_val</b> if no
2259 * latest consensus, or if it has no parameter called <b>weight_name</b>. */
2261 networkstatus_get_bw_weight(networkstatus_t
*ns
, const char *weight_name
,
2262 int32_t default_val
)
2266 if (!ns
) /* if they pass in null, go find it ourselves */
2267 ns
= networkstatus_get_latest_consensus();
2269 if (!ns
|| !ns
->weight_params
)
2272 max
= circuit_build_times_get_bw_scale(ns
);
2273 param
= get_net_param_from_list(ns
->weight_params
, weight_name
,
2275 BW_MAX_WEIGHT_SCALE
);
2277 log_warn(LD_DIR
, "Value of consensus weight %s was too large, capping "
2278 "to %d", weight_name
, max
);
2284 /** Return the name of the consensus flavor <b>flav</b> as used to identify
2285 * the flavor in directory documents. */
2287 networkstatus_get_flavor_name(consensus_flavor_t flav
)
2292 case FLAV_MICRODESC
:
2295 tor_fragile_assert();
2300 /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
2301 * or -1 if the flavor is not recognized. */
2303 networkstatus_parse_flavor_name(const char *flavname
)
2305 if (!strcmp(flavname
, "ns"))
2307 else if (!strcmp(flavname
, "microdesc"))
2308 return FLAV_MICRODESC
;
2313 /** If <b>question</b> is a string beginning with "ns/" in a format the
2314 * control interface expects for a GETINFO question, set *<b>answer</b> to a
2315 * newly-allocated string containing networkstatus lines for the appropriate
2316 * ORs. Return 0 on success, -1 on unrecognized question format. */
2318 getinfo_helper_networkstatus(control_connection_t
*conn
,
2319 const char *question
, char **answer
,
2320 const char **errmsg
)
2322 const routerstatus_t
*status
;
2325 if (!current_consensus
) {
2326 *answer
= tor_strdup("");
2330 if (!strcmp(question
, "ns/all")) {
2331 smartlist_t
*statuses
= smartlist_new();
2332 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
2333 const routerstatus_t
*, rs
,
2335 smartlist_add(statuses
, networkstatus_getinfo_helper_single(rs
));
2337 *answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2338 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2339 smartlist_free(statuses
);
2341 } else if (!strcmpstart(question
, "ns/id/")) {
2344 if (base16_decode(d
, DIGEST_LEN
, question
+6, strlen(question
+6))) {
2345 *errmsg
= "Data not decodeable as hex";
2348 status
= router_get_consensus_status_by_id(d
);
2349 } else if (!strcmpstart(question
, "ns/name/")) {
2350 status
= router_get_consensus_status_by_nickname(question
+8, 0);
2351 } else if (!strcmpstart(question
, "ns/purpose/")) {
2352 *answer
= networkstatus_getinfo_by_purpose(question
+11, time(NULL
));
2353 return *answer
? 0 : -1;
2359 *answer
= networkstatus_getinfo_helper_single(status
);
2363 /** Free all storage held locally in this module. */
2365 networkstatus_free_all(void)
2368 if (networkstatus_v2_list
) {
2369 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
2370 networkstatus_v2_free(ns
));
2371 smartlist_free(networkstatus_v2_list
);
2372 networkstatus_v2_list
= NULL
;
2375 digestmap_free(v2_download_status_map
, _tor_free
);
2376 v2_download_status_map
= NULL
;
2377 networkstatus_vote_free(current_ns_consensus
);
2378 networkstatus_vote_free(current_md_consensus
);
2379 current_md_consensus
= current_ns_consensus
= NULL
;
2381 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2382 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2383 if (waiting
->consensus
) {
2384 networkstatus_vote_free(waiting
->consensus
);
2385 waiting
->consensus
= NULL
;
2387 tor_free(waiting
->body
);
2390 strmap_free(named_server_map
, _tor_free
);
2391 strmap_free(unnamed_server_map
, NULL
);