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-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
10 * \brief Structures and functions for tracking what we know about the routers
11 * on the Tor network, and correlating information from networkstatus,
12 * routerinfo, and microdescs.
14 * The key structure here is node_t: that's the canonical way to refer
15 * to a Tor relay that we might want to build a circuit through. Every
16 * node_t has either a routerinfo_t, or a routerstatus_t from the current
17 * networkstatus consensus. If it has a routerstatus_t, it will also
18 * need to have a microdesc_t before you can use it for circuits.
20 * The nodelist_t is a global singleton that maps identities to node_t
21 * objects. Access them with the node_get_*() functions. The nodelist_t
22 * is maintained by calls throughout the codebase
24 * Generally, other code should not have to reach inside a node_t to
25 * see what information it has. Instead, you should call one of the
26 * many accessor functions that works on a generic node_t. If there
27 * isn't one that does what you need, it's better to make such a function,
30 * For historical reasons, some of the functions that select a node_t
31 * from the list of all usable node_t objects are in the routerlist.c
32 * module, since they originally selected a routerinfo_t. (TODO: They
35 * (TODO: Perhaps someday we should abstract the remaining ways of
36 * talking about a relay to also be node_t instances. Those would be
37 * routerstatus_t as used for directory requests, and dir_server_t as
38 * used for authorities and fallback directories.)
41 #define NODELIST_PRIVATE
48 #include "entrynodes.h"
50 #include "hs_common.h"
51 #include "hs_client.h"
53 #include "microdesc.h"
54 #include "networkstatus.h"
58 #include "rendservice.h"
60 #include "routerlist.h"
61 #include "routerparse.h"
62 #include "routerset.h"
67 static void nodelist_drop_node(node_t
*node
, int remove_from_ht
);
68 static void node_free(node_t
*node
);
70 /** count_usable_descriptors counts descriptors with these flag(s)
73 /* All descriptors regardless of flags */
74 USABLE_DESCRIPTOR_ALL
= 0,
75 /* Only descriptors with the Exit flag */
76 USABLE_DESCRIPTOR_EXIT_ONLY
= 1
77 } usable_descriptor_t
;
78 static void count_usable_descriptors(int *num_present
,
80 smartlist_t
*descs_out
,
81 const networkstatus_t
*consensus
,
84 usable_descriptor_t exit_only
);
85 static void update_router_have_minimum_dir_info(void);
86 static double get_frac_paths_needed_for_circs(const or_options_t
*options
,
87 const networkstatus_t
*ns
);
89 /** A nodelist_t holds a node_t object for every router we're "willing to use
90 * for something". Specifically, it should hold a node_t for every node that
91 * is currently in the routerlist, or currently in the consensus we're using.
93 typedef struct nodelist_t
{
94 /* A list of all the nodes. */
96 /* Hash table to map from node ID digest to node. */
97 HT_HEAD(nodelist_map
, node_t
) nodes_by_id
;
98 /* Hash table to map from node Ed25519 ID to node.
100 * Whenever a node's routerinfo or microdescriptor is about to change,
101 * you should remove it from this map with node_remove_from_ed25519_map().
102 * Whenever a node's routerinfo or microdescriptor has just chaned,
103 * you should add it to this map with node_add_to_ed25519_map().
105 HT_HEAD(nodelist_ed_map
, node_t
) nodes_by_ed_id
;
108 static inline unsigned int
109 node_id_hash(const node_t
*node
)
111 return (unsigned) siphash24g(node
->identity
, DIGEST_LEN
);
114 static inline unsigned int
115 node_id_eq(const node_t
*node1
, const node_t
*node2
)
117 return tor_memeq(node1
->identity
, node2
->identity
, DIGEST_LEN
);
120 HT_PROTOTYPE(nodelist_map
, node_t
, ht_ent
, node_id_hash
, node_id_eq
)
121 HT_GENERATE2(nodelist_map
, node_t
, ht_ent
, node_id_hash
, node_id_eq
,
122 0.6, tor_reallocarray_
, tor_free_
)
124 static inline unsigned int
125 node_ed_id_hash(const node_t
*node
)
127 return (unsigned) siphash24g(node
->ed25519_id
.pubkey
, ED25519_PUBKEY_LEN
);
130 static inline unsigned int
131 node_ed_id_eq(const node_t
*node1
, const node_t
*node2
)
133 return ed25519_pubkey_eq(&node1
->ed25519_id
, &node2
->ed25519_id
);
136 HT_PROTOTYPE(nodelist_ed_map
, node_t
, ed_ht_ent
, node_ed_id_hash
,
138 HT_GENERATE2(nodelist_ed_map
, node_t
, ed_ht_ent
, node_ed_id_hash
,
139 node_ed_id_eq
, 0.6, tor_reallocarray_
, tor_free_
)
141 /** The global nodelist. */
142 static nodelist_t
*the_nodelist
=NULL
;
144 /** Create an empty nodelist if we haven't done so already. */
148 if (PREDICT_UNLIKELY(the_nodelist
== NULL
)) {
149 the_nodelist
= tor_malloc_zero(sizeof(nodelist_t
));
150 HT_INIT(nodelist_map
, &the_nodelist
->nodes_by_id
);
151 HT_INIT(nodelist_ed_map
, &the_nodelist
->nodes_by_ed_id
);
152 the_nodelist
->nodes
= smartlist_new();
156 /** As node_get_by_id, but returns a non-const pointer */
158 node_get_mutable_by_id(const char *identity_digest
)
160 node_t search
, *node
;
161 if (PREDICT_UNLIKELY(the_nodelist
== NULL
))
164 memcpy(&search
.identity
, identity_digest
, DIGEST_LEN
);
165 node
= HT_FIND(nodelist_map
, &the_nodelist
->nodes_by_id
, &search
);
169 /** As node_get_by_ed25519_id, but returns a non-const pointer */
171 node_get_mutable_by_ed25519_id(const ed25519_public_key_t
*ed_id
)
173 node_t search
, *node
;
174 if (PREDICT_UNLIKELY(the_nodelist
== NULL
))
176 if (BUG(ed_id
== NULL
) || BUG(ed25519_public_key_is_zero(ed_id
)))
179 memcpy(&search
.ed25519_id
, ed_id
, sizeof(search
.ed25519_id
));
180 node
= HT_FIND(nodelist_ed_map
, &the_nodelist
->nodes_by_ed_id
, &search
);
184 /** Return the node_t whose identity is <b>identity_digest</b>, or NULL
185 * if no such node exists. */
186 MOCK_IMPL(const node_t
*,
187 node_get_by_id
,(const char *identity_digest
))
189 return node_get_mutable_by_id(identity_digest
);
192 /** Return the node_t whose ed25519 identity is <b>ed_id</b>, or NULL
193 * if no such node exists. */
194 MOCK_IMPL(const node_t
*,
195 node_get_by_ed25519_id
,(const ed25519_public_key_t
*ed_id
))
197 return node_get_mutable_by_ed25519_id(ed_id
);
200 /** Internal: return the node_t whose identity_digest is
201 * <b>identity_digest</b>. If none exists, create a new one, add it to the
202 * nodelist, and return it.
204 * Requires that the nodelist be initialized.
207 node_get_or_create(const char *identity_digest
)
211 if ((node
= node_get_mutable_by_id(identity_digest
)))
214 node
= tor_malloc_zero(sizeof(node_t
));
215 memcpy(node
->identity
, identity_digest
, DIGEST_LEN
);
216 HT_INSERT(nodelist_map
, &the_nodelist
->nodes_by_id
, node
);
218 smartlist_add(the_nodelist
->nodes
, node
);
219 node
->nodelist_idx
= smartlist_len(the_nodelist
->nodes
) - 1;
220 node
->hsdir_index
= tor_malloc_zero(sizeof(hsdir_index_t
));
227 /** Remove <b>node</b> from the ed25519 map (if it present), and
228 * set its ed25519_id field to zero. */
230 node_remove_from_ed25519_map(node_t
*node
)
232 tor_assert(the_nodelist
);
235 if (ed25519_public_key_is_zero(&node
->ed25519_id
)) {
241 HT_FIND(nodelist_ed_map
, &the_nodelist
->nodes_by_ed_id
, node
);
242 if (BUG(search
!= node
)) {
243 goto clear_and_return
;
246 search
= HT_REMOVE(nodelist_ed_map
, &the_nodelist
->nodes_by_ed_id
, node
);
247 tor_assert(search
== node
);
251 memset(&node
->ed25519_id
, 0, sizeof(node
->ed25519_id
));
255 /** If <b>node</b> has an ed25519 id, and it is not already in the ed25519 id
256 * map, set its ed25519_id field, and add it to the ed25519 map.
259 node_add_to_ed25519_map(node_t
*node
)
261 tor_assert(the_nodelist
);
264 if (! ed25519_public_key_is_zero(&node
->ed25519_id
)) {
268 const ed25519_public_key_t
*key
= node_get_ed25519_id(node
);
274 memcpy(&node
->ed25519_id
, key
, sizeof(node
->ed25519_id
));
275 old
= HT_FIND(nodelist_ed_map
, &the_nodelist
->nodes_by_ed_id
, node
);
277 /* XXXX order matters here, and this may mean that authorities aren't
280 memset(&node
->ed25519_id
, 0, sizeof(node
->ed25519_id
));
284 HT_INSERT(nodelist_ed_map
, &the_nodelist
->nodes_by_ed_id
, node
);
288 /* For a given <b>node</b> for the consensus <b>ns</b>, set the hsdir index
289 * for the node, both current and next if possible. This can only fails if the
290 * node_t ed25519 identity key can't be found which would be a bug. */
292 node_set_hsdir_index(node_t
*node
, const networkstatus_t
*ns
)
294 time_t now
= approx_time();
295 const ed25519_public_key_t
*node_identity_pk
;
296 uint8_t *fetch_srv
= NULL
, *store_first_srv
= NULL
, *store_second_srv
= NULL
;
297 uint64_t next_time_period_num
, current_time_period_num
;
298 uint64_t fetch_tp
, store_first_tp
, store_second_tp
;
303 if (!networkstatus_is_live(ns
, now
)) {
304 static struct ratelim_t live_consensus_ratelim
= RATELIM_INIT(30 * 60);
305 log_fn_ratelim(&live_consensus_ratelim
, LOG_INFO
, LD_GENERAL
,
306 "Not setting hsdir index with a non-live consensus.");
310 node_identity_pk
= node_get_ed25519_id(node
);
311 if (node_identity_pk
== NULL
) {
312 log_debug(LD_GENERAL
, "ed25519 identity public key not found when "
313 "trying to build the hsdir indexes for node %s",
314 node_describe(node
));
318 /* Get the current and next time period number. */
319 current_time_period_num
= hs_get_time_period_num(0);
320 next_time_period_num
= hs_get_next_time_period_num(0);
322 /* We always use the current time period for fetching descs */
323 fetch_tp
= current_time_period_num
;
325 /* Now extract the needed SRVs and time periods for building hsdir indices */
326 if (hs_in_period_between_tp_and_srv(ns
, now
)) {
327 fetch_srv
= hs_get_current_srv(fetch_tp
, ns
);
329 store_first_tp
= hs_get_previous_time_period_num(0);
330 store_second_tp
= current_time_period_num
;
332 fetch_srv
= hs_get_previous_srv(fetch_tp
, ns
);
334 store_first_tp
= current_time_period_num
;
335 store_second_tp
= next_time_period_num
;
338 /* We always use the old SRV for storing the first descriptor and the latest
339 * SRV for storing the second descriptor */
340 store_first_srv
= hs_get_previous_srv(store_first_tp
, ns
);
341 store_second_srv
= hs_get_current_srv(store_second_tp
, ns
);
343 /* Build the fetch index. */
344 hs_build_hsdir_index(node_identity_pk
, fetch_srv
, fetch_tp
,
345 node
->hsdir_index
->fetch
);
347 /* If we are in the time segment between SRV#N and TP#N, the fetch index is
348 the same as the first store index */
349 if (!hs_in_period_between_tp_and_srv(ns
, now
)) {
350 memcpy(node
->hsdir_index
->store_first
, node
->hsdir_index
->fetch
,
351 sizeof(node
->hsdir_index
->store_first
));
353 hs_build_hsdir_index(node_identity_pk
, store_first_srv
, store_first_tp
,
354 node
->hsdir_index
->store_first
);
357 /* If we are in the time segment between TP#N and SRV#N+1, the fetch index is
358 the same as the second store index */
359 if (hs_in_period_between_tp_and_srv(ns
, now
)) {
360 memcpy(node
->hsdir_index
->store_second
, node
->hsdir_index
->fetch
,
361 sizeof(node
->hsdir_index
->store_second
));
363 hs_build_hsdir_index(node_identity_pk
, store_second_srv
, store_second_tp
,
364 node
->hsdir_index
->store_second
);
369 tor_free(store_first_srv
);
370 tor_free(store_second_srv
);
374 /** Recompute all node hsdir indices. */
376 nodelist_recompute_all_hsdir_indices(void)
378 networkstatus_t
*consensus
;
383 /* Get a live consensus. Abort if not found */
384 consensus
= networkstatus_get_live_consensus(approx_time());
389 /* Recompute all hsdir indices */
390 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
391 node_set_hsdir_index(node
, consensus
);
392 } SMARTLIST_FOREACH_END(node
);
395 /** Called when a node's address changes. */
397 node_addrs_changed(node_t
*node
)
399 node
->last_reachable
= node
->last_reachable6
= 0;
403 /** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
404 * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
405 * to the previous routerinfo.
408 nodelist_set_routerinfo(routerinfo_t
*ri
, routerinfo_t
**ri_old_out
)
411 const char *id_digest
;
416 id_digest
= ri
->cache_info
.identity_digest
;
417 node
= node_get_or_create(id_digest
);
419 node_remove_from_ed25519_map(node
);
422 if (!routers_have_same_or_addrs(node
->ri
, ri
)) {
423 node_addrs_changed(node
);
427 *ri_old_out
= node
->ri
;
434 node_add_to_ed25519_map(node
);
436 if (node
->country
== -1)
437 node_set_country(node
);
439 if (authdir_mode(get_options()) && !had_router
) {
440 const char *discard
=NULL
;
441 uint32_t status
= dirserv_router_get_status(ri
, &discard
, LOG_INFO
);
442 dirserv_set_node_flags_from_authoritative_status(node
, status
);
445 /* Setting the HSDir index requires the ed25519 identity key which can
446 * only be found either in the ri or md. This is why this is called here.
447 * Only nodes supporting HSDir=2 protocol version needs this index. */
448 if (node
->rs
&& node
->rs
->supports_v3_hsdir
) {
449 node_set_hsdir_index(node
,
450 networkstatus_get_latest_consensus());
456 /** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
458 * Called when a new microdesc has arrived and the usable consensus flavor
462 nodelist_add_microdesc(microdesc_t
*md
)
464 networkstatus_t
*ns
=
465 networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC
);
466 const routerstatus_t
*rs
;
472 /* Microdescriptors don't carry an identity digest, so we need to figure
473 * it out by looking up the routerstatus. */
474 rs
= router_get_consensus_status_by_descriptor_digest(ns
, md
->digest
);
477 node
= node_get_mutable_by_id(rs
->identity_digest
);
479 node_remove_from_ed25519_map(node
);
481 node
->md
->held_by_nodes
--;
485 /* Setting the HSDir index requires the ed25519 identity key which can
486 * only be found either in the ri or md. This is why this is called here.
487 * Only nodes supporting HSDir=2 protocol version needs this index. */
488 if (rs
->supports_v3_hsdir
) {
489 node_set_hsdir_index(node
, ns
);
491 node_add_to_ed25519_map(node
);
497 /** Tell the nodelist that the current usable consensus is <b>ns</b>.
498 * This makes the nodelist change all of the routerstatus entries for
499 * the nodes, drop nodes that no longer have enough info to get used,
500 * and grab microdescriptors into nodes as appropriate.
503 nodelist_set_consensus(networkstatus_t
*ns
)
505 const or_options_t
*options
= get_options();
506 int authdir
= authdir_mode_v3(options
);
509 if (ns
->flavor
== FLAV_MICRODESC
)
510 (void) get_microdesc_cache(); /* Make sure it exists first. */
512 SMARTLIST_FOREACH(the_nodelist
->nodes
, node_t
*, node
,
515 SMARTLIST_FOREACH_BEGIN(ns
->routerstatus_list
, routerstatus_t
*, rs
) {
516 node_t
*node
= node_get_or_create(rs
->identity_digest
);
518 if (ns
->flavor
== FLAV_MICRODESC
) {
519 if (node
->md
== NULL
||
520 tor_memneq(node
->md
->digest
,rs
->descriptor_digest
,DIGEST256_LEN
)) {
521 node_remove_from_ed25519_map(node
);
523 node
->md
->held_by_nodes
--;
524 node
->md
= microdesc_cache_lookup_by_digest256(NULL
,
525 rs
->descriptor_digest
);
527 node
->md
->held_by_nodes
++;
528 node_add_to_ed25519_map(node
);
532 if (rs
->supports_v3_hsdir
) {
533 node_set_hsdir_index(node
, ns
);
535 node_set_country(node
);
537 /* If we're not an authdir, believe others. */
539 node
->is_valid
= rs
->is_valid
;
540 node
->is_running
= rs
->is_flagged_running
;
541 node
->is_fast
= rs
->is_fast
;
542 node
->is_stable
= rs
->is_stable
;
543 node
->is_possible_guard
= rs
->is_possible_guard
;
544 node
->is_exit
= rs
->is_exit
;
545 node
->is_bad_exit
= rs
->is_bad_exit
;
546 node
->is_hs_dir
= rs
->is_hs_dir
;
547 node
->ipv6_preferred
= 0;
548 if (fascist_firewall_prefer_ipv6_orport(options
) &&
549 (tor_addr_is_null(&rs
->ipv6_addr
) == 0 ||
550 (node
->md
&& tor_addr_is_null(&node
->md
->ipv6_addr
) == 0)))
551 node
->ipv6_preferred
= 1;
554 } SMARTLIST_FOREACH_END(rs
);
559 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
560 /* We have no routerstatus for this router. Clear flags so we can skip
563 tor_assert(node
->ri
); /* if it had only an md, or nothing, purge
564 * would have removed it. */
565 if (node
->ri
->purpose
== ROUTER_PURPOSE_GENERAL
) {
566 /* Clear all flags. */
567 node
->is_valid
= node
->is_running
= node
->is_hs_dir
=
568 node
->is_fast
= node
->is_stable
=
569 node
->is_possible_guard
= node
->is_exit
=
570 node
->is_bad_exit
= node
->ipv6_preferred
= 0;
573 } SMARTLIST_FOREACH_END(node
);
577 /** Helper: return true iff a node has a usable amount of information*/
579 node_is_usable(const node_t
*node
)
581 return (node
->rs
) || (node
->ri
);
584 /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
585 * node with <b>identity_digest</b>. */
587 nodelist_remove_microdesc(const char *identity_digest
, microdesc_t
*md
)
589 node_t
*node
= node_get_mutable_by_id(identity_digest
);
590 if (node
&& node
->md
== md
) {
593 if (! node_get_ed25519_id(node
)) {
594 node_remove_from_ed25519_map(node
);
599 /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
601 nodelist_remove_routerinfo(routerinfo_t
*ri
)
603 node_t
*node
= node_get_mutable_by_id(ri
->cache_info
.identity_digest
);
604 if (node
&& node
->ri
== ri
) {
606 if (! node_is_usable(node
)) {
607 nodelist_drop_node(node
, 1);
613 /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
616 nodelist_drop_node(node_t
*node
, int remove_from_ht
)
620 if (remove_from_ht
) {
621 tmp
= HT_REMOVE(nodelist_map
, &the_nodelist
->nodes_by_id
, node
);
622 tor_assert(tmp
== node
);
624 node_remove_from_ed25519_map(node
);
626 idx
= node
->nodelist_idx
;
627 tor_assert(idx
>= 0);
629 tor_assert(node
== smartlist_get(the_nodelist
->nodes
, idx
));
630 smartlist_del(the_nodelist
->nodes
, idx
);
631 if (idx
< smartlist_len(the_nodelist
->nodes
)) {
632 tmp
= smartlist_get(the_nodelist
->nodes
, idx
);
633 tmp
->nodelist_idx
= idx
;
635 node
->nodelist_idx
= -1;
638 /** Return a newly allocated smartlist of the nodes that have <b>md</b> as
639 * their microdescriptor. */
641 nodelist_find_nodes_with_microdesc(const microdesc_t
*md
)
643 smartlist_t
*result
= smartlist_new();
645 if (the_nodelist
== NULL
)
648 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
649 if (node
->md
== md
) {
650 smartlist_add(result
, node
);
652 } SMARTLIST_FOREACH_END(node
);
657 /** Release storage held by <b>node</b> */
659 node_free(node_t
*node
)
664 node
->md
->held_by_nodes
--;
665 tor_assert(node
->nodelist_idx
== -1);
666 tor_free(node
->hsdir_index
);
670 /** Remove all entries from the nodelist that don't have enough info to be
671 * usable for anything. */
676 if (PREDICT_UNLIKELY(the_nodelist
== NULL
))
679 /* Remove the non-usable nodes. */
680 for (iter
= HT_START(nodelist_map
, &the_nodelist
->nodes_by_id
); iter
; ) {
681 node_t
*node
= *iter
;
683 if (node
->md
&& !node
->rs
) {
684 /* An md is only useful if there is an rs. */
685 node
->md
->held_by_nodes
--;
689 if (node_is_usable(node
)) {
690 iter
= HT_NEXT(nodelist_map
, &the_nodelist
->nodes_by_id
, iter
);
692 iter
= HT_NEXT_RMV(nodelist_map
, &the_nodelist
->nodes_by_id
, iter
);
693 nodelist_drop_node(node
, 0);
697 nodelist_assert_ok();
700 /** Release all storage held by the nodelist. */
702 nodelist_free_all(void)
704 if (PREDICT_UNLIKELY(the_nodelist
== NULL
))
707 HT_CLEAR(nodelist_map
, &the_nodelist
->nodes_by_id
);
708 HT_CLEAR(nodelist_ed_map
, &the_nodelist
->nodes_by_ed_id
);
709 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
710 node
->nodelist_idx
= -1;
712 } SMARTLIST_FOREACH_END(node
);
714 smartlist_free(the_nodelist
->nodes
);
716 tor_free(the_nodelist
);
719 /** Check that the nodelist is internally consistent, and consistent with
720 * the directory info it's derived from.
723 nodelist_assert_ok(void)
725 routerlist_t
*rl
= router_get_routerlist();
726 networkstatus_t
*ns
= networkstatus_get_latest_consensus();
732 dm
= digestmap_new();
734 /* every routerinfo in rl->routers should be in the nodelist. */
736 SMARTLIST_FOREACH_BEGIN(rl
->routers
, routerinfo_t
*, ri
) {
737 const node_t
*node
= node_get_by_id(ri
->cache_info
.identity_digest
);
738 tor_assert(node
&& node
->ri
== ri
);
739 tor_assert(fast_memeq(ri
->cache_info
.identity_digest
,
740 node
->identity
, DIGEST_LEN
));
741 tor_assert(! digestmap_get(dm
, node
->identity
));
742 digestmap_set(dm
, node
->identity
, (void*)node
);
743 } SMARTLIST_FOREACH_END(ri
);
746 /* every routerstatus in ns should be in the nodelist */
748 SMARTLIST_FOREACH_BEGIN(ns
->routerstatus_list
, routerstatus_t
*, rs
) {
749 const node_t
*node
= node_get_by_id(rs
->identity_digest
);
750 tor_assert(node
&& node
->rs
== rs
);
751 tor_assert(fast_memeq(rs
->identity_digest
, node
->identity
, DIGEST_LEN
));
752 digestmap_set(dm
, node
->identity
, (void*)node
);
753 if (ns
->flavor
== FLAV_MICRODESC
) {
754 /* If it's a microdesc consensus, every entry that has a
755 * microdescriptor should be in the nodelist.
758 microdesc_cache_lookup_by_digest256(NULL
, rs
->descriptor_digest
);
759 tor_assert(md
== node
->md
);
761 tor_assert(md
->held_by_nodes
>= 1);
763 } SMARTLIST_FOREACH_END(rs
);
766 /* The nodelist should have no other entries, and its entries should be
768 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
769 tor_assert(digestmap_get(dm
, node
->identity
) != NULL
);
770 tor_assert(node_sl_idx
== node
->nodelist_idx
);
771 } SMARTLIST_FOREACH_END(node
);
773 /* Every node listed with an ed25519 identity should be listed by that
776 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
777 if (!ed25519_public_key_is_zero(&node
->ed25519_id
)) {
778 tor_assert(node
== node_get_by_ed25519_id(&node
->ed25519_id
));
780 } SMARTLIST_FOREACH_END(node
);
783 HT_FOREACH(idx
, nodelist_ed_map
, &the_nodelist
->nodes_by_ed_id
) {
785 tor_assert(node
== node_get_by_ed25519_id(&node
->ed25519_id
));
788 tor_assert((long)smartlist_len(the_nodelist
->nodes
) ==
789 (long)HT_SIZE(&the_nodelist
->nodes_by_id
));
791 tor_assert((long)smartlist_len(the_nodelist
->nodes
) >=
792 (long)HT_SIZE(&the_nodelist
->nodes_by_ed_id
));
794 digestmap_free(dm
, NULL
);
797 /** Return a list of a node_t * for every node we know about. The caller
798 * MUST NOT modify the list. (You can set and clear flags in the nodes if
799 * you must, but you must not add or remove nodes.) */
800 MOCK_IMPL(smartlist_t
*,
801 nodelist_get_list
,(void))
804 return the_nodelist
->nodes
;
807 /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
808 * or $DIGEST~name, return the node with the matching identity digest and
809 * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
810 * is not well-formed. DOCDOC flags */
812 node_get_by_hex_id(const char *hex_id
, unsigned flags
)
814 char digest_buf
[DIGEST_LEN
];
815 char nn_buf
[MAX_NICKNAME_LEN
+1];
818 (void) flags
; // XXXX
820 if (hex_digest_nickname_decode(hex_id
, digest_buf
, &nn_char
, nn_buf
)==0) {
821 const node_t
*node
= node_get_by_id(digest_buf
);
824 if (nn_char
== '=') {
825 /* "=" indicates a Named relay, but there aren't any of those now. */
834 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
835 * the corresponding node_t, or NULL if none exists. Warn the user if they
836 * have specified a router by nickname, unless the NNF_NO_WARN_UNNAMED bit is
837 * set in <b>flags</b>. */
838 MOCK_IMPL(const node_t
*,
839 node_get_by_nickname
,(const char *nickname
, unsigned flags
))
841 const int warn_if_unnamed
= !(flags
& NNF_NO_WARN_UNNAMED
);
846 /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
849 if ((node
= node_get_by_hex_id(nickname
, flags
)) != NULL
)
853 if (!strcasecmp(nickname
, UNNAMED_ROUTER_NICKNAME
))
856 /* Okay, so the name is not canonical for anybody. */
858 smartlist_t
*matches
= smartlist_new();
859 const node_t
*choice
= NULL
;
861 SMARTLIST_FOREACH_BEGIN(the_nodelist
->nodes
, node_t
*, node
) {
862 if (!strcasecmp(node_get_nickname(node
), nickname
))
863 smartlist_add(matches
, node
);
864 } SMARTLIST_FOREACH_END(node
);
866 if (smartlist_len(matches
)>1 && warn_if_unnamed
) {
867 int any_unwarned
= 0;
868 SMARTLIST_FOREACH_BEGIN(matches
, node_t
*, node
) {
869 if (!node
->name_lookup_warned
) {
870 node
->name_lookup_warned
= 1;
873 } SMARTLIST_FOREACH_END(node
);
876 log_warn(LD_CONFIG
, "There are multiple matches for the name %s, "
877 "but none is listed as Named in the directory consensus. "
878 "Choosing one arbitrarily.", nickname
);
880 } else if (smartlist_len(matches
)==1 && warn_if_unnamed
) {
881 char fp
[HEX_DIGEST_LEN
+1];
882 node_t
*node
= smartlist_get(matches
, 0);
883 if (! node
->name_lookup_warned
) {
884 base16_encode(fp
, sizeof(fp
), node
->identity
, DIGEST_LEN
);
886 "You specified a relay \"%s\" by name, but nicknames can be "
887 "used by any relay, not just the one you meant. "
888 "To make sure you get the same relay in the future, refer "
889 "to it by key, as \"$%s\".", nickname
, fp
);
890 node
->name_lookup_warned
= 1;
894 if (smartlist_len(matches
))
895 choice
= smartlist_get(matches
, 0);
897 smartlist_free(matches
);
902 /** Return the Ed25519 identity key for the provided node, or NULL if it
903 * doesn't have one. */
904 const ed25519_public_key_t
*
905 node_get_ed25519_id(const node_t
*node
)
907 const ed25519_public_key_t
*ri_pk
= NULL
;
908 const ed25519_public_key_t
*md_pk
= NULL
;
910 if (node
->ri
->cache_info
.signing_key_cert
) {
911 ri_pk
= &node
->ri
->cache_info
.signing_key_cert
->signing_key
;
912 if (BUG(ed25519_public_key_is_zero(ri_pk
)))
918 if (node
->md
->ed25519_identity_pkey
) {
919 md_pk
= node
->md
->ed25519_identity_pkey
;
923 if (ri_pk
&& md_pk
) {
924 if (ed25519_pubkey_eq(ri_pk
, md_pk
)) {
927 /* This can happen if the relay gets flagged NoEdConsensus which will be
928 * triggered on all relays of the network. Thus a protocol warning. */
929 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
930 "Inconsistent ed25519 identities in the nodelist");
940 /** Return true iff this node's Ed25519 identity matches <b>id</b>.
941 * (An absent Ed25519 identity matches NULL or zero.) */
943 node_ed25519_id_matches(const node_t
*node
, const ed25519_public_key_t
*id
)
945 const ed25519_public_key_t
*node_id
= node_get_ed25519_id(node
);
946 if (node_id
== NULL
|| ed25519_public_key_is_zero(node_id
)) {
947 return id
== NULL
|| ed25519_public_key_is_zero(id
);
949 return id
&& ed25519_pubkey_eq(node_id
, id
);
953 /** Return true iff <b>node</b> supports authenticating itself
954 * by ed25519 ID during the link handshake in a way that we can understand
955 * when we probe it. */
957 node_supports_ed25519_link_authentication(const node_t
*node
)
959 /* XXXX Oh hm. What if some day in the future there are link handshake
960 * versions that aren't 3 but which are ed25519 */
961 if (! node_get_ed25519_id(node
))
964 const char *protos
= node
->ri
->protocol_list
;
967 return protocol_list_supports_protocol(protos
, PRT_LINKAUTH
, 3);
970 return node
->rs
->supports_ed25519_link_handshake
;
972 tor_assert_nonfatal_unreached_once();
976 /** Return true iff <b>node</b> supports the hidden service directory version
977 * 3 protocol (proposal 224). */
979 node_supports_v3_hsdir(const node_t
*node
)
984 return node
->rs
->supports_v3_hsdir
;
987 if (node
->ri
->protocol_list
== NULL
) {
990 /* Bug #22447 forces us to filter on tor version:
991 * If platform is a Tor version, and older than 0.3.0.8, return False.
992 * Else, obey the protocol list. */
993 if (node
->ri
->platform
) {
994 if (!strcmpstart(node
->ri
->platform
, "Tor ") &&
995 !tor_version_as_new_as(node
->ri
->platform
, "0.3.0.8")) {
999 return protocol_list_supports_protocol(node
->ri
->protocol_list
,
1000 PRT_HSDIR
, PROTOVER_HSDIR_V3
);
1002 tor_assert_nonfatal_unreached_once();
1006 /** Return true iff <b>node</b> supports ed25519 authentication as an hidden
1007 * service introduction point.*/
1009 node_supports_ed25519_hs_intro(const node_t
*node
)
1014 return node
->rs
->supports_ed25519_hs_intro
;
1017 if (node
->ri
->protocol_list
== NULL
) {
1020 return protocol_list_supports_protocol(node
->ri
->protocol_list
,
1021 PRT_HSINTRO
, PROTOVER_HS_INTRO_V3
);
1023 tor_assert_nonfatal_unreached_once();
1027 /** Return true iff <b>node</b> supports to be a rendezvous point for hidden
1028 * service version 3 (HSRend=2). */
1030 node_supports_v3_rendezvous_point(const node_t
*node
)
1035 return node
->rs
->supports_v3_rendezvous_point
;
1038 if (node
->ri
->protocol_list
== NULL
) {
1041 return protocol_list_supports_protocol(node
->ri
->protocol_list
,
1043 PROTOVER_HS_RENDEZVOUS_POINT_V3
);
1045 tor_assert_nonfatal_unreached_once();
1049 /** Return the RSA ID key's SHA1 digest for the provided node. */
1051 node_get_rsa_id_digest(const node_t
*node
)
1054 return (const uint8_t*)node
->identity
;
1057 /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
1059 node_get_nickname(const node_t
*node
)
1063 return node
->rs
->nickname
;
1065 return node
->ri
->nickname
;
1070 /** Return true iff <b>node</b> appears to be a directory authority or
1071 * directory cache */
1073 node_is_dir(const node_t
*node
)
1076 routerstatus_t
* rs
= node
->rs
;
1077 /* This is true if supports_tunnelled_dir_requests is true which
1078 * indicates that we support directory request tunnelled or through the
1080 return rs
->is_v2_dir
;
1081 } else if (node
->ri
) {
1082 routerinfo_t
* ri
= node
->ri
;
1083 /* Both tunnelled request is supported or DirPort is set. */
1084 return ri
->supports_tunnelled_dir_requests
;
1090 /** Return true iff <b>node</b> has either kind of usable descriptor -- that
1091 * is, a routerdescriptor or a microdescriptor. */
1093 node_has_descriptor(const node_t
*node
)
1096 (node
->rs
&& node
->md
));
1099 /** Return the router_purpose of <b>node</b>. */
1101 node_get_purpose(const node_t
*node
)
1104 return node
->ri
->purpose
;
1106 return ROUTER_PURPOSE_GENERAL
;
1109 /** Compute the verbose ("extended") nickname of <b>node</b> and store it
1110 * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
1111 * <b>verbose_name_out</b> */
1113 node_get_verbose_nickname(const node_t
*node
,
1114 char *verbose_name_out
)
1116 const char *nickname
= node_get_nickname(node
);
1117 verbose_name_out
[0] = '$';
1118 base16_encode(verbose_name_out
+1, HEX_DIGEST_LEN
+1, node
->identity
,
1122 verbose_name_out
[1+HEX_DIGEST_LEN
] = '~';
1123 strlcpy(verbose_name_out
+1+HEX_DIGEST_LEN
+1, nickname
, MAX_NICKNAME_LEN
+1);
1126 /** Compute the verbose ("extended") nickname of node with
1127 * given <b>id_digest</b> and store it into the MAX_VERBOSE_NICKNAME_LEN+1
1128 * character buffer at <b>verbose_name_out</b>
1130 * If node_get_by_id() returns NULL, base 16 encoding of
1131 * <b>id_digest</b> is returned instead. */
1133 node_get_verbose_nickname_by_id(const char *id_digest
,
1134 char *verbose_name_out
)
1136 const node_t
*node
= node_get_by_id(id_digest
);
1138 verbose_name_out
[0] = '$';
1139 base16_encode(verbose_name_out
+1, HEX_DIGEST_LEN
+1, id_digest
, DIGEST_LEN
);
1141 node_get_verbose_nickname(node
, verbose_name_out
);
1145 /** Return true iff it seems that <b>node</b> allows circuits to exit
1146 * through it directlry from the client. */
1148 node_allows_single_hop_exits(const node_t
*node
)
1150 if (node
&& node
->ri
)
1151 return node
->ri
->allow_single_hop_exits
;
1156 /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
1157 * actually permit anything to exit, or we don't know its exit policy */
1159 node_exit_policy_rejects_all(const node_t
*node
)
1161 if (node
->rejects_all
)
1165 return node
->ri
->policy_is_reject_star
;
1167 return node
->md
->exit_policy
== NULL
||
1168 short_policy_is_reject_star(node
->md
->exit_policy
);
1173 /** Return true iff the exit policy for <b>node</b> is such that we can treat
1174 * rejecting an address of type <b>family</b> unexpectedly as a sign of that
1175 * node's failure. */
1177 node_exit_policy_is_exact(const node_t
*node
, sa_family_t family
)
1179 if (family
== AF_UNSPEC
) {
1180 return 1; /* Rejecting an address but not telling us what address
1182 } else if (family
== AF_INET
) {
1183 return node
->ri
!= NULL
;
1184 } else if (family
== AF_INET6
) {
1187 tor_fragile_assert();
1191 /* Check if the "addr" and port_field fields from r are a valid non-listening
1192 * address/port. If so, set valid to true and add a newly allocated
1193 * tor_addr_port_t containing "addr" and port_field to sl.
1194 * "addr" is an IPv4 host-order address and port_field is a uint16_t.
1195 * r is typically a routerinfo_t or routerstatus_t.
1197 #define SL_ADD_NEW_IPV4_AP(r, port_field, sl, valid) \
1199 if (tor_addr_port_is_valid_ipv4h((r)->addr, (r)->port_field, 0)) { \
1201 tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t)); \
1202 tor_addr_from_ipv4h(&ap->addr, (r)->addr); \
1203 ap->port = (r)->port_field; \
1204 smartlist_add((sl), ap); \
1208 /* Check if the "addr" and port_field fields from r are a valid non-listening
1209 * address/port. If so, set valid to true and add a newly allocated
1210 * tor_addr_port_t containing "addr" and port_field to sl.
1211 * "addr" is a tor_addr_t and port_field is a uint16_t.
1212 * r is typically a routerinfo_t or routerstatus_t.
1214 #define SL_ADD_NEW_IPV6_AP(r, port_field, sl, valid) \
1216 if (tor_addr_port_is_valid(&(r)->ipv6_addr, (r)->port_field, 0)) { \
1218 tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t)); \
1219 tor_addr_copy(&ap->addr, &(r)->ipv6_addr); \
1220 ap->port = (r)->port_field; \
1221 smartlist_add((sl), ap); \
1225 /** Return list of tor_addr_port_t with all OR ports (in the sense IP
1226 * addr + TCP port) for <b>node</b>. Caller must free all elements
1227 * using tor_free() and free the list using smartlist_free().
1229 * XXX this is potentially a memory fragmentation hog -- if on
1230 * critical path consider the option of having the caller allocate the
1234 node_get_all_orports(const node_t
*node
)
1236 smartlist_t
*sl
= smartlist_new();
1239 /* Find a valid IPv4 address and port */
1240 if (node
->ri
!= NULL
) {
1241 SL_ADD_NEW_IPV4_AP(node
->ri
, or_port
, sl
, valid
);
1244 /* If we didn't find a valid address/port in the ri, try the rs */
1245 if (!valid
&& node
->rs
!= NULL
) {
1246 SL_ADD_NEW_IPV4_AP(node
->rs
, or_port
, sl
, valid
);
1249 /* Find a valid IPv6 address and port */
1251 if (node
->ri
!= NULL
) {
1252 SL_ADD_NEW_IPV6_AP(node
->ri
, ipv6_orport
, sl
, valid
);
1255 if (!valid
&& node
->rs
!= NULL
) {
1256 SL_ADD_NEW_IPV6_AP(node
->rs
, ipv6_orport
, sl
, valid
);
1259 if (!valid
&& node
->md
!= NULL
) {
1260 SL_ADD_NEW_IPV6_AP(node
->md
, ipv6_orport
, sl
, valid
);
1266 #undef SL_ADD_NEW_IPV4_AP
1267 #undef SL_ADD_NEW_IPV6_AP
1269 /** Wrapper around node_get_prim_orport for backward
1272 node_get_addr(const node_t
*node
, tor_addr_t
*addr_out
)
1275 node_get_prim_orport(node
, &ap
);
1276 tor_addr_copy(addr_out
, &ap
.addr
);
1279 /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
1280 * seem to have one. */
1282 node_get_prim_addr_ipv4h(const node_t
*node
)
1284 /* Don't check the ORPort or DirPort, as this function isn't port-specific,
1285 * and the node might have a valid IPv4 address, yet have a zero
1286 * ORPort or DirPort.
1288 if (node
->ri
&& tor_addr_is_valid_ipv4h(node
->ri
->addr
, 0)) {
1289 return node
->ri
->addr
;
1290 } else if (node
->rs
&& tor_addr_is_valid_ipv4h(node
->rs
->addr
, 0)) {
1291 return node
->rs
->addr
;
1296 /** Copy a string representation of an IP address for <b>node</b> into
1297 * the <b>len</b>-byte buffer at <b>buf</b>. */
1299 node_get_address_string(const node_t
*node
, char *buf
, size_t len
)
1301 uint32_t ipv4_addr
= node_get_prim_addr_ipv4h(node
);
1303 if (tor_addr_is_valid_ipv4h(ipv4_addr
, 0)) {
1305 tor_addr_from_ipv4h(&addr
, ipv4_addr
);
1306 tor_addr_to_str(buf
, &addr
, len
, 0);
1307 } else if (len
> 0) {
1312 /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
1315 node_get_declared_uptime(const node_t
*node
)
1318 return node
->ri
->uptime
;
1323 /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
1325 node_get_platform(const node_t
*node
)
1327 /* If we wanted, we could record the version in the routerstatus_t, since
1328 * the consensus lists it. We don't, though, so this function just won't
1329 * work with microdescriptors. */
1331 return node
->ri
->platform
;
1336 /** Return true iff <b>node</b> is one representing this router. */
1338 node_is_me(const node_t
*node
)
1340 return router_digest_is_me(node
->identity
);
1343 /** Return <b>node</b> declared family (as a list of names), or NULL if
1344 * the node didn't declare a family. */
1346 node_get_declared_family(const node_t
*node
)
1348 if (node
->ri
&& node
->ri
->declared_family
)
1349 return node
->ri
->declared_family
;
1350 else if (node
->md
&& node
->md
->family
)
1351 return node
->md
->family
;
1356 /* Does this node have a valid IPv6 address?
1357 * Prefer node_has_ipv6_orport() or node_has_ipv6_dirport() for
1358 * checking specific ports. */
1360 node_has_ipv6_addr(const node_t
*node
)
1362 /* Don't check the ORPort or DirPort, as this function isn't port-specific,
1363 * and the node might have a valid IPv6 address, yet have a zero
1364 * ORPort or DirPort.
1366 if (node
->ri
&& tor_addr_is_valid(&node
->ri
->ipv6_addr
, 0))
1368 if (node
->rs
&& tor_addr_is_valid(&node
->rs
->ipv6_addr
, 0))
1370 if (node
->md
&& tor_addr_is_valid(&node
->md
->ipv6_addr
, 0))
1376 /* Does this node have a valid IPv6 ORPort? */
1378 node_has_ipv6_orport(const node_t
*node
)
1380 tor_addr_port_t ipv6_orport
;
1381 node_get_pref_ipv6_orport(node
, &ipv6_orport
);
1382 return tor_addr_port_is_valid_ap(&ipv6_orport
, 0);
1385 /* Does this node have a valid IPv6 DirPort? */
1387 node_has_ipv6_dirport(const node_t
*node
)
1389 tor_addr_port_t ipv6_dirport
;
1390 node_get_pref_ipv6_dirport(node
, &ipv6_dirport
);
1391 return tor_addr_port_is_valid_ap(&ipv6_dirport
, 0);
1394 /** Return 1 if we prefer the IPv6 address and OR TCP port of
1395 * <b>node</b>, else 0.
1397 * We prefer the IPv6 address if the router has an IPv6 address,
1398 * and we can use IPv6 addresses, and:
1399 * i) the node_t says that it prefers IPv6
1401 * ii) the router has no IPv4 OR address.
1403 * If you don't have a node, consider looking it up.
1404 * If there is no node, use fascist_firewall_prefer_ipv6_orport().
1407 node_ipv6_or_preferred(const node_t
*node
)
1409 const or_options_t
*options
= get_options();
1410 tor_addr_port_t ipv4_addr
;
1411 node_assert_ok(node
);
1413 /* XX/teor - node->ipv6_preferred is set from
1414 * fascist_firewall_prefer_ipv6_orport() each time the consensus is loaded.
1416 if (!fascist_firewall_use_ipv6(options
)) {
1418 } else if (node
->ipv6_preferred
|| node_get_prim_orport(node
, &ipv4_addr
)) {
1419 return node_has_ipv6_orport(node
);
1424 #define RETURN_IPV4_AP(r, port_field, ap_out) \
1426 if (r && tor_addr_port_is_valid_ipv4h((r)->addr, (r)->port_field, 0)) { \
1427 tor_addr_from_ipv4h(&(ap_out)->addr, (r)->addr); \
1428 (ap_out)->port = (r)->port_field; \
1433 /** Copy the primary (IPv4) OR port (IP address and TCP port) for
1434 * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
1435 * port was copied, else return non-zero.*/
1437 node_get_prim_orport(const node_t
*node
, tor_addr_port_t
*ap_out
)
1439 node_assert_ok(node
);
1442 /* Clear the address, as a safety precaution if calling functions ignore the
1444 tor_addr_make_null(&ap_out
->addr
, AF_INET
);
1447 /* Check ri first, because rewrite_node_address_for_bridge() updates
1448 * node->ri with the configured bridge address. */
1450 RETURN_IPV4_AP(node
->ri
, or_port
, ap_out
);
1451 RETURN_IPV4_AP(node
->rs
, or_port
, ap_out
);
1452 /* Microdescriptors only have an IPv6 address */
1457 /** Copy the preferred OR port (IP address and TCP port) for
1458 * <b>node</b> into *<b>ap_out</b>. */
1460 node_get_pref_orport(const node_t
*node
, tor_addr_port_t
*ap_out
)
1464 if (node_ipv6_or_preferred(node
)) {
1465 node_get_pref_ipv6_orport(node
, ap_out
);
1467 /* the primary ORPort is always on IPv4 */
1468 node_get_prim_orport(node
, ap_out
);
1472 /** Copy the preferred IPv6 OR port (IP address and TCP port) for
1473 * <b>node</b> into *<b>ap_out</b>. */
1475 node_get_pref_ipv6_orport(const node_t
*node
, tor_addr_port_t
*ap_out
)
1477 node_assert_ok(node
);
1480 /* Check ri first, because rewrite_node_address_for_bridge() updates
1481 * node->ri with the configured bridge address.
1482 * Prefer rs over md for consistency with the fascist_firewall_* functions.
1483 * Check if the address or port are valid, and try another alternative
1484 * if they are not. */
1486 if (node
->ri
&& tor_addr_port_is_valid(&node
->ri
->ipv6_addr
,
1487 node
->ri
->ipv6_orport
, 0)) {
1488 tor_addr_copy(&ap_out
->addr
, &node
->ri
->ipv6_addr
);
1489 ap_out
->port
= node
->ri
->ipv6_orport
;
1490 } else if (node
->rs
&& tor_addr_port_is_valid(&node
->rs
->ipv6_addr
,
1491 node
->rs
->ipv6_orport
, 0)) {
1492 tor_addr_copy(&ap_out
->addr
, &node
->rs
->ipv6_addr
);
1493 ap_out
->port
= node
->rs
->ipv6_orport
;
1494 } else if (node
->md
&& tor_addr_port_is_valid(&node
->md
->ipv6_addr
,
1495 node
->md
->ipv6_orport
, 0)) {
1496 tor_addr_copy(&ap_out
->addr
, &node
->md
->ipv6_addr
);
1497 ap_out
->port
= node
->md
->ipv6_orport
;
1499 tor_addr_make_null(&ap_out
->addr
, AF_INET6
);
1504 /** Return 1 if we prefer the IPv6 address and Dir TCP port of
1505 * <b>node</b>, else 0.
1507 * We prefer the IPv6 address if the router has an IPv6 address,
1508 * and we can use IPv6 addresses, and:
1509 * i) the router has no IPv4 Dir address.
1511 * ii) our preference is for IPv6 Dir addresses.
1513 * If there is no node, use fascist_firewall_prefer_ipv6_dirport().
1516 node_ipv6_dir_preferred(const node_t
*node
)
1518 const or_options_t
*options
= get_options();
1519 tor_addr_port_t ipv4_addr
;
1520 node_assert_ok(node
);
1522 /* node->ipv6_preferred is set from fascist_firewall_prefer_ipv6_orport(),
1523 * so we can't use it to determine DirPort IPv6 preference.
1524 * This means that bridge clients will use IPv4 DirPorts by default.
1526 if (!fascist_firewall_use_ipv6(options
)) {
1528 } else if (node_get_prim_dirport(node
, &ipv4_addr
)
1529 || fascist_firewall_prefer_ipv6_dirport(get_options())) {
1530 return node_has_ipv6_dirport(node
);
1535 /** Copy the primary (IPv4) Dir port (IP address and TCP port) for
1536 * <b>node</b> into *<b>ap_out</b>. Return 0 if a valid address and
1537 * port was copied, else return non-zero.*/
1539 node_get_prim_dirport(const node_t
*node
, tor_addr_port_t
*ap_out
)
1541 node_assert_ok(node
);
1544 /* Check ri first, because rewrite_node_address_for_bridge() updates
1545 * node->ri with the configured bridge address. */
1547 RETURN_IPV4_AP(node
->ri
, dir_port
, ap_out
);
1548 RETURN_IPV4_AP(node
->rs
, dir_port
, ap_out
);
1549 /* Microdescriptors only have an IPv6 address */
1554 #undef RETURN_IPV4_AP
1556 /** Copy the preferred Dir port (IP address and TCP port) for
1557 * <b>node</b> into *<b>ap_out</b>. */
1559 node_get_pref_dirport(const node_t
*node
, tor_addr_port_t
*ap_out
)
1563 if (node_ipv6_dir_preferred(node
)) {
1564 node_get_pref_ipv6_dirport(node
, ap_out
);
1566 /* the primary DirPort is always on IPv4 */
1567 node_get_prim_dirport(node
, ap_out
);
1571 /** Copy the preferred IPv6 Dir port (IP address and TCP port) for
1572 * <b>node</b> into *<b>ap_out</b>. */
1574 node_get_pref_ipv6_dirport(const node_t
*node
, tor_addr_port_t
*ap_out
)
1576 node_assert_ok(node
);
1579 /* Check ri first, because rewrite_node_address_for_bridge() updates
1580 * node->ri with the configured bridge address.
1581 * Prefer rs over md for consistency with the fascist_firewall_* functions.
1582 * Check if the address or port are valid, and try another alternative
1583 * if they are not. */
1585 /* Assume IPv4 and IPv6 dirports are the same */
1586 if (node
->ri
&& tor_addr_port_is_valid(&node
->ri
->ipv6_addr
,
1587 node
->ri
->dir_port
, 0)) {
1588 tor_addr_copy(&ap_out
->addr
, &node
->ri
->ipv6_addr
);
1589 ap_out
->port
= node
->ri
->dir_port
;
1590 } else if (node
->rs
&& tor_addr_port_is_valid(&node
->rs
->ipv6_addr
,
1591 node
->rs
->dir_port
, 0)) {
1592 tor_addr_copy(&ap_out
->addr
, &node
->rs
->ipv6_addr
);
1593 ap_out
->port
= node
->rs
->dir_port
;
1595 tor_addr_make_null(&ap_out
->addr
, AF_INET6
);
1600 /** Return true iff <b>md</b> has a curve25519 onion key.
1601 * Use node_has_curve25519_onion_key() instead of calling this directly. */
1603 microdesc_has_curve25519_onion_key(const microdesc_t
*md
)
1609 if (!md
->onion_curve25519_pkey
) {
1613 if (tor_mem_is_zero((const char*)md
->onion_curve25519_pkey
->public_key
,
1614 CURVE25519_PUBKEY_LEN
)) {
1621 /** Return true iff <b>node</b> has a curve25519 onion key. */
1623 node_has_curve25519_onion_key(const node_t
*node
)
1629 return routerinfo_has_curve25519_onion_key(node
->ri
);
1631 return microdesc_has_curve25519_onion_key(node
->md
);
1636 /** Refresh the country code of <b>ri</b>. This function MUST be called on
1637 * each router when the GeoIP database is reloaded, and on all new routers. */
1639 node_set_country(node_t
*node
)
1641 tor_addr_t addr
= TOR_ADDR_NULL
;
1645 tor_addr_from_ipv4h(&addr
, node
->rs
->addr
);
1647 tor_addr_from_ipv4h(&addr
, node
->ri
->addr
);
1649 node
->country
= geoip_get_country_by_addr(&addr
);
1652 /** Set the country code of all routers in the routerlist. */
1654 nodelist_refresh_countries(void)
1656 smartlist_t
*nodes
= nodelist_get_list();
1657 SMARTLIST_FOREACH(nodes
, node_t
*, node
,
1658 node_set_country(node
));
1661 /** Return true iff router1 and router2 have similar enough network addresses
1662 * that we should treat them as being in the same family */
1664 addrs_in_same_network_family(const tor_addr_t
*a1
,
1665 const tor_addr_t
*a2
)
1667 return 0 == tor_addr_compare_masked(a1
, a2
, 16, CMP_SEMANTIC
);
1670 /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
1671 * (case-insensitive), or if <b>node's</b> identity key digest
1672 * matches a hexadecimal value stored in <b>nickname</b>. Return
1673 * false otherwise. */
1675 node_nickname_matches(const node_t
*node
, const char *nickname
)
1677 const char *n
= node_get_nickname(node
);
1678 if (n
&& nickname
[0]!='$' && !strcasecmp(n
, nickname
))
1680 return hex_digest_nickname_matches(nickname
,
1685 /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
1687 node_in_nickname_smartlist(const smartlist_t
*lst
, const node_t
*node
)
1690 SMARTLIST_FOREACH(lst
, const char *, name
, {
1691 if (node_nickname_matches(node
, name
))
1697 /** Return true iff r1 and r2 are in the same family, but not the same
1700 nodes_in_same_family(const node_t
*node1
, const node_t
*node2
)
1702 const or_options_t
*options
= get_options();
1704 /* Are they in the same family because of their addresses? */
1705 if (options
->EnforceDistinctSubnets
) {
1707 node_get_addr(node1
, &a1
);
1708 node_get_addr(node2
, &a2
);
1709 if (addrs_in_same_network_family(&a1
, &a2
))
1713 /* Are they in the same family because the agree they are? */
1715 const smartlist_t
*f1
, *f2
;
1716 f1
= node_get_declared_family(node1
);
1717 f2
= node_get_declared_family(node2
);
1719 node_in_nickname_smartlist(f1
, node2
) &&
1720 node_in_nickname_smartlist(f2
, node1
))
1724 /* Are they in the same option because the user says they are? */
1725 if (options
->NodeFamilySets
) {
1726 SMARTLIST_FOREACH(options
->NodeFamilySets
, const routerset_t
*, rs
, {
1727 if (routerset_contains_node(rs
, node1
) &&
1728 routerset_contains_node(rs
, node2
))
1737 * Add all the family of <b>node</b>, including <b>node</b> itself, to
1738 * the smartlist <b>sl</b>.
1740 * This is used to make sure we don't pick siblings in a single path, or
1741 * pick more than one relay from a family for our entry guard list.
1742 * Note that a node may be added to <b>sl</b> more than once if it is
1743 * part of <b>node</b>'s family for more than one reason.
1746 nodelist_add_node_and_family(smartlist_t
*sl
, const node_t
*node
)
1748 const smartlist_t
*all_nodes
= nodelist_get_list();
1749 const smartlist_t
*declared_family
;
1750 const or_options_t
*options
= get_options();
1754 declared_family
= node_get_declared_family(node
);
1756 /* Let's make sure that we have the node itself, if it's a real node. */
1758 const node_t
*real_node
= node_get_by_id(node
->identity
);
1760 smartlist_add(sl
, (node_t
*)real_node
);
1763 /* First, add any nodes with similar network addresses. */
1764 if (options
->EnforceDistinctSubnets
) {
1765 tor_addr_t node_addr
;
1766 node_get_addr(node
, &node_addr
);
1768 SMARTLIST_FOREACH_BEGIN(all_nodes
, const node_t
*, node2
) {
1770 node_get_addr(node2
, &a
);
1771 if (addrs_in_same_network_family(&a
, &node_addr
))
1772 smartlist_add(sl
, (void*)node2
);
1773 } SMARTLIST_FOREACH_END(node2
);
1776 /* Now, add all nodes in the declared_family of this node, if they
1777 * also declare this node to be in their family. */
1778 if (declared_family
) {
1779 /* Add every r such that router declares familyness with node, and node
1780 * declares familyhood with router. */
1781 SMARTLIST_FOREACH_BEGIN(declared_family
, const char *, name
) {
1782 const node_t
*node2
;
1783 const smartlist_t
*family2
;
1784 if (!(node2
= node_get_by_nickname(name
, NNF_NO_WARN_UNNAMED
)))
1786 if (!(family2
= node_get_declared_family(node2
)))
1788 SMARTLIST_FOREACH_BEGIN(family2
, const char *, name2
) {
1789 if (node_nickname_matches(node
, name2
)) {
1790 smartlist_add(sl
, (void*)node2
);
1793 } SMARTLIST_FOREACH_END(name2
);
1794 } SMARTLIST_FOREACH_END(name
);
1797 /* If the user declared any families locally, honor those too. */
1798 if (options
->NodeFamilySets
) {
1799 SMARTLIST_FOREACH(options
->NodeFamilySets
, const routerset_t
*, rs
, {
1800 if (routerset_contains_node(rs
, node
)) {
1801 routerset_get_all_nodes(sl
, rs
, NULL
, 0);
1807 /** Find a router that's up, that has this IP address, and
1808 * that allows exit to this address:port, or return NULL if there
1810 * Don't exit enclave to excluded relays -- it wouldn't actually
1811 * hurt anything, but this way there are fewer confused users.
1814 router_find_exact_exit_enclave(const char *address
, uint16_t port
)
1819 const or_options_t
*options
= get_options();
1821 if (!tor_inet_aton(address
, &in
))
1822 return NULL
; /* it's not an IP already */
1823 addr
= ntohl(in
.s_addr
);
1825 tor_addr_from_ipv4h(&a
, addr
);
1827 SMARTLIST_FOREACH(nodelist_get_list(), const node_t
*, node
, {
1828 if (node_get_addr_ipv4h(node
) == addr
&&
1830 compare_tor_addr_to_node_policy(&a
, port
, node
) ==
1831 ADDR_POLICY_ACCEPTED
&&
1832 !routerset_contains_node(options
->ExcludeExitNodesUnion_
, node
))
1838 /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
1839 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
1840 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
1842 * If <b>need_guard</b>, we require that the router is a possible entry guard.
1845 node_is_unreliable(const node_t
*node
, int need_uptime
,
1846 int need_capacity
, int need_guard
)
1848 if (need_uptime
&& !node
->is_stable
)
1850 if (need_capacity
&& !node
->is_fast
)
1852 if (need_guard
&& !node
->is_possible_guard
)
1857 /** Return 1 if all running sufficiently-stable routers we can use will reject
1858 * addr:port. Return 0 if any might accept it. */
1860 router_exit_policy_all_nodes_reject(const tor_addr_t
*addr
, uint16_t port
,
1863 addr_policy_result_t r
;
1865 SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t
*, node
) {
1866 if (node
->is_running
&&
1867 !node_is_unreliable(node
, need_uptime
, 0, 0)) {
1869 r
= compare_tor_addr_to_node_policy(addr
, port
, node
);
1871 if (r
!= ADDR_POLICY_REJECTED
&& r
!= ADDR_POLICY_PROBABLY_REJECTED
)
1872 return 0; /* this one could be ok. good enough. */
1874 } SMARTLIST_FOREACH_END(node
);
1875 return 1; /* all will reject. */
1878 /** Mark the router with ID <b>digest</b> as running or non-running
1879 * in our routerlist. */
1881 router_set_status(const char *digest
, int up
)
1886 SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
1888 if (tor_memeq(d
->digest
, digest
, DIGEST_LEN
))
1889 d
->is_running
= up
);
1891 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
1893 if (tor_memeq(d
->digest
, digest
, DIGEST_LEN
))
1894 d
->is_running
= up
);
1896 node
= node_get_mutable_by_id(digest
);
1899 log_debug(LD_DIR
,"Marking router %s as %s.",
1900 node_describe(node
), up
? "up" : "down");
1902 if (!up
&& node_is_me(node
) && !net_is_disabled())
1903 log_warn(LD_NET
, "We just marked ourself as down. Are your external "
1904 "addresses reachable?");
1906 if (bool_neq(node
->is_running
, up
))
1907 router_dir_info_changed();
1909 node
->is_running
= up
;
1913 /** True iff, the last time we checked whether we had enough directory info
1914 * to build circuits, the answer was "yes". If there are no exits in the
1915 * consensus, we act as if we have 100% of the exit directory info. */
1916 static int have_min_dir_info
= 0;
1918 /** Does the consensus contain nodes that can exit? */
1919 static consensus_path_type_t have_consensus_path
= CONSENSUS_PATH_UNKNOWN
;
1921 /** True iff enough has changed since the last time we checked whether we had
1922 * enough directory info to build circuits that our old answer can no longer
1924 static int need_to_update_have_min_dir_info
= 1;
1925 /** String describing what we're missing before we have enough directory
1927 static char dir_info_status
[512] = "";
1929 /** Return true iff we have enough consensus information to
1930 * start building circuits. Right now, this means "a consensus that's
1931 * less than a day old, and at least 60% of router descriptors (configurable),
1932 * weighted by bandwidth. Treat the exit fraction as 100% if there are
1933 * no exits in the consensus."
1934 * To obtain the final weighted bandwidth, we multiply the
1935 * weighted bandwidth fraction for each position (guard, middle, exit). */
1937 router_have_minimum_dir_info
,(void))
1939 static int logged_delay
=0;
1940 const char *delay_fetches_msg
= NULL
;
1941 if (should_delay_dir_fetches(get_options(), &delay_fetches_msg
)) {
1943 log_notice(LD_DIR
, "Delaying directory fetches: %s", delay_fetches_msg
);
1945 strlcpy(dir_info_status
, delay_fetches_msg
, sizeof(dir_info_status
));
1948 logged_delay
= 0; /* reset it if we get this far */
1950 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info
)) {
1951 update_router_have_minimum_dir_info();
1954 return have_min_dir_info
;
1957 /** Set to CONSENSUS_PATH_EXIT if there is at least one exit node
1958 * in the consensus. We update this flag in compute_frac_paths_available if
1959 * there is at least one relay that has an Exit flag in the consensus.
1960 * Used to avoid building exit circuits when they will almost certainly fail.
1961 * Set to CONSENSUS_PATH_INTERNAL if there are no exits in the consensus.
1962 * (This situation typically occurs during bootstrap of a test network.)
1963 * Set to CONSENSUS_PATH_UNKNOWN if we have never checked, or have
1964 * reason to believe our last known value was invalid or has expired.
1965 * If we're in a network with TestingDirAuthVoteExit set,
1966 * this can cause router_have_consensus_path() to be set to
1967 * CONSENSUS_PATH_EXIT, even if there are no nodes with accept exit policies.
1969 MOCK_IMPL(consensus_path_type_t
,
1970 router_have_consensus_path
, (void))
1972 return have_consensus_path
;
1975 /** Called when our internal view of the directory has changed. This can be
1976 * when the authorities change, networkstatuses change, the list of routerdescs
1977 * changes, or number of running routers changes.
1980 router_dir_info_changed(void)
1982 need_to_update_have_min_dir_info
= 1;
1983 rend_hsdir_routers_changed();
1984 hs_service_dir_info_changed();
1985 hs_client_dir_info_changed();
1988 /** Return a string describing what we're missing before we have enough
1989 * directory info. */
1991 get_dir_info_status_string(void)
1993 return dir_info_status
;
1996 /** Iterate over the servers listed in <b>consensus</b>, and count how many of
1997 * them seem like ones we'd use (store this in *<b>num_usable</b>), and how
1998 * many of <em>those</em> we have descriptors for (store this in
1999 * *<b>num_present</b>).
2001 * If <b>in_set</b> is non-NULL, only consider those routers in <b>in_set</b>.
2002 * If <b>exit_only</b> is USABLE_DESCRIPTOR_EXIT_ONLY, only consider nodes
2003 * with the Exit flag.
2004 * If *<b>descs_out</b> is present, add a node_t for each usable descriptor
2008 count_usable_descriptors(int *num_present
, int *num_usable
,
2009 smartlist_t
*descs_out
,
2010 const networkstatus_t
*consensus
,
2012 routerset_t
*in_set
,
2013 usable_descriptor_t exit_only
)
2015 const int md
= (consensus
->flavor
== FLAV_MICRODESC
);
2016 *num_present
= 0, *num_usable
= 0;
2018 SMARTLIST_FOREACH_BEGIN(consensus
->routerstatus_list
, routerstatus_t
*, rs
)
2020 const node_t
*node
= node_get_by_id(rs
->identity_digest
);
2022 continue; /* This would be a bug: every entry in the consensus is
2023 * supposed to have a node. */
2024 if (exit_only
== USABLE_DESCRIPTOR_EXIT_ONLY
&& ! rs
->is_exit
)
2026 if (in_set
&& ! routerset_contains_routerstatus(in_set
, rs
, -1))
2028 if (client_would_use_router(rs
, now
)) {
2029 const char * const digest
= rs
->descriptor_digest
;
2031 ++*num_usable
; /* the consensus says we want it. */
2033 present
= NULL
!= microdesc_cache_lookup_by_digest256(NULL
, digest
);
2035 present
= NULL
!= router_get_by_descriptor_digest(digest
);
2037 /* we have the descriptor listed in the consensus. */
2041 smartlist_add(descs_out
, (node_t
*)node
);
2044 SMARTLIST_FOREACH_END(rs
);
2046 log_debug(LD_DIR
, "%d usable, %d present (%s%s).",
2047 *num_usable
, *num_present
,
2048 md
? "microdesc" : "desc",
2049 exit_only
== USABLE_DESCRIPTOR_EXIT_ONLY
? " exits" : "s");
2052 /** Return an estimate of which fraction of usable paths through the Tor
2053 * network we have available for use. Count how many routers seem like ones
2054 * we'd use (store this in *<b>num_usable_out</b>), and how many of
2055 * <em>those</em> we have descriptors for (store this in
2056 * *<b>num_present_out</b>.)
2058 * If **<b>status_out</b> is present, allocate a new string and print the
2059 * available percentages of guard, middle, and exit nodes to it, noting
2060 * whether there are exits in the consensus.
2061 * If there are no exits in the consensus, we treat the exit fraction as 100%,
2062 * but set router_have_consensus_path() so that we can only build internal
2065 compute_frac_paths_available(const networkstatus_t
*consensus
,
2066 const or_options_t
*options
, time_t now
,
2067 int *num_present_out
, int *num_usable_out
,
2070 smartlist_t
*guards
= smartlist_new();
2071 smartlist_t
*mid
= smartlist_new();
2072 smartlist_t
*exits
= smartlist_new();
2073 double f_guard
, f_mid
, f_exit
;
2074 double f_path
= 0.0;
2075 /* Used to determine whether there are any exits in the consensus */
2077 /* Used to determine whether there are any exits with descriptors */
2079 const int authdir
= authdir_mode_v3(options
);
2081 count_usable_descriptors(num_present_out
, num_usable_out
,
2082 mid
, consensus
, now
, NULL
,
2083 USABLE_DESCRIPTOR_ALL
);
2084 if (options
->EntryNodes
) {
2085 count_usable_descriptors(&np
, &nu
, guards
, consensus
, now
,
2086 options
->EntryNodes
, USABLE_DESCRIPTOR_ALL
);
2088 SMARTLIST_FOREACH(mid
, const node_t
*, node
, {
2090 if (node
->rs
&& node
->rs
->is_possible_guard
)
2091 smartlist_add(guards
, (node_t
*)node
);
2093 if (node
->is_possible_guard
)
2094 smartlist_add(guards
, (node_t
*)node
);
2099 /* All nodes with exit flag
2100 * If we're in a network with TestingDirAuthVoteExit set,
2101 * this can cause false positives on have_consensus_path,
2102 * incorrectly setting it to CONSENSUS_PATH_EXIT. This is
2103 * an unavoidable feature of forcing authorities to declare
2104 * certain nodes as exits.
2106 count_usable_descriptors(&np
, &nu
, exits
, consensus
, now
,
2107 NULL
, USABLE_DESCRIPTOR_EXIT_ONLY
);
2109 "%s: %d present, %d usable",
2114 /* We need at least 1 exit present in the consensus to consider
2115 * building exit paths */
2116 /* Update our understanding of whether the consensus has exits */
2117 consensus_path_type_t old_have_consensus_path
= have_consensus_path
;
2118 have_consensus_path
= ((nu
> 0) ?
2119 CONSENSUS_PATH_EXIT
:
2120 CONSENSUS_PATH_INTERNAL
);
2122 if (have_consensus_path
== CONSENSUS_PATH_INTERNAL
2123 && old_have_consensus_path
!= have_consensus_path
) {
2125 "The current consensus has no exit nodes. "
2126 "Tor can only build internal paths, "
2127 "such as paths to hidden services.");
2129 /* However, exit nodes can reachability self-test using this consensus,
2130 * join the network, and appear in a later consensus. This will allow
2131 * the network to build exit paths, such as paths for world wide web
2132 * browsing (as distinct from hidden service web browsing). */
2135 f_guard
= frac_nodes_with_descriptors(guards
, WEIGHT_FOR_GUARD
);
2136 f_mid
= frac_nodes_with_descriptors(mid
, WEIGHT_FOR_MID
);
2137 f_exit
= frac_nodes_with_descriptors(exits
, WEIGHT_FOR_EXIT
);
2140 "f_guard: %.2f, f_mid: %.2f, f_exit: %.2f",
2145 smartlist_free(guards
);
2146 smartlist_free(mid
);
2147 smartlist_free(exits
);
2149 if (options
->ExitNodes
) {
2150 double f_myexit
, f_myexit_unflagged
;
2151 smartlist_t
*myexits
= smartlist_new();
2152 smartlist_t
*myexits_unflagged
= smartlist_new();
2154 /* All nodes with exit flag in ExitNodes option */
2155 count_usable_descriptors(&np
, &nu
, myexits
, consensus
, now
,
2156 options
->ExitNodes
, USABLE_DESCRIPTOR_EXIT_ONLY
);
2158 "%s: %d present, %d usable",
2163 /* Now compute the nodes in the ExitNodes option where which we don't know
2164 * what their exit policy is, or we know it permits something. */
2165 count_usable_descriptors(&np
, &nu
, myexits_unflagged
,
2167 options
->ExitNodes
, USABLE_DESCRIPTOR_ALL
);
2169 "%s: %d present, %d usable",
2170 "myexits_unflagged (initial)",
2174 SMARTLIST_FOREACH_BEGIN(myexits_unflagged
, const node_t
*, node
) {
2175 if (node_has_descriptor(node
) && node_exit_policy_rejects_all(node
)) {
2176 SMARTLIST_DEL_CURRENT(myexits_unflagged
, node
);
2177 /* this node is not actually an exit */
2179 /* this node is unusable as an exit */
2182 } SMARTLIST_FOREACH_END(node
);
2185 "%s: %d present, %d usable",
2186 "myexits_unflagged (final)",
2190 f_myexit
= frac_nodes_with_descriptors(myexits
,WEIGHT_FOR_EXIT
);
2192 frac_nodes_with_descriptors(myexits_unflagged
,WEIGHT_FOR_EXIT
);
2195 "f_exit: %.2f, f_myexit: %.2f, f_myexit_unflagged: %.2f",
2198 f_myexit_unflagged
);
2200 /* If our ExitNodes list has eliminated every possible Exit node, and there
2201 * were some possible Exit nodes, then instead consider nodes that permit
2202 * exiting to some ports. */
2203 if (smartlist_len(myexits
) == 0 &&
2204 smartlist_len(myexits_unflagged
)) {
2205 f_myexit
= f_myexit_unflagged
;
2208 smartlist_free(myexits
);
2209 smartlist_free(myexits_unflagged
);
2211 /* This is a tricky point here: we don't want to make it easy for a
2212 * directory to trickle exits to us until it learns which exits we have
2213 * configured, so require that we have a threshold both of total exits
2214 * and usable exits. */
2215 if (f_myexit
< f_exit
)
2219 /* if the consensus has no exits, treat the exit fraction as 100% */
2220 if (router_have_consensus_path() != CONSENSUS_PATH_EXIT
) {
2224 f_path
= f_guard
* f_mid
* f_exit
;
2227 tor_asprintf(status_out
,
2228 "%d%% of guards bw, "
2229 "%d%% of midpoint bw, and "
2230 "%d%% of exit bw%s = "
2235 (router_have_consensus_path() == CONSENSUS_PATH_EXIT
?
2237 " (no exits in consensus)"),
2243 /** We just fetched a new set of descriptors. Compute how far through
2244 * the "loading descriptors" bootstrapping phase we are, so we can inform
2245 * the controller of our progress. */
2247 count_loading_descriptors_progress(void)
2249 int num_present
= 0, num_usable
=0;
2250 time_t now
= time(NULL
);
2251 const or_options_t
*options
= get_options();
2252 const networkstatus_t
*consensus
=
2253 networkstatus_get_reasonably_live_consensus(now
,usable_consensus_flavor());
2254 double paths
, fraction
;
2257 return 0; /* can't count descriptors if we have no list of them */
2259 paths
= compute_frac_paths_available(consensus
, options
, now
,
2260 &num_present
, &num_usable
,
2263 fraction
= paths
/ get_frac_paths_needed_for_circs(options
,consensus
);
2265 return 0; /* it's not the number of descriptors holding us back */
2266 return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS
+ (int)
2267 (fraction
*(BOOTSTRAP_STATUS_CONN_OR
-1 -
2268 BOOTSTRAP_STATUS_LOADING_DESCRIPTORS
));
2271 /** Return the fraction of paths needed before we're willing to build
2272 * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
2274 get_frac_paths_needed_for_circs(const or_options_t
*options
,
2275 const networkstatus_t
*ns
)
2277 #define DFLT_PCT_USABLE_NEEDED 60
2278 if (options
->PathsNeededToBuildCircuits
>= 0.0) {
2279 return options
->PathsNeededToBuildCircuits
;
2281 return networkstatus_get_param(ns
, "min_paths_for_circs_pct",
2282 DFLT_PCT_USABLE_NEEDED
,
2287 /** Change the value of have_min_dir_info, setting it true iff we have enough
2288 * network and router information to build circuits. Clear the value of
2289 * need_to_update_have_min_dir_info. */
2291 update_router_have_minimum_dir_info(void)
2293 time_t now
= time(NULL
);
2295 int num_present
=0, num_usable
=0;
2296 const or_options_t
*options
= get_options();
2297 const networkstatus_t
*consensus
=
2298 networkstatus_get_reasonably_live_consensus(now
,usable_consensus_flavor());
2302 if (!networkstatus_get_latest_consensus())
2303 strlcpy(dir_info_status
, "We have no usable consensus.",
2304 sizeof(dir_info_status
));
2306 strlcpy(dir_info_status
, "We have no recent usable consensus.",
2307 sizeof(dir_info_status
));
2312 using_md
= consensus
->flavor
== FLAV_MICRODESC
;
2314 /* Check fraction of available paths */
2316 char *status
= NULL
;
2317 double paths
= compute_frac_paths_available(consensus
, options
, now
,
2318 &num_present
, &num_usable
,
2321 if (paths
< get_frac_paths_needed_for_circs(options
,consensus
)) {
2322 tor_snprintf(dir_info_status
, sizeof(dir_info_status
),
2323 "We need more %sdescriptors: we have %d/%d, and "
2324 "can only build %d%% of likely paths. (We have %s.)",
2325 using_md
?"micro":"", num_present
, num_usable
,
2326 (int)(paths
*100), status
);
2329 control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS
, 0);
2337 { /* Check entry guard dirinfo status */
2338 char *guard_error
= entry_guards_get_err_str_if_dir_info_missing(using_md
,
2342 strlcpy(dir_info_status
, guard_error
, sizeof(dir_info_status
));
2343 tor_free(guard_error
);
2351 /* If paths have just become available in this update. */
2352 if (res
&& !have_min_dir_info
) {
2353 control_event_client_status(LOG_NOTICE
, "ENOUGH_DIR_INFO");
2354 if (control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR
, 0) == 0) {
2356 "We now have enough directory information to build circuits.");
2360 /* If paths have just become unavailable in this update. */
2361 if (!res
&& have_min_dir_info
) {
2362 int quiet
= directory_too_idle_to_fetch_descriptors(options
, now
);
2363 tor_log(quiet
? LOG_INFO
: LOG_NOTICE
, LD_DIR
,
2364 "Our directory information is no longer up-to-date "
2365 "enough to build circuits: %s", dir_info_status
);
2367 /* a) make us log when we next complete a circuit, so we know when Tor
2368 * is back up and usable, and b) disable some activities that Tor
2369 * should only do while circuits are working, like reachability tests
2370 * and fetching bridge descriptors only over circuits. */
2371 note_that_we_maybe_cant_complete_circuits();
2372 have_consensus_path
= CONSENSUS_PATH_UNKNOWN
;
2373 control_event_client_status(LOG_NOTICE
, "NOT_ENOUGH_DIR_INFO");
2375 have_min_dir_info
= res
;
2376 need_to_update_have_min_dir_info
= 0;