Merge branch 'maint-0.2.5' into maint-0.2.6
[tor.git] / src / or / networkstatus.c
blobda110fdff68950b2676b6c287941e108b6237268
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-2015, 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 #define NETWORKSTATUS_PRIVATE
14 #include "or.h"
15 #include "channel.h"
16 #include "circuitmux.h"
17 #include "circuitmux_ewma.h"
18 #include "circuitstats.h"
19 #include "config.h"
20 #include "connection.h"
21 #include "connection_or.h"
22 #include "control.h"
23 #include "directory.h"
24 #include "dirserv.h"
25 #include "dirvote.h"
26 #include "entrynodes.h"
27 #include "main.h"
28 #include "microdesc.h"
29 #include "networkstatus.h"
30 #include "nodelist.h"
31 #include "relay.h"
32 #include "router.h"
33 #include "routerlist.h"
34 #include "routerparse.h"
35 #include "transports.h"
37 /** Map from lowercase nickname to identity digest of named server, if any. */
38 static strmap_t *named_server_map = NULL;
39 /** Map from lowercase nickname to (void*)1 for all names that are listed
40 * as unnamed for some server in the consensus. */
41 static strmap_t *unnamed_server_map = NULL;
43 /** Most recently received and validated v3 consensus network status,
44 * of whichever type we are using for our own circuits. This will be the same
45 * as one of current_ns_consensus or current_md_consensus.
47 #define current_consensus \
48 (we_use_microdescriptors_for_circuits(get_options()) ? \
49 current_md_consensus : current_ns_consensus)
51 /** Most recently received and validated v3 "ns"-flavored consensus network
52 * status. */
53 static networkstatus_t *current_ns_consensus = NULL;
55 /** Most recently received and validated v3 "microdec"-flavored consensus
56 * network status. */
57 static networkstatus_t *current_md_consensus = NULL;
59 /** A v3 consensus networkstatus that we've received, but which we don't
60 * have enough certificates to be happy about. */
61 typedef struct consensus_waiting_for_certs_t {
62 /** The consensus itself. */
63 networkstatus_t *consensus;
64 /** The encoded version of the consensus, nul-terminated. */
65 char *body;
66 /** When did we set the current value of consensus_waiting_for_certs? If
67 * this is too recent, we shouldn't try to fetch a new consensus for a
68 * little while, to give ourselves time to get certificates for this one. */
69 time_t set_at;
70 /** Set to 1 if we've been holding on to it for so long we should maybe
71 * treat it as being bad. */
72 int dl_failed;
73 } consensus_waiting_for_certs_t;
75 /** An array, for each flavor of consensus we might want, of consensuses that
76 * we have downloaded, but which we cannot verify due to having insufficient
77 * authority certificates. */
78 static consensus_waiting_for_certs_t
79 consensus_waiting_for_certs[N_CONSENSUS_FLAVORS];
81 /** A time before which we shouldn't try to replace the current consensus:
82 * this will be at some point after the next consensus becomes valid, but
83 * before the current consensus becomes invalid. */
84 static time_t time_to_download_next_consensus[N_CONSENSUS_FLAVORS];
85 /** Download status for the current consensus networkstatus. */
86 static download_status_t consensus_dl_status[N_CONSENSUS_FLAVORS] =
88 { 0, 0, DL_SCHED_CONSENSUS },
89 { 0, 0, DL_SCHED_CONSENSUS },
92 /** True iff we have logged a warning about this OR's version being older than
93 * listed by the authorities. */
94 static int have_warned_about_old_version = 0;
95 /** True iff we have logged a warning about this OR's version being newer than
96 * listed by the authorities. */
97 static int have_warned_about_new_version = 0;
99 static void routerstatus_list_update_named_server_map(void);
101 /** Forget that we've warned about anything networkstatus-related, so we will
102 * give fresh warnings if the same behavior happens again. */
103 void
104 networkstatus_reset_warnings(void)
106 if (current_consensus) {
107 SMARTLIST_FOREACH(nodelist_get_list(), node_t *, node,
108 node->name_lookup_warned = 0);
111 have_warned_about_old_version = 0;
112 have_warned_about_new_version = 0;
115 /** Reset the descriptor download failure count on all networkstatus docs, so
116 * that we can retry any long-failed documents immediately.
118 void
119 networkstatus_reset_download_failures(void)
121 int i;
123 for (i=0; i < N_CONSENSUS_FLAVORS; ++i)
124 download_status_reset(&consensus_dl_status[i]);
127 /** Read every cached v3 consensus networkstatus from the disk. */
129 router_reload_consensus_networkstatus(void)
131 char *filename;
132 char *s;
133 const unsigned int flags = NSSET_FROM_CACHE | NSSET_DONT_DOWNLOAD_CERTS;
134 int flav;
136 /* FFFF Suppress warnings if cached consensus is bad? */
137 for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
138 char buf[128];
139 const char *flavor = networkstatus_get_flavor_name(flav);
140 if (flav == FLAV_NS) {
141 filename = get_datadir_fname("cached-consensus");
142 } else {
143 tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
144 filename = get_datadir_fname(buf);
146 s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
147 if (s) {
148 if (networkstatus_set_current_consensus(s, flavor, flags) < -1) {
149 log_warn(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
150 flavor, filename);
152 tor_free(s);
154 tor_free(filename);
156 if (flav == FLAV_NS) {
157 filename = get_datadir_fname("unverified-consensus");
158 } else {
159 tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
160 filename = get_datadir_fname(buf);
163 s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
164 if (s) {
165 if (networkstatus_set_current_consensus(s, flavor,
166 flags|NSSET_WAS_WAITING_FOR_CERTS)) {
167 log_info(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
168 flavor, filename);
170 tor_free(s);
172 tor_free(filename);
175 if (!current_consensus) {
176 if (!named_server_map)
177 named_server_map = strmap_new();
178 if (!unnamed_server_map)
179 unnamed_server_map = strmap_new();
182 update_certificate_downloads(time(NULL));
184 routers_update_all_from_networkstatus(time(NULL), 3);
185 update_microdescs_from_networkstatus(time(NULL));
187 return 0;
190 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
191 STATIC void
192 vote_routerstatus_free(vote_routerstatus_t *rs)
194 vote_microdesc_hash_t *h, *next;
195 if (!rs)
196 return;
197 tor_free(rs->version);
198 tor_free(rs->status.exitsummary);
199 for (h = rs->microdesc; h; h = next) {
200 tor_free(h->microdesc_hash_line);
201 next = h->next;
202 tor_free(h);
204 tor_free(rs);
207 /** Free all storage held by the routerstatus object <b>rs</b>. */
208 void
209 routerstatus_free(routerstatus_t *rs)
211 if (!rs)
212 return;
213 tor_free(rs->exitsummary);
214 tor_free(rs);
217 /** Free all storage held in <b>sig</b> */
218 void
219 document_signature_free(document_signature_t *sig)
221 tor_free(sig->signature);
222 tor_free(sig);
225 /** Return a newly allocated copy of <b>sig</b> */
226 document_signature_t *
227 document_signature_dup(const document_signature_t *sig)
229 document_signature_t *r = tor_memdup(sig, sizeof(document_signature_t));
230 if (r->signature)
231 r->signature = tor_memdup(sig->signature, sig->signature_len);
232 return r;
235 /** Free all storage held in <b>ns</b>. */
236 void
237 networkstatus_vote_free(networkstatus_t *ns)
239 if (!ns)
240 return;
242 tor_free(ns->client_versions);
243 tor_free(ns->server_versions);
244 if (ns->known_flags) {
245 SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
246 smartlist_free(ns->known_flags);
248 if (ns->weight_params) {
249 SMARTLIST_FOREACH(ns->weight_params, char *, c, tor_free(c));
250 smartlist_free(ns->weight_params);
252 if (ns->net_params) {
253 SMARTLIST_FOREACH(ns->net_params, char *, c, tor_free(c));
254 smartlist_free(ns->net_params);
256 if (ns->supported_methods) {
257 SMARTLIST_FOREACH(ns->supported_methods, char *, c, tor_free(c));
258 smartlist_free(ns->supported_methods);
260 if (ns->package_lines) {
261 SMARTLIST_FOREACH(ns->package_lines, char *, c, tor_free(c));
262 smartlist_free(ns->package_lines);
264 if (ns->voters) {
265 SMARTLIST_FOREACH_BEGIN(ns->voters, networkstatus_voter_info_t *, voter) {
266 tor_free(voter->nickname);
267 tor_free(voter->address);
268 tor_free(voter->contact);
269 if (voter->sigs) {
270 SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
271 document_signature_free(sig));
272 smartlist_free(voter->sigs);
274 tor_free(voter);
275 } SMARTLIST_FOREACH_END(voter);
276 smartlist_free(ns->voters);
278 authority_cert_free(ns->cert);
280 if (ns->routerstatus_list) {
281 if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
282 SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
283 vote_routerstatus_free(rs));
284 } else {
285 SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
286 routerstatus_free(rs));
289 smartlist_free(ns->routerstatus_list);
292 digestmap_free(ns->desc_digest_map, NULL);
294 memwipe(ns, 11, sizeof(*ns));
295 tor_free(ns);
298 /** Return the voter info from <b>vote</b> for the voter whose identity digest
299 * is <b>identity</b>, or NULL if no such voter is associated with
300 * <b>vote</b>. */
301 networkstatus_voter_info_t *
302 networkstatus_get_voter_by_id(networkstatus_t *vote,
303 const char *identity)
305 if (!vote || !vote->voters)
306 return NULL;
307 SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
308 if (fast_memeq(voter->identity_digest, identity, DIGEST_LEN))
309 return voter);
310 return NULL;
313 /** Check whether the signature <b>sig</b> is correctly signed with the
314 * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
315 * signing key; otherwise set the good_signature or bad_signature flag on
316 * <b>voter</b>, and return 0. */
318 networkstatus_check_document_signature(const networkstatus_t *consensus,
319 document_signature_t *sig,
320 const authority_cert_t *cert)
322 char key_digest[DIGEST_LEN];
323 const int dlen = sig->alg == DIGEST_SHA1 ? DIGEST_LEN : DIGEST256_LEN;
324 char *signed_digest;
325 size_t signed_digest_len;
327 if (crypto_pk_get_digest(cert->signing_key, key_digest)<0)
328 return -1;
329 if (tor_memneq(sig->signing_key_digest, key_digest, DIGEST_LEN) ||
330 tor_memneq(sig->identity_digest, cert->cache_info.identity_digest,
331 DIGEST_LEN))
332 return -1;
334 if (authority_cert_is_blacklisted(cert)) {
335 /* We implement blacklisting for authority signing keys by treating
336 * all their signatures as always bad. That way we don't get into
337 * crazy loops of dropping and re-fetching signatures. */
338 log_warn(LD_DIR, "Ignoring a consensus signature made with deprecated"
339 " signing key %s",
340 hex_str(cert->signing_key_digest, DIGEST_LEN));
341 sig->bad_signature = 1;
342 return 0;
345 signed_digest_len = crypto_pk_keysize(cert->signing_key);
346 signed_digest = tor_malloc(signed_digest_len);
347 if (crypto_pk_public_checksig(cert->signing_key,
348 signed_digest,
349 signed_digest_len,
350 sig->signature,
351 sig->signature_len) < dlen ||
352 tor_memneq(signed_digest, consensus->digests.d[sig->alg], dlen)) {
353 log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
354 sig->bad_signature = 1;
355 } else {
356 sig->good_signature = 1;
358 tor_free(signed_digest);
359 return 0;
362 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
363 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
364 * signature from every recognized authority on it, 0 if there are
365 * enough good signatures from recognized authorities on it, -1 if we might
366 * get enough good signatures by fetching missing certificates, and -2
367 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
368 * about every problem; if warn is at least 1, warn only if we can't get
369 * enough signatures; if warn is negative, log nothing at all. */
371 networkstatus_check_consensus_signature(networkstatus_t *consensus,
372 int warn)
374 int n_good = 0;
375 int n_missing_key = 0, n_dl_failed_key = 0;
376 int n_bad = 0;
377 int n_unknown = 0;
378 int n_no_signature = 0;
379 int n_v3_authorities = get_n_authorities(V3_DIRINFO);
380 int n_required = n_v3_authorities/2 + 1;
381 smartlist_t *list_good = smartlist_new();
382 smartlist_t *list_no_signature = smartlist_new();
383 smartlist_t *need_certs_from = smartlist_new();
384 smartlist_t *unrecognized = smartlist_new();
385 smartlist_t *missing_authorities = smartlist_new();
386 int severity;
387 time_t now = time(NULL);
389 tor_assert(consensus->type == NS_TYPE_CONSENSUS);
391 SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *,
392 voter) {
393 int good_here = 0;
394 int bad_here = 0;
395 int unknown_here = 0;
396 int missing_key_here = 0, dl_failed_key_here = 0;
397 SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
398 if (!sig->good_signature && !sig->bad_signature &&
399 sig->signature) {
400 /* we can try to check the signature. */
401 int is_v3_auth = trusteddirserver_get_by_v3_auth_digest(
402 sig->identity_digest) != NULL;
403 authority_cert_t *cert =
404 authority_cert_get_by_digests(sig->identity_digest,
405 sig->signing_key_digest);
406 tor_assert(tor_memeq(sig->identity_digest, voter->identity_digest,
407 DIGEST_LEN));
409 if (!is_v3_auth) {
410 smartlist_add(unrecognized, voter);
411 ++unknown_here;
412 continue;
413 } else if (!cert || cert->expires < now) {
414 smartlist_add(need_certs_from, voter);
415 ++missing_key_here;
416 if (authority_cert_dl_looks_uncertain(sig->identity_digest))
417 ++dl_failed_key_here;
418 continue;
420 if (networkstatus_check_document_signature(consensus, sig, cert) < 0) {
421 smartlist_add(need_certs_from, voter);
422 ++missing_key_here;
423 if (authority_cert_dl_looks_uncertain(sig->identity_digest))
424 ++dl_failed_key_here;
425 continue;
428 if (sig->good_signature)
429 ++good_here;
430 else if (sig->bad_signature)
431 ++bad_here;
432 } SMARTLIST_FOREACH_END(sig);
434 if (good_here) {
435 ++n_good;
436 smartlist_add(list_good, voter->nickname);
437 } else if (bad_here) {
438 ++n_bad;
439 } else if (missing_key_here) {
440 ++n_missing_key;
441 if (dl_failed_key_here)
442 ++n_dl_failed_key;
443 } else if (unknown_here) {
444 ++n_unknown;
445 } else {
446 ++n_no_signature;
447 smartlist_add(list_no_signature, voter->nickname);
449 } SMARTLIST_FOREACH_END(voter);
451 /* Now see whether we're missing any voters entirely. */
452 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
453 dir_server_t *, ds,
455 if ((ds->type & V3_DIRINFO) &&
456 !networkstatus_get_voter_by_id(consensus, ds->v3_identity_digest))
457 smartlist_add(missing_authorities, ds);
460 if (warn > 1 || (warn >= 0 &&
461 (n_good + n_missing_key - n_dl_failed_key < n_required))) {
462 severity = LOG_WARN;
463 } else {
464 severity = LOG_INFO;
467 if (warn >= 0) {
468 SMARTLIST_FOREACH(unrecognized, networkstatus_voter_info_t *, voter,
470 tor_log(severity, LD_DIR, "Consensus includes unrecognized authority "
471 "'%s' at %s:%d (contact %s; identity %s)",
472 voter->nickname, voter->address, (int)voter->dir_port,
473 voter->contact?voter->contact:"n/a",
474 hex_str(voter->identity_digest, DIGEST_LEN));
476 SMARTLIST_FOREACH(need_certs_from, networkstatus_voter_info_t *, voter,
478 tor_log(severity, LD_DIR, "Looks like we need to download a new "
479 "certificate from authority '%s' at %s:%d (contact %s; "
480 "identity %s)",
481 voter->nickname, voter->address, (int)voter->dir_port,
482 voter->contact?voter->contact:"n/a",
483 hex_str(voter->identity_digest, DIGEST_LEN));
485 SMARTLIST_FOREACH(missing_authorities, dir_server_t *, ds,
487 tor_log(severity, LD_DIR, "Consensus does not include configured "
488 "authority '%s' at %s:%d (identity %s)",
489 ds->nickname, ds->address, (int)ds->dir_port,
490 hex_str(ds->v3_identity_digest, DIGEST_LEN));
493 char *joined;
494 smartlist_t *sl = smartlist_new();
495 char *tmp = smartlist_join_strings(list_good, " ", 0, NULL);
496 smartlist_add_asprintf(sl,
497 "A consensus needs %d good signatures from recognized "
498 "authorities for us to accept it. This one has %d (%s).",
499 n_required, n_good, tmp);
500 tor_free(tmp);
501 if (n_no_signature) {
502 tmp = smartlist_join_strings(list_no_signature, " ", 0, NULL);
503 smartlist_add_asprintf(sl,
504 "%d (%s) of the authorities we know didn't sign it.",
505 n_no_signature, tmp);
506 tor_free(tmp);
508 if (n_unknown) {
509 smartlist_add_asprintf(sl,
510 "It has %d signatures from authorities we don't "
511 "recognize.", n_unknown);
513 if (n_bad) {
514 smartlist_add_asprintf(sl, "%d of the signatures on it didn't verify "
515 "correctly.", n_bad);
517 if (n_missing_key) {
518 smartlist_add_asprintf(sl,
519 "We were unable to check %d of the signatures, "
520 "because we were missing the keys.", n_missing_key);
522 joined = smartlist_join_strings(sl, " ", 0, NULL);
523 tor_log(severity, LD_DIR, "%s", joined);
524 tor_free(joined);
525 SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
526 smartlist_free(sl);
530 smartlist_free(list_good);
531 smartlist_free(list_no_signature);
532 smartlist_free(unrecognized);
533 smartlist_free(need_certs_from);
534 smartlist_free(missing_authorities);
536 if (n_good == n_v3_authorities)
537 return 1;
538 else if (n_good >= n_required)
539 return 0;
540 else if (n_good + n_missing_key >= n_required)
541 return -1;
542 else
543 return -2;
546 /** How far in the future do we allow a network-status to get before removing
547 * it? (seconds) */
548 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
550 /** Helper for bsearching a list of routerstatus_t pointers: compare a
551 * digest in the key to the identity digest of a routerstatus_t. */
553 compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
555 const char *key = _key;
556 const routerstatus_t *rs = *_member;
557 return tor_memcmp(key, rs->identity_digest, DIGEST_LEN);
560 /** Helper for bsearching a list of routerstatus_t pointers: compare a
561 * digest in the key to the identity digest of a routerstatus_t. */
563 compare_digest_to_vote_routerstatus_entry(const void *_key,
564 const void **_member)
566 const char *key = _key;
567 const vote_routerstatus_t *vrs = *_member;
568 return tor_memcmp(key, vrs->status.identity_digest, DIGEST_LEN);
571 /** As networkstatus_find_entry, but do not return a const pointer */
572 routerstatus_t *
573 networkstatus_vote_find_mutable_entry(networkstatus_t *ns, const char *digest)
575 return smartlist_bsearch(ns->routerstatus_list, digest,
576 compare_digest_to_routerstatus_entry);
579 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
580 * NULL if none was found. */
581 const routerstatus_t *
582 networkstatus_vote_find_entry(networkstatus_t *ns, const char *digest)
584 return networkstatus_vote_find_mutable_entry(ns, digest);
587 /*XXXX MOVE make this static once functions are moved into this file. */
588 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
589 * <b>digest</b>. Return value and set *<b>found_out</b> as for
590 * smartlist_bsearch_idx(). */
592 networkstatus_vote_find_entry_idx(networkstatus_t *ns,
593 const char *digest, int *found_out)
595 return smartlist_bsearch_idx(ns->routerstatus_list, digest,
596 compare_digest_to_routerstatus_entry,
597 found_out);
600 /** As router_get_consensus_status_by_descriptor_digest, but does not return
601 * a const pointer. */
602 MOCK_IMPL(routerstatus_t *,
603 router_get_mutable_consensus_status_by_descriptor_digest,(
604 networkstatus_t *consensus,
605 const char *digest))
607 if (!consensus)
608 consensus = current_consensus;
609 if (!consensus)
610 return NULL;
611 if (!consensus->desc_digest_map) {
612 digestmap_t *m = consensus->desc_digest_map = digestmap_new();
613 SMARTLIST_FOREACH(consensus->routerstatus_list,
614 routerstatus_t *, rs,
616 digestmap_set(m, rs->descriptor_digest, rs);
619 return digestmap_get(consensus->desc_digest_map, digest);
622 /** Return the consensus view of the status of the router whose current
623 * <i>descriptor</i> digest in <b>consensus</b> is <b>digest</b>, or NULL if
624 * no such router is known. */
625 const routerstatus_t *
626 router_get_consensus_status_by_descriptor_digest(networkstatus_t *consensus,
627 const char *digest)
629 return router_get_mutable_consensus_status_by_descriptor_digest(
630 consensus, digest);
633 /** Given the digest of a router descriptor, return its current download
634 * status, or NULL if the digest is unrecognized. */
635 MOCK_IMPL(download_status_t *,
636 router_get_dl_status_by_descriptor_digest,(const char *d))
638 routerstatus_t *rs;
639 if (!current_ns_consensus)
640 return NULL;
641 if ((rs = router_get_mutable_consensus_status_by_descriptor_digest(
642 current_ns_consensus, d)))
643 return &rs->dl_status;
645 return NULL;
648 /** As router_get_consensus_status_by_id, but do not return a const pointer */
649 routerstatus_t *
650 router_get_mutable_consensus_status_by_id(const char *digest)
652 if (!current_consensus)
653 return NULL;
654 return smartlist_bsearch(current_consensus->routerstatus_list, digest,
655 compare_digest_to_routerstatus_entry);
658 /** Return the consensus view of the status of the router whose identity
659 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
660 const routerstatus_t *
661 router_get_consensus_status_by_id(const char *digest)
663 return router_get_mutable_consensus_status_by_id(digest);
666 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
667 * the corresponding routerstatus_t, or NULL if none exists. Warn the
668 * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
669 * nickname, but the Named flag isn't set for that router. */
670 const routerstatus_t *
671 router_get_consensus_status_by_nickname(const char *nickname,
672 int warn_if_unnamed)
674 const node_t *node = node_get_by_nickname(nickname, warn_if_unnamed);
675 if (node)
676 return node->rs;
677 else
678 return NULL;
681 /** Return the identity digest that's mapped to officially by
682 * <b>nickname</b>. */
683 const char *
684 networkstatus_get_router_digest_by_nickname(const char *nickname)
686 if (!named_server_map)
687 return NULL;
688 return strmap_get_lc(named_server_map, nickname);
691 /** Return true iff <b>nickname</b> is disallowed from being the nickname
692 * of any server. */
694 networkstatus_nickname_is_unnamed(const char *nickname)
696 if (!unnamed_server_map)
697 return 0;
698 return strmap_get_lc(unnamed_server_map, nickname) != NULL;
701 /** How frequently do directory authorities re-download fresh networkstatus
702 * documents? */
703 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
705 /** How frequently do non-authority directory caches re-download fresh
706 * networkstatus documents? */
707 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
709 /** Return true iff, given the options listed in <b>options</b>, <b>flavor</b>
710 * is the flavor of a consensus networkstatus that we would like to fetch. */
711 static int
712 we_want_to_fetch_flavor(const or_options_t *options, int flavor)
714 if (flavor < 0 || flavor > N_CONSENSUS_FLAVORS) {
715 /* This flavor is crazy; we don't want it */
716 /*XXXX handle unrecognized flavors later */
717 return 0;
719 if (authdir_mode_v3(options) || directory_caches_dir_info(options)) {
720 /* We want to serve all flavors to others, regardless if we would use
721 * it ourselves. */
722 return 1;
724 if (options->FetchUselessDescriptors) {
725 /* In order to get all descriptors, we need to fetch all consensuses. */
726 return 1;
728 /* Otherwise, we want the flavor only if we want to use it to build
729 * circuits. */
730 return flavor == usable_consensus_flavor();
733 /** How long will we hang onto a possibly live consensus for which we're
734 * fetching certs before we check whether there is a better one? */
735 #define DELAY_WHILE_FETCHING_CERTS (20*60)
737 /** If we want to download a fresh consensus, launch a new download as
738 * appropriate. */
739 static void
740 update_consensus_networkstatus_downloads(time_t now)
742 int i;
743 const or_options_t *options = get_options();
745 for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
746 /* XXXX need some way to download unknown flavors if we are caching. */
747 const char *resource;
748 consensus_waiting_for_certs_t *waiting;
749 networkstatus_t *c;
751 if (! we_want_to_fetch_flavor(options, i))
752 continue;
754 c = networkstatus_get_latest_consensus_by_flavor(i);
755 if (! (c && c->valid_after <= now && now <= c->valid_until)) {
756 /* No live consensus? Get one now!*/
757 time_to_download_next_consensus[i] = now;
760 if (time_to_download_next_consensus[i] > now)
761 continue; /* Wait until the current consensus is older. */
763 resource = networkstatus_get_flavor_name(i);
765 /* Let's make sure we remembered to update consensus_dl_status */
766 tor_assert(consensus_dl_status[i].schedule == DL_SCHED_CONSENSUS);
768 if (!download_status_is_ready(&consensus_dl_status[i], now,
769 options->TestingConsensusMaxDownloadTries))
770 continue; /* We failed downloading a consensus too recently. */
771 if (connection_dir_get_by_purpose_and_resource(
772 DIR_PURPOSE_FETCH_CONSENSUS, resource))
773 continue; /* There's an in-progress download.*/
775 waiting = &consensus_waiting_for_certs[i];
776 if (waiting->consensus) {
777 /* XXXX make sure this doesn't delay sane downloads. */
778 if (waiting->set_at + DELAY_WHILE_FETCHING_CERTS > now) {
779 continue; /* We're still getting certs for this one. */
780 } else {
781 if (!waiting->dl_failed) {
782 download_status_failed(&consensus_dl_status[i], 0);
783 waiting->dl_failed=1;
788 log_info(LD_DIR, "Launching %s networkstatus consensus download.",
789 networkstatus_get_flavor_name(i));
791 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS,
792 ROUTER_PURPOSE_GENERAL, resource,
793 PDS_RETRY_IF_NO_SERVERS);
797 /** Called when an attempt to download a consensus fails: note that the
798 * failure occurred, and possibly retry. */
799 void
800 networkstatus_consensus_download_failed(int status_code, const char *flavname)
802 int flav = networkstatus_parse_flavor_name(flavname);
803 if (flav >= 0) {
804 tor_assert(flav < N_CONSENSUS_FLAVORS);
805 /* XXXX handle unrecognized flavors */
806 download_status_failed(&consensus_dl_status[flav], status_code);
807 /* Retry immediately, if appropriate. */
808 update_consensus_networkstatus_downloads(time(NULL));
812 /** How long do we (as a cache) wait after a consensus becomes non-fresh
813 * before trying to fetch another? */
814 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
816 /** Update the time at which we'll consider replacing the current
817 * consensus of flavor <b>flav</b> */
818 static void
819 update_consensus_networkstatus_fetch_time_impl(time_t now, int flav)
821 const or_options_t *options = get_options();
822 networkstatus_t *c = networkstatus_get_latest_consensus_by_flavor(flav);
823 const char *flavor = networkstatus_get_flavor_name(flav);
824 if (! we_want_to_fetch_flavor(get_options(), flav))
825 return;
827 if (c && c->valid_after <= now && now <= c->valid_until) {
828 long dl_interval;
829 long interval = c->fresh_until - c->valid_after;
830 long min_sec_before_caching = CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
831 time_t start;
833 if (min_sec_before_caching > interval/16) {
834 /* Usually we allow 2-minutes slop factor in case clocks get
835 desynchronized a little. If we're on a private network with
836 a crazy-fast voting interval, though, 2 minutes may be too
837 much. */
838 min_sec_before_caching = interval/16;
839 /* make sure we always delay by at least a second before caching */
840 if (min_sec_before_caching == 0) {
841 min_sec_before_caching = 1;
845 if (directory_fetches_dir_info_early(options)) {
846 /* We want to cache the next one at some point after this one
847 * is no longer fresh... */
848 start = (time_t)(c->fresh_until + min_sec_before_caching);
849 /* Some clients may need the consensus sooner than others. */
850 if (options->FetchDirInfoExtraEarly || authdir_mode_v3(options)) {
851 dl_interval = 60;
852 if (min_sec_before_caching + dl_interval > interval)
853 dl_interval = interval/2;
854 } else {
855 /* But only in the first half-interval after that. */
856 dl_interval = interval/2;
858 } else {
859 /* We're an ordinary client or a bridge. Give all the caches enough
860 * time to download the consensus. */
861 start = (time_t)(c->fresh_until + (interval*3)/4);
862 /* But download the next one well before this one is expired. */
863 dl_interval = ((c->valid_until - start) * 7 )/ 8;
865 /* If we're a bridge user, make use of the numbers we just computed
866 * to choose the rest of the interval *after* them. */
867 if (directory_fetches_dir_info_later(options)) {
868 /* Give all the *clients* enough time to download the consensus. */
869 start = (time_t)(start + dl_interval + min_sec_before_caching);
870 /* But try to get it before ours actually expires. */
871 dl_interval = (c->valid_until - start) - min_sec_before_caching;
874 /* catch low dl_interval in crazy-fast networks */
875 if (dl_interval < 1)
876 dl_interval = 1;
877 /* catch late start in crazy-fast networks */
878 if (start+dl_interval >= c->valid_until)
879 start = c->valid_until - dl_interval - 1;
880 log_debug(LD_DIR,
881 "fresh_until: %ld start: %ld "
882 "dl_interval: %ld valid_until: %ld ",
883 (long)c->fresh_until, (long)start, dl_interval,
884 (long)c->valid_until);
885 /* We must not try to replace c while it's still fresh: */
886 tor_assert(c->fresh_until < start);
887 /* We must download the next one before c is invalid: */
888 tor_assert(start+dl_interval < c->valid_until);
889 time_to_download_next_consensus[flav] =
890 start + crypto_rand_int((int)dl_interval);
892 char tbuf1[ISO_TIME_LEN+1];
893 char tbuf2[ISO_TIME_LEN+1];
894 char tbuf3[ISO_TIME_LEN+1];
895 format_local_iso_time(tbuf1, c->fresh_until);
896 format_local_iso_time(tbuf2, c->valid_until);
897 format_local_iso_time(tbuf3, time_to_download_next_consensus[flav]);
898 log_info(LD_DIR, "Live %s consensus %s the most recent until %s and "
899 "will expire at %s; fetching the next one at %s.",
900 flavor, (c->fresh_until > now) ? "will be" : "was",
901 tbuf1, tbuf2, tbuf3);
903 } else {
904 time_to_download_next_consensus[flav] = now;
905 log_info(LD_DIR, "No live %s consensus; we should fetch one immediately.",
906 flavor);
910 /** Update the time at which we'll consider replacing the current
911 * consensus of flavor 'flavor' */
912 void
913 update_consensus_networkstatus_fetch_time(time_t now)
915 int i;
916 for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
917 if (we_want_to_fetch_flavor(get_options(), i))
918 update_consensus_networkstatus_fetch_time_impl(now, i);
922 /** Return 1 if there's a reason we shouldn't try any directory
923 * fetches yet (e.g. we demand bridges and none are yet known).
924 * Else return 0.
926 * If we return 1 and <b>msg_out</b> is provided, set <b>msg_out</b>
927 * to an explanation of why directory fetches are delayed. (If we
928 * return 0, we set msg_out to NULL.)
931 should_delay_dir_fetches(const or_options_t *options, const char **msg_out)
933 if (msg_out) {
934 *msg_out = NULL;
937 if (options->DisableNetwork) {
938 if (msg_out) {
939 *msg_out = "DisableNetwork is set.";
941 log_info(LD_DIR, "Delaying dir fetches (DisableNetwork is set)");
942 return 1;
945 if (options->UseBridges) {
946 if (!any_bridge_descriptors_known()) {
947 if (msg_out) {
948 *msg_out = "No running bridges";
950 log_info(LD_DIR, "Delaying dir fetches (no running bridges known)");
951 return 1;
954 if (pt_proxies_configuration_pending()) {
955 if (msg_out) {
956 *msg_out = "Pluggable transport proxies still configuring";
958 log_info(LD_DIR, "Delaying dir fetches (pt proxies still configuring)");
959 return 1;
963 return 0;
966 /** Launch requests for networkstatus documents and authority certificates as
967 * appropriate. */
968 void
969 update_networkstatus_downloads(time_t now)
971 const or_options_t *options = get_options();
972 if (should_delay_dir_fetches(options, NULL))
973 return;
974 update_consensus_networkstatus_downloads(now);
975 update_certificate_downloads(now);
978 /** Launch requests as appropriate for missing directory authority
979 * certificates. */
980 void
981 update_certificate_downloads(time_t now)
983 int i;
984 for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
985 if (consensus_waiting_for_certs[i].consensus)
986 authority_certs_fetch_missing(consensus_waiting_for_certs[i].consensus,
987 now);
990 if (current_ns_consensus)
991 authority_certs_fetch_missing(current_ns_consensus, now);
992 if (current_md_consensus)
993 authority_certs_fetch_missing(current_md_consensus, now);
996 /** Return 1 if we have a consensus but we don't have enough certificates
997 * to start using it yet. */
999 consensus_is_waiting_for_certs(void)
1001 return consensus_waiting_for_certs[usable_consensus_flavor()].consensus
1002 ? 1 : 0;
1005 /** Return the most recent consensus that we have downloaded, or NULL if we
1006 * don't have one. */
1007 networkstatus_t *
1008 networkstatus_get_latest_consensus(void)
1010 return current_consensus;
1013 /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
1014 * if we don't have one. */
1015 MOCK_IMPL(networkstatus_t *,
1016 networkstatus_get_latest_consensus_by_flavor,(consensus_flavor_t f))
1018 if (f == FLAV_NS)
1019 return current_ns_consensus;
1020 else if (f == FLAV_MICRODESC)
1021 return current_md_consensus;
1022 else {
1023 tor_assert(0);
1024 return NULL;
1028 /** Return the most recent consensus that we have downloaded, or NULL if it is
1029 * no longer live. */
1030 networkstatus_t *
1031 networkstatus_get_live_consensus(time_t now)
1033 if (current_consensus &&
1034 current_consensus->valid_after <= now &&
1035 now <= current_consensus->valid_until)
1036 return current_consensus;
1037 else
1038 return NULL;
1041 /* XXXX remove this in favor of get_live_consensus. But actually,
1042 * leave something like it for bridge users, who need to not totally
1043 * lose if they spend a while fetching a new consensus. */
1044 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1045 * consensuses. */
1046 networkstatus_t *
1047 networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
1049 #define REASONABLY_LIVE_TIME (24*60*60)
1050 networkstatus_t *consensus =
1051 networkstatus_get_latest_consensus_by_flavor(flavor);
1052 if (consensus &&
1053 consensus->valid_after <= now &&
1054 now <= consensus->valid_until+REASONABLY_LIVE_TIME)
1055 return consensus;
1056 else
1057 return NULL;
1060 /** Given two router status entries for the same router identity, return 1 if
1061 * if the contents have changed between them. Otherwise, return 0. */
1062 static int
1063 routerstatus_has_changed(const routerstatus_t *a, const routerstatus_t *b)
1065 tor_assert(tor_memeq(a->identity_digest, b->identity_digest, DIGEST_LEN));
1067 return strcmp(a->nickname, b->nickname) ||
1068 fast_memneq(a->descriptor_digest, b->descriptor_digest, DIGEST_LEN) ||
1069 a->addr != b->addr ||
1070 a->or_port != b->or_port ||
1071 a->dir_port != b->dir_port ||
1072 a->is_authority != b->is_authority ||
1073 a->is_exit != b->is_exit ||
1074 a->is_stable != b->is_stable ||
1075 a->is_fast != b->is_fast ||
1076 a->is_flagged_running != b->is_flagged_running ||
1077 a->is_named != b->is_named ||
1078 a->is_unnamed != b->is_unnamed ||
1079 a->is_valid != b->is_valid ||
1080 a->is_possible_guard != b->is_possible_guard ||
1081 a->is_bad_exit != b->is_bad_exit ||
1082 a->is_hs_dir != b->is_hs_dir ||
1083 a->version_known != b->version_known;
1086 /** Notify controllers of any router status entries that changed between
1087 * <b>old_c</b> and <b>new_c</b>. */
1088 static void
1089 notify_control_networkstatus_changed(const networkstatus_t *old_c,
1090 const networkstatus_t *new_c)
1092 smartlist_t *changed;
1093 if (old_c == new_c)
1094 return;
1096 /* tell the controller exactly which relays are still listed, as well
1097 * as what they're listed as */
1098 control_event_newconsensus(new_c);
1100 if (!control_event_is_interesting(EVENT_NS))
1101 return;
1103 if (!old_c) {
1104 control_event_networkstatus_changed(new_c->routerstatus_list);
1105 return;
1107 changed = smartlist_new();
1109 SMARTLIST_FOREACH_JOIN(
1110 old_c->routerstatus_list, const routerstatus_t *, rs_old,
1111 new_c->routerstatus_list, const routerstatus_t *, rs_new,
1112 tor_memcmp(rs_old->identity_digest,
1113 rs_new->identity_digest, DIGEST_LEN),
1114 smartlist_add(changed, (void*) rs_new)) {
1115 if (routerstatus_has_changed(rs_old, rs_new))
1116 smartlist_add(changed, (void*)rs_new);
1117 } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
1119 control_event_networkstatus_changed(changed);
1120 smartlist_free(changed);
1123 /** Copy all the ancillary information (like router download status and so on)
1124 * from <b>old_c</b> to <b>new_c</b>. */
1125 static void
1126 networkstatus_copy_old_consensus_info(networkstatus_t *new_c,
1127 const networkstatus_t *old_c)
1129 if (old_c == new_c)
1130 return;
1131 if (!old_c || !smartlist_len(old_c->routerstatus_list))
1132 return;
1134 SMARTLIST_FOREACH_JOIN(old_c->routerstatus_list, routerstatus_t *, rs_old,
1135 new_c->routerstatus_list, routerstatus_t *, rs_new,
1136 tor_memcmp(rs_old->identity_digest,
1137 rs_new->identity_digest, DIGEST_LEN),
1138 STMT_NIL) {
1139 /* Okay, so we're looking at the same identity. */
1140 rs_new->last_dir_503_at = rs_old->last_dir_503_at;
1142 if (tor_memeq(rs_old->descriptor_digest, rs_new->descriptor_digest,
1143 DIGEST256_LEN)) {
1144 /* And the same descriptor too! */
1145 memcpy(&rs_new->dl_status, &rs_old->dl_status,sizeof(download_status_t));
1147 } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
1150 /** Try to replace the current cached v3 networkstatus with the one in
1151 * <b>consensus</b>. If we don't have enough certificates to validate it,
1152 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1154 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1155 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1156 * already received, but we were waiting for certificates on it. If flags &
1157 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1158 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1159 * consensus, even if it comes from many days in the past.
1161 * Return 0 on success, <0 on failure. On failure, caller should increment
1162 * the failure count as appropriate.
1164 * We return -1 for mild failures that don't need to be reported to the
1165 * user, and -2 for more serious problems.
1168 networkstatus_set_current_consensus(const char *consensus,
1169 const char *flavor,
1170 unsigned flags)
1172 networkstatus_t *c=NULL;
1173 int r, result = -1;
1174 time_t now = time(NULL);
1175 const or_options_t *options = get_options();
1176 char *unverified_fname = NULL, *consensus_fname = NULL;
1177 int flav = networkstatus_parse_flavor_name(flavor);
1178 const unsigned from_cache = flags & NSSET_FROM_CACHE;
1179 const unsigned was_waiting_for_certs = flags & NSSET_WAS_WAITING_FOR_CERTS;
1180 const unsigned dl_certs = !(flags & NSSET_DONT_DOWNLOAD_CERTS);
1181 const unsigned accept_obsolete = flags & NSSET_ACCEPT_OBSOLETE;
1182 const unsigned require_flavor = flags & NSSET_REQUIRE_FLAVOR;
1183 const digests_t *current_digests = NULL;
1184 consensus_waiting_for_certs_t *waiting = NULL;
1185 time_t current_valid_after = 0;
1186 int free_consensus = 1; /* Free 'c' at the end of the function */
1187 int old_ewma_enabled;
1189 if (flav < 0) {
1190 /* XXXX we don't handle unrecognized flavors yet. */
1191 log_warn(LD_BUG, "Unrecognized consensus flavor %s", flavor);
1192 return -2;
1195 /* Make sure it's parseable. */
1196 c = networkstatus_parse_vote_from_string(consensus, NULL, NS_TYPE_CONSENSUS);
1197 if (!c) {
1198 log_warn(LD_DIR, "Unable to parse networkstatus consensus");
1199 result = -2;
1200 goto done;
1203 if ((int)c->flavor != flav) {
1204 /* This wasn't the flavor we thought we were getting. */
1205 if (require_flavor) {
1206 log_warn(LD_DIR, "Got consensus with unexpected flavor %s (wanted %s)",
1207 networkstatus_get_flavor_name(c->flavor), flavor);
1208 goto done;
1210 flav = c->flavor;
1211 flavor = networkstatus_get_flavor_name(flav);
1214 if (flav != usable_consensus_flavor() &&
1215 !directory_caches_dir_info(options)) {
1216 /* This consensus is totally boring to us: we won't use it, and we won't
1217 * serve it. Drop it. */
1218 goto done;
1221 if (from_cache && !accept_obsolete &&
1222 c->valid_until < now-OLD_ROUTER_DESC_MAX_AGE) {
1223 log_info(LD_DIR, "Loaded an expired consensus. Discarding.");
1224 goto done;
1227 if (!strcmp(flavor, "ns")) {
1228 consensus_fname = get_datadir_fname("cached-consensus");
1229 unverified_fname = get_datadir_fname("unverified-consensus");
1230 if (current_ns_consensus) {
1231 current_digests = &current_ns_consensus->digests;
1232 current_valid_after = current_ns_consensus->valid_after;
1234 } else if (!strcmp(flavor, "microdesc")) {
1235 consensus_fname = get_datadir_fname("cached-microdesc-consensus");
1236 unverified_fname = get_datadir_fname("unverified-microdesc-consensus");
1237 if (current_md_consensus) {
1238 current_digests = &current_md_consensus->digests;
1239 current_valid_after = current_md_consensus->valid_after;
1241 } else {
1242 cached_dir_t *cur;
1243 char buf[128];
1244 tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
1245 consensus_fname = get_datadir_fname(buf);
1246 tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
1247 unverified_fname = get_datadir_fname(buf);
1248 cur = dirserv_get_consensus(flavor);
1249 if (cur) {
1250 current_digests = &cur->digests;
1251 current_valid_after = cur->published;
1255 if (current_digests &&
1256 tor_memeq(&c->digests, current_digests, sizeof(c->digests))) {
1257 /* We already have this one. That's a failure. */
1258 log_info(LD_DIR, "Got a %s consensus we already have", flavor);
1259 goto done;
1262 if (current_valid_after && c->valid_after <= current_valid_after) {
1263 /* We have a newer one. There's no point in accepting this one,
1264 * even if it's great. */
1265 log_info(LD_DIR, "Got a %s consensus at least as old as the one we have",
1266 flavor);
1267 goto done;
1270 /* Make sure it's signed enough. */
1271 if ((r=networkstatus_check_consensus_signature(c, 1))<0) {
1272 if (r == -1) {
1273 /* Okay, so it _might_ be signed enough if we get more certificates. */
1274 if (!was_waiting_for_certs) {
1275 log_info(LD_DIR,
1276 "Not enough certificates to check networkstatus consensus");
1278 if (!current_valid_after ||
1279 c->valid_after > current_valid_after) {
1280 waiting = &consensus_waiting_for_certs[flav];
1281 networkstatus_vote_free(waiting->consensus);
1282 tor_free(waiting->body);
1283 waiting->consensus = c;
1284 free_consensus = 0;
1285 waiting->body = tor_strdup(consensus);
1286 waiting->set_at = now;
1287 waiting->dl_failed = 0;
1288 if (!from_cache) {
1289 write_str_to_file(unverified_fname, consensus, 0);
1291 if (dl_certs)
1292 authority_certs_fetch_missing(c, now);
1293 /* This case is not a success or a failure until we get the certs
1294 * or fail to get the certs. */
1295 result = 0;
1296 } else {
1297 /* Even if we had enough signatures, we'd never use this as the
1298 * latest consensus. */
1299 if (was_waiting_for_certs && from_cache)
1300 if (unlink(unverified_fname) != 0) {
1301 log_warn(LD_FS,
1302 "Failed to unlink %s: %s",
1303 unverified_fname, strerror(errno));
1306 goto done;
1307 } else {
1308 /* This can never be signed enough: Kill it. */
1309 if (!was_waiting_for_certs) {
1310 log_warn(LD_DIR, "Not enough good signatures on networkstatus "
1311 "consensus");
1312 result = -2;
1314 if (was_waiting_for_certs && (r < -1) && from_cache) {
1315 if (unlink(unverified_fname) != 0) {
1316 log_warn(LD_FS,
1317 "Failed to unlink %s: %s",
1318 unverified_fname, strerror(errno));
1321 goto done;
1325 if (!from_cache && flav == usable_consensus_flavor())
1326 control_event_client_status(LOG_NOTICE, "CONSENSUS_ARRIVED");
1328 /* Are we missing any certificates at all? */
1329 if (r != 1 && dl_certs)
1330 authority_certs_fetch_missing(c, now);
1332 if (flav == usable_consensus_flavor()) {
1333 notify_control_networkstatus_changed(current_consensus, c);
1335 if (flav == FLAV_NS) {
1336 if (current_ns_consensus) {
1337 networkstatus_copy_old_consensus_info(c, current_ns_consensus);
1338 networkstatus_vote_free(current_ns_consensus);
1339 /* Defensive programming : we should set current_consensus very soon,
1340 * but we're about to call some stuff in the meantime, and leaving this
1341 * dangling pointer around has proven to be trouble. */
1342 current_ns_consensus = NULL;
1344 current_ns_consensus = c;
1345 free_consensus = 0; /* avoid free */
1346 } else if (flav == FLAV_MICRODESC) {
1347 if (current_md_consensus) {
1348 networkstatus_copy_old_consensus_info(c, current_md_consensus);
1349 networkstatus_vote_free(current_md_consensus);
1350 /* more defensive programming */
1351 current_md_consensus = NULL;
1353 current_md_consensus = c;
1354 free_consensus = 0; /* avoid free */
1357 waiting = &consensus_waiting_for_certs[flav];
1358 if (waiting->consensus &&
1359 waiting->consensus->valid_after <= c->valid_after) {
1360 networkstatus_vote_free(waiting->consensus);
1361 waiting->consensus = NULL;
1362 if (consensus != waiting->body)
1363 tor_free(waiting->body);
1364 else
1365 waiting->body = NULL;
1366 waiting->set_at = 0;
1367 waiting->dl_failed = 0;
1368 if (unlink(unverified_fname) != 0) {
1369 log_warn(LD_FS,
1370 "Failed to unlink %s: %s",
1371 unverified_fname, strerror(errno));
1375 /* Reset the failure count only if this consensus is actually valid. */
1376 if (c->valid_after <= now && now <= c->valid_until) {
1377 download_status_reset(&consensus_dl_status[flav]);
1378 } else {
1379 if (!from_cache)
1380 download_status_failed(&consensus_dl_status[flav], 0);
1383 if (flav == usable_consensus_flavor()) {
1384 /* XXXXNM Microdescs: needs a non-ns variant. ???? NM*/
1385 update_consensus_networkstatus_fetch_time(now);
1387 nodelist_set_consensus(current_consensus);
1389 dirvote_recalculate_timing(options, now);
1390 routerstatus_list_update_named_server_map();
1392 /* Update ewma and adjust policy if needed; first cache the old value */
1393 old_ewma_enabled = cell_ewma_enabled();
1394 /* Change the cell EWMA settings */
1395 cell_ewma_set_scale_factor(options, networkstatus_get_latest_consensus());
1396 /* If we just enabled ewma, set the cmux policy on all active channels */
1397 if (cell_ewma_enabled() && !old_ewma_enabled) {
1398 channel_set_cmux_policy_everywhere(&ewma_policy);
1399 } else if (!cell_ewma_enabled() && old_ewma_enabled) {
1400 /* Turn it off everywhere */
1401 channel_set_cmux_policy_everywhere(NULL);
1404 /* XXXX024 this call might be unnecessary here: can changing the
1405 * current consensus really alter our view of any OR's rate limits? */
1406 connection_or_update_token_buckets(get_connection_array(), options);
1408 circuit_build_times_new_consensus_params(get_circuit_build_times_mutable(),
1409 current_consensus);
1412 if (directory_caches_dir_info(options)) {
1413 dirserv_set_cached_consensus_networkstatus(consensus,
1414 flavor,
1415 &c->digests,
1416 c->valid_after);
1419 if (!from_cache) {
1420 write_str_to_file(consensus_fname, consensus, 0);
1423 /** If a consensus appears more than this many seconds before its declared
1424 * valid-after time, declare that our clock is skewed. */
1425 #define EARLY_CONSENSUS_NOTICE_SKEW 60
1427 if (now < c->valid_after - EARLY_CONSENSUS_NOTICE_SKEW) {
1428 char tbuf[ISO_TIME_LEN+1];
1429 char dbuf[64];
1430 long delta = now - c->valid_after;
1431 format_iso_time(tbuf, c->valid_after);
1432 format_time_interval(dbuf, sizeof(dbuf), delta);
1433 log_warn(LD_GENERAL, "Our clock is %s behind the time published in the "
1434 "consensus network status document (%s UTC). Tor needs an "
1435 "accurate clock to work correctly. Please check your time and "
1436 "date settings!", dbuf, tbuf);
1437 control_event_general_status(LOG_WARN,
1438 "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta);
1441 router_dir_info_changed();
1443 result = 0;
1444 done:
1445 if (free_consensus)
1446 networkstatus_vote_free(c);
1447 tor_free(consensus_fname);
1448 tor_free(unverified_fname);
1449 return result;
1452 /** Called when we have gotten more certificates: see whether we can
1453 * now verify a pending consensus. */
1454 void
1455 networkstatus_note_certs_arrived(void)
1457 int i;
1458 for (i=0; i<N_CONSENSUS_FLAVORS; ++i) {
1459 consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
1460 if (!waiting->consensus)
1461 continue;
1462 if (networkstatus_check_consensus_signature(waiting->consensus, 0)>=0) {
1463 char *waiting_body = waiting->body;
1464 if (!networkstatus_set_current_consensus(
1465 waiting_body,
1466 networkstatus_get_flavor_name(i),
1467 NSSET_WAS_WAITING_FOR_CERTS)) {
1468 tor_free(waiting_body);
1474 /** If the network-status list has changed since the last time we called this
1475 * function, update the status of every routerinfo from the network-status
1476 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
1477 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
1479 void
1480 routers_update_all_from_networkstatus(time_t now, int dir_version)
1482 routerlist_t *rl = router_get_routerlist();
1483 networkstatus_t *consensus = networkstatus_get_reasonably_live_consensus(now,
1484 FLAV_NS);
1486 if (!consensus || dir_version < 3) /* nothing more we should do */
1487 return;
1489 /* calls router_dir_info_changed() when it's done -- more routers
1490 * might be up or down now, which might affect whether there's enough
1491 * directory info. */
1492 routers_update_status_from_consensus_networkstatus(rl->routers, 0);
1494 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
1495 ri->cache_info.routerlist_index = ri_sl_idx);
1496 if (rl->old_routers)
1497 signed_descs_update_status_from_consensus_networkstatus(rl->old_routers);
1499 if (!have_warned_about_old_version) {
1500 int is_server = server_mode(get_options());
1501 version_status_t status;
1502 const char *recommended = is_server ?
1503 consensus->server_versions : consensus->client_versions;
1504 status = tor_version_is_obsolete(VERSION, recommended);
1506 if (status == VS_RECOMMENDED) {
1507 log_info(LD_GENERAL, "The directory authorities say my version is ok.");
1508 } else if (status == VS_EMPTY) {
1509 log_info(LD_GENERAL,
1510 "The directory authorities don't recommend any versions.");
1511 } else if (status == VS_NEW || status == VS_NEW_IN_SERIES) {
1512 if (!have_warned_about_new_version) {
1513 log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
1514 "recommended version%s, according to the directory "
1515 "authorities. Recommended versions are: %s",
1516 VERSION,
1517 status == VS_NEW_IN_SERIES ? " in its series" : "",
1518 recommended);
1519 have_warned_about_new_version = 1;
1520 control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
1521 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1522 VERSION, "NEW", recommended);
1524 } else {
1525 log_warn(LD_GENERAL, "Please upgrade! "
1526 "This version of Tor (%s) is %s, according to the directory "
1527 "authorities. Recommended versions are: %s",
1528 VERSION,
1529 status == VS_OLD ? "obsolete" : "not recommended",
1530 recommended);
1531 have_warned_about_old_version = 1;
1532 control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
1533 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
1534 VERSION, status == VS_OLD ? "OBSOLETE" : "UNRECOMMENDED",
1535 recommended);
1540 /** Update our view of the list of named servers from the most recently
1541 * retrieved networkstatus consensus. */
1542 static void
1543 routerstatus_list_update_named_server_map(void)
1545 if (!current_consensus)
1546 return;
1548 strmap_free(named_server_map, tor_free_);
1549 named_server_map = strmap_new();
1550 strmap_free(unnamed_server_map, NULL);
1551 unnamed_server_map = strmap_new();
1552 SMARTLIST_FOREACH_BEGIN(current_consensus->routerstatus_list,
1553 const routerstatus_t *, rs) {
1554 if (rs->is_named) {
1555 strmap_set_lc(named_server_map, rs->nickname,
1556 tor_memdup(rs->identity_digest, DIGEST_LEN));
1558 if (rs->is_unnamed) {
1559 strmap_set_lc(unnamed_server_map, rs->nickname, (void*)1);
1561 } SMARTLIST_FOREACH_END(rs);
1564 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
1565 * according to our current consensus networkstatus. May re-order
1566 * <b>routers</b>. */
1567 void
1568 routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
1569 int reset_failures)
1571 const or_options_t *options = get_options();
1572 int authdir = authdir_mode_v3(options);
1573 networkstatus_t *ns = current_consensus;
1574 if (!ns || !smartlist_len(ns->routerstatus_list))
1575 return;
1577 routers_sort_by_identity(routers);
1579 SMARTLIST_FOREACH_JOIN(ns->routerstatus_list, routerstatus_t *, rs,
1580 routers, routerinfo_t *, router,
1581 tor_memcmp(rs->identity_digest,
1582 router->cache_info.identity_digest, DIGEST_LEN),
1584 }) {
1585 /* Is it the same descriptor, or only the same identity? */
1586 if (tor_memeq(router->cache_info.signed_descriptor_digest,
1587 rs->descriptor_digest, DIGEST_LEN)) {
1588 if (ns->valid_until > router->cache_info.last_listed_as_valid_until)
1589 router->cache_info.last_listed_as_valid_until = ns->valid_until;
1592 if (authdir) {
1593 /* If we _are_ an authority, we should check whether this router
1594 * is one that will cause us to need a reachability test. */
1595 routerinfo_t *old_router =
1596 router_get_mutable_by_digest(router->cache_info.identity_digest);
1597 if (old_router != router) {
1598 router->needs_retest_if_added =
1599 dirserv_should_launch_reachability_test(router, old_router);
1602 if (reset_failures) {
1603 download_status_reset(&rs->dl_status);
1605 } SMARTLIST_FOREACH_JOIN_END(rs, router);
1607 router_dir_info_changed();
1610 /** Given a list of signed_descriptor_t, update their fields (mainly, when
1611 * they were last listed) from the most recent consensus. */
1612 void
1613 signed_descs_update_status_from_consensus_networkstatus(smartlist_t *descs)
1615 networkstatus_t *ns = current_ns_consensus;
1616 if (!ns)
1617 return;
1619 if (!ns->desc_digest_map) {
1620 char dummy[DIGEST_LEN];
1621 /* instantiates the digest map. */
1622 memset(dummy, 0, sizeof(dummy));
1623 router_get_consensus_status_by_descriptor_digest(ns, dummy);
1625 SMARTLIST_FOREACH(descs, signed_descriptor_t *, d,
1627 const routerstatus_t *rs = digestmap_get(ns->desc_digest_map,
1628 d->signed_descriptor_digest);
1629 if (rs) {
1630 if (ns->valid_until > d->last_listed_as_valid_until)
1631 d->last_listed_as_valid_until = ns->valid_until;
1636 /** Generate networkstatus lines for a single routerstatus_t object, and
1637 * return the result in a newly allocated string. Used only by controller
1638 * interface (for now.) */
1639 char *
1640 networkstatus_getinfo_helper_single(const routerstatus_t *rs)
1642 return routerstatus_format_entry(rs, NULL, NS_CONTROL_PORT, NULL);
1645 /** Alloc and return a string describing routerstatuses for the most
1646 * recent info of each router we know about that is of purpose
1647 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
1649 * Right now this function is oriented toward listing bridges (you
1650 * shouldn't use this for general-purpose routers, since those
1651 * should be listed from the consensus, not from the routers list). */
1652 char *
1653 networkstatus_getinfo_by_purpose(const char *purpose_string, time_t now)
1655 time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
1656 char *answer;
1657 routerlist_t *rl = router_get_routerlist();
1658 smartlist_t *statuses;
1659 uint8_t purpose = router_purpose_from_string(purpose_string);
1660 routerstatus_t rs;
1661 int bridge_auth = authdir_mode_bridge(get_options());
1663 if (purpose == ROUTER_PURPOSE_UNKNOWN) {
1664 log_info(LD_DIR, "Unrecognized purpose '%s' when listing router statuses.",
1665 purpose_string);
1666 return NULL;
1669 statuses = smartlist_new();
1670 SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
1671 node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
1672 if (!node)
1673 continue;
1674 if (ri->cache_info.published_on < cutoff)
1675 continue;
1676 if (ri->purpose != purpose)
1677 continue;
1678 if (bridge_auth && ri->purpose == ROUTER_PURPOSE_BRIDGE)
1679 dirserv_set_router_is_running(ri, now);
1680 /* then generate and write out status lines for each of them */
1681 set_routerstatus_from_routerinfo(&rs, node, ri, now, 0, 0);
1682 smartlist_add(statuses, networkstatus_getinfo_helper_single(&rs));
1683 } SMARTLIST_FOREACH_END(ri);
1685 answer = smartlist_join_strings(statuses, "", 0, NULL);
1686 SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
1687 smartlist_free(statuses);
1688 return answer;
1691 /** Write out router status entries for all our bridge descriptors. */
1692 void
1693 networkstatus_dump_bridge_status_to_file(time_t now)
1695 char *status = networkstatus_getinfo_by_purpose("bridge", now);
1696 const or_options_t *options = get_options();
1697 char *fname = NULL;
1698 char *thresholds = NULL;
1699 char *published_thresholds_and_status = NULL;
1700 routerlist_t *rl = router_get_routerlist();
1701 char published[ISO_TIME_LEN+1];
1703 format_iso_time(published, now);
1704 dirserv_compute_bridge_flag_thresholds(rl);
1705 thresholds = dirserv_get_flag_thresholds_line();
1706 tor_asprintf(&published_thresholds_and_status,
1707 "published %s\nflag-thresholds %s\n%s",
1708 published, thresholds, status);
1709 tor_asprintf(&fname, "%s"PATH_SEPARATOR"networkstatus-bridges",
1710 options->DataDirectory);
1711 write_str_to_file(fname,published_thresholds_and_status,0);
1712 tor_free(thresholds);
1713 tor_free(published_thresholds_and_status);
1714 tor_free(fname);
1715 tor_free(status);
1718 /* DOCDOC get_net_param_from_list */
1719 static int32_t
1720 get_net_param_from_list(smartlist_t *net_params, const char *param_name,
1721 int32_t default_val, int32_t min_val, int32_t max_val)
1723 int32_t res = default_val;
1724 size_t name_len = strlen(param_name);
1726 tor_assert(max_val > min_val);
1727 tor_assert(min_val <= default_val);
1728 tor_assert(max_val >= default_val);
1730 SMARTLIST_FOREACH_BEGIN(net_params, const char *, p) {
1731 if (!strcmpstart(p, param_name) && p[name_len] == '=') {
1732 int ok=0;
1733 long v = tor_parse_long(p+name_len+1, 10, INT32_MIN,
1734 INT32_MAX, &ok, NULL);
1735 if (ok) {
1736 res = (int32_t) v;
1737 break;
1740 } SMARTLIST_FOREACH_END(p);
1742 if (res < min_val) {
1743 log_warn(LD_DIR, "Consensus parameter %s is too small. Got %d, raising to "
1744 "%d.", param_name, res, min_val);
1745 res = min_val;
1746 } else if (res > max_val) {
1747 log_warn(LD_DIR, "Consensus parameter %s is too large. Got %d, capping to "
1748 "%d.", param_name, res, max_val);
1749 res = max_val;
1752 return res;
1755 /** Return the value of a integer parameter from the networkstatus <b>ns</b>
1756 * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
1757 * latest consensus ourselves. Return <b>default_val</b> if no latest
1758 * consensus, or if it has no parameter called <b>param_name</b>.
1759 * Make sure the value parsed from the consensus is at least
1760 * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
1761 * if necessary. */
1762 int32_t
1763 networkstatus_get_param(const networkstatus_t *ns, const char *param_name,
1764 int32_t default_val, int32_t min_val, int32_t max_val)
1766 if (!ns) /* if they pass in null, go find it ourselves */
1767 ns = networkstatus_get_latest_consensus();
1769 if (!ns || !ns->net_params)
1770 return default_val;
1772 return get_net_param_from_list(ns->net_params, param_name,
1773 default_val, min_val, max_val);
1777 * Retrieve the consensus parameter that governs the
1778 * fixed-point precision of our network balancing 'bandwidth-weights'
1779 * (which are themselves integer consensus values). We divide them
1780 * by this value and ensure they never exceed this value.
1783 networkstatus_get_weight_scale_param(networkstatus_t *ns)
1785 return networkstatus_get_param(ns, "bwweightscale",
1786 BW_WEIGHT_SCALE,
1787 BW_MIN_WEIGHT_SCALE,
1788 BW_MAX_WEIGHT_SCALE);
1791 /** Return the value of a integer bw weight parameter from the networkstatus
1792 * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
1793 * loading the latest consensus ourselves. Return <b>default_val</b> if no
1794 * latest consensus, or if it has no parameter called <b>weight_name</b>. */
1795 int32_t
1796 networkstatus_get_bw_weight(networkstatus_t *ns, const char *weight_name,
1797 int32_t default_val)
1799 int32_t param;
1800 int max;
1801 if (!ns) /* if they pass in null, go find it ourselves */
1802 ns = networkstatus_get_latest_consensus();
1804 if (!ns || !ns->weight_params)
1805 return default_val;
1807 max = networkstatus_get_weight_scale_param(ns);
1808 param = get_net_param_from_list(ns->weight_params, weight_name,
1809 default_val, -1,
1810 BW_MAX_WEIGHT_SCALE);
1811 if (param > max) {
1812 log_warn(LD_DIR, "Value of consensus weight %s was too large, capping "
1813 "to %d", weight_name, max);
1814 param = max;
1816 return param;
1819 /** Return the name of the consensus flavor <b>flav</b> as used to identify
1820 * the flavor in directory documents. */
1821 const char *
1822 networkstatus_get_flavor_name(consensus_flavor_t flav)
1824 switch (flav) {
1825 case FLAV_NS:
1826 return "ns";
1827 case FLAV_MICRODESC:
1828 return "microdesc";
1829 default:
1830 tor_fragile_assert();
1831 return "??";
1835 /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
1836 * or -1 if the flavor is not recognized. */
1838 networkstatus_parse_flavor_name(const char *flavname)
1840 if (!strcmp(flavname, "ns"))
1841 return FLAV_NS;
1842 else if (!strcmp(flavname, "microdesc"))
1843 return FLAV_MICRODESC;
1844 else
1845 return -1;
1848 /** Return 0 if this routerstatus is obsolete, too new, isn't
1849 * running, or otherwise not a descriptor that we would make any
1850 * use of even if we had it. Else return 1. */
1852 client_would_use_router(const routerstatus_t *rs, time_t now,
1853 const or_options_t *options)
1855 if (!rs->is_flagged_running && !options->FetchUselessDescriptors) {
1856 /* If we had this router descriptor, we wouldn't even bother using it.
1857 * But, if we want to have a complete list, fetch it anyway. */
1858 return 0;
1860 if (rs->published_on + options->TestingEstimatedDescriptorPropagationTime
1861 > now) {
1862 /* Most caches probably don't have this descriptor yet. */
1863 return 0;
1865 if (rs->published_on + OLD_ROUTER_DESC_MAX_AGE < now) {
1866 /* We'd drop it immediately for being too old. */
1867 return 0;
1869 return 1;
1872 /** If <b>question</b> is a string beginning with "ns/" in a format the
1873 * control interface expects for a GETINFO question, set *<b>answer</b> to a
1874 * newly-allocated string containing networkstatus lines for the appropriate
1875 * ORs. Return 0 on success, -1 on unrecognized question format. */
1877 getinfo_helper_networkstatus(control_connection_t *conn,
1878 const char *question, char **answer,
1879 const char **errmsg)
1881 const routerstatus_t *status;
1882 (void) conn;
1884 if (!current_consensus) {
1885 *answer = tor_strdup("");
1886 return 0;
1889 if (!strcmp(question, "ns/all")) {
1890 smartlist_t *statuses = smartlist_new();
1891 SMARTLIST_FOREACH(current_consensus->routerstatus_list,
1892 const routerstatus_t *, rs,
1894 smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
1896 *answer = smartlist_join_strings(statuses, "", 0, NULL);
1897 SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
1898 smartlist_free(statuses);
1899 return 0;
1900 } else if (!strcmpstart(question, "ns/id/")) {
1901 char d[DIGEST_LEN];
1902 const char *q = question + 6;
1903 if (*q == '$')
1904 ++q;
1906 if (base16_decode(d, DIGEST_LEN, q, strlen(q))) {
1907 *errmsg = "Data not decodeable as hex";
1908 return -1;
1910 status = router_get_consensus_status_by_id(d);
1911 } else if (!strcmpstart(question, "ns/name/")) {
1912 status = router_get_consensus_status_by_nickname(question+8, 0);
1913 } else if (!strcmpstart(question, "ns/purpose/")) {
1914 *answer = networkstatus_getinfo_by_purpose(question+11, time(NULL));
1915 return *answer ? 0 : -1;
1916 } else if (!strcmp(question, "consensus/packages")) {
1917 const networkstatus_t *ns = networkstatus_get_latest_consensus();
1918 if (ns && ns->package_lines)
1919 *answer = smartlist_join_strings(ns->package_lines, "\n", 0, NULL);
1920 else
1921 *errmsg = "No consensus available";
1922 return *answer ? 0 : -1;
1923 } else if (!strcmp(question, "consensus/valid-after") ||
1924 !strcmp(question, "consensus/fresh-until") ||
1925 !strcmp(question, "consensus/valid-until")) {
1926 const networkstatus_t *ns = networkstatus_get_latest_consensus();
1927 if (ns) {
1928 time_t t;
1929 if (!strcmp(question, "consensus/valid-after"))
1930 t = ns->valid_after;
1931 else if (!strcmp(question, "consensus/fresh-until"))
1932 t = ns->fresh_until;
1933 else
1934 t = ns->valid_until;
1936 char tbuf[ISO_TIME_LEN+1];
1937 format_iso_time(tbuf, t);
1938 *answer = tor_strdup(tbuf);
1939 } else {
1940 *errmsg = "No consensus available";
1942 return *answer ? 0 : -1;
1943 } else {
1944 return 0;
1947 if (status)
1948 *answer = networkstatus_getinfo_helper_single(status);
1949 return 0;
1952 /** Free all storage held locally in this module. */
1953 void
1954 networkstatus_free_all(void)
1956 int i;
1957 networkstatus_vote_free(current_ns_consensus);
1958 networkstatus_vote_free(current_md_consensus);
1959 current_md_consensus = current_ns_consensus = NULL;
1961 for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
1962 consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
1963 if (waiting->consensus) {
1964 networkstatus_vote_free(waiting->consensus);
1965 waiting->consensus = NULL;
1967 tor_free(waiting->body);
1970 strmap_free(named_server_map, tor_free_);
1971 strmap_free(unnamed_server_map, NULL);