entrynodes.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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-2016, 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. /* Forward declare for guard_selection_t; entrynodes.c has the real struct */
  13. typedef struct guard_selection_s guard_selection_t;
  14. /* Forward declare for entry_guard_t; the real declaration is private. */
  15. typedef struct entry_guard_t entry_guard_t;
  16. #define GUARD_REACHABLE_NO 0
  17. #define GUARD_REACHABLE_YES 1
  18. #define GUARD_REACHABLE_MAYBE 2
  19. /* Information about a guard's pathbias status.
  20. * These fields are used in circpathbias.c to try to detect entry
  21. * nodes that are failing circuits at a suspicious frequency.
  22. */
  23. typedef struct guard_pathbias_t {
  24. unsigned int path_bias_noticed : 1; /**< Did we alert the user about path
  25. * bias for this node already? */
  26. unsigned int path_bias_warned : 1; /**< Did we alert the user about path bias
  27. * for this node already? */
  28. unsigned int path_bias_extreme : 1; /**< Did we alert the user about path
  29. * bias for this node already? */
  30. unsigned int path_bias_disabled : 1; /**< Have we disabled this node because
  31. * of path bias issues? */
  32. unsigned int path_bias_use_noticed : 1; /**< Did we alert the user about path
  33. * use bias for this node already? */
  34. unsigned int path_bias_use_extreme : 1; /**< Did we alert the user about path
  35. * use bias for this node already? */
  36. double circ_attempts; /**< Number of circuits this guard has "attempted" */
  37. double circ_successes; /**< Number of successfully built circuits using
  38. * this guard as first hop. */
  39. double successful_circuits_closed; /**< Number of circuits that carried
  40. * streams successfully. */
  41. double collapsed_circuits; /**< Number of fully built circuits that were
  42. * remotely closed before any streams were
  43. * attempted. */
  44. double unusable_circuits; /**< Number of circuits for which streams were
  45. * attempted, but none succeeded. */
  46. double timeouts; /**< Number of 'right-censored' circuit timeouts for this
  47. * guard. */
  48. double use_attempts; /**< Number of circuits we tried to use with streams */
  49. double use_successes; /**< Number of successfully used circuits using
  50. * this guard as first hop. */
  51. } guard_pathbias_t;
  52. #if defined(ENTRYNODES_PRIVATE)
  53. /** An entry_guard_t represents our information about a chosen long-term
  54. * first hop, known as a "helper" node in the literature. We can't just
  55. * use a node_t, since we want to remember these even when we
  56. * don't have any directory info. */
  57. struct entry_guard_t {
  58. char nickname[MAX_HEX_NICKNAME_LEN+1];
  59. char identity[DIGEST_LEN];
  60. ed25519_public_key_t ed_id;
  61. /* XXXX prop271 DOCDOC document all these fields better */
  62. /* Persistent fields, present for all sampled guards. */
  63. time_t sampled_on_date;
  64. time_t unlisted_since_date; // can be zero
  65. char *sampled_by_version;
  66. unsigned currently_listed : 1;
  67. /* Persistent fields, for confirmed guards. */
  68. time_t confirmed_on_date; /* 0 if not confirmed */
  69. int confirmed_idx; /* -1 if not confirmed; otherwise the order that this
  70. * item should occur in the CONFIRMED_GUARDS ordered
  71. * list */
  72. /* ==== Non-persistent fields. */
  73. /* == These are used by sampled guards */
  74. time_t last_tried_to_connect;
  75. unsigned is_reachable : 2; /* One of GUARD_REACHABLE_{NO,YES,MAYBE} */
  76. unsigned is_pending : 1;
  77. time_t failing_since;
  78. /* These determine presence in filtered guards and usable-filtered-guards
  79. * respectively. */
  80. unsigned is_filtered_guard : 1;
  81. unsigned is_usable_filtered_guard : 1;
  82. /** This string holds any fields that we are maintaining because
  83. * we saw them in the state, even if we don't understand them. */
  84. char *extra_state_fields;
  85. /**
  86. * @name legacy guard selection algorithm fields
  87. *
  88. * These are used and maintained by the legacy (pre-prop271) entry guard
  89. * algorithm. Most of them we will remove as prop271 gets implemented.
  90. * The rest we'll migrate over, if they are 100% semantically identical to
  91. * their prop271 equivalents. XXXXprop271
  92. */
  93. /**@{*/
  94. time_t chosen_on_date; /**< Approximately when was this guard added?
  95. * "0" if we don't know. */
  96. char *chosen_by_version; /**< What tor version added this guard? NULL
  97. * if we don't know. */
  98. unsigned int made_contact : 1; /**< 0 if we have never connected to this
  99. * router, 1 if we have. */
  100. unsigned int can_retry : 1; /**< Should we retry connecting to this entry,
  101. * in spite of having it marked as unreachable?*/
  102. unsigned int is_dir_cache : 1; /**< Is this node a directory cache? */
  103. time_t bad_since; /**< 0 if this guard is currently usable, or the time at
  104. * which it was observed to become (according to the
  105. * directory or the user configuration) unusable. */
  106. time_t unreachable_since; /**< 0 if we can connect to this guard, or the
  107. * time at which we first noticed we couldn't
  108. * connect to it. */
  109. time_t last_attempted; /**< 0 if we can connect to this guard, or the time
  110. * at which we last failed to connect to it. */
  111. /**}@*/
  112. /** Path bias information for this guard. */
  113. guard_pathbias_t pb;
  114. };
  115. #endif
  116. #if 1
  117. /* XXXX NM I would prefer that all of this stuff be private to
  118. * entrynodes.c. */
  119. entry_guard_t *entry_guard_get_by_id_digest_for_guard_selection(
  120. guard_selection_t *gs, const char *digest);
  121. entry_guard_t *entry_guard_get_by_id_digest(const char *digest);
  122. void entry_guards_changed_for_guard_selection(guard_selection_t *gs);
  123. void entry_guards_changed(void);
  124. guard_selection_t * get_guard_selection_info(void);
  125. const smartlist_t *get_entry_guards_for_guard_selection(
  126. guard_selection_t *gs);
  127. const smartlist_t *get_entry_guards(void);
  128. int num_live_entry_guards_for_guard_selection(
  129. guard_selection_t *gs,
  130. int for_directory);
  131. int num_live_entry_guards(int for_directory);
  132. #endif
  133. const node_t *entry_guard_find_node(const entry_guard_t *guard);
  134. void entry_guard_mark_bad(entry_guard_t *guard);
  135. const char *entry_guard_get_rsa_id_digest(const entry_guard_t *guard);
  136. const char *entry_guard_describe(const entry_guard_t *guard);
  137. guard_pathbias_t *entry_guard_get_pathbias_state(entry_guard_t *guard);
  138. #ifdef ENTRYNODES_PRIVATE
  139. STATIC time_t randomize_time(time_t now, time_t max_backdate);
  140. STATIC void entry_guard_add_to_sample(guard_selection_t *gs,
  141. node_t *node);
  142. STATIC char *entry_guard_encode_for_state(entry_guard_t *guard);
  143. STATIC entry_guard_t *entry_guard_parse_from_state(const char *s);
  144. STATIC void entry_guard_free(entry_guard_t *e);
  145. STATIC const node_t *add_an_entry_guard(guard_selection_t *gs,
  146. const node_t *chosen,
  147. int reset_status, int prepend,
  148. int for_discovery, int for_directory);
  149. STATIC int populate_live_entry_guards(smartlist_t *live_entry_guards,
  150. const smartlist_t *all_entry_guards,
  151. const node_t *chosen_exit,
  152. dirinfo_type_t dirinfo_type,
  153. int for_directory,
  154. int need_uptime, int need_capacity);
  155. STATIC int decide_num_guards(const or_options_t *options, int for_directory);
  156. STATIC void entry_guards_set_from_config(guard_selection_t *gs,
  157. const or_options_t *options);
  158. /** Flags to be passed to entry_is_live() to indicate what kind of
  159. * entry nodes we are looking for. */
  160. typedef enum {
  161. ENTRY_NEED_UPTIME = 1<<0,
  162. ENTRY_NEED_CAPACITY = 1<<1,
  163. ENTRY_ASSUME_REACHABLE = 1<<2,
  164. ENTRY_NEED_DESCRIPTOR = 1<<3,
  165. } entry_is_live_flags_t;
  166. STATIC const node_t *entry_is_live(const entry_guard_t *e,
  167. entry_is_live_flags_t flags,
  168. const char **msg);
  169. STATIC int entry_is_time_to_retry(const entry_guard_t *e, time_t now);
  170. #endif
  171. void remove_all_entry_guards_for_guard_selection(guard_selection_t *gs);
  172. void remove_all_entry_guards(void);
  173. void entry_guards_compute_status_for_guard_selection(
  174. guard_selection_t *gs, const or_options_t *options, time_t now);
  175. void entry_guards_compute_status(const or_options_t *options, time_t now);
  176. int entry_guard_register_connect_status_for_guard_selection(
  177. guard_selection_t *gs, const char *digest, int succeeded,
  178. int mark_relay_status, time_t now);
  179. int entry_guard_register_connect_status(const char *digest, int succeeded,
  180. int mark_relay_status, time_t now);
  181. void entry_nodes_should_be_added_for_guard_selection(guard_selection_t *gs);
  182. void entry_nodes_should_be_added(void);
  183. int entry_list_is_constrained(const or_options_t *options);
  184. const node_t *choose_random_entry(cpath_build_state_t *state);
  185. const node_t *choose_random_dirguard(dirinfo_type_t t);
  186. int entry_guards_parse_state_for_guard_selection(
  187. guard_selection_t *gs, or_state_t *state, int set, char **msg);
  188. int entry_guards_parse_state(or_state_t *state, int set, char **msg);
  189. void entry_guards_update_state(or_state_t *state);
  190. int getinfo_helper_entry_guards(control_connection_t *conn,
  191. const char *question, char **answer,
  192. const char **errmsg);
  193. int is_node_used_as_guard_for_guard_selection(guard_selection_t *gs,
  194. const node_t *node);
  195. MOCK_DECL(int, is_node_used_as_guard, (const node_t *node));
  196. int entries_known_but_down(const or_options_t *options);
  197. void entries_retry_all(const or_options_t *options);
  198. void entry_guards_free_all(void);
  199. double pathbias_get_close_success_count(entry_guard_t *guard);
  200. double pathbias_get_use_success_count(entry_guard_t *guard);
  201. /** Contains the bandwidth of a relay as a guard and as a non-guard
  202. * after the guardfraction has been considered. */
  203. typedef struct guardfraction_bandwidth_t {
  204. /** Bandwidth as a guard after guardfraction has been considered. */
  205. int guard_bw;
  206. /** Bandwidth as a non-guard after guardfraction has been considered. */
  207. int non_guard_bw;
  208. } guardfraction_bandwidth_t;
  209. int should_apply_guardfraction(const networkstatus_t *ns);
  210. void
  211. guard_get_guardfraction_bandwidth(guardfraction_bandwidth_t *guardfraction_bw,
  212. int orig_bandwidth,
  213. uint32_t guardfraction_percentage);
  214. #endif