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);
1368 const time_t remove_if_unlisted_since,
1369 const time_t maybe_remove_if_sampled_before,
1370 const time_t remove_if_confirmed_before)
1372 size_t n_changes = 0;
1379 if (guard->currently_listed == 0 &&
1380 guard->unlisted_since_date < remove_if_unlisted_since) {
1386 log_info(
LD_GUARD,
"Removing sampled guard %s: it has been unlisted "
1390 }
else if (guard->sampled_on_date < maybe_remove_if_sampled_before) {
1395 if (guard->confirmed_on_date == 0) {
1397 log_info(
LD_GUARD,
"Removing sampled guard %s: it was sampled "
1398 "over %d days ago, but never confirmed.",
1401 }
else if (guard->confirmed_on_date < remove_if_confirmed_before) {
1403 log_info(
LD_GUARD,
"Removing sampled guard %s: it was sampled "
1404 "over %d days ago, and confirmed over %d days ago.",
1415 entry_guard_free(guard);
1417 } SMARTLIST_FOREACH_END(guard);
1435 log_info(
LD_GUARD,
"Not updating the sample guard set; we have "
1436 "no reasonably live consensus.");
1439 log_info(
LD_GUARD,
"Updating sampled guard status based on received "
1445 const time_t remove_if_unlisted_since =
1447 const time_t maybe_remove_if_sampled_before =
1449 const time_t remove_if_confirmed_before =
1455 remove_if_unlisted_since,
1456 maybe_remove_if_sampled_before,
1457 remove_if_confirmed_before);
1460 gs->primary_guards_up_to_date = 0;
1514 FIREWALL_OR_CONNECTION,
1527 entry_guard_t *guard)
1529 if (guard->currently_listed == 0)
1531 if (guard->pb.path_bias_disabled)
1534 if (gs->type == GS_TYPE_BRIDGE) {
1571 if (
get_options()->EnforceDistinctSubnets && guard->bridge_addr) {
1575 &guard->bridge_addr->addr)) {
1585STATIC entry_guard_restriction_t *
1586guard_create_exit_restriction(
const uint8_t *exit_id)
1588 entry_guard_restriction_t *rst = NULL;
1589 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1590 rst->type = RST_EXIT_NODE;
1591 memcpy(rst->exclude_id, exit_id,
DIGEST_LEN);
1597STATIC entry_guard_restriction_t *
1600 entry_guard_restriction_t *rst = NULL;
1601 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1602 rst->type = RST_EXCL_LIST;
1610#define MIN_GUARDS_FOR_MD_RESTRICTION 10
1623 log_info(
LD_GUARD,
"Not setting md restriction: only %d"
1624 " usable guards.", num_usable_guards);
1634STATIC entry_guard_restriction_t *
1637 entry_guard_restriction_t *rst = NULL;
1640 log_debug(
LD_GUARD,
"Not setting md restriction: too few "
1641 "filtered guards.");
1645 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1646 rst->type = RST_OUTDATED_MD_DIRSERVER;
1653guard_obeys_exit_restriction(
const entry_guard_t *guard,
1654 const entry_guard_restriction_t *rst)
1673 log_info(
LD_GENERAL,
"Skipping %s dirserver: outdated",
1678 log_debug(
LD_GENERAL,
"%s dirserver obeys md restrictions",
1690 const entry_guard_restriction_t *rst)
1696 if (rst->type == RST_EXIT_NODE) {
1697 return guard_obeys_exit_restriction(guard, rst);
1698 }
else if (rst->type == RST_OUTDATED_MD_DIRSERVER) {
1700 }
else if (rst->type == RST_EXCL_LIST) {
1713 guard_selection_t *gs,
1714 entry_guard_t *guard)
1716 unsigned was_filtered = guard->is_filtered_guard;
1717 guard->is_filtered_guard = 0;
1718 guard->is_usable_filtered_guard = 0;
1721 guard->is_filtered_guard = 1;
1723 if (guard->is_reachable != GUARD_REACHABLE_NO)
1724 guard->is_usable_filtered_guard = 1;
1728 log_debug(
LD_GUARD,
"Updated sampled guard %s: filtered=%d; "
1730 guard->is_filtered_guard, guard->is_usable_filtered_guard);
1732 if (!
bool_eq(was_filtered, guard->is_filtered_guard)) {
1734 gs->primary_guards_up_to_date = 0;
1748 } SMARTLIST_FOREACH_END(guard);
1764 const entry_guard_restriction_t *rst,
1768 entry_guard_t *result = NULL;
1769 const unsigned exclude_confirmed = flags & SAMPLE_EXCLUDE_CONFIRMED;
1770 const unsigned exclude_primary = flags & SAMPLE_EXCLUDE_PRIMARY;
1771 const unsigned exclude_pending = flags & SAMPLE_EXCLUDE_PENDING;
1772 const unsigned no_update_primary = flags & SAMPLE_NO_UPDATE_PRIMARY;
1773 const unsigned need_descriptor = flags & SAMPLE_EXCLUDE_NO_DESCRIPTOR;
1777 } SMARTLIST_FOREACH_END(guard);
1781 log_info(
LD_GUARD,
"Trying to sample a reachable guard: We know of %d "
1782 "in the USABLE_FILTERED set.", n_reachable_filtered);
1785 if (n_reachable_filtered < min_filtered_sample) {
1786 log_info(
LD_GUARD,
" (That isn't enough. Trying to expand the sample.)");
1790 if (exclude_primary && !gs->primary_guards_up_to_date && !no_update_primary)
1799 if (! guard->is_usable_filtered_guard)
1801 if (exclude_confirmed && guard->confirmed_idx >= 0)
1803 if (exclude_primary && guard->is_primary)
1805 if (exclude_pending && guard->is_pending)
1810 } SMARTLIST_FOREACH_END(guard);
1812 log_info(
LD_GUARD,
" (After filters [%x], we have %d guards to consider.)",
1813 flags, smartlist_len(reachable_filtered_sample));
1815 if (smartlist_len(reachable_filtered_sample)) {
1826 result = smartlist_get(reachable_filtered_sample, 0);
1827 log_info(
LD_GUARD,
" (Selected %s.)",
1830 smartlist_free(reachable_filtered_sample);
1836compare_guards_by_confirmed_idx(
const void **a_,
const void **b_)
1838 const entry_guard_t *a = *a_, *b = *b_;
1839 if (a->confirmed_idx < b->confirmed_idx)
1841 else if (a->confirmed_idx > b->confirmed_idx)
1853 const entry_guard_t *a = *a_, *b = *b_;
1854 if (a->sampled_idx < b->sampled_idx)
1856 else if (a->sampled_idx > b->sampled_idx)
1872 if (guard->confirmed_idx >= 0)
1874 } SMARTLIST_FOREACH_END(guard);
1876 smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_confirmed_idx);
1878 int any_changed = 0;
1880 if (guard->confirmed_idx != guard_sl_idx) {
1882 guard->confirmed_idx = guard_sl_idx;
1884 } SMARTLIST_FOREACH_END(guard);
1886 gs->next_confirmed_idx = smartlist_len(gs->confirmed_entry_guards);
1902 if (BUG(guard->confirmed_on_date && guard->confirmed_idx >= 0))
1911 log_info(
LD_GUARD,
"Marking %s as a confirmed guard (index %d)",
1913 gs->next_confirmed_idx);
1915 guard->confirmed_idx = gs->next_confirmed_idx++;
1923 gs->primary_guards_up_to_date = 0;
1938 static int running = 0;
1949 gs->primary_guards_up_to_date = 1;
1953 if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
1955 if (! guard->is_filtered_guard)
1957 guard->is_primary = 1;
1959 } SMARTLIST_FOREACH_END(guard);
1970 if (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS &&
1971 guard->is_filtered_guard) {
1972 guard->is_primary = 1;
1977 guard->is_primary = 0;
1979 } SMARTLIST_FOREACH_END(guard);
1982 while (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS) {
1984 SAMPLE_EXCLUDE_CONFIRMED|
1985 SAMPLE_EXCLUDE_PRIMARY|
1986 SAMPLE_NO_UPDATE_PRIMARY);
1989 guard->is_primary = 1;
1996 tor_assert_nonfatal(
1997 bool_eq(guard->is_primary,
1998 smartlist_contains(new_primary_guards, guard)));
2003 new_primary_guards);
2005 log_info(
LD_GUARD,
"Primary entry guards have changed. "
2006 "New primary guard list is: ");
2007 int n = smartlist_len(new_primary_guards);
2009 log_info(
LD_GUARD,
" %d/%d: %s%s%s",
2011 g->confirmed_idx >= 0 ?
" (confirmed)" :
"",
2012 g->is_filtered_guard ?
"" :
" (excluded by filter)");
2013 } SMARTLIST_FOREACH_END(g);
2017 smartlist_free(old_primary_guards);
2018 smartlist_free(gs->primary_entry_guards);
2019 gs->primary_entry_guards = new_primary_guards;
2020 gs->primary_guards_up_to_date = 1;
2032 const unsigned SIX_HOURS = 6 * 3600;
2033 const unsigned FOUR_DAYS = 4 * 86400;
2034 const unsigned SEVEN_DAYS = 7 * 86400;
2037 if (now > failing_since) {
2038 tdiff = now - failing_since;
2044 time_t maximum;
int primary_delay;
int nonprimary_delay;
2047 { SIX_HOURS, 10*60, 1*60*60 },
2048 { FOUR_DAYS, 90*60, 4*60*60 },
2049 { SEVEN_DAYS, 4*60*60, 18*60*60 },
2050 { TIME_MAX, 9*60*60, 36*60*60 }
2056 if (tdiff <= delays[i].maximum) {
2057 return is_primary ? delays[i].primary_delay : delays[i].nonprimary_delay;
2073 if (guard->is_reachable != GUARD_REACHABLE_NO)
2079 const time_t last_attempt = guard->last_tried_to_connect;
2089 if (BUG(last_attempt == 0) ||
2090 now >= last_attempt + delay) {
2092 char tbuf[ISO_TIME_LEN+1];
2094 log_info(
LD_GUARD,
"Marked %s%sguard %s for possible retry, since we "
2095 "haven't tried to use it since %s.",
2096 guard->is_primary?
"primary ":
"",
2097 guard->confirmed_idx>=0?
"confirmed ":
"",
2101 guard->is_reachable = GUARD_REACHABLE_MAYBE;
2102 if (guard->is_filtered_guard)
2103 guard->is_usable_filtered_guard = 1;
2121static entry_guard_t *
2124 const entry_guard_restriction_t *rst,
2125 unsigned *state_out)
2127 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2128 entry_guard_t *chosen_guard = NULL;
2136 log_info(
LD_GUARD,
"Entry guard %s doesn't obey restriction, we test the"
2140 if (guard->is_reachable != GUARD_REACHABLE_NO) {
2142 log_info(
LD_GUARD,
"Guard %s does not have a descriptor",
2146 *state_out = GUARD_CIRC_STATE_USABLE_ON_COMPLETION;
2149 if (smartlist_len(usable_primary_guards) >= num_entry_guards)
2152 } SMARTLIST_FOREACH_END(guard);
2154 if (smartlist_len(usable_primary_guards)) {
2157 "Selected primary guard %s for circuit from a list size of %d.",
2159 smartlist_len(usable_primary_guards));
2160 smartlist_free(usable_primary_guards);
2163 smartlist_free(usable_primary_guards);
2164 return chosen_guard;
2173static entry_guard_t *
2176 const entry_guard_restriction_t *rst,
2177 unsigned *state_out)
2179 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2182 if (guard->is_primary)
2187 if (guard->is_usable_filtered_guard && ! guard->is_pending) {
2190 guard->is_pending = 1;
2192 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2193 log_info(
LD_GUARD,
"No primary guards available. Selected confirmed "
2194 "guard %s for circuit. Will try other guards before using "
2199 } SMARTLIST_FOREACH_END(guard);
2210static entry_guard_t *
2213 const entry_guard_restriction_t *rst,
2214 unsigned *state_out)
2216 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2217 entry_guard_t *chosen_guard = NULL;
2219 if (need_descriptor)
2220 flags |= SAMPLE_EXCLUDE_NO_DESCRIPTOR;
2223 SAMPLE_EXCLUDE_CONFIRMED |
2224 SAMPLE_EXCLUDE_PRIMARY |
2225 SAMPLE_EXCLUDE_PENDING |
2227 if (!chosen_guard) {
2231 chosen_guard->is_pending = 1;
2232 chosen_guard->last_tried_to_connect =
approx_time();
2233 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2234 log_info(
LD_GUARD,
"No primary or confirmed guards available. Selected "
2235 "guard %s for circuit. Will try other guards before "
2236 "using this circuit.",
2238 return chosen_guard;
2252 const entry_guard_restriction_t *rst,
2253 unsigned *state_out)
2255 entry_guard_t *chosen_guard = NULL;
2259 if (!gs->primary_guards_up_to_date)
2266 return chosen_guard;
2274 return chosen_guard;
2280 if (chosen_guard == NULL) {
2281 log_info(
LD_GUARD,
"Absolutely no sampled guards were available. "
2282 "Marking all guards for retry and starting from top again.");
2283 mark_all_guards_maybe_reachable(gs);
2287 return chosen_guard;
2296 entry_guard_t *guard)
2300 guard->is_reachable = GUARD_REACHABLE_NO;
2301 guard->is_usable_filtered_guard = 0;
2303 guard->is_pending = 0;
2304 if (guard->failing_since == 0)
2310 log_info(
LD_GUARD,
"Recorded failure for %s%sguard %s",
2311 guard->is_primary?
"primary ":
"",
2312 guard->confirmed_idx>=0?
"confirmed ":
"",
2333 entry_guard_t *guard,
2339 const time_t last_time_on_internet = gs->last_time_on_internet;
2343 if (guard->is_reachable != GUARD_REACHABLE_YES) {
2353 guard->is_reachable = GUARD_REACHABLE_YES;
2354 guard->failing_since = 0;
2355 guard->is_pending = 0;
2356 if (guard->is_filtered_guard)
2357 guard->is_usable_filtered_guard = 1;
2359 if (guard->confirmed_idx < 0) {
2361 if (!gs->primary_guards_up_to_date)
2366 switch (old_state) {
2367 case GUARD_CIRC_STATE_COMPLETE:
2368 case GUARD_CIRC_STATE_USABLE_ON_COMPLETION:
2369 new_state = GUARD_CIRC_STATE_COMPLETE;
2374 case GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD:
2375 if (guard->is_primary) {
2383 new_state = GUARD_CIRC_STATE_COMPLETE;
2385 new_state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2390 if (! guard->is_primary) {
2397 log_info(
LD_GUARD,
"Recorded success for %s%sguard %s",
2398 guard->is_primary?
"primary ":
"",
2399 guard->confirmed_idx>=0?
"confirmed ":
"",
2417 if (a->confirmed_idx < 0) {
2418 if (b->confirmed_idx >= 0)
2421 if (b->confirmed_idx < 0)
2425 return (a->confirmed_idx < b->confirmed_idx);
2430 if (a->is_pending) {
2431 if (! b->is_pending)
2435 return a->last_tried_to_connect < b->last_tried_to_connect;
2449 if (rst && rst->excluded) {
2452 smartlist_free(rst->excluded);
2465 entry_guard_restriction_free(state->restrictions);
2466 entry_guard_handle_free(state->guard);
2474 entry_guard_restriction_t *rst))
2476 circuit_guard_state_t *result;
2478 result = tor_malloc_zero(
sizeof(circuit_guard_state_t));
2479 result->guard = entry_guard_handle_new(guard);
2480 result->state = state;
2482 result->restrictions = rst;
2500 entry_guard_restriction_t *rst,
2501 const node_t **chosen_node_out,
2502 circuit_guard_state_t **guard_state_out)
2507 *chosen_node_out = NULL;
2508 *guard_state_out = NULL;
2511 entry_guard_t *guard =
2515 if (BUG(state == 0))
2521 if (BUG(usage != GUARD_USAGE_DIRGUARD &&
2525 *chosen_node_out = node;
2530 entry_guard_restriction_free(rst);
2546 if (BUG(*guard_state_p == NULL))
2547 return GUARD_USABLE_NEVER;
2549 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2550 if (! guard || BUG(guard->in_selection == NULL))
2551 return GUARD_USABLE_NEVER;
2555 (*guard_state_p)->state);
2557 (*guard_state_p)->state = newstate;
2560 if (newstate == GUARD_CIRC_STATE_COMPLETE) {
2561 return GUARD_USABLE_NOW;
2563 return GUARD_MAYBE_USABLE_LATER;
2573 if (BUG(*guard_state_p == NULL))
2575 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2581 guard->is_pending = 0;
2582 circuit_guard_state_free(*guard_state_p);
2583 *guard_state_p = NULL;
2594 if (BUG(*guard_state_p == NULL))
2597 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2598 if (! guard || BUG(guard->in_selection == NULL))
2603 (*guard_state_p)->state = GUARD_CIRC_STATE_DEAD;
2629 } SMARTLIST_FOREACH_END(circ);
2630 smartlist_free(pending);
2641 if (!gs->primary_guards_up_to_date)
2645 if (guard->is_reachable != GUARD_REACHABLE_NO)
2647 } SMARTLIST_FOREACH_END(guard);
2660 const entry_guard_restriction_t *rst,
2669 entry_guard_t *guard_a = entry_guard_handle_get(state_a->guard);
2670 entry_guard_t *guard_b = entry_guard_handle_get(state_b->guard);
2675 }
else if (! guard_b) {
2707 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2708 "but not all primary guards were definitely down.");
2714 int n_complete_blocking = 0;
2723 entry_guard_t *guard = entry_guard_handle_get(state->guard);
2724 if (!guard || guard->in_selection != gs)
2732 } SMARTLIST_FOREACH_END(circ);
2736 if (BUG(state == NULL))
2739 if (state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD) {
2741 if (! best_waiting_circuit ||
2743 best_waiting_circuit = circ;
2746 } SMARTLIST_FOREACH_END(circ);
2748 if (! best_waiting_circuit) {
2749 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2750 "but didn't find any.");
2757 const entry_guard_restriction_t *rst_on_best_waiting =
2770 if (BUG(state == NULL))
2772 if (state->state != GUARD_CIRC_STATE_COMPLETE)
2776 best_waiting_circuit))
2777 ++n_complete_blocking;
2778 } SMARTLIST_FOREACH_END(circ);
2780 if (n_complete_blocking) {
2781 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2782 "%d complete and %d guard-stalled. At least one complete "
2783 "circuit had higher priority, so not upgrading.",
2784 n_complete, n_waiting);
2793 int n_blockers_found = 0;
2794 const time_t state_set_at_cutoff =
2798 if (BUG(state == NULL))
2800 if (state->state != GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD)
2802 if (state->state_set_at <= state_set_at_cutoff)
2805 best_waiting_circuit))
2807 } SMARTLIST_FOREACH_END(circ);
2809 if (n_blockers_found) {
2810 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2811 "%d guard-stalled, but %d pending circuit(s) had higher "
2812 "guard priority, so not upgrading.",
2813 n_waiting, n_blockers_found);
2820 int n_succeeded = 0;
2823 if (BUG(state == NULL))
2825 if (circ != best_waiting_circuit && rst_on_best_waiting) {
2830 if (state->state != GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD)
2835 state->state = GUARD_CIRC_STATE_COMPLETE;
2839 } SMARTLIST_FOREACH_END(circ);
2841 log_info(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2842 "%d guard-stalled, %d complete. %d of the guard-stalled "
2843 "circuit(s) had high enough priority to upgrade.",
2844 n_waiting, n_complete, n_succeeded);
2846 tor_assert_nonfatal(n_succeeded >= 1);
2847 smartlist_free(all_circuits);
2851 smartlist_free(all_circuits);
2862 if (guard_state == NULL)
2864 const time_t expire_if_waiting_since =
2866 return (guard_state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
2867 && guard_state->state_set_at < expire_if_waiting_since);
2902 char tbuf[ISO_TIME_LEN+1];
2909 if (guard->bridge_addr) {
2912 guard->bridge_addr->port);
2922 if (guard->sampled_by_version) {
2924 guard->sampled_by_version);
2927 if (guard->unlisted_since_date > 0) {
2933 (
int)guard->currently_listed);
2935 if (guard->confirmed_idx >= 0) {
2942 const double EPSILON = 1.0e-6;
2950 #define PB_FIELD(field) do { \
2951 if (pb->field >= EPSILON) { \
2952 smartlist_add_asprintf(result, "pb_" #field "=%f", pb->field); \
2955 PB_FIELD(use_attempts);
2956 PB_FIELD(use_successes);
2957 PB_FIELD(circ_attempts);
2958 PB_FIELD(circ_successes);
2959 PB_FIELD(successful_circuits_closed);
2960 PB_FIELD(collapsed_circuits);
2961 PB_FIELD(unusable_circuits);
2966 if (guard->extra_state_fields)
2971 smartlist_free(result);
2982 *extra, strmap_t *vals)
2985 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
2988 const char *eq = strchr(entry,
'=');
2993 char *key = tor_strndup(entry, eq-entry);
2994 char **target = strmap_get(vals, key);
2995 if (target == NULL || *target != NULL) {
3002 *target = tor_strdup(eq+1);
3005 } SMARTLIST_FOREACH_END(entry);
3013 *unlisted_since,
char *confirmed_on)
3015#define HANDLE_TIME(field) do { \
3017 int r = parse_iso_time_nospace(field, &field ## _time); \
3019 log_warn(LD_CIRC, "Unable to parse %s %s from guard", \
3020 #field, escaped(field)); \
3021 field##_time = -1; \
3026 time_t sampled_on_time = 0;
3027 time_t unlisted_since_time = 0;
3028 time_t confirmed_on_time = 0;
3030 HANDLE_TIME(sampled_on);
3031 HANDLE_TIME(unlisted_since);
3032 HANDLE_TIME(confirmed_on);
3034 if (sampled_on_time <= 0)
3036 if (unlisted_since_time < 0)
3037 unlisted_since_time = 0;
3038 if (confirmed_on_time < 0)
3039 confirmed_on_time = 0;
3043 guard->sampled_on_date = sampled_on_time;
3044 guard->unlisted_since_date = unlisted_since_time;
3045 guard->confirmed_on_date = confirmed_on_time;
3061 char *rsa_id = NULL;
3062 char *nickname = NULL;
3063 char *sampled_on = NULL;
3064 char *sampled_idx = NULL;
3065 char *sampled_by = NULL;
3066 char *unlisted_since = NULL;
3067 char *listed = NULL;
3068 char *confirmed_on = NULL;
3069 char *confirmed_idx = NULL;
3070 char *bridge_addr = NULL;
3073 char *pb_use_attempts = NULL;
3074 char *pb_use_successes = NULL;
3075 char *pb_circ_attempts = NULL;
3076 char *pb_circ_successes = NULL;
3077 char *pb_successful_circuits_closed = NULL;
3078 char *pb_collapsed_circuits = NULL;
3079 char *pb_unusable_circuits = NULL;
3080 char *pb_timeouts = NULL;
3088 strmap_t *vals = strmap_new();
3090 strmap_set(vals, #f, &f);
3097 FIELD(unlisted_since);
3099 FIELD(confirmed_on);
3100 FIELD(confirmed_idx);
3102 FIELD(pb_use_attempts);
3103 FIELD(pb_use_successes);
3104 FIELD(pb_circ_attempts);
3105 FIELD(pb_circ_successes);
3106 FIELD(pb_successful_circuits_closed);
3107 FIELD(pb_collapsed_circuits);
3108 FIELD(pb_unusable_circuits);
3114 smartlist_free(entries);
3115 strmap_free(vals, NULL);
3118 entry_guard_t *guard = tor_malloc_zero(
sizeof(entry_guard_t));
3119 guard->is_persistent = 1;
3122 log_warn(
LD_CIRC,
"Guard missing 'in' field");
3126 guard->selection_name = in;
3129 if (rsa_id == NULL) {
3130 log_warn(
LD_CIRC,
"Guard missing RSA ID field");
3137 log_warn(
LD_CIRC,
"Unable to decode guard identity %s",
escaped(rsa_id));
3142 strlcpy(guard->nickname, nickname,
sizeof(guard->nickname));
3144 guard->nickname[0]=
'$';
3151 memset(&res, 0,
sizeof(res));
3153 &res.addr, &res.port, -1);
3155 guard->bridge_addr = tor_memdup(&res,
sizeof(res));
3164 guard->sampled_by_version = sampled_by;
3167 if (listed && strcmp(listed,
"0"))
3168 guard->currently_listed = 1;
3171 guard->confirmed_idx = -1;
3172 if (confirmed_idx) {
3174 long idx =
tor_parse_long(confirmed_idx, 10, 0, INT_MAX, &ok, NULL);
3176 log_warn(
LD_GUARD,
"Guard has invalid confirmed_idx %s",
3179 guard->confirmed_idx = (int)idx;
3185 long idx =
tor_parse_long(sampled_idx, 10, 0, INT_MAX, &ok, NULL);
3187 log_warn(
LD_GUARD,
"Guard has invalid sampled_idx %s",
3190 guard->sampled_idx = invalid_sampled_idx++;
3192 guard->sampled_idx = (int)idx;
3194 }
else if (confirmed_idx) {
3198 guard->sampled_idx = guard->confirmed_idx;
3200 log_info(
LD_GUARD,
"The state file seems to be into a status that could"
3201 " yield to weird entry node selection: we're missing both a"
3202 " sampled_idx and a confirmed_idx.");
3203 guard->sampled_idx = invalid_sampled_idx++;
3207 if (smartlist_len(extra) > 0) {
3212 guard->is_reachable = GUARD_REACHABLE_MAYBE;
3214#define PB_FIELD(field) \
3216 if (pb_ ## field) { \
3218 double r = tor_parse_double(pb_ ## field, 0.0, 1e9, &ok, NULL); \
3220 log_warn(LD_CIRC, "Guard has invalid pb_%s %s", \
3221 #field, pb_ ## field); \
3223 guard->pb.field = r; \
3227 PB_FIELD(use_attempts);
3228 PB_FIELD(use_successes);
3229 PB_FIELD(circ_attempts);
3230 PB_FIELD(circ_successes);
3231 PB_FIELD(successful_circuits_closed);
3232 PB_FIELD(collapsed_circuits);
3233 PB_FIELD(unusable_circuits);
3247 entry_guard_free(guard);
3266 tor_free(pb_successful_circuits_closed);
3272 smartlist_free(extra);
3292 if (guard->is_persistent == 0)
3295 (*nextline)->key = tor_strdup(
"Guard");
3297 nextline = &(*nextline)->next;
3299 } SMARTLIST_FOREACH_END(guard);
3300 } SMARTLIST_FOREACH_END(gs);
3302 config_free_lines(state->
Guard);
3303 state->
Guard = lines;
3324 guard_selection_free(gs);
3328 } SMARTLIST_FOREACH_END(gs);
3331 for ( ; line != NULL; line = line->next) {
3333 if (guard == NULL) {
3338 if (!strcmp(guard->selection_name,
"legacy")) {
3340 entry_guard_free(guard);
3345 guard_selection_t *gs;
3350 guard->in_selection = gs;
3355 if (gs->next_sampled_idx <= guard->sampled_idx) {
3356 gs->next_sampled_idx = guard->sampled_idx + 1;
3360 entry_guard_free(guard);
3371 } SMARTLIST_FOREACH_END(gs);
3373 return n_errors ? -1 : 0;
3407circuit_guard_state_t *
3410 circuit_guard_state_t *guard_state = NULL;
3411 entry_guard_t *guard = NULL;
3425 GUARD_CIRC_STATE_USABLE_ON_COMPLETION,
3437 entry_guard_handles_clear(e);
3478 if (BUG(gs->type != GS_TYPE_BRIDGE)) {
3484 if (! guard->is_filtered_guard)
3487 if (guard->is_reachable == GUARD_REACHABLE_NO)
3490 if (!use_maybe_reachable && guard->is_reachable == GUARD_REACHABLE_MAYBE)
3495 if (node && node->ri)
3497 } SMARTLIST_FOREACH_END(guard);
3508 const double EPSILON = 1.0e-9;
3513 if (node->pb.use_attempts >
EPSILON &&
3517 node->pb.path_bias_disabled = 1;
3519 "Path use bias is too high (%f/%f); disabling node %s",
3520 node->pb.circ_successes, node->pb.circ_attempts,
3531 const double EPSILON = 1.0e-9;
3536 if (node->pb.circ_attempts >
EPSILON &&
3540 node->pb.path_bias_disabled = 1;
3542 "Path bias is too high (%f/%f); disabling node %s",
3543 node->pb.circ_successes, node->pb.circ_attempts,
3562 if (msg && *msg == NULL) {
3563 *msg = tor_strdup(
"parsing error");
3572#define SLOW_GUARD_STATE_FLUSH_TIME 600
3575#define FAST_GUARD_STATE_FLUSH_TIME 30
3650 entry_guard_t *guard = entry_guard_handle_get(guard_state->guard);
3651 if (!guard || BUG(guard->in_selection == NULL)) {
3665 const char *status = NULL;
3668 char tbuf[ISO_TIME_LEN+1];
3676 if (e->confirmed_idx < 0) {
3677 status =
"never-connected";
3678 }
else if (! e->currently_listed) {
3679 when = e->unlisted_since_date;
3680 status =
"unusable";
3681 }
else if (! e->is_filtered_guard) {
3682 status =
"unusable";
3683 }
else if (e->is_reachable == GUARD_REACHABLE_NO) {
3684 when = e->failing_since;
3700 char *result = NULL;
3703 tor_asprintf(&result,
"%s %s %s\n", nbuf, status, tbuf);
3721 const char *question,
char **answer,
3722 const char **errmsg)
3731 if (!strcmp(question,
"entry-guards") ||
3732 !strcmp(question,
"helper-nodes")) {
3734 guards = gs->sampled_entry_guards;
3741 } SMARTLIST_FOREACH_END(e);
3768 uint32_t guardfraction_percentage)
3770 double guardfraction_fraction;
3774 guardfraction_fraction = guardfraction_percentage / 100.0;
3776 long guard_bw =
tor_lround(guardfraction_fraction * orig_bandwidth);
3779 guardfraction_bw->
guard_bw = (int) guard_bw;
3781 guardfraction_bw->
non_guard_bw = orig_bandwidth - (int) guard_bw;
3792 int mark_circuits = 0;
3801 return mark_circuits;
3810 circuit_guard_state_t **guard_state_out)
3813 const uint8_t *exit_id = NULL;
3814 entry_guard_restriction_t *rst = NULL;
3818 rst = guard_create_conflux_restriction(circ);
3828 rst = guard_create_exit_restriction(exit_id);
3832 GUARD_USAGE_TRAFFIC,
3835 guard_state_out) < 0) {
3849 char *old_name = tor_strdup(gs->name);
3850 guard_selection_type_t old_type = gs->type;
3853 control_event_guard(entry->nickname, entry->identity,
"DROPPED");
3861 guard_selection_free(gs);
3882 circuit_guard_state_t **guard_state_out)
3885 entry_guard_restriction_t *rst = NULL;
3893 GUARD_USAGE_DIRGUARD,
3896 guard_state_out) < 0) {
3926 int num_present,
int num_usable)
3928 if (!gs->primary_guards_up_to_date)
3931 char *ret_str = NULL;
3932 int n_missing_descriptors = 0;
3933 int n_considered = 0;
3934 int num_primary_to_check;
3940 num_primary_to_check++;
3944 if (guard->is_reachable == GUARD_REACHABLE_NO)
3948 n_missing_descriptors++;
3949 if (n_considered >= num_primary_to_check)
3951 } SMARTLIST_FOREACH_END(guard);
3954 if (!n_missing_descriptors) {
3959 tor_asprintf(&ret_str,
"We're missing descriptors for %d/%d of our "
3960 "primary entry guards (total %sdescriptors: %d/%d). "
3961 "That's ok. We will try to fetch missing descriptors soon.",
3962 n_missing_descriptors, num_primary_to_check,
3963 using_mds?
"micro":
"", num_present, num_usable);
3972 int num_present,
int num_usable)
3977 num_present, num_usable);
3988 if (gs->sampled_entry_guards) {
3990 entry_guard_free(e));
3991 smartlist_free(gs->sampled_entry_guards);
3992 gs->sampled_entry_guards = NULL;
3995 smartlist_free(gs->confirmed_entry_guards);
3996 smartlist_free(gs->primary_entry_guards);
4015#define layer2_guard_free(val) \
4016 FREE_AND_NULL(layer2_guard_t, layer2_guard_free_, (val))
4057static routerset_t *layer2_routerset = NULL;
4060#define NUMBER_SECOND_GUARDS 4
4067#define MIN_SECOND_GUARD_LIFETIME (3600*24)
4068#define MAX_SECOND_GUARD_LIFETIME (3600*24*12)
4075 "guard-hs-l2-number",
4085 "guard-hs-l2-lifetime-min",
4095 "guard-hs-l2-lifetime-max",
4096 MAX_SECOND_GUARD_LIFETIME,
4111 if (BUG(min >= max)) {
4137 log_info(
LD_GENERAL,
"Removing expired Layer2 guard %s",
4141 layer2_guard_free(g);
4149 log_info(
LD_GENERAL,
"Removing %s Layer2 guard %s",
4150 rs ?
"unsuitable" :
"missing",
4154 layer2_guard_free(g);
4158 } SMARTLIST_FOREACH_END(g);
4161 int new_guards_needed_n =
4163 if (new_guards_needed_n <= 0) {
4167 log_info(
LD_GENERAL,
"Adding %d guards to Layer2 routerset",
4168 new_guards_needed_n);
4178 } SMARTLIST_FOREACH_END(g);
4181 for (
int i = 0; i < new_guards_needed_n; i++) {
4182 const node_t *choice = NULL;
4197 log_info(
LD_GENERAL,
"Adding Layer2 guard %s",
4208 smartlist_free(excluded);
4211 routerset_free(layer2_routerset);
4218 } SMARTLIST_FOREACH_END(g);
4234 layer2_guard_free(g);
4235 } SMARTLIST_FOREACH_END(g);
4255 return layer2_routerset;
4270 guard_selection_free(gs);
4271 } SMARTLIST_FOREACH_END(gs);
4282 layer2_guard_free(g);
4283 } SMARTLIST_FOREACH_END(g);
4286 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 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)
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)
char identity[DIGEST_LEN]
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)