116#define ENTRYNODES_PRIVATE
132#include "feature/client/circpathbias.h"
168 guard_selection_t *gs,
169 entry_guard_t *guard);
176 const uint8_t *rsa_id_digest,
177 const char *nickname,
182 const entry_guard_restriction_t *rst);
221STATIC guard_selection_type_t
225 if (type == GS_TYPE_INFER) {
226 if (!strcmp(
name,
"bridges"))
227 type = GS_TYPE_BRIDGE;
228 else if (!strcmp(
name,
"restricted"))
229 type = GS_TYPE_RESTRICTED;
231 type = GS_TYPE_NORMAL;
241 guard_selection_type_t type)
243 guard_selection_t *gs;
247 gs = tor_malloc_zero(
sizeof(*gs));
248 gs->name = tor_strdup(
name);
264 guard_selection_type_t type,
265 int create_if_absent)
271 if (!strcmp(gs->name,
name))
273 } SMARTLIST_FOREACH_END(gs);
275 if (! create_if_absent)
278 log_debug(
LD_GUARD,
"Creating a guard selection called %s",
name);
282 return new_selection;
296 guard_selection_type_t type = GS_TYPE_INFER;
306 log_notice(
LD_GUARD,
"Starting with guard context \"%s\"",
name);
326 static char buf[256];
329 strlen(guard->nickname) ? guard->nickname :
"[bridge]",
338 return guard->identity;
348HANDLE_IMPL(entry_guard, entry_guard_t, ATTR_UNUSED
STATIC)
359 time_t earliest = now - max_backdate;
363 if (latest <= earliest)
364 latest = earliest + 1;
385 DFLT_MAX_SAMPLE_THRESHOLD_PERCENT,
396 DFLT_MAX_SAMPLE_SIZE,
406 DFLT_MIN_FILTERED_SAMPLE_SIZE,
416 "guard-remove-unlisted-guards-after-days",
417 DFLT_REMOVE_UNLISTED_GUARDS_AFTER_DAYS,
442 "guard-lifetime-days",
443 DFLT_GUARD_LIFETIME_DAYS, 1, 365*10);
457 DFLT_GUARD_CONFIRMED_MIN_LIFETIME_DAYS,
470 if (configured_primaries) {
471 return configured_primaries;
477 "guard-n-primary-guards",
478 DFLT_N_PRIMARY_GUARDS, 1, INT32_MAX);
488 const char *param_name;
493 if (usage == GUARD_USAGE_DIRGUARD) {
495 param_name =
"guard-n-primary-dir-guards-to-use";
496 param_default = DFLT_N_PRIMARY_DIR_GUARDS_TO_USE;
499 param_name =
"guard-n-primary-guards-to-use";
500 param_default = DFLT_N_PRIMARY_GUARDS_TO_USE;
502 if (configured >= 1) {
506 param_name, param_default, 1, INT32_MAX);
516 DFLT_INTERNET_LIKELY_DOWN_INTERVAL,
528 "guard-nonprimary-guard-connect-timeout",
529 DFLT_NONPRIMARY_GUARD_CONNECT_TIMEOUT,
540 "guard-nonprimary-guard-idle-timeout",
541 DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT,
552 "guard-meaningful-restriction-percent",
553 DFLT_MEANINGFUL_RESTRICTION_PERCENT,
565 "guard-extreme-restriction-percent",
566 DFLT_EXTREME_RESTRICTION_PERCENT,
573mark_guard_maybe_reachable(entry_guard_t *guard)
575 if (guard->is_reachable != GUARD_REACHABLE_NO) {
581 guard->is_reachable = GUARD_REACHABLE_MAYBE;
582 if (guard->is_filtered_guard)
583 guard->is_usable_filtered_guard = 1;
608 if (!gs->primary_guards_up_to_date)
612 mark_guard_maybe_reachable(guard);
613 } SMARTLIST_FOREACH_END(guard);
619mark_all_guards_maybe_reachable(guard_selection_t *gs)
624 mark_guard_maybe_reachable(guard);
625 } SMARTLIST_FOREACH_END(guard);
638 const guard_selection_t *old_selection,
639 guard_selection_type_t *type_out)
645 *type_out = GS_TYPE_BRIDGE;
651 *type_out = GS_TYPE_NORMAL;
656 int n_guards = 0, n_passing_filter = 0;
664 } SMARTLIST_FOREACH_END(node);
668 const int meaningful_threshold_high =
670 const int meaningful_threshold_mid =
672 const int meaningful_threshold_low =
674 const int extreme_threshold =
691 static int have_warned_extreme_threshold = 0;
693 n_passing_filter < extreme_threshold &&
694 ! have_warned_extreme_threshold) {
695 have_warned_extreme_threshold = 1;
696 const double exclude_frac =
697 (n_guards - n_passing_filter) / (
double)n_guards;
698 log_warn(
LD_GUARD,
"Your configuration excludes %d%% of all possible "
699 "guards. That's likely to make you stand out from the "
700 "rest of the world.", (
int)(exclude_frac * 100));
705 if (old_selection == NULL) {
706 if (n_passing_filter >= meaningful_threshold_mid) {
707 *type_out = GS_TYPE_NORMAL;
710 *type_out = GS_TYPE_RESTRICTED;
720 if (n_passing_filter >= meaningful_threshold_high) {
721 *type_out = GS_TYPE_NORMAL;
723 }
else if (n_passing_filter < meaningful_threshold_low) {
724 *type_out = GS_TYPE_RESTRICTED;
728 *type_out = old_selection->type;
729 return old_selection->name;
749 guard_selection_type_t type = GS_TYPE_INFER;
761 if (! strcmp(cur_name, new_name)) {
763 "Staying with guard context \"%s\" (no change)", new_name);
767 log_notice(
LD_GUARD,
"Switching to guard context \"%s\" (was using \"%s\")",
769 guard_selection_t *new_guard_context;
802 const uint8_t *rsa_id)
809 } SMARTLIST_FOREACH_END(guard);
815static entry_guard_t *
821 entry_guard_t *guard;
836 const uint8_t *identity = NULL;
838 identity = (
const uint8_t *)guard->identity;
840 if (BUG(guard->bridge_addr == NULL))
844 &guard->bridge_addr->addr,
845 guard->bridge_addr->port,
846 (
const char*)identity);
867 log_info(
LD_GUARD,
"Adding %s to the entry guard sample set.",
886static entry_guard_t *
888 const uint8_t *rsa_id_digest,
889 const char *nickname,
898 if (BUG(!rsa_id_digest && !bridge_addrport))
901 entry_guard_t *guard = tor_malloc_zero(
sizeof(entry_guard_t));
904 guard->is_persistent = (rsa_id_digest != NULL);
905 guard->selection_name = tor_strdup(gs->name);
907 memcpy(guard->identity, rsa_id_digest,
DIGEST_LEN);
909 strlcpy(guard->nickname, nickname,
sizeof(guard->nickname));
911 tor_free(guard->sampled_by_version);
912 guard->sampled_by_version = tor_strdup(VERSION);
913 guard->currently_listed = 1;
914 guard->sampled_idx = gs->next_sampled_idx++;
915 guard->confirmed_idx = -1;
918 guard->is_reachable = GUARD_REACHABLE_MAYBE;
920 guard->bridge_addr = tor_memdup(bridge_addrport,
sizeof(*bridge_addrport));
923 guard->in_selection = gs;
938static entry_guard_t *
958static entry_guard_t *
969 } SMARTLIST_FOREACH_END(g);
978 const uint8_t *rsa_id_digest)
990 int make_persistent = 0;
993 memcpy(g->identity, rsa_id_digest,
DIGEST_LEN);
997 if (BUG(! g->is_persistent))
1001 base16_encode(old_id,
sizeof(old_id), g->identity,
sizeof(g->identity));
1002 log_warn(
LD_BUG,
"We 'learned' an identity %s for a bridge at %s:%d, but "
1003 "we already knew a different one (%s). Ignoring the new info as "
1011 if (make_persistent) {
1012 g->is_persistent = 1;
1027 const entry_guard_restriction_t *rst)
1029 int n_reachable_filtered_guards = 0;
1034 if (guard->is_usable_filtered_guard)
1035 ++n_reachable_filtered_guards;
1036 } SMARTLIST_FOREACH_END(guard);
1037 return n_reachable_filtered_guards;
1046 const int using_bridges = (gs->type == GS_TYPE_BRIDGE);
1057 const int max_sample = MIN(max_sample_by_pct, max_sample_absolute);
1058 if (max_sample < min_sample)
1074 guard_selection_t *gs,
1081 if (gs->type == GS_TYPE_BRIDGE) {
1089 } SMARTLIST_FOREACH_END(bridge);
1092 const int n_sampled = smartlist_len(gs->sampled_entry_guards);
1099 } SMARTLIST_FOREACH_END(guard);
1104 if (gs->type == GS_TYPE_RESTRICTED) {
1116 } SMARTLIST_FOREACH_END(node);
1119 digestset_free(sampled_guard_ids);
1122 *n_guards_out = n_guards;
1123 return eligible_guards;
1130static entry_guard_t *
1134 entry_guard_t *added_guard;
1135 if (gs->type == GS_TYPE_BRIDGE) {
1162 if (gs->type == GS_TYPE_BRIDGE) {
1184 log_info(
LD_GUARD,
"Not expanding the sample guard set; we have "
1185 "no reasonably live consensus.");
1189 int n_sampled = smartlist_len(gs->sampled_entry_guards);
1190 entry_guard_t *added_guard = NULL;
1198 log_info(
LD_GUARD,
"Expanding the sample guard set. We have %d guards "
1199 "in the sample, and %d eligible guards to extend it with.",
1200 n_sampled, smartlist_len(eligible_guards));
1202 while (n_usable_filtered_guards < min_filtered_sample) {
1204 if (n_sampled >= max_sample) {
1205 log_info(
LD_GUARD,
"Not expanding the guard sample any further; "
1206 "just hit the maximum sample threshold of %d",
1212 if (smartlist_len(eligible_guards) == 0) {
1217 log_info(
LD_GUARD,
"Not expanding the guard sample any further; "
1218 "just ran out of eligible guards");
1230 if (added_guard->is_usable_filtered_guard)
1231 ++n_usable_filtered_guards;
1235 smartlist_free(eligible_guards);
1244 entry_guard_t *guard)
1246 if (guard->is_primary) {
1247 guard->is_primary = 0;
1255 if (guard->confirmed_idx >= 0) {
1257 guard->confirmed_idx = -1;
1258 guard->confirmed_on_date = 0;
1274 if (gs->type == GS_TYPE_BRIDGE) {
1299 size_t n_changes = 0;
1303 const time_t unlisted_since_slop =
1310 if (is_listed && ! guard->currently_listed) {
1312 guard->currently_listed = 1;
1313 guard->unlisted_since_date = 0;
1314 log_info(
LD_GUARD,
"Sampled guard %s is now listed again.",
1316 }
else if (!is_listed && guard->currently_listed) {
1318 guard->currently_listed = 0;
1320 unlisted_since_slop);
1321 log_info(
LD_GUARD,
"Sampled guard %s is now unlisted.",
1323 }
else if (is_listed && guard->currently_listed) {
1324 log_debug(
LD_GUARD,
"Sampled guard %s is still listed.",
1327 tor_assert(! is_listed && ! guard->currently_listed);
1328 log_debug(
LD_GUARD,
"Sampled guard %s is still unlisted.",
1333 if (guard->currently_listed && guard->unlisted_since_date) {
1335 guard->unlisted_since_date = 0;
1336 log_warn(
LD_BUG,
"Sampled guard %s was listed, but with "
1337 "unlisted_since_date set. Fixing.",
1339 }
else if (!guard->currently_listed && ! guard->unlisted_since_date) {
1342 unlisted_since_slop);
1343 log_warn(
LD_BUG,
"Sampled guard %s was unlisted, but with "
1344 "unlisted_since_date unset. Fixing.",
1347 } SMARTLIST_FOREACH_END(guard);
1369 const time_t remove_if_unlisted_since,
1370 const time_t maybe_remove_if_sampled_before,
1371 const time_t remove_if_confirmed_before)
1373 size_t n_changes = 0;
1380 if (guard->currently_listed == 0 &&
1381 guard->unlisted_since_date < remove_if_unlisted_since) {
1388 log_info(
LD_GUARD,
"Removing sampled guard %s: it has been unlisted "
1391 }
else if (guard->confirmed_on_date == 0) {
1392 if (guard->sampled_on_date < maybe_remove_if_sampled_before) {
1396 log_info(
LD_GUARD,
"Removing sampled guard %s: it was sampled "
1397 "over %d days ago, but never confirmed.",
1402 if (guard->confirmed_on_date < remove_if_confirmed_before) {
1407 log_info(
LD_GUARD,
"Removing sampled guard %s: it was sampled "
1408 "over %d days ago, and confirmed over %d days ago.",
1419 entry_guard_free(guard);
1421 } SMARTLIST_FOREACH_END(guard);
1439 log_info(
LD_GUARD,
"Not updating the sample guard set; we have "
1440 "no reasonably live consensus.");
1443 log_info(
LD_GUARD,
"Updating sampled guard status based on received "
1449 const time_t remove_if_unlisted_since =
1451 const time_t maybe_remove_if_sampled_before =
1453 const time_t remove_if_confirmed_before =
1459 remove_if_unlisted_since,
1460 maybe_remove_if_sampled_before,
1461 remove_if_confirmed_before);
1464 gs->primary_guards_up_to_date = 0;
1518 FIREWALL_OR_CONNECTION,
1531 entry_guard_t *guard)
1533 if (guard->currently_listed == 0)
1535 if (guard->pb.path_bias_disabled)
1538 if (gs->type == GS_TYPE_BRIDGE) {
1575 if (
get_options()->EnforceDistinctSubnets && guard->bridge_addr) {
1579 &guard->bridge_addr->addr)) {
1589STATIC entry_guard_restriction_t *
1590guard_create_exit_restriction(
const uint8_t *exit_id)
1592 entry_guard_restriction_t *rst = NULL;
1593 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1594 rst->type = RST_EXIT_NODE;
1595 memcpy(rst->exclude_id, exit_id,
DIGEST_LEN);
1601STATIC entry_guard_restriction_t *
1603 const uint8_t *exit_id)
1605 entry_guard_restriction_t *rst = NULL;
1606 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1607 rst->type = RST_EXCL_LIST;
1610 memcpy(rst->exclude_id, exit_id,
DIGEST_LEN);
1616#define MIN_GUARDS_FOR_MD_RESTRICTION 10
1629 log_info(
LD_GUARD,
"Not setting md restriction: only %d"
1630 " usable guards.", num_usable_guards);
1640STATIC entry_guard_restriction_t *
1643 entry_guard_restriction_t *rst = NULL;
1646 log_debug(
LD_GUARD,
"Not setting md restriction: too few "
1647 "filtered guards.");
1651 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1652 rst->type = RST_OUTDATED_MD_DIRSERVER;
1659guard_obeys_exit_restriction(
const entry_guard_t *guard,
1660 const entry_guard_restriction_t *rst)
1663 rst->type == RST_EXCL_LIST);
1680 log_info(
LD_GENERAL,
"Skipping %s dirserver: outdated",
1685 log_debug(
LD_GENERAL,
"%s dirserver obeys md restrictions",
1699 return (rst->type == RST_OUTDATED_MD_DIRSERVER);
1708 const entry_guard_restriction_t *rst)
1714 if (rst->type == RST_EXIT_NODE) {
1715 return guard_obeys_exit_restriction(guard, rst);
1716 }
else if (rst->type == RST_OUTDATED_MD_DIRSERVER) {
1718 }
else if (rst->type == RST_EXCL_LIST) {
1719 return guard_obeys_exit_restriction(guard, rst) &&
1732 guard_selection_t *gs,
1733 entry_guard_t *guard)
1735 unsigned was_filtered = guard->is_filtered_guard;
1736 guard->is_filtered_guard = 0;
1737 guard->is_usable_filtered_guard = 0;
1740 guard->is_filtered_guard = 1;
1742 if (guard->is_reachable != GUARD_REACHABLE_NO)
1743 guard->is_usable_filtered_guard = 1;
1747 log_debug(
LD_GUARD,
"Updated sampled guard %s: filtered=%d; "
1749 guard->is_filtered_guard, guard->is_usable_filtered_guard);
1751 if (!
bool_eq(was_filtered, guard->is_filtered_guard)) {
1753 gs->primary_guards_up_to_date = 0;
1767 } SMARTLIST_FOREACH_END(guard);
1783 const entry_guard_restriction_t *rst,
1787 entry_guard_t *result = NULL;
1788 const unsigned exclude_confirmed = flags & SAMPLE_EXCLUDE_CONFIRMED;
1789 const unsigned exclude_primary = flags & SAMPLE_EXCLUDE_PRIMARY;
1790 const unsigned exclude_pending = flags & SAMPLE_EXCLUDE_PENDING;
1791 const unsigned no_update_primary = flags & SAMPLE_NO_UPDATE_PRIMARY;
1792 const unsigned need_descriptor = flags & SAMPLE_EXCLUDE_NO_DESCRIPTOR;
1796 } SMARTLIST_FOREACH_END(guard);
1800 log_info(
LD_GUARD,
"Trying to sample a reachable guard: We know of %d "
1801 "in the USABLE_FILTERED set.", n_reachable_filtered);
1804 if (n_reachable_filtered < min_filtered_sample) {
1805 log_info(
LD_GUARD,
" (That isn't enough. Trying to expand the sample.)");
1809 if (exclude_primary && !gs->primary_guards_up_to_date && !no_update_primary)
1818 if (! guard->is_usable_filtered_guard)
1820 if (exclude_confirmed && guard->confirmed_idx >= 0)
1822 if (exclude_primary && guard->is_primary)
1824 if (exclude_pending && guard->is_pending)
1829 } SMARTLIST_FOREACH_END(guard);
1831 log_info(
LD_GUARD,
" (After filters [%x], we have %d guards to consider.)",
1832 flags, smartlist_len(reachable_filtered_sample));
1834 if (smartlist_len(reachable_filtered_sample)) {
1845 result = smartlist_get(reachable_filtered_sample, 0);
1846 log_info(
LD_GUARD,
" (Selected %s.)",
1849 smartlist_free(reachable_filtered_sample);
1855compare_guards_by_confirmed_idx(
const void **a_,
const void **b_)
1857 const entry_guard_t *a = *a_, *b = *b_;
1858 if (a->confirmed_idx < b->confirmed_idx)
1860 else if (a->confirmed_idx > b->confirmed_idx)
1872 const entry_guard_t *a = *a_, *b = *b_;
1873 if (a->sampled_idx < b->sampled_idx)
1875 else if (a->sampled_idx > b->sampled_idx)
1891 if (guard->confirmed_idx >= 0)
1893 } SMARTLIST_FOREACH_END(guard);
1895 smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_confirmed_idx);
1897 int any_changed = 0;
1899 if (guard->confirmed_idx != guard_sl_idx) {
1901 guard->confirmed_idx = guard_sl_idx;
1903 } SMARTLIST_FOREACH_END(guard);
1905 gs->next_confirmed_idx = smartlist_len(gs->confirmed_entry_guards);
1921 if (BUG(guard->confirmed_on_date && guard->confirmed_idx >= 0))
1930 log_info(
LD_GUARD,
"Marking %s as a confirmed guard (index %d)",
1932 gs->next_confirmed_idx);
1934 guard->confirmed_idx = gs->next_confirmed_idx++;
1942 gs->primary_guards_up_to_date = 0;
1957 static int running = 0;
1968 gs->primary_guards_up_to_date = 1;
1972 if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
1974 if (! guard->is_filtered_guard)
1976 guard->is_primary = 1;
1978 } SMARTLIST_FOREACH_END(guard);
1989 if (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS &&
1990 guard->is_filtered_guard) {
1991 guard->is_primary = 1;
1996 guard->is_primary = 0;
1998 } SMARTLIST_FOREACH_END(guard);
2001 while (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS) {
2003 SAMPLE_EXCLUDE_CONFIRMED|
2004 SAMPLE_EXCLUDE_PRIMARY|
2005 SAMPLE_NO_UPDATE_PRIMARY);
2008 guard->is_primary = 1;
2015 tor_assert_nonfatal(
2016 bool_eq(guard->is_primary,
2017 smartlist_contains(new_primary_guards, guard)));
2022 new_primary_guards);
2024 log_info(
LD_GUARD,
"Primary entry guards have changed. "
2025 "New primary guard list is: ");
2026 int n = smartlist_len(new_primary_guards);
2028 log_info(
LD_GUARD,
" %d/%d: %s%s%s",
2030 g->confirmed_idx >= 0 ?
" (confirmed)" :
"",
2031 g->is_filtered_guard ?
"" :
" (excluded by filter)");
2032 } SMARTLIST_FOREACH_END(g);
2036 smartlist_free(old_primary_guards);
2037 smartlist_free(gs->primary_entry_guards);
2038 gs->primary_entry_guards = new_primary_guards;
2039 gs->primary_guards_up_to_date = 1;
2051 const unsigned SIX_HOURS = 6 * 3600;
2052 const unsigned FOUR_DAYS = 4 * 86400;
2053 const unsigned SEVEN_DAYS = 7 * 86400;
2056 if (now > failing_since) {
2057 tdiff = now - failing_since;
2063 time_t maximum;
int primary_delay;
int nonprimary_delay;
2066 { SIX_HOURS, 10*60, 1*60*60 },
2067 { FOUR_DAYS, 90*60, 4*60*60 },
2068 { SEVEN_DAYS, 4*60*60, 18*60*60 },
2069 { TIME_MAX, 9*60*60, 36*60*60 }
2075 if (tdiff <= delays[i].maximum) {
2076 return is_primary ? delays[i].primary_delay : delays[i].nonprimary_delay;
2092 if (guard->is_reachable != GUARD_REACHABLE_NO)
2098 const time_t last_attempt = guard->last_tried_to_connect;
2108 if (BUG(last_attempt == 0) ||
2109 now >= last_attempt + delay) {
2111 char tbuf[ISO_TIME_LEN+1];
2113 log_info(
LD_GUARD,
"Marked %s%sguard %s for possible retry, since we "
2114 "haven't tried to use it since %s.",
2115 guard->is_primary?
"primary ":
"",
2116 guard->confirmed_idx>=0?
"confirmed ":
"",
2120 guard->is_reachable = GUARD_REACHABLE_MAYBE;
2121 if (guard->is_filtered_guard)
2122 guard->is_usable_filtered_guard = 1;
2140static entry_guard_t *
2143 const entry_guard_restriction_t *rst,
2144 unsigned *state_out)
2146 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2147 entry_guard_t *chosen_guard = NULL;
2151 int num_entry_guards_considered = 0;
2156 log_info(
LD_GUARD,
"Entry guard %s doesn't obey restriction, we test the"
2160 "Skipping guard %s due to circuit path restriction. "
2161 "Have %d, considered: %d, to consider: %d",
2163 smartlist_len(usable_primary_guards),
2164 num_entry_guards_considered,
2165 num_entry_guards_to_consider);
2168 num_entry_guards_considered++;
2172 if (num_entry_guards_considered >= num_entry_guards_to_consider) {
2176 if (smartlist_len(usable_primary_guards) == 0) {
2177 static ratelim_t guardlog = RATELIM_INIT(60);
2179 "All current guards excluded by path restriction "
2180 "type %d; using an additional guard.",
2189 if (guard->is_reachable != GUARD_REACHABLE_NO) {
2191 log_info(
LD_GUARD,
"Guard %s does not have a descriptor",
2195 *state_out = GUARD_CIRC_STATE_USABLE_ON_COMPLETION;
2198 num_entry_guards_considered++;
2202 if (num_entry_guards_considered >= num_entry_guards_to_consider) {
2206 log_info(
LD_GUARD,
"Guard %s is not reachable",
2209 } SMARTLIST_FOREACH_END(guard);
2211 if (smartlist_len(usable_primary_guards)) {
2214 "Selected primary guard %s for circuit from a list size of %d.",
2216 smartlist_len(usable_primary_guards));
2220 } SMARTLIST_FOREACH_END(guard);
2221 smartlist_free(usable_primary_guards);
2224 smartlist_free(usable_primary_guards);
2225 return chosen_guard;
2234static entry_guard_t *
2237 const entry_guard_restriction_t *rst,
2238 unsigned *state_out)
2240 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2243 if (guard->is_primary)
2248 if (guard->is_usable_filtered_guard && ! guard->is_pending) {
2251 guard->is_pending = 1;
2253 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2254 log_info(
LD_GUARD,
"No primary guards available. Selected confirmed "
2255 "guard %s for circuit. Will try other guards before using "
2260 } SMARTLIST_FOREACH_END(guard);
2271static entry_guard_t *
2274 const entry_guard_restriction_t *rst,
2275 unsigned *state_out)
2277 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2278 entry_guard_t *chosen_guard = NULL;
2280 if (need_descriptor)
2281 flags |= SAMPLE_EXCLUDE_NO_DESCRIPTOR;
2284 SAMPLE_EXCLUDE_CONFIRMED |
2285 SAMPLE_EXCLUDE_PRIMARY |
2286 SAMPLE_EXCLUDE_PENDING |
2288 if (!chosen_guard) {
2292 chosen_guard->is_pending = 1;
2293 chosen_guard->last_tried_to_connect =
approx_time();
2294 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2295 log_info(
LD_GUARD,
"No primary or confirmed guards available. Selected "
2296 "guard %s for circuit. Will try other guards before "
2297 "using this circuit.",
2299 return chosen_guard;
2313 const entry_guard_restriction_t *rst,
2314 unsigned *state_out)
2316 entry_guard_t *chosen_guard = NULL;
2320 if (!gs->primary_guards_up_to_date)
2327 log_info(
LD_GUARD,
"Selected primary guard %s for circuit.",
2329 return chosen_guard;
2338 log_info(
LD_GUARD,
"Selected confirmed guard %s for circuit.",
2340 return chosen_guard;
2347 if (chosen_guard == NULL) {
2348 log_info(
LD_GUARD,
"Absolutely no sampled guards were available. "
2349 "Marking all guards for retry and starting from top again.");
2350 mark_all_guards_maybe_reachable(gs);
2354 log_info(
LD_GUARD,
"Selected filtered guard %s for circuit.",
2356 return chosen_guard;
2365 entry_guard_t *guard)
2369 guard->is_reachable = GUARD_REACHABLE_NO;
2370 guard->is_usable_filtered_guard = 0;
2372 guard->is_pending = 0;
2373 if (guard->failing_since == 0)
2379 log_info(
LD_GUARD,
"Recorded failure for %s%sguard %s",
2380 guard->is_primary?
"primary ":
"",
2381 guard->confirmed_idx>=0?
"confirmed ":
"",
2402 entry_guard_t *guard,
2408 const time_t last_time_on_internet = gs->last_time_on_internet;
2412 if (guard->is_reachable != GUARD_REACHABLE_YES) {
2422 guard->is_reachable = GUARD_REACHABLE_YES;
2423 guard->failing_since = 0;
2424 guard->is_pending = 0;
2425 if (guard->is_filtered_guard)
2426 guard->is_usable_filtered_guard = 1;
2428 if (guard->confirmed_idx < 0) {
2430 if (!gs->primary_guards_up_to_date)
2435 switch (old_state) {
2436 case GUARD_CIRC_STATE_COMPLETE:
2437 case GUARD_CIRC_STATE_USABLE_ON_COMPLETION:
2438 new_state = GUARD_CIRC_STATE_COMPLETE;
2443 case GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD:
2444 if (guard->is_primary) {
2452 new_state = GUARD_CIRC_STATE_COMPLETE;
2454 new_state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2459 if (! guard->is_primary) {
2466 log_info(
LD_GUARD,
"Recorded success for %s%sguard %s",
2467 guard->is_primary?
"primary ":
"",
2468 guard->confirmed_idx>=0?
"confirmed ":
"",
2486 if (a->confirmed_idx < 0) {
2487 if (b->confirmed_idx >= 0)
2490 if (b->confirmed_idx < 0)
2494 return (a->confirmed_idx < b->confirmed_idx);
2499 if (a->is_pending) {
2500 if (! b->is_pending)
2504 return a->last_tried_to_connect < b->last_tried_to_connect;
2518 if (rst && rst->excluded) {
2521 smartlist_free(rst->excluded);
2534 entry_guard_restriction_free(state->restrictions);
2535 entry_guard_handle_free(state->guard);
2543 entry_guard_restriction_t *rst))
2545 circuit_guard_state_t *result;
2547 result = tor_malloc_zero(
sizeof(circuit_guard_state_t));
2548 result->guard = entry_guard_handle_new(guard);
2549 result->state = state;
2551 result->restrictions = rst;
2569 entry_guard_restriction_t *rst,
2570 const node_t **chosen_node_out,
2571 circuit_guard_state_t **guard_state_out)
2576 *chosen_node_out = NULL;
2577 *guard_state_out = NULL;
2580 entry_guard_t *guard =
2584 if (BUG(state == 0))
2590 if (BUG(usage != GUARD_USAGE_DIRGUARD &&
2594 *chosen_node_out = node;
2599 entry_guard_restriction_free(rst);
2615 if (BUG(*guard_state_p == NULL))
2616 return GUARD_USABLE_NEVER;
2618 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2619 if (! guard || BUG(guard->in_selection == NULL))
2620 return GUARD_USABLE_NEVER;
2624 (*guard_state_p)->state);
2626 (*guard_state_p)->state = newstate;
2629 if (newstate == GUARD_CIRC_STATE_COMPLETE) {
2630 return GUARD_USABLE_NOW;
2632 return GUARD_MAYBE_USABLE_LATER;
2642 if (BUG(*guard_state_p == NULL))
2644 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2650 guard->is_pending = 0;
2651 circuit_guard_state_free(*guard_state_p);
2652 *guard_state_p = NULL;
2663 if (BUG(*guard_state_p == NULL))
2666 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2667 if (! guard || BUG(guard->in_selection == NULL))
2672 (*guard_state_p)->state = GUARD_CIRC_STATE_DEAD;
2698 } SMARTLIST_FOREACH_END(circ);
2699 smartlist_free(pending);
2710 if (!gs->primary_guards_up_to_date)
2714 if (guard->is_reachable != GUARD_REACHABLE_NO)
2716 } SMARTLIST_FOREACH_END(guard);
2729 const entry_guard_restriction_t *rst,
2738 entry_guard_t *guard_a = entry_guard_handle_get(state_a->guard);
2739 entry_guard_t *guard_b = entry_guard_handle_get(state_b->guard);
2744 }
else if (! guard_b) {
2776 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2777 "but not all primary guards were definitely down.");
2783 int n_complete_blocking = 0;
2792 entry_guard_t *guard = entry_guard_handle_get(state->guard);
2793 if (!guard || guard->in_selection != gs)
2801 } SMARTLIST_FOREACH_END(circ);
2805 if (BUG(state == NULL))
2808 if (state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD) {
2810 if (! best_waiting_circuit ||
2812 best_waiting_circuit = circ;
2815 } SMARTLIST_FOREACH_END(circ);
2817 if (! best_waiting_circuit) {
2818 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2819 "but didn't find any.");
2826 const entry_guard_restriction_t *rst_on_best_waiting =
2839 if (BUG(state == NULL))
2841 if (state->state != GUARD_CIRC_STATE_COMPLETE)
2845 best_waiting_circuit))
2846 ++n_complete_blocking;
2847 } SMARTLIST_FOREACH_END(circ);
2849 if (n_complete_blocking) {
2850 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2851 "%d complete and %d guard-stalled. At least one complete "
2852 "circuit had higher priority, so not upgrading.",
2853 n_complete, n_waiting);
2862 int n_blockers_found = 0;
2863 const time_t state_set_at_cutoff =
2867 if (BUG(state == NULL))
2869 if (state->state != GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD)
2871 if (state->state_set_at <= state_set_at_cutoff)
2874 best_waiting_circuit))
2876 } SMARTLIST_FOREACH_END(circ);
2878 if (n_blockers_found) {
2879 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2880 "%d guard-stalled, but %d pending circuit(s) had higher "
2881 "guard priority, so not upgrading.",
2882 n_waiting, n_blockers_found);
2889 int n_succeeded = 0;
2892 if (BUG(state == NULL))
2894 if (circ != best_waiting_circuit && rst_on_best_waiting) {
2899 if (state->state != GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD)
2904 state->state = GUARD_CIRC_STATE_COMPLETE;
2908 } SMARTLIST_FOREACH_END(circ);
2910 log_info(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2911 "%d guard-stalled, %d complete. %d of the guard-stalled "
2912 "circuit(s) had high enough priority to upgrade.",
2913 n_waiting, n_complete, n_succeeded);
2915 tor_assert_nonfatal(n_succeeded >= 1);
2916 smartlist_free(all_circuits);
2920 smartlist_free(all_circuits);
2931 if (guard_state == NULL)
2933 const time_t expire_if_waiting_since =
2935 return (guard_state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
2936 && guard_state->state_set_at < expire_if_waiting_since);
2971 char tbuf[ISO_TIME_LEN+1];
2978 if (guard->bridge_addr) {
2981 guard->bridge_addr->port);
2991 if (guard->sampled_by_version) {
2993 guard->sampled_by_version);
2996 if (guard->unlisted_since_date > 0) {
3002 (
int)guard->currently_listed);
3004 if (guard->confirmed_idx >= 0) {
3011 const double EPSILON = 1.0e-6;
3019 #define PB_FIELD(field) do { \
3020 if (pb->field >= EPSILON) { \
3021 smartlist_add_asprintf(result, "pb_" #field "=%f", pb->field); \
3024 PB_FIELD(use_attempts);
3025 PB_FIELD(use_successes);
3026 PB_FIELD(circ_attempts);
3027 PB_FIELD(circ_successes);
3028 PB_FIELD(successful_circuits_closed);
3029 PB_FIELD(collapsed_circuits);
3030 PB_FIELD(unusable_circuits);
3035 if (guard->extra_state_fields)
3040 smartlist_free(result);
3051 *extra, strmap_t *vals)
3054 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
3057 const char *eq = strchr(entry,
'=');
3062 char *key = tor_strndup(entry, eq-entry);
3063 char **target = strmap_get(vals, key);
3064 if (target == NULL || *target != NULL) {
3071 *target = tor_strdup(eq+1);
3074 } SMARTLIST_FOREACH_END(entry);
3082 *unlisted_since,
char *confirmed_on)
3084#define HANDLE_TIME(field) do { \
3086 int r = parse_iso_time_nospace(field, &field ## _time); \
3088 log_warn(LD_CIRC, "Unable to parse %s %s from guard", \
3089 #field, escaped(field)); \
3090 field##_time = -1; \
3095 time_t sampled_on_time = 0;
3096 time_t unlisted_since_time = 0;
3097 time_t confirmed_on_time = 0;
3099 HANDLE_TIME(sampled_on);
3100 HANDLE_TIME(unlisted_since);
3101 HANDLE_TIME(confirmed_on);
3103 if (sampled_on_time <= 0)
3105 if (unlisted_since_time < 0)
3106 unlisted_since_time = 0;
3107 if (confirmed_on_time < 0)
3108 confirmed_on_time = 0;
3112 guard->sampled_on_date = sampled_on_time;
3113 guard->unlisted_since_date = unlisted_since_time;
3114 guard->confirmed_on_date = confirmed_on_time;
3130 char *rsa_id = NULL;
3131 char *nickname = NULL;
3132 char *sampled_on = NULL;
3133 char *sampled_idx = NULL;
3134 char *sampled_by = NULL;
3135 char *unlisted_since = NULL;
3136 char *listed = NULL;
3137 char *confirmed_on = NULL;
3138 char *confirmed_idx = NULL;
3139 char *bridge_addr = NULL;
3142 char *pb_use_attempts = NULL;
3143 char *pb_use_successes = NULL;
3144 char *pb_circ_attempts = NULL;
3145 char *pb_circ_successes = NULL;
3146 char *pb_successful_circuits_closed = NULL;
3147 char *pb_collapsed_circuits = NULL;
3148 char *pb_unusable_circuits = NULL;
3149 char *pb_timeouts = NULL;
3157 strmap_t *vals = strmap_new();
3159 strmap_set(vals, #f, &f);
3166 FIELD(unlisted_since);
3168 FIELD(confirmed_on);
3169 FIELD(confirmed_idx);
3171 FIELD(pb_use_attempts);
3172 FIELD(pb_use_successes);
3173 FIELD(pb_circ_attempts);
3174 FIELD(pb_circ_successes);
3175 FIELD(pb_successful_circuits_closed);
3176 FIELD(pb_collapsed_circuits);
3177 FIELD(pb_unusable_circuits);
3183 smartlist_free(entries);
3184 strmap_free(vals, NULL);
3187 entry_guard_t *guard = tor_malloc_zero(
sizeof(entry_guard_t));
3188 guard->is_persistent = 1;
3191 log_warn(
LD_CIRC,
"Guard missing 'in' field");
3195 guard->selection_name = in;
3198 if (rsa_id == NULL) {
3199 log_warn(
LD_CIRC,
"Guard missing RSA ID field");
3206 log_warn(
LD_CIRC,
"Unable to decode guard identity %s",
escaped(rsa_id));
3211 strlcpy(guard->nickname, nickname,
sizeof(guard->nickname));
3213 guard->nickname[0]=
'$';
3220 memset(&res, 0,
sizeof(res));
3222 &res.addr, &res.port, -1);
3224 guard->bridge_addr = tor_memdup(&res,
sizeof(res));
3233 guard->sampled_by_version = sampled_by;
3236 if (listed && strcmp(listed,
"0"))
3237 guard->currently_listed = 1;
3240 guard->confirmed_idx = -1;
3241 if (confirmed_idx) {
3243 long idx =
tor_parse_long(confirmed_idx, 10, 0, INT_MAX, &ok, NULL);
3245 log_warn(
LD_GUARD,
"Guard has invalid confirmed_idx %s",
3248 guard->confirmed_idx = (int)idx;
3254 long idx =
tor_parse_long(sampled_idx, 10, 0, INT_MAX, &ok, NULL);
3256 log_warn(
LD_GUARD,
"Guard has invalid sampled_idx %s",
3259 guard->sampled_idx = invalid_sampled_idx++;
3261 guard->sampled_idx = (int)idx;
3263 }
else if (confirmed_idx) {
3267 guard->sampled_idx = guard->confirmed_idx;
3269 log_info(
LD_GUARD,
"The state file seems to be into a status that could"
3270 " yield to weird entry node selection: we're missing both a"
3271 " sampled_idx and a confirmed_idx.");
3272 guard->sampled_idx = invalid_sampled_idx++;
3276 if (smartlist_len(extra) > 0) {
3281 guard->is_reachable = GUARD_REACHABLE_MAYBE;
3283#define PB_FIELD(field) \
3285 if (pb_ ## field) { \
3287 double r = tor_parse_double(pb_ ## field, 0.0, 1e9, &ok, NULL); \
3289 log_warn(LD_CIRC, "Guard has invalid pb_%s %s", \
3290 #field, pb_ ## field); \
3292 guard->pb.field = r; \
3296 PB_FIELD(use_attempts);
3297 PB_FIELD(use_successes);
3298 PB_FIELD(circ_attempts);
3299 PB_FIELD(circ_successes);
3300 PB_FIELD(successful_circuits_closed);
3301 PB_FIELD(collapsed_circuits);
3302 PB_FIELD(unusable_circuits);
3316 entry_guard_free(guard);
3335 tor_free(pb_successful_circuits_closed);
3341 smartlist_free(extra);
3361 if (guard->is_persistent == 0)
3364 (*nextline)->key = tor_strdup(
"Guard");
3366 nextline = &(*nextline)->next;
3368 } SMARTLIST_FOREACH_END(guard);
3369 } SMARTLIST_FOREACH_END(gs);
3371 config_free_lines(state->
Guard);
3372 state->
Guard = lines;
3393 guard_selection_free(gs);
3397 } SMARTLIST_FOREACH_END(gs);
3400 for ( ; line != NULL; line = line->next) {
3402 if (guard == NULL) {
3407 if (!strcmp(guard->selection_name,
"legacy")) {
3409 entry_guard_free(guard);
3414 guard_selection_t *gs;
3419 guard->in_selection = gs;
3424 if (gs->next_sampled_idx <= guard->sampled_idx) {
3425 gs->next_sampled_idx = guard->sampled_idx + 1;
3429 entry_guard_free(guard);
3440 } SMARTLIST_FOREACH_END(gs);
3442 return n_errors ? -1 : 0;
3476circuit_guard_state_t *
3479 circuit_guard_state_t *guard_state = NULL;
3480 entry_guard_t *guard = NULL;
3494 GUARD_CIRC_STATE_USABLE_ON_COMPLETION,
3506 entry_guard_handles_clear(e);
3547 if (BUG(gs->type != GS_TYPE_BRIDGE)) {
3553 if (! guard->is_filtered_guard)
3556 if (guard->is_reachable == GUARD_REACHABLE_NO)
3559 if (!use_maybe_reachable && guard->is_reachable == GUARD_REACHABLE_MAYBE)
3564 if (node && node->ri)
3566 } SMARTLIST_FOREACH_END(guard);
3577 const double EPSILON = 1.0e-9;
3582 if (node->pb.use_attempts >
EPSILON &&
3586 node->pb.path_bias_disabled = 1;
3588 "Path use bias is too high (%f/%f); disabling node %s",
3589 node->pb.circ_successes, node->pb.circ_attempts,
3600 const double EPSILON = 1.0e-9;
3605 if (node->pb.circ_attempts >
EPSILON &&
3609 node->pb.path_bias_disabled = 1;
3611 "Path bias is too high (%f/%f); disabling node %s",
3612 node->pb.circ_successes, node->pb.circ_attempts,
3631 if (msg && *msg == NULL) {
3632 *msg = tor_strdup(
"parsing error");
3641#define SLOW_GUARD_STATE_FLUSH_TIME 600
3644#define FAST_GUARD_STATE_FLUSH_TIME 30
3719 entry_guard_t *guard = entry_guard_handle_get(guard_state->guard);
3720 if (!guard || BUG(guard->in_selection == NULL)) {
3734 const char *status = NULL;
3737 char tbuf[ISO_TIME_LEN+1];
3745 if (e->confirmed_idx < 0) {
3746 status =
"never-connected";
3747 }
else if (! e->currently_listed) {
3748 when = e->unlisted_since_date;
3749 status =
"unusable";
3750 }
else if (! e->is_filtered_guard) {
3751 status =
"unusable";
3752 }
else if (e->is_reachable == GUARD_REACHABLE_NO) {
3753 when = e->failing_since;
3769 char *result = NULL;
3772 tor_asprintf(&result,
"%s %s %s\n", nbuf, status, tbuf);
3790 const char *question,
char **answer,
3791 const char **errmsg)
3800 if (!strcmp(question,
"entry-guards") ||
3801 !strcmp(question,
"helper-nodes")) {
3803 guards = gs->sampled_entry_guards;
3810 } SMARTLIST_FOREACH_END(e);
3837 uint32_t guardfraction_percentage)
3839 double guardfraction_fraction;
3843 guardfraction_fraction = guardfraction_percentage / 100.0;
3845 long guard_bw =
tor_lround(guardfraction_fraction * orig_bandwidth);
3848 guardfraction_bw->
guard_bw = (int) guard_bw;
3850 guardfraction_bw->
non_guard_bw = orig_bandwidth - (int) guard_bw;
3861 int mark_circuits = 0;
3870 return mark_circuits;
3879 circuit_guard_state_t **guard_state_out)
3882 const uint8_t *exit_id = NULL;
3883 entry_guard_restriction_t *rst = NULL;
3886 if (CIRCUIT_IS_CONFLUX(
TO_CIRCUIT(circ)) && state
3888 rst = guard_create_conflux_restriction(circ, exit_id);
3898 rst = guard_create_exit_restriction(exit_id);
3902 GUARD_USAGE_TRAFFIC,
3905 guard_state_out) < 0) {
3919 char *old_name = tor_strdup(gs->name);
3920 guard_selection_type_t old_type = gs->type;
3923 control_event_guard(entry->nickname, entry->identity,
"DROPPED");
3931 guard_selection_free(gs);
3952 circuit_guard_state_t **guard_state_out)
3955 entry_guard_restriction_t *rst = NULL;
3963 GUARD_USAGE_DIRGUARD,
3966 guard_state_out) < 0) {
3996 int num_present,
int num_usable)
3998 if (!gs->primary_guards_up_to_date)
4001 char *ret_str = NULL;
4002 int n_missing_descriptors = 0;
4003 int n_considered = 0;
4004 int num_primary_to_check;
4010 num_primary_to_check++;
4014 if (guard->is_reachable == GUARD_REACHABLE_NO)
4018 n_missing_descriptors++;
4019 if (n_considered >= num_primary_to_check)
4021 } SMARTLIST_FOREACH_END(guard);
4024 if (!n_missing_descriptors) {
4029 tor_asprintf(&ret_str,
"We're missing descriptors for %d/%d of our "
4030 "primary entry guards (total %sdescriptors: %d/%d). "
4031 "That's ok. We will try to fetch missing descriptors soon.",
4032 n_missing_descriptors, num_primary_to_check,
4033 using_mds?
"micro":
"", num_present, num_usable);
4042 int num_present,
int num_usable)
4047 num_present, num_usable);
4058 if (gs->sampled_entry_guards) {
4060 entry_guard_free(e));
4061 smartlist_free(gs->sampled_entry_guards);
4062 gs->sampled_entry_guards = NULL;
4065 smartlist_free(gs->confirmed_entry_guards);
4066 smartlist_free(gs->primary_entry_guards);
4085#define layer2_guard_free(val) \
4086 FREE_AND_NULL(layer2_guard_t, layer2_guard_free_, (val))
4127static routerset_t *layer2_routerset = NULL;
4130#define NUMBER_SECOND_GUARDS 4
4137#define MIN_SECOND_GUARD_LIFETIME (3600*24)
4138#define MAX_SECOND_GUARD_LIFETIME (3600*24*12)
4145 "guard-hs-l2-number",
4155 "guard-hs-l2-lifetime-min",
4165 "guard-hs-l2-lifetime-max",
4166 MAX_SECOND_GUARD_LIFETIME,
4181 if (BUG(min >= max)) {
4207 log_info(
LD_GENERAL,
"Removing expired Layer2 guard %s",
4211 layer2_guard_free(g);
4219 log_info(
LD_GENERAL,
"Removing %s Layer2 guard %s",
4220 rs ?
"unsuitable" :
"missing",
4224 layer2_guard_free(g);
4228 } SMARTLIST_FOREACH_END(g);
4231 int new_guards_needed_n =
4233 if (new_guards_needed_n <= 0) {
4237 log_info(
LD_GENERAL,
"Adding %d guards to Layer2 routerset",
4238 new_guards_needed_n);
4248 } SMARTLIST_FOREACH_END(g);
4251 for (
int i = 0; i < new_guards_needed_n; i++) {
4252 const node_t *choice = NULL;
4267 log_info(
LD_GENERAL,
"Adding Layer2 guard %s",
4278 smartlist_free(excluded);
4281 routerset_free(layer2_routerset);
4288 } SMARTLIST_FOREACH_END(g);
4304 layer2_guard_free(g);
4305 } SMARTLIST_FOREACH_END(g);
4325 return layer2_routerset;
4340 guard_selection_free(gs);
4341 } SMARTLIST_FOREACH_END(gs);
4352 layer2_guard_free(g);
4353 } SMARTLIST_FOREACH_END(g);
4356 routerset_free(layer2_routerset);
int tor_addr_port_parse(int severity, const char *addrport, tor_addr_t *address_out, uint16_t *port_out, int default_port)
int tor_addr_port_eq(const tor_addr_port_t *a, const tor_addr_port_t *b)
#define fmt_and_decorate_addr(a)
const char * hex_str(const char *from, size_t fromlen)
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
download_status_t * get_bridge_dl_status_by_id(const char *digest)
const uint8_t * bridge_get_rsa_id_digest(const bridge_info_t *bridge)
int node_is_a_configured_bridge(const node_t *node)
const tor_addr_port_t * bridge_get_addr_port(const bridge_info_t *bridge)
const smartlist_t * bridge_list_get(void)
bridge_info_t * get_configured_bridge_by_exact_addr_port_digest(const tor_addr_t *addr, uint16_t port, const char *digest)
Header file for circuitbuild.c.
Header file for channel.c.
double pathbias_get_extreme_use_rate(const or_options_t *options)
double pathbias_get_extreme_rate(const or_options_t *options)
double pathbias_get_use_success_count(entry_guard_t *guard)
int pathbias_get_dropguards(const or_options_t *options)
double pathbias_get_close_success_count(entry_guard_t *guard)
circuit_guard_state_t * origin_circuit_get_guard_state(origin_circuit_t *circ)
const uint8_t * build_state_get_exit_rsa_id(cpath_build_state_t *state)
Header file for circuitbuild.c.
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
void circuit_get_all_pending_on_channel(smartlist_t *out, channel_t *chan)
Header file for circuitlist.c.
#define CIRCUIT_IS_ORIGIN(c)
void circuit_build_times_free_timeouts(circuit_build_times_t *cbt)
circuit_build_times_t * get_circuit_build_times_mutable(void)
Header file for circuitstats.c.
int circuit_should_use_vanguards(uint8_t purpose)
Header file for circuituse.c.
const or_options_t * get_options(void)
Header file for config.c.
void conflux_add_guards_to_exclude_list(const origin_circuit_t *orig_circ, smartlist_t *excluded)
Header file for conflux_pool.c.
Header file for conflux_util.c.
Header file for connection.c.
int control_event_guard(const char *nickname, const char *digest, const char *status)
Header file for control_events.c.
void * smartlist_choose(const smartlist_t *sl)
Common functions for using (pseudo-)random number generators.
time_t crypto_rand_time_range(time_t min, time_t max)
int crypto_rand_int_range(unsigned int min, unsigned int max)
const char * node_describe(const node_t *node)
Header file for describe.c.
int tor_memeq(const void *a, const void *b, size_t sz)
#define tor_memneq(a, b, sz)
void digestset_add(digestset_t *set, const char *digest)
digestset_t * digestset_new(int max_guess)
int digestset_probably_contains(const digestset_t *set, const char *digest)
Types to handle sets of digests, based on bloom filters.
Header file for directory.c.
#define DIR_PURPOSE_FETCH_MICRODESC
void download_status_reset(download_status_t *dls)
Header file for dlstatus.c.
void entry_guard_failed(circuit_guard_state_t **guard_state_p)
char * guard_selection_get_err_str_if_dir_info_missing(guard_selection_t *gs, int using_mds, int num_present, int num_usable)
STATIC int get_n_primary_guards(void)
const node_t * entry_guard_find_node(const entry_guard_t *guard)
void entry_guard_learned_bridge_identity(const tor_addr_port_t *addrport, const uint8_t *rsa_id_digest)
STATIC void entry_guards_update_primary(guard_selection_t *gs)
static entry_guard_t * entry_guard_add_to_sample_impl(guard_selection_t *gs, const uint8_t *rsa_id_digest, const char *nickname, const tor_addr_port_t *bridge_addrport)
void entry_guard_chan_failed(channel_t *chan)
static void entry_guards_update_guards_in_state(or_state_t *state)
const routerset_t * get_layer2_guards(void)
void entry_guards_changed(void)
static int guard_obeys_md_dirserver_restriction(const entry_guard_t *guard)
STATIC void entry_guards_note_guard_failure(guard_selection_t *gs, entry_guard_t *guard)
STATIC void guard_selection_free_(guard_selection_t *gs)
STATIC guard_selection_t * guard_selection_new(const char *name, guard_selection_type_t type)
void remove_all_entry_guards(void)
STATIC guard_selection_t * get_guard_selection_by_name(const char *name, guard_selection_type_t type, int create_if_absent)
void purge_vanguards_lite(void)
static int reasonably_live_consensus_is_missing(const guard_selection_t *gs)
static void entry_guard_set_filtered_flags(const or_options_t *options, guard_selection_t *gs, entry_guard_t *guard)
STATIC double get_meaningful_restriction_threshold(void)
STATIC guard_selection_type_t guard_selection_infer_type(guard_selection_type_t type, const char *name)
void entry_guard_cancel(circuit_guard_state_t **guard_state_p)
static entry_guard_t * select_and_add_guard_item_for_sample(guard_selection_t *gs, smartlist_t *eligible_guards)
STATIC int get_nonprimary_guard_idle_timeout(void)
static entry_guard_t * select_primary_guard_for_circuit(guard_selection_t *gs, guard_usage_t usage, const entry_guard_restriction_t *rst, unsigned *state_out)
entry_guard_t * entry_guard_get_by_id_digest(const char *digest)
STATIC void mark_primary_guards_maybe_reachable(guard_selection_t *gs)
int entry_guards_upgrade_waiting_circuits(guard_selection_t *gs, const smartlist_t *all_circuits_in, smartlist_t *newly_complete_out)
#define NUMBER_SECOND_GUARDS
static guard_selection_t * curr_guard_context
static void remove_guard_from_confirmed_and_primary_lists(guard_selection_t *gs, entry_guard_t *guard)
void remove_all_entry_guards_for_guard_selection(guard_selection_t *gs)
int num_bridges_usable(int use_maybe_reachable)
#define MIN_GUARDS_FOR_MD_RESTRICTION
static int guard_has_descriptor(const entry_guard_t *guard)
STATIC double get_extreme_restriction_threshold(void)
int entry_guards_update_all(guard_selection_t *gs)
static smartlist_t * layer2_guards
STATIC circuit_guard_state_t * circuit_guard_state_new(entry_guard_t *guard, unsigned state, entry_guard_restriction_t *rst)
STATIC entry_guard_t * first_reachable_filtered_entry_guard(guard_selection_t *gs, const entry_guard_restriction_t *rst, unsigned flags)
static entry_guard_t * get_sampled_guard_for_bridge(guard_selection_t *gs, const bridge_info_t *bridge)
STATIC int get_min_filtered_sample_size(void)
static int get_number_of_layer2_hs_guards(void)
static int guard_in_node_family(const entry_guard_t *guard, const node_t *node)
static size_t sampled_guards_update_consensus_presence(guard_selection_t *gs)
static int should_set_md_dirserver_restriction(void)
STATIC void entry_guard_free_(entry_guard_t *e)
STATIC void entry_guard_restriction_free_(entry_guard_restriction_t *rst)
int entry_list_is_constrained(const or_options_t *options)
STATIC int get_guard_confirmed_min_lifetime(void)
guard_usable_t entry_guard_succeeded(circuit_guard_state_t **guard_state_p)
circuit_guard_state_t * get_guard_state_for_bridge_desc_fetch(const char *digest)
static entry_guard_t * select_confirmed_guard_for_circuit(guard_selection_t *gs, guard_usage_t usage, const entry_guard_restriction_t *rst, unsigned *state_out)
static size_t sampled_guards_prune_obsolete_entries(guard_selection_t *gs, const time_t remove_if_unlisted_since, const time_t maybe_remove_if_sampled_before, const time_t remove_if_confirmed_before)
STATIC int entry_guard_has_higher_priority(entry_guard_t *a, entry_guard_t *b)
void entry_guards_note_internet_connectivity(guard_selection_t *gs)
const char * entry_guard_get_rsa_id_digest(const entry_guard_t *guard)
char * entry_guards_get_err_str_if_dir_info_missing(int using_mds, int num_present, int num_usable)
STATIC void entry_guards_update_confirmed(guard_selection_t *gs)
STATIC char * entry_guard_encode_for_state(entry_guard_t *guard, int dense_sampled_idx)
STATIC void entry_guards_update_filtered_sets(guard_selection_t *gs)
void entry_guards_update_state(or_state_t *state)
bool vanguards_lite_is_enabled(void)
static void pathbias_check_close_success_count(entry_guard_t *guard)
static int get_layer2_hs_guard_lifetime(void)
int update_guard_selection_choice(const or_options_t *options)
STATIC entry_guard_restriction_t * guard_create_dirserver_md_restriction(void)
static bool entry_guard_restriction_is_reachability(const entry_guard_restriction_t *rst)
static int compare_guards_by_sampled_idx(const void **a_, const void **b_)
void circuit_guard_state_free_(circuit_guard_state_t *state)
int entry_guard_pick_for_circuit(guard_selection_t *gs, guard_usage_t usage, entry_guard_restriction_t *rst, const node_t **chosen_node_out, circuit_guard_state_t **guard_state_out)
STATIC int get_remove_unlisted_guards_after_days(void)
static void create_initial_guard_context(void)
STATIC int get_max_sample_size_absolute(void)
STATIC int get_nonprimary_guard_connect_timeout(void)
void maintain_layer2_guards(void)
static void parse_from_state_handle_time(entry_guard_t *guard, char *sampled_on, char *unlisted_since, char *confirmed_on)
STATIC entry_guard_t * get_sampled_guard_with_id(guard_selection_t *gs, const uint8_t *rsa_id)
int entry_guards_parse_state(or_state_t *state, int set, char **msg)
STATIC unsigned entry_guards_note_guard_success(guard_selection_t *gs, entry_guard_t *guard, unsigned old_state)
static int entry_guard_obeys_restriction(const entry_guard_t *guard, const entry_guard_restriction_t *rst)
int entry_guard_state_should_expire(circuit_guard_state_t *guard_state)
guard_pathbias_t * entry_guard_get_pathbias_state(entry_guard_t *guard)
const node_t * guards_choose_guard(const origin_circuit_t *circ, cpath_build_state_t *state, uint8_t purpose, circuit_guard_state_t **guard_state_out)
int entry_guard_could_succeed(const circuit_guard_state_t *guard_state)
static entry_guard_t * entry_guard_add_bridge_to_sample(guard_selection_t *gs, const bridge_info_t *bridge)
static void pathbias_check_use_success_count(entry_guard_t *guard)
int guards_retry_optimistic(const or_options_t *options)
STATIC int entry_guard_is_listed(guard_selection_t *gs, const entry_guard_t *guard)
const node_t * guards_choose_dirguard(uint8_t dir_purpose, circuit_guard_state_t **guard_state_out)
static entry_guard_t * select_filtered_guard_for_circuit(guard_selection_t *gs, guard_usage_t usage, const entry_guard_restriction_t *rst, unsigned *state_out)
static smartlist_t * get_eligible_guards(const or_options_t *options, guard_selection_t *gs, int *n_guards_out)
STATIC void entry_guard_consider_retry(entry_guard_t *guard)
static int entry_guard_passes_filter(const or_options_t *options, guard_selection_t *gs, entry_guard_t *guard)
#define SLOW_GUARD_STATE_FLUSH_TIME
STATIC double get_max_sample_threshold(void)
int should_apply_guardfraction(const networkstatus_t *ns)
void entry_guards_changed_for_guard_selection(guard_selection_t *gs)
STATIC entry_guard_t * entry_guard_add_to_sample(guard_selection_t *gs, const node_t *node)
static int get_min_lifetime_of_layer2_hs_guards(void)
static int bridge_passes_guard_filter(const or_options_t *options, const bridge_info_t *bridge)
static entry_guard_t * get_sampled_guard_by_bridge_addr(guard_selection_t *gs, const tor_addr_port_t *addrport)
static smartlist_t * guard_contexts
STATIC int num_reachable_filtered_guards(const guard_selection_t *gs, const entry_guard_restriction_t *rst)
static int have_sampled_guard_with_id(guard_selection_t *gs, const uint8_t *rsa_id)
STATIC int entry_guards_all_primary_guards_are_down(guard_selection_t *gs)
STATIC time_t randomize_time(time_t now, time_t max_backdate)
static time_t get_remove_unlisted_guards_after_seconds(void)
STATIC int get_n_primary_guards_to_use(guard_usage_t usage)
STATIC const char * choose_guard_selection(const or_options_t *options, const networkstatus_t *live_ns, const guard_selection_t *old_selection, guard_selection_type_t *type_out)
static int get_retry_schedule(time_t failing_since, time_t now, int is_primary)
static int entry_guards_load_guards_from_state(or_state_t *state, int set)
STATIC char * getinfo_helper_format_single_entry_guard(const entry_guard_t *e)
#define FAST_GUARD_STATE_FLUSH_TIME
static int node_is_possible_guard(const node_t *node)
#define MIN_SECOND_GUARD_LIFETIME
STATIC void make_guard_confirmed(guard_selection_t *gs, entry_guard_t *guard)
static int circ_state_has_higher_priority(origin_circuit_t *a, const entry_guard_restriction_t *rst, origin_circuit_t *b)
int getinfo_helper_entry_guards(control_connection_t *conn, const char *question, char **answer, const char **errmsg)
guard_selection_t * get_guard_selection_info(void)
STATIC entry_guard_t * entry_guard_parse_from_state(const char *s)
STATIC entry_guard_t * select_entry_guard_for_circuit(guard_selection_t *gs, guard_usage_t usage, const entry_guard_restriction_t *rst, unsigned *state_out)
STATIC int get_guard_lifetime(void)
static int get_max_lifetime_of_layer2_hs_guards(void)
static void parse_from_state_set_vals(const char *s, smartlist_t *entries, smartlist_t *extra, strmap_t *vals)
static int get_max_sample_size(guard_selection_t *gs, int n_guards)
int guards_update_all(void)
CTASSERT(NUMBER_SECOND_GUARDS< 20)
STATIC void sampled_guards_update_from_consensus(guard_selection_t *gs)
entry_guard_t * entry_guard_get_by_id_digest_for_guard_selection(guard_selection_t *gs, const char *digest)
static bridge_info_t * get_bridge_info_for_guard(const entry_guard_t *guard)
static int entry_guards_dirty
const char * entry_guard_describe(const entry_guard_t *guard)
STATIC entry_guard_t * entry_guards_expand_sample(guard_selection_t *gs)
static int node_passes_guard_filter(const or_options_t *options, const node_t *node)
void entry_guards_free_all(void)
STATIC int get_internet_likely_down_interval(void)
Header file for circuitbuild.c.
const char * escaped(const char *s)
long tor_lround(double d)
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Header file for mainloop.c.
int usable_consensus_flavor(void)
int microdesc_relay_is_outdated_dirserver(const char *relay_digest)
Header file for microdesc.c.
networkstatus_t * networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
const routerstatus_t * router_get_consensus_status_by_id(const char *digest)
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.
int is_legal_nickname(const char *s)
Header file for nickname.c.
const node_t * router_choose_random_node(smartlist_t *excludedsmartlist, routerset_t *excludedset, router_crn_flags_t flags)
const node_t * node_sl_choose_by_bandwidth(const smartlist_t *sl, bandwidth_weight_rule_t rule)
Header file for node_select.c.
Node information structure.
const node_t * node_get_by_id(const char *identity_digest)
void router_dir_info_changed(void)
const smartlist_t * nodelist_get_list(void)
int router_addrs_in_same_network(const tor_addr_t *a1, const tor_addr_t *a2)
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
const char * node_get_nickname(const node_t *node)
int node_is_dir(const node_t *node)
void node_get_addr(const node_t *node, tor_addr_t *addr_out)
void node_get_verbose_nickname(const node_t *node, char *verbose_name_out)
int nodes_in_same_family(const node_t *node1, const node_t *node2)
int router_have_minimum_dir_info(void)
Header file for nodelist.c.
Master header file for Tor-specific functionality.
#define MAX_VERBOSE_NICKNAME_LEN
The or_state_t structure, which represents Tor's state file.
Origin circuit structure.
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
int reachable_addr_allows_addr(const tor_addr_t *addr, uint16_t port, firewall_connection_t fw_connection, int pref_only, int pref_ipv6)
int reachable_addr_allows_node(const node_t *node, firewall_connection_t fw_connection, int pref_only)
Header file for policies.c.
int tor_asprintf(char **strp, const char *fmt,...)
int tor_snprintf(char *str, size_t size, const char *format,...)
int router_digest_is_me(const char *digest)
Header file for router.c.
routerset_t * routerset_new(void)
int routerset_contains_node(const routerset_t *set, const node_t *node)
int routerset_parse(routerset_t *target, const char *s, const char *description)
int routerset_contains_bridge(const routerset_t *set, const bridge_info_t *bridge)
Header file for routerset.c.
Routerstatus (consensus entry) structure.
int smartlist_ptrs_eq(const smartlist_t *s1, const smartlist_t *s2)
int smartlist_contains_digest(const smartlist_t *sl, const char *element)
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
void smartlist_remove_keeporder(smartlist_t *sl, const void *element)
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
int smartlist_contains(const smartlist_t *sl, const void *element)
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_clear(smartlist_t *sl)
void smartlist_remove(smartlist_t *sl, const void *element)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
#define SMARTLIST_DEL_CURRENT(sl, var)
#define SMARTLIST_DEL_CURRENT_KEEPORDER(sl, var)
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)
void or_state_mark_dirty(or_state_t *state, time_t when)
or_state_t * get_or_state(void)
double successful_circuits_closed
char identity[DIGEST_LEN]
char identity[DIGEST_LEN]
unsigned int is_possible_guard
struct routerset_t * EntryNodes
struct routerset_t * ExcludeNodes
struct config_line_t * Guard
struct circuit_guard_state_t * guard_state
#define MOCK_IMPL(rv, funcname, arglist)
void format_iso_time_nospace(char *buf, time_t t)
void format_iso_time(char *buf, time_t t)
void format_local_iso_time(char *buf, time_t t)
Headers for transports.c.
#define tor_assert_nonfatal_unreached()
#define FALLTHROUGH_UNLESS_ALL_BUGS_ARE_FATAL
int tor_digest_is_zero(const char *digest)