routerlist.h 9.2 KB

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