In routerlist_assert_ok(), check r2 before taking &(r2->cache_info)
[tor.git] / src / or / nodelist.c
blob7b1f338bd475e394fcfcc8ba4b8647d5ffd10820
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-2013, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 #include "or.h"
8 #include "address.h"
9 #include "config.h"
10 #include "control.h"
11 #include "dirserv.h"
12 #include "geoip.h"
13 #include "main.h"
14 #include "microdesc.h"
15 #include "networkstatus.h"
16 #include "nodelist.h"
17 #include "policies.h"
18 #include "rendservice.h"
19 #include "router.h"
20 #include "routerlist.h"
21 #include "routerset.h"
23 #include <string.h>
25 static void nodelist_drop_node(node_t *node, int remove_from_ht);
26 static void node_free(node_t *node);
27 static void update_router_have_minimum_dir_info(void);
28 static double get_frac_paths_needed_for_circs(const or_options_t *options,
29 const networkstatus_t *ns);
31 /** A nodelist_t holds a node_t object for every router we're "willing to use
32 * for something". Specifically, it should hold a node_t for every node that
33 * is currently in the routerlist, or currently in the consensus we're using.
35 typedef struct nodelist_t {
36 /* A list of all the nodes. */
37 smartlist_t *nodes;
38 /* Hash table to map from node ID digest to node. */
39 HT_HEAD(nodelist_map, node_t) nodes_by_id;
41 } nodelist_t;
43 static INLINE unsigned int
44 node_id_hash(const node_t *node)
46 return (unsigned) siphash24g(node->identity, DIGEST_LEN);
49 static INLINE unsigned int
50 node_id_eq(const node_t *node1, const node_t *node2)
52 return tor_memeq(node1->identity, node2->identity, DIGEST_LEN);
55 HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq);
56 HT_GENERATE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
57 0.6, malloc, realloc, free);
59 /** The global nodelist. */
60 static nodelist_t *the_nodelist=NULL;
62 /** Create an empty nodelist if we haven't done so already. */
63 static void
64 init_nodelist(void)
66 if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
67 the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
68 HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
69 the_nodelist->nodes = smartlist_new();
73 /** As node_get_by_id, but returns a non-const pointer */
74 node_t *
75 node_get_mutable_by_id(const char *identity_digest)
77 node_t search, *node;
78 if (PREDICT_UNLIKELY(the_nodelist == NULL))
79 return NULL;
81 memcpy(&search.identity, identity_digest, DIGEST_LEN);
82 node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
83 return node;
86 /** Return the node_t whose identity is <b>identity_digest</b>, or NULL
87 * if no such node exists. */
88 MOCK_IMPL(const node_t *,
89 node_get_by_id,(const char *identity_digest))
91 return node_get_mutable_by_id(identity_digest);
94 /** Internal: return the node_t whose identity_digest is
95 * <b>identity_digest</b>. If none exists, create a new one, add it to the
96 * nodelist, and return it.
98 * Requires that the nodelist be initialized.
100 static node_t *
101 node_get_or_create(const char *identity_digest)
103 node_t *node;
105 if ((node = node_get_mutable_by_id(identity_digest)))
106 return node;
108 node = tor_malloc_zero(sizeof(node_t));
109 memcpy(node->identity, identity_digest, DIGEST_LEN);
110 HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
112 smartlist_add(the_nodelist->nodes, node);
113 node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
115 node->country = -1;
117 return node;
120 /** Called when a node's address changes. */
121 static void
122 node_addrs_changed(node_t *node)
124 node->last_reachable = node->last_reachable6 = 0;
125 node->country = -1;
128 /** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
129 * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
130 * to the previous routerinfo.
132 node_t *
133 nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
135 node_t *node;
136 const char *id_digest;
137 int had_router = 0;
138 tor_assert(ri);
140 init_nodelist();
141 id_digest = ri->cache_info.identity_digest;
142 node = node_get_or_create(id_digest);
144 if (node->ri) {
145 if (!routers_have_same_or_addrs(node->ri, ri)) {
146 node_addrs_changed(node);
148 had_router = 1;
149 if (ri_old_out)
150 *ri_old_out = node->ri;
151 } else {
152 if (ri_old_out)
153 *ri_old_out = NULL;
155 node->ri = ri;
157 if (node->country == -1)
158 node_set_country(node);
160 if (authdir_mode(get_options()) && !had_router) {
161 const char *discard=NULL;
162 uint32_t status = dirserv_router_get_status(ri, &discard);
163 dirserv_set_node_flags_from_authoritative_status(node, status);
166 return node;
169 /** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
171 * Called when a new microdesc has arrived and the usable consensus flavor
172 * is "microdesc".
174 node_t *
175 nodelist_add_microdesc(microdesc_t *md)
177 networkstatus_t *ns =
178 networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
179 const routerstatus_t *rs;
180 node_t *node;
181 if (ns == NULL)
182 return NULL;
183 init_nodelist();
185 /* Microdescriptors don't carry an identity digest, so we need to figure
186 * it out by looking up the routerstatus. */
187 rs = router_get_consensus_status_by_descriptor_digest(ns, md->digest);
188 if (rs == NULL)
189 return NULL;
190 node = node_get_mutable_by_id(rs->identity_digest);
191 if (node) {
192 if (node->md)
193 node->md->held_by_nodes--;
194 node->md = md;
195 md->held_by_nodes++;
197 return node;
200 /** Tell the nodelist that the current usable consensus is <b>ns</b>.
201 * This makes the nodelist change all of the routerstatus entries for
202 * the nodes, drop nodes that no longer have enough info to get used,
203 * and grab microdescriptors into nodes as appropriate.
205 void
206 nodelist_set_consensus(networkstatus_t *ns)
208 const or_options_t *options = get_options();
209 int authdir = authdir_mode_v3(options);
210 int client = !server_mode(options);
212 init_nodelist();
213 if (ns->flavor == FLAV_MICRODESC)
214 (void) get_microdesc_cache(); /* Make sure it exists first. */
216 SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
217 node->rs = NULL);
219 SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
220 node_t *node = node_get_or_create(rs->identity_digest);
221 node->rs = rs;
222 if (ns->flavor == FLAV_MICRODESC) {
223 if (node->md == NULL ||
224 tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
225 if (node->md)
226 node->md->held_by_nodes--;
227 node->md = microdesc_cache_lookup_by_digest256(NULL,
228 rs->descriptor_digest);
229 if (node->md)
230 node->md->held_by_nodes++;
234 node_set_country(node);
236 /* If we're not an authdir, believe others. */
237 if (!authdir) {
238 node->is_valid = rs->is_valid;
239 node->is_running = rs->is_flagged_running;
240 node->is_fast = rs->is_fast;
241 node->is_stable = rs->is_stable;
242 node->is_possible_guard = rs->is_possible_guard;
243 node->is_exit = rs->is_exit;
244 node->is_bad_directory = rs->is_bad_directory;
245 node->is_bad_exit = rs->is_bad_exit;
246 node->is_hs_dir = rs->is_hs_dir;
247 node->ipv6_preferred = 0;
248 if (client && options->ClientPreferIPv6ORPort == 1 &&
249 (tor_addr_is_null(&rs->ipv6_addr) == 0 ||
250 (node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
251 node->ipv6_preferred = 1;
254 } SMARTLIST_FOREACH_END(rs);
256 nodelist_purge();
258 if (! authdir) {
259 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
260 /* We have no routerstatus for this router. Clear flags so we can skip
261 * it, maybe.*/
262 if (!node->rs) {
263 tor_assert(node->ri); /* if it had only an md, or nothing, purge
264 * would have removed it. */
265 if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
266 /* Clear all flags. */
267 node->is_valid = node->is_running = node->is_hs_dir =
268 node->is_fast = node->is_stable =
269 node->is_possible_guard = node->is_exit =
270 node->is_bad_exit = node->is_bad_directory =
271 node->ipv6_preferred = 0;
274 } SMARTLIST_FOREACH_END(node);
278 /** Helper: return true iff a node has a usable amount of information*/
279 static INLINE int
280 node_is_usable(const node_t *node)
282 return (node->rs) || (node->ri);
285 /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
286 * node with <b>identity_digest</b>. */
287 void
288 nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
290 node_t *node = node_get_mutable_by_id(identity_digest);
291 if (node && node->md == md) {
292 node->md = NULL;
293 md->held_by_nodes--;
297 /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
298 void
299 nodelist_remove_routerinfo(routerinfo_t *ri)
301 node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
302 if (node && node->ri == ri) {
303 node->ri = NULL;
304 if (! node_is_usable(node)) {
305 nodelist_drop_node(node, 1);
306 node_free(node);
311 /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
312 * with.) */
313 static void
314 nodelist_drop_node(node_t *node, int remove_from_ht)
316 node_t *tmp;
317 int idx;
318 if (remove_from_ht) {
319 tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
320 tor_assert(tmp == node);
323 idx = node->nodelist_idx;
324 tor_assert(idx >= 0);
326 tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
327 smartlist_del(the_nodelist->nodes, idx);
328 if (idx < smartlist_len(the_nodelist->nodes)) {
329 tmp = smartlist_get(the_nodelist->nodes, idx);
330 tmp->nodelist_idx = idx;
332 node->nodelist_idx = -1;
335 /** Return a newly allocated smartlist of the nodes that have <b>md</b> as
336 * their microdescriptor. */
337 smartlist_t *
338 nodelist_find_nodes_with_microdesc(const microdesc_t *md)
340 smartlist_t *result = smartlist_new();
342 if (the_nodelist == NULL)
343 return result;
345 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
346 if (node->md == md) {
347 smartlist_add(result, node);
349 } SMARTLIST_FOREACH_END(node);
351 return result;
354 /** Release storage held by <b>node</b> */
355 static void
356 node_free(node_t *node)
358 if (!node)
359 return;
360 if (node->md)
361 node->md->held_by_nodes--;
362 tor_assert(node->nodelist_idx == -1);
363 tor_free(node);
366 /** Remove all entries from the nodelist that don't have enough info to be
367 * usable for anything. */
368 void
369 nodelist_purge(void)
371 node_t **iter;
372 if (PREDICT_UNLIKELY(the_nodelist == NULL))
373 return;
375 /* Remove the non-usable nodes. */
376 for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
377 node_t *node = *iter;
379 if (node->md && !node->rs) {
380 /* An md is only useful if there is an rs. */
381 node->md->held_by_nodes--;
382 node->md = NULL;
385 if (node_is_usable(node)) {
386 iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
387 } else {
388 iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
389 nodelist_drop_node(node, 0);
390 node_free(node);
393 nodelist_assert_ok();
396 /** Release all storage held by the nodelist. */
397 void
398 nodelist_free_all(void)
400 if (PREDICT_UNLIKELY(the_nodelist == NULL))
401 return;
403 HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
404 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
405 node->nodelist_idx = -1;
406 node_free(node);
407 } SMARTLIST_FOREACH_END(node);
409 smartlist_free(the_nodelist->nodes);
411 tor_free(the_nodelist);
414 /** Check that the nodelist is internally consistent, and consistent with
415 * the directory info it's derived from.
417 void
418 nodelist_assert_ok(void)
420 routerlist_t *rl = router_get_routerlist();
421 networkstatus_t *ns = networkstatus_get_latest_consensus();
422 digestmap_t *dm;
424 if (!the_nodelist)
425 return;
427 dm = digestmap_new();
429 /* every routerinfo in rl->routers should be in the nodelist. */
430 if (rl) {
431 SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
432 const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
433 tor_assert(node && node->ri == ri);
434 tor_assert(fast_memeq(ri->cache_info.identity_digest,
435 node->identity, DIGEST_LEN));
436 tor_assert(! digestmap_get(dm, node->identity));
437 digestmap_set(dm, node->identity, (void*)node);
438 } SMARTLIST_FOREACH_END(ri);
441 /* every routerstatus in ns should be in the nodelist */
442 if (ns) {
443 SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
444 const node_t *node = node_get_by_id(rs->identity_digest);
445 tor_assert(node && node->rs == rs);
446 tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
447 digestmap_set(dm, node->identity, (void*)node);
448 if (ns->flavor == FLAV_MICRODESC) {
449 /* If it's a microdesc consensus, every entry that has a
450 * microdescriptor should be in the nodelist.
452 microdesc_t *md =
453 microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
454 tor_assert(md == node->md);
455 if (md)
456 tor_assert(md->held_by_nodes >= 1);
458 } SMARTLIST_FOREACH_END(rs);
461 /* The nodelist should have no other entries, and its entries should be
462 * well-formed. */
463 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
464 tor_assert(digestmap_get(dm, node->identity) != NULL);
465 tor_assert(node_sl_idx == node->nodelist_idx);
466 } SMARTLIST_FOREACH_END(node);
468 tor_assert((long)smartlist_len(the_nodelist->nodes) ==
469 (long)HT_SIZE(&the_nodelist->nodes_by_id));
471 digestmap_free(dm, NULL);
474 /** Return a list of a node_t * for every node we know about. The caller
475 * MUST NOT modify the list. (You can set and clear flags in the nodes if
476 * you must, but you must not add or remove nodes.) */
477 smartlist_t *
478 nodelist_get_list(void)
480 init_nodelist();
481 return the_nodelist->nodes;
484 /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
485 * or $DIGEST~name, return the node with the matching identity digest and
486 * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
487 * is not well-formed. */
488 const node_t *
489 node_get_by_hex_id(const char *hex_id)
491 char digest_buf[DIGEST_LEN];
492 char nn_buf[MAX_NICKNAME_LEN+1];
493 char nn_char='\0';
495 if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
496 const node_t *node = node_get_by_id(digest_buf);
497 if (!node)
498 return NULL;
499 if (nn_char) {
500 const char *real_name = node_get_nickname(node);
501 if (!real_name || strcasecmp(real_name, nn_buf))
502 return NULL;
503 if (nn_char == '=') {
504 const char *named_id =
505 networkstatus_get_router_digest_by_nickname(nn_buf);
506 if (!named_id || tor_memneq(named_id, digest_buf, DIGEST_LEN))
507 return NULL;
510 return node;
513 return NULL;
516 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
517 * the corresponding node_t, or NULL if none exists. Warn the user if
518 * <b>warn_if_unnamed</b> is set, and they have specified a router by
519 * nickname, but the Named flag isn't set for that router. */
520 const node_t *
521 node_get_by_nickname(const char *nickname, int warn_if_unnamed)
523 const node_t *node;
524 if (!the_nodelist)
525 return NULL;
527 /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
528 if ((node = node_get_by_hex_id(nickname)) != NULL)
529 return node;
531 if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
532 return NULL;
534 /* Okay, so if we get here, the nickname is just a nickname. Is there
535 * a binding for it in the consensus? */
537 const char *named_id =
538 networkstatus_get_router_digest_by_nickname(nickname);
539 if (named_id)
540 return node_get_by_id(named_id);
543 /* Is it marked as owned-by-someone-else? */
544 if (networkstatus_nickname_is_unnamed(nickname)) {
545 log_info(LD_GENERAL, "The name %s is listed as Unnamed: there is some "
546 "router that holds it, but not one listed in the current "
547 "consensus.", escaped(nickname));
548 return NULL;
551 /* Okay, so the name is not canonical for anybody. */
553 smartlist_t *matches = smartlist_new();
554 const node_t *choice = NULL;
556 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
557 if (!strcasecmp(node_get_nickname(node), nickname))
558 smartlist_add(matches, node);
559 } SMARTLIST_FOREACH_END(node);
561 if (smartlist_len(matches)>1 && warn_if_unnamed) {
562 int any_unwarned = 0;
563 SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
564 if (!node->name_lookup_warned) {
565 node->name_lookup_warned = 1;
566 any_unwarned = 1;
568 } SMARTLIST_FOREACH_END(node);
570 if (any_unwarned) {
571 log_warn(LD_CONFIG, "There are multiple matches for the name %s, "
572 "but none is listed as Named in the directory consensus. "
573 "Choosing one arbitrarily.", nickname);
575 } else if (smartlist_len(matches)>1 && warn_if_unnamed) {
576 char fp[HEX_DIGEST_LEN+1];
577 node_t *node = smartlist_get(matches, 0);
578 if (node->name_lookup_warned) {
579 base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
580 log_warn(LD_CONFIG,
581 "You specified a server \"%s\" by name, but the directory "
582 "authorities do not have any key registered for this "
583 "nickname -- so it could be used by any server, not just "
584 "the one you meant. "
585 "To make sure you get the same server in the future, refer "
586 "to it by key, as \"$%s\".", nickname, fp);
587 node->name_lookup_warned = 1;
591 if (smartlist_len(matches))
592 choice = smartlist_get(matches, 0);
594 smartlist_free(matches);
595 return choice;
599 /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
600 const char *
601 node_get_nickname(const node_t *node)
603 tor_assert(node);
604 if (node->rs)
605 return node->rs->nickname;
606 else if (node->ri)
607 return node->ri->nickname;
608 else
609 return NULL;
612 /** Return true iff the nickname of <b>node</b> is canonical, based on the
613 * latest consensus. */
615 node_is_named(const node_t *node)
617 const char *named_id;
618 const char *nickname = node_get_nickname(node);
619 if (!nickname)
620 return 0;
621 named_id = networkstatus_get_router_digest_by_nickname(nickname);
622 if (!named_id)
623 return 0;
624 return tor_memeq(named_id, node->identity, DIGEST_LEN);
627 /** Return true iff <b>node</b> appears to be a directory authority or
628 * directory cache */
630 node_is_dir(const node_t *node)
632 if (node->rs)
633 return node->rs->dir_port != 0;
634 else if (node->ri)
635 return node->ri->dir_port != 0;
636 else
637 return 0;
640 /** Return true iff <b>node</b> has either kind of usable descriptor -- that
641 * is, a routerdescriptor or a microdescriptor. */
643 node_has_descriptor(const node_t *node)
645 return (node->ri ||
646 (node->rs && node->md));
649 /** Return the router_purpose of <b>node</b>. */
651 node_get_purpose(const node_t *node)
653 if (node->ri)
654 return node->ri->purpose;
655 else
656 return ROUTER_PURPOSE_GENERAL;
659 /** Compute the verbose ("extended") nickname of <b>node</b> and store it
660 * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
661 * <b>verbose_name_out</b> */
662 void
663 node_get_verbose_nickname(const node_t *node,
664 char *verbose_name_out)
666 const char *nickname = node_get_nickname(node);
667 int is_named = node_is_named(node);
668 verbose_name_out[0] = '$';
669 base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
670 DIGEST_LEN);
671 if (!nickname)
672 return;
673 verbose_name_out[1+HEX_DIGEST_LEN] = is_named ? '=' : '~';
674 strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
677 /** Compute the verbose ("extended") nickname of node with
678 * given <b>id_digest</b> and store it into the MAX_VERBOSE_NICKNAME_LEN+1
679 * character buffer at <b>verbose_name_out</b>
681 * If node_get_by_id() returns NULL, base 16 encoding of
682 * <b>id_digest</b> is returned instead. */
683 void
684 node_get_verbose_nickname_by_id(const char *id_digest,
685 char *verbose_name_out)
687 const node_t *node = node_get_by_id(id_digest);
688 if (!node) {
689 verbose_name_out[0] = '$';
690 base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
691 } else {
692 node_get_verbose_nickname(node, verbose_name_out);
696 /** Return true iff it seems that <b>node</b> allows circuits to exit
697 * through it directlry from the client. */
699 node_allows_single_hop_exits(const node_t *node)
701 if (node && node->ri)
702 return node->ri->allow_single_hop_exits;
703 else
704 return 0;
707 /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
708 * actually permit anything to exit, or we don't know its exit policy */
710 node_exit_policy_rejects_all(const node_t *node)
712 if (node->rejects_all)
713 return 1;
715 if (node->ri)
716 return node->ri->policy_is_reject_star;
717 else if (node->md)
718 return node->md->exit_policy == NULL ||
719 short_policy_is_reject_star(node->md->exit_policy);
720 else
721 return 1;
724 /** Return true iff the exit policy for <b>node</b> is such that we can treat
725 * rejecting an address of type <b>family</b> unexpectedly as a sign of that
726 * node's failure. */
728 node_exit_policy_is_exact(const node_t *node, sa_family_t family)
730 if (family == AF_UNSPEC) {
731 return 1; /* Rejecting an address but not telling us what address
732 * is a bad sign. */
733 } else if (family == AF_INET) {
734 return node->ri != NULL;
735 } else if (family == AF_INET6) {
736 return 0;
738 tor_fragile_assert();
739 return 1;
742 /** Return list of tor_addr_port_t with all OR ports (in the sense IP
743 * addr + TCP port) for <b>node</b>. Caller must free all elements
744 * using tor_free() and free the list using smartlist_free().
746 * XXX this is potentially a memory fragmentation hog -- if on
747 * critical path consider the option of having the caller allocate the
748 * memory
750 smartlist_t *
751 node_get_all_orports(const node_t *node)
753 smartlist_t *sl = smartlist_new();
755 if (node->ri != NULL) {
756 if (node->ri->addr != 0) {
757 tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
758 tor_addr_from_ipv4h(&ap->addr, node->ri->addr);
759 ap->port = node->ri->or_port;
760 smartlist_add(sl, ap);
762 if (!tor_addr_is_null(&node->ri->ipv6_addr)) {
763 tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
764 tor_addr_copy(&ap->addr, &node->ri->ipv6_addr);
765 ap->port = node->ri->or_port;
766 smartlist_add(sl, ap);
768 } else if (node->rs != NULL) {
769 tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t));
770 tor_addr_from_ipv4h(&ap->addr, node->rs->addr);
771 ap->port = node->rs->or_port;
772 smartlist_add(sl, ap);
775 return sl;
778 /** Wrapper around node_get_prim_orport for backward
779 compatibility. */
780 void
781 node_get_addr(const node_t *node, tor_addr_t *addr_out)
783 tor_addr_port_t ap;
784 node_get_prim_orport(node, &ap);
785 tor_addr_copy(addr_out, &ap.addr);
788 /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
789 * seem to have one. */
790 uint32_t
791 node_get_prim_addr_ipv4h(const node_t *node)
793 if (node->ri) {
794 return node->ri->addr;
795 } else if (node->rs) {
796 return node->rs->addr;
798 return 0;
801 /** Copy a string representation of an IP address for <b>node</b> into
802 * the <b>len</b>-byte buffer at <b>buf</b>. */
803 void
804 node_get_address_string(const node_t *node, char *buf, size_t len)
806 if (node->ri) {
807 strlcpy(buf, fmt_addr32(node->ri->addr), len);
808 } else if (node->rs) {
809 tor_addr_t addr;
810 tor_addr_from_ipv4h(&addr, node->rs->addr);
811 tor_addr_to_str(buf, &addr, len, 0);
812 } else {
813 buf[0] = '\0';
817 /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
818 * one. */
819 long
820 node_get_declared_uptime(const node_t *node)
822 if (node->ri)
823 return node->ri->uptime;
824 else
825 return -1;
828 /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
829 const char *
830 node_get_platform(const node_t *node)
832 /* If we wanted, we could record the version in the routerstatus_t, since
833 * the consensus lists it. We don't, though, so this function just won't
834 * work with microdescriptors. */
835 if (node->ri)
836 return node->ri->platform;
837 else
838 return NULL;
841 /** Return <b>node</b>'s time of publication, or 0 if we don't have one. */
842 time_t
843 node_get_published_on(const node_t *node)
845 if (node->ri)
846 return node->ri->cache_info.published_on;
847 else
848 return 0;
851 /** Return true iff <b>node</b> is one representing this router. */
853 node_is_me(const node_t *node)
855 return router_digest_is_me(node->identity);
858 /** Return <b>node</b> declared family (as a list of names), or NULL if
859 * the node didn't declare a family. */
860 const smartlist_t *
861 node_get_declared_family(const node_t *node)
863 if (node->ri && node->ri->declared_family)
864 return node->ri->declared_family;
865 else if (node->md && node->md->family)
866 return node->md->family;
867 else
868 return NULL;
871 /** Return 1 if we prefer the IPv6 address and OR TCP port of
872 * <b>node</b>, else 0.
874 * We prefer the IPv6 address if the router has an IPv6 address and
875 * i) the node_t says that it prefers IPv6
876 * or
877 * ii) the router has no IPv4 address. */
879 node_ipv6_preferred(const node_t *node)
881 tor_addr_port_t ipv4_addr;
882 node_assert_ok(node);
884 if (node->ipv6_preferred || node_get_prim_orport(node, &ipv4_addr)) {
885 if (node->ri)
886 return !tor_addr_is_null(&node->ri->ipv6_addr);
887 if (node->md)
888 return !tor_addr_is_null(&node->md->ipv6_addr);
889 if (node->rs)
890 return !tor_addr_is_null(&node->rs->ipv6_addr);
892 return 0;
895 /** Copy the primary (IPv4) OR port (IP address and TCP port) for
896 * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
897 * port was copied, else return non-zero.*/
899 node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
901 node_assert_ok(node);
902 tor_assert(ap_out);
904 if (node->ri) {
905 if (node->ri->addr == 0 || node->ri->or_port == 0)
906 return -1;
907 tor_addr_from_ipv4h(&ap_out->addr, node->ri->addr);
908 ap_out->port = node->ri->or_port;
909 return 0;
911 if (node->rs) {
912 if (node->rs->addr == 0 || node->rs->or_port == 0)
913 return -1;
914 tor_addr_from_ipv4h(&ap_out->addr, node->rs->addr);
915 ap_out->port = node->rs->or_port;
916 return 0;
918 return -1;
921 /** Copy the preferred OR port (IP address and TCP port) for
922 * <b>node</b> into *<b>ap_out</b>. */
923 void
924 node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
926 const or_options_t *options = get_options();
927 tor_assert(ap_out);
929 /* Cheap implementation of config option ClientUseIPv6 -- simply
930 don't prefer IPv6 when ClientUseIPv6 is not set and we're not a
931 client running with bridges. See #4455 for more on this subject.
933 Note that this filter is too strict since we're hindering not
934 only clients! Erring on the safe side shouldn't be a problem
935 though. XXX move this check to where outgoing connections are
936 made? -LN */
937 if ((options->ClientUseIPv6 || options->UseBridges) &&
938 node_ipv6_preferred(node)) {
939 node_get_pref_ipv6_orport(node, ap_out);
940 } else {
941 node_get_prim_orport(node, ap_out);
945 /** Copy the preferred IPv6 OR port (IP address and TCP port) for
946 * <b>node</b> into *<b>ap_out</b>. */
947 void
948 node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
950 node_assert_ok(node);
951 tor_assert(ap_out);
953 /* We prefer the microdesc over a potential routerstatus here. They
954 are not being synchronised atm so there might be a chance that
955 they differ at some point, f.ex. when flipping
956 UseMicrodescriptors? -LN */
958 if (node->ri) {
959 tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
960 ap_out->port = node->ri->ipv6_orport;
961 } else if (node->md) {
962 tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
963 ap_out->port = node->md->ipv6_orport;
964 } else if (node->rs) {
965 tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
966 ap_out->port = node->rs->ipv6_orport;
970 /** Return true iff <b>node</b> has a curve25519 onion key. */
972 node_has_curve25519_onion_key(const node_t *node)
974 if (node->ri)
975 return node->ri->onion_curve25519_pkey != NULL;
976 else if (node->md)
977 return node->md->onion_curve25519_pkey != NULL;
978 else
979 return 0;
982 /** Refresh the country code of <b>ri</b>. This function MUST be called on
983 * each router when the GeoIP database is reloaded, and on all new routers. */
984 void
985 node_set_country(node_t *node)
987 tor_addr_t addr = TOR_ADDR_NULL;
989 /* XXXXipv6 */
990 if (node->rs)
991 tor_addr_from_ipv4h(&addr, node->rs->addr);
992 else if (node->ri)
993 tor_addr_from_ipv4h(&addr, node->ri->addr);
995 node->country = geoip_get_country_by_addr(&addr);
998 /** Set the country code of all routers in the routerlist. */
999 void
1000 nodelist_refresh_countries(void)
1002 smartlist_t *nodes = nodelist_get_list();
1003 SMARTLIST_FOREACH(nodes, node_t *, node,
1004 node_set_country(node));
1007 /** Return true iff router1 and router2 have similar enough network addresses
1008 * that we should treat them as being in the same family */
1009 static INLINE int
1010 addrs_in_same_network_family(const tor_addr_t *a1,
1011 const tor_addr_t *a2)
1013 return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
1016 /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
1017 * (case-insensitive), or if <b>node's</b> identity key digest
1018 * matches a hexadecimal value stored in <b>nickname</b>. Return
1019 * false otherwise. */
1020 static int
1021 node_nickname_matches(const node_t *node, const char *nickname)
1023 const char *n = node_get_nickname(node);
1024 if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
1025 return 1;
1026 return hex_digest_nickname_matches(nickname,
1027 node->identity,
1029 node_is_named(node));
1032 /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
1033 static INLINE int
1034 node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
1036 if (!lst) return 0;
1037 SMARTLIST_FOREACH(lst, const char *, name, {
1038 if (node_nickname_matches(node, name))
1039 return 1;
1041 return 0;
1044 /** Return true iff r1 and r2 are in the same family, but not the same
1045 * router. */
1047 nodes_in_same_family(const node_t *node1, const node_t *node2)
1049 const or_options_t *options = get_options();
1051 /* Are they in the same family because of their addresses? */
1052 if (options->EnforceDistinctSubnets) {
1053 tor_addr_t a1, a2;
1054 node_get_addr(node1, &a1);
1055 node_get_addr(node2, &a2);
1056 if (addrs_in_same_network_family(&a1, &a2))
1057 return 1;
1060 /* Are they in the same family because the agree they are? */
1062 const smartlist_t *f1, *f2;
1063 f1 = node_get_declared_family(node1);
1064 f2 = node_get_declared_family(node2);
1065 if (f1 && f2 &&
1066 node_in_nickname_smartlist(f1, node2) &&
1067 node_in_nickname_smartlist(f2, node1))
1068 return 1;
1071 /* Are they in the same option because the user says they are? */
1072 if (options->NodeFamilySets) {
1073 SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
1074 if (routerset_contains_node(rs, node1) &&
1075 routerset_contains_node(rs, node2))
1076 return 1;
1080 return 0;
1084 * Add all the family of <b>node</b>, including <b>node</b> itself, to
1085 * the smartlist <b>sl</b>.
1087 * This is used to make sure we don't pick siblings in a single path, or
1088 * pick more than one relay from a family for our entry guard list.
1089 * Note that a node may be added to <b>sl</b> more than once if it is
1090 * part of <b>node</b>'s family for more than one reason.
1092 void
1093 nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
1095 const smartlist_t *all_nodes = nodelist_get_list();
1096 const smartlist_t *declared_family;
1097 const or_options_t *options = get_options();
1099 tor_assert(node);
1101 declared_family = node_get_declared_family(node);
1103 /* Let's make sure that we have the node itself, if it's a real node. */
1105 const node_t *real_node = node_get_by_id(node->identity);
1106 if (real_node)
1107 smartlist_add(sl, (node_t*)real_node);
1110 /* First, add any nodes with similar network addresses. */
1111 if (options->EnforceDistinctSubnets) {
1112 tor_addr_t node_addr;
1113 node_get_addr(node, &node_addr);
1115 SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
1116 tor_addr_t a;
1117 node_get_addr(node2, &a);
1118 if (addrs_in_same_network_family(&a, &node_addr))
1119 smartlist_add(sl, (void*)node2);
1120 } SMARTLIST_FOREACH_END(node2);
1123 /* Now, add all nodes in the declared_family of this node, if they
1124 * also declare this node to be in their family. */
1125 if (declared_family) {
1126 /* Add every r such that router declares familyness with node, and node
1127 * declares familyhood with router. */
1128 SMARTLIST_FOREACH_BEGIN(declared_family, const char *, name) {
1129 const node_t *node2;
1130 const smartlist_t *family2;
1131 if (!(node2 = node_get_by_nickname(name, 0)))
1132 continue;
1133 if (!(family2 = node_get_declared_family(node2)))
1134 continue;
1135 SMARTLIST_FOREACH_BEGIN(family2, const char *, name2) {
1136 if (node_nickname_matches(node, name2)) {
1137 smartlist_add(sl, (void*)node2);
1138 break;
1140 } SMARTLIST_FOREACH_END(name2);
1141 } SMARTLIST_FOREACH_END(name);
1144 /* If the user declared any families locally, honor those too. */
1145 if (options->NodeFamilySets) {
1146 SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
1147 if (routerset_contains_node(rs, node)) {
1148 routerset_get_all_nodes(sl, rs, NULL, 0);
1154 /** Find a router that's up, that has this IP address, and
1155 * that allows exit to this address:port, or return NULL if there
1156 * isn't a good one.
1157 * Don't exit enclave to excluded relays -- it wouldn't actually
1158 * hurt anything, but this way there are fewer confused users.
1160 const node_t *
1161 router_find_exact_exit_enclave(const char *address, uint16_t port)
1162 {/*XXXX MOVE*/
1163 uint32_t addr;
1164 struct in_addr in;
1165 tor_addr_t a;
1166 const or_options_t *options = get_options();
1168 if (!tor_inet_aton(address, &in))
1169 return NULL; /* it's not an IP already */
1170 addr = ntohl(in.s_addr);
1172 tor_addr_from_ipv4h(&a, addr);
1174 SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
1175 if (node_get_addr_ipv4h(node) == addr &&
1176 node->is_running &&
1177 compare_tor_addr_to_node_policy(&a, port, node) ==
1178 ADDR_POLICY_ACCEPTED &&
1179 !routerset_contains_node(options->ExcludeExitNodesUnion_, node))
1180 return node;
1182 return NULL;
1185 /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
1186 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
1187 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
1188 * bandwidth.
1189 * If <b>need_guard</b>, we require that the router is a possible entry guard.
1192 node_is_unreliable(const node_t *node, int need_uptime,
1193 int need_capacity, int need_guard)
1195 if (need_uptime && !node->is_stable)
1196 return 1;
1197 if (need_capacity && !node->is_fast)
1198 return 1;
1199 if (need_guard && !node->is_possible_guard)
1200 return 1;
1201 return 0;
1204 /** Return 1 if all running sufficiently-stable routers we can use will reject
1205 * addr:port. Return 0 if any might accept it. */
1207 router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
1208 int need_uptime)
1210 addr_policy_result_t r;
1212 SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
1213 if (node->is_running &&
1214 !node_is_unreliable(node, need_uptime, 0, 0)) {
1216 r = compare_tor_addr_to_node_policy(addr, port, node);
1218 if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
1219 return 0; /* this one could be ok. good enough. */
1221 } SMARTLIST_FOREACH_END(node);
1222 return 1; /* all will reject. */
1225 /** Mark the router with ID <b>digest</b> as running or non-running
1226 * in our routerlist. */
1227 void
1228 router_set_status(const char *digest, int up)
1230 node_t *node;
1231 tor_assert(digest);
1233 SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
1234 dir_server_t *, d,
1235 if (tor_memeq(d->digest, digest, DIGEST_LEN))
1236 d->is_running = up);
1238 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
1239 dir_server_t *, d,
1240 if (tor_memeq(d->digest, digest, DIGEST_LEN))
1241 d->is_running = up);
1243 node = node_get_mutable_by_id(digest);
1244 if (node) {
1245 #if 0
1246 log_debug(LD_DIR,"Marking router %s as %s.",
1247 node_describe(node), up ? "up" : "down");
1248 #endif
1249 if (!up && node_is_me(node) && !net_is_disabled())
1250 log_warn(LD_NET, "We just marked ourself as down. Are your external "
1251 "addresses reachable?");
1253 if (bool_neq(node->is_running, up))
1254 router_dir_info_changed();
1256 node->is_running = up;
1260 /** True iff, the last time we checked whether we had enough directory info
1261 * to build circuits, the answer was "yes". */
1262 static int have_min_dir_info = 0;
1263 /** True iff enough has changed since the last time we checked whether we had
1264 * enough directory info to build circuits that our old answer can no longer
1265 * be trusted. */
1266 static int need_to_update_have_min_dir_info = 1;
1267 /** String describing what we're missing before we have enough directory
1268 * info. */
1269 static char dir_info_status[256] = "";
1271 /** Return true iff we have enough networkstatus and router information to
1272 * start building circuits. Right now, this means "more than half the
1273 * networkstatus documents, and at least 1/4 of expected routers." */
1274 //XXX should consider whether we have enough exiting nodes here.
1276 router_have_minimum_dir_info(void)
1278 static int logged_delay=0;
1279 const char *delay_fetches_msg = NULL;
1280 if (should_delay_dir_fetches(get_options(), &delay_fetches_msg)) {
1281 if (!logged_delay)
1282 log_notice(LD_DIR, "Delaying directory fetches: %s", delay_fetches_msg);
1283 logged_delay=1;
1284 strlcpy(dir_info_status, delay_fetches_msg, sizeof(dir_info_status));
1285 return 0;
1287 logged_delay = 0; /* reset it if we get this far */
1289 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
1290 update_router_have_minimum_dir_info();
1293 return have_min_dir_info;
1296 /** Called when our internal view of the directory has changed. This can be
1297 * when the authorities change, networkstatuses change, the list of routerdescs
1298 * changes, or number of running routers changes.
1300 void
1301 router_dir_info_changed(void)
1303 need_to_update_have_min_dir_info = 1;
1304 rend_hsdir_routers_changed();
1307 /** Return a string describing what we're missing before we have enough
1308 * directory info. */
1309 const char *
1310 get_dir_info_status_string(void)
1312 return dir_info_status;
1315 /** Iterate over the servers listed in <b>consensus</b>, and count how many of
1316 * them seem like ones we'd use, and how many of <em>those</em> we have
1317 * descriptors for. Store the former in *<b>num_usable</b> and the latter in
1318 * *<b>num_present</b>. If <b>in_set</b> is non-NULL, only consider those
1319 * routers in <b>in_set</b>. If <b>exit_only</b> is true, only consider nodes
1320 * with the Exit flag. If *descs_out is present, add a node_t for each
1321 * usable descriptor to it.
1323 static void
1324 count_usable_descriptors(int *num_present, int *num_usable,
1325 smartlist_t *descs_out,
1326 const networkstatus_t *consensus,
1327 const or_options_t *options, time_t now,
1328 routerset_t *in_set, int exit_only)
1330 const int md = (consensus->flavor == FLAV_MICRODESC);
1331 *num_present = 0, *num_usable=0;
1333 SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
1335 const node_t *node = node_get_by_id(rs->identity_digest);
1336 if (!node)
1337 continue; /* This would be a bug: every entry in the consensus is
1338 * supposed to have a node. */
1339 if (exit_only && ! rs->is_exit)
1340 continue;
1341 if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
1342 continue;
1343 if (client_would_use_router(rs, now, options)) {
1344 const char * const digest = rs->descriptor_digest;
1345 int present;
1346 ++*num_usable; /* the consensus says we want it. */
1347 if (md)
1348 present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
1349 else
1350 present = NULL != router_get_by_descriptor_digest(digest);
1351 if (present) {
1352 /* we have the descriptor listed in the consensus. */
1353 ++*num_present;
1355 if (descs_out)
1356 smartlist_add(descs_out, (node_t*)node);
1359 SMARTLIST_FOREACH_END(rs);
1361 log_debug(LD_DIR, "%d usable, %d present (%s%s).",
1362 *num_usable, *num_present,
1363 md ? "microdesc" : "desc", exit_only ? " exits" : "s");
1366 /** Return an estimate of which fraction of usable paths through the Tor
1367 * network we have available for use. */
1368 static double
1369 compute_frac_paths_available(const networkstatus_t *consensus,
1370 const or_options_t *options, time_t now,
1371 int *num_present_out, int *num_usable_out,
1372 char **status_out)
1374 smartlist_t *guards = smartlist_new();
1375 smartlist_t *mid = smartlist_new();
1376 smartlist_t *exits = smartlist_new();
1377 smartlist_t *myexits= smartlist_new();
1378 smartlist_t *myexits_unflagged = smartlist_new();
1379 double f_guard, f_mid, f_exit, f_myexit, f_myexit_unflagged;
1380 int np, nu; /* Ignored */
1381 const int authdir = authdir_mode_v3(options);
1383 count_usable_descriptors(num_present_out, num_usable_out,
1384 mid, consensus, options, now, NULL, 0);
1385 if (options->EntryNodes) {
1386 count_usable_descriptors(&np, &nu, guards, consensus, options, now,
1387 options->EntryNodes, 0);
1388 } else {
1389 SMARTLIST_FOREACH(mid, const node_t *, node, {
1390 if (authdir) {
1391 if (node->rs && node->rs->is_possible_guard)
1392 smartlist_add(guards, (node_t*)node);
1393 } else {
1394 if (node->is_possible_guard)
1395 smartlist_add(guards, (node_t*)node);
1400 /* All nodes with exit flag */
1401 count_usable_descriptors(&np, &nu, exits, consensus, options, now,
1402 NULL, 1);
1403 /* All nodes with exit flag in ExitNodes option */
1404 count_usable_descriptors(&np, &nu, myexits, consensus, options, now,
1405 options->ExitNodes, 1);
1406 /* Now compute the nodes in the ExitNodes option where which we don't know
1407 * what their exit policy is, or we know it permits something. */
1408 count_usable_descriptors(&np, &nu, myexits_unflagged,
1409 consensus, options, now,
1410 options->ExitNodes, 0);
1411 SMARTLIST_FOREACH_BEGIN(myexits_unflagged, const node_t *, node) {
1412 if (node_has_descriptor(node) && node_exit_policy_rejects_all(node))
1413 SMARTLIST_DEL_CURRENT(myexits_unflagged, node);
1414 } SMARTLIST_FOREACH_END(node);
1416 f_guard = frac_nodes_with_descriptors(guards, WEIGHT_FOR_GUARD);
1417 f_mid = frac_nodes_with_descriptors(mid, WEIGHT_FOR_MID);
1418 f_exit = frac_nodes_with_descriptors(exits, WEIGHT_FOR_EXIT);
1419 f_myexit= frac_nodes_with_descriptors(myexits,WEIGHT_FOR_EXIT);
1420 f_myexit_unflagged=
1421 frac_nodes_with_descriptors(myexits_unflagged,WEIGHT_FOR_EXIT);
1423 /* If our ExitNodes list has eliminated every possible Exit node, and there
1424 * were some possible Exit nodes, then instead consider nodes that permit
1425 * exiting to some ports. */
1426 if (smartlist_len(myexits) == 0 &&
1427 smartlist_len(myexits_unflagged)) {
1428 f_myexit = f_myexit_unflagged;
1431 smartlist_free(guards);
1432 smartlist_free(mid);
1433 smartlist_free(exits);
1434 smartlist_free(myexits);
1435 smartlist_free(myexits_unflagged);
1437 /* This is a tricky point here: we don't want to make it easy for a
1438 * directory to trickle exits to us until it learns which exits we have
1439 * configured, so require that we have a threshold both of total exits
1440 * and usable exits. */
1441 if (f_myexit < f_exit)
1442 f_exit = f_myexit;
1444 if (status_out)
1445 tor_asprintf(status_out,
1446 "%d%% of guards bw, "
1447 "%d%% of midpoint bw, and "
1448 "%d%% of exit bw",
1449 (int)(f_guard*100),
1450 (int)(f_mid*100),
1451 (int)(f_exit*100));
1453 return f_guard * f_mid * f_exit;
1456 /** We just fetched a new set of descriptors. Compute how far through
1457 * the "loading descriptors" bootstrapping phase we are, so we can inform
1458 * the controller of our progress. */
1460 count_loading_descriptors_progress(void)
1462 int num_present = 0, num_usable=0;
1463 time_t now = time(NULL);
1464 const or_options_t *options = get_options();
1465 const networkstatus_t *consensus =
1466 networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
1467 double paths, fraction;
1469 if (!consensus)
1470 return 0; /* can't count descriptors if we have no list of them */
1472 paths = compute_frac_paths_available(consensus, options, now,
1473 &num_present, &num_usable,
1474 NULL);
1476 fraction = paths / get_frac_paths_needed_for_circs(options,consensus);
1477 if (fraction > 1.0)
1478 return 0; /* it's not the number of descriptors holding us back */
1479 return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
1480 (fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
1481 BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
1484 /** Return the fraction of paths needed before we're willing to build
1485 * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
1486 static double
1487 get_frac_paths_needed_for_circs(const or_options_t *options,
1488 const networkstatus_t *ns)
1490 #define DFLT_PCT_USABLE_NEEDED 60
1491 if (options->PathsNeededToBuildCircuits >= 0.0) {
1492 return options->PathsNeededToBuildCircuits;
1493 } else {
1494 return networkstatus_get_param(ns, "min_paths_for_circs_pct",
1495 DFLT_PCT_USABLE_NEEDED,
1496 25, 95)/100.0;
1500 /** Change the value of have_min_dir_info, setting it true iff we have enough
1501 * network and router information to build circuits. Clear the value of
1502 * need_to_update_have_min_dir_info. */
1503 static void
1504 update_router_have_minimum_dir_info(void)
1506 time_t now = time(NULL);
1507 int res;
1508 const or_options_t *options = get_options();
1509 const networkstatus_t *consensus =
1510 networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
1511 int using_md;
1513 if (!consensus) {
1514 if (!networkstatus_get_latest_consensus())
1515 strlcpy(dir_info_status, "We have no usable consensus.",
1516 sizeof(dir_info_status));
1517 else
1518 strlcpy(dir_info_status, "We have no recent usable consensus.",
1519 sizeof(dir_info_status));
1520 res = 0;
1521 goto done;
1524 using_md = consensus->flavor == FLAV_MICRODESC;
1527 char *status = NULL;
1528 int num_present=0, num_usable=0;
1529 double paths = compute_frac_paths_available(consensus, options, now,
1530 &num_present, &num_usable,
1531 &status);
1533 if (paths < get_frac_paths_needed_for_circs(options,consensus)) {
1534 tor_snprintf(dir_info_status, sizeof(dir_info_status),
1535 "We need more %sdescriptors: we have %d/%d, and "
1536 "can only build %d%% of likely paths. (We have %s.)",
1537 using_md?"micro":"", num_present, num_usable,
1538 (int)(paths*100), status);
1539 /* log_notice(LD_NET, "%s", dir_info_status); */
1540 tor_free(status);
1541 res = 0;
1542 control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
1543 goto done;
1546 tor_free(status);
1547 res = 1;
1550 done:
1551 if (res && !have_min_dir_info) {
1552 log_notice(LD_DIR,
1553 "We now have enough directory information to build circuits.");
1554 control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
1555 control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0);
1557 if (!res && have_min_dir_info) {
1558 int quiet = directory_too_idle_to_fetch_descriptors(options, now);
1559 tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
1560 "Our directory information is no longer up-to-date "
1561 "enough to build circuits: %s", dir_info_status);
1563 /* a) make us log when we next complete a circuit, so we know when Tor
1564 * is back up and usable, and b) disable some activities that Tor
1565 * should only do while circuits are working, like reachability tests
1566 * and fetching bridge descriptors only over circuits. */
1567 can_complete_circuit = 0;
1569 control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
1571 have_min_dir_info = res;
1572 need_to_update_have_min_dir_info = 0;