Bump copyright date to 2019
[tor.git] / src / feature / nodelist / nodelist.c
blob9a277018037a9d3aa661df343f0e9f5494867e06
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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file nodelist.c
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,
28 * and then use it.
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
33 * should move!)
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
43 #include "core/or/or.h"
44 #include "app/config/config.h"
45 #include "core/mainloop/mainloop.h"
46 #include "core/mainloop/netstatus.h"
47 #include "core/or/address_set.h"
48 #include "core/or/policies.h"
49 #include "core/or/protover.h"
50 #include "feature/client/bridges.h"
51 #include "feature/client/entrynodes.h"
52 #include "feature/control/control.h"
53 #include "feature/dirauth/process_descs.h"
54 #include "feature/dircache/dirserv.h"
55 #include "feature/hs/hs_client.h"
56 #include "feature/hs/hs_common.h"
57 #include "feature/nodelist/describe.h"
58 #include "feature/nodelist/dirlist.h"
59 #include "feature/nodelist/microdesc.h"
60 #include "feature/nodelist/networkstatus.h"
61 #include "feature/nodelist/node_select.h"
62 #include "feature/nodelist/nodefamily.h"
63 #include "feature/nodelist/nodelist.h"
64 #include "feature/nodelist/routerlist.h"
65 #include "feature/nodelist/routerset.h"
66 #include "feature/nodelist/torcert.h"
67 #include "feature/rend/rendservice.h"
68 #include "lib/encoding/binascii.h"
69 #include "lib/err/backtrace.h"
70 #include "lib/geoip/geoip.h"
71 #include "lib/net/address.h"
73 #include <string.h>
75 #include "feature/dirauth/authmode.h"
77 #include "feature/dirclient/dir_server_st.h"
78 #include "feature/nodelist/microdesc_st.h"
79 #include "feature/nodelist/networkstatus_st.h"
80 #include "feature/nodelist/node_st.h"
81 #include "feature/nodelist/routerinfo_st.h"
82 #include "feature/nodelist/routerlist_st.h"
83 #include "feature/nodelist/routerstatus_st.h"
85 static void nodelist_drop_node(node_t *node, int remove_from_ht);
86 #define node_free(val) \
87 FREE_AND_NULL(node_t, node_free_, (val))
88 static void node_free_(node_t *node);
90 /** count_usable_descriptors counts descriptors with these flag(s)
92 typedef enum {
93 /* All descriptors regardless of flags or exit policies */
94 USABLE_DESCRIPTOR_ALL = 0U,
95 /* Only count descriptors with an exit policy that allows at least one port
97 USABLE_DESCRIPTOR_EXIT_POLICY = 1U << 0,
98 /* Only count descriptors for relays that have the exit flag in the
99 * consensus */
100 USABLE_DESCRIPTOR_EXIT_FLAG = 1U << 1,
101 /* Only count descriptors for relays that have the policy and the flag */
102 USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG = (USABLE_DESCRIPTOR_EXIT_POLICY |
103 USABLE_DESCRIPTOR_EXIT_FLAG)
104 } usable_descriptor_t;
105 static void count_usable_descriptors(int *num_present,
106 int *num_usable,
107 smartlist_t *descs_out,
108 const networkstatus_t *consensus,
109 time_t now,
110 routerset_t *in_set,
111 usable_descriptor_t exit_only);
112 static void update_router_have_minimum_dir_info(void);
113 static double get_frac_paths_needed_for_circs(const or_options_t *options,
114 const networkstatus_t *ns);
115 static void node_add_to_address_set(const node_t *node);
117 /** A nodelist_t holds a node_t object for every router we're "willing to use
118 * for something". Specifically, it should hold a node_t for every node that
119 * is currently in the routerlist, or currently in the consensus we're using.
121 typedef struct nodelist_t {
122 /* A list of all the nodes. */
123 smartlist_t *nodes;
124 /* Hash table to map from node ID digest to node. */
125 HT_HEAD(nodelist_map, node_t) nodes_by_id;
126 /* Hash table to map from node Ed25519 ID to node.
128 * Whenever a node's routerinfo or microdescriptor is about to change,
129 * you should remove it from this map with node_remove_from_ed25519_map().
130 * Whenever a node's routerinfo or microdescriptor has just chaned,
131 * you should add it to this map with node_add_to_ed25519_map().
133 HT_HEAD(nodelist_ed_map, node_t) nodes_by_ed_id;
135 /* Set of addresses that belong to nodes we believe in. */
136 address_set_t *node_addrs;
138 /* The valid-after time of the last live consensus that initialized the
139 * nodelist. We use this to detect outdated nodelists that need to be
140 * rebuilt using a newer consensus. */
141 time_t live_consensus_valid_after;
142 } nodelist_t;
144 static inline unsigned int
145 node_id_hash(const node_t *node)
147 return (unsigned) siphash24g(node->identity, DIGEST_LEN);
150 static inline unsigned int
151 node_id_eq(const node_t *node1, const node_t *node2)
153 return tor_memeq(node1->identity, node2->identity, DIGEST_LEN);
156 HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq)
157 HT_GENERATE2(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
158 0.6, tor_reallocarray_, tor_free_)
160 static inline unsigned int
161 node_ed_id_hash(const node_t *node)
163 return (unsigned) siphash24g(node->ed25519_id.pubkey, ED25519_PUBKEY_LEN);
166 static inline unsigned int
167 node_ed_id_eq(const node_t *node1, const node_t *node2)
169 return ed25519_pubkey_eq(&node1->ed25519_id, &node2->ed25519_id);
172 HT_PROTOTYPE(nodelist_ed_map, node_t, ed_ht_ent, node_ed_id_hash,
173 node_ed_id_eq)
174 HT_GENERATE2(nodelist_ed_map, node_t, ed_ht_ent, node_ed_id_hash,
175 node_ed_id_eq, 0.6, tor_reallocarray_, tor_free_)
177 /** The global nodelist. */
178 static nodelist_t *the_nodelist=NULL;
180 /** Create an empty nodelist if we haven't done so already. */
181 static void
182 init_nodelist(void)
184 if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
185 the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
186 HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
187 HT_INIT(nodelist_ed_map, &the_nodelist->nodes_by_ed_id);
188 the_nodelist->nodes = smartlist_new();
192 /** As node_get_by_id, but returns a non-const pointer */
193 MOCK_IMPL(node_t *,
194 node_get_mutable_by_id,(const char *identity_digest))
196 node_t search, *node;
197 if (PREDICT_UNLIKELY(the_nodelist == NULL))
198 return NULL;
200 memcpy(&search.identity, identity_digest, DIGEST_LEN);
201 node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
202 return node;
205 /** As node_get_by_ed25519_id, but returns a non-const pointer */
206 node_t *
207 node_get_mutable_by_ed25519_id(const ed25519_public_key_t *ed_id)
209 node_t search, *node;
210 if (PREDICT_UNLIKELY(the_nodelist == NULL))
211 return NULL;
212 if (BUG(ed_id == NULL) || BUG(ed25519_public_key_is_zero(ed_id)))
213 return NULL;
215 memcpy(&search.ed25519_id, ed_id, sizeof(search.ed25519_id));
216 node = HT_FIND(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, &search);
217 return node;
220 /** Return the node_t whose identity is <b>identity_digest</b>, or NULL
221 * if no such node exists. */
222 MOCK_IMPL(const node_t *,
223 node_get_by_id,(const char *identity_digest))
225 return node_get_mutable_by_id(identity_digest);
228 /** Return the node_t whose ed25519 identity is <b>ed_id</b>, or NULL
229 * if no such node exists. */
230 MOCK_IMPL(const node_t *,
231 node_get_by_ed25519_id,(const ed25519_public_key_t *ed_id))
233 return node_get_mutable_by_ed25519_id(ed_id);
236 /** Internal: return the node_t whose identity_digest is
237 * <b>identity_digest</b>. If none exists, create a new one, add it to the
238 * nodelist, and return it.
240 * Requires that the nodelist be initialized.
242 static node_t *
243 node_get_or_create(const char *identity_digest)
245 node_t *node;
247 if ((node = node_get_mutable_by_id(identity_digest)))
248 return node;
250 node = tor_malloc_zero(sizeof(node_t));
251 memcpy(node->identity, identity_digest, DIGEST_LEN);
252 HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
254 smartlist_add(the_nodelist->nodes, node);
255 node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
257 node->country = -1;
259 return node;
262 /** Remove <b>node</b> from the ed25519 map (if it present), and
263 * set its ed25519_id field to zero. */
264 static int
265 node_remove_from_ed25519_map(node_t *node)
267 tor_assert(the_nodelist);
268 tor_assert(node);
270 if (ed25519_public_key_is_zero(&node->ed25519_id)) {
271 return 0;
274 int rv = 0;
275 node_t *search =
276 HT_FIND(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
277 if (BUG(search != node)) {
278 goto clear_and_return;
281 search = HT_REMOVE(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
282 tor_assert(search == node);
283 rv = 1;
285 clear_and_return:
286 memset(&node->ed25519_id, 0, sizeof(node->ed25519_id));
287 return rv;
290 /** Helper function to log details of duplicated ed2559_ids */
291 static void
292 node_log_dup_ed_id(const node_t *old, const node_t *node, const char *ed_id)
294 char *s;
295 char *olddesc = tor_strdup(node_describe(old));
297 tor_asprintf(&s, "Reused ed25519_id %s: old %s new %s", ed_id,
298 olddesc, node_describe(node));
299 log_backtrace(LOG_NOTICE, LD_DIR, s);
300 tor_free(olddesc);
301 tor_free(s);
304 /** If <b>node</b> has an ed25519 id, and it is not already in the ed25519 id
305 * map, set its ed25519_id field, and add it to the ed25519 map.
307 static int
308 node_add_to_ed25519_map(node_t *node)
310 tor_assert(the_nodelist);
311 tor_assert(node);
313 if (! ed25519_public_key_is_zero(&node->ed25519_id)) {
314 return 0;
317 const ed25519_public_key_t *key = node_get_ed25519_id(node);
318 if (!key) {
319 return 0;
322 node_t *old;
323 memcpy(&node->ed25519_id, key, sizeof(node->ed25519_id));
324 old = HT_FIND(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
325 if (old) {
326 char ed_id[BASE32_BUFSIZE(sizeof(key->pubkey))];
328 base32_encode(ed_id, sizeof(ed_id), (const char *)key->pubkey,
329 sizeof(key->pubkey));
330 if (BUG(old == node)) {
331 /* Actual bug: all callers of this function call
332 * node_remove_from_ed25519_map first. */
333 log_err(LD_BUG,
334 "Unexpectedly found deleted node with ed25519_id %s", ed_id);
335 } else {
336 /* Distinct nodes sharing a ed25519 id, possibly due to relay
337 * misconfiguration. The key pinning might not catch this,
338 * possibly due to downloading a missing descriptor during
339 * consensus voting. */
340 node_log_dup_ed_id(old, node, ed_id);
341 memset(&node->ed25519_id, 0, sizeof(node->ed25519_id));
343 return 0;
346 HT_INSERT(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
347 return 1;
350 /* For a given <b>node</b> for the consensus <b>ns</b>, set the hsdir index
351 * for the node, both current and next if possible. This can only fails if the
352 * node_t ed25519 identity key can't be found which would be a bug. */
353 STATIC void
354 node_set_hsdir_index(node_t *node, const networkstatus_t *ns)
356 time_t now = approx_time();
357 const ed25519_public_key_t *node_identity_pk;
358 uint8_t *fetch_srv = NULL, *store_first_srv = NULL, *store_second_srv = NULL;
359 uint64_t next_time_period_num, current_time_period_num;
360 uint64_t fetch_tp, store_first_tp, store_second_tp;
362 tor_assert(node);
363 tor_assert(ns);
365 if (!networkstatus_is_live(ns, now)) {
366 static struct ratelim_t live_consensus_ratelim = RATELIM_INIT(30 * 60);
367 log_fn_ratelim(&live_consensus_ratelim, LOG_INFO, LD_GENERAL,
368 "Not setting hsdir index with a non-live consensus.");
369 goto done;
372 node_identity_pk = node_get_ed25519_id(node);
373 if (node_identity_pk == NULL) {
374 log_debug(LD_GENERAL, "ed25519 identity public key not found when "
375 "trying to build the hsdir indexes for node %s",
376 node_describe(node));
377 goto done;
380 /* Get the current and next time period number. */
381 current_time_period_num = hs_get_time_period_num(0);
382 next_time_period_num = hs_get_next_time_period_num(0);
384 /* We always use the current time period for fetching descs */
385 fetch_tp = current_time_period_num;
387 /* Now extract the needed SRVs and time periods for building hsdir indices */
388 if (hs_in_period_between_tp_and_srv(ns, now)) {
389 fetch_srv = hs_get_current_srv(fetch_tp, ns);
391 store_first_tp = hs_get_previous_time_period_num(0);
392 store_second_tp = current_time_period_num;
393 } else {
394 fetch_srv = hs_get_previous_srv(fetch_tp, ns);
396 store_first_tp = current_time_period_num;
397 store_second_tp = next_time_period_num;
400 /* We always use the old SRV for storing the first descriptor and the latest
401 * SRV for storing the second descriptor */
402 store_first_srv = hs_get_previous_srv(store_first_tp, ns);
403 store_second_srv = hs_get_current_srv(store_second_tp, ns);
405 /* Build the fetch index. */
406 hs_build_hsdir_index(node_identity_pk, fetch_srv, fetch_tp,
407 node->hsdir_index.fetch);
409 /* If we are in the time segment between SRV#N and TP#N, the fetch index is
410 the same as the first store index */
411 if (!hs_in_period_between_tp_and_srv(ns, now)) {
412 memcpy(node->hsdir_index.store_first, node->hsdir_index.fetch,
413 sizeof(node->hsdir_index.store_first));
414 } else {
415 hs_build_hsdir_index(node_identity_pk, store_first_srv, store_first_tp,
416 node->hsdir_index.store_first);
419 /* If we are in the time segment between TP#N and SRV#N+1, the fetch index is
420 the same as the second store index */
421 if (hs_in_period_between_tp_and_srv(ns, now)) {
422 memcpy(node->hsdir_index.store_second, node->hsdir_index.fetch,
423 sizeof(node->hsdir_index.store_second));
424 } else {
425 hs_build_hsdir_index(node_identity_pk, store_second_srv, store_second_tp,
426 node->hsdir_index.store_second);
429 done:
430 tor_free(fetch_srv);
431 tor_free(store_first_srv);
432 tor_free(store_second_srv);
433 return;
436 /** Called when a node's address changes. */
437 static void
438 node_addrs_changed(node_t *node)
440 node->last_reachable = node->last_reachable6 = 0;
441 node->country = -1;
444 /** Add all address information about <b>node</b> to the current address
445 * set (if there is one).
447 static void
448 node_add_to_address_set(const node_t *node)
450 if (!the_nodelist || !the_nodelist->node_addrs)
451 return;
453 /* These various address sources can be redundant, but it's likely faster
454 * to add them all than to compare them all for equality. */
456 if (node->rs) {
457 if (node->rs->addr)
458 address_set_add_ipv4h(the_nodelist->node_addrs, node->rs->addr);
459 if (!tor_addr_is_null(&node->rs->ipv6_addr))
460 address_set_add(the_nodelist->node_addrs, &node->rs->ipv6_addr);
462 if (node->ri) {
463 if (node->ri->addr)
464 address_set_add_ipv4h(the_nodelist->node_addrs, node->ri->addr);
465 if (!tor_addr_is_null(&node->ri->ipv6_addr))
466 address_set_add(the_nodelist->node_addrs, &node->ri->ipv6_addr);
468 if (node->md) {
469 if (!tor_addr_is_null(&node->md->ipv6_addr))
470 address_set_add(the_nodelist->node_addrs, &node->md->ipv6_addr);
474 /** Return true if <b>addr</b> is the address of some node in the nodelist.
475 * If not, probably return false. */
477 nodelist_probably_contains_address(const tor_addr_t *addr)
479 if (BUG(!addr))
480 return 0;
482 if (!the_nodelist || !the_nodelist->node_addrs)
483 return 0;
485 return address_set_probably_contains(the_nodelist->node_addrs, addr);
488 /** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
489 * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
490 * to the previous routerinfo.
492 node_t *
493 nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
495 node_t *node;
496 const char *id_digest;
497 int had_router = 0;
498 tor_assert(ri);
500 init_nodelist();
501 id_digest = ri->cache_info.identity_digest;
502 node = node_get_or_create(id_digest);
504 node_remove_from_ed25519_map(node);
506 if (node->ri) {
507 if (!routers_have_same_or_addrs(node->ri, ri)) {
508 node_addrs_changed(node);
510 had_router = 1;
511 if (ri_old_out)
512 *ri_old_out = node->ri;
513 } else {
514 if (ri_old_out)
515 *ri_old_out = NULL;
517 node->ri = ri;
519 node_add_to_ed25519_map(node);
521 if (node->country == -1)
522 node_set_country(node);
524 if (authdir_mode(get_options()) && !had_router) {
525 const char *discard=NULL;
526 uint32_t status = dirserv_router_get_status(ri, &discard, LOG_INFO);
527 dirserv_set_node_flags_from_authoritative_status(node, status);
530 /* Setting the HSDir index requires the ed25519 identity key which can
531 * only be found either in the ri or md. This is why this is called here.
532 * Only nodes supporting HSDir=2 protocol version needs this index. */
533 if (node->rs && node->rs->pv.supports_v3_hsdir) {
534 node_set_hsdir_index(node,
535 networkstatus_get_latest_consensus());
538 node_add_to_address_set(node);
540 return node;
543 /** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
545 * Called when a new microdesc has arrived and the usable consensus flavor
546 * is "microdesc".
548 node_t *
549 nodelist_add_microdesc(microdesc_t *md)
551 networkstatus_t *ns =
552 networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
553 const routerstatus_t *rs;
554 node_t *node;
555 if (ns == NULL)
556 return NULL;
557 init_nodelist();
559 /* Microdescriptors don't carry an identity digest, so we need to figure
560 * it out by looking up the routerstatus. */
561 rs = router_get_consensus_status_by_descriptor_digest(ns, md->digest);
562 if (rs == NULL)
563 return NULL;
564 node = node_get_mutable_by_id(rs->identity_digest);
565 if (node == NULL)
566 return NULL;
568 node_remove_from_ed25519_map(node);
569 if (node->md)
570 node->md->held_by_nodes--;
572 node->md = md;
573 md->held_by_nodes++;
574 /* Setting the HSDir index requires the ed25519 identity key which can
575 * only be found either in the ri or md. This is why this is called here.
576 * Only nodes supporting HSDir=2 protocol version needs this index. */
577 if (rs->pv.supports_v3_hsdir) {
578 node_set_hsdir_index(node, ns);
580 node_add_to_ed25519_map(node);
581 node_add_to_address_set(node);
583 return node;
586 /* Default value. */
587 #define ESTIMATED_ADDRESS_PER_NODE 2
589 /* Return the estimated number of address per node_t. This is used for the
590 * size of the bloom filter in the nodelist (node_addrs). */
591 MOCK_IMPL(int,
592 get_estimated_address_per_node, (void))
594 return ESTIMATED_ADDRESS_PER_NODE;
597 /** Tell the nodelist that the current usable consensus is <b>ns</b>.
598 * This makes the nodelist change all of the routerstatus entries for
599 * the nodes, drop nodes that no longer have enough info to get used,
600 * and grab microdescriptors into nodes as appropriate.
602 void
603 nodelist_set_consensus(networkstatus_t *ns)
605 const or_options_t *options = get_options();
606 int authdir = authdir_mode_v3(options);
608 init_nodelist();
609 if (ns->flavor == FLAV_MICRODESC)
610 (void) get_microdesc_cache(); /* Make sure it exists first. */
612 SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
613 node->rs = NULL);
615 /* Conservatively estimate that every node will have 2 addresses. */
616 const int estimated_addresses = smartlist_len(ns->routerstatus_list) *
617 get_estimated_address_per_node();
618 address_set_free(the_nodelist->node_addrs);
619 the_nodelist->node_addrs = address_set_new(estimated_addresses);
621 SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
622 node_t *node = node_get_or_create(rs->identity_digest);
623 node->rs = rs;
624 if (ns->flavor == FLAV_MICRODESC) {
625 if (node->md == NULL ||
626 tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
627 node_remove_from_ed25519_map(node);
628 if (node->md)
629 node->md->held_by_nodes--;
630 node->md = microdesc_cache_lookup_by_digest256(NULL,
631 rs->descriptor_digest);
632 if (node->md)
633 node->md->held_by_nodes++;
634 node_add_to_ed25519_map(node);
638 if (rs->pv.supports_v3_hsdir) {
639 node_set_hsdir_index(node, ns);
641 node_set_country(node);
643 /* If we're not an authdir, believe others. */
644 if (!authdir) {
645 node->is_valid = rs->is_valid;
646 node->is_running = rs->is_flagged_running;
647 node->is_fast = rs->is_fast;
648 node->is_stable = rs->is_stable;
649 node->is_possible_guard = rs->is_possible_guard;
650 node->is_exit = rs->is_exit;
651 node->is_bad_exit = rs->is_bad_exit;
652 node->is_hs_dir = rs->is_hs_dir;
653 node->ipv6_preferred = 0;
654 if (fascist_firewall_prefer_ipv6_orport(options) &&
655 (tor_addr_is_null(&rs->ipv6_addr) == 0 ||
656 (node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
657 node->ipv6_preferred = 1;
660 } SMARTLIST_FOREACH_END(rs);
662 nodelist_purge();
664 /* Now add all the nodes we have to the address set. */
665 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
666 node_add_to_address_set(node);
667 } SMARTLIST_FOREACH_END(node);
669 if (! authdir) {
670 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
671 /* We have no routerstatus for this router. Clear flags so we can skip
672 * it, maybe.*/
673 if (!node->rs) {
674 tor_assert(node->ri); /* if it had only an md, or nothing, purge
675 * would have removed it. */
676 if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
677 /* Clear all flags. */
678 node->is_valid = node->is_running = node->is_hs_dir =
679 node->is_fast = node->is_stable =
680 node->is_possible_guard = node->is_exit =
681 node->is_bad_exit = node->ipv6_preferred = 0;
684 } SMARTLIST_FOREACH_END(node);
687 /* If the consensus is live, note down the consensus valid-after that formed
688 * the nodelist. */
689 if (networkstatus_is_live(ns, approx_time())) {
690 the_nodelist->live_consensus_valid_after = ns->valid_after;
694 /** Return 1 iff <b>node</b> has Exit flag and no BadExit flag.
695 * Otherwise, return 0.
698 node_is_good_exit(const node_t *node)
700 return node->is_exit && ! node->is_bad_exit;
703 /** Helper: return true iff a node has a usable amount of information*/
704 static inline int
705 node_is_usable(const node_t *node)
707 return (node->rs) || (node->ri);
710 /** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
711 * node with <b>identity_digest</b>. */
712 void
713 nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
715 node_t *node = node_get_mutable_by_id(identity_digest);
716 if (node && node->md == md) {
717 node->md = NULL;
718 md->held_by_nodes--;
719 if (! node_get_ed25519_id(node)) {
720 node_remove_from_ed25519_map(node);
725 /** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
726 void
727 nodelist_remove_routerinfo(routerinfo_t *ri)
729 node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
730 if (node && node->ri == ri) {
731 node->ri = NULL;
732 if (! node_is_usable(node)) {
733 nodelist_drop_node(node, 1);
734 node_free(node);
739 /** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
740 * with.) */
741 static void
742 nodelist_drop_node(node_t *node, int remove_from_ht)
744 node_t *tmp;
745 int idx;
746 if (remove_from_ht) {
747 tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
748 tor_assert(tmp == node);
750 node_remove_from_ed25519_map(node);
752 idx = node->nodelist_idx;
753 tor_assert(idx >= 0);
755 tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
756 smartlist_del(the_nodelist->nodes, idx);
757 if (idx < smartlist_len(the_nodelist->nodes)) {
758 tmp = smartlist_get(the_nodelist->nodes, idx);
759 tmp->nodelist_idx = idx;
761 node->nodelist_idx = -1;
764 /** Return a newly allocated smartlist of the nodes that have <b>md</b> as
765 * their microdescriptor. */
766 smartlist_t *
767 nodelist_find_nodes_with_microdesc(const microdesc_t *md)
769 smartlist_t *result = smartlist_new();
771 if (the_nodelist == NULL)
772 return result;
774 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
775 if (node->md == md) {
776 smartlist_add(result, node);
778 } SMARTLIST_FOREACH_END(node);
780 return result;
783 /** Release storage held by <b>node</b> */
784 static void
785 node_free_(node_t *node)
787 if (!node)
788 return;
789 if (node->md)
790 node->md->held_by_nodes--;
791 tor_assert(node->nodelist_idx == -1);
792 tor_free(node);
795 /** Remove all entries from the nodelist that don't have enough info to be
796 * usable for anything. */
797 void
798 nodelist_purge(void)
800 node_t **iter;
801 if (PREDICT_UNLIKELY(the_nodelist == NULL))
802 return;
804 /* Remove the non-usable nodes. */
805 for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
806 node_t *node = *iter;
808 if (node->md && !node->rs) {
809 /* An md is only useful if there is an rs. */
810 node->md->held_by_nodes--;
811 node->md = NULL;
814 if (node_is_usable(node)) {
815 iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
816 } else {
817 iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
818 nodelist_drop_node(node, 0);
819 node_free(node);
822 nodelist_assert_ok();
825 /** Release all storage held by the nodelist. */
826 void
827 nodelist_free_all(void)
829 if (PREDICT_UNLIKELY(the_nodelist == NULL))
830 return;
832 HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
833 HT_CLEAR(nodelist_ed_map, &the_nodelist->nodes_by_ed_id);
834 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
835 node->nodelist_idx = -1;
836 node_free(node);
837 } SMARTLIST_FOREACH_END(node);
839 smartlist_free(the_nodelist->nodes);
841 address_set_free(the_nodelist->node_addrs);
842 the_nodelist->node_addrs = NULL;
844 tor_free(the_nodelist);
847 /** Check that the nodelist is internally consistent, and consistent with
848 * the directory info it's derived from.
850 void
851 nodelist_assert_ok(void)
853 routerlist_t *rl = router_get_routerlist();
854 networkstatus_t *ns = networkstatus_get_latest_consensus();
855 digestmap_t *dm;
857 if (!the_nodelist)
858 return;
860 dm = digestmap_new();
862 /* every routerinfo in rl->routers should be in the nodelist. */
863 if (rl) {
864 SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
865 const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
866 tor_assert(node && node->ri == ri);
867 tor_assert(fast_memeq(ri->cache_info.identity_digest,
868 node->identity, DIGEST_LEN));
869 tor_assert(! digestmap_get(dm, node->identity));
870 digestmap_set(dm, node->identity, (void*)node);
871 } SMARTLIST_FOREACH_END(ri);
874 /* every routerstatus in ns should be in the nodelist */
875 if (ns) {
876 SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
877 const node_t *node = node_get_by_id(rs->identity_digest);
878 tor_assert(node && node->rs == rs);
879 tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
880 digestmap_set(dm, node->identity, (void*)node);
881 if (ns->flavor == FLAV_MICRODESC) {
882 /* If it's a microdesc consensus, every entry that has a
883 * microdescriptor should be in the nodelist.
885 microdesc_t *md =
886 microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
887 tor_assert(md == node->md);
888 if (md)
889 tor_assert(md->held_by_nodes >= 1);
891 } SMARTLIST_FOREACH_END(rs);
894 /* The nodelist should have no other entries, and its entries should be
895 * well-formed. */
896 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
897 tor_assert(digestmap_get(dm, node->identity) != NULL);
898 tor_assert(node_sl_idx == node->nodelist_idx);
899 } SMARTLIST_FOREACH_END(node);
901 /* Every node listed with an ed25519 identity should be listed by that
902 * identity.
904 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
905 if (!ed25519_public_key_is_zero(&node->ed25519_id)) {
906 tor_assert(node == node_get_by_ed25519_id(&node->ed25519_id));
908 } SMARTLIST_FOREACH_END(node);
910 node_t **idx;
911 HT_FOREACH(idx, nodelist_ed_map, &the_nodelist->nodes_by_ed_id) {
912 node_t *node = *idx;
913 tor_assert(node == node_get_by_ed25519_id(&node->ed25519_id));
916 tor_assert((long)smartlist_len(the_nodelist->nodes) ==
917 (long)HT_SIZE(&the_nodelist->nodes_by_id));
919 tor_assert((long)smartlist_len(the_nodelist->nodes) >=
920 (long)HT_SIZE(&the_nodelist->nodes_by_ed_id));
922 digestmap_free(dm, NULL);
925 /** Ensure that the nodelist has been created with the most recent consensus.
926 * If that's not the case, make it so. */
927 void
928 nodelist_ensure_freshness(networkstatus_t *ns)
930 tor_assert(ns);
932 /* We don't even have a nodelist: this is a NOP. */
933 if (!the_nodelist) {
934 return;
937 if (the_nodelist->live_consensus_valid_after != ns->valid_after) {
938 log_info(LD_GENERAL, "Nodelist was not fresh: rebuilding. (%d / %d)",
939 (int) the_nodelist->live_consensus_valid_after,
940 (int) ns->valid_after);
941 nodelist_set_consensus(ns);
944 /** Return a list of a node_t * for every node we know about. The caller
945 * MUST NOT modify the list. (You can set and clear flags in the nodes if
946 * you must, but you must not add or remove nodes.) */
947 MOCK_IMPL(smartlist_t *,
948 nodelist_get_list,(void))
950 init_nodelist();
951 return the_nodelist->nodes;
954 /** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
955 * or $DIGEST~name, return the node with the matching identity digest and
956 * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
957 * is not well-formed. DOCDOC flags */
958 const node_t *
959 node_get_by_hex_id(const char *hex_id, unsigned flags)
961 char digest_buf[DIGEST_LEN];
962 char nn_buf[MAX_NICKNAME_LEN+1];
963 char nn_char='\0';
965 (void) flags; // XXXX
967 if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
968 const node_t *node = node_get_by_id(digest_buf);
969 if (!node)
970 return NULL;
971 if (nn_char == '=') {
972 /* "=" indicates a Named relay, but there aren't any of those now. */
973 return NULL;
975 return node;
978 return NULL;
981 /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
982 * the corresponding node_t, or NULL if none exists. Warn the user if they
983 * have specified a router by nickname, unless the NNF_NO_WARN_UNNAMED bit is
984 * set in <b>flags</b>. */
985 MOCK_IMPL(const node_t *,
986 node_get_by_nickname,(const char *nickname, unsigned flags))
988 const int warn_if_unnamed = !(flags & NNF_NO_WARN_UNNAMED);
990 if (!the_nodelist)
991 return NULL;
993 /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
995 const node_t *node;
996 if ((node = node_get_by_hex_id(nickname, flags)) != NULL)
997 return node;
1000 if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
1001 return NULL;
1003 /* Okay, so the name is not canonical for anybody. */
1005 smartlist_t *matches = smartlist_new();
1006 const node_t *choice = NULL;
1008 SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
1009 if (!strcasecmp(node_get_nickname(node), nickname))
1010 smartlist_add(matches, node);
1011 } SMARTLIST_FOREACH_END(node);
1013 if (smartlist_len(matches)>1 && warn_if_unnamed) {
1014 int any_unwarned = 0;
1015 SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
1016 if (!node->name_lookup_warned) {
1017 node->name_lookup_warned = 1;
1018 any_unwarned = 1;
1020 } SMARTLIST_FOREACH_END(node);
1022 if (any_unwarned) {
1023 log_warn(LD_CONFIG, "There are multiple matches for the name %s. "
1024 "Choosing one arbitrarily.", nickname);
1026 } else if (smartlist_len(matches)==1 && warn_if_unnamed) {
1027 char fp[HEX_DIGEST_LEN+1];
1028 node_t *node = smartlist_get(matches, 0);
1029 if (! node->name_lookup_warned) {
1030 base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
1031 log_warn(LD_CONFIG,
1032 "You specified a relay \"%s\" by name, but nicknames can be "
1033 "used by any relay, not just the one you meant. "
1034 "To make sure you get the same relay in the future, refer "
1035 "to it by key, as \"$%s\".", nickname, fp);
1036 node->name_lookup_warned = 1;
1040 if (smartlist_len(matches))
1041 choice = smartlist_get(matches, 0);
1043 smartlist_free(matches);
1044 return choice;
1048 /** Return the Ed25519 identity key for the provided node, or NULL if it
1049 * doesn't have one. */
1050 const ed25519_public_key_t *
1051 node_get_ed25519_id(const node_t *node)
1053 const ed25519_public_key_t *ri_pk = NULL;
1054 const ed25519_public_key_t *md_pk = NULL;
1056 if (node->ri) {
1057 if (node->ri->cache_info.signing_key_cert) {
1058 ri_pk = &node->ri->cache_info.signing_key_cert->signing_key;
1059 /* Checking whether routerinfo ed25519 is all zero.
1060 * Our descriptor parser should make sure this never happens. */
1061 if (BUG(ed25519_public_key_is_zero(ri_pk)))
1062 ri_pk = NULL;
1066 if (node->md) {
1067 if (node->md->ed25519_identity_pkey) {
1068 md_pk = node->md->ed25519_identity_pkey;
1069 /* Checking whether microdesc ed25519 is all zero.
1070 * Our descriptor parser should make sure this never happens. */
1071 if (BUG(ed25519_public_key_is_zero(md_pk)))
1072 md_pk = NULL;
1076 if (ri_pk && md_pk) {
1077 if (ed25519_pubkey_eq(ri_pk, md_pk)) {
1078 return ri_pk;
1079 } else {
1080 /* This can happen if the relay gets flagged NoEdConsensus which will be
1081 * triggered on all relays of the network. Thus a protocol warning. */
1082 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1083 "Inconsistent ed25519 identities in the nodelist");
1084 return NULL;
1086 } else if (ri_pk) {
1087 return ri_pk;
1088 } else {
1089 return md_pk;
1093 /** Return true iff this node's Ed25519 identity matches <b>id</b>.
1094 * (An absent Ed25519 identity matches NULL or zero.) */
1096 node_ed25519_id_matches(const node_t *node, const ed25519_public_key_t *id)
1098 const ed25519_public_key_t *node_id = node_get_ed25519_id(node);
1099 if (node_id == NULL || ed25519_public_key_is_zero(node_id)) {
1100 return id == NULL || ed25519_public_key_is_zero(id);
1101 } else {
1102 return id && ed25519_pubkey_eq(node_id, id);
1106 /** Dummy object that should be unreturnable. Used to ensure that
1107 * node_get_protover_summary_flags() always returns non-NULL. */
1108 static const protover_summary_flags_t zero_protover_flags = {
1109 0,0,0,0,0,0,0,0
1112 /** Return the protover_summary_flags for a given node. */
1113 static const protover_summary_flags_t *
1114 node_get_protover_summary_flags(const node_t *node)
1116 if (node->rs) {
1117 return &node->rs->pv;
1118 } else if (node->ri) {
1119 return &node->ri->pv;
1120 } else {
1121 /* This should be impossible: every node should have a routerstatus or a
1122 * router descriptor or both. But just in case we've messed up somehow,
1123 * return a nice empty set of flags to indicate "this node supports
1124 * nothing." */
1125 tor_assert_nonfatal_unreached_once();
1126 return &zero_protover_flags;
1130 /** Return true iff <b>node</b> supports authenticating itself
1131 * by ed25519 ID during the link handshake. If <b>compatible_with_us</b>,
1132 * it needs to be using a link authentication method that we understand.
1133 * If not, any plausible link authentication method will do. */
1135 node_supports_ed25519_link_authentication(const node_t *node,
1136 int compatible_with_us)
1138 if (! node_get_ed25519_id(node))
1139 return 0;
1141 const protover_summary_flags_t *pv = node_get_protover_summary_flags(node);
1143 if (compatible_with_us)
1144 return pv->supports_ed25519_link_handshake_compat;
1145 else
1146 return pv->supports_ed25519_link_handshake_any;
1149 /** Return true iff <b>node</b> supports the hidden service directory version
1150 * 3 protocol (proposal 224). */
1152 node_supports_v3_hsdir(const node_t *node)
1154 tor_assert(node);
1156 return node_get_protover_summary_flags(node)->supports_v3_hsdir;
1159 /** Return true iff <b>node</b> supports ed25519 authentication as an hidden
1160 * service introduction point.*/
1162 node_supports_ed25519_hs_intro(const node_t *node)
1164 tor_assert(node);
1166 return node_get_protover_summary_flags(node)->supports_ed25519_hs_intro;
1169 /** Return true iff <b>node</b> supports to be a rendezvous point for hidden
1170 * service version 3 (HSRend=2). */
1172 node_supports_v3_rendezvous_point(const node_t *node)
1174 tor_assert(node);
1176 /* We can't use a v3 rendezvous point without the curve25519 onion pk. */
1177 if (!node_get_curve25519_onion_key(node)) {
1178 return 0;
1181 return node_get_protover_summary_flags(node)->supports_v3_rendezvous_point;
1184 /** Return the RSA ID key's SHA1 digest for the provided node. */
1185 const uint8_t *
1186 node_get_rsa_id_digest(const node_t *node)
1188 tor_assert(node);
1189 return (const uint8_t*)node->identity;
1192 /** Return the nickname of <b>node</b>, or NULL if we can't find one. */
1193 const char *
1194 node_get_nickname(const node_t *node)
1196 tor_assert(node);
1197 if (node->rs)
1198 return node->rs->nickname;
1199 else if (node->ri)
1200 return node->ri->nickname;
1201 else
1202 return NULL;
1205 /** Return true iff <b>node</b> appears to be a directory authority or
1206 * directory cache */
1208 node_is_dir(const node_t *node)
1210 if (node->rs) {
1211 routerstatus_t * rs = node->rs;
1212 /* This is true if supports_tunnelled_dir_requests is true which
1213 * indicates that we support directory request tunnelled or through the
1214 * DirPort. */
1215 return rs->is_v2_dir;
1216 } else if (node->ri) {
1217 routerinfo_t * ri = node->ri;
1218 /* Both tunnelled request is supported or DirPort is set. */
1219 return ri->supports_tunnelled_dir_requests;
1220 } else {
1221 return 0;
1225 /** Return true iff <b>node</b> has either kind of descriptor -- that
1226 * is, a routerdescriptor or a microdescriptor.
1228 * You should probably use node_has_preferred_descriptor() instead.
1231 node_has_any_descriptor(const node_t *node)
1233 return (node->ri ||
1234 (node->rs && node->md));
1237 /** Return true iff <b>node</b> has the kind of descriptor we would prefer to
1238 * use for it, given our configuration and how we intend to use the node.
1240 * If <b>for_direct_connect</b> is true, we intend to connect to the node
1241 * directly, as the first hop of a circuit; otherwise, we intend to connect to
1242 * it indirectly, or use it as if we were connecting to it indirectly. */
1244 node_has_preferred_descriptor(const node_t *node,
1245 int for_direct_connect)
1247 const int is_bridge = node_is_a_configured_bridge(node);
1248 const int we_use_mds = we_use_microdescriptors_for_circuits(get_options());
1250 if ((is_bridge && for_direct_connect) || !we_use_mds) {
1251 /* We need an ri in this case. */
1252 if (!node->ri)
1253 return 0;
1254 } else {
1255 /* Otherwise we need an rs and an md. */
1256 if (node->rs == NULL || node->md == NULL)
1257 return 0;
1260 return 1;
1263 /** Return the router_purpose of <b>node</b>. */
1265 node_get_purpose(const node_t *node)
1267 if (node->ri)
1268 return node->ri->purpose;
1269 else
1270 return ROUTER_PURPOSE_GENERAL;
1273 /** Compute the verbose ("extended") nickname of <b>node</b> and store it
1274 * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
1275 * <b>verbose_name_out</b> */
1276 void
1277 node_get_verbose_nickname(const node_t *node,
1278 char *verbose_name_out)
1280 const char *nickname = node_get_nickname(node);
1281 verbose_name_out[0] = '$';
1282 base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
1283 DIGEST_LEN);
1284 if (!nickname)
1285 return;
1286 verbose_name_out[1+HEX_DIGEST_LEN] = '~';
1287 strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
1290 /** Compute the verbose ("extended") nickname of node with
1291 * given <b>id_digest</b> and store it into the MAX_VERBOSE_NICKNAME_LEN+1
1292 * character buffer at <b>verbose_name_out</b>
1294 * If node_get_by_id() returns NULL, base 16 encoding of
1295 * <b>id_digest</b> is returned instead. */
1296 void
1297 node_get_verbose_nickname_by_id(const char *id_digest,
1298 char *verbose_name_out)
1300 const node_t *node = node_get_by_id(id_digest);
1301 if (!node) {
1302 verbose_name_out[0] = '$';
1303 base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
1304 } else {
1305 node_get_verbose_nickname(node, verbose_name_out);
1309 /** Return true iff it seems that <b>node</b> allows circuits to exit
1310 * through it directlry from the client. */
1312 node_allows_single_hop_exits(const node_t *node)
1314 if (node && node->ri)
1315 return node->ri->allow_single_hop_exits;
1316 else
1317 return 0;
1320 /** Return true iff it seems that <b>node</b> has an exit policy that doesn't
1321 * actually permit anything to exit, or we don't know its exit policy */
1323 node_exit_policy_rejects_all(const node_t *node)
1325 if (node->rejects_all)
1326 return 1;
1328 if (node->ri)
1329 return node->ri->policy_is_reject_star;
1330 else if (node->md)
1331 return node->md->exit_policy == NULL ||
1332 short_policy_is_reject_star(node->md->exit_policy);
1333 else
1334 return 1;
1337 /** Return true iff the exit policy for <b>node</b> is such that we can treat
1338 * rejecting an address of type <b>family</b> unexpectedly as a sign of that
1339 * node's failure. */
1341 node_exit_policy_is_exact(const node_t *node, sa_family_t family)
1343 if (family == AF_UNSPEC) {
1344 return 1; /* Rejecting an address but not telling us what address
1345 * is a bad sign. */
1346 } else if (family == AF_INET) {
1347 return node->ri != NULL;
1348 } else if (family == AF_INET6) {
1349 return 0;
1351 tor_fragile_assert();
1352 return 1;
1355 /* Check if the "addr" and port_field fields from r are a valid non-listening
1356 * address/port. If so, set valid to true and add a newly allocated
1357 * tor_addr_port_t containing "addr" and port_field to sl.
1358 * "addr" is an IPv4 host-order address and port_field is a uint16_t.
1359 * r is typically a routerinfo_t or routerstatus_t.
1361 #define SL_ADD_NEW_IPV4_AP(r, port_field, sl, valid) \
1362 STMT_BEGIN \
1363 if (tor_addr_port_is_valid_ipv4h((r)->addr, (r)->port_field, 0)) { \
1364 valid = 1; \
1365 tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t)); \
1366 tor_addr_from_ipv4h(&ap->addr, (r)->addr); \
1367 ap->port = (r)->port_field; \
1368 smartlist_add((sl), ap); \
1370 STMT_END
1372 /* Check if the "addr" and port_field fields from r are a valid non-listening
1373 * address/port. If so, set valid to true and add a newly allocated
1374 * tor_addr_port_t containing "addr" and port_field to sl.
1375 * "addr" is a tor_addr_t and port_field is a uint16_t.
1376 * r is typically a routerinfo_t or routerstatus_t.
1378 #define SL_ADD_NEW_IPV6_AP(r, port_field, sl, valid) \
1379 STMT_BEGIN \
1380 if (tor_addr_port_is_valid(&(r)->ipv6_addr, (r)->port_field, 0)) { \
1381 valid = 1; \
1382 tor_addr_port_t *ap = tor_malloc(sizeof(tor_addr_port_t)); \
1383 tor_addr_copy(&ap->addr, &(r)->ipv6_addr); \
1384 ap->port = (r)->port_field; \
1385 smartlist_add((sl), ap); \
1387 STMT_END
1389 /** Return list of tor_addr_port_t with all OR ports (in the sense IP
1390 * addr + TCP port) for <b>node</b>. Caller must free all elements
1391 * using tor_free() and free the list using smartlist_free().
1393 * XXX this is potentially a memory fragmentation hog -- if on
1394 * critical path consider the option of having the caller allocate the
1395 * memory
1397 smartlist_t *
1398 node_get_all_orports(const node_t *node)
1400 smartlist_t *sl = smartlist_new();
1401 int valid = 0;
1403 /* Find a valid IPv4 address and port */
1404 if (node->ri != NULL) {
1405 SL_ADD_NEW_IPV4_AP(node->ri, or_port, sl, valid);
1408 /* If we didn't find a valid address/port in the ri, try the rs */
1409 if (!valid && node->rs != NULL) {
1410 SL_ADD_NEW_IPV4_AP(node->rs, or_port, sl, valid);
1413 /* Find a valid IPv6 address and port */
1414 valid = 0;
1415 if (node->ri != NULL) {
1416 SL_ADD_NEW_IPV6_AP(node->ri, ipv6_orport, sl, valid);
1419 if (!valid && node->rs != NULL) {
1420 SL_ADD_NEW_IPV6_AP(node->rs, ipv6_orport, sl, valid);
1423 if (!valid && node->md != NULL) {
1424 SL_ADD_NEW_IPV6_AP(node->md, ipv6_orport, sl, valid);
1427 return sl;
1430 #undef SL_ADD_NEW_IPV4_AP
1431 #undef SL_ADD_NEW_IPV6_AP
1433 /** Wrapper around node_get_prim_orport for backward
1434 compatibility. */
1435 void
1436 node_get_addr(const node_t *node, tor_addr_t *addr_out)
1438 tor_addr_port_t ap;
1439 node_get_prim_orport(node, &ap);
1440 tor_addr_copy(addr_out, &ap.addr);
1443 /** Return the host-order IPv4 address for <b>node</b>, or 0 if it doesn't
1444 * seem to have one. */
1445 uint32_t
1446 node_get_prim_addr_ipv4h(const node_t *node)
1448 /* Don't check the ORPort or DirPort, as this function isn't port-specific,
1449 * and the node might have a valid IPv4 address, yet have a zero
1450 * ORPort or DirPort.
1452 if (node->ri && tor_addr_is_valid_ipv4h(node->ri->addr, 0)) {
1453 return node->ri->addr;
1454 } else if (node->rs && tor_addr_is_valid_ipv4h(node->rs->addr, 0)) {
1455 return node->rs->addr;
1457 return 0;
1460 /** Copy a string representation of an IP address for <b>node</b> into
1461 * the <b>len</b>-byte buffer at <b>buf</b>. */
1462 void
1463 node_get_address_string(const node_t *node, char *buf, size_t len)
1465 uint32_t ipv4_addr = node_get_prim_addr_ipv4h(node);
1467 if (tor_addr_is_valid_ipv4h(ipv4_addr, 0)) {
1468 tor_addr_t addr;
1469 tor_addr_from_ipv4h(&addr, ipv4_addr);
1470 tor_addr_to_str(buf, &addr, len, 0);
1471 } else if (len > 0) {
1472 buf[0] = '\0';
1476 /** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
1477 * one. */
1478 long
1479 node_get_declared_uptime(const node_t *node)
1481 if (node->ri)
1482 return node->ri->uptime;
1483 else
1484 return -1;
1487 /** Return <b>node</b>'s platform string, or NULL if we don't know it. */
1488 const char *
1489 node_get_platform(const node_t *node)
1491 /* If we wanted, we could record the version in the routerstatus_t, since
1492 * the consensus lists it. We don't, though, so this function just won't
1493 * work with microdescriptors. */
1494 if (node->ri)
1495 return node->ri->platform;
1496 else
1497 return NULL;
1500 /** Return true iff <b>node</b> is one representing this router. */
1502 node_is_me(const node_t *node)
1504 return router_digest_is_me(node->identity);
1507 /* Does this node have a valid IPv6 address?
1508 * Prefer node_has_ipv6_orport() or node_has_ipv6_dirport() for
1509 * checking specific ports. */
1511 node_has_ipv6_addr(const node_t *node)
1513 /* Don't check the ORPort or DirPort, as this function isn't port-specific,
1514 * and the node might have a valid IPv6 address, yet have a zero
1515 * ORPort or DirPort.
1517 if (node->ri && tor_addr_is_valid(&node->ri->ipv6_addr, 0))
1518 return 1;
1519 if (node->rs && tor_addr_is_valid(&node->rs->ipv6_addr, 0))
1520 return 1;
1521 if (node->md && tor_addr_is_valid(&node->md->ipv6_addr, 0))
1522 return 1;
1524 return 0;
1527 /* Does this node have a valid IPv6 ORPort? */
1529 node_has_ipv6_orport(const node_t *node)
1531 tor_addr_port_t ipv6_orport;
1532 node_get_pref_ipv6_orport(node, &ipv6_orport);
1533 return tor_addr_port_is_valid_ap(&ipv6_orport, 0);
1536 /* Does this node have a valid IPv6 DirPort? */
1538 node_has_ipv6_dirport(const node_t *node)
1540 tor_addr_port_t ipv6_dirport;
1541 node_get_pref_ipv6_dirport(node, &ipv6_dirport);
1542 return tor_addr_port_is_valid_ap(&ipv6_dirport, 0);
1545 /** Return 1 if we prefer the IPv6 address and OR TCP port of
1546 * <b>node</b>, else 0.
1548 * We prefer the IPv6 address if the router has an IPv6 address,
1549 * and we can use IPv6 addresses, and:
1550 * i) the node_t says that it prefers IPv6
1551 * or
1552 * ii) the router has no IPv4 OR address.
1554 * If you don't have a node, consider looking it up.
1555 * If there is no node, use fascist_firewall_prefer_ipv6_orport().
1558 node_ipv6_or_preferred(const node_t *node)
1560 const or_options_t *options = get_options();
1561 tor_addr_port_t ipv4_addr;
1562 node_assert_ok(node);
1564 /* XX/teor - node->ipv6_preferred is set from
1565 * fascist_firewall_prefer_ipv6_orport() each time the consensus is loaded.
1567 node_get_prim_orport(node, &ipv4_addr);
1568 if (!fascist_firewall_use_ipv6(options)) {
1569 return 0;
1570 } else if (node->ipv6_preferred ||
1571 !tor_addr_port_is_valid_ap(&ipv4_addr, 0)) {
1572 return node_has_ipv6_orport(node);
1574 return 0;
1577 #define RETURN_IPV4_AP(r, port_field, ap_out) \
1578 STMT_BEGIN \
1579 if (r && tor_addr_port_is_valid_ipv4h((r)->addr, (r)->port_field, 0)) { \
1580 tor_addr_from_ipv4h(&(ap_out)->addr, (r)->addr); \
1581 (ap_out)->port = (r)->port_field; \
1583 STMT_END
1585 /** Copy the primary (IPv4) OR port (IP address and TCP port) for <b>node</b>
1586 * into *<b>ap_out</b>. */
1587 void
1588 node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
1590 node_assert_ok(node);
1591 tor_assert(ap_out);
1593 /* Clear the address, as a safety precaution if calling functions ignore the
1594 * return value */
1595 tor_addr_make_null(&ap_out->addr, AF_INET);
1596 ap_out->port = 0;
1598 /* Check ri first, because rewrite_node_address_for_bridge() updates
1599 * node->ri with the configured bridge address. */
1601 RETURN_IPV4_AP(node->ri, or_port, ap_out);
1602 RETURN_IPV4_AP(node->rs, or_port, ap_out);
1603 /* Microdescriptors only have an IPv6 address */
1606 /** Copy the preferred OR port (IP address and TCP port) for
1607 * <b>node</b> into *<b>ap_out</b>. */
1608 void
1609 node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
1611 tor_assert(ap_out);
1613 if (node_ipv6_or_preferred(node)) {
1614 node_get_pref_ipv6_orport(node, ap_out);
1615 } else {
1616 /* the primary ORPort is always on IPv4 */
1617 node_get_prim_orport(node, ap_out);
1621 /** Copy the preferred IPv6 OR port (IP address and TCP port) for
1622 * <b>node</b> into *<b>ap_out</b>. */
1623 void
1624 node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
1626 node_assert_ok(node);
1627 tor_assert(ap_out);
1628 memset(ap_out, 0, sizeof(*ap_out));
1630 /* Check ri first, because rewrite_node_address_for_bridge() updates
1631 * node->ri with the configured bridge address.
1632 * Prefer rs over md for consistency with the fascist_firewall_* functions.
1633 * Check if the address or port are valid, and try another alternative
1634 * if they are not. */
1636 if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
1637 node->ri->ipv6_orport, 0)) {
1638 tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
1639 ap_out->port = node->ri->ipv6_orport;
1640 } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
1641 node->rs->ipv6_orport, 0)) {
1642 tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
1643 ap_out->port = node->rs->ipv6_orport;
1644 } else if (node->md && tor_addr_port_is_valid(&node->md->ipv6_addr,
1645 node->md->ipv6_orport, 0)) {
1646 tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
1647 ap_out->port = node->md->ipv6_orport;
1648 } else {
1649 tor_addr_make_null(&ap_out->addr, AF_INET6);
1650 ap_out->port = 0;
1654 /** Return 1 if we prefer the IPv6 address and Dir TCP port of
1655 * <b>node</b>, else 0.
1657 * We prefer the IPv6 address if the router has an IPv6 address,
1658 * and we can use IPv6 addresses, and:
1659 * i) the router has no IPv4 Dir address.
1660 * or
1661 * ii) our preference is for IPv6 Dir addresses.
1663 * If there is no node, use fascist_firewall_prefer_ipv6_dirport().
1666 node_ipv6_dir_preferred(const node_t *node)
1668 const or_options_t *options = get_options();
1669 tor_addr_port_t ipv4_addr;
1670 node_assert_ok(node);
1672 /* node->ipv6_preferred is set from fascist_firewall_prefer_ipv6_orport(),
1673 * so we can't use it to determine DirPort IPv6 preference.
1674 * This means that bridge clients will use IPv4 DirPorts by default.
1676 node_get_prim_dirport(node, &ipv4_addr);
1677 if (!fascist_firewall_use_ipv6(options)) {
1678 return 0;
1679 } else if (!tor_addr_port_is_valid_ap(&ipv4_addr, 0)
1680 || fascist_firewall_prefer_ipv6_dirport(get_options())) {
1681 return node_has_ipv6_dirport(node);
1683 return 0;
1686 /** Copy the primary (IPv4) Dir port (IP address and TCP port) for <b>node</b>
1687 * into *<b>ap_out</b>. */
1688 void
1689 node_get_prim_dirport(const node_t *node, tor_addr_port_t *ap_out)
1691 node_assert_ok(node);
1692 tor_assert(ap_out);
1694 /* Clear the address, as a safety precaution if calling functions ignore the
1695 * return value */
1696 tor_addr_make_null(&ap_out->addr, AF_INET);
1697 ap_out->port = 0;
1699 /* Check ri first, because rewrite_node_address_for_bridge() updates
1700 * node->ri with the configured bridge address. */
1702 RETURN_IPV4_AP(node->ri, dir_port, ap_out);
1703 RETURN_IPV4_AP(node->rs, dir_port, ap_out);
1704 /* Microdescriptors only have an IPv6 address */
1707 #undef RETURN_IPV4_AP
1709 /** Copy the preferred Dir port (IP address and TCP port) for
1710 * <b>node</b> into *<b>ap_out</b>. */
1711 void
1712 node_get_pref_dirport(const node_t *node, tor_addr_port_t *ap_out)
1714 tor_assert(ap_out);
1716 if (node_ipv6_dir_preferred(node)) {
1717 node_get_pref_ipv6_dirport(node, ap_out);
1718 } else {
1719 /* the primary DirPort is always on IPv4 */
1720 node_get_prim_dirport(node, ap_out);
1724 /** Copy the preferred IPv6 Dir port (IP address and TCP port) for
1725 * <b>node</b> into *<b>ap_out</b>. */
1726 void
1727 node_get_pref_ipv6_dirport(const node_t *node, tor_addr_port_t *ap_out)
1729 node_assert_ok(node);
1730 tor_assert(ap_out);
1732 /* Check ri first, because rewrite_node_address_for_bridge() updates
1733 * node->ri with the configured bridge address.
1734 * Prefer rs over md for consistency with the fascist_firewall_* functions.
1735 * Check if the address or port are valid, and try another alternative
1736 * if they are not. */
1738 /* Assume IPv4 and IPv6 dirports are the same */
1739 if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
1740 node->ri->dir_port, 0)) {
1741 tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
1742 ap_out->port = node->ri->dir_port;
1743 } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
1744 node->rs->dir_port, 0)) {
1745 tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
1746 ap_out->port = node->rs->dir_port;
1747 } else {
1748 tor_addr_make_null(&ap_out->addr, AF_INET6);
1749 ap_out->port = 0;
1753 /** Return true iff <b>md</b> has a curve25519 onion key.
1754 * Use node_has_curve25519_onion_key() instead of calling this directly. */
1755 static int
1756 microdesc_has_curve25519_onion_key(const microdesc_t *md)
1758 if (!md) {
1759 return 0;
1762 if (!md->onion_curve25519_pkey) {
1763 return 0;
1766 if (tor_mem_is_zero((const char*)md->onion_curve25519_pkey->public_key,
1767 CURVE25519_PUBKEY_LEN)) {
1768 return 0;
1771 return 1;
1774 /** Return true iff <b>node</b> has a curve25519 onion key. */
1776 node_has_curve25519_onion_key(const node_t *node)
1778 return node_get_curve25519_onion_key(node) != NULL;
1781 /** Return the curve25519 key of <b>node</b>, or NULL if none. */
1782 const curve25519_public_key_t *
1783 node_get_curve25519_onion_key(const node_t *node)
1785 if (!node)
1786 return NULL;
1787 if (routerinfo_has_curve25519_onion_key(node->ri))
1788 return node->ri->onion_curve25519_pkey;
1789 else if (microdesc_has_curve25519_onion_key(node->md))
1790 return node->md->onion_curve25519_pkey;
1791 else
1792 return NULL;
1795 /* Return a newly allocacted RSA onion public key taken from the given node.
1797 * Return NULL if node is NULL or no RSA onion public key can be found. It is
1798 * the caller responsability to free the returned object. */
1799 crypto_pk_t *
1800 node_get_rsa_onion_key(const node_t *node)
1802 crypto_pk_t *pk = NULL;
1803 const char *onion_pkey;
1804 size_t onion_pkey_len;
1806 if (!node) {
1807 goto end;
1810 if (node->ri) {
1811 onion_pkey = node->ri->onion_pkey;
1812 onion_pkey_len = node->ri->onion_pkey_len;
1813 } else if (node->rs && node->md) {
1814 onion_pkey = node->md->onion_pkey;
1815 onion_pkey_len = node->md->onion_pkey_len;
1816 } else {
1817 /* No descriptor or microdescriptor. */
1818 goto end;
1820 pk = router_get_rsa_onion_pkey(onion_pkey, onion_pkey_len);
1822 end:
1823 return pk;
1826 /** Refresh the country code of <b>ri</b>. This function MUST be called on
1827 * each router when the GeoIP database is reloaded, and on all new routers. */
1828 void
1829 node_set_country(node_t *node)
1831 tor_addr_t addr = TOR_ADDR_NULL;
1833 /* XXXXipv6 */
1834 if (node->rs)
1835 tor_addr_from_ipv4h(&addr, node->rs->addr);
1836 else if (node->ri)
1837 tor_addr_from_ipv4h(&addr, node->ri->addr);
1839 node->country = geoip_get_country_by_addr(&addr);
1842 /** Set the country code of all routers in the routerlist. */
1843 void
1844 nodelist_refresh_countries(void)
1846 smartlist_t *nodes = nodelist_get_list();
1847 SMARTLIST_FOREACH(nodes, node_t *, node,
1848 node_set_country(node));
1851 /** Return true iff router1 and router2 have similar enough network addresses
1852 * that we should treat them as being in the same family */
1854 addrs_in_same_network_family(const tor_addr_t *a1,
1855 const tor_addr_t *a2)
1857 if (tor_addr_is_null(a1) || tor_addr_is_null(a2))
1858 return 0;
1860 switch (tor_addr_family(a1)) {
1861 case AF_INET:
1862 return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
1863 case AF_INET6:
1864 return 0 == tor_addr_compare_masked(a1, a2, 32, CMP_SEMANTIC);
1865 default:
1866 /* If not IPv4 or IPv6, return 0. */
1867 return 0;
1871 /** Return true if <b>node</b>'s nickname matches <b>nickname</b>
1872 * (case-insensitive), or if <b>node's</b> identity key digest
1873 * matches a hexadecimal value stored in <b>nickname</b>. Return
1874 * false otherwise. */
1875 STATIC int
1876 node_nickname_matches(const node_t *node, const char *nickname)
1878 const char *n = node_get_nickname(node);
1879 if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
1880 return 1;
1881 return hex_digest_nickname_matches(nickname,
1882 node->identity,
1886 /** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
1887 STATIC int
1888 node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
1890 if (!lst) return 0;
1891 SMARTLIST_FOREACH(lst, const char *, name, {
1892 if (node_nickname_matches(node, name))
1893 return 1;
1895 return 0;
1898 /** Return true iff n1's declared family contains n2. */
1899 STATIC int
1900 node_family_contains(const node_t *n1, const node_t *n2)
1902 if (n1->ri && n1->ri->declared_family) {
1903 return node_in_nickname_smartlist(n1->ri->declared_family, n2);
1904 } else if (n1->md) {
1905 return nodefamily_contains_node(n1->md->family, n2);
1906 } else {
1907 return 0;
1912 * Return true iff <b>node</b> has declared a nonempty family.
1914 STATIC bool
1915 node_has_declared_family(const node_t *node)
1917 if (node->ri && node->ri->declared_family &&
1918 smartlist_len(node->ri->declared_family)) {
1919 return true;
1922 if (node->md && node->md->family) {
1923 return true;
1926 return false;
1930 * Add to <b>out</b> every node_t that is listed by <b>node</b> as being in
1931 * its family. (Note that these nodes are not in node's family unless they
1932 * also agree that node is in their family.)
1934 STATIC void
1935 node_lookup_declared_family(smartlist_t *out, const node_t *node)
1937 if (node->ri && node->ri->declared_family &&
1938 smartlist_len(node->ri->declared_family)) {
1939 SMARTLIST_FOREACH_BEGIN(node->ri->declared_family, const char *, name) {
1940 const node_t *n2 = node_get_by_nickname(name, NNF_NO_WARN_UNNAMED);
1941 if (n2) {
1942 smartlist_add(out, (node_t *)n2);
1944 } SMARTLIST_FOREACH_END(name);
1945 return;
1948 if (node->md && node->md->family) {
1949 nodefamily_add_nodes_to_smartlist(node->md->family, out);
1953 /** Return true iff r1 and r2 are in the same family, but not the same
1954 * router. */
1956 nodes_in_same_family(const node_t *node1, const node_t *node2)
1958 const or_options_t *options = get_options();
1960 /* Are they in the same family because of their addresses? */
1961 if (options->EnforceDistinctSubnets) {
1962 tor_addr_t a1, a2;
1963 node_get_addr(node1, &a1);
1964 node_get_addr(node2, &a2);
1966 tor_addr_port_t ap6_1, ap6_2;
1967 node_get_pref_ipv6_orport(node1, &ap6_1);
1968 node_get_pref_ipv6_orport(node2, &ap6_2);
1970 if (addrs_in_same_network_family(&a1, &a2) ||
1971 addrs_in_same_network_family(&ap6_1.addr, &ap6_2.addr))
1972 return 1;
1975 /* Are they in the same family because the agree they are? */
1976 if (node_family_contains(node1, node2) &&
1977 node_family_contains(node2, node1)) {
1978 return 1;
1981 /* Are they in the same family because the user says they are? */
1982 if (options->NodeFamilySets) {
1983 SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
1984 if (routerset_contains_node(rs, node1) &&
1985 routerset_contains_node(rs, node2))
1986 return 1;
1990 return 0;
1994 * Add all the family of <b>node</b>, including <b>node</b> itself, to
1995 * the smartlist <b>sl</b>.
1997 * This is used to make sure we don't pick siblings in a single path, or
1998 * pick more than one relay from a family for our entry guard list.
1999 * Note that a node may be added to <b>sl</b> more than once if it is
2000 * part of <b>node</b>'s family for more than one reason.
2002 void
2003 nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
2005 const smartlist_t *all_nodes = nodelist_get_list();
2006 const or_options_t *options = get_options();
2008 tor_assert(node);
2010 /* Let's make sure that we have the node itself, if it's a real node. */
2012 const node_t *real_node = node_get_by_id(node->identity);
2013 if (real_node)
2014 smartlist_add(sl, (node_t*)real_node);
2017 /* First, add any nodes with similar network addresses. */
2018 if (options->EnforceDistinctSubnets) {
2019 tor_addr_t node_addr;
2020 tor_addr_port_t node_ap6;
2021 node_get_addr(node, &node_addr);
2022 node_get_pref_ipv6_orport(node, &node_ap6);
2024 SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
2025 tor_addr_t a;
2026 tor_addr_port_t ap6;
2027 node_get_addr(node2, &a);
2028 node_get_pref_ipv6_orport(node2, &ap6);
2029 if (addrs_in_same_network_family(&a, &node_addr) ||
2030 addrs_in_same_network_family(&ap6.addr, &node_ap6.addr))
2031 smartlist_add(sl, (void*)node2);
2032 } SMARTLIST_FOREACH_END(node2);
2035 /* Now, add all nodes in the declared family of this node, if they
2036 * also declare this node to be in their family. */
2037 if (node_has_declared_family(node)) {
2038 smartlist_t *declared_family = smartlist_new();
2039 node_lookup_declared_family(declared_family, node);
2041 /* Add every r such that router declares familyness with node, and node
2042 * declares familyhood with router. */
2043 SMARTLIST_FOREACH_BEGIN(declared_family, const node_t *, node2) {
2044 if (node_family_contains(node2, node)) {
2045 smartlist_add(sl, (void*)node2);
2047 } SMARTLIST_FOREACH_END(node2);
2048 smartlist_free(declared_family);
2051 /* If the user declared any families locally, honor those too. */
2052 if (options->NodeFamilySets) {
2053 SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
2054 if (routerset_contains_node(rs, node)) {
2055 routerset_get_all_nodes(sl, rs, NULL, 0);
2061 /** Find a router that's up, that has this IP address, and
2062 * that allows exit to this address:port, or return NULL if there
2063 * isn't a good one.
2064 * Don't exit enclave to excluded relays -- it wouldn't actually
2065 * hurt anything, but this way there are fewer confused users.
2067 const node_t *
2068 router_find_exact_exit_enclave(const char *address, uint16_t port)
2069 {/*XXXX MOVE*/
2070 uint32_t addr;
2071 struct in_addr in;
2072 tor_addr_t a;
2073 const or_options_t *options = get_options();
2075 if (!tor_inet_aton(address, &in))
2076 return NULL; /* it's not an IP already */
2077 addr = ntohl(in.s_addr);
2079 tor_addr_from_ipv4h(&a, addr);
2081 SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
2082 if (node_get_addr_ipv4h(node) == addr &&
2083 node->is_running &&
2084 compare_tor_addr_to_node_policy(&a, port, node) ==
2085 ADDR_POLICY_ACCEPTED &&
2086 !routerset_contains_node(options->ExcludeExitNodesUnion_, node))
2087 return node;
2089 return NULL;
2092 /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
2093 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
2094 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
2095 * bandwidth.
2096 * If <b>need_guard</b>, we require that the router is a possible entry guard.
2099 node_is_unreliable(const node_t *node, int need_uptime,
2100 int need_capacity, int need_guard)
2102 if (need_uptime && !node->is_stable)
2103 return 1;
2104 if (need_capacity && !node->is_fast)
2105 return 1;
2106 if (need_guard && !node->is_possible_guard)
2107 return 1;
2108 return 0;
2111 /** Return 1 if all running sufficiently-stable routers we can use will reject
2112 * addr:port. Return 0 if any might accept it. */
2114 router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
2115 int need_uptime)
2117 addr_policy_result_t r;
2119 SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
2120 if (node->is_running &&
2121 !node_is_unreliable(node, need_uptime, 0, 0)) {
2123 r = compare_tor_addr_to_node_policy(addr, port, node);
2125 if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
2126 return 0; /* this one could be ok. good enough. */
2128 } SMARTLIST_FOREACH_END(node);
2129 return 1; /* all will reject. */
2132 /** Mark the router with ID <b>digest</b> as running or non-running
2133 * in our routerlist. */
2134 void
2135 router_set_status(const char *digest, int up)
2137 node_t *node;
2138 tor_assert(digest);
2140 SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
2141 dir_server_t *, d,
2142 if (tor_memeq(d->digest, digest, DIGEST_LEN))
2143 d->is_running = up);
2145 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
2146 dir_server_t *, d,
2147 if (tor_memeq(d->digest, digest, DIGEST_LEN))
2148 d->is_running = up);
2150 node = node_get_mutable_by_id(digest);
2151 if (node) {
2152 #if 0
2153 log_debug(LD_DIR,"Marking router %s as %s.",
2154 node_describe(node), up ? "up" : "down");
2155 #endif
2156 if (!up && node_is_me(node) && !net_is_disabled())
2157 log_warn(LD_NET, "We just marked ourself as down. Are your external "
2158 "addresses reachable?");
2160 if (bool_neq(node->is_running, up))
2161 router_dir_info_changed();
2163 node->is_running = up;
2167 /** True iff, the last time we checked whether we had enough directory info
2168 * to build circuits, the answer was "yes". If there are no exits in the
2169 * consensus, we act as if we have 100% of the exit directory info. */
2170 static int have_min_dir_info = 0;
2172 /** Does the consensus contain nodes that can exit? */
2173 static consensus_path_type_t have_consensus_path = CONSENSUS_PATH_UNKNOWN;
2175 /** True iff enough has changed since the last time we checked whether we had
2176 * enough directory info to build circuits that our old answer can no longer
2177 * be trusted. */
2178 static int need_to_update_have_min_dir_info = 1;
2179 /** String describing what we're missing before we have enough directory
2180 * info. */
2181 static char dir_info_status[512] = "";
2183 /** Return true iff we have enough consensus information to
2184 * start building circuits. Right now, this means "a consensus that's
2185 * less than a day old, and at least 60% of router descriptors (configurable),
2186 * weighted by bandwidth. Treat the exit fraction as 100% if there are
2187 * no exits in the consensus."
2188 * To obtain the final weighted bandwidth, we multiply the
2189 * weighted bandwidth fraction for each position (guard, middle, exit). */
2190 MOCK_IMPL(int,
2191 router_have_minimum_dir_info,(void))
2193 static int logged_delay=0;
2194 const char *delay_fetches_msg = NULL;
2195 if (should_delay_dir_fetches(get_options(), &delay_fetches_msg)) {
2196 if (!logged_delay)
2197 log_notice(LD_DIR, "Delaying directory fetches: %s", delay_fetches_msg);
2198 logged_delay=1;
2199 strlcpy(dir_info_status, delay_fetches_msg, sizeof(dir_info_status));
2200 return 0;
2202 logged_delay = 0; /* reset it if we get this far */
2204 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
2205 update_router_have_minimum_dir_info();
2208 return have_min_dir_info;
2211 /** Set to CONSENSUS_PATH_EXIT if there is at least one exit node
2212 * in the consensus. We update this flag in compute_frac_paths_available if
2213 * there is at least one relay that has an Exit flag in the consensus.
2214 * Used to avoid building exit circuits when they will almost certainly fail.
2215 * Set to CONSENSUS_PATH_INTERNAL if there are no exits in the consensus.
2216 * (This situation typically occurs during bootstrap of a test network.)
2217 * Set to CONSENSUS_PATH_UNKNOWN if we have never checked, or have
2218 * reason to believe our last known value was invalid or has expired.
2219 * If we're in a network with TestingDirAuthVoteExit set,
2220 * this can cause router_have_consensus_path() to be set to
2221 * CONSENSUS_PATH_EXIT, even if there are no nodes with accept exit policies.
2223 MOCK_IMPL(consensus_path_type_t,
2224 router_have_consensus_path, (void))
2226 return have_consensus_path;
2229 /** Called when our internal view of the directory has changed. This can be
2230 * when the authorities change, networkstatuses change, the list of routerdescs
2231 * changes, or number of running routers changes.
2233 void
2234 router_dir_info_changed(void)
2236 need_to_update_have_min_dir_info = 1;
2237 rend_hsdir_routers_changed();
2238 hs_service_dir_info_changed();
2239 hs_client_dir_info_changed();
2242 /** Return a string describing what we're missing before we have enough
2243 * directory info. */
2244 const char *
2245 get_dir_info_status_string(void)
2247 return dir_info_status;
2250 /** Iterate over the servers listed in <b>consensus</b>, and count how many of
2251 * them seem like ones we'd use (store this in *<b>num_usable</b>), and how
2252 * many of <em>those</em> we have descriptors for (store this in
2253 * *<b>num_present</b>).
2255 * If <b>in_set</b> is non-NULL, only consider those routers in <b>in_set</b>.
2256 * If <b>exit_only</b> & USABLE_DESCRIPTOR_EXIT_POLICY, only consider nodes
2257 * present if they have an exit policy that accepts at least one port.
2258 * If <b>exit_only</b> & USABLE_DESCRIPTOR_EXIT_FLAG, only consider nodes
2259 * usable if they have the exit flag in the consensus.
2261 * If *<b>descs_out</b> is present, add a node_t for each usable descriptor
2262 * to it.
2264 static void
2265 count_usable_descriptors(int *num_present, int *num_usable,
2266 smartlist_t *descs_out,
2267 const networkstatus_t *consensus,
2268 time_t now,
2269 routerset_t *in_set,
2270 usable_descriptor_t exit_only)
2272 const int md = (consensus->flavor == FLAV_MICRODESC);
2273 *num_present = 0, *num_usable = 0;
2275 SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
2277 const node_t *node = node_get_by_id(rs->identity_digest);
2278 if (!node)
2279 continue; /* This would be a bug: every entry in the consensus is
2280 * supposed to have a node. */
2281 if ((exit_only & USABLE_DESCRIPTOR_EXIT_FLAG) && ! rs->is_exit)
2282 continue;
2283 if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
2284 continue;
2285 if (client_would_use_router(rs, now)) {
2286 const char * const digest = rs->descriptor_digest;
2287 int present;
2288 ++*num_usable; /* the consensus says we want it. */
2289 if (md)
2290 present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
2291 else
2292 present = NULL != router_get_by_descriptor_digest(digest);
2293 if (present) {
2294 /* Do the policy check last, because it requires a descriptor,
2295 * and is potentially expensive */
2296 if ((exit_only & USABLE_DESCRIPTOR_EXIT_POLICY) &&
2297 node_exit_policy_rejects_all(node)) {
2298 continue;
2300 /* we have the descriptor listed in the consensus, and it
2301 * satisfies our exit constraints (if any) */
2302 ++*num_present;
2304 if (descs_out)
2305 smartlist_add(descs_out, (node_t*)node);
2308 SMARTLIST_FOREACH_END(rs);
2310 log_debug(LD_DIR, "%d usable, %d present (%s%s%s%s%s).",
2311 *num_usable, *num_present,
2312 md ? "microdesc" : "desc",
2313 (exit_only & USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG) ?
2314 " exit" : "s",
2315 (exit_only & USABLE_DESCRIPTOR_EXIT_POLICY) ?
2316 " policies" : "" ,
2317 (exit_only == USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG) ?
2318 " and" : "" ,
2319 (exit_only & USABLE_DESCRIPTOR_EXIT_FLAG) ?
2320 " flags" : "" );
2323 /** Return an estimate of which fraction of usable paths through the Tor
2324 * network we have available for use. Count how many routers seem like ones
2325 * we'd use (store this in *<b>num_usable_out</b>), and how many of
2326 * <em>those</em> we have descriptors for (store this in
2327 * *<b>num_present_out</b>.)
2329 * If **<b>status_out</b> is present, allocate a new string and print the
2330 * available percentages of guard, middle, and exit nodes to it, noting
2331 * whether there are exits in the consensus.
2332 * If there are no exits in the consensus, we treat the exit fraction as 100%,
2333 * but set router_have_consensus_path() so that we can only build internal
2334 * paths. */
2335 static double
2336 compute_frac_paths_available(const networkstatus_t *consensus,
2337 const or_options_t *options, time_t now,
2338 int *num_present_out, int *num_usable_out,
2339 char **status_out)
2341 smartlist_t *guards = smartlist_new();
2342 smartlist_t *mid = smartlist_new();
2343 smartlist_t *exits = smartlist_new();
2344 double f_guard, f_mid, f_exit;
2345 double f_path = 0.0;
2346 /* Used to determine whether there are any exits in the consensus */
2347 int np = 0;
2348 /* Used to determine whether there are any exits with descriptors */
2349 int nu = 0;
2350 const int authdir = authdir_mode_v3(options);
2352 count_usable_descriptors(num_present_out, num_usable_out,
2353 mid, consensus, now, options->MiddleNodes,
2354 USABLE_DESCRIPTOR_ALL);
2355 log_debug(LD_NET,
2356 "%s: %d present, %d usable",
2357 "mid",
2359 nu);
2361 if (options->EntryNodes) {
2362 count_usable_descriptors(&np, &nu, guards, consensus, now,
2363 options->EntryNodes, USABLE_DESCRIPTOR_ALL);
2364 log_debug(LD_NET,
2365 "%s: %d present, %d usable",
2366 "guard",
2368 nu);
2369 } else {
2370 SMARTLIST_FOREACH(mid, const node_t *, node, {
2371 if (authdir) {
2372 if (node->rs && node->rs->is_possible_guard)
2373 smartlist_add(guards, (node_t*)node);
2374 } else {
2375 if (node->is_possible_guard)
2376 smartlist_add(guards, (node_t*)node);
2379 log_debug(LD_NET,
2380 "%s: %d possible",
2381 "guard",
2382 smartlist_len(guards));
2385 /* All nodes with exit policy and flag */
2386 count_usable_descriptors(&np, &nu, exits, consensus, now,
2387 NULL, USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG);
2388 log_debug(LD_NET,
2389 "%s: %d present, %d usable",
2390 "exits",
2392 nu);
2394 /* We need at least 1 exit (flag and policy) in the consensus to consider
2395 * building exit paths */
2396 /* Update our understanding of whether the consensus has exits */
2397 consensus_path_type_t old_have_consensus_path = have_consensus_path;
2398 have_consensus_path = ((np > 0) ?
2399 CONSENSUS_PATH_EXIT :
2400 CONSENSUS_PATH_INTERNAL);
2402 if (old_have_consensus_path != have_consensus_path) {
2403 if (have_consensus_path == CONSENSUS_PATH_INTERNAL) {
2404 log_notice(LD_NET,
2405 "The current consensus has no exit nodes. "
2406 "Tor can only build internal paths, "
2407 "such as paths to onion services.");
2409 /* However, exit nodes can reachability self-test using this consensus,
2410 * join the network, and appear in a later consensus. This will allow
2411 * the network to build exit paths, such as paths for world wide web
2412 * browsing (as distinct from hidden service web browsing). */
2413 } else if (old_have_consensus_path == CONSENSUS_PATH_INTERNAL) {
2414 log_notice(LD_NET,
2415 "The current consensus contains exit nodes. "
2416 "Tor can build exit and internal paths.");
2420 f_guard = frac_nodes_with_descriptors(guards, WEIGHT_FOR_GUARD, 1);
2421 f_mid = frac_nodes_with_descriptors(mid, WEIGHT_FOR_MID, 0);
2422 f_exit = frac_nodes_with_descriptors(exits, WEIGHT_FOR_EXIT, 0);
2424 /* If we are using bridges and have at least one bridge with a full
2425 * descriptor, assume f_guard is 1.0. */
2426 if (options->UseBridges && num_bridges_usable(0) > 0)
2427 f_guard = 1.0;
2429 log_debug(LD_NET,
2430 "f_guard: %.2f, f_mid: %.2f, f_exit: %.2f",
2431 f_guard,
2432 f_mid,
2433 f_exit);
2435 smartlist_free(guards);
2436 smartlist_free(mid);
2437 smartlist_free(exits);
2439 if (options->ExitNodes) {
2440 double f_myexit, f_myexit_unflagged;
2441 smartlist_t *myexits= smartlist_new();
2442 smartlist_t *myexits_unflagged = smartlist_new();
2444 /* All nodes with exit policy and flag in ExitNodes option */
2445 count_usable_descriptors(&np, &nu, myexits, consensus, now,
2446 options->ExitNodes,
2447 USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG);
2448 log_debug(LD_NET,
2449 "%s: %d present, %d usable",
2450 "myexits",
2452 nu);
2454 /* Now compute the nodes in the ExitNodes option where we know their exit
2455 * policy permits something. */
2456 count_usable_descriptors(&np, &nu, myexits_unflagged,
2457 consensus, now,
2458 options->ExitNodes,
2459 USABLE_DESCRIPTOR_EXIT_POLICY);
2460 log_debug(LD_NET,
2461 "%s: %d present, %d usable",
2462 "myexits_unflagged (initial)",
2464 nu);
2466 f_myexit= frac_nodes_with_descriptors(myexits,WEIGHT_FOR_EXIT, 0);
2467 f_myexit_unflagged=
2468 frac_nodes_with_descriptors(myexits_unflagged,
2469 WEIGHT_FOR_EXIT, 0);
2471 log_debug(LD_NET,
2472 "f_exit: %.2f, f_myexit: %.2f, f_myexit_unflagged: %.2f",
2473 f_exit,
2474 f_myexit,
2475 f_myexit_unflagged);
2477 /* If our ExitNodes list has eliminated every possible Exit node, and there
2478 * were some possible Exit nodes, then instead consider nodes that permit
2479 * exiting to some ports. */
2480 if (smartlist_len(myexits) == 0 &&
2481 smartlist_len(myexits_unflagged)) {
2482 f_myexit = f_myexit_unflagged;
2485 smartlist_free(myexits);
2486 smartlist_free(myexits_unflagged);
2488 /* This is a tricky point here: we don't want to make it easy for a
2489 * directory to trickle exits to us until it learns which exits we have
2490 * configured, so require that we have a threshold both of total exits
2491 * and usable exits. */
2492 if (f_myexit < f_exit)
2493 f_exit = f_myexit;
2496 /* if the consensus has no exits, we can only build onion service paths,
2497 * which are G - M - M. So use the middle fraction for the exit fraction. */
2498 if (router_have_consensus_path() != CONSENSUS_PATH_EXIT) {
2499 /* If there are no exits in the consensus, then f_exit is always 0, so
2500 * it is safe to replace f_exit with f_mid. */
2501 if (!BUG(f_exit > 0.0)) {
2502 f_exit = f_mid;
2506 f_path = f_guard * f_mid * f_exit;
2508 if (status_out)
2509 tor_asprintf(status_out,
2510 "%d%% of guards bw, "
2511 "%d%% of midpoint bw, and "
2512 "%d%% of %s = "
2513 "%d%% of path bw",
2514 (int)(f_guard*100),
2515 (int)(f_mid*100),
2516 (int)(f_exit*100),
2517 (router_have_consensus_path() == CONSENSUS_PATH_EXIT ?
2518 "exit bw" :
2519 "end bw (no exits in consensus, using mid)"),
2520 (int)(f_path*100));
2522 return f_path;
2525 /** We just fetched a new set of descriptors. Compute how far through
2526 * the "loading descriptors" bootstrapping phase we are, so we can inform
2527 * the controller of our progress. */
2529 count_loading_descriptors_progress(void)
2531 int num_present = 0, num_usable=0;
2532 time_t now = time(NULL);
2533 const or_options_t *options = get_options();
2534 const networkstatus_t *consensus =
2535 networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
2536 double paths, fraction;
2538 if (!consensus)
2539 return 0; /* can't count descriptors if we have no list of them */
2541 paths = compute_frac_paths_available(consensus, options, now,
2542 &num_present, &num_usable,
2543 NULL);
2545 fraction = paths / get_frac_paths_needed_for_circs(options,consensus);
2546 if (fraction > 1.0)
2547 return 0; /* it's not the number of descriptors holding us back */
2548 return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
2549 (fraction*(BOOTSTRAP_STATUS_ENOUGH_DIRINFO-1 -
2550 BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
2553 /** Return the fraction of paths needed before we're willing to build
2554 * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
2555 static double
2556 get_frac_paths_needed_for_circs(const or_options_t *options,
2557 const networkstatus_t *ns)
2559 #define DFLT_PCT_USABLE_NEEDED 60
2560 if (options->PathsNeededToBuildCircuits >= 0.0) {
2561 return options->PathsNeededToBuildCircuits;
2562 } else {
2563 return networkstatus_get_param(ns, "min_paths_for_circs_pct",
2564 DFLT_PCT_USABLE_NEEDED,
2565 25, 95)/100.0;
2569 /** Change the value of have_min_dir_info, setting it true iff we have enough
2570 * network and router information to build circuits. Clear the value of
2571 * need_to_update_have_min_dir_info. */
2572 static void
2573 update_router_have_minimum_dir_info(void)
2575 time_t now = time(NULL);
2576 int res;
2577 int num_present=0, num_usable=0;
2578 const or_options_t *options = get_options();
2579 const networkstatus_t *consensus =
2580 networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
2581 int using_md;
2583 if (!consensus) {
2584 if (!networkstatus_get_latest_consensus())
2585 strlcpy(dir_info_status, "We have no usable consensus.",
2586 sizeof(dir_info_status));
2587 else
2588 strlcpy(dir_info_status, "We have no recent usable consensus.",
2589 sizeof(dir_info_status));
2590 res = 0;
2591 goto done;
2594 using_md = consensus->flavor == FLAV_MICRODESC;
2596 /* Check fraction of available paths */
2598 char *status = NULL;
2599 double paths = compute_frac_paths_available(consensus, options, now,
2600 &num_present, &num_usable,
2601 &status);
2603 if (paths < get_frac_paths_needed_for_circs(options,consensus)) {
2604 tor_snprintf(dir_info_status, sizeof(dir_info_status),
2605 "We need more %sdescriptors: we have %d/%d, and "
2606 "can only build %d%% of likely paths. (We have %s.)",
2607 using_md?"micro":"", num_present, num_usable,
2608 (int)(paths*100), status);
2609 tor_free(status);
2610 res = 0;
2611 control_event_boot_dir(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
2612 goto done;
2615 tor_free(status);
2616 res = 1;
2619 { /* Check entry guard dirinfo status */
2620 char *guard_error = entry_guards_get_err_str_if_dir_info_missing(using_md,
2621 num_present,
2622 num_usable);
2623 if (guard_error) {
2624 strlcpy(dir_info_status, guard_error, sizeof(dir_info_status));
2625 tor_free(guard_error);
2626 res = 0;
2627 goto done;
2631 done:
2633 /* If paths have just become available in this update. */
2634 if (res && !have_min_dir_info) {
2635 control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
2636 control_event_boot_dir(BOOTSTRAP_STATUS_ENOUGH_DIRINFO, 0);
2637 log_info(LD_DIR,
2638 "We now have enough directory information to build circuits.");
2641 /* If paths have just become unavailable in this update. */
2642 if (!res && have_min_dir_info) {
2643 int quiet = directory_too_idle_to_fetch_descriptors(options, now);
2644 tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
2645 "Our directory information is no longer up-to-date "
2646 "enough to build circuits: %s", dir_info_status);
2648 /* a) make us log when we next complete a circuit, so we know when Tor
2649 * is back up and usable, and b) disable some activities that Tor
2650 * should only do while circuits are working, like reachability tests
2651 * and fetching bridge descriptors only over circuits. */
2652 note_that_we_maybe_cant_complete_circuits();
2653 have_consensus_path = CONSENSUS_PATH_UNKNOWN;
2654 control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
2656 have_min_dir_info = res;
2657 need_to_update_have_min_dir_info = 0;