commit 1ec604f0f9dc7f642e5614de4741671105d6945f Author: teor teor@riseup.net Date: Mon May 11 17:31:20 2020 +1000
nodelist: Move node flag checks
Move node flag checks to router_add_running_nodes_to_smartlist(), where they are actually used.
Part of 34200. --- src/core/or/circuitbuild.c | 10 +------- src/feature/nodelist/node_select.c | 38 +++++------------------------- src/feature/nodelist/routerlist.c | 48 ++++++++++++++++++++++++++++++-------- src/feature/nodelist/routerlist.h | 7 +----- 4 files changed, 46 insertions(+), 57 deletions(-)
diff --git a/src/core/or/circuitbuild.c b/src/core/or/circuitbuild.c index d7b07c490..b840ca7a7 100644 --- a/src/core/or/circuitbuild.c +++ b/src/core/or/circuitbuild.c @@ -1795,15 +1795,7 @@ pick_restricted_middle_node(router_crn_flags_t flags, tor_assert(pick_from);
/* Add all running nodes to all_live_nodes */ - router_add_running_nodes_to_smartlist(all_live_nodes, - (flags & CRN_NEED_UPTIME) != 0, - (flags & CRN_NEED_CAPACITY) != 0, - (flags & CRN_NEED_GUARD) != 0, - (flags & CRN_NEED_DESC) != 0, - (flags & CRN_PREF_ADDR) != 0, - (flags & CRN_DIRECT_CONN) != 0, - (flags & CRN_RENDEZVOUS_V3) != 0, - (flags & CRN_INITIATE_IPV6_EXTEND) != 0); + router_add_running_nodes_to_smartlist(all_live_nodes, flags);
/* Filter all_live_nodes to only add live *and* whitelisted middles * to the list whitelisted_live_middles. */ diff --git a/src/feature/nodelist/node_select.c b/src/feature/nodelist/node_select.c index 7015063d0..66665afba 100644 --- a/src/feature/nodelist/node_select.c +++ b/src/feature/nodelist/node_select.c @@ -934,44 +934,21 @@ nodelist_subtract(smartlist_t *sl, const smartlist_t *excluded) * in <b>excludedsmartlist</b>, or which matches <b>excludedset</b>, even if * they are the only nodes available. * - * If the following <b>flags</b> are set: - * - <b>CRN_NEED_UPTIME</b>: if any router has more than a minimum uptime, - * return one of those; - * - <b>CRN_NEED_CAPACITY</b>: weight your choice by the advertised capacity - * of each router; - * - <b>CRN_NEED_GUARD</b>: only consider Guard routers; - * - <b>CRN_WEIGHT_AS_EXIT</b>: we weight bandwidths as if picking an exit - * node, otherwise we weight bandwidths for - * picking a relay node (that is, possibly - * discounting exit nodes); - * - <b>CRN_NEED_DESC</b>: only consider nodes that have a routerinfo or - * microdescriptor -- that is, enough info to be - * used to build a circuit; - * - <b>CRN_DIRECT_CONN</b>: only consider nodes that are suitable for direct - * connections. Check ReachableAddresses, - * ClientUseIPv4 0, and - * fascist_firewall_use_ipv6() == 0); - * - <b>CRN_PREF_ADDR</b>: only consider nodes that have an address that is - * preferred by the ClientPreferIPv6ORPort setting. - * - <b>CRN_RENDEZVOUS_V3</b>: only consider nodes that can become v3 onion - * service rendezvous points. - * - <b>CRN_INITIATE_IPV6_EXTEND</b>: only consider routers than can initiate - * IPv6 extends. + * <b>flags</b> is a set of CRN_* flags, see + * router_add_running_nodes_to_smartlist() for details. */ const node_t * router_choose_random_node(smartlist_t *excludedsmartlist, routerset_t *excludedset, router_crn_flags_t flags) -{ /* XXXX MOVE */ +{ + /* A limited set of flags, used for weighting and fallback node selection. + */ const int need_uptime = (flags & CRN_NEED_UPTIME) != 0; const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0; const int need_guard = (flags & CRN_NEED_GUARD) != 0; const int weight_for_exit = (flags & CRN_WEIGHT_AS_EXIT) != 0; - const int need_desc = (flags & CRN_NEED_DESC) != 0; const int pref_addr = (flags & CRN_PREF_ADDR) != 0; - const int direct_conn = (flags & CRN_DIRECT_CONN) != 0; - const int rendezvous_v3 = (flags & CRN_RENDEZVOUS_V3) != 0; - const bool initiate_ipv6_extend = (flags & CRN_INITIATE_IPV6_EXTEND) != 0;
smartlist_t *sl=smartlist_new(), *excludednodes=smartlist_new(); @@ -989,10 +966,7 @@ router_choose_random_node(smartlist_t *excludedsmartlist, if ((r = router_get_my_routerinfo())) routerlist_add_node_and_family(excludednodes, r);
- router_add_running_nodes_to_smartlist(sl, need_uptime, need_capacity, - need_guard, need_desc, pref_addr, - direct_conn, rendezvous_v3, - initiate_ipv6_extend); + router_add_running_nodes_to_smartlist(sl, flags); log_debug(LD_CIRC, "We found %d running nodes.", smartlist_len(sl)); diff --git a/src/feature/nodelist/routerlist.c b/src/feature/nodelist/routerlist.c index d4dfffa1a..110622283 100644 --- a/src/feature/nodelist/routerlist.c +++ b/src/feature/nodelist/routerlist.c @@ -512,21 +512,49 @@ routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2) }
/** Add every suitable node from our nodelist to <b>sl</b>, so that - * we can pick a node for a circuit. See router_choose_random_node() - * for details. + * we can pick a node for a circuit. + * + * If the following <b>flags</b> are set: + * - <b>CRN_NEED_UPTIME</b>: if any router has more than a minimum uptime, + * return one of those; + * - <b>CRN_NEED_CAPACITY</b>: weight your choice by the advertised capacity + * of each router; + * - <b>CRN_NEED_GUARD</b>: only consider Guard routers; + * - <b>CRN_WEIGHT_AS_EXIT</b>: we weight bandwidths as if picking an exit + * node, otherwise we weight bandwidths for + * picking a relay node (that is, possibly + * discounting exit nodes); + * - <b>CRN_NEED_DESC</b>: only consider nodes that have a routerinfo or + * microdescriptor -- that is, enough info to be + * used to build a circuit; + * - <b>CRN_DIRECT_CONN</b>: only consider nodes that are suitable for direct + * connections. Check ReachableAddresses, + * ClientUseIPv4 0, and + * fascist_firewall_use_ipv6() == 0); + * - <b>CRN_PREF_ADDR</b>: only consider nodes that have an address that is + * preferred by the ClientPreferIPv6ORPort setting. + * - <b>CRN_RENDEZVOUS_V3</b>: only consider nodes that can become v3 onion + * service rendezvous points. + * - <b>CRN_INITIATE_IPV6_EXTEND</b>: only consider routers than can initiate + * IPv6 extends. */ void -router_add_running_nodes_to_smartlist(smartlist_t *sl, int need_uptime, - int need_capacity, int need_guard, - int need_desc, int pref_addr, - int direct_conn, - bool rendezvous_v3, - bool initiate_ipv6_extend) -{ +router_add_running_nodes_to_smartlist(smartlist_t *sl, int flags) +{ + /* The full set of flags used for node selection. */ + const int need_uptime = (flags & CRN_NEED_UPTIME) != 0; + const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0; + const int need_guard = (flags & CRN_NEED_GUARD) != 0; + const int need_desc = (flags & CRN_NEED_DESC) != 0; + const int pref_addr = (flags & CRN_PREF_ADDR) != 0; + const int direct_conn = (flags & CRN_DIRECT_CONN) != 0; + const int rendezvous_v3 = (flags & CRN_RENDEZVOUS_V3) != 0; + const bool initiate_ipv6_extend = (flags & CRN_INITIATE_IPV6_EXTEND) != 0; + const int check_reach = !router_or_conn_should_skip_reachable_address_check( get_options(), pref_addr); - /* XXXX MOVE */ + SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) { if (!node->is_running || !node->is_valid) continue; diff --git a/src/feature/nodelist/routerlist.h b/src/feature/nodelist/routerlist.h index d6d606428..7d0788bac 100644 --- a/src/feature/nodelist/routerlist.h +++ b/src/feature/nodelist/routerlist.h @@ -58,12 +58,7 @@ int router_dir_conn_should_skip_reachable_address_check( int try_ip_pref); void router_reset_status_download_failures(void); int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2); -void router_add_running_nodes_to_smartlist(smartlist_t *sl, int need_uptime, - int need_capacity, int need_guard, - int need_desc, int pref_addr, - int direct_conn, - bool rendezvous_v3, - bool initiate_ipv6_extend); +void router_add_running_nodes_to_smartlist(smartlist_t *sl, int flags);
const routerinfo_t *routerlist_find_my_routerinfo(void); uint32_t router_get_advertised_bandwidth(const routerinfo_t *router);