routerlist.h (9727B)
1 /* Copyright (c) 2001-2004, Roger Dingledine. 2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. 3 * Copyright (c) 2007-2021, The Tor Project, Inc. */ 4 /* See LICENSE for licensing information */ 5 6 /** 7 * \file routerlist.h 8 * \brief Header file for routerlist.c. 9 **/ 10 11 #ifndef TOR_ROUTERLIST_H 12 #define TOR_ROUTERLIST_H 13 14 #include "lib/testsupport/testsupport.h" 15 16 /** Return value for router_add_to_routerlist() and dirserv_add_descriptor() */ 17 typedef enum was_router_added_t { 18 /* Router was added successfully. */ 19 ROUTER_ADDED_SUCCESSFULLY = 1, 20 /* Extrainfo document was rejected because no corresponding router 21 * descriptor was found OR router descriptor was rejected because 22 * it was incompatible with its extrainfo document. */ 23 ROUTER_BAD_EI = -1, 24 /* Router descriptor was rejected because it is already known. */ 25 ROUTER_IS_ALREADY_KNOWN = -2, 26 /* General purpose router was rejected, because it was not listed 27 * in consensus. */ 28 ROUTER_NOT_IN_CONSENSUS = -3, 29 /* Router was neither in directory consensus nor in any of 30 * networkstatus documents. Caching it to access later. 31 * (Applies to fetched descriptors only.) */ 32 ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS = -4, 33 /* Router was rejected by directory authority. */ 34 ROUTER_AUTHDIR_REJECTS = -5, 35 /* Bridge descriptor was rejected because such bridge was not one 36 * of the bridges we have listed in our configuration. */ 37 ROUTER_WAS_NOT_WANTED = -6, 38 /* Router descriptor was rejected because it was older than 39 * OLD_ROUTER_DESC_MAX_AGE. */ 40 ROUTER_WAS_TOO_OLD = -7, /* note contrast with 'ROUTER_IS_ALREADY_KNOWN' */ 41 /* Some certs on this router are expired. */ 42 ROUTER_CERTS_EXPIRED = -8, 43 /* We couldn't format the annotations for this router. This is a directory 44 * authority bug. */ 45 ROUTER_AUTHDIR_BUG_ANNOTATIONS = -10 46 } was_router_added_t; 47 48 /** How long do we avoid using a directory server after it's given us a 503? */ 49 #define DIR_503_TIMEOUT (60*60) 50 51 int router_reload_router_list(void); 52 53 int router_or_conn_should_skip_reachable_address_check( 54 const or_options_t *options, 55 int try_ip_pref); 56 int router_dir_conn_should_skip_reachable_address_check( 57 const or_options_t *options, 58 int try_ip_pref); 59 void router_reset_status_download_failures(void); 60 int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2); 61 bool router_can_choose_node(const node_t *node, int flags); 62 void router_add_running_nodes_to_smartlist(smartlist_t *sl, int flags); 63 64 const routerinfo_t *routerlist_find_my_routerinfo(void); 65 uint32_t router_get_advertised_bandwidth(const routerinfo_t *router); 66 uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router); 67 68 int hexdigest_to_digest(const char *hexdigest, char *digest); 69 const routerinfo_t *router_get_by_id_digest(const char *digest); 70 routerinfo_t *router_get_mutable_by_digest(const char *digest); 71 signed_descriptor_t *router_get_by_descriptor_digest(const char *digest); 72 MOCK_DECL(signed_descriptor_t *,router_get_by_extrainfo_digest, 73 (const char *digest)); 74 MOCK_DECL(signed_descriptor_t *,extrainfo_get_by_descriptor_digest, 75 (const char *digest)); 76 const char *signed_descriptor_get_body(const signed_descriptor_t *desc); 77 const char *signed_descriptor_get_annotations(const signed_descriptor_t *desc); 78 routerlist_t *router_get_routerlist(void); 79 void routerinfo_free_(routerinfo_t *router); 80 #define routerinfo_free(router) \ 81 FREE_AND_NULL(routerinfo_t, routerinfo_free_, (router)) 82 void extrainfo_free_(extrainfo_t *extrainfo); 83 #define extrainfo_free(ei) FREE_AND_NULL(extrainfo_t, extrainfo_free_, (ei)) 84 void routerlist_free_(routerlist_t *rl); 85 #define routerlist_free(rl) FREE_AND_NULL(routerlist_t, routerlist_free_, (rl)) 86 void dump_routerlist_mem_usage(int severity); 87 void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, 88 time_t now); 89 void routerlist_free_all(void); 90 void routerlist_reset_warnings(void); 91 92 /* XXXX move this */ 93 void list_pending_downloads(digestmap_t *result, 94 digest256map_t *result256, 95 int purpose, const char *prefix); 96 97 static int WRA_WAS_ADDED(was_router_added_t s); 98 static int WRA_WAS_OUTDATED(was_router_added_t s); 99 static int WRA_WAS_REJECTED(was_router_added_t s); 100 static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s); 101 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor 102 * was added. It might still be necessary to check whether the descriptor 103 * generator should be notified. 104 */ 105 static inline int 106 WRA_WAS_ADDED(was_router_added_t s) { 107 return s == ROUTER_ADDED_SUCCESSFULLY; 108 } 109 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor 110 * was not added because it was either: 111 * - not in the consensus 112 * - neither in the consensus nor in any networkstatus document 113 * - it was outdated. 114 * - its certificates were expired. 115 */ 116 static inline int WRA_WAS_OUTDATED(was_router_added_t s) 117 { 118 return (s == ROUTER_WAS_TOO_OLD || 119 s == ROUTER_IS_ALREADY_KNOWN || 120 s == ROUTER_NOT_IN_CONSENSUS || 121 s == ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS || 122 s == ROUTER_CERTS_EXPIRED); 123 } 124 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor 125 * was flat-out rejected. */ 126 static inline int WRA_WAS_REJECTED(was_router_added_t s) 127 { 128 return (s == ROUTER_AUTHDIR_REJECTS); 129 } 130 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor 131 * was flat-out rejected. */ 132 static inline int WRA_NEVER_DOWNLOADABLE(was_router_added_t s) 133 { 134 return (s == ROUTER_AUTHDIR_REJECTS || 135 s == ROUTER_BAD_EI || 136 s == ROUTER_WAS_TOO_OLD || 137 s == ROUTER_CERTS_EXPIRED); 138 } 139 was_router_added_t router_add_to_routerlist(routerinfo_t *router, 140 const char **msg, 141 int from_cache, 142 int from_fetch); 143 was_router_added_t router_add_extrainfo_to_routerlist( 144 extrainfo_t *ei, const char **msg, 145 int from_cache, int from_fetch); 146 void routerlist_descriptors_added(smartlist_t *sl, int from_cache); 147 void routerlist_remove_old_routers(void); 148 void routerlist_drop_bridge_descriptors(void); 149 int router_load_single_router(const char *s, uint8_t purpose, int cache, 150 const char **msg); 151 int router_load_routers_from_string(const char *s, const char *eos, 152 saved_location_t saved_location, 153 smartlist_t *requested_fingerprints, 154 int descriptor_digests, 155 const char *prepend_annotations); 156 void router_load_extrainfo_from_string(const char *s, const char *eos, 157 saved_location_t saved_location, 158 smartlist_t *requested_fingerprints, 159 int descriptor_digests); 160 161 void routerlist_retry_directory_downloads(time_t now); 162 163 int router_exit_policy_rejects_all(const routerinfo_t *router); 164 165 void update_consensus_router_descriptor_downloads(time_t now, int is_vote, 166 networkstatus_t *consensus); 167 void update_router_descriptor_downloads(time_t now); 168 void update_all_descriptor_downloads(time_t now); 169 void update_extrainfo_downloads(time_t now); 170 void router_reset_descriptor_download_failures(void); 171 int router_differences_are_cosmetic(const routerinfo_t *r1, 172 const routerinfo_t *r2); 173 int routerinfo_incompatible_with_extrainfo(const crypto_pk_t *ri, 174 extrainfo_t *ei, 175 signed_descriptor_t *sd, 176 const char **msg); 177 int routerinfo_has_curve25519_onion_key(const routerinfo_t *ri); 178 int routerstatus_version_supports_extend2_cells(const routerstatus_t *rs, 179 int allow_unknown_versions); 180 181 void routerlist_assert_ok(const routerlist_t *rl); 182 const char *esc_router_info(const routerinfo_t *router); 183 void routers_sort_by_identity(smartlist_t *routers); 184 185 void refresh_all_country_info(void); 186 187 void list_pending_microdesc_downloads(digest256map_t *result); 188 void launch_descriptor_downloads(int purpose, 189 smartlist_t *downloadable, 190 const routerstatus_t *source, 191 time_t now); 192 193 int hex_digest_nickname_decode(const char *hexdigest, 194 char *digest_out, 195 char *nickname_qualifier_out, 196 char *nickname_out); 197 int hex_digest_nickname_matches(const char *hexdigest, 198 const char *identity_digest, 199 const char *nickname); 200 201 #ifdef ROUTERLIST_PRIVATE 202 MOCK_DECL(int, router_descriptor_is_older_than, (const routerinfo_t *router, 203 int seconds)); 204 MOCK_DECL(STATIC was_router_added_t, extrainfo_insert, 205 (routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible)); 206 207 MOCK_DECL(STATIC void, initiate_descriptor_downloads, 208 (const routerstatus_t *source, int purpose, smartlist_t *digests, 209 int lo, int hi, int pds_flags)); 210 211 #endif /* defined(ROUTERLIST_PRIVATE) */ 212 213 #endif /* !defined(TOR_ROUTERLIST_H) */