Merge remote-tracking branch 'karsten/task-18460-2' into maint-0.2.8
[tor.git] / src / or / routerlist.h
blobbc48c2087c9ec6a06310f93d80c2e22ac763236d
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2016, 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);
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,
49 int flags);
50 dir_server_t *router_get_trusteddirserver_by_digest(const char *d);
51 dir_server_t *router_get_fallback_dirserver_by_digest(
52 const char *digest);
53 int router_digest_is_fallback_dir(const char *digest);
54 dir_server_t *trusteddirserver_get_by_v3_auth_digest(const char *d);
55 const routerstatus_t *router_pick_trusteddirserver(dirinfo_type_t type,
56 int flags);
57 const routerstatus_t *router_pick_fallback_dirserver(dirinfo_type_t type,
58 int flags);
59 int router_get_my_share_of_directory_requests(double *v3_share_out);
60 void router_reset_status_download_failures(void);
61 int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2);
62 void router_add_running_nodes_to_smartlist(smartlist_t *sl, int allow_invalid,
63 int need_uptime, int need_capacity,
64 int need_guard, int need_desc,
65 int pref_addr);
67 const routerinfo_t *routerlist_find_my_routerinfo(void);
68 uint32_t router_get_advertised_bandwidth(const routerinfo_t *router);
69 uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router);
71 const node_t *node_sl_choose_by_bandwidth(const smartlist_t *sl,
72 bandwidth_weight_rule_t rule);
73 double frac_nodes_with_descriptors(const smartlist_t *sl,
74 bandwidth_weight_rule_t rule);
76 const node_t *router_choose_random_node(smartlist_t *excludedsmartlist,
77 struct routerset_t *excludedset,
78 router_crn_flags_t flags);
80 int router_is_named(const routerinfo_t *router);
81 int router_digest_is_trusted_dir_type(const char *digest,
82 dirinfo_type_t type);
83 #define router_digest_is_trusted_dir(d) \
84 router_digest_is_trusted_dir_type((d), NO_DIRINFO)
86 int router_addr_is_trusted_dir(uint32_t addr);
87 int hexdigest_to_digest(const char *hexdigest, char *digest);
88 const routerinfo_t *router_get_by_id_digest(const char *digest);
89 routerinfo_t *router_get_mutable_by_digest(const char *digest);
90 signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
91 MOCK_DECL(signed_descriptor_t *,router_get_by_extrainfo_digest,
92 (const char *digest));
93 signed_descriptor_t *extrainfo_get_by_descriptor_digest(const char *digest);
94 const char *signed_descriptor_get_body(const signed_descriptor_t *desc);
95 const char *signed_descriptor_get_annotations(const signed_descriptor_t *desc);
96 routerlist_t *router_get_routerlist(void);
97 void routerinfo_free(routerinfo_t *router);
98 void extrainfo_free(extrainfo_t *extrainfo);
99 void routerlist_free(routerlist_t *rl);
100 void dump_routerlist_mem_usage(int severity);
101 void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old,
102 time_t now);
103 void routerlist_free_all(void);
104 void routerlist_reset_warnings(void);
106 static int WRA_WAS_ADDED(was_router_added_t s);
107 static int WRA_WAS_OUTDATED(was_router_added_t s);
108 static int WRA_WAS_REJECTED(was_router_added_t s);
109 static int WRA_NEVER_DOWNLOADABLE(was_router_added_t s);
110 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
111 * was added. It might still be necessary to check whether the descriptor
112 * generator should be notified.
114 static inline int
115 WRA_WAS_ADDED(was_router_added_t s) {
116 return s == ROUTER_ADDED_SUCCESSFULLY || s == ROUTER_ADDED_NOTIFY_GENERATOR;
118 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
119 * was not added because it was either:
120 * - not in the consensus
121 * - neither in the consensus nor in any networkstatus document
122 * - it was outdated.
123 * - its certificates were expired.
125 static inline int WRA_WAS_OUTDATED(was_router_added_t s)
127 return (s == ROUTER_WAS_TOO_OLD ||
128 s == ROUTER_IS_ALREADY_KNOWN ||
129 s == ROUTER_NOT_IN_CONSENSUS ||
130 s == ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS ||
131 s == ROUTER_CERTS_EXPIRED);
133 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
134 * was flat-out rejected. */
135 static inline int WRA_WAS_REJECTED(was_router_added_t s)
137 return (s == ROUTER_AUTHDIR_REJECTS);
139 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
140 * was flat-out rejected. */
141 static inline int WRA_NEVER_DOWNLOADABLE(was_router_added_t s)
143 return (s == ROUTER_AUTHDIR_REJECTS ||
144 s == ROUTER_BAD_EI ||
145 s == ROUTER_WAS_TOO_OLD ||
146 s == ROUTER_CERTS_EXPIRED);
148 was_router_added_t router_add_to_routerlist(routerinfo_t *router,
149 const char **msg,
150 int from_cache,
151 int from_fetch);
152 was_router_added_t router_add_extrainfo_to_routerlist(
153 extrainfo_t *ei, const char **msg,
154 int from_cache, int from_fetch);
155 void routerlist_descriptors_added(smartlist_t *sl, int from_cache);
156 void routerlist_remove_old_routers(void);
157 int router_load_single_router(const char *s, uint8_t purpose, int cache,
158 const char **msg);
159 int router_load_routers_from_string(const char *s, const char *eos,
160 saved_location_t saved_location,
161 smartlist_t *requested_fingerprints,
162 int descriptor_digests,
163 const char *prepend_annotations);
164 void router_load_extrainfo_from_string(const char *s, const char *eos,
165 saved_location_t saved_location,
166 smartlist_t *requested_fingerprints,
167 int descriptor_digests);
169 void routerlist_retry_directory_downloads(time_t now);
171 int router_exit_policy_rejects_all(const routerinfo_t *router);
173 dir_server_t *trusted_dir_server_new(const char *nickname, const char *address,
174 uint16_t dir_port, uint16_t or_port,
175 const tor_addr_port_t *addrport_ipv6,
176 const char *digest, const char *v3_auth_digest,
177 dirinfo_type_t type, double weight);
178 dir_server_t *fallback_dir_server_new(const tor_addr_t *addr,
179 uint16_t dir_port, uint16_t or_port,
180 const tor_addr_port_t *addrport_ipv6,
181 const char *id_digest, double weight);
182 void dir_server_add(dir_server_t *ent);
184 void authority_cert_free(authority_cert_t *cert);
185 void clear_dir_servers(void);
186 void update_consensus_router_descriptor_downloads(time_t now, int is_vote,
187 networkstatus_t *consensus);
188 void update_router_descriptor_downloads(time_t now);
189 void update_all_descriptor_downloads(time_t now);
190 void update_extrainfo_downloads(time_t now);
191 void router_reset_descriptor_download_failures(void);
192 int router_differences_are_cosmetic(const routerinfo_t *r1,
193 const routerinfo_t *r2);
194 int routerinfo_incompatible_with_extrainfo(const routerinfo_t *ri,
195 extrainfo_t *ei,
196 signed_descriptor_t *sd,
197 const char **msg);
199 void routerlist_assert_ok(const routerlist_t *rl);
200 const char *esc_router_info(const routerinfo_t *router);
201 void routers_sort_by_identity(smartlist_t *routers);
203 void refresh_all_country_info(void);
205 void list_pending_microdesc_downloads(digest256map_t *result);
206 void launch_descriptor_downloads(int purpose,
207 smartlist_t *downloadable,
208 const routerstatus_t *source,
209 time_t now);
211 int hex_digest_nickname_decode(const char *hexdigest,
212 char *digest_out,
213 char *nickname_qualifier_out,
214 char *nickname_out);
215 int hex_digest_nickname_matches(const char *hexdigest,
216 const char *identity_digest,
217 const char *nickname, int is_named);
219 #ifdef ROUTERLIST_PRIVATE
220 /** Helper type for choosing routers by bandwidth: contains a union of
221 * double and uint64_t. Before we call scale_array_elements_to_u64, it holds
222 * a double; after, it holds a uint64_t. */
223 typedef union u64_dbl_t {
224 uint64_t u64;
225 double dbl;
226 } u64_dbl_t;
228 STATIC int choose_array_element_by_weight(const u64_dbl_t *entries,
229 int n_entries);
230 STATIC void scale_array_elements_to_u64(u64_dbl_t *entries, int n_entries,
231 uint64_t *total_out);
232 STATIC const routerstatus_t *router_pick_directory_server_impl(
233 dirinfo_type_t auth, int flags,
234 int *n_busy_out);
236 MOCK_DECL(int, router_descriptor_is_older_than, (const routerinfo_t *router,
237 int seconds));
238 MOCK_DECL(STATIC was_router_added_t, extrainfo_insert,
239 (routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible));
241 MOCK_DECL(STATIC void, initiate_descriptor_downloads,
242 (const routerstatus_t *source, int purpose, smartlist_t *digests,
243 int lo, int hi, int pds_flags));
244 STATIC int router_is_already_dir_fetching(const tor_addr_port_t *ap,
245 int serverdesc, int microdesc);
247 #endif
249 #endif