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-2010, 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 typedef struct consensus_waiting_for_certs_t
{
39 /** The consensus itself. */
40 networkstatus_t
*consensus
;
41 /** The encoded version of the consensus, nul-terminated. */
43 /** When did we set the current value of consensus_waiting_for_certs? If
44 * this is too recent, we shouldn't try to fetch a new consensus for a
45 * little while, to give ourselves time to get certificates for this one. */
47 /** Set to 1 if we've been holding on to it for so long we should maybe
48 * treat it as being bad. */
50 } consensus_waiting_for_certs_t
;
52 static consensus_waiting_for_certs_t
53 consensus_waiting_for_certs
[N_CONSENSUS_FLAVORS
];
55 /** The last time we tried to download a networkstatus, or 0 for "never". We
56 * use this to rate-limit download attempts for directory caches (including
57 * mirrors). Clients don't use this now. */
58 static time_t last_networkstatus_download_attempted
= 0;
60 /** A time before which we shouldn't try to replace the current consensus:
61 * this will be at some point after the next consensus becomes valid, but
62 * before the current consensus becomes invalid. */
63 static time_t time_to_download_next_consensus
= 0;
64 /** Download status for the current consensus networkstatus. */
65 static download_status_t consensus_dl_status
[N_CONSENSUS_FLAVORS
];
67 /** True iff we have logged a warning about this OR's version being older than
68 * listed by the authorities. */
69 static int have_warned_about_old_version
= 0;
70 /** True iff we have logged a warning about this OR's version being newer than
71 * listed by the authorities. */
72 static int have_warned_about_new_version
= 0;
74 static void download_status_map_update_from_v2_networkstatus(void);
75 static void routerstatus_list_update_named_server_map(void);
77 /** Forget that we've warned about anything networkstatus-related, so we will
78 * give fresh warnings if the same behavior happens again. */
80 networkstatus_reset_warnings(void)
82 if (current_consensus
) {
83 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
85 rs
->name_lookup_warned
= 0);
88 have_warned_about_old_version
= 0;
89 have_warned_about_new_version
= 0;
92 /** Reset the descriptor download failure count on all networkstatus docs, so
93 * that we can retry any long-failed documents immediately.
96 networkstatus_reset_download_failures(void)
99 const smartlist_t
*networkstatus_v2_list
= networkstatus_get_v2_list();
100 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
101 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
103 if (!router_get_by_descriptor_digest(rs
->descriptor_digest
))
104 rs
->need_to_mirror
= 1;
107 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
)
108 download_status_reset(&consensus_dl_status
[i
]);
109 if (v2_download_status_map
) {
110 digestmap_iter_t
*iter
;
111 digestmap_t
*map
= v2_download_status_map
;
114 download_status_t
*dls
;
115 for (iter
= digestmap_iter_init(map
); !digestmap_iter_done(iter
);
116 iter
= digestmap_iter_next(map
, iter
) ) {
117 digestmap_iter_get(iter
, &key
, &val
);
119 download_status_reset(dls
);
124 /** Repopulate our list of network_status_t objects from the list cached on
125 * disk. Return 0 on success, -1 on failure. */
127 router_reload_v2_networkstatus(void)
129 smartlist_t
*entries
;
132 char *filename
= get_datadir_fname("cached-status");
133 int maybe_delete
= !directory_caches_v2_dir_info(get_options());
134 time_t now
= time(NULL
);
135 if (!networkstatus_v2_list
)
136 networkstatus_v2_list
= smartlist_create();
138 entries
= tor_listdir(filename
);
139 if (!entries
) { /* dir doesn't exist */
142 } else if (!smartlist_len(entries
) && maybe_delete
) {
145 smartlist_free(entries
);
149 SMARTLIST_FOREACH(entries
, const char *, fn
, {
150 char buf
[DIGEST_LEN
];
152 filename
= get_datadir_fname2("cached-status", fn
);
153 remove_file_if_very_old(filename
, now
);
157 if (strlen(fn
) != HEX_DIGEST_LEN
||
158 base16_decode(buf
, sizeof(buf
), fn
, strlen(fn
))) {
160 "Skipping cached-status file with unexpected name \"%s\"",fn
);
163 filename
= get_datadir_fname2("cached-status", fn
);
164 s
= read_file_to_str(filename
, 0, &st
);
166 if (router_set_networkstatus_v2(s
, st
.st_mtime
, NS_FROM_CACHE
,
168 log_warn(LD_FS
, "Couldn't load networkstatus from \"%s\"",filename
);
174 SMARTLIST_FOREACH(entries
, char *, fn
, tor_free(fn
));
175 smartlist_free(entries
);
176 networkstatus_v2_list_clean(time(NULL
));
177 routers_update_all_from_networkstatus(time(NULL
), 2);
181 /** Read every cached v3 consensus networkstatus from the disk. */
183 router_reload_consensus_networkstatus(void)
188 or_options_t
*options
= get_options();
189 const unsigned int flags
= NSSET_FROM_CACHE
| NSSET_DONT_DOWNLOAD_CERTS
;
192 /* FFFF Suppress warnings if cached consensus is bad? */
193 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
195 const char *flavor
= networkstatus_get_flavor_name(flav
);
196 if (flav
== FLAV_NS
) {
197 filename
= get_datadir_fname("cached-consensus");
199 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
200 filename
= get_datadir_fname(buf
);
202 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
204 if (networkstatus_set_current_consensus(s
, flavor
, flags
) < -1) {
205 log_warn(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
212 if (flav
== FLAV_NS
) {
213 filename
= get_datadir_fname("unverified-consensus");
215 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
216 filename
= get_datadir_fname(buf
);
219 s
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
221 if (networkstatus_set_current_consensus(s
, flavor
,
222 flags
|NSSET_WAS_WAITING_FOR_CERTS
)) {
223 log_info(LD_FS
, "Couldn't load consensus %s networkstatus from \"%s\"",
231 if (!current_consensus
||
232 (stat(options
->FallbackNetworkstatusFile
, &st
)==0 &&
233 st
.st_mtime
> current_consensus
->valid_after
)) {
234 s
= read_file_to_str(options
->FallbackNetworkstatusFile
,
235 RFTS_IGNORE_MISSING
, NULL
);
237 if (networkstatus_set_current_consensus(s
, "ns",
238 flags
|NSSET_ACCEPT_OBSOLETE
)) {
239 log_info(LD_FS
, "Couldn't load consensus networkstatus from \"%s\"",
240 options
->FallbackNetworkstatusFile
);
243 "Loaded fallback consensus networkstatus from \"%s\"",
244 options
->FallbackNetworkstatusFile
);
250 if (!current_consensus
) {
251 if (!named_server_map
)
252 named_server_map
= strmap_new();
253 if (!unnamed_server_map
)
254 unnamed_server_map
= strmap_new();
257 update_certificate_downloads(time(NULL
));
259 routers_update_all_from_networkstatus(time(NULL
), 3);
264 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
266 vote_routerstatus_free(vote_routerstatus_t
*rs
)
268 vote_microdesc_hash_t
*h
, *next
;
271 tor_free(rs
->version
);
272 tor_free(rs
->status
.exitsummary
);
273 for (h
= rs
->microdesc
; h
; h
= next
) {
274 tor_free(h
->microdesc_hash_line
);
281 /** Free all storage held by the routerstatus object <b>rs</b>. */
283 routerstatus_free(routerstatus_t
*rs
)
287 tor_free(rs
->exitsummary
);
291 /** Free all storage held by the networkstatus object <b>ns</b>. */
293 networkstatus_v2_free(networkstatus_v2_t
*ns
)
297 tor_free(ns
->source_address
);
298 tor_free(ns
->contact
);
300 crypto_free_pk_env(ns
->signing_key
);
301 tor_free(ns
->client_versions
);
302 tor_free(ns
->server_versions
);
304 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
305 routerstatus_free(rs
));
306 smartlist_free(ns
->entries
);
311 /** Free all storage held in <b>sig</b> */
313 document_signature_free(document_signature_t
*sig
)
315 tor_free(sig
->signature
);
319 /** Return a newly allocated copy of <b>sig</b> */
320 document_signature_t
*
321 document_signature_dup(const document_signature_t
*sig
)
323 document_signature_t
*r
= tor_memdup(sig
, sizeof(document_signature_t
));
325 r
->signature
= tor_memdup(sig
->signature
, sig
->signature_len
);
329 /** Free all storage held in <b>ns</b>. */
331 networkstatus_vote_free(networkstatus_t
*ns
)
336 tor_free(ns
->client_versions
);
337 tor_free(ns
->server_versions
);
338 if (ns
->known_flags
) {
339 SMARTLIST_FOREACH(ns
->known_flags
, char *, c
, tor_free(c
));
340 smartlist_free(ns
->known_flags
);
342 if (ns
->net_params
) {
343 SMARTLIST_FOREACH(ns
->net_params
, char *, c
, tor_free(c
));
344 smartlist_free(ns
->net_params
);
346 if (ns
->supported_methods
) {
347 SMARTLIST_FOREACH(ns
->supported_methods
, char *, c
, tor_free(c
));
348 smartlist_free(ns
->supported_methods
);
351 SMARTLIST_FOREACH_BEGIN(ns
->voters
, networkstatus_voter_info_t
*, voter
) {
352 tor_free(voter
->nickname
);
353 tor_free(voter
->address
);
354 tor_free(voter
->contact
);
356 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
357 document_signature_free(sig
));
358 smartlist_free(voter
->sigs
);
361 } SMARTLIST_FOREACH_END(voter
);
362 smartlist_free(ns
->voters
);
364 authority_cert_free(ns
->cert
);
366 if (ns
->routerstatus_list
) {
367 if (ns
->type
== NS_TYPE_VOTE
|| ns
->type
== NS_TYPE_OPINION
) {
368 SMARTLIST_FOREACH(ns
->routerstatus_list
, vote_routerstatus_t
*, rs
,
369 vote_routerstatus_free(rs
));
371 SMARTLIST_FOREACH(ns
->routerstatus_list
, routerstatus_t
*, rs
,
372 routerstatus_free(rs
));
375 smartlist_free(ns
->routerstatus_list
);
378 digestmap_free(ns
->desc_digest_map
, NULL
);
380 memset(ns
, 11, sizeof(*ns
));
384 /** Return the voter info from <b>vote</b> for the voter whose identity digest
385 * is <b>identity</b>, or NULL if no such voter is associated with
387 networkstatus_voter_info_t
*
388 networkstatus_get_voter_by_id(networkstatus_t
*vote
,
389 const char *identity
)
391 if (!vote
|| !vote
->voters
)
393 SMARTLIST_FOREACH(vote
->voters
, networkstatus_voter_info_t
*, voter
,
394 if (!memcmp(voter
->identity_digest
, identity
, DIGEST_LEN
))
399 /** Check whether the signature <b>sig</b> is correctly signed with the
400 * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
401 * signing key; otherwise set the good_signature or bad_signature flag on
402 * <b>voter</b>, and return 0. */
404 networkstatus_check_document_signature(const networkstatus_t
*consensus
,
405 document_signature_t
*sig
,
406 const authority_cert_t
*cert
)
408 char key_digest
[DIGEST_LEN
];
409 const int dlen
= sig
->alg
== DIGEST_SHA1
? DIGEST_LEN
: DIGEST256_LEN
;
411 size_t signed_digest_len
;
413 if (crypto_pk_get_digest(cert
->signing_key
, key_digest
)<0)
415 if (memcmp(sig
->signing_key_digest
, key_digest
, DIGEST_LEN
) ||
416 memcmp(sig
->identity_digest
, cert
->cache_info
.identity_digest
,
420 signed_digest_len
= crypto_pk_keysize(cert
->signing_key
);
421 signed_digest
= tor_malloc(signed_digest_len
);
422 if (crypto_pk_public_checksig(cert
->signing_key
,
425 sig
->signature_len
) < dlen
||
426 memcmp(signed_digest
, consensus
->digests
.d
[sig
->alg
], dlen
)) {
427 log_warn(LD_DIR
, "Got a bad signature on a networkstatus vote");
428 sig
->bad_signature
= 1;
430 sig
->good_signature
= 1;
432 tor_free(signed_digest
);
436 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
437 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
438 * signature from every recognized authority on it, 0 if there are
439 * enough good signatures from recognized authorities on it, -1 if we might
440 * get enough good signatures by fetching missing certificates, and -2
441 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
442 * about every problem; if warn is at least 1, warn only if we can't get
443 * enough signatures; if warn is negative, log nothing at all. */
445 networkstatus_check_consensus_signature(networkstatus_t
*consensus
,
449 int n_missing_key
= 0;
452 int n_no_signature
= 0;
453 int n_v3_authorities
= get_n_authorities(V3_AUTHORITY
);
454 int n_required
= n_v3_authorities
/2 + 1;
455 smartlist_t
*need_certs_from
= smartlist_create();
456 smartlist_t
*unrecognized
= smartlist_create();
457 smartlist_t
*missing_authorities
= smartlist_create();
459 time_t now
= time(NULL
);
461 tor_assert(consensus
->type
== NS_TYPE_CONSENSUS
);
463 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*,
467 int missing_key_here
= 0;
468 SMARTLIST_FOREACH_BEGIN(voter
->sigs
, document_signature_t
*, sig
) {
469 if (!sig
->good_signature
&& !sig
->bad_signature
&&
471 /* we can try to check the signature. */
472 int is_v3_auth
= trusteddirserver_get_by_v3_auth_digest(
473 sig
->identity_digest
) != NULL
;
474 authority_cert_t
*cert
=
475 authority_cert_get_by_digests(sig
->identity_digest
,
476 sig
->signing_key_digest
);
477 tor_assert(!memcmp(sig
->identity_digest
, voter
->identity_digest
,
481 smartlist_add(unrecognized
, voter
);
484 } else if (!cert
|| cert
->expires
< now
) {
485 smartlist_add(need_certs_from
, voter
);
489 if (networkstatus_check_document_signature(consensus
, sig
, cert
) < 0) {
490 smartlist_add(need_certs_from
, voter
);
495 if (sig
->good_signature
)
497 else if (sig
->bad_signature
)
499 } SMARTLIST_FOREACH_END(sig
);
504 else if (missing_key_here
)
508 } SMARTLIST_FOREACH_END(voter
);
510 /* Now see whether we're missing any voters entirely. */
511 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
512 trusted_dir_server_t
*, ds
,
514 if ((ds
->type
& V3_AUTHORITY
) &&
515 !networkstatus_get_voter_by_id(consensus
, ds
->v3_identity_digest
))
516 smartlist_add(missing_authorities
, ds
);
519 if (warn
> 1 || (warn
>= 0 && n_good
< n_required
))
525 SMARTLIST_FOREACH(unrecognized
, networkstatus_voter_info_t
*, voter
,
527 log_info(LD_DIR
, "Consensus includes unrecognized authority '%s' "
528 "at %s:%d (contact %s; identity %s)",
529 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
530 voter
->contact
?voter
->contact
:"n/a",
531 hex_str(voter
->identity_digest
, DIGEST_LEN
));
533 SMARTLIST_FOREACH(need_certs_from
, networkstatus_voter_info_t
*, voter
,
535 log_info(LD_DIR
, "Looks like we need to download a new certificate "
536 "from authority '%s' at %s:%d (contact %s; identity %s)",
537 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
538 voter
->contact
?voter
->contact
:"n/a",
539 hex_str(voter
->identity_digest
, DIGEST_LEN
));
541 SMARTLIST_FOREACH(missing_authorities
, trusted_dir_server_t
*, ds
,
543 log_info(LD_DIR
, "Consensus does not include configured "
544 "authority '%s' at %s:%d (identity %s)",
545 ds
->nickname
, ds
->address
, (int)ds
->dir_port
,
546 hex_str(ds
->v3_identity_digest
, DIGEST_LEN
));
548 log(severity
, LD_DIR
,
549 "%d unknown, %d missing key, %d good, %d bad, %d no signature, "
550 "%d required", n_unknown
, n_missing_key
, n_good
, n_bad
,
551 n_no_signature
, n_required
);
554 smartlist_free(unrecognized
);
555 smartlist_free(need_certs_from
);
556 smartlist_free(missing_authorities
);
558 if (n_good
== n_v3_authorities
)
560 else if (n_good
>= n_required
)
562 else if (n_good
+ n_missing_key
>= n_required
)
568 /** Helper: return a newly allocated string containing the name of the filename
569 * where we plan to cache the network status with the given identity digest. */
571 networkstatus_get_cache_filename(const char *identity_digest
)
573 char fp
[HEX_DIGEST_LEN
+1];
574 base16_encode(fp
, HEX_DIGEST_LEN
+1, identity_digest
, DIGEST_LEN
);
575 return get_datadir_fname2("cached-status", fp
);
578 /** Helper for smartlist_sort: Compare two networkstatus objects by
579 * publication date. */
581 _compare_networkstatus_v2_published_on(const void **_a
, const void **_b
)
583 const networkstatus_v2_t
*a
= *_a
, *b
= *_b
;
584 if (a
->published_on
< b
->published_on
)
586 else if (a
->published_on
> b
->published_on
)
592 /** Add the parsed v2 networkstatus in <b>ns</b> (with original document in
593 * <b>s</b>) to the disk cache (and the in-memory directory server cache) as
596 add_networkstatus_to_cache(const char *s
,
597 v2_networkstatus_source_t source
,
598 networkstatus_v2_t
*ns
)
600 if (source
!= NS_FROM_CACHE
) {
601 char *fn
= networkstatus_get_cache_filename(ns
->identity_digest
);
602 if (write_str_to_file(fn
, s
, 0)<0) {
603 log_notice(LD_FS
, "Couldn't write cached network status to \"%s\"", fn
);
608 if (directory_caches_v2_dir_info(get_options()))
609 dirserv_set_cached_networkstatus_v2(s
,
616 /** How far in the future do we allow a network-status to get before removing
618 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
620 /** Given a string <b>s</b> containing a network status that we received at
621 * <b>arrived_at</b> from <b>source</b>, try to parse it, see if we want to
622 * store it, and put it into our cache as necessary.
624 * If <b>source</b> is NS_FROM_DIR or NS_FROM_CACHE, do not replace our
625 * own networkstatus_t (if we're an authoritative directory server).
627 * If <b>source</b> is NS_FROM_CACHE, do not write our networkstatus_t to the
630 * If <b>requested_fingerprints</b> is provided, it must contain a list of
631 * uppercased identity fingerprints. Do not update any networkstatus whose
632 * fingerprint is not on the list; after updating a networkstatus, remove its
633 * fingerprint from the list.
635 * Return 0 on success, -1 on failure.
637 * Callers should make sure that routers_update_all_from_networkstatus() is
638 * invoked after this function succeeds.
641 router_set_networkstatus_v2(const char *s
, time_t arrived_at
,
642 v2_networkstatus_source_t source
,
643 smartlist_t
*requested_fingerprints
)
645 networkstatus_v2_t
*ns
;
649 trusted_dir_server_t
*trusted_dir
= NULL
;
650 const char *source_desc
= NULL
;
651 char fp
[HEX_DIGEST_LEN
+1];
652 char published
[ISO_TIME_LEN
+1];
654 if (!directory_caches_v2_dir_info(get_options()))
655 return 0; /* Don't bother storing it. */
657 ns
= networkstatus_v2_parse_from_string(s
);
659 log_warn(LD_DIR
, "Couldn't parse network status.");
662 base16_encode(fp
, HEX_DIGEST_LEN
+1, ns
->identity_digest
, DIGEST_LEN
);
664 router_get_trusteddirserver_by_digest(ns
->identity_digest
)) ||
665 !(trusted_dir
->type
& V2_AUTHORITY
)) {
666 log_info(LD_DIR
, "Network status was signed, but not by an authoritative "
667 "directory we recognize.");
670 source_desc
= trusted_dir
->description
;
673 if (arrived_at
> now
)
676 ns
->received_on
= arrived_at
;
678 format_iso_time(published
, ns
->published_on
);
680 if (ns
->published_on
> now
+ NETWORKSTATUS_ALLOW_SKEW
) {
682 long delta
= now
- ns
->published_on
;
683 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
684 log_warn(LD_GENERAL
, "Network status from %s was published %s in the "
685 "future (%s GMT). Check your time and date settings! "
687 source_desc
, dbuf
, published
);
688 control_event_general_status(LOG_WARN
,
689 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=NETWORKSTATUS:%s:%d",
690 delta
, ns
->source_address
, ns
->source_dirport
);
694 if (!networkstatus_v2_list
)
695 networkstatus_v2_list
= smartlist_create();
697 if ( (source
== NS_FROM_DIR_BY_FP
|| source
== NS_FROM_DIR_ALL
) &&
698 router_digest_is_me(ns
->identity_digest
)) {
699 /* Don't replace our own networkstatus when we get it from somebody else.*/
700 networkstatus_v2_free(ns
);
704 if (requested_fingerprints
) {
705 if (smartlist_string_isin(requested_fingerprints
, fp
)) {
706 smartlist_string_remove(requested_fingerprints
, fp
);
708 if (source
!= NS_FROM_DIR_ALL
) {
710 smartlist_join_strings(requested_fingerprints
," ",0,NULL
);
712 "We received a network status with a fingerprint (%s) that we "
713 "never requested. (We asked for: %s.) Dropping.",
723 /* We got a non-trusted networkstatus, and we're a directory cache.
724 * This means that we asked an authority, and it told us about another
725 * authority we didn't recognize. */
727 "We do not recognize authority (%s) but we are willing "
729 add_networkstatus_to_cache(s
, source
, ns
);
730 networkstatus_v2_free(ns
);
736 for (i
=0; i
< smartlist_len(networkstatus_v2_list
); ++i
) {
737 networkstatus_v2_t
*old_ns
= smartlist_get(networkstatus_v2_list
, i
);
739 if (!memcmp(old_ns
->identity_digest
, ns
->identity_digest
, DIGEST_LEN
)) {
740 if (!memcmp(old_ns
->networkstatus_digest
,
741 ns
->networkstatus_digest
, DIGEST_LEN
)) {
742 /* Same one we had before. */
743 networkstatus_v2_free(ns
);
744 tor_assert(trusted_dir
);
746 "Not replacing network-status from %s (published %s); "
747 "we already have it.",
748 trusted_dir
->description
, published
);
749 if (old_ns
->received_on
< arrived_at
) {
750 if (source
!= NS_FROM_CACHE
) {
752 fn
= networkstatus_get_cache_filename(old_ns
->identity_digest
);
753 /* We use mtime to tell when it arrived, so update that. */
757 old_ns
->received_on
= arrived_at
;
759 download_status_failed(&trusted_dir
->v2_ns_dl_status
, 0);
761 } else if (old_ns
->published_on
>= ns
->published_on
) {
762 char old_published
[ISO_TIME_LEN
+1];
763 format_iso_time(old_published
, old_ns
->published_on
);
764 tor_assert(trusted_dir
);
766 "Not replacing network-status from %s (published %s);"
767 " we have a newer one (published %s) for this authority.",
768 trusted_dir
->description
, published
,
770 networkstatus_v2_free(ns
);
771 download_status_failed(&trusted_dir
->v2_ns_dl_status
, 0);
774 networkstatus_v2_free(old_ns
);
775 smartlist_set(networkstatus_v2_list
, i
, ns
);
782 if (source
!= NS_FROM_CACHE
&& trusted_dir
) {
783 download_status_reset(&trusted_dir
->v2_ns_dl_status
);
787 smartlist_add(networkstatus_v2_list
, ns
);
789 /** Retain any routerinfo mentioned in a V2 networkstatus for at least this
791 #define V2_NETWORKSTATUS_ROUTER_LIFETIME (3*60*60)
794 time_t live_until
= ns
->published_on
+ V2_NETWORKSTATUS_ROUTER_LIFETIME
;
795 SMARTLIST_FOREACH(ns
->entries
, routerstatus_t
*, rs
,
797 signed_descriptor_t
*sd
=
798 router_get_by_descriptor_digest(rs
->descriptor_digest
);
800 if (sd
->last_listed_as_valid_until
< live_until
)
801 sd
->last_listed_as_valid_until
= live_until
;
803 rs
->need_to_mirror
= 1;
808 log_info(LD_DIR
, "Setting networkstatus %s %s (published %s)",
809 source
== NS_FROM_CACHE
?"cached from":
810 ((source
== NS_FROM_DIR_BY_FP
|| source
== NS_FROM_DIR_ALL
) ?
811 "downloaded from":"generated for"),
812 trusted_dir
->description
, published
);
813 networkstatus_v2_list_has_changed
= 1;
815 smartlist_sort(networkstatus_v2_list
,
816 _compare_networkstatus_v2_published_on
);
819 add_networkstatus_to_cache(s
, source
, ns
);
824 /** Remove all very-old network_status_t objects from memory and from the
827 networkstatus_v2_list_clean(time_t now
)
830 if (!networkstatus_v2_list
)
833 for (i
= 0; i
< smartlist_len(networkstatus_v2_list
); ++i
) {
834 networkstatus_v2_t
*ns
= smartlist_get(networkstatus_v2_list
, i
);
836 if (ns
->published_on
+ MAX_NETWORKSTATUS_AGE
> now
)
838 /* Okay, this one is too old. Remove it from the list, and delete it
840 smartlist_del(networkstatus_v2_list
, i
--);
841 fname
= networkstatus_get_cache_filename(ns
->identity_digest
);
842 if (file_status(fname
) == FN_FILE
) {
843 log_info(LD_DIR
, "Removing too-old networkstatus in %s", fname
);
847 if (directory_caches_v2_dir_info(get_options())) {
848 dirserv_set_cached_networkstatus_v2(NULL
, ns
->identity_digest
, 0);
850 networkstatus_v2_free(ns
);
853 /* And now go through the directory cache for any cached untrusted
854 * networkstatuses and other network info. */
855 dirserv_clear_old_networkstatuses(now
- MAX_NETWORKSTATUS_AGE
);
856 dirserv_clear_old_v1_info(now
);
859 /** Helper for bsearching a list of routerstatus_t pointers: compare a
860 * digest in the key to the identity digest of a routerstatus_t. */
862 compare_digest_to_routerstatus_entry(const void *_key
, const void **_member
)
864 const char *key
= _key
;
865 const routerstatus_t
*rs
= *_member
;
866 return memcmp(key
, rs
->identity_digest
, DIGEST_LEN
);
869 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
870 * NULL if none was found. */
872 networkstatus_v2_find_entry(networkstatus_v2_t
*ns
, const char *digest
)
874 return smartlist_bsearch(ns
->entries
, digest
,
875 compare_digest_to_routerstatus_entry
);
878 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
879 * NULL if none was found. */
881 networkstatus_vote_find_entry(networkstatus_t
*ns
, const char *digest
)
883 return smartlist_bsearch(ns
->routerstatus_list
, digest
,
884 compare_digest_to_routerstatus_entry
);
887 /*XXXX make this static once functions are moved into this file. */
888 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
889 * <b>digest</b>. Return value and set *<b>found_out</b> as for
890 * smartlist_bsearch_idx(). */
892 networkstatus_vote_find_entry_idx(networkstatus_t
*ns
,
893 const char *digest
, int *found_out
)
895 return smartlist_bsearch_idx(ns
->routerstatus_list
, digest
,
896 compare_digest_to_routerstatus_entry
,
900 /** Return a list of the v2 networkstatus documents. */
902 networkstatus_get_v2_list(void)
904 if (!networkstatus_v2_list
)
905 networkstatus_v2_list
= smartlist_create();
906 return networkstatus_v2_list
;
909 /** Return the consensus view of the status of the router whose current
910 * <i>descriptor</i> digest is <b>digest</b>, or NULL if no such router is
913 router_get_consensus_status_by_descriptor_digest(const char *digest
)
915 if (!current_consensus
) return NULL
;
916 if (!current_consensus
->desc_digest_map
) {
917 digestmap_t
* m
= current_consensus
->desc_digest_map
= digestmap_new();
918 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
919 routerstatus_t
*, rs
,
921 digestmap_set(m
, rs
->descriptor_digest
, rs
);
924 return digestmap_get(current_consensus
->desc_digest_map
, digest
);
927 /** Given the digest of a router descriptor, return its current download
928 * status, or NULL if the digest is unrecognized. */
930 router_get_dl_status_by_descriptor_digest(const char *d
)
933 if ((rs
= router_get_consensus_status_by_descriptor_digest(d
)))
934 return &rs
->dl_status
;
935 if (v2_download_status_map
)
936 return digestmap_get(v2_download_status_map
, d
);
941 /** Return the consensus view of the status of the router whose identity
942 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
944 router_get_consensus_status_by_id(const char *digest
)
946 if (!current_consensus
)
948 return smartlist_bsearch(current_consensus
->routerstatus_list
, digest
,
949 compare_digest_to_routerstatus_entry
);
952 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
953 * the corresponding routerstatus_t, or NULL if none exists. Warn the
954 * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
955 * nickname, but the Named flag isn't set for that router. */
957 router_get_consensus_status_by_nickname(const char *nickname
,
960 char digest
[DIGEST_LEN
];
961 routerstatus_t
*best
=NULL
;
962 smartlist_t
*matches
=NULL
;
963 const char *named_id
=NULL
;
965 if (!current_consensus
|| !nickname
)
968 /* Is this name really a hexadecimal identity digest? */
969 if (nickname
[0] == '$') {
970 if (base16_decode(digest
, DIGEST_LEN
, nickname
+1, strlen(nickname
+1))<0)
972 return networkstatus_vote_find_entry(current_consensus
, digest
);
973 } else if (strlen(nickname
) == HEX_DIGEST_LEN
&&
974 (base16_decode(digest
, DIGEST_LEN
, nickname
, strlen(nickname
))==0)) {
975 return networkstatus_vote_find_entry(current_consensus
, digest
);
978 /* Is there a server that is Named with this name? */
979 if (named_server_map
)
980 named_id
= strmap_get_lc(named_server_map
, nickname
);
982 return networkstatus_vote_find_entry(current_consensus
, named_id
);
984 /* Okay; is this name listed as Unnamed? */
985 if (unnamed_server_map
&&
986 strmap_get_lc(unnamed_server_map
, nickname
)) {
987 log_info(LD_GENERAL
, "The name %s is listed as Unnamed; it is not the "
988 "canonical name of any server we know.", escaped(nickname
));
992 /* This name is not canonical for any server; go through the list and
993 * see who it matches. */
994 /*XXXX This is inefficient; optimize it if it matters. */
995 matches
= smartlist_create();
996 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
997 routerstatus_t
*, lrs
,
999 if (!strcasecmp(lrs
->nickname
, nickname
)) {
1000 if (lrs
->is_named
) {
1001 tor_fragile_assert() /* This should never happen. */
1002 smartlist_free(matches
);
1005 if (lrs
->is_unnamed
) {
1006 tor_fragile_assert(); /* nor should this. */
1007 smartlist_clear(matches
);
1011 smartlist_add(matches
, lrs
);
1017 if (smartlist_len(matches
)>1 && warn_if_unnamed
) {
1019 SMARTLIST_FOREACH(matches
, routerstatus_t
*, lrs
,
1021 if (! lrs
->name_lookup_warned
) {
1022 lrs
->name_lookup_warned
=1;
1027 log_warn(LD_CONFIG
,"There are multiple matches for the nickname \"%s\","
1028 " but none is listed as named by the directory authorities. "
1029 "Choosing one arbitrarily.", nickname
);
1031 } else if (warn_if_unnamed
&& best
&& !best
->name_lookup_warned
) {
1032 char fp
[HEX_DIGEST_LEN
+1];
1033 base16_encode(fp
, sizeof(fp
),
1034 best
->identity_digest
, DIGEST_LEN
);
1036 "When looking up a status, you specified a server \"%s\" by name, "
1037 "but the directory authorities do not have any key registered for "
1038 "this nickname -- so it could be used by any server, "
1039 "not just the one you meant. "
1040 "To make sure you get the same server in the future, refer to "
1041 "it by key, as \"$%s\".", nickname
, fp
);
1042 best
->name_lookup_warned
= 1;
1044 smartlist_free(matches
);
1048 /** Return the identity digest that's mapped to officially by
1049 * <b>nickname</b>. */
1051 networkstatus_get_router_digest_by_nickname(const char *nickname
)
1053 if (!named_server_map
)
1055 return strmap_get_lc(named_server_map
, nickname
);
1058 /** Return true iff <b>nickname</b> is disallowed from being the nickname
1061 networkstatus_nickname_is_unnamed(const char *nickname
)
1063 if (!unnamed_server_map
)
1065 return strmap_get_lc(unnamed_server_map
, nickname
) != NULL
;
1068 /** How frequently do directory authorities re-download fresh networkstatus
1070 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
1072 /** How frequently do non-authority directory caches re-download fresh
1073 * networkstatus documents? */
1074 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
1076 /** We are a directory server, and so cache network_status documents.
1077 * Initiate downloads as needed to update them. For v2 authorities,
1078 * this means asking each trusted directory for its network-status.
1079 * For caches, this means asking a random v2 authority for all
1083 update_v2_networkstatus_cache_downloads(time_t now
)
1085 int authority
= authdir_mode_v2(get_options());
1087 authority
? AUTHORITY_NS_CACHE_INTERVAL
: NONAUTHORITY_NS_CACHE_INTERVAL
;
1088 const smartlist_t
*trusted_dir_servers
= router_get_trusted_dir_servers();
1090 if (last_networkstatus_download_attempted
+ interval
>= now
)
1093 last_networkstatus_download_attempted
= now
;
1096 /* An authority launches a separate connection for everybody. */
1097 SMARTLIST_FOREACH_BEGIN(trusted_dir_servers
, trusted_dir_server_t
*, ds
)
1099 char resource
[HEX_DIGEST_LEN
+6]; /* fp/hexdigit.z\0 */
1101 if (!(ds
->type
& V2_AUTHORITY
))
1103 if (router_digest_is_me(ds
->digest
))
1105 tor_addr_from_ipv4h(&addr
, ds
->addr
);
1106 /* Is this quite sensible with IPv6 or multiple addresses? */
1107 if (connection_get_by_type_addr_port_purpose(
1108 CONN_TYPE_DIR
, &addr
, ds
->dir_port
,
1109 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
)) {
1110 /* XXX the above dir_port won't be accurate if we're
1111 * doing a tunneled conn. In that case it should be or_port.
1112 * How to guess from here? Maybe make the function less general
1113 * and have it know that it's looking for dir conns. -RD */
1114 /* Only directory caches download v2 networkstatuses, and they
1115 * don't use tunneled connections. I think it's okay to ignore
1119 strlcpy(resource
, "fp/", sizeof(resource
));
1120 base16_encode(resource
+3, sizeof(resource
)-3, ds
->digest
, DIGEST_LEN
);
1121 strlcat(resource
, ".z", sizeof(resource
));
1122 directory_initiate_command_routerstatus(
1123 &ds
->fake_status
, DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
,
1124 ROUTER_PURPOSE_GENERAL
,
1125 0, /* Not private */
1127 NULL
, 0 /* No payload. */,
1130 SMARTLIST_FOREACH_END(ds
);
1132 /* A non-authority cache launches one connection to a random authority. */
1133 /* (Check whether we're currently fetching network-status objects.) */
1134 if (!connection_get_by_type_purpose(CONN_TYPE_DIR
,
1135 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
))
1136 directory_get_from_dirserver(DIR_PURPOSE_FETCH_V2_NETWORKSTATUS
,
1137 ROUTER_PURPOSE_GENERAL
, "all.z",
1138 PDS_RETRY_IF_NO_SERVERS
);
1142 /** How many times will we try to fetch a consensus before we give up? */
1143 #define CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES 8
1144 /** How long will we hang onto a possibly live consensus for which we're
1145 * fetching certs before we check whether there is a better one? */
1146 #define DELAY_WHILE_FETCHING_CERTS (20*60)
1148 /** If we want to download a fresh consensus, launch a new download as
1151 update_consensus_networkstatus_downloads(time_t now
)
1153 or_options_t
*options
= get_options();
1155 if (!networkstatus_get_live_consensus(now
))
1156 time_to_download_next_consensus
= now
; /* No live consensus? Get one now!*/
1157 if (time_to_download_next_consensus
> now
)
1158 return; /* Wait until the current consensus is older. */
1159 if (authdir_mode_v3(options
))
1160 return; /* Authorities never fetch a consensus */
1161 /* XXXXNM Microdescs: may need to download more types. */
1162 if (!download_status_is_ready(&consensus_dl_status
[FLAV_NS
], now
,
1163 CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES
))
1164 return; /* We failed downloading a consensus too recently. */
1165 if (connection_get_by_type_purpose(CONN_TYPE_DIR
,
1166 DIR_PURPOSE_FETCH_CONSENSUS
))
1167 return; /* There's an in-progress download.*/
1169 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1170 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
1171 if (waiting
->consensus
) {
1172 /* XXXX make sure this doesn't delay sane downloads. */
1173 if (waiting
->set_at
+ DELAY_WHILE_FETCHING_CERTS
> now
)
1174 return; /* We're still getting certs for this one. */
1176 if (!waiting
->dl_failed
) {
1177 download_status_failed(&consensus_dl_status
[FLAV_NS
], 0);
1178 waiting
->dl_failed
=1;
1184 log_info(LD_DIR
, "Launching networkstatus consensus download.");
1185 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
1186 ROUTER_PURPOSE_GENERAL
, NULL
,
1187 PDS_RETRY_IF_NO_SERVERS
);
1190 /** Called when an attempt to download a consensus fails: note that the
1191 * failure occurred, and possibly retry. */
1193 networkstatus_consensus_download_failed(int status_code
)
1195 /* XXXXNM Microdescs: may need to handle more types. */
1196 download_status_failed(&consensus_dl_status
[FLAV_NS
], status_code
);
1197 /* Retry immediately, if appropriate. */
1198 update_consensus_networkstatus_downloads(time(NULL
));
1201 /** How long do we (as a cache) wait after a consensus becomes non-fresh
1202 * before trying to fetch another? */
1203 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
1205 /** Update the time at which we'll consider replacing the current
1208 update_consensus_networkstatus_fetch_time(time_t now
)
1210 or_options_t
*options
= get_options();
1211 networkstatus_t
*c
= networkstatus_get_live_consensus(now
);
1214 long interval
= c
->fresh_until
- c
->valid_after
;
1216 if (directory_fetches_dir_info_early(options
)) {
1217 /* We want to cache the next one at some point after this one
1218 * is no longer fresh... */
1219 start
= c
->fresh_until
+ CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1220 /* Some clients may need the consensus sooner than others. */
1221 if (options
->FetchDirInfoExtraEarly
) {
1224 /* But only in the first half-interval after that. */
1225 dl_interval
= interval
/2;
1228 /* We're an ordinary client or a bridge. Give all the caches enough
1229 * time to download the consensus. */
1230 start
= c
->fresh_until
+ (interval
*3)/4;
1231 /* But download the next one well before this one is expired. */
1232 dl_interval
= ((c
->valid_until
- start
) * 7 )/ 8;
1234 /* If we're a bridge user, make use of the numbers we just computed
1235 * to choose the rest of the interval *after* them. */
1236 if (directory_fetches_dir_info_later(options
)) {
1237 /* Give all the *clients* enough time to download the consensus. */
1238 start
= start
+ dl_interval
+ CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1239 /* But try to get it before ours actually expires. */
1240 dl_interval
= (c
->valid_until
- start
) -
1241 CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1244 if (dl_interval
< 1)
1246 /* We must not try to replace c while it's still fresh: */
1247 tor_assert(c
->fresh_until
< start
);
1248 /* We must download the next one before c is invalid: */
1249 tor_assert(start
+dl_interval
< c
->valid_until
);
1250 time_to_download_next_consensus
= start
+crypto_rand_int((int)dl_interval
);
1252 char tbuf1
[ISO_TIME_LEN
+1];
1253 char tbuf2
[ISO_TIME_LEN
+1];
1254 char tbuf3
[ISO_TIME_LEN
+1];
1255 format_local_iso_time(tbuf1
, c
->fresh_until
);
1256 format_local_iso_time(tbuf2
, c
->valid_until
);
1257 format_local_iso_time(tbuf3
, time_to_download_next_consensus
);
1258 log_info(LD_DIR
, "Live consensus %s the most recent until %s and will "
1259 "expire at %s; fetching the next one at %s.",
1260 (c
->fresh_until
> now
) ? "will be" : "was",
1261 tbuf1
, tbuf2
, tbuf3
);
1264 time_to_download_next_consensus
= now
;
1265 log_info(LD_DIR
, "No live consensus; we should fetch one immediately.");
1269 /** Return 1 if there's a reason we shouldn't try any directory
1270 * fetches yet (e.g. we demand bridges and none are yet known).
1273 should_delay_dir_fetches(or_options_t
*options
)
1275 if (options
->UseBridges
&& !any_bridge_descriptors_known()) {
1276 log_info(LD_DIR
, "delaying dir fetches (no running bridges known)");
1282 /** Launch requests for networkstatus documents and authority certificates as
1285 update_networkstatus_downloads(time_t now
)
1287 or_options_t
*options
= get_options();
1288 if (should_delay_dir_fetches(options
))
1290 if (directory_fetches_dir_info_early(options
))
1291 update_v2_networkstatus_cache_downloads(now
);
1292 update_consensus_networkstatus_downloads(now
);
1293 update_certificate_downloads(now
);
1296 /** Launch requests as appropriate for missing directory authority
1299 update_certificate_downloads(time_t now
)
1302 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1303 if (consensus_waiting_for_certs
[i
].consensus
)
1304 authority_certs_fetch_missing(consensus_waiting_for_certs
[i
].consensus
,
1308 authority_certs_fetch_missing(current_consensus
, now
);
1311 /** Return 1 if we have a consensus but we don't have enough certificates
1312 * to start using it yet. */
1314 consensus_is_waiting_for_certs(void)
1316 return consensus_waiting_for_certs
[USABLE_CONSENSUS_FLAVOR
].consensus
1320 /** Return the network status with a given identity digest. */
1321 networkstatus_v2_t
*
1322 networkstatus_v2_get_by_digest(const char *digest
)
1324 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
1326 if (!memcmp(ns
->identity_digest
, digest
, DIGEST_LEN
))
1332 /** Return the most recent consensus that we have downloaded, or NULL if we
1333 * don't have one. */
1335 networkstatus_get_latest_consensus(void)
1337 return current_consensus
;
1340 /** Return the most recent consensus that we have downloaded, or NULL if it is
1341 * no longer live. */
1343 networkstatus_get_live_consensus(time_t now
)
1345 if (current_consensus
&&
1346 current_consensus
->valid_after
<= now
&&
1347 now
<= current_consensus
->valid_until
)
1348 return current_consensus
;
1353 /* XXXX remove this in favor of get_live_consensus. But actually,
1354 * leave something like it for bridge users, who need to not totally
1355 * lose if they spend a while fetching a new consensus. */
1356 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1359 networkstatus_get_reasonably_live_consensus(time_t now
)
1361 #define REASONABLY_LIVE_TIME (24*60*60)
1362 if (current_consensus
&&
1363 current_consensus
->valid_after
<= now
&&
1364 now
<= current_consensus
->valid_until
+REASONABLY_LIVE_TIME
)
1365 return current_consensus
;
1370 /** Given two router status entries for the same router identity, return 1 if
1371 * if the contents have changed between them. Otherwise, return 0. */
1373 routerstatus_has_changed(const routerstatus_t
*a
, const routerstatus_t
*b
)
1375 tor_assert(!memcmp(a
->identity_digest
, b
->identity_digest
, DIGEST_LEN
));
1377 return strcmp(a
->nickname
, b
->nickname
) ||
1378 memcmp(a
->descriptor_digest
, b
->descriptor_digest
, DIGEST_LEN
) ||
1379 a
->addr
!= b
->addr
||
1380 a
->or_port
!= b
->or_port
||
1381 a
->dir_port
!= b
->dir_port
||
1382 a
->is_authority
!= b
->is_authority
||
1383 a
->is_exit
!= b
->is_exit
||
1384 a
->is_stable
!= b
->is_stable
||
1385 a
->is_fast
!= b
->is_fast
||
1386 a
->is_running
!= b
->is_running
||
1387 a
->is_named
!= b
->is_named
||
1388 a
->is_unnamed
!= b
->is_unnamed
||
1389 a
->is_valid
!= b
->is_valid
||
1390 a
->is_v2_dir
!= b
->is_v2_dir
||
1391 a
->is_possible_guard
!= b
->is_possible_guard
||
1392 a
->is_bad_exit
!= b
->is_bad_exit
||
1393 a
->is_bad_directory
!= b
->is_bad_directory
||
1394 a
->is_hs_dir
!= b
->is_hs_dir
||
1395 a
->version_known
!= b
->version_known
||
1396 a
->version_supports_begindir
!= b
->version_supports_begindir
||
1397 a
->version_supports_extrainfo_upload
!=
1398 b
->version_supports_extrainfo_upload
||
1399 a
->version_supports_conditional_consensus
!=
1400 b
->version_supports_conditional_consensus
||
1401 a
->version_supports_v3_dir
!= b
->version_supports_v3_dir
;
1404 /** Notify controllers of any router status entries that changed between
1405 * <b>old_c</b> and <b>new_c</b>. */
1407 notify_control_networkstatus_changed(const networkstatus_t
*old_c
,
1408 const networkstatus_t
*new_c
)
1410 smartlist_t
*changed
;
1414 /* tell the controller exactly which relays are still listed, as well
1415 * as what they're listed as */
1416 control_event_newconsensus(new_c
);
1418 if (!control_event_is_interesting(EVENT_NS
))
1422 control_event_networkstatus_changed(new_c
->routerstatus_list
);
1425 changed
= smartlist_create();
1427 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1428 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1429 memcmp(rs_old
->identity_digest
,
1430 rs_new
->identity_digest
, DIGEST_LEN
),
1431 smartlist_add(changed
, rs_new
)) {
1432 if (routerstatus_has_changed(rs_old
, rs_new
))
1433 smartlist_add(changed
, rs_new
);
1434 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1436 control_event_networkstatus_changed(changed
);
1437 smartlist_free(changed
);
1440 /** Copy all the ancillary information (like router download status and so on)
1441 * from <b>old_c</b> to <b>new_c</b>. */
1443 networkstatus_copy_old_consensus_info(networkstatus_t
*new_c
,
1444 const networkstatus_t
*old_c
)
1448 if (!old_c
|| !smartlist_len(old_c
->routerstatus_list
))
1451 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1452 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1453 memcmp(rs_old
->identity_digest
,
1454 rs_new
->identity_digest
, DIGEST_LEN
),
1456 /* Okay, so we're looking at the same identity. */
1457 rs_new
->name_lookup_warned
= rs_old
->name_lookup_warned
;
1458 rs_new
->last_dir_503_at
= rs_old
->last_dir_503_at
;
1460 if (!memcmp(rs_old
->descriptor_digest
, rs_new
->descriptor_digest
,
1462 /* And the same descriptor too! */
1463 memcpy(&rs_new
->dl_status
, &rs_old
->dl_status
,sizeof(download_status_t
));
1465 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1468 /** Try to replace the current cached v3 networkstatus with the one in
1469 * <b>consensus</b>. If we don't have enough certificates to validate it,
1470 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1472 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1473 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1474 * already received, but we were waiting for certificates on it. If flags &
1475 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1476 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1477 * consensus, even if it comes from many days in the past.
1479 * Return 0 on success, <0 on failure. On failure, caller should increment
1480 * the failure count as appropriate.
1482 * We return -1 for mild failures that don't need to be reported to the
1483 * user, and -2 for more serious problems.
1486 networkstatus_set_current_consensus(const char *consensus
,
1490 networkstatus_t
*c
=NULL
;
1492 time_t now
= time(NULL
);
1493 char *unverified_fname
= NULL
, *consensus_fname
= NULL
;
1494 int flav
= networkstatus_parse_flavor_name(flavor
);
1495 const unsigned from_cache
= flags
& NSSET_FROM_CACHE
;
1496 const unsigned was_waiting_for_certs
= flags
& NSSET_WAS_WAITING_FOR_CERTS
;
1497 const unsigned dl_certs
= !(flags
& NSSET_DONT_DOWNLOAD_CERTS
);
1498 const unsigned accept_obsolete
= flags
& NSSET_ACCEPT_OBSOLETE
;
1499 const unsigned require_flavor
= flags
& NSSET_REQUIRE_FLAVOR
;
1500 const digests_t
*current_digests
= NULL
;
1501 consensus_waiting_for_certs_t
*waiting
= NULL
;
1502 time_t current_valid_after
= 0;
1505 /* XXXX we don't handle unrecognized flavors yet. */
1506 log_warn(LD_BUG
, "Unrecognized consensus flavor %s", flavor
);
1510 /* Make sure it's parseable. */
1511 c
= networkstatus_parse_vote_from_string(consensus
, NULL
, NS_TYPE_CONSENSUS
);
1513 log_warn(LD_DIR
, "Unable to parse networkstatus consensus");
1518 if ((int)c
->flavor
!= flav
) {
1519 /* This wasn't the flavor we thought we were getting. */
1520 if (require_flavor
) {
1521 log_warn(LD_DIR
, "Got consensus with unexpected flavor %s (wanted %s)",
1522 networkstatus_get_flavor_name(c
->flavor
), flavor
);
1526 flavor
= networkstatus_get_flavor_name(flav
);
1529 if (flav
!= USABLE_CONSENSUS_FLAVOR
&&
1530 !directory_caches_dir_info(get_options())) {
1531 /* This consensus is totally boring to us: we won't use it, and we won't
1532 * serve it. Drop it. */
1536 if (from_cache
&& !accept_obsolete
&&
1537 c
->valid_until
< now
-OLD_ROUTER_DESC_MAX_AGE
) {
1538 /* XXX022 when we try to make fallbackconsensus work again, we should
1539 * consider taking this out. Until then, believing obsolete consensuses
1540 * is causing more harm than good. See also bug 887. */
1541 log_info(LD_DIR
, "Loaded an expired consensus. Discarding.");
1545 if (!strcmp(flavor
, "ns")) {
1546 consensus_fname
= get_datadir_fname("cached-consensus");
1547 unverified_fname
= get_datadir_fname("unverified-consensus");
1548 if (current_consensus
) {
1549 current_digests
= ¤t_consensus
->digests
;
1550 current_valid_after
= current_consensus
->valid_after
;
1555 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
1556 consensus_fname
= get_datadir_fname(buf
);
1557 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
1558 unverified_fname
= get_datadir_fname(buf
);
1559 cur
= dirserv_get_consensus(flavor
);
1561 current_digests
= &cur
->digests
;
1562 current_valid_after
= cur
->published
;
1566 if (current_digests
&&
1567 !memcmp(&c
->digests
, current_digests
, sizeof(c
->digests
))) {
1568 /* We already have this one. That's a failure. */
1569 log_info(LD_DIR
, "Got a %s consensus we already have", flavor
);
1573 if (current_valid_after
&& c
->valid_after
<= current_valid_after
) {
1574 /* We have a newer one. There's no point in accepting this one,
1575 * even if it's great. */
1576 log_info(LD_DIR
, "Got a %s consensus at least as old as the one we have",
1581 /* Make sure it's signed enough. */
1582 if ((r
=networkstatus_check_consensus_signature(c
, 1))<0) {
1584 /* Okay, so it _might_ be signed enough if we get more certificates. */
1585 if (!was_waiting_for_certs
) {
1587 "Not enough certificates to check networkstatus consensus");
1589 if (!current_valid_after
||
1590 c
->valid_after
> current_valid_after
) {
1591 waiting
= &consensus_waiting_for_certs
[flav
];
1592 networkstatus_vote_free(waiting
->consensus
);
1593 tor_free(waiting
->body
);
1594 waiting
->consensus
= c
;
1595 c
= NULL
; /* Prevent free. */
1596 waiting
->body
= tor_strdup(consensus
);
1597 waiting
->set_at
= now
;
1598 waiting
->dl_failed
= 0;
1600 write_str_to_file(unverified_fname
, consensus
, 0);
1603 authority_certs_fetch_missing(c
, now
);
1604 /* This case is not a success or a failure until we get the certs
1605 * or fail to get the certs. */
1608 /* Even if we had enough signatures, we'd never use this as the
1609 * latest consensus. */
1610 if (was_waiting_for_certs
&& from_cache
)
1611 unlink(unverified_fname
);
1615 /* This can never be signed enough: Kill it. */
1616 if (!was_waiting_for_certs
) {
1617 log_warn(LD_DIR
, "Not enough good signatures on networkstatus "
1621 if (was_waiting_for_certs
&& (r
< -1) && from_cache
)
1622 unlink(unverified_fname
);
1627 if (!from_cache
&& flav
== USABLE_CONSENSUS_FLAVOR
)
1628 control_event_client_status(LOG_NOTICE
, "CONSENSUS_ARRIVED");
1630 /* Are we missing any certificates at all? */
1631 if (r
!= 1 && dl_certs
)
1632 authority_certs_fetch_missing(c
, now
);
1634 if (flav
== USABLE_CONSENSUS_FLAVOR
) {
1635 notify_control_networkstatus_changed(current_consensus
, c
);
1637 if (current_consensus
) {
1638 networkstatus_copy_old_consensus_info(c
, current_consensus
);
1639 networkstatus_vote_free(current_consensus
);
1643 waiting
= &consensus_waiting_for_certs
[flav
];
1644 if (waiting
->consensus
&&
1645 waiting
->consensus
->valid_after
<= c
->valid_after
) {
1646 networkstatus_vote_free(waiting
->consensus
);
1647 waiting
->consensus
= NULL
;
1648 if (consensus
!= waiting
->body
)
1649 tor_free(waiting
->body
);
1651 waiting
->body
= NULL
;
1652 waiting
->set_at
= 0;
1653 waiting
->dl_failed
= 0;
1654 unlink(unverified_fname
);
1657 /* Reset the failure count only if this consensus is actually valid. */
1658 if (c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1659 download_status_reset(&consensus_dl_status
[flav
]);
1662 download_status_failed(&consensus_dl_status
[flav
], 0);
1665 if (directory_caches_dir_info(get_options())) {
1666 dirserv_set_cached_consensus_networkstatus(consensus
,
1672 if (flav
== USABLE_CONSENSUS_FLAVOR
) {
1673 current_consensus
= c
;
1674 c
= NULL
; /* Prevent free. */
1676 /* XXXXNM Microdescs: needs a non-ns variant. */
1677 update_consensus_networkstatus_fetch_time(now
);
1678 dirvote_recalculate_timing(get_options(), now
);
1679 routerstatus_list_update_named_server_map();
1680 cell_ewma_set_scale_factor(get_options(), current_consensus
);
1681 circuit_build_times_new_consensus_params(&circ_times
, current_consensus
);
1685 write_str_to_file(consensus_fname
, consensus
, 0);
1688 if (ftime_definitely_before(now
, current_consensus
->valid_after
)) {
1689 char tbuf
[ISO_TIME_LEN
+1];
1691 long delta
= now
- current_consensus
->valid_after
;
1692 format_iso_time(tbuf
, current_consensus
->valid_after
);
1693 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
1694 log_warn(LD_GENERAL
, "Our clock is %s behind the time published in the "
1695 "consensus network status document (%s GMT). Tor needs an "
1696 "accurate clock to work correctly. Please check your time and "
1697 "date settings!", dbuf
, tbuf
);
1698 control_event_general_status(LOG_WARN
,
1699 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta
);
1702 router_dir_info_changed();
1706 networkstatus_vote_free(c
);
1707 tor_free(consensus_fname
);
1708 tor_free(unverified_fname
);
1712 /** Called when we have gotten more certificates: see whether we can
1713 * now verify a pending consensus. */
1715 networkstatus_note_certs_arrived(void)
1718 for (i
=0; i
<N_CONSENSUS_FLAVORS
; ++i
) {
1719 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
1720 if (!waiting
->consensus
)
1722 if (networkstatus_check_consensus_signature(waiting
->consensus
, 0)>=0) {
1723 if (!networkstatus_set_current_consensus(
1725 networkstatus_get_flavor_name(i
),
1726 NSSET_WAS_WAITING_FOR_CERTS
)) {
1727 tor_free(waiting
->body
);
1733 /** If the network-status list has changed since the last time we called this
1734 * function, update the status of every routerinfo from the network-status
1735 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
1736 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
1739 routers_update_all_from_networkstatus(time_t now
, int dir_version
)
1741 routerlist_t
*rl
= router_get_routerlist();
1742 networkstatus_t
*consensus
= networkstatus_get_live_consensus(now
);
1744 if (networkstatus_v2_list_has_changed
)
1745 download_status_map_update_from_v2_networkstatus();
1747 if (!consensus
|| dir_version
< 3) /* nothing more we should do */
1750 /* calls router_dir_info_changed() when it's done -- more routers
1751 * might be up or down now, which might affect whether there's enough
1752 * directory info. */
1753 routers_update_status_from_consensus_networkstatus(rl
->routers
, 0);
1755 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
,
1756 ri
->cache_info
.routerlist_index
= ri_sl_idx
);
1757 if (rl
->old_routers
)
1758 signed_descs_update_status_from_consensus_networkstatus(rl
->old_routers
);
1760 if (!have_warned_about_old_version
) {
1761 int is_server
= server_mode(get_options());
1762 version_status_t status
;
1763 const char *recommended
= is_server
?
1764 consensus
->server_versions
: consensus
->client_versions
;
1765 status
= tor_version_is_obsolete(VERSION
, recommended
);
1767 if (status
== VS_RECOMMENDED
) {
1768 log_info(LD_GENERAL
, "The directory authorities say my version is ok.");
1769 } else if (status
== VS_EMPTY
) {
1770 log_info(LD_GENERAL
,
1771 "The directory authorities don't recommend any versions.");
1772 } else if (status
== VS_NEW
|| status
== VS_NEW_IN_SERIES
) {
1773 if (!have_warned_about_new_version
) {
1774 log_notice(LD_GENERAL
, "This version of Tor (%s) is newer than any "
1775 "recommended version%s, according to the directory "
1776 "authorities. Recommended versions are: %s",
1778 status
== VS_NEW_IN_SERIES
? " in its series" : "",
1780 have_warned_about_new_version
= 1;
1781 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1782 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1783 VERSION
, "NEW", recommended
);
1786 log_warn(LD_GENERAL
, "Please upgrade! "
1787 "This version of Tor (%s) is %s, according to the directory "
1788 "authorities. Recommended versions are: %s",
1790 status
== VS_OLD
? "obsolete" : "not recommended",
1792 have_warned_about_old_version
= 1;
1793 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
1794 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1795 VERSION
, status
== VS_OLD
? "OBSOLETE" : "UNRECOMMENDED",
1801 /** Update v2_download_status_map to contain an entry for every router
1802 * descriptor listed in the v2 networkstatuses. */
1804 download_status_map_update_from_v2_networkstatus(void)
1806 digestmap_t
*dl_status
;
1807 if (!networkstatus_v2_list
)
1809 if (!v2_download_status_map
)
1810 v2_download_status_map
= digestmap_new();
1812 dl_status
= digestmap_new();
1813 SMARTLIST_FOREACH_BEGIN(networkstatus_v2_list
, networkstatus_v2_t
*, ns
) {
1814 SMARTLIST_FOREACH_BEGIN(ns
->entries
, routerstatus_t
*, rs
) {
1815 const char *d
= rs
->descriptor_digest
;
1816 download_status_t
*s
;
1817 if (digestmap_get(dl_status
, d
))
1819 if (!(s
= digestmap_remove(v2_download_status_map
, d
))) {
1820 s
= tor_malloc_zero(sizeof(download_status_t
));
1822 digestmap_set(dl_status
, d
, s
);
1823 } SMARTLIST_FOREACH_END(rs
);
1824 } SMARTLIST_FOREACH_END(ns
);
1825 digestmap_free(v2_download_status_map
, _tor_free
);
1826 v2_download_status_map
= dl_status
;
1827 networkstatus_v2_list_has_changed
= 0;
1830 /** Update our view of the list of named servers from the most recently
1831 * retrieved networkstatus consensus. */
1833 routerstatus_list_update_named_server_map(void)
1835 if (!current_consensus
)
1838 strmap_free(named_server_map
, _tor_free
);
1839 named_server_map
= strmap_new();
1840 strmap_free(unnamed_server_map
, NULL
);
1841 unnamed_server_map
= strmap_new();
1842 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
, routerstatus_t
*, rs
,
1845 strmap_set_lc(named_server_map
, rs
->nickname
,
1846 tor_memdup(rs
->identity_digest
, DIGEST_LEN
));
1848 if (rs
->is_unnamed
) {
1849 strmap_set_lc(unnamed_server_map
, rs
->nickname
, (void*)1);
1854 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
1855 * according to our current consensus networkstatus. May re-order
1856 * <b>routers</b>. */
1858 routers_update_status_from_consensus_networkstatus(smartlist_t
*routers
,
1861 trusted_dir_server_t
*ds
;
1862 or_options_t
*options
= get_options();
1863 int authdir
= authdir_mode_v2(options
) || authdir_mode_v3(options
);
1864 int namingdir
= authdir
&& options
->NamingAuthoritativeDir
;
1865 networkstatus_t
*ns
= current_consensus
;
1866 if (!ns
|| !smartlist_len(ns
->routerstatus_list
))
1868 if (!networkstatus_v2_list
)
1869 networkstatus_v2_list
= smartlist_create();
1871 routers_sort_by_identity(routers
);
1873 SMARTLIST_FOREACH_JOIN(ns
->routerstatus_list
, routerstatus_t
*, rs
,
1874 routers
, routerinfo_t
*, router
,
1875 memcmp(rs
->identity_digest
,
1876 router
->cache_info
.identity_digest
, DIGEST_LEN
),
1878 /* We have no routerstatus for this router. Clear flags and skip it. */
1880 router
->is_named
= 0;
1882 if (router
->purpose
== ROUTER_PURPOSE_GENERAL
)
1883 router_clear_status_flags(router
);
1886 /* We have a routerstatus for this router. */
1887 const char *digest
= router
->cache_info
.identity_digest
;
1889 ds
= router_get_trusteddirserver_by_digest(digest
);
1892 if (rs
->is_named
&& !strcasecmp(router
->nickname
, rs
->nickname
))
1893 router
->is_named
= 1;
1895 router
->is_named
= 0;
1897 /* Is it the same descriptor, or only the same identity? */
1898 if (!memcmp(router
->cache_info
.signed_descriptor_digest
,
1899 rs
->descriptor_digest
, DIGEST_LEN
)) {
1900 if (ns
->valid_until
> router
->cache_info
.last_listed_as_valid_until
)
1901 router
->cache_info
.last_listed_as_valid_until
= ns
->valid_until
;
1905 /* If we're not an authdir, believe others. */
1906 router
->is_valid
= rs
->is_valid
;
1907 router
->is_running
= rs
->is_running
;
1908 router
->is_fast
= rs
->is_fast
;
1909 router
->is_stable
= rs
->is_stable
;
1910 router
->is_possible_guard
= rs
->is_possible_guard
;
1911 router
->is_exit
= rs
->is_exit
;
1912 router
->is_bad_directory
= rs
->is_bad_directory
;
1913 router
->is_bad_exit
= rs
->is_bad_exit
;
1914 router
->is_hs_dir
= rs
->is_hs_dir
;
1916 if (router
->is_running
&& ds
) {
1917 download_status_reset(&ds
->v2_ns_dl_status
);
1919 if (reset_failures
) {
1920 download_status_reset(&rs
->dl_status
);
1922 } SMARTLIST_FOREACH_JOIN_END(rs
, router
);
1924 /* Now update last_listed_as_valid_until from v2 networkstatuses. */
1925 /* XXXX If this is slow, we need to rethink the code. */
1926 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
, {
1927 time_t live_until
= ns
->published_on
+ V2_NETWORKSTATUS_ROUTER_LIFETIME
;
1928 SMARTLIST_FOREACH_JOIN(ns
->entries
, routerstatus_t
*, rs
,
1929 routers
, routerinfo_t
*, ri
,
1930 memcmp(rs
->identity_digest
,
1931 ri
->cache_info
.identity_digest
, DIGEST_LEN
),
1933 if (!memcmp(ri
->cache_info
.signed_descriptor_digest
,
1934 rs
->descriptor_digest
, DIGEST_LEN
)) {
1935 if (live_until
> ri
->cache_info
.last_listed_as_valid_until
)
1936 ri
->cache_info
.last_listed_as_valid_until
= live_until
;
1938 } SMARTLIST_FOREACH_JOIN_END(rs
, ri
);
1941 router_dir_info_changed();
1944 /** Given a list of signed_descriptor_t, update their fields (mainly, when
1945 * they were last listed) from the most recent consensus. */
1947 signed_descs_update_status_from_consensus_networkstatus(smartlist_t
*descs
)
1949 networkstatus_t
*ns
= current_consensus
;
1953 if (!ns
->desc_digest_map
) {
1954 char dummy
[DIGEST_LEN
];
1955 /* instantiates the digest map. */
1956 memset(dummy
, 0, sizeof(dummy
));
1957 router_get_consensus_status_by_descriptor_digest(dummy
);
1959 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, d
,
1961 routerstatus_t
*rs
= digestmap_get(ns
->desc_digest_map
,
1962 d
->signed_descriptor_digest
);
1964 if (ns
->valid_until
> d
->last_listed_as_valid_until
)
1965 d
->last_listed_as_valid_until
= ns
->valid_until
;
1970 /** Generate networkstatus lines for a single routerstatus_t object, and
1971 * return the result in a newly allocated string. Used only by controller
1972 * interface (for now.) */
1974 networkstatus_getinfo_helper_single(routerstatus_t
*rs
)
1976 char buf
[RS_ENTRY_LEN
+1];
1977 routerstatus_format_entry(buf
, sizeof(buf
), rs
, NULL
, NS_CONTROL_PORT
);
1978 return tor_strdup(buf
);
1981 /** Alloc and return a string describing routerstatuses for the most
1982 * recent info of each router we know about that is of purpose
1983 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
1985 * Right now this function is oriented toward listing bridges (you
1986 * shouldn't use this for general-purpose routers, since those
1987 * should be listed from the consensus, not from the routers list). */
1989 networkstatus_getinfo_by_purpose(const char *purpose_string
, time_t now
)
1991 time_t cutoff
= now
- ROUTER_MAX_AGE_TO_PUBLISH
;
1993 routerlist_t
*rl
= router_get_routerlist();
1994 smartlist_t
*statuses
;
1995 uint8_t purpose
= router_purpose_from_string(purpose_string
);
1997 int bridge_auth
= authdir_mode_bridge(get_options());
1999 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
2000 log_info(LD_DIR
, "Unrecognized purpose '%s' when listing router statuses.",
2005 statuses
= smartlist_create();
2006 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
, {
2007 if (ri
->cache_info
.published_on
< cutoff
)
2009 if (ri
->purpose
!= purpose
)
2011 if (bridge_auth
&& ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
2012 dirserv_set_router_is_running(ri
, now
);
2013 /* then generate and write out status lines for each of them */
2014 set_routerstatus_from_routerinfo(&rs
, ri
, now
, 0, 0, 0, 0);
2015 smartlist_add(statuses
, networkstatus_getinfo_helper_single(&rs
));
2018 answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2019 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2020 smartlist_free(statuses
);
2024 /** Write out router status entries for all our bridge descriptors. */
2026 networkstatus_dump_bridge_status_to_file(time_t now
)
2028 char *status
= networkstatus_getinfo_by_purpose("bridge", now
);
2029 or_options_t
*options
= get_options();
2030 size_t len
= strlen(options
->DataDirectory
) + 32;
2031 char *fname
= tor_malloc(len
);
2032 tor_snprintf(fname
, len
, "%s"PATH_SEPARATOR
"networkstatus-bridges",
2033 options
->DataDirectory
);
2034 write_str_to_file(fname
,status
,0);
2040 get_net_param_from_list(smartlist_t
*net_params
, const char *param_name
,
2043 size_t name_len
= strlen(param_name
);
2045 SMARTLIST_FOREACH_BEGIN(net_params
, const char *, p
) {
2046 if (!strcmpstart(p
, param_name
) && p
[name_len
] == '=') {
2048 long v
= tor_parse_long(p
+name_len
+1, 10, INT32_MIN
,
2049 INT32_MAX
, &ok
, NULL
);
2053 } SMARTLIST_FOREACH_END(p
);
2058 /** Return the value of a integer parameter from the networkstatus <b>ns</b>
2059 * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
2060 * latest consensus ourselves. Return <b>default_val</b> if no latest
2061 * consensus, or if it has no parameter called <b>param_name</b>. */
2063 networkstatus_get_param(networkstatus_t
*ns
, const char *param_name
,
2064 int32_t default_val
)
2066 if (!ns
) /* if they pass in null, go find it ourselves */
2067 ns
= networkstatus_get_latest_consensus();
2069 if (!ns
|| !ns
->net_params
)
2072 return get_net_param_from_list(ns
->net_params
, param_name
, default_val
);
2075 /** Return the value of a integer bw weight parameter from the networkstatus
2076 * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
2077 * loading the latest consensus ourselves. Return <b>default_val</b> if no
2078 * latest consensus, or if it has no parameter called <b>param_name</b>. */
2080 networkstatus_get_bw_weight(networkstatus_t
*ns
, const char *weight_name
,
2081 int32_t default_val
)
2083 if (!ns
) /* if they pass in null, go find it ourselves */
2084 ns
= networkstatus_get_latest_consensus();
2086 if (!ns
|| !ns
->weight_params
)
2089 return get_net_param_from_list(ns
->weight_params
, weight_name
, default_val
);
2092 /** Return the name of the consensus flavor <b>flav</b> as used to identify
2093 * the flavor in directory documents. */
2095 networkstatus_get_flavor_name(consensus_flavor_t flav
)
2100 case FLAV_MICRODESC
:
2103 tor_fragile_assert();
2108 /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
2109 * or -1 if the flavor is not recognized. */
2111 networkstatus_parse_flavor_name(const char *flavname
)
2113 if (!strcmp(flavname
, "ns"))
2115 else if (!strcmp(flavname
, "microdesc"))
2116 return FLAV_MICRODESC
;
2121 /** If <b>question</b> is a string beginning with "ns/" in a format the
2122 * control interface expects for a GETINFO question, set *<b>answer</b> to a
2123 * newly-allocated string containing networkstatus lines for the appropriate
2124 * ORs. Return 0 on success, -1 on unrecognized question format. */
2126 getinfo_helper_networkstatus(control_connection_t
*conn
,
2127 const char *question
, char **answer
)
2129 routerstatus_t
*status
;
2132 if (!current_consensus
) {
2133 *answer
= tor_strdup("");
2137 if (!strcmp(question
, "ns/all")) {
2138 smartlist_t
*statuses
= smartlist_create();
2139 SMARTLIST_FOREACH(current_consensus
->routerstatus_list
,
2140 routerstatus_t
*, rs
,
2142 smartlist_add(statuses
, networkstatus_getinfo_helper_single(rs
));
2144 *answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2145 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2146 smartlist_free(statuses
);
2148 } else if (!strcmpstart(question
, "ns/id/")) {
2151 if (base16_decode(d
, DIGEST_LEN
, question
+6, strlen(question
+6)))
2153 status
= router_get_consensus_status_by_id(d
);
2154 } else if (!strcmpstart(question
, "ns/name/")) {
2155 status
= router_get_consensus_status_by_nickname(question
+8, 0);
2156 } else if (!strcmpstart(question
, "ns/purpose/")) {
2157 *answer
= networkstatus_getinfo_by_purpose(question
+11, time(NULL
));
2158 return *answer
? 0 : -1;
2164 *answer
= networkstatus_getinfo_helper_single(status
);
2168 /** Free all storage held locally in this module. */
2170 networkstatus_free_all(void)
2173 if (networkstatus_v2_list
) {
2174 SMARTLIST_FOREACH(networkstatus_v2_list
, networkstatus_v2_t
*, ns
,
2175 networkstatus_v2_free(ns
));
2176 smartlist_free(networkstatus_v2_list
);
2177 networkstatus_v2_list
= NULL
;
2180 digestmap_free(v2_download_status_map
, _tor_free
);
2181 v2_download_status_map
= NULL
;
2182 networkstatus_vote_free(current_consensus
);
2183 current_consensus
= NULL
;
2185 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2186 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2187 if (waiting
->consensus
) {
2188 networkstatus_vote_free(waiting
->consensus
);
2189 waiting
->consensus
= NULL
;
2191 tor_free(waiting
->body
);
2194 strmap_free(named_server_map
, _tor_free
);
2195 strmap_free(unnamed_server_map
, NULL
);