Cache control file
[tor/appveyor.git] / src / or / routerlist.h
blob83f4d1002f9f1392975ab747da255d656d1e5ce3
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file routerlist.h
8 * \brief Header file for routerlist.c.
9 **/
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,
32 int flush, const char *source_dir);
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 const char *dir_hint);
43 int router_reload_router_list(void);
44 int authority_cert_dl_looks_uncertain(const char *id_digest);
45 const smartlist_t *router_get_trusted_dir_servers(void);
46 const smartlist_t *router_get_fallback_dir_servers(void);
47 int authority_cert_is_blacklisted(const authority_cert_t *cert);
49 const routerstatus_t *router_pick_directory_server(dirinfo_type_t type,
50 int flags);
51 dir_server_t *router_get_trusteddirserver_by_digest(const char *d);
52 dir_server_t *router_get_fallback_dirserver_by_digest(
53 const char *digest);
54 int router_digest_is_fallback_dir(const char *digest);
55 MOCK_DECL(dir_server_t *, trusteddirserver_get_by_v3_auth_digest,
56 (const char *d));
57 const routerstatus_t *router_pick_trusteddirserver(dirinfo_type_t type,
58 int flags);
59 const routerstatus_t *router_pick_fallback_dirserver(dirinfo_type_t type,
60 int flags);
61 int router_skip_or_reachability(const or_options_t *options, int try_ip_pref);
62 int router_get_my_share_of_directory_requests(double *v3_share_out);
63 void router_reset_status_download_failures(void);
64 int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2);
65 void router_add_running_nodes_to_smartlist(smartlist_t *sl, int need_uptime,
66 int need_capacity, int need_guard,
67 int need_desc, int pref_addr,
68 int direct_conn);
70 const routerinfo_t *routerlist_find_my_routerinfo(void);
71 uint32_t router_get_advertised_bandwidth(const routerinfo_t *router);
72 uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router);
74 const node_t *node_sl_choose_by_bandwidth(const smartlist_t *sl,
75 bandwidth_weight_rule_t rule);
76 double frac_nodes_with_descriptors(const smartlist_t *sl,
77 bandwidth_weight_rule_t rule);
79 const node_t *router_choose_random_node(smartlist_t *excludedsmartlist,
80 struct routerset_t *excludedset,
81 router_crn_flags_t flags);
83 int router_digest_is_trusted_dir_type(const char *digest,
84 dirinfo_type_t type);
85 #define router_digest_is_trusted_dir(d) \
86 router_digest_is_trusted_dir_type((d), NO_DIRINFO)
88 int hexdigest_to_digest(const char *hexdigest, char *digest);
89 const routerinfo_t *router_get_by_id_digest(const char *digest);
90 routerinfo_t *router_get_mutable_by_digest(const char *digest);
91 signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
92 MOCK_DECL(signed_descriptor_t *,router_get_by_extrainfo_digest,
93 (const char *digest));
94 MOCK_DECL(signed_descriptor_t *,extrainfo_get_by_descriptor_digest,
95 (const char *digest));
96 const char *signed_descriptor_get_body(const signed_descriptor_t *desc);
97 const char *signed_descriptor_get_annotations(const signed_descriptor_t *desc);
98 routerlist_t *router_get_routerlist(void);
99 void routerinfo_free_(routerinfo_t *router);
100 #define routerinfo_free(router) \
101 FREE_AND_NULL(routerinfo_t, routerinfo_free_, (router))
102 void extrainfo_free_(extrainfo_t *extrainfo);
103 #define extrainfo_free(ei) FREE_AND_NULL(extrainfo_t, extrainfo_free_, (ei))
104 void routerlist_free_(routerlist_t *rl);
105 #define routerlist_free(rl) FREE_AND_NULL(routerlist_t, routerlist_free_, (rl))
106 void dump_routerlist_mem_usage(int severity);
107 void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old,
108 time_t now);
109 void routerlist_free_all(void);
110 void routerlist_reset_warnings(void);
112 MOCK_DECL(smartlist_t *, list_authority_ids_with_downloads, (void));
113 MOCK_DECL(download_status_t *, id_only_download_status_for_authority_id,
114 (const char *digest));
115 MOCK_DECL(smartlist_t *, list_sk_digests_for_authority_id,
116 (const char *digest));
117 MOCK_DECL(download_status_t *, download_status_for_authority_id_and_sk,
118 (const char *id_digest, const char *sk_digest));
120 static int WRA_WAS_ADDED(was_router_added_t s);
121 static int WRA_WAS_OUTDATED(was_router_added_t s);
122 static int WRA_WAS_REJECTED(was_router_added_t s);
123 static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s);
124 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
125 * was added. It might still be necessary to check whether the descriptor
126 * generator should be notified.
128 static inline int
129 WRA_WAS_ADDED(was_router_added_t s) {
130 return s == ROUTER_ADDED_SUCCESSFULLY;
132 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
133 * was not added because it was either:
134 * - not in the consensus
135 * - neither in the consensus nor in any networkstatus document
136 * - it was outdated.
137 * - its certificates were expired.
139 static inline int WRA_WAS_OUTDATED(was_router_added_t s)
141 return (s == ROUTER_WAS_TOO_OLD ||
142 s == ROUTER_IS_ALREADY_KNOWN ||
143 s == ROUTER_NOT_IN_CONSENSUS ||
144 s == ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS ||
145 s == ROUTER_CERTS_EXPIRED);
147 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
148 * was flat-out rejected. */
149 static inline int WRA_WAS_REJECTED(was_router_added_t s)
151 return (s == ROUTER_AUTHDIR_REJECTS);
153 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
154 * was flat-out rejected. */
155 static inline int WRA_NEVER_DOWNLOADABLE(was_router_added_t s)
157 return (s == ROUTER_AUTHDIR_REJECTS ||
158 s == ROUTER_BAD_EI ||
159 s == ROUTER_WAS_TOO_OLD ||
160 s == ROUTER_CERTS_EXPIRED);
162 was_router_added_t router_add_to_routerlist(routerinfo_t *router,
163 const char **msg,
164 int from_cache,
165 int from_fetch);
166 was_router_added_t router_add_extrainfo_to_routerlist(
167 extrainfo_t *ei, const char **msg,
168 int from_cache, int from_fetch);
169 void routerlist_descriptors_added(smartlist_t *sl, int from_cache);
170 void routerlist_remove_old_routers(void);
171 int router_load_single_router(const char *s, uint8_t purpose, int cache,
172 const char **msg);
173 int router_load_routers_from_string(const char *s, const char *eos,
174 saved_location_t saved_location,
175 smartlist_t *requested_fingerprints,
176 int descriptor_digests,
177 const char *prepend_annotations);
178 void router_load_extrainfo_from_string(const char *s, const char *eos,
179 saved_location_t saved_location,
180 smartlist_t *requested_fingerprints,
181 int descriptor_digests);
183 void routerlist_retry_directory_downloads(time_t now);
185 int router_exit_policy_rejects_all(const routerinfo_t *router);
187 dir_server_t *trusted_dir_server_new(const char *nickname, const char *address,
188 uint16_t dir_port, uint16_t or_port,
189 const tor_addr_port_t *addrport_ipv6,
190 const char *digest, const char *v3_auth_digest,
191 dirinfo_type_t type, double weight);
192 dir_server_t *fallback_dir_server_new(const tor_addr_t *addr,
193 uint16_t dir_port, uint16_t or_port,
194 const tor_addr_port_t *addrport_ipv6,
195 const char *id_digest, double weight);
196 void dir_server_add(dir_server_t *ent);
198 void authority_cert_free_(authority_cert_t *cert);
199 #define authority_cert_free(cert) \
200 FREE_AND_NULL(authority_cert_t, authority_cert_free_, (cert))
201 void clear_dir_servers(void);
202 void update_consensus_router_descriptor_downloads(time_t now, int is_vote,
203 networkstatus_t *consensus);
204 void update_router_descriptor_downloads(time_t now);
205 void update_all_descriptor_downloads(time_t now);
206 void update_extrainfo_downloads(time_t now);
207 void router_reset_descriptor_download_failures(void);
208 int router_differences_are_cosmetic(const routerinfo_t *r1,
209 const routerinfo_t *r2);
210 int routerinfo_incompatible_with_extrainfo(const crypto_pk_t *ri,
211 extrainfo_t *ei,
212 signed_descriptor_t *sd,
213 const char **msg);
214 int routerinfo_has_curve25519_onion_key(const routerinfo_t *ri);
215 int routerstatus_version_supports_extend2_cells(const routerstatus_t *rs,
216 int allow_unknown_versions);
218 void routerlist_assert_ok(const routerlist_t *rl);
219 const char *esc_router_info(const routerinfo_t *router);
220 void routers_sort_by_identity(smartlist_t *routers);
222 void refresh_all_country_info(void);
224 void list_pending_microdesc_downloads(digest256map_t *result);
225 void launch_descriptor_downloads(int purpose,
226 smartlist_t *downloadable,
227 const routerstatus_t *source,
228 time_t now);
230 int hex_digest_nickname_decode(const char *hexdigest,
231 char *digest_out,
232 char *nickname_qualifier_out,
233 char *nickname_out);
234 int hex_digest_nickname_matches(const char *hexdigest,
235 const char *identity_digest,
236 const char *nickname);
238 #ifdef ROUTERLIST_PRIVATE
239 STATIC int choose_array_element_by_weight(const uint64_t *entries,
240 int n_entries);
241 STATIC void scale_array_elements_to_u64(uint64_t *entries_out,
242 const double *entries_in,
243 int n_entries,
244 uint64_t *total_out);
245 STATIC const routerstatus_t *router_pick_directory_server_impl(
246 dirinfo_type_t auth, int flags,
247 int *n_busy_out);
249 MOCK_DECL(int, router_descriptor_is_older_than, (const routerinfo_t *router,
250 int seconds));
251 MOCK_DECL(STATIC was_router_added_t, extrainfo_insert,
252 (routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible));
254 MOCK_DECL(STATIC void, initiate_descriptor_downloads,
255 (const routerstatus_t *source, int purpose, smartlist_t *digests,
256 int lo, int hi, int pds_flags));
257 STATIC int router_is_already_dir_fetching(const tor_addr_port_t *ap,
258 int serverdesc, int microdesc);
260 #endif /* defined(ROUTERLIST_PRIVATE) */
262 #endif /* !defined(TOR_ROUTERLIST_H) */