routerlist.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. int get_n_authorities(dirinfo_type_t type);
  65. int trusted_dirs_reload_certs(void);
  66. /*
  67. * Pass one of these as source to trusted_dirs_load_certs_from_string()
  68. * to indicate whence string originates; this controls error handling
  69. * behavior such as marking downloads as failed.
  70. */
  71. #define TRUSTED_DIRS_CERTS_SRC_SELF 0
  72. #define TRUSTED_DIRS_CERTS_SRC_FROM_STORE 1
  73. #define TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST 2
  74. #define TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST 3
  75. #define TRUSTED_DIRS_CERTS_SRC_FROM_VOTE 4
  76. int trusted_dirs_load_certs_from_string(const char *contents, int source,
  77. int flush, const char *source_dir);
  78. void trusted_dirs_flush_certs_to_disk(void);
  79. authority_cert_t *authority_cert_get_newest_by_id(const char *id_digest);
  80. authority_cert_t *authority_cert_get_by_sk_digest(const char *sk_digest);
  81. authority_cert_t *authority_cert_get_by_digests(const char *id_digest,
  82. const char *sk_digest);
  83. void authority_cert_get_all(smartlist_t *certs_out);
  84. void authority_cert_dl_failed(const char *id_digest,
  85. const char *signing_key_digest, int status);
  86. void authority_certs_fetch_missing(networkstatus_t *status, time_t now,
  87. const char *dir_hint);
  88. int router_reload_router_list(void);
  89. int authority_cert_dl_looks_uncertain(const char *id_digest);
  90. const smartlist_t *router_get_trusted_dir_servers(void);
  91. const smartlist_t *router_get_fallback_dir_servers(void);
  92. int authority_cert_is_blacklisted(const authority_cert_t *cert);
  93. const routerstatus_t *router_pick_directory_server(dirinfo_type_t type,
  94. int flags);
  95. dir_server_t *router_get_trusteddirserver_by_digest(const char *d);
  96. dir_server_t *router_get_fallback_dirserver_by_digest(
  97. const char *digest);
  98. int router_digest_is_fallback_dir(const char *digest);
  99. MOCK_DECL(dir_server_t *, trusteddirserver_get_by_v3_auth_digest,
  100. (const char *d));
  101. const routerstatus_t *router_pick_trusteddirserver(dirinfo_type_t type,
  102. int flags);
  103. const routerstatus_t *router_pick_fallback_dirserver(dirinfo_type_t type,
  104. int flags);
  105. int router_skip_or_reachability(const or_options_t *options, int try_ip_pref);
  106. int router_get_my_share_of_directory_requests(double *v3_share_out);
  107. void router_reset_status_download_failures(void);
  108. int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2);
  109. void router_add_running_nodes_to_smartlist(smartlist_t *sl, int need_uptime,
  110. int need_capacity, int need_guard,
  111. int need_desc, int pref_addr,
  112. int direct_conn);
  113. const routerinfo_t *routerlist_find_my_routerinfo(void);
  114. uint32_t router_get_advertised_bandwidth(const routerinfo_t *router);
  115. uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router);
  116. const node_t *node_sl_choose_by_bandwidth(const smartlist_t *sl,
  117. bandwidth_weight_rule_t rule);
  118. double frac_nodes_with_descriptors(const smartlist_t *sl,
  119. bandwidth_weight_rule_t rule,
  120. int for_direct_conn);
  121. const node_t *router_choose_random_node(smartlist_t *excludedsmartlist,
  122. struct routerset_t *excludedset,
  123. router_crn_flags_t flags);
  124. int router_digest_is_trusted_dir_type(const char *digest,
  125. dirinfo_type_t type);
  126. #define router_digest_is_trusted_dir(d) \
  127. router_digest_is_trusted_dir_type((d), NO_DIRINFO)
  128. int hexdigest_to_digest(const char *hexdigest, char *digest);
  129. const routerinfo_t *router_get_by_id_digest(const char *digest);
  130. routerinfo_t *router_get_mutable_by_digest(const char *digest);
  131. signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
  132. MOCK_DECL(signed_descriptor_t *,router_get_by_extrainfo_digest,
  133. (const char *digest));
  134. MOCK_DECL(signed_descriptor_t *,extrainfo_get_by_descriptor_digest,
  135. (const char *digest));
  136. const char *signed_descriptor_get_body(const signed_descriptor_t *desc);
  137. const char *signed_descriptor_get_annotations(const signed_descriptor_t *desc);
  138. routerlist_t *router_get_routerlist(void);
  139. void routerinfo_free_(routerinfo_t *router);
  140. #define routerinfo_free(router) \
  141. FREE_AND_NULL(routerinfo_t, routerinfo_free_, (router))
  142. void extrainfo_free_(extrainfo_t *extrainfo);
  143. #define extrainfo_free(ei) FREE_AND_NULL(extrainfo_t, extrainfo_free_, (ei))
  144. void routerlist_free_(routerlist_t *rl);
  145. #define routerlist_free(rl) FREE_AND_NULL(routerlist_t, routerlist_free_, (rl))
  146. void dump_routerlist_mem_usage(int severity);
  147. void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old,
  148. time_t now);
  149. void routerlist_free_all(void);
  150. void routerlist_reset_warnings(void);
  151. MOCK_DECL(smartlist_t *, list_authority_ids_with_downloads, (void));
  152. MOCK_DECL(download_status_t *, id_only_download_status_for_authority_id,
  153. (const char *digest));
  154. MOCK_DECL(smartlist_t *, list_sk_digests_for_authority_id,
  155. (const char *digest));
  156. MOCK_DECL(download_status_t *, download_status_for_authority_id_and_sk,
  157. (const char *id_digest, const char *sk_digest));
  158. static int WRA_WAS_ADDED(was_router_added_t s);
  159. static int WRA_WAS_OUTDATED(was_router_added_t s);
  160. static int WRA_WAS_REJECTED(was_router_added_t s);
  161. static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s);
  162. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  163. * was added. It might still be necessary to check whether the descriptor
  164. * generator should be notified.
  165. */
  166. static inline int
  167. WRA_WAS_ADDED(was_router_added_t s) {
  168. return s == ROUTER_ADDED_SUCCESSFULLY;
  169. }
  170. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  171. * was not added because it was either:
  172. * - not in the consensus
  173. * - neither in the consensus nor in any networkstatus document
  174. * - it was outdated.
  175. * - its certificates were expired.
  176. */
  177. static inline int WRA_WAS_OUTDATED(was_router_added_t s)
  178. {
  179. return (s == ROUTER_WAS_TOO_OLD ||
  180. s == ROUTER_IS_ALREADY_KNOWN ||
  181. s == ROUTER_NOT_IN_CONSENSUS ||
  182. s == ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS ||
  183. s == ROUTER_CERTS_EXPIRED);
  184. }
  185. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  186. * was flat-out rejected. */
  187. static inline int WRA_WAS_REJECTED(was_router_added_t s)
  188. {
  189. return (s == ROUTER_AUTHDIR_REJECTS);
  190. }
  191. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  192. * was flat-out rejected. */
  193. static inline int WRA_NEVER_DOWNLOADABLE(was_router_added_t s)
  194. {
  195. return (s == ROUTER_AUTHDIR_REJECTS ||
  196. s == ROUTER_BAD_EI ||
  197. s == ROUTER_WAS_TOO_OLD ||
  198. s == ROUTER_CERTS_EXPIRED);
  199. }
  200. was_router_added_t router_add_to_routerlist(routerinfo_t *router,
  201. const char **msg,
  202. int from_cache,
  203. int from_fetch);
  204. was_router_added_t router_add_extrainfo_to_routerlist(
  205. extrainfo_t *ei, const char **msg,
  206. int from_cache, int from_fetch);
  207. void routerlist_descriptors_added(smartlist_t *sl, int from_cache);
  208. void routerlist_remove_old_routers(void);
  209. int router_load_single_router(const char *s, uint8_t purpose, int cache,
  210. const char **msg);
  211. int router_load_routers_from_string(const char *s, const char *eos,
  212. saved_location_t saved_location,
  213. smartlist_t *requested_fingerprints,
  214. int descriptor_digests,
  215. const char *prepend_annotations);
  216. void router_load_extrainfo_from_string(const char *s, const char *eos,
  217. saved_location_t saved_location,
  218. smartlist_t *requested_fingerprints,
  219. int descriptor_digests);
  220. void routerlist_retry_directory_downloads(time_t now);
  221. int router_exit_policy_rejects_all(const routerinfo_t *router);
  222. dir_server_t *trusted_dir_server_new(const char *nickname, const char *address,
  223. uint16_t dir_port, uint16_t or_port,
  224. const tor_addr_port_t *addrport_ipv6,
  225. const char *digest, const char *v3_auth_digest,
  226. dirinfo_type_t type, double weight);
  227. dir_server_t *fallback_dir_server_new(const tor_addr_t *addr,
  228. uint16_t dir_port, uint16_t or_port,
  229. const tor_addr_port_t *addrport_ipv6,
  230. const char *id_digest, double weight);
  231. void dir_server_add(dir_server_t *ent);
  232. void authority_cert_free_(authority_cert_t *cert);
  233. #define authority_cert_free(cert) \
  234. FREE_AND_NULL(authority_cert_t, authority_cert_free_, (cert))
  235. void clear_dir_servers(void);
  236. void update_consensus_router_descriptor_downloads(time_t now, int is_vote,
  237. networkstatus_t *consensus);
  238. void update_router_descriptor_downloads(time_t now);
  239. void update_all_descriptor_downloads(time_t now);
  240. void update_extrainfo_downloads(time_t now);
  241. void router_reset_descriptor_download_failures(void);
  242. int router_differences_are_cosmetic(const routerinfo_t *r1,
  243. const routerinfo_t *r2);
  244. int routerinfo_incompatible_with_extrainfo(const crypto_pk_t *ri,
  245. extrainfo_t *ei,
  246. signed_descriptor_t *sd,
  247. const char **msg);
  248. int routerinfo_has_curve25519_onion_key(const routerinfo_t *ri);
  249. int routerstatus_version_supports_extend2_cells(const routerstatus_t *rs,
  250. int allow_unknown_versions);
  251. void routerlist_assert_ok(const routerlist_t *rl);
  252. const char *esc_router_info(const routerinfo_t *router);
  253. void routers_sort_by_identity(smartlist_t *routers);
  254. void refresh_all_country_info(void);
  255. void list_pending_microdesc_downloads(digest256map_t *result);
  256. void launch_descriptor_downloads(int purpose,
  257. smartlist_t *downloadable,
  258. const routerstatus_t *source,
  259. time_t now);
  260. int hex_digest_nickname_decode(const char *hexdigest,
  261. char *digest_out,
  262. char *nickname_qualifier_out,
  263. char *nickname_out);
  264. int hex_digest_nickname_matches(const char *hexdigest,
  265. const char *identity_digest,
  266. const char *nickname);
  267. #ifdef ROUTERLIST_PRIVATE
  268. STATIC int choose_array_element_by_weight(const uint64_t *entries,
  269. int n_entries);
  270. STATIC void scale_array_elements_to_u64(uint64_t *entries_out,
  271. const double *entries_in,
  272. int n_entries,
  273. uint64_t *total_out);
  274. STATIC const routerstatus_t *router_pick_directory_server_impl(
  275. dirinfo_type_t auth, int flags,
  276. int *n_busy_out);
  277. MOCK_DECL(int, router_descriptor_is_older_than, (const routerinfo_t *router,
  278. int seconds));
  279. MOCK_DECL(STATIC was_router_added_t, extrainfo_insert,
  280. (routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible));
  281. MOCK_DECL(STATIC void, initiate_descriptor_downloads,
  282. (const routerstatus_t *source, int purpose, smartlist_t *digests,
  283. int lo, int hi, int pds_flags));
  284. STATIC int router_is_already_dir_fetching(const tor_addr_port_t *ap,
  285. int serverdesc, int microdesc);
  286. #endif /* defined(ROUTERLIST_PRIVATE) */
  287. #endif /* !defined(TOR_ROUTERLIST_H) */