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-2011, 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 "networkstatus.h"
26 #include "routerlist.h"
27 #include "routerparse.h"
29 /* For tracking v2 networkstatus documents. Only caches do this now. */
31 /** Map from descriptor digest of routers listed in the v2 networkstatus
32 * documents to download_status_t* */
33 static digestmap_t
*v2_download_status_map
= NULL
;
34 /** Global list of all of the current v2 network_status documents that we know
35 * about. This list is kept sorted by published_on. */
36 static smartlist_t
*networkstatus_v2_list
= NULL
;
37 /** True iff any member of networkstatus_v2_list has changed since the last
38 * time we called download_status_map_update_from_v2_networkstatus() */
39 static int networkstatus_v2_list_has_changed
= 0;
41 /** Map from lowercase nickname to identity digest of named server, if any. */
42 static strmap_t
*named_server_map
= NULL
;
43 /** Map from lowercase nickname to (void*)1 for all names that are listed
44 * as unnamed for some server in the consensus. */
45 static strmap_t
*unnamed_server_map
= NULL
;
47 /** Most recently received and validated v3 consensus network status. */
48 static networkstatus_t
*current_consensus
= NULL
;
50 /** A v3 consensus networkstatus that we've received, but which we don't
51 * have enough certificates to be happy about. */
52 typedef struct consensus_waiting_for_certs_t
{
53 /** The consensus itself. */
54 networkstatus_t
*consensus
;
55 /** The encoded version of the consensus, nul-terminated. */
57 /** When did we set the current value of consensus_waiting_for_certs? If
58 * this is too recent, we shouldn't try to fetch a new consensus for a
59 * little while, to give ourselves time to get certificates for this one. */
61 /** Set to 1 if we've been holding on to it for so long we should maybe
62 * treat it as being bad. */
64 } consensus_waiting_for_certs_t
;
66 static consensus_waiting_for_certs_t
67 consensus_waiting_for_certs
[N_CONSENSUS_FLAVORS
];
69 /** The last time we tried to download a networkstatus, or 0 for "never". We
70 * use this to rate-limit download attempts for directory caches (including
71 * mirrors). Clients don't use this now. */
72 static time_t last_networkstatus_download_attempted
= 0;
74 /** A time before which we shouldn't try to replace the current consensus:
75 * this will be at some point after the next consensus becomes valid, but
76 * before the current consensus becomes invalid. */
77 static time_t time_to_download_next_consensus
= 0;
78 /** Download status for the current consensus networkstatus. */
79 static download_status_t consensus_dl_status
[N_CONSENSUS_FLAVORS
];
81 /** True iff we have logged a warning about this OR's version being older than
82 * listed by the authorities. */
83 static int have_warned_about_old_version
= 0;
84 /** True iff we have logged a warning about this OR's version being newer than
85 * listed by the authorities. */
86 static int have_warned_about_new_version
= 0;
88 static void download_status_map_update_from_v2_networkstatus(void);
89 static void routerstatus_list_update_named_server_map(void);
91 /** Forget that we've warned about anything networkstatus-related, so we will
92 * give fresh warnings if the same behavior happens again. */
94 networkstatus_reset_warnings(void)
96 if (current_consensus
) {
97 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
99 rs
->name_lookup_warned
= 0);
102 have_warned_about_old_version
= 0;
103 have_warned_about_new_version
= 0;
106 /** Reset the descriptor download failure count on all networkstatus docs, so
107 * that we can retry any long-failed documents immediately.
110 networkstatus_reset_download_failures(void)
113 const smartlist_t
*networkstatus_v2_list
= networkstatus_get_v2_list();
114 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
115 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
117 if (!router_get_by_descriptor_digest(rs
->descriptor_digest
))
118 rs
->need_to_mirror
= 1;
121 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
)
122 download_status_reset(&consensus_dl_status
[i
]);
123 if (v2_download_status_map
) {
124 digestmap_iter_t
*iter
;
125 digestmap_t
*map
= v2_download_status_map
;
128 download_status_t
*dls
;
129 for (iter
= digestmap_iter_init(map
); !digestmap_iter_done(iter
);
130 iter
= digestmap_iter_next(map
, iter
) ) {
131 digestmap_iter_get(iter
, &key
, &val
);
133 download_status_reset(dls
);
138 /** Repopulate our list of network_status_t objects from the list cached on
139 * disk. Return 0 on success, -1 on failure. */
141 router_reload_v2_networkstatus(void)
143 smartlist_t
*entries
;
146 char *filename
= get_datadir_fname("cached-status");
147 int maybe_delete
= !directory_caches_v2_dir_info(get_options());
148 time_t now
= time(NULL
);
149 if (!networkstatus_v2_list
)
150 networkstatus_v2_list
= smartlist_create();
152 entries
= tor_listdir(filename
);
153 if (!entries
) { /* dir doesn't exist */
156 } else if (!smartlist_len(entries
) && maybe_delete
) {
159 smartlist_free(entries
);
163 SMARTLIST_FOREACH(entries
, const char *, fn
, {
164 char buf
[DIGEST_LEN
];
166 filename
= get_datadir_fname2("cached-status", fn
);
167 remove_file_if_very_old(filename
, now
);
171 if (strlen(fn
) != HEX_DIGEST_LEN
||
172 base16_decode(buf
, sizeof(buf
), fn
, strlen(fn
))) {
174 "Skipping cached-status file with unexpected name \"%s\"",fn
);
177 filename
= get_datadir_fname2("cached-status", fn
);
178 s
= read_file_to_str(filename
, 0, &st
);
180 if (router_set_networkstatus_v2(s
, st
.st_mtime
, NS_FROM_CACHE
,
182 log_warn(LD_FS
, "Couldn't load networkstatus from \"%s\"",filename
);
188 SMARTLIST_FOREACH(entries
, char *, fn
, tor_free(fn
));
189 smartlist_free(entries
);
190 networkstatus_v2_list_clean(time(NULL
));
191 routers_update_all_from_networkstatus(time(NULL
), 2);
195 /** Read every cached v3 consensus networkstatus from the disk. */
197 router_reload_consensus_networkstatus(void)
202 or_options_t
*options
= get_options();
203 const unsigned int flags
= NSSET_FROM_CACHE
| NSSET_DONT_DOWNLOAD_CERTS
;
206 /* FFFF Suppress warnings if cached consensus is bad? */
207 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
209 const char *flavor
= networkstatus_get_flavor_name(flav
);
210 if (flav
== FLAV_NS
) {
211 filename
= get_datadir_fname("cached-consensus");
213 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
214 filename
= get_datadir_fname(buf
);
216 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
218 if (networkstatus_set_current_consensus(s
, flavor
, flags
) < -1) {
219 log_warn(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
226 if (flav
== FLAV_NS
) {
227 filename
= get_datadir_fname("unverified-consensus");
229 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
230 filename
= get_datadir_fname(buf
);
233 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
235 if (networkstatus_set_current_consensus(s
, flavor
,
236 flags
|NSSET_WAS_WAITING_FOR_CERTS
)) {
237 log_info(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
245 if (!current_consensus
||
246 (stat(options
->FallbackNetworkstatusFile
, &st
)==0 &&
247 st
.st_mtime
> current_consensus
->valid_after
)) {
248 s
= read_file_to_str(options
->FallbackNetworkstatusFile
,
249 RFTS_IGNORE_MISSING
, NULL
);
251 if (networkstatus_set_current_consensus(s
, "ns",
252 flags
|NSSET_ACCEPT_OBSOLETE
)) {
253 log_info(LD_FS
, "Couldn't load consensus networkstatus from \"%s\"",
254 options
->FallbackNetworkstatusFile
);
257 "Loaded fallback consensus networkstatus from \"%s\"",
258 options
->FallbackNetworkstatusFile
);
264 if (!current_consensus
) {
265 if (!named_server_map
)
266 named_server_map
= strmap_new();
267 if (!unnamed_server_map
)
268 unnamed_server_map
= strmap_new();
271 update_certificate_downloads(time(NULL
));
273 routers_update_all_from_networkstatus(time(NULL
), 3);
278 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
280 vote_routerstatus_free(vote_routerstatus_t
*rs
)
282 vote_microdesc_hash_t
*h
, *next
;
285 tor_free(rs
->version
);
286 tor_free(rs
->status
.exitsummary
);
287 for (h
= rs
->microdesc
; h
; h
= next
) {
288 tor_free(h
->microdesc_hash_line
);
295 /** Free all storage held by the routerstatus object <b>rs</b>. */
297 routerstatus_free(routerstatus_t
*rs
)
301 tor_free(rs
->exitsummary
);
305 /** Free all storage held by the networkstatus object <b>ns</b>. */
307 networkstatus_v2_free(networkstatus_v2_t
*ns
)
311 tor_free(ns
->source_address
);
312 tor_free(ns
->contact
);
314 crypto_free_pk_env(ns
->signing_key
);
315 tor_free(ns
->client_versions
);
316 tor_free(ns
->server_versions
);
318 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
319 routerstatus_free(rs
));
320 smartlist_free(ns
->entries
);
325 /** Free all storage held in <b>sig</b> */
327 document_signature_free(document_signature_t
*sig
)
329 tor_free(sig
->signature
);
333 /** Return a newly allocated copy of <b>sig</b> */
334 document_signature_t
*
335 document_signature_dup(const document_signature_t
*sig
)
337 document_signature_t
*r
= tor_memdup(sig
, sizeof(document_signature_t
));
339 r
->signature
= tor_memdup(sig
->signature
, sig
->signature_len
);
343 /** Free all storage held in <b>ns</b>. */
345 networkstatus_vote_free(networkstatus_t
*ns
)
350 tor_free(ns
->client_versions
);
351 tor_free(ns
->server_versions
);
352 if (ns
->known_flags
) {
353 SMARTLIST_FOREACH(ns
->known_flags
, char *, c
, tor_free(c
));
354 smartlist_free(ns
->known_flags
);
356 if (ns
->weight_params
) {
357 SMARTLIST_FOREACH(ns
->weight_params
, char *, c
, tor_free(c
));
358 smartlist_free(ns
->weight_params
);
360 if (ns
->net_params
) {
361 SMARTLIST_FOREACH(ns
->net_params
, char *, c
, tor_free(c
));
362 smartlist_free(ns
->net_params
);
364 if (ns
->supported_methods
) {
365 SMARTLIST_FOREACH(ns
->supported_methods
, char *, c
, tor_free(c
));
366 smartlist_free(ns
->supported_methods
);
369 SMARTLIST_FOREACH_BEGIN(ns
->voters
, networkstatus_voter_info_t
*, voter
) {
370 tor_free(voter
->nickname
);
371 tor_free(voter
->address
);
372 tor_free(voter
->contact
);
374 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
375 document_signature_free(sig
));
376 smartlist_free(voter
->sigs
);
379 } SMARTLIST_FOREACH_END(voter
);
380 smartlist_free(ns
->voters
);
382 authority_cert_free(ns
->cert
);
384 if (ns
->routerstatus_list
) {
385 if (ns
->type
== NS_TYPE_VOTE
|| ns
->type
== NS_TYPE_OPINION
) {
386 SMARTLIST_FOREACH(ns
->routerstatus_list
, vote_routerstatus_t
*, rs
,
387 vote_routerstatus_free(rs
));
389 SMARTLIST_FOREACH(ns
->routerstatus_list
, routerstatus_t
*, rs
,
390 routerstatus_free(rs
));
393 smartlist_free(ns
->routerstatus_list
);
396 digestmap_free(ns
->desc_digest_map
, NULL
);
398 memset(ns
, 11, sizeof(*ns
));
402 /** Return the voter info from <b>vote</b> for the voter whose identity digest
403 * is <b>identity</b>, or NULL if no such voter is associated with
405 networkstatus_voter_info_t
*
406 networkstatus_get_voter_by_id(networkstatus_t
*vote
,
407 const char *identity
)
409 if (!vote
|| !vote
->voters
)
411 SMARTLIST_FOREACH(vote
->voters
, networkstatus_voter_info_t
*, voter
,
412 if (!memcmp(voter
->identity_digest
, identity
, DIGEST_LEN
))
417 /** Check whether the signature <b>sig</b> is correctly signed with the
418 * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
419 * signing key; otherwise set the good_signature or bad_signature flag on
420 * <b>voter</b>, and return 0. */
422 networkstatus_check_document_signature(const networkstatus_t
*consensus
,
423 document_signature_t
*sig
,
424 const authority_cert_t
*cert
)
426 char key_digest
[DIGEST_LEN
];
427 const int dlen
= sig
->alg
== DIGEST_SHA1
? DIGEST_LEN
: DIGEST256_LEN
;
429 size_t signed_digest_len
;
431 if (crypto_pk_get_digest(cert
->signing_key
, key_digest
)<0)
433 if (memcmp(sig
->signing_key_digest
, key_digest
, DIGEST_LEN
) ||
434 memcmp(sig
->identity_digest
, cert
->cache_info
.identity_digest
,
438 signed_digest_len
= crypto_pk_keysize(cert
->signing_key
);
439 signed_digest
= tor_malloc(signed_digest_len
);
440 if (crypto_pk_public_checksig(cert
->signing_key
,
444 sig
->signature_len
) < dlen
||
445 memcmp(signed_digest
, consensus
->digests
.d
[sig
->alg
], dlen
)) {
446 log_warn(LD_DIR
, "Got a bad signature on a networkstatus vote");
447 sig
->bad_signature
= 1;
449 sig
->good_signature
= 1;
451 tor_free(signed_digest
);
455 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
456 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
457 * signature from every recognized authority on it, 0 if there are
458 * enough good signatures from recognized authorities on it, -1 if we might
459 * get enough good signatures by fetching missing certificates, and -2
460 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
461 * about every problem; if warn is at least 1, warn only if we can't get
462 * enough signatures; if warn is negative, log nothing at all. */
464 networkstatus_check_consensus_signature(networkstatus_t
*consensus
,
468 int n_missing_key
= 0, n_dl_failed_key
= 0;
471 int n_no_signature
= 0;
472 int n_v3_authorities
= get_n_authorities(V3_AUTHORITY
);
473 int n_required
= n_v3_authorities
/2 + 1;
474 smartlist_t
*need_certs_from
= smartlist_create();
475 smartlist_t
*unrecognized
= smartlist_create();
476 smartlist_t
*missing_authorities
= smartlist_create();
478 time_t now
= time(NULL
);
480 tor_assert(consensus
->type
== NS_TYPE_CONSENSUS
);
482 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*,
486 int unknown_here
= 0;
487 int missing_key_here
= 0, dl_failed_key_here
= 0;
488 SMARTLIST_FOREACH_BEGIN(voter
->sigs
, document_signature_t
*, sig
) {
489 if (!sig
->good_signature
&& !sig
->bad_signature
&&
491 /* we can try to check the signature. */
492 int is_v3_auth
= trusteddirserver_get_by_v3_auth_digest(
493 sig
->identity_digest
) != NULL
;
494 authority_cert_t
*cert
=
495 authority_cert_get_by_digests(sig
->identity_digest
,
496 sig
->signing_key_digest
);
497 tor_assert(!memcmp(sig
->identity_digest
, voter
->identity_digest
,
501 smartlist_add(unrecognized
, voter
);
504 } else if (!cert
|| cert
->expires
< now
) {
505 smartlist_add(need_certs_from
, voter
);
507 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
508 ++dl_failed_key_here
;
511 if (networkstatus_check_document_signature(consensus
, sig
, cert
) < 0) {
512 smartlist_add(need_certs_from
, voter
);
514 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
515 ++dl_failed_key_here
;
519 if (sig
->good_signature
)
521 else if (sig
->bad_signature
)
523 } SMARTLIST_FOREACH_END(sig
);
528 else if (missing_key_here
) {
530 if (dl_failed_key_here
)
532 } else if (unknown_here
) {
537 } SMARTLIST_FOREACH_END(voter
);
539 /* Now see whether we're missing any voters entirely. */
540 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
541 trusted_dir_server_t
*, ds
,
543 if ((ds
->type
& V3_AUTHORITY
) &&
544 !networkstatus_get_voter_by_id(consensus
, ds
->v3_identity_digest
))
545 smartlist_add(missing_authorities
, ds
);
548 if (warn
> 1 || (warn
>= 0 &&
549 (n_good
+ n_missing_key
- n_dl_failed_key
< n_required
))) {
556 SMARTLIST_FOREACH(unrecognized
, networkstatus_voter_info_t
*, voter
,
558 log(severity
, LD_DIR
, "Consensus includes unrecognized authority "
559 "'%s' at %s:%d (contact %s; identity %s)",
560 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
561 voter
->contact
?voter
->contact
:"n/a",
562 hex_str(voter
->identity_digest
, DIGEST_LEN
));
564 SMARTLIST_FOREACH(need_certs_from
, networkstatus_voter_info_t
*, voter
,
566 log(severity
, LD_DIR
, "Looks like we need to download a new "
567 "certificate from authority '%s' at %s:%d (contact %s; "
569 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
570 voter
->contact
?voter
->contact
:"n/a",
571 hex_str(voter
->identity_digest
, DIGEST_LEN
));
573 SMARTLIST_FOREACH(missing_authorities
, trusted_dir_server_t
*, ds
,
575 log(severity
, LD_DIR
, "Consensus does not include configured "
576 "authority '%s' at %s:%d (identity %s)",
577 ds
->nickname
, ds
->address
, (int)ds
->dir_port
,
578 hex_str(ds
->v3_identity_digest
, DIGEST_LEN
));
581 smartlist_t
*sl
= smartlist_create();
583 tor_asprintf(&cp
, "A consensus needs %d good signatures from recognized "
584 "authorities for us to accept it. This one has %d.",
586 smartlist_add(sl
,cp
);
587 if (n_no_signature
) {
588 tor_asprintf(&cp
, "%d of the authorities we know didn't sign it.",
590 smartlist_add(sl
,cp
);
593 tor_asprintf(&cp
, "It has %d signatures from authorities we don't "
594 "recognize.", n_unknown
);
595 smartlist_add(sl
,cp
);
598 tor_asprintf(&cp
, "%d of the signatures on it didn't verify "
599 "correctly.", n_bad
);
600 smartlist_add(sl
,cp
);
603 tor_asprintf(&cp
, "We were unable to check %d of the signatures, "
604 "because we were missing the keys.", n_missing_key
);
605 smartlist_add(sl
,cp
);
607 cp
= smartlist_join_strings(sl
, " ", 0, NULL
);
608 log(severity
, LD_DIR
, "%s", cp
);
610 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
615 smartlist_free(unrecognized
);
616 smartlist_free(need_certs_from
);
617 smartlist_free(missing_authorities
);
619 if (n_good
== n_v3_authorities
)
621 else if (n_good
>= n_required
)
623 else if (n_good
+ n_missing_key
>= n_required
)
629 /** Helper: return a newly allocated string containing the name of the filename
630 * where we plan to cache the network status with the given identity digest. */
632 networkstatus_get_cache_filename(const char *identity_digest
)
634 char fp
[HEX_DIGEST_LEN
+1];
635 base16_encode(fp
, HEX_DIGEST_LEN
+1, identity_digest
, DIGEST_LEN
);
636 return get_datadir_fname2("cached-status", fp
);
639 /** Helper for smartlist_sort: Compare two networkstatus objects by
640 * publication date. */
642 _compare_networkstatus_v2_published_on(const void **_a
, const void **_b
)
644 const networkstatus_v2_t
*a
= *_a
, *b
= *_b
;
645 if (a
->published_on
< b
->published_on
)
647 else if (a
->published_on
> b
->published_on
)
653 /** Add the parsed v2 networkstatus in <b>ns</b> (with original document in
654 * <b>s</b>) to the disk cache (and the in-memory directory server cache) as
657 add_networkstatus_to_cache(const char *s
,
658 v2_networkstatus_source_t source
,
659 networkstatus_v2_t
*ns
)
661 if (source
!= NS_FROM_CACHE
) {
662 char *fn
= networkstatus_get_cache_filename(ns
->identity_digest
);
663 if (write_str_to_file(fn
, s
, 0)<0) {
664 log_notice(LD_FS
, "Couldn't write cached network status to \"%s\"", fn
);
669 if (directory_caches_v2_dir_info(get_options()))
670 dirserv_set_cached_networkstatus_v2(s
,
677 /** How far in the future do we allow a network-status to get before removing
679 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
681 /** Given a string <b>s</b> containing a network status that we received at
682 * <b>arrived_at</b> from <b>source</b>, try to parse it, see if we want to
683 * store it, and put it into our cache as necessary.
685 * If <b>source</b> is NS_FROM_DIR or NS_FROM_CACHE, do not replace our
686 * own networkstatus_t (if we're an authoritative directory server).
688 * If <b>source</b> is NS_FROM_CACHE, do not write our networkstatus_t to the
691 * If <b>requested_fingerprints</b> is provided, it must contain a list of
692 * uppercased identity fingerprints. Do not update any networkstatus whose
693 * fingerprint is not on the list; after updating a networkstatus, remove its
694 * fingerprint from the list.
696 * Return 0 on success, -1 on failure.
698 * Callers should make sure that routers_update_all_from_networkstatus() is
699 * invoked after this function succeeds.
702 router_set_networkstatus_v2(const char *s
, time_t arrived_at
,
703 v2_networkstatus_source_t source
,
704 smartlist_t
*requested_fingerprints
)
706 networkstatus_v2_t
*ns
;
710 trusted_dir_server_t
*trusted_dir
= NULL
;
711 const char *source_desc
= NULL
;
712 char fp
[HEX_DIGEST_LEN
+1];
713 char published
[ISO_TIME_LEN
+1];
715 if (!directory_caches_v2_dir_info(get_options()))
716 return 0; /* Don't bother storing it. */
718 ns
= networkstatus_v2_parse_from_string(s
);
720 log_warn(LD_DIR
, "Couldn't parse network status.");
723 base16_encode(fp
, HEX_DIGEST_LEN
+1, ns
->identity_digest
, DIGEST_LEN
);
725 router_get_trusteddirserver_by_digest(ns
->identity_digest
)) ||
726 !(trusted_dir
->type
& V2_AUTHORITY
)) {
727 log_info(LD_DIR
, "Network status was signed, but not by an authoritative "
728 "directory we recognize.");
731 source_desc
= trusted_dir
->description
;
734 if (arrived_at
> now
)
737 ns
->received_on
= arrived_at
;
739 format_iso_time(published
, ns
->published_on
);
741 if (ns
->published_on
> now
+ NETWORKSTATUS_ALLOW_SKEW
) {
743 long delta
= now
- ns
->published_on
;
744 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
745 log_warn(LD_GENERAL
, "Network status from %s was published %s in the "
746 "future (%s GMT). Check your time and date settings! "
748 source_desc
, dbuf
, published
);
749 control_event_general_status(LOG_WARN
,
750 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=NETWORKSTATUS:%s:%d",
751 delta
, ns
->source_address
, ns
->source_dirport
);
755 if (!networkstatus_v2_list
)
756 networkstatus_v2_list
= smartlist_create();
758 if ( (source
== NS_FROM_DIR_BY_FP
|| source
== NS_FROM_DIR_ALL
) &&
759 router_digest_is_me(ns
->identity_digest
)) {
760 /* Don't replace our own networkstatus when we get it from somebody else.*/
761 networkstatus_v2_free(ns
);
765 if (requested_fingerprints
) {
766 if (smartlist_string_isin(requested_fingerprints
, fp
)) {
767 smartlist_string_remove(requested_fingerprints
, fp
);
769 if (source
!= NS_FROM_DIR_ALL
) {
771 smartlist_join_strings(requested_fingerprints
," ",0,NULL
);
773 "We received a network status with a fingerprint (%s) that we "
774 "never requested. (We asked for: %s.) Dropping.",
784 /* We got a non-trusted networkstatus, and we're a directory cache.
785 * This means that we asked an authority, and it told us about another
786 * authority we didn't recognize. */
788 "We do not recognize authority (%s) but we are willing "
790 add_networkstatus_to_cache(s
, source
, ns
);
791 networkstatus_v2_free(ns
);
797 for (i
=0; i
< smartlist_len(networkstatus_v2_list
); ++i
) {
798 networkstatus_v2_t
*old_ns
= smartlist_get(networkstatus_v2_list
, i
);
800 if (!memcmp(old_ns
->identity_digest
, ns
->identity_digest
, DIGEST_LEN
)) {
801 if (!memcmp(old_ns
->networkstatus_digest
,
802 ns
->networkstatus_digest
, DIGEST_LEN
)) {
803 /* Same one we had before. */
804 networkstatus_v2_free(ns
);
805 tor_assert(trusted_dir
);
807 "Not replacing network-status from %s (published %s); "
808 "we already have it.",
809 trusted_dir
->description
, published
);
810 if (old_ns
->received_on
< arrived_at
) {
811 if (source
!= NS_FROM_CACHE
) {
813 fn
= networkstatus_get_cache_filename(old_ns
->identity_digest
);
814 /* We use mtime to tell when it arrived, so update that. */
818 old_ns
->received_on
= arrived_at
;
820 download_status_failed(&trusted_dir
->v2_ns_dl_status
, 0);
822 } else if (old_ns
->published_on
>= ns
->published_on
) {
823 char old_published
[ISO_TIME_LEN
+1];
824 format_iso_time(old_published
, old_ns
->published_on
);
825 tor_assert(trusted_dir
);
827 "Not replacing network-status from %s (published %s);"
828 " we have a newer one (published %s) for this authority.",
829 trusted_dir
->description
, published
,
831 networkstatus_v2_free(ns
);
832 download_status_failed(&trusted_dir
->v2_ns_dl_status
, 0);
835 networkstatus_v2_free(old_ns
);
836 smartlist_set(networkstatus_v2_list
, i
, ns
);
843 if (source
!= NS_FROM_CACHE
&& trusted_dir
) {
844 download_status_reset(&trusted_dir
->v2_ns_dl_status
);
848 smartlist_add(networkstatus_v2_list
, ns
);
850 /** Retain any routerinfo mentioned in a V2 networkstatus for at least this
852 #define V2_NETWORKSTATUS_ROUTER_LIFETIME (3*60*60)
855 time_t live_until
= ns
->published_on
+ V2_NETWORKSTATUS_ROUTER_LIFETIME
;
856 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
858 signed_descriptor_t
*sd
=
859 router_get_by_descriptor_digest(rs
->descriptor_digest
);
861 if (sd
->last_listed_as_valid_until
< live_until
)
862 sd
->last_listed_as_valid_until
= live_until
;
864 rs
->need_to_mirror
= 1;
869 log_info(LD_DIR
, "Setting networkstatus %s %s (published %s)",
870 source
== NS_FROM_CACHE
?"cached from":
871 ((source
== NS_FROM_DIR_BY_FP
|| source
== NS_FROM_DIR_ALL
) ?
872 "downloaded from":"generated for"),
873 trusted_dir
->description
, published
);
874 networkstatus_v2_list_has_changed
= 1;
876 smartlist_sort(networkstatus_v2_list
,
877 _compare_networkstatus_v2_published_on
);
880 add_networkstatus_to_cache(s
, source
, ns
);
885 /** Remove all very-old network_status_t objects from memory and from the
888 networkstatus_v2_list_clean(time_t now
)
891 if (!networkstatus_v2_list
)
894 for (i
= 0; i
< smartlist_len(networkstatus_v2_list
); ++i
) {
895 networkstatus_v2_t
*ns
= smartlist_get(networkstatus_v2_list
, i
);
897 if (ns
->published_on
+ MAX_NETWORKSTATUS_AGE
> now
)
899 /* Okay, this one is too old. Remove it from the list, and delete it
901 smartlist_del(networkstatus_v2_list
, i
--);
902 fname
= networkstatus_get_cache_filename(ns
->identity_digest
);
903 if (file_status(fname
) == FN_FILE
) {
904 log_info(LD_DIR
, "Removing too-old networkstatus in %s", fname
);
908 if (directory_caches_v2_dir_info(get_options())) {
909 dirserv_set_cached_networkstatus_v2(NULL
, ns
->identity_digest
, 0);
911 networkstatus_v2_free(ns
);
914 /* And now go through the directory cache for any cached untrusted
915 * networkstatuses and other network info. */
916 dirserv_clear_old_networkstatuses(now
- MAX_NETWORKSTATUS_AGE
);
917 dirserv_clear_old_v1_info(now
);
920 /** Helper for bsearching a list of routerstatus_t pointers: compare a
921 * digest in the key to the identity digest of a routerstatus_t. */
923 compare_digest_to_routerstatus_entry(const void *_key
, const void **_member
)
925 const char *key
= _key
;
926 const routerstatus_t
*rs
= *_member
;
927 return memcmp(key
, rs
->identity_digest
, DIGEST_LEN
);
930 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
931 * NULL if none was found. */
933 networkstatus_v2_find_entry(networkstatus_v2_t
*ns
, const char *digest
)
935 return smartlist_bsearch(ns
->entries
, digest
,
936 compare_digest_to_routerstatus_entry
);
939 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
940 * NULL if none was found. */
942 networkstatus_vote_find_entry(networkstatus_t
*ns
, const char *digest
)
944 return smartlist_bsearch(ns
->routerstatus_list
, digest
,
945 compare_digest_to_routerstatus_entry
);
948 /*XXXX make this static once functions are moved into this file. */
949 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
950 * <b>digest</b>. Return value and set *<b>found_out</b> as for
951 * smartlist_bsearch_idx(). */
953 networkstatus_vote_find_entry_idx(networkstatus_t
*ns
,
954 const char *digest
, int *found_out
)
956 return smartlist_bsearch_idx(ns
->routerstatus_list
, digest
,
957 compare_digest_to_routerstatus_entry
,
961 /** Return a list of the v2 networkstatus documents. */
963 networkstatus_get_v2_list(void)
965 if (!networkstatus_v2_list
)
966 networkstatus_v2_list
= smartlist_create();
967 return networkstatus_v2_list
;
970 /** Return the consensus view of the status of the router whose current
971 * <i>descriptor</i> digest is <b>digest</b>, or NULL if no such router is
974 router_get_consensus_status_by_descriptor_digest(const char *digest
)
976 if (!current_consensus
) return NULL
;
977 if (!current_consensus
->desc_digest_map
) {
978 digestmap_t
* m
= current_consensus
->desc_digest_map
= digestmap_new();
979 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
980 routerstatus_t
*, rs
,
982 digestmap_set(m
, rs
->descriptor_digest
, rs
);
985 return digestmap_get(current_consensus
->desc_digest_map
, digest
);
988 /** Given the digest of a router descriptor, return its current download
989 * status, or NULL if the digest is unrecognized. */
991 router_get_dl_status_by_descriptor_digest(const char *d
)
994 if ((rs
= router_get_consensus_status_by_descriptor_digest(d
)))
995 return &rs
->dl_status
;
996 if (v2_download_status_map
)
997 return digestmap_get(v2_download_status_map
, d
);
1002 /** Return the consensus view of the status of the router whose identity
1003 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
1005 router_get_consensus_status_by_id(const char *digest
)
1007 if (!current_consensus
)
1009 return smartlist_bsearch(current_consensus
->routerstatus_list
, digest
,
1010 compare_digest_to_routerstatus_entry
);
1013 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
1014 * the corresponding routerstatus_t, or NULL if none exists. Warn the
1015 * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
1016 * nickname, but the Named flag isn't set for that router. */
1018 router_get_consensus_status_by_nickname(const char *nickname
,
1019 int warn_if_unnamed
)
1021 char digest
[DIGEST_LEN
];
1022 routerstatus_t
*best
=NULL
;
1023 smartlist_t
*matches
=NULL
;
1024 const char *named_id
=NULL
;
1026 if (!current_consensus
|| !nickname
)
1029 /* Is this name really a hexadecimal identity digest? */
1030 if (nickname
[0] == '$') {
1031 if (base16_decode(digest
, DIGEST_LEN
, nickname
+1, strlen(nickname
+1))<0)
1033 return networkstatus_vote_find_entry(current_consensus
, digest
);
1034 } else if (strlen(nickname
) == HEX_DIGEST_LEN
&&
1035 (base16_decode(digest
, DIGEST_LEN
, nickname
, strlen(nickname
))==0)) {
1036 return networkstatus_vote_find_entry(current_consensus
, digest
);
1039 /* Is there a server that is Named with this name? */
1040 if (named_server_map
)
1041 named_id
= strmap_get_lc(named_server_map
, nickname
);
1043 return networkstatus_vote_find_entry(current_consensus
, named_id
);
1045 /* Okay; is this name listed as Unnamed? */
1046 if (unnamed_server_map
&&
1047 strmap_get_lc(unnamed_server_map
, nickname
)) {
1048 log_info(LD_GENERAL
, "The name %s is listed as Unnamed; it is not the "
1049 "canonical name of any server we know.", escaped(nickname
));
1053 /* This name is not canonical for any server; go through the list and
1054 * see who it matches. */
1055 /*XXXX This is inefficient; optimize it if it matters. */
1056 matches
= smartlist_create();
1057 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
1058 routerstatus_t
*, lrs
,
1060 if (!strcasecmp(lrs
->nickname
, nickname
)) {
1061 if (lrs
->is_named
) {
1062 tor_fragile_assert() /* This should never happen. */
1063 smartlist_free(matches
);
1066 if (lrs
->is_unnamed
) {
1067 tor_fragile_assert(); /* nor should this. */
1068 smartlist_clear(matches
);
1072 smartlist_add(matches
, lrs
);
1078 if (smartlist_len(matches
)>1 && warn_if_unnamed
) {
1080 SMARTLIST_FOREACH(matches
, routerstatus_t
*, lrs
,
1082 if (! lrs
->name_lookup_warned
) {
1083 lrs
->name_lookup_warned
=1;
1088 log_warn(LD_CONFIG
,"There are multiple matches for the nickname \"%s\","
1089 " but none is listed as named by the directory authorities. "
1090 "Choosing one arbitrarily.", nickname
);
1092 } else if (warn_if_unnamed
&& best
&& !best
->name_lookup_warned
) {
1093 char fp
[HEX_DIGEST_LEN
+1];
1094 base16_encode(fp
, sizeof(fp
),
1095 best
->identity_digest
, DIGEST_LEN
);
1097 "When looking up a status, you specified a server \"%s\" by name, "
1098 "but the directory authorities do not have any key registered for "
1099 "this nickname -- so it could be used by any server, "
1100 "not just the one you meant. "
1101 "To make sure you get the same server in the future, refer to "
1102 "it by key, as \"$%s\".", nickname
, fp
);
1103 best
->name_lookup_warned
= 1;
1105 smartlist_free(matches
);
1109 /** Return the identity digest that's mapped to officially by
1110 * <b>nickname</b>. */
1112 networkstatus_get_router_digest_by_nickname(const char *nickname
)
1114 if (!named_server_map
)
1116 return strmap_get_lc(named_server_map
, nickname
);
1119 /** Return true iff <b>nickname</b> is disallowed from being the nickname
1122 networkstatus_nickname_is_unnamed(const char *nickname
)
1124 if (!unnamed_server_map
)
1126 return strmap_get_lc(unnamed_server_map
, nickname
) != NULL
;
1129 /** How frequently do directory authorities re-download fresh networkstatus
1131 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
1133 /** How frequently do non-authority directory caches re-download fresh
1134 * networkstatus documents? */
1135 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
1137 /** We are a directory server, and so cache network_status documents.
1138 * Initiate downloads as needed to update them. For v2 authorities,
1139 * this means asking each trusted directory for its network-status.
1140 * For caches, this means asking a random v2 authority for all
1144 update_v2_networkstatus_cache_downloads(time_t now
)
1146 int authority
= authdir_mode_v2(get_options());
1148 authority
? AUTHORITY_NS_CACHE_INTERVAL
: NONAUTHORITY_NS_CACHE_INTERVAL
;
1149 const smartlist_t
*trusted_dir_servers
= router_get_trusted_dir_servers();
1151 if (last_networkstatus_download_attempted
+ interval
>= now
)
1154 last_networkstatus_download_attempted
= now
;
1157 /* An authority launches a separate connection for everybody. */
1158 SMARTLIST_FOREACH_BEGIN(trusted_dir_servers
, trusted_dir_server_t
*, ds
)
1160 char resource
[HEX_DIGEST_LEN
+6]; /* fp/hexdigit.z\0 */
1162 if (!(ds
->type
& V2_AUTHORITY
))
1164 if (router_digest_is_me(ds
->digest
))
1166 tor_addr_from_ipv4h(&addr
, ds
->addr
);
1167 /* Is this quite sensible with IPv6 or multiple addresses? */
1168 if (connection_get_by_type_addr_port_purpose(
1169 CONN_TYPE_DIR
, &addr
, ds
->dir_port
,
1170 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
)) {
1171 /* XXX the above dir_port won't be accurate if we're
1172 * doing a tunneled conn. In that case it should be or_port.
1173 * How to guess from here? Maybe make the function less general
1174 * and have it know that it's looking for dir conns. -RD */
1175 /* Only directory caches download v2 networkstatuses, and they
1176 * don't use tunneled connections. I think it's okay to ignore
1180 strlcpy(resource
, "fp/", sizeof(resource
));
1181 base16_encode(resource
+3, sizeof(resource
)-3, ds
->digest
, DIGEST_LEN
);
1182 strlcat(resource
, ".z", sizeof(resource
));
1183 directory_initiate_command_routerstatus(
1184 &ds
->fake_status
, DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
,
1185 ROUTER_PURPOSE_GENERAL
,
1186 0, /* Not private */
1188 NULL
, 0 /* No payload. */,
1191 SMARTLIST_FOREACH_END(ds
);
1193 /* A non-authority cache launches one connection to a random authority. */
1194 /* (Check whether we're currently fetching network-status objects.) */
1195 if (!connection_get_by_type_purpose(CONN_TYPE_DIR
,
1196 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
))
1197 directory_get_from_dirserver(DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
,
1198 ROUTER_PURPOSE_GENERAL
, "all.z",
1199 PDS_RETRY_IF_NO_SERVERS
);
1203 /** How many times will we try to fetch a consensus before we give up? */
1204 #define CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES 8
1205 /** How long will we hang onto a possibly live consensus for which we're
1206 * fetching certs before we check whether there is a better one? */
1207 #define DELAY_WHILE_FETCHING_CERTS (20*60)
1209 /** If we want to download a fresh consensus, launch a new download as
1212 update_consensus_networkstatus_downloads(time_t now
)
1215 if (!networkstatus_get_live_consensus(now
))
1216 time_to_download_next_consensus
= now
; /* No live consensus? Get one now!*/
1217 if (time_to_download_next_consensus
> now
)
1218 return; /* Wait until the current consensus is older. */
1219 /* XXXXNM Microdescs: may need to download more types. */
1220 if (!download_status_is_ready(&consensus_dl_status
[FLAV_NS
], now
,
1221 CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES
))
1222 return; /* We failed downloading a consensus too recently. */
1223 if (connection_get_by_type_purpose(CONN_TYPE_DIR
,
1224 DIR_PURPOSE_FETCH_CONSENSUS
))
1225 return; /* There's an in-progress download.*/
1227 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1228 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
1229 if (waiting
->consensus
) {
1230 /* XXXX make sure this doesn't delay sane downloads. */
1231 if (waiting
->set_at
+ DELAY_WHILE_FETCHING_CERTS
> now
)
1232 return; /* We're still getting certs for this one. */
1234 if (!waiting
->dl_failed
) {
1235 download_status_failed(&consensus_dl_status
[FLAV_NS
], 0);
1236 waiting
->dl_failed
=1;
1242 log_info(LD_DIR
, "Launching networkstatus consensus download.");
1243 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
1244 ROUTER_PURPOSE_GENERAL
, NULL
,
1245 PDS_RETRY_IF_NO_SERVERS
);
1248 /** Called when an attempt to download a consensus fails: note that the
1249 * failure occurred, and possibly retry. */
1251 networkstatus_consensus_download_failed(int status_code
)
1253 /* XXXXNM Microdescs: may need to handle more types. */
1254 download_status_failed(&consensus_dl_status
[FLAV_NS
], status_code
);
1255 /* Retry immediately, if appropriate. */
1256 update_consensus_networkstatus_downloads(time(NULL
));
1259 /** How long do we (as a cache) wait after a consensus becomes non-fresh
1260 * before trying to fetch another? */
1261 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
1263 /** Update the time at which we'll consider replacing the current
1266 update_consensus_networkstatus_fetch_time(time_t now
)
1268 or_options_t
*options
= get_options();
1269 networkstatus_t
*c
= networkstatus_get_live_consensus(now
);
1272 long interval
= c
->fresh_until
- c
->valid_after
;
1273 long min_sec_before_caching
= CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1276 if (min_sec_before_caching
> interval
/16) {
1277 /* Usually we allow 2-minutes slop factor in case clocks get
1278 desynchronized a little. If we're on a private network with
1279 a crazy-fast voting interval, though, 2 minutes may be too
1281 min_sec_before_caching
= interval
/16;
1284 if (directory_fetches_dir_info_early(options
)) {
1285 /* We want to cache the next one at some point after this one
1286 * is no longer fresh... */
1287 start
= c
->fresh_until
+ min_sec_before_caching
;
1288 /* Some clients may need the consensus sooner than others. */
1289 if (options
->FetchDirInfoExtraEarly
|| authdir_mode_v3(options
)) {
1291 if (min_sec_before_caching
+ dl_interval
> interval
)
1292 dl_interval
= interval
/2;
1294 /* But only in the first half-interval after that. */
1295 dl_interval
= interval
/2;
1298 /* We're an ordinary client or a bridge. Give all the caches enough
1299 * time to download the consensus. */
1300 start
= c
->fresh_until
+ (interval
*3)/4;
1301 /* But download the next one well before this one is expired. */
1302 dl_interval
= ((c
->valid_until
- start
) * 7 )/ 8;
1304 /* If we're a bridge user, make use of the numbers we just computed
1305 * to choose the rest of the interval *after* them. */
1306 if (directory_fetches_dir_info_later(options
)) {
1307 /* Give all the *clients* enough time to download the consensus. */
1308 start
= start
+ dl_interval
+ min_sec_before_caching
;
1309 /* But try to get it before ours actually expires. */
1310 dl_interval
= (c
->valid_until
- start
) - min_sec_before_caching
;
1313 if (dl_interval
< 1)
1315 /* We must not try to replace c while it's still fresh: */
1316 tor_assert(c
->fresh_until
< start
);
1317 /* We must download the next one before c is invalid: */
1318 tor_assert(start
+dl_interval
< c
->valid_until
);
1319 time_to_download_next_consensus
= start
+crypto_rand_int((int)dl_interval
);
1321 char tbuf1
[ISO_TIME_LEN
+1];
1322 char tbuf2
[ISO_TIME_LEN
+1];
1323 char tbuf3
[ISO_TIME_LEN
+1];
1324 format_local_iso_time(tbuf1
, c
->fresh_until
);
1325 format_local_iso_time(tbuf2
, c
->valid_until
);
1326 format_local_iso_time(tbuf3
, time_to_download_next_consensus
);
1327 log_info(LD_DIR
, "Live consensus %s the most recent until %s and will "
1328 "expire at %s; fetching the next one at %s.",
1329 (c
->fresh_until
> now
) ? "will be" : "was",
1330 tbuf1
, tbuf2
, tbuf3
);
1333 time_to_download_next_consensus
= now
;
1334 log_info(LD_DIR
, "No live consensus; we should fetch one immediately.");
1338 /** Return 1 if there's a reason we shouldn't try any directory
1339 * fetches yet (e.g. we demand bridges and none are yet known).
1342 should_delay_dir_fetches(or_options_t
*options
)
1344 if (options
->UseBridges
&& !any_bridge_descriptors_known()) {
1345 log_info(LD_DIR
, "delaying dir fetches (no running bridges known)");
1351 /** Launch requests for networkstatus documents and authority certificates as
1354 update_networkstatus_downloads(time_t now
)
1356 or_options_t
*options
= get_options();
1357 if (should_delay_dir_fetches(options
))
1359 if (directory_fetches_dir_info_early(options
))
1360 update_v2_networkstatus_cache_downloads(now
);
1361 update_consensus_networkstatus_downloads(now
);
1362 update_certificate_downloads(now
);
1365 /** Launch requests as appropriate for missing directory authority
1368 update_certificate_downloads(time_t now
)
1371 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1372 if (consensus_waiting_for_certs
[i
].consensus
)
1373 authority_certs_fetch_missing(consensus_waiting_for_certs
[i
].consensus
,
1377 authority_certs_fetch_missing(current_consensus
, now
);
1380 /** Return 1 if we have a consensus but we don't have enough certificates
1381 * to start using it yet. */
1383 consensus_is_waiting_for_certs(void)
1385 return consensus_waiting_for_certs
[USABLE_CONSENSUS_FLAVOR
].consensus
1389 /** Return the network status with a given identity digest. */
1390 networkstatus_v2_t
*
1391 networkstatus_v2_get_by_digest(const char *digest
)
1393 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
1395 if (!memcmp(ns
->identity_digest
, digest
, DIGEST_LEN
))
1401 /** Return the most recent consensus that we have downloaded, or NULL if we
1402 * don't have one. */
1404 networkstatus_get_latest_consensus(void)
1406 return current_consensus
;
1409 /** Return the most recent consensus that we have downloaded, or NULL if it is
1410 * no longer live. */
1412 networkstatus_get_live_consensus(time_t now
)
1414 if (current_consensus
&&
1415 current_consensus
->valid_after
<= now
&&
1416 now
<= current_consensus
->valid_until
)
1417 return current_consensus
;
1422 /* XXXX remove this in favor of get_live_consensus. But actually,
1423 * leave something like it for bridge users, who need to not totally
1424 * lose if they spend a while fetching a new consensus. */
1425 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1428 networkstatus_get_reasonably_live_consensus(time_t now
)
1430 #define REASONABLY_LIVE_TIME (24*60*60)
1431 if (current_consensus
&&
1432 current_consensus
->valid_after
<= now
&&
1433 now
<= current_consensus
->valid_until
+REASONABLY_LIVE_TIME
)
1434 return current_consensus
;
1439 /** Given two router status entries for the same router identity, return 1 if
1440 * if the contents have changed between them. Otherwise, return 0. */
1442 routerstatus_has_changed(const routerstatus_t
*a
, const routerstatus_t
*b
)
1444 tor_assert(!memcmp(a
->identity_digest
, b
->identity_digest
, DIGEST_LEN
));
1446 return strcmp(a
->nickname
, b
->nickname
) ||
1447 memcmp(a
->descriptor_digest
, b
->descriptor_digest
, DIGEST_LEN
) ||
1448 a
->addr
!= b
->addr
||
1449 a
->or_port
!= b
->or_port
||
1450 a
->dir_port
!= b
->dir_port
||
1451 a
->is_authority
!= b
->is_authority
||
1452 a
->is_exit
!= b
->is_exit
||
1453 a
->is_stable
!= b
->is_stable
||
1454 a
->is_fast
!= b
->is_fast
||
1455 a
->is_running
!= b
->is_running
||
1456 a
->is_named
!= b
->is_named
||
1457 a
->is_unnamed
!= b
->is_unnamed
||
1458 a
->is_valid
!= b
->is_valid
||
1459 a
->is_v2_dir
!= b
->is_v2_dir
||
1460 a
->is_possible_guard
!= b
->is_possible_guard
||
1461 a
->is_bad_exit
!= b
->is_bad_exit
||
1462 a
->is_bad_directory
!= b
->is_bad_directory
||
1463 a
->is_hs_dir
!= b
->is_hs_dir
||
1464 a
->version_known
!= b
->version_known
||
1465 a
->version_supports_begindir
!= b
->version_supports_begindir
||
1466 a
->version_supports_extrainfo_upload
!=
1467 b
->version_supports_extrainfo_upload
||
1468 a
->version_supports_conditional_consensus
!=
1469 b
->version_supports_conditional_consensus
||
1470 a
->version_supports_v3_dir
!= b
->version_supports_v3_dir
;
1473 /** Notify controllers of any router status entries that changed between
1474 * <b>old_c</b> and <b>new_c</b>. */
1476 notify_control_networkstatus_changed(const networkstatus_t
*old_c
,
1477 const networkstatus_t
*new_c
)
1479 smartlist_t
*changed
;
1483 /* tell the controller exactly which relays are still listed, as well
1484 * as what they're listed as */
1485 control_event_newconsensus(new_c
);
1487 if (!control_event_is_interesting(EVENT_NS
))
1491 control_event_networkstatus_changed(new_c
->routerstatus_list
);
1494 changed
= smartlist_create();
1496 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1497 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1498 memcmp(rs_old
->identity_digest
,
1499 rs_new
->identity_digest
, DIGEST_LEN
),
1500 smartlist_add(changed
, rs_new
)) {
1501 if (routerstatus_has_changed(rs_old
, rs_new
))
1502 smartlist_add(changed
, rs_new
);
1503 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1505 control_event_networkstatus_changed(changed
);
1506 smartlist_free(changed
);
1509 /** Copy all the ancillary information (like router download status and so on)
1510 * from <b>old_c</b> to <b>new_c</b>. */
1512 networkstatus_copy_old_consensus_info(networkstatus_t
*new_c
,
1513 const networkstatus_t
*old_c
)
1517 if (!old_c
|| !smartlist_len(old_c
->routerstatus_list
))
1520 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1521 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1522 memcmp(rs_old
->identity_digest
,
1523 rs_new
->identity_digest
, DIGEST_LEN
),
1525 /* Okay, so we're looking at the same identity. */
1526 rs_new
->name_lookup_warned
= rs_old
->name_lookup_warned
;
1527 rs_new
->last_dir_503_at
= rs_old
->last_dir_503_at
;
1529 if (!memcmp(rs_old
->descriptor_digest
, rs_new
->descriptor_digest
,
1531 /* And the same descriptor too! */
1532 memcpy(&rs_new
->dl_status
, &rs_old
->dl_status
,sizeof(download_status_t
));
1534 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1537 /** Try to replace the current cached v3 networkstatus with the one in
1538 * <b>consensus</b>. If we don't have enough certificates to validate it,
1539 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1541 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1542 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1543 * already received, but we were waiting for certificates on it. If flags &
1544 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1545 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1546 * consensus, even if it comes from many days in the past.
1548 * Return 0 on success, <0 on failure. On failure, caller should increment
1549 * the failure count as appropriate.
1551 * We return -1 for mild failures that don't need to be reported to the
1552 * user, and -2 for more serious problems.
1555 networkstatus_set_current_consensus(const char *consensus
,
1559 networkstatus_t
*c
=NULL
;
1561 time_t now
= time(NULL
);
1562 or_options_t
*options
= get_options();
1563 char *unverified_fname
= NULL
, *consensus_fname
= NULL
;
1564 int flav
= networkstatus_parse_flavor_name(flavor
);
1565 const unsigned from_cache
= flags
& NSSET_FROM_CACHE
;
1566 const unsigned was_waiting_for_certs
= flags
& NSSET_WAS_WAITING_FOR_CERTS
;
1567 const unsigned dl_certs
= !(flags
& NSSET_DONT_DOWNLOAD_CERTS
);
1568 const unsigned accept_obsolete
= flags
& NSSET_ACCEPT_OBSOLETE
;
1569 const unsigned require_flavor
= flags
& NSSET_REQUIRE_FLAVOR
;
1570 const digests_t
*current_digests
= NULL
;
1571 consensus_waiting_for_certs_t
*waiting
= NULL
;
1572 time_t current_valid_after
= 0;
1573 int free_consensus
= 1; /* Free 'c' at the end of the function */
1576 /* XXXX we don't handle unrecognized flavors yet. */
1577 log_warn(LD_BUG
, "Unrecognized consensus flavor %s", flavor
);
1581 /* Make sure it's parseable. */
1582 c
= networkstatus_parse_vote_from_string(consensus
, NULL
, NS_TYPE_CONSENSUS
);
1584 log_warn(LD_DIR
, "Unable to parse networkstatus consensus");
1589 if ((int)c
->flavor
!= flav
) {
1590 /* This wasn't the flavor we thought we were getting. */
1591 if (require_flavor
) {
1592 log_warn(LD_DIR
, "Got consensus with unexpected flavor %s (wanted %s)",
1593 networkstatus_get_flavor_name(c
->flavor
), flavor
);
1597 flavor
= networkstatus_get_flavor_name(flav
);
1600 if (flav
!= USABLE_CONSENSUS_FLAVOR
&&
1601 !directory_caches_dir_info(options
)) {
1602 /* This consensus is totally boring to us: we won't use it, and we won't
1603 * serve it. Drop it. */
1607 if (from_cache
&& !accept_obsolete
&&
1608 c
->valid_until
< now
-OLD_ROUTER_DESC_MAX_AGE
) {
1609 /* XXX022 when we try to make fallbackconsensus work again, we should
1610 * consider taking this out. Until then, believing obsolete consensuses
1611 * is causing more harm than good. See also bug 887. */
1612 log_info(LD_DIR
, "Loaded an expired consensus. Discarding.");
1616 if (!strcmp(flavor
, "ns")) {
1617 consensus_fname
= get_datadir_fname("cached-consensus");
1618 unverified_fname
= get_datadir_fname("unverified-consensus");
1619 if (current_consensus
) {
1620 current_digests
= ¤t_consensus
->digests
;
1621 current_valid_after
= current_consensus
->valid_after
;
1626 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
1627 consensus_fname
= get_datadir_fname(buf
);
1628 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
1629 unverified_fname
= get_datadir_fname(buf
);
1630 cur
= dirserv_get_consensus(flavor
);
1632 current_digests
= &cur
->digests
;
1633 current_valid_after
= cur
->published
;
1637 if (current_digests
&&
1638 !memcmp(&c
->digests
, current_digests
, sizeof(c
->digests
))) {
1639 /* We already have this one. That's a failure. */
1640 log_info(LD_DIR
, "Got a %s consensus we already have", flavor
);
1644 if (current_valid_after
&& c
->valid_after
<= current_valid_after
) {
1645 /* We have a newer one. There's no point in accepting this one,
1646 * even if it's great. */
1647 log_info(LD_DIR
, "Got a %s consensus at least as old as the one we have",
1652 /* Make sure it's signed enough. */
1653 if ((r
=networkstatus_check_consensus_signature(c
, 1))<0) {
1655 /* Okay, so it _might_ be signed enough if we get more certificates. */
1656 if (!was_waiting_for_certs
) {
1658 "Not enough certificates to check networkstatus consensus");
1660 if (!current_valid_after
||
1661 c
->valid_after
> current_valid_after
) {
1662 waiting
= &consensus_waiting_for_certs
[flav
];
1663 networkstatus_vote_free(waiting
->consensus
);
1664 tor_free(waiting
->body
);
1665 waiting
->consensus
= c
;
1667 waiting
->body
= tor_strdup(consensus
);
1668 waiting
->set_at
= now
;
1669 waiting
->dl_failed
= 0;
1671 write_str_to_file(unverified_fname
, consensus
, 0);
1674 authority_certs_fetch_missing(c
, now
);
1675 /* This case is not a success or a failure until we get the certs
1676 * or fail to get the certs. */
1679 /* Even if we had enough signatures, we'd never use this as the
1680 * latest consensus. */
1681 if (was_waiting_for_certs
&& from_cache
)
1682 unlink(unverified_fname
);
1686 /* This can never be signed enough: Kill it. */
1687 if (!was_waiting_for_certs
) {
1688 log_warn(LD_DIR
, "Not enough good signatures on networkstatus "
1692 if (was_waiting_for_certs
&& (r
< -1) && from_cache
)
1693 unlink(unverified_fname
);
1698 if (!from_cache
&& flav
== USABLE_CONSENSUS_FLAVOR
)
1699 control_event_client_status(LOG_NOTICE
, "CONSENSUS_ARRIVED");
1701 /* Are we missing any certificates at all? */
1702 if (r
!= 1 && dl_certs
)
1703 authority_certs_fetch_missing(c
, now
);
1705 if (flav
== USABLE_CONSENSUS_FLAVOR
) {
1706 notify_control_networkstatus_changed(current_consensus
, c
);
1708 if (current_consensus
) {
1709 networkstatus_copy_old_consensus_info(c
, current_consensus
);
1710 networkstatus_vote_free(current_consensus
);
1711 /* Defensive programming : we should set current_consensus very soon,
1712 * but we're about to call some stuff in the meantime, and leaving this
1713 * dangling pointer around has proven to be trouble. */
1714 current_consensus
= NULL
;
1718 waiting
= &consensus_waiting_for_certs
[flav
];
1719 if (waiting
->consensus
&&
1720 waiting
->consensus
->valid_after
<= c
->valid_after
) {
1721 networkstatus_vote_free(waiting
->consensus
);
1722 waiting
->consensus
= NULL
;
1723 if (consensus
!= waiting
->body
)
1724 tor_free(waiting
->body
);
1726 waiting
->body
= NULL
;
1727 waiting
->set_at
= 0;
1728 waiting
->dl_failed
= 0;
1729 unlink(unverified_fname
);
1732 /* Reset the failure count only if this consensus is actually valid. */
1733 if (c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1734 download_status_reset(&consensus_dl_status
[flav
]);
1737 download_status_failed(&consensus_dl_status
[flav
], 0);
1740 if (flav
== USABLE_CONSENSUS_FLAVOR
) {
1741 current_consensus
= c
;
1742 free_consensus
= 0; /* Prevent free. */
1744 /* XXXXNM Microdescs: needs a non-ns variant. */
1745 update_consensus_networkstatus_fetch_time(now
);
1746 dirvote_recalculate_timing(options
, now
);
1747 routerstatus_list_update_named_server_map();
1748 cell_ewma_set_scale_factor(options
, current_consensus
);
1750 /* XXX022 where is the right place to put this call? */
1751 connection_or_update_token_buckets(get_connection_array(), options
);
1753 circuit_build_times_new_consensus_params(&circ_times
, current_consensus
);
1756 if (directory_caches_dir_info(options
)) {
1757 dirserv_set_cached_consensus_networkstatus(consensus
,
1764 write_str_to_file(consensus_fname
, consensus
, 0);
1767 if (ftime_definitely_before(now
, current_consensus
->valid_after
)) {
1768 char tbuf
[ISO_TIME_LEN
+1];
1770 long delta
= now
- current_consensus
->valid_after
;
1771 format_iso_time(tbuf
, current_consensus
->valid_after
);
1772 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
1773 log_warn(LD_GENERAL
, "Our clock is %s behind the time published in the "
1774 "consensus network status document (%s GMT). Tor needs an "
1775 "accurate clock to work correctly. Please check your time and "
1776 "date settings!", dbuf
, tbuf
);
1777 control_event_general_status(LOG_WARN
,
1778 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta
);
1781 router_dir_info_changed();
1786 networkstatus_vote_free(c
);
1787 tor_free(consensus_fname
);
1788 tor_free(unverified_fname
);
1792 /** Called when we have gotten more certificates: see whether we can
1793 * now verify a pending consensus. */
1795 networkstatus_note_certs_arrived(void)
1798 for (i
=0; i
<N_CONSENSUS_FLAVORS
; ++i
) {
1799 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
1800 if (!waiting
->consensus
)
1802 if (networkstatus_check_consensus_signature(waiting
->consensus
, 0)>=0) {
1803 if (!networkstatus_set_current_consensus(
1805 networkstatus_get_flavor_name(i
),
1806 NSSET_WAS_WAITING_FOR_CERTS
)) {
1807 tor_free(waiting
->body
);
1813 /** If the network-status list has changed since the last time we called this
1814 * function, update the status of every routerinfo from the network-status
1815 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
1816 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
1819 routers_update_all_from_networkstatus(time_t now
, int dir_version
)
1821 routerlist_t
*rl
= router_get_routerlist();
1822 networkstatus_t
*consensus
= networkstatus_get_live_consensus(now
);
1824 if (networkstatus_v2_list_has_changed
)
1825 download_status_map_update_from_v2_networkstatus();
1827 if (!consensus
|| dir_version
< 3) /* nothing more we should do */
1830 /* calls router_dir_info_changed() when it's done -- more routers
1831 * might be up or down now, which might affect whether there's enough
1832 * directory info. */
1833 routers_update_status_from_consensus_networkstatus(rl
->routers
, 0);
1835 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
,
1836 ri
->cache_info
.routerlist_index
= ri_sl_idx
);
1837 if (rl
->old_routers
)
1838 signed_descs_update_status_from_consensus_networkstatus(rl
->old_routers
);
1840 if (!have_warned_about_old_version
) {
1841 int is_server
= server_mode(get_options());
1842 version_status_t status
;
1843 const char *recommended
= is_server
?
1844 consensus
->server_versions
: consensus
->client_versions
;
1845 status
= tor_version_is_obsolete(VERSION
, recommended
);
1847 if (status
== VS_RECOMMENDED
) {
1848 log_info(LD_GENERAL
, "The directory authorities say my version is ok.");
1849 } else if (status
== VS_EMPTY
) {
1850 log_info(LD_GENERAL
,
1851 "The directory authorities don't recommend any versions.");
1852 } else if (status
== VS_NEW
|| status
== VS_NEW_IN_SERIES
) {
1853 if (!have_warned_about_new_version
) {
1854 log_notice(LD_GENERAL
, "This version of Tor (%s) is newer than any "
1855 "recommended version%s, according to the directory "
1856 "authorities. Recommended versions are: %s",
1858 status
== VS_NEW_IN_SERIES
? " in its series" : "",
1860 have_warned_about_new_version
= 1;
1861 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1862 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1863 VERSION
, "NEW", recommended
);
1866 log_warn(LD_GENERAL
, "Please upgrade! "
1867 "This version of Tor (%s) is %s, according to the directory "
1868 "authorities. Recommended versions are: %s",
1870 status
== VS_OLD
? "obsolete" : "not recommended",
1872 have_warned_about_old_version
= 1;
1873 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1874 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1875 VERSION
, status
== VS_OLD
? "OBSOLETE" : "UNRECOMMENDED",
1881 /** Update v2_download_status_map to contain an entry for every router
1882 * descriptor listed in the v2 networkstatuses. */
1884 download_status_map_update_from_v2_networkstatus(void)
1886 digestmap_t
*dl_status
;
1887 if (!networkstatus_v2_list
)
1889 if (!v2_download_status_map
)
1890 v2_download_status_map
= digestmap_new();
1892 dl_status
= digestmap_new();
1893 SMARTLIST_FOREACH_BEGIN(networkstatus_v2_list
, networkstatus_v2_t
*, ns
) {
1894 SMARTLIST_FOREACH_BEGIN(ns
->entries
, routerstatus_t
*, rs
) {
1895 const char *d
= rs
->descriptor_digest
;
1896 download_status_t
*s
;
1897 if (digestmap_get(dl_status
, d
))
1899 if (!(s
= digestmap_remove(v2_download_status_map
, d
))) {
1900 s
= tor_malloc_zero(sizeof(download_status_t
));
1902 digestmap_set(dl_status
, d
, s
);
1903 } SMARTLIST_FOREACH_END(rs
);
1904 } SMARTLIST_FOREACH_END(ns
);
1905 digestmap_free(v2_download_status_map
, _tor_free
);
1906 v2_download_status_map
= dl_status
;
1907 networkstatus_v2_list_has_changed
= 0;
1910 /** Update our view of the list of named servers from the most recently
1911 * retrieved networkstatus consensus. */
1913 routerstatus_list_update_named_server_map(void)
1915 if (!current_consensus
)
1918 strmap_free(named_server_map
, _tor_free
);
1919 named_server_map
= strmap_new();
1920 strmap_free(unnamed_server_map
, NULL
);
1921 unnamed_server_map
= strmap_new();
1922 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
, routerstatus_t
*, rs
,
1925 strmap_set_lc(named_server_map
, rs
->nickname
,
1926 tor_memdup(rs
->identity_digest
, DIGEST_LEN
));
1928 if (rs
->is_unnamed
) {
1929 strmap_set_lc(unnamed_server_map
, rs
->nickname
, (void*)1);
1934 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
1935 * according to our current consensus networkstatus. May re-order
1936 * <b>routers</b>. */
1938 routers_update_status_from_consensus_networkstatus(smartlist_t
*routers
,
1941 trusted_dir_server_t
*ds
;
1942 or_options_t
*options
= get_options();
1943 int authdir
= authdir_mode_v2(options
) || authdir_mode_v3(options
);
1944 int namingdir
= authdir
&& options
->NamingAuthoritativeDir
;
1945 networkstatus_t
*ns
= current_consensus
;
1946 if (!ns
|| !smartlist_len(ns
->routerstatus_list
))
1948 if (!networkstatus_v2_list
)
1949 networkstatus_v2_list
= smartlist_create();
1951 routers_sort_by_identity(routers
);
1953 SMARTLIST_FOREACH_JOIN(ns
->routerstatus_list
, routerstatus_t
*, rs
,
1954 routers
, routerinfo_t
*, router
,
1955 memcmp(rs
->identity_digest
,
1956 router
->cache_info
.identity_digest
, DIGEST_LEN
),
1958 /* We have no routerstatus for this router. Clear flags and skip it. */
1960 router
->is_named
= 0;
1962 if (router
->purpose
== ROUTER_PURPOSE_GENERAL
)
1963 router_clear_status_flags(router
);
1966 /* We have a routerstatus for this router. */
1967 const char *digest
= router
->cache_info
.identity_digest
;
1969 ds
= router_get_trusteddirserver_by_digest(digest
);
1972 if (rs
->is_named
&& !strcasecmp(router
->nickname
, rs
->nickname
))
1973 router
->is_named
= 1;
1975 router
->is_named
= 0;
1977 /* Is it the same descriptor, or only the same identity? */
1978 if (!memcmp(router
->cache_info
.signed_descriptor_digest
,
1979 rs
->descriptor_digest
, DIGEST_LEN
)) {
1980 if (ns
->valid_until
> router
->cache_info
.last_listed_as_valid_until
)
1981 router
->cache_info
.last_listed_as_valid_until
= ns
->valid_until
;
1985 /* If we're not an authdir, believe others. */
1986 router
->is_valid
= rs
->is_valid
;
1987 router
->is_running
= rs
->is_running
;
1988 router
->is_fast
= rs
->is_fast
;
1989 router
->is_stable
= rs
->is_stable
;
1990 router
->is_possible_guard
= rs
->is_possible_guard
;
1991 router
->is_exit
= rs
->is_exit
;
1992 router
->is_bad_directory
= rs
->is_bad_directory
;
1993 router
->is_bad_exit
= rs
->is_bad_exit
;
1994 router
->is_hs_dir
= rs
->is_hs_dir
;
1996 /* If we _are_ an authority, we should check whether this router
1997 * is one that will cause us to need a reachability test. */
1998 routerinfo_t
*old_router
=
1999 router_get_by_digest(router
->cache_info
.identity_digest
);
2000 if (old_router
!= router
) {
2001 router
->needs_retest_if_added
=
2002 dirserv_should_launch_reachability_test(router
, old_router
);
2005 if (router
->is_running
&& ds
) {
2006 download_status_reset(&ds
->v2_ns_dl_status
);
2008 if (reset_failures
) {
2009 download_status_reset(&rs
->dl_status
);
2011 } SMARTLIST_FOREACH_JOIN_END(rs
, router
);
2013 /* Now update last_listed_as_valid_until from v2 networkstatuses. */
2014 /* XXXX If this is slow, we need to rethink the code. */
2015 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
, {
2016 time_t live_until
= ns
->published_on
+ V2_NETWORKSTATUS_ROUTER_LIFETIME
;
2017 SMARTLIST_FOREACH_JOIN(ns
->entries
, routerstatus_t
*, rs
,
2018 routers
, routerinfo_t
*, ri
,
2019 memcmp(rs
->identity_digest
,
2020 ri
->cache_info
.identity_digest
, DIGEST_LEN
),
2022 if (!memcmp(ri
->cache_info
.signed_descriptor_digest
,
2023 rs
->descriptor_digest
, DIGEST_LEN
)) {
2024 if (live_until
> ri
->cache_info
.last_listed_as_valid_until
)
2025 ri
->cache_info
.last_listed_as_valid_until
= live_until
;
2027 } SMARTLIST_FOREACH_JOIN_END(rs
, ri
);
2030 router_dir_info_changed();
2033 /** Given a list of signed_descriptor_t, update their fields (mainly, when
2034 * they were last listed) from the most recent consensus. */
2036 signed_descs_update_status_from_consensus_networkstatus(smartlist_t
*descs
)
2038 networkstatus_t
*ns
= current_consensus
;
2042 if (!ns
->desc_digest_map
) {
2043 char dummy
[DIGEST_LEN
];
2044 /* instantiates the digest map. */
2045 memset(dummy
, 0, sizeof(dummy
));
2046 router_get_consensus_status_by_descriptor_digest(dummy
);
2048 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, d
,
2050 routerstatus_t
*rs
= digestmap_get(ns
->desc_digest_map
,
2051 d
->signed_descriptor_digest
);
2053 if (ns
->valid_until
> d
->last_listed_as_valid_until
)
2054 d
->last_listed_as_valid_until
= ns
->valid_until
;
2059 /** Generate networkstatus lines for a single routerstatus_t object, and
2060 * return the result in a newly allocated string. Used only by controller
2061 * interface (for now.) */
2063 networkstatus_getinfo_helper_single(routerstatus_t
*rs
)
2065 char buf
[RS_ENTRY_LEN
+1];
2066 routerstatus_format_entry(buf
, sizeof(buf
), rs
, NULL
, NS_CONTROL_PORT
);
2067 return tor_strdup(buf
);
2070 /** Alloc and return a string describing routerstatuses for the most
2071 * recent info of each router we know about that is of purpose
2072 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
2074 * Right now this function is oriented toward listing bridges (you
2075 * shouldn't use this for general-purpose routers, since those
2076 * should be listed from the consensus, not from the routers list). */
2078 networkstatus_getinfo_by_purpose(const char *purpose_string
, time_t now
)
2080 time_t cutoff
= now
- ROUTER_MAX_AGE_TO_PUBLISH
;
2082 routerlist_t
*rl
= router_get_routerlist();
2083 smartlist_t
*statuses
;
2084 uint8_t purpose
= router_purpose_from_string(purpose_string
);
2086 int bridge_auth
= authdir_mode_bridge(get_options());
2088 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
2089 log_info(LD_DIR
, "Unrecognized purpose '%s' when listing router statuses.",
2094 statuses
= smartlist_create();
2095 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
, {
2096 if (ri
->cache_info
.published_on
< cutoff
)
2098 if (ri
->purpose
!= purpose
)
2100 if (bridge_auth
&& ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
2101 dirserv_set_router_is_running(ri
, now
);
2102 /* then generate and write out status lines for each of them */
2103 set_routerstatus_from_routerinfo(&rs
, ri
, now
, 0, 0, 0);
2104 smartlist_add(statuses
, networkstatus_getinfo_helper_single(&rs
));
2107 answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2108 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2109 smartlist_free(statuses
);
2113 /** Write out router status entries for all our bridge descriptors. */
2115 networkstatus_dump_bridge_status_to_file(time_t now
)
2117 char *status
= networkstatus_getinfo_by_purpose("bridge", now
);
2118 or_options_t
*options
= get_options();
2119 size_t len
= strlen(options
->DataDirectory
) + 32;
2120 char *fname
= tor_malloc(len
);
2121 tor_snprintf(fname
, len
, "%s"PATH_SEPARATOR
"networkstatus-bridges",
2122 options
->DataDirectory
);
2123 write_str_to_file(fname
,status
,0);
2129 get_net_param_from_list(smartlist_t
*net_params
, const char *param_name
,
2132 size_t name_len
= strlen(param_name
);
2134 SMARTLIST_FOREACH_BEGIN(net_params
, const char *, p
) {
2135 if (!strcmpstart(p
, param_name
) && p
[name_len
] == '=') {
2137 long v
= tor_parse_long(p
+name_len
+1, 10, INT32_MIN
,
2138 INT32_MAX
, &ok
, NULL
);
2142 } SMARTLIST_FOREACH_END(p
);
2147 /** Return the value of a integer parameter from the networkstatus <b>ns</b>
2148 * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
2149 * latest consensus ourselves. Return <b>default_val</b> if no latest
2150 * consensus, or if it has no parameter called <b>param_name</b>. */
2152 networkstatus_get_param(networkstatus_t
*ns
, const char *param_name
,
2153 int32_t default_val
)
2155 if (!ns
) /* if they pass in null, go find it ourselves */
2156 ns
= networkstatus_get_latest_consensus();
2158 if (!ns
|| !ns
->net_params
)
2161 return get_net_param_from_list(ns
->net_params
, param_name
, default_val
);
2164 /** Return the value of a integer bw weight parameter from the networkstatus
2165 * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
2166 * loading the latest consensus ourselves. Return <b>default_val</b> if no
2167 * latest consensus, or if it has no parameter called <b>param_name</b>. */
2169 networkstatus_get_bw_weight(networkstatus_t
*ns
, const char *weight_name
,
2170 int32_t default_val
)
2172 if (!ns
) /* if they pass in null, go find it ourselves */
2173 ns
= networkstatus_get_latest_consensus();
2175 if (!ns
|| !ns
->weight_params
)
2178 return get_net_param_from_list(ns
->weight_params
, weight_name
, default_val
);
2181 /** Return the name of the consensus flavor <b>flav</b> as used to identify
2182 * the flavor in directory documents. */
2184 networkstatus_get_flavor_name(consensus_flavor_t flav
)
2189 case FLAV_MICRODESC
:
2192 tor_fragile_assert();
2197 /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
2198 * or -1 if the flavor is not recognized. */
2200 networkstatus_parse_flavor_name(const char *flavname
)
2202 if (!strcmp(flavname
, "ns"))
2204 else if (!strcmp(flavname
, "microdesc"))
2205 return FLAV_MICRODESC
;
2210 /** If <b>question</b> is a string beginning with "ns/" in a format the
2211 * control interface expects for a GETINFO question, set *<b>answer</b> to a
2212 * newly-allocated string containing networkstatus lines for the appropriate
2213 * ORs. Return 0 on success, -1 on unrecognized question format. */
2215 getinfo_helper_networkstatus(control_connection_t
*conn
,
2216 const char *question
, char **answer
,
2217 const char **errmsg
)
2219 routerstatus_t
*status
;
2222 if (!current_consensus
) {
2223 *answer
= tor_strdup("");
2227 if (!strcmp(question
, "ns/all")) {
2228 smartlist_t
*statuses
= smartlist_create();
2229 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
2230 routerstatus_t
*, rs
,
2232 smartlist_add(statuses
, networkstatus_getinfo_helper_single(rs
));
2234 *answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2235 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2236 smartlist_free(statuses
);
2238 } else if (!strcmpstart(question
, "ns/id/")) {
2241 if (base16_decode(d
, DIGEST_LEN
, question
+6, strlen(question
+6))) {
2242 *errmsg
= "Data not decodeable as hex";
2245 status
= router_get_consensus_status_by_id(d
);
2246 } else if (!strcmpstart(question
, "ns/name/")) {
2247 status
= router_get_consensus_status_by_nickname(question
+8, 0);
2248 } else if (!strcmpstart(question
, "ns/purpose/")) {
2249 *answer
= networkstatus_getinfo_by_purpose(question
+11, time(NULL
));
2250 return *answer
? 0 : -1;
2256 *answer
= networkstatus_getinfo_helper_single(status
);
2260 /** Free all storage held locally in this module. */
2262 networkstatus_free_all(void)
2265 if (networkstatus_v2_list
) {
2266 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
2267 networkstatus_v2_free(ns
));
2268 smartlist_free(networkstatus_v2_list
);
2269 networkstatus_v2_list
= NULL
;
2272 digestmap_free(v2_download_status_map
, _tor_free
);
2273 v2_download_status_map
= NULL
;
2274 networkstatus_vote_free(current_consensus
);
2275 current_consensus
= NULL
;
2277 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2278 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2279 if (waiting
->consensus
) {
2280 networkstatus_vote_free(waiting
->consensus
);
2281 waiting
->consensus
= NULL
;
2283 tor_free(waiting
->body
);
2286 strmap_free(named_server_map
, _tor_free
);
2287 strmap_free(unnamed_server_map
, NULL
);