Revert "Backport fix for bug 997."
[tor/rransom.git] / src / or / networkstatus.c
blob573197a53ff0adce552a2128916b45bb7f6b6d42
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-2009, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file networkstatus.c
9 * \brief Functions and structures for handling network status documents as a
10 * client or cache.
13 #include "or.h"
15 /* For tracking v2 networkstatus documents. Only caches do this now. */
17 /** Map from descriptor digest of routers listed in the v2 networkstatus
18 * documents to download_status_t* */
19 static digestmap_t *v2_download_status_map = NULL;
20 /** Global list of all of the current v2 network_status documents that we know
21 * about. This list is kept sorted by published_on. */
22 static smartlist_t *networkstatus_v2_list = NULL;
23 /** True iff any member of networkstatus_v2_list has changed since the last
24 * time we called download_status_map_update_from_v2_networkstatus() */
25 static int networkstatus_v2_list_has_changed = 0;
27 /** Map from lowercase nickname to identity digest of named server, if any. */
28 static strmap_t *named_server_map = NULL;
29 /** Map from lowercase nickname to (void*)1 for all names that are listed
30 * as unnamed for some server in the consensus. */
31 static strmap_t *unnamed_server_map = NULL;
33 /** Most recently received and validated v3 consensus network status. */
34 static networkstatus_t *current_consensus = NULL;
36 /** A v3 consensus networkstatus that we've received, but which we don't
37 * have enough certificates to be happy about. */
38 static networkstatus_t *consensus_waiting_for_certs = NULL;
39 /** The encoded version of consensus_waiting_for_certs. */
40 static char *consensus_waiting_for_certs_body = NULL;
41 /** When did we set the current value of consensus_waiting_for_certs? If this
42 * is too recent, we shouldn't try to fetch a new consensus for a little while,
43 * to give ourselves time to get certificates for this one. */
44 static time_t consensus_waiting_for_certs_set_at = 0;
45 /** Set to 1 if we've been holding on to consensus_waiting_for_certs so long
46 * that we should treat it as maybe being bad. */
47 static int consensus_waiting_for_certs_dl_failed = 0;
49 /** The last time we tried to download a networkstatus, or 0 for "never". We
50 * use this to rate-limit download attempts for directory caches (including
51 * mirrors). Clients don't use this now. */
52 static time_t last_networkstatus_download_attempted = 0;
54 /** A time before which we shouldn't try to replace the current consensus:
55 * this will be at some point after the next consensus becomes valid, but
56 * before the current consensus becomes invalid. */
57 static time_t time_to_download_next_consensus = 0;
58 /** Download status for the current consensus networkstatus. */
59 static download_status_t consensus_dl_status = { 0, 0, DL_SCHED_CONSENSUS };
61 /** True iff we have logged a warning about this OR's version being older than
62 * listed by the authorities. */
63 static int have_warned_about_old_version = 0;
64 /** True iff we have logged a warning about this OR's version being newer than
65 * listed by the authorities. */
66 static int have_warned_about_new_version = 0;
68 static void download_status_map_update_from_v2_networkstatus(void);
69 static void routerstatus_list_update_named_server_map(void);
71 /** Forget that we've warned about anything networkstatus-related, so we will
72 * give fresh warnings if the same behavior happens again. */
73 void
74 networkstatus_reset_warnings(void)
76 if (current_consensus) {
77 SMARTLIST_FOREACH(current_consensus->routerstatus_list,
78 routerstatus_t *, rs,
79 rs->name_lookup_warned = 0);
82 have_warned_about_old_version = 0;
83 have_warned_about_new_version = 0;
86 /** Reset the descriptor download failure count on all networkstatus docs, so
87 * that we can retry any long-failed documents immediately.
89 void
90 networkstatus_reset_download_failures(void)
92 const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
93 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
94 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
96 if (!router_get_by_descriptor_digest(rs->descriptor_digest))
97 rs->need_to_mirror = 1;
98 }));;
100 download_status_reset(&consensus_dl_status);
101 if (v2_download_status_map) {
102 digestmap_iter_t *iter;
103 digestmap_t *map = v2_download_status_map;
104 const char *key;
105 void *val;
106 download_status_t *dls;
107 for (iter = digestmap_iter_init(map); !digestmap_iter_done(iter);
108 iter = digestmap_iter_next(map, iter) ) {
109 digestmap_iter_get(iter, &key, &val);
110 dls = val;
111 download_status_reset(dls);
116 /** Repopulate our list of network_status_t objects from the list cached on
117 * disk. Return 0 on success, -1 on failure. */
119 router_reload_v2_networkstatus(void)
121 smartlist_t *entries;
122 struct stat st;
123 char *s;
124 char *filename = get_datadir_fname("cached-status");
125 int maybe_delete = !directory_caches_v2_dir_info(get_options());
126 time_t now = time(NULL);
127 if (!networkstatus_v2_list)
128 networkstatus_v2_list = smartlist_create();
130 entries = tor_listdir(filename);
131 if (!entries) { /* dir doesn't exist */
132 tor_free(filename);
133 return 0;
134 } else if (!smartlist_len(entries) && maybe_delete) {
135 rmdir(filename);
136 tor_free(filename);
137 smartlist_free(entries);
138 return 0;
140 tor_free(filename);
141 SMARTLIST_FOREACH(entries, const char *, fn, {
142 char buf[DIGEST_LEN];
143 if (maybe_delete) {
144 filename = get_datadir_fname2("cached-status", fn);
145 remove_file_if_very_old(filename, now);
146 tor_free(filename);
147 continue;
149 if (strlen(fn) != HEX_DIGEST_LEN ||
150 base16_decode(buf, sizeof(buf), fn, strlen(fn))) {
151 log_info(LD_DIR,
152 "Skipping cached-status file with unexpected name \"%s\"",fn);
153 continue;
155 filename = get_datadir_fname2("cached-status", fn);
156 s = read_file_to_str(filename, 0, &st);
157 if (s) {
158 if (router_set_networkstatus_v2(s, st.st_mtime, NS_FROM_CACHE,
159 NULL)<0) {
160 log_warn(LD_FS, "Couldn't load networkstatus from \"%s\"",filename);
162 tor_free(s);
164 tor_free(filename);
166 SMARTLIST_FOREACH(entries, char *, fn, tor_free(fn));
167 smartlist_free(entries);
168 networkstatus_v2_list_clean(time(NULL));
169 routers_update_all_from_networkstatus(time(NULL), 2);
170 return 0;
173 /** Read the cached v3 consensus networkstatus from the disk. */
175 router_reload_consensus_networkstatus(void)
177 char *filename;
178 char *s;
179 struct stat st;
180 or_options_t *options = get_options();
181 const unsigned int flags = NSSET_FROM_CACHE | NSSET_DONT_DOWNLOAD_CERTS;
183 /* FFFF Suppress warnings if cached consensus is bad? */
185 filename = get_datadir_fname("cached-consensus");
186 s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
187 if (s) {
188 if (networkstatus_set_current_consensus(s, flags) < -1) {
189 log_warn(LD_FS, "Couldn't load consensus networkstatus from \"%s\"",
190 filename);
192 tor_free(s);
194 tor_free(filename);
196 filename = get_datadir_fname("unverified-consensus");
197 s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
198 if (s) {
199 if (networkstatus_set_current_consensus(s,
200 flags|NSSET_WAS_WAITING_FOR_CERTS)) {
201 log_info(LD_FS, "Couldn't load consensus networkstatus from \"%s\"",
202 filename);
204 tor_free(s);
206 tor_free(filename);
208 if (!current_consensus ||
209 (stat(options->FallbackNetworkstatusFile, &st)==0 &&
210 st.st_mtime > current_consensus->valid_after)) {
211 s = read_file_to_str(options->FallbackNetworkstatusFile,
212 RFTS_IGNORE_MISSING, NULL);
213 if (s) {
214 if (networkstatus_set_current_consensus(s,
215 flags|NSSET_ACCEPT_OBSOLETE)) {
216 log_info(LD_FS, "Couldn't load consensus networkstatus from \"%s\"",
217 options->FallbackNetworkstatusFile);
218 } else {
219 log_notice(LD_FS,
220 "Loaded fallback consensus networkstatus from \"%s\"",
221 options->FallbackNetworkstatusFile);
223 tor_free(s);
227 if (!current_consensus) {
228 if (!named_server_map)
229 named_server_map = strmap_new();
230 if (!unnamed_server_map)
231 unnamed_server_map = strmap_new();
234 update_certificate_downloads(time(NULL));
236 routers_update_all_from_networkstatus(time(NULL), 3);
238 return 0;
241 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
242 static void
243 vote_routerstatus_free(vote_routerstatus_t *rs)
245 tor_free(rs->version);
246 tor_free(rs->status.exitsummary);
247 tor_free(rs);
250 /** Free all storage held by the routerstatus object <b>rs</b>. */
251 void
252 routerstatus_free(routerstatus_t *rs)
254 tor_free(rs->exitsummary);
255 tor_free(rs);
258 /** Free all storage held by the networkstatus object <b>ns</b>. */
259 void
260 networkstatus_v2_free(networkstatus_v2_t *ns)
262 tor_free(ns->source_address);
263 tor_free(ns->contact);
264 if (ns->signing_key)
265 crypto_free_pk_env(ns->signing_key);
266 tor_free(ns->client_versions);
267 tor_free(ns->server_versions);
268 if (ns->entries) {
269 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
270 routerstatus_free(rs));
271 smartlist_free(ns->entries);
273 tor_free(ns);
276 /** Clear all storage held in <b>ns</b>. */
277 void
278 networkstatus_vote_free(networkstatus_t *ns)
280 if (!ns)
281 return;
283 tor_free(ns->client_versions);
284 tor_free(ns->server_versions);
285 if (ns->known_flags) {
286 SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
287 smartlist_free(ns->known_flags);
289 if (ns->supported_methods) {
290 SMARTLIST_FOREACH(ns->supported_methods, char *, c, tor_free(c));
291 smartlist_free(ns->supported_methods);
293 if (ns->voters) {
294 SMARTLIST_FOREACH(ns->voters, networkstatus_voter_info_t *, voter,
296 tor_free(voter->nickname);
297 tor_free(voter->address);
298 tor_free(voter->contact);
299 tor_free(voter->signature);
300 tor_free(voter);
302 smartlist_free(ns->voters);
304 if (ns->cert)
305 authority_cert_free(ns->cert);
307 if (ns->routerstatus_list) {
308 if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
309 SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
310 vote_routerstatus_free(rs));
311 } else {
312 SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
313 routerstatus_free(rs));
316 smartlist_free(ns->routerstatus_list);
318 if (ns->desc_digest_map)
319 digestmap_free(ns->desc_digest_map, NULL);
321 memset(ns, 11, sizeof(*ns));
322 tor_free(ns);
325 /** Return the voter info from <b>vote</b> for the voter whose identity digest
326 * is <b>identity</b>, or NULL if no such voter is associated with
327 * <b>vote</b>. */
328 networkstatus_voter_info_t *
329 networkstatus_get_voter_by_id(networkstatus_t *vote,
330 const char *identity)
332 if (!vote || !vote->voters)
333 return NULL;
334 SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
335 if (!memcmp(voter->identity_digest, identity, DIGEST_LEN))
336 return voter);
337 return NULL;
340 /** Check whether the signature on <b>voter</b> is correctly signed by
341 * the signing key of <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
342 * signing key; otherwise set the good_signature or bad_signature flag on
343 * <b>voter</b>, and return 0. */
344 /* (private; exposed for testing.) */
346 networkstatus_check_voter_signature(networkstatus_t *consensus,
347 networkstatus_voter_info_t *voter,
348 authority_cert_t *cert)
350 char d[DIGEST_LEN];
351 char *signed_digest;
352 size_t signed_digest_len;
353 if (crypto_pk_get_digest(cert->signing_key, d)<0)
354 return -1;
355 if (memcmp(voter->signing_key_digest, d, DIGEST_LEN))
356 return -1;
357 signed_digest_len = crypto_pk_keysize(cert->signing_key);
358 signed_digest = tor_malloc(signed_digest_len);
359 if (crypto_pk_public_checksig(cert->signing_key,
360 signed_digest,
361 voter->signature,
362 voter->signature_len) != DIGEST_LEN ||
363 memcmp(signed_digest, consensus->networkstatus_digest, DIGEST_LEN)) {
364 log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
365 voter->bad_signature = 1;
366 } else {
367 voter->good_signature = 1;
369 tor_free(signed_digest);
370 return 0;
373 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
374 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
375 * signature from every recognized authority on it, 0 if there are
376 * enough good signatures from recognized authorities on it, -1 if we might
377 * get enough good signatures by fetching missing certificates, and -2
378 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
379 * about every problem; if warn is at least 1, warn only if we can't get
380 * enough signatures; if warn is negative, log nothing at all. */
382 networkstatus_check_consensus_signature(networkstatus_t *consensus,
383 int warn)
385 int n_good = 0;
386 int n_missing_key = 0;
387 int n_bad = 0;
388 int n_unknown = 0;
389 int n_no_signature = 0;
390 int n_v3_authorities = get_n_authorities(V3_AUTHORITY);
391 int n_required = n_v3_authorities/2 + 1;
392 smartlist_t *need_certs_from = smartlist_create();
393 smartlist_t *unrecognized = smartlist_create();
394 smartlist_t *missing_authorities = smartlist_create();
395 int severity;
396 time_t now = time(NULL);
398 tor_assert(consensus->type == NS_TYPE_CONSENSUS);
400 SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, voter,
402 if (!voter->good_signature && !voter->bad_signature && voter->signature) {
403 /* we can try to check the signature. */
404 int is_v3_auth = trusteddirserver_get_by_v3_auth_digest(
405 voter->identity_digest) != NULL;
406 authority_cert_t *cert =
407 authority_cert_get_by_digests(voter->identity_digest,
408 voter->signing_key_digest);
409 if (!is_v3_auth) {
410 smartlist_add(unrecognized, voter);
411 ++n_unknown;
412 continue;
413 } else if (!cert || cert->expires < now) {
414 smartlist_add(need_certs_from, voter);
415 ++n_missing_key;
416 continue;
418 if (networkstatus_check_voter_signature(consensus, voter, cert) < 0) {
419 smartlist_add(need_certs_from, voter);
420 ++n_missing_key;
421 continue;
424 if (voter->good_signature)
425 ++n_good;
426 else if (voter->bad_signature)
427 ++n_bad;
428 else
429 ++n_no_signature;
432 /* Now see whether we're missing any voters entirely. */
433 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
434 trusted_dir_server_t *, ds,
436 if ((ds->type & V3_AUTHORITY) &&
437 !networkstatus_get_voter_by_id(consensus, ds->v3_identity_digest))
438 smartlist_add(missing_authorities, ds);
441 if (warn > 1 || (warn >= 0 && n_good < n_required))
442 severity = LOG_WARN;
443 else
444 severity = LOG_INFO;
446 if (warn >= 0) {
447 SMARTLIST_FOREACH(unrecognized, networkstatus_voter_info_t *, voter,
449 log_info(LD_DIR, "Consensus includes unrecognized authority '%s' "
450 "at %s:%d (contact %s; identity %s)",
451 voter->nickname, voter->address, (int)voter->dir_port,
452 voter->contact?voter->contact:"n/a",
453 hex_str(voter->identity_digest, DIGEST_LEN));
455 SMARTLIST_FOREACH(need_certs_from, networkstatus_voter_info_t *, voter,
457 log_info(LD_DIR, "Looks like we need to download a new certificate "
458 "from authority '%s' at %s:%d (contact %s; identity %s)",
459 voter->nickname, voter->address, (int)voter->dir_port,
460 voter->contact?voter->contact:"n/a",
461 hex_str(voter->identity_digest, DIGEST_LEN));
463 SMARTLIST_FOREACH(missing_authorities, trusted_dir_server_t *, ds,
465 log_info(LD_DIR, "Consensus does not include configured "
466 "authority '%s' at %s:%d (identity %s)",
467 ds->nickname, ds->address, (int)ds->dir_port,
468 hex_str(ds->v3_identity_digest, DIGEST_LEN));
470 log(severity, LD_DIR,
471 "%d unknown, %d missing key, %d good, %d bad, %d no signature, "
472 "%d required", n_unknown, n_missing_key, n_good, n_bad,
473 n_no_signature, n_required);
476 smartlist_free(unrecognized);
477 smartlist_free(need_certs_from);
478 smartlist_free(missing_authorities);
480 if (n_good == n_v3_authorities)
481 return 1;
482 else if (n_good >= n_required)
483 return 0;
484 else if (n_good + n_missing_key >= n_required)
485 return -1;
486 else
487 return -2;
490 /** Helper: return a newly allocated string containing the name of the filename
491 * where we plan to cache the network status with the given identity digest. */
492 char *
493 networkstatus_get_cache_filename(const char *identity_digest)
495 char fp[HEX_DIGEST_LEN+1];
496 base16_encode(fp, HEX_DIGEST_LEN+1, identity_digest, DIGEST_LEN);
497 return get_datadir_fname2("cached-status", fp);
500 /** Helper for smartlist_sort: Compare two networkstatus objects by
501 * publication date. */
502 static int
503 _compare_networkstatus_v2_published_on(const void **_a, const void **_b)
505 const networkstatus_v2_t *a = *_a, *b = *_b;
506 if (a->published_on < b->published_on)
507 return -1;
508 else if (a->published_on > b->published_on)
509 return 1;
510 else
511 return 0;
514 /** Add the parsed v2 networkstatus in <b>ns</b> (with original document in
515 * <b>s</b>) to the disk cache (and the in-memory directory server cache) as
516 * appropriate. */
517 static int
518 add_networkstatus_to_cache(const char *s,
519 v2_networkstatus_source_t source,
520 networkstatus_v2_t *ns)
522 if (source != NS_FROM_CACHE) {
523 char *fn = networkstatus_get_cache_filename(ns->identity_digest);
524 if (write_str_to_file(fn, s, 0)<0) {
525 log_notice(LD_FS, "Couldn't write cached network status to \"%s\"", fn);
527 tor_free(fn);
530 if (directory_caches_v2_dir_info(get_options()))
531 dirserv_set_cached_networkstatus_v2(s,
532 ns->identity_digest,
533 ns->published_on);
535 return 0;
538 /** How far in the future do we allow a network-status to get before removing
539 * it? (seconds) */
540 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
542 /** Given a string <b>s</b> containing a network status that we received at
543 * <b>arrived_at</b> from <b>source</b>, try to parse it, see if we want to
544 * store it, and put it into our cache as necessary.
546 * If <b>source</b> is NS_FROM_DIR or NS_FROM_CACHE, do not replace our
547 * own networkstatus_t (if we're an authoritative directory server).
549 * If <b>source</b> is NS_FROM_CACHE, do not write our networkstatus_t to the
550 * cache.
552 * If <b>requested_fingerprints</b> is provided, it must contain a list of
553 * uppercased identity fingerprints. Do not update any networkstatus whose
554 * fingerprint is not on the list; after updating a networkstatus, remove its
555 * fingerprint from the list.
557 * Return 0 on success, -1 on failure.
559 * Callers should make sure that routers_update_all_from_networkstatus() is
560 * invoked after this function succeeds.
563 router_set_networkstatus_v2(const char *s, time_t arrived_at,
564 v2_networkstatus_source_t source,
565 smartlist_t *requested_fingerprints)
567 networkstatus_v2_t *ns;
568 int i, found;
569 time_t now;
570 int skewed = 0;
571 trusted_dir_server_t *trusted_dir = NULL;
572 const char *source_desc = NULL;
573 char fp[HEX_DIGEST_LEN+1];
574 char published[ISO_TIME_LEN+1];
576 if (!directory_caches_v2_dir_info(get_options()))
577 return 0; /* Don't bother storing it. */
579 ns = networkstatus_v2_parse_from_string(s);
580 if (!ns) {
581 log_warn(LD_DIR, "Couldn't parse network status.");
582 return -1;
584 base16_encode(fp, HEX_DIGEST_LEN+1, ns->identity_digest, DIGEST_LEN);
585 if (!(trusted_dir =
586 router_get_trusteddirserver_by_digest(ns->identity_digest)) ||
587 !(trusted_dir->type & V2_AUTHORITY)) {
588 log_info(LD_DIR, "Network status was signed, but not by an authoritative "
589 "directory we recognize.");
590 source_desc = fp;
591 } else {
592 source_desc = trusted_dir->description;
594 now = time(NULL);
595 if (arrived_at > now)
596 arrived_at = now;
598 ns->received_on = arrived_at;
600 format_iso_time(published, ns->published_on);
602 if (ns->published_on > now + NETWORKSTATUS_ALLOW_SKEW) {
603 char dbuf[64];
604 long delta = now - ns->published_on;
605 format_time_interval(dbuf, sizeof(dbuf), delta);
606 log_warn(LD_GENERAL, "Network status from %s was published %s in the "
607 "future (%s GMT). Check your time and date settings! "
608 "Not caching.",
609 source_desc, dbuf, published);
610 control_event_general_status(LOG_WARN,
611 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=NETWORKSTATUS:%s:%d",
612 delta, ns->source_address, ns->source_dirport);
613 skewed = 1;
616 if (!networkstatus_v2_list)
617 networkstatus_v2_list = smartlist_create();
619 if ( (source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) &&
620 router_digest_is_me(ns->identity_digest)) {
621 /* Don't replace our own networkstatus when we get it from somebody else.*/
622 networkstatus_v2_free(ns);
623 return 0;
626 if (requested_fingerprints) {
627 if (smartlist_string_isin(requested_fingerprints, fp)) {
628 smartlist_string_remove(requested_fingerprints, fp);
629 } else {
630 if (source != NS_FROM_DIR_ALL) {
631 char *requested =
632 smartlist_join_strings(requested_fingerprints," ",0,NULL);
633 log_warn(LD_DIR,
634 "We received a network status with a fingerprint (%s) that we "
635 "never requested. (We asked for: %s.) Dropping.",
636 fp, requested);
637 tor_free(requested);
638 return 0;
643 if (!trusted_dir) {
644 if (!skewed) {
645 /* We got a non-trusted networkstatus, and we're a directory cache.
646 * This means that we asked an authority, and it told us about another
647 * authority we didn't recognize. */
648 log_info(LD_DIR,
649 "We do not recognize authority (%s) but we are willing "
650 "to cache it.", fp);
651 add_networkstatus_to_cache(s, source, ns);
652 networkstatus_v2_free(ns);
654 return 0;
657 found = 0;
658 for (i=0; i < smartlist_len(networkstatus_v2_list); ++i) {
659 networkstatus_v2_t *old_ns = smartlist_get(networkstatus_v2_list, i);
661 if (!memcmp(old_ns->identity_digest, ns->identity_digest, DIGEST_LEN)) {
662 if (!memcmp(old_ns->networkstatus_digest,
663 ns->networkstatus_digest, DIGEST_LEN)) {
664 /* Same one we had before. */
665 networkstatus_v2_free(ns);
666 tor_assert(trusted_dir);
667 log_info(LD_DIR,
668 "Not replacing network-status from %s (published %s); "
669 "we already have it.",
670 trusted_dir->description, published);
671 if (old_ns->received_on < arrived_at) {
672 if (source != NS_FROM_CACHE) {
673 char *fn;
674 fn = networkstatus_get_cache_filename(old_ns->identity_digest);
675 /* We use mtime to tell when it arrived, so update that. */
676 touch_file(fn);
677 tor_free(fn);
679 old_ns->received_on = arrived_at;
681 download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
682 return 0;
683 } else if (old_ns->published_on >= ns->published_on) {
684 char old_published[ISO_TIME_LEN+1];
685 format_iso_time(old_published, old_ns->published_on);
686 tor_assert(trusted_dir);
687 log_info(LD_DIR,
688 "Not replacing network-status from %s (published %s);"
689 " we have a newer one (published %s) for this authority.",
690 trusted_dir->description, published,
691 old_published);
692 networkstatus_v2_free(ns);
693 download_status_failed(&trusted_dir->v2_ns_dl_status, 0);
694 return 0;
695 } else {
696 networkstatus_v2_free(old_ns);
697 smartlist_set(networkstatus_v2_list, i, ns);
698 found = 1;
699 break;
704 if (source != NS_FROM_CACHE && trusted_dir) {
705 download_status_reset(&trusted_dir->v2_ns_dl_status);
708 if (!found)
709 smartlist_add(networkstatus_v2_list, ns);
711 /** Retain any routerinfo mentioned in a V2 networkstatus for at least this
712 * long. */
713 #define V2_NETWORKSTATUS_ROUTER_LIFETIME (3*60*60)
716 time_t live_until = ns->published_on + V2_NETWORKSTATUS_ROUTER_LIFETIME;
717 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
719 signed_descriptor_t *sd =
720 router_get_by_descriptor_digest(rs->descriptor_digest);
721 if (sd) {
722 if (sd->last_listed_as_valid_until < live_until)
723 sd->last_listed_as_valid_until = live_until;
724 } else {
725 rs->need_to_mirror = 1;
730 log_info(LD_DIR, "Setting networkstatus %s %s (published %s)",
731 source == NS_FROM_CACHE?"cached from":
732 ((source == NS_FROM_DIR_BY_FP || source == NS_FROM_DIR_ALL) ?
733 "downloaded from":"generated for"),
734 trusted_dir->description, published);
735 networkstatus_v2_list_has_changed = 1;
737 smartlist_sort(networkstatus_v2_list,
738 _compare_networkstatus_v2_published_on);
740 if (!skewed)
741 add_networkstatus_to_cache(s, source, ns);
743 return 0;
746 /** Remove all very-old network_status_t objects from memory and from the
747 * disk cache. */
748 void
749 networkstatus_v2_list_clean(time_t now)
751 int i;
752 if (!networkstatus_v2_list)
753 return;
755 for (i = 0; i < smartlist_len(networkstatus_v2_list); ++i) {
756 networkstatus_v2_t *ns = smartlist_get(networkstatus_v2_list, i);
757 char *fname = NULL;
758 if (ns->published_on + MAX_NETWORKSTATUS_AGE > now)
759 continue;
760 /* Okay, this one is too old. Remove it from the list, and delete it
761 * from the cache. */
762 smartlist_del(networkstatus_v2_list, i--);
763 fname = networkstatus_get_cache_filename(ns->identity_digest);
764 if (file_status(fname) == FN_FILE) {
765 log_info(LD_DIR, "Removing too-old networkstatus in %s", fname);
766 unlink(fname);
768 tor_free(fname);
769 if (directory_caches_v2_dir_info(get_options())) {
770 dirserv_set_cached_networkstatus_v2(NULL, ns->identity_digest, 0);
772 networkstatus_v2_free(ns);
775 /* And now go through the directory cache for any cached untrusted
776 * networkstatuses and other network info. */
777 dirserv_clear_old_networkstatuses(now - MAX_NETWORKSTATUS_AGE);
778 dirserv_clear_old_v1_info(now);
781 /** Helper for bsearching a list of routerstatus_t pointers: compare a
782 * digest in the key to the identity digest of a routerstatus_t. */
783 static int
784 _compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
786 const char *key = _key;
787 const routerstatus_t *rs = *_member;
788 return memcmp(key, rs->identity_digest, DIGEST_LEN);
791 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
792 * NULL if none was found. */
793 routerstatus_t *
794 networkstatus_v2_find_entry(networkstatus_v2_t *ns, const char *digest)
796 return smartlist_bsearch(ns->entries, digest,
797 _compare_digest_to_routerstatus_entry);
800 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
801 * NULL if none was found. */
802 routerstatus_t *
803 networkstatus_vote_find_entry(networkstatus_t *ns, const char *digest)
805 return smartlist_bsearch(ns->routerstatus_list, digest,
806 _compare_digest_to_routerstatus_entry);
809 /*XXXX make this static once functions are moved into this file. */
810 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
811 * <b>digest</b>. Return value and set *<b>found_out</b> as for
812 * smartlist_bsearch_idx(). */
814 networkstatus_vote_find_entry_idx(networkstatus_t *ns,
815 const char *digest, int *found_out)
817 return smartlist_bsearch_idx(ns->routerstatus_list, digest,
818 _compare_digest_to_routerstatus_entry,
819 found_out);
822 /** Return a list of the v2 networkstatus documents. */
823 const smartlist_t *
824 networkstatus_get_v2_list(void)
826 if (!networkstatus_v2_list)
827 networkstatus_v2_list = smartlist_create();
828 return networkstatus_v2_list;
831 /** Return the consensus view of the status of the router whose current
832 * <i>descriptor</i> digest is <b>digest</b>, or NULL if no such router is
833 * known. */
834 routerstatus_t *
835 router_get_consensus_status_by_descriptor_digest(const char *digest)
837 if (!current_consensus) return NULL;
838 if (!current_consensus->desc_digest_map) {
839 digestmap_t * m = current_consensus->desc_digest_map = digestmap_new();
840 SMARTLIST_FOREACH(current_consensus->routerstatus_list,
841 routerstatus_t *, rs,
843 digestmap_set(m, rs->descriptor_digest, rs);
846 return digestmap_get(current_consensus->desc_digest_map, digest);
849 /** Given the digest of a router descriptor, return its current download
850 * status, or NULL if the digest is unrecognized. */
851 download_status_t *
852 router_get_dl_status_by_descriptor_digest(const char *d)
854 routerstatus_t *rs;
855 if ((rs = router_get_consensus_status_by_descriptor_digest(d)))
856 return &rs->dl_status;
857 if (v2_download_status_map)
858 return digestmap_get(v2_download_status_map, d);
860 return NULL;
863 /** Return the consensus view of the status of the router whose identity
864 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
865 routerstatus_t *
866 router_get_consensus_status_by_id(const char *digest)
868 if (!current_consensus)
869 return NULL;
870 return smartlist_bsearch(current_consensus->routerstatus_list, digest,
871 _compare_digest_to_routerstatus_entry);
874 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
875 * the corresponding routerstatus_t, or NULL if none exists. Warn the
876 * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
877 * nickname, but the Named flag isn't set for that router. */
878 routerstatus_t *
879 router_get_consensus_status_by_nickname(const char *nickname,
880 int warn_if_unnamed)
882 char digest[DIGEST_LEN];
883 routerstatus_t *best=NULL;
884 smartlist_t *matches=NULL;
885 const char *named_id=NULL;
887 if (!current_consensus || !nickname)
888 return NULL;
890 /* Is this name really a hexadecimal identity digest? */
891 if (nickname[0] == '$') {
892 if (base16_decode(digest, DIGEST_LEN, nickname+1, strlen(nickname+1))<0)
893 return NULL;
894 return networkstatus_vote_find_entry(current_consensus, digest);
895 } else if (strlen(nickname) == HEX_DIGEST_LEN &&
896 (base16_decode(digest, DIGEST_LEN, nickname, strlen(nickname))==0)) {
897 return networkstatus_vote_find_entry(current_consensus, digest);
900 /* Is there a server that is Named with this name? */
901 if (named_server_map)
902 named_id = strmap_get_lc(named_server_map, nickname);
903 if (named_id)
904 return networkstatus_vote_find_entry(current_consensus, named_id);
906 /* Okay; is this name listed as Unnamed? */
907 if (unnamed_server_map &&
908 strmap_get_lc(unnamed_server_map, nickname)) {
909 log_info(LD_GENERAL, "The name %s is listed as Unnamed; it is not the "
910 "canonical name of any server we know.", escaped(nickname));
911 return NULL;
914 /* This name is not canonical for any server; go through the list and
915 * see who it matches. */
916 /*XXXX This is inefficient; optimize it if it matters. */
917 matches = smartlist_create();
918 SMARTLIST_FOREACH(current_consensus->routerstatus_list,
919 routerstatus_t *, lrs,
921 if (!strcasecmp(lrs->nickname, nickname)) {
922 if (lrs->is_named) {
923 tor_fragile_assert() /* This should never happen. */
924 smartlist_free(matches);
925 return lrs;
926 } else {
927 if (lrs->is_unnamed) {
928 tor_fragile_assert(); /* nor should this. */
929 smartlist_clear(matches);
930 best=NULL;
931 break;
933 smartlist_add(matches, lrs);
934 best = lrs;
939 if (smartlist_len(matches)>1 && warn_if_unnamed) {
940 int any_unwarned=0;
941 SMARTLIST_FOREACH(matches, routerstatus_t *, lrs,
943 if (! lrs->name_lookup_warned) {
944 lrs->name_lookup_warned=1;
945 any_unwarned=1;
948 if (any_unwarned) {
949 log_warn(LD_CONFIG,"There are multiple matches for the nickname \"%s\","
950 " but none is listed as named by the directory authorities. "
951 "Choosing one arbitrarily.", nickname);
953 } else if (warn_if_unnamed && best && !best->name_lookup_warned) {
954 char fp[HEX_DIGEST_LEN+1];
955 base16_encode(fp, sizeof(fp),
956 best->identity_digest, DIGEST_LEN);
957 log_warn(LD_CONFIG,
958 "When looking up a status, you specified a server \"%s\" by name, "
959 "but the directory authorities do not have any key registered for "
960 "this nickname -- so it could be used by any server, "
961 "not just the one you meant. "
962 "To make sure you get the same server in the future, refer to "
963 "it by key, as \"$%s\".", nickname, fp);
964 best->name_lookup_warned = 1;
966 smartlist_free(matches);
967 return best;
970 /** Return the identity digest that's mapped to officially by
971 * <b>nickname</b>. */
972 const char *
973 networkstatus_get_router_digest_by_nickname(const char *nickname)
975 if (!named_server_map)
976 return NULL;
977 return strmap_get_lc(named_server_map, nickname);
980 /** Return true iff <b>nickname</b> is disallowed from being the nickname
981 * of any server. */
983 networkstatus_nickname_is_unnamed(const char *nickname)
985 if (!unnamed_server_map)
986 return 0;
987 return strmap_get_lc(unnamed_server_map, nickname) != NULL;
990 /** How frequently do directory authorities re-download fresh networkstatus
991 * documents? */
992 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
994 /** How frequently do non-authority directory caches re-download fresh
995 * networkstatus documents? */
996 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
998 /** We are a directory server, and so cache network_status documents.
999 * Initiate downloads as needed to update them. For v2 authorities,
1000 * this means asking each trusted directory for its network-status.
1001 * For caches, this means asking a random v2 authority for all
1002 * network-statuses.
1004 static void
1005 update_v2_networkstatus_cache_downloads(time_t now)
1007 int authority = authdir_mode_v2(get_options());
1008 int interval =
1009 authority ? AUTHORITY_NS_CACHE_INTERVAL : NONAUTHORITY_NS_CACHE_INTERVAL;
1010 const smartlist_t *trusted_dir_servers = router_get_trusted_dir_servers();
1012 if (last_networkstatus_download_attempted + interval >= now)
1013 return;
1015 last_networkstatus_download_attempted = now;
1017 if (authority) {
1018 /* An authority launches a separate connection for everybody. */
1019 SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, trusted_dir_server_t *, ds)
1021 char resource[HEX_DIGEST_LEN+6]; /* fp/hexdigit.z\0 */
1022 tor_addr_t addr;
1023 if (!(ds->type & V2_AUTHORITY))
1024 continue;
1025 if (router_digest_is_me(ds->digest))
1026 continue;
1027 tor_addr_from_ipv4h(&addr, ds->addr);
1028 /* Is this quite sensible with IPv6 or multiple addresses? */
1029 if (connection_get_by_type_addr_port_purpose(
1030 CONN_TYPE_DIR, &addr, ds->dir_port,
1031 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS)) {
1032 /* XXX the above dir_port won't be accurate if we're
1033 * doing a tunneled conn. In that case it should be or_port.
1034 * How to guess from here? Maybe make the function less general
1035 * and have it know that it's looking for dir conns. -RD */
1036 /* Only directory caches download v2 networkstatuses, and they
1037 * don't use tunneled connections. I think it's okay to ignore
1038 * this. */
1039 continue;
1041 strlcpy(resource, "fp/", sizeof(resource));
1042 base16_encode(resource+3, sizeof(resource)-3, ds->digest, DIGEST_LEN);
1043 strlcat(resource, ".z", sizeof(resource));
1044 directory_initiate_command_routerstatus(
1045 &ds->fake_status, DIR_PURPOSE_FETCH_V2_NETWORKSTATUS,
1046 ROUTER_PURPOSE_GENERAL,
1047 0, /* Not private */
1048 resource,
1049 NULL, 0 /* No payload. */,
1050 0 /* No I-M-S. */);
1052 SMARTLIST_FOREACH_END(ds);
1053 } else {
1054 /* A non-authority cache launches one connection to a random authority. */
1055 /* (Check whether we're currently fetching network-status objects.) */
1056 if (!connection_get_by_type_purpose(CONN_TYPE_DIR,
1057 DIR_PURPOSE_FETCH_V2_NETWORKSTATUS))
1058 directory_get_from_dirserver(DIR_PURPOSE_FETCH_V2_NETWORKSTATUS,
1059 ROUTER_PURPOSE_GENERAL, "all.z",
1060 PDS_RETRY_IF_NO_SERVERS);
1064 /** How many times will we try to fetch a consensus before we give up? */
1065 #define CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES 8
1066 /** How long will we hang onto a possibly live consensus for which we're
1067 * fetching certs before we check whether there is a better one? */
1068 #define DELAY_WHILE_FETCHING_CERTS (20*60)
1070 /** If we want to download a fresh consensus, launch a new download as
1071 * appropriate. */
1072 static void
1073 update_consensus_networkstatus_downloads(time_t now)
1075 or_options_t *options = get_options();
1076 if (!networkstatus_get_live_consensus(now))
1077 time_to_download_next_consensus = now; /* No live consensus? Get one now!*/
1078 if (time_to_download_next_consensus > now)
1079 return; /* Wait until the current consensus is older. */
1080 if (authdir_mode_v3(options))
1081 return; /* Authorities never fetch a consensus */
1082 if (!download_status_is_ready(&consensus_dl_status, now,
1083 CONSENSUS_NETWORKSTATUS_MAX_DL_TRIES))
1084 return; /* We failed downloading a consensus too recently. */
1085 if (connection_get_by_type_purpose(CONN_TYPE_DIR,
1086 DIR_PURPOSE_FETCH_CONSENSUS))
1087 return; /* There's an in-progress download.*/
1089 if (consensus_waiting_for_certs) {
1090 /* XXXX make sure this doesn't delay sane downloads. */
1091 if (consensus_waiting_for_certs_set_at + DELAY_WHILE_FETCHING_CERTS > now)
1092 return; /* We're still getting certs for this one. */
1093 else {
1094 if (!consensus_waiting_for_certs_dl_failed) {
1095 download_status_failed(&consensus_dl_status, 0);
1096 consensus_waiting_for_certs_dl_failed=1;
1101 log_info(LD_DIR, "Launching networkstatus consensus download.");
1102 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS,
1103 ROUTER_PURPOSE_GENERAL, NULL,
1104 PDS_RETRY_IF_NO_SERVERS);
1107 /** Called when an attempt to download a consensus fails: note that the
1108 * failure occurred, and possibly retry. */
1109 void
1110 networkstatus_consensus_download_failed(int status_code)
1112 download_status_failed(&consensus_dl_status, status_code);
1113 /* Retry immediately, if appropriate. */
1114 update_consensus_networkstatus_downloads(time(NULL));
1117 /** How long do we (as a cache) wait after a consensus becomes non-fresh
1118 * before trying to fetch another? */
1119 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
1121 /** Update the time at which we'll consider replacing the current
1122 * consensus. */
1123 void
1124 update_consensus_networkstatus_fetch_time(time_t now)
1126 or_options_t *options = get_options();
1127 networkstatus_t *c = networkstatus_get_live_consensus(now);
1128 if (c) {
1129 long dl_interval;
1130 long interval = c->fresh_until - c->valid_after;
1131 time_t start;
1132 if (directory_fetches_dir_info_early(options)) {
1133 /* We want to cache the next one at some point after this one
1134 * is no longer fresh... */
1135 start = c->fresh_until + CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
1136 /* But only in the first half-interval after that. */
1137 dl_interval = interval/2;
1138 } else {
1139 /* We're an ordinary client or a bridge. Give all the caches enough
1140 * time to download the consensus. */
1141 start = c->fresh_until + (interval*3)/4;
1142 /* But download the next one well before this one is expired. */
1143 dl_interval = ((c->valid_until - start) * 7 )/ 8;
1145 /* If we're a bridge user, make use of the numbers we just computed
1146 * to choose the rest of the interval *after* them. */
1147 if (directory_fetches_dir_info_later(options)) {
1148 /* Give all the *clients* enough time to download the consensus. */
1149 start = start + dl_interval + CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
1150 /* But try to get it before ours actually expires. */
1151 dl_interval = (c->valid_until - start) -
1152 CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
1155 if (dl_interval < 1)
1156 dl_interval = 1;
1157 /* We must not try to replace c while it's still the most valid: */
1158 tor_assert(c->fresh_until < start);
1159 /* We must download the next one before c is invalid: */
1160 tor_assert(start+dl_interval < c->valid_until);
1161 time_to_download_next_consensus = start +crypto_rand_int((int)dl_interval);
1163 char tbuf1[ISO_TIME_LEN+1];
1164 char tbuf2[ISO_TIME_LEN+1];
1165 char tbuf3[ISO_TIME_LEN+1];
1166 format_local_iso_time(tbuf1, c->fresh_until);
1167 format_local_iso_time(tbuf2, c->valid_until);
1168 format_local_iso_time(tbuf3, time_to_download_next_consensus);
1169 log_info(LD_DIR, "Live consensus %s the most recent until %s and will "
1170 "expire at %s; fetching the next one at %s.",
1171 (c->fresh_until > now) ? "will be" : "was",
1172 tbuf1, tbuf2, tbuf3);
1174 } else {
1175 time_to_download_next_consensus = now;
1176 log_info(LD_DIR, "No live consensus; we should fetch one immediately.");
1181 /** Return 1 if there's a reason we shouldn't try any directory
1182 * fetches yet (e.g. we demand bridges and none are yet known).
1183 * Else return 0. */
1185 should_delay_dir_fetches(or_options_t *options)
1187 if (options->UseBridges && !any_bridge_descriptors_known()) {
1188 log_info(LD_DIR, "delaying dir fetches (no running bridges known)");
1189 return 1;
1191 return 0;
1194 /** Launch requests for networkstatus documents and authority certificates as
1195 * appropriate. */
1196 void
1197 update_networkstatus_downloads(time_t now)
1199 or_options_t *options = get_options();
1200 if (should_delay_dir_fetches(options))
1201 return;
1202 if (directory_fetches_dir_info_early(options))
1203 update_v2_networkstatus_cache_downloads(now);
1204 update_consensus_networkstatus_downloads(now);
1205 update_certificate_downloads(now);
1208 /** Launch requests as appropriate for missing directory authority
1209 * certificates. */
1210 void
1211 update_certificate_downloads(time_t now)
1213 if (consensus_waiting_for_certs)
1214 authority_certs_fetch_missing(consensus_waiting_for_certs, now);
1215 else
1216 authority_certs_fetch_missing(current_consensus, now);
1219 /** Return 1 if we have a consensus but we don't have enough certificates
1220 * to start using it yet. */
1222 consensus_is_waiting_for_certs(void)
1224 return consensus_waiting_for_certs ? 1 : 0;
1227 /** Return the network status with a given identity digest. */
1228 networkstatus_v2_t *
1229 networkstatus_v2_get_by_digest(const char *digest)
1231 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
1233 if (!memcmp(ns->identity_digest, digest, DIGEST_LEN))
1234 return ns;
1236 return NULL;
1239 /** Return the most recent consensus that we have downloaded, or NULL if we
1240 * don't have one. */
1241 networkstatus_t *
1242 networkstatus_get_latest_consensus(void)
1244 return current_consensus;
1247 /** Return the most recent consensus that we have downloaded, or NULL if it is
1248 * no longer live. */
1249 networkstatus_t *
1250 networkstatus_get_live_consensus(time_t now)
1252 if (current_consensus &&
1253 current_consensus->valid_after <= now &&
1254 now <= current_consensus->valid_until)
1255 return current_consensus;
1256 else
1257 return NULL;
1260 /* XXXX remove this in favor of get_live_consensus. But actually,
1261 * leave something like it for bridge users, who need to not totally
1262 * lose if they spend a while fetching a new consensus. */
1263 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1264 * consensuses. */
1265 networkstatus_t *
1266 networkstatus_get_reasonably_live_consensus(time_t now)
1268 #define REASONABLY_LIVE_TIME (24*60*60)
1269 if (current_consensus &&
1270 current_consensus->valid_after <= now &&
1271 now <= current_consensus->valid_until+REASONABLY_LIVE_TIME)
1272 return current_consensus;
1273 else
1274 return NULL;
1277 /** Given two router status entries for the same router identity, return 1 if
1278 * if the contents have changed between them. Otherwise, return 0. */
1279 static int
1280 routerstatus_has_changed(const routerstatus_t *a, const routerstatus_t *b)
1282 tor_assert(!memcmp(a->identity_digest, b->identity_digest, DIGEST_LEN));
1284 return strcmp(a->nickname, b->nickname) ||
1285 memcmp(a->descriptor_digest, b->descriptor_digest, DIGEST_LEN) ||
1286 a->addr != b->addr ||
1287 a->or_port != b->or_port ||
1288 a->dir_port != b->dir_port ||
1289 a->is_authority != b->is_authority ||
1290 a->is_exit != b->is_exit ||
1291 a->is_stable != b->is_stable ||
1292 a->is_fast != b->is_fast ||
1293 a->is_running != b->is_running ||
1294 a->is_named != b->is_named ||
1295 a->is_unnamed != b->is_unnamed ||
1296 a->is_valid != b->is_valid ||
1297 a->is_v2_dir != b->is_v2_dir ||
1298 a->is_possible_guard != b->is_possible_guard ||
1299 a->is_bad_exit != b->is_bad_exit ||
1300 a->is_bad_directory != b->is_bad_directory ||
1301 a->is_hs_dir != b->is_hs_dir ||
1302 a->version_known != b->version_known ||
1303 a->version_supports_begindir != b->version_supports_begindir ||
1304 a->version_supports_extrainfo_upload !=
1305 b->version_supports_extrainfo_upload ||
1306 a->version_supports_conditional_consensus !=
1307 b->version_supports_conditional_consensus ||
1308 a->version_supports_v3_dir != b->version_supports_v3_dir;
1311 /** Notify controllers of any router status entries that changed between
1312 * <b>old_c</b> and <b>new_c</b>. */
1313 static void
1314 notify_control_networkstatus_changed(const networkstatus_t *old_c,
1315 const networkstatus_t *new_c)
1317 smartlist_t *changed;
1318 if (old_c == new_c)
1319 return;
1321 /* tell the controller exactly which relays are still listed, as well
1322 * as what they're listed as */
1323 control_event_newconsensus(new_c);
1325 if (!control_event_is_interesting(EVENT_NS))
1326 return;
1328 if (!old_c) {
1329 control_event_networkstatus_changed(new_c->routerstatus_list);
1330 return;
1332 changed = smartlist_create();
1334 SMARTLIST_FOREACH_JOIN(old_c->routerstatus_list, routerstatus_t *, rs_old,
1335 new_c->routerstatus_list, routerstatus_t *, rs_new,
1336 memcmp(rs_old->identity_digest,
1337 rs_new->identity_digest, DIGEST_LEN),
1338 smartlist_add(changed, rs_new)) {
1339 if (routerstatus_has_changed(rs_old, rs_new))
1340 smartlist_add(changed, rs_new);
1341 } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
1343 control_event_networkstatus_changed(changed);
1344 smartlist_free(changed);
1347 /** Copy all the ancillary information (like router download status and so on)
1348 * from <b>old_c</b> to <b>new_c</b>. */
1349 static void
1350 networkstatus_copy_old_consensus_info(networkstatus_t *new_c,
1351 const networkstatus_t *old_c)
1353 if (old_c == new_c)
1354 return;
1355 if (!old_c || !smartlist_len(old_c->routerstatus_list))
1356 return;
1358 SMARTLIST_FOREACH_JOIN(old_c->routerstatus_list, routerstatus_t *, rs_old,
1359 new_c->routerstatus_list, routerstatus_t *, rs_new,
1360 memcmp(rs_old->identity_digest,
1361 rs_new->identity_digest, DIGEST_LEN),
1362 STMT_NIL) {
1363 /* Okay, so we're looking at the same identity. */
1364 rs_new->name_lookup_warned = rs_old->name_lookup_warned;
1365 rs_new->last_dir_503_at = rs_old->last_dir_503_at;
1367 if (!memcmp(rs_old->descriptor_digest, rs_new->descriptor_digest,
1368 DIGEST_LEN)) {
1369 /* And the same descriptor too! */
1370 memcpy(&rs_new->dl_status, &rs_old->dl_status,sizeof(download_status_t));
1372 } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
1375 /** Try to replace the current cached v3 networkstatus with the one in
1376 * <b>consensus</b>. If we don't have enough certificates to validate it,
1377 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1379 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1380 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1381 * already received, but we were waiting for certificates on it. If flags &
1382 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1383 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1384 * consensus, even if it comes from many days in the past.
1386 * Return 0 on success, <0 on failure. On failure, caller should increment
1387 * the failure count as appropriate.
1389 * We return -1 for mild failures that don't need to be reported to the
1390 * user, and -2 for more serious problems.
1393 networkstatus_set_current_consensus(const char *consensus, unsigned flags)
1395 networkstatus_t *c;
1396 int r, result = -1;
1397 time_t now = time(NULL);
1398 char *unverified_fname = NULL, *consensus_fname = NULL;
1399 const unsigned from_cache = flags & NSSET_FROM_CACHE;
1400 const unsigned was_waiting_for_certs = flags & NSSET_WAS_WAITING_FOR_CERTS;
1401 const unsigned dl_certs = !(flags & NSSET_DONT_DOWNLOAD_CERTS);
1402 const unsigned accept_obsolete = flags & NSSET_ACCEPT_OBSOLETE;
1404 /* Make sure it's parseable. */
1405 c = networkstatus_parse_vote_from_string(consensus, NULL, NS_TYPE_CONSENSUS);
1406 if (!c) {
1407 log_warn(LD_DIR, "Unable to parse networkstatus consensus");
1408 result = -2;
1409 goto done;
1412 if (from_cache && !accept_obsolete &&
1413 c->valid_until < now-OLD_ROUTER_DESC_MAX_AGE) {
1414 /* XXX022 when we try to make fallbackconsensus work again, we should
1415 * consider taking this out. Until then, believing obsolete consensuses
1416 * is causing more harm than good. See also bug 887. */
1417 log_info(LD_DIR, "Loaded an obsolete consensus. Discarding.");
1418 goto done;
1421 if (current_consensus &&
1422 !memcmp(c->networkstatus_digest, current_consensus->networkstatus_digest,
1423 DIGEST_LEN)) {
1424 /* We already have this one. That's a failure. */
1425 log_info(LD_DIR, "Got a consensus we already have");
1426 goto done;
1429 if (current_consensus && c->valid_after <= current_consensus->valid_after) {
1430 /* We have a newer one. There's no point in accepting this one,
1431 * even if it's great. */
1432 log_info(LD_DIR, "Got a consensus at least as old as the one we have");
1433 goto done;
1436 consensus_fname = get_datadir_fname("cached-consensus");
1437 unverified_fname = get_datadir_fname("unverified-consensus");
1439 /* Make sure it's signed enough. */
1440 if ((r=networkstatus_check_consensus_signature(c, 1))<0) {
1441 if (r == -1) {
1442 /* Okay, so it _might_ be signed enough if we get more certificates. */
1443 if (!was_waiting_for_certs) {
1444 log_info(LD_DIR,
1445 "Not enough certificates to check networkstatus consensus");
1447 if (!current_consensus ||
1448 c->valid_after > current_consensus->valid_after) {
1449 if (consensus_waiting_for_certs)
1450 networkstatus_vote_free(consensus_waiting_for_certs);
1451 tor_free(consensus_waiting_for_certs_body);
1452 consensus_waiting_for_certs = c;
1453 c = NULL; /* Prevent free. */
1454 consensus_waiting_for_certs_body = tor_strdup(consensus);
1455 consensus_waiting_for_certs_set_at = now;
1456 consensus_waiting_for_certs_dl_failed = 0;
1457 if (!from_cache) {
1458 write_str_to_file(unverified_fname, consensus, 0);
1460 if (dl_certs)
1461 authority_certs_fetch_missing(c, now);
1462 /* This case is not a success or a failure until we get the certs
1463 * or fail to get the certs. */
1464 result = 0;
1465 } else {
1466 /* Even if we had enough signatures, we'd never use this as the
1467 * latest consensus. */
1468 if (was_waiting_for_certs && from_cache)
1469 unlink(unverified_fname);
1471 goto done;
1472 } else {
1473 /* This can never be signed enough: Kill it. */
1474 if (!was_waiting_for_certs) {
1475 log_warn(LD_DIR, "Not enough good signatures on networkstatus "
1476 "consensus");
1477 result = -2;
1479 if (was_waiting_for_certs && (r < -1) && from_cache)
1480 unlink(unverified_fname);
1481 goto done;
1485 if (!from_cache)
1486 control_event_client_status(LOG_NOTICE, "CONSENSUS_ARRIVED");
1488 /* Are we missing any certificates at all? */
1489 if (r != 1 && dl_certs)
1490 authority_certs_fetch_missing(c, now);
1492 notify_control_networkstatus_changed(current_consensus, c);
1494 if (current_consensus) {
1495 networkstatus_copy_old_consensus_info(c, current_consensus);
1496 networkstatus_vote_free(current_consensus);
1499 if (consensus_waiting_for_certs &&
1500 consensus_waiting_for_certs->valid_after <= c->valid_after) {
1501 networkstatus_vote_free(consensus_waiting_for_certs);
1502 consensus_waiting_for_certs = NULL;
1503 if (consensus != consensus_waiting_for_certs_body)
1504 tor_free(consensus_waiting_for_certs_body);
1505 else
1506 consensus_waiting_for_certs_body = NULL;
1507 consensus_waiting_for_certs_set_at = 0;
1508 consensus_waiting_for_certs_dl_failed = 0;
1509 unlink(unverified_fname);
1512 /* Reset the failure count only if this consensus is actually valid. */
1513 if (c->valid_after <= now && now <= c->valid_until) {
1514 download_status_reset(&consensus_dl_status);
1515 } else {
1516 if (!from_cache)
1517 download_status_failed(&consensus_dl_status, 0);
1520 current_consensus = c;
1521 c = NULL; /* Prevent free. */
1523 update_consensus_networkstatus_fetch_time(now);
1524 dirvote_recalculate_timing(get_options(), now);
1525 routerstatus_list_update_named_server_map();
1527 if (!from_cache) {
1528 write_str_to_file(consensus_fname, consensus, 0);
1531 if (directory_caches_dir_info(get_options()))
1532 dirserv_set_cached_networkstatus_v3(consensus,
1533 current_consensus->valid_after);
1535 if (ftime_definitely_before(now, current_consensus->valid_after)) {
1536 char tbuf[ISO_TIME_LEN+1];
1537 char dbuf[64];
1538 long delta = now - current_consensus->valid_after;
1539 format_iso_time(tbuf, current_consensus->valid_after);
1540 format_time_interval(dbuf, sizeof(dbuf), delta);
1541 log_warn(LD_GENERAL, "Our clock is %s behind the time published in the "
1542 "consensus network status document (%s GMT). Tor needs an "
1543 "accurate clock to work correctly. Please check your time and "
1544 "date settings!", dbuf, tbuf);
1545 control_event_general_status(LOG_WARN,
1546 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta);
1549 router_dir_info_changed();
1551 result = 0;
1552 done:
1553 if (c)
1554 networkstatus_vote_free(c);
1555 tor_free(consensus_fname);
1556 tor_free(unverified_fname);
1557 return result;
1560 /** Called when we have gotten more certificates: see whether we can
1561 * now verify a pending consensus. */
1562 void
1563 networkstatus_note_certs_arrived(void)
1565 if (consensus_waiting_for_certs) {
1566 if (networkstatus_check_consensus_signature(
1567 consensus_waiting_for_certs, 0)>=0) {
1568 if (!networkstatus_set_current_consensus(
1569 consensus_waiting_for_certs_body,
1570 NSSET_WAS_WAITING_FOR_CERTS)) {
1571 tor_free(consensus_waiting_for_certs_body);
1577 /** If the network-status list has changed since the last time we called this
1578 * function, update the status of every routerinfo from the network-status
1579 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
1580 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
1582 void
1583 routers_update_all_from_networkstatus(time_t now, int dir_version)
1585 routerlist_t *rl = router_get_routerlist();
1586 networkstatus_t *consensus = networkstatus_get_live_consensus(now);
1588 if (networkstatus_v2_list_has_changed)
1589 download_status_map_update_from_v2_networkstatus();
1591 if (!consensus || dir_version < 3) /* nothing more we should do */
1592 return;
1594 /* calls router_dir_info_changed() when it's done -- more routers
1595 * might be up or down now, which might affect whether there's enough
1596 * directory info. */
1597 routers_update_status_from_consensus_networkstatus(rl->routers, 0);
1599 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
1600 ri->cache_info.routerlist_index = ri_sl_idx);
1601 if (rl->old_routers)
1602 signed_descs_update_status_from_consensus_networkstatus(rl->old_routers);
1604 if (!have_warned_about_old_version) {
1605 int is_server = server_mode(get_options());
1606 version_status_t status;
1607 const char *recommended = is_server ?
1608 consensus->server_versions : consensus->client_versions;
1609 status = tor_version_is_obsolete(VERSION, recommended);
1611 if (status == VS_RECOMMENDED) {
1612 log_info(LD_GENERAL, "The directory authorities say my version is ok.");
1613 } else if (status == VS_EMPTY) {
1614 log_info(LD_GENERAL,
1615 "The directory authorities don't recommend any versions.");
1616 } else if (status == VS_NEW || status == VS_NEW_IN_SERIES) {
1617 if (!have_warned_about_new_version) {
1618 log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
1619 "recommended version%s, according to the directory "
1620 "authorities. Recommended versions are: %s",
1621 VERSION,
1622 status == VS_NEW_IN_SERIES ? " in its series" : "",
1623 recommended);
1624 have_warned_about_new_version = 1;
1625 control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
1626 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1627 VERSION, "NEW", recommended);
1629 } else {
1630 log_warn(LD_GENERAL, "Please upgrade! "
1631 "This version of Tor (%s) is %s, according to the directory "
1632 "authorities. Recommended versions are: %s",
1633 VERSION,
1634 status == VS_OLD ? "obsolete" : "not recommended",
1635 recommended);
1636 have_warned_about_old_version = 1;
1637 control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
1638 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1639 VERSION, status == VS_OLD ? "OBSOLETE" : "UNRECOMMENDED",
1640 recommended);
1645 /** Update v2_download_status_map to contain an entry for every router
1646 * descriptor listed in the v2 networkstatuses. */
1647 static void
1648 download_status_map_update_from_v2_networkstatus(void)
1650 digestmap_t *dl_status;
1651 if (!networkstatus_v2_list)
1652 return;
1653 if (!v2_download_status_map)
1654 v2_download_status_map = digestmap_new();
1656 dl_status = digestmap_new();
1657 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
1659 SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
1661 const char *d = rs->descriptor_digest;
1662 download_status_t *s;
1663 if (digestmap_get(dl_status, d))
1664 continue;
1665 if (!(s = digestmap_remove(v2_download_status_map, d))) {
1666 s = tor_malloc_zero(sizeof(download_status_t));
1668 digestmap_set(dl_status, d, s);
1671 digestmap_free(v2_download_status_map, _tor_free);
1672 v2_download_status_map = dl_status;
1673 networkstatus_v2_list_has_changed = 0;
1676 /** Update our view of the list of named servers from the most recently
1677 * retrieved networkstatus consensus. */
1678 static void
1679 routerstatus_list_update_named_server_map(void)
1681 if (!current_consensus)
1682 return;
1684 if (named_server_map)
1685 strmap_free(named_server_map, _tor_free);
1686 named_server_map = strmap_new();
1687 if (unnamed_server_map)
1688 strmap_free(unnamed_server_map, NULL);
1689 unnamed_server_map = strmap_new();
1690 SMARTLIST_FOREACH(current_consensus->routerstatus_list, routerstatus_t *, rs,
1692 if (rs->is_named) {
1693 strmap_set_lc(named_server_map, rs->nickname,
1694 tor_memdup(rs->identity_digest, DIGEST_LEN));
1696 if (rs->is_unnamed) {
1697 strmap_set_lc(unnamed_server_map, rs->nickname, (void*)1);
1702 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
1703 * according to our current consensus networkstatus. May re-order
1704 * <b>routers</b>. */
1705 void
1706 routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
1707 int reset_failures)
1709 trusted_dir_server_t *ds;
1710 or_options_t *options = get_options();
1711 int authdir = authdir_mode_v2(options) || authdir_mode_v3(options);
1712 int namingdir = authdir && options->NamingAuthoritativeDir;
1713 networkstatus_t *ns = current_consensus;
1714 if (!ns || !smartlist_len(ns->routerstatus_list))
1715 return;
1716 if (!networkstatus_v2_list)
1717 networkstatus_v2_list = smartlist_create();
1719 routers_sort_by_identity(routers);
1721 SMARTLIST_FOREACH_JOIN(ns->routerstatus_list, routerstatus_t *, rs,
1722 routers, routerinfo_t *, router,
1723 memcmp(rs->identity_digest,
1724 router->cache_info.identity_digest, DIGEST_LEN),
1726 /* We have no routerstatus for this router. Clear flags and skip it. */
1727 if (!namingdir)
1728 router->is_named = 0;
1729 if (!authdir) {
1730 if (router->purpose == ROUTER_PURPOSE_GENERAL)
1731 router_clear_status_flags(router);
1733 }) {
1734 /* We have a routerstatus for this router. */
1735 const char *digest = router->cache_info.identity_digest;
1737 ds = router_get_trusteddirserver_by_digest(digest);
1739 if (!namingdir) {
1740 if (rs->is_named && !strcasecmp(router->nickname, rs->nickname))
1741 router->is_named = 1;
1742 else
1743 router->is_named = 0;
1745 /* Is it the same descriptor, or only the same identity? */
1746 if (!memcmp(router->cache_info.signed_descriptor_digest,
1747 rs->descriptor_digest, DIGEST_LEN)) {
1748 if (ns->valid_until > router->cache_info.last_listed_as_valid_until)
1749 router->cache_info.last_listed_as_valid_until = ns->valid_until;
1752 if (!authdir) {
1753 /* If we're not an authdir, believe others. */
1754 router->is_valid = rs->is_valid;
1755 router->is_running = rs->is_running;
1756 router->is_fast = rs->is_fast;
1757 router->is_stable = rs->is_stable;
1758 router->is_possible_guard = rs->is_possible_guard;
1759 router->is_exit = rs->is_exit;
1760 router->is_bad_directory = rs->is_bad_directory;
1761 router->is_bad_exit = rs->is_bad_exit;
1762 router->is_hs_dir = rs->is_hs_dir;
1764 if (router->is_running && ds) {
1765 download_status_reset(&ds->v2_ns_dl_status);
1767 if (reset_failures) {
1768 download_status_reset(&rs->dl_status);
1770 } SMARTLIST_FOREACH_JOIN_END(rs, router);
1772 /* Now update last_listed_as_valid_until from v2 networkstatuses. */
1773 /* XXXX If this is slow, we need to rethink the code. */
1774 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns, {
1775 time_t live_until = ns->published_on + V2_NETWORKSTATUS_ROUTER_LIFETIME;
1776 SMARTLIST_FOREACH_JOIN(ns->entries, routerstatus_t *, rs,
1777 routers, routerinfo_t *, ri,
1778 memcmp(rs->identity_digest,
1779 ri->cache_info.identity_digest, DIGEST_LEN),
1780 STMT_NIL) {
1781 if (!memcmp(ri->cache_info.signed_descriptor_digest,
1782 rs->descriptor_digest, DIGEST_LEN)) {
1783 if (live_until > ri->cache_info.last_listed_as_valid_until)
1784 ri->cache_info.last_listed_as_valid_until = live_until;
1786 } SMARTLIST_FOREACH_JOIN_END(rs, ri);
1789 router_dir_info_changed();
1792 /** Given a list of signed_descriptor_t, update their fields (mainly, when
1793 * they were last listed) from the most recent consensus. */
1794 void
1795 signed_descs_update_status_from_consensus_networkstatus(smartlist_t *descs)
1797 networkstatus_t *ns = current_consensus;
1798 if (!ns)
1799 return;
1801 if (!ns->desc_digest_map) {
1802 char dummy[DIGEST_LEN];
1803 /* instantiates the digest map. */
1804 memset(dummy, 0, sizeof(dummy));
1805 router_get_consensus_status_by_descriptor_digest(dummy);
1807 SMARTLIST_FOREACH(descs, signed_descriptor_t *, d,
1809 routerstatus_t *rs = digestmap_get(ns->desc_digest_map,
1810 d->signed_descriptor_digest);
1811 if (rs) {
1812 if (ns->valid_until > d->last_listed_as_valid_until)
1813 d->last_listed_as_valid_until = ns->valid_until;
1818 /** Generate networkstatus lines for a single routerstatus_t object, and
1819 * return the result in a newly allocated string. Used only by controller
1820 * interface (for now.) */
1821 char *
1822 networkstatus_getinfo_helper_single(routerstatus_t *rs)
1824 char buf[RS_ENTRY_LEN+1];
1825 routerstatus_format_entry(buf, sizeof(buf), rs, NULL, 0, 1);
1826 return tor_strdup(buf);
1829 /** Alloc and return a string describing routerstatuses for the most
1830 * recent info of each router we know about that is of purpose
1831 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
1833 * Right now this function is oriented toward listing bridges (you
1834 * shouldn't use this for general-purpose routers, since those
1835 * should be listed from the consensus, not from the routers list). */
1836 char *
1837 networkstatus_getinfo_by_purpose(const char *purpose_string, time_t now)
1839 time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
1840 char *answer;
1841 routerlist_t *rl = router_get_routerlist();
1842 smartlist_t *statuses;
1843 uint8_t purpose = router_purpose_from_string(purpose_string);
1844 routerstatus_t rs;
1845 int bridge_auth = authdir_mode_bridge(get_options());
1847 if (purpose == ROUTER_PURPOSE_UNKNOWN) {
1848 log_info(LD_DIR, "Unrecognized purpose '%s' when listing router statuses.",
1849 purpose_string);
1850 return NULL;
1853 statuses = smartlist_create();
1854 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
1855 if (ri->cache_info.published_on < cutoff)
1856 continue;
1857 if (ri->purpose != purpose)
1858 continue;
1859 if (bridge_auth && ri->purpose == ROUTER_PURPOSE_BRIDGE)
1860 dirserv_set_router_is_running(ri, now);
1861 /* then generate and write out status lines for each of them */
1862 set_routerstatus_from_routerinfo(&rs, ri, now, 0, 0, 0, 0);
1863 smartlist_add(statuses, networkstatus_getinfo_helper_single(&rs));
1866 answer = smartlist_join_strings(statuses, "", 0, NULL);
1867 SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
1868 smartlist_free(statuses);
1869 return answer;
1872 /** Write out router status entries for all our bridge descriptors. */
1873 void
1874 networkstatus_dump_bridge_status_to_file(time_t now)
1876 char *status = networkstatus_getinfo_by_purpose("bridge", now);
1877 or_options_t *options = get_options();
1878 size_t len = strlen(options->DataDirectory) + 32;
1879 char *fname = tor_malloc(len);
1880 tor_snprintf(fname, len, "%s"PATH_SEPARATOR"networkstatus-bridges",
1881 options->DataDirectory);
1882 write_str_to_file(fname,status,0);
1883 tor_free(fname);
1884 tor_free(status);
1887 /** If <b>question</b> is a string beginning with "ns/" in a format the
1888 * control interface expects for a GETINFO question, set *<b>answer</b> to a
1889 * newly-allocated string containing networkstatus lines for the appropriate
1890 * ORs. Return 0 on success, -1 on unrecognized question format. */
1892 getinfo_helper_networkstatus(control_connection_t *conn,
1893 const char *question, char **answer)
1895 routerstatus_t *status;
1896 (void) conn;
1898 if (!current_consensus) {
1899 *answer = tor_strdup("");
1900 return 0;
1903 if (!strcmp(question, "ns/all")) {
1904 smartlist_t *statuses = smartlist_create();
1905 SMARTLIST_FOREACH(current_consensus->routerstatus_list,
1906 routerstatus_t *, rs,
1908 smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
1910 *answer = smartlist_join_strings(statuses, "", 0, NULL);
1911 SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
1912 smartlist_free(statuses);
1913 return 0;
1914 } else if (!strcmpstart(question, "ns/id/")) {
1915 char d[DIGEST_LEN];
1917 if (base16_decode(d, DIGEST_LEN, question+6, strlen(question+6)))
1918 return -1;
1919 status = router_get_consensus_status_by_id(d);
1920 } else if (!strcmpstart(question, "ns/name/")) {
1921 status = router_get_consensus_status_by_nickname(question+8, 0);
1922 } else if (!strcmpstart(question, "ns/purpose/")) {
1923 *answer = networkstatus_getinfo_by_purpose(question+11, time(NULL));
1924 return *answer ? 0 : -1;
1925 } else {
1926 return -1;
1929 if (status)
1930 *answer = networkstatus_getinfo_helper_single(status);
1931 return 0;
1934 /** Free all storage held locally in this module. */
1935 void
1936 networkstatus_free_all(void)
1938 if (networkstatus_v2_list) {
1939 SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
1940 networkstatus_v2_free(ns));
1941 smartlist_free(networkstatus_v2_list);
1942 networkstatus_v2_list = NULL;
1944 if (v2_download_status_map) {
1945 digestmap_free(v2_download_status_map, _tor_free);
1946 v2_download_status_map = NULL;
1948 if (current_consensus) {
1949 networkstatus_vote_free(current_consensus);
1950 current_consensus = NULL;
1952 if (consensus_waiting_for_certs) {
1953 networkstatus_vote_free(consensus_waiting_for_certs);
1954 consensus_waiting_for_certs = NULL;
1956 tor_free(consensus_waiting_for_certs_body);
1957 if (named_server_map) {
1958 strmap_free(named_server_map, _tor_free);
1960 if (unnamed_server_map) {
1961 strmap_free(unnamed_server_map, NULL);