Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
nodelist.c
Go to the documentation of this file.
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-2021, The Tor Project, Inc. */
5/* See LICENSE for licensing information */
6
7/**
8 * \file nodelist.c
9 *
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.
13 *
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.
19 *
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
23 *
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.
29 *
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!)
34 *
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.)
39 */
40
41#define NODELIST_PRIVATE
42
43#include "core/or/or.h"
44#include "app/config/config.h"
47#include "core/or/address_set.h"
48#include "core/or/policies.h"
49#include "core/or/protover.h"
68#include "lib/err/backtrace.h"
69#include "lib/geoip/geoip.h"
70#include "lib/net/address.h"
71
72#include <string.h>
73
75
83
84static void nodelist_drop_node(node_t *node, int remove_from_ht);
85#define node_free(val) \
86 FREE_AND_NULL(node_t, node_free_, (val))
87static void node_free_(node_t *node);
88
89/** count_usable_descriptors counts descriptors with these flag(s)
90 */
91typedef enum {
92 /* All descriptors regardless of flags or exit policies */
93 USABLE_DESCRIPTOR_ALL = 0U,
94 /* Only count descriptors with an exit policy that allows at least one port
95 */
96 USABLE_DESCRIPTOR_EXIT_POLICY = 1U << 0,
97 /* Only count descriptors for relays that have the exit flag in the
98 * consensus */
99 USABLE_DESCRIPTOR_EXIT_FLAG = 1U << 1,
100 /* Only count descriptors for relays that have the policy and the flag */
101 USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG = (USABLE_DESCRIPTOR_EXIT_POLICY |
102 USABLE_DESCRIPTOR_EXIT_FLAG)
104static void count_usable_descriptors(int *num_present,
105 int *num_usable,
106 smartlist_t *descs_out,
107 const networkstatus_t *consensus,
108 time_t now,
109 routerset_t *in_set,
110 usable_descriptor_t exit_only);
112static double get_frac_paths_needed_for_circs(const or_options_t *options,
113 const networkstatus_t *ns);
114static void node_add_to_address_set(const node_t *node);
115
116/** A nodelist_t holds a node_t object for every router we're "willing to use
117 * for something". Specifically, it should hold a node_t for every node that
118 * is currently in the routerlist, or currently in the consensus we're using.
119 */
120typedef struct nodelist_t {
121 /* A list of all the nodes. */
122 smartlist_t *nodes;
123 /* Hash table to map from node ID digest to node. */
124 HT_HEAD(nodelist_map, node_t) nodes_by_id;
125 /* Hash table to map from node Ed25519 ID to node.
126 *
127 * Whenever a node's routerinfo or microdescriptor is about to change,
128 * you should remove it from this map with node_remove_from_ed25519_map().
129 * Whenever a node's routerinfo or microdescriptor has just changed,
130 * you should add it to this map with node_add_to_ed25519_map().
131 */
132 HT_HEAD(nodelist_ed_map, node_t) nodes_by_ed_id;
133
134 /* Set of addresses that belong to nodes we believe in. */
135 address_set_t *node_addrs;
136
137 /* Set of addresses + port that belong to nodes we know and that we don't
138 * allow network re-entry towards them. */
139 digestmap_t *reentry_set;
140
141 /* The valid-after time of the last live consensus that initialized the
142 * nodelist. We use this to detect outdated nodelists that need to be
143 * rebuilt using a newer consensus. */
144 time_t live_consensus_valid_after;
145} nodelist_t;
146
147static inline unsigned int
148node_id_hash(const node_t *node)
149{
150 return (unsigned) siphash24g(node->identity, DIGEST_LEN);
151}
152
153static inline unsigned int
154node_id_eq(const node_t *node1, const node_t *node2)
155{
156 return tor_memeq(node1->identity, node2->identity, DIGEST_LEN);
157}
158
159HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq);
160HT_GENERATE2(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
162
163static inline unsigned int
164node_ed_id_hash(const node_t *node)
165{
166 return (unsigned) siphash24g(node->ed25519_id.pubkey, ED25519_PUBKEY_LEN);
167}
168
169static inline unsigned int
170node_ed_id_eq(const node_t *node1, const node_t *node2)
171{
172 return ed25519_pubkey_eq(&node1->ed25519_id, &node2->ed25519_id);
173}
174
175HT_PROTOTYPE(nodelist_ed_map, node_t, ed_ht_ent, node_ed_id_hash,
176 node_ed_id_eq);
177HT_GENERATE2(nodelist_ed_map, node_t, ed_ht_ent, node_ed_id_hash,
178 node_ed_id_eq, 0.6, tor_reallocarray_, tor_free_);
179
180/** The global nodelist. */
182
183/** Create an empty nodelist if we haven't done so already. */
184static void
186{
187 if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
188 the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
189 HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
190 HT_INIT(nodelist_ed_map, &the_nodelist->nodes_by_ed_id);
191 the_nodelist->nodes = smartlist_new();
192 }
193}
194
195/** As node_get_by_id, but returns a non-const pointer */
197node_get_mutable_by_id,(const char *identity_digest))
198{
199 node_t search, *node;
200 if (PREDICT_UNLIKELY(the_nodelist == NULL))
201 return NULL;
202
203 memcpy(&search.identity, identity_digest, DIGEST_LEN);
204 node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
205 return node;
206}
207
208/** As node_get_by_ed25519_id, but returns a non-const pointer */
209node_t *
211{
212 node_t search, *node;
213 if (PREDICT_UNLIKELY(the_nodelist == NULL))
214 return NULL;
215 if (BUG(ed_id == NULL) || BUG(ed25519_public_key_is_zero(ed_id)))
216 return NULL;
217
218 memcpy(&search.ed25519_id, ed_id, sizeof(search.ed25519_id));
219 node = HT_FIND(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, &search);
220 return node;
221}
222
223/** Return the node_t whose identity is <b>identity_digest</b>, or NULL
224 * if no such node exists. */
225MOCK_IMPL(const node_t *,
226node_get_by_id,(const char *identity_digest))
227{
228 return node_get_mutable_by_id(identity_digest);
229}
230
231/** Return the node_t whose ed25519 identity is <b>ed_id</b>, or NULL
232 * if no such node exists. */
233MOCK_IMPL(const node_t *,
235{
236 return node_get_mutable_by_ed25519_id(ed_id);
237}
238
239/** Internal: return the node_t whose identity_digest is
240 * <b>identity_digest</b>. If none exists, create a new one, add it to the
241 * nodelist, and return it.
242 *
243 * Requires that the nodelist be initialized.
244 */
245static node_t *
246node_get_or_create(const char *identity_digest)
247{
248 node_t *node;
249
250 if ((node = node_get_mutable_by_id(identity_digest)))
251 return node;
252
253 node = tor_malloc_zero(sizeof(node_t));
254 memcpy(node->identity, identity_digest, DIGEST_LEN);
255 HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
256
257 smartlist_add(the_nodelist->nodes, node);
258 node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
259
260 node->country = -1;
261
262 return node;
263}
264
265/** Remove <b>node</b> from the ed25519 map (if it present), and
266 * set its ed25519_id field to zero. */
267static int
269{
271 tor_assert(node);
272
274 return 0;
275 }
276
277 int rv = 0;
278 node_t *search =
279 HT_FIND(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
280 if (BUG(search != node)) {
281 goto clear_and_return;
282 }
283
284 search = HT_REMOVE(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
285 tor_assert(search == node);
286 rv = 1;
287
288 clear_and_return:
289 memset(&node->ed25519_id, 0, sizeof(node->ed25519_id));
290 return rv;
291}
292
293/** Helper function to log details of duplicated ed2559_ids */
294static void
295node_log_dup_ed_id(const node_t *old, const node_t *node, const char *ed_id)
296{
297 char *s;
298 char *olddesc = tor_strdup(node_describe(old));
299
300 tor_asprintf(&s, "Reused ed25519_id %s: old %s new %s", ed_id,
301 olddesc, node_describe(node));
302 log_backtrace(LOG_NOTICE, LD_DIR, s);
303 tor_free(olddesc);
304 tor_free(s);
305}
306
307/** If <b>node</b> has an ed25519 id, and it is not already in the ed25519 id
308 * map, set its ed25519_id field, and add it to the ed25519 map.
309 */
310static int
312{
314 tor_assert(node);
315
317 return 0;
318 }
319
320 const ed25519_public_key_t *key = node_get_ed25519_id(node);
321 if (!key) {
322 return 0;
323 }
324
325 node_t *old;
326 memcpy(&node->ed25519_id, key, sizeof(node->ed25519_id));
327 old = HT_FIND(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
328 if (old) {
329 char ed_id[BASE32_BUFSIZE(sizeof(key->pubkey))];
330
331 base32_encode(ed_id, sizeof(ed_id), (const char *)key->pubkey,
332 sizeof(key->pubkey));
333 if (BUG(old == node)) {
334 /* Actual bug: all callers of this function call
335 * node_remove_from_ed25519_map first. */
336 log_err(LD_BUG,
337 "Unexpectedly found deleted node with ed25519_id %s", ed_id);
338 } else {
339 /* Distinct nodes sharing a ed25519 id, possibly due to relay
340 * misconfiguration. The key pinning might not catch this,
341 * possibly due to downloading a missing descriptor during
342 * consensus voting. */
343 node_log_dup_ed_id(old, node, ed_id);
344 memset(&node->ed25519_id, 0, sizeof(node->ed25519_id));
345 }
346 return 0;
347 }
348
349 HT_INSERT(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
350 return 1;
351}
352
353/* For a given <b>node</b> for the consensus <b>ns</b>, set the hsdir index
354 * for the node, both current and next if possible. This can only fails if the
355 * node_t ed25519 identity key can't be found which would be a bug. */
356STATIC void
357node_set_hsdir_index(node_t *node, const networkstatus_t *ns)
358{
359 time_t now = approx_time();
360 const ed25519_public_key_t *node_identity_pk;
361 uint8_t *fetch_srv = NULL, *store_first_srv = NULL, *store_second_srv = NULL;
362 uint64_t next_time_period_num, current_time_period_num;
363 uint64_t fetch_tp, store_first_tp, store_second_tp;
364
365 tor_assert(node);
366 tor_assert(ns);
367
369 static struct ratelim_t live_consensus_ratelim = RATELIM_INIT(30 * 60);
370 log_fn_ratelim(&live_consensus_ratelim, LOG_INFO, LD_GENERAL,
371 "Not setting hsdir index with a non-live consensus.");
372 goto done;
373 }
374
375 node_identity_pk = node_get_ed25519_id(node);
376 if (node_identity_pk == NULL) {
377 log_debug(LD_GENERAL, "ed25519 identity public key not found when "
378 "trying to build the hsdir indexes for node %s",
379 node_describe(node));
380 goto done;
381 }
382
383 /* Get the current and next time period number. */
384 current_time_period_num = hs_get_time_period_num(0);
385 next_time_period_num = hs_get_next_time_period_num(0);
386
387 /* We always use the current time period for fetching descs */
388 fetch_tp = current_time_period_num;
389
390 /* Now extract the needed SRVs and time periods for building hsdir indices */
391 if (hs_in_period_between_tp_and_srv(ns, now)) {
392 fetch_srv = hs_get_current_srv(fetch_tp, ns);
393
394 store_first_tp = hs_get_previous_time_period_num(0);
395 store_second_tp = current_time_period_num;
396 } else {
397 fetch_srv = hs_get_previous_srv(fetch_tp, ns);
398
399 store_first_tp = current_time_period_num;
400 store_second_tp = next_time_period_num;
401 }
402
403 /* We always use the old SRV for storing the first descriptor and the latest
404 * SRV for storing the second descriptor */
405 store_first_srv = hs_get_previous_srv(store_first_tp, ns);
406 store_second_srv = hs_get_current_srv(store_second_tp, ns);
407
408 /* Build the fetch index. */
409 hs_build_hsdir_index(node_identity_pk, fetch_srv, fetch_tp,
410 node->hsdir_index.fetch);
411
412 /* If we are in the time segment between SRV#N and TP#N, the fetch index is
413 the same as the first store index */
414 if (!hs_in_period_between_tp_and_srv(ns, now)) {
415 memcpy(node->hsdir_index.store_first, node->hsdir_index.fetch,
416 sizeof(node->hsdir_index.store_first));
417 } else {
418 hs_build_hsdir_index(node_identity_pk, store_first_srv, store_first_tp,
419 node->hsdir_index.store_first);
420 }
421
422 /* If we are in the time segment between TP#N and SRV#N+1, the fetch index is
423 the same as the second store index */
424 if (hs_in_period_between_tp_and_srv(ns, now)) {
425 memcpy(node->hsdir_index.store_second, node->hsdir_index.fetch,
426 sizeof(node->hsdir_index.store_second));
427 } else {
428 hs_build_hsdir_index(node_identity_pk, store_second_srv, store_second_tp,
429 node->hsdir_index.store_second);
430 }
431
432 done:
433 tor_free(fetch_srv);
434 tor_free(store_first_srv);
435 tor_free(store_second_srv);
436 return;
437}
438
439/** Called when a node's address changes. */
440static void
442{
443 node->last_reachable = node->last_reachable6 = 0;
444 node->country = -1;
445}
446
447/** Add all address information about <b>node</b> to the current address
448 * set (if there is one).
449 */
450static void
452{
453 if (!the_nodelist ||
454 !the_nodelist->node_addrs || !the_nodelist->reentry_set)
455 return;
456
457 /* These various address sources can be redundant, but it's likely faster to
458 * add them all than to compare them all for equality.
459 *
460 * For relays, we only add the ORPort in the addr+port set since we want to
461 * allow re-entry into the network to the DirPort so the self reachability
462 * test succeeds and thus the 0 value for the DirPort. */
463
464 if (node->rs) {
465 if (!tor_addr_is_null(&node->rs->ipv4_addr))
466 nodelist_add_addr_to_address_set(&node->rs->ipv4_addr,
467 node->rs->ipv4_orport, 0);
468 if (!tor_addr_is_null(&node->rs->ipv6_addr))
470 node->rs->ipv6_orport, 0);
471 }
472 if (node->ri) {
473 if (!tor_addr_is_null(&node->ri->ipv4_addr))
475 node->ri->ipv4_orport, 0);
476 if (!tor_addr_is_null(&node->ri->ipv6_addr))
478 node->ri->ipv6_orport, 0);
479 }
480 if (node->md) {
481 if (!tor_addr_is_null(&node->md->ipv6_addr))
483 node->md->ipv6_orport, 0);
484 }
485}
486
487/** Build a construction for the reentry set consisting of an address and port
488 * pair.
489 *
490 * If the given address is _not_ AF_INET or AF_INET6, then the item is an
491 * array of 0s.
492 *
493 * Return a pointer to a static buffer containing the item. Next call to this
494 * function invalidates its previous content. */
495static char *
496build_addr_port_item(const tor_addr_t *addr, const uint16_t port)
497{
498 /* At most 16 bytes are put in this (IPv6) and then 2 bytes for the port
499 * which is within the maximum of 20 bytes (DIGEST_LEN). */
500 static char data[DIGEST_LEN];
501
502 memset(data, 0, sizeof(data));
503 switch (tor_addr_family(addr)) {
504 case AF_INET:
505 memcpy(data, &addr->addr.in_addr.s_addr, 4);
506 break;
507 case AF_INET6:
508 memcpy(data, &addr->addr.in6_addr.s6_addr, 16);
509 break;
510 case AF_UNSPEC:
511 /* Leave the 0. */
512 break;
513 default:
514 /* LCOV_EXCL_START */
516 /* LCOV_EXCL_STOP */
517 }
518
519 memcpy(data + 16, &port, sizeof(port));
520 return data;
521}
522
523/** Add the given address into the nodelist address set. */
524void
526 uint16_t or_port, uint16_t dir_port)
527{
528 if (BUG(!addr) || tor_addr_is_null(addr) ||
529 (!tor_addr_is_v4(addr) && !tor_addr_is_v6(addr)) ||
530 !the_nodelist || !the_nodelist->node_addrs ||
531 !the_nodelist->reentry_set) {
532 return;
533 }
534 address_set_add(the_nodelist->node_addrs, addr);
535 if (or_port != 0) {
536 digestmap_set(the_nodelist->reentry_set,
537 build_addr_port_item(addr, or_port), (void*) 1);
538 }
539 if (dir_port != 0) {
540 digestmap_set(the_nodelist->reentry_set,
541 build_addr_port_item(addr, dir_port), (void*) 1);
542 }
543}
544
545/** Return true if <b>addr</b> is the address of some node in the nodelist.
546 * If not, probably return false. */
547int
549{
550 if (BUG(!addr))
551 return 0;
552
553 if (!the_nodelist || !the_nodelist->node_addrs)
554 return 0;
555
556 return address_set_probably_contains(the_nodelist->node_addrs, addr);
557}
558
559/** Return true if <b>addr</b> is the address of some node in the nodelist and
560 * corresponds also to the given port. If not, probably return false. */
561bool
562nodelist_reentry_contains(const tor_addr_t *addr, uint16_t port)
563{
564 if (BUG(!addr) || BUG(!port))
565 return false;
566
567 if (!the_nodelist || !the_nodelist->reentry_set)
568 return false;
569
570 return digestmap_get(the_nodelist->reentry_set,
571 build_addr_port_item(addr, port)) != NULL;
572}
573
574/** Add <b>ri</b> to an appropriate node in the nodelist. If we replace an
575 * old routerinfo, and <b>ri_old_out</b> is not NULL, set *<b>ri_old_out</b>
576 * to the previous routerinfo.
577 */
578node_t *
580{
581 node_t *node;
582 const char *id_digest;
583 int had_router = 0;
584 tor_assert(ri);
585
587 id_digest = ri->cache_info.identity_digest;
588 node = node_get_or_create(id_digest);
589
591
592 if (node->ri) {
593 if (!routers_have_same_or_addrs(node->ri, ri)) {
594 node_addrs_changed(node);
595 }
596 had_router = 1;
597 if (ri_old_out)
598 *ri_old_out = node->ri;
599 } else {
600 if (ri_old_out)
601 *ri_old_out = NULL;
602 }
603 node->ri = ri;
604
606
607 if (node->country == -1)
608 node_set_country(node);
609
610 if (authdir_mode(get_options()) && !had_router) {
611 const char *discard=NULL;
612 uint32_t status = dirserv_router_get_status(ri, &discard, LOG_INFO);
614 }
615
616 /* Setting the HSDir index requires the ed25519 identity key which can
617 * only be found either in the ri or md. This is why this is called here.
618 * Only nodes supporting HSDir=2 protocol version needs this index. */
619 if (node->rs && node->rs->pv.supports_v3_hsdir) {
620 node_set_hsdir_index(node,
622 }
623
625
626 return node;
627}
628
629/** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
630 *
631 * Called when a new microdesc has arrived and the usable consensus flavor
632 * is "microdesc".
633 **/
634node_t *
636{
637 networkstatus_t *ns =
639 const routerstatus_t *rs;
640 node_t *node;
641 if (ns == NULL)
642 return NULL;
644
645 /* Microdescriptors don't carry an identity digest, so we need to figure
646 * it out by looking up the routerstatus. */
648 if (rs == NULL)
649 return NULL;
651 if (node == NULL)
652 return NULL;
653
655 if (node->md)
656 node->md->held_by_nodes--;
657
658 node->md = md;
659 md->held_by_nodes++;
660 /* Setting the HSDir index requires the ed25519 identity key which can
661 * only be found either in the ri or md. This is why this is called here.
662 * Only nodes supporting HSDir=2 protocol version needs this index. */
663 if (rs->pv.supports_v3_hsdir) {
664 node_set_hsdir_index(node, ns);
665 }
668
669 return node;
670}
671
672/* Default value. */
673#define ESTIMATED_ADDRESS_PER_NODE 2
674
675/* Return the estimated number of address per node_t. This is used for the
676 * size of the bloom filter in the nodelist (node_addrs). */
677MOCK_IMPL(int,
678get_estimated_address_per_node, (void))
679{
680 return ESTIMATED_ADDRESS_PER_NODE;
681}
682
683/**
684 * If true, we use relays' listed family members in order to
685 * determine which relays are in the same family.
686 */
687static int use_family_lists = 1;
688/**
689 * If true, we use relays' validated family IDs in order to
690 * determine which relays are in the same family.
691 */
692static int use_family_ids = 1;
693
694/**
695 * Update consensus parameters relevant to nodelist operations.
696 *
697 * We need to cache these values rather than searching for them every time
698 * we check whether two relays are in the same family.
699 **/
700static void
702{
703 use_family_lists = networkstatus_get_param(ns, "use-family-lists",
704 1, 0, 1); // default, low, high
705 use_family_ids = networkstatus_get_param(ns, "use-family-ids",
706 1, 0, 1); // default, low, high
707}
708
709/** Tell the nodelist that the current usable consensus is <b>ns</b>.
710 * This makes the nodelist change all of the routerstatus entries for
711 * the nodes, drop nodes that no longer have enough info to get used,
712 * and grab microdescriptors into nodes as appropriate.
713 */
714void
716{
717 const or_options_t *options = get_options();
718 int authdir = authdir_mode_v3(options);
719
721 if (ns->flavor == FLAV_MICRODESC)
722 (void) get_microdesc_cache(); /* Make sure it exists first. */
723
724 SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
725 node->rs = NULL);
726
728
729 /* Conservatively estimate that every node will have 2 addresses (v4 and
730 * v6). Then we add the number of configured trusted authorities we have. */
731 int estimated_addresses = smartlist_len(ns->routerstatus_list) *
732 get_estimated_address_per_node();
733 estimated_addresses += (get_n_authorities(V3_DIRINFO | BRIDGE_DIRINFO) *
734 get_estimated_address_per_node());
735 /* Clear our sets because we will repopulate them with what this new
736 * consensus contains. */
737 address_set_free(the_nodelist->node_addrs);
738 the_nodelist->node_addrs = address_set_new(estimated_addresses);
739 digestmap_free(the_nodelist->reentry_set, NULL);
740 the_nodelist->reentry_set = digestmap_new();
741
743 node_t *node = node_get_or_create(rs->identity_digest);
744 node->rs = rs;
745 if (ns->flavor == FLAV_MICRODESC) {
746 if (node->md == NULL ||
747 tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
749 if (node->md)
750 node->md->held_by_nodes--;
752 rs->descriptor_digest);
753 if (node->md)
754 node->md->held_by_nodes++;
756 }
757 }
758
759 if (rs->pv.supports_v3_hsdir) {
760 node_set_hsdir_index(node, ns);
761 }
762 node_set_country(node);
763
764 /* Set node's flags based on rs's flags. */
765 {
766 node->is_valid = rs->is_valid;
767 node->is_running = rs->is_flagged_running;
768 node->is_fast = rs->is_fast;
769 node->is_stable = rs->is_stable;
770 node->is_possible_guard = rs->is_possible_guard;
771 node->is_exit = rs->is_exit;
772 if (!authdir) {
773 /* Authdirs treat is_bad_exit specially in that they only assign
774 * it when the descriptor arrives. So when a dir auth is reading
775 * the flags from an existing consensus, don't believe the bit
776 * here, else it will get stuck 'on' forever. */
777 node->is_bad_exit = rs->is_bad_exit;
778 }
779 node->is_hs_dir = rs->is_hs_dir;
780 node->ipv6_preferred = 0;
782 (tor_addr_is_null(&rs->ipv6_addr) == 0 ||
783 (node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
784 node->ipv6_preferred = 1;
785 }
786
787 } SMARTLIST_FOREACH_END(rs);
788
790
791 /* Now add all the nodes we have to the address set. */
794 } SMARTLIST_FOREACH_END(node);
795 /* Then, add all trusted configured directories. Some might not be in the
796 * consensus so make sure we know them. */
798
799 if (! authdir) {
801 /* We have no routerstatus for this router. Clear flags so we can skip
802 * it, maybe.*/
803 if (!node->rs) {
804 tor_assert(node->ri); /* if it had only an md, or nothing, purge
805 * would have removed it. */
806 if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
807 /* Clear all flags. */
808 node->is_valid = node->is_running = node->is_hs_dir =
809 node->is_fast = node->is_stable =
810 node->is_possible_guard = node->is_exit =
811 node->is_bad_exit = node->ipv6_preferred = 0;
812 }
813 }
814 } SMARTLIST_FOREACH_END(node);
815 }
816
817 /* If the consensus is live, note down the consensus valid-after that formed
818 * the nodelist. */
820 the_nodelist->live_consensus_valid_after = ns->valid_after;
821 }
822}
823
824/** Return 1 iff <b>node</b> has Exit flag and no BadExit flag.
825 * Otherwise, return 0.
826 */
827int
829{
830 return node->is_exit && ! node->is_bad_exit;
831}
832
833/** Helper: return true iff a node has a usable amount of information*/
834static inline int
836{
837 return (node->rs) || (node->ri);
838}
839
840/** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
841 * node with <b>identity_digest</b>. */
842void
843nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
844{
845 node_t *node = node_get_mutable_by_id(identity_digest);
846 if (node && node->md == md) {
847 node->md = NULL;
848 md->held_by_nodes--;
849 if (! node_get_ed25519_id(node)) {
851 }
852 }
853}
854
855/** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
856void
858{
859 node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
860 if (node && node->ri == ri) {
861 node->ri = NULL;
862 if (! node_is_usable(node)) {
863 nodelist_drop_node(node, 1);
864 node_free(node);
865 }
866 }
867}
868
869/** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
870 * with.) */
871static void
872nodelist_drop_node(node_t *node, int remove_from_ht)
873{
874 node_t *tmp;
875 int idx;
876 if (remove_from_ht) {
877 tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
878 tor_assert(tmp == node);
879 }
881
882 idx = node->nodelist_idx;
883 tor_assert(idx >= 0);
884
885 tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
886 smartlist_del(the_nodelist->nodes, idx);
887 if (idx < smartlist_len(the_nodelist->nodes)) {
888 tmp = smartlist_get(the_nodelist->nodes, idx);
889 tmp->nodelist_idx = idx;
890 }
891 node->nodelist_idx = -1;
892}
893
894/** Return a newly allocated smartlist of the nodes that have <b>md</b> as
895 * their microdescriptor. */
898{
899 smartlist_t *result = smartlist_new();
900
901 if (the_nodelist == NULL)
902 return result;
903
905 if (node->md == md) {
906 smartlist_add(result, node);
907 }
908 } SMARTLIST_FOREACH_END(node);
909
910 return result;
911}
912
913/** Release storage held by <b>node</b> */
914static void
916{
917 if (!node)
918 return;
919 if (node->md)
920 node->md->held_by_nodes--;
921 tor_assert(node->nodelist_idx == -1);
922 tor_free(node);
923}
924
925/** Remove all entries from the nodelist that don't have enough info to be
926 * usable for anything. */
927void
929{
930 node_t **iter;
931 if (PREDICT_UNLIKELY(the_nodelist == NULL))
932 return;
933
934 /* Remove the non-usable nodes. */
935 for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
936 node_t *node = *iter;
937
938 if (node->md && !node->rs) {
939 /* An md is only useful if there is an rs. */
940 node->md->held_by_nodes--;
941 node->md = NULL;
942 }
943
944 if (node_is_usable(node)) {
945 iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
946 } else {
947 iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
948 nodelist_drop_node(node, 0);
949 node_free(node);
950 }
951 }
953}
954
955/** Release all storage held by the nodelist. */
956void
958{
959 if (PREDICT_UNLIKELY(the_nodelist == NULL))
960 return;
961
962 HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
963 HT_CLEAR(nodelist_ed_map, &the_nodelist->nodes_by_ed_id);
965 node->nodelist_idx = -1;
966 node_free(node);
967 } SMARTLIST_FOREACH_END(node);
968
969 smartlist_free(the_nodelist->nodes);
970
971 address_set_free(the_nodelist->node_addrs);
972 the_nodelist->node_addrs = NULL;
973 digestmap_free(the_nodelist->reentry_set, NULL);
974 the_nodelist->reentry_set = NULL;
975
977}
978
979/** Check that the nodelist is internally consistent, and consistent with
980 * the directory info it's derived from.
981 */
982void
984{
987 digestmap_t *dm;
988
989 if (!the_nodelist)
990 return;
991
992 dm = digestmap_new();
993
994 /* every routerinfo in rl->routers should be in the nodelist. */
995 if (rl) {
997 const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
998 tor_assert(node && node->ri == ri);
999 tor_assert(fast_memeq(ri->cache_info.identity_digest,
1000 node->identity, DIGEST_LEN));
1001 tor_assert(! digestmap_get(dm, node->identity));
1002 digestmap_set(dm, node->identity, (void*)node);
1003 } SMARTLIST_FOREACH_END(ri);
1004 }
1005
1006 /* every routerstatus in ns should be in the nodelist */
1007 if (ns) {
1009 const node_t *node = node_get_by_id(rs->identity_digest);
1010 tor_assert(node && node->rs == rs);
1011 tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
1012 digestmap_set(dm, node->identity, (void*)node);
1013 if (ns->flavor == FLAV_MICRODESC) {
1014 /* If it's a microdesc consensus, every entry that has a
1015 * microdescriptor should be in the nodelist.
1016 */
1017 microdesc_t *md =
1018 microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
1019 tor_assert(md == node->md);
1020 if (md)
1021 tor_assert(md->held_by_nodes >= 1);
1022 }
1023 } SMARTLIST_FOREACH_END(rs);
1024 }
1025
1026 /* The nodelist should have no other entries, and its entries should be
1027 * well-formed. */
1029 tor_assert(digestmap_get(dm, node->identity) != NULL);
1030 tor_assert(node_sl_idx == node->nodelist_idx);
1031 } SMARTLIST_FOREACH_END(node);
1032
1033 /* Every node listed with an ed25519 identity should be listed by that
1034 * identity.
1035 */
1039 }
1040 } SMARTLIST_FOREACH_END(node);
1041
1042 node_t **idx;
1043 HT_FOREACH(idx, nodelist_ed_map, &the_nodelist->nodes_by_ed_id) {
1044 node_t *node = *idx;
1046 }
1047
1048 tor_assert((long)smartlist_len(the_nodelist->nodes) ==
1049 (long)HT_SIZE(&the_nodelist->nodes_by_id));
1050
1051 tor_assert((long)smartlist_len(the_nodelist->nodes) >=
1052 (long)HT_SIZE(&the_nodelist->nodes_by_ed_id));
1053
1054 digestmap_free(dm, NULL);
1055}
1056
1057/** Ensure that the nodelist has been created with the most recent consensus.
1058 * If that's not the case, make it so. */
1059void
1061{
1062 tor_assert(ns);
1063
1064 /* We don't even have a nodelist: this is a NOP. */
1065 if (!the_nodelist) {
1066 return;
1067 }
1068
1069 if (the_nodelist->live_consensus_valid_after != ns->valid_after) {
1070 log_info(LD_GENERAL, "Nodelist was not fresh: rebuilding. (%d / %d)",
1071 (int) the_nodelist->live_consensus_valid_after,
1072 (int) ns->valid_after);
1074 }
1075}
1076
1077/** Return a list of a node_t * for every node we know about. The caller
1078 * MUST NOT modify the list. (You can set and clear flags in the nodes if
1079 * you must, but you must not add or remove nodes.) */
1080MOCK_IMPL(const smartlist_t *,
1082{
1083 init_nodelist();
1084 return the_nodelist->nodes;
1085}
1086
1087/** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
1088 * or $DIGEST~name, return the node with the matching identity digest and
1089 * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
1090 * is not well-formed. DOCDOC flags */
1091const node_t *
1092node_get_by_hex_id(const char *hex_id, unsigned flags)
1093{
1094 char digest_buf[DIGEST_LEN];
1095 char nn_buf[MAX_NICKNAME_LEN+1];
1096 char nn_char='\0';
1097
1098 (void) flags; // XXXX
1099
1100 if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
1101 const node_t *node = node_get_by_id(digest_buf);
1102 if (!node)
1103 return NULL;
1104 if (nn_char == '=') {
1105 /* "=" indicates a Named relay, but there aren't any of those now. */
1106 return NULL;
1107 }
1108 return node;
1109 }
1110
1111 return NULL;
1112}
1113
1114/** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
1115 * the corresponding node_t, or NULL if none exists. Warn the user if they
1116 * have specified a router by nickname, unless the NNF_NO_WARN_UNNAMED bit is
1117 * set in <b>flags</b>. */
1118MOCK_IMPL(const node_t *,
1119node_get_by_nickname,(const char *nickname, unsigned flags))
1120{
1121 const int warn_if_unnamed = !(flags & NNF_NO_WARN_UNNAMED);
1122
1123 if (!the_nodelist)
1124 return NULL;
1125
1126 /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
1127 {
1128 const node_t *node;
1129 if ((node = node_get_by_hex_id(nickname, flags)) != NULL)
1130 return node;
1131 }
1132
1133 if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
1134 return NULL;
1135
1136 /* Okay, so the name is not canonical for anybody. */
1137 {
1138 smartlist_t *matches = smartlist_new();
1139 const node_t *choice = NULL;
1140
1142 if (!strcasecmp(node_get_nickname(node), nickname))
1143 smartlist_add(matches, node);
1144 } SMARTLIST_FOREACH_END(node);
1145
1146 if (smartlist_len(matches)>1 && warn_if_unnamed) {
1147 int any_unwarned = 0;
1148 SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
1149 if (!node->name_lookup_warned) {
1150 node->name_lookup_warned = 1;
1151 any_unwarned = 1;
1152 }
1153 } SMARTLIST_FOREACH_END(node);
1154
1155 if (any_unwarned) {
1156 log_warn(LD_CONFIG, "There are multiple matches for the name %s. "
1157 "Choosing one arbitrarily.", nickname);
1158 }
1159 } else if (smartlist_len(matches)==1 && warn_if_unnamed) {
1160 char fp[HEX_DIGEST_LEN+1];
1161 node_t *node = smartlist_get(matches, 0);
1162 if (! node->name_lookup_warned) {
1163 base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
1164 log_warn(LD_CONFIG,
1165 "You specified a relay \"%s\" by name, but nicknames can be "
1166 "used by any relay, not just the one you meant. "
1167 "To make sure you get the same relay in the future, refer "
1168 "to it by key, as \"$%s\".", nickname, fp);
1169 node->name_lookup_warned = 1;
1170 }
1171 }
1172
1173 if (smartlist_len(matches))
1174 choice = smartlist_get(matches, 0);
1175
1176 smartlist_free(matches);
1177 return choice;
1178 }
1179}
1180
1181/** Return the Ed25519 identity key for the provided node, or NULL if it
1182 * doesn't have one. */
1185{
1186 const ed25519_public_key_t *ri_pk = NULL;
1187 const ed25519_public_key_t *md_pk = NULL;
1188
1189 if (node->ri) {
1190 if (node->ri->cache_info.signing_key_cert) {
1191 ri_pk = &node->ri->cache_info.signing_key_cert->signing_key;
1192 /* Checking whether routerinfo ed25519 is all zero.
1193 * Our descriptor parser should make sure this never happens. */
1194 if (BUG(ed25519_public_key_is_zero(ri_pk)))
1195 ri_pk = NULL;
1196 }
1197 }
1198
1199 if (node->md) {
1200 if (node->md->ed25519_identity_pkey) {
1201 md_pk = node->md->ed25519_identity_pkey;
1202 /* Checking whether microdesc ed25519 is all zero.
1203 * Our descriptor parser should make sure this never happens. */
1204 if (BUG(ed25519_public_key_is_zero(md_pk)))
1205 md_pk = NULL;
1206 }
1207 }
1208
1209 if (ri_pk && md_pk) {
1210 if (ed25519_pubkey_eq(ri_pk, md_pk)) {
1211 return ri_pk;
1212 } else {
1213 /* This can happen if the relay gets flagged NoEdConsensus which will be
1214 * triggered on all relays of the network. Thus a protocol warning. */
1215 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1216 "Inconsistent ed25519 identities in the nodelist");
1217 return NULL;
1218 }
1219 } else if (ri_pk) {
1220 return ri_pk;
1221 } else {
1222 return md_pk;
1223 }
1224}
1225
1226/** Return true iff this node's Ed25519 identity matches <b>id</b>.
1227 * (An absent Ed25519 identity matches NULL or zero.) */
1228int
1230{
1231 const ed25519_public_key_t *node_id = node_get_ed25519_id(node);
1232 if (node_id == NULL || ed25519_public_key_is_zero(node_id)) {
1233 return id == NULL || ed25519_public_key_is_zero(id);
1234 } else {
1235 return id && ed25519_pubkey_eq(node_id, id);
1236 }
1237}
1238
1239/** Dummy object that should be unreturnable. Used to ensure that
1240 * node_get_protover_summary_flags() always returns non-NULL. */
1242 0,0,0,0,0,0,0,0,0,0,0,0,0,0
1243};
1244
1245/** Return the protover_summary_flags for a given node. */
1246static const protover_summary_flags_t *
1248{
1249 if (node->rs) {
1250 return &node->rs->pv;
1251 } else if (node->ri) {
1252 return &node->ri->pv;
1253 } else {
1254 /* This should be impossible: every node should have a routerstatus or a
1255 * router descriptor or both. But just in case we've messed up somehow,
1256 * return a nice empty set of flags to indicate "this node supports
1257 * nothing." */
1258 tor_assert_nonfatal_unreached_once();
1259 return &zero_protover_flags;
1260 }
1261}
1262
1263/** Return true iff <b>node</b> supports authenticating itself
1264 * by ed25519 ID during the link handshake. If <b>compatible_with_us</b>,
1265 * it needs to be using a link authentication method that we understand.
1266 * If not, any plausible link authentication method will do. */
1267MOCK_IMPL(bool,
1269 bool compatible_with_us))
1270{
1271 if (! node_get_ed25519_id(node))
1272 return 0;
1273
1275
1276 if (compatible_with_us)
1278 else
1280}
1281
1282/** Return true iff <b>node</b> supports the hidden service directory version
1283 * 3 protocol (proposal 224). */
1284bool
1286{
1287 tor_assert(node);
1288
1290}
1291
1292/** Return true iff <b>node</b> supports ed25519 authentication as an hidden
1293 * service introduction point.*/
1294bool
1296{
1297 tor_assert(node);
1298
1300}
1301
1302/** Return true iff <b>node</b> can be a rendezvous point for hidden
1303 * service version 3 (HSRend=2). */
1304bool
1306{
1307 tor_assert(node);
1308
1309 /* We can't use a v3 rendezvous point without the curve25519 onion pk. */
1310 if (!node_get_curve25519_onion_key(node)) {
1311 return 0;
1312 }
1313
1315}
1316
1317/** Return true iff <b>node</b> supports the DoS ESTABLISH_INTRO cell
1318 * extension. */
1319bool
1321{
1322 tor_assert(node);
1323
1324 return node_get_protover_summary_flags(node)->
1325 supports_establish_intro_dos_extension;
1326}
1327
1328/** Return true iff <b>node</b> can initiate IPv6 extends (Relay=3).
1329 *
1330 * This check should only be performed by client path selection code.
1331 *
1332 * Extending relays should check their own IPv6 support using
1333 * router_can_extend_over_ipv6(). Like other extends, they should not verify
1334 * the link specifiers in the extend cell against the consensus, because it
1335 * may be out of date. */
1336bool
1338{
1339 tor_assert(node);
1340
1341 /* Relays can't initiate an IPv6 extend, unless they have an IPv6 ORPort. */
1342 if (!node_has_ipv6_orport(node)) {
1343 return 0;
1344 }
1345
1346 /* Initiating relays also need to support the relevant protocol version. */
1347 return
1349}
1350
1351/** Return true iff <b>node</b> can accept IPv6 extends (Relay=2 or Relay=3)
1352 * from other relays. If <b>need_canonical_ipv6_conn</b> is true, also check
1353 * if the relay supports canonical IPv6 connections (Relay=3 only).
1354 *
1355 * This check should only be performed by client path selection code.
1356 */
1357bool
1359 bool need_canonical_ipv6_conn)
1360{
1361 tor_assert(node);
1362
1363 /* Relays can't accept an IPv6 extend, unless they have an IPv6 ORPort. */
1364 if (!node_has_ipv6_orport(node)) {
1365 return 0;
1366 }
1367
1368 /* Accepting relays also need to support the relevant protocol version. */
1369 if (need_canonical_ipv6_conn) {
1370 return
1372 } else {
1373 return
1375 }
1376}
1377
1378/** Return true iff the given node supports conflux (Relay=5) */
1379bool
1381{
1382 tor_assert(node);
1383
1385}
1386
1387/** Return the RSA ID key's SHA1 digest for the provided node. */
1388const uint8_t *
1390{
1391 tor_assert(node);
1392 return (const uint8_t*)node->identity;
1393}
1394
1395/* Returns a new smartlist with all possible link specifiers from node:
1396 * - legacy ID is mandatory thus MUST be present in node;
1397 * - include ed25519 link specifier if present in the node, and the node
1398 * supports ed25519 link authentication, and:
1399 * - if direct_conn is true, its link versions are compatible with us,
1400 * - if direct_conn is false, regardless of its link versions;
1401 * - include IPv4 link specifier, if the primary address is not IPv4, log a
1402 * BUG() warning, and return an empty smartlist;
1403 * - include IPv6 link specifier if present in the node.
1404 *
1405 * If node is NULL, returns an empty smartlist.
1406 *
1407 * The smartlist must be freed using link_specifier_smartlist_free(). */
1409node_get_link_specifier_smartlist,(const node_t *node, bool direct_conn))
1410{
1411 link_specifier_t *ls;
1412 tor_addr_port_t ap;
1413 smartlist_t *lspecs = smartlist_new();
1414
1415 if (!node)
1416 return lspecs;
1417
1418 /* Get the relay's IPv4 address. */
1419 node_get_prim_orport(node, &ap);
1420
1421 /* We expect the node's primary address to be a valid IPv4 address.
1422 * This conforms to the protocol, which requires either an IPv4 or IPv6
1423 * address (or both). */
1424 if (BUG(!tor_addr_is_v4(&ap.addr)) ||
1425 BUG(!tor_addr_port_is_valid_ap(&ap, 0))) {
1426 return lspecs;
1427 }
1428
1429 ls = link_specifier_new();
1430 link_specifier_set_ls_type(ls, LS_IPV4);
1431 link_specifier_set_un_ipv4_addr(ls, tor_addr_to_ipv4h(&ap.addr));
1432 link_specifier_set_un_ipv4_port(ls, ap.port);
1433 /* Four bytes IPv4 and two bytes port. */
1434 link_specifier_set_ls_len(ls, sizeof(ap.addr.addr.in_addr) +
1435 sizeof(ap.port));
1436 smartlist_add(lspecs, ls);
1437
1438 /* Legacy ID is mandatory and will always be present in node. */
1439 ls = link_specifier_new();
1440 link_specifier_set_ls_type(ls, LS_LEGACY_ID);
1441 memcpy(link_specifier_getarray_un_legacy_id(ls), node->identity,
1442 link_specifier_getlen_un_legacy_id(ls));
1443 link_specifier_set_ls_len(ls, link_specifier_getlen_un_legacy_id(ls));
1444 smartlist_add(lspecs, ls);
1445
1446 /* ed25519 ID is only included if the node has it, and the node declares a
1447 protocol version that supports ed25519 link authentication.
1448 If direct_conn is true, we also require that the node's link version is
1449 compatible with us. (Otherwise, we will be sending the ed25519 key
1450 to another tor, which may support different link versions.) */
1452 node_supports_ed25519_link_authentication(node, direct_conn)) {
1453 ls = link_specifier_new();
1454 link_specifier_set_ls_type(ls, LS_ED25519_ID);
1455 memcpy(link_specifier_getarray_un_ed25519_id(ls), &node->ed25519_id,
1456 link_specifier_getlen_un_ed25519_id(ls));
1457 link_specifier_set_ls_len(ls, link_specifier_getlen_un_ed25519_id(ls));
1458 smartlist_add(lspecs, ls);
1459 }
1460
1461 /* Check for IPv6. If so, include it as well. */
1462 if (node_has_ipv6_orport(node)) {
1463 ls = link_specifier_new();
1464 node_get_pref_ipv6_orport(node, &ap);
1465 link_specifier_set_ls_type(ls, LS_IPV6);
1466 size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
1467 const uint8_t *in6_addr = tor_addr_to_in6_addr8(&ap.addr);
1468 uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
1469 memcpy(ipv6_array, in6_addr, addr_len);
1470 link_specifier_set_un_ipv6_port(ls, ap.port);
1471 /* Sixteen bytes IPv6 and two bytes port. */
1472 link_specifier_set_ls_len(ls, addr_len + sizeof(ap.port));
1473 smartlist_add(lspecs, ls);
1474 }
1475
1476 return lspecs;
1477}
1478
1479/* Free a link specifier list. */
1480void
1481link_specifier_smartlist_free_(smartlist_t *ls_list)
1482{
1483 if (!ls_list)
1484 return;
1485
1486 SMARTLIST_FOREACH(ls_list, link_specifier_t *, lspec,
1487 link_specifier_free(lspec));
1488 smartlist_free(ls_list);
1489}
1490
1491/** Return the nickname of <b>node</b>, or NULL if we can't find one. */
1492const char *
1494{
1495 tor_assert(node);
1496 if (node->rs)
1497 return node->rs->nickname;
1498 else if (node->ri)
1499 return node->ri->nickname;
1500 else
1501 return NULL;
1502}
1503
1504/** Return true iff <b>node</b> appears to be a directory authority or
1505 * directory cache */
1506int
1508{
1509 if (node->rs) {
1510 routerstatus_t *rs = node->rs;
1511 /* This is true if supports_tunnelled_dir_requests is true which
1512 * indicates that we support directory request tunnelled or through the
1513 * DirPort. */
1514 return rs->is_v2_dir;
1515 } else if (node->ri) {
1516 routerinfo_t *ri = node->ri;
1517 /* Both tunnelled request is supported or DirPort is set. */
1519 } else {
1520 return 0;
1521 }
1522}
1523
1524/** Return true iff <b>node</b> has either kind of descriptor -- that
1525 * is, a routerdescriptor or a microdescriptor.
1526 *
1527 * You should probably use node_has_preferred_descriptor() instead.
1528 **/
1529int
1531{
1532 return (node->ri ||
1533 (node->rs && node->md));
1534}
1535
1536/** Return true iff <b>node</b> has the kind of descriptor we would prefer to
1537 * use for it, given our configuration and how we intend to use the node.
1538 *
1539 * If <b>for_direct_connect</b> is true, we intend to connect to the node
1540 * directly, as the first hop of a circuit; otherwise, we intend to connect to
1541 * it indirectly, or use it as if we were connecting to it indirectly. */
1542int
1544 int for_direct_connect)
1545{
1546 const int is_bridge = node_is_a_configured_bridge(node);
1547 const int we_use_mds = we_use_microdescriptors_for_circuits(get_options());
1548
1549 if ((is_bridge && for_direct_connect) || !we_use_mds) {
1550 /* We need an ri in this case. */
1551 if (!node->ri)
1552 return 0;
1553 } else {
1554 /* Otherwise we need an rs and an md. */
1555 if (node->rs == NULL || node->md == NULL)
1556 return 0;
1557 }
1558
1559 return 1;
1560}
1561
1562/** Return the router_purpose of <b>node</b>. */
1563int
1565{
1566 if (node->ri)
1567 return node->ri->purpose;
1568 else
1570}
1571
1572/** Compute the verbose ("extended") nickname of <b>node</b> and store it
1573 * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
1574 * <b>verbose_name_out</b> */
1575void
1577 char *verbose_name_out)
1578{
1579 const char *nickname = node_get_nickname(node);
1580 verbose_name_out[0] = '$';
1581 base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
1582 DIGEST_LEN);
1583 if (!nickname)
1584 return;
1585 verbose_name_out[1+HEX_DIGEST_LEN] = '~';
1586 strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
1587}
1588
1589/** Compute the verbose ("extended") nickname of node with
1590 * given <b>id_digest</b> and store it into the MAX_VERBOSE_NICKNAME_LEN+1
1591 * character buffer at <b>verbose_name_out</b>
1592 *
1593 * If node_get_by_id() returns NULL, base 16 encoding of
1594 * <b>id_digest</b> is returned instead. */
1595void
1597 char *verbose_name_out)
1598{
1599 const node_t *node = node_get_by_id(id_digest);
1600 if (!node) {
1601 verbose_name_out[0] = '$';
1602 base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
1603 } else {
1604 node_get_verbose_nickname(node, verbose_name_out);
1605 }
1606}
1607
1608/** Return true iff it seems that <b>node</b> allows circuits to exit
1609 * through it directlry from the client. */
1610int
1612{
1613 if (node && node->ri)
1614 return node->ri->allow_single_hop_exits;
1615 else
1616 return 0;
1617}
1618
1619/** Return true iff it seems that <b>node</b> has an exit policy that doesn't
1620 * actually permit anything to exit, or we don't know its exit policy */
1621int
1623{
1624 if (node->rejects_all)
1625 return 1;
1626
1627 if (node->ri)
1628 return node->ri->policy_is_reject_star;
1629 else if (node->md)
1630 return node->md->policy_is_reject_star;
1631 else
1632 return 1;
1633}
1634
1635/** Return true iff the exit policy for <b>node</b> is such that we can treat
1636 * rejecting an address of type <b>family</b> unexpectedly as a sign of that
1637 * node's failure. */
1638int
1640{
1641 if (family == AF_UNSPEC) {
1642 return 1; /* Rejecting an address but not telling us what address
1643 * is a bad sign. */
1644 } else if (family == AF_INET) {
1645 return node->ri != NULL;
1646 } else if (family == AF_INET6) {
1647 return 0;
1648 }
1650 return 1;
1651}
1652
1653/* Check if the "addr" and port_field fields from r are a valid non-listening
1654 * address/port. If so, set valid to true and add a newly allocated
1655 * tor_addr_port_t containing "addr" and port_field to sl.
1656 * "addr" is an IPv4 host-order address and port_field is a uint16_t.
1657 * r is typically a routerinfo_t or routerstatus_t.
1658 */
1659#define SL_ADD_NEW_AP(r, addr_field, port_field, sl, valid) \
1660 STMT_BEGIN \
1661 if (tor_addr_port_is_valid(&(r)->addr_field, (r)->port_field, 0)) { \
1662 valid = 1; \
1663 tor_addr_port_t *ap = tor_addr_port_new(&(r)->addr_field, \
1664 (r)->port_field); \
1665 smartlist_add((sl), ap); \
1666 } \
1667 STMT_END
1668
1669/** Return list of tor_addr_port_t with all OR ports (in the sense IP
1670 * addr + TCP port) for <b>node</b>. Caller must free all elements
1671 * using tor_free() and free the list using smartlist_free().
1672 *
1673 * XXX this is potentially a memory fragmentation hog -- if on
1674 * critical path consider the option of having the caller allocate the
1675 * memory
1676 */
1679{
1680 smartlist_t *sl = smartlist_new();
1681 int valid = 0;
1682
1683 /* Find a valid IPv4 address and port */
1684 if (node->ri != NULL) {
1685 SL_ADD_NEW_AP(node->ri, ipv4_addr, ipv4_orport, sl, valid);
1686 }
1687
1688 /* If we didn't find a valid address/port in the ri, try the rs */
1689 if (!valid && node->rs != NULL) {
1690 SL_ADD_NEW_AP(node->rs, ipv4_addr, ipv4_orport, sl, valid);
1691 }
1692
1693 /* Find a valid IPv6 address and port */
1694 valid = 0;
1695 if (node->ri != NULL) {
1696 SL_ADD_NEW_AP(node->ri, ipv6_addr, ipv6_orport, sl, valid);
1697 }
1698
1699 if (!valid && node->rs != NULL) {
1700 SL_ADD_NEW_AP(node->rs, ipv6_addr, ipv6_orport, sl, valid);
1701 }
1702
1703 if (!valid && node->md != NULL) {
1704 SL_ADD_NEW_AP(node->md, ipv6_addr, ipv6_orport, sl, valid);
1705 }
1706
1707 return sl;
1708}
1709
1710#undef SL_ADD_NEW_AP
1711
1712/** Wrapper around node_get_prim_orport for backward
1713 compatibility. */
1714void
1715node_get_addr(const node_t *node, tor_addr_t *addr_out)
1716{
1717 tor_addr_port_t ap;
1718 node_get_prim_orport(node, &ap);
1719 tor_addr_copy(addr_out, &ap.addr);
1720}
1721
1722/** Return the IPv4 address for <b>node</b>, or NULL if none found. */
1723static const tor_addr_t *
1725{
1726 /* Don't check the ORPort or DirPort, as this function isn't port-specific,
1727 * and the node might have a valid IPv4 address, yet have a zero
1728 * ORPort or DirPort.
1729 */
1730 if (node->ri && tor_addr_is_valid(&node->ri->ipv4_addr, 0)) {
1731 return &node->ri->ipv4_addr;
1732 } else if (node->rs && tor_addr_is_valid(&node->rs->ipv4_addr, 0)) {
1733 return &node->rs->ipv4_addr;
1734 }
1735 return NULL;
1736}
1737
1738/** Copy a string representation of an IP address for <b>node</b> into
1739 * the <b>len</b>-byte buffer at <b>buf</b>. */
1740void
1741node_get_address_string(const node_t *node, char *buf, size_t len)
1742{
1743 const tor_addr_t *ipv4_addr = node_get_prim_addr_ipv4(node);
1744
1745 if (ipv4_addr) {
1746 tor_addr_to_str(buf, ipv4_addr, len, 0);
1747 } else if (len > 0) {
1748 buf[0] = '\0';
1749 }
1750}
1751
1752/** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
1753 * one. */
1754long
1756{
1757 if (node->ri)
1758 return node->ri->uptime;
1759 else
1760 return -1;
1761}
1762
1763/** Return <b>node</b>'s platform string, or NULL if we don't know it. */
1764const char *
1766{
1767 /* If we wanted, we could record the version in the routerstatus_t, since
1768 * the consensus lists it. We don't, though, so this function just won't
1769 * work with microdescriptors. */
1770 if (node->ri)
1771 return node->ri->platform;
1772 else
1773 return NULL;
1774}
1775
1776/** Return true iff <b>node</b> is one representing this router. */
1777int
1778node_is_me(const node_t *node)
1779{
1780 return router_digest_is_me(node->identity);
1781}
1782
1783/* Does this node have a valid IPv6 address?
1784 * Prefer node_has_ipv6_orport() or node_has_ipv6_dirport() for
1785 * checking specific ports. */
1786int
1787node_has_ipv6_addr(const node_t *node)
1788{
1789 /* Don't check the ORPort or DirPort, as this function isn't port-specific,
1790 * and the node might have a valid IPv6 address, yet have a zero
1791 * ORPort or DirPort.
1792 */
1793 if (node->ri && tor_addr_is_valid(&node->ri->ipv6_addr, 0))
1794 return 1;
1795 if (node->rs && tor_addr_is_valid(&node->rs->ipv6_addr, 0))
1796 return 1;
1797 if (node->md && tor_addr_is_valid(&node->md->ipv6_addr, 0))
1798 return 1;
1799
1800 return 0;
1801}
1802
1803/* Does this node have a valid IPv6 ORPort? */
1804int
1805node_has_ipv6_orport(const node_t *node)
1806{
1807 tor_addr_port_t ipv6_orport;
1808 node_get_pref_ipv6_orport(node, &ipv6_orport);
1809 return tor_addr_port_is_valid_ap(&ipv6_orport, 0);
1810}
1811
1812/* Does this node have a valid IPv6 DirPort? */
1813int
1814node_has_ipv6_dirport(const node_t *node)
1815{
1816 tor_addr_port_t ipv6_dirport;
1817 node_get_pref_ipv6_dirport(node, &ipv6_dirport);
1818 return tor_addr_port_is_valid_ap(&ipv6_dirport, 0);
1819}
1820
1821/** Return 1 if we prefer the IPv6 address and OR TCP port of
1822 * <b>node</b>, else 0.
1823 *
1824 * We prefer the IPv6 address if the router has an IPv6 address,
1825 * and we can use IPv6 addresses, and:
1826 * i) the node_t says that it prefers IPv6
1827 * or
1828 * ii) the router has no IPv4 OR address.
1829 *
1830 * If you don't have a node, consider looking it up.
1831 * If there is no node, use reachable_addr_prefer_ipv6_orport().
1832 */
1833int
1835{
1836 const or_options_t *options = get_options();
1837 tor_addr_port_t ipv4_addr;
1838 node_assert_ok(node);
1839
1840 /* XX/teor - node->ipv6_preferred is set from
1841 * reachable_addr_prefer_ipv6_orport() each time the consensus is loaded.
1842 */
1843 node_get_prim_orport(node, &ipv4_addr);
1844 if (!reachable_addr_use_ipv6(options)) {
1845 return 0;
1846 } else if (node->ipv6_preferred ||
1847 !tor_addr_port_is_valid_ap(&ipv4_addr, 0)) {
1848 return node_has_ipv6_orport(node);
1849 }
1850 return 0;
1851}
1852
1853#define RETURN_IPV4_AP(r, port_field, ap_out) \
1854 STMT_BEGIN \
1855 if (r && tor_addr_port_is_valid(&(r)->ipv4_addr, (r)->port_field, 0)) { \
1856 tor_addr_copy(&(ap_out)->addr, &(r)->ipv4_addr); \
1857 (ap_out)->port = (r)->port_field; \
1858 } \
1859 STMT_END
1860
1861/** Copy the primary (IPv4) OR port (IP address and TCP port) for <b>node</b>
1862 * into *<b>ap_out</b>. */
1863void
1865{
1866 node_assert_ok(node);
1867 tor_assert(ap_out);
1868
1869 /* Clear the address, as a safety precaution if calling functions ignore the
1870 * return value */
1871 tor_addr_make_null(&ap_out->addr, AF_INET);
1872 ap_out->port = 0;
1873
1874 /* Check ri first, because rewrite_node_address_for_bridge() updates
1875 * node->ri with the configured bridge address. */
1876
1877 RETURN_IPV4_AP(node->ri, ipv4_orport, ap_out);
1878 RETURN_IPV4_AP(node->rs, ipv4_orport, ap_out);
1879 /* Microdescriptors only have an IPv6 address */
1880}
1881
1882/** Copy the preferred OR port (IP address and TCP port) for
1883 * <b>node</b> into *<b>ap_out</b>. */
1884void
1886{
1887 tor_assert(ap_out);
1888
1889 if (node_ipv6_or_preferred(node)) {
1890 node_get_pref_ipv6_orport(node, ap_out);
1891 } else {
1892 /* the primary ORPort is always on IPv4 */
1893 node_get_prim_orport(node, ap_out);
1894 }
1895}
1896
1897/** Copy the preferred IPv6 OR port (IP address and TCP port) for
1898 * <b>node</b> into *<b>ap_out</b>. */
1899void
1901{
1902 node_assert_ok(node);
1903 tor_assert(ap_out);
1904 memset(ap_out, 0, sizeof(*ap_out));
1905
1906 /* Check ri first, because rewrite_node_address_for_bridge() updates
1907 * node->ri with the configured bridge address.
1908 * Prefer rs over md for consistency with the fascist_firewall_* functions.
1909 * Check if the address or port are valid, and try another alternative
1910 * if they are not. */
1911
1912 if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
1913 node->ri->ipv6_orport, 0)) {
1914 tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
1915 ap_out->port = node->ri->ipv6_orport;
1916 } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
1917 node->rs->ipv6_orport, 0)) {
1918 tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
1919 ap_out->port = node->rs->ipv6_orport;
1920 } else if (node->md && tor_addr_port_is_valid(&node->md->ipv6_addr,
1921 node->md->ipv6_orport, 0)) {
1922 tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
1923 ap_out->port = node->md->ipv6_orport;
1924 } else {
1925 tor_addr_make_null(&ap_out->addr, AF_INET6);
1926 ap_out->port = 0;
1927 }
1928}
1929
1930/** Return 1 if we prefer the IPv6 address and Dir TCP port of
1931 * <b>node</b>, else 0.
1932 *
1933 * We prefer the IPv6 address if the router has an IPv6 address,
1934 * and we can use IPv6 addresses, and:
1935 * i) the router has no IPv4 Dir address.
1936 * or
1937 * ii) our preference is for IPv6 Dir addresses.
1938 *
1939 * If there is no node, use reachable_addr_prefer_ipv6_dirport().
1940 */
1941int
1943{
1944 const or_options_t *options = get_options();
1945 tor_addr_port_t ipv4_addr;
1946 node_assert_ok(node);
1947
1948 /* node->ipv6_preferred is set from reachable_addr_prefer_ipv6_orport(),
1949 * so we can't use it to determine DirPort IPv6 preference.
1950 * This means that bridge clients will use IPv4 DirPorts by default.
1951 */
1952 node_get_prim_dirport(node, &ipv4_addr);
1953 if (!reachable_addr_use_ipv6(options)) {
1954 return 0;
1955 } else if (!tor_addr_port_is_valid_ap(&ipv4_addr, 0)
1957 return node_has_ipv6_dirport(node);
1958 }
1959 return 0;
1960}
1961
1962/** Copy the primary (IPv4) Dir port (IP address and TCP port) for <b>node</b>
1963 * into *<b>ap_out</b>. */
1964void
1966{
1967 node_assert_ok(node);
1968 tor_assert(ap_out);
1969
1970 /* Clear the address, as a safety precaution if calling functions ignore the
1971 * return value */
1972 tor_addr_make_null(&ap_out->addr, AF_INET);
1973 ap_out->port = 0;
1974
1975 /* Check ri first, because rewrite_node_address_for_bridge() updates
1976 * node->ri with the configured bridge address. */
1977
1978 RETURN_IPV4_AP(node->ri, ipv4_dirport, ap_out);
1979 RETURN_IPV4_AP(node->rs, ipv4_dirport, ap_out);
1980 /* Microdescriptors only have an IPv6 address */
1981}
1982
1983#undef RETURN_IPV4_AP
1984
1985/** Copy the preferred Dir port (IP address and TCP port) for
1986 * <b>node</b> into *<b>ap_out</b>. */
1987void
1989{
1990 tor_assert(ap_out);
1991
1992 if (node_ipv6_dir_preferred(node)) {
1993 node_get_pref_ipv6_dirport(node, ap_out);
1994 } else {
1995 /* the primary DirPort is always on IPv4 */
1996 node_get_prim_dirport(node, ap_out);
1997 }
1998}
1999
2000/** Copy the preferred IPv6 Dir port (IP address and TCP port) for
2001 * <b>node</b> into *<b>ap_out</b>. */
2002void
2004{
2005 node_assert_ok(node);
2006 tor_assert(ap_out);
2007
2008 /* Check ri first, because rewrite_node_address_for_bridge() updates
2009 * node->ri with the configured bridge address.
2010 * Prefer rs over md for consistency with the fascist_firewall_* functions.
2011 * Check if the address or port are valid, and try another alternative
2012 * if they are not. */
2013
2014 /* Assume IPv4 and IPv6 dirports are the same */
2015 if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
2016 node->ri->ipv4_dirport, 0)) {
2017 tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
2018 ap_out->port = node->ri->ipv4_dirport;
2019 } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
2020 node->rs->ipv4_dirport, 0)) {
2021 tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
2022 ap_out->port = node->rs->ipv4_dirport;
2023 } else {
2024 tor_addr_make_null(&ap_out->addr, AF_INET6);
2025 ap_out->port = 0;
2026 }
2027}
2028
2029/** Return true iff <b>md</b> has a curve25519 onion key.
2030 * Use node_has_curve25519_onion_key() instead of calling this directly. */
2031static int
2033{
2034 if (!md) {
2035 return 0;
2036 }
2037
2038 if (!md->onion_curve25519_pkey) {
2039 return 0;
2040 }
2041
2042 if (fast_mem_is_zero((const char*)md->onion_curve25519_pkey->public_key,
2044 return 0;
2045 }
2046
2047 return 1;
2048}
2049
2050/** Return true iff <b>node</b> has a curve25519 onion key. */
2051int
2053{
2054 return node_get_curve25519_onion_key(node) != NULL;
2055}
2056
2057/** Return the curve25519 key of <b>node</b>, or NULL if none. */
2060{
2061 if (!node)
2062 return NULL;
2063 if (routerinfo_has_curve25519_onion_key(node->ri))
2064 return node->ri->onion_curve25519_pkey;
2065 else if (microdesc_has_curve25519_onion_key(node->md))
2066 return node->md->onion_curve25519_pkey;
2067 else
2068 return NULL;
2069}
2070
2071/** Refresh the country code of <b>ri</b>. This function MUST be called on
2072 * each router when the GeoIP database is reloaded, and on all new routers. */
2073void
2075{
2076 const tor_addr_t *ipv4_addr = NULL;
2077
2078 /* XXXXipv6 */
2079 if (node->rs)
2080 ipv4_addr = &node->rs->ipv4_addr;
2081 else if (node->ri)
2082 ipv4_addr = &node->ri->ipv4_addr;
2083
2084 /* IPv4 is mandatory for a relay so this should not happen unless we are
2085 * attempting to set the country code on a node without a descriptor. */
2086 if (BUG(!ipv4_addr)) {
2087 node->country = -1;
2088 return;
2089 }
2090 node->country = geoip_get_country_by_addr(ipv4_addr);
2091}
2092
2093/** Set the country code of all routers in the routerlist. */
2094void
2096{
2097 const smartlist_t *nodes = nodelist_get_list();
2098 SMARTLIST_FOREACH(nodes, node_t *, node,
2099 node_set_country(node));
2100}
2101
2102/** Return true iff router1 and router2 have similar enough network addresses
2103 * that we should treat them as being in the same family */
2104int
2106 const tor_addr_t *a2)
2107{
2108 if (tor_addr_is_null(a1) || tor_addr_is_null(a2))
2109 return 0;
2110
2111 switch (tor_addr_family(a1)) {
2112 case AF_INET:
2113 return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
2114 case AF_INET6:
2115 return 0 == tor_addr_compare_masked(a1, a2, 32, CMP_SEMANTIC);
2116 default:
2117 /* If not IPv4 or IPv6, return 0. */
2118 return 0;
2119 }
2120}
2121
2122/** Return true if <b>node</b>'s nickname matches <b>nickname</b>
2123 * (case-insensitive), or if <b>node's</b> identity key digest
2124 * matches a hexadecimal value stored in <b>nickname</b>. Return
2125 * false otherwise. */
2126STATIC int
2127node_nickname_matches(const node_t *node, const char *nickname)
2128{
2129 const char *n = node_get_nickname(node);
2130 if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
2131 return 1;
2132 return hex_digest_nickname_matches(nickname,
2133 node->identity,
2134 n);
2135}
2136
2137/** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
2138STATIC int
2140{
2141 if (!lst) return 0;
2142 SMARTLIST_FOREACH(lst, const char *, name, {
2143 if (node_nickname_matches(node, name))
2144 return 1;
2145 });
2146 return 0;
2147}
2148
2149/** Return true iff n1's declared family contains n2. */
2150STATIC int
2152{
2153 if (n1->ri && n1->ri->declared_family) {
2154 return node_in_nickname_smartlist(n1->ri->declared_family, n2);
2155 } else if (n1->md) {
2156 return nodefamily_contains_node(n1->md->family, n2);
2157 } else {
2158 return 0;
2159 }
2160}
2161
2162/**
2163 * Return true iff <b>node</b> has declared a nonempty family.
2164 **/
2165STATIC bool
2167{
2168 if (node->ri && node->ri->declared_family &&
2169 smartlist_len(node->ri->declared_family)) {
2170 return true;
2171 }
2172
2173 if (node->md && node->md->family) {
2174 return true;
2175 }
2176
2177 return false;
2178}
2179
2180/**
2181 * Return the listed family IDs of `a`, if it has any.
2182 */
2183static const smartlist_t *
2185{
2186 if (node->ri && node->ri->family_ids) {
2187 return node->ri->family_ids;
2188 } else if (node->md && node->md->family_ids) {
2189 return node->md->family_ids;
2190 } else {
2191 return NULL;
2192 }
2193}
2194
2195/**
2196 * Return true iff `a` and `b` have any family ID in common.
2197 **/
2198static bool
2200{
2201 const smartlist_t *ids_a = node_get_family_ids(a);
2202 const smartlist_t *ids_b = node_get_family_ids(b);
2203 if (ids_a == NULL || ids_b == NULL)
2204 return false;
2205 SMARTLIST_FOREACH(ids_a, const char *, id, {
2206 if (smartlist_contains_string(ids_b, id))
2207 return true;
2208 });
2209 return false;
2210}
2211
2212/**
2213 * Add to <b>out</b> every node_t that is listed by <b>node</b> as being in
2214 * its family. (Note that these nodes are not in node's family unless they
2215 * also agree that node is in their family.)
2216 **/
2217STATIC void
2219{
2220 if (node->ri && node->ri->declared_family &&
2221 smartlist_len(node->ri->declared_family)) {
2222 SMARTLIST_FOREACH_BEGIN(node->ri->declared_family, const char *, name) {
2223 const node_t *n2 = node_get_by_nickname(name, NNF_NO_WARN_UNNAMED);
2224 if (n2) {
2225 smartlist_add(out, (node_t *)n2);
2226 }
2227 } SMARTLIST_FOREACH_END(name);
2228 return;
2229 }
2230
2231 if (node->md && node->md->family) {
2233 }
2234}
2235
2236/** Return true iff r1 and r2 are in the same family, but not the same
2237 * router. */
2238int
2239nodes_in_same_family(const node_t *node1, const node_t *node2)
2240{
2241 const or_options_t *options = get_options();
2242
2243 /* Are they in the same family because of their addresses? */
2244 if (options->EnforceDistinctSubnets) {
2245 tor_addr_t a1, a2;
2246 node_get_addr(node1, &a1);
2247 node_get_addr(node2, &a2);
2248
2249 tor_addr_port_t ap6_1, ap6_2;
2250 node_get_pref_ipv6_orport(node1, &ap6_1);
2251 node_get_pref_ipv6_orport(node2, &ap6_2);
2252
2253 if (router_addrs_in_same_network(&a1, &a2) ||
2254 router_addrs_in_same_network(&ap6_1.addr, &ap6_2.addr))
2255 return 1;
2256 }
2257
2258 /* Are they in the same family because they agree they are? */
2259 if (use_family_lists &&
2260 node_family_list_contains(node1, node2) &&
2261 node_family_list_contains(node2, node1)) {
2262 return 1;
2263 }
2264
2265 /* Are they in the same family because they have a common
2266 * verified family ID? */
2267 if (use_family_ids &&
2268 nodes_have_common_family_id(node1, node2)) {
2269 return 1;
2270 }
2271
2272 /* Are they in the same family because the user says they are? */
2273 if (options->NodeFamilySets) {
2274 SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
2275 if (routerset_contains_node(rs, node1) &&
2276 routerset_contains_node(rs, node2))
2277 return 1;
2278 });
2279 }
2280
2281 return 0;
2282}
2283
2284/**
2285 * Add all the family of <b>node</b>, including <b>node</b> itself, to
2286 * the smartlist <b>sl</b>.
2287 *
2288 * This is used to make sure we don't pick siblings in a single path, or
2289 * pick more than one relay from a family for our entry guard list.
2290 * Note that a node may be added to <b>sl</b> more than once if it is
2291 * part of <b>node</b>'s family for more than one reason.
2292 */
2293void
2295{
2296 const smartlist_t *all_nodes = nodelist_get_list();
2297 const or_options_t *options = get_options();
2298
2299 tor_assert(node);
2300
2301 /* Let's make sure that we have the node itself, if it's a real node. */
2302 {
2303 const node_t *real_node = node_get_by_id(node->identity);
2304 if (real_node)
2305 smartlist_add(sl, (node_t*)real_node);
2306 }
2307
2308 /* First, add any nodes with similar network addresses. */
2309 if (options->EnforceDistinctSubnets) {
2310 tor_addr_t node_addr;
2311 tor_addr_port_t node_ap6;
2312 node_get_addr(node, &node_addr);
2313 node_get_pref_ipv6_orport(node, &node_ap6);
2314
2315 SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
2316 tor_addr_t a;
2317 tor_addr_port_t ap6;
2318 node_get_addr(node2, &a);
2319 node_get_pref_ipv6_orport(node2, &ap6);
2320 if (router_addrs_in_same_network(&a, &node_addr) ||
2321 router_addrs_in_same_network(&ap6.addr, &node_ap6.addr))
2322 smartlist_add(sl, (void*)node2);
2323 } SMARTLIST_FOREACH_END(node2);
2324 }
2325
2326 /* Now, add all nodes in the declared family of this node, if they
2327 * also declare this node to be in their family. */
2328 if (use_family_lists &&
2330 smartlist_t *declared_family = smartlist_new();
2331 node_lookup_declared_family_list(declared_family, node);
2332
2333 /* Add every r such that router declares familyness with node, and node
2334 * declares familyhood with router. */
2335 SMARTLIST_FOREACH_BEGIN(declared_family, const node_t *, node2) {
2336 if (node_family_list_contains(node2, node)) {
2337 smartlist_add(sl, (void*)node2);
2338 }
2339 } SMARTLIST_FOREACH_END(node2);
2340 smartlist_free(declared_family);
2341 }
2342
2343 /* Now add all the nodes that share a verified family ID with this node. */
2344 if (use_family_ids &&
2345 node_get_family_ids(node)) {
2346 SMARTLIST_FOREACH(all_nodes, const node_t *, node2, {
2347 if (nodes_have_common_family_id(node, node2)) {
2348 smartlist_add(sl, (void *)node2);
2349 }
2350 });
2351 }
2352
2353 /* If the user declared any families locally, honor those too. */
2354 if (options->NodeFamilySets) {
2355 SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
2356 if (routerset_contains_node(rs, node)) {
2357 routerset_get_all_nodes(sl, rs, NULL, 0);
2358 }
2359 });
2360 }
2361}
2362
2363/** Find a router that's up, that has this IP address, and
2364 * that allows exit to this address:port, or return NULL if there
2365 * isn't a good one.
2366 * Don't exit enclave to excluded relays -- it wouldn't actually
2367 * hurt anything, but this way there are fewer confused users.
2368 */
2369const node_t *
2370router_find_exact_exit_enclave(const char *address, uint16_t port)
2371{/*XXXX MOVE*/
2372 struct in_addr in;
2373 tor_addr_t ipv4_addr;
2374 const or_options_t *options = get_options();
2375
2376 if (!tor_inet_aton(address, &in))
2377 return NULL; /* it's not an IP already */
2378 tor_addr_from_in(&ipv4_addr, &in);
2379
2380 SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
2381 if (tor_addr_eq(node_get_prim_addr_ipv4(node), &ipv4_addr) &&
2382 node->is_running &&
2383 compare_tor_addr_to_node_policy(&ipv4_addr, port, node) ==
2386 return node;
2387 });
2388 return NULL;
2389}
2390
2391/** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
2392 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
2393 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
2394 * bandwidth.
2395 * If <b>need_guard</b>, we require that the router is a possible entry guard.
2396 */
2397int
2398node_is_unreliable(const node_t *node, int need_uptime,
2399 int need_capacity, int need_guard)
2400{
2401 if (need_uptime && !node->is_stable)
2402 return 1;
2403 if (need_capacity && !node->is_fast)
2404 return 1;
2405 if (need_guard && !node->is_possible_guard)
2406 return 1;
2407 return 0;
2408}
2409
2410/** Return 1 if all running sufficiently-stable routers we can use will reject
2411 * addr:port. Return 0 if any might accept it. */
2412int
2414 int need_uptime)
2415{
2417
2419 if (node->is_running &&
2420 !node_is_unreliable(node, need_uptime, 0, 0)) {
2421
2422 r = compare_tor_addr_to_node_policy(addr, port, node);
2423
2425 return 0; /* this one could be ok. good enough. */
2426 }
2427 } SMARTLIST_FOREACH_END(node);
2428 return 1; /* all will reject. */
2429}
2430
2431/** Mark the router with ID <b>digest</b> as running or non-running
2432 * in our routerlist. */
2433void
2434router_set_status(const char *digest, int up)
2435{
2436 node_t *node;
2437 tor_assert(digest);
2438
2439 SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
2440 dir_server_t *, d,
2441 if (tor_memeq(d->digest, digest, DIGEST_LEN))
2442 d->is_running = up);
2443
2444 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
2445 dir_server_t *, d,
2446 if (tor_memeq(d->digest, digest, DIGEST_LEN))
2447 d->is_running = up);
2448
2449 node = node_get_mutable_by_id(digest);
2450 if (node) {
2451#if 0
2452 log_debug(LD_DIR,"Marking router %s as %s.",
2453 node_describe(node), up ? "up" : "down");
2454#endif
2455 if (!up && node_is_me(node) && !net_is_disabled())
2456 log_warn(LD_NET, "We just marked ourself as down. Are your external "
2457 "addresses reachable?");
2458
2459 if (bool_neq(node->is_running, up))
2461
2462 node->is_running = up;
2463 }
2464}
2465
2466/** True iff, the last time we checked whether we had enough directory info
2467 * to build circuits, the answer was "yes". If there are no exits in the
2468 * consensus, we act as if we have 100% of the exit directory info. */
2469static int have_min_dir_info = 0;
2470
2471/** Does the consensus contain nodes that can exit? */
2472static consensus_path_type_t have_consensus_path = CONSENSUS_PATH_UNKNOWN;
2473
2474/** True iff enough has changed since the last time we checked whether we had
2475 * enough directory info to build circuits that our old answer can no longer
2476 * be trusted. */
2478/** String describing what we're missing before we have enough directory
2479 * info. */
2480static char dir_info_status[512] = "";
2481
2482/** Return true iff we have enough consensus information to
2483 * start building circuits. Right now, this means "a consensus that's
2484 * less than a day old, and at least 60% of router descriptors (configurable),
2485 * weighted by bandwidth. Treat the exit fraction as 100% if there are
2486 * no exits in the consensus."
2487 * To obtain the final weighted bandwidth, we multiply the
2488 * weighted bandwidth fraction for each position (guard, middle, exit). */
2489MOCK_IMPL(int,
2491{
2492 static int logged_delay=0;
2493 const char *delay_fetches_msg = NULL;
2494 if (should_delay_dir_fetches(get_options(), &delay_fetches_msg)) {
2495 if (!logged_delay)
2496 log_notice(LD_DIR, "Delaying directory fetches: %s", delay_fetches_msg);
2497 logged_delay=1;
2498 strlcpy(dir_info_status, delay_fetches_msg, sizeof(dir_info_status));
2499 return 0;
2500 }
2501 logged_delay = 0; /* reset it if we get this far */
2502
2503 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
2505 }
2506
2507 return have_min_dir_info;
2508}
2509
2510/** Set to CONSENSUS_PATH_EXIT if there is at least one exit node
2511 * in the consensus. We update this flag in compute_frac_paths_available if
2512 * there is at least one relay that has an Exit flag in the consensus.
2513 * Used to avoid building exit circuits when they will almost certainly fail.
2514 * Set to CONSENSUS_PATH_INTERNAL if there are no exits in the consensus.
2515 * (This situation typically occurs during bootstrap of a test network.)
2516 * Set to CONSENSUS_PATH_UNKNOWN if we have never checked, or have
2517 * reason to believe our last known value was invalid or has expired.
2518 * If we're in a network with TestingDirAuthVoteExit set,
2519 * this can cause router_have_consensus_path() to be set to
2520 * CONSENSUS_PATH_EXIT, even if there are no nodes with accept exit policies.
2521 */
2524{
2525 return have_consensus_path;
2526}
2527
2528/** Called when our internal view of the directory has changed. This can be
2529 * when the authorities change, networkstatuses change, the list of routerdescs
2530 * changes, or number of running routers changes.
2531 */
2532void
2534{
2538}
2539
2540/** Return a string describing what we're missing before we have enough
2541 * directory info. */
2542const char *
2544{
2545 return dir_info_status;
2546}
2547
2548/** Iterate over the servers listed in <b>consensus</b>, and count how many of
2549 * them seem like ones we'd use (store this in *<b>num_usable</b>), and how
2550 * many of <em>those</em> we have descriptors for (store this in
2551 * *<b>num_present</b>).
2552 *
2553 * If <b>in_set</b> is non-NULL, only consider those routers in <b>in_set</b>.
2554 * If <b>exit_only</b> & USABLE_DESCRIPTOR_EXIT_POLICY, only consider nodes
2555 * present if they have an exit policy that accepts at least one port.
2556 * If <b>exit_only</b> & USABLE_DESCRIPTOR_EXIT_FLAG, only consider nodes
2557 * usable if they have the exit flag in the consensus.
2558 *
2559 * If *<b>descs_out</b> is present, add a node_t for each usable descriptor
2560 * to it.
2561 */
2562static void
2563count_usable_descriptors(int *num_present, int *num_usable,
2564 smartlist_t *descs_out,
2565 const networkstatus_t *consensus,
2566 time_t now,
2567 routerset_t *in_set,
2568 usable_descriptor_t exit_only)
2569{
2570 const int md = (consensus->flavor == FLAV_MICRODESC);
2571 *num_present = 0, *num_usable = 0;
2572
2574 {
2575 const node_t *node = node_get_by_id(rs->identity_digest);
2576 if (!node)
2577 continue; /* This would be a bug: every entry in the consensus is
2578 * supposed to have a node. */
2579 if ((exit_only & USABLE_DESCRIPTOR_EXIT_FLAG) && ! rs->is_exit)
2580 continue;
2581 if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
2582 continue;
2583 if (client_would_use_router(rs, now)) {
2584 const char * const digest = rs->descriptor_digest;
2585 int present;
2586 ++*num_usable; /* the consensus says we want it. */
2587 if (md)
2588 present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
2589 else
2590 present = NULL != router_get_by_descriptor_digest(digest);
2591 if (present) {
2592 /* Do the policy check last, because it requires a descriptor,
2593 * and is potentially expensive */
2594 if ((exit_only & USABLE_DESCRIPTOR_EXIT_POLICY) &&
2596 continue;
2597 }
2598 /* we have the descriptor listed in the consensus, and it
2599 * satisfies our exit constraints (if any) */
2600 ++*num_present;
2601 }
2602 if (descs_out)
2603 smartlist_add(descs_out, (node_t*)node);
2604 }
2605 }
2606 SMARTLIST_FOREACH_END(rs);
2607
2608 log_debug(LD_DIR, "%d usable, %d present (%s%s%s%s%s).",
2609 *num_usable, *num_present,
2610 md ? "microdesc" : "desc",
2611 (exit_only & USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG) ?
2612 " exit" : "s",
2613 (exit_only & USABLE_DESCRIPTOR_EXIT_POLICY) ?
2614 " policies" : "" ,
2615 (exit_only == USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG) ?
2616 " and" : "" ,
2617 (exit_only & USABLE_DESCRIPTOR_EXIT_FLAG) ?
2618 " flags" : "" );
2619}
2620
2621/** Return an estimate of which fraction of usable paths through the Tor
2622 * network we have available for use. Count how many routers seem like ones
2623 * we'd use (store this in *<b>num_usable_out</b>), and how many of
2624 * <em>those</em> we have descriptors for (store this in
2625 * *<b>num_present_out</b>.)
2626 *
2627 * If **<b>status_out</b> is present, allocate a new string and print the
2628 * available percentages of guard, middle, and exit nodes to it, noting
2629 * whether there are exits in the consensus.
2630 * If there are no exits in the consensus, we treat the exit fraction as 100%,
2631 * but set router_have_consensus_path() so that we can only build internal
2632 * paths. */
2633static double
2635 const or_options_t *options, time_t now,
2636 int *num_present_out, int *num_usable_out,
2637 char **status_out)
2638{
2639 smartlist_t *guards = smartlist_new();
2640 smartlist_t *mid = smartlist_new();
2641 smartlist_t *exits = smartlist_new();
2642 double f_guard, f_mid, f_exit;
2643 double f_path = 0.0;
2644 /* Used to determine whether there are any exits in the consensus */
2645 int np = 0;
2646 /* Used to determine whether there are any exits with descriptors */
2647 int nu = 0;
2648 const int authdir = authdir_mode_v3(options);
2649
2650 count_usable_descriptors(num_present_out, num_usable_out,
2651 mid, consensus, now, options->MiddleNodes,
2652 USABLE_DESCRIPTOR_ALL);
2653 log_debug(LD_NET,
2654 "%s: %d present, %d usable",
2655 "mid",
2656 np,
2657 nu);
2658
2659 if (options->EntryNodes) {
2660 count_usable_descriptors(&np, &nu, guards, consensus, now,
2661 options->EntryNodes, USABLE_DESCRIPTOR_ALL);
2662 log_debug(LD_NET,
2663 "%s: %d present, %d usable",
2664 "guard",
2665 np,
2666 nu);
2667 } else {
2668 SMARTLIST_FOREACH(mid, const node_t *, node, {
2669 if (authdir) {
2670 if (node->rs && node->rs->is_possible_guard)
2671 smartlist_add(guards, (node_t*)node);
2672 } else {
2673 if (node->is_possible_guard)
2674 smartlist_add(guards, (node_t*)node);
2675 }
2676 });
2677 log_debug(LD_NET,
2678 "%s: %d possible",
2679 "guard",
2680 smartlist_len(guards));
2681 }
2682
2683 /* All nodes with exit policy and flag */
2684 count_usable_descriptors(&np, &nu, exits, consensus, now,
2685 NULL, USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG);
2686 log_debug(LD_NET,
2687 "%s: %d present, %d usable",
2688 "exits",
2689 np,
2690 nu);
2691
2692 /* We need at least 1 exit (flag and policy) in the consensus to consider
2693 * building exit paths */
2694 /* Update our understanding of whether the consensus has exits */
2695 consensus_path_type_t old_have_consensus_path = have_consensus_path;
2696 have_consensus_path = ((np > 0) ?
2697 CONSENSUS_PATH_EXIT :
2698 CONSENSUS_PATH_INTERNAL);
2699
2700 if (old_have_consensus_path != have_consensus_path) {
2701 if (have_consensus_path == CONSENSUS_PATH_INTERNAL) {
2702 log_notice(LD_NET,
2703 "The current consensus has no exit nodes. "
2704 "Tor can only build internal paths, "
2705 "such as paths to onion services.");
2706
2707 /* However, exit nodes can reachability self-test using this consensus,
2708 * join the network, and appear in a later consensus. This will allow
2709 * the network to build exit paths, such as paths for world wide web
2710 * browsing (as distinct from hidden service web browsing). */
2711 } else if (old_have_consensus_path == CONSENSUS_PATH_INTERNAL) {
2712 log_notice(LD_NET,
2713 "The current consensus contains exit nodes. "
2714 "Tor can build exit and internal paths.");
2715 }
2716 }
2717
2718 f_guard = frac_nodes_with_descriptors(guards, WEIGHT_FOR_GUARD, 1);
2719 f_mid = frac_nodes_with_descriptors(mid, WEIGHT_FOR_MID, 0);
2720 f_exit = frac_nodes_with_descriptors(exits, WEIGHT_FOR_EXIT, 0);
2721
2722 /* If we are using bridges and have at least one bridge with a full
2723 * descriptor, assume f_guard is 1.0. */
2724 if (options->UseBridges && num_bridges_usable(0) > 0)
2725 f_guard = 1.0;
2726
2727 log_debug(LD_NET,
2728 "f_guard: %.2f, f_mid: %.2f, f_exit: %.2f",
2729 f_guard,
2730 f_mid,
2731 f_exit);
2732
2733 smartlist_free(guards);
2734 smartlist_free(mid);
2735 smartlist_free(exits);
2736
2737 if (options->ExitNodes) {
2738 double f_myexit, f_myexit_unflagged;
2739 smartlist_t *myexits= smartlist_new();
2740 smartlist_t *myexits_unflagged = smartlist_new();
2741
2742 /* All nodes with exit policy and flag in ExitNodes option */
2743 count_usable_descriptors(&np, &nu, myexits, consensus, now,
2744 options->ExitNodes,
2745 USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG);
2746 log_debug(LD_NET,
2747 "%s: %d present, %d usable",
2748 "myexits",
2749 np,
2750 nu);
2751
2752 /* Now compute the nodes in the ExitNodes option where we know their exit
2753 * policy permits something. */
2754 count_usable_descriptors(&np, &nu, myexits_unflagged,
2755 consensus, now,
2756 options->ExitNodes,
2757 USABLE_DESCRIPTOR_EXIT_POLICY);
2758 log_debug(LD_NET,
2759 "%s: %d present, %d usable",
2760 "myexits_unflagged (initial)",
2761 np,
2762 nu);
2763
2764 f_myexit= frac_nodes_with_descriptors(myexits,WEIGHT_FOR_EXIT, 0);
2765 f_myexit_unflagged=
2766 frac_nodes_with_descriptors(myexits_unflagged,
2767 WEIGHT_FOR_EXIT, 0);
2768
2769 log_debug(LD_NET,
2770 "f_exit: %.2f, f_myexit: %.2f, f_myexit_unflagged: %.2f",
2771 f_exit,
2772 f_myexit,
2773 f_myexit_unflagged);
2774
2775 /* If our ExitNodes list has eliminated every possible Exit node, and there
2776 * were some possible Exit nodes, then instead consider nodes that permit
2777 * exiting to some ports. */
2778 if (smartlist_len(myexits) == 0 &&
2779 smartlist_len(myexits_unflagged)) {
2780 f_myexit = f_myexit_unflagged;
2781 }
2782
2783 smartlist_free(myexits);
2784 smartlist_free(myexits_unflagged);
2785
2786 /* This is a tricky point here: we don't want to make it easy for a
2787 * directory to trickle exits to us until it learns which exits we have
2788 * configured, so require that we have a threshold both of total exits
2789 * and usable exits. */
2790 if (f_myexit < f_exit)
2791 f_exit = f_myexit;
2792 }
2793
2794 /* If the consensus has no exits that pass flag, descriptor, and policy
2795 * checks, we can only build onion service paths, which are G - M - M. */
2796 if (router_have_consensus_path() != CONSENSUS_PATH_EXIT) {
2797 /* If the exit bandwidth weight fraction is not zero, we need to wait for
2798 * descriptors for those exits. (The bandwidth weight fraction does not
2799 * check for descriptors.)
2800 * If the exit bandwidth fraction is zero, there are no exits in the
2801 * consensus at all. So it is safe to replace f_exit with f_mid.
2802 *
2803 * f_exit is non-negative, but some compilers complain about float and ==
2804 */
2805 if (f_exit <= 0.0) {
2806 f_exit = f_mid;
2807 }
2808 }
2809
2810 f_path = f_guard * f_mid * f_exit;
2811
2812 if (status_out)
2813 tor_asprintf(status_out,
2814 "%d%% of guards bw, "
2815 "%d%% of midpoint bw, and "
2816 "%d%% of %s = "
2817 "%d%% of path bw",
2818 (int)(f_guard*100),
2819 (int)(f_mid*100),
2820 (int)(f_exit*100),
2821 (router_have_consensus_path() == CONSENSUS_PATH_EXIT ?
2822 "exit bw" :
2823 "end bw (no exits in consensus, using mid)"),
2824 (int)(f_path*100));
2825
2826 return f_path;
2827}
2828
2829/** We just fetched a new set of descriptors. Compute how far through
2830 * the "loading descriptors" bootstrapping phase we are, so we can inform
2831 * the controller of our progress. */
2832int
2834{
2835 int num_present = 0, num_usable=0;
2836 time_t now = time(NULL);
2837 const or_options_t *options = get_options();
2838 const networkstatus_t *consensus =
2840 double paths, fraction;
2841
2842 if (!consensus)
2843 return 0; /* can't count descriptors if we have no list of them */
2844
2845 paths = compute_frac_paths_available(consensus, options, now,
2846 &num_present, &num_usable,
2847 NULL);
2848
2849 fraction = paths / get_frac_paths_needed_for_circs(options,consensus);
2850 if (fraction > 1.0)
2851 return 0; /* it's not the number of descriptors holding us back */
2852 return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
2853 (fraction*(BOOTSTRAP_STATUS_ENOUGH_DIRINFO-1 -
2854 BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
2855}
2856
2857/** Return the fraction of paths needed before we're willing to build
2858 * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
2859static double
2861 const networkstatus_t *ns)
2862{
2863#define DFLT_PCT_USABLE_NEEDED 60
2864 if (options->PathsNeededToBuildCircuits >= 0.0) {
2865 return options->PathsNeededToBuildCircuits;
2866 } else {
2867 return networkstatus_get_param(ns, "min_paths_for_circs_pct",
2868 DFLT_PCT_USABLE_NEEDED,
2869 25, 95)/100.0;
2870 }
2871}
2872
2873/** Change the value of have_min_dir_info, setting it true iff we have enough
2874 * network and router information to build circuits. Clear the value of
2875 * need_to_update_have_min_dir_info. */
2876static void
2878{
2879 time_t now = time(NULL);
2880 int res;
2881 int num_present=0, num_usable=0;
2882 const or_options_t *options = get_options();
2883 const networkstatus_t *consensus =
2885 int using_md;
2886 static int be_loud_when_things_work_again = 0;
2887
2888 if (!consensus) {
2890 strlcpy(dir_info_status, "We have no usable consensus.",
2891 sizeof(dir_info_status));
2892 else
2893 strlcpy(dir_info_status, "We have no recent usable consensus.",
2894 sizeof(dir_info_status));
2895 res = 0;
2896 goto done;
2897 }
2898
2899 using_md = consensus->flavor == FLAV_MICRODESC;
2900
2901 /* Check fraction of available paths */
2902 {
2903 char *status = NULL;
2904 double paths = compute_frac_paths_available(consensus, options, now,
2905 &num_present, &num_usable,
2906 &status);
2907
2908 if (paths < get_frac_paths_needed_for_circs(options,consensus)) {
2910 "We need more %sdescriptors: we have %d/%d, and "
2911 "can only build %d%% of likely paths. (We have %s.)",
2912 using_md?"micro":"", num_present, num_usable,
2913 (int)(paths*100), status);
2914 tor_free(status);
2915 res = 0;
2916 control_event_boot_dir(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
2917 goto done;
2918 }
2919
2920 tor_free(status);
2921 res = 1;
2922 }
2923
2924 { /* Check entry guard dirinfo status */
2925 char *guard_error = entry_guards_get_err_str_if_dir_info_missing(using_md,
2926 num_present,
2927 num_usable);
2928 if (guard_error) {
2929 strlcpy(dir_info_status, guard_error, sizeof(dir_info_status));
2930 tor_free(guard_error);
2931 res = 0;
2932 goto done;
2933 }
2934 }
2935
2936 done:
2937
2938 /* If paths have just become available in this update. */
2939 if (res && !have_min_dir_info) {
2940 control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
2941 control_event_boot_dir(BOOTSTRAP_STATUS_ENOUGH_DIRINFO, 0);
2942 tor_log(be_loud_when_things_work_again ? LOG_NOTICE : LOG_INFO, LD_DIR,
2943 "We now have enough directory information to build circuits.");
2944 be_loud_when_things_work_again = 0;
2945 }
2946
2947 /* If paths have just become unavailable in this update. */
2948 if (!res && have_min_dir_info) {
2951 "Our directory information is no longer up-to-date "
2952 "enough to build circuits: %s", dir_info_status);
2953 if (!quiet) {
2954 /* remember to do a notice-level log when things come back */
2955 be_loud_when_things_work_again = 1;
2956 }
2957
2958 /* a) make us log when we next complete a circuit, so we know when Tor
2959 * is back up and usable, and b) disable some activities that Tor
2960 * should only do while circuits are working, like reachability tests
2961 * and fetching bridge descriptors only over circuits. */
2963 have_consensus_path = CONSENSUS_PATH_UNKNOWN;
2964 control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
2965 }
2966 have_min_dir_info = res;
2968}
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:933
void tor_addr_make_null(tor_addr_t *a, sa_family_t family)
Definition: address.c:235
int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2, maskbits_t mbits, tor_addr_comparison_t how)
Definition: address.c:1005
int tor_addr_is_v4(const tor_addr_t *addr)
Definition: address.c:750
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:780
int tor_addr_is_v6(const tor_addr_t *addr)
Definition: address.c:770
const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len, int decorate)
Definition: address.c:328
Headers for address.h.
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:187
static uint32_t tor_addr_to_ipv4h(const tor_addr_t *a)
Definition: address.h:160
#define tor_addr_to_in6_addr8(x)
Definition: address.h:135
#define tor_addr_from_in(dest, in)
Definition: address.h:331
#define tor_addr_eq(a, b)
Definition: address.h:280
void address_set_add(address_set_t *set, const struct tor_addr_t *addr)
Definition: address_set.c:47
int address_set_probably_contains(const address_set_t *set, const struct tor_addr_t *addr)
Definition: address_set.c:66
address_set_t * address_set_new(int max_addresses_guess)
Definition: address_set.c:33
Types to handle sets of addresses.
time_t approx_time(void)
Definition: approx_time.c:32
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
Header file for directory authority mode.
Header for backtrace.c.
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:60
Header for binascii.c.
int node_is_a_configured_bridge(const node_t *node)
Definition: bridges.c:391
Header file for circuitbuild.c.
int quiet
Definition: config.c:2479
const char * name
Definition: config.c:2471
const or_options_t * get_options(void)
Definition: config.c:947
Header file for config.c.
void control_event_boot_dir(bootstrap_status_t status, int progress)
int control_event_client_status(int severity, const char *format,...)
Header file for control_events.c.
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
const char * node_describe(const node_t *node)
Definition: describe.c:160
Header file for describe.c.
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
#define fast_memeq(a, b, c)
Definition: di_ops.h:35
#define tor_memneq(a, b, sz)
Definition: di_ops.h:21
#define DIGEST_LEN
Definition: digest_sizes.h:20
#define DIGEST256_LEN
Definition: digest_sizes.h:23
Trusted/fallback directory server structure.
int dirclient_too_idle_to_fetch_descriptors(const or_options_t *options, time_t now)
Header for feature/dirclient/dirclient_modes.c.
int get_n_authorities(dirinfo_type_t type)
Definition: dirlist.c:103
void dirlist_add_trusted_dir_addresses(void)
Definition: dirlist.c:87
Header file for dirlist.c.
int num_bridges_usable(int use_maybe_reachable)
Definition: entrynodes.c:3535
char * entry_guards_get_err_str_if_dir_info_missing(int using_mds, int num_present, int num_usable)
Definition: entrynodes.c:4037
Header file for circuitbuild.c.
int geoip_get_country_by_addr(const tor_addr_t *addr)
Definition: geoip.c:424
Header file for geoip.c.
HT_PROTOTYPE(hs_circuitmap_ht, circuit_t, hs_circuitmap_node, hs_circuit_hash_token, hs_circuits_have_same_token)
void hs_client_dir_info_changed(void)
Definition: hs_client.c:2775
Header file containing client data for the HS subsystem.
uint64_t hs_get_time_period_num(time_t now)
Definition: hs_common.c:269
uint8_t * hs_get_current_srv(uint64_t time_period_num, const networkstatus_t *ns)
Definition: hs_common.c:1117
uint64_t hs_get_next_time_period_num(time_t now)
Definition: hs_common.c:306
uint8_t * hs_get_previous_srv(uint64_t time_period_num, const networkstatus_t *ns)
Definition: hs_common.c:1135
uint64_t hs_get_previous_time_period_num(time_t now)
Definition: hs_common.c:315
void hs_build_hsdir_index(const ed25519_public_key_t *identity_pk, const uint8_t *srv_value, uint64_t period_num, uint8_t *hsdir_index_out)
Definition: hs_common.c:1078
int hs_in_period_between_tp_and_srv(const networkstatus_t *consensus, time_t now)
Definition: hs_common.c:987
Header file containing common data for the whole HS subsystem.
void hs_service_dir_info_changed(void)
Definition: hs_service.c:4370
typedef HT_HEAD(hs_service_ht, hs_service_t) hs_service_ht
int tor_inet_aton(const char *str, struct in_addr *addr)
Definition: inaddr.c:40
uint16_t sa_family_t
Definition: inaddr_st.h:77
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:591
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:288
#define LD_PROTOCOL
Definition: log.h:72
#define LD_BUG
Definition: log.h:86
#define LD_NET
Definition: log.h:66
#define LD_GENERAL
Definition: log.h:62
#define LD_DIR
Definition: log.h:88
#define LOG_NOTICE
Definition: log.h:50
#define LD_CONFIG
Definition: log.h:68
#define LOG_INFO
Definition: log.h:45
#define bool_neq(a, b)
Definition: logic.h:18
void note_that_we_maybe_cant_complete_circuits(void)
Definition: mainloop.c:234
Header file for mainloop.c.
void * tor_reallocarray_(void *ptr, size_t sz1, size_t sz2)
Definition: malloc.c:146
void tor_free_(void *mem)
Definition: malloc.c:227
#define tor_free(p)
Definition: malloc.h:56
int usable_consensus_flavor(void)
Definition: microdesc.c:1088
microdesc_t * microdesc_cache_lookup_by_digest256(microdesc_cache_t *cache, const char *d)
Definition: microdesc.c:948
microdesc_cache_t * get_microdesc_cache(void)
Definition: microdesc.c:251
int we_use_microdescriptors_for_circuits(const or_options_t *options)
Definition: microdesc.c:1057
Header file for microdesc.c.
Microdescriptor structure.
int net_is_disabled(void)
Definition: netstatus.c:25
Header for netstatus.c.
networkstatus_t * networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
int networkstatus_consensus_reasonably_live(const networkstatus_t *consensus, time_t now)
networkstatus_t * networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
int client_would_use_router(const routerstatus_t *rs, time_t now)
networkstatus_t * networkstatus_get_latest_consensus(void)
const routerstatus_t * router_get_consensus_status_by_descriptor_digest(networkstatus_t *consensus, const char *digest)
int networkstatus_is_live(const networkstatus_t *ns, time_t now)
int should_delay_dir_fetches(const or_options_t *options, const char **msg_out)
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Header file for networkstatus.c.
Networkstatus consensus/vote structure.
double frac_nodes_with_descriptors(const smartlist_t *sl, bandwidth_weight_rule_t rule, int for_direct_conn)
Definition: node_select.c:821
Header file for node_select.c.
Node information structure.
bool nodefamily_contains_node(const nodefamily_t *family, const node_t *node)
Definition: nodefamily.c:328
void nodefamily_add_nodes_to_smartlist(const nodefamily_t *family, smartlist_t *out)
Definition: nodefamily.c:342
Header file for nodefamily.c.
static void node_free_(node_t *node)
Definition: nodelist.c:915
const node_t * router_find_exact_exit_enclave(const char *address, uint16_t port)
Definition: nodelist.c:2370
static int node_add_to_ed25519_map(node_t *node)
Definition: nodelist.c:311
void node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1885
bool node_supports_establish_intro_dos_extension(const node_t *node)
Definition: nodelist.c:1320
static nodelist_t * the_nodelist
Definition: nodelist.c:181
void node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1864
int nodelist_probably_contains_address(const tor_addr_t *addr)
Definition: nodelist.c:548
void node_get_verbose_nickname_by_id(const char *id_digest, char *verbose_name_out)
Definition: nodelist.c:1596
static bool nodes_have_common_family_id(const node_t *a, const node_t *b)
Definition: nodelist.c:2199
const uint8_t * node_get_rsa_id_digest(const node_t *node)
Definition: nodelist.c:1389
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:226
static double get_frac_paths_needed_for_circs(const or_options_t *options, const networkstatus_t *ns)
Definition: nodelist.c:2860
static double compute_frac_paths_available(const networkstatus_t *consensus, const or_options_t *options, time_t now, int *num_present_out, int *num_usable_out, char **status_out)
Definition: nodelist.c:2634
static char dir_info_status[512]
Definition: nodelist.c:2480
void nodelist_free_all(void)
Definition: nodelist.c:957
void nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
Definition: nodelist.c:2294
static int use_family_lists
Definition: nodelist.c:687
void nodelist_refresh_countries(void)
Definition: nodelist.c:2095
void router_dir_info_changed(void)
Definition: nodelist.c:2533
const smartlist_t * nodelist_get_list(void)
Definition: nodelist.c:1081
STATIC bool node_has_declared_family_list(const node_t *node)
Definition: nodelist.c:2166
int node_is_good_exit(const node_t *node)
Definition: nodelist.c:828
const node_t * node_get_by_nickname(const char *nickname, unsigned flags)
Definition: nodelist.c:1119
static void node_add_to_address_set(const node_t *node)
Definition: nodelist.c:451
void node_get_address_string(const node_t *node, char *buf, size_t len)
Definition: nodelist.c:1741
const char * node_get_platform(const node_t *node)
Definition: nodelist.c:1765
static void init_nodelist(void)
Definition: nodelist.c:185
int node_exit_policy_is_exact(const node_t *node, sa_family_t family)
Definition: nodelist.c:1639
int router_addrs_in_same_network(const tor_addr_t *a1, const tor_addr_t *a2)
Definition: nodelist.c:2105
bool node_supports_accepting_ipv6_extends(const node_t *node, bool need_canonical_ipv6_conn)
Definition: nodelist.c:1358
static void nodelist_update_consensus_params(const networkstatus_t *ns)
Definition: nodelist.c:701
static const tor_addr_t * node_get_prim_addr_ipv4(const node_t *node)
Definition: nodelist.c:1724
static void update_router_have_minimum_dir_info(void)
Definition: nodelist.c:2877
usable_descriptor_t
Definition: nodelist.c:91
static void count_usable_descriptors(int *num_present, int *num_usable, smartlist_t *descs_out, const networkstatus_t *consensus, time_t now, routerset_t *in_set, usable_descriptor_t exit_only)
Definition: nodelist.c:2563
static int microdesc_has_curve25519_onion_key(const microdesc_t *md)
Definition: nodelist.c:2032
void node_get_pref_dirport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1988
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
Definition: nodelist.c:1543
static int node_is_usable(const node_t *node)
Definition: nodelist.c:835
STATIC int node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
Definition: nodelist.c:2139
STATIC int node_family_list_contains(const node_t *n1, const node_t *n2)
Definition: nodelist.c:2151
node_t * nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
Definition: nodelist.c:579
smartlist_t * node_get_all_orports(const node_t *node)
Definition: nodelist.c:1678
node_t * node_get_mutable_by_ed25519_id(const ed25519_public_key_t *ed_id)
Definition: nodelist.c:210
int node_is_me(const node_t *node)
Definition: nodelist.c:1778
const node_t * node_get_by_ed25519_id(const ed25519_public_key_t *ed_id)
Definition: nodelist.c:234
static const protover_summary_flags_t zero_protover_flags
Definition: nodelist.c:1241
const char * node_get_nickname(const node_t *node)
Definition: nodelist.c:1493
bool node_supports_conflux(const node_t *node)
Definition: nodelist.c:1380
static int use_family_ids
Definition: nodelist.c:692
int node_has_any_descriptor(const node_t *node)
Definition: nodelist.c:1530
void nodelist_purge(void)
Definition: nodelist.c:928
node_t * nodelist_add_microdesc(microdesc_t *md)
Definition: nodelist.c:635
int node_is_dir(const node_t *node)
Definition: nodelist.c:1507
int node_is_unreliable(const node_t *node, int need_uptime, int need_capacity, int need_guard)
Definition: nodelist.c:2398
smartlist_t * nodelist_find_nodes_with_microdesc(const microdesc_t *md)
Definition: nodelist.c:897
bool node_supports_v3_rendezvous_point(const node_t *node)
Definition: nodelist.c:1305
STATIC void node_lookup_declared_family_list(smartlist_t *out, const node_t *node)
Definition: nodelist.c:2218
void node_get_prim_dirport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1965
bool node_supports_ed25519_link_authentication(const node_t *node, bool compatible_with_us)
Definition: nodelist.c:1269
bool node_supports_v3_hsdir(const node_t *node)
Definition: nodelist.c:1285
static consensus_path_type_t have_consensus_path
Definition: nodelist.c:2472
int node_ipv6_dir_preferred(const node_t *node)
Definition: nodelist.c:1942
int router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port, int need_uptime)
Definition: nodelist.c:2413
long node_get_declared_uptime(const node_t *node)
Definition: nodelist.c:1755
const char * get_dir_info_status_string(void)
Definition: nodelist.c:2543
static void node_addrs_changed(node_t *node)
Definition: nodelist.c:441
static node_t * node_get_or_create(const char *identity_digest)
Definition: nodelist.c:246
consensus_path_type_t router_have_consensus_path(void)
Definition: nodelist.c:2523
int node_ed25519_id_matches(const node_t *node, const ed25519_public_key_t *id)
Definition: nodelist.c:1229
static int need_to_update_have_min_dir_info
Definition: nodelist.c:2477
static int have_min_dir_info
Definition: nodelist.c:2469
static int node_remove_from_ed25519_map(node_t *node)
Definition: nodelist.c:268
void nodelist_add_addr_to_address_set(const tor_addr_t *addr, uint16_t or_port, uint16_t dir_port)
Definition: nodelist.c:525
static const smartlist_t * node_get_family_ids(const node_t *node)
Definition: nodelist.c:2184
static const protover_summary_flags_t * node_get_protover_summary_flags(const node_t *node)
Definition: nodelist.c:1247
int node_allows_single_hop_exits(const node_t *node)
Definition: nodelist.c:1611
const curve25519_public_key_t * node_get_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:2059
int node_get_purpose(const node_t *node)
Definition: nodelist.c:1564
void node_get_pref_ipv6_dirport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:2003
const ed25519_public_key_t * node_get_ed25519_id(const node_t *node)
Definition: nodelist.c:1184
void nodelist_set_consensus(const networkstatus_t *ns)
Definition: nodelist.c:715
bool nodelist_reentry_contains(const tor_addr_t *addr, uint16_t port)
Definition: nodelist.c:562
node_t * node_get_mutable_by_id(const char *identity_digest)
Definition: nodelist.c:197
void nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
Definition: nodelist.c:843
const node_t * node_get_by_hex_id(const char *hex_id, unsigned flags)
Definition: nodelist.c:1092
void node_set_country(node_t *node)
Definition: nodelist.c:2074
int node_ipv6_or_preferred(const node_t *node)
Definition: nodelist.c:1834
void nodelist_remove_routerinfo(routerinfo_t *ri)
Definition: nodelist.c:857
void nodelist_ensure_freshness(const networkstatus_t *ns)
Definition: nodelist.c:1060
STATIC int node_nickname_matches(const node_t *node, const char *nickname)
Definition: nodelist.c:2127
void node_get_addr(const node_t *node, tor_addr_t *addr_out)
Definition: nodelist.c:1715
static char * build_addr_port_item(const tor_addr_t *addr, const uint16_t port)
Definition: nodelist.c:496
static void node_log_dup_ed_id(const node_t *old, const node_t *node, const char *ed_id)
Definition: nodelist.c:295
int count_loading_descriptors_progress(void)
Definition: nodelist.c:2833
void node_get_verbose_nickname(const node_t *node, char *verbose_name_out)
Definition: nodelist.c:1576
bool node_supports_initiating_ipv6_extends(const node_t *node)
Definition: nodelist.c:1337
void node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1900
int nodes_in_same_family(const node_t *node1, const node_t *node2)
Definition: nodelist.c:2239
static void nodelist_drop_node(node_t *node, int remove_from_ht)
Definition: nodelist.c:872
int node_has_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:2052
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2490
void nodelist_assert_ok(void)
Definition: nodelist.c:983
int node_exit_policy_rejects_all(const node_t *node)
Definition: nodelist.c:1622
bool node_supports_ed25519_hs_intro(const node_t *node)
Definition: nodelist.c:1295
void router_set_status(const char *digest, int up)
Definition: nodelist.c:2434
Header file for nodelist.c.
consensus_path_type_t
Definition: nodelist.h:151
Master header file for Tor-specific functionality.
#define MAX_NICKNAME_LEN
Definition: or.h:112
#define UNNAMED_ROUTER_NICKNAME
Definition: or.h:497
@ V3_DIRINFO
Definition: or.h:878
@ BRIDGE_DIRINFO
Definition: or.h:880
int reachable_addr_prefer_ipv6_dirport(const or_options_t *options)
Definition: policies.c:512
int reachable_addr_prefer_ipv6_orport(const or_options_t *options)
Definition: policies.c:490
int reachable_addr_use_ipv6(const or_options_t *options)
Definition: policies.c:451
addr_policy_result_t compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port, const node_t *node)
Definition: policies.c:2907
Header file for policies.c.
addr_policy_result_t
Definition: policies.h:38
@ ADDR_POLICY_ACCEPTED
Definition: policies.h:40
@ ADDR_POLICY_PROBABLY_REJECTED
Definition: policies.h:48
@ ADDR_POLICY_REJECTED
Definition: policies.h:42
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
void dirserv_set_node_flags_from_authoritative_status(node_t *node, uint32_t authstatus)
uint32_t dirserv_router_get_status(const routerinfo_t *router, const char **msg, int severity)
Header file for process_descs.c.
Headers and type declarations for protover.c.
int router_digest_is_me(const char *digest)
Definition: router.c:1775
Router descriptor structure.
#define ROUTER_PURPOSE_GENERAL
int hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest, const char *nickname)
Definition: routerlist.c:722
routerlist_t * router_get_routerlist(void)
Definition: routerlist.c:897
signed_descriptor_t * router_get_by_descriptor_digest(const char *digest)
Definition: routerlist.c:786
int hex_digest_nickname_decode(const char *hexdigest, char *digest_out, char *nickname_qualifier_char_out, char *nickname_out)
Definition: routerlist.c:686
int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2)
Definition: routerlist.c:507
Header file for routerlist.c.
Router descriptor list structure.
int routerset_contains_routerstatus(const routerset_t *set, const routerstatus_t *rs, country_t country)
Definition: routerset.c:339
int routerset_contains_node(const routerset_t *set, const node_t *node)
Definition: routerset.c:353
Header file for routerset.c.
Routerstatus (consensus entry) structure.
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_del(smartlist_t *sl, int idx)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
unsigned int is_running
Definition: dir_server_st.h:39
uint8_t fetch[DIGEST256_LEN]
uint8_t store_first[DIGEST256_LEN]
uint8_t store_second[DIGEST256_LEN]
unsigned int held_by_nodes
Definition: microdesc_st.h:47
uint16_t ipv6_orport
Definition: microdesc_st.h:74
tor_addr_t ipv6_addr
Definition: microdesc_st.h:72
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: microdesc_st.h:68
char digest[DIGEST256_LEN]
Definition: microdesc_st.h:63
struct smartlist_t * family_ids
Definition: microdesc_st.h:80
unsigned int policy_is_reject_star
Definition: microdesc_st.h:45
struct nodefamily_t * family
Definition: microdesc_st.h:76
struct ed25519_public_key_t * ed25519_identity_pkey
Definition: microdesc_st.h:70
smartlist_t * routerstatus_list
consensus_flavor_t flavor
Definition: node_st.h:34
unsigned int is_bad_exit
Definition: node_st.h:71
country_t country
Definition: node_st.h:97
unsigned int is_running
Definition: node_st.h:63
int nodelist_idx
Definition: node_st.h:42
unsigned int rejects_all
Definition: node_st.h:88
unsigned int is_hs_dir
Definition: node_st.h:75
unsigned int is_valid
Definition: node_st.h:65
ed25519_public_key_t ed25519_id
Definition: node_st.h:53
char identity[DIGEST_LEN]
Definition: node_st.h:46
unsigned int is_possible_guard
Definition: node_st.h:69
unsigned int is_stable
Definition: node_st.h:68
unsigned int is_exit
Definition: node_st.h:70
time_t last_reachable
Definition: node_st.h:103
unsigned int ipv6_preferred
Definition: node_st.h:93
unsigned int name_lookup_warned
Definition: node_st.h:83
struct routerset_t * ExcludeExitNodesUnion_
struct smartlist_t * NodeFamilySets
int EnforceDistinctSubnets
struct routerset_t * EntryNodes
struct routerset_t * ExitNodes
double PathsNeededToBuildCircuits
struct routerset_t * MiddleNodes
unsigned int supports_ed25519_link_handshake_compat
Definition: or.h:798
unsigned int supports_v3_rendezvous_point
Definition: or.h:822
unsigned int supports_initiating_ipv6_extends
Definition: or.h:789
unsigned int supports_v3_hsdir
Definition: or.h:817
unsigned int supports_ed25519_link_handshake_any
Definition: or.h:803
unsigned int supports_canonical_ipv6_conns
Definition: or.h:793
unsigned int supports_conflux
Definition: or.h:833
unsigned int supports_accepting_ipv6_extends
Definition: or.h:785
unsigned int supports_ed25519_hs_intro
Definition: or.h:808
unsigned int allow_single_hop_exits
Definition: routerinfo_st.h:80
char * platform
Definition: routerinfo_st.h:52
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:31
tor_addr_t ipv4_addr
Definition: routerinfo_st.h:26
protover_summary_flags_t pv
smartlist_t * declared_family
Definition: routerinfo_st.h:69
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: routerinfo_st.h:47
struct smartlist_t * family_ids
Definition: routerinfo_st.h:74
unsigned int policy_is_reject_star
Definition: routerinfo_st.h:85
uint8_t purpose
unsigned int supports_tunnelled_dir_requests
Definition: routerinfo_st.h:94
char * nickname
Definition: routerinfo_st.h:23
smartlist_t * routers
Definition: routerlist_st.h:32
uint16_t ipv6_orport
tor_addr_t ipv6_addr
protover_summary_flags_t pv
char identity_digest[DIGEST_LEN]
char nickname[MAX_NICKNAME_LEN+1]
uint16_t ipv4_dirport
unsigned int is_possible_guard
uint16_t ipv4_orport
char identity_digest[DIGEST_LEN]
struct tor_cert_st * signing_key_cert
#define STATIC
Definition: testsupport.h:32
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
Header for torcert.c.
#define tor_assert(expr)
Definition: util_bug.h:103
#define tor_fragile_assert()
Definition: util_bug.h:278
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:76
#define ED25519_PUBKEY_LEN
Definition: x25519_sizes.h:27
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20