116#define ENTRYNODES_PRIVATE
132#include "feature/client/circpathbias.h"
168 guard_selection_t *gs,
169 entry_guard_t *guard);
177 const uint8_t *rsa_id_digest,
178 const char *nickname,
183 const entry_guard_restriction_t *rst);
222STATIC guard_selection_type_t
226 if (type == GS_TYPE_INFER) {
227 if (!strcmp(
name,
"bridges"))
228 type = GS_TYPE_BRIDGE;
229 else if (!strcmp(
name,
"restricted"))
230 type = GS_TYPE_RESTRICTED;
232 type = GS_TYPE_NORMAL;
242 guard_selection_type_t type)
244 guard_selection_t *gs;
248 gs = tor_malloc_zero(
sizeof(*gs));
249 gs->name = tor_strdup(
name);
265 guard_selection_type_t type,
266 int create_if_absent)
272 if (!strcmp(gs->name,
name))
274 } SMARTLIST_FOREACH_END(gs);
276 if (! create_if_absent)
279 log_debug(
LD_GUARD,
"Creating a guard selection called %s",
name);
283 return new_selection;
297 guard_selection_type_t type = GS_TYPE_INFER;
307 log_notice(
LD_GUARD,
"Starting with guard context \"%s\"",
name);
327 static char buf[256];
330 strlen(guard->nickname) ? guard->nickname :
"[bridge]",
339 return guard->identity;
349HANDLE_IMPL(entry_guard, entry_guard_t, ATTR_UNUSED
STATIC)
360 time_t earliest = now - max_backdate;
364 if (latest <= earliest)
365 latest = earliest + 1;
386 DFLT_MAX_SAMPLE_THRESHOLD_PERCENT,
397 DFLT_MAX_SAMPLE_SIZE,
407 DFLT_MIN_FILTERED_SAMPLE_SIZE,
417 "guard-remove-unlisted-guards-after-days",
418 DFLT_REMOVE_UNLISTED_GUARDS_AFTER_DAYS,
443 "guard-lifetime-days",
444 DFLT_GUARD_LIFETIME_DAYS, 1, 365*10);
458 DFLT_GUARD_CONFIRMED_MIN_LIFETIME_DAYS,
471 if (configured_primaries) {
472 return configured_primaries;
478 "guard-n-primary-guards",
479 DFLT_N_PRIMARY_GUARDS, 1, INT32_MAX);
489 const char *param_name;
494 if (usage == GUARD_USAGE_DIRGUARD) {
496 param_name =
"guard-n-primary-dir-guards-to-use";
497 param_default = DFLT_N_PRIMARY_DIR_GUARDS_TO_USE;
500 param_name =
"guard-n-primary-guards-to-use";
501 param_default = DFLT_N_PRIMARY_GUARDS_TO_USE;
503 if (configured >= 1) {
507 param_name, param_default, 1, INT32_MAX);
517 DFLT_INTERNET_LIKELY_DOWN_INTERVAL,
529 "guard-nonprimary-guard-connect-timeout",
530 DFLT_NONPRIMARY_GUARD_CONNECT_TIMEOUT,
541 "guard-nonprimary-guard-idle-timeout",
542 DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT,
553 "guard-meaningful-restriction-percent",
554 DFLT_MEANINGFUL_RESTRICTION_PERCENT,
566 "guard-extreme-restriction-percent",
567 DFLT_EXTREME_RESTRICTION_PERCENT,
574mark_guard_maybe_reachable(entry_guard_t *guard)
576 if (guard->is_reachable != GUARD_REACHABLE_NO) {
582 guard->is_reachable = GUARD_REACHABLE_MAYBE;
583 if (guard->is_filtered_guard)
584 guard->is_usable_filtered_guard = 1;
612 if (!gs->primary_guards_up_to_date)
616 mark_guard_maybe_reachable(guard);
617 } SMARTLIST_FOREACH_END(guard);
623mark_all_guards_maybe_reachable(guard_selection_t *gs)
628 mark_guard_maybe_reachable(guard);
629 } SMARTLIST_FOREACH_END(guard);
642 const guard_selection_t *old_selection,
643 guard_selection_type_t *type_out)
649 *type_out = GS_TYPE_BRIDGE;
655 *type_out = GS_TYPE_NORMAL;
660 int n_guards = 0, n_passing_filter = 0;
668 } SMARTLIST_FOREACH_END(node);
672 const int meaningful_threshold_high =
674 const int meaningful_threshold_mid =
676 const int meaningful_threshold_low =
678 const int extreme_threshold =
695 static int have_warned_extreme_threshold = 0;
697 n_passing_filter < extreme_threshold &&
698 ! have_warned_extreme_threshold) {
699 have_warned_extreme_threshold = 1;
700 const double exclude_frac =
701 (n_guards - n_passing_filter) / (
double)n_guards;
702 log_warn(
LD_GUARD,
"Your configuration excludes %d%% of all possible "
703 "guards. That's likely to make you stand out from the "
704 "rest of the world.", (
int)(exclude_frac * 100));
709 if (old_selection == NULL) {
710 if (n_passing_filter >= meaningful_threshold_mid) {
711 *type_out = GS_TYPE_NORMAL;
714 *type_out = GS_TYPE_RESTRICTED;
724 if (n_passing_filter >= meaningful_threshold_high) {
725 *type_out = GS_TYPE_NORMAL;
727 }
else if (n_passing_filter < meaningful_threshold_low) {
728 *type_out = GS_TYPE_RESTRICTED;
732 *type_out = old_selection->type;
733 return old_selection->name;
753 guard_selection_type_t type = GS_TYPE_INFER;
765 if (! strcmp(cur_name, new_name)) {
767 "Staying with guard context \"%s\" (no change)", new_name);
771 log_notice(
LD_GUARD,
"Switching to guard context \"%s\" (was using \"%s\")",
773 guard_selection_t *new_guard_context;
806 const uint8_t *rsa_id)
813 } SMARTLIST_FOREACH_END(guard);
819static entry_guard_t *
825 entry_guard_t *guard;
840 const uint8_t *identity = NULL;
842 identity = (
const uint8_t *)guard->identity;
844 if (BUG(guard->bridge_addr == NULL))
848 &guard->bridge_addr->addr,
849 guard->bridge_addr->port,
850 (
const char*)identity);
871 log_info(
LD_GUARD,
"Adding %s to the entry guard sample set.",
890static entry_guard_t *
892 const uint8_t *rsa_id_digest,
893 const char *nickname,
902 if (BUG(!rsa_id_digest && !bridge_addrport))
905 entry_guard_t *guard = tor_malloc_zero(
sizeof(entry_guard_t));
908 guard->is_persistent = (rsa_id_digest != NULL);
909 guard->selection_name = tor_strdup(gs->name);
911 memcpy(guard->identity, rsa_id_digest,
DIGEST_LEN);
913 strlcpy(guard->nickname, nickname,
sizeof(guard->nickname));
915 tor_free(guard->sampled_by_version);
916 guard->sampled_by_version = tor_strdup(VERSION);
917 guard->currently_listed = 1;
918 guard->sampled_idx = gs->next_sampled_idx++;
919 guard->confirmed_idx = -1;
922 guard->is_reachable = GUARD_REACHABLE_MAYBE;
924 guard->bridge_addr = tor_memdup(bridge_addrport,
sizeof(*bridge_addrport));
927 guard->in_selection = gs;
942static entry_guard_t *
962static entry_guard_t *
973 } SMARTLIST_FOREACH_END(g);
982 const uint8_t *rsa_id_digest)
994 int make_persistent = 0;
997 memcpy(g->identity, rsa_id_digest,
DIGEST_LEN);
1001 if (BUG(! g->is_persistent))
1002 make_persistent = 1;
1005 base16_encode(old_id,
sizeof(old_id), g->identity,
sizeof(g->identity));
1006 log_warn(
LD_BUG,
"We 'learned' an identity %s for a bridge at %s:%d, but "
1007 "we already knew a different one (%s). Ignoring the new info as "
1015 if (make_persistent) {
1016 g->is_persistent = 1;
1031 const entry_guard_restriction_t *rst)
1033 int n_reachable_filtered_guards = 0;
1038 if (guard->is_usable_filtered_guard)
1039 ++n_reachable_filtered_guards;
1040 } SMARTLIST_FOREACH_END(guard);
1041 return n_reachable_filtered_guards;
1050 const int using_bridges = (gs->type == GS_TYPE_BRIDGE);
1061 const int max_sample = MIN(max_sample_by_pct, max_sample_absolute);
1062 if (max_sample < min_sample)
1078 guard_selection_t *gs,
1085 if (gs->type == GS_TYPE_BRIDGE) {
1093 } SMARTLIST_FOREACH_END(bridge);
1096 const int n_sampled = smartlist_len(gs->sampled_entry_guards);
1103 } SMARTLIST_FOREACH_END(guard);
1108 if (gs->type == GS_TYPE_RESTRICTED) {
1120 } SMARTLIST_FOREACH_END(node);
1123 digestset_free(sampled_guard_ids);
1126 *n_guards_out = n_guards;
1127 return eligible_guards;
1134static entry_guard_t *
1138 entry_guard_t *added_guard;
1139 if (gs->type == GS_TYPE_BRIDGE) {
1166 if (gs->type == GS_TYPE_BRIDGE) {
1188 log_info(
LD_GUARD,
"Not expanding the sample guard set; we have "
1189 "no reasonably live consensus.");
1193 int n_sampled = smartlist_len(gs->sampled_entry_guards);
1194 entry_guard_t *added_guard = NULL;
1202 log_info(
LD_GUARD,
"Expanding the sample guard set. We have %d guards "
1203 "in the sample, and %d eligible guards to extend it with.",
1204 n_sampled, smartlist_len(eligible_guards));
1206 while (n_usable_filtered_guards < min_filtered_sample) {
1208 if (n_sampled >= max_sample) {
1209 log_info(
LD_GUARD,
"Not expanding the guard sample any further; "
1210 "just hit the maximum sample threshold of %d",
1216 if (smartlist_len(eligible_guards) == 0) {
1221 log_info(
LD_GUARD,
"Not expanding the guard sample any further; "
1222 "just ran out of eligible guards");
1234 if (added_guard->is_usable_filtered_guard)
1235 ++n_usable_filtered_guards;
1239 smartlist_free(eligible_guards);
1248 entry_guard_t *guard)
1250 if (guard->is_primary) {
1251 guard->is_primary = 0;
1259 if (guard->confirmed_idx >= 0) {
1261 guard->confirmed_idx = -1;
1262 guard->confirmed_on_date = 0;
1278 if (gs->type == GS_TYPE_BRIDGE) {
1303 size_t n_changes = 0;
1307 const time_t unlisted_since_slop =
1314 if (is_listed && ! guard->currently_listed) {
1316 guard->currently_listed = 1;
1317 guard->unlisted_since_date = 0;
1318 log_info(
LD_GUARD,
"Sampled guard %s is now listed again.",
1320 }
else if (!is_listed && guard->currently_listed) {
1322 guard->currently_listed = 0;
1324 unlisted_since_slop);
1325 log_info(
LD_GUARD,
"Sampled guard %s is now unlisted.",
1327 }
else if (is_listed && guard->currently_listed) {
1328 log_debug(
LD_GUARD,
"Sampled guard %s is still listed.",
1331 tor_assert(! is_listed && ! guard->currently_listed);
1332 log_debug(
LD_GUARD,
"Sampled guard %s is still unlisted.",
1337 if (guard->currently_listed && guard->unlisted_since_date) {
1339 guard->unlisted_since_date = 0;
1340 log_warn(
LD_BUG,
"Sampled guard %s was listed, but with "
1341 "unlisted_since_date set. Fixing.",
1343 }
else if (!guard->currently_listed && ! guard->unlisted_since_date) {
1346 unlisted_since_slop);
1347 log_warn(
LD_BUG,
"Sampled guard %s was unlisted, but with "
1348 "unlisted_since_date unset. Fixing.",
1351 } SMARTLIST_FOREACH_END(guard);
1373 const time_t remove_if_unlisted_since,
1374 const time_t maybe_remove_if_sampled_before,
1375 const time_t remove_if_confirmed_before)
1377 size_t n_changes = 0;
1384 if (guard->currently_listed == 0 &&
1385 guard->unlisted_since_date < remove_if_unlisted_since) {
1392 log_info(
LD_GUARD,
"Removing sampled guard %s: it has been unlisted "
1395 }
else if (guard->confirmed_on_date == 0) {
1396 if (guard->sampled_on_date < maybe_remove_if_sampled_before) {
1400 log_info(
LD_GUARD,
"Removing sampled guard %s: it was sampled "
1401 "over %d days ago, but never confirmed.",
1406 if (guard->confirmed_on_date < remove_if_confirmed_before) {
1411 log_info(
LD_GUARD,
"Removing sampled guard %s: it was sampled "
1412 "over %d days ago, and confirmed over %d days ago.",
1423 entry_guard_free(guard);
1425 } SMARTLIST_FOREACH_END(guard);
1443 log_info(
LD_GUARD,
"Not updating the sample guard set; we have "
1444 "no reasonably live consensus.");
1447 log_info(
LD_GUARD,
"Updating sampled guard status based on received "
1453 const time_t remove_if_unlisted_since =
1455 const time_t maybe_remove_if_sampled_before =
1457 const time_t remove_if_confirmed_before =
1463 remove_if_unlisted_since,
1464 maybe_remove_if_sampled_before,
1465 remove_if_confirmed_before);
1468 gs->primary_guards_up_to_date = 0;
1522 FIREWALL_OR_CONNECTION,
1535 entry_guard_t *guard)
1537 if (guard->currently_listed == 0)
1539 if (guard->pb.path_bias_disabled)
1542 if (gs->type == GS_TYPE_BRIDGE) {
1579 if (
get_options()->EnforceDistinctSubnets && guard->bridge_addr) {
1583 &guard->bridge_addr->addr)) {
1593STATIC entry_guard_restriction_t *
1594guard_create_exit_restriction(
const uint8_t *exit_id)
1596 entry_guard_restriction_t *rst = NULL;
1597 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1598 rst->type = RST_EXIT_NODE;
1599 memcpy(rst->exclude_id, exit_id,
DIGEST_LEN);
1605STATIC entry_guard_restriction_t *
1607 const uint8_t *exit_id)
1609 entry_guard_restriction_t *rst = NULL;
1610 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1611 rst->type = RST_EXCL_LIST;
1614 memcpy(rst->exclude_id, exit_id,
DIGEST_LEN);
1620#define MIN_GUARDS_FOR_MD_RESTRICTION 10
1633 log_info(
LD_GUARD,
"Not setting md restriction: only %d"
1634 " usable guards.", num_usable_guards);
1644STATIC entry_guard_restriction_t *
1647 entry_guard_restriction_t *rst = NULL;
1650 log_debug(
LD_GUARD,
"Not setting md restriction: too few "
1651 "filtered guards.");
1655 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1656 rst->type = RST_OUTDATED_MD_DIRSERVER;
1663guard_obeys_exit_restriction(
const entry_guard_t *guard,
1664 const entry_guard_restriction_t *rst)
1667 rst->type == RST_EXCL_LIST);
1684 log_info(
LD_GENERAL,
"Skipping %s dirserver: outdated",
1689 log_debug(
LD_GENERAL,
"%s dirserver obeys md restrictions",
1703 return (rst->type == RST_OUTDATED_MD_DIRSERVER);
1712 const entry_guard_restriction_t *rst)
1718 if (rst->type == RST_EXIT_NODE) {
1719 return guard_obeys_exit_restriction(guard, rst);
1720 }
else if (rst->type == RST_OUTDATED_MD_DIRSERVER) {
1722 }
else if (rst->type == RST_EXCL_LIST) {
1723 return guard_obeys_exit_restriction(guard, rst) &&
1736 guard_selection_t *gs,
1737 entry_guard_t *guard)
1739 unsigned was_filtered = guard->is_filtered_guard;
1740 guard->is_filtered_guard = 0;
1741 guard->is_usable_filtered_guard = 0;
1744 guard->is_filtered_guard = 1;
1746 if (guard->is_reachable != GUARD_REACHABLE_NO)
1747 guard->is_usable_filtered_guard = 1;
1751 log_debug(
LD_GUARD,
"Updated sampled guard %s: filtered=%d; "
1753 guard->is_filtered_guard, guard->is_usable_filtered_guard);
1755 if (!
bool_eq(was_filtered, guard->is_filtered_guard)) {
1757 gs->primary_guards_up_to_date = 0;
1771 } SMARTLIST_FOREACH_END(guard);
1787 const entry_guard_restriction_t *rst,
1791 entry_guard_t *result = NULL;
1792 const unsigned exclude_confirmed = flags & SAMPLE_EXCLUDE_CONFIRMED;
1793 const unsigned exclude_primary = flags & SAMPLE_EXCLUDE_PRIMARY;
1794 const unsigned exclude_pending = flags & SAMPLE_EXCLUDE_PENDING;
1795 const unsigned no_update_primary = flags & SAMPLE_NO_UPDATE_PRIMARY;
1796 const unsigned need_descriptor = flags & SAMPLE_EXCLUDE_NO_DESCRIPTOR;
1800 } SMARTLIST_FOREACH_END(guard);
1804 log_info(
LD_GUARD,
"Trying to sample a reachable guard: We know of %d "
1805 "in the USABLE_FILTERED set.", n_reachable_filtered);
1808 if (n_reachable_filtered < min_filtered_sample) {
1809 log_info(
LD_GUARD,
" (That isn't enough. Trying to expand the sample.)");
1813 if (exclude_primary && !gs->primary_guards_up_to_date && !no_update_primary)
1822 if (! guard->is_usable_filtered_guard)
1824 if (exclude_confirmed && guard->confirmed_idx >= 0)
1826 if (exclude_primary && guard->is_primary)
1828 if (exclude_pending && guard->is_pending)
1833 } SMARTLIST_FOREACH_END(guard);
1835 log_info(
LD_GUARD,
" (After filters [%x], we have %d guards to consider.)",
1836 flags, smartlist_len(reachable_filtered_sample));
1838 if (smartlist_len(reachable_filtered_sample)) {
1849 result = smartlist_get(reachable_filtered_sample, 0);
1850 log_info(
LD_GUARD,
" (Selected %s.)",
1853 smartlist_free(reachable_filtered_sample);
1859compare_guards_by_confirmed_idx(
const void **a_,
const void **b_)
1861 const entry_guard_t *a = *a_, *b = *b_;
1862 if (a->confirmed_idx < b->confirmed_idx)
1864 else if (a->confirmed_idx > b->confirmed_idx)
1876 const entry_guard_t *a = *a_, *b = *b_;
1877 if (a->sampled_idx < b->sampled_idx)
1879 else if (a->sampled_idx > b->sampled_idx)
1895 if (guard->confirmed_idx >= 0)
1897 } SMARTLIST_FOREACH_END(guard);
1899 smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_confirmed_idx);
1901 int any_changed = 0;
1903 if (guard->confirmed_idx != guard_sl_idx) {
1905 guard->confirmed_idx = guard_sl_idx;
1907 } SMARTLIST_FOREACH_END(guard);
1909 gs->next_confirmed_idx = smartlist_len(gs->confirmed_entry_guards);
1925 if (BUG(guard->confirmed_on_date && guard->confirmed_idx >= 0))
1934 log_info(
LD_GUARD,
"Marking %s as a confirmed guard (index %d)",
1936 gs->next_confirmed_idx);
1938 guard->confirmed_idx = gs->next_confirmed_idx++;
1946 gs->primary_guards_up_to_date = 0;
1961 static int running = 0;
1972 gs->primary_guards_up_to_date = 1;
1976 if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
1978 if (! guard->is_filtered_guard)
1980 guard->is_primary = 1;
1982 } SMARTLIST_FOREACH_END(guard);
1993 if (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS &&
1994 guard->is_filtered_guard) {
1995 guard->is_primary = 1;
2000 guard->is_primary = 0;
2002 } SMARTLIST_FOREACH_END(guard);
2005 while (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS) {
2007 SAMPLE_EXCLUDE_CONFIRMED|
2008 SAMPLE_EXCLUDE_PRIMARY|
2009 SAMPLE_NO_UPDATE_PRIMARY);
2012 guard->is_primary = 1;
2019 tor_assert_nonfatal(
2020 bool_eq(guard->is_primary,
2021 smartlist_contains(new_primary_guards, guard)));
2026 new_primary_guards);
2028 log_info(
LD_GUARD,
"Primary entry guards have changed. "
2029 "New primary guard list is: ");
2030 int n = smartlist_len(new_primary_guards);
2032 log_info(
LD_GUARD,
" %d/%d: %s%s%s",
2034 g->confirmed_idx >= 0 ?
" (confirmed)" :
"",
2035 g->is_filtered_guard ?
"" :
" (excluded by filter)");
2036 } SMARTLIST_FOREACH_END(g);
2040 smartlist_free(old_primary_guards);
2041 smartlist_free(gs->primary_entry_guards);
2042 gs->primary_entry_guards = new_primary_guards;
2043 gs->primary_guards_up_to_date = 1;
2055 const unsigned SIX_HOURS = 6 * 3600;
2056 const unsigned FOUR_DAYS = 4 * 86400;
2057 const unsigned SEVEN_DAYS = 7 * 86400;
2060 if (now > failing_since) {
2061 tdiff = now - failing_since;
2067 time_t maximum;
int primary_delay;
int nonprimary_delay;
2070 { SIX_HOURS, 10*60, 1*60*60 },
2071 { FOUR_DAYS, 90*60, 4*60*60 },
2072 { SEVEN_DAYS, 4*60*60, 18*60*60 },
2073 { TIME_MAX, 9*60*60, 36*60*60 }
2079 if (tdiff <= delays[i].maximum) {
2080 return is_primary ? delays[i].primary_delay : delays[i].nonprimary_delay;
2096 if (guard->is_reachable != GUARD_REACHABLE_NO)
2102 const time_t last_attempt = guard->last_tried_to_connect;
2112 if (BUG(last_attempt == 0) ||
2113 now >= last_attempt + delay) {
2115 char tbuf[ISO_TIME_LEN+1];
2117 log_info(
LD_GUARD,
"Marked %s%sguard %s for possible retry, since we "
2118 "haven't tried to use it since %s.",
2119 guard->is_primary?
"primary ":
"",
2120 guard->confirmed_idx>=0?
"confirmed ":
"",
2124 guard->is_reachable = GUARD_REACHABLE_MAYBE;
2125 if (guard->is_filtered_guard)
2126 guard->is_usable_filtered_guard = 1;
2144static entry_guard_t *
2147 const entry_guard_restriction_t *rst,
2148 unsigned *state_out)
2150 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2151 entry_guard_t *chosen_guard = NULL;
2155 int num_entry_guards_considered = 0;
2160 log_info(
LD_GUARD,
"Entry guard %s doesn't obey restriction, we test the"
2164 "Skipping guard %s due to circuit path restriction. "
2165 "Have %d, considered: %d, to consider: %d",
2167 smartlist_len(usable_primary_guards),
2168 num_entry_guards_considered,
2169 num_entry_guards_to_consider);
2172 num_entry_guards_considered++;
2176 if (num_entry_guards_considered >= num_entry_guards_to_consider) {
2180 if (smartlist_len(usable_primary_guards) == 0) {
2181 static ratelim_t guardlog = RATELIM_INIT(60);
2183 "All current guards excluded by path restriction "
2184 "type %d; using an additional guard.",
2193 if (guard->is_reachable != GUARD_REACHABLE_NO) {
2195 log_info(
LD_GUARD,
"Guard %s does not have a descriptor",
2199 *state_out = GUARD_CIRC_STATE_USABLE_ON_COMPLETION;
2202 num_entry_guards_considered++;
2206 if (num_entry_guards_considered >= num_entry_guards_to_consider) {
2210 log_info(
LD_GUARD,
"Guard %s is not reachable",
2213 } SMARTLIST_FOREACH_END(guard);
2215 if (smartlist_len(usable_primary_guards)) {
2218 "Selected primary guard %s for circuit from a list size of %d.",
2220 smartlist_len(usable_primary_guards));
2224 } SMARTLIST_FOREACH_END(guard);
2225 smartlist_free(usable_primary_guards);
2228 smartlist_free(usable_primary_guards);
2229 return chosen_guard;
2238static entry_guard_t *
2241 const entry_guard_restriction_t *rst,
2242 unsigned *state_out)
2244 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2247 if (guard->is_primary)
2252 if (guard->is_usable_filtered_guard && ! guard->is_pending) {
2255 guard->is_pending = 1;
2257 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2258 log_info(
LD_GUARD,
"No primary guards available. Selected confirmed "
2259 "guard %s for circuit. Will try other guards before using "
2264 } SMARTLIST_FOREACH_END(guard);
2275static entry_guard_t *
2278 const entry_guard_restriction_t *rst,
2279 unsigned *state_out)
2281 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2282 entry_guard_t *chosen_guard = NULL;
2284 if (need_descriptor)
2285 flags |= SAMPLE_EXCLUDE_NO_DESCRIPTOR;
2288 SAMPLE_EXCLUDE_CONFIRMED |
2289 SAMPLE_EXCLUDE_PRIMARY |
2290 SAMPLE_EXCLUDE_PENDING |
2292 if (!chosen_guard) {
2296 chosen_guard->is_pending = 1;
2297 chosen_guard->last_tried_to_connect =
approx_time();
2298 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2299 log_info(
LD_GUARD,
"No primary or confirmed guards available. Selected "
2300 "guard %s for circuit. Will try other guards before "
2301 "using this circuit.",
2303 return chosen_guard;
2317 const entry_guard_restriction_t *rst,
2318 unsigned *state_out)
2320 entry_guard_t *chosen_guard = NULL;
2324 if (!gs->primary_guards_up_to_date)
2331 log_info(
LD_GUARD,
"Selected primary guard %s for circuit.",
2333 return chosen_guard;
2342 log_info(
LD_GUARD,
"Selected confirmed guard %s for circuit.",
2344 return chosen_guard;
2351 if (chosen_guard == NULL) {
2352 log_info(
LD_GUARD,
"Absolutely no sampled guards were available. "
2353 "Marking all guards for retry and starting from top again.");
2354 mark_all_guards_maybe_reachable(gs);
2358 log_info(
LD_GUARD,
"Selected filtered guard %s for circuit.",
2360 return chosen_guard;
2369 entry_guard_t *guard)
2373 guard->is_reachable = GUARD_REACHABLE_NO;
2374 guard->is_usable_filtered_guard = 0;
2376 guard->is_pending = 0;
2377 if (guard->failing_since == 0)
2383 log_info(
LD_GUARD,
"Recorded failure for %s%sguard %s",
2384 guard->is_primary?
"primary ":
"",
2385 guard->confirmed_idx>=0?
"confirmed ":
"",
2406 entry_guard_t *guard,
2412 const time_t last_time_on_internet = gs->last_time_on_internet;
2416 if (guard->is_reachable != GUARD_REACHABLE_YES) {
2426 guard->is_reachable = GUARD_REACHABLE_YES;
2427 guard->failing_since = 0;
2428 guard->is_pending = 0;
2429 if (guard->is_filtered_guard)
2430 guard->is_usable_filtered_guard = 1;
2432 if (guard->confirmed_idx < 0) {
2434 if (!gs->primary_guards_up_to_date)
2439 switch (old_state) {
2440 case GUARD_CIRC_STATE_COMPLETE:
2441 case GUARD_CIRC_STATE_USABLE_ON_COMPLETION:
2442 new_state = GUARD_CIRC_STATE_COMPLETE;
2447 case GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD:
2448 if (guard->is_primary) {
2456 new_state = GUARD_CIRC_STATE_COMPLETE;
2458 new_state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2463 if (! guard->is_primary) {
2470 log_info(
LD_GUARD,
"Recorded success for %s%sguard %s",
2471 guard->is_primary?
"primary ":
"",
2472 guard->confirmed_idx>=0?
"confirmed ":
"",
2490 if (a->confirmed_idx < 0) {
2491 if (b->confirmed_idx >= 0)
2494 if (b->confirmed_idx < 0)
2498 return (a->confirmed_idx < b->confirmed_idx);
2503 if (a->is_pending) {
2504 if (! b->is_pending)
2508 return a->last_tried_to_connect < b->last_tried_to_connect;
2522 if (rst && rst->excluded) {
2525 smartlist_free(rst->excluded);
2538 entry_guard_restriction_free(state->restrictions);
2539 entry_guard_handle_free(state->guard);
2547 entry_guard_restriction_t *rst))
2549 circuit_guard_state_t *result;
2551 result = tor_malloc_zero(
sizeof(circuit_guard_state_t));
2552 result->guard = entry_guard_handle_new(guard);
2553 result->state = state;
2555 result->restrictions = rst;
2573 entry_guard_restriction_t *rst,
2574 const node_t **chosen_node_out,
2575 circuit_guard_state_t **guard_state_out)
2580 *chosen_node_out = NULL;
2581 *guard_state_out = NULL;
2584 entry_guard_t *guard =
2588 if (BUG(state == 0))
2594 if (BUG(usage != GUARD_USAGE_DIRGUARD &&
2598 *chosen_node_out = node;
2603 entry_guard_restriction_free(rst);
2619 if (BUG(*guard_state_p == NULL))
2620 return GUARD_USABLE_NEVER;
2622 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2623 if (! guard || BUG(guard->in_selection == NULL))
2624 return GUARD_USABLE_NEVER;
2628 (*guard_state_p)->state);
2630 (*guard_state_p)->state = newstate;
2633 if (newstate == GUARD_CIRC_STATE_COMPLETE) {
2634 return GUARD_USABLE_NOW;
2636 return GUARD_MAYBE_USABLE_LATER;
2646 if (BUG(*guard_state_p == NULL))
2648 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2655 guard->is_pending = 0;
2656 circuit_guard_state_free(*guard_state_p);
2657 *guard_state_p = NULL;
2661struct entry_guard_handle_t *
2664 entry_guard_t *guard = state ? entry_guard_handle_get(state->guard) : NULL;
2665 return guard ? entry_guard_handle_new(guard) : NULL;
2672 entry_guard_handle_free(handle);
2687 entry_guard_t *guard = entry_guard_handle_get(handle);
2688 if (!guard || !guard->in_selection)
2703 if (!gs->primary_guards_up_to_date)
2707 if (guard->is_reachable != GUARD_REACHABLE_NO)
2709 } SMARTLIST_FOREACH_END(guard);
2722 const entry_guard_restriction_t *rst,
2731 entry_guard_t *guard_a = entry_guard_handle_get(state_a->guard);
2732 entry_guard_t *guard_b = entry_guard_handle_get(state_b->guard);
2737 }
else if (! guard_b) {
2769 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2770 "but not all primary guards were definitely down.");
2776 int n_complete_blocking = 0;
2785 entry_guard_t *guard = entry_guard_handle_get(state->guard);
2786 if (!guard || guard->in_selection != gs)
2794 } SMARTLIST_FOREACH_END(circ);
2798 if (BUG(state == NULL))
2801 if (state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD) {
2803 if (! best_waiting_circuit ||
2805 best_waiting_circuit = circ;
2808 } SMARTLIST_FOREACH_END(circ);
2810 if (! best_waiting_circuit) {
2811 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2812 "but didn't find any.");
2819 const entry_guard_restriction_t *rst_on_best_waiting =
2832 if (BUG(state == NULL))
2834 if (state->state != GUARD_CIRC_STATE_COMPLETE)
2838 best_waiting_circuit))
2839 ++n_complete_blocking;
2840 } SMARTLIST_FOREACH_END(circ);
2842 if (n_complete_blocking) {
2843 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2844 "%d complete and %d guard-stalled. At least one complete "
2845 "circuit had higher priority, so not upgrading.",
2846 n_complete, n_waiting);
2855 int n_blockers_found = 0;
2856 const time_t state_set_at_cutoff =
2860 if (BUG(state == NULL))
2862 if (state->state != GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD)
2864 if (state->state_set_at <= state_set_at_cutoff)
2867 best_waiting_circuit))
2869 } SMARTLIST_FOREACH_END(circ);
2871 if (n_blockers_found) {
2872 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2873 "%d guard-stalled, but %d pending circuit(s) had higher "
2874 "guard priority, so not upgrading.",
2875 n_waiting, n_blockers_found);
2882 int n_succeeded = 0;
2885 if (BUG(state == NULL))
2887 if (circ != best_waiting_circuit && rst_on_best_waiting) {
2892 if (state->state != GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD)
2897 state->state = GUARD_CIRC_STATE_COMPLETE;
2901 } SMARTLIST_FOREACH_END(circ);
2903 log_info(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2904 "%d guard-stalled, %d complete. %d of the guard-stalled "
2905 "circuit(s) had high enough priority to upgrade.",
2906 n_waiting, n_complete, n_succeeded);
2908 tor_assert_nonfatal(n_succeeded >= 1);
2909 smartlist_free(all_circuits);
2913 smartlist_free(all_circuits);
2924 if (guard_state == NULL)
2926 const time_t expire_if_waiting_since =
2928 return (guard_state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
2929 && guard_state->state_set_at < expire_if_waiting_since);
2964 char tbuf[ISO_TIME_LEN+1];
2971 if (guard->bridge_addr) {
2974 guard->bridge_addr->port);
2984 if (guard->sampled_by_version) {
2986 guard->sampled_by_version);
2989 if (guard->unlisted_since_date > 0) {
2995 (
int)guard->currently_listed);
2997 if (guard->confirmed_idx >= 0) {
3004 const double EPSILON = 1.0e-6;
3012 #define PB_FIELD(field) do { \
3013 if (pb->field >= EPSILON) { \
3014 smartlist_add_asprintf(result, "pb_" #field "=%f", pb->field); \
3017 PB_FIELD(use_attempts);
3018 PB_FIELD(use_successes);
3019 PB_FIELD(circ_attempts);
3020 PB_FIELD(circ_successes);
3021 PB_FIELD(successful_circuits_closed);
3022 PB_FIELD(collapsed_circuits);
3023 PB_FIELD(unusable_circuits);
3028 if (guard->extra_state_fields)
3033 smartlist_free(result);
3044 *extra, strmap_t *vals)
3047 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
3050 const char *eq = strchr(entry,
'=');
3055 char *key = tor_strndup(entry, eq-entry);
3056 char **target = strmap_get(vals, key);
3057 if (target == NULL || *target != NULL) {
3064 *target = tor_strdup(eq+1);
3067 } SMARTLIST_FOREACH_END(entry);
3075 *unlisted_since,
char *confirmed_on)
3077#define HANDLE_TIME(field) do { \
3079 int r = parse_iso_time_nospace(field, &field ## _time); \
3081 log_warn(LD_CIRC, "Unable to parse %s %s from guard", \
3082 #field, escaped(field)); \
3083 field##_time = -1; \
3088 time_t sampled_on_time = 0;
3089 time_t unlisted_since_time = 0;
3090 time_t confirmed_on_time = 0;
3092 HANDLE_TIME(sampled_on);
3093 HANDLE_TIME(unlisted_since);
3094 HANDLE_TIME(confirmed_on);
3096 if (sampled_on_time <= 0)
3098 if (unlisted_since_time < 0)
3099 unlisted_since_time = 0;
3100 if (confirmed_on_time < 0)
3101 confirmed_on_time = 0;
3105 guard->sampled_on_date = sampled_on_time;
3106 guard->unlisted_since_date = unlisted_since_time;
3107 guard->confirmed_on_date = confirmed_on_time;
3123 char *rsa_id = NULL;
3124 char *nickname = NULL;
3125 char *sampled_on = NULL;
3126 char *sampled_idx = NULL;
3127 char *sampled_by = NULL;
3128 char *unlisted_since = NULL;
3129 char *listed = NULL;
3130 char *confirmed_on = NULL;
3131 char *confirmed_idx = NULL;
3132 char *bridge_addr = NULL;
3135 char *pb_use_attempts = NULL;
3136 char *pb_use_successes = NULL;
3137 char *pb_circ_attempts = NULL;
3138 char *pb_circ_successes = NULL;
3139 char *pb_successful_circuits_closed = NULL;
3140 char *pb_collapsed_circuits = NULL;
3141 char *pb_unusable_circuits = NULL;
3142 char *pb_timeouts = NULL;
3150 strmap_t *vals = strmap_new();
3152 strmap_set(vals, #f, &f);
3159 FIELD(unlisted_since);
3161 FIELD(confirmed_on);
3162 FIELD(confirmed_idx);
3164 FIELD(pb_use_attempts);
3165 FIELD(pb_use_successes);
3166 FIELD(pb_circ_attempts);
3167 FIELD(pb_circ_successes);
3168 FIELD(pb_successful_circuits_closed);
3169 FIELD(pb_collapsed_circuits);
3170 FIELD(pb_unusable_circuits);
3176 smartlist_free(entries);
3177 strmap_free(vals, NULL);
3180 entry_guard_t *guard = tor_malloc_zero(
sizeof(entry_guard_t));
3181 guard->is_persistent = 1;
3184 log_warn(
LD_CIRC,
"Guard missing 'in' field");
3188 guard->selection_name = in;
3191 if (rsa_id == NULL) {
3192 log_warn(
LD_CIRC,
"Guard missing RSA ID field");
3199 log_warn(
LD_CIRC,
"Unable to decode guard identity %s",
escaped(rsa_id));
3204 strlcpy(guard->nickname, nickname,
sizeof(guard->nickname));
3206 guard->nickname[0]=
'$';
3213 memset(&res, 0,
sizeof(res));
3215 &res.addr, &res.port, -1);
3217 guard->bridge_addr = tor_memdup(&res,
sizeof(res));
3226 guard->sampled_by_version = sampled_by;
3229 if (listed && strcmp(listed,
"0"))
3230 guard->currently_listed = 1;
3233 guard->confirmed_idx = -1;
3234 if (confirmed_idx) {
3236 long idx =
tor_parse_long(confirmed_idx, 10, 0, INT_MAX, &ok, NULL);
3238 log_warn(
LD_GUARD,
"Guard has invalid confirmed_idx %s",
3241 guard->confirmed_idx = (int)idx;
3247 long idx =
tor_parse_long(sampled_idx, 10, 0, INT_MAX, &ok, NULL);
3249 log_warn(
LD_GUARD,
"Guard has invalid sampled_idx %s",
3252 guard->sampled_idx = invalid_sampled_idx++;
3254 guard->sampled_idx = (int)idx;
3256 }
else if (confirmed_idx) {
3260 guard->sampled_idx = guard->confirmed_idx;
3262 log_info(
LD_GUARD,
"The state file seems to be into a status that could"
3263 " yield to weird entry node selection: we're missing both a"
3264 " sampled_idx and a confirmed_idx.");
3265 guard->sampled_idx = invalid_sampled_idx++;
3269 if (smartlist_len(extra) > 0) {
3274 guard->is_reachable = GUARD_REACHABLE_MAYBE;
3276#define PB_FIELD(field) \
3278 if (pb_ ## field) { \
3280 double r = tor_parse_double(pb_ ## field, 0.0, 1e9, &ok, NULL); \
3282 log_warn(LD_CIRC, "Guard has invalid pb_%s %s", \
3283 #field, pb_ ## field); \
3285 guard->pb.field = r; \
3289 PB_FIELD(use_attempts);
3290 PB_FIELD(use_successes);
3291 PB_FIELD(circ_attempts);
3292 PB_FIELD(circ_successes);
3293 PB_FIELD(successful_circuits_closed);
3294 PB_FIELD(collapsed_circuits);
3295 PB_FIELD(unusable_circuits);
3309 entry_guard_free(guard);
3328 tor_free(pb_successful_circuits_closed);
3334 smartlist_free(extra);
3354 if (guard->is_persistent == 0)
3357 (*nextline)->key = tor_strdup(
"Guard");
3359 nextline = &(*nextline)->next;
3361 } SMARTLIST_FOREACH_END(guard);
3362 } SMARTLIST_FOREACH_END(gs);
3364 config_free_lines(state->
Guard);
3365 state->
Guard = lines;
3386 guard_selection_free(gs);
3390 } SMARTLIST_FOREACH_END(gs);
3393 for ( ; line != NULL; line = line->next) {
3395 if (guard == NULL) {
3400 if (!strcmp(guard->selection_name,
"legacy")) {
3402 entry_guard_free(guard);
3407 guard_selection_t *gs;
3412 guard->in_selection = gs;
3417 if (gs->next_sampled_idx <= guard->sampled_idx) {
3418 gs->next_sampled_idx = guard->sampled_idx + 1;
3422 entry_guard_free(guard);
3433 } SMARTLIST_FOREACH_END(gs);
3435 return n_errors ? -1 : 0;
3469circuit_guard_state_t *
3472 circuit_guard_state_t *guard_state = NULL;
3473 entry_guard_t *guard = NULL;
3486 GUARD_CIRC_STATE_USABLE_ON_COMPLETION,
3498 entry_guard_handles_clear(e);
3539 if (BUG(gs->type != GS_TYPE_BRIDGE)) {
3545 if (! guard->is_filtered_guard)
3548 if (guard->is_reachable == GUARD_REACHABLE_NO)
3551 if (!use_maybe_reachable && guard->is_reachable == GUARD_REACHABLE_MAYBE)
3556 if (node && node->ri)
3558 } SMARTLIST_FOREACH_END(guard);
3569 const double EPSILON = 1.0e-9;
3574 if (node->pb.use_attempts >
EPSILON &&
3578 node->pb.path_bias_disabled = 1;
3580 "Path use bias is too high (%f/%f); disabling node %s",
3581 node->pb.circ_successes, node->pb.circ_attempts,
3592 const double EPSILON = 1.0e-9;
3597 if (node->pb.circ_attempts >
EPSILON &&
3601 node->pb.path_bias_disabled = 1;
3603 "Path bias is too high (%f/%f); disabling node %s",
3604 node->pb.circ_successes, node->pb.circ_attempts,
3623 if (msg && *msg == NULL) {
3624 *msg = tor_strdup(
"parsing error");
3633#define SLOW_GUARD_STATE_FLUSH_TIME 600
3636#define FAST_GUARD_STATE_FLUSH_TIME 30
3711 entry_guard_t *guard = entry_guard_handle_get(guard_state->guard);
3712 if (!guard || BUG(guard->in_selection == NULL)) {
3726 const char *status = NULL;
3729 char tbuf[ISO_TIME_LEN+1];
3737 if (e->confirmed_idx < 0) {
3738 status =
"never-connected";
3739 }
else if (! e->currently_listed) {
3740 when = e->unlisted_since_date;
3741 status =
"unusable";
3742 }
else if (! e->is_filtered_guard) {
3743 status =
"unusable";
3744 }
else if (e->is_reachable == GUARD_REACHABLE_NO) {
3745 when = e->failing_since;
3761 char *result = NULL;
3764 tor_asprintf(&result,
"%s %s %s\n", nbuf, status, tbuf);
3782 const char *question,
char **answer,
3783 const char **errmsg)
3792 if (!strcmp(question,
"entry-guards") ||
3793 !strcmp(question,
"helper-nodes")) {
3795 guards = gs->sampled_entry_guards;
3802 } SMARTLIST_FOREACH_END(e);
3829 uint32_t guardfraction_percentage)
3831 double guardfraction_fraction;
3835 guardfraction_fraction = guardfraction_percentage / 100.0;
3837 long guard_bw =
tor_lround(guardfraction_fraction * orig_bandwidth);
3840 guardfraction_bw->
guard_bw = (int) guard_bw;
3842 guardfraction_bw->
non_guard_bw = orig_bandwidth - (int) guard_bw;
3853 int mark_circuits = 0;
3862 return mark_circuits;
3871 circuit_guard_state_t **guard_state_out)
3874 const uint8_t *exit_id = NULL;
3875 entry_guard_restriction_t *rst = NULL;
3878 if (CIRCUIT_IS_CONFLUX(
TO_CIRCUIT(circ)) && state
3880 rst = guard_create_conflux_restriction(circ, exit_id);
3890 rst = guard_create_exit_restriction(exit_id);
3894 GUARD_USAGE_TRAFFIC,
3897 guard_state_out) < 0) {
3911 char *old_name = tor_strdup(gs->name);
3912 guard_selection_type_t old_type = gs->type;
3915 control_event_guard(entry->nickname, entry->identity,
"DROPPED");
3923 guard_selection_free(gs);
3944 circuit_guard_state_t **guard_state_out)
3947 entry_guard_restriction_t *rst = NULL;
3955 GUARD_USAGE_DIRGUARD,
3958 guard_state_out) < 0) {
3988 int num_present,
int num_usable)
3990 if (!gs->primary_guards_up_to_date)
3993 char *ret_str = NULL;
3994 int n_missing_descriptors = 0;
3995 int n_considered = 0;
3996 int num_primary_to_check;
4002 num_primary_to_check++;
4006 if (guard->is_reachable == GUARD_REACHABLE_NO)
4010 n_missing_descriptors++;
4011 if (n_considered >= num_primary_to_check)
4013 } SMARTLIST_FOREACH_END(guard);
4016 if (!n_missing_descriptors) {
4021 tor_asprintf(&ret_str,
"We're missing descriptors for %d/%d of our "
4022 "primary entry guards (total %sdescriptors: %d/%d). "
4023 "That's ok. We will try to fetch missing descriptors soon.",
4024 n_missing_descriptors, num_primary_to_check,
4025 using_mds?
"micro":
"", num_present, num_usable);
4034 int num_present,
int num_usable)
4039 num_present, num_usable);
4050 if (gs->sampled_entry_guards) {
4052 entry_guard_free(e));
4053 smartlist_free(gs->sampled_entry_guards);
4054 gs->sampled_entry_guards = NULL;
4057 smartlist_free(gs->confirmed_entry_guards);
4058 smartlist_free(gs->primary_entry_guards);
4077#define layer2_guard_free(val) \
4078 FREE_AND_NULL(layer2_guard_t, layer2_guard_free_, (val))
4119static routerset_t *layer2_routerset = NULL;
4122#define NUMBER_SECOND_GUARDS 4
4129#define MIN_SECOND_GUARD_LIFETIME (3600*24)
4130#define MAX_SECOND_GUARD_LIFETIME (3600*24*12)
4137 "guard-hs-l2-number",
4147 "guard-hs-l2-lifetime-min",
4157 "guard-hs-l2-lifetime-max",
4158 MAX_SECOND_GUARD_LIFETIME,
4173 if (BUG(min >= max)) {
4199 log_info(
LD_GENERAL,
"Removing expired Layer2 guard %s",
4203 layer2_guard_free(g);
4211 log_info(
LD_GENERAL,
"Removing %s Layer2 guard %s",
4212 rs ?
"unsuitable" :
"missing",
4216 layer2_guard_free(g);
4220 } SMARTLIST_FOREACH_END(g);
4223 int new_guards_needed_n =
4225 if (new_guards_needed_n <= 0) {
4229 log_info(
LD_GENERAL,
"Adding %d guards to Layer2 routerset",
4230 new_guards_needed_n);
4240 } SMARTLIST_FOREACH_END(g);
4243 for (
int i = 0; i < new_guards_needed_n; i++) {
4244 const node_t *choice = NULL;
4259 log_info(
LD_GENERAL,
"Adding Layer2 guard %s",
4270 smartlist_free(excluded);
4273 routerset_free(layer2_routerset);
4280 } SMARTLIST_FOREACH_END(g);
4296 layer2_guard_free(g);
4297 } SMARTLIST_FOREACH_END(g);
4317 return layer2_routerset;
4332 guard_selection_free(gs);
4333 } SMARTLIST_FOREACH_END(gs);
4344 layer2_guard_free(g);
4345 } SMARTLIST_FOREACH_END(g);
4348 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)
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)
download_status_t * bridge_get_dl_status(bridge_info_t *bridge)
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.
Header file for circuitlist.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.
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)
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)
void entry_guard_connection_failed(struct entry_guard_handle_t *handle)
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)
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)
struct entry_guard_handle_t * entry_guard_handle_from_state(const circuit_guard_state_t *state)
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)
void entry_guard_handle_release(struct entry_guard_handle_t *handle)
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)
circuit_guard_state_t * get_guard_state_for_bridge_desc_fetch(const bridge_info_t *bridge)
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
#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)