#define NODELIST_PRIVATE
#include "core/or/or.h"
#include "app/config/config.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/netstatus.h"
#include "core/or/address_set.h"
#include "core/or/policies.h"
#include "core/or/protover.h"
#include "feature/client/bridges.h"
#include "feature/client/entrynodes.h"
#include "feature/control/control_events.h"
#include "feature/dirauth/process_descs.h"
#include "feature/dirclient/dirclient_modes.h"
#include "feature/hs/hs_client.h"
#include "feature/hs/hs_common.h"
#include "feature/nodelist/describe.h"
#include "feature/nodelist/dirlist.h"
#include "feature/nodelist/microdesc.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/node_select.h"
#include "feature/nodelist/nodefamily.h"
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerlist.h"
#include "feature/nodelist/routerset.h"
#include "feature/nodelist/torcert.h"
#include "feature/rend/rendservice.h"
#include "lib/encoding/binascii.h"
#include "lib/err/backtrace.h"
#include "lib/geoip/geoip.h"
#include "lib/net/address.h"
#include <string.h>
#include "feature/dirauth/authmode.h"
#include "feature/dirclient/dir_server_st.h"
#include "feature/nodelist/microdesc_st.h"
#include "feature/nodelist/networkstatus_st.h"
#include "feature/nodelist/node_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/routerlist_st.h"
#include "feature/nodelist/routerstatus_st.h"
static void nodelist_drop_node(node_t *node, int remove_from_ht);
#define node_free(val) \
FREE_AND_NULL(node_t, node_free_, (val))
static void node_free_(node_t *node);
typedef enum {
USABLE_DESCRIPTOR_ALL = 0U,
USABLE_DESCRIPTOR_EXIT_POLICY = 1U << 0,
USABLE_DESCRIPTOR_EXIT_FLAG = 1U << 1,
USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG = (USABLE_DESCRIPTOR_EXIT_POLICY |
USABLE_DESCRIPTOR_EXIT_FLAG)
} usable_descriptor_t;
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);
static void update_router_have_minimum_dir_info(void);
static double get_frac_paths_needed_for_circs(const or_options_t *options,
const networkstatus_t *ns);
static void node_add_to_address_set(const node_t *node);
typedef struct nodelist_t {
smartlist_t *nodes;
HT_HEAD(nodelist_map, node_t) nodes_by_id;
HT_HEAD(nodelist_ed_map, node_t) nodes_by_ed_id;
address_set_t *node_addrs;
digestmap_t *reentry_set;
time_t live_consensus_valid_after;
} nodelist_t;
static inline unsigned int
node_id_hash(const node_t *node)
{
return (unsigned) siphash24g(node->identity, DIGEST_LEN);
}
static inline unsigned int
node_id_eq(const node_t *node1, const node_t *node2)
{
return tor_memeq(node1->identity, node2->identity, DIGEST_LEN);
}
HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq);
HT_GENERATE2(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
0.6, tor_reallocarray_, tor_free_);
static inline unsigned int
node_ed_id_hash(const node_t *node)
{
return (unsigned) siphash24g(node->ed25519_id.pubkey, ED25519_PUBKEY_LEN);
}
static inline unsigned int
node_ed_id_eq(const node_t *node1, const node_t *node2)
{
return ed25519_pubkey_eq(&node1->ed25519_id, &node2->ed25519_id);
}
HT_PROTOTYPE(nodelist_ed_map, node_t, ed_ht_ent, node_ed_id_hash,
node_ed_id_eq);
HT_GENERATE2(nodelist_ed_map, node_t, ed_ht_ent, node_ed_id_hash,
node_ed_id_eq, 0.6, tor_reallocarray_, tor_free_);
static nodelist_t *the_nodelist=NULL;
static void
init_nodelist(void)
{
if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
HT_INIT(nodelist_ed_map, &the_nodelist->nodes_by_ed_id);
the_nodelist->nodes = smartlist_new();
}
}
MOCK_IMPL(node_t *,
node_get_mutable_by_id,(const char *identity_digest))
{
node_t search, *node;
if (PREDICT_UNLIKELY(the_nodelist == NULL))
return NULL;
memcpy(&search.identity, identity_digest, DIGEST_LEN);
node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
return node;
}
node_t *
node_get_mutable_by_ed25519_id(const ed25519_public_key_t *ed_id)
{
node_t search, *node;
if (PREDICT_UNLIKELY(the_nodelist == NULL))
return NULL;
if (BUG(ed_id == NULL) || BUG(ed25519_public_key_is_zero(ed_id)))
return NULL;
memcpy(&search.ed25519_id, ed_id, sizeof(search.ed25519_id));
node = HT_FIND(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, &search);
return node;
}
MOCK_IMPL(const node_t *,
node_get_by_id,(const char *identity_digest))
{
return node_get_mutable_by_id(identity_digest);
}
MOCK_IMPL(const node_t *,
node_get_by_ed25519_id,(const ed25519_public_key_t *ed_id))
{
return node_get_mutable_by_ed25519_id(ed_id);
}
static node_t *
node_get_or_create(const char *identity_digest)
{
node_t *node;
if ((node = node_get_mutable_by_id(identity_digest)))
return node;
node = tor_malloc_zero(sizeof(node_t));
memcpy(node->identity, identity_digest, DIGEST_LEN);
HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
smartlist_add(the_nodelist->nodes, node);
node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
node->country = -1;
return node;
}
static int
node_remove_from_ed25519_map(node_t *node)
{
tor_assert(the_nodelist);
tor_assert(node);
if (ed25519_public_key_is_zero(&node->ed25519_id)) {
return 0;
}
int rv = 0;
node_t *search =
HT_FIND(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
if (BUG(search != node)) {
goto clear_and_return;
}
search = HT_REMOVE(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
tor_assert(search == node);
rv = 1;
clear_and_return:
memset(&node->ed25519_id, 0, sizeof(node->ed25519_id));
return rv;
}
static void
node_log_dup_ed_id(const node_t *old, const node_t *node, const char *ed_id)
{
char *s;
char *olddesc = tor_strdup(node_describe(old));
tor_asprintf(&s, "Reused ed25519_id %s: old %s new %s", ed_id,
olddesc, node_describe(node));
log_backtrace(LOG_NOTICE, LD_DIR, s);
tor_free(olddesc);
tor_free(s);
}
static int
node_add_to_ed25519_map(node_t *node)
{
tor_assert(the_nodelist);
tor_assert(node);
if (! ed25519_public_key_is_zero(&node->ed25519_id)) {
return 0;
}
const ed25519_public_key_t *key = node_get_ed25519_id(node);
if (!key) {
return 0;
}
node_t *old;
memcpy(&node->ed25519_id, key, sizeof(node->ed25519_id));
old = HT_FIND(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
if (old) {
char ed_id[BASE32_BUFSIZE(sizeof(key->pubkey))];
base32_encode(ed_id, sizeof(ed_id), (const char *)key->pubkey,
sizeof(key->pubkey));
if (BUG(old == node)) {
log_err(LD_BUG,
"Unexpectedly found deleted node with ed25519_id %s", ed_id);
} else {
node_log_dup_ed_id(old, node, ed_id);
memset(&node->ed25519_id, 0, sizeof(node->ed25519_id));
}
return 0;
}
HT_INSERT(nodelist_ed_map, &the_nodelist->nodes_by_ed_id, node);
return 1;
}
STATIC void
node_set_hsdir_index(node_t *node, const networkstatus_t *ns)
{
time_t now = approx_time();
const ed25519_public_key_t *node_identity_pk;
uint8_t *fetch_srv = NULL, *store_first_srv = NULL, *store_second_srv = NULL;
uint64_t next_time_period_num, current_time_period_num;
uint64_t fetch_tp, store_first_tp, store_second_tp;
tor_assert(node);
tor_assert(ns);
if (!networkstatus_consensus_reasonably_live(ns, now)) {
static struct ratelim_t live_consensus_ratelim = RATELIM_INIT(30 * 60);
log_fn_ratelim(&live_consensus_ratelim, LOG_INFO, LD_GENERAL,
"Not setting hsdir index with a non-live consensus.");
goto done;
}
node_identity_pk = node_get_ed25519_id(node);
if (node_identity_pk == NULL) {
log_debug(LD_GENERAL, "ed25519 identity public key not found when "
"trying to build the hsdir indexes for node %s",
node_describe(node));
goto done;
}
current_time_period_num = hs_get_time_period_num(0);
next_time_period_num = hs_get_next_time_period_num(0);
fetch_tp = current_time_period_num;
if (hs_in_period_between_tp_and_srv(ns, now)) {
fetch_srv = hs_get_current_srv(fetch_tp, ns);
store_first_tp = hs_get_previous_time_period_num(0);
store_second_tp = current_time_period_num;
} else {
fetch_srv = hs_get_previous_srv(fetch_tp, ns);
store_first_tp = current_time_period_num;
store_second_tp = next_time_period_num;
}
store_first_srv = hs_get_previous_srv(store_first_tp, ns);
store_second_srv = hs_get_current_srv(store_second_tp, ns);
hs_build_hsdir_index(node_identity_pk, fetch_srv, fetch_tp,
node->hsdir_index.fetch);
if (!hs_in_period_between_tp_and_srv(ns, now)) {
memcpy(node->hsdir_index.store_first, node->hsdir_index.fetch,
sizeof(node->hsdir_index.store_first));
} else {
hs_build_hsdir_index(node_identity_pk, store_first_srv, store_first_tp,
node->hsdir_index.store_first);
}
if (hs_in_period_between_tp_and_srv(ns, now)) {
memcpy(node->hsdir_index.store_second, node->hsdir_index.fetch,
sizeof(node->hsdir_index.store_second));
} else {
hs_build_hsdir_index(node_identity_pk, store_second_srv, store_second_tp,
node->hsdir_index.store_second);
}
done:
tor_free(fetch_srv);
tor_free(store_first_srv);
tor_free(store_second_srv);
return;
}
static void
node_addrs_changed(node_t *node)
{
node->last_reachable = node->last_reachable6 = 0;
node->country = -1;
}
static void
node_add_to_address_set(const node_t *node)
{
if (!the_nodelist ||
!the_nodelist->node_addrs || !the_nodelist->reentry_set)
return;
if (node->rs) {
if (!tor_addr_is_null(&node->rs->ipv4_addr))
nodelist_add_addr_to_address_set(&node->rs->ipv4_addr,
node->rs->ipv4_orport, 0);
if (!tor_addr_is_null(&node->rs->ipv6_addr))
nodelist_add_addr_to_address_set(&node->rs->ipv6_addr,
node->rs->ipv6_orport, 0);
}
if (node->ri) {
if (!tor_addr_is_null(&node->ri->ipv4_addr))
nodelist_add_addr_to_address_set(&node->ri->ipv4_addr,
node->ri->ipv4_orport, 0);
if (!tor_addr_is_null(&node->ri->ipv6_addr))
nodelist_add_addr_to_address_set(&node->ri->ipv6_addr,
node->ri->ipv6_orport, 0);
}
if (node->md) {
if (!tor_addr_is_null(&node->md->ipv6_addr))
nodelist_add_addr_to_address_set(&node->md->ipv6_addr,
node->md->ipv6_orport, 0);
}
}
static char *
build_addr_port_item(const tor_addr_t *addr, const uint16_t port)
{
static char data[DIGEST_LEN];
memset(data, 0, sizeof(data));
switch (tor_addr_family(addr)) {
case AF_INET:
memcpy(data, &addr->addr.in_addr.s_addr, 4);
break;
case AF_INET6:
memcpy(data, &addr->addr.in6_addr.s6_addr, 16);
break;
case AF_UNSPEC:
break;
default:
tor_fragile_assert();
}
memcpy(data + 16, &port, sizeof(port));
return data;
}
void
nodelist_add_addr_to_address_set(const tor_addr_t *addr,
uint16_t or_port, uint16_t dir_port)
{
if (BUG(!addr) || tor_addr_is_null(addr) ||
(!tor_addr_is_v4(addr) && !tor_addr_is_v6(addr)) ||
!the_nodelist || !the_nodelist->node_addrs ||
!the_nodelist->reentry_set) {
return;
}
address_set_add(the_nodelist->node_addrs, addr);
if (or_port != 0) {
digestmap_set(the_nodelist->reentry_set,
build_addr_port_item(addr, or_port), (void*) 1);
}
if (dir_port != 0) {
digestmap_set(the_nodelist->reentry_set,
build_addr_port_item(addr, dir_port), (void*) 1);
}
}
int
nodelist_probably_contains_address(const tor_addr_t *addr)
{
if (BUG(!addr))
return 0;
if (!the_nodelist || !the_nodelist->node_addrs)
return 0;
return address_set_probably_contains(the_nodelist->node_addrs, addr);
}
bool
nodelist_reentry_contains(const tor_addr_t *addr, uint16_t port)
{
if (BUG(!addr) || BUG(!port))
return false;
if (!the_nodelist || !the_nodelist->reentry_set)
return false;
return digestmap_get(the_nodelist->reentry_set,
build_addr_port_item(addr, port)) != NULL;
}
node_t *
nodelist_set_routerinfo(routerinfo_t *ri, routerinfo_t **ri_old_out)
{
node_t *node;
const char *id_digest;
int had_router = 0;
tor_assert(ri);
init_nodelist();
id_digest = ri->cache_info.identity_digest;
node = node_get_or_create(id_digest);
node_remove_from_ed25519_map(node);
if (node->ri) {
if (!routers_have_same_or_addrs(node->ri, ri)) {
node_addrs_changed(node);
}
had_router = 1;
if (ri_old_out)
*ri_old_out = node->ri;
} else {
if (ri_old_out)
*ri_old_out = NULL;
}
node->ri = ri;
node_add_to_ed25519_map(node);
if (node->country == -1)
node_set_country(node);
if (authdir_mode(get_options()) && !had_router) {
const char *discard=NULL;
uint32_t status = dirserv_router_get_status(ri, &discard, LOG_INFO);
dirserv_set_node_flags_from_authoritative_status(node, status);
}
if (node->rs && node->rs->pv.supports_v3_hsdir) {
node_set_hsdir_index(node,
networkstatus_get_latest_consensus());
}
node_add_to_address_set(node);
return node;
}
node_t *
nodelist_add_microdesc(microdesc_t *md)
{
networkstatus_t *ns =
networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
const routerstatus_t *rs;
node_t *node;
if (ns == NULL)
return NULL;
init_nodelist();
rs = router_get_consensus_status_by_descriptor_digest(ns, md->digest);
if (rs == NULL)
return NULL;
node = node_get_mutable_by_id(rs->identity_digest);
if (node == NULL)
return NULL;
node_remove_from_ed25519_map(node);
if (node->md)
node->md->held_by_nodes--;
node->md = md;
md->held_by_nodes++;
if (rs->pv.supports_v3_hsdir) {
node_set_hsdir_index(node, ns);
}
node_add_to_ed25519_map(node);
node_add_to_address_set(node);
return node;
}
#define ESTIMATED_ADDRESS_PER_NODE 2
MOCK_IMPL(int,
get_estimated_address_per_node, (void))
{
return ESTIMATED_ADDRESS_PER_NODE;
}
void
nodelist_set_consensus(const networkstatus_t *ns)
{
const or_options_t *options = get_options();
int authdir = authdir_mode_v3(options);
init_nodelist();
if (ns->flavor == FLAV_MICRODESC)
(void) get_microdesc_cache();
SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
node->rs = NULL);
int estimated_addresses = smartlist_len(ns->routerstatus_list) *
get_estimated_address_per_node();
estimated_addresses += (get_n_authorities(V3_DIRINFO | BRIDGE_DIRINFO) *
get_estimated_address_per_node());
address_set_free(the_nodelist->node_addrs);
the_nodelist->node_addrs = address_set_new(estimated_addresses);
digestmap_free(the_nodelist->reentry_set, NULL);
the_nodelist->reentry_set = digestmap_new();
SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
node_t *node = node_get_or_create(rs->identity_digest);
node->rs = rs;
if (ns->flavor == FLAV_MICRODESC) {
if (node->md == NULL ||
tor_memneq(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
node_remove_from_ed25519_map(node);
if (node->md)
node->md->held_by_nodes--;
node->md = microdesc_cache_lookup_by_digest256(NULL,
rs->descriptor_digest);
if (node->md)
node->md->held_by_nodes++;
node_add_to_ed25519_map(node);
}
}
if (rs->pv.supports_v3_hsdir) {
node_set_hsdir_index(node, ns);
}
node_set_country(node);
if (!authdir) {
node->is_valid = rs->is_valid;
node->is_running = rs->is_flagged_running;
node->is_fast = rs->is_fast;
node->is_stable = rs->is_stable;
node->is_possible_guard = rs->is_possible_guard;
node->is_exit = rs->is_exit;
node->is_bad_exit = rs->is_bad_exit;
node->is_hs_dir = rs->is_hs_dir;
node->ipv6_preferred = 0;
if (reachable_addr_prefer_ipv6_orport(options) &&
(tor_addr_is_null(&rs->ipv6_addr) == 0 ||
(node->md && tor_addr_is_null(&node->md->ipv6_addr) == 0)))
node->ipv6_preferred = 1;
}
} SMARTLIST_FOREACH_END(rs);
nodelist_purge();
SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
node_add_to_address_set(node);
} SMARTLIST_FOREACH_END(node);
dirlist_add_trusted_dir_addresses();
if (! authdir) {
SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
if (!node->rs) {
tor_assert(node->ri);
if (node->ri->purpose == ROUTER_PURPOSE_GENERAL) {
node->is_valid = node->is_running = node->is_hs_dir =
node->is_fast = node->is_stable =
node->is_possible_guard = node->is_exit =
node->is_bad_exit = node->ipv6_preferred = 0;
}
}
} SMARTLIST_FOREACH_END(node);
}
if (networkstatus_is_live(ns, approx_time())) {
the_nodelist->live_consensus_valid_after = ns->valid_after;
}
}
int
node_is_good_exit(const node_t *node)
{
return node->is_exit && ! node->is_bad_exit;
}
static inline int
node_is_usable(const node_t *node)
{
return (node->rs) || (node->ri);
}
void
nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
{
node_t *node = node_get_mutable_by_id(identity_digest);
if (node && node->md == md) {
node->md = NULL;
md->held_by_nodes--;
if (! node_get_ed25519_id(node)) {
node_remove_from_ed25519_map(node);
}
}
}
void
nodelist_remove_routerinfo(routerinfo_t *ri)
{
node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
if (node && node->ri == ri) {
node->ri = NULL;
if (! node_is_usable(node)) {
nodelist_drop_node(node, 1);
node_free(node);
}
}
}
static void
nodelist_drop_node(node_t *node, int remove_from_ht)
{
node_t *tmp;
int idx;
if (remove_from_ht) {
tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
tor_assert(tmp == node);
}
node_remove_from_ed25519_map(node);
idx = node->nodelist_idx;
tor_assert(idx >= 0);
tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
smartlist_del(the_nodelist->nodes, idx);
if (idx < smartlist_len(the_nodelist->nodes)) {
tmp = smartlist_get(the_nodelist->nodes, idx);
tmp->nodelist_idx = idx;
}
node->nodelist_idx = -1;
}
smartlist_t *
nodelist_find_nodes_with_microdesc(const microdesc_t *md)
{
smartlist_t *result = smartlist_new();
if (the_nodelist == NULL)
return result;
SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
if (node->md == md) {
smartlist_add(result, node);
}
} SMARTLIST_FOREACH_END(node);
return result;
}
static void
node_free_(node_t *node)
{
if (!node)
return;
if (node->md)
node->md->held_by_nodes--;
tor_assert(node->nodelist_idx == -1);
tor_free(node);
}
void
nodelist_purge(void)
{
node_t **iter;
if (PREDICT_UNLIKELY(the_nodelist == NULL))
return;
for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
node_t *node = *iter;
if (node->md && !node->rs) {
node->md->held_by_nodes--;
node->md = NULL;
}
if (node_is_usable(node)) {
iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
} else {
iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
nodelist_drop_node(node, 0);
node_free(node);
}
}
nodelist_assert_ok();
}
void
nodelist_free_all(void)
{
if (PREDICT_UNLIKELY(the_nodelist == NULL))
return;
HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
HT_CLEAR(nodelist_ed_map, &the_nodelist->nodes_by_ed_id);
SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
node->nodelist_idx = -1;
node_free(node);
} SMARTLIST_FOREACH_END(node);
smartlist_free(the_nodelist->nodes);
address_set_free(the_nodelist->node_addrs);
the_nodelist->node_addrs = NULL;
digestmap_free(the_nodelist->reentry_set, NULL);
the_nodelist->reentry_set = NULL;
tor_free(the_nodelist);
}
void
nodelist_assert_ok(void)
{
routerlist_t *rl = router_get_routerlist();
networkstatus_t *ns = networkstatus_get_latest_consensus();
digestmap_t *dm;
if (!the_nodelist)
return;
dm = digestmap_new();
if (rl) {
SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
tor_assert(node && node->ri == ri);
tor_assert(fast_memeq(ri->cache_info.identity_digest,
node->identity, DIGEST_LEN));
tor_assert(! digestmap_get(dm, node->identity));
digestmap_set(dm, node->identity, (void*)node);
} SMARTLIST_FOREACH_END(ri);
}
if (ns) {
SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
const node_t *node = node_get_by_id(rs->identity_digest);
tor_assert(node && node->rs == rs);
tor_assert(fast_memeq(rs->identity_digest, node->identity, DIGEST_LEN));
digestmap_set(dm, node->identity, (void*)node);
if (ns->flavor == FLAV_MICRODESC) {
microdesc_t *md =
microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
tor_assert(md == node->md);
if (md)
tor_assert(md->held_by_nodes >= 1);
}
} SMARTLIST_FOREACH_END(rs);
}
SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
tor_assert(digestmap_get(dm, node->identity) != NULL);
tor_assert(node_sl_idx == node->nodelist_idx);
} SMARTLIST_FOREACH_END(node);
SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
if (!ed25519_public_key_is_zero(&node->ed25519_id)) {
tor_assert(node == node_get_by_ed25519_id(&node->ed25519_id));
}
} SMARTLIST_FOREACH_END(node);
node_t **idx;
HT_FOREACH(idx, nodelist_ed_map, &the_nodelist->nodes_by_ed_id) {
node_t *node = *idx;
tor_assert(node == node_get_by_ed25519_id(&node->ed25519_id));
}
tor_assert((long)smartlist_len(the_nodelist->nodes) ==
(long)HT_SIZE(&the_nodelist->nodes_by_id));
tor_assert((long)smartlist_len(the_nodelist->nodes) >=
(long)HT_SIZE(&the_nodelist->nodes_by_ed_id));
digestmap_free(dm, NULL);
}
void
nodelist_ensure_freshness(const networkstatus_t *ns)
{
tor_assert(ns);
if (!the_nodelist) {
return;
}
if (the_nodelist->live_consensus_valid_after != ns->valid_after) {
log_info(LD_GENERAL, "Nodelist was not fresh: rebuilding. (%d / %d)",
(int) the_nodelist->live_consensus_valid_after,
(int) ns->valid_after);
nodelist_set_consensus(ns);
}
}
MOCK_IMPL(const smartlist_t *,
nodelist_get_list,(void))
{
init_nodelist();
return the_nodelist->nodes;
}
const node_t *
node_get_by_hex_id(const char *hex_id, unsigned flags)
{
char digest_buf[DIGEST_LEN];
char nn_buf[MAX_NICKNAME_LEN+1];
char nn_char='\0';
(void) flags;
if (hex_digest_nickname_decode(hex_id, digest_buf, &nn_char, nn_buf)==0) {
const node_t *node = node_get_by_id(digest_buf);
if (!node)
return NULL;
if (nn_char == '=') {
return NULL;
}
return node;
}
return NULL;
}
MOCK_IMPL(const node_t *,
node_get_by_nickname,(const char *nickname, unsigned flags))
{
const int warn_if_unnamed = !(flags & NNF_NO_WARN_UNNAMED);
if (!the_nodelist)
return NULL;
{
const node_t *node;
if ((node = node_get_by_hex_id(nickname, flags)) != NULL)
return node;
}
if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
return NULL;
{
smartlist_t *matches = smartlist_new();
const node_t *choice = NULL;
SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
if (!strcasecmp(node_get_nickname(node), nickname))
smartlist_add(matches, node);
} SMARTLIST_FOREACH_END(node);
if (smartlist_len(matches)>1 && warn_if_unnamed) {
int any_unwarned = 0;
SMARTLIST_FOREACH_BEGIN(matches, node_t *, node) {
if (!node->name_lookup_warned) {
node->name_lookup_warned = 1;
any_unwarned = 1;
}
} SMARTLIST_FOREACH_END(node);
if (any_unwarned) {
log_warn(LD_CONFIG, "There are multiple matches for the name %s. "
"Choosing one arbitrarily.", nickname);
}
} else if (smartlist_len(matches)==1 && warn_if_unnamed) {
char fp[HEX_DIGEST_LEN+1];
node_t *node = smartlist_get(matches, 0);
if (! node->name_lookup_warned) {
base16_encode(fp, sizeof(fp), node->identity, DIGEST_LEN);
log_warn(LD_CONFIG,
"You specified a relay \"%s\" by name, but nicknames can be "
"used by any relay, not just the one you meant. "
"To make sure you get the same relay in the future, refer "
"to it by key, as \"$%s\".", nickname, fp);
node->name_lookup_warned = 1;
}
}
if (smartlist_len(matches))
choice = smartlist_get(matches, 0);
smartlist_free(matches);
return choice;
}
}
MOCK_IMPL(const ed25519_public_key_t *,
node_get_ed25519_id,(const node_t *node))
{
const ed25519_public_key_t *ri_pk = NULL;
const ed25519_public_key_t *md_pk = NULL;
if (node->ri) {
if (node->ri->cache_info.signing_key_cert) {
ri_pk = &node->ri->cache_info.signing_key_cert->signing_key;
if (BUG(ed25519_public_key_is_zero(ri_pk)))
ri_pk = NULL;
}
}
if (node->md) {
if (node->md->ed25519_identity_pkey) {
md_pk = node->md->ed25519_identity_pkey;
if (BUG(ed25519_public_key_is_zero(md_pk)))
md_pk = NULL;
}
}
if (ri_pk && md_pk) {
if (ed25519_pubkey_eq(ri_pk, md_pk)) {
return ri_pk;
} else {
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
"Inconsistent ed25519 identities in the nodelist");
return NULL;
}
} else if (ri_pk) {
return ri_pk;
} else {
return md_pk;
}
}
int
node_ed25519_id_matches(const node_t *node, const ed25519_public_key_t *id)
{
const ed25519_public_key_t *node_id = node_get_ed25519_id(node);
if (node_id == NULL || ed25519_public_key_is_zero(node_id)) {
return id == NULL || ed25519_public_key_is_zero(id);
} else {
return id && ed25519_pubkey_eq(node_id, id);
}
}
static const protover_summary_flags_t zero_protover_flags = {
0,0,0,0,0,0,0,0,0,0,0,0
};
static const protover_summary_flags_t *
node_get_protover_summary_flags(const node_t *node)
{
if (node->rs) {
return &node->rs->pv;
} else if (node->ri) {
return &node->ri->pv;
} else {
tor_assert_nonfatal_unreached_once();
return &zero_protover_flags;
}
}
MOCK_IMPL(bool,
node_supports_ed25519_link_authentication,(const node_t *node,
bool compatible_with_us))
{
if (! node_get_ed25519_id(node))
return 0;
const protover_summary_flags_t *pv = node_get_protover_summary_flags(node);
if (compatible_with_us)
return pv->supports_ed25519_link_handshake_compat;
else
return pv->supports_ed25519_link_handshake_any;
}
bool
node_supports_v3_hsdir(const node_t *node)
{
tor_assert(node);
return node_get_protover_summary_flags(node)->supports_v3_hsdir;
}
bool
node_supports_ed25519_hs_intro(const node_t *node)
{
tor_assert(node);
return node_get_protover_summary_flags(node)->supports_ed25519_hs_intro;
}
bool
node_supports_v3_rendezvous_point(const node_t *node)
{
tor_assert(node);
if (!node_get_curve25519_onion_key(node)) {
return 0;
}
return node_get_protover_summary_flags(node)->supports_v3_rendezvous_point;
}
bool
node_supports_establish_intro_dos_extension(const node_t *node)
{
tor_assert(node);
return node_get_protover_summary_flags(node)->
supports_establish_intro_dos_extension;
}
bool
node_supports_initiating_ipv6_extends(const node_t *node)
{
tor_assert(node);
if (!node_has_ipv6_orport(node)) {
return 0;
}
return
node_get_protover_summary_flags(node)->supports_initiating_ipv6_extends;
}
bool
node_supports_accepting_ipv6_extends(const node_t *node,
bool need_canonical_ipv6_conn)
{
tor_assert(node);
if (!node_has_ipv6_orport(node)) {
return 0;
}
if (need_canonical_ipv6_conn) {
return
node_get_protover_summary_flags(node)->supports_canonical_ipv6_conns;
} else {
return
node_get_protover_summary_flags(node)->supports_accepting_ipv6_extends;
}
}
const uint8_t *
node_get_rsa_id_digest(const node_t *node)
{
tor_assert(node);
return (const uint8_t*)node->identity;
}
MOCK_IMPL(smartlist_t *,
node_get_link_specifier_smartlist,(const node_t *node, bool direct_conn))
{
link_specifier_t *ls;
tor_addr_port_t ap;
smartlist_t *lspecs = smartlist_new();
if (!node)
return lspecs;
node_get_prim_orport(node, &ap);
if (BUG(!tor_addr_is_v4(&ap.addr)) ||
BUG(!tor_addr_port_is_valid_ap(&ap, 0))) {
return lspecs;
}
ls = link_specifier_new();
link_specifier_set_ls_type(ls, LS_IPV4);
link_specifier_set_un_ipv4_addr(ls, tor_addr_to_ipv4h(&ap.addr));
link_specifier_set_un_ipv4_port(ls, ap.port);
link_specifier_set_ls_len(ls, sizeof(ap.addr.addr.in_addr) +
sizeof(ap.port));
smartlist_add(lspecs, ls);
ls = link_specifier_new();
link_specifier_set_ls_type(ls, LS_LEGACY_ID);
memcpy(link_specifier_getarray_un_legacy_id(ls), node->identity,
link_specifier_getlen_un_legacy_id(ls));
link_specifier_set_ls_len(ls, link_specifier_getlen_un_legacy_id(ls));
smartlist_add(lspecs, ls);
if (!ed25519_public_key_is_zero(&node->ed25519_id) &&
node_supports_ed25519_link_authentication(node, direct_conn)) {
ls = link_specifier_new();
link_specifier_set_ls_type(ls, LS_ED25519_ID);
memcpy(link_specifier_getarray_un_ed25519_id(ls), &node->ed25519_id,
link_specifier_getlen_un_ed25519_id(ls));
link_specifier_set_ls_len(ls, link_specifier_getlen_un_ed25519_id(ls));
smartlist_add(lspecs, ls);
}
if (node_has_ipv6_orport(node)) {
ls = link_specifier_new();
node_get_pref_ipv6_orport(node, &ap);
link_specifier_set_ls_type(ls, LS_IPV6);
size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
const uint8_t *in6_addr = tor_addr_to_in6_addr8(&ap.addr);
uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
memcpy(ipv6_array, in6_addr, addr_len);
link_specifier_set_un_ipv6_port(ls, ap.port);
link_specifier_set_ls_len(ls, addr_len + sizeof(ap.port));
smartlist_add(lspecs, ls);
}
return lspecs;
}
void
link_specifier_smartlist_free_(smartlist_t *ls_list)
{
if (!ls_list)
return;
SMARTLIST_FOREACH(ls_list, link_specifier_t *, lspec,
link_specifier_free(lspec));
smartlist_free(ls_list);
}
const char *
node_get_nickname(const node_t *node)
{
tor_assert(node);
if (node->rs)
return node->rs->nickname;
else if (node->ri)
return node->ri->nickname;
else
return NULL;
}
int
node_is_dir(const node_t *node)
{
if (node->rs) {
routerstatus_t * rs = node->rs;
return rs->is_v2_dir;
} else if (node->ri) {
routerinfo_t * ri = node->ri;
return ri->supports_tunnelled_dir_requests;
} else {
return 0;
}
}
int
node_has_any_descriptor(const node_t *node)
{
return (node->ri ||
(node->rs && node->md));
}
int
node_has_preferred_descriptor(const node_t *node,
int for_direct_connect)
{
const int is_bridge = node_is_a_configured_bridge(node);
const int we_use_mds = we_use_microdescriptors_for_circuits(get_options());
if ((is_bridge && for_direct_connect) || !we_use_mds) {
if (!node->ri)
return 0;
} else {
if (node->rs == NULL || node->md == NULL)
return 0;
}
return 1;
}
int
node_get_purpose(const node_t *node)
{
if (node->ri)
return node->ri->purpose;
else
return ROUTER_PURPOSE_GENERAL;
}
void
node_get_verbose_nickname(const node_t *node,
char *verbose_name_out)
{
const char *nickname = node_get_nickname(node);
verbose_name_out[0] = '$';
base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, node->identity,
DIGEST_LEN);
if (!nickname)
return;
verbose_name_out[1+HEX_DIGEST_LEN] = '~';
strlcpy(verbose_name_out+1+HEX_DIGEST_LEN+1, nickname, MAX_NICKNAME_LEN+1);
}
void
node_get_verbose_nickname_by_id(const char *id_digest,
char *verbose_name_out)
{
const node_t *node = node_get_by_id(id_digest);
if (!node) {
verbose_name_out[0] = '$';
base16_encode(verbose_name_out+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
} else {
node_get_verbose_nickname(node, verbose_name_out);
}
}
int
node_allows_single_hop_exits(const node_t *node)
{
if (node && node->ri)
return node->ri->allow_single_hop_exits;
else
return 0;
}
int
node_exit_policy_rejects_all(const node_t *node)
{
if (node->rejects_all)
return 1;
if (node->ri)
return node->ri->policy_is_reject_star;
else if (node->md)
return node->md->policy_is_reject_star;
else
return 1;
}
int
node_exit_policy_is_exact(const node_t *node, sa_family_t family)
{
if (family == AF_UNSPEC) {
return 1;
} else if (family == AF_INET) {
return node->ri != NULL;
} else if (family == AF_INET6) {
return 0;
}
tor_fragile_assert();
return 1;
}
#define SL_ADD_NEW_AP(r, addr_field, port_field, sl, valid) \
STMT_BEGIN \
if (tor_addr_port_is_valid(&(r)->addr_field, (r)->port_field, 0)) { \
valid = 1; \
tor_addr_port_t *ap = tor_addr_port_new(&(r)->addr_field, \
(r)->port_field); \
smartlist_add((sl), ap); \
} \
STMT_END
smartlist_t *
node_get_all_orports(const node_t *node)
{
smartlist_t *sl = smartlist_new();
int valid = 0;
if (node->ri != NULL) {
SL_ADD_NEW_AP(node->ri, ipv4_addr, ipv4_orport, sl, valid);
}
if (!valid && node->rs != NULL) {
SL_ADD_NEW_AP(node->rs, ipv4_addr, ipv4_orport, sl, valid);
}
valid = 0;
if (node->ri != NULL) {
SL_ADD_NEW_AP(node->ri, ipv6_addr, ipv6_orport, sl, valid);
}
if (!valid && node->rs != NULL) {
SL_ADD_NEW_AP(node->rs, ipv6_addr, ipv6_orport, sl, valid);
}
if (!valid && node->md != NULL) {
SL_ADD_NEW_AP(node->md, ipv6_addr, ipv6_orport, sl, valid);
}
return sl;
}
#undef SL_ADD_NEW_AP
void
node_get_addr(const node_t *node, tor_addr_t *addr_out)
{
tor_addr_port_t ap;
node_get_prim_orport(node, &ap);
tor_addr_copy(addr_out, &ap.addr);
}
static const tor_addr_t *
node_get_prim_addr_ipv4(const node_t *node)
{
if (node->ri && tor_addr_is_valid(&node->ri->ipv4_addr, 0)) {
return &node->ri->ipv4_addr;
} else if (node->rs && tor_addr_is_valid(&node->rs->ipv4_addr, 0)) {
return &node->rs->ipv4_addr;
}
return NULL;
}
void
node_get_address_string(const node_t *node, char *buf, size_t len)
{
const tor_addr_t *ipv4_addr = node_get_prim_addr_ipv4(node);
if (ipv4_addr) {
tor_addr_to_str(buf, ipv4_addr, len, 0);
} else if (len > 0) {
buf[0] = '\0';
}
}
long
node_get_declared_uptime(const node_t *node)
{
if (node->ri)
return node->ri->uptime;
else
return -1;
}
const char *
node_get_platform(const node_t *node)
{
if (node->ri)
return node->ri->platform;
else
return NULL;
}
int
node_is_me(const node_t *node)
{
return router_digest_is_me(node->identity);
}
int
node_has_ipv6_addr(const node_t *node)
{
if (node->ri && tor_addr_is_valid(&node->ri->ipv6_addr, 0))
return 1;
if (node->rs && tor_addr_is_valid(&node->rs->ipv6_addr, 0))
return 1;
if (node->md && tor_addr_is_valid(&node->md->ipv6_addr, 0))
return 1;
return 0;
}
int
node_has_ipv6_orport(const node_t *node)
{
tor_addr_port_t ipv6_orport;
node_get_pref_ipv6_orport(node, &ipv6_orport);
return tor_addr_port_is_valid_ap(&ipv6_orport, 0);
}
int
node_has_ipv6_dirport(const node_t *node)
{
tor_addr_port_t ipv6_dirport;
node_get_pref_ipv6_dirport(node, &ipv6_dirport);
return tor_addr_port_is_valid_ap(&ipv6_dirport, 0);
}
int
node_ipv6_or_preferred(const node_t *node)
{
const or_options_t *options = get_options();
tor_addr_port_t ipv4_addr;
node_assert_ok(node);
node_get_prim_orport(node, &ipv4_addr);
if (!reachable_addr_use_ipv6(options)) {
return 0;
} else if (node->ipv6_preferred ||
!tor_addr_port_is_valid_ap(&ipv4_addr, 0)) {
return node_has_ipv6_orport(node);
}
return 0;
}
#define RETURN_IPV4_AP(r, port_field, ap_out) \
STMT_BEGIN \
if (r && tor_addr_port_is_valid(&(r)->ipv4_addr, (r)->port_field, 0)) { \
tor_addr_copy(&(ap_out)->addr, &(r)->ipv4_addr); \
(ap_out)->port = (r)->port_field; \
} \
STMT_END
void
node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
{
node_assert_ok(node);
tor_assert(ap_out);
tor_addr_make_null(&ap_out->addr, AF_INET);
ap_out->port = 0;
RETURN_IPV4_AP(node->ri, ipv4_orport, ap_out);
RETURN_IPV4_AP(node->rs, ipv4_orport, ap_out);
}
void
node_get_pref_orport(const node_t *node, tor_addr_port_t *ap_out)
{
tor_assert(ap_out);
if (node_ipv6_or_preferred(node)) {
node_get_pref_ipv6_orport(node, ap_out);
} else {
node_get_prim_orport(node, ap_out);
}
}
void
node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
{
node_assert_ok(node);
tor_assert(ap_out);
memset(ap_out, 0, sizeof(*ap_out));
if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
node->ri->ipv6_orport, 0)) {
tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
ap_out->port = node->ri->ipv6_orport;
} else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
node->rs->ipv6_orport, 0)) {
tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
ap_out->port = node->rs->ipv6_orport;
} else if (node->md && tor_addr_port_is_valid(&node->md->ipv6_addr,
node->md->ipv6_orport, 0)) {
tor_addr_copy(&ap_out->addr, &node->md->ipv6_addr);
ap_out->port = node->md->ipv6_orport;
} else {
tor_addr_make_null(&ap_out->addr, AF_INET6);
ap_out->port = 0;
}
}
int
node_ipv6_dir_preferred(const node_t *node)
{
const or_options_t *options = get_options();
tor_addr_port_t ipv4_addr;
node_assert_ok(node);
node_get_prim_dirport(node, &ipv4_addr);
if (!reachable_addr_use_ipv6(options)) {
return 0;
} else if (!tor_addr_port_is_valid_ap(&ipv4_addr, 0)
|| reachable_addr_prefer_ipv6_dirport(get_options())) {
return node_has_ipv6_dirport(node);
}
return 0;
}
void
node_get_prim_dirport(const node_t *node, tor_addr_port_t *ap_out)
{
node_assert_ok(node);
tor_assert(ap_out);
tor_addr_make_null(&ap_out->addr, AF_INET);
ap_out->port = 0;
RETURN_IPV4_AP(node->ri, ipv4_dirport, ap_out);
RETURN_IPV4_AP(node->rs, ipv4_dirport, ap_out);
}
#undef RETURN_IPV4_AP
void
node_get_pref_dirport(const node_t *node, tor_addr_port_t *ap_out)
{
tor_assert(ap_out);
if (node_ipv6_dir_preferred(node)) {
node_get_pref_ipv6_dirport(node, ap_out);
} else {
node_get_prim_dirport(node, ap_out);
}
}
void
node_get_pref_ipv6_dirport(const node_t *node, tor_addr_port_t *ap_out)
{
node_assert_ok(node);
tor_assert(ap_out);
if (node->ri && tor_addr_port_is_valid(&node->ri->ipv6_addr,
node->ri->ipv4_dirport, 0)) {
tor_addr_copy(&ap_out->addr, &node->ri->ipv6_addr);
ap_out->port = node->ri->ipv4_dirport;
} else if (node->rs && tor_addr_port_is_valid(&node->rs->ipv6_addr,
node->rs->ipv4_dirport, 0)) {
tor_addr_copy(&ap_out->addr, &node->rs->ipv6_addr);
ap_out->port = node->rs->ipv4_dirport;
} else {
tor_addr_make_null(&ap_out->addr, AF_INET6);
ap_out->port = 0;
}
}
static int
microdesc_has_curve25519_onion_key(const microdesc_t *md)
{
if (!md) {
return 0;
}
if (!md->onion_curve25519_pkey) {
return 0;
}
if (fast_mem_is_zero((const char*)md->onion_curve25519_pkey->public_key,
CURVE25519_PUBKEY_LEN)) {
return 0;
}
return 1;
}
int
node_has_curve25519_onion_key(const node_t *node)
{
return node_get_curve25519_onion_key(node) != NULL;
}
const curve25519_public_key_t *
node_get_curve25519_onion_key(const node_t *node)
{
if (!node)
return NULL;
if (routerinfo_has_curve25519_onion_key(node->ri))
return node->ri->onion_curve25519_pkey;
else if (microdesc_has_curve25519_onion_key(node->md))
return node->md->onion_curve25519_pkey;
else
return NULL;
}
crypto_pk_t *
node_get_rsa_onion_key(const node_t *node)
{
crypto_pk_t *pk = NULL;
const char *onion_pkey;
size_t onion_pkey_len;
if (!node) {
goto end;
}
if (node->ri) {
onion_pkey = node->ri->onion_pkey;
onion_pkey_len = node->ri->onion_pkey_len;
} else if (node->rs && node->md) {
onion_pkey = node->md->onion_pkey;
onion_pkey_len = node->md->onion_pkey_len;
} else {
goto end;
}
pk = router_get_rsa_onion_pkey(onion_pkey, onion_pkey_len);
end:
return pk;
}
void
node_set_country(node_t *node)
{
const tor_addr_t *ipv4_addr = NULL;
if (node->rs)
ipv4_addr = &node->rs->ipv4_addr;
else if (node->ri)
ipv4_addr = &node->ri->ipv4_addr;
if (BUG(!ipv4_addr)) {
node->country = -1;
return;
}
node->country = geoip_get_country_by_addr(ipv4_addr);
}
void
nodelist_refresh_countries(void)
{
const smartlist_t *nodes = nodelist_get_list();
SMARTLIST_FOREACH(nodes, node_t *, node,
node_set_country(node));
}
int
router_addrs_in_same_network(const tor_addr_t *a1,
const tor_addr_t *a2)
{
if (tor_addr_is_null(a1) || tor_addr_is_null(a2))
return 0;
switch (tor_addr_family(a1)) {
case AF_INET:
return 0 == tor_addr_compare_masked(a1, a2, 16, CMP_SEMANTIC);
case AF_INET6:
return 0 == tor_addr_compare_masked(a1, a2, 32, CMP_SEMANTIC);
default:
return 0;
}
}
STATIC int
node_nickname_matches(const node_t *node, const char *nickname)
{
const char *n = node_get_nickname(node);
if (n && nickname[0]!='$' && !strcasecmp(n, nickname))
return 1;
return hex_digest_nickname_matches(nickname,
node->identity,
n);
}
STATIC int
node_in_nickname_smartlist(const smartlist_t *lst, const node_t *node)
{
if (!lst) return 0;
SMARTLIST_FOREACH(lst, const char *, name, {
if (node_nickname_matches(node, name))
return 1;
});
return 0;
}
STATIC int
node_family_contains(const node_t *n1, const node_t *n2)
{
if (n1->ri && n1->ri->declared_family) {
return node_in_nickname_smartlist(n1->ri->declared_family, n2);
} else if (n1->md) {
return nodefamily_contains_node(n1->md->family, n2);
} else {
return 0;
}
}
STATIC bool
node_has_declared_family(const node_t *node)
{
if (node->ri && node->ri->declared_family &&
smartlist_len(node->ri->declared_family)) {
return true;
}
if (node->md && node->md->family) {
return true;
}
return false;
}
STATIC void
node_lookup_declared_family(smartlist_t *out, const node_t *node)
{
if (node->ri && node->ri->declared_family &&
smartlist_len(node->ri->declared_family)) {
SMARTLIST_FOREACH_BEGIN(node->ri->declared_family, const char *, name) {
const node_t *n2 = node_get_by_nickname(name, NNF_NO_WARN_UNNAMED);
if (n2) {
smartlist_add(out, (node_t *)n2);
}
} SMARTLIST_FOREACH_END(name);
return;
}
if (node->md && node->md->family) {
nodefamily_add_nodes_to_smartlist(node->md->family, out);
}
}
int
nodes_in_same_family(const node_t *node1, const node_t *node2)
{
const or_options_t *options = get_options();
if (options->EnforceDistinctSubnets) {
tor_addr_t a1, a2;
node_get_addr(node1, &a1);
node_get_addr(node2, &a2);
tor_addr_port_t ap6_1, ap6_2;
node_get_pref_ipv6_orport(node1, &ap6_1);
node_get_pref_ipv6_orport(node2, &ap6_2);
if (router_addrs_in_same_network(&a1, &a2) ||
router_addrs_in_same_network(&ap6_1.addr, &ap6_2.addr))
return 1;
}
if (node_family_contains(node1, node2) &&
node_family_contains(node2, node1)) {
return 1;
}
if (options->NodeFamilySets) {
SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
if (routerset_contains_node(rs, node1) &&
routerset_contains_node(rs, node2))
return 1;
});
}
return 0;
}
void
nodelist_add_node_and_family(smartlist_t *sl, const node_t *node)
{
const smartlist_t *all_nodes = nodelist_get_list();
const or_options_t *options = get_options();
tor_assert(node);
{
const node_t *real_node = node_get_by_id(node->identity);
if (real_node)
smartlist_add(sl, (node_t*)real_node);
}
if (options->EnforceDistinctSubnets) {
tor_addr_t node_addr;
tor_addr_port_t node_ap6;
node_get_addr(node, &node_addr);
node_get_pref_ipv6_orport(node, &node_ap6);
SMARTLIST_FOREACH_BEGIN(all_nodes, const node_t *, node2) {
tor_addr_t a;
tor_addr_port_t ap6;
node_get_addr(node2, &a);
node_get_pref_ipv6_orport(node2, &ap6);
if (router_addrs_in_same_network(&a, &node_addr) ||
router_addrs_in_same_network(&ap6.addr, &node_ap6.addr))
smartlist_add(sl, (void*)node2);
} SMARTLIST_FOREACH_END(node2);
}
if (node_has_declared_family(node)) {
smartlist_t *declared_family = smartlist_new();
node_lookup_declared_family(declared_family, node);
SMARTLIST_FOREACH_BEGIN(declared_family, const node_t *, node2) {
if (node_family_contains(node2, node)) {
smartlist_add(sl, (void*)node2);
}
} SMARTLIST_FOREACH_END(node2);
smartlist_free(declared_family);
}
if (options->NodeFamilySets) {
SMARTLIST_FOREACH(options->NodeFamilySets, const routerset_t *, rs, {
if (routerset_contains_node(rs, node)) {
routerset_get_all_nodes(sl, rs, NULL, 0);
}
});
}
}
const node_t *
router_find_exact_exit_enclave(const char *address, uint16_t port)
{
struct in_addr in;
tor_addr_t ipv4_addr;
const or_options_t *options = get_options();
if (!tor_inet_aton(address, &in))
return NULL;
tor_addr_from_in(&ipv4_addr, &in);
SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
if (tor_addr_eq(node_get_prim_addr_ipv4(node), &ipv4_addr) &&
node->is_running &&
compare_tor_addr_to_node_policy(&ipv4_addr, port, node) ==
ADDR_POLICY_ACCEPTED &&
!routerset_contains_node(options->ExcludeExitNodesUnion_, node))
return node;
});
return NULL;
}
int
node_is_unreliable(const node_t *node, int need_uptime,
int need_capacity, int need_guard)
{
if (need_uptime && !node->is_stable)
return 1;
if (need_capacity && !node->is_fast)
return 1;
if (need_guard && !node->is_possible_guard)
return 1;
return 0;
}
int
router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
int need_uptime)
{
addr_policy_result_t r;
SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
if (node->is_running &&
!node_is_unreliable(node, need_uptime, 0, 0)) {
r = compare_tor_addr_to_node_policy(addr, port, node);
if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
return 0;
}
} SMARTLIST_FOREACH_END(node);
return 1;
}
void
router_set_status(const char *digest, int up)
{
node_t *node;
tor_assert(digest);
SMARTLIST_FOREACH(router_get_fallback_dir_servers(),
dir_server_t *, d,
if (tor_memeq(d->digest, digest, DIGEST_LEN))
d->is_running = up);
SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
dir_server_t *, d,
if (tor_memeq(d->digest, digest, DIGEST_LEN))
d->is_running = up);
node = node_get_mutable_by_id(digest);
if (node) {
#if 0#endif
if (!up && node_is_me(node) && !net_is_disabled())
log_warn(LD_NET, "We just marked ourself as down. Are your external "
"addresses reachable?");
if (bool_neq(node->is_running, up))
router_dir_info_changed();
node->is_running = up;
}
}
static int have_min_dir_info = 0;
static consensus_path_type_t have_consensus_path = CONSENSUS_PATH_UNKNOWN;
static int need_to_update_have_min_dir_info = 1;
static char dir_info_status[512] = "";
MOCK_IMPL(int,
router_have_minimum_dir_info,(void))
{
static int logged_delay=0;
const char *delay_fetches_msg = NULL;
if (should_delay_dir_fetches(get_options(), &delay_fetches_msg)) {
if (!logged_delay)
log_notice(LD_DIR, "Delaying directory fetches: %s", delay_fetches_msg);
logged_delay=1;
strlcpy(dir_info_status, delay_fetches_msg, sizeof(dir_info_status));
return 0;
}
logged_delay = 0;
if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
update_router_have_minimum_dir_info();
}
return have_min_dir_info;
}
MOCK_IMPL(consensus_path_type_t,
router_have_consensus_path, (void))
{
return have_consensus_path;
}
void
router_dir_info_changed(void)
{
need_to_update_have_min_dir_info = 1;
rend_hsdir_routers_changed();
hs_service_dir_info_changed();
hs_client_dir_info_changed();
}
const char *
get_dir_info_status_string(void)
{
return dir_info_status;
}
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)
{
const int md = (consensus->flavor == FLAV_MICRODESC);
*num_present = 0, *num_usable = 0;
SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, routerstatus_t *, rs)
{
const node_t *node = node_get_by_id(rs->identity_digest);
if (!node)
continue;
if ((exit_only & USABLE_DESCRIPTOR_EXIT_FLAG) && ! rs->is_exit)
continue;
if (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
continue;
if (client_would_use_router(rs, now)) {
const char * const digest = rs->descriptor_digest;
int present;
++*num_usable;
if (md)
present = NULL != microdesc_cache_lookup_by_digest256(NULL, digest);
else
present = NULL != router_get_by_descriptor_digest(digest);
if (present) {
if ((exit_only & USABLE_DESCRIPTOR_EXIT_POLICY) &&
node_exit_policy_rejects_all(node)) {
continue;
}
++*num_present;
}
if (descs_out)
smartlist_add(descs_out, (node_t*)node);
}
}
SMARTLIST_FOREACH_END(rs);
log_debug(LD_DIR, "%d usable, %d present (%s%s%s%s%s).",
*num_usable, *num_present,
md ? "microdesc" : "desc",
(exit_only & USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG) ?
" exit" : "s",
(exit_only & USABLE_DESCRIPTOR_EXIT_POLICY) ?
" policies" : "" ,
(exit_only == USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG) ?
" and" : "" ,
(exit_only & USABLE_DESCRIPTOR_EXIT_FLAG) ?
" flags" : "" );
}
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)
{
smartlist_t *guards = smartlist_new();
smartlist_t *mid = smartlist_new();
smartlist_t *exits = smartlist_new();
double f_guard, f_mid, f_exit;
double f_path = 0.0;
int np = 0;
int nu = 0;
const int authdir = authdir_mode_v3(options);
count_usable_descriptors(num_present_out, num_usable_out,
mid, consensus, now, options->MiddleNodes,
USABLE_DESCRIPTOR_ALL);
log_debug(LD_NET,
"%s: %d present, %d usable",
"mid",
np,
nu);
if (options->EntryNodes) {
count_usable_descriptors(&np, &nu, guards, consensus, now,
options->EntryNodes, USABLE_DESCRIPTOR_ALL);
log_debug(LD_NET,
"%s: %d present, %d usable",
"guard",
np,
nu);
} else {
SMARTLIST_FOREACH(mid, const node_t *, node, {
if (authdir) {
if (node->rs && node->rs->is_possible_guard)
smartlist_add(guards, (node_t*)node);
} else {
if (node->is_possible_guard)
smartlist_add(guards, (node_t*)node);
}
});
log_debug(LD_NET,
"%s: %d possible",
"guard",
smartlist_len(guards));
}
count_usable_descriptors(&np, &nu, exits, consensus, now,
NULL, USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG);
log_debug(LD_NET,
"%s: %d present, %d usable",
"exits",
np,
nu);
consensus_path_type_t old_have_consensus_path = have_consensus_path;
have_consensus_path = ((np > 0) ?
CONSENSUS_PATH_EXIT :
CONSENSUS_PATH_INTERNAL);
if (old_have_consensus_path != have_consensus_path) {
if (have_consensus_path == CONSENSUS_PATH_INTERNAL) {
log_notice(LD_NET,
"The current consensus has no exit nodes. "
"Tor can only build internal paths, "
"such as paths to onion services.");
} else if (old_have_consensus_path == CONSENSUS_PATH_INTERNAL) {
log_notice(LD_NET,
"The current consensus contains exit nodes. "
"Tor can build exit and internal paths.");
}
}
f_guard = frac_nodes_with_descriptors(guards, WEIGHT_FOR_GUARD, 1);
f_mid = frac_nodes_with_descriptors(mid, WEIGHT_FOR_MID, 0);
f_exit = frac_nodes_with_descriptors(exits, WEIGHT_FOR_EXIT, 0);
if (options->UseBridges && num_bridges_usable(0) > 0)
f_guard = 1.0;
log_debug(LD_NET,
"f_guard: %.2f, f_mid: %.2f, f_exit: %.2f",
f_guard,
f_mid,
f_exit);
smartlist_free(guards);
smartlist_free(mid);
smartlist_free(exits);
if (options->ExitNodes) {
double f_myexit, f_myexit_unflagged;
smartlist_t *myexits= smartlist_new();
smartlist_t *myexits_unflagged = smartlist_new();
count_usable_descriptors(&np, &nu, myexits, consensus, now,
options->ExitNodes,
USABLE_DESCRIPTOR_EXIT_POLICY_AND_FLAG);
log_debug(LD_NET,
"%s: %d present, %d usable",
"myexits",
np,
nu);
count_usable_descriptors(&np, &nu, myexits_unflagged,
consensus, now,
options->ExitNodes,
USABLE_DESCRIPTOR_EXIT_POLICY);
log_debug(LD_NET,
"%s: %d present, %d usable",
"myexits_unflagged (initial)",
np,
nu);
f_myexit= frac_nodes_with_descriptors(myexits,WEIGHT_FOR_EXIT, 0);
f_myexit_unflagged=
frac_nodes_with_descriptors(myexits_unflagged,
WEIGHT_FOR_EXIT, 0);
log_debug(LD_NET,
"f_exit: %.2f, f_myexit: %.2f, f_myexit_unflagged: %.2f",
f_exit,
f_myexit,
f_myexit_unflagged);
if (smartlist_len(myexits) == 0 &&
smartlist_len(myexits_unflagged)) {
f_myexit = f_myexit_unflagged;
}
smartlist_free(myexits);
smartlist_free(myexits_unflagged);
if (f_myexit < f_exit)
f_exit = f_myexit;
}
if (router_have_consensus_path() != CONSENSUS_PATH_EXIT) {
if (f_exit <= 0.0) {
f_exit = f_mid;
}
}
f_path = f_guard * f_mid * f_exit;
if (status_out)
tor_asprintf(status_out,
"%d%% of guards bw, "
"%d%% of midpoint bw, and "
"%d%% of %s = "
"%d%% of path bw",
(int)(f_guard*100),
(int)(f_mid*100),
(int)(f_exit*100),
(router_have_consensus_path() == CONSENSUS_PATH_EXIT ?
"exit bw" :
"end bw (no exits in consensus, using mid)"),
(int)(f_path*100));
return f_path;
}
int
count_loading_descriptors_progress(void)
{
int num_present = 0, num_usable=0;
time_t now = time(NULL);
const or_options_t *options = get_options();
const networkstatus_t *consensus =
networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
double paths, fraction;
if (!consensus)
return 0;
paths = compute_frac_paths_available(consensus, options, now,
&num_present, &num_usable,
NULL);
fraction = paths / get_frac_paths_needed_for_circs(options,consensus);
if (fraction > 1.0)
return 0;
return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
(fraction*(BOOTSTRAP_STATUS_ENOUGH_DIRINFO-1 -
BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
}
static double
get_frac_paths_needed_for_circs(const or_options_t *options,
const networkstatus_t *ns)
{
#define DFLT_PCT_USABLE_NEEDED 60
if (options->PathsNeededToBuildCircuits >= 0.0) {
return options->PathsNeededToBuildCircuits;
} else {
return networkstatus_get_param(ns, "min_paths_for_circs_pct",
DFLT_PCT_USABLE_NEEDED,
25, 95)/100.0;
}
}
static void
update_router_have_minimum_dir_info(void)
{
time_t now = time(NULL);
int res;
int num_present=0, num_usable=0;
const or_options_t *options = get_options();
const networkstatus_t *consensus =
networkstatus_get_reasonably_live_consensus(now,usable_consensus_flavor());
int using_md;
if (!consensus) {
if (!networkstatus_get_latest_consensus())
strlcpy(dir_info_status, "We have no usable consensus.",
sizeof(dir_info_status));
else
strlcpy(dir_info_status, "We have no recent usable consensus.",
sizeof(dir_info_status));
res = 0;
goto done;
}
using_md = consensus->flavor == FLAV_MICRODESC;
{
char *status = NULL;
double paths = compute_frac_paths_available(consensus, options, now,
&num_present, &num_usable,
&status);
if (paths < get_frac_paths_needed_for_circs(options,consensus)) {
tor_snprintf(dir_info_status, sizeof(dir_info_status),
"We need more %sdescriptors: we have %d/%d, and "
"can only build %d%% of likely paths. (We have %s.)",
using_md?"micro":"", num_present, num_usable,
(int)(paths*100), status);
tor_free(status);
res = 0;
control_event_boot_dir(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
goto done;
}
tor_free(status);
res = 1;
}
{
char *guard_error = entry_guards_get_err_str_if_dir_info_missing(using_md,
num_present,
num_usable);
if (guard_error) {
strlcpy(dir_info_status, guard_error, sizeof(dir_info_status));
tor_free(guard_error);
res = 0;
goto done;
}
}
done:
if (res && !have_min_dir_info) {
control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
control_event_boot_dir(BOOTSTRAP_STATUS_ENOUGH_DIRINFO, 0);
log_info(LD_DIR,
"We now have enough directory information to build circuits.");
}
if (!res && have_min_dir_info) {
int quiet = dirclient_too_idle_to_fetch_descriptors(options, now);
tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
"Our directory information is no longer up-to-date "
"enough to build circuits: %s", dir_info_status);
note_that_we_maybe_cant_complete_circuits();
have_consensus_path = CONSENSUS_PATH_UNKNOWN;
control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
}
have_min_dir_info = res;
need_to_update_have_min_dir_info = 0;
}