routerlist.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, 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 'ROUTER_IS_ALREADY_KNOWN' */
  37. /* Some certs on this router are expired. */
  38. ROUTER_CERTS_EXPIRED = -8,
  39. /* We couldn't format the annotations for this router. This is a directory
  40. * authority bug. */
  41. ROUTER_AUTHDIR_BUG_ANNOTATIONS = -10
  42. } was_router_added_t;
  43. /** How long do we avoid using a directory server after it's given us a 503? */
  44. #define DIR_503_TIMEOUT (60*60)
  45. int router_reload_router_list(void);
  46. int router_skip_or_reachability(const or_options_t *options, int try_ip_pref);
  47. int router_skip_dir_reachability(const or_options_t *options, int try_ip_pref);
  48. void router_reset_status_download_failures(void);
  49. int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2);
  50. void router_add_running_nodes_to_smartlist(smartlist_t *sl, int need_uptime,
  51. int need_capacity, int need_guard,
  52. int need_desc, int pref_addr,
  53. int direct_conn);
  54. const routerinfo_t *routerlist_find_my_routerinfo(void);
  55. uint32_t router_get_advertised_bandwidth(const routerinfo_t *router);
  56. uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router);
  57. int hexdigest_to_digest(const char *hexdigest, char *digest);
  58. const routerinfo_t *router_get_by_id_digest(const char *digest);
  59. routerinfo_t *router_get_mutable_by_digest(const char *digest);
  60. signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
  61. MOCK_DECL(signed_descriptor_t *,router_get_by_extrainfo_digest,
  62. (const char *digest));
  63. MOCK_DECL(signed_descriptor_t *,extrainfo_get_by_descriptor_digest,
  64. (const char *digest));
  65. const char *signed_descriptor_get_body(const signed_descriptor_t *desc);
  66. const char *signed_descriptor_get_annotations(const signed_descriptor_t *desc);
  67. routerlist_t *router_get_routerlist(void);
  68. void routerinfo_free_(routerinfo_t *router);
  69. #define routerinfo_free(router) \
  70. FREE_AND_NULL(routerinfo_t, routerinfo_free_, (router))
  71. void extrainfo_free_(extrainfo_t *extrainfo);
  72. #define extrainfo_free(ei) FREE_AND_NULL(extrainfo_t, extrainfo_free_, (ei))
  73. void routerlist_free_(routerlist_t *rl);
  74. #define routerlist_free(rl) FREE_AND_NULL(routerlist_t, routerlist_free_, (rl))
  75. void dump_routerlist_mem_usage(int severity);
  76. void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old,
  77. time_t now);
  78. void routerlist_free_all(void);
  79. void routerlist_reset_warnings(void);
  80. /* XXXX move this */
  81. void list_pending_downloads(digestmap_t *result,
  82. digest256map_t *result256,
  83. int purpose, const char *prefix);
  84. static int WRA_WAS_ADDED(was_router_added_t s);
  85. static int WRA_WAS_OUTDATED(was_router_added_t s);
  86. static int WRA_WAS_REJECTED(was_router_added_t s);
  87. static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s);
  88. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  89. * was added. It might still be necessary to check whether the descriptor
  90. * generator should be notified.
  91. */
  92. static inline int
  93. WRA_WAS_ADDED(was_router_added_t s) {
  94. return s == ROUTER_ADDED_SUCCESSFULLY;
  95. }
  96. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  97. * was not added because it was either:
  98. * - not in the consensus
  99. * - neither in the consensus nor in any networkstatus document
  100. * - it was outdated.
  101. * - its certificates were expired.
  102. */
  103. static inline int WRA_WAS_OUTDATED(was_router_added_t s)
  104. {
  105. return (s == ROUTER_WAS_TOO_OLD ||
  106. s == ROUTER_IS_ALREADY_KNOWN ||
  107. s == ROUTER_NOT_IN_CONSENSUS ||
  108. s == ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS ||
  109. s == ROUTER_CERTS_EXPIRED);
  110. }
  111. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  112. * was flat-out rejected. */
  113. static inline int WRA_WAS_REJECTED(was_router_added_t s)
  114. {
  115. return (s == ROUTER_AUTHDIR_REJECTS);
  116. }
  117. /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
  118. * was flat-out rejected. */
  119. static inline int WRA_NEVER_DOWNLOADABLE(was_router_added_t s)
  120. {
  121. return (s == ROUTER_AUTHDIR_REJECTS ||
  122. s == ROUTER_BAD_EI ||
  123. s == ROUTER_WAS_TOO_OLD ||
  124. s == ROUTER_CERTS_EXPIRED);
  125. }
  126. was_router_added_t router_add_to_routerlist(routerinfo_t *router,
  127. const char **msg,
  128. int from_cache,
  129. int from_fetch);
  130. was_router_added_t router_add_extrainfo_to_routerlist(
  131. extrainfo_t *ei, const char **msg,
  132. int from_cache, int from_fetch);
  133. void routerlist_descriptors_added(smartlist_t *sl, int from_cache);
  134. void routerlist_remove_old_routers(void);
  135. int router_load_single_router(const char *s, uint8_t purpose, int cache,
  136. const char **msg);
  137. int router_load_routers_from_string(const char *s, const char *eos,
  138. saved_location_t saved_location,
  139. smartlist_t *requested_fingerprints,
  140. int descriptor_digests,
  141. const char *prepend_annotations);
  142. void router_load_extrainfo_from_string(const char *s, const char *eos,
  143. saved_location_t saved_location,
  144. smartlist_t *requested_fingerprints,
  145. int descriptor_digests);
  146. void routerlist_retry_directory_downloads(time_t now);
  147. int router_exit_policy_rejects_all(const routerinfo_t *router);
  148. void update_consensus_router_descriptor_downloads(time_t now, int is_vote,
  149. networkstatus_t *consensus);
  150. void update_router_descriptor_downloads(time_t now);
  151. void update_all_descriptor_downloads(time_t now);
  152. void update_extrainfo_downloads(time_t now);
  153. void router_reset_descriptor_download_failures(void);
  154. int router_differences_are_cosmetic(const routerinfo_t *r1,
  155. const routerinfo_t *r2);
  156. int routerinfo_incompatible_with_extrainfo(const crypto_pk_t *ri,
  157. extrainfo_t *ei,
  158. signed_descriptor_t *sd,
  159. const char **msg);
  160. int routerinfo_has_curve25519_onion_key(const routerinfo_t *ri);
  161. int routerstatus_version_supports_extend2_cells(const routerstatus_t *rs,
  162. int allow_unknown_versions);
  163. void routerlist_assert_ok(const routerlist_t *rl);
  164. const char *esc_router_info(const routerinfo_t *router);
  165. void routers_sort_by_identity(smartlist_t *routers);
  166. void refresh_all_country_info(void);
  167. void list_pending_microdesc_downloads(digest256map_t *result);
  168. void launch_descriptor_downloads(int purpose,
  169. smartlist_t *downloadable,
  170. const routerstatus_t *source,
  171. time_t now);
  172. int hex_digest_nickname_decode(const char *hexdigest,
  173. char *digest_out,
  174. char *nickname_qualifier_out,
  175. char *nickname_out);
  176. int hex_digest_nickname_matches(const char *hexdigest,
  177. const char *identity_digest,
  178. const char *nickname);
  179. #ifdef ROUTERLIST_PRIVATE
  180. MOCK_DECL(int, router_descriptor_is_older_than, (const routerinfo_t *router,
  181. int seconds));
  182. MOCK_DECL(STATIC was_router_added_t, extrainfo_insert,
  183. (routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible));
  184. MOCK_DECL(STATIC void, initiate_descriptor_downloads,
  185. (const routerstatus_t *source, int purpose, smartlist_t *digests,
  186. int lo, int hi, int pds_flags));
  187. #endif /* defined(ROUTERLIST_PRIVATE) */
  188. #endif /* !defined(TOR_ROUTERLIST_H) */