routerlist.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file routerlist.h
  7. * \brief Header file for routerlist.c.
  8. **/
  9. #ifndef TOR_ROUTERLIST_H
  10. #define TOR_ROUTERLIST_H
  11. #include "lib/testsupport/testsupport.h"
  12. /** Return value for router_add_to_routerlist() and dirserv_add_descriptor() */
  13. typedef enum was_router_added_t {
  14. /* Router was added successfully. */
  15. ROUTER_ADDED_SUCCESSFULLY = 1,
  16. /* Extrainfo document was rejected because no corresponding router
  17. * descriptor was found OR router descriptor was rejected because
  18. * it was incompatible with its extrainfo document. */
  19. ROUTER_BAD_EI = -1,
  20. /* Router descriptor was rejected because it is already known. */
  21. ROUTER_IS_ALREADY_KNOWN = -2,
  22. /* General purpose router was rejected, because it was not listed
  23. * in consensus. */
  24. ROUTER_NOT_IN_CONSENSUS = -3,
  25. /* Router was neither in directory consensus nor in any of
  26. * networkstatus documents. Caching it to access later.
  27. * (Applies to fetched descriptors only.) */
  28. ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS = -4,
  29. /* Router was rejected by directory authority. */
  30. ROUTER_AUTHDIR_REJECTS = -5,
  31. /* Bridge descriptor was rejected because such bridge was not one
  32. * of the bridges we have listed in our configuration. */
  33. ROUTER_WAS_NOT_WANTED = -6,
  34. /* Router descriptor was rejected because it was older than
  35. * OLD_ROUTER_DESC_MAX_AGE. */
  36. ROUTER_WAS_TOO_OLD = -7, /* note contrast with 'NOT_NEW' */
  37. /* DOCDOC */
  38. ROUTER_CERTS_EXPIRED = -8
  39. } was_router_added_t;
  40. /** Flags to be passed to control router_choose_random_node() to indicate what
  41. * kind of nodes to pick according to what algorithm. */
  42. typedef enum router_crn_flags_t {
  43. CRN_NEED_UPTIME = 1<<0,
  44. CRN_NEED_CAPACITY = 1<<1,
  45. CRN_NEED_GUARD = 1<<2,
  46. /* XXXX not used, apparently. */
  47. CRN_WEIGHT_AS_EXIT = 1<<5,
  48. CRN_NEED_DESC = 1<<6,
  49. /* On clients, only provide nodes that satisfy ClientPreferIPv6OR */
  50. CRN_PREF_ADDR = 1<<7,
  51. /* On clients, only provide nodes that we can connect to directly, based on
  52. * our firewall rules */
  53. CRN_DIRECT_CONN = 1<<8,
  54. /* On clients, only provide nodes with HSRend >= 2 protocol version which
  55. * is required for hidden service version >= 3. */
  56. CRN_RENDEZVOUS_V3 = 1<<9,
  57. } router_crn_flags_t;
  58. /** Possible ways to weight routers when choosing one randomly. See
  59. * routerlist_sl_choose_by_bandwidth() for more information.*/
  60. typedef enum bandwidth_weight_rule_t {
  61. NO_WEIGHTING, WEIGHT_FOR_EXIT, WEIGHT_FOR_MID, WEIGHT_FOR_GUARD,
  62. WEIGHT_FOR_DIR
  63. } bandwidth_weight_rule_t;
  64. /* Flags for pick_directory_server() and pick_trusteddirserver(). */
  65. /** Flag to indicate that we should not automatically be willing to use
  66. * ourself to answer a directory request.
  67. * Passed to router_pick_directory_server (et al).*/
  68. #define PDS_ALLOW_SELF (1<<0)
  69. /** Flag to indicate that if no servers seem to be up, we should mark all
  70. * directory servers as up and try again.
  71. * Passed to router_pick_directory_server (et al).*/
  72. #define PDS_RETRY_IF_NO_SERVERS (1<<1)
  73. /** Flag to indicate that we should not exclude directory servers that
  74. * our ReachableAddress settings would exclude. This usually means that
  75. * we're going to connect to the server over Tor, and so we don't need to
  76. * worry about our firewall telling us we can't.
  77. * Passed to router_pick_directory_server (et al).*/
  78. #define PDS_IGNORE_FASCISTFIREWALL (1<<2)
  79. /** Flag to indicate that we should not use any directory authority to which
  80. * we have an existing directory connection for downloading server descriptors
  81. * or extrainfo documents.
  82. *
  83. * Passed to router_pick_directory_server (et al)
  84. */
  85. #define PDS_NO_EXISTING_SERVERDESC_FETCH (1<<3)
  86. /** Flag to indicate that we should not use any directory authority to which
  87. * we have an existing directory connection for downloading microdescs.
  88. *
  89. * Passed to router_pick_directory_server (et al)
  90. */
  91. #define PDS_NO_EXISTING_MICRODESC_FETCH (1<<4)
  92. int get_n_authorities(dirinfo_type_t type);
  93. int trusted_dirs_reload_certs(void);
  94. /*
  95. * Pass one of these as source to trusted_dirs_load_certs_from_string()
  96. * to indicate whence string originates; this controls error handling
  97. * behavior such as marking downloads as failed.
  98. */
  99. #define TRUSTED_DIRS_CERTS_SRC_SELF 0
  100. #define TRUSTED_DIRS_CERTS_SRC_FROM_STORE 1
  101. #define TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST 2
  102. #define TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST 3
  103. #define TRUSTED_DIRS_CERTS_SRC_FROM_VOTE 4
  104. int trusted_dirs_load_certs_from_string(const char *contents, int source,
  105. int flush, const char *source_dir);
  106. void trusted_dirs_flush_certs_to_disk(void);
  107. authority_cert_t *authority_cert_get_newest_by_id(const char *id_digest);
  108. authority_cert_t *authority_cert_get_by_sk_digest(const char *sk_digest);
  109. authority_cert_t *authority_cert_get_by_digests(const char *id_digest,
  110. const char *sk_digest);
  111. void authority_cert_get_all(smartlist_t *certs_out);
  112. void authority_cert_dl_failed(const char *id_digest,
  113. const char *signing_key_digest, int status);
  114. void authority_certs_fetch_missing(networkstatus_t *status, time_t now,
  115. const char *dir_hint);
  116. int router_reload_router_list(void);
  117. int authority_cert_dl_looks_uncertain(const char *id_digest);
  118. const smartlist_t *router_get_trusted_dir_servers(void);
  119. const smartlist_t *router_get_fallback_dir_servers(void);
  120. int authority_cert_is_blacklisted(const authority_cert_t *cert);
  121. const routerstatus_t *router_pick_directory_server(dirinfo_type_t type,
  122. int flags);
  123. dir_server_t *router_get_trusteddirserver_by_digest(const char *d);
  124. dir_server_t *router_get_fallback_dirserver_by_digest(
  125. const char *digest);
  126. int router_digest_is_fallback_dir(const char *digest);
  127. MOCK_DECL(dir_server_t *, trusteddirserver_get_by_v3_auth_digest,
  128. (const char *d));
  129. const routerstatus_t *router_pick_trusteddirserver(dirinfo_type_t type,
  130. int flags);
  131. const routerstatus_t *router_pick_fallback_dirserver(dirinfo_type_t type,
  132. int flags);
  133. int router_skip_or_reachability(const or_options_t *options, int try_ip_pref);
  134. int router_get_my_share_of_directory_requests(double *v3_share_out);
  135. void router_reset_status_download_failures(void);
  136. int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2);
  137. void router_add_running_nodes_to_smartlist(smartlist_t *sl, int need_uptime,
  138. int need_capacity, int need_guard,
  139. int need_desc, int pref_addr,
  140. int direct_conn);
  141. const routerinfo_t *routerlist_find_my_routerinfo(void);
  142. uint32_t router_get_advertised_bandwidth(const routerinfo_t *router);
  143. uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router);
  144. const node_t *node_sl_choose_by_bandwidth(const smartlist_t *sl,
  145. bandwidth_weight_rule_t rule);
  146. double frac_nodes_with_descriptors(const smartlist_t *sl,
  147. bandwidth_weight_rule_t rule,
  148. int for_direct_conn);
  149. const node_t *router_choose_random_node(smartlist_t *excludedsmartlist,
  150. struct routerset_t *excludedset,
  151. router_crn_flags_t flags);
  152. int router_digest_is_trusted_dir_type(const char *digest,
  153. dirinfo_type_t type);
  154. #define router_digest_is_trusted_dir(d) \
  155. router_digest_is_trusted_dir_type((d), NO_DIRINFO)
  156. int hexdigest_to_digest(const char *hexdigest, char *digest);
  157. const routerinfo_t *router_get_by_id_digest(const char *digest);
  158. routerinfo_t *router_get_mutable_by_digest(const char *digest);
  159. signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
  160. MOCK_DECL(signed_descriptor_t *,router_get_by_extrainfo_digest,
  161. (const char *digest));
  162. MOCK_DECL(signed_descriptor_t *,extrainfo_get_by_descriptor_digest,
  163. (const char *digest));
  164. const char *signed_descriptor_get_body(const signed_descriptor_t *desc);
  165. const char *signed_descriptor_get_annotations(const signed_descriptor_t *desc);
  166. routerlist_t *router_get_routerlist(void);
  167. void routerinfo_free_(routerinfo_t *router);
  168. #define routerinfo_free(router) \
  169. FREE_AND_NULL(routerinfo_t, routerinfo_free_, (router))
  170. void extrainfo_free_(extrainfo_t *extrainfo);
  171. #define extrainfo_free(ei) FREE_AND_NULL(extrainfo_t, extrainfo_free_, (ei))
  172. void routerlist_free_(routerlist_t *rl);
  173. #define routerlist_free(rl) FREE_AND_NULL(routerlist_t, routerlist_free_, (rl))
  174. void dump_routerlist_mem_usage(int severity);
  175. void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old,
  176. time_t now);
  177. void routerlist_free_all(void);
  178. void routerlist_reset_warnings(void);
  179. MOCK_DECL(smartlist_t *, list_authority_ids_with_downloads, (void));
  180. MOCK_DECL(download_status_t *, id_only_download_status_for_authority_id,
  181. (const char *digest));
  182. MOCK_DECL(smartlist_t *, list_sk_digests_for_authority_id,
  183. (const char *digest));
  184. MOCK_DECL(download_status_t *, download_status_for_authority_id_and_sk,
  185. (const char *id_digest, const char *sk_digest));
  186. static int WRA_WAS_ADDED(was_router_added_t s);
  187. static int WRA_WAS_OUTDATED(was_router_added_t s);
  188. static int WRA_WAS_REJECTED(was_router_added_t s);
  189. static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s);
  190. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  191. * was added. It might still be necessary to check whether the descriptor
  192. * generator should be notified.
  193. */
  194. static inline int
  195. WRA_WAS_ADDED(was_router_added_t s) {
  196. return s == ROUTER_ADDED_SUCCESSFULLY;
  197. }
  198. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  199. * was not added because it was either:
  200. * - not in the consensus
  201. * - neither in the consensus nor in any networkstatus document
  202. * - it was outdated.
  203. * - its certificates were expired.
  204. */
  205. static inline int WRA_WAS_OUTDATED(was_router_added_t s)
  206. {
  207. return (s == ROUTER_WAS_TOO_OLD ||
  208. s == ROUTER_IS_ALREADY_KNOWN ||
  209. s == ROUTER_NOT_IN_CONSENSUS ||
  210. s == ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS ||
  211. s == ROUTER_CERTS_EXPIRED);
  212. }
  213. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  214. * was flat-out rejected. */
  215. static inline int WRA_WAS_REJECTED(was_router_added_t s)
  216. {
  217. return (s == ROUTER_AUTHDIR_REJECTS);
  218. }
  219. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  220. * was flat-out rejected. */
  221. static inline int WRA_NEVER_DOWNLOADABLE(was_router_added_t s)
  222. {
  223. return (s == ROUTER_AUTHDIR_REJECTS ||
  224. s == ROUTER_BAD_EI ||
  225. s == ROUTER_WAS_TOO_OLD ||
  226. s == ROUTER_CERTS_EXPIRED);
  227. }
  228. was_router_added_t router_add_to_routerlist(routerinfo_t *router,
  229. const char **msg,
  230. int from_cache,
  231. int from_fetch);
  232. was_router_added_t router_add_extrainfo_to_routerlist(
  233. extrainfo_t *ei, const char **msg,
  234. int from_cache, int from_fetch);
  235. void routerlist_descriptors_added(smartlist_t *sl, int from_cache);
  236. void routerlist_remove_old_routers(void);
  237. int router_load_single_router(const char *s, uint8_t purpose, int cache,
  238. const char **msg);
  239. int router_load_routers_from_string(const char *s, const char *eos,
  240. saved_location_t saved_location,
  241. smartlist_t *requested_fingerprints,
  242. int descriptor_digests,
  243. const char *prepend_annotations);
  244. void router_load_extrainfo_from_string(const char *s, const char *eos,
  245. saved_location_t saved_location,
  246. smartlist_t *requested_fingerprints,
  247. int descriptor_digests);
  248. void routerlist_retry_directory_downloads(time_t now);
  249. int router_exit_policy_rejects_all(const routerinfo_t *router);
  250. dir_server_t *trusted_dir_server_new(const char *nickname, const char *address,
  251. uint16_t dir_port, uint16_t or_port,
  252. const tor_addr_port_t *addrport_ipv6,
  253. const char *digest, const char *v3_auth_digest,
  254. dirinfo_type_t type, double weight);
  255. dir_server_t *fallback_dir_server_new(const tor_addr_t *addr,
  256. uint16_t dir_port, uint16_t or_port,
  257. const tor_addr_port_t *addrport_ipv6,
  258. const char *id_digest, double weight);
  259. void dir_server_add(dir_server_t *ent);
  260. void authority_cert_free_(authority_cert_t *cert);
  261. #define authority_cert_free(cert) \
  262. FREE_AND_NULL(authority_cert_t, authority_cert_free_, (cert))
  263. void clear_dir_servers(void);
  264. void update_consensus_router_descriptor_downloads(time_t now, int is_vote,
  265. networkstatus_t *consensus);
  266. void update_router_descriptor_downloads(time_t now);
  267. void update_all_descriptor_downloads(time_t now);
  268. void update_extrainfo_downloads(time_t now);
  269. void router_reset_descriptor_download_failures(void);
  270. int router_differences_are_cosmetic(const routerinfo_t *r1,
  271. const routerinfo_t *r2);
  272. int routerinfo_incompatible_with_extrainfo(const crypto_pk_t *ri,
  273. extrainfo_t *ei,
  274. signed_descriptor_t *sd,
  275. const char **msg);
  276. int routerinfo_has_curve25519_onion_key(const routerinfo_t *ri);
  277. int routerstatus_version_supports_extend2_cells(const routerstatus_t *rs,
  278. int allow_unknown_versions);
  279. void routerlist_assert_ok(const routerlist_t *rl);
  280. const char *esc_router_info(const routerinfo_t *router);
  281. void routers_sort_by_identity(smartlist_t *routers);
  282. void refresh_all_country_info(void);
  283. void list_pending_microdesc_downloads(digest256map_t *result);
  284. void launch_descriptor_downloads(int purpose,
  285. smartlist_t *downloadable,
  286. const routerstatus_t *source,
  287. time_t now);
  288. int hex_digest_nickname_decode(const char *hexdigest,
  289. char *digest_out,
  290. char *nickname_qualifier_out,
  291. char *nickname_out);
  292. int hex_digest_nickname_matches(const char *hexdigest,
  293. const char *identity_digest,
  294. const char *nickname);
  295. #ifdef ROUTERLIST_PRIVATE
  296. STATIC int choose_array_element_by_weight(const uint64_t *entries,
  297. int n_entries);
  298. STATIC void scale_array_elements_to_u64(uint64_t *entries_out,
  299. const double *entries_in,
  300. int n_entries,
  301. uint64_t *total_out);
  302. STATIC const routerstatus_t *router_pick_directory_server_impl(
  303. dirinfo_type_t auth, int flags,
  304. int *n_busy_out);
  305. MOCK_DECL(int, router_descriptor_is_older_than, (const routerinfo_t *router,
  306. int seconds));
  307. MOCK_DECL(STATIC was_router_added_t, extrainfo_insert,
  308. (routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible));
  309. MOCK_DECL(STATIC void, initiate_descriptor_downloads,
  310. (const routerstatus_t *source, int purpose, smartlist_t *digests,
  311. int lo, int hi, int pds_flags));
  312. STATIC int router_is_already_dir_fetching(const tor_addr_port_t *ap,
  313. int serverdesc, int microdesc);
  314. #endif /* defined(ROUTERLIST_PRIVATE) */
  315. #endif /* !defined(TOR_ROUTERLIST_H) */