entrynodes.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file entrynodes.h
  8. * \brief Header file for circuitbuild.c.
  9. **/
  10. #ifndef TOR_ENTRYNODES_H
  11. #define TOR_ENTRYNODES_H
  12. #include "lib/container/handles.h"
  13. /* Forward declare for guard_selection_t; entrynodes.c has the real struct */
  14. typedef struct guard_selection_s guard_selection_t;
  15. /* Forward declare for entry_guard_t; the real declaration is private. */
  16. typedef struct entry_guard_t entry_guard_t;
  17. /* Forward declaration for circuit_guard_state_t; the real declaration is
  18. private. */
  19. typedef struct circuit_guard_state_t circuit_guard_state_t;
  20. /* Forward declaration for entry_guard_restriction_t; the real declaration is
  21. private. */
  22. typedef struct entry_guard_restriction_t entry_guard_restriction_t;
  23. /* Information about a guard's pathbias status.
  24. * These fields are used in circpathbias.c to try to detect entry
  25. * nodes that are failing circuits at a suspicious frequency.
  26. */
  27. typedef struct guard_pathbias_t {
  28. unsigned int path_bias_noticed : 1; /**< Did we alert the user about path
  29. * bias for this node already? */
  30. unsigned int path_bias_warned : 1; /**< Did we alert the user about path bias
  31. * for this node already? */
  32. unsigned int path_bias_extreme : 1; /**< Did we alert the user about path
  33. * bias for this node already? */
  34. unsigned int path_bias_disabled : 1; /**< Have we disabled this node because
  35. * of path bias issues? */
  36. unsigned int path_bias_use_noticed : 1; /**< Did we alert the user about path
  37. * use bias for this node already? */
  38. unsigned int path_bias_use_extreme : 1; /**< Did we alert the user about path
  39. * use bias for this node already? */
  40. double circ_attempts; /**< Number of circuits this guard has "attempted" */
  41. double circ_successes; /**< Number of successfully built circuits using
  42. * this guard as first hop. */
  43. double successful_circuits_closed; /**< Number of circuits that carried
  44. * streams successfully. */
  45. double collapsed_circuits; /**< Number of fully built circuits that were
  46. * remotely closed before any streams were
  47. * attempted. */
  48. double unusable_circuits; /**< Number of circuits for which streams were
  49. * attempted, but none succeeded. */
  50. double timeouts; /**< Number of 'right-censored' circuit timeouts for this
  51. * guard. */
  52. double use_attempts; /**< Number of circuits we tried to use with streams */
  53. double use_successes; /**< Number of successfully used circuits using
  54. * this guard as first hop. */
  55. } guard_pathbias_t;
  56. #if defined(ENTRYNODES_PRIVATE)
  57. #include "lib/crypt_ops/crypto_ed25519.h"
  58. /**
  59. * @name values for entry_guard_t.is_reachable.
  60. *
  61. * See entry_guard_t.is_reachable for more information.
  62. */
  63. /**@{*/
  64. #define GUARD_REACHABLE_NO 0
  65. #define GUARD_REACHABLE_YES 1
  66. #define GUARD_REACHABLE_MAYBE 2
  67. /**@}*/
  68. /** An entry_guard_t represents our information about a chosen long-term
  69. * first hop, known as a "helper" node in the literature. We can't just
  70. * use a node_t, since we want to remember these even when we
  71. * don't have any directory info. */
  72. struct entry_guard_t {
  73. HANDLE_ENTRY(entry_guard, entry_guard_t);
  74. char nickname[MAX_HEX_NICKNAME_LEN+1];
  75. char identity[DIGEST_LEN];
  76. ed25519_public_key_t ed_id;
  77. /**
  78. * @name new guard selection algorithm fields.
  79. *
  80. * Only the new (prop271) algorithm uses these. For a more full
  81. * description of the algorithm, see the module documentation for
  82. * entrynodes.c
  83. */
  84. /**@{*/
  85. /* == Persistent fields, present for all sampled guards. */
  86. /** When was this guard added to the sample? */
  87. time_t sampled_on_date;
  88. /** Since what date has this guard been "unlisted"? A guard counts as
  89. * unlisted if we have a live consensus that does not include it, or
  90. * if we have a live consensus that does not include it as a usable
  91. * guard. This field is zero when the guard is listed. */
  92. time_t unlisted_since_date; // can be zero
  93. /** What version of Tor added this guard to the sample? */
  94. char *sampled_by_version;
  95. /** Is this guard listed right now? If this is set, then
  96. * unlisted_since_date should be set too. */
  97. unsigned currently_listed : 1;
  98. /* == Persistent fields, for confirmed guards only */
  99. /** When was this guard confirmed? (That is, when did we first use it
  100. * successfully and decide to keep it?) This field is zero if this is not a
  101. * confirmed guard. */
  102. time_t confirmed_on_date; /* 0 if not confirmed */
  103. /**
  104. * In what order was this guard confirmed? Guards with lower indices
  105. * appear earlier on the confirmed list. If the confirmed list is compacted,
  106. * this field corresponds to the index of this guard on the confirmed list.
  107. *
  108. * This field is set to -1 if this guard is not confirmed.
  109. */
  110. int confirmed_idx; /* -1 if not confirmed; otherwise the order that this
  111. * item should occur in the CONFIRMED_GUARDS ordered
  112. * list */
  113. /**
  114. * Which selection does this guard belong to?
  115. */
  116. char *selection_name;
  117. /** Bridges only: address of the bridge. */
  118. tor_addr_port_t *bridge_addr;
  119. /* ==== Non-persistent fields. */
  120. /* == These are used by sampled guards */
  121. /** When did we last decide to try using this guard for a circuit? 0 for
  122. * "not since we started up." */
  123. time_t last_tried_to_connect;
  124. /** How reachable do we consider this guard to be? One of
  125. * GUARD_REACHABLE_NO, GUARD_REACHABLE_YES, or GUARD_REACHABLE_MAYBE. */
  126. unsigned is_reachable : 2;
  127. /** Boolean: true iff this guard is pending. A pending guard is one
  128. * that we have an in-progress circuit through, and which we do not plan
  129. * to try again until it either succeeds or fails. Primary guards can
  130. * never be pending. */
  131. unsigned is_pending : 1;
  132. /** If true, don't write this guard to disk. (Used for bridges with unknown
  133. * identities) */
  134. unsigned is_persistent : 1;
  135. /** When did we get the earliest connection failure for this guard?
  136. * We clear this field on a successful connect. We do _not_ clear it
  137. * when we mark the guard as "MAYBE" reachable.
  138. */
  139. time_t failing_since;
  140. /* == Set inclusion flags. */
  141. /** If true, this guard is in the filtered set. The filtered set includes
  142. * all sampled guards that our configuration allows us to use. */
  143. unsigned is_filtered_guard : 1;
  144. /** If true, this guard is in the usable filtered set. The usable filtered
  145. * set includes all filtered guards that are not believed to be
  146. * unreachable. (That is, those for which is_reachable is not
  147. * GUARD_REACHABLE_NO) */
  148. unsigned is_usable_filtered_guard : 1;
  149. unsigned is_primary:1;
  150. /** This string holds any fields that we are maintaining because
  151. * we saw them in the state, even if we don't understand them. */
  152. char *extra_state_fields;
  153. /** Backpointer to the guard selection that this guard belongs to.
  154. * The entry_guard_t must never outlive its guard_selection. */
  155. guard_selection_t *in_selection;
  156. /**@}*/
  157. /** Path bias information for this guard. */
  158. guard_pathbias_t pb;
  159. };
  160. /**
  161. * Possible rules for a guard selection to follow
  162. */
  163. typedef enum guard_selection_type_t {
  164. /** Infer the type of this selection from its name. */
  165. GS_TYPE_INFER=0,
  166. /** Use the normal guard selection algorithm, taking our sample from the
  167. * complete list of guards in the consensus. */
  168. GS_TYPE_NORMAL=1,
  169. /** Use the normal guard selection algorithm, taking our sample from the
  170. * configured bridges, and allowing it to grow as large as all the configured
  171. * bridges */
  172. GS_TYPE_BRIDGE,
  173. /** Use the normal guard selection algorithm, taking our sample from the
  174. * set of filtered nodes. */
  175. GS_TYPE_RESTRICTED,
  176. } guard_selection_type_t;
  177. /**
  178. * All of the the context for guard selection on a particular client.
  179. *
  180. * We maintain multiple guard selection contexts for a client, depending
  181. * aspects on its current configuration -- whether an extremely
  182. * restrictive EntryNodes is used, whether UseBridges is enabled, and so
  183. * on.)
  184. *
  185. * See the module documentation for entrynodes.c for more information
  186. * about guard selection algorithms.
  187. */
  188. struct guard_selection_s {
  189. /**
  190. * The name for this guard-selection object. (Must not contain spaces).
  191. */
  192. char *name;
  193. /**
  194. * What rules does this guard-selection object follow?
  195. */
  196. guard_selection_type_t type;
  197. /**
  198. * A value of 1 means that primary_entry_guards is up-to-date with respect to
  199. * the consensus and status info that we currently have; 0 means we need to
  200. * recalculate it before using primary_entry_guards or the is_primary flag on
  201. * any guard.
  202. */
  203. int primary_guards_up_to_date;
  204. /**
  205. * A list of the sampled entry guards, as entry_guard_t structures.
  206. * Not in any particular order. When we 'sample' a guard, we are
  207. * noting it as a possible guard to pick in the future. The use of
  208. * sampling here prevents us from being forced by an attacker to try
  209. * every guard on the network. This list is persistent.
  210. */
  211. smartlist_t *sampled_entry_guards;
  212. /**
  213. * Ordered list (from highest to lowest priority) of guards that we
  214. * have successfully contacted and decided to use. Every member of
  215. * this list is a member of sampled_entry_guards. Every member should
  216. * have confirmed_on_date set, and have confirmed_idx greater than
  217. * any earlier member of the list.
  218. *
  219. * This list is persistent. It is a subset of the elements in
  220. * sampled_entry_guards, and its pointers point to elements of
  221. * sampled_entry_guards.
  222. */
  223. smartlist_t *confirmed_entry_guards;
  224. /**
  225. * Ordered list (from highest to lowest priority) of guards that we
  226. * are willing to use the most happily. These guards may or may not
  227. * yet be confirmed yet. If we can use one of these guards, we are
  228. * probably not on a network that is trying to restrict our guard
  229. * choices.
  230. *
  231. * This list is a subset of the elements in
  232. * sampled_entry_guards, and its pointers point to elements of
  233. * sampled_entry_guards.
  234. */
  235. smartlist_t *primary_entry_guards;
  236. /** When did we last successfully build a circuit or use a circuit? */
  237. time_t last_time_on_internet;
  238. /** What confirmed_idx value should the next-added member of
  239. * confirmed_entry_guards receive? */
  240. int next_confirmed_idx;
  241. };
  242. struct entry_guard_handle_t;
  243. /** Types of restrictions we impose when picking guard nodes */
  244. typedef enum guard_restriction_type_t {
  245. /* Don't pick the same guard node as our exit node (or its family) */
  246. RST_EXIT_NODE = 0,
  247. /* Don't pick dirguards that have previously shown to be outdated */
  248. RST_OUTDATED_MD_DIRSERVER = 1
  249. } guard_restriction_type_t;
  250. /**
  251. * A restriction to remember which entry guards are off-limits for a given
  252. * circuit.
  253. *
  254. * Note: This mechanism is NOT for recording which guards are never to be
  255. * used: only which guards cannot be used on <em>one particular circuit</em>.
  256. */
  257. struct entry_guard_restriction_t {
  258. /* What type of restriction are we imposing? */
  259. guard_restriction_type_t type;
  260. /* In case of restriction type RST_EXIT_NODE, the guard's RSA identity
  261. * digest must not equal this; and it must not be in the same family as any
  262. * node with this digest. */
  263. uint8_t exclude_id[DIGEST_LEN];
  264. };
  265. /**
  266. * Per-circuit state to track whether we'll be able to use the circuit.
  267. */
  268. struct circuit_guard_state_t {
  269. /** Handle to the entry guard object for this circuit. */
  270. struct entry_guard_handle_t *guard;
  271. /** The time at which <b>state</b> last changed. */
  272. time_t state_set_at;
  273. /** One of GUARD_CIRC_STATE_* */
  274. uint8_t state;
  275. /**
  276. * A set of restrictions that were placed on this guard when we selected it
  277. * for this particular circuit. We need to remember the restrictions here,
  278. * since any guard that breaks these restrictions will not block this
  279. * circuit from becoming COMPLETE.
  280. */
  281. entry_guard_restriction_t *restrictions;
  282. };
  283. #endif /* defined(ENTRYNODES_PRIVATE) */
  284. /* Common entry points for old and new guard code */
  285. int guards_update_all(void);
  286. const node_t *guards_choose_guard(cpath_build_state_t *state,
  287. uint8_t purpose,
  288. circuit_guard_state_t **guard_state_out);
  289. const node_t *guards_choose_dirguard(uint8_t dir_purpose,
  290. circuit_guard_state_t **guard_state_out);
  291. #if 1
  292. /* XXXX NM I would prefer that all of this stuff be private to
  293. * entrynodes.c. */
  294. entry_guard_t *entry_guard_get_by_id_digest_for_guard_selection(
  295. guard_selection_t *gs, const char *digest);
  296. entry_guard_t *entry_guard_get_by_id_digest(const char *digest);
  297. circuit_guard_state_t *
  298. get_guard_state_for_bridge_desc_fetch(const char *digest);
  299. void entry_guards_changed_for_guard_selection(guard_selection_t *gs);
  300. void entry_guards_changed(void);
  301. guard_selection_t * get_guard_selection_info(void);
  302. int num_live_entry_guards_for_guard_selection(
  303. guard_selection_t *gs,
  304. int for_directory);
  305. int num_live_entry_guards(int for_directory);
  306. #endif /* 1 */
  307. const node_t *entry_guard_find_node(const entry_guard_t *guard);
  308. const char *entry_guard_get_rsa_id_digest(const entry_guard_t *guard);
  309. const char *entry_guard_describe(const entry_guard_t *guard);
  310. guard_pathbias_t *entry_guard_get_pathbias_state(entry_guard_t *guard);
  311. /** Enum to specify how we're going to use a given guard, when we're picking
  312. * one for immediate use. */
  313. typedef enum {
  314. GUARD_USAGE_TRAFFIC = 0,
  315. GUARD_USAGE_DIRGUARD = 1
  316. } guard_usage_t;
  317. #define circuit_guard_state_free(val) \
  318. FREE_AND_NULL(circuit_guard_state_t, circuit_guard_state_free_, (val))
  319. void circuit_guard_state_free_(circuit_guard_state_t *state);
  320. int entry_guard_pick_for_circuit(guard_selection_t *gs,
  321. guard_usage_t usage,
  322. entry_guard_restriction_t *rst,
  323. const node_t **chosen_node_out,
  324. circuit_guard_state_t **guard_state_out);
  325. /* We just connected to an entry guard. What should we do with the circuit? */
  326. typedef enum {
  327. GUARD_USABLE_NEVER = -1, /* Never use the circuit */
  328. GUARD_MAYBE_USABLE_LATER = 0, /* Keep it. We might use it in the future */
  329. GUARD_USABLE_NOW = 1, /* Use it right now */
  330. } guard_usable_t;
  331. guard_usable_t entry_guard_succeeded(circuit_guard_state_t **guard_state_p);
  332. void entry_guard_failed(circuit_guard_state_t **guard_state_p);
  333. void entry_guard_cancel(circuit_guard_state_t **guard_state_p);
  334. void entry_guard_chan_failed(channel_t *chan);
  335. int entry_guards_update_all(guard_selection_t *gs);
  336. int entry_guards_upgrade_waiting_circuits(guard_selection_t *gs,
  337. const smartlist_t *all_circuits,
  338. smartlist_t *newly_complete_out);
  339. int entry_guard_state_should_expire(circuit_guard_state_t *guard_state);
  340. void entry_guards_note_internet_connectivity(guard_selection_t *gs);
  341. int update_guard_selection_choice(const or_options_t *options);
  342. int entry_guard_could_succeed(const circuit_guard_state_t *guard_state);
  343. MOCK_DECL(int,num_bridges_usable,(int use_maybe_reachable));
  344. #ifdef ENTRYNODES_PRIVATE
  345. /**
  346. * @name Default values for the parameters for the new (prop271) entry guard
  347. * algorithm.
  348. */
  349. /**@{*/
  350. /**
  351. * We never let our sampled guard set grow larger than this percentage
  352. * of the guards on the network.
  353. */
  354. #define DFLT_MAX_SAMPLE_THRESHOLD_PERCENT 20
  355. /**
  356. * We never let our sampled guard set grow larger than this number of
  357. * guards.
  358. */
  359. #define DFLT_MAX_SAMPLE_SIZE 60
  360. /**
  361. * We always try to make our sample contain at least this many guards.
  362. */
  363. #define DFLT_MIN_FILTERED_SAMPLE_SIZE 20
  364. /**
  365. * If a guard is unlisted for this many days in a row, we remove it.
  366. */
  367. #define DFLT_REMOVE_UNLISTED_GUARDS_AFTER_DAYS 20
  368. /**
  369. * We remove unconfirmed guards from the sample after this many days,
  370. * regardless of whether they are listed or unlisted.
  371. */
  372. #define DFLT_GUARD_LIFETIME_DAYS 120
  373. /**
  374. * We remove confirmed guards from the sample if they were sampled
  375. * GUARD_LIFETIME_DAYS ago and confirmed this many days ago.
  376. */
  377. #define DFLT_GUARD_CONFIRMED_MIN_LIFETIME_DAYS 60
  378. /**
  379. * How many guards do we try to keep on our primary guard list?
  380. */
  381. #define DFLT_N_PRIMARY_GUARDS 3
  382. /**
  383. * Of the live guards on the primary guard list, how many do we consider when
  384. * choosing a guard to use?
  385. */
  386. #define DFLT_N_PRIMARY_GUARDS_TO_USE 1
  387. /**
  388. * As DFLT_N_PRIMARY_GUARDS, but for choosing which directory guard to use.
  389. */
  390. #define DFLT_N_PRIMARY_DIR_GUARDS_TO_USE 3
  391. /**
  392. * If we haven't successfully built or used a circuit in this long, then
  393. * consider that the internet is probably down.
  394. */
  395. #define DFLT_INTERNET_LIKELY_DOWN_INTERVAL (10*60)
  396. /**
  397. * If we're trying to connect to a nonprimary guard for at least this
  398. * many seconds, and we haven't gotten the connection to work, we will treat
  399. * lower-priority guards as usable.
  400. */
  401. #define DFLT_NONPRIMARY_GUARD_CONNECT_TIMEOUT 15
  402. /**
  403. * If a circuit has been sitting around in 'waiting for better guard' state
  404. * for at least this long, we'll expire it.
  405. */
  406. #define DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT (10*60)
  407. /**
  408. * If our configuration retains fewer than this fraction of guards from the
  409. * torrc, we are in a restricted setting.
  410. */
  411. #define DFLT_MEANINGFUL_RESTRICTION_PERCENT 20
  412. /**
  413. * If our configuration retains fewer than this fraction of guards from the
  414. * torrc, we are in an extremely restricted setting, and should warn.
  415. */
  416. #define DFLT_EXTREME_RESTRICTION_PERCENT 1
  417. /**@}*/
  418. STATIC double get_max_sample_threshold(void);
  419. STATIC int get_max_sample_size_absolute(void);
  420. STATIC int get_min_filtered_sample_size(void);
  421. STATIC int get_remove_unlisted_guards_after_days(void);
  422. STATIC int get_guard_lifetime(void);
  423. STATIC int get_guard_confirmed_min_lifetime(void);
  424. STATIC int get_n_primary_guards(void);
  425. STATIC int get_n_primary_guards_to_use(guard_usage_t usage);
  426. STATIC int get_internet_likely_down_interval(void);
  427. STATIC int get_nonprimary_guard_connect_timeout(void);
  428. STATIC int get_nonprimary_guard_idle_timeout(void);
  429. STATIC double get_meaningful_restriction_threshold(void);
  430. STATIC double get_extreme_restriction_threshold(void);
  431. HANDLE_DECL(entry_guard, entry_guard_t, STATIC)
  432. #define entry_guard_handle_free(h) \
  433. FREE_AND_NULL(entry_guard_handle_t, entry_guard_handle_free_, (h))
  434. STATIC guard_selection_type_t guard_selection_infer_type(
  435. guard_selection_type_t type_in,
  436. const char *name);
  437. STATIC guard_selection_t *guard_selection_new(const char *name,
  438. guard_selection_type_t type);
  439. STATIC guard_selection_t *get_guard_selection_by_name(
  440. const char *name, guard_selection_type_t type, int create_if_absent);
  441. STATIC void guard_selection_free_(guard_selection_t *gs);
  442. #define guard_selection_free(gs) \
  443. FREE_AND_NULL(guard_selection_t, guard_selection_free_, (gs))
  444. MOCK_DECL(STATIC int, entry_guard_is_listed,
  445. (guard_selection_t *gs, const entry_guard_t *guard));
  446. STATIC const char *choose_guard_selection(const or_options_t *options,
  447. const networkstatus_t *ns,
  448. const guard_selection_t *old_selection,
  449. guard_selection_type_t *type_out);
  450. STATIC entry_guard_t *get_sampled_guard_with_id(guard_selection_t *gs,
  451. const uint8_t *rsa_id);
  452. MOCK_DECL(STATIC time_t, randomize_time, (time_t now, time_t max_backdate));
  453. MOCK_DECL(STATIC circuit_guard_state_t *,
  454. circuit_guard_state_new,(entry_guard_t *guard, unsigned state,
  455. entry_guard_restriction_t *rst));
  456. STATIC entry_guard_t *entry_guard_add_to_sample(guard_selection_t *gs,
  457. const node_t *node);
  458. STATIC entry_guard_t *entry_guards_expand_sample(guard_selection_t *gs);
  459. STATIC char *entry_guard_encode_for_state(entry_guard_t *guard);
  460. STATIC entry_guard_t *entry_guard_parse_from_state(const char *s);
  461. #define entry_guard_free(e) \
  462. FREE_AND_NULL(entry_guard_t, entry_guard_free_, (e))
  463. STATIC void entry_guard_free_(entry_guard_t *e);
  464. STATIC void entry_guards_update_filtered_sets(guard_selection_t *gs);
  465. STATIC int entry_guards_all_primary_guards_are_down(guard_selection_t *gs);
  466. /**
  467. * @name Flags for sample_reachable_filtered_entry_guards()
  468. */
  469. /**@{*/
  470. #define SAMPLE_EXCLUDE_CONFIRMED (1u<<0)
  471. #define SAMPLE_EXCLUDE_PRIMARY (1u<<1)
  472. #define SAMPLE_EXCLUDE_PENDING (1u<<2)
  473. #define SAMPLE_NO_UPDATE_PRIMARY (1u<<3)
  474. #define SAMPLE_EXCLUDE_NO_DESCRIPTOR (1u<<4)
  475. /**@}*/
  476. STATIC entry_guard_t *sample_reachable_filtered_entry_guards(
  477. guard_selection_t *gs,
  478. const entry_guard_restriction_t *rst,
  479. unsigned flags);
  480. STATIC void entry_guard_consider_retry(entry_guard_t *guard);
  481. STATIC void make_guard_confirmed(guard_selection_t *gs, entry_guard_t *guard);
  482. STATIC void entry_guards_update_confirmed(guard_selection_t *gs);
  483. STATIC void entry_guards_update_primary(guard_selection_t *gs);
  484. STATIC int num_reachable_filtered_guards(const guard_selection_t *gs,
  485. const entry_guard_restriction_t *rst);
  486. STATIC void sampled_guards_update_from_consensus(guard_selection_t *gs);
  487. /**
  488. * @name Possible guard-states for a circuit.
  489. */
  490. /**@{*/
  491. /** State for a circuit that can (so far as the guard subsystem is
  492. * concerned) be used for actual traffic as soon as it is successfully
  493. * opened. */
  494. #define GUARD_CIRC_STATE_USABLE_ON_COMPLETION 1
  495. /** State for an non-open circuit that we shouldn't use for actual
  496. * traffic, when it completes, unless other circuits to preferable
  497. * guards fail. */
  498. #define GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD 2
  499. /** State for an open circuit that we shouldn't use for actual traffic
  500. * unless other circuits to preferable guards fail. */
  501. #define GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD 3
  502. /** State for a circuit that can (so far as the guard subsystem is
  503. * concerned) be used for actual traffic. */
  504. #define GUARD_CIRC_STATE_COMPLETE 4
  505. /** State for a circuit that is unusable, and will not become usable. */
  506. #define GUARD_CIRC_STATE_DEAD 5
  507. /**@}*/
  508. STATIC void entry_guards_note_guard_failure(guard_selection_t *gs,
  509. entry_guard_t *guard);
  510. STATIC entry_guard_t *select_entry_guard_for_circuit(guard_selection_t *gs,
  511. guard_usage_t usage,
  512. const entry_guard_restriction_t *rst,
  513. unsigned *state_out);
  514. STATIC void mark_primary_guards_maybe_reachable(guard_selection_t *gs);
  515. STATIC unsigned entry_guards_note_guard_success(guard_selection_t *gs,
  516. entry_guard_t *guard,
  517. unsigned old_state);
  518. STATIC int entry_guard_has_higher_priority(entry_guard_t *a, entry_guard_t *b);
  519. STATIC char *getinfo_helper_format_single_entry_guard(const entry_guard_t *e);
  520. STATIC entry_guard_restriction_t *guard_create_exit_restriction(
  521. const uint8_t *exit_id);
  522. STATIC entry_guard_restriction_t *guard_create_dirserver_md_restriction(void);
  523. STATIC void entry_guard_restriction_free_(entry_guard_restriction_t *rst);
  524. #define entry_guard_restriction_free(rst) \
  525. FREE_AND_NULL(entry_guard_restriction_t, \
  526. entry_guard_restriction_free_, (rst))
  527. #endif /* defined(ENTRYNODES_PRIVATE) */
  528. void remove_all_entry_guards_for_guard_selection(guard_selection_t *gs);
  529. void remove_all_entry_guards(void);
  530. struct bridge_info_t;
  531. void entry_guard_learned_bridge_identity(const tor_addr_port_t *addrport,
  532. const uint8_t *rsa_id_digest);
  533. int entry_list_is_constrained(const or_options_t *options);
  534. int guards_retry_optimistic(const or_options_t *options);
  535. int entry_guards_parse_state_for_guard_selection(
  536. guard_selection_t *gs, or_state_t *state, int set, char **msg);
  537. int entry_guards_parse_state(or_state_t *state, int set, char **msg);
  538. void entry_guards_update_state(or_state_t *state);
  539. int getinfo_helper_entry_guards(control_connection_t *conn,
  540. const char *question, char **answer,
  541. const char **errmsg);
  542. int entries_known_but_down(const or_options_t *options);
  543. void entries_retry_all(const or_options_t *options);
  544. char *entry_guards_get_err_str_if_dir_info_missing(int using_mds,
  545. int num_present, int num_usable);
  546. char *guard_selection_get_err_str_if_dir_info_missing(guard_selection_t *gs,
  547. int using_mds,
  548. int num_present, int num_usable);
  549. void entry_guards_free_all(void);
  550. double pathbias_get_close_success_count(entry_guard_t *guard);
  551. double pathbias_get_use_success_count(entry_guard_t *guard);
  552. /** Contains the bandwidth of a relay as a guard and as a non-guard
  553. * after the guardfraction has been considered. */
  554. typedef struct guardfraction_bandwidth_t {
  555. /** Bandwidth as a guard after guardfraction has been considered. */
  556. int guard_bw;
  557. /** Bandwidth as a non-guard after guardfraction has been considered. */
  558. int non_guard_bw;
  559. } guardfraction_bandwidth_t;
  560. int should_apply_guardfraction(const networkstatus_t *ns);
  561. void
  562. guard_get_guardfraction_bandwidth(guardfraction_bandwidth_t *guardfraction_bw,
  563. int orig_bandwidth,
  564. uint32_t guardfraction_percentage);
  565. #endif /* !defined(TOR_ENTRYNODES_H) */