routerlist.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, 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 "testsupport.h"
  12. int get_n_authorities(dirinfo_type_t type);
  13. int trusted_dirs_reload_certs(void);
  14. /*
  15. * Pass one of these as source to trusted_dirs_load_certs_from_string()
  16. * to indicate whence string originates; this controls error handling
  17. * behavior such as marking downloads as failed.
  18. */
  19. #define TRUSTED_DIRS_CERTS_SRC_SELF 0
  20. #define TRUSTED_DIRS_CERTS_SRC_FROM_STORE 1
  21. #define TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST 2
  22. #define TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST 3
  23. #define TRUSTED_DIRS_CERTS_SRC_FROM_VOTE 4
  24. int trusted_dirs_load_certs_from_string(const char *contents, int source,
  25. int flush, const char *source_dir);
  26. void trusted_dirs_flush_certs_to_disk(void);
  27. authority_cert_t *authority_cert_get_newest_by_id(const char *id_digest);
  28. authority_cert_t *authority_cert_get_by_sk_digest(const char *sk_digest);
  29. authority_cert_t *authority_cert_get_by_digests(const char *id_digest,
  30. const char *sk_digest);
  31. void authority_cert_get_all(smartlist_t *certs_out);
  32. void authority_cert_dl_failed(const char *id_digest,
  33. const char *signing_key_digest, int status);
  34. void authority_certs_fetch_missing(networkstatus_t *status, time_t now,
  35. const char *dir_hint);
  36. int router_reload_router_list(void);
  37. int authority_cert_dl_looks_uncertain(const char *id_digest);
  38. const smartlist_t *router_get_trusted_dir_servers(void);
  39. const smartlist_t *router_get_fallback_dir_servers(void);
  40. int authority_cert_is_blacklisted(const authority_cert_t *cert);
  41. const routerstatus_t *router_pick_directory_server(dirinfo_type_t type,
  42. int flags);
  43. dir_server_t *router_get_trusteddirserver_by_digest(const char *d);
  44. dir_server_t *router_get_fallback_dirserver_by_digest(
  45. const char *digest);
  46. int router_digest_is_fallback_dir(const char *digest);
  47. dir_server_t *trusteddirserver_get_by_v3_auth_digest(const char *d);
  48. const routerstatus_t *router_pick_trusteddirserver(dirinfo_type_t type,
  49. int flags);
  50. const routerstatus_t *router_pick_fallback_dirserver(dirinfo_type_t type,
  51. int flags);
  52. int router_get_my_share_of_directory_requests(double *v3_share_out);
  53. void router_reset_status_download_failures(void);
  54. int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2);
  55. void router_add_running_nodes_to_smartlist(smartlist_t *sl, int allow_invalid,
  56. int need_uptime, int need_capacity,
  57. int need_guard, int need_desc,
  58. int pref_addr);
  59. const routerinfo_t *routerlist_find_my_routerinfo(void);
  60. uint32_t router_get_advertised_bandwidth(const routerinfo_t *router);
  61. uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router);
  62. const node_t *node_sl_choose_by_bandwidth(const smartlist_t *sl,
  63. bandwidth_weight_rule_t rule);
  64. double frac_nodes_with_descriptors(const smartlist_t *sl,
  65. bandwidth_weight_rule_t rule);
  66. const node_t *router_choose_random_node(smartlist_t *excludedsmartlist,
  67. struct routerset_t *excludedset,
  68. router_crn_flags_t flags);
  69. int router_is_named(const routerinfo_t *router);
  70. int router_digest_is_trusted_dir_type(const char *digest,
  71. dirinfo_type_t type);
  72. #define router_digest_is_trusted_dir(d) \
  73. router_digest_is_trusted_dir_type((d), NO_DIRINFO)
  74. int router_addr_is_trusted_dir(uint32_t addr);
  75. int hexdigest_to_digest(const char *hexdigest, char *digest);
  76. const routerinfo_t *router_get_by_id_digest(const char *digest);
  77. routerinfo_t *router_get_mutable_by_digest(const char *digest);
  78. signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
  79. MOCK_DECL(signed_descriptor_t *,router_get_by_extrainfo_digest,
  80. (const char *digest));
  81. signed_descriptor_t *extrainfo_get_by_descriptor_digest(const char *digest);
  82. const char *signed_descriptor_get_body(const signed_descriptor_t *desc);
  83. const char *signed_descriptor_get_annotations(const signed_descriptor_t *desc);
  84. routerlist_t *router_get_routerlist(void);
  85. void routerinfo_free(routerinfo_t *router);
  86. void extrainfo_free(extrainfo_t *extrainfo);
  87. void routerlist_free(routerlist_t *rl);
  88. void dump_routerlist_mem_usage(int severity);
  89. void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old,
  90. time_t now);
  91. void routerlist_free_all(void);
  92. void routerlist_reset_warnings(void);
  93. MOCK_DECL(smartlist_t *, list_authority_ids_with_downloads, (void);)
  94. MOCK_DECL(download_status_t *, id_only_download_status_for_authority_id,
  95. (const char *digest));
  96. MOCK_DECL(smartlist_t *, list_sk_digests_for_authority_id,
  97. (const char *digest));
  98. MOCK_DECL(download_status_t *, download_status_for_authority_id_and_sk,
  99. (const char *id_digest, const char *sk_digest));
  100. static int WRA_WAS_ADDED(was_router_added_t s);
  101. static int WRA_WAS_OUTDATED(was_router_added_t s);
  102. static int WRA_WAS_REJECTED(was_router_added_t s);
  103. static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s);
  104. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  105. * was added. It might still be necessary to check whether the descriptor
  106. * generator should be notified.
  107. */
  108. static inline int
  109. WRA_WAS_ADDED(was_router_added_t s) {
  110. return s == ROUTER_ADDED_SUCCESSFULLY || s == ROUTER_ADDED_NOTIFY_GENERATOR;
  111. }
  112. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  113. * was not added because it was either:
  114. * - not in the consensus
  115. * - neither in the consensus nor in any networkstatus document
  116. * - it was outdated.
  117. * - its certificates were expired.
  118. */
  119. static inline int WRA_WAS_OUTDATED(was_router_added_t s)
  120. {
  121. return (s == ROUTER_WAS_TOO_OLD ||
  122. s == ROUTER_IS_ALREADY_KNOWN ||
  123. s == ROUTER_NOT_IN_CONSENSUS ||
  124. s == ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS ||
  125. s == ROUTER_CERTS_EXPIRED);
  126. }
  127. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  128. * was flat-out rejected. */
  129. static inline int WRA_WAS_REJECTED(was_router_added_t s)
  130. {
  131. return (s == ROUTER_AUTHDIR_REJECTS);
  132. }
  133. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  134. * was flat-out rejected. */
  135. static inline int WRA_NEVER_DOWNLOADABLE(was_router_added_t s)
  136. {
  137. return (s == ROUTER_AUTHDIR_REJECTS ||
  138. s == ROUTER_BAD_EI ||
  139. s == ROUTER_WAS_TOO_OLD ||
  140. s == ROUTER_CERTS_EXPIRED);
  141. }
  142. was_router_added_t router_add_to_routerlist(routerinfo_t *router,
  143. const char **msg,
  144. int from_cache,
  145. int from_fetch);
  146. was_router_added_t router_add_extrainfo_to_routerlist(
  147. extrainfo_t *ei, const char **msg,
  148. int from_cache, int from_fetch);
  149. void routerlist_descriptors_added(smartlist_t *sl, int from_cache);
  150. void routerlist_remove_old_routers(void);
  151. int router_load_single_router(const char *s, uint8_t purpose, int cache,
  152. const char **msg);
  153. int router_load_routers_from_string(const char *s, const char *eos,
  154. saved_location_t saved_location,
  155. smartlist_t *requested_fingerprints,
  156. int descriptor_digests,
  157. const char *prepend_annotations);
  158. void router_load_extrainfo_from_string(const char *s, const char *eos,
  159. saved_location_t saved_location,
  160. smartlist_t *requested_fingerprints,
  161. int descriptor_digests);
  162. void routerlist_retry_directory_downloads(time_t now);
  163. int router_exit_policy_rejects_all(const routerinfo_t *router);
  164. dir_server_t *trusted_dir_server_new(const char *nickname, const char *address,
  165. uint16_t dir_port, uint16_t or_port,
  166. const tor_addr_port_t *addrport_ipv6,
  167. const char *digest, const char *v3_auth_digest,
  168. dirinfo_type_t type, double weight);
  169. dir_server_t *fallback_dir_server_new(const tor_addr_t *addr,
  170. uint16_t dir_port, uint16_t or_port,
  171. const tor_addr_port_t *addrport_ipv6,
  172. const char *id_digest, double weight);
  173. void dir_server_add(dir_server_t *ent);
  174. void authority_cert_free(authority_cert_t *cert);
  175. void clear_dir_servers(void);
  176. void update_consensus_router_descriptor_downloads(time_t now, int is_vote,
  177. networkstatus_t *consensus);
  178. void update_router_descriptor_downloads(time_t now);
  179. void update_all_descriptor_downloads(time_t now);
  180. void update_extrainfo_downloads(time_t now);
  181. void router_reset_descriptor_download_failures(void);
  182. int router_differences_are_cosmetic(const routerinfo_t *r1,
  183. const routerinfo_t *r2);
  184. int routerinfo_incompatible_with_extrainfo(const crypto_pk_t *ri,
  185. extrainfo_t *ei,
  186. signed_descriptor_t *sd,
  187. const char **msg);
  188. void routerlist_assert_ok(const routerlist_t *rl);
  189. const char *esc_router_info(const routerinfo_t *router);
  190. void routers_sort_by_identity(smartlist_t *routers);
  191. void refresh_all_country_info(void);
  192. void list_pending_microdesc_downloads(digest256map_t *result);
  193. void launch_descriptor_downloads(int purpose,
  194. smartlist_t *downloadable,
  195. const routerstatus_t *source,
  196. time_t now);
  197. int hex_digest_nickname_decode(const char *hexdigest,
  198. char *digest_out,
  199. char *nickname_qualifier_out,
  200. char *nickname_out);
  201. int hex_digest_nickname_matches(const char *hexdigest,
  202. const char *identity_digest,
  203. const char *nickname, int is_named);
  204. #ifdef ROUTERLIST_PRIVATE
  205. STATIC int choose_array_element_by_weight(const uint64_t *entries,
  206. int n_entries);
  207. STATIC void scale_array_elements_to_u64(uint64_t *entries_out,
  208. const double *entries_in,
  209. int n_entries,
  210. uint64_t *total_out);
  211. STATIC const routerstatus_t *router_pick_directory_server_impl(
  212. dirinfo_type_t auth, int flags,
  213. int *n_busy_out);
  214. MOCK_DECL(int, router_descriptor_is_older_than, (const routerinfo_t *router,
  215. int seconds));
  216. MOCK_DECL(STATIC was_router_added_t, extrainfo_insert,
  217. (routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible));
  218. MOCK_DECL(STATIC void, initiate_descriptor_downloads,
  219. (const routerstatus_t *source, int purpose, smartlist_t *digests,
  220. int lo, int hi, int pds_flags));
  221. STATIC int router_is_already_dir_fetching(const tor_addr_port_t *ap,
  222. int serverdesc, int microdesc);
  223. #endif
  224. #endif