116 #define ENTRYNODES_PRIVATE
131 #include "feature/client/circpathbias.h"
164 guard_selection_t *gs,
165 entry_guard_t *guard);
172 const uint8_t *rsa_id_digest,
173 const char *nickname,
178 const entry_guard_restriction_t *rst);
217 STATIC guard_selection_type_t
221 if (type == GS_TYPE_INFER) {
222 if (!strcmp(
name,
"bridges"))
223 type = GS_TYPE_BRIDGE;
224 else if (!strcmp(
name,
"restricted"))
225 type = GS_TYPE_RESTRICTED;
227 type = GS_TYPE_NORMAL;
235 STATIC guard_selection_t *
237 guard_selection_type_t type)
239 guard_selection_t *gs;
243 gs = tor_malloc_zero(
sizeof(*gs));
244 gs->name = tor_strdup(
name);
258 STATIC guard_selection_t *
260 guard_selection_type_t type,
261 int create_if_absent)
267 if (!strcmp(gs->name,
name))
269 } SMARTLIST_FOREACH_END(gs);
271 if (! create_if_absent)
274 log_debug(
LD_GUARD,
"Creating a guard selection called %s",
name);
278 return new_selection;
292 guard_selection_type_t type = GS_TYPE_INFER;
302 log_notice(
LD_GUARD,
"Starting with guard context \"%s\"",
name);
322 static char buf[256];
325 strlen(guard->nickname) ? guard->nickname :
"[bridge]",
334 return guard->identity;
344 HANDLE_IMPL(entry_guard, entry_guard_t, ATTR_UNUSED
STATIC)
355 time_t earliest = now - max_backdate;
359 if (latest <= earliest)
360 latest = earliest + 1;
381 DFLT_MAX_SAMPLE_THRESHOLD_PERCENT,
392 DFLT_MAX_SAMPLE_SIZE,
402 DFLT_MIN_FILTERED_SAMPLE_SIZE,
412 "guard-remove-unlisted-guards-after-days",
413 DFLT_REMOVE_UNLISTED_GUARDS_AFTER_DAYS,
438 "guard-lifetime-days",
439 DFLT_GUARD_LIFETIME_DAYS, 1, 365*10);
453 DFLT_GUARD_CONFIRMED_MIN_LIFETIME_DAYS,
466 if (configured_primaries) {
467 return configured_primaries;
473 "guard-n-primary-guards",
474 DFLT_N_PRIMARY_GUARDS, 1, INT32_MAX);
484 const char *param_name;
489 if (usage == GUARD_USAGE_DIRGUARD) {
491 param_name =
"guard-n-primary-dir-guards-to-use";
492 param_default = DFLT_N_PRIMARY_DIR_GUARDS_TO_USE;
495 param_name =
"guard-n-primary-guards-to-use";
496 param_default = DFLT_N_PRIMARY_GUARDS_TO_USE;
498 if (configured >= 1) {
502 param_name, param_default, 1, INT32_MAX);
512 DFLT_INTERNET_LIKELY_DOWN_INTERVAL,
524 "guard-nonprimary-guard-connect-timeout",
525 DFLT_NONPRIMARY_GUARD_CONNECT_TIMEOUT,
536 "guard-nonprimary-guard-idle-timeout",
537 DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT,
548 "guard-meaningful-restriction-percent",
549 DFLT_MEANINGFUL_RESTRICTION_PERCENT,
561 "guard-extreme-restriction-percent",
562 DFLT_EXTREME_RESTRICTION_PERCENT,
569 mark_guard_maybe_reachable(entry_guard_t *guard)
571 if (guard->is_reachable != GUARD_REACHABLE_NO) {
577 guard->is_reachable = GUARD_REACHABLE_MAYBE;
578 if (guard->is_filtered_guard)
579 guard->is_usable_filtered_guard = 1;
604 if (!gs->primary_guards_up_to_date)
608 mark_guard_maybe_reachable(guard);
609 } SMARTLIST_FOREACH_END(guard);
615 mark_all_guards_maybe_reachable(guard_selection_t *gs)
620 mark_guard_maybe_reachable(guard);
621 } SMARTLIST_FOREACH_END(guard);
634 const guard_selection_t *old_selection,
635 guard_selection_type_t *type_out)
641 *type_out = GS_TYPE_BRIDGE;
647 *type_out = GS_TYPE_NORMAL;
652 int n_guards = 0, n_passing_filter = 0;
660 } SMARTLIST_FOREACH_END(node);
664 const int meaningful_threshold_high =
666 const int meaningful_threshold_mid =
668 const int meaningful_threshold_low =
670 const int extreme_threshold =
687 static int have_warned_extreme_threshold = 0;
689 n_passing_filter < extreme_threshold &&
690 ! have_warned_extreme_threshold) {
691 have_warned_extreme_threshold = 1;
692 const double exclude_frac =
693 (n_guards - n_passing_filter) / (
double)n_guards;
694 log_warn(
LD_GUARD,
"Your configuration excludes %d%% of all possible "
695 "guards. That's likely to make you stand out from the "
696 "rest of the world.", (
int)(exclude_frac * 100));
701 if (old_selection == NULL) {
702 if (n_passing_filter >= meaningful_threshold_mid) {
703 *type_out = GS_TYPE_NORMAL;
706 *type_out = GS_TYPE_RESTRICTED;
716 if (n_passing_filter >= meaningful_threshold_high) {
717 *type_out = GS_TYPE_NORMAL;
719 }
else if (n_passing_filter < meaningful_threshold_low) {
720 *type_out = GS_TYPE_RESTRICTED;
724 *type_out = old_selection->type;
725 return old_selection->name;
745 guard_selection_type_t type = GS_TYPE_INFER;
757 if (! strcmp(cur_name, new_name)) {
759 "Staying with guard context \"%s\" (no change)", new_name);
763 log_notice(
LD_GUARD,
"Switching to guard context \"%s\" (was using \"%s\")",
765 guard_selection_t *new_guard_context;
798 const uint8_t *rsa_id)
805 } SMARTLIST_FOREACH_END(guard);
811 static entry_guard_t *
817 entry_guard_t *guard;
832 const uint8_t *identity = NULL;
834 identity = (
const uint8_t *)guard->
identity;
836 if (BUG(guard->bridge_addr == NULL))
840 &guard->bridge_addr->addr,
841 guard->bridge_addr->port,
842 (
const char*)identity);
863 log_info(
LD_GUARD,
"Adding %s to the entry guard sample set.",
882 static entry_guard_t *
884 const uint8_t *rsa_id_digest,
885 const char *nickname,
894 if (BUG(!rsa_id_digest && !bridge_addrport))
897 entry_guard_t *guard = tor_malloc_zero(
sizeof(entry_guard_t));
900 guard->is_persistent = (rsa_id_digest != NULL);
901 guard->selection_name = tor_strdup(gs->name);
903 memcpy(guard->identity, rsa_id_digest,
DIGEST_LEN);
905 strlcpy(guard->nickname, nickname,
sizeof(guard->nickname));
907 tor_free(guard->sampled_by_version);
908 guard->sampled_by_version = tor_strdup(VERSION);
909 guard->currently_listed = 1;
910 guard->sampled_idx = gs->next_sampled_idx++;
911 guard->confirmed_idx = -1;
914 guard->is_reachable = GUARD_REACHABLE_MAYBE;
916 guard->bridge_addr = tor_memdup(bridge_addrport,
sizeof(*bridge_addrport));
919 guard->in_selection = gs;
934 static entry_guard_t *
954 static entry_guard_t *
965 } SMARTLIST_FOREACH_END(g);
974 const uint8_t *rsa_id_digest)
986 int make_persistent = 0;
989 memcpy(g->identity, rsa_id_digest,
DIGEST_LEN);
993 if (BUG(! g->is_persistent))
997 base16_encode(old_id,
sizeof(old_id), g->identity,
sizeof(g->identity));
998 log_warn(
LD_BUG,
"We 'learned' an identity %s for a bridge at %s:%d, but "
999 "we already knew a different one (%s). Ignoring the new info as "
1007 if (make_persistent) {
1008 g->is_persistent = 1;
1023 const entry_guard_restriction_t *rst)
1025 int n_reachable_filtered_guards = 0;
1030 if (guard->is_usable_filtered_guard)
1031 ++n_reachable_filtered_guards;
1032 } SMARTLIST_FOREACH_END(guard);
1033 return n_reachable_filtered_guards;
1042 const int using_bridges = (gs->type == GS_TYPE_BRIDGE);
1053 const int max_sample = MIN(max_sample_by_pct, max_sample_absolute);
1054 if (max_sample < min_sample)
1070 guard_selection_t *gs,
1077 if (gs->type == GS_TYPE_BRIDGE) {
1085 } SMARTLIST_FOREACH_END(bridge);
1088 const int n_sampled = smartlist_len(gs->sampled_entry_guards);
1095 } SMARTLIST_FOREACH_END(guard);
1100 if (gs->type == GS_TYPE_RESTRICTED) {
1112 } SMARTLIST_FOREACH_END(node);
1115 digestset_free(sampled_guard_ids);
1118 *n_guards_out = n_guards;
1119 return eligible_guards;
1126 static entry_guard_t *
1130 entry_guard_t *added_guard;
1131 if (gs->type == GS_TYPE_BRIDGE) {
1158 if (gs->type == GS_TYPE_BRIDGE) {
1180 log_info(
LD_GUARD,
"Not expanding the sample guard set; we have "
1181 "no reasonably live consensus.");
1185 int n_sampled = smartlist_len(gs->sampled_entry_guards);
1186 entry_guard_t *added_guard = NULL;
1194 log_info(
LD_GUARD,
"Expanding the sample guard set. We have %d guards "
1195 "in the sample, and %d eligible guards to extend it with.",
1196 n_sampled, smartlist_len(eligible_guards));
1198 while (n_usable_filtered_guards < min_filtered_sample) {
1200 if (n_sampled >= max_sample) {
1201 log_info(
LD_GUARD,
"Not expanding the guard sample any further; "
1202 "just hit the maximum sample threshold of %d",
1208 if (smartlist_len(eligible_guards) == 0) {
1213 log_info(
LD_GUARD,
"Not expanding the guard sample any further; "
1214 "just ran out of eligible guards");
1226 if (added_guard->is_usable_filtered_guard)
1227 ++n_usable_filtered_guards;
1231 smartlist_free(eligible_guards);
1240 entry_guard_t *guard)
1242 if (guard->is_primary) {
1243 guard->is_primary = 0;
1251 if (guard->confirmed_idx >= 0) {
1253 guard->confirmed_idx = -1;
1254 guard->confirmed_on_date = 0;
1270 if (gs->type == GS_TYPE_BRIDGE) {
1295 size_t n_changes = 0;
1299 const time_t unlisted_since_slop =
1306 if (is_listed && ! guard->currently_listed) {
1308 guard->currently_listed = 1;
1309 guard->unlisted_since_date = 0;
1310 log_info(
LD_GUARD,
"Sampled guard %s is now listed again.",
1312 }
else if (!is_listed && guard->currently_listed) {
1314 guard->currently_listed = 0;
1316 unlisted_since_slop);
1317 log_info(
LD_GUARD,
"Sampled guard %s is now unlisted.",
1319 }
else if (is_listed && guard->currently_listed) {
1320 log_debug(
LD_GUARD,
"Sampled guard %s is still listed.",
1323 tor_assert(! is_listed && ! guard->currently_listed);
1324 log_debug(
LD_GUARD,
"Sampled guard %s is still unlisted.",
1329 if (guard->currently_listed && guard->unlisted_since_date) {
1331 guard->unlisted_since_date = 0;
1332 log_warn(
LD_BUG,
"Sampled guard %s was listed, but with "
1333 "unlisted_since_date set. Fixing.",
1335 }
else if (!guard->currently_listed && ! guard->unlisted_since_date) {
1338 unlisted_since_slop);
1339 log_warn(
LD_BUG,
"Sampled guard %s was unlisted, but with "
1340 "unlisted_since_date unset. Fixing.",
1343 } SMARTLIST_FOREACH_END(guard);
1364 const time_t remove_if_unlisted_since,
1365 const time_t maybe_remove_if_sampled_before,
1366 const time_t remove_if_confirmed_before)
1368 size_t n_changes = 0;
1375 if (guard->currently_listed == 0 &&
1376 guard->unlisted_since_date < remove_if_unlisted_since) {
1382 log_info(
LD_GUARD,
"Removing sampled guard %s: it has been unlisted "
1386 }
else if (guard->sampled_on_date < maybe_remove_if_sampled_before) {
1391 if (guard->confirmed_on_date == 0) {
1393 log_info(
LD_GUARD,
"Removing sampled guard %s: it was sampled "
1394 "over %d days ago, but never confirmed.",
1397 }
else if (guard->confirmed_on_date < remove_if_confirmed_before) {
1399 log_info(
LD_GUARD,
"Removing sampled guard %s: it was sampled "
1400 "over %d days ago, and confirmed over %d days ago.",
1411 entry_guard_free(guard);
1413 } SMARTLIST_FOREACH_END(guard);
1431 log_info(
LD_GUARD,
"Not updating the sample guard set; we have "
1432 "no reasonably live consensus.");
1435 log_info(
LD_GUARD,
"Updating sampled guard status based on received "
1441 const time_t remove_if_unlisted_since =
1443 const time_t maybe_remove_if_sampled_before =
1445 const time_t remove_if_confirmed_before =
1451 remove_if_unlisted_since,
1452 maybe_remove_if_sampled_before,
1453 remove_if_confirmed_before);
1456 gs->primary_guards_up_to_date = 0;
1510 FIREWALL_OR_CONNECTION,
1523 entry_guard_t *guard)
1525 if (guard->currently_listed == 0)
1527 if (guard->pb.path_bias_disabled)
1530 if (gs->type == GS_TYPE_BRIDGE) {
1567 if (
get_options()->EnforceDistinctSubnets && guard->bridge_addr) {
1571 &guard->bridge_addr->addr)) {
1581 STATIC entry_guard_restriction_t *
1582 guard_create_exit_restriction(
const uint8_t *exit_id)
1584 entry_guard_restriction_t *rst = NULL;
1585 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1586 rst->type = RST_EXIT_NODE;
1587 memcpy(rst->exclude_id, exit_id,
DIGEST_LEN);
1593 #define MIN_GUARDS_FOR_MD_RESTRICTION 10
1606 log_info(
LD_GUARD,
"Not setting md restriction: only %d"
1607 " usable guards.", num_usable_guards);
1617 STATIC entry_guard_restriction_t *
1620 entry_guard_restriction_t *rst = NULL;
1623 log_debug(
LD_GUARD,
"Not setting md restriction: too few "
1624 "filtered guards.");
1628 rst = tor_malloc_zero(
sizeof(entry_guard_restriction_t));
1629 rst->type = RST_OUTDATED_MD_DIRSERVER;
1636 guard_obeys_exit_restriction(
const entry_guard_t *guard,
1637 const entry_guard_restriction_t *rst)
1656 log_info(
LD_GENERAL,
"Skipping %s dirserver: outdated",
1661 log_debug(
LD_GENERAL,
"%s dirserver obeys md restrictions",
1673 const entry_guard_restriction_t *rst)
1679 if (rst->type == RST_EXIT_NODE) {
1680 return guard_obeys_exit_restriction(guard, rst);
1681 }
else if (rst->type == RST_OUTDATED_MD_DIRSERVER) {
1694 guard_selection_t *gs,
1695 entry_guard_t *guard)
1697 unsigned was_filtered = guard->is_filtered_guard;
1698 guard->is_filtered_guard = 0;
1699 guard->is_usable_filtered_guard = 0;
1702 guard->is_filtered_guard = 1;
1704 if (guard->is_reachable != GUARD_REACHABLE_NO)
1705 guard->is_usable_filtered_guard = 1;
1709 log_debug(
LD_GUARD,
"Updated sampled guard %s: filtered=%d; "
1711 guard->is_filtered_guard, guard->is_usable_filtered_guard);
1713 if (!
bool_eq(was_filtered, guard->is_filtered_guard)) {
1715 gs->primary_guards_up_to_date = 0;
1729 } SMARTLIST_FOREACH_END(guard);
1745 const entry_guard_restriction_t *rst,
1749 entry_guard_t *result = NULL;
1750 const unsigned exclude_confirmed = flags & SAMPLE_EXCLUDE_CONFIRMED;
1751 const unsigned exclude_primary = flags & SAMPLE_EXCLUDE_PRIMARY;
1752 const unsigned exclude_pending = flags & SAMPLE_EXCLUDE_PENDING;
1753 const unsigned no_update_primary = flags & SAMPLE_NO_UPDATE_PRIMARY;
1754 const unsigned need_descriptor = flags & SAMPLE_EXCLUDE_NO_DESCRIPTOR;
1758 } SMARTLIST_FOREACH_END(guard);
1762 log_info(
LD_GUARD,
"Trying to sample a reachable guard: We know of %d "
1763 "in the USABLE_FILTERED set.", n_reachable_filtered);
1766 if (n_reachable_filtered < min_filtered_sample) {
1767 log_info(
LD_GUARD,
" (That isn't enough. Trying to expand the sample.)");
1771 if (exclude_primary && !gs->primary_guards_up_to_date && !no_update_primary)
1780 if (! guard->is_usable_filtered_guard)
1782 if (exclude_confirmed && guard->confirmed_idx >= 0)
1784 if (exclude_primary && guard->is_primary)
1786 if (exclude_pending && guard->is_pending)
1791 } SMARTLIST_FOREACH_END(guard);
1793 log_info(
LD_GUARD,
" (After filters [%x], we have %d guards to consider.)",
1794 flags, smartlist_len(reachable_filtered_sample));
1796 if (smartlist_len(reachable_filtered_sample)) {
1807 result = smartlist_get(reachable_filtered_sample, 0);
1808 log_info(
LD_GUARD,
" (Selected %s.)",
1811 smartlist_free(reachable_filtered_sample);
1817 compare_guards_by_confirmed_idx(
const void **a_,
const void **b_)
1819 const entry_guard_t *a = *a_, *b = *b_;
1820 if (a->confirmed_idx < b->confirmed_idx)
1822 else if (a->confirmed_idx > b->confirmed_idx)
1834 const entry_guard_t *a = *a_, *b = *b_;
1835 if (a->sampled_idx < b->sampled_idx)
1837 else if (a->sampled_idx > b->sampled_idx)
1853 if (guard->confirmed_idx >= 0)
1855 } SMARTLIST_FOREACH_END(guard);
1857 smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_confirmed_idx);
1859 int any_changed = 0;
1861 if (guard->confirmed_idx != guard_sl_idx) {
1863 guard->confirmed_idx = guard_sl_idx;
1865 } SMARTLIST_FOREACH_END(guard);
1867 gs->next_confirmed_idx = smartlist_len(gs->confirmed_entry_guards);
1883 if (BUG(guard->confirmed_on_date && guard->confirmed_idx >= 0))
1892 log_info(
LD_GUARD,
"Marking %s as a confirmed guard (index %d)",
1894 gs->next_confirmed_idx);
1896 guard->confirmed_idx = gs->next_confirmed_idx++;
1904 gs->primary_guards_up_to_date = 0;
1919 static int running = 0;
1930 gs->primary_guards_up_to_date = 1;
1934 if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
1936 if (! guard->is_filtered_guard)
1938 guard->is_primary = 1;
1940 } SMARTLIST_FOREACH_END(guard);
1951 if (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS &&
1952 guard->is_filtered_guard) {
1953 guard->is_primary = 1;
1958 guard->is_primary = 0;
1960 } SMARTLIST_FOREACH_END(guard);
1963 while (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS) {
1965 SAMPLE_EXCLUDE_CONFIRMED|
1966 SAMPLE_EXCLUDE_PRIMARY|
1967 SAMPLE_NO_UPDATE_PRIMARY);
1970 guard->is_primary = 1;
1977 tor_assert_nonfatal(
1978 bool_eq(guard->is_primary,
1979 smartlist_contains(new_primary_guards, guard)));
1984 new_primary_guards);
1986 log_info(
LD_GUARD,
"Primary entry guards have changed. "
1987 "New primary guard list is: ");
1988 int n = smartlist_len(new_primary_guards);
1990 log_info(
LD_GUARD,
" %d/%d: %s%s%s",
1992 g->confirmed_idx >= 0 ?
" (confirmed)" :
"",
1993 g->is_filtered_guard ?
"" :
" (excluded by filter)");
1994 } SMARTLIST_FOREACH_END(g);
1998 smartlist_free(old_primary_guards);
1999 smartlist_free(gs->primary_entry_guards);
2000 gs->primary_entry_guards = new_primary_guards;
2001 gs->primary_guards_up_to_date = 1;
2013 const unsigned SIX_HOURS = 6 * 3600;
2014 const unsigned FOUR_DAYS = 4 * 86400;
2015 const unsigned SEVEN_DAYS = 7 * 86400;
2018 if (now > failing_since) {
2019 tdiff = now - failing_since;
2025 time_t maximum;
int primary_delay;
int nonprimary_delay;
2028 { SIX_HOURS, 10*60, 1*60*60 },
2029 { FOUR_DAYS, 90*60, 4*60*60 },
2030 { SEVEN_DAYS, 4*60*60, 18*60*60 },
2031 { TIME_MAX, 9*60*60, 36*60*60 }
2037 if (tdiff <= delays[i].maximum) {
2038 return is_primary ? delays[i].primary_delay : delays[i].nonprimary_delay;
2054 if (guard->is_reachable != GUARD_REACHABLE_NO)
2060 const time_t last_attempt = guard->last_tried_to_connect;
2070 if (BUG(last_attempt == 0) ||
2071 now >= last_attempt + delay) {
2073 char tbuf[ISO_TIME_LEN+1];
2075 log_info(
LD_GUARD,
"Marked %s%sguard %s for possible retry, since we "
2076 "haven't tried to use it since %s.",
2077 guard->is_primary?
"primary ":
"",
2078 guard->confirmed_idx>=0?
"confirmed ":
"",
2082 guard->is_reachable = GUARD_REACHABLE_MAYBE;
2083 if (guard->is_filtered_guard)
2084 guard->is_usable_filtered_guard = 1;
2102 static entry_guard_t *
2105 const entry_guard_restriction_t *rst,
2106 unsigned *state_out)
2108 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2109 entry_guard_t *chosen_guard = NULL;
2117 log_info(
LD_GUARD,
"Entry guard %s doesn't obey restriction, we test the"
2121 if (guard->is_reachable != GUARD_REACHABLE_NO) {
2123 log_info(
LD_GUARD,
"Guard %s does not have a descriptor",
2127 *state_out = GUARD_CIRC_STATE_USABLE_ON_COMPLETION;
2130 if (smartlist_len(usable_primary_guards) >= num_entry_guards)
2133 } SMARTLIST_FOREACH_END(guard);
2135 if (smartlist_len(usable_primary_guards)) {
2138 "Selected primary guard %s for circuit from a list size of %d.",
2140 smartlist_len(usable_primary_guards));
2141 smartlist_free(usable_primary_guards);
2144 smartlist_free(usable_primary_guards);
2145 return chosen_guard;
2154 static entry_guard_t *
2157 const entry_guard_restriction_t *rst,
2158 unsigned *state_out)
2160 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2163 if (guard->is_primary)
2168 if (guard->is_usable_filtered_guard && ! guard->is_pending) {
2171 guard->is_pending = 1;
2173 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2174 log_info(
LD_GUARD,
"No primary guards available. Selected confirmed "
2175 "guard %s for circuit. Will try other guards before using "
2180 } SMARTLIST_FOREACH_END(guard);
2191 static entry_guard_t *
2194 const entry_guard_restriction_t *rst,
2195 unsigned *state_out)
2197 const int need_descriptor = (usage == GUARD_USAGE_TRAFFIC);
2198 entry_guard_t *chosen_guard = NULL;
2200 if (need_descriptor)
2201 flags |= SAMPLE_EXCLUDE_NO_DESCRIPTOR;
2204 SAMPLE_EXCLUDE_CONFIRMED |
2205 SAMPLE_EXCLUDE_PRIMARY |
2206 SAMPLE_EXCLUDE_PENDING |
2208 if (!chosen_guard) {
2212 chosen_guard->is_pending = 1;
2213 chosen_guard->last_tried_to_connect =
approx_time();
2214 *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
2215 log_info(
LD_GUARD,
"No primary or confirmed guards available. Selected "
2216 "guard %s for circuit. Will try other guards before "
2217 "using this circuit.",
2219 return chosen_guard;
2233 const entry_guard_restriction_t *rst,
2234 unsigned *state_out)
2236 entry_guard_t *chosen_guard = NULL;
2240 if (!gs->primary_guards_up_to_date)
2247 return chosen_guard;
2255 return chosen_guard;
2261 if (chosen_guard == NULL) {
2262 log_info(
LD_GUARD,
"Absolutely no sampled guards were available. "
2263 "Marking all guards for retry and starting from top again.");
2264 mark_all_guards_maybe_reachable(gs);
2268 return chosen_guard;
2277 entry_guard_t *guard)
2281 guard->is_reachable = GUARD_REACHABLE_NO;
2282 guard->is_usable_filtered_guard = 0;
2284 guard->is_pending = 0;
2285 if (guard->failing_since == 0)
2291 log_info(
LD_GUARD,
"Recorded failure for %s%sguard %s",
2292 guard->is_primary?
"primary ":
"",
2293 guard->confirmed_idx>=0?
"confirmed ":
"",
2314 entry_guard_t *guard,
2320 const time_t last_time_on_internet = gs->last_time_on_internet;
2324 if (guard->is_reachable != GUARD_REACHABLE_YES) {
2334 guard->is_reachable = GUARD_REACHABLE_YES;
2335 guard->failing_since = 0;
2336 guard->is_pending = 0;
2337 if (guard->is_filtered_guard)
2338 guard->is_usable_filtered_guard = 1;
2340 if (guard->confirmed_idx < 0) {
2342 if (!gs->primary_guards_up_to_date)
2347 switch (old_state) {
2348 case GUARD_CIRC_STATE_COMPLETE:
2349 case GUARD_CIRC_STATE_USABLE_ON_COMPLETION:
2350 new_state = GUARD_CIRC_STATE_COMPLETE;
2355 case GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD:
2356 if (guard->is_primary) {
2364 new_state = GUARD_CIRC_STATE_COMPLETE;
2366 new_state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
2371 if (! guard->is_primary) {
2378 log_info(
LD_GUARD,
"Recorded success for %s%sguard %s",
2379 guard->is_primary?
"primary ":
"",
2380 guard->confirmed_idx>=0?
"confirmed ":
"",
2398 if (a->confirmed_idx < 0) {
2399 if (b->confirmed_idx >= 0)
2402 if (b->confirmed_idx < 0)
2406 return (a->confirmed_idx < b->confirmed_idx);
2411 if (a->is_pending) {
2412 if (! b->is_pending)
2416 return a->last_tried_to_connect < b->last_tried_to_connect;
2441 entry_guard_restriction_free(state->restrictions);
2442 entry_guard_handle_free(state->guard);
2450 entry_guard_restriction_t *rst))
2452 circuit_guard_state_t *result;
2454 result = tor_malloc_zero(
sizeof(circuit_guard_state_t));
2455 result->guard = entry_guard_handle_new(guard);
2456 result->state = state;
2458 result->restrictions = rst;
2476 entry_guard_restriction_t *rst,
2477 const node_t **chosen_node_out,
2478 circuit_guard_state_t **guard_state_out)
2483 *chosen_node_out = NULL;
2484 *guard_state_out = NULL;
2487 entry_guard_t *guard =
2491 if (BUG(state == 0))
2497 if (BUG(usage != GUARD_USAGE_DIRGUARD &&
2501 *chosen_node_out = node;
2506 entry_guard_restriction_free(rst);
2522 if (BUG(*guard_state_p == NULL))
2523 return GUARD_USABLE_NEVER;
2525 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2526 if (! guard || BUG(guard->in_selection == NULL))
2527 return GUARD_USABLE_NEVER;
2531 (*guard_state_p)->state);
2533 (*guard_state_p)->state = newstate;
2536 if (newstate == GUARD_CIRC_STATE_COMPLETE) {
2537 return GUARD_USABLE_NOW;
2539 return GUARD_MAYBE_USABLE_LATER;
2549 if (BUG(*guard_state_p == NULL))
2551 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2557 guard->is_pending = 0;
2558 circuit_guard_state_free(*guard_state_p);
2559 *guard_state_p = NULL;
2570 if (BUG(*guard_state_p == NULL))
2573 entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
2574 if (! guard || BUG(guard->in_selection == NULL))
2579 (*guard_state_p)->state = GUARD_CIRC_STATE_DEAD;
2605 } SMARTLIST_FOREACH_END(circ);
2606 smartlist_free(pending);
2617 if (!gs->primary_guards_up_to_date)
2621 if (guard->is_reachable != GUARD_REACHABLE_NO)
2623 } SMARTLIST_FOREACH_END(guard);
2636 const entry_guard_restriction_t *rst,
2645 entry_guard_t *guard_a = entry_guard_handle_get(state_a->guard);
2646 entry_guard_t *guard_b = entry_guard_handle_get(state_b->guard);
2651 }
else if (! guard_b) {
2683 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2684 "but not all primary guards were definitely down.");
2690 int n_complete_blocking = 0;
2699 entry_guard_t *guard = entry_guard_handle_get(state->guard);
2700 if (!guard || guard->in_selection != gs)
2708 } SMARTLIST_FOREACH_END(circ);
2712 if (BUG(state == NULL))
2715 if (state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD) {
2717 if (! best_waiting_circuit ||
2719 best_waiting_circuit = circ;
2722 } SMARTLIST_FOREACH_END(circ);
2724 if (! best_waiting_circuit) {
2725 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits, "
2726 "but didn't find any.");
2733 const entry_guard_restriction_t *rst_on_best_waiting =
2746 if (BUG(state == NULL))
2748 if (state->state != GUARD_CIRC_STATE_COMPLETE)
2752 best_waiting_circuit))
2753 ++n_complete_blocking;
2754 } SMARTLIST_FOREACH_END(circ);
2756 if (n_complete_blocking) {
2757 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2758 "%d complete and %d guard-stalled. At least one complete "
2759 "circuit had higher priority, so not upgrading.",
2760 n_complete, n_waiting);
2769 int n_blockers_found = 0;
2770 const time_t state_set_at_cutoff =
2774 if (BUG(state == NULL))
2776 if (state->state != GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD)
2778 if (state->state_set_at <= state_set_at_cutoff)
2781 best_waiting_circuit))
2783 } SMARTLIST_FOREACH_END(circ);
2785 if (n_blockers_found) {
2786 log_debug(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2787 "%d guard-stalled, but %d pending circuit(s) had higher "
2788 "guard priority, so not upgrading.",
2789 n_waiting, n_blockers_found);
2796 int n_succeeded = 0;
2799 if (BUG(state == NULL))
2801 if (circ != best_waiting_circuit && rst_on_best_waiting) {
2806 if (state->state != GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD)
2811 state->state = GUARD_CIRC_STATE_COMPLETE;
2815 } SMARTLIST_FOREACH_END(circ);
2817 log_info(
LD_GUARD,
"Considered upgrading guard-stalled circuits: found "
2818 "%d guard-stalled, %d complete. %d of the guard-stalled "
2819 "circuit(s) had high enough priority to upgrade.",
2820 n_waiting, n_complete, n_succeeded);
2822 tor_assert_nonfatal(n_succeeded >= 1);
2823 smartlist_free(all_circuits);
2827 smartlist_free(all_circuits);
2838 if (guard_state == NULL)
2840 const time_t expire_if_waiting_since =
2842 return (guard_state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
2843 && guard_state->state_set_at < expire_if_waiting_since);
2878 char tbuf[ISO_TIME_LEN+1];
2885 if (guard->bridge_addr) {
2888 guard->bridge_addr->port);
2898 if (guard->sampled_by_version) {
2900 guard->sampled_by_version);
2903 if (guard->unlisted_since_date > 0) {
2909 (
int)guard->currently_listed);
2911 if (guard->confirmed_idx >= 0) {
2918 const double EPSILON = 1.0e-6;
2926 #define PB_FIELD(field) do { \
2927 if (pb->field >= EPSILON) { \
2928 smartlist_add_asprintf(result, "pb_" #field "=%f", pb->field); \
2931 PB_FIELD(use_attempts);
2932 PB_FIELD(use_successes);
2933 PB_FIELD(circ_attempts);
2934 PB_FIELD(circ_successes);
2935 PB_FIELD(successful_circuits_closed);
2936 PB_FIELD(collapsed_circuits);
2937 PB_FIELD(unusable_circuits);
2942 if (guard->extra_state_fields)
2947 smartlist_free(result);
2958 *extra, strmap_t *vals)
2961 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
2964 const char *eq = strchr(entry,
'=');
2969 char *key = tor_strndup(entry, eq-entry);
2970 char **target = strmap_get(vals, key);
2971 if (target == NULL || *target != NULL) {
2978 *target = tor_strdup(eq+1);
2981 } SMARTLIST_FOREACH_END(entry);
2989 *unlisted_since,
char *confirmed_on)
2991 #define HANDLE_TIME(field) do { \
2993 int r = parse_iso_time_nospace(field, &field ## _time); \
2995 log_warn(LD_CIRC, "Unable to parse %s %s from guard", \
2996 #field, escaped(field)); \
2997 field##_time = -1; \
3002 time_t sampled_on_time = 0;
3003 time_t unlisted_since_time = 0;
3004 time_t confirmed_on_time = 0;
3006 HANDLE_TIME(sampled_on);
3007 HANDLE_TIME(unlisted_since);
3008 HANDLE_TIME(confirmed_on);
3010 if (sampled_on_time <= 0)
3012 if (unlisted_since_time < 0)
3013 unlisted_since_time = 0;
3014 if (confirmed_on_time < 0)
3015 confirmed_on_time = 0;
3019 guard->sampled_on_date = sampled_on_time;
3020 guard->unlisted_since_date = unlisted_since_time;
3021 guard->confirmed_on_date = confirmed_on_time;
3037 char *rsa_id = NULL;
3038 char *nickname = NULL;
3039 char *sampled_on = NULL;
3040 char *sampled_idx = NULL;
3041 char *sampled_by = NULL;
3042 char *unlisted_since = NULL;
3043 char *listed = NULL;
3044 char *confirmed_on = NULL;
3045 char *confirmed_idx = NULL;
3046 char *bridge_addr = NULL;
3049 char *pb_use_attempts = NULL;
3050 char *pb_use_successes = NULL;
3051 char *pb_circ_attempts = NULL;
3052 char *pb_circ_successes = NULL;
3053 char *pb_successful_circuits_closed = NULL;
3054 char *pb_collapsed_circuits = NULL;
3055 char *pb_unusable_circuits = NULL;
3056 char *pb_timeouts = NULL;
3064 strmap_t *vals = strmap_new();
3066 strmap_set(vals, #f, &f);
3073 FIELD(unlisted_since);
3075 FIELD(confirmed_on);
3076 FIELD(confirmed_idx);
3078 FIELD(pb_use_attempts);
3079 FIELD(pb_use_successes);
3080 FIELD(pb_circ_attempts);
3081 FIELD(pb_circ_successes);
3082 FIELD(pb_successful_circuits_closed);
3083 FIELD(pb_collapsed_circuits);
3084 FIELD(pb_unusable_circuits);
3090 smartlist_free(entries);
3091 strmap_free(vals, NULL);
3094 entry_guard_t *guard = tor_malloc_zero(
sizeof(entry_guard_t));
3095 guard->is_persistent = 1;
3098 log_warn(
LD_CIRC,
"Guard missing 'in' field");
3102 guard->selection_name = in;
3105 if (rsa_id == NULL) {
3106 log_warn(
LD_CIRC,
"Guard missing RSA ID field");
3113 log_warn(
LD_CIRC,
"Unable to decode guard identity %s",
escaped(rsa_id));
3118 strlcpy(guard->nickname, nickname,
sizeof(guard->nickname));
3120 guard->nickname[0]=
'$';
3127 memset(&res, 0,
sizeof(res));
3129 &res.addr, &res.port, -1);
3131 guard->bridge_addr = tor_memdup(&res,
sizeof(res));
3140 guard->sampled_by_version = sampled_by;
3143 if (listed && strcmp(listed,
"0"))
3144 guard->currently_listed = 1;
3147 guard->confirmed_idx = -1;
3148 if (confirmed_idx) {
3150 long idx =
tor_parse_long(confirmed_idx, 10, 0, INT_MAX, &ok, NULL);
3152 log_warn(
LD_GUARD,
"Guard has invalid confirmed_idx %s",
3155 guard->confirmed_idx = (int)idx;
3161 long idx =
tor_parse_long(sampled_idx, 10, 0, INT_MAX, &ok, NULL);
3163 log_warn(
LD_GUARD,
"Guard has invalid sampled_idx %s",
3166 guard->sampled_idx = invalid_sampled_idx++;
3168 guard->sampled_idx = (int)idx;
3170 }
else if (confirmed_idx) {
3174 guard->sampled_idx = guard->confirmed_idx;
3176 log_info(
LD_GUARD,
"The state file seems to be into a status that could"
3177 " yield to weird entry node selection: we're missing both a"
3178 " sampled_idx and a confirmed_idx.");
3179 guard->sampled_idx = invalid_sampled_idx++;
3183 if (smartlist_len(extra) > 0) {
3188 guard->is_reachable = GUARD_REACHABLE_MAYBE;
3190 #define PB_FIELD(field) \
3192 if (pb_ ## field) { \
3194 double r = tor_parse_double(pb_ ## field, 0.0, 1e9, &ok, NULL); \
3196 log_warn(LD_CIRC, "Guard has invalid pb_%s %s", \
3197 #field, pb_ ## field); \
3199 guard->pb.field = r; \
3203 PB_FIELD(use_attempts);
3204 PB_FIELD(use_successes);
3205 PB_FIELD(circ_attempts);
3206 PB_FIELD(circ_successes);
3207 PB_FIELD(successful_circuits_closed);
3208 PB_FIELD(collapsed_circuits);
3209 PB_FIELD(unusable_circuits);
3223 entry_guard_free(guard);
3242 tor_free(pb_successful_circuits_closed);
3248 smartlist_free(extra);
3268 if (guard->is_persistent == 0)
3271 (*nextline)->key = tor_strdup(
"Guard");
3273 nextline = &(*nextline)->next;
3275 } SMARTLIST_FOREACH_END(guard);
3276 } SMARTLIST_FOREACH_END(gs);
3278 config_free_lines(state->
Guard);
3279 state->
Guard = lines;
3300 guard_selection_free(gs);
3304 } SMARTLIST_FOREACH_END(gs);
3307 for ( ; line != NULL; line = line->next) {
3309 if (guard == NULL) {
3314 if (!strcmp(guard->selection_name,
"legacy")) {
3316 entry_guard_free(guard);
3321 guard_selection_t *gs;
3326 guard->in_selection = gs;
3331 if (gs->next_sampled_idx <= guard->sampled_idx) {
3332 gs->next_sampled_idx = guard->sampled_idx + 1;
3336 entry_guard_free(guard);
3347 } SMARTLIST_FOREACH_END(gs);
3349 return n_errors ? -1 : 0;
3383 circuit_guard_state_t *
3386 circuit_guard_state_t *guard_state = NULL;
3387 entry_guard_t *guard = NULL;
3401 GUARD_CIRC_STATE_USABLE_ON_COMPLETION,
3413 entry_guard_handles_clear(e);
3454 if (BUG(gs->type != GS_TYPE_BRIDGE)) {
3460 if (! guard->is_filtered_guard)
3463 if (guard->is_reachable == GUARD_REACHABLE_NO)
3466 if (!use_maybe_reachable && guard->is_reachable == GUARD_REACHABLE_MAYBE)
3471 if (node && node->ri)
3473 } SMARTLIST_FOREACH_END(guard);
3484 const double EPSILON = 1.0e-9;
3489 if (node->pb.use_attempts >
EPSILON &&
3493 node->pb.path_bias_disabled = 1;
3495 "Path use bias is too high (%f/%f); disabling node %s",
3496 node->pb.circ_successes, node->pb.circ_attempts,
3507 const double EPSILON = 1.0e-9;
3512 if (node->pb.circ_attempts >
EPSILON &&
3516 node->pb.path_bias_disabled = 1;
3518 "Path bias is too high (%f/%f); disabling node %s",
3519 node->pb.circ_successes, node->pb.circ_attempts,
3538 if (msg && *msg == NULL) {
3539 *msg = tor_strdup(
"parsing error");
3548 #define SLOW_GUARD_STATE_FLUSH_TIME 600
3551 #define FAST_GUARD_STATE_FLUSH_TIME 30
3626 entry_guard_t *guard = entry_guard_handle_get(guard_state->guard);
3627 if (!guard || BUG(guard->in_selection == NULL)) {
3641 const char *status = NULL;
3644 char tbuf[ISO_TIME_LEN+1];
3652 if (e->confirmed_idx < 0) {
3653 status =
"never-connected";
3654 }
else if (! e->currently_listed) {
3655 when = e->unlisted_since_date;
3656 status =
"unusable";
3657 }
else if (! e->is_filtered_guard) {
3658 status =
"unusable";
3659 }
else if (e->is_reachable == GUARD_REACHABLE_NO) {
3660 when = e->failing_since;
3676 char *result = NULL;
3679 tor_asprintf(&result,
"%s %s %s\n", nbuf, status, tbuf);
3697 const char *question,
char **answer,
3698 const char **errmsg)
3707 if (!strcmp(question,
"entry-guards") ||
3708 !strcmp(question,
"helper-nodes")) {
3710 guards = gs->sampled_entry_guards;
3717 } SMARTLIST_FOREACH_END(e);
3744 uint32_t guardfraction_percentage)
3746 double guardfraction_fraction;
3750 guardfraction_fraction = guardfraction_percentage / 100.0;
3752 long guard_bw =
tor_lround(guardfraction_fraction * orig_bandwidth);
3755 guardfraction_bw->
guard_bw = (int) guard_bw;
3757 guardfraction_bw->
non_guard_bw = orig_bandwidth - (int) guard_bw;
3768 int mark_circuits = 0;
3777 return mark_circuits;
3785 circuit_guard_state_t **guard_state_out)
3788 const uint8_t *exit_id = NULL;
3789 entry_guard_restriction_t *rst = NULL;
3799 rst = guard_create_exit_restriction(exit_id);
3803 GUARD_USAGE_TRAFFIC,
3806 guard_state_out) < 0) {
3820 char *old_name = tor_strdup(gs->name);
3821 guard_selection_type_t old_type = gs->type;
3824 control_event_guard(entry->nickname, entry->identity,
"DROPPED");
3832 guard_selection_free(gs);
3853 circuit_guard_state_t **guard_state_out)
3856 entry_guard_restriction_t *rst = NULL;
3864 GUARD_USAGE_DIRGUARD,
3867 guard_state_out) < 0) {
3897 int num_present,
int num_usable)
3899 if (!gs->primary_guards_up_to_date)
3902 char *ret_str = NULL;
3903 int n_missing_descriptors = 0;
3904 int n_considered = 0;
3905 int num_primary_to_check;
3911 num_primary_to_check++;
3915 if (guard->is_reachable == GUARD_REACHABLE_NO)
3919 n_missing_descriptors++;
3920 if (n_considered >= num_primary_to_check)
3922 } SMARTLIST_FOREACH_END(guard);
3925 if (!n_missing_descriptors) {
3930 tor_asprintf(&ret_str,
"We're missing descriptors for %d/%d of our "
3931 "primary entry guards (total %sdescriptors: %d/%d). "
3932 "That's ok. We will try to fetch missing descriptors soon.",
3933 n_missing_descriptors, num_primary_to_check,
3934 using_mds?
"micro":
"", num_present, num_usable);
3943 int num_present,
int num_usable)
3948 num_present, num_usable);
3959 if (gs->sampled_entry_guards) {
3961 entry_guard_free(e));
3962 smartlist_free(gs->sampled_entry_guards);
3963 gs->sampled_entry_guards = NULL;
3966 smartlist_free(gs->confirmed_entry_guards);
3967 smartlist_free(gs->primary_entry_guards);
3986 #define layer2_guard_free(val) \
3987 FREE_AND_NULL(layer2_guard_t, layer2_guard_free_, (val))
4028 static routerset_t *layer2_routerset = NULL;
4031 #define NUMBER_SECOND_GUARDS 4
4038 #define MIN_SECOND_GUARD_LIFETIME (3600*24)
4039 #define MAX_SECOND_GUARD_LIFETIME (3600*24*12)
4046 "guard-hs-l2-number",
4056 "guard-hs-l2-lifetime-min",
4066 "guard-hs-l2-lifetime-max",
4067 MAX_SECOND_GUARD_LIFETIME,
4082 if (BUG(min >= max)) {
4108 log_info(
LD_GENERAL,
"Removing expired Layer2 guard %s",
4112 layer2_guard_free(g);
4119 log_info(
LD_GENERAL,
"Removing missing Layer2 guard %s",
4123 layer2_guard_free(g);
4127 } SMARTLIST_FOREACH_END(g);
4130 int new_guards_needed_n =
4132 if (new_guards_needed_n <= 0) {
4136 log_info(
LD_GENERAL,
"Adding %d guards to Layer2 routerset",
4137 new_guards_needed_n);
4147 } SMARTLIST_FOREACH_END(g);
4150 for (
int i = 0; i < new_guards_needed_n; i++) {
4151 const node_t *choice = NULL;
4166 log_info(
LD_GENERAL,
"Adding Layer2 guard %s",
4177 smartlist_free(excluded);
4180 routerset_free(layer2_routerset);
4187 } SMARTLIST_FOREACH_END(g);
4203 layer2_guard_free(g);
4204 } SMARTLIST_FOREACH_END(g);
4224 return layer2_routerset;
4239 guard_selection_free(gs);
4240 } SMARTLIST_FOREACH_END(gs);
4251 layer2_guard_free(g);
4252 } SMARTLIST_FOREACH_END(g);
4255 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)
bridge_info_t * get_configured_bridge_by_exact_addr_port_digest(const tor_addr_t *addr, uint16_t port, const char *digest)
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 uint8_t * bridge_get_rsa_id_digest(const bridge_info_t *bridge)
download_status_t * get_bridge_dl_status_by_id(const char *digest)
const smartlist_t * bridge_list_get(void)
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)
const uint8_t * build_state_get_exit_rsa_id(cpath_build_state_t *state)
circuit_guard_state_t * origin_circuit_get_guard_state(origin_circuit_t *circ)
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.
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)
int digestset_probably_contains(const digestset_t *set, const char *digest)
digestset_t * digestset_new(int max_guess)
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)
STATIC int get_n_primary_guards(void)
entry_guard_t * entry_guard_get_by_id_digest(const char *digest)
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)
char * guard_selection_get_err_str_if_dir_info_missing(guard_selection_t *gs, int using_mds, int num_present, int num_usable)
static void entry_guards_update_guards_in_state(or_state_t *state)
static entry_guard_t * select_and_add_guard_item_for_sample(guard_selection_t *gs, smartlist_t *eligible_guards)
void entry_guards_changed(void)
const node_t * entry_guard_find_node(const entry_guard_t *guard)
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)
char * entry_guards_get_err_str_if_dir_info_missing(int using_mds, int num_present, int num_usable)
void remove_all_entry_guards(void)
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 entry_guard_t * get_sampled_guard_for_bridge(guard_selection_t *gs, const bridge_info_t *bridge)
STATIC entry_guard_t * entry_guards_expand_sample(guard_selection_t *gs)
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 int get_nonprimary_guard_idle_timeout(void)
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)
guard_pathbias_t * entry_guard_get_pathbias_state(entry_guard_t *guard)
const char * entry_guard_describe(const entry_guard_t *guard)
static entry_guard_t * entry_guard_add_bridge_to_sample(guard_selection_t *gs, const bridge_info_t *bridge)
static smartlist_t * layer2_guards
STATIC int get_min_filtered_sample_size(void)
STATIC guard_selection_t * get_guard_selection_by_name(const char *name, guard_selection_type_t type, int create_if_absent)
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)
STATIC entry_guard_t * entry_guard_add_to_sample(guard_selection_t *gs, const node_t *node)
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_restriction_t * guard_create_dirserver_md_restriction(void)
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 routerset_t * get_layer2_guards(void)
STATIC entry_guard_t * first_reachable_filtered_entry_guard(guard_selection_t *gs, const entry_guard_restriction_t *rst, unsigned flags)
STATIC void entry_guards_update_confirmed(guard_selection_t *gs)
STATIC void entry_guards_update_filtered_sets(guard_selection_t *gs)
void entry_guards_update_state(or_state_t *state)
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)
bool vanguards_lite_is_enabled(void)
static void pathbias_check_close_success_count(entry_guard_t *guard)
const char * entry_guard_get_rsa_id_digest(const 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_t * get_sampled_guard_with_id(guard_selection_t *gs, const uint8_t *rsa_id)
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 char * getinfo_helper_format_single_entry_guard(const entry_guard_t *e)
STATIC int get_remove_unlisted_guards_after_days(void)
const node_t * guards_choose_dirguard(uint8_t dir_purpose, circuit_guard_state_t **guard_state_out)
static void create_initial_guard_context(void)
STATIC int get_max_sample_size_absolute(void)
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_nonprimary_guard_connect_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)
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)
entry_guard_t * entry_guard_get_by_id_digest_for_guard_selection(guard_selection_t *gs, const char *digest)
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)
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)
int entry_guard_could_succeed(const circuit_guard_state_t *guard_state)
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)
circuit_guard_state_t * get_guard_state_for_bridge_desc_fetch(const char *digest)
STATIC char * entry_guard_encode_for_state(entry_guard_t *guard, int dense_sampled_idx)
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)
const node_t * guards_choose_guard(cpath_build_state_t *state, uint8_t purpose, circuit_guard_state_t **guard_state_out)
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)
#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 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 circuit_guard_state_t * circuit_guard_state_new(entry_guard_t *guard, unsigned state, 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 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)
#define FAST_GUARD_STATE_FLUSH_TIME
static int node_is_possible_guard(const node_t *node)
#define MIN_SECOND_GUARD_LIFETIME
static smartlist_t * get_eligible_guards(const or_options_t *options, guard_selection_t *gs, int *n_guards_out)
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)
STATIC int get_guard_lifetime(void)
static int get_max_lifetime_of_layer2_hs_guards(void)
guard_selection_t * get_guard_selection_info(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)
static int entry_guards_dirty
static int node_passes_guard_filter(const or_options_t *options, const node_t *node)
static bridge_info_t * get_bridge_info_for_guard(const entry_guard_t *guard)
STATIC guard_selection_t * guard_selection_new(const char *name, guard_selection_type_t type)
STATIC entry_guard_t * entry_guard_parse_from_state(const char *s)
void entry_guards_free_all(void)
STATIC int get_internet_likely_down_interval(void)
Header file for circuitbuild.c.
const char * escaped(const char *s)
long tor_lround(double d)
Header file for mainloop.c.
int usable_consensus_flavor(void)
int microdesc_relay_is_outdated_dirserver(const char *relay_digest)
Header file for microdesc.c.
networkstatus_t * networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
const routerstatus_t * router_get_consensus_status_by_id(const char *digest)
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Header file for networkstatus.c.
int is_legal_nickname(const char *s)
Header file for nickname.c.
const node_t * router_choose_random_node(smartlist_t *excludedsmartlist, routerset_t *excludedset, router_crn_flags_t flags)
const node_t * node_sl_choose_by_bandwidth(const smartlist_t *sl, bandwidth_weight_rule_t rule)
Header file for node_select.c.
Node information structure.
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)
const node_t * node_get_by_id(const char *identity_digest)
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.
int routerset_contains_node(const routerset_t *set, const node_t *node)
int routerset_parse(routerset_t *target, const char *s, const char *description)
routerset_t * routerset_new(void)
int routerset_contains_bridge(const routerset_t *set, const bridge_info_t *bridge)
Header file for routerset.c.
int smartlist_ptrs_eq(const smartlist_t *s1, const smartlist_t *s2)
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)
smartlist_t * smartlist_new(void)
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
int smartlist_contains(const smartlist_t *sl, const void *element)
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)
or_state_t * get_or_state(void)
void or_state_mark_dirty(or_state_t *state, time_t when)
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)