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-2008, 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
15 /* For tracking v2 networkstatus documents. Only caches do this now. */
17 /** Map from descriptor digest of routers listed in the v2 networkstatus
18 * documents to download_status_t* */
19 static digestmap_t
*v2_download_status_map
= NULL
;
20 /** Global list of all of the current v2 network_status documents that we know
21 * about. This list is kept sorted by published_on. */
22 static smartlist_t
*networkstatus_v2_list
= NULL
;
23 /** True iff any member of networkstatus_v2_list has changed since the last
24 * time we called download_status_map_update_from_v2_networkstatus() */
25 static int networkstatus_v2_list_has_changed
= 0;
27 /** Map from lowercase nickname to identity digest of named server, if any. */
28 static strmap_t
*named_server_map
= NULL
;
29 /** Map from lowercase nickname to (void*)1 for all names that are listed
30 * as unnamed for some server in the consensus. */
31 static strmap_t
*unnamed_server_map
= NULL
;
33 /** Most recently received and validated v3 consensus network status. */
34 static networkstatus_t
*current_consensus
= NULL
;
36 /** A v3 consensus networkstatus that we've received, but which we don't
37 * have enough certificates to be happy about. */
38 static networkstatus_t
*consensus_waiting_for_certs
= NULL
;
39 /** The encoded version of consensus_waiting_for_certs. */
40 static char *consensus_waiting_for_certs_body
= NULL
;
41 /** When did we set the current value of consensus_waiting_for_certs? If this
42 * is too recent, we shouldn't try to fetch a new consensus for a little while,
43 * to give ourselves time to get certificates for this one. */
44 static time_t consensus_waiting_for_certs_set_at
= 0;
45 /** Set to 1 if we've been holding on to consensus_waiting_for_certs so long
46 * that we should treat it as maybe being bad. */
47 static int consensus_waiting_for_certs_dl_failed
= 0;
49 /** The last time we tried to download a networkstatus, or 0 for "never". We
50 * use this to rate-limit download attempts for directory caches (including
51 * mirrors). Clients don't use this now. */
52 static time_t last_networkstatus_download_attempted
= 0;
54 /** A time before which we shouldn't try to replace the current consensus:
55 * this will be at some point after the next consensus becomes valid, but
56 * before the current consensus becomes invalid. */
57 static time_t time_to_download_next_consensus
= 0;
58 /** Download status for the current consensus networkstatus. */
59 static download_status_t consensus_dl_status
= { 0, 0, DL_SCHED_CONSENSUS
};
61 /** True iff we have logged a warning about this OR's version being older than
62 * listed by the authorities. */
63 static int have_warned_about_old_version
= 0;
64 /** True iff we have logged a warning about this OR's version being newer than
65 * listed by the authorities. */
66 static int have_warned_about_new_version
= 0;
68 static void download_status_map_update_from_v2_networkstatus(void);
69 static void routerstatus_list_update_named_server_map(void);
71 /** Forget that we've warned about anything networkstatus-related, so we will
72 * give fresh warnings if the same behavior happens again. */
74 networkstatus_reset_warnings(void)
76 if (current_consensus
) {
77 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
79 rs
->name_lookup_warned
= 0);
82 have_warned_about_old_version
= 0;
83 have_warned_about_new_version
= 0;
86 /** Reset the descriptor download failure count on all networkstatus docs, so
87 * that we can retry any long-failed documents immediately.
90 networkstatus_reset_download_failures(void)
92 const smartlist_t
*networkstatus_v2_list
= networkstatus_get_v2_list();
93 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
94 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
96 if (!router_get_by_descriptor_digest(rs
->descriptor_digest
))
97 rs
->need_to_mirror
= 1;
100 download_status_reset(&consensus_dl_status
);
101 if (v2_download_status_map
) {
102 digestmap_iter_t
*iter
;
103 digestmap_t
*map
= v2_download_status_map
;
106 download_status_t
*dls
;
107 for (iter
= digestmap_iter_init(map
); !digestmap_iter_done(iter
);
108 iter
= digestmap_iter_next(map
, iter
) ) {
109 digestmap_iter_get(iter
, &key
, &val
);
111 download_status_reset(dls
);
116 /** Repopulate our list of network_status_t objects from the list cached on
117 * disk. Return 0 on success, -1 on failure. */
119 router_reload_v2_networkstatus(void)
121 smartlist_t
*entries
;
124 char *filename
= get_datadir_fname("cached-status");
125 int maybe_delete
= !directory_caches_v2_dir_info(get_options());
126 time_t now
= time(NULL
);
127 if (!networkstatus_v2_list
)
128 networkstatus_v2_list
= smartlist_create();
130 entries
= tor_listdir(filename
);
131 if (!entries
) { /* dir doesn't exist */
134 } else if (!smartlist_len(entries
) && maybe_delete
) {
137 smartlist_free(entries
);
141 SMARTLIST_FOREACH(entries
, const char *, fn
, {
142 char buf
[DIGEST_LEN
];
144 filename
= get_datadir_fname2("cached-status", fn
);
145 remove_file_if_very_old(filename
, now
);
149 if (strlen(fn
) != HEX_DIGEST_LEN
||
150 base16_decode(buf
, sizeof(buf
), fn
, strlen(fn
))) {
152 "Skipping cached-status file with unexpected name \"%s\"",fn
);
155 filename
= get_datadir_fname2("cached-status", fn
);
156 s
= read_file_to_str(filename
, 0, &st
);
158 if (router_set_networkstatus_v2(s
, st
.st_mtime
, NS_FROM_CACHE
,
160 log_warn(LD_FS
, "Couldn't load networkstatus from \"%s\"",filename
);
166 SMARTLIST_FOREACH(entries
, char *, fn
, tor_free(fn
));
167 smartlist_free(entries
);
168 networkstatus_v2_list_clean(time(NULL
));
169 routers_update_all_from_networkstatus(time(NULL
), 2);
173 /** Read the cached v3 consensus networkstatus from the disk. */
175 router_reload_consensus_networkstatus(void)
180 or_options_t
*options
= get_options();
181 const unsigned int flags
= NSSET_FROM_CACHE
| NSSET_DONT_DOWNLOAD_CERTS
;
183 /* FFFF Suppress warnings if cached consensus is bad? */
185 filename
= get_datadir_fname("cached-consensus");
186 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
188 if (networkstatus_set_current_consensus(s
, flags
) < -1) {
189 log_warn(LD_FS
, "Couldn't load consensus networkstatus from \"%s\"",
196 filename
= get_datadir_fname("unverified-consensus");
197 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
199 if (networkstatus_set_current_consensus(s
,
200 flags
|NSSET_WAS_WAITING_FOR_CERTS
)) {
201 log_info(LD_FS
, "Couldn't load consensus networkstatus from \"%s\"",
208 if (!current_consensus
||
209 (stat(options
->FallbackNetworkstatusFile
, &st
)==0 &&
210 st
.st_mtime
> current_consensus
->valid_after
)) {
211 s
= read_file_to_str(options
->FallbackNetworkstatusFile
,
212 RFTS_IGNORE_MISSING
, NULL
);
214 if (networkstatus_set_current_consensus(s
,
215 flags
|NSSET_ACCEPT_OBSOLETE
)) {
216 log_info(LD_FS
, "Couldn't load consensus networkstatus from \"%s\"",
217 options
->FallbackNetworkstatusFile
);
220 "Loaded fallback consensus networkstatus from \"%s\"",
221 options
->FallbackNetworkstatusFile
);
227 if (!current_consensus
) {
228 if (!named_server_map
)
229 named_server_map
= strmap_new();
230 if (!unnamed_server_map
)
231 unnamed_server_map
= strmap_new();
234 update_certificate_downloads(time(NULL
));
236 routers_update_all_from_networkstatus(time(NULL
), 3);
241 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
243 vote_routerstatus_free(vote_routerstatus_t
*rs
)
245 tor_free(rs
->version
);
246 tor_free(rs
->status
.exitsummary
);
250 /** Free all storage held by the routerstatus object <b>rs</b>. */
252 routerstatus_free(routerstatus_t
*rs
)
254 tor_free(rs
->exitsummary
);
258 /** Free all storage held by the networkstatus object <b>ns</b>. */
260 networkstatus_v2_free(networkstatus_v2_t
*ns
)
262 tor_free(ns
->source_address
);
263 tor_free(ns
->contact
);
265 crypto_free_pk_env(ns
->signing_key
);
266 tor_free(ns
->client_versions
);
267 tor_free(ns
->server_versions
);
269 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
270 routerstatus_free(rs
));
271 smartlist_free(ns
->entries
);
276 /** Clear all storage held in <b>ns</b>. */
278 networkstatus_vote_free(networkstatus_t
*ns
)
283 tor_free(ns
->client_versions
);
284 tor_free(ns
->server_versions
);
285 if (ns
->known_flags
) {
286 SMARTLIST_FOREACH(ns
->known_flags
, char *, c
, tor_free(c
));
287 smartlist_free(ns
->known_flags
);
289 if (ns
->supported_methods
) {
290 SMARTLIST_FOREACH(ns
->supported_methods
, char *, c
, tor_free(c
));
291 smartlist_free(ns
->supported_methods
);
294 SMARTLIST_FOREACH(ns
->voters
, networkstatus_voter_info_t
*, voter
,
296 tor_free(voter
->nickname
);
297 tor_free(voter
->address
);
298 tor_free(voter
->contact
);
299 tor_free(voter
->signature
);
302 smartlist_free(ns
->voters
);
305 authority_cert_free(ns
->cert
);
307 if (ns
->routerstatus_list
) {
308 if (ns
->type
== NS_TYPE_VOTE
|| ns
->type
== NS_TYPE_OPINION
) {
309 SMARTLIST_FOREACH(ns
->routerstatus_list
, vote_routerstatus_t
*, rs
,
310 vote_routerstatus_free(rs
));
312 SMARTLIST_FOREACH(ns
->routerstatus_list
, routerstatus_t
*, rs
,
313 routerstatus_free(rs
));
316 smartlist_free(ns
->routerstatus_list
);
318 if (ns
->desc_digest_map
)
319 digestmap_free(ns
->desc_digest_map
, NULL
);
321 memset(ns
, 11, sizeof(*ns
));
325 /** Return the voter info from <b>vote</b> for the voter whose identity digest
326 * is <b>identity</b>, or NULL if no such voter is associated with
328 networkstatus_voter_info_t
*
329 networkstatus_get_voter_by_id(networkstatus_t
*vote
,
330 const char *identity
)
332 if (!vote
|| !vote
->voters
)
334 SMARTLIST_FOREACH(vote
->voters
, networkstatus_voter_info_t
*, voter
,
335 if (!memcmp(voter
->identity_digest
, identity
, DIGEST_LEN
))
340 /** Check whether the signature on <b>voter</b> is correctly signed by
341 * the signing key of <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
342 * signing key; otherwise set the good_signature or bad_signature flag on
343 * <b>voter</b>, and return 0. */
344 /* (private; exposed for testing.) */
346 networkstatus_check_voter_signature(networkstatus_t
*consensus
,
347 networkstatus_voter_info_t
*voter
,
348 authority_cert_t
*cert
)
352 size_t signed_digest_len
;
353 if (crypto_pk_get_digest(cert
->signing_key
, d
)<0)
355 if (memcmp(voter
->signing_key_digest
, d
, DIGEST_LEN
))
357 signed_digest_len
= crypto_pk_keysize(cert
->signing_key
);
358 signed_digest
= tor_malloc(signed_digest_len
);
359 if (crypto_pk_public_checksig(cert
->signing_key
,
362 voter
->signature_len
) != DIGEST_LEN
||
363 memcmp(signed_digest
, consensus
->networkstatus_digest
, DIGEST_LEN
)) {
364 log_warn(LD_DIR
, "Got a bad signature on a networkstatus vote");
365 voter
->bad_signature
= 1;
367 voter
->good_signature
= 1;
369 tor_free(signed_digest
);
373 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
374 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
375 * signature from every recognized authority on it, 0 if there are
376 * enough good signatures from recognized authorities on it, -1 if we might
377 * get enough good signatures by fetching missing certificates, and -2
378 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
379 * about every problem; if warn is at least 1, warn only if we can't get
380 * enough signatures; if warn is negative, log nothing at all. */
382 networkstatus_check_consensus_signature(networkstatus_t
*consensus
,
386 int n_missing_key
= 0;
389 int n_no_signature
= 0;
390 int n_v3_authorities
= get_n_authorities(V3_AUTHORITY
);
391 int n_required
= n_v3_authorities
/2 + 1;
392 smartlist_t
*need_certs_from
= smartlist_create();
393 smartlist_t
*unrecognized
= smartlist_create();
394 smartlist_t
*missing_authorities
= smartlist_create();
396 time_t now
= time(NULL
);
398 tor_assert(consensus
->type
== NS_TYPE_CONSENSUS
);
400 SMARTLIST_FOREACH(consensus
->voters
, networkstatus_voter_info_t
*, voter
,
402 if (!voter
->good_signature
&& !voter
->bad_signature
&& voter
->signature
) {
403 /* we can try to check the signature. */
404 int is_v3_auth
= trusteddirserver_get_by_v3_auth_digest(
405 voter
->identity_digest
) != NULL
;
406 authority_cert_t
*cert
=
407 authority_cert_get_by_digests(voter
->identity_digest
,
408 voter
->signing_key_digest
);
410 smartlist_add(unrecognized
, voter
);
413 } else if (!cert
|| cert
->expires
< now
) {
414 smartlist_add(need_certs_from
, voter
);
418 if (networkstatus_check_voter_signature(consensus
, voter
, cert
) < 0) {
419 smartlist_add(need_certs_from
, voter
);
424 if (voter
->good_signature
)
426 else if (voter
->bad_signature
)
432 /* Now see whether we're missing any voters entirely. */
433 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
434 trusted_dir_server_t
*, ds
,
436 if ((ds
->type
& V3_AUTHORITY
) &&
437 !networkstatus_get_voter_by_id(consensus
, ds
->v3_identity_digest
))
438 smartlist_add(missing_authorities
, ds
);
441 if (warn
> 1 || (warn
>= 0 && n_good
< n_required
))
447 SMARTLIST_FOREACH(unrecognized
, networkstatus_voter_info_t
*, voter
,
449 log_info(LD_DIR
, "Consensus includes unrecognized authority '%s' "
450 "at %s:%d (contact %s; identity %s)",
451 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
452 voter
->contact
?voter
->contact
:"n/a",
453 hex_str(voter
->identity_digest
, DIGEST_LEN
));
455 SMARTLIST_FOREACH(need_certs_from
, networkstatus_voter_info_t
*, voter
,
457 log_info(LD_DIR
, "Looks like we need to download a new certificate "
458 "from authority '%s' at %s:%d (contact %s; identity %s)",
459 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
460 voter
->contact
?voter
->contact
:"n/a",
461 hex_str(voter
->identity_digest
, DIGEST_LEN
));
463 SMARTLIST_FOREACH(missing_authorities
, trusted_dir_server_t
*, ds
,
465 log_info(LD_DIR
, "Consensus does not include configured "
466 "authority '%s' at %s:%d (identity %s)",
467 ds
->nickname
, ds
->address
, (int)ds
->dir_port
,
468 hex_str(ds
->v3_identity_digest
, DIGEST_LEN
));
470 log(severity
, LD_DIR
,
471 "%d unknown, %d missing key, %d good, %d bad, %d no signature, "
472 "%d required", n_unknown
, n_missing_key
, n_good
, n_bad
,
473 n_no_signature
, n_required
);
476 smartlist_free(unrecognized
);
477 smartlist_free(need_certs_from
);
478 smartlist_free(missing_authorities
);
480 if (n_good
== n_v3_authorities
)
482 else if (n_good
>= n_required
)
484 else if (n_good
+ n_missing_key
>= n_required
)
490 /** Helper: return a newly allocated string containing the name of the filename
491 * where we plan to cache the network status with the given identity digest. */
493 networkstatus_get_cache_filename(const char *identity_digest
)
495 char fp
[HEX_DIGEST_LEN
+1];
496 base16_encode(fp
, HEX_DIGEST_LEN
+1, identity_digest
, DIGEST_LEN
);
497 return get_datadir_fname2("cached-status", fp
);
500 /** Helper for smartlist_sort: Compare two networkstatus objects by
501 * publication date. */
503 _compare_networkstatus_v2_published_on(const void **_a
, const void **_b
)
505 const networkstatus_v2_t
*a
= *_a
, *b
= *_b
;
506 if (a
->published_on
< b
->published_on
)
508 else if (a
->published_on
> b
->published_on
)
514 /** Add the parsed v2 networkstatus in <b>ns</b> (with original document in
515 * <b>s</b>) to the disk cache (and the in-memory directory server cache) as
518 add_networkstatus_to_cache(const char *s
,
519 v2_networkstatus_source_t source
,
520 networkstatus_v2_t
*ns
)
522 if (source
!= NS_FROM_CACHE
) {
523 char *fn
= networkstatus_get_cache_filename(ns
->identity_digest
);
524 if (write_str_to_file(fn
, s
, 0)<0) {
525 log_notice(LD_FS
, "Couldn't write cached network status to \"%s\"", fn
);
530 if (directory_caches_v2_dir_info(get_options()))
531 dirserv_set_cached_networkstatus_v2(s
,
538 /** How far in the future do we allow a network-status to get before removing
540 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
542 /** Given a string <b>s</b> containing a network status that we received at
543 * <b>arrived_at</b> from <b>source</b>, try to parse it, see if we want to
544 * store it, and put it into our cache as necessary.
546 * If <b>source</b> is NS_FROM_DIR or NS_FROM_CACHE, do not replace our
547 * own networkstatus_t (if we're an authoritative directory server).
549 * If <b>source</b> is NS_FROM_CACHE, do not write our networkstatus_t to the
552 * If <b>requested_fingerprints</b> is provided, it must contain a list of
553 * uppercased identity fingerprints. Do not update any networkstatus whose
554 * fingerprint is not on the list; after updating a networkstatus, remove its
555 * fingerprint from the list.
557 * Return 0 on success, -1 on failure.
559 * Callers should make sure that routers_update_all_from_networkstatus() is
560 * invoked after this function succeeds.
563 router_set_networkstatus_v2(const char *s
, time_t arrived_at
,
564 v2_networkstatus_source_t source
,
565 smartlist_t
*requested_fingerprints
)
567 networkstatus_v2_t
*ns
;
571 trusted_dir_server_t
*trusted_dir
= NULL
;
572 const char *source_desc
= NULL
;
573 char fp
[HEX_DIGEST_LEN
+1];
574 char published
[ISO_TIME_LEN
+1];
576 if (!directory_caches_v2_dir_info(get_options()))
577 return 0; /* Don't bother storing it. */
579 ns
= networkstatus_v2_parse_from_string(s
);
581 log_warn(LD_DIR
, "Couldn't parse network status.");
584 base16_encode(fp
, HEX_DIGEST_LEN
+1, ns
->identity_digest
, DIGEST_LEN
);
586 router_get_trusteddirserver_by_digest(ns
->identity_digest
)) ||
587 !(trusted_dir
->type
& V2_AUTHORITY
)) {
588 log_info(LD_DIR
, "Network status was signed, but not by an authoritative "
589 "directory we recognize.");
592 source_desc
= trusted_dir
->description
;
595 if (arrived_at
> now
)
598 ns
->received_on
= arrived_at
;
600 format_iso_time(published
, ns
->published_on
);
602 if (ns
->published_on
> now
+ NETWORKSTATUS_ALLOW_SKEW
) {
604 long delta
= now
- ns
->published_on
;
605 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
606 log_warn(LD_GENERAL
, "Network status from %s was published %s in the "
607 "future (%s GMT). Check your time and date settings! "
609 source_desc
, dbuf
, published
);
610 control_event_general_status(LOG_WARN
,
611 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=NETWORKSTATUS:%s:%d",
612 delta
, ns
->source_address
, ns
->source_dirport
);
616 if (!networkstatus_v2_list
)
617 networkstatus_v2_list
= smartlist_create();
619 if ( (source
== NS_FROM_DIR_BY_FP
|| source
== NS_FROM_DIR_ALL
) &&
620 router_digest_is_me(ns
->identity_digest
)) {
621 /* Don't replace our own networkstatus when we get it from somebody else.*/
622 networkstatus_v2_free(ns
);
626 if (requested_fingerprints
) {
627 if (smartlist_string_isin(requested_fingerprints
, fp
)) {
628 smartlist_string_remove(requested_fingerprints
, fp
);
630 if (source
!= NS_FROM_DIR_ALL
) {
632 smartlist_join_strings(requested_fingerprints
," ",0,NULL
);
634 "We received a network status with a fingerprint (%s) that we "
635 "never requested. (We asked for: %s.) Dropping.",
645 /* We got a non-trusted networkstatus, and we're a directory cache.
646 * This means that we asked an authority, and it told us about another
647 * authority we didn't recognize. */
649 "We do not recognize authority (%s) but we are willing "
651 add_networkstatus_to_cache(s
, source
, ns
);
652 networkstatus_v2_free(ns
);
658 for (i
=0; i
< smartlist_len(networkstatus_v2_list
); ++i
) {
659 networkstatus_v2_t
*old_ns
= smartlist_get(networkstatus_v2_list
, i
);
661 if (!memcmp(old_ns
->identity_digest
, ns
->identity_digest
, DIGEST_LEN
)) {
662 if (!memcmp(old_ns
->networkstatus_digest
,
663 ns
->networkstatus_digest
, DIGEST_LEN
)) {
664 /* Same one we had before. */
665 networkstatus_v2_free(ns
);
666 tor_assert(trusted_dir
);
668 "Not replacing network-status from %s (published %s); "
669 "we already have it.",
670 trusted_dir
->description
, published
);
671 if (old_ns
->received_on
< arrived_at
) {
672 if (source
!= NS_FROM_CACHE
) {
674 fn
= networkstatus_get_cache_filename(old_ns
->identity_digest
);
675 /* We use mtime to tell when it arrived, so update that. */
679 old_ns
->received_on
= arrived_at
;
681 download_status_failed(&trusted_dir
->v2_ns_dl_status
, 0);
683 } else if (old_ns
->published_on
>= ns
->published_on
) {
684 char old_published
[ISO_TIME_LEN
+1];
685 format_iso_time(old_published
, old_ns
->published_on
);
686 tor_assert(trusted_dir
);
688 "Not replacing network-status from %s (published %s);"
689 " we have a newer one (published %s) for this authority.",
690 trusted_dir
->description
, published
,
692 networkstatus_v2_free(ns
);
693 download_status_failed(&trusted_dir
->v2_ns_dl_status
, 0);
696 networkstatus_v2_free(old_ns
);
697 smartlist_set(networkstatus_v2_list
, i
, ns
);
704 if (source
!= NS_FROM_CACHE
&& trusted_dir
) {
705 download_status_reset(&trusted_dir
->v2_ns_dl_status
);
709 smartlist_add(networkstatus_v2_list
, ns
);
711 /** Retain any routerinfo mentioned in a V2 networkstatus for at least this
713 #define V2_NETWORKSTATUS_ROUTER_LIFETIME (3*60*60)
716 time_t live_until
= ns
->published_on
+ V2_NETWORKSTATUS_ROUTER_LIFETIME
;
717 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
719 signed_descriptor_t
*sd
=
720 router_get_by_descriptor_digest(rs
->descriptor_digest
);
722 if (sd
->last_listed_as_valid_until
< live_until
)
723 sd
->last_listed_as_valid_until
= live_until
;
725 rs
->need_to_mirror
= 1;
730 log_info(LD_DIR
, "Setting networkstatus %s %s (published %s)",
731 source
== NS_FROM_CACHE
?"cached from":
732 ((source
== NS_FROM_DIR_BY_FP
|| source
== NS_FROM_DIR_ALL
) ?
733 "downloaded from":"generated for"),
734 trusted_dir
->description
, published
);
735 networkstatus_v2_list_has_changed
= 1;
737 smartlist_sort(networkstatus_v2_list
,
738 _compare_networkstatus_v2_published_on
);
741 add_networkstatus_to_cache(s
, source
, ns
);
746 /** Remove all very-old network_status_t objects from memory and from the
749 networkstatus_v2_list_clean(time_t now
)
752 if (!networkstatus_v2_list
)
755 for (i
= 0; i
< smartlist_len(networkstatus_v2_list
); ++i
) {
756 networkstatus_v2_t
*ns
= smartlist_get(networkstatus_v2_list
, i
);
758 if (ns
->published_on
+ MAX_NETWORKSTATUS_AGE
> now
)
760 /* Okay, this one is too old. Remove it from the list, and delete it
762 smartlist_del(networkstatus_v2_list
, i
--);
763 fname
= networkstatus_get_cache_filename(ns
->identity_digest
);
764 if (file_status(fname
) == FN_FILE
) {
765 log_info(LD_DIR
, "Removing too-old networkstatus in %s", fname
);
769 if (directory_caches_v2_dir_info(get_options())) {
770 dirserv_set_cached_networkstatus_v2(NULL
, ns
->identity_digest
, 0);
772 networkstatus_v2_free(ns
);
775 /* And now go through the directory cache for any cached untrusted
776 * networkstatuses and other network info. */
777 dirserv_clear_old_networkstatuses(now
- MAX_NETWORKSTATUS_AGE
);
778 dirserv_clear_old_v1_info(now
);
781 /** Helper for bsearching a list of routerstatus_t pointers: compare a
782 * digest in the key to the identity digest of a routerstatus_t. */
784 _compare_digest_to_routerstatus_entry(const void *_key
, const void **_member
)
786 const char *key
= _key
;
787 const routerstatus_t
*rs
= *_member
;
788 return memcmp(key
, rs
->identity_digest
, DIGEST_LEN
);
791 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
792 * NULL if none was found. */
794 networkstatus_v2_find_entry(networkstatus_v2_t
*ns
, const char *digest
)
796 return smartlist_bsearch(ns
->entries
, digest
,
797 _compare_digest_to_routerstatus_entry
);
800 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
801 * NULL if none was found. */
803 networkstatus_vote_find_entry(networkstatus_t
*ns
, const char *digest
)
805 return smartlist_bsearch(ns
->routerstatus_list
, digest
,
806 _compare_digest_to_routerstatus_entry
);
809 /*XXXX make this static once functions are moved into this file. */
810 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
811 * <b>digest</b>. Return value and set *<b>found_out</b> as for
812 * smartlist_bsearch_idx(). */
814 networkstatus_vote_find_entry_idx(networkstatus_t
*ns
,
815 const char *digest
, int *found_out
)
817 return smartlist_bsearch_idx(ns
->routerstatus_list
, digest
,
818 _compare_digest_to_routerstatus_entry
,
822 /** Return a list of the v2 networkstatus documents. */
824 networkstatus_get_v2_list(void)
826 if (!networkstatus_v2_list
)
827 networkstatus_v2_list
= smartlist_create();
828 return networkstatus_v2_list
;
831 /** Return the consensus view of the status of the router whose current
832 * <i>descriptor</i> digest is <b>digest</b>, or NULL if no such router is
835 router_get_consensus_status_by_descriptor_digest(const char *digest
)
837 if (!current_consensus
) return NULL
;
838 if (!current_consensus
->desc_digest_map
) {
839 digestmap_t
* m
= current_consensus
->desc_digest_map
= digestmap_new();
840 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
841 routerstatus_t
*, rs
,
843 digestmap_set(m
, rs
->descriptor_digest
, rs
);
846 return digestmap_get(current_consensus
->desc_digest_map
, digest
);
849 /** Given the digest of a router descriptor, return its current download
850 * status, or NULL if the digest is unrecognized. */
852 router_get_dl_status_by_descriptor_digest(const char *d
)
855 if ((rs
= router_get_consensus_status_by_descriptor_digest(d
)))
856 return &rs
->dl_status
;
857 if (v2_download_status_map
)
858 return digestmap_get(v2_download_status_map
, d
);
863 /** Return the consensus view of the status of the router whose identity
864 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
866 router_get_consensus_status_by_id(const char *digest
)
868 if (!current_consensus
)
870 return smartlist_bsearch(current_consensus
->routerstatus_list
, digest
,
871 _compare_digest_to_routerstatus_entry
);
874 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
875 * the corresponding routerstatus_t, or NULL if none exists. Warn the
876 * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
877 * nickname, but the Named flag isn't set for that router. */
879 router_get_consensus_status_by_nickname(const char *nickname
,
882 char digest
[DIGEST_LEN
];
883 routerstatus_t
*best
=NULL
;
884 smartlist_t
*matches
=NULL
;
885 const char *named_id
=NULL
;
887 if (!current_consensus
|| !nickname
)
890 /* Is this name really a hexadecimal identity digest? */
891 if (nickname
[0] == '$') {
892 if (base16_decode(digest
, DIGEST_LEN
, nickname
+1, strlen(nickname
+1))<0)
894 return networkstatus_vote_find_entry(current_consensus
, digest
);
895 } else if (strlen(nickname
) == HEX_DIGEST_LEN
&&
896 (base16_decode(digest
, DIGEST_LEN
, nickname
, strlen(nickname
))==0)) {
897 return networkstatus_vote_find_entry(current_consensus
, digest
);
900 /* Is there a server that is Named with this name? */
901 if (named_server_map
)
902 named_id
= strmap_get_lc(named_server_map
, nickname
);
904 return networkstatus_vote_find_entry(current_consensus
, named_id
);
906 /* Okay; is this name listed as Unnamed? */
907 if (unnamed_server_map
&&
908 strmap_get_lc(unnamed_server_map
, nickname
)) {
909 log_info(LD_GENERAL
, "The name %s is listed as Unnamed; it is not the "
910 "canonical name of any server we know.", escaped(nickname
));
914 /* This name is not canonical for any server; go through the list and
915 * see who it matches. */
916 /*XXXX This is inefficient; optimize it if it matters. */
917 matches
= smartlist_create();
918 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
919 routerstatus_t
*, lrs
,
921 if (!strcasecmp(lrs
->nickname
, nickname
)) {
923 tor_fragile_assert() /* This should never happen. */
924 smartlist_free(matches
);
927 if (lrs
->is_unnamed
) {
928 tor_fragile_assert(); /* nor should this. */
929 smartlist_clear(matches
);
933 smartlist_add(matches
, lrs
);
939 if (smartlist_len(matches
)>1 && warn_if_unnamed
) {
941 SMARTLIST_FOREACH(matches
, routerstatus_t
*, lrs
,
943 if (! lrs
->name_lookup_warned
) {
944 lrs
->name_lookup_warned
=1;
949 log_warn(LD_CONFIG
,"There are multiple matches for the nickname \"%s\","
950 " but none is listed as named by the directory authorites. "
951 "Choosing one arbitrarily.", nickname
);
953 } else if (warn_if_unnamed
&& best
&& !best
->name_lookup_warned
) {
954 char fp
[HEX_DIGEST_LEN
+1];
955 base16_encode(fp
, sizeof(fp
),
956 best
->identity_digest
, DIGEST_LEN
);
958 "When looking up a status, you specified a server \"%s\" by name, "
959 "but the directory authorities do not have any key registered for "
960 "this nickname -- so it could be used by any server, "
961 "not just the one you meant. "
962 "To make sure you get the same server in the future, refer to "
963 "it by key, as \"$%s\".", nickname
, fp
);
964 best
->name_lookup_warned
= 1;
966 smartlist_free(matches
);
970 /** Return the identity digest that's mapped to officially by
971 * <b>nickname</b>. */
973 networkstatus_get_router_digest_by_nickname(const char *nickname
)
975 if (!named_server_map
)
977 return strmap_get_lc(named_server_map
, nickname
);
980 /** Return true iff <b>nickname</b> is disallowed from being the nickname
983 networkstatus_nickname_is_unnamed(const char *nickname
)
985 if (!unnamed_server_map
)
987 return strmap_get_lc(unnamed_server_map
, nickname
) != NULL
;
990 /** How frequently do directory authorities re-download fresh networkstatus
992 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
994 /** How frequently do non-authority directory caches re-download fresh
995 * networkstatus documents? */
996 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
998 /** We are a directory server, and so cache network_status documents.
999 * Initiate downloads as needed to update them. For v2 authorities,
1000 * this means asking each trusted directory for its network-status.
1001 * For caches, this means asking a random v2 authority for all
1005 update_v2_networkstatus_cache_downloads(time_t now
)
1007 int authority
= authdir_mode_v2(get_options());
1009 authority
? AUTHORITY_NS_CACHE_INTERVAL
: NONAUTHORITY_NS_CACHE_INTERVAL
;
1010 const smartlist_t
*trusted_dir_servers
= router_get_trusted_dir_servers();
1012 if (last_networkstatus_download_attempted
+ interval
>= now
)
1015 last_networkstatus_download_attempted
= now
;
1018 /* An authority launches a separate connection for everybody. */
1019 SMARTLIST_FOREACH_BEGIN(trusted_dir_servers
, trusted_dir_server_t
*, ds
)
1021 char resource
[HEX_DIGEST_LEN
+6]; /* fp/hexdigit.z\0 */
1023 if (!(ds
->type
& V2_AUTHORITY
))
1025 if (router_digest_is_me(ds
->digest
))
1027 tor_addr_from_ipv4h(&addr
, ds
->addr
);
1028 /* Is this quite sensible with IPv6 or multiple addresses? */
1029 if (connection_get_by_type_addr_port_purpose(
1030 CONN_TYPE_DIR
, &addr
, ds
->dir_port
,
1031 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
)) {
1032 /* XXX the above dir_port won't be accurate if we're
1033 * doing a tunneled conn. In that case it should be or_port.
1034 * How to guess from here? Maybe make the function less general
1035 * and have it know that it's looking for dir conns. -RD */
1036 /* Only directory caches download v2 networkstatuses, and they
1037 * don't use tunneled connections. I think it's okay to ignore
1041 strlcpy(resource
, "fp/", sizeof(resource
));
1042 base16_encode(resource
+3, sizeof(resource
)-3, ds
->digest
, DIGEST_LEN
);
1043 strlcat(resource
, ".z", sizeof(resource
));
1044 directory_initiate_command_routerstatus(
1045 &ds
->fake_status
, DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
,
1046 ROUTER_PURPOSE_GENERAL
,
1047 0, /* Not private */
1049 NULL
, 0 /* No payload. */,
1052 SMARTLIST_FOREACH_END(ds
);
1054 /* A non-authority cache launches one connection to a random authority. */
1055 /* (Check whether we're currently fetching network-status objects.) */
1056 if (!connection_get_by_type_purpose(CONN_TYPE_DIR
,
1057 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
))
1058 directory_get_from_dirserver(DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
,
1059 ROUTER_PURPOSE_GENERAL
, "all.z",
1060 PDS_RETRY_IF_NO_SERVERS
);
1064 /** How many times will we try to fetch a consensus before we give up? */
1065 #define CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES 8
1066 /** How long will we hang onto a possibly live consensus for which we're
1067 * fetching certs before we check whether there is a better one? */
1068 #define DELAY_WHILE_FETCHING_CERTS (20*60)
1070 /** If we want to download a fresh consensus, launch a new download as
1073 update_consensus_networkstatus_downloads(time_t now
)
1075 or_options_t
*options
= get_options();
1076 if (!networkstatus_get_live_consensus(now
))
1077 time_to_download_next_consensus
= now
; /* No live consensus? Get one now!*/
1078 if (time_to_download_next_consensus
> now
)
1079 return; /* Wait until the current consensus is older. */
1080 if (authdir_mode_v3(options
))
1081 return; /* Authorities never fetch a consensus */
1082 if (!download_status_is_ready(&consensus_dl_status
, now
,
1083 CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES
))
1084 return; /* We failed downloading a consensus too recently. */
1085 if (connection_get_by_type_purpose(CONN_TYPE_DIR
,
1086 DIR_PURPOSE_FETCH_CONSENSUS
))
1087 return; /* There's an in-progress download.*/
1089 if (consensus_waiting_for_certs
) {
1090 /* XXXX make sure this doesn't delay sane downloads. */
1091 if (consensus_waiting_for_certs_set_at
+ DELAY_WHILE_FETCHING_CERTS
> now
)
1092 return; /* We're still getting certs for this one. */
1094 if (!consensus_waiting_for_certs_dl_failed
) {
1095 download_status_failed(&consensus_dl_status
, 0);
1096 consensus_waiting_for_certs_dl_failed
=1;
1101 log_info(LD_DIR
, "Launching networkstatus consensus download.");
1102 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
1103 ROUTER_PURPOSE_GENERAL
, NULL
,
1104 PDS_RETRY_IF_NO_SERVERS
);
1107 /** Called when an attempt to download a consensus fails: note that the
1108 * failure occurred, and possibly retry. */
1110 networkstatus_consensus_download_failed(int status_code
)
1112 download_status_failed(&consensus_dl_status
, status_code
);
1113 /* Retry immediately, if appropriate. */
1114 update_consensus_networkstatus_downloads(time(NULL
));
1117 /** How long do we (as a cache) wait after a consensus becomes non-fresh
1118 * before trying to fetch another? */
1119 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
1121 /** Update the time at which we'll consider replacing the current
1124 update_consensus_networkstatus_fetch_time(time_t now
)
1126 or_options_t
*options
= get_options();
1127 networkstatus_t
*c
= networkstatus_get_live_consensus(now
);
1130 long interval
= c
->fresh_until
- c
->valid_after
;
1132 if (directory_fetches_dir_info_early(options
)) {
1133 /* We want to cache the next one at some point after this one
1134 * is no longer fresh... */
1135 start
= c
->fresh_until
+ CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1136 /* But only in the first half-interval after that. */
1137 dl_interval
= interval
/2;
1139 /* We're an ordinary client or a bridge. Give all the caches enough
1140 * time to download the consensus. */
1141 start
= c
->fresh_until
+ (interval
*3)/4;
1142 /* But download the next one well before this one is expired. */
1143 dl_interval
= ((c
->valid_until
- start
) * 7 )/ 8;
1145 /* If we're a bridge user, make use of the numbers we just computed
1146 * to choose the rest of the interval *after* them. */
1147 if (directory_fetches_dir_info_later(options
)) {
1148 /* Give all the *clients* enough time to download the consensus. */
1149 start
= start
+ dl_interval
+ CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1150 /* But try to get it before ours actually expires. */
1151 dl_interval
= (c
->valid_until
- start
) -
1152 CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1155 if (dl_interval
< 1)
1157 /* We must not try to replace c while it's still the most valid: */
1158 tor_assert(c
->fresh_until
< start
);
1159 /* We must download the next one before c is invalid: */
1160 tor_assert(start
+dl_interval
< c
->valid_until
);
1161 time_to_download_next_consensus
= start
+crypto_rand_int((int)dl_interval
);
1163 char tbuf1
[ISO_TIME_LEN
+1];
1164 char tbuf2
[ISO_TIME_LEN
+1];
1165 char tbuf3
[ISO_TIME_LEN
+1];
1166 format_local_iso_time(tbuf1
, c
->fresh_until
);
1167 format_local_iso_time(tbuf2
, c
->valid_until
);
1168 format_local_iso_time(tbuf3
, time_to_download_next_consensus
);
1169 log_info(LD_DIR
, "Live consensus %s the most recent until %s and will "
1170 "expire at %s; fetching the next one at %s.",
1171 (c
->fresh_until
> now
) ? "will be" : "was",
1172 tbuf1
, tbuf2
, tbuf3
);
1175 time_to_download_next_consensus
= now
;
1176 log_info(LD_DIR
, "No live consensus; we should fetch one immediately.");
1181 /** Return 1 if there's a reason we shouldn't try any directory
1182 * fetches yet (e.g. we demand bridges and none are yet known).
1185 should_delay_dir_fetches(or_options_t
*options
)
1187 if (options
->UseBridges
&& !any_bridge_descriptors_known()) {
1188 log_info(LD_DIR
, "delaying dir fetches (no running bridges known)");
1194 /** Launch requests for networkstatus documents and authority certificates as
1197 update_networkstatus_downloads(time_t now
)
1199 or_options_t
*options
= get_options();
1200 if (should_delay_dir_fetches(options
))
1202 if (directory_fetches_dir_info_early(options
))
1203 update_v2_networkstatus_cache_downloads(now
);
1204 update_consensus_networkstatus_downloads(now
);
1205 update_certificate_downloads(now
);
1208 /** Launch requests as appropriate for missing directory authority
1211 update_certificate_downloads(time_t now
)
1213 if (consensus_waiting_for_certs
)
1214 authority_certs_fetch_missing(consensus_waiting_for_certs
, now
);
1216 authority_certs_fetch_missing(current_consensus
, now
);
1219 /** Return 1 if we have a consensus but we don't have enough certificates
1220 * to start using it yet. */
1222 consensus_is_waiting_for_certs(void)
1224 return consensus_waiting_for_certs
? 1 : 0;
1227 /** Return the network status with a given identity digest. */
1228 networkstatus_v2_t
*
1229 networkstatus_v2_get_by_digest(const char *digest
)
1231 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
1233 if (!memcmp(ns
->identity_digest
, digest
, DIGEST_LEN
))
1239 /** Return the most recent consensus that we have downloaded, or NULL if we
1240 * don't have one. */
1242 networkstatus_get_latest_consensus(void)
1244 return current_consensus
;
1247 /** Return the most recent consensus that we have downloaded, or NULL if it is
1248 * no longer live. */
1250 networkstatus_get_live_consensus(time_t now
)
1252 if (current_consensus
&&
1253 current_consensus
->valid_after
<= now
&&
1254 now
<= current_consensus
->valid_until
)
1255 return current_consensus
;
1260 /* XXXX remove this in favor of get_live_consensus. But actually,
1261 * leave something like it for bridge users, who need to not totally
1262 * lose if they spend a while fetching a new consensus. */
1263 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1266 networkstatus_get_reasonably_live_consensus(time_t now
)
1268 #define REASONABLY_LIVE_TIME (24*60*60)
1269 if (current_consensus
&&
1270 current_consensus
->valid_after
<= now
&&
1271 now
<= current_consensus
->valid_until
+REASONABLY_LIVE_TIME
)
1272 return current_consensus
;
1277 /** Given two router status entries for the same router identity, return 1 if
1278 * if the contents have changed between them. Otherwise, return 0. */
1280 routerstatus_has_changed(const routerstatus_t
*a
, const routerstatus_t
*b
)
1282 tor_assert(!memcmp(a
->identity_digest
, b
->identity_digest
, DIGEST_LEN
));
1284 return strcmp(a
->nickname
, b
->nickname
) ||
1285 memcmp(a
->descriptor_digest
, b
->descriptor_digest
, DIGEST_LEN
) ||
1286 a
->addr
!= b
->addr
||
1287 a
->or_port
!= b
->or_port
||
1288 a
->dir_port
!= b
->dir_port
||
1289 a
->is_authority
!= b
->is_authority
||
1290 a
->is_exit
!= b
->is_exit
||
1291 a
->is_stable
!= b
->is_stable
||
1292 a
->is_fast
!= b
->is_fast
||
1293 a
->is_running
!= b
->is_running
||
1294 a
->is_named
!= b
->is_named
||
1295 a
->is_unnamed
!= b
->is_unnamed
||
1296 a
->is_valid
!= b
->is_valid
||
1297 a
->is_v2_dir
!= b
->is_v2_dir
||
1298 a
->is_possible_guard
!= b
->is_possible_guard
||
1299 a
->is_bad_exit
!= b
->is_bad_exit
||
1300 a
->is_bad_directory
!= b
->is_bad_directory
||
1301 a
->is_hs_dir
!= b
->is_hs_dir
||
1302 a
->version_known
!= b
->version_known
||
1303 a
->version_supports_begindir
!= b
->version_supports_begindir
||
1304 a
->version_supports_extrainfo_upload
!=
1305 b
->version_supports_extrainfo_upload
||
1306 a
->version_supports_conditional_consensus
!=
1307 b
->version_supports_conditional_consensus
||
1308 a
->version_supports_v3_dir
!= b
->version_supports_v3_dir
;
1311 /** Notify controllers of any router status entries that changed between
1312 * <b>old_c</b> and <b>new_c</b>. */
1314 notify_control_networkstatus_changed(const networkstatus_t
*old_c
,
1315 const networkstatus_t
*new_c
)
1317 smartlist_t
*changed
;
1321 /* tell the controller exactly which relays are still listed, as well
1322 * as what they're listed as */
1323 control_event_newconsensus(new_c
);
1326 control_event_networkstatus_changed(new_c
->routerstatus_list
);
1329 changed
= smartlist_create();
1331 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1332 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1333 memcmp(rs_old
->identity_digest
,
1334 rs_new
->identity_digest
, DIGEST_LEN
),
1335 smartlist_add(changed
, rs_new
)) {
1336 if (routerstatus_has_changed(rs_old
, rs_new
))
1337 smartlist_add(changed
, rs_new
);
1338 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1340 control_event_networkstatus_changed(changed
);
1341 smartlist_free(changed
);
1344 /** Copy all the ancillary information (like router download status and so on)
1345 * from <b>old_c</b> to <b>new_c</b>. */
1347 networkstatus_copy_old_consensus_info(networkstatus_t
*new_c
,
1348 const networkstatus_t
*old_c
)
1352 if (!old_c
|| !smartlist_len(old_c
->routerstatus_list
))
1355 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1356 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1357 memcmp(rs_old
->identity_digest
,
1358 rs_new
->identity_digest
, DIGEST_LEN
),
1360 /* Okay, so we're looking at the same identity. */
1361 rs_new
->name_lookup_warned
= rs_old
->name_lookup_warned
;
1362 rs_new
->last_dir_503_at
= rs_old
->last_dir_503_at
;
1364 if (!memcmp(rs_old
->descriptor_digest
, rs_new
->descriptor_digest
,
1366 /* And the same descriptor too! */
1367 memcpy(&rs_new
->dl_status
, &rs_old
->dl_status
,sizeof(download_status_t
));
1369 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1372 /** Try to replace the current cached v3 networkstatus with the one in
1373 * <b>consensus</b>. If we don't have enough certificates to validate it,
1374 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1376 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1377 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1378 * already received, but we were waiting for certificates on it. If flags &
1379 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1380 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1381 * consensus, even if it comes from many days in the past.
1383 * Return 0 on success, <0 on failure. On failure, caller should increment
1384 * the failure count as appropriate.
1386 * We return -1 for mild failures that don't need to be reported to the
1387 * user, and -2 for more serious problems.
1390 networkstatus_set_current_consensus(const char *consensus
, unsigned flags
)
1394 time_t now
= time(NULL
);
1395 char *unverified_fname
= NULL
, *consensus_fname
= NULL
;
1396 const unsigned from_cache
= flags
& NSSET_FROM_CACHE
;
1397 const unsigned was_waiting_for_certs
= flags
& NSSET_WAS_WAITING_FOR_CERTS
;
1398 const unsigned dl_certs
= !(flags
& NSSET_DONT_DOWNLOAD_CERTS
);
1399 const unsigned accept_obsolete
= flags
& NSSET_ACCEPT_OBSOLETE
;
1401 /* Make sure it's parseable. */
1402 c
= networkstatus_parse_vote_from_string(consensus
, NULL
, NS_TYPE_CONSENSUS
);
1404 log_warn(LD_DIR
, "Unable to parse networkstatus consensus");
1409 if (from_cache
&& !accept_obsolete
&&
1410 c
->valid_until
< now
-OLD_ROUTER_DESC_MAX_AGE
) {
1411 /* XXX022 when we try to make fallbackconsensus work again, we should
1412 * consider taking this out. Until then, believing obsolete consensuses
1413 * is causing more harm than good. See also bug 887. */
1414 log_info(LD_DIR
, "Loaded an obsolete consensus. Discarding.");
1418 if (current_consensus
&&
1419 !memcmp(c
->networkstatus_digest
, current_consensus
->networkstatus_digest
,
1421 /* We already have this one. That's a failure. */
1422 log_info(LD_DIR
, "Got a consensus we already have");
1426 if (current_consensus
&& c
->valid_after
<= current_consensus
->valid_after
) {
1427 /* We have a newer one. There's no point in accepting this one,
1428 * even if it's great. */
1429 log_info(LD_DIR
, "Got a consensus at least as old as the one we have");
1433 consensus_fname
= get_datadir_fname("cached-consensus");
1434 unverified_fname
= get_datadir_fname("unverified-consensus");
1436 /* Make sure it's signed enough. */
1437 if ((r
=networkstatus_check_consensus_signature(c
, 1))<0) {
1439 /* Okay, so it _might_ be signed enough if we get more certificates. */
1440 if (!was_waiting_for_certs
) {
1442 "Not enough certificates to check networkstatus consensus");
1444 if (!current_consensus
||
1445 c
->valid_after
> current_consensus
->valid_after
) {
1446 if (consensus_waiting_for_certs
)
1447 networkstatus_vote_free(consensus_waiting_for_certs
);
1448 tor_free(consensus_waiting_for_certs_body
);
1449 consensus_waiting_for_certs
= c
;
1450 c
= NULL
; /* Prevent free. */
1451 consensus_waiting_for_certs_body
= tor_strdup(consensus
);
1452 consensus_waiting_for_certs_set_at
= now
;
1453 consensus_waiting_for_certs_dl_failed
= 0;
1455 write_str_to_file(unverified_fname
, consensus
, 0);
1458 authority_certs_fetch_missing(c
, now
);
1459 /* This case is not a success or a failure until we get the certs
1460 * or fail to get the certs. */
1463 /* Even if we had enough signatures, we'd never use this as the
1464 * latest consensus. */
1465 if (was_waiting_for_certs
&& from_cache
)
1466 unlink(unverified_fname
);
1470 /* This can never be signed enough: Kill it. */
1471 if (!was_waiting_for_certs
) {
1472 log_warn(LD_DIR
, "Not enough good signatures on networkstatus "
1476 if (was_waiting_for_certs
&& (r
< -1) && from_cache
)
1477 unlink(unverified_fname
);
1483 control_event_client_status(LOG_NOTICE
, "CONSENSUS_ARRIVED");
1485 /* Are we missing any certificates at all? */
1486 if (r
!= 1 && dl_certs
)
1487 authority_certs_fetch_missing(c
, now
);
1489 if (control_event_is_interesting(EVENT_NS
))
1490 notify_control_networkstatus_changed(current_consensus
, c
);
1492 if (current_consensus
) {
1493 networkstatus_copy_old_consensus_info(c
, current_consensus
);
1494 networkstatus_vote_free(current_consensus
);
1497 if (consensus_waiting_for_certs
&&
1498 consensus_waiting_for_certs
->valid_after
<= c
->valid_after
) {
1499 networkstatus_vote_free(consensus_waiting_for_certs
);
1500 consensus_waiting_for_certs
= NULL
;
1501 if (consensus
!= consensus_waiting_for_certs_body
)
1502 tor_free(consensus_waiting_for_certs_body
);
1504 consensus_waiting_for_certs_body
= NULL
;
1505 consensus_waiting_for_certs_set_at
= 0;
1506 consensus_waiting_for_certs_dl_failed
= 0;
1507 unlink(unverified_fname
);
1510 /* Reset the failure count only if this consensus is actually valid. */
1511 if (c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1512 download_status_reset(&consensus_dl_status
);
1515 download_status_failed(&consensus_dl_status
, 0);
1518 current_consensus
= c
;
1519 c
= NULL
; /* Prevent free. */
1521 update_consensus_networkstatus_fetch_time(now
);
1522 dirvote_recalculate_timing(get_options(), now
);
1523 routerstatus_list_update_named_server_map();
1526 write_str_to_file(consensus_fname
, consensus
, 0);
1529 if (directory_caches_dir_info(get_options()))
1530 dirserv_set_cached_networkstatus_v3(consensus
,
1531 current_consensus
->valid_after
);
1533 if (ftime_definitely_before(now
, current_consensus
->valid_after
)) {
1534 char tbuf
[ISO_TIME_LEN
+1];
1536 long delta
= now
- current_consensus
->valid_after
;
1537 format_iso_time(tbuf
, current_consensus
->valid_after
);
1538 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
1539 log_warn(LD_GENERAL
, "Our clock is %s behind the time published in the "
1540 "consensus network status document (%s GMT). Tor needs an "
1541 "accurate clock to work correctly. Please check your time and "
1542 "date settings!", dbuf
, tbuf
);
1543 control_event_general_status(LOG_WARN
,
1544 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta
);
1547 router_dir_info_changed();
1552 networkstatus_vote_free(c
);
1553 tor_free(consensus_fname
);
1554 tor_free(unverified_fname
);
1558 /** Called when we have gotten more certificates: see whether we can
1559 * now verify a pending consensus. */
1561 networkstatus_note_certs_arrived(void)
1563 if (consensus_waiting_for_certs
) {
1564 if (networkstatus_check_consensus_signature(
1565 consensus_waiting_for_certs
, 0)>=0) {
1566 if (!networkstatus_set_current_consensus(
1567 consensus_waiting_for_certs_body
,
1568 NSSET_WAS_WAITING_FOR_CERTS
)) {
1569 tor_free(consensus_waiting_for_certs_body
);
1575 /** If the network-status list has changed since the last time we called this
1576 * function, update the status of every routerinfo from the network-status
1577 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
1578 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
1581 routers_update_all_from_networkstatus(time_t now
, int dir_version
)
1583 routerlist_t
*rl
= router_get_routerlist();
1584 networkstatus_t
*consensus
= networkstatus_get_live_consensus(now
);
1586 if (networkstatus_v2_list_has_changed
)
1587 download_status_map_update_from_v2_networkstatus();
1589 if (!consensus
|| dir_version
< 3) /* nothing more we should do */
1592 /* calls router_dir_info_changed() when it's done -- more routers
1593 * might be up or down now, which might affect whether there's enough
1594 * directory info. */
1595 routers_update_status_from_consensus_networkstatus(rl
->routers
, 0);
1597 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
,
1598 ri
->cache_info
.routerlist_index
= ri_sl_idx
);
1599 if (rl
->old_routers
)
1600 signed_descs_update_status_from_consensus_networkstatus(rl
->old_routers
);
1602 if (!have_warned_about_old_version
) {
1603 int is_server
= server_mode(get_options());
1604 version_status_t status
;
1605 const char *recommended
= is_server
?
1606 consensus
->server_versions
: consensus
->client_versions
;
1607 status
= tor_version_is_obsolete(VERSION
, recommended
);
1609 if (status
== VS_RECOMMENDED
) {
1610 log_info(LD_GENERAL
, "The directory authorities say my version is ok.");
1611 } else if (status
== VS_EMPTY
) {
1612 log_info(LD_GENERAL
,
1613 "The directory authorities don't recommend any versions.");
1614 } else if (status
== VS_NEW
|| status
== VS_NEW_IN_SERIES
) {
1615 if (!have_warned_about_new_version
) {
1616 log_notice(LD_GENERAL
, "This version of Tor (%s) is newer than any "
1617 "recommended version%s, according to the directory "
1618 "authorities. Recommended versions are: %s",
1620 status
== VS_NEW_IN_SERIES
? " in its series" : "",
1622 have_warned_about_new_version
= 1;
1623 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1624 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1625 VERSION
, "NEW", recommended
);
1628 log_warn(LD_GENERAL
, "Please upgrade! "
1629 "This version of Tor (%s) is %s, according to the directory "
1630 "authorities. Recommended versions are: %s",
1632 status
== VS_OLD
? "obsolete" : "not recommended",
1634 have_warned_about_old_version
= 1;
1635 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1636 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1637 VERSION
, status
== VS_OLD
? "OBSOLETE" : "UNRECOMMENDED",
1643 /** Update v2_download_status_map to contain an entry for every router
1644 * descriptor listed in the v2 networkstatuses. */
1646 download_status_map_update_from_v2_networkstatus(void)
1648 digestmap_t
*dl_status
;
1649 if (!networkstatus_v2_list
)
1651 if (!v2_download_status_map
)
1652 v2_download_status_map
= digestmap_new();
1654 dl_status
= digestmap_new();
1655 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
1657 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
1659 const char *d
= rs
->descriptor_digest
;
1660 download_status_t
*s
;
1661 if (digestmap_get(dl_status
, d
))
1663 if (!(s
= digestmap_remove(v2_download_status_map
, d
))) {
1664 s
= tor_malloc_zero(sizeof(download_status_t
));
1666 digestmap_set(dl_status
, d
, s
);
1669 digestmap_free(v2_download_status_map
, _tor_free
);
1670 v2_download_status_map
= dl_status
;
1671 networkstatus_v2_list_has_changed
= 0;
1674 /** Update our view of the list of named servers from the most recently
1675 * retrieved networkstatus consensus. */
1677 routerstatus_list_update_named_server_map(void)
1679 if (!current_consensus
)
1682 if (named_server_map
)
1683 strmap_free(named_server_map
, _tor_free
);
1684 named_server_map
= strmap_new();
1685 if (unnamed_server_map
)
1686 strmap_free(unnamed_server_map
, NULL
);
1687 unnamed_server_map
= strmap_new();
1688 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
, routerstatus_t
*, rs
,
1691 strmap_set_lc(named_server_map
, rs
->nickname
,
1692 tor_memdup(rs
->identity_digest
, DIGEST_LEN
));
1694 if (rs
->is_unnamed
) {
1695 strmap_set_lc(unnamed_server_map
, rs
->nickname
, (void*)1);
1700 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
1701 * according to our current consensus networkstatus. May re-order
1702 * <b>routers</b>. */
1704 routers_update_status_from_consensus_networkstatus(smartlist_t
*routers
,
1707 trusted_dir_server_t
*ds
;
1708 or_options_t
*options
= get_options();
1709 int authdir
= authdir_mode_v2(options
) || authdir_mode_v3(options
);
1710 int namingdir
= authdir
&& options
->NamingAuthoritativeDir
;
1711 networkstatus_t
*ns
= current_consensus
;
1712 if (!ns
|| !smartlist_len(ns
->routerstatus_list
))
1714 if (!networkstatus_v2_list
)
1715 networkstatus_v2_list
= smartlist_create();
1717 routers_sort_by_identity(routers
);
1719 SMARTLIST_FOREACH_JOIN(ns
->routerstatus_list
, routerstatus_t
*, rs
,
1720 routers
, routerinfo_t
*, router
,
1721 memcmp(rs
->identity_digest
,
1722 router
->cache_info
.identity_digest
, DIGEST_LEN
),
1724 /* We have no routerstatus for this router. Clear flags and skip it. */
1726 router
->is_named
= 0;
1728 if (router
->purpose
== ROUTER_PURPOSE_GENERAL
)
1729 router_clear_status_flags(router
);
1732 /* We have a routerstatus for this router. */
1733 const char *digest
= router
->cache_info
.identity_digest
;
1735 ds
= router_get_trusteddirserver_by_digest(digest
);
1738 if (rs
->is_named
&& !strcasecmp(router
->nickname
, rs
->nickname
))
1739 router
->is_named
= 1;
1741 router
->is_named
= 0;
1743 /* Is it the same descriptor, or only the same identity? */
1744 if (!memcmp(router
->cache_info
.signed_descriptor_digest
,
1745 rs
->descriptor_digest
, DIGEST_LEN
)) {
1746 if (ns
->valid_until
> router
->cache_info
.last_listed_as_valid_until
)
1747 router
->cache_info
.last_listed_as_valid_until
= ns
->valid_until
;
1751 /* If we're not an authdir, believe others. */
1752 router
->is_valid
= rs
->is_valid
;
1753 router
->is_running
= rs
->is_running
;
1754 router
->is_fast
= rs
->is_fast
;
1755 router
->is_stable
= rs
->is_stable
;
1756 router
->is_possible_guard
= rs
->is_possible_guard
;
1757 router
->is_exit
= rs
->is_exit
;
1758 router
->is_bad_directory
= rs
->is_bad_directory
;
1759 router
->is_bad_exit
= rs
->is_bad_exit
;
1760 router
->is_hs_dir
= rs
->is_hs_dir
;
1762 if (router
->is_running
&& ds
) {
1763 download_status_reset(&ds
->v2_ns_dl_status
);
1765 if (reset_failures
) {
1766 download_status_reset(&rs
->dl_status
);
1768 } SMARTLIST_FOREACH_JOIN_END(rs
, router
);
1770 /* Now update last_listed_as_valid_until from v2 networkstatuses. */
1771 /* XXXX If this is slow, we need to rethink the code. */
1772 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
, {
1773 time_t live_until
= ns
->published_on
+ V2_NETWORKSTATUS_ROUTER_LIFETIME
;
1774 SMARTLIST_FOREACH_JOIN(ns
->entries
, routerstatus_t
*, rs
,
1775 routers
, routerinfo_t
*, ri
,
1776 memcmp(rs
->identity_digest
,
1777 ri
->cache_info
.identity_digest
, DIGEST_LEN
),
1779 if (!memcmp(ri
->cache_info
.signed_descriptor_digest
,
1780 rs
->descriptor_digest
, DIGEST_LEN
)) {
1781 if (live_until
> ri
->cache_info
.last_listed_as_valid_until
)
1782 ri
->cache_info
.last_listed_as_valid_until
= live_until
;
1784 } SMARTLIST_FOREACH_JOIN_END(rs
, ri
);
1787 router_dir_info_changed();
1790 /** Given a list of signed_descriptor_t, update their fields (mainly, when
1791 * they were last listed) from the most recent consensus. */
1793 signed_descs_update_status_from_consensus_networkstatus(smartlist_t
*descs
)
1795 networkstatus_t
*ns
= current_consensus
;
1799 if (!ns
->desc_digest_map
) {
1800 char dummy
[DIGEST_LEN
];
1801 /* instantiates the digest map. */
1802 memset(dummy
, 0, sizeof(dummy
));
1803 router_get_consensus_status_by_descriptor_digest(dummy
);
1805 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, d
,
1807 routerstatus_t
*rs
= digestmap_get(ns
->desc_digest_map
,
1808 d
->signed_descriptor_digest
);
1810 if (ns
->valid_until
> d
->last_listed_as_valid_until
)
1811 d
->last_listed_as_valid_until
= ns
->valid_until
;
1816 /** Generate networkstatus lines for a single routerstatus_t object, and
1817 * return the result in a newly allocated string. Used only by controller
1818 * interface (for now.) */
1820 networkstatus_getinfo_helper_single(routerstatus_t
*rs
)
1822 char buf
[RS_ENTRY_LEN
+1];
1823 routerstatus_format_entry(buf
, sizeof(buf
), rs
, NULL
, 0, 1);
1824 return tor_strdup(buf
);
1827 /** Alloc and return a string describing routerstatuses for the most
1828 * recent info of each router we know about that is of purpose
1829 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
1831 * Right now this function is oriented toward listing bridges (you
1832 * shouldn't use this for general-purpose routers, since those
1833 * should be listed from the consensus, not from the routers list). */
1835 networkstatus_getinfo_by_purpose(const char *purpose_string
, time_t now
)
1837 time_t cutoff
= now
- ROUTER_MAX_AGE_TO_PUBLISH
;
1839 routerlist_t
*rl
= router_get_routerlist();
1840 smartlist_t
*statuses
;
1841 uint8_t purpose
= router_purpose_from_string(purpose_string
);
1843 int bridge_auth
= authdir_mode_bridge(get_options());
1845 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
1846 log_info(LD_DIR
, "Unrecognized purpose '%s' when listing router statuses.",
1851 statuses
= smartlist_create();
1852 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
, {
1853 if (ri
->cache_info
.published_on
< cutoff
)
1855 if (ri
->purpose
!= purpose
)
1857 if (bridge_auth
&& ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
1858 dirserv_set_router_is_running(ri
, now
);
1859 /* then generate and write out status lines for each of them */
1860 set_routerstatus_from_routerinfo(&rs
, ri
, now
, 0, 0, 0, 0);
1861 smartlist_add(statuses
, networkstatus_getinfo_helper_single(&rs
));
1864 answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
1865 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
1866 smartlist_free(statuses
);
1870 /** Write out router status entries for all our bridge descriptors. */
1872 networkstatus_dump_bridge_status_to_file(time_t now
)
1874 char *status
= networkstatus_getinfo_by_purpose("bridge", now
);
1875 or_options_t
*options
= get_options();
1876 size_t len
= strlen(options
->DataDirectory
) + 32;
1877 char *fname
= tor_malloc(len
);
1878 tor_snprintf(fname
, len
, "%s"PATH_SEPARATOR
"networkstatus-bridges",
1879 options
->DataDirectory
);
1880 write_str_to_file(fname
,status
,0);
1885 /** If <b>question</b> is a string beginning with "ns/" in a format the
1886 * control interface expects for a GETINFO question, set *<b>answer</b> to a
1887 * newly-allocated string containing networkstatus lines for the appropriate
1888 * ORs. Return 0 on success, -1 on unrecognized question format. */
1890 getinfo_helper_networkstatus(control_connection_t
*conn
,
1891 const char *question
, char **answer
)
1893 routerstatus_t
*status
;
1896 if (!current_consensus
) {
1897 *answer
= tor_strdup("");
1901 if (!strcmp(question
, "ns/all")) {
1902 smartlist_t
*statuses
= smartlist_create();
1903 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
1904 routerstatus_t
*, rs
,
1906 smartlist_add(statuses
, networkstatus_getinfo_helper_single(rs
));
1908 *answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
1909 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
1910 smartlist_free(statuses
);
1912 } else if (!strcmpstart(question
, "ns/id/")) {
1915 if (base16_decode(d
, DIGEST_LEN
, question
+6, strlen(question
+6)))
1917 status
= router_get_consensus_status_by_id(d
);
1918 } else if (!strcmpstart(question
, "ns/name/")) {
1919 status
= router_get_consensus_status_by_nickname(question
+8, 0);
1920 } else if (!strcmpstart(question
, "ns/purpose/")) {
1921 *answer
= networkstatus_getinfo_by_purpose(question
+11, time(NULL
));
1922 return *answer
? 0 : -1;
1928 *answer
= networkstatus_getinfo_helper_single(status
);
1932 /** Free all storage held locally in this module. */
1934 networkstatus_free_all(void)
1936 if (networkstatus_v2_list
) {
1937 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
1938 networkstatus_v2_free(ns
));
1939 smartlist_free(networkstatus_v2_list
);
1940 networkstatus_v2_list
= NULL
;
1942 if (v2_download_status_map
) {
1943 digestmap_free(v2_download_status_map
, _tor_free
);
1944 v2_download_status_map
= NULL
;
1946 if (current_consensus
) {
1947 networkstatus_vote_free(current_consensus
);
1948 current_consensus
= NULL
;
1950 if (consensus_waiting_for_certs
) {
1951 networkstatus_vote_free(consensus_waiting_for_certs
);
1952 consensus_waiting_for_certs
= NULL
;
1954 tor_free(consensus_waiting_for_certs_body
);
1955 if (named_server_map
) {
1956 strmap_free(named_server_map
, _tor_free
);
1958 if (unnamed_server_map
) {
1959 strmap_free(unnamed_server_map
, NULL
);