1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2015, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Header file for routerlist.c.
11 #ifndef TOR_ROUTERLIST_H
12 #define TOR_ROUTERLIST_H
14 #include "testsupport.h"
16 int get_n_authorities(dirinfo_type_t type
);
17 int trusted_dirs_reload_certs(void);
20 * Pass one of these as source to trusted_dirs_load_certs_from_string()
21 * to indicate whence string originates; this controls error handling
22 * behavior such as marking downloads as failed.
25 #define TRUSTED_DIRS_CERTS_SRC_SELF 0
26 #define TRUSTED_DIRS_CERTS_SRC_FROM_STORE 1
27 #define TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST 2
28 #define TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST 3
29 #define TRUSTED_DIRS_CERTS_SRC_FROM_VOTE 4
31 int trusted_dirs_load_certs_from_string(const char *contents
, int source
,
33 void trusted_dirs_flush_certs_to_disk(void);
34 authority_cert_t
*authority_cert_get_newest_by_id(const char *id_digest
);
35 authority_cert_t
*authority_cert_get_by_sk_digest(const char *sk_digest
);
36 authority_cert_t
*authority_cert_get_by_digests(const char *id_digest
,
37 const char *sk_digest
);
38 void authority_cert_get_all(smartlist_t
*certs_out
);
39 void authority_cert_dl_failed(const char *id_digest
,
40 const char *signing_key_digest
, int status
);
41 void authority_certs_fetch_missing(networkstatus_t
*status
, time_t now
);
42 int router_reload_router_list(void);
43 int authority_cert_dl_looks_uncertain(const char *id_digest
);
44 const smartlist_t
*router_get_trusted_dir_servers(void);
45 const smartlist_t
*router_get_fallback_dir_servers(void);
46 int authority_cert_is_blacklisted(const authority_cert_t
*cert
);
48 const routerstatus_t
*router_pick_directory_server(dirinfo_type_t type
,
50 dir_server_t
*router_get_trusteddirserver_by_digest(const char *d
);
51 dir_server_t
*router_get_fallback_dirserver_by_digest(
53 dir_server_t
*trusteddirserver_get_by_v3_auth_digest(const char *d
);
54 const routerstatus_t
*router_pick_trusteddirserver(dirinfo_type_t type
,
56 const routerstatus_t
*router_pick_fallback_dirserver(dirinfo_type_t type
,
58 int router_get_my_share_of_directory_requests(double *v3_share_out
);
59 void router_reset_status_download_failures(void);
60 int routers_have_same_or_addrs(const routerinfo_t
*r1
, const routerinfo_t
*r2
);
61 void router_add_running_nodes_to_smartlist(smartlist_t
*sl
, int allow_invalid
,
62 int need_uptime
, int need_capacity
,
63 int need_guard
, int need_desc
);
65 const routerinfo_t
*routerlist_find_my_routerinfo(void);
66 uint32_t router_get_advertised_bandwidth(const routerinfo_t
*router
);
67 uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t
*router
);
69 const node_t
*node_sl_choose_by_bandwidth(const smartlist_t
*sl
,
70 bandwidth_weight_rule_t rule
);
71 double frac_nodes_with_descriptors(const smartlist_t
*sl
,
72 bandwidth_weight_rule_t rule
);
74 const node_t
*router_choose_random_node(smartlist_t
*excludedsmartlist
,
75 struct routerset_t
*excludedset
,
76 router_crn_flags_t flags
);
78 int router_is_named(const routerinfo_t
*router
);
79 int router_digest_is_trusted_dir_type(const char *digest
,
81 #define router_digest_is_trusted_dir(d) \
82 router_digest_is_trusted_dir_type((d), NO_DIRINFO)
84 int router_addr_is_trusted_dir(uint32_t addr
);
85 int hexdigest_to_digest(const char *hexdigest
, char *digest
);
86 const routerinfo_t
*router_get_by_id_digest(const char *digest
);
87 routerinfo_t
*router_get_mutable_by_digest(const char *digest
);
88 signed_descriptor_t
*router_get_by_descriptor_digest(const char *digest
);
89 MOCK_DECL(signed_descriptor_t
*,router_get_by_extrainfo_digest
,
90 (const char *digest
));
91 signed_descriptor_t
*extrainfo_get_by_descriptor_digest(const char *digest
);
92 const char *signed_descriptor_get_body(const signed_descriptor_t
*desc
);
93 const char *signed_descriptor_get_annotations(const signed_descriptor_t
*desc
);
94 routerlist_t
*router_get_routerlist(void);
95 void routerinfo_free(routerinfo_t
*router
);
96 void extrainfo_free(extrainfo_t
*extrainfo
);
97 void routerlist_free(routerlist_t
*rl
);
98 void dump_routerlist_mem_usage(int severity
);
99 void routerlist_remove(routerlist_t
*rl
, routerinfo_t
*ri
, int make_old
,
101 void routerlist_free_all(void);
102 void routerlist_reset_warnings(void);
104 static int WRA_WAS_ADDED(was_router_added_t s
);
105 static int WRA_WAS_OUTDATED(was_router_added_t s
);
106 static int WRA_WAS_REJECTED(was_router_added_t s
);
107 static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s
);
108 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
109 * was added. It might still be necessary to check whether the descriptor
110 * generator should be notified.
113 WRA_WAS_ADDED(was_router_added_t s
) {
114 return s
== ROUTER_ADDED_SUCCESSFULLY
|| s
== ROUTER_ADDED_NOTIFY_GENERATOR
;
116 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
117 * was not added because it was either:
118 * - not in the consensus
119 * - neither in the consensus nor in any networkstatus document
121 * - its certificates were expired.
123 static INLINE
int WRA_WAS_OUTDATED(was_router_added_t s
)
125 return (s
== ROUTER_WAS_TOO_OLD
||
126 s
== ROUTER_IS_ALREADY_KNOWN
||
127 s
== ROUTER_NOT_IN_CONSENSUS
||
128 s
== ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS
||
129 s
== ROUTER_CERTS_EXPIRED
);
131 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
132 * was flat-out rejected. */
133 static INLINE
int WRA_WAS_REJECTED(was_router_added_t s
)
135 return (s
== ROUTER_AUTHDIR_REJECTS
);
137 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
138 * was flat-out rejected. */
139 static INLINE
int WRA_NEVER_DOWNLOADABLE(was_router_added_t s
)
141 return (s
== ROUTER_AUTHDIR_REJECTS
||
142 s
== ROUTER_BAD_EI
||
143 s
== ROUTER_WAS_TOO_OLD
||
144 s
== ROUTER_CERTS_EXPIRED
);
146 was_router_added_t
router_add_to_routerlist(routerinfo_t
*router
,
150 was_router_added_t
router_add_extrainfo_to_routerlist(
151 extrainfo_t
*ei
, const char **msg
,
152 int from_cache
, int from_fetch
);
153 void routerlist_descriptors_added(smartlist_t
*sl
, int from_cache
);
154 void routerlist_remove_old_routers(void);
155 int router_load_single_router(const char *s
, uint8_t purpose
, int cache
,
157 int router_load_routers_from_string(const char *s
, const char *eos
,
158 saved_location_t saved_location
,
159 smartlist_t
*requested_fingerprints
,
160 int descriptor_digests
,
161 const char *prepend_annotations
);
162 void router_load_extrainfo_from_string(const char *s
, const char *eos
,
163 saved_location_t saved_location
,
164 smartlist_t
*requested_fingerprints
,
165 int descriptor_digests
);
167 void routerlist_retry_directory_downloads(time_t now
);
169 int router_exit_policy_rejects_all(const routerinfo_t
*router
);
171 dir_server_t
*trusted_dir_server_new(const char *nickname
, const char *address
,
172 uint16_t dir_port
, uint16_t or_port
,
173 const char *digest
, const char *v3_auth_digest
,
174 dirinfo_type_t type
, double weight
);
175 dir_server_t
*fallback_dir_server_new(const tor_addr_t
*addr
,
176 uint16_t dir_port
, uint16_t or_port
,
177 const char *id_digest
, double weight
);
178 void dir_server_add(dir_server_t
*ent
);
180 void authority_cert_free(authority_cert_t
*cert
);
181 void clear_dir_servers(void);
182 void update_consensus_router_descriptor_downloads(time_t now
, int is_vote
,
183 networkstatus_t
*consensus
);
184 void update_router_descriptor_downloads(time_t now
);
185 void update_all_descriptor_downloads(time_t now
);
186 void update_extrainfo_downloads(time_t now
);
187 void router_reset_descriptor_download_failures(void);
188 int router_differences_are_cosmetic(const routerinfo_t
*r1
,
189 const routerinfo_t
*r2
);
190 int routerinfo_incompatible_with_extrainfo(const routerinfo_t
*ri
,
192 signed_descriptor_t
*sd
,
195 void routerlist_assert_ok(const routerlist_t
*rl
);
196 const char *esc_router_info(const routerinfo_t
*router
);
197 void routers_sort_by_identity(smartlist_t
*routers
);
199 void refresh_all_country_info(void);
201 int hid_serv_get_responsible_directories(smartlist_t
*responsible_dirs
,
203 int hid_serv_acting_as_directory(void);
204 int hid_serv_responsible_for_desc_id(const char *id
);
206 void list_pending_microdesc_downloads(digest256map_t
*result
);
207 void launch_descriptor_downloads(int purpose
,
208 smartlist_t
*downloadable
,
209 const routerstatus_t
*source
,
212 int hex_digest_nickname_decode(const char *hexdigest
,
214 char *nickname_qualifier_out
,
216 int hex_digest_nickname_matches(const char *hexdigest
,
217 const char *identity_digest
,
218 const char *nickname
, int is_named
);
220 #ifdef ROUTERLIST_PRIVATE
221 /** Helper type for choosing routers by bandwidth: contains a union of
222 * double and uint64_t. Before we call scale_array_elements_to_u64, it holds
223 * a double; after, it holds a uint64_t. */
224 typedef union u64_dbl_t
{
229 STATIC
int choose_array_element_by_weight(const u64_dbl_t
*entries
,
231 STATIC
void scale_array_elements_to_u64(u64_dbl_t
*entries
, int n_entries
,
232 uint64_t *total_out
);
234 MOCK_DECL(int, router_descriptor_is_older_than
, (const routerinfo_t
*router
,
236 MOCK_DECL(STATIC was_router_added_t
, extrainfo_insert
,
237 (routerlist_t
*rl
, extrainfo_t
*ei
, int warn_if_incompatible
));
239 MOCK_DECL(STATIC
void, initiate_descriptor_downloads
,
240 (const routerstatus_t
*source
, int purpose
, smartlist_t
*digests
,
241 int lo
, int hi
, int pds_flags
));