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",
1692 return (rst->type == RST_OUTDATED_MD_DIRSERVER);
1701 const entry_guard_restriction_t *rst)
1707 if (rst->type == RST_EXIT_NODE) {
1708 return guard_obeys_exit_restriction(guard, rst);
1709 }
else if (rst->type == RST_OUTDATED_MD_DIRSERVER) {
1711 }
else if (rst->type == RST_EXCL_LIST) {
1724 guard_selection_t *gs,
1725 entry_guard_t *guard)
1727 unsigned was_filtered = guard->is_filtered_guard;
1728 guard->is_filtered_guard = 0;
1729 guard->is_usable_filtered_guard = 0;
1732 guard->is_filtered_guard = 1;
1734 if (guard->is_reachable != GUARD_REACHABLE_NO)
1735 guard->is_usable_filtered_guard = 1;
1739 log_debug(
LD_GUARD,
"Updated sampled guard %s: filtered=%d; "
1741 guard->is_filtered_guard, guard->is_usable_filtered_guard);
1743 if (!
bool_eq(was_filtered, guard->is_filtered_guard)) {
1745 gs->primary_guards_up_to_date = 0;
1759 } SMARTLIST_FOREACH_END(guard);
1775 const entry_guard_restriction_t *rst,
1779 entry_guard_t *result = NULL;
1780 const unsigned exclude_confirmed = flags & SAMPLE_EXCLUDE_CONFIRMED;
1781 const unsigned exclude_primary = flags & SAMPLE_EXCLUDE_PRIMARY;
1782 const unsigned exclude_pending = flags & SAMPLE_EXCLUDE_PENDING;
1783 const unsigned no_update_primary = flags & SAMPLE_NO_UPDATE_PRIMARY;
1784 const unsigned need_descriptor = flags & SAMPLE_EXCLUDE_NO_DESCRIPTOR;
1788 } SMARTLIST_FOREACH_END(guard);
1792 log_info(
LD_GUARD,
"Trying to sample a reachable guard: We know of %d "
1793 "in the USABLE_FILTERED set.", n_reachable_filtered);
1796 if (n_reachable_filtered < min_filtered_sample) {
1797 log_info(
LD_GUARD,
" (That isn't enough. Trying to expand the sample.)");
1801 if (exclude_primary && !gs->primary_guards_up_to_date && !no_update_primary)
1810 if (! guard->is_usable_filtered_guard)
1812 if (exclude_confirmed && guard->confirmed_idx >= 0)
1814 if (exclude_primary && guard->is_primary)
1816 if (exclude_pending && guard->is_pending)
1821 } SMARTLIST_FOREACH_END(guard);
1823 log_info(
LD_GUARD,
" (After filters [%x], we have %d guards to consider.)",
1824 flags, smartlist_len(reachable_filtered_sample));
1826 if (smartlist_len(reachable_filtered_sample)) {
1837 result = smartlist_get(reachable_filtered_sample, 0);
1838 log_info(
LD_GUARD,
" (Selected %s.)",
1841 smartlist_free(reachable_filtered_sample);
1847compare_guards_by_confirmed_idx(
const void **a_,
const void **b_)
1849 const entry_guard_t *a = *a_, *b = *b_;
1850 if (a->confirmed_idx < b->confirmed_idx)
1852 else if (a->confirmed_idx > b->confirmed_idx)
1864 const entry_guard_t *a = *a_, *b = *b_;
1865 if (a->sampled_idx < b->sampled_idx)
1867 else if (a->sampled_idx > b->sampled_idx)
1883 if (guard->confirmed_idx >= 0)
1885 } SMARTLIST_FOREACH_END(guard);
1887 smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_confirmed_idx);
1889 int any_changed = 0;
1891 if (guard->confirmed_idx != guard_sl_idx) {
1893 guard->confirmed_idx = guard_sl_idx;
1895 } SMARTLIST_FOREACH_END(guard);
1897 gs->next_confirmed_idx = smartlist_len(gs->confirmed_entry_guards);
1913 if (BUG(guard->confirmed_on_date && guard->confirmed_idx >= 0))
1922 log_info(
LD_GUARD,
"Marking %s as a confirmed guard (index %d)",
1924 gs->next_confirmed_idx);
1926 guard->confirmed_idx = gs->next_confirmed_idx++;
1934 gs->primary_guards_up_to_date = 0;
1949 static int running = 0;
1960 gs->primary_guards_up_to_date = 1;
1964 if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
1966 if (! guard->is_filtered_guard)
1968 guard->is_primary = 1;
1970 } SMARTLIST_FOREACH_END(guard);
1981 if (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS &&
1982 guard->is_filtered_guard) {
1983 guard->is_primary = 1;
1988 guard->is_primary = 0;
1990 } SMARTLIST_FOREACH_END(guard);
1993 while (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS) {
1995 SAMPLE_EXCLUDE_CONFIRMED|
1996 SAMPLE_EXCLUDE_PRIMARY|
1997 SAMPLE_NO_UPDATE_PRIMARY);
2000 guard->is_primary = 1;
2007 tor_assert_nonfatal(
2008 bool_eq(guard->is_primary,
2009 smartlist_contains(new_primary_guards, guard)));
2014 new_primary_guards);
2016 log_info(
LD_GUARD,
"Primary entry guards have changed. "
2017 "New primary guard list is: ");
2018 int n = smartlist_len(new_primary_guards);
2020 log_info(
LD_GUARD,
" %d/%d: %s%s%s",
2022 g->confirmed_idx >= 0 ?
" (confirmed)" :
"",
2023 g->is_filtered_guard ?
"" :
" (excluded by filter)");
2024 } SMARTLIST_FOREACH_END(g);
2028 smartlist_free(old_primary_guards);
2029 smartlist_free(gs->primary_entry_guards);
2030 gs->primary_entry_guards = new_primary_guards;
2031 gs->primary_guards_up_to_date = 1;
2043 const unsigned SIX_HOURS = 6 * 3600;
2044 const unsigned FOUR_DAYS = 4 * 86400;
2045 const unsigned SEVEN_DAYS = 7 * 86400;
2048 if (now > failing_since) {
2049 tdiff = now - failing_since;
2055 time_t maximum;
int primary_delay;
int nonprimary_delay;
2058 { SIX_HOURS, 10*60, 1*60*60 },
2059 { FOUR_DAYS, 90*60, 4*60*60 },
2060 { SEVEN_DAYS, 4*60*60, 18*60*60 },
2061 { TIME_MAX, 9*60*60, 36*60*60 }
2067 if (tdiff <= delays[i].maximum) {
2068 return is_primary ? delays[i].primary_delay : delays[i].nonprimary_delay;
2084 if (guard->is_reachable != GUARD_REACHABLE_NO)
2090 const time_t last_attempt = guard->last_tried_to_connect;
2100 if (BUG(last_attempt == 0) ||
2101 now >= last_attempt + delay) {
2103 char tbuf[ISO_TIME_LEN+1];
2105 log_info(
LD_GUARD,
"Marked %s%sguard %s for possible retry, since we "
2106 "haven't tried to use it since %s.",
2107 guard->is_primary?
"primary ":
"",
2108 guard->confirmed_idx>=0?
"confirmed ":
"",
2112 guard->is_reachable = GUARD_REACHABLE_MAYBE;
2113 if (guard->is_filtered_guard)
2114 guard->is_usable_filtered_guard = 1;
2132static entry_guard_t *
2135 const entry_guard_restriction_t *rst,
2136 unsigned *state_out)
2138 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2139 entry_guard_t *chosen_guard = NULL;
2143 int num_entry_guards_considered = 0;
2148 log_info(
LD_GUARD,
"Entry guard %s doesn't obey restriction, we test the"
2152 "Skipping guard %s due to circuit path restriction. "
2153 "Have %d, considered: %d, to consider: %d",
2155 smartlist_len(usable_primary_guards),
2156 num_entry_guards_considered,
2157 num_entry_guards_to_consider);
2160 num_entry_guards_considered++;
2164 if (num_entry_guards_considered >= num_entry_guards_to_consider) {
2168 if (smartlist_len(usable_primary_guards) == 0) {
2169 static ratelim_t guardlog = RATELIM_INIT(60);
2171 "All current guards excluded by path restriction "
2172 "type %d; using an additional guard.",
2181 if (guard->is_reachable != GUARD_REACHABLE_NO) {
2183 log_info(
LD_GUARD,
"Guard %s does not have a descriptor",
2187 *state_out = GUARD_CIRC_STATE_USABLE_ON_COMPLETION;
2190 num_entry_guards_considered++;
2194 if (num_entry_guards_considered >= num_entry_guards_to_consider) {
2198 log_info(
LD_GUARD,
"Guard %s is not reachable",
2201 } SMARTLIST_FOREACH_END(guard);
2203 if (smartlist_len(usable_primary_guards)) {
2206 "Selected primary guard %s for circuit from a list size of %d.",
2208 smartlist_len(usable_primary_guards));
2212 } SMARTLIST_FOREACH_END(guard);
2213 smartlist_free(usable_primary_guards);
2216 smartlist_free(usable_primary_guards);
2217 return chosen_guard;
2226static entry_guard_t *
2229 const entry_guard_restriction_t *rst,
2230 unsigned *state_out)
2232 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2235 if (guard->is_primary)
2240 if (guard->is_usable_filtered_guard && ! guard->is_pending) {
2243 guard->is_pending = 1;
2245 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2246 log_info(
LD_GUARD,
"No primary guards available. Selected confirmed "
2247 "guard %s for circuit. Will try other guards before using "
2252 } SMARTLIST_FOREACH_END(guard);
2263static entry_guard_t *
2266 const entry_guard_restriction_t *rst,
2267 unsigned *state_out)
2269 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2270 entry_guard_t *chosen_guard = NULL;
2272 if (need_descriptor)
2273 flags |= SAMPLE_EXCLUDE_NO_DESCRIPTOR;
2276 SAMPLE_EXCLUDE_CONFIRMED |
2277 SAMPLE_EXCLUDE_PRIMARY |
2278 SAMPLE_EXCLUDE_PENDING |
2280 if (!chosen_guard) {
2284 chosen_guard->is_pending = 1;
2285 chosen_guard->last_tried_to_connect =
approx_time();
2286 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2287 log_info(
LD_GUARD,
"No primary or confirmed guards available. Selected "
2288 "guard %s for circuit. Will try other guards before "
2289 "using this circuit.",
2291 return chosen_guard;
2305 const entry_guard_restriction_t *rst,
2306 unsigned *state_out)
2308 entry_guard_t *chosen_guard = NULL;
2312 if (!gs->primary_guards_up_to_date)
2319 log_info(
LD_GUARD,
"Selected primary guard %s for circuit.",
2321 return chosen_guard;
2330 log_info(
LD_GUARD,
"Selected confirmed guard %s for circuit.",
2332 return chosen_guard;
2339 if (chosen_guard == NULL) {
2340 log_info(
LD_GUARD,
"Absolutely no sampled guards were available. "
2341 "Marking all guards for retry and starting from top again.");
2342 mark_all_guards_maybe_reachable(gs);
2346 log_info(
LD_GUARD,
"Selected filtered guard %s for circuit.",
2348 return chosen_guard;
2357 entry_guard_t *guard)
2361 guard->is_reachable = GUARD_REACHABLE_NO;
2362 guard->is_usable_filtered_guard = 0;
2364 guard->is_pending = 0;
2365 if (guard->failing_since == 0)
2371 log_info(
LD_GUARD,
"Recorded failure for %s%sguard %s",
2372 guard->is_primary?
"primary ":
"",
2373 guard->confirmed_idx>=0?
"confirmed ":
"",
2394 entry_guard_t *guard,
2400 const time_t last_time_on_internet = gs->last_time_on_internet;
2404 if (guard->is_reachable != GUARD_REACHABLE_YES) {
2414 guard->is_reachable = GUARD_REACHABLE_YES;
2415 guard->failing_since = 0;
2416 guard->is_pending = 0;
2417 if (guard->is_filtered_guard)
2418 guard->is_usable_filtered_guard = 1;
2420 if (guard->confirmed_idx < 0) {
2422 if (!gs->primary_guards_up_to_date)
2427 switch (old_state) {
2428 case GUARD_CIRC_STATE_COMPLETE:
2429 case GUARD_CIRC_STATE_USABLE_ON_COMPLETION:
2430 new_state = GUARD_CIRC_STATE_COMPLETE;
2435 case GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD:
2436 if (guard->is_primary) {
2444 new_state = GUARD_CIRC_STATE_COMPLETE;
2446 new_state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2451 if (! guard->is_primary) {
2458 log_info(
LD_GUARD,
"Recorded success for %s%sguard %s",
2459 guard->is_primary?
"primary ":
"",
2460 guard->confirmed_idx>=0?
"confirmed ":
"",
2478 if (a->confirmed_idx < 0) {
2479 if (b->confirmed_idx >= 0)
2482 if (b->confirmed_idx < 0)
2486 return (a->confirmed_idx < b->confirmed_idx);
2491 if (a->is_pending) {
2492 if (! b->is_pending)
2496 return a->last_tried_to_connect < b->last_tried_to_connect;
2510 if (rst && rst->excluded) {
2513 smartlist_free(rst->excluded);
2526 entry_guard_restriction_free(state->restrictions);
2527 entry_guard_handle_free(state->guard);
2535 entry_guard_restriction_t *rst))
2537 circuit_guard_state_t *result;
2539 result = tor_malloc_zero(
sizeof(circuit_guard_state_t));
2540 result->guard = entry_guard_handle_new(guard);
2541 result->state = state;
2543 result->restrictions = rst;
2561 entry_guard_restriction_t *rst,
2562 const node_t **chosen_node_out,
2563 circuit_guard_state_t **guard_state_out)
2568 *chosen_node_out = NULL;
2569 *guard_state_out = NULL;
2572 entry_guard_t *guard =
2576 if (BUG(state == 0))
2582 if (BUG(usage != GUARD_USAGE_DIRGUARD &&
2586 *chosen_node_out = node;
2591 entry_guard_restriction_free(rst);
2607 if (BUG(*guard_state_p == NULL))
2608 return GUARD_USABLE_NEVER;
2610 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2611 if (! guard || BUG(guard->in_selection == NULL))
2612 return GUARD_USABLE_NEVER;
2616 (*guard_state_p)->state);
2618 (*guard_state_p)->state = newstate;
2621 if (newstate == GUARD_CIRC_STATE_COMPLETE) {
2622 return GUARD_USABLE_NOW;
2624 return GUARD_MAYBE_USABLE_LATER;
2634 if (BUG(*guard_state_p == NULL))
2636 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2642 guard->is_pending = 0;
2643 circuit_guard_state_free(*guard_state_p);
2644 *guard_state_p = NULL;
2655 if (BUG(*guard_state_p == NULL))
2658 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2659 if (! guard || BUG(guard->in_selection == NULL))
2664 (*guard_state_p)->state = GUARD_CIRC_STATE_DEAD;
2690 } SMARTLIST_FOREACH_END(circ);
2691 smartlist_free(pending);
2702 if (!gs->primary_guards_up_to_date)
2706 if (guard->is_reachable != GUARD_REACHABLE_NO)
2708 } SMARTLIST_FOREACH_END(guard);
2721 const entry_guard_restriction_t *rst,
2730 entry_guard_t *guard_a = entry_guard_handle_get(state_a->guard);
2731 entry_guard_t *guard_b = entry_guard_handle_get(state_b->guard);
2736 }
else if (! guard_b) {
2768 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2769 "but not all primary guards were definitely down.");
2775 int n_complete_blocking = 0;
2784 entry_guard_t *guard = entry_guard_handle_get(state->guard);
2785 if (!guard || guard->in_selection != gs)
2793 } SMARTLIST_FOREACH_END(circ);
2797 if (BUG(state == NULL))
2800 if (state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD) {
2802 if (! best_waiting_circuit ||
2804 best_waiting_circuit = circ;
2807 } SMARTLIST_FOREACH_END(circ);
2809 if (! best_waiting_circuit) {
2810 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2811 "but didn't find any.");
2818 const entry_guard_restriction_t *rst_on_best_waiting =
2831 if (BUG(state == NULL))
2833 if (state->state != GUARD_CIRC_STATE_COMPLETE)
2837 best_waiting_circuit))
2838 ++n_complete_blocking;
2839 } SMARTLIST_FOREACH_END(circ);
2841 if (n_complete_blocking) {
2842 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2843 "%d complete and %d guard-stalled. At least one complete "
2844 "circuit had higher priority, so not upgrading.",
2845 n_complete, n_waiting);
2854 int n_blockers_found = 0;
2855 const time_t state_set_at_cutoff =
2859 if (BUG(state == NULL))
2861 if (state->state != GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD)
2863 if (state->state_set_at <= state_set_at_cutoff)
2866 best_waiting_circuit))
2868 } SMARTLIST_FOREACH_END(circ);
2870 if (n_blockers_found) {
2871 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2872 "%d guard-stalled, but %d pending circuit(s) had higher "
2873 "guard priority, so not upgrading.",
2874 n_waiting, n_blockers_found);
2881 int n_succeeded = 0;
2884 if (BUG(state == NULL))
2886 if (circ != best_waiting_circuit && rst_on_best_waiting) {
2891 if (state->state != GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD)
2896 state->state = GUARD_CIRC_STATE_COMPLETE;
2900 } SMARTLIST_FOREACH_END(circ);
2902 log_info(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2903 "%d guard-stalled, %d complete. %d of the guard-stalled "
2904 "circuit(s) had high enough priority to upgrade.",
2905 n_waiting, n_complete, n_succeeded);
2907 tor_assert_nonfatal(n_succeeded >= 1);
2908 smartlist_free(all_circuits);
2912 smartlist_free(all_circuits);
2923 if (guard_state == NULL)
2925 const time_t expire_if_waiting_since =
2927 return (guard_state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
2928 && guard_state->state_set_at < expire_if_waiting_since);
2963 char tbuf[ISO_TIME_LEN+1];
2970 if (guard->bridge_addr) {
2973 guard->bridge_addr->port);
2983 if (guard->sampled_by_version) {
2985 guard->sampled_by_version);
2988 if (guard->unlisted_since_date > 0) {
2994 (
int)guard->currently_listed);
2996 if (guard->confirmed_idx >= 0) {
3003 const double EPSILON = 1.0e-6;
3011 #define PB_FIELD(field) do { \
3012 if (pb->field >= EPSILON) { \
3013 smartlist_add_asprintf(result, "pb_" #field "=%f", pb->field); \
3016 PB_FIELD(use_attempts);
3017 PB_FIELD(use_successes);
3018 PB_FIELD(circ_attempts);
3019 PB_FIELD(circ_successes);
3020 PB_FIELD(successful_circuits_closed);
3021 PB_FIELD(collapsed_circuits);
3022 PB_FIELD(unusable_circuits);
3027 if (guard->extra_state_fields)
3032 smartlist_free(result);
3043 *extra, strmap_t *vals)
3046 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
3049 const char *eq = strchr(entry,
'=');
3054 char *key = tor_strndup(entry, eq-entry);
3055 char **target = strmap_get(vals, key);
3056 if (target == NULL || *target != NULL) {
3063 *target = tor_strdup(eq+1);
3066 } SMARTLIST_FOREACH_END(entry);
3074 *unlisted_since,
char *confirmed_on)
3076#define HANDLE_TIME(field) do { \
3078 int r = parse_iso_time_nospace(field, &field ## _time); \
3080 log_warn(LD_CIRC, "Unable to parse %s %s from guard", \
3081 #field, escaped(field)); \
3082 field##_time = -1; \
3087 time_t sampled_on_time = 0;
3088 time_t unlisted_since_time = 0;
3089 time_t confirmed_on_time = 0;
3091 HANDLE_TIME(sampled_on);
3092 HANDLE_TIME(unlisted_since);
3093 HANDLE_TIME(confirmed_on);
3095 if (sampled_on_time <= 0)
3097 if (unlisted_since_time < 0)
3098 unlisted_since_time = 0;
3099 if (confirmed_on_time < 0)
3100 confirmed_on_time = 0;
3104 guard->sampled_on_date = sampled_on_time;
3105 guard->unlisted_since_date = unlisted_since_time;
3106 guard->confirmed_on_date = confirmed_on_time;
3122 char *rsa_id = NULL;
3123 char *nickname = NULL;
3124 char *sampled_on = NULL;
3125 char *sampled_idx = NULL;
3126 char *sampled_by = NULL;
3127 char *unlisted_since = NULL;
3128 char *listed = NULL;
3129 char *confirmed_on = NULL;
3130 char *confirmed_idx = NULL;
3131 char *bridge_addr = NULL;
3134 char *pb_use_attempts = NULL;
3135 char *pb_use_successes = NULL;
3136 char *pb_circ_attempts = NULL;
3137 char *pb_circ_successes = NULL;
3138 char *pb_successful_circuits_closed = NULL;
3139 char *pb_collapsed_circuits = NULL;
3140 char *pb_unusable_circuits = NULL;
3141 char *pb_timeouts = NULL;
3149 strmap_t *vals = strmap_new();
3151 strmap_set(vals, #f, &f);
3158 FIELD(unlisted_since);
3160 FIELD(confirmed_on);
3161 FIELD(confirmed_idx);
3163 FIELD(pb_use_attempts);
3164 FIELD(pb_use_successes);
3165 FIELD(pb_circ_attempts);
3166 FIELD(pb_circ_successes);
3167 FIELD(pb_successful_circuits_closed);
3168 FIELD(pb_collapsed_circuits);
3169 FIELD(pb_unusable_circuits);
3175 smartlist_free(entries);
3176 strmap_free(vals, NULL);
3179 entry_guard_t *guard = tor_malloc_zero(
sizeof(entry_guard_t));
3180 guard->is_persistent = 1;
3183 log_warn(
LD_CIRC,
"Guard missing 'in' field");
3187 guard->selection_name = in;
3190 if (rsa_id == NULL) {
3191 log_warn(
LD_CIRC,
"Guard missing RSA ID field");
3198 log_warn(
LD_CIRC,
"Unable to decode guard identity %s",
escaped(rsa_id));
3203 strlcpy(guard->nickname, nickname,
sizeof(guard->nickname));
3205 guard->nickname[0]=
'$';
3212 memset(&res, 0,
sizeof(res));
3214 &res.addr, &res.port, -1);
3216 guard->bridge_addr = tor_memdup(&res,
sizeof(res));
3225 guard->sampled_by_version = sampled_by;
3228 if (listed && strcmp(listed,
"0"))
3229 guard->currently_listed = 1;
3232 guard->confirmed_idx = -1;
3233 if (confirmed_idx) {
3235 long idx =
tor_parse_long(confirmed_idx, 10, 0, INT_MAX, &ok, NULL);
3237 log_warn(
LD_GUARD,
"Guard has invalid confirmed_idx %s",
3240 guard->confirmed_idx = (int)idx;
3246 long idx =
tor_parse_long(sampled_idx, 10, 0, INT_MAX, &ok, NULL);
3248 log_warn(
LD_GUARD,
"Guard has invalid sampled_idx %s",
3251 guard->sampled_idx = invalid_sampled_idx++;
3253 guard->sampled_idx = (int)idx;
3255 }
else if (confirmed_idx) {
3259 guard->sampled_idx = guard->confirmed_idx;
3261 log_info(
LD_GUARD,
"The state file seems to be into a status that could"
3262 " yield to weird entry node selection: we're missing both a"
3263 " sampled_idx and a confirmed_idx.");
3264 guard->sampled_idx = invalid_sampled_idx++;
3268 if (smartlist_len(extra) > 0) {
3273 guard->is_reachable = GUARD_REACHABLE_MAYBE;
3275#define PB_FIELD(field) \
3277 if (pb_ ## field) { \
3279 double r = tor_parse_double(pb_ ## field, 0.0, 1e9, &ok, NULL); \
3281 log_warn(LD_CIRC, "Guard has invalid pb_%s %s", \
3282 #field, pb_ ## field); \
3284 guard->pb.field = r; \
3288 PB_FIELD(use_attempts);
3289 PB_FIELD(use_successes);
3290 PB_FIELD(circ_attempts);
3291 PB_FIELD(circ_successes);
3292 PB_FIELD(successful_circuits_closed);
3293 PB_FIELD(collapsed_circuits);
3294 PB_FIELD(unusable_circuits);
3308 entry_guard_free(guard);
3327 tor_free(pb_successful_circuits_closed);
3333 smartlist_free(extra);
3353 if (guard->is_persistent == 0)
3356 (*nextline)->key = tor_strdup(
"Guard");
3358 nextline = &(*nextline)->next;
3360 } SMARTLIST_FOREACH_END(guard);
3361 } SMARTLIST_FOREACH_END(gs);
3363 config_free_lines(state->
Guard);
3364 state->
Guard = lines;
3385 guard_selection_free(gs);
3389 } SMARTLIST_FOREACH_END(gs);
3392 for ( ; line != NULL; line = line->next) {
3394 if (guard == NULL) {
3399 if (!strcmp(guard->selection_name,
"legacy")) {
3401 entry_guard_free(guard);
3406 guard_selection_t *gs;
3411 guard->in_selection = gs;
3416 if (gs->next_sampled_idx <= guard->sampled_idx) {
3417 gs->next_sampled_idx = guard->sampled_idx + 1;
3421 entry_guard_free(guard);
3432 } SMARTLIST_FOREACH_END(gs);
3434 return n_errors ? -1 : 0;
3468circuit_guard_state_t *
3471 circuit_guard_state_t *guard_state = NULL;
3472 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;
3879 rst = guard_create_conflux_restriction(circ);
3889 rst = guard_create_exit_restriction(exit_id);
3893 GUARD_USAGE_TRAFFIC,
3896 guard_state_out) < 0) {
3910 char *old_name = tor_strdup(gs->name);
3911 guard_selection_type_t old_type = gs->type;
3914 control_event_guard(entry->nickname, entry->identity,
"DROPPED");
3922 guard_selection_free(gs);
3943 circuit_guard_state_t **guard_state_out)
3946 entry_guard_restriction_t *rst = NULL;
3954 GUARD_USAGE_DIRGUARD,
3957 guard_state_out) < 0) {
3987 int num_present,
int num_usable)
3989 if (!gs->primary_guards_up_to_date)
3992 char *ret_str = NULL;
3993 int n_missing_descriptors = 0;
3994 int n_considered = 0;
3995 int num_primary_to_check;
4001 num_primary_to_check++;
4005 if (guard->is_reachable == GUARD_REACHABLE_NO)
4009 n_missing_descriptors++;
4010 if (n_considered >= num_primary_to_check)
4012 } SMARTLIST_FOREACH_END(guard);
4015 if (!n_missing_descriptors) {
4020 tor_asprintf(&ret_str,
"We're missing descriptors for %d/%d of our "
4021 "primary entry guards (total %sdescriptors: %d/%d). "
4022 "That's ok. We will try to fetch missing descriptors soon.",
4023 n_missing_descriptors, num_primary_to_check,
4024 using_mds?
"micro":
"", num_present, num_usable);
4033 int num_present,
int num_usable)
4038 num_present, num_usable);
4049 if (gs->sampled_entry_guards) {
4051 entry_guard_free(e));
4052 smartlist_free(gs->sampled_entry_guards);
4053 gs->sampled_entry_guards = NULL;
4056 smartlist_free(gs->confirmed_entry_guards);
4057 smartlist_free(gs->primary_entry_guards);
4076#define layer2_guard_free(val) \
4077 FREE_AND_NULL(layer2_guard_t, layer2_guard_free_, (val))
4118static routerset_t *layer2_routerset = NULL;
4121#define NUMBER_SECOND_GUARDS 4
4128#define MIN_SECOND_GUARD_LIFETIME (3600*24)
4129#define MAX_SECOND_GUARD_LIFETIME (3600*24*12)
4136 "guard-hs-l2-number",
4146 "guard-hs-l2-lifetime-min",
4156 "guard-hs-l2-lifetime-max",
4157 MAX_SECOND_GUARD_LIFETIME,
4172 if (BUG(min >= max)) {
4198 log_info(
LD_GENERAL,
"Removing expired Layer2 guard %s",
4202 layer2_guard_free(g);
4210 log_info(
LD_GENERAL,
"Removing %s Layer2 guard %s",
4211 rs ?
"unsuitable" :
"missing",
4215 layer2_guard_free(g);
4219 } SMARTLIST_FOREACH_END(g);
4222 int new_guards_needed_n =
4224 if (new_guards_needed_n <= 0) {
4228 log_info(
LD_GENERAL,
"Adding %d guards to Layer2 routerset",
4229 new_guards_needed_n);
4239 } SMARTLIST_FOREACH_END(g);
4242 for (
int i = 0; i < new_guards_needed_n; i++) {
4243 const node_t *choice = NULL;
4258 log_info(
LD_GENERAL,
"Adding Layer2 guard %s",
4269 smartlist_free(excluded);
4272 routerset_free(layer2_routerset);
4279 } SMARTLIST_FOREACH_END(g);
4295 layer2_guard_free(g);
4296 } SMARTLIST_FOREACH_END(g);
4316 return layer2_routerset;
4331 guard_selection_free(gs);
4332 } SMARTLIST_FOREACH_END(gs);
4343 layer2_guard_free(g);
4344 } SMARTLIST_FOREACH_END(g);
4347 routerset_free(layer2_routerset);
int tor_addr_port_parse(int severity, const char *addrport, tor_addr_t *address_out, uint16_t *port_out, int default_port)
int tor_addr_port_eq(const tor_addr_port_t *a, const tor_addr_port_t *b)
#define fmt_and_decorate_addr(a)
const char * hex_str(const char *from, size_t fromlen)
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
download_status_t * get_bridge_dl_status_by_id(const char *digest)
const uint8_t * bridge_get_rsa_id_digest(const bridge_info_t *bridge)
int node_is_a_configured_bridge(const node_t *node)
const tor_addr_port_t * bridge_get_addr_port(const bridge_info_t *bridge)
const smartlist_t * bridge_list_get(void)
bridge_info_t * get_configured_bridge_by_exact_addr_port_digest(const tor_addr_t *addr, uint16_t port, const char *digest)
Header file for circuitbuild.c.
Header file for channel.c.
double pathbias_get_extreme_use_rate(const or_options_t *options)
double pathbias_get_extreme_rate(const or_options_t *options)
double pathbias_get_use_success_count(entry_guard_t *guard)
int pathbias_get_dropguards(const or_options_t *options)
double pathbias_get_close_success_count(entry_guard_t *guard)
circuit_guard_state_t * origin_circuit_get_guard_state(origin_circuit_t *circ)
const uint8_t * build_state_get_exit_rsa_id(cpath_build_state_t *state)
Header file for circuitbuild.c.
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
void circuit_get_all_pending_on_channel(smartlist_t *out, channel_t *chan)
Header file for circuitlist.c.
#define CIRCUIT_IS_ORIGIN(c)
void circuit_build_times_free_timeouts(circuit_build_times_t *cbt)
circuit_build_times_t * get_circuit_build_times_mutable(void)
Header file for circuitstats.c.
int circuit_should_use_vanguards(uint8_t purpose)
Header file for circuituse.c.
const or_options_t * get_options(void)
Header file for config.c.
void conflux_add_guards_to_exclude_list(const origin_circuit_t *orig_circ, smartlist_t *excluded)
Header file for conflux_pool.c.
Header file for conflux_util.c.
Header file for connection.c.
int control_event_guard(const char *nickname, const char *digest, const char *status)
Header file for control_events.c.
void * smartlist_choose(const smartlist_t *sl)
Common functions for using (pseudo-)random number generators.
time_t crypto_rand_time_range(time_t min, time_t max)
int crypto_rand_int_range(unsigned int min, unsigned int max)
const char * node_describe(const node_t *node)
Header file for describe.c.
int tor_memeq(const void *a, const void *b, size_t sz)
#define tor_memneq(a, b, sz)
void digestset_add(digestset_t *set, const char *digest)
digestset_t * digestset_new(int max_guess)
int digestset_probably_contains(const digestset_t *set, const char *digest)
Types to handle sets of digests, based on bloom filters.
Header file for directory.c.
#define DIR_PURPOSE_FETCH_MICRODESC
void download_status_reset(download_status_t *dls)
Header file for dlstatus.c.
void entry_guard_failed(circuit_guard_state_t **guard_state_p)
char * guard_selection_get_err_str_if_dir_info_missing(guard_selection_t *gs, int using_mds, int num_present, int num_usable)
STATIC int get_n_primary_guards(void)
const node_t * entry_guard_find_node(const entry_guard_t *guard)
void entry_guard_learned_bridge_identity(const tor_addr_port_t *addrport, const uint8_t *rsa_id_digest)
STATIC void entry_guards_update_primary(guard_selection_t *gs)
static entry_guard_t * entry_guard_add_to_sample_impl(guard_selection_t *gs, const uint8_t *rsa_id_digest, const char *nickname, const tor_addr_port_t *bridge_addrport)
void entry_guard_chan_failed(channel_t *chan)
static void entry_guards_update_guards_in_state(or_state_t *state)
const routerset_t * get_layer2_guards(void)
void entry_guards_changed(void)
static int guard_obeys_md_dirserver_restriction(const entry_guard_t *guard)
STATIC void entry_guards_note_guard_failure(guard_selection_t *gs, entry_guard_t *guard)
STATIC void guard_selection_free_(guard_selection_t *gs)
STATIC guard_selection_t * guard_selection_new(const char *name, guard_selection_type_t type)
void remove_all_entry_guards(void)
STATIC guard_selection_t * get_guard_selection_by_name(const char *name, guard_selection_type_t type, int create_if_absent)
void purge_vanguards_lite(void)
static int reasonably_live_consensus_is_missing(const guard_selection_t *gs)
static void entry_guard_set_filtered_flags(const or_options_t *options, guard_selection_t *gs, entry_guard_t *guard)
STATIC double get_meaningful_restriction_threshold(void)
STATIC guard_selection_type_t guard_selection_infer_type(guard_selection_type_t type, const char *name)
void entry_guard_cancel(circuit_guard_state_t **guard_state_p)
static entry_guard_t * select_and_add_guard_item_for_sample(guard_selection_t *gs, smartlist_t *eligible_guards)
STATIC int get_nonprimary_guard_idle_timeout(void)
static entry_guard_t * select_primary_guard_for_circuit(guard_selection_t *gs, guard_usage_t usage, const entry_guard_restriction_t *rst, unsigned *state_out)
entry_guard_t * entry_guard_get_by_id_digest(const char *digest)
STATIC void mark_primary_guards_maybe_reachable(guard_selection_t *gs)
int entry_guards_upgrade_waiting_circuits(guard_selection_t *gs, const smartlist_t *all_circuits_in, smartlist_t *newly_complete_out)
#define NUMBER_SECOND_GUARDS
static guard_selection_t * curr_guard_context
static void remove_guard_from_confirmed_and_primary_lists(guard_selection_t *gs, entry_guard_t *guard)
void remove_all_entry_guards_for_guard_selection(guard_selection_t *gs)
int num_bridges_usable(int use_maybe_reachable)
#define MIN_GUARDS_FOR_MD_RESTRICTION
static int guard_has_descriptor(const entry_guard_t *guard)
STATIC double get_extreme_restriction_threshold(void)
int entry_guards_update_all(guard_selection_t *gs)
static smartlist_t * layer2_guards
STATIC circuit_guard_state_t * circuit_guard_state_new(entry_guard_t *guard, unsigned state, entry_guard_restriction_t *rst)
STATIC entry_guard_t * first_reachable_filtered_entry_guard(guard_selection_t *gs, const entry_guard_restriction_t *rst, unsigned flags)
static entry_guard_t * get_sampled_guard_for_bridge(guard_selection_t *gs, const bridge_info_t *bridge)
STATIC int get_min_filtered_sample_size(void)
static int get_number_of_layer2_hs_guards(void)
static int guard_in_node_family(const entry_guard_t *guard, const node_t *node)
static size_t sampled_guards_update_consensus_presence(guard_selection_t *gs)
static int should_set_md_dirserver_restriction(void)
STATIC void entry_guard_free_(entry_guard_t *e)
STATIC void entry_guard_restriction_free_(entry_guard_restriction_t *rst)
int entry_list_is_constrained(const or_options_t *options)
STATIC int get_guard_confirmed_min_lifetime(void)
guard_usable_t entry_guard_succeeded(circuit_guard_state_t **guard_state_p)
circuit_guard_state_t * get_guard_state_for_bridge_desc_fetch(const char *digest)
static entry_guard_t * select_confirmed_guard_for_circuit(guard_selection_t *gs, guard_usage_t usage, const entry_guard_restriction_t *rst, unsigned *state_out)
static size_t sampled_guards_prune_obsolete_entries(guard_selection_t *gs, const time_t remove_if_unlisted_since, const time_t maybe_remove_if_sampled_before, const time_t remove_if_confirmed_before)
STATIC int entry_guard_has_higher_priority(entry_guard_t *a, entry_guard_t *b)
void entry_guards_note_internet_connectivity(guard_selection_t *gs)
const char * entry_guard_get_rsa_id_digest(const entry_guard_t *guard)
char * entry_guards_get_err_str_if_dir_info_missing(int using_mds, int num_present, int num_usable)
STATIC void entry_guards_update_confirmed(guard_selection_t *gs)
STATIC char * entry_guard_encode_for_state(entry_guard_t *guard, int dense_sampled_idx)
STATIC void entry_guards_update_filtered_sets(guard_selection_t *gs)
void entry_guards_update_state(or_state_t *state)
bool vanguards_lite_is_enabled(void)
static void pathbias_check_close_success_count(entry_guard_t *guard)
static int get_layer2_hs_guard_lifetime(void)
int update_guard_selection_choice(const or_options_t *options)
STATIC entry_guard_restriction_t * guard_create_dirserver_md_restriction(void)
static bool entry_guard_restriction_is_reachability(const entry_guard_restriction_t *rst)
static int compare_guards_by_sampled_idx(const void **a_, const void **b_)
void circuit_guard_state_free_(circuit_guard_state_t *state)
int entry_guard_pick_for_circuit(guard_selection_t *gs, guard_usage_t usage, entry_guard_restriction_t *rst, const node_t **chosen_node_out, circuit_guard_state_t **guard_state_out)
STATIC int get_remove_unlisted_guards_after_days(void)
static void create_initial_guard_context(void)
STATIC int get_max_sample_size_absolute(void)
STATIC int get_nonprimary_guard_connect_timeout(void)
void maintain_layer2_guards(void)
static void parse_from_state_handle_time(entry_guard_t *guard, char *sampled_on, char *unlisted_since, char *confirmed_on)
STATIC entry_guard_t * get_sampled_guard_with_id(guard_selection_t *gs, const uint8_t *rsa_id)
int entry_guards_parse_state(or_state_t *state, int set, char **msg)
STATIC unsigned entry_guards_note_guard_success(guard_selection_t *gs, entry_guard_t *guard, unsigned old_state)
static int entry_guard_obeys_restriction(const entry_guard_t *guard, const entry_guard_restriction_t *rst)
int entry_guard_state_should_expire(circuit_guard_state_t *guard_state)
guard_pathbias_t * entry_guard_get_pathbias_state(entry_guard_t *guard)
const node_t * guards_choose_guard(const origin_circuit_t *circ, cpath_build_state_t *state, uint8_t purpose, circuit_guard_state_t **guard_state_out)
int entry_guard_could_succeed(const circuit_guard_state_t *guard_state)
static entry_guard_t * entry_guard_add_bridge_to_sample(guard_selection_t *gs, const bridge_info_t *bridge)
static void pathbias_check_use_success_count(entry_guard_t *guard)
int guards_retry_optimistic(const or_options_t *options)
STATIC int entry_guard_is_listed(guard_selection_t *gs, const entry_guard_t *guard)
const node_t * guards_choose_dirguard(uint8_t dir_purpose, circuit_guard_state_t **guard_state_out)
static entry_guard_t * select_filtered_guard_for_circuit(guard_selection_t *gs, guard_usage_t usage, const entry_guard_restriction_t *rst, unsigned *state_out)
static smartlist_t * get_eligible_guards(const or_options_t *options, guard_selection_t *gs, int *n_guards_out)
STATIC void entry_guard_consider_retry(entry_guard_t *guard)
static int entry_guard_passes_filter(const or_options_t *options, guard_selection_t *gs, entry_guard_t *guard)
#define SLOW_GUARD_STATE_FLUSH_TIME
STATIC double get_max_sample_threshold(void)
int should_apply_guardfraction(const networkstatus_t *ns)
void entry_guards_changed_for_guard_selection(guard_selection_t *gs)
STATIC entry_guard_t * entry_guard_add_to_sample(guard_selection_t *gs, const node_t *node)
static int get_min_lifetime_of_layer2_hs_guards(void)
static int bridge_passes_guard_filter(const or_options_t *options, const bridge_info_t *bridge)
static entry_guard_t * get_sampled_guard_by_bridge_addr(guard_selection_t *gs, const tor_addr_port_t *addrport)
static smartlist_t * guard_contexts
STATIC int num_reachable_filtered_guards(const guard_selection_t *gs, const entry_guard_restriction_t *rst)
static int have_sampled_guard_with_id(guard_selection_t *gs, const uint8_t *rsa_id)
STATIC int entry_guards_all_primary_guards_are_down(guard_selection_t *gs)
STATIC time_t randomize_time(time_t now, time_t max_backdate)
static time_t get_remove_unlisted_guards_after_seconds(void)
STATIC int get_n_primary_guards_to_use(guard_usage_t usage)
STATIC const char * choose_guard_selection(const or_options_t *options, const networkstatus_t *live_ns, const guard_selection_t *old_selection, guard_selection_type_t *type_out)
static int get_retry_schedule(time_t failing_since, time_t now, int is_primary)
static int entry_guards_load_guards_from_state(or_state_t *state, int set)
STATIC char * getinfo_helper_format_single_entry_guard(const entry_guard_t *e)
#define FAST_GUARD_STATE_FLUSH_TIME
static int node_is_possible_guard(const node_t *node)
#define MIN_SECOND_GUARD_LIFETIME
STATIC void make_guard_confirmed(guard_selection_t *gs, entry_guard_t *guard)
static int circ_state_has_higher_priority(origin_circuit_t *a, const entry_guard_restriction_t *rst, origin_circuit_t *b)
int getinfo_helper_entry_guards(control_connection_t *conn, const char *question, char **answer, const char **errmsg)
guard_selection_t * get_guard_selection_info(void)
STATIC entry_guard_t * entry_guard_parse_from_state(const char *s)
STATIC entry_guard_t * select_entry_guard_for_circuit(guard_selection_t *gs, guard_usage_t usage, const entry_guard_restriction_t *rst, unsigned *state_out)
STATIC int get_guard_lifetime(void)
static int get_max_lifetime_of_layer2_hs_guards(void)
static void parse_from_state_set_vals(const char *s, smartlist_t *entries, smartlist_t *extra, strmap_t *vals)
static int get_max_sample_size(guard_selection_t *gs, int n_guards)
int guards_update_all(void)
CTASSERT(NUMBER_SECOND_GUARDS< 20)
STATIC void sampled_guards_update_from_consensus(guard_selection_t *gs)
entry_guard_t * entry_guard_get_by_id_digest_for_guard_selection(guard_selection_t *gs, const char *digest)
static bridge_info_t * get_bridge_info_for_guard(const entry_guard_t *guard)
static int entry_guards_dirty
const char * entry_guard_describe(const entry_guard_t *guard)
STATIC entry_guard_t * entry_guards_expand_sample(guard_selection_t *gs)
static int node_passes_guard_filter(const or_options_t *options, const node_t *node)
void entry_guards_free_all(void)
STATIC int get_internet_likely_down_interval(void)
Header file for circuitbuild.c.
const char * escaped(const char *s)
long tor_lround(double d)
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Header file for mainloop.c.
int usable_consensus_flavor(void)
int microdesc_relay_is_outdated_dirserver(const char *relay_digest)
Header file for microdesc.c.
networkstatus_t * networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
const routerstatus_t * router_get_consensus_status_by_id(const char *digest)
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Header file for networkstatus.c.
int is_legal_nickname(const char *s)
Header file for nickname.c.
const node_t * router_choose_random_node(smartlist_t *excludedsmartlist, routerset_t *excludedset, router_crn_flags_t flags)
const node_t * node_sl_choose_by_bandwidth(const smartlist_t *sl, bandwidth_weight_rule_t rule)
Header file for node_select.c.
Node information structure.
const node_t * node_get_by_id(const char *identity_digest)
void router_dir_info_changed(void)
const smartlist_t * nodelist_get_list(void)
int router_addrs_in_same_network(const tor_addr_t *a1, const tor_addr_t *a2)
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
const char * node_get_nickname(const node_t *node)
int node_is_dir(const node_t *node)
void node_get_addr(const node_t *node, tor_addr_t *addr_out)
void node_get_verbose_nickname(const node_t *node, char *verbose_name_out)
int nodes_in_same_family(const node_t *node1, const node_t *node2)
int router_have_minimum_dir_info(void)
Header file for nodelist.c.
Master header file for Tor-specific functionality.
#define MAX_VERBOSE_NICKNAME_LEN
The or_state_t structure, which represents Tor's state file.
Origin circuit structure.
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
int reachable_addr_allows_addr(const tor_addr_t *addr, uint16_t port, firewall_connection_t fw_connection, int pref_only, int pref_ipv6)
int reachable_addr_allows_node(const node_t *node, firewall_connection_t fw_connection, int pref_only)
Header file for policies.c.
int tor_asprintf(char **strp, const char *fmt,...)
int tor_snprintf(char *str, size_t size, const char *format,...)
int router_digest_is_me(const char *digest)
Header file for router.c.
routerset_t * routerset_new(void)
int routerset_contains_node(const routerset_t *set, const node_t *node)
int routerset_parse(routerset_t *target, const char *s, const char *description)
int routerset_contains_bridge(const routerset_t *set, const bridge_info_t *bridge)
Header file for routerset.c.
Routerstatus (consensus entry) structure.
int smartlist_ptrs_eq(const smartlist_t *s1, const smartlist_t *s2)
int smartlist_contains_digest(const smartlist_t *sl, const char *element)
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
void smartlist_remove_keeporder(smartlist_t *sl, const void *element)
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
int smartlist_contains(const smartlist_t *sl, const void *element)
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_clear(smartlist_t *sl)
void smartlist_remove(smartlist_t *sl, const void *element)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
#define SMARTLIST_DEL_CURRENT(sl, var)
#define SMARTLIST_DEL_CURRENT_KEEPORDER(sl, var)
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)
void or_state_mark_dirty(or_state_t *state, time_t when)
or_state_t * get_or_state(void)
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)