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 */
14 #include "microdesc.h"
15 #include "networkstatus.h"
18 #include "rendservice.h"
20 #include "routerlist.h"
21 #include "routerset.h"
25 static void nodelist_drop_node(node_t
*node
, int remove_from_ht
);
26 static void node_free(node_t
*node
);
28 /** count_usable_descriptors counts descriptors with these flag(s)
31 /* All descriptors regardless of flags */
32 USABLE_DESCRIPTOR_ALL
= 0,
33 /* Only descriptors with the Exit flag */
34 USABLE_DESCRIPTOR_EXIT_ONLY
= 1
35 } usable_descriptor_t
;
36 static void count_usable_descriptors(int *num_present
,
38 smartlist_t
*descs_out
,
39 const networkstatus_t
*consensus
,
40 const or_options_t
*options
,
43 usable_descriptor_t exit_only
);
44 static void update_router_have_minimum_dir_info(void);
45 static double get_frac_paths_needed_for_circs(const or_options_t
*options
,
46 const networkstatus_t
*ns
);
48 /** A nodelist_t holds a node_t object for every router we're "willing to use
49 * for something". Specifically, it should hold a node_t for every node that
50 * is currently in the routerlist, or currently in the consensus we're using.
52 typedef struct nodelist_t
{
53 /* A list of all the nodes. */
55 /* Hash table to map from node ID digest to node. */
56 HT_HEAD(nodelist_map
, node_t
) nodes_by_id
;
60 static INLINE
unsigned int
61 node_id_hash(const node_t
*node
)
63 return (unsigned) siphash24g(node
->identity
, DIGEST_LEN
);
66 static INLINE
unsigned int
67 node_id_eq(const node_t
*node1
, const node_t
*node2
)
69 return tor_memeq(node1
->identity
, node2
->identity
, DIGEST_LEN
);
72 HT_PROTOTYPE(nodelist_map
, node_t
, ht_ent
, node_id_hash
, node_id_eq
);
73 HT_GENERATE2(nodelist_map
, node_t
, ht_ent
, node_id_hash
, node_id_eq
,
74 0.6, tor_reallocarray_
, tor_free_
)
76 /** The global nodelist. */
77 static nodelist_t
*the_nodelist
=NULL
;
79 /** Create an empty nodelist if we haven't done so already. */
83 if (PREDICT_UNLIKELY(the_nodelist
== NULL
)) {
84 the_nodelist
= tor_malloc_zero(sizeof(nodelist_t
));
85 HT_INIT(nodelist_map
, &the_nodelist
->nodes_by_id
);
86 the_nodelist
->nodes
= smartlist_new();
90 /** As node_get_by_id, but returns a non-const pointer */
92 node_get_mutable_by_id(const char *identity_digest
)
95 if (PREDICT_UNLIKELY(the_nodelist
== NULL
))
98 memcpy(&search
.identity
, identity_digest
, DIGEST_LEN
);
99 node
= HT_FIND(nodelist_map
, &the_nodelist
->nodes_by_id
, &search
);
103 /** Return the node_t whose identity is <b>identity_digest</b>, or NULL
104 * if no such node exists. */
105 MOCK_IMPL(const node_t
*,
106 node_get_by_id
,(const char *identity_digest
))
108 return node_get_mutable_by_id(identity_digest
);
111 /** Internal: return the node_t whose identity_digest is
112 * <b>identity_digest</b>. If none exists, create a new one, add it to the
113 * nodelist, and return it.
115 * Requires that the nodelist be initialized.
118 node_get_or_create(const char *identity_digest
)
122 if ((node
= node_get_mutable_by_id(identity_digest
)))
125 node
= tor_malloc_zero(sizeof(node_t
));
126 memcpy(node
->identity
, identity_digest
, DIGEST_LEN
);
127 HT_INSERT(nodelist_map
, &the_nodelist
->nodes_by_id
, node
);
129 smartlist_add(the_nodelist
->nodes
, node
);
130 node
->nodelist_idx
= smartlist_len(the_nodelist
->nodes
) - 1;
137 /** Called when a node's address changes. */
139 node_addrs_changed(node_t
*node
)
141 node
->last_reachable
= node
->last_reachable6
= 0;
145 /** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
146 * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
147 * to the previous routerinfo.
150 nodelist_set_routerinfo(routerinfo_t
*ri
, routerinfo_t
**ri_old_out
)
153 const char *id_digest
;
158 id_digest
= ri
->cache_info
.identity_digest
;
159 node
= node_get_or_create(id_digest
);
162 if (!routers_have_same_or_addrs(node
->ri
, ri
)) {
163 node_addrs_changed(node
);
167 *ri_old_out
= node
->ri
;
174 if (node
->country
== -1)
175 node_set_country(node
);
177 if (authdir_mode(get_options()) && !had_router
) {
178 const char *discard
=NULL
;
179 uint32_t status
= dirserv_router_get_status(ri
, &discard
, LOG_INFO
);
180 dirserv_set_node_flags_from_authoritative_status(node
, status
);
186 /** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
188 * Called when a new microdesc has arrived and the usable consensus flavor
192 nodelist_add_microdesc(microdesc_t
*md
)
194 networkstatus_t
*ns
=
195 networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC
);
196 const routerstatus_t
*rs
;
202 /* Microdescriptors don't carry an identity digest, so we need to figure
203 * it out by looking up the routerstatus. */
204 rs
= router_get_consensus_status_by_descriptor_digest(ns
, md
->digest
);
207 node
= node_get_mutable_by_id(rs
->identity_digest
);
210 node
->md
->held_by_nodes
--;
217 /** Tell the nodelist that the current usable consensus is <b>ns</b>.
218 * This makes the nodelist change all of the routerstatus entries for
219 * the nodes, drop nodes that no longer have enough info to get used,
220 * and grab microdescriptors into nodes as appropriate.
223 nodelist_set_consensus(networkstatus_t
*ns
)
225 const or_options_t
*options
= get_options();
226 int authdir
= authdir_mode_v3(options
);
227 int client
= !server_mode(options
);
230 if (ns
->flavor
== FLAV_MICRODESC
)
231 (void) get_microdesc_cache(); /* Make sure it exists first. */
233 SMARTLIST_FOREACH(the_nodelist
->nodes
, node_t
*, node
,
236 SMARTLIST_FOREACH_BEGIN(ns
->routerstatus_list
, routerstatus_t
*, rs
) {
237 node_t
*node
= node_get_or_create(rs
->identity_digest
);
239 if (ns
->flavor
== FLAV_MICRODESC
) {
240 if (node
->md
== NULL
||
241 tor_memneq(node
->md
->digest
,rs
->descriptor_digest
,DIGEST256_LEN
)) {
243 node
->md
->held_by_nodes
--;
244 node
->md
= microdesc_cache_lookup_by_digest256(NULL
,
245 rs
->descriptor_digest
);
247 node
->md
->held_by_nodes
++;
251 node_set_country(node
);
253 /* If we're not an authdir, believe others. */
255 node
->is_valid
= rs
->is_valid
;
256 node
->is_running
= rs
->is_flagged_running
;
257 node
->is_fast
= rs
->is_fast
;
258 node
->is_stable
= rs
->is_stable
;
259 node
->is_possible_guard
= rs
->is_possible_guard
;
260 node
->is_exit
= rs
->is_exit
;
261 node
->is_bad_exit
= rs
->is_bad_exit
;
262 node
->is_hs_dir
= rs
->is_hs_dir
;
263 node
->ipv6_preferred
= 0;
264 if (client
&& options
->ClientPreferIPv6ORPort
== 1 &&
265 (tor_addr_is_null(&rs
->ipv6_addr
) == 0 ||
266 (node
->md
&& tor_addr_is_null(&node
->md
->ipv6_addr
) == 0)))
267 node
->ipv6_preferred
= 1;
270 } SMARTLIST_FOREACH_END(rs
);
275 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
276 /* We have no routerstatus for this router. Clear flags so we can skip
279 tor_assert(node
->ri
); /* if it had only an md, or nothing, purge
280 * would have removed it. */
281 if (node
->ri
->purpose
== ROUTER_PURPOSE_GENERAL
) {
282 /* Clear all flags. */
283 node
->is_valid
= node
->is_running
= node
->is_hs_dir
=
284 node
->is_fast
= node
->is_stable
=
285 node
->is_possible_guard
= node
->is_exit
=
286 node
->is_bad_exit
= node
->ipv6_preferred
= 0;
289 } SMARTLIST_FOREACH_END(node
);
293 /** Helper: return true iff a node has a usable amount of information*/
295 node_is_usable(const node_t
*node
)
297 return (node
->rs
) || (node
->ri
);
300 /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
301 * node with <b>identity_digest</b>. */
303 nodelist_remove_microdesc(const char *identity_digest
, microdesc_t
*md
)
305 node_t
*node
= node_get_mutable_by_id(identity_digest
);
306 if (node
&& node
->md
== md
) {
312 /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
314 nodelist_remove_routerinfo(routerinfo_t
*ri
)
316 node_t
*node
= node_get_mutable_by_id(ri
->cache_info
.identity_digest
);
317 if (node
&& node
->ri
== ri
) {
319 if (! node_is_usable(node
)) {
320 nodelist_drop_node(node
, 1);
326 /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
329 nodelist_drop_node(node_t
*node
, int remove_from_ht
)
333 if (remove_from_ht
) {
334 tmp
= HT_REMOVE(nodelist_map
, &the_nodelist
->nodes_by_id
, node
);
335 tor_assert(tmp
== node
);
338 idx
= node
->nodelist_idx
;
339 tor_assert(idx
>= 0);
341 tor_assert(node
== smartlist_get(the_nodelist
->nodes
, idx
));
342 smartlist_del(the_nodelist
->nodes
, idx
);
343 if (idx
< smartlist_len(the_nodelist
->nodes
)) {
344 tmp
= smartlist_get(the_nodelist
->nodes
, idx
);
345 tmp
->nodelist_idx
= idx
;
347 node
->nodelist_idx
= -1;
350 /** Return a newly allocated smartlist of the nodes that have <b>md</b> as
351 * their microdescriptor. */
353 nodelist_find_nodes_with_microdesc(const microdesc_t
*md
)
355 smartlist_t
*result
= smartlist_new();
357 if (the_nodelist
== NULL
)
360 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
361 if (node
->md
== md
) {
362 smartlist_add(result
, node
);
364 } SMARTLIST_FOREACH_END(node
);
369 /** Release storage held by <b>node</b> */
371 node_free(node_t
*node
)
376 node
->md
->held_by_nodes
--;
377 tor_assert(node
->nodelist_idx
== -1);
381 /** Remove all entries from the nodelist that don't have enough info to be
382 * usable for anything. */
387 if (PREDICT_UNLIKELY(the_nodelist
== NULL
))
390 /* Remove the non-usable nodes. */
391 for (iter
= HT_START(nodelist_map
, &the_nodelist
->nodes_by_id
); iter
; ) {
392 node_t
*node
= *iter
;
394 if (node
->md
&& !node
->rs
) {
395 /* An md is only useful if there is an rs. */
396 node
->md
->held_by_nodes
--;
400 if (node_is_usable(node
)) {
401 iter
= HT_NEXT(nodelist_map
, &the_nodelist
->nodes_by_id
, iter
);
403 iter
= HT_NEXT_RMV(nodelist_map
, &the_nodelist
->nodes_by_id
, iter
);
404 nodelist_drop_node(node
, 0);
408 nodelist_assert_ok();
411 /** Release all storage held by the nodelist. */
413 nodelist_free_all(void)
415 if (PREDICT_UNLIKELY(the_nodelist
== NULL
))
418 HT_CLEAR(nodelist_map
, &the_nodelist
->nodes_by_id
);
419 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
420 node
->nodelist_idx
= -1;
422 } SMARTLIST_FOREACH_END(node
);
424 smartlist_free(the_nodelist
->nodes
);
426 tor_free(the_nodelist
);
429 /** Check that the nodelist is internally consistent, and consistent with
430 * the directory info it's derived from.
433 nodelist_assert_ok(void)
435 routerlist_t
*rl
= router_get_routerlist();
436 networkstatus_t
*ns
= networkstatus_get_latest_consensus();
442 dm
= digestmap_new();
444 /* every routerinfo in rl->routers should be in the nodelist. */
446 SMARTLIST_FOREACH_BEGIN(rl
->routers
, routerinfo_t
*, ri
) {
447 const node_t
*node
= node_get_by_id(ri
->cache_info
.identity_digest
);
448 tor_assert(node
&& node
->ri
== ri
);
449 tor_assert(fast_memeq(ri
->cache_info
.identity_digest
,
450 node
->identity
, DIGEST_LEN
));
451 tor_assert(! digestmap_get(dm
, node
->identity
));
452 digestmap_set(dm
, node
->identity
, (void*)node
);
453 } SMARTLIST_FOREACH_END(ri
);
456 /* every routerstatus in ns should be in the nodelist */
458 SMARTLIST_FOREACH_BEGIN(ns
->routerstatus_list
, routerstatus_t
*, rs
) {
459 const node_t
*node
= node_get_by_id(rs
->identity_digest
);
460 tor_assert(node
&& node
->rs
== rs
);
461 tor_assert(fast_memeq(rs
->identity_digest
, node
->identity
, DIGEST_LEN
));
462 digestmap_set(dm
, node
->identity
, (void*)node
);
463 if (ns
->flavor
== FLAV_MICRODESC
) {
464 /* If it's a microdesc consensus, every entry that has a
465 * microdescriptor should be in the nodelist.
468 microdesc_cache_lookup_by_digest256(NULL
, rs
->descriptor_digest
);
469 tor_assert(md
== node
->md
);
471 tor_assert(md
->held_by_nodes
>= 1);
473 } SMARTLIST_FOREACH_END(rs
);
476 /* The nodelist should have no other entries, and its entries should be
478 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
479 tor_assert(digestmap_get(dm
, node
->identity
) != NULL
);
480 tor_assert(node_sl_idx
== node
->nodelist_idx
);
481 } SMARTLIST_FOREACH_END(node
);
483 tor_assert((long)smartlist_len(the_nodelist
->nodes
) ==
484 (long)HT_SIZE(&the_nodelist
->nodes_by_id
));
486 digestmap_free(dm
, NULL
);
489 /** Return a list of a node_t * for every node we know about. The caller
490 * MUST NOT modify the list. (You can set and clear flags in the nodes if
491 * you must, but you must not add or remove nodes.) */
492 MOCK_IMPL(smartlist_t
*,
493 nodelist_get_list
,(void))
496 return the_nodelist
->nodes
;
499 /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
500 * or $DIGEST~name, return the node with the matching identity digest and
501 * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
502 * is not well-formed. */
504 node_get_by_hex_id(const char *hex_id
)
506 char digest_buf
[DIGEST_LEN
];
507 char nn_buf
[MAX_NICKNAME_LEN
+1];
510 if (hex_digest_nickname_decode(hex_id
, digest_buf
, &nn_char
, nn_buf
)==0) {
511 const node_t
*node
= node_get_by_id(digest_buf
);
515 const char *real_name
= node_get_nickname(node
);
516 if (!real_name
|| strcasecmp(real_name
, nn_buf
))
518 if (nn_char
== '=') {
519 const char *named_id
=
520 networkstatus_get_router_digest_by_nickname(nn_buf
);
521 if (!named_id
|| tor_memneq(named_id
, digest_buf
, DIGEST_LEN
))
531 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
532 * the corresponding node_t, or NULL if none exists. Warn the user if
533 * <b>warn_if_unnamed</b> is set, and they have specified a router by
534 * nickname, but the Named flag isn't set for that router. */
535 MOCK_IMPL(const node_t
*,
536 node_get_by_nickname
,(const char *nickname
, int warn_if_unnamed
))
542 /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
543 if ((node
= node_get_by_hex_id(nickname
)) != NULL
)
546 if (!strcasecmp(nickname
, UNNAMED_ROUTER_NICKNAME
))
549 /* Okay, so if we get here, the nickname is just a nickname. Is there
550 * a binding for it in the consensus? */
552 const char *named_id
=
553 networkstatus_get_router_digest_by_nickname(nickname
);
555 return node_get_by_id(named_id
);
558 /* Is it marked as owned-by-someone-else? */
559 if (networkstatus_nickname_is_unnamed(nickname
)) {
560 log_info(LD_GENERAL
, "The name %s is listed as Unnamed: there is some "
561 "router that holds it, but not one listed in the current "
562 "consensus.", escaped(nickname
));
566 /* Okay, so the name is not canonical for anybody. */
568 smartlist_t
*matches
= smartlist_new();
569 const node_t
*choice
= NULL
;
571 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
572 if (!strcasecmp(node_get_nickname(node
), nickname
))
573 smartlist_add(matches
, node
);
574 } SMARTLIST_FOREACH_END(node
);
576 if (smartlist_len(matches
)>1 && warn_if_unnamed
) {
577 int any_unwarned
= 0;
578 SMARTLIST_FOREACH_BEGIN(matches
, node_t
*, node
) {
579 if (!node
->name_lookup_warned
) {
580 node
->name_lookup_warned
= 1;
583 } SMARTLIST_FOREACH_END(node
);
586 log_warn(LD_CONFIG
, "There are multiple matches for the name %s, "
587 "but none is listed as Named in the directory consensus. "
588 "Choosing one arbitrarily.", nickname
);
590 } else if (smartlist_len(matches
)==1 && warn_if_unnamed
) {
591 char fp
[HEX_DIGEST_LEN
+1];
592 node_t
*node
= smartlist_get(matches
, 0);
593 if (! node
->name_lookup_warned
) {
594 base16_encode(fp
, sizeof(fp
), node
->identity
, DIGEST_LEN
);
596 "You specified a server \"%s\" by name, but the directory "
597 "authorities do not have any key registered for this "
598 "nickname -- so it could be used by any server, not just "
599 "the one you meant. "
600 "To make sure you get the same server in the future, refer "
601 "to it by key, as \"$%s\".", nickname
, fp
);
602 node
->name_lookup_warned
= 1;
606 if (smartlist_len(matches
))
607 choice
= smartlist_get(matches
, 0);
609 smartlist_free(matches
);
614 /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
616 node_get_nickname(const node_t
*node
)
620 return node
->rs
->nickname
;
622 return node
->ri
->nickname
;
627 /** Return true iff the nickname of <b>node</b> is canonical, based on the
628 * latest consensus. */
630 node_is_named(const node_t
*node
)
632 const char *named_id
;
633 const char *nickname
= node_get_nickname(node
);
636 named_id
= networkstatus_get_router_digest_by_nickname(nickname
);
639 return tor_memeq(named_id
, node
->identity
, DIGEST_LEN
);
642 /** Return true iff <b>node</b> appears to be a directory authority or
645 node_is_dir(const node_t
*node
)
648 return node
->rs
->dir_port
!= 0;
650 return node
->ri
->dir_port
!= 0;
655 /** Return true iff <b>node</b> has either kind of usable descriptor -- that
656 * is, a routerdescriptor or a microdescriptor. */
658 node_has_descriptor(const node_t
*node
)
661 (node
->rs
&& node
->md
));
664 /** Return the router_purpose of <b>node</b>. */
666 node_get_purpose(const node_t
*node
)
669 return node
->ri
->purpose
;
671 return ROUTER_PURPOSE_GENERAL
;
674 /** Compute the verbose ("extended") nickname of <b>node</b> and store it
675 * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
676 * <b>verbose_name_out</b> */
678 node_get_verbose_nickname(const node_t
*node
,
679 char *verbose_name_out
)
681 const char *nickname
= node_get_nickname(node
);
682 int is_named
= node_is_named(node
);
683 verbose_name_out
[0] = '$';
684 base16_encode(verbose_name_out
+1, HEX_DIGEST_LEN
+1, node
->identity
,
688 verbose_name_out
[1+HEX_DIGEST_LEN
] = is_named
? '=' : '~';
689 strlcpy(verbose_name_out
+1+HEX_DIGEST_LEN
+1, nickname
, MAX_NICKNAME_LEN
+1);
692 /** Compute the verbose ("extended") nickname of node with
693 * given <b>id_digest</b> and store it into the MAX_VERBOSE_NICKNAME_LEN+1
694 * character buffer at <b>verbose_name_out</b>
696 * If node_get_by_id() returns NULL, base 16 encoding of
697 * <b>id_digest</b> is returned instead. */
699 node_get_verbose_nickname_by_id(const char *id_digest
,
700 char *verbose_name_out
)
702 const node_t
*node
= node_get_by_id(id_digest
);
704 verbose_name_out
[0] = '$';
705 base16_encode(verbose_name_out
+1, HEX_DIGEST_LEN
+1, id_digest
, DIGEST_LEN
);
707 node_get_verbose_nickname(node
, verbose_name_out
);
711 /** Return true iff it seems that <b>node</b> allows circuits to exit
712 * through it directlry from the client. */
714 node_allows_single_hop_exits(const node_t
*node
)
716 if (node
&& node
->ri
)
717 return node
->ri
->allow_single_hop_exits
;
722 /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
723 * actually permit anything to exit, or we don't know its exit policy */
725 node_exit_policy_rejects_all(const node_t
*node
)
727 if (node
->rejects_all
)
731 return node
->ri
->policy_is_reject_star
;
733 return node
->md
->exit_policy
== NULL
||
734 short_policy_is_reject_star(node
->md
->exit_policy
);
739 /** Return true iff the exit policy for <b>node</b> is such that we can treat
740 * rejecting an address of type <b>family</b> unexpectedly as a sign of that
743 node_exit_policy_is_exact(const node_t
*node
, sa_family_t family
)
745 if (family
== AF_UNSPEC
) {
746 return 1; /* Rejecting an address but not telling us what address
748 } else if (family
== AF_INET
) {
749 return node
->ri
!= NULL
;
750 } else if (family
== AF_INET6
) {
753 tor_fragile_assert();
757 /** Return list of tor_addr_port_t with all OR ports (in the sense IP
758 * addr + TCP port) for <b>node</b>. Caller must free all elements
759 * using tor_free() and free the list using smartlist_free().
761 * XXX this is potentially a memory fragmentation hog -- if on
762 * critical path consider the option of having the caller allocate the
766 node_get_all_orports(const node_t
*node
)
768 smartlist_t
*sl
= smartlist_new();
770 if (node
->ri
!= NULL
) {
771 if (node
->ri
->addr
!= 0) {
772 tor_addr_port_t
*ap
= tor_malloc(sizeof(tor_addr_port_t
));
773 tor_addr_from_ipv4h(&ap
->addr
, node
->ri
->addr
);
774 ap
->port
= node
->ri
->or_port
;
775 smartlist_add(sl
, ap
);
777 if (!tor_addr_is_null(&node
->ri
->ipv6_addr
)) {
778 tor_addr_port_t
*ap
= tor_malloc(sizeof(tor_addr_port_t
));
779 tor_addr_copy(&ap
->addr
, &node
->ri
->ipv6_addr
);
780 ap
->port
= node
->ri
->or_port
;
781 smartlist_add(sl
, ap
);
783 } else if (node
->rs
!= NULL
) {
784 tor_addr_port_t
*ap
= tor_malloc(sizeof(tor_addr_port_t
));
785 tor_addr_from_ipv4h(&ap
->addr
, node
->rs
->addr
);
786 ap
->port
= node
->rs
->or_port
;
787 smartlist_add(sl
, ap
);
793 /** Wrapper around node_get_prim_orport for backward
796 node_get_addr(const node_t
*node
, tor_addr_t
*addr_out
)
799 node_get_prim_orport(node
, &ap
);
800 tor_addr_copy(addr_out
, &ap
.addr
);
803 /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
804 * seem to have one. */
806 node_get_prim_addr_ipv4h(const node_t
*node
)
809 return node
->ri
->addr
;
810 } else if (node
->rs
) {
811 return node
->rs
->addr
;
816 /** Copy a string representation of an IP address for <b>node</b> into
817 * the <b>len</b>-byte buffer at <b>buf</b>. */
819 node_get_address_string(const node_t
*node
, char *buf
, size_t len
)
822 strlcpy(buf
, fmt_addr32(node
->ri
->addr
), len
);
823 } else if (node
->rs
) {
825 tor_addr_from_ipv4h(&addr
, node
->rs
->addr
);
826 tor_addr_to_str(buf
, &addr
, len
, 0);
832 /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
835 node_get_declared_uptime(const node_t
*node
)
838 return node
->ri
->uptime
;
843 /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
845 node_get_platform(const node_t
*node
)
847 /* If we wanted, we could record the version in the routerstatus_t, since
848 * the consensus lists it. We don't, though, so this function just won't
849 * work with microdescriptors. */
851 return node
->ri
->platform
;
856 /** Return <b>node</b>'s time of publication, or 0 if we don't have one. */
858 node_get_published_on(const node_t
*node
)
861 return node
->ri
->cache_info
.published_on
;
866 /** Return true iff <b>node</b> is one representing this router. */
868 node_is_me(const node_t
*node
)
870 return router_digest_is_me(node
->identity
);
873 /** Return <b>node</b> declared family (as a list of names), or NULL if
874 * the node didn't declare a family. */
876 node_get_declared_family(const node_t
*node
)
878 if (node
->ri
&& node
->ri
->declared_family
)
879 return node
->ri
->declared_family
;
880 else if (node
->md
&& node
->md
->family
)
881 return node
->md
->family
;
886 /** Return 1 if we prefer the IPv6 address and OR TCP port of
887 * <b>node</b>, else 0.
889 * We prefer the IPv6 address if the router has an IPv6 address and
890 * i) the node_t says that it prefers IPv6
892 * ii) the router has no IPv4 address. */
894 node_ipv6_preferred(const node_t
*node
)
896 tor_addr_port_t ipv4_addr
;
897 node_assert_ok(node
);
899 if (node
->ipv6_preferred
|| node_get_prim_orport(node
, &ipv4_addr
)) {
901 return !tor_addr_is_null(&node
->ri
->ipv6_addr
);
903 return !tor_addr_is_null(&node
->md
->ipv6_addr
);
905 return !tor_addr_is_null(&node
->rs
->ipv6_addr
);
910 /** Copy the primary (IPv4) OR port (IP address and TCP port) for
911 * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
912 * port was copied, else return non-zero.*/
914 node_get_prim_orport(const node_t
*node
, tor_addr_port_t
*ap_out
)
916 node_assert_ok(node
);
920 if (node
->ri
->addr
== 0 || node
->ri
->or_port
== 0)
922 tor_addr_from_ipv4h(&ap_out
->addr
, node
->ri
->addr
);
923 ap_out
->port
= node
->ri
->or_port
;
927 if (node
->rs
->addr
== 0 || node
->rs
->or_port
== 0)
929 tor_addr_from_ipv4h(&ap_out
->addr
, node
->rs
->addr
);
930 ap_out
->port
= node
->rs
->or_port
;
936 /** Copy the preferred OR port (IP address and TCP port) for
937 * <b>node</b> into *<b>ap_out</b>. */
939 node_get_pref_orport(const node_t
*node
, tor_addr_port_t
*ap_out
)
941 const or_options_t
*options
= get_options();
944 /* Cheap implementation of config option ClientUseIPv6 -- simply
945 don't prefer IPv6 when ClientUseIPv6 is not set and we're not a
946 client running with bridges. See #4455 for more on this subject.
948 Note that this filter is too strict since we're hindering not
949 only clients! Erring on the safe side shouldn't be a problem
950 though. XXX move this check to where outgoing connections are
952 if ((options
->ClientUseIPv6
|| options
->UseBridges
) &&
953 node_ipv6_preferred(node
)) {
954 node_get_pref_ipv6_orport(node
, ap_out
);
956 node_get_prim_orport(node
, ap_out
);
960 /** Copy the preferred IPv6 OR port (IP address and TCP port) for
961 * <b>node</b> into *<b>ap_out</b>. */
963 node_get_pref_ipv6_orport(const node_t
*node
, tor_addr_port_t
*ap_out
)
965 node_assert_ok(node
);
968 /* We prefer the microdesc over a potential routerstatus here. They
969 are not being synchronised atm so there might be a chance that
970 they differ at some point, f.ex. when flipping
971 UseMicrodescriptors? -LN */
974 tor_addr_copy(&ap_out
->addr
, &node
->ri
->ipv6_addr
);
975 ap_out
->port
= node
->ri
->ipv6_orport
;
976 } else if (node
->md
) {
977 tor_addr_copy(&ap_out
->addr
, &node
->md
->ipv6_addr
);
978 ap_out
->port
= node
->md
->ipv6_orport
;
979 } else if (node
->rs
) {
980 tor_addr_copy(&ap_out
->addr
, &node
->rs
->ipv6_addr
);
981 ap_out
->port
= node
->rs
->ipv6_orport
;
985 /** Return true iff <b>node</b> has a curve25519 onion key. */
987 node_has_curve25519_onion_key(const node_t
*node
)
990 return node
->ri
->onion_curve25519_pkey
!= NULL
;
992 return node
->md
->onion_curve25519_pkey
!= NULL
;
997 /** Refresh the country code of <b>ri</b>. This function MUST be called on
998 * each router when the GeoIP database is reloaded, and on all new routers. */
1000 node_set_country(node_t
*node
)
1002 tor_addr_t addr
= TOR_ADDR_NULL
;
1006 tor_addr_from_ipv4h(&addr
, node
->rs
->addr
);
1008 tor_addr_from_ipv4h(&addr
, node
->ri
->addr
);
1010 node
->country
= geoip_get_country_by_addr(&addr
);
1013 /** Set the country code of all routers in the routerlist. */
1015 nodelist_refresh_countries(void)
1017 smartlist_t
*nodes
= nodelist_get_list();
1018 SMARTLIST_FOREACH(nodes
, node_t
*, node
,
1019 node_set_country(node
));
1022 /** Return true iff router1 and router2 have similar enough network addresses
1023 * that we should treat them as being in the same family */
1025 addrs_in_same_network_family(const tor_addr_t
*a1
,
1026 const tor_addr_t
*a2
)
1028 return 0 == tor_addr_compare_masked(a1
, a2
, 16, CMP_SEMANTIC
);
1031 /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
1032 * (case-insensitive), or if <b>node's</b> identity key digest
1033 * matches a hexadecimal value stored in <b>nickname</b>. Return
1034 * false otherwise. */
1036 node_nickname_matches(const node_t
*node
, const char *nickname
)
1038 const char *n
= node_get_nickname(node
);
1039 if (n
&& nickname
[0]!='$' && !strcasecmp(n
, nickname
))
1041 return hex_digest_nickname_matches(nickname
,
1044 node_is_named(node
));
1047 /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
1049 node_in_nickname_smartlist(const smartlist_t
*lst
, const node_t
*node
)
1052 SMARTLIST_FOREACH(lst
, const char *, name
, {
1053 if (node_nickname_matches(node
, name
))
1059 /** Return true iff r1 and r2 are in the same family, but not the same
1062 nodes_in_same_family(const node_t
*node1
, const node_t
*node2
)
1064 const or_options_t
*options
= get_options();
1066 /* Are they in the same family because of their addresses? */
1067 if (options
->EnforceDistinctSubnets
) {
1069 node_get_addr(node1
, &a1
);
1070 node_get_addr(node2
, &a2
);
1071 if (addrs_in_same_network_family(&a1
, &a2
))
1075 /* Are they in the same family because the agree they are? */
1077 const smartlist_t
*f1
, *f2
;
1078 f1
= node_get_declared_family(node1
);
1079 f2
= node_get_declared_family(node2
);
1081 node_in_nickname_smartlist(f1
, node2
) &&
1082 node_in_nickname_smartlist(f2
, node1
))
1086 /* Are they in the same option because the user says they are? */
1087 if (options
->NodeFamilySets
) {
1088 SMARTLIST_FOREACH(options
->NodeFamilySets
, const routerset_t
*, rs
, {
1089 if (routerset_contains_node(rs
, node1
) &&
1090 routerset_contains_node(rs
, node2
))
1099 * Add all the family of <b>node</b>, including <b>node</b> itself, to
1100 * the smartlist <b>sl</b>.
1102 * This is used to make sure we don't pick siblings in a single path, or
1103 * pick more than one relay from a family for our entry guard list.
1104 * Note that a node may be added to <b>sl</b> more than once if it is
1105 * part of <b>node</b>'s family for more than one reason.
1108 nodelist_add_node_and_family(smartlist_t
*sl
, const node_t
*node
)
1110 const smartlist_t
*all_nodes
= nodelist_get_list();
1111 const smartlist_t
*declared_family
;
1112 const or_options_t
*options
= get_options();
1116 declared_family
= node_get_declared_family(node
);
1118 /* Let's make sure that we have the node itself, if it's a real node. */
1120 const node_t
*real_node
= node_get_by_id(node
->identity
);
1122 smartlist_add(sl
, (node_t
*)real_node
);
1125 /* First, add any nodes with similar network addresses. */
1126 if (options
->EnforceDistinctSubnets
) {
1127 tor_addr_t node_addr
;
1128 node_get_addr(node
, &node_addr
);
1130 SMARTLIST_FOREACH_BEGIN(all_nodes
, const node_t
*, node2
) {
1132 node_get_addr(node2
, &a
);
1133 if (addrs_in_same_network_family(&a
, &node_addr
))
1134 smartlist_add(sl
, (void*)node2
);
1135 } SMARTLIST_FOREACH_END(node2
);
1138 /* Now, add all nodes in the declared_family of this node, if they
1139 * also declare this node to be in their family. */
1140 if (declared_family
) {
1141 /* Add every r such that router declares familyness with node, and node
1142 * declares familyhood with router. */
1143 SMARTLIST_FOREACH_BEGIN(declared_family
, const char *, name
) {
1144 const node_t
*node2
;
1145 const smartlist_t
*family2
;
1146 if (!(node2
= node_get_by_nickname(name
, 0)))
1148 if (!(family2
= node_get_declared_family(node2
)))
1150 SMARTLIST_FOREACH_BEGIN(family2
, const char *, name2
) {
1151 if (node_nickname_matches(node
, name2
)) {
1152 smartlist_add(sl
, (void*)node2
);
1155 } SMARTLIST_FOREACH_END(name2
);
1156 } SMARTLIST_FOREACH_END(name
);
1159 /* If the user declared any families locally, honor those too. */
1160 if (options
->NodeFamilySets
) {
1161 SMARTLIST_FOREACH(options
->NodeFamilySets
, const routerset_t
*, rs
, {
1162 if (routerset_contains_node(rs
, node
)) {
1163 routerset_get_all_nodes(sl
, rs
, NULL
, 0);
1169 /** Find a router that's up, that has this IP address, and
1170 * that allows exit to this address:port, or return NULL if there
1172 * Don't exit enclave to excluded relays -- it wouldn't actually
1173 * hurt anything, but this way there are fewer confused users.
1176 router_find_exact_exit_enclave(const char *address
, uint16_t port
)
1181 const or_options_t
*options
= get_options();
1183 if (!tor_inet_aton(address
, &in
))
1184 return NULL
; /* it's not an IP already */
1185 addr
= ntohl(in
.s_addr
);
1187 tor_addr_from_ipv4h(&a
, addr
);
1189 SMARTLIST_FOREACH(nodelist_get_list(), const node_t
*, node
, {
1190 if (node_get_addr_ipv4h(node
) == addr
&&
1192 compare_tor_addr_to_node_policy(&a
, port
, node
) ==
1193 ADDR_POLICY_ACCEPTED
&&
1194 !routerset_contains_node(options
->ExcludeExitNodesUnion_
, node
))
1200 /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
1201 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
1202 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
1204 * If <b>need_guard</b>, we require that the router is a possible entry guard.
1207 node_is_unreliable(const node_t
*node
, int need_uptime
,
1208 int need_capacity
, int need_guard
)
1210 if (need_uptime
&& !node
->is_stable
)
1212 if (need_capacity
&& !node
->is_fast
)
1214 if (need_guard
&& !node
->is_possible_guard
)
1219 /** Return 1 if all running sufficiently-stable routers we can use will reject
1220 * addr:port. Return 0 if any might accept it. */
1222 router_exit_policy_all_nodes_reject(const tor_addr_t
*addr
, uint16_t port
,
1225 addr_policy_result_t r
;
1227 SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t
*, node
) {
1228 if (node
->is_running
&&
1229 !node_is_unreliable(node
, need_uptime
, 0, 0)) {
1231 r
= compare_tor_addr_to_node_policy(addr
, port
, node
);
1233 if (r
!= ADDR_POLICY_REJECTED
&& r
!= ADDR_POLICY_PROBABLY_REJECTED
)
1234 return 0; /* this one could be ok. good enough. */
1236 } SMARTLIST_FOREACH_END(node
);
1237 return 1; /* all will reject. */
1240 /** Mark the router with ID <b>digest</b> as running or non-running
1241 * in our routerlist. */
1243 router_set_status(const char *digest
, int up
)
1248 SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
1250 if (tor_memeq(d
->digest
, digest
, DIGEST_LEN
))
1251 d
->is_running
= up
);
1253 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
1255 if (tor_memeq(d
->digest
, digest
, DIGEST_LEN
))
1256 d
->is_running
= up
);
1258 node
= node_get_mutable_by_id(digest
);
1261 log_debug(LD_DIR
,"Marking router %s as %s.",
1262 node_describe(node
), up
? "up" : "down");
1264 if (!up
&& node_is_me(node
) && !net_is_disabled())
1265 log_warn(LD_NET
, "We just marked ourself as down. Are your external "
1266 "addresses reachable?");
1268 if (bool_neq(node
->is_running
, up
))
1269 router_dir_info_changed();
1271 node
->is_running
= up
;
1275 /** True iff, the last time we checked whether we had enough directory info
1276 * to build circuits, the answer was "yes". If there are no exits in the
1277 * consensus, we act as if we have 100% of the exit directory info. */
1278 static int have_min_dir_info
= 0;
1280 /** Does the consensus contain nodes that can exit? */
1281 static consensus_path_type_t have_consensus_path
= CONSENSUS_PATH_UNKNOWN
;
1283 /** True iff enough has changed since the last time we checked whether we had
1284 * enough directory info to build circuits that our old answer can no longer
1286 static int need_to_update_have_min_dir_info
= 1;
1287 /** String describing what we're missing before we have enough directory
1289 static char dir_info_status
[512] = "";
1291 /** Return true iff we have enough consensus information to
1292 * start building circuits. Right now, this means "a consensus that's
1293 * less than a day old, and at least 60% of router descriptors (configurable),
1294 * weighted by bandwidth. Treat the exit fraction as 100% if there are
1295 * no exits in the consensus."
1296 * To obtain the final weighted bandwidth, we multiply the
1297 * weighted bandwidth fraction for each position (guard, middle, exit). */
1299 router_have_minimum_dir_info(void)
1301 static int logged_delay
=0;
1302 const char *delay_fetches_msg
= NULL
;
1303 if (should_delay_dir_fetches(get_options(), &delay_fetches_msg
)) {
1305 log_notice(LD_DIR
, "Delaying directory fetches: %s", delay_fetches_msg
);
1307 strlcpy(dir_info_status
, delay_fetches_msg
, sizeof(dir_info_status
));
1310 logged_delay
= 0; /* reset it if we get this far */
1312 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info
)) {
1313 update_router_have_minimum_dir_info();
1316 return have_min_dir_info
;
1319 /** Set to CONSENSUS_PATH_EXIT if there is at least one exit node
1320 * in the consensus. We update this flag in compute_frac_paths_available if
1321 * there is at least one relay that has an Exit flag in the consensus.
1322 * Used to avoid building exit circuits when they will almost certainly fail.
1323 * Set to CONSENSUS_PATH_INTERNAL if there are no exits in the consensus.
1324 * (This situation typically occurs during bootstrap of a test network.)
1325 * Set to CONSENSUS_PATH_UNKNOWN if we have never checked, or have
1326 * reason to believe our last known value was invalid or has expired.
1327 * If we're in a network with TestingDirAuthVoteExit set,
1328 * this can cause router_have_consensus_path() to be set to
1329 * CONSENSUS_PATH_EXIT, even if there are no nodes with accept exit policies.
1331 consensus_path_type_t
1332 router_have_consensus_path(void)
1334 return have_consensus_path
;
1337 /** Called when our internal view of the directory has changed. This can be
1338 * when the authorities change, networkstatuses change, the list of routerdescs
1339 * changes, or number of running routers changes.
1342 router_dir_info_changed(void)
1344 need_to_update_have_min_dir_info
= 1;
1345 rend_hsdir_routers_changed();
1348 /** Return a string describing what we're missing before we have enough
1349 * directory info. */
1351 get_dir_info_status_string(void)
1353 return dir_info_status
;
1356 /** Iterate over the servers listed in <b>consensus</b>, and count how many of
1357 * them seem like ones we'd use (store this in *<b>num_usable</b>), and how
1358 * many of <em>those</em> we have descriptors for (store this in
1359 * *<b>num_present</b>).
1361 * If <b>in_set</b> is non-NULL, only consider those routers in <b>in_set</b>.
1362 * If <b>exit_only</b> is USABLE_DESCRIPTOR_EXIT_ONLY, only consider nodes
1363 * with the Exit flag.
1364 * If *<b>descs_out</b> is present, add a node_t for each usable descriptor
1368 count_usable_descriptors(int *num_present
, int *num_usable
,
1369 smartlist_t
*descs_out
,
1370 const networkstatus_t
*consensus
,
1371 const or_options_t
*options
, time_t now
,
1372 routerset_t
*in_set
,
1373 usable_descriptor_t exit_only
)
1375 const int md
= (consensus
->flavor
== FLAV_MICRODESC
);
1376 *num_present
= 0, *num_usable
= 0;
1378 SMARTLIST_FOREACH_BEGIN(consensus
->routerstatus_list
, routerstatus_t
*, rs
)
1380 const node_t
*node
= node_get_by_id(rs
->identity_digest
);
1382 continue; /* This would be a bug: every entry in the consensus is
1383 * supposed to have a node. */
1384 if (exit_only
== USABLE_DESCRIPTOR_EXIT_ONLY
&& ! rs
->is_exit
)
1386 if (in_set
&& ! routerset_contains_routerstatus(in_set
, rs
, -1))
1388 if (client_would_use_router(rs
, now
, options
)) {
1389 const char * const digest
= rs
->descriptor_digest
;
1391 ++*num_usable
; /* the consensus says we want it. */
1393 present
= NULL
!= microdesc_cache_lookup_by_digest256(NULL
, digest
);
1395 present
= NULL
!= router_get_by_descriptor_digest(digest
);
1397 /* we have the descriptor listed in the consensus. */
1401 smartlist_add(descs_out
, (node_t
*)node
);
1404 SMARTLIST_FOREACH_END(rs
);
1406 log_debug(LD_DIR
, "%d usable, %d present (%s%s).",
1407 *num_usable
, *num_present
,
1408 md
? "microdesc" : "desc",
1409 exit_only
== USABLE_DESCRIPTOR_EXIT_ONLY
? " exits" : "s");
1412 /** Return an estimate of which fraction of usable paths through the Tor
1413 * network we have available for use. Count how many routers seem like ones
1414 * we'd use (store this in *<b>num_usable_out</b>), and how many of
1415 * <em>those</em> we have descriptors for (store this in
1416 * *<b>num_present_out</b>.)
1418 * If **<b>status_out</b> is present, allocate a new string and print the
1419 * available percentages of guard, middle, and exit nodes to it, noting
1420 * whether there are exits in the consensus.
1421 * If there are no guards in the consensus,
1422 * we treat the exit fraction as 100%.
1425 compute_frac_paths_available(const networkstatus_t
*consensus
,
1426 const or_options_t
*options
, time_t now
,
1427 int *num_present_out
, int *num_usable_out
,
1430 smartlist_t
*guards
= smartlist_new();
1431 smartlist_t
*mid
= smartlist_new();
1432 smartlist_t
*exits
= smartlist_new();
1433 double f_guard
, f_mid
, f_exit
;
1434 double f_path
= 0.0;
1435 /* Used to determine whether there are any exits in the consensus */
1437 /* Used to determine whether there are any exits with descriptors */
1439 const int authdir
= authdir_mode_v3(options
);
1441 count_usable_descriptors(num_present_out
, num_usable_out
,
1442 mid
, consensus
, options
, now
, NULL
,
1443 USABLE_DESCRIPTOR_ALL
);
1444 if (options
->EntryNodes
) {
1445 count_usable_descriptors(&np
, &nu
, guards
, consensus
, options
, now
,
1446 options
->EntryNodes
, USABLE_DESCRIPTOR_ALL
);
1448 SMARTLIST_FOREACH(mid
, const node_t
*, node
, {
1450 if (node
->rs
&& node
->rs
->is_possible_guard
)
1451 smartlist_add(guards
, (node_t
*)node
);
1453 if (node
->is_possible_guard
)
1454 smartlist_add(guards
, (node_t
*)node
);
1459 /* All nodes with exit flag
1460 * If we're in a network with TestingDirAuthVoteExit set,
1461 * this can cause false positives on have_consensus_path,
1462 * incorrectly setting it to CONSENSUS_PATH_EXIT. This is
1463 * an unavoidable feature of forcing authorities to declare
1464 * certain nodes as exits.
1466 count_usable_descriptors(&np
, &nu
, exits
, consensus
, options
, now
,
1467 NULL
, USABLE_DESCRIPTOR_EXIT_ONLY
);
1469 "%s: %d present, %d usable",
1474 /* We need at least 1 exit present in the consensus to consider
1475 * building exit paths */
1476 /* Update our understanding of whether the consensus has exits */
1477 consensus_path_type_t old_have_consensus_path
= have_consensus_path
;
1478 have_consensus_path
= ((nu
> 0) ?
1479 CONSENSUS_PATH_EXIT
:
1480 CONSENSUS_PATH_INTERNAL
);
1482 if (have_consensus_path
== CONSENSUS_PATH_INTERNAL
1483 && old_have_consensus_path
!= have_consensus_path
) {
1485 "The current consensus has no exit nodes. "
1486 "Tor can only build internal paths, "
1487 "such as paths to hidden services.");
1489 /* However, exit nodes can reachability self-test using this consensus,
1490 * join the network, and appear in a later consensus. This will allow
1491 * the network to build exit paths, such as paths for world wide web
1492 * browsing (as distinct from hidden service web browsing). */
1495 f_guard
= frac_nodes_with_descriptors(guards
, WEIGHT_FOR_GUARD
);
1496 f_mid
= frac_nodes_with_descriptors(mid
, WEIGHT_FOR_MID
);
1497 f_exit
= frac_nodes_with_descriptors(exits
, WEIGHT_FOR_EXIT
);
1500 "f_guard: %.2f, f_mid: %.2f, f_exit: %.2f",
1505 smartlist_free(guards
);
1506 smartlist_free(mid
);
1507 smartlist_free(exits
);
1509 if (options
->ExitNodes
) {
1510 double f_myexit
, f_myexit_unflagged
;
1511 smartlist_t
*myexits
= smartlist_new();
1512 smartlist_t
*myexits_unflagged
= smartlist_new();
1514 /* All nodes with exit flag in ExitNodes option */
1515 count_usable_descriptors(&np
, &nu
, myexits
, consensus
, options
, now
,
1516 options
->ExitNodes
, USABLE_DESCRIPTOR_EXIT_ONLY
);
1518 "%s: %d present, %d usable",
1523 /* Now compute the nodes in the ExitNodes option where which we don't know
1524 * what their exit policy is, or we know it permits something. */
1525 count_usable_descriptors(&np
, &nu
, myexits_unflagged
,
1526 consensus
, options
, now
,
1527 options
->ExitNodes
, USABLE_DESCRIPTOR_ALL
);
1529 "%s: %d present, %d usable",
1530 "myexits_unflagged (initial)",
1534 SMARTLIST_FOREACH_BEGIN(myexits_unflagged
, const node_t
*, node
) {
1535 if (node_has_descriptor(node
) && node_exit_policy_rejects_all(node
)) {
1536 SMARTLIST_DEL_CURRENT(myexits_unflagged
, node
);
1537 /* this node is not actually an exit */
1539 /* this node is unusable as an exit */
1542 } SMARTLIST_FOREACH_END(node
);
1545 "%s: %d present, %d usable",
1546 "myexits_unflagged (final)",
1550 f_myexit
= frac_nodes_with_descriptors(myexits
,WEIGHT_FOR_EXIT
);
1552 frac_nodes_with_descriptors(myexits_unflagged
,WEIGHT_FOR_EXIT
);
1555 "f_exit: %.2f, f_myexit: %.2f, f_myexit_unflagged: %.2f",
1558 f_myexit_unflagged
);
1560 /* If our ExitNodes list has eliminated every possible Exit node, and there
1561 * were some possible Exit nodes, then instead consider nodes that permit
1562 * exiting to some ports. */
1563 if (smartlist_len(myexits
) == 0 &&
1564 smartlist_len(myexits_unflagged
)) {
1565 f_myexit
= f_myexit_unflagged
;
1568 smartlist_free(myexits
);
1569 smartlist_free(myexits_unflagged
);
1571 /* This is a tricky point here: we don't want to make it easy for a
1572 * directory to trickle exits to us until it learns which exits we have
1573 * configured, so require that we have a threshold both of total exits
1574 * and usable exits. */
1575 if (f_myexit
< f_exit
)
1579 /* if the consensus has no exits, treat the exit fraction as 100% */
1580 if (router_have_consensus_path() != CONSENSUS_PATH_EXIT
) {
1584 f_path
= f_guard
* f_mid
* f_exit
;
1587 tor_asprintf(status_out
,
1588 "%d%% of guards bw, "
1589 "%d%% of midpoint bw, and "
1590 "%d%% of exit bw%s = "
1595 (router_have_consensus_path() == CONSENSUS_PATH_EXIT
?
1597 " (no exits in consensus)"),
1603 /** We just fetched a new set of descriptors. Compute how far through
1604 * the "loading descriptors" bootstrapping phase we are, so we can inform
1605 * the controller of our progress. */
1607 count_loading_descriptors_progress(void)
1609 int num_present
= 0, num_usable
=0;
1610 time_t now
= time(NULL
);
1611 const or_options_t
*options
= get_options();
1612 const networkstatus_t
*consensus
=
1613 networkstatus_get_reasonably_live_consensus(now
,usable_consensus_flavor());
1614 double paths
, fraction
;
1617 return 0; /* can't count descriptors if we have no list of them */
1619 paths
= compute_frac_paths_available(consensus
, options
, now
,
1620 &num_present
, &num_usable
,
1623 fraction
= paths
/ get_frac_paths_needed_for_circs(options
,consensus
);
1625 return 0; /* it's not the number of descriptors holding us back */
1626 return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS
+ (int)
1627 (fraction
*(BOOTSTRAP_STATUS_CONN_OR
-1 -
1628 BOOTSTRAP_STATUS_LOADING_DESCRIPTORS
));
1631 /** Return the fraction of paths needed before we're willing to build
1632 * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
1634 get_frac_paths_needed_for_circs(const or_options_t
*options
,
1635 const networkstatus_t
*ns
)
1637 #define DFLT_PCT_USABLE_NEEDED 60
1638 if (options
->PathsNeededToBuildCircuits
>= 0.0) {
1639 return options
->PathsNeededToBuildCircuits
;
1641 return networkstatus_get_param(ns
, "min_paths_for_circs_pct",
1642 DFLT_PCT_USABLE_NEEDED
,
1647 /** Change the value of have_min_dir_info, setting it true iff we have enough
1648 * network and router information to build circuits. Clear the value of
1649 * need_to_update_have_min_dir_info. */
1651 update_router_have_minimum_dir_info(void)
1653 time_t now
= time(NULL
);
1655 const or_options_t
*options
= get_options();
1656 const networkstatus_t
*consensus
=
1657 networkstatus_get_reasonably_live_consensus(now
,usable_consensus_flavor());
1661 if (!networkstatus_get_latest_consensus())
1662 strlcpy(dir_info_status
, "We have no usable consensus.",
1663 sizeof(dir_info_status
));
1665 strlcpy(dir_info_status
, "We have no recent usable consensus.",
1666 sizeof(dir_info_status
));
1671 using_md
= consensus
->flavor
== FLAV_MICRODESC
;
1673 /* Check fraction of available paths */
1675 char *status
= NULL
;
1676 int num_present
=0, num_usable
=0;
1677 double paths
= compute_frac_paths_available(consensus
, options
, now
,
1678 &num_present
, &num_usable
,
1681 if (paths
< get_frac_paths_needed_for_circs(options
,consensus
)) {
1682 tor_snprintf(dir_info_status
, sizeof(dir_info_status
),
1683 "We need more %sdescriptors: we have %d/%d, and "
1684 "can only build %d%% of likely paths. (We have %s.)",
1685 using_md
?"micro":"", num_present
, num_usable
,
1686 (int)(paths
*100), status
);
1689 control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS
, 0);
1699 /* If paths have just become available in this update. */
1700 if (res
&& !have_min_dir_info
) {
1701 control_event_client_status(LOG_NOTICE
, "ENOUGH_DIR_INFO");
1702 if (control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR
, 0) == 0) {
1704 "We now have enough directory information to build circuits.");
1708 /* If paths have just become unavailable in this update. */
1709 if (!res
&& have_min_dir_info
) {
1710 int quiet
= directory_too_idle_to_fetch_descriptors(options
, now
);
1711 tor_log(quiet
? LOG_INFO
: LOG_NOTICE
, LD_DIR
,
1712 "Our directory information is no longer up-to-date "
1713 "enough to build circuits: %s", dir_info_status
);
1715 /* a) make us log when we next complete a circuit, so we know when Tor
1716 * is back up and usable, and b) disable some activities that Tor
1717 * should only do while circuits are working, like reachability tests
1718 * and fetching bridge descriptors only over circuits. */
1719 note_that_we_maybe_cant_complete_circuits();
1720 have_consensus_path
= CONSENSUS_PATH_UNKNOWN
;
1721 control_event_client_status(LOG_NOTICE
, "NOT_ENOUGH_DIR_INFO");
1723 have_min_dir_info
= res
;
1724 need_to_update_have_min_dir_info
= 0;