Tor 0.4.9.0-alpha-dev
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/** Tell the nodelist that the current usable consensus is <b>ns</b>.
684 * This makes the nodelist change all of the routerstatus entries for
685 * the nodes, drop nodes that no longer have enough info to get used,
686 * and grab microdescriptors into nodes as appropriate.
687 */
688void
690{
691 const or_options_t *options = get_options();
692 int authdir = authdir_mode_v3(options);
693
695 if (ns->flavor == FLAV_MICRODESC)
696 (void) get_microdesc_cache(); /* Make sure it exists first. */
697
698 SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
699 node->rs = NULL);
700
701 /* Conservatively estimate that every node will have 2 addresses (v4 and
702 * v6). Then we add the number of configured trusted authorities we have. */
703 int estimated_addresses = smartlist_len(ns->routerstatus_list) *
704 get_estimated_address_per_node();
705 estimated_addresses += (get_n_authorities(V3_DIRINFO | BRIDGE_DIRINFO) *
706 get_estimated_address_per_node());
707 /* Clear our sets because we will repopulate them with what this new
708 * consensus contains. */
709 address_set_free(the_nodelist->node_addrs);
710 the_nodelist->node_addrs = address_set_new(estimated_addresses);
711 digestmap_free(the_nodelist->reentry_set, NULL);
712 the_nodelist->reentry_set = digestmap_new();
713
715 node_t *node = node_get_or_create(rs->identity_digest);
716 node->rs = rs;
717 if (ns->flavor == FLAV_MICRODESC) {
718 if (node->md == NULL ||
719 tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
721 if (node->md)
722 node->md->held_by_nodes--;
724 rs->descriptor_digest);
725 if (node->md)
726 node->md->held_by_nodes++;
728 }
729 }
730
731 if (rs->pv.supports_v3_hsdir) {
732 node_set_hsdir_index(node, ns);
733 }
734 node_set_country(node);
735
736 /* If we're not an authdir, believe others. */
737 if (!authdir) {
738 node->is_valid = rs->is_valid;
739 node->is_running = rs->is_flagged_running;
740 node->is_fast = rs->is_fast;
741 node->is_stable = rs->is_stable;
742 node->is_possible_guard = rs->is_possible_guard;
743 node->is_exit = rs->is_exit;
744 node->is_bad_exit = rs->is_bad_exit;
745 node->is_hs_dir = rs->is_hs_dir;
746 node->ipv6_preferred = 0;
748 (tor_addr_is_null(&rs->ipv6_addr) == 0 ||
749 (node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
750 node->ipv6_preferred = 1;
751 }
752
753 } SMARTLIST_FOREACH_END(rs);
754
756
757 /* Now add all the nodes we have to the address set. */
760 } SMARTLIST_FOREACH_END(node);
761 /* Then, add all trusted configured directories. Some might not be in the
762 * consensus so make sure we know them. */
764
765 if (! authdir) {
767 /* We have no routerstatus for this router. Clear flags so we can skip
768 * it, maybe.*/
769 if (!node->rs) {
770 tor_assert(node->ri); /* if it had only an md, or nothing, purge
771 * would have removed it. */
772 if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
773 /* Clear all flags. */
774 node->is_valid = node->is_running = node->is_hs_dir =
775 node->is_fast = node->is_stable =
776 node->is_possible_guard = node->is_exit =
777 node->is_bad_exit = node->ipv6_preferred = 0;
778 }
779 }
780 } SMARTLIST_FOREACH_END(node);
781 }
782
783 /* If the consensus is live, note down the consensus valid-after that formed
784 * the nodelist. */
786 the_nodelist->live_consensus_valid_after = ns->valid_after;
787 }
788}
789
790/** Return 1 iff <b>node</b> has Exit flag and no BadExit flag.
791 * Otherwise, return 0.
792 */
793int
795{
796 return node->is_exit && ! node->is_bad_exit;
797}
798
799/** Helper: return true iff a node has a usable amount of information*/
800static inline int
802{
803 return (node->rs) || (node->ri);
804}
805
806/** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
807 * node with <b>identity_digest</b>. */
808void
809nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
810{
811 node_t *node = node_get_mutable_by_id(identity_digest);
812 if (node && node->md == md) {
813 node->md = NULL;
814 md->held_by_nodes--;
815 if (! node_get_ed25519_id(node)) {
817 }
818 }
819}
820
821/** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
822void
824{
825 node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
826 if (node && node->ri == ri) {
827 node->ri = NULL;
828 if (! node_is_usable(node)) {
829 nodelist_drop_node(node, 1);
830 node_free(node);
831 }
832 }
833}
834
835/** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
836 * with.) */
837static void
838nodelist_drop_node(node_t *node, int remove_from_ht)
839{
840 node_t *tmp;
841 int idx;
842 if (remove_from_ht) {
843 tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
844 tor_assert(tmp == node);
845 }
847
848 idx = node->nodelist_idx;
849 tor_assert(idx >= 0);
850
851 tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
852 smartlist_del(the_nodelist->nodes, idx);
853 if (idx < smartlist_len(the_nodelist->nodes)) {
854 tmp = smartlist_get(the_nodelist->nodes, idx);
855 tmp->nodelist_idx = idx;
856 }
857 node->nodelist_idx = -1;
858}
859
860/** Return a newly allocated smartlist of the nodes that have <b>md</b> as
861 * their microdescriptor. */
864{
865 smartlist_t *result = smartlist_new();
866
867 if (the_nodelist == NULL)
868 return result;
869
871 if (node->md == md) {
872 smartlist_add(result, node);
873 }
874 } SMARTLIST_FOREACH_END(node);
875
876 return result;
877}
878
879/** Release storage held by <b>node</b> */
880static void
882{
883 if (!node)
884 return;
885 if (node->md)
886 node->md->held_by_nodes--;
887 tor_assert(node->nodelist_idx == -1);
888 tor_free(node);
889}
890
891/** Remove all entries from the nodelist that don't have enough info to be
892 * usable for anything. */
893void
895{
896 node_t **iter;
897 if (PREDICT_UNLIKELY(the_nodelist == NULL))
898 return;
899
900 /* Remove the non-usable nodes. */
901 for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
902 node_t *node = *iter;
903
904 if (node->md && !node->rs) {
905 /* An md is only useful if there is an rs. */
906 node->md->held_by_nodes--;
907 node->md = NULL;
908 }
909
910 if (node_is_usable(node)) {
911 iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
912 } else {
913 iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
914 nodelist_drop_node(node, 0);
915 node_free(node);
916 }
917 }
919}
920
921/** Release all storage held by the nodelist. */
922void
924{
925 if (PREDICT_UNLIKELY(the_nodelist == NULL))
926 return;
927
928 HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
929 HT_CLEAR(nodelist_ed_map, &the_nodelist->nodes_by_ed_id);
931 node->nodelist_idx = -1;
932 node_free(node);
933 } SMARTLIST_FOREACH_END(node);
934
935 smartlist_free(the_nodelist->nodes);
936
937 address_set_free(the_nodelist->node_addrs);
938 the_nodelist->node_addrs = NULL;
939 digestmap_free(the_nodelist->reentry_set, NULL);
940 the_nodelist->reentry_set = NULL;
941
943}
944
945/** Check that the nodelist is internally consistent, and consistent with
946 * the directory info it's derived from.
947 */
948void
950{
953 digestmap_t *dm;
954
955 if (!the_nodelist)
956 return;
957
958 dm = digestmap_new();
959
960 /* every routerinfo in rl->routers should be in the nodelist. */
961 if (rl) {
963 const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
964 tor_assert(node && node->ri == ri);
965 tor_assert(fast_memeq(ri->cache_info.identity_digest,
966 node->identity, DIGEST_LEN));
967 tor_assert(! digestmap_get(dm, node->identity));
968 digestmap_set(dm, node->identity, (void*)node);
969 } SMARTLIST_FOREACH_END(ri);
970 }
971
972 /* every routerstatus in ns should be in the nodelist */
973 if (ns) {
975 const node_t *node = node_get_by_id(rs->identity_digest);
976 tor_assert(node && node->rs == rs);
977 tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
978 digestmap_set(dm, node->identity, (void*)node);
979 if (ns->flavor == FLAV_MICRODESC) {
980 /* If it's a microdesc consensus, every entry that has a
981 * microdescriptor should be in the nodelist.
982 */
983 microdesc_t *md =
984 microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
985 tor_assert(md == node->md);
986 if (md)
987 tor_assert(md->held_by_nodes >= 1);
988 }
989 } SMARTLIST_FOREACH_END(rs);
990 }
991
992 /* The nodelist should have no other entries, and its entries should be
993 * well-formed. */
995 tor_assert(digestmap_get(dm, node->identity) != NULL);
996 tor_assert(node_sl_idx == node->nodelist_idx);
997 } SMARTLIST_FOREACH_END(node);
998
999 /* Every node listed with an ed25519 identity should be listed by that
1000 * identity.
1001 */
1005 }
1006 } SMARTLIST_FOREACH_END(node);
1007
1008 node_t **idx;
1009 HT_FOREACH(idx, nodelist_ed_map, &the_nodelist->nodes_by_ed_id) {
1010 node_t *node = *idx;
1012 }
1013
1014 tor_assert((long)smartlist_len(the_nodelist->nodes) ==
1015 (long)HT_SIZE(&the_nodelist->nodes_by_id));
1016
1017 tor_assert((long)smartlist_len(the_nodelist->nodes) >=
1018 (long)HT_SIZE(&the_nodelist->nodes_by_ed_id));
1019
1020 digestmap_free(dm, NULL);
1021}
1022
1023/** Ensure that the nodelist has been created with the most recent consensus.
1024 * If that's not the case, make it so. */
1025void
1027{
1028 tor_assert(ns);
1029
1030 /* We don't even have a nodelist: this is a NOP. */
1031 if (!the_nodelist) {
1032 return;
1033 }
1034
1035 if (the_nodelist->live_consensus_valid_after != ns->valid_after) {
1036 log_info(LD_GENERAL, "Nodelist was not fresh: rebuilding. (%d / %d)",
1037 (int) the_nodelist->live_consensus_valid_after,
1038 (int) ns->valid_after);
1040 }
1041}
1042
1043/** Return a list of a node_t * for every node we know about. The caller
1044 * MUST NOT modify the list. (You can set and clear flags in the nodes if
1045 * you must, but you must not add or remove nodes.) */
1046MOCK_IMPL(const smartlist_t *,
1048{
1049 init_nodelist();
1050 return the_nodelist->nodes;
1051}
1052
1053/** Given a hex-encoded nickname of the format DIGEST, $DIGEST, $DIGEST=name,
1054 * or $DIGEST~name, return the node with the matching identity digest and
1055 * nickname (if any). Return NULL if no such node exists, or if <b>hex_id</b>
1056 * is not well-formed. DOCDOC flags */
1057const node_t *
1058node_get_by_hex_id(const char *hex_id, unsigned flags)
1059{
1060 char digest_buf[DIGEST_LEN];
1061 char nn_buf[MAX_NICKNAME_LEN+1];
1062 char nn_char='\0';
1063
1064 (void) flags; // XXXX
1065
1066 if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
1067 const node_t *node = node_get_by_id(digest_buf);
1068 if (!node)
1069 return NULL;
1070 if (nn_char == '=') {
1071 /* "=" indicates a Named relay, but there aren't any of those now. */
1072 return NULL;
1073 }
1074 return node;
1075 }
1076
1077 return NULL;
1078}
1079
1080/** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
1081 * the corresponding node_t, or NULL if none exists. Warn the user if they
1082 * have specified a router by nickname, unless the NNF_NO_WARN_UNNAMED bit is
1083 * set in <b>flags</b>. */
1084MOCK_IMPL(const node_t *,
1085node_get_by_nickname,(const char *nickname, unsigned flags))
1086{
1087 const int warn_if_unnamed = !(flags & NNF_NO_WARN_UNNAMED);
1088
1089 if (!the_nodelist)
1090 return NULL;
1091
1092 /* Handle these cases: DIGEST, $DIGEST, $DIGEST=name, $DIGEST~name. */
1093 {
1094 const node_t *node;
1095 if ((node = node_get_by_hex_id(nickname, flags)) != NULL)
1096 return node;
1097 }
1098
1099 if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
1100 return NULL;
1101
1102 /* Okay, so the name is not canonical for anybody. */
1103 {
1104 smartlist_t *matches = smartlist_new();
1105 const node_t *choice = NULL;
1106
1108 if (!strcasecmp(node_get_nickname(node), nickname))
1109 smartlist_add(matches, node);
1110 } SMARTLIST_FOREACH_END(node);
1111
1112 if (smartlist_len(matches)>1 && warn_if_unnamed) {
1113 int any_unwarned = 0;
1114 SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
1115 if (!node->name_lookup_warned) {
1116 node->name_lookup_warned = 1;
1117 any_unwarned = 1;
1118 }
1119 } SMARTLIST_FOREACH_END(node);
1120
1121 if (any_unwarned) {
1122 log_warn(LD_CONFIG, "There are multiple matches for the name %s. "
1123 "Choosing one arbitrarily.", nickname);
1124 }
1125 } else if (smartlist_len(matches)==1 && warn_if_unnamed) {
1126 char fp[HEX_DIGEST_LEN+1];
1127 node_t *node = smartlist_get(matches, 0);
1128 if (! node->name_lookup_warned) {
1129 base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
1130 log_warn(LD_CONFIG,
1131 "You specified a relay \"%s\" by name, but nicknames can be "
1132 "used by any relay, not just the one you meant. "
1133 "To make sure you get the same relay in the future, refer "
1134 "to it by key, as \"$%s\".", nickname, fp);
1135 node->name_lookup_warned = 1;
1136 }
1137 }
1138
1139 if (smartlist_len(matches))
1140 choice = smartlist_get(matches, 0);
1141
1142 smartlist_free(matches);
1143 return choice;
1144 }
1145}
1146
1147/** Return the Ed25519 identity key for the provided node, or NULL if it
1148 * doesn't have one. */
1151{
1152 const ed25519_public_key_t *ri_pk = NULL;
1153 const ed25519_public_key_t *md_pk = NULL;
1154
1155 if (node->ri) {
1156 if (node->ri->cache_info.signing_key_cert) {
1157 ri_pk = &node->ri->cache_info.signing_key_cert->signing_key;
1158 /* Checking whether routerinfo ed25519 is all zero.
1159 * Our descriptor parser should make sure this never happens. */
1160 if (BUG(ed25519_public_key_is_zero(ri_pk)))
1161 ri_pk = NULL;
1162 }
1163 }
1164
1165 if (node->md) {
1166 if (node->md->ed25519_identity_pkey) {
1167 md_pk = node->md->ed25519_identity_pkey;
1168 /* Checking whether microdesc ed25519 is all zero.
1169 * Our descriptor parser should make sure this never happens. */
1170 if (BUG(ed25519_public_key_is_zero(md_pk)))
1171 md_pk = NULL;
1172 }
1173 }
1174
1175 if (ri_pk && md_pk) {
1176 if (ed25519_pubkey_eq(ri_pk, md_pk)) {
1177 return ri_pk;
1178 } else {
1179 /* This can happen if the relay gets flagged NoEdConsensus which will be
1180 * triggered on all relays of the network. Thus a protocol warning. */
1181 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1182 "Inconsistent ed25519 identities in the nodelist");
1183 return NULL;
1184 }
1185 } else if (ri_pk) {
1186 return ri_pk;
1187 } else {
1188 return md_pk;
1189 }
1190}
1191
1192/** Return true iff this node's Ed25519 identity matches <b>id</b>.
1193 * (An absent Ed25519 identity matches NULL or zero.) */
1194int
1196{
1197 const ed25519_public_key_t *node_id = node_get_ed25519_id(node);
1198 if (node_id == NULL || ed25519_public_key_is_zero(node_id)) {
1199 return id == NULL || ed25519_public_key_is_zero(id);
1200 } else {
1201 return id && ed25519_pubkey_eq(node_id, id);
1202 }
1203}
1204
1205/** Dummy object that should be unreturnable. Used to ensure that
1206 * node_get_protover_summary_flags() always returns non-NULL. */
1208 0,0,0,0,0,0,0,0,0,0,0,0,0,0
1209};
1210
1211/** Return the protover_summary_flags for a given node. */
1212static const protover_summary_flags_t *
1214{
1215 if (node->rs) {
1216 return &node->rs->pv;
1217 } else if (node->ri) {
1218 return &node->ri->pv;
1219 } else {
1220 /* This should be impossible: every node should have a routerstatus or a
1221 * router descriptor or both. But just in case we've messed up somehow,
1222 * return a nice empty set of flags to indicate "this node supports
1223 * nothing." */
1224 tor_assert_nonfatal_unreached_once();
1225 return &zero_protover_flags;
1226 }
1227}
1228
1229/** Return true iff <b>node</b> supports authenticating itself
1230 * by ed25519 ID during the link handshake. If <b>compatible_with_us</b>,
1231 * it needs to be using a link authentication method that we understand.
1232 * If not, any plausible link authentication method will do. */
1233MOCK_IMPL(bool,
1235 bool compatible_with_us))
1236{
1237 if (! node_get_ed25519_id(node))
1238 return 0;
1239
1241
1242 if (compatible_with_us)
1244 else
1246}
1247
1248/** Return true iff <b>node</b> supports the hidden service directory version
1249 * 3 protocol (proposal 224). */
1250bool
1252{
1253 tor_assert(node);
1254
1256}
1257
1258/** Return true iff <b>node</b> supports ed25519 authentication as an hidden
1259 * service introduction point.*/
1260bool
1262{
1263 tor_assert(node);
1264
1266}
1267
1268/** Return true iff <b>node</b> can be a rendezvous point for hidden
1269 * service version 3 (HSRend=2). */
1270bool
1272{
1273 tor_assert(node);
1274
1275 /* We can't use a v3 rendezvous point without the curve25519 onion pk. */
1276 if (!node_get_curve25519_onion_key(node)) {
1277 return 0;
1278 }
1279
1281}
1282
1283/** Return true iff <b>node</b> supports the DoS ESTABLISH_INTRO cell
1284 * extension. */
1285bool
1287{
1288 tor_assert(node);
1289
1290 return node_get_protover_summary_flags(node)->
1291 supports_establish_intro_dos_extension;
1292}
1293
1294/** Return true iff <b>node</b> can initiate IPv6 extends (Relay=3).
1295 *
1296 * This check should only be performed by client path selection code.
1297 *
1298 * Extending relays should check their own IPv6 support using
1299 * router_can_extend_over_ipv6(). Like other extends, they should not verify
1300 * the link specifiers in the extend cell against the consensus, because it
1301 * may be out of date. */
1302bool
1304{
1305 tor_assert(node);
1306
1307 /* Relays can't initiate an IPv6 extend, unless they have an IPv6 ORPort. */
1308 if (!node_has_ipv6_orport(node)) {
1309 return 0;
1310 }
1311
1312 /* Initiating relays also need to support the relevant protocol version. */
1313 return
1315}
1316
1317/** Return true iff <b>node</b> can accept IPv6 extends (Relay=2 or Relay=3)
1318 * from other relays. If <b>need_canonical_ipv6_conn</b> is true, also check
1319 * if the relay supports canonical IPv6 connections (Relay=3 only).
1320 *
1321 * This check should only be performed by client path selection code.
1322 */
1323bool
1325 bool need_canonical_ipv6_conn)
1326{
1327 tor_assert(node);
1328
1329 /* Relays can't accept an IPv6 extend, unless they have an IPv6 ORPort. */
1330 if (!node_has_ipv6_orport(node)) {
1331 return 0;
1332 }
1333
1334 /* Accepting relays also need to support the relevant protocol version. */
1335 if (need_canonical_ipv6_conn) {
1336 return
1338 } else {
1339 return
1341 }
1342}
1343
1344/** Return true iff the given node supports conflux (Relay=5) */
1345bool
1347{
1348 tor_assert(node);
1349
1351}
1352
1353/** Return the RSA ID key's SHA1 digest for the provided node. */
1354const uint8_t *
1356{
1357 tor_assert(node);
1358 return (const uint8_t*)node->identity;
1359}
1360
1361/* Returns a new smartlist with all possible link specifiers from node:
1362 * - legacy ID is mandatory thus MUST be present in node;
1363 * - include ed25519 link specifier if present in the node, and the node
1364 * supports ed25519 link authentication, and:
1365 * - if direct_conn is true, its link versions are compatible with us,
1366 * - if direct_conn is false, regardless of its link versions;
1367 * - include IPv4 link specifier, if the primary address is not IPv4, log a
1368 * BUG() warning, and return an empty smartlist;
1369 * - include IPv6 link specifier if present in the node.
1370 *
1371 * If node is NULL, returns an empty smartlist.
1372 *
1373 * The smartlist must be freed using link_specifier_smartlist_free(). */
1375node_get_link_specifier_smartlist,(const node_t *node, bool direct_conn))
1376{
1377 link_specifier_t *ls;
1378 tor_addr_port_t ap;
1379 smartlist_t *lspecs = smartlist_new();
1380
1381 if (!node)
1382 return lspecs;
1383
1384 /* Get the relay's IPv4 address. */
1385 node_get_prim_orport(node, &ap);
1386
1387 /* We expect the node's primary address to be a valid IPv4 address.
1388 * This conforms to the protocol, which requires either an IPv4 or IPv6
1389 * address (or both). */
1390 if (BUG(!tor_addr_is_v4(&ap.addr)) ||
1391 BUG(!tor_addr_port_is_valid_ap(&ap, 0))) {
1392 return lspecs;
1393 }
1394
1395 ls = link_specifier_new();
1396 link_specifier_set_ls_type(ls, LS_IPV4);
1397 link_specifier_set_un_ipv4_addr(ls, tor_addr_to_ipv4h(&ap.addr));
1398 link_specifier_set_un_ipv4_port(ls, ap.port);
1399 /* Four bytes IPv4 and two bytes port. */
1400 link_specifier_set_ls_len(ls, sizeof(ap.addr.addr.in_addr) +
1401 sizeof(ap.port));
1402 smartlist_add(lspecs, ls);
1403
1404 /* Legacy ID is mandatory and will always be present in node. */
1405 ls = link_specifier_new();
1406 link_specifier_set_ls_type(ls, LS_LEGACY_ID);
1407 memcpy(link_specifier_getarray_un_legacy_id(ls), node->identity,
1408 link_specifier_getlen_un_legacy_id(ls));
1409 link_specifier_set_ls_len(ls, link_specifier_getlen_un_legacy_id(ls));
1410 smartlist_add(lspecs, ls);
1411
1412 /* ed25519 ID is only included if the node has it, and the node declares a
1413 protocol version that supports ed25519 link authentication.
1414 If direct_conn is true, we also require that the node's link version is
1415 compatible with us. (Otherwise, we will be sending the ed25519 key
1416 to another tor, which may support different link versions.) */
1418 node_supports_ed25519_link_authentication(node, direct_conn)) {
1419 ls = link_specifier_new();
1420 link_specifier_set_ls_type(ls, LS_ED25519_ID);
1421 memcpy(link_specifier_getarray_un_ed25519_id(ls), &node->ed25519_id,
1422 link_specifier_getlen_un_ed25519_id(ls));
1423 link_specifier_set_ls_len(ls, link_specifier_getlen_un_ed25519_id(ls));
1424 smartlist_add(lspecs, ls);
1425 }
1426
1427 /* Check for IPv6. If so, include it as well. */
1428 if (node_has_ipv6_orport(node)) {
1429 ls = link_specifier_new();
1430 node_get_pref_ipv6_orport(node, &ap);
1431 link_specifier_set_ls_type(ls, LS_IPV6);
1432 size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
1433 const uint8_t *in6_addr = tor_addr_to_in6_addr8(&ap.addr);
1434 uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
1435 memcpy(ipv6_array, in6_addr, addr_len);
1436 link_specifier_set_un_ipv6_port(ls, ap.port);
1437 /* Sixteen bytes IPv6 and two bytes port. */
1438 link_specifier_set_ls_len(ls, addr_len + sizeof(ap.port));
1439 smartlist_add(lspecs, ls);
1440 }
1441
1442 return lspecs;
1443}
1444
1445/* Free a link specifier list. */
1446void
1447link_specifier_smartlist_free_(smartlist_t *ls_list)
1448{
1449 if (!ls_list)
1450 return;
1451
1452 SMARTLIST_FOREACH(ls_list, link_specifier_t *, lspec,
1453 link_specifier_free(lspec));
1454 smartlist_free(ls_list);
1455}
1456
1457/** Return the nickname of <b>node</b>, or NULL if we can't find one. */
1458const char *
1460{
1461 tor_assert(node);
1462 if (node->rs)
1463 return node->rs->nickname;
1464 else if (node->ri)
1465 return node->ri->nickname;
1466 else
1467 return NULL;
1468}
1469
1470/** Return true iff <b>node</b> appears to be a directory authority or
1471 * directory cache */
1472int
1474{
1475 if (node->rs) {
1476 routerstatus_t * rs = node->rs;
1477 /* This is true if supports_tunnelled_dir_requests is true which
1478 * indicates that we support directory request tunnelled or through the
1479 * DirPort. */
1480 return rs->is_v2_dir;
1481 } else if (node->ri) {
1482 routerinfo_t * ri = node->ri;
1483 /* Both tunnelled request is supported or DirPort is set. */
1485 } else {
1486 return 0;
1487 }
1488}
1489
1490/** Return true iff <b>node</b> has either kind of descriptor -- that
1491 * is, a routerdescriptor or a microdescriptor.
1492 *
1493 * You should probably use node_has_preferred_descriptor() instead.
1494 **/
1495int
1497{
1498 return (node->ri ||
1499 (node->rs && node->md));
1500}
1501
1502/** Return true iff <b>node</b> has the kind of descriptor we would prefer to
1503 * use for it, given our configuration and how we intend to use the node.
1504 *
1505 * If <b>for_direct_connect</b> is true, we intend to connect to the node
1506 * directly, as the first hop of a circuit; otherwise, we intend to connect to
1507 * it indirectly, or use it as if we were connecting to it indirectly. */
1508int
1510 int for_direct_connect)
1511{
1512 const int is_bridge = node_is_a_configured_bridge(node);
1513 const int we_use_mds = we_use_microdescriptors_for_circuits(get_options());
1514
1515 if ((is_bridge && for_direct_connect) || !we_use_mds) {
1516 /* We need an ri in this case. */
1517 if (!node->ri)
1518 return 0;
1519 } else {
1520 /* Otherwise we need an rs and an md. */
1521 if (node->rs == NULL || node->md == NULL)
1522 return 0;
1523 }
1524
1525 return 1;
1526}
1527
1528/** Return the router_purpose of <b>node</b>. */
1529int
1531{
1532 if (node->ri)
1533 return node->ri->purpose;
1534 else
1536}
1537
1538/** Compute the verbose ("extended") nickname of <b>node</b> and store it
1539 * into the MAX_VERBOSE_NICKNAME_LEN+1 character buffer at
1540 * <b>verbose_name_out</b> */
1541void
1543 char *verbose_name_out)
1544{
1545 const char *nickname = node_get_nickname(node);
1546 verbose_name_out[0] = '$';
1547 base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
1548 DIGEST_LEN);
1549 if (!nickname)
1550 return;
1551 verbose_name_out[1+HEX_DIGEST_LEN] = '~';
1552 strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
1553}
1554
1555/** Compute the verbose ("extended") nickname of node with
1556 * given <b>id_digest</b> and store it into the MAX_VERBOSE_NICKNAME_LEN+1
1557 * character buffer at <b>verbose_name_out</b>
1558 *
1559 * If node_get_by_id() returns NULL, base 16 encoding of
1560 * <b>id_digest</b> is returned instead. */
1561void
1563 char *verbose_name_out)
1564{
1565 const node_t *node = node_get_by_id(id_digest);
1566 if (!node) {
1567 verbose_name_out[0] = '$';
1568 base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
1569 } else {
1570 node_get_verbose_nickname(node, verbose_name_out);
1571 }
1572}
1573
1574/** Return true iff it seems that <b>node</b> allows circuits to exit
1575 * through it directlry from the client. */
1576int
1578{
1579 if (node && node->ri)
1580 return node->ri->allow_single_hop_exits;
1581 else
1582 return 0;
1583}
1584
1585/** Return true iff it seems that <b>node</b> has an exit policy that doesn't
1586 * actually permit anything to exit, or we don't know its exit policy */
1587int
1589{
1590 if (node->rejects_all)
1591 return 1;
1592
1593 if (node->ri)
1594 return node->ri->policy_is_reject_star;
1595 else if (node->md)
1596 return node->md->policy_is_reject_star;
1597 else
1598 return 1;
1599}
1600
1601/** Return true iff the exit policy for <b>node</b> is such that we can treat
1602 * rejecting an address of type <b>family</b> unexpectedly as a sign of that
1603 * node's failure. */
1604int
1606{
1607 if (family == AF_UNSPEC) {
1608 return 1; /* Rejecting an address but not telling us what address
1609 * is a bad sign. */
1610 } else if (family == AF_INET) {
1611 return node->ri != NULL;
1612 } else if (family == AF_INET6) {
1613 return 0;
1614 }
1616 return 1;
1617}
1618
1619/* Check if the "addr" and port_field fields from r are a valid non-listening
1620 * address/port. If so, set valid to true and add a newly allocated
1621 * tor_addr_port_t containing "addr" and port_field to sl.
1622 * "addr" is an IPv4 host-order address and port_field is a uint16_t.
1623 * r is typically a routerinfo_t or routerstatus_t.
1624 */
1625#define SL_ADD_NEW_AP(r, addr_field, port_field, sl, valid) \
1626 STMT_BEGIN \
1627 if (tor_addr_port_is_valid(&(r)->addr_field, (r)->port_field, 0)) { \
1628 valid = 1; \
1629 tor_addr_port_t *ap = tor_addr_port_new(&(r)->addr_field, \
1630 (r)->port_field); \
1631 smartlist_add((sl), ap); \
1632 } \
1633 STMT_END
1634
1635/** Return list of tor_addr_port_t with all OR ports (in the sense IP
1636 * addr + TCP port) for <b>node</b>. Caller must free all elements
1637 * using tor_free() and free the list using smartlist_free().
1638 *
1639 * XXX this is potentially a memory fragmentation hog -- if on
1640 * critical path consider the option of having the caller allocate the
1641 * memory
1642 */
1645{
1646 smartlist_t *sl = smartlist_new();
1647 int valid = 0;
1648
1649 /* Find a valid IPv4 address and port */
1650 if (node->ri != NULL) {
1651 SL_ADD_NEW_AP(node->ri, ipv4_addr, ipv4_orport, sl, valid);
1652 }
1653
1654 /* If we didn't find a valid address/port in the ri, try the rs */
1655 if (!valid && node->rs != NULL) {
1656 SL_ADD_NEW_AP(node->rs, ipv4_addr, ipv4_orport, sl, valid);
1657 }
1658
1659 /* Find a valid IPv6 address and port */
1660 valid = 0;
1661 if (node->ri != NULL) {
1662 SL_ADD_NEW_AP(node->ri, ipv6_addr, ipv6_orport, sl, valid);
1663 }
1664
1665 if (!valid && node->rs != NULL) {
1666 SL_ADD_NEW_AP(node->rs, ipv6_addr, ipv6_orport, sl, valid);
1667 }
1668
1669 if (!valid && node->md != NULL) {
1670 SL_ADD_NEW_AP(node->md, ipv6_addr, ipv6_orport, sl, valid);
1671 }
1672
1673 return sl;
1674}
1675
1676#undef SL_ADD_NEW_AP
1677
1678/** Wrapper around node_get_prim_orport for backward
1679 compatibility. */
1680void
1681node_get_addr(const node_t *node, tor_addr_t *addr_out)
1682{
1683 tor_addr_port_t ap;
1684 node_get_prim_orport(node, &ap);
1685 tor_addr_copy(addr_out, &ap.addr);
1686}
1687
1688/** Return the IPv4 address for <b>node</b>, or NULL if none found. */
1689static const tor_addr_t *
1691{
1692 /* Don't check the ORPort or DirPort, as this function isn't port-specific,
1693 * and the node might have a valid IPv4 address, yet have a zero
1694 * ORPort or DirPort.
1695 */
1696 if (node->ri && tor_addr_is_valid(&node->ri->ipv4_addr, 0)) {
1697 return &node->ri->ipv4_addr;
1698 } else if (node->rs && tor_addr_is_valid(&node->rs->ipv4_addr, 0)) {
1699 return &node->rs->ipv4_addr;
1700 }
1701 return NULL;
1702}
1703
1704/** Copy a string representation of an IP address for <b>node</b> into
1705 * the <b>len</b>-byte buffer at <b>buf</b>. */
1706void
1707node_get_address_string(const node_t *node, char *buf, size_t len)
1708{
1709 const tor_addr_t *ipv4_addr = node_get_prim_addr_ipv4(node);
1710
1711 if (ipv4_addr) {
1712 tor_addr_to_str(buf, ipv4_addr, len, 0);
1713 } else if (len > 0) {
1714 buf[0] = '\0';
1715 }
1716}
1717
1718/** Return <b>node</b>'s declared uptime, or -1 if it doesn't seem to have
1719 * one. */
1720long
1722{
1723 if (node->ri)
1724 return node->ri->uptime;
1725 else
1726 return -1;
1727}
1728
1729/** Return <b>node</b>'s platform string, or NULL if we don't know it. */
1730const char *
1732{
1733 /* If we wanted, we could record the version in the routerstatus_t, since
1734 * the consensus lists it. We don't, though, so this function just won't
1735 * work with microdescriptors. */
1736 if (node->ri)
1737 return node->ri->platform;
1738 else
1739 return NULL;
1740}
1741
1742/** Return true iff <b>node</b> is one representing this router. */
1743int
1744node_is_me(const node_t *node)
1745{
1746 return router_digest_is_me(node->identity);
1747}
1748
1749/* Does this node have a valid IPv6 address?
1750 * Prefer node_has_ipv6_orport() or node_has_ipv6_dirport() for
1751 * checking specific ports. */
1752int
1753node_has_ipv6_addr(const node_t *node)
1754{
1755 /* Don't check the ORPort or DirPort, as this function isn't port-specific,
1756 * and the node might have a valid IPv6 address, yet have a zero
1757 * ORPort or DirPort.
1758 */
1759 if (node->ri && tor_addr_is_valid(&node->ri->ipv6_addr, 0))
1760 return 1;
1761 if (node->rs && tor_addr_is_valid(&node->rs->ipv6_addr, 0))
1762 return 1;
1763 if (node->md && tor_addr_is_valid(&node->md->ipv6_addr, 0))
1764 return 1;
1765
1766 return 0;
1767}
1768
1769/* Does this node have a valid IPv6 ORPort? */
1770int
1771node_has_ipv6_orport(const node_t *node)
1772{
1773 tor_addr_port_t ipv6_orport;
1774 node_get_pref_ipv6_orport(node, &ipv6_orport);
1775 return tor_addr_port_is_valid_ap(&ipv6_orport, 0);
1776}
1777
1778/* Does this node have a valid IPv6 DirPort? */
1779int
1780node_has_ipv6_dirport(const node_t *node)
1781{
1782 tor_addr_port_t ipv6_dirport;
1783 node_get_pref_ipv6_dirport(node, &ipv6_dirport);
1784 return tor_addr_port_is_valid_ap(&ipv6_dirport, 0);
1785}
1786
1787/** Return 1 if we prefer the IPv6 address and OR TCP port of
1788 * <b>node</b>, else 0.
1789 *
1790 * We prefer the IPv6 address if the router has an IPv6 address,
1791 * and we can use IPv6 addresses, and:
1792 * i) the node_t says that it prefers IPv6
1793 * or
1794 * ii) the router has no IPv4 OR address.
1795 *
1796 * If you don't have a node, consider looking it up.
1797 * If there is no node, use reachable_addr_prefer_ipv6_orport().
1798 */
1799int
1801{
1802 const or_options_t *options = get_options();
1803 tor_addr_port_t ipv4_addr;
1804 node_assert_ok(node);
1805
1806 /* XX/teor - node->ipv6_preferred is set from
1807 * reachable_addr_prefer_ipv6_orport() each time the consensus is loaded.
1808 */
1809 node_get_prim_orport(node, &ipv4_addr);
1810 if (!reachable_addr_use_ipv6(options)) {
1811 return 0;
1812 } else if (node->ipv6_preferred ||
1813 !tor_addr_port_is_valid_ap(&ipv4_addr, 0)) {
1814 return node_has_ipv6_orport(node);
1815 }
1816 return 0;
1817}
1818
1819#define RETURN_IPV4_AP(r, port_field, ap_out) \
1820 STMT_BEGIN \
1821 if (r && tor_addr_port_is_valid(&(r)->ipv4_addr, (r)->port_field, 0)) { \
1822 tor_addr_copy(&(ap_out)->addr, &(r)->ipv4_addr); \
1823 (ap_out)->port = (r)->port_field; \
1824 } \
1825 STMT_END
1826
1827/** Copy the primary (IPv4) OR port (IP address and TCP port) for <b>node</b>
1828 * into *<b>ap_out</b>. */
1829void
1831{
1832 node_assert_ok(node);
1833 tor_assert(ap_out);
1834
1835 /* Clear the address, as a safety precaution if calling functions ignore the
1836 * return value */
1837 tor_addr_make_null(&ap_out->addr, AF_INET);
1838 ap_out->port = 0;
1839
1840 /* Check ri first, because rewrite_node_address_for_bridge() updates
1841 * node->ri with the configured bridge address. */
1842
1843 RETURN_IPV4_AP(node->ri, ipv4_orport, ap_out);
1844 RETURN_IPV4_AP(node->rs, ipv4_orport, ap_out);
1845 /* Microdescriptors only have an IPv6 address */
1846}
1847
1848/** Copy the preferred OR port (IP address and TCP port) for
1849 * <b>node</b> into *<b>ap_out</b>. */
1850void
1852{
1853 tor_assert(ap_out);
1854
1855 if (node_ipv6_or_preferred(node)) {
1856 node_get_pref_ipv6_orport(node, ap_out);
1857 } else {
1858 /* the primary ORPort is always on IPv4 */
1859 node_get_prim_orport(node, ap_out);
1860 }
1861}
1862
1863/** Copy the preferred IPv6 OR port (IP address and TCP port) for
1864 * <b>node</b> into *<b>ap_out</b>. */
1865void
1867{
1868 node_assert_ok(node);
1869 tor_assert(ap_out);
1870 memset(ap_out, 0, sizeof(*ap_out));
1871
1872 /* Check ri first, because rewrite_node_address_for_bridge() updates
1873 * node->ri with the configured bridge address.
1874 * Prefer rs over md for consistency with the fascist_firewall_* functions.
1875 * Check if the address or port are valid, and try another alternative
1876 * if they are not. */
1877
1878 if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
1879 node->ri->ipv6_orport, 0)) {
1880 tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
1881 ap_out->port = node->ri->ipv6_orport;
1882 } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
1883 node->rs->ipv6_orport, 0)) {
1884 tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
1885 ap_out->port = node->rs->ipv6_orport;
1886 } else if (node->md && tor_addr_port_is_valid(&node->md->ipv6_addr,
1887 node->md->ipv6_orport, 0)) {
1888 tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
1889 ap_out->port = node->md->ipv6_orport;
1890 } else {
1891 tor_addr_make_null(&ap_out->addr, AF_INET6);
1892 ap_out->port = 0;
1893 }
1894}
1895
1896/** Return 1 if we prefer the IPv6 address and Dir TCP port of
1897 * <b>node</b>, else 0.
1898 *
1899 * We prefer the IPv6 address if the router has an IPv6 address,
1900 * and we can use IPv6 addresses, and:
1901 * i) the router has no IPv4 Dir address.
1902 * or
1903 * ii) our preference is for IPv6 Dir addresses.
1904 *
1905 * If there is no node, use reachable_addr_prefer_ipv6_dirport().
1906 */
1907int
1909{
1910 const or_options_t *options = get_options();
1911 tor_addr_port_t ipv4_addr;
1912 node_assert_ok(node);
1913
1914 /* node->ipv6_preferred is set from reachable_addr_prefer_ipv6_orport(),
1915 * so we can't use it to determine DirPort IPv6 preference.
1916 * This means that bridge clients will use IPv4 DirPorts by default.
1917 */
1918 node_get_prim_dirport(node, &ipv4_addr);
1919 if (!reachable_addr_use_ipv6(options)) {
1920 return 0;
1921 } else if (!tor_addr_port_is_valid_ap(&ipv4_addr, 0)
1923 return node_has_ipv6_dirport(node);
1924 }
1925 return 0;
1926}
1927
1928/** Copy the primary (IPv4) Dir port (IP address and TCP port) for <b>node</b>
1929 * into *<b>ap_out</b>. */
1930void
1932{
1933 node_assert_ok(node);
1934 tor_assert(ap_out);
1935
1936 /* Clear the address, as a safety precaution if calling functions ignore the
1937 * return value */
1938 tor_addr_make_null(&ap_out->addr, AF_INET);
1939 ap_out->port = 0;
1940
1941 /* Check ri first, because rewrite_node_address_for_bridge() updates
1942 * node->ri with the configured bridge address. */
1943
1944 RETURN_IPV4_AP(node->ri, ipv4_dirport, ap_out);
1945 RETURN_IPV4_AP(node->rs, ipv4_dirport, ap_out);
1946 /* Microdescriptors only have an IPv6 address */
1947}
1948
1949#undef RETURN_IPV4_AP
1950
1951/** Copy the preferred Dir port (IP address and TCP port) for
1952 * <b>node</b> into *<b>ap_out</b>. */
1953void
1955{
1956 tor_assert(ap_out);
1957
1958 if (node_ipv6_dir_preferred(node)) {
1959 node_get_pref_ipv6_dirport(node, ap_out);
1960 } else {
1961 /* the primary DirPort is always on IPv4 */
1962 node_get_prim_dirport(node, ap_out);
1963 }
1964}
1965
1966/** Copy the preferred IPv6 Dir port (IP address and TCP port) for
1967 * <b>node</b> into *<b>ap_out</b>. */
1968void
1970{
1971 node_assert_ok(node);
1972 tor_assert(ap_out);
1973
1974 /* Check ri first, because rewrite_node_address_for_bridge() updates
1975 * node->ri with the configured bridge address.
1976 * Prefer rs over md for consistency with the fascist_firewall_* functions.
1977 * Check if the address or port are valid, and try another alternative
1978 * if they are not. */
1979
1980 /* Assume IPv4 and IPv6 dirports are the same */
1981 if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
1982 node->ri->ipv4_dirport, 0)) {
1983 tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
1984 ap_out->port = node->ri->ipv4_dirport;
1985 } else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
1986 node->rs->ipv4_dirport, 0)) {
1987 tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
1988 ap_out->port = node->rs->ipv4_dirport;
1989 } else {
1990 tor_addr_make_null(&ap_out->addr, AF_INET6);
1991 ap_out->port = 0;
1992 }
1993}
1994
1995/** Return true iff <b>md</b> has a curve25519 onion key.
1996 * Use node_has_curve25519_onion_key() instead of calling this directly. */
1997static int
1999{
2000 if (!md) {
2001 return 0;
2002 }
2003
2004 if (!md->onion_curve25519_pkey) {
2005 return 0;
2006 }
2007
2008 if (fast_mem_is_zero((const char*)md->onion_curve25519_pkey->public_key,
2010 return 0;
2011 }
2012
2013 return 1;
2014}
2015
2016/** Return true iff <b>node</b> has a curve25519 onion key. */
2017int
2019{
2020 return node_get_curve25519_onion_key(node) != NULL;
2021}
2022
2023/** Return the curve25519 key of <b>node</b>, or NULL if none. */
2026{
2027 if (!node)
2028 return NULL;
2029 if (routerinfo_has_curve25519_onion_key(node->ri))
2030 return node->ri->onion_curve25519_pkey;
2031 else if (microdesc_has_curve25519_onion_key(node->md))
2032 return node->md->onion_curve25519_pkey;
2033 else
2034 return NULL;
2035}
2036
2037/** Refresh the country code of <b>ri</b>. This function MUST be called on
2038 * each router when the GeoIP database is reloaded, and on all new routers. */
2039void
2041{
2042 const tor_addr_t *ipv4_addr = NULL;
2043
2044 /* XXXXipv6 */
2045 if (node->rs)
2046 ipv4_addr = &node->rs->ipv4_addr;
2047 else if (node->ri)
2048 ipv4_addr = &node->ri->ipv4_addr;
2049
2050 /* IPv4 is mandatory for a relay so this should not happen unless we are
2051 * attempting to set the country code on a node without a descriptor. */
2052 if (BUG(!ipv4_addr)) {
2053 node->country = -1;
2054 return;
2055 }
2056 node->country = geoip_get_country_by_addr(ipv4_addr);
2057}
2058
2059/** Set the country code of all routers in the routerlist. */
2060void
2062{
2063 const smartlist_t *nodes = nodelist_get_list();
2064 SMARTLIST_FOREACH(nodes, node_t *, node,
2065 node_set_country(node));
2066}
2067
2068/** Return true iff router1 and router2 have similar enough network addresses
2069 * that we should treat them as being in the same family */
2070int
2072 const tor_addr_t *a2)
2073{
2074 if (tor_addr_is_null(a1) || tor_addr_is_null(a2))
2075 return 0;
2076
2077 switch (tor_addr_family(a1)) {
2078 case AF_INET:
2079 return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
2080 case AF_INET6:
2081 return 0 == tor_addr_compare_masked(a1, a2, 32, CMP_SEMANTIC);
2082 default:
2083 /* If not IPv4 or IPv6, return 0. */
2084 return 0;
2085 }
2086}
2087
2088/** Return true if <b>node</b>'s nickname matches <b>nickname</b>
2089 * (case-insensitive), or if <b>node's</b> identity key digest
2090 * matches a hexadecimal value stored in <b>nickname</b>. Return
2091 * false otherwise. */
2092STATIC int
2093node_nickname_matches(const node_t *node, const char *nickname)
2094{
2095 const char *n = node_get_nickname(node);
2096 if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
2097 return 1;
2098 return hex_digest_nickname_matches(nickname,
2099 node->identity,
2100 n);
2101}
2102
2103/** Return true iff <b>node</b> is named by some nickname in <b>lst</b>. */
2104STATIC int
2106{
2107 if (!lst) return 0;
2108 SMARTLIST_FOREACH(lst, const char *, name, {
2109 if (node_nickname_matches(node, name))
2110 return 1;
2111 });
2112 return 0;
2113}
2114
2115/** Return true iff n1's declared family contains n2. */
2116STATIC int
2118{
2119 if (n1->ri && n1->ri->declared_family) {
2120 return node_in_nickname_smartlist(n1->ri->declared_family, n2);
2121 } else if (n1->md) {
2122 return nodefamily_contains_node(n1->md->family, n2);
2123 } else {
2124 return 0;
2125 }
2126}
2127
2128/**
2129 * Return true iff <b>node</b> has declared a nonempty family.
2130 **/
2131STATIC bool
2133{
2134 if (node->ri && node->ri->declared_family &&
2135 smartlist_len(node->ri->declared_family)) {
2136 return true;
2137 }
2138
2139 if (node->md && node->md->family) {
2140 return true;
2141 }
2142
2143 return false;
2144}
2145
2146/**
2147 * Add to <b>out</b> every node_t that is listed by <b>node</b> as being in
2148 * its family. (Note that these nodes are not in node's family unless they
2149 * also agree that node is in their family.)
2150 **/
2151STATIC void
2153{
2154 if (node->ri && node->ri->declared_family &&
2155 smartlist_len(node->ri->declared_family)) {
2156 SMARTLIST_FOREACH_BEGIN(node->ri->declared_family, const char *, name) {
2157 const node_t *n2 = node_get_by_nickname(name, NNF_NO_WARN_UNNAMED);
2158 if (n2) {
2159 smartlist_add(out, (node_t *)n2);
2160 }
2161 } SMARTLIST_FOREACH_END(name);
2162 return;
2163 }
2164
2165 if (node->md && node->md->family) {
2167 }
2168}
2169
2170/** Return true iff r1 and r2 are in the same family, but not the same
2171 * router. */
2172int
2173nodes_in_same_family(const node_t *node1, const node_t *node2)
2174{
2175 const or_options_t *options = get_options();
2176
2177 /* Are they in the same family because of their addresses? */
2178 if (options->EnforceDistinctSubnets) {
2179 tor_addr_t a1, a2;
2180 node_get_addr(node1, &a1);
2181 node_get_addr(node2, &a2);
2182
2183 tor_addr_port_t ap6_1, ap6_2;
2184 node_get_pref_ipv6_orport(node1, &ap6_1);
2185 node_get_pref_ipv6_orport(node2, &ap6_2);
2186
2187 if (router_addrs_in_same_network(&a1, &a2) ||
2188 router_addrs_in_same_network(&ap6_1.addr, &ap6_2.addr))
2189 return 1;
2190 }
2191
2192 /* Are they in the same family because the agree they are? */
2193 if (node_family_contains(node1, node2) &&
2194 node_family_contains(node2, node1)) {
2195 return 1;
2196 }
2197
2198 /* Are they in the same family because the user says they are? */
2199 if (options->NodeFamilySets) {
2200 SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
2201 if (routerset_contains_node(rs, node1) &&
2202 routerset_contains_node(rs, node2))
2203 return 1;
2204 });
2205 }
2206
2207 return 0;
2208}
2209
2210/**
2211 * Add all the family of <b>node</b>, including <b>node</b> itself, to
2212 * the smartlist <b>sl</b>.
2213 *
2214 * This is used to make sure we don't pick siblings in a single path, or
2215 * pick more than one relay from a family for our entry guard list.
2216 * Note that a node may be added to <b>sl</b> more than once if it is
2217 * part of <b>node</b>'s family for more than one reason.
2218 */
2219void
2221{
2222 const smartlist_t *all_nodes = nodelist_get_list();
2223 const or_options_t *options = get_options();
2224
2225 tor_assert(node);
2226
2227 /* Let's make sure that we have the node itself, if it's a real node. */
2228 {
2229 const node_t *real_node = node_get_by_id(node->identity);
2230 if (real_node)
2231 smartlist_add(sl, (node_t*)real_node);
2232 }
2233
2234 /* First, add any nodes with similar network addresses. */
2235 if (options->EnforceDistinctSubnets) {
2236 tor_addr_t node_addr;
2237 tor_addr_port_t node_ap6;
2238 node_get_addr(node, &node_addr);
2239 node_get_pref_ipv6_orport(node, &node_ap6);
2240
2241 SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
2242 tor_addr_t a;
2243 tor_addr_port_t ap6;
2244 node_get_addr(node2, &a);
2245 node_get_pref_ipv6_orport(node2, &ap6);
2246 if (router_addrs_in_same_network(&a, &node_addr) ||
2247 router_addrs_in_same_network(&ap6.addr, &node_ap6.addr))
2248 smartlist_add(sl, (void*)node2);
2249 } SMARTLIST_FOREACH_END(node2);
2250 }
2251
2252 /* Now, add all nodes in the declared family of this node, if they
2253 * also declare this node to be in their family. */
2254 if (node_has_declared_family(node)) {
2255 smartlist_t *declared_family = smartlist_new();
2256 node_lookup_declared_family(declared_family, node);
2257
2258 /* Add every r such that router declares familyness with node, and node
2259 * declares familyhood with router. */
2260 SMARTLIST_FOREACH_BEGIN(declared_family, const node_t *, node2) {
2261 if (node_family_contains(node2, node)) {
2262 smartlist_add(sl, (void*)node2);
2263 }
2264 } SMARTLIST_FOREACH_END(node2);
2265 smartlist_free(declared_family);
2266 }
2267
2268 /* If the user declared any families locally, honor those too. */
2269 if (options->NodeFamilySets) {
2270 SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
2271 if (routerset_contains_node(rs, node)) {
2272 routerset_get_all_nodes(sl, rs, NULL, 0);
2273 }
2274 });
2275 }
2276}
2277
2278/** Find a router that's up, that has this IP address, and
2279 * that allows exit to this address:port, or return NULL if there
2280 * isn't a good one.
2281 * Don't exit enclave to excluded relays -- it wouldn't actually
2282 * hurt anything, but this way there are fewer confused users.
2283 */
2284const node_t *
2285router_find_exact_exit_enclave(const char *address, uint16_t port)
2286{/*XXXX MOVE*/
2287 struct in_addr in;
2288 tor_addr_t ipv4_addr;
2289 const or_options_t *options = get_options();
2290
2291 if (!tor_inet_aton(address, &in))
2292 return NULL; /* it's not an IP already */
2293 tor_addr_from_in(&ipv4_addr, &in);
2294
2295 SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
2296 if (tor_addr_eq(node_get_prim_addr_ipv4(node), &ipv4_addr) &&
2297 node->is_running &&
2298 compare_tor_addr_to_node_policy(&ipv4_addr, port, node) ==
2301 return node;
2302 });
2303 return NULL;
2304}
2305
2306/** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
2307 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
2308 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
2309 * bandwidth.
2310 * If <b>need_guard</b>, we require that the router is a possible entry guard.
2311 */
2312int
2313node_is_unreliable(const node_t *node, int need_uptime,
2314 int need_capacity, int need_guard)
2315{
2316 if (need_uptime && !node->is_stable)
2317 return 1;
2318 if (need_capacity && !node->is_fast)
2319 return 1;
2320 if (need_guard && !node->is_possible_guard)
2321 return 1;
2322 return 0;
2323}
2324
2325/** Return 1 if all running sufficiently-stable routers we can use will reject
2326 * addr:port. Return 0 if any might accept it. */
2327int
2329 int need_uptime)
2330{
2332
2334 if (node->is_running &&
2335 !node_is_unreliable(node, need_uptime, 0, 0)) {
2336
2337 r = compare_tor_addr_to_node_policy(addr, port, node);
2338
2340 return 0; /* this one could be ok. good enough. */
2341 }
2342 } SMARTLIST_FOREACH_END(node);
2343 return 1; /* all will reject. */
2344}
2345
2346/** Mark the router with ID <b>digest</b> as running or non-running
2347 * in our routerlist. */
2348void
2349router_set_status(const char *digest, int up)
2350{
2351 node_t *node;
2352 tor_assert(digest);
2353
2354 SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
2355 dir_server_t *, d,
2356 if (tor_memeq(d->digest, digest, DIGEST_LEN))
2357 d->is_running = up);
2358
2359 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
2360 dir_server_t *, d,
2361 if (tor_memeq(d->digest, digest, DIGEST_LEN))
2362 d->is_running = up);
2363
2364 node = node_get_mutable_by_id(digest);
2365 if (node) {
2366#if 0
2367 log_debug(LD_DIR,"Marking router %s as %s.",
2368 node_describe(node), up ? "up" : "down");
2369#endif
2370 if (!up && node_is_me(node) && !net_is_disabled())
2371 log_warn(LD_NET, "We just marked ourself as down. Are your external "
2372 "addresses reachable?");
2373
2374 if (bool_neq(node->is_running, up))
2376
2377 node->is_running = up;
2378 }
2379}
2380
2381/** True iff, the last time we checked whether we had enough directory info
2382 * to build circuits, the answer was "yes". If there are no exits in the
2383 * consensus, we act as if we have 100% of the exit directory info. */
2384static int have_min_dir_info = 0;
2385
2386/** Does the consensus contain nodes that can exit? */
2387static consensus_path_type_t have_consensus_path = CONSENSUS_PATH_UNKNOWN;
2388
2389/** True iff enough has changed since the last time we checked whether we had
2390 * enough directory info to build circuits that our old answer can no longer
2391 * be trusted. */
2393/** String describing what we're missing before we have enough directory
2394 * info. */
2395static char dir_info_status[512] = "";
2396
2397/** Return true iff we have enough consensus information to
2398 * start building circuits. Right now, this means "a consensus that's
2399 * less than a day old, and at least 60% of router descriptors (configurable),
2400 * weighted by bandwidth. Treat the exit fraction as 100% if there are
2401 * no exits in the consensus."
2402 * To obtain the final weighted bandwidth, we multiply the
2403 * weighted bandwidth fraction for each position (guard, middle, exit). */
2404MOCK_IMPL(int,
2406{
2407 static int logged_delay=0;
2408 const char *delay_fetches_msg = NULL;
2409 if (should_delay_dir_fetches(get_options(), &delay_fetches_msg)) {
2410 if (!logged_delay)
2411 log_notice(LD_DIR, "Delaying directory fetches: %s", delay_fetches_msg);
2412 logged_delay=1;
2413 strlcpy(dir_info_status, delay_fetches_msg, sizeof(dir_info_status));
2414 return 0;
2415 }
2416 logged_delay = 0; /* reset it if we get this far */
2417
2418 if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
2420 }
2421
2422 return have_min_dir_info;
2423}
2424
2425/** Set to CONSENSUS_PATH_EXIT if there is at least one exit node
2426 * in the consensus. We update this flag in compute_frac_paths_available if
2427 * there is at least one relay that has an Exit flag in the consensus.
2428 * Used to avoid building exit circuits when they will almost certainly fail.
2429 * Set to CONSENSUS_PATH_INTERNAL if there are no exits in the consensus.
2430 * (This situation typically occurs during bootstrap of a test network.)
2431 * Set to CONSENSUS_PATH_UNKNOWN if we have never checked, or have
2432 * reason to believe our last known value was invalid or has expired.
2433 * If we're in a network with TestingDirAuthVoteExit set,
2434 * this can cause router_have_consensus_path() to be set to
2435 * CONSENSUS_PATH_EXIT, even if there are no nodes with accept exit policies.
2436 */
2439{
2440 return have_consensus_path;
2441}
2442
2443/** Called when our internal view of the directory has changed. This can be
2444 * when the authorities change, networkstatuses change, the list of routerdescs
2445 * changes, or number of running routers changes.
2446 */
2447void
2449{
2453}
2454
2455/** Return a string describing what we're missing before we have enough
2456 * directory info. */
2457const char *
2459{
2460 return dir_info_status;
2461}
2462
2463/** Iterate over the servers listed in <b>consensus</b>, and count how many of
2464 * them seem like ones we'd use (store this in *<b>num_usable</b>), and how
2465 * many of <em>those</em> we have descriptors for (store this in
2466 * *<b>num_present</b>).
2467 *
2468 * If <b>in_set</b> is non-NULL, only consider those routers in <b>in_set</b>.
2469 * If <b>exit_only</b> & USABLE_DESCRIPTOR_EXIT_POLICY, only consider nodes
2470 * present if they have an exit policy that accepts at least one port.
2471 * If <b>exit_only</b> & USABLE_DESCRIPTOR_EXIT_FLAG, only consider nodes
2472 * usable if they have the exit flag in the consensus.
2473 *
2474 * If *<b>descs_out</b> is present, add a node_t for each usable descriptor
2475 * to it.
2476 */
2477static void
2478count_usable_descriptors(int *num_present, int *num_usable,
2479 smartlist_t *descs_out,
2480 const networkstatus_t *consensus,
2481 time_t now,
2482 routerset_t *in_set,
2483 usable_descriptor_t exit_only)
2484{
2485 const int md = (consensus->flavor == FLAV_MICRODESC);
2486 *num_present = 0, *num_usable = 0;
2487
2489 {
2490 const node_t *node = node_get_by_id(rs->identity_digest);
2491 if (!node)
2492 continue; /* This would be a bug: every entry in the consensus is
2493 * supposed to have a node. */
2494 if ((exit_only & USABLE_DESCRIPTOR_EXIT_FLAG) && ! rs->is_exit)
2495 continue;
2496 if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
2497 continue;
2498 if (client_would_use_router(rs, now)) {
2499 const char * const digest = rs->descriptor_digest;
2500 int present;
2501 ++*num_usable; /* the consensus says we want it. */
2502 if (md)
2503 present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
2504 else
2505 present = NULL != router_get_by_descriptor_digest(digest);
2506 if (present) {
2507 /* Do the policy check last, because it requires a descriptor,
2508 * and is potentially expensive */
2509 if ((exit_only & USABLE_DESCRIPTOR_EXIT_POLICY) &&
2511 continue;
2512 }
2513 /* we have the descriptor listed in the consensus, and it
2514 * satisfies our exit constraints (if any) */
2515 ++*num_present;
2516 }
2517 if (descs_out)
2518 smartlist_add(descs_out, (node_t*)node);
2519 }
2520 }
2521 SMARTLIST_FOREACH_END(rs);
2522
2523 log_debug(LD_DIR, "%d usable, %d present (%s%s%s%s%s).",
2524 *num_usable, *num_present,
2525 md ? "microdesc" : "desc",
2526 (exit_only & USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG) ?
2527 " exit" : "s",
2528 (exit_only & USABLE_DESCRIPTOR_EXIT_POLICY) ?
2529 " policies" : "" ,
2530 (exit_only == USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG) ?
2531 " and" : "" ,
2532 (exit_only & USABLE_DESCRIPTOR_EXIT_FLAG) ?
2533 " flags" : "" );
2534}
2535
2536/** Return an estimate of which fraction of usable paths through the Tor
2537 * network we have available for use. Count how many routers seem like ones
2538 * we'd use (store this in *<b>num_usable_out</b>), and how many of
2539 * <em>those</em> we have descriptors for (store this in
2540 * *<b>num_present_out</b>.)
2541 *
2542 * If **<b>status_out</b> is present, allocate a new string and print the
2543 * available percentages of guard, middle, and exit nodes to it, noting
2544 * whether there are exits in the consensus.
2545 * If there are no exits in the consensus, we treat the exit fraction as 100%,
2546 * but set router_have_consensus_path() so that we can only build internal
2547 * paths. */
2548static double
2550 const or_options_t *options, time_t now,
2551 int *num_present_out, int *num_usable_out,
2552 char **status_out)
2553{
2554 smartlist_t *guards = smartlist_new();
2555 smartlist_t *mid = smartlist_new();
2556 smartlist_t *exits = smartlist_new();
2557 double f_guard, f_mid, f_exit;
2558 double f_path = 0.0;
2559 /* Used to determine whether there are any exits in the consensus */
2560 int np = 0;
2561 /* Used to determine whether there are any exits with descriptors */
2562 int nu = 0;
2563 const int authdir = authdir_mode_v3(options);
2564
2565 count_usable_descriptors(num_present_out, num_usable_out,
2566 mid, consensus, now, options->MiddleNodes,
2567 USABLE_DESCRIPTOR_ALL);
2568 log_debug(LD_NET,
2569 "%s: %d present, %d usable",
2570 "mid",
2571 np,
2572 nu);
2573
2574 if (options->EntryNodes) {
2575 count_usable_descriptors(&np, &nu, guards, consensus, now,
2576 options->EntryNodes, USABLE_DESCRIPTOR_ALL);
2577 log_debug(LD_NET,
2578 "%s: %d present, %d usable",
2579 "guard",
2580 np,
2581 nu);
2582 } else {
2583 SMARTLIST_FOREACH(mid, const node_t *, node, {
2584 if (authdir) {
2585 if (node->rs && node->rs->is_possible_guard)
2586 smartlist_add(guards, (node_t*)node);
2587 } else {
2588 if (node->is_possible_guard)
2589 smartlist_add(guards, (node_t*)node);
2590 }
2591 });
2592 log_debug(LD_NET,
2593 "%s: %d possible",
2594 "guard",
2595 smartlist_len(guards));
2596 }
2597
2598 /* All nodes with exit policy and flag */
2599 count_usable_descriptors(&np, &nu, exits, consensus, now,
2600 NULL, USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG);
2601 log_debug(LD_NET,
2602 "%s: %d present, %d usable",
2603 "exits",
2604 np,
2605 nu);
2606
2607 /* We need at least 1 exit (flag and policy) in the consensus to consider
2608 * building exit paths */
2609 /* Update our understanding of whether the consensus has exits */
2610 consensus_path_type_t old_have_consensus_path = have_consensus_path;
2611 have_consensus_path = ((np > 0) ?
2612 CONSENSUS_PATH_EXIT :
2613 CONSENSUS_PATH_INTERNAL);
2614
2615 if (old_have_consensus_path != have_consensus_path) {
2616 if (have_consensus_path == CONSENSUS_PATH_INTERNAL) {
2617 log_notice(LD_NET,
2618 "The current consensus has no exit nodes. "
2619 "Tor can only build internal paths, "
2620 "such as paths to onion services.");
2621
2622 /* However, exit nodes can reachability self-test using this consensus,
2623 * join the network, and appear in a later consensus. This will allow
2624 * the network to build exit paths, such as paths for world wide web
2625 * browsing (as distinct from hidden service web browsing). */
2626 } else if (old_have_consensus_path == CONSENSUS_PATH_INTERNAL) {
2627 log_notice(LD_NET,
2628 "The current consensus contains exit nodes. "
2629 "Tor can build exit and internal paths.");
2630 }
2631 }
2632
2633 f_guard = frac_nodes_with_descriptors(guards, WEIGHT_FOR_GUARD, 1);
2634 f_mid = frac_nodes_with_descriptors(mid, WEIGHT_FOR_MID, 0);
2635 f_exit = frac_nodes_with_descriptors(exits, WEIGHT_FOR_EXIT, 0);
2636
2637 /* If we are using bridges and have at least one bridge with a full
2638 * descriptor, assume f_guard is 1.0. */
2639 if (options->UseBridges && num_bridges_usable(0) > 0)
2640 f_guard = 1.0;
2641
2642 log_debug(LD_NET,
2643 "f_guard: %.2f, f_mid: %.2f, f_exit: %.2f",
2644 f_guard,
2645 f_mid,
2646 f_exit);
2647
2648 smartlist_free(guards);
2649 smartlist_free(mid);
2650 smartlist_free(exits);
2651
2652 if (options->ExitNodes) {
2653 double f_myexit, f_myexit_unflagged;
2654 smartlist_t *myexits= smartlist_new();
2655 smartlist_t *myexits_unflagged = smartlist_new();
2656
2657 /* All nodes with exit policy and flag in ExitNodes option */
2658 count_usable_descriptors(&np, &nu, myexits, consensus, now,
2659 options->ExitNodes,
2660 USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG);
2661 log_debug(LD_NET,
2662 "%s: %d present, %d usable",
2663 "myexits",
2664 np,
2665 nu);
2666
2667 /* Now compute the nodes in the ExitNodes option where we know their exit
2668 * policy permits something. */
2669 count_usable_descriptors(&np, &nu, myexits_unflagged,
2670 consensus, now,
2671 options->ExitNodes,
2672 USABLE_DESCRIPTOR_EXIT_POLICY);
2673 log_debug(LD_NET,
2674 "%s: %d present, %d usable",
2675 "myexits_unflagged (initial)",
2676 np,
2677 nu);
2678
2679 f_myexit= frac_nodes_with_descriptors(myexits,WEIGHT_FOR_EXIT, 0);
2680 f_myexit_unflagged=
2681 frac_nodes_with_descriptors(myexits_unflagged,
2682 WEIGHT_FOR_EXIT, 0);
2683
2684 log_debug(LD_NET,
2685 "f_exit: %.2f, f_myexit: %.2f, f_myexit_unflagged: %.2f",
2686 f_exit,
2687 f_myexit,
2688 f_myexit_unflagged);
2689
2690 /* If our ExitNodes list has eliminated every possible Exit node, and there
2691 * were some possible Exit nodes, then instead consider nodes that permit
2692 * exiting to some ports. */
2693 if (smartlist_len(myexits) == 0 &&
2694 smartlist_len(myexits_unflagged)) {
2695 f_myexit = f_myexit_unflagged;
2696 }
2697
2698 smartlist_free(myexits);
2699 smartlist_free(myexits_unflagged);
2700
2701 /* This is a tricky point here: we don't want to make it easy for a
2702 * directory to trickle exits to us until it learns which exits we have
2703 * configured, so require that we have a threshold both of total exits
2704 * and usable exits. */
2705 if (f_myexit < f_exit)
2706 f_exit = f_myexit;
2707 }
2708
2709 /* If the consensus has no exits that pass flag, descriptor, and policy
2710 * checks, we can only build onion service paths, which are G - M - M. */
2711 if (router_have_consensus_path() != CONSENSUS_PATH_EXIT) {
2712 /* If the exit bandwidth weight fraction is not zero, we need to wait for
2713 * descriptors for those exits. (The bandwidth weight fraction does not
2714 * check for descriptors.)
2715 * If the exit bandwidth fraction is zero, there are no exits in the
2716 * consensus at all. So it is safe to replace f_exit with f_mid.
2717 *
2718 * f_exit is non-negative, but some compilers complain about float and ==
2719 */
2720 if (f_exit <= 0.0) {
2721 f_exit = f_mid;
2722 }
2723 }
2724
2725 f_path = f_guard * f_mid * f_exit;
2726
2727 if (status_out)
2728 tor_asprintf(status_out,
2729 "%d%% of guards bw, "
2730 "%d%% of midpoint bw, and "
2731 "%d%% of %s = "
2732 "%d%% of path bw",
2733 (int)(f_guard*100),
2734 (int)(f_mid*100),
2735 (int)(f_exit*100),
2736 (router_have_consensus_path() == CONSENSUS_PATH_EXIT ?
2737 "exit bw" :
2738 "end bw (no exits in consensus, using mid)"),
2739 (int)(f_path*100));
2740
2741 return f_path;
2742}
2743
2744/** We just fetched a new set of descriptors. Compute how far through
2745 * the "loading descriptors" bootstrapping phase we are, so we can inform
2746 * the controller of our progress. */
2747int
2749{
2750 int num_present = 0, num_usable=0;
2751 time_t now = time(NULL);
2752 const or_options_t *options = get_options();
2753 const networkstatus_t *consensus =
2755 double paths, fraction;
2756
2757 if (!consensus)
2758 return 0; /* can't count descriptors if we have no list of them */
2759
2760 paths = compute_frac_paths_available(consensus, options, now,
2761 &num_present, &num_usable,
2762 NULL);
2763
2764 fraction = paths / get_frac_paths_needed_for_circs(options,consensus);
2765 if (fraction > 1.0)
2766 return 0; /* it's not the number of descriptors holding us back */
2767 return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
2768 (fraction*(BOOTSTRAP_STATUS_ENOUGH_DIRINFO-1 -
2769 BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
2770}
2771
2772/** Return the fraction of paths needed before we're willing to build
2773 * circuits, as configured in <b>options</b>, or in the consensus <b>ns</b>. */
2774static double
2776 const networkstatus_t *ns)
2777{
2778#define DFLT_PCT_USABLE_NEEDED 60
2779 if (options->PathsNeededToBuildCircuits >= 0.0) {
2780 return options->PathsNeededToBuildCircuits;
2781 } else {
2782 return networkstatus_get_param(ns, "min_paths_for_circs_pct",
2783 DFLT_PCT_USABLE_NEEDED,
2784 25, 95)/100.0;
2785 }
2786}
2787
2788/** Change the value of have_min_dir_info, setting it true iff we have enough
2789 * network and router information to build circuits. Clear the value of
2790 * need_to_update_have_min_dir_info. */
2791static void
2793{
2794 time_t now = time(NULL);
2795 int res;
2796 int num_present=0, num_usable=0;
2797 const or_options_t *options = get_options();
2798 const networkstatus_t *consensus =
2800 int using_md;
2801 static int be_loud_when_things_work_again = 0;
2802
2803 if (!consensus) {
2805 strlcpy(dir_info_status, "We have no usable consensus.",
2806 sizeof(dir_info_status));
2807 else
2808 strlcpy(dir_info_status, "We have no recent usable consensus.",
2809 sizeof(dir_info_status));
2810 res = 0;
2811 goto done;
2812 }
2813
2814 using_md = consensus->flavor == FLAV_MICRODESC;
2815
2816 /* Check fraction of available paths */
2817 {
2818 char *status = NULL;
2819 double paths = compute_frac_paths_available(consensus, options, now,
2820 &num_present, &num_usable,
2821 &status);
2822
2823 if (paths < get_frac_paths_needed_for_circs(options,consensus)) {
2825 "We need more %sdescriptors: we have %d/%d, and "
2826 "can only build %d%% of likely paths. (We have %s.)",
2827 using_md?"micro":"", num_present, num_usable,
2828 (int)(paths*100), status);
2829 tor_free(status);
2830 res = 0;
2831 control_event_boot_dir(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
2832 goto done;
2833 }
2834
2835 tor_free(status);
2836 res = 1;
2837 }
2838
2839 { /* Check entry guard dirinfo status */
2840 char *guard_error = entry_guards_get_err_str_if_dir_info_missing(using_md,
2841 num_present,
2842 num_usable);
2843 if (guard_error) {
2844 strlcpy(dir_info_status, guard_error, sizeof(dir_info_status));
2845 tor_free(guard_error);
2846 res = 0;
2847 goto done;
2848 }
2849 }
2850
2851 done:
2852
2853 /* If paths have just become available in this update. */
2854 if (res && !have_min_dir_info) {
2855 control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
2856 control_event_boot_dir(BOOTSTRAP_STATUS_ENOUGH_DIRINFO, 0);
2857 tor_log(be_loud_when_things_work_again ? LOG_NOTICE : LOG_INFO, LD_DIR,
2858 "We now have enough directory information to build circuits.");
2859 be_loud_when_things_work_again = 0;
2860 }
2861
2862 /* If paths have just become unavailable in this update. */
2863 if (!res && have_min_dir_info) {
2866 "Our directory information is no longer up-to-date "
2867 "enough to build circuits: %s", dir_info_status);
2868 if (!quiet) {
2869 /* remember to do a notice-level log when things come back */
2870 be_loud_when_things_work_again = 1;
2871 }
2872
2873 /* a) make us log when we next complete a circuit, so we know when Tor
2874 * is back up and usable, and b) disable some activities that Tor
2875 * should only do while circuits are working, like reachability tests
2876 * and fetching bridge descriptors only over circuits. */
2878 have_consensus_path = CONSENSUS_PATH_UNKNOWN;
2879 control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
2880 }
2881 have_min_dir_info = res;
2883}
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:2470
const char * name
Definition: config.c:2462
const or_options_t * get_options(void)
Definition: config.c:944
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:3531
char * entry_guards_get_err_str_if_dir_info_missing(int using_mds, int num_present, int num_usable)
Definition: entrynodes.c:4032
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:4362
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:1084
microdesc_t * microdesc_cache_lookup_by_digest256(microdesc_cache_t *cache, const char *d)
Definition: microdesc.c:944
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:1053
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:881
const node_t * router_find_exact_exit_enclave(const char *address, uint16_t port)
Definition: nodelist.c:2285
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:1851
bool node_supports_establish_intro_dos_extension(const node_t *node)
Definition: nodelist.c:1286
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:1830
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:1562
const uint8_t * node_get_rsa_id_digest(const node_t *node)
Definition: nodelist.c:1355
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:2775
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:2549
static char dir_info_status[512]
Definition: nodelist.c:2395
void nodelist_free_all(void)
Definition: nodelist.c:923
void nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
Definition: nodelist.c:2220
void nodelist_refresh_countries(void)
Definition: nodelist.c:2061
STATIC void node_lookup_declared_family(smartlist_t *out, const node_t *node)
Definition: nodelist.c:2152
void router_dir_info_changed(void)
Definition: nodelist.c:2448
const smartlist_t * nodelist_get_list(void)
Definition: nodelist.c:1047
int node_is_good_exit(const node_t *node)
Definition: nodelist.c:794
const node_t * node_get_by_nickname(const char *nickname, unsigned flags)
Definition: nodelist.c:1085
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:1707
const char * node_get_platform(const node_t *node)
Definition: nodelist.c:1731
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:1605
int router_addrs_in_same_network(const tor_addr_t *a1, const tor_addr_t *a2)
Definition: nodelist.c:2071
bool node_supports_accepting_ipv6_extends(const node_t *node, bool need_canonical_ipv6_conn)
Definition: nodelist.c:1324
static const tor_addr_t * node_get_prim_addr_ipv4(const node_t *node)
Definition: nodelist.c:1690
static void update_router_have_minimum_dir_info(void)
Definition: nodelist.c:2792
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:2478
static int microdesc_has_curve25519_onion_key(const microdesc_t *md)
Definition: nodelist.c:1998
void node_get_pref_dirport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1954
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
Definition: nodelist.c:1509
static int node_is_usable(const node_t *node)
Definition: nodelist.c:801
STATIC int node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
Definition: nodelist.c:2105
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:1644
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:1744
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:1207
const char * node_get_nickname(const node_t *node)
Definition: nodelist.c:1459
bool node_supports_conflux(const node_t *node)
Definition: nodelist.c:1346
int node_has_any_descriptor(const node_t *node)
Definition: nodelist.c:1496
void nodelist_purge(void)
Definition: nodelist.c:894
node_t * nodelist_add_microdesc(microdesc_t *md)
Definition: nodelist.c:635
int node_is_dir(const node_t *node)
Definition: nodelist.c:1473
int node_is_unreliable(const node_t *node, int need_uptime, int need_capacity, int need_guard)
Definition: nodelist.c:2313
smartlist_t * nodelist_find_nodes_with_microdesc(const microdesc_t *md)
Definition: nodelist.c:863
bool node_supports_v3_rendezvous_point(const node_t *node)
Definition: nodelist.c:1271
void node_get_prim_dirport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1931
bool node_supports_ed25519_link_authentication(const node_t *node, bool compatible_with_us)
Definition: nodelist.c:1235
bool node_supports_v3_hsdir(const node_t *node)
Definition: nodelist.c:1251
static consensus_path_type_t have_consensus_path
Definition: nodelist.c:2387
int node_ipv6_dir_preferred(const node_t *node)
Definition: nodelist.c:1908
int router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port, int need_uptime)
Definition: nodelist.c:2328
long node_get_declared_uptime(const node_t *node)
Definition: nodelist.c:1721
const char * get_dir_info_status_string(void)
Definition: nodelist.c:2458
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:2438
int node_ed25519_id_matches(const node_t *node, const ed25519_public_key_t *id)
Definition: nodelist.c:1195
static int need_to_update_have_min_dir_info
Definition: nodelist.c:2392
static int have_min_dir_info
Definition: nodelist.c:2384
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 protover_summary_flags_t * node_get_protover_summary_flags(const node_t *node)
Definition: nodelist.c:1213
int node_allows_single_hop_exits(const node_t *node)
Definition: nodelist.c:1577
const curve25519_public_key_t * node_get_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:2025
int node_get_purpose(const node_t *node)
Definition: nodelist.c:1530
void node_get_pref_ipv6_dirport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1969
const ed25519_public_key_t * node_get_ed25519_id(const node_t *node)
Definition: nodelist.c:1150
void nodelist_set_consensus(const networkstatus_t *ns)
Definition: nodelist.c:689
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:809
const node_t * node_get_by_hex_id(const char *hex_id, unsigned flags)
Definition: nodelist.c:1058
void node_set_country(node_t *node)
Definition: nodelist.c:2040
int node_ipv6_or_preferred(const node_t *node)
Definition: nodelist.c:1800
void nodelist_remove_routerinfo(routerinfo_t *ri)
Definition: nodelist.c:823
void nodelist_ensure_freshness(const networkstatus_t *ns)
Definition: nodelist.c:1026
STATIC bool node_has_declared_family(const node_t *node)
Definition: nodelist.c:2132
STATIC int node_nickname_matches(const node_t *node, const char *nickname)
Definition: nodelist.c:2093
void node_get_addr(const node_t *node, tor_addr_t *addr_out)
Definition: nodelist.c:1681
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:2748
void node_get_verbose_nickname(const node_t *node, char *verbose_name_out)
Definition: nodelist.c:1542
bool node_supports_initiating_ipv6_extends(const node_t *node)
Definition: nodelist.c:1303
void node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1866
STATIC int node_family_contains(const node_t *n1, const node_t *n2)
Definition: nodelist.c:2117
int nodes_in_same_family(const node_t *node1, const node_t *node2)
Definition: nodelist.c:2173
static void nodelist_drop_node(node_t *node, int remove_from_ht)
Definition: nodelist.c:838
int node_has_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:2018
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2405
void nodelist_assert_ok(void)
Definition: nodelist.c:949
int node_exit_policy_rejects_all(const node_t *node)
Definition: nodelist.c:1588
bool node_supports_ed25519_hs_intro(const node_t *node)
Definition: nodelist.c:1261
void router_set_status(const char *digest, int up)
Definition: nodelist.c:2349
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:449
@ V3_DIRINFO
Definition: or.h:790
@ BRIDGE_DIRINFO
Definition: or.h:792
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:1768
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:723
routerlist_t * router_get_routerlist(void)
Definition: routerlist.c:898
signed_descriptor_t * router_get_by_descriptor_digest(const char *digest)
Definition: routerlist.c:787
int hex_digest_nickname_decode(const char *hexdigest, char *digest_out, char *nickname_qualifier_char_out, char *nickname_out)
Definition: routerlist.c:687
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.
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:46
uint16_t ipv6_orport
Definition: microdesc_st.h:73
tor_addr_t ipv6_addr
Definition: microdesc_st.h:71
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: microdesc_st.h:67
char digest[DIGEST256_LEN]
Definition: microdesc_st.h:62
unsigned int policy_is_reject_star
Definition: microdesc_st.h:44
struct nodefamily_t * family
Definition: microdesc_st.h:75
struct ed25519_public_key_t * ed25519_identity_pkey
Definition: microdesc_st.h:69
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:94
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:85
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:100
unsigned int ipv6_preferred
Definition: node_st.h:90
unsigned int name_lookup_warned
Definition: node_st.h:80
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:710
unsigned int supports_v3_rendezvous_point
Definition: or.h:734
unsigned int supports_initiating_ipv6_extends
Definition: or.h:701
unsigned int supports_v3_hsdir
Definition: or.h:729
unsigned int supports_ed25519_link_handshake_any
Definition: or.h:715
unsigned int supports_canonical_ipv6_conns
Definition: or.h:705
unsigned int supports_conflux
Definition: or.h:745
unsigned int supports_accepting_ipv6_extends
Definition: or.h:697
unsigned int supports_ed25519_hs_intro
Definition: or.h:720
unsigned int allow_single_hop_exits
Definition: routerinfo_st.h:75
char * platform
Definition: routerinfo_st.h:51
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:30
tor_addr_t ipv4_addr
Definition: routerinfo_st.h:25
protover_summary_flags_t pv
Definition: routerinfo_st.h:96
smartlist_t * declared_family
Definition: routerinfo_st.h:68
struct curve25519_public_key_t * onion_curve25519_pkey
Definition: routerinfo_st.h:46
unsigned int policy_is_reject_star
Definition: routerinfo_st.h:80
uint8_t purpose
unsigned int supports_tunnelled_dir_requests
Definition: routerinfo_st.h:89
char * nickname
Definition: routerinfo_st.h:22
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