Merge remote-tracking branch 'public/bug6538'
[tor.git] / src / or / routerlist.h
blobab58b9ee5a53805985caaca92057c0194ed52804
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2012, 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 int get_n_authorities(dirinfo_type_t type);
15 int trusted_dirs_reload_certs(void);
16 int trusted_dirs_load_certs_from_string(const char *contents, int from_store,
17 int flush);
18 void trusted_dirs_flush_certs_to_disk(void);
19 authority_cert_t *authority_cert_get_newest_by_id(const char *id_digest);
20 authority_cert_t *authority_cert_get_by_sk_digest(const char *sk_digest);
21 authority_cert_t *authority_cert_get_by_digests(const char *id_digest,
22 const char *sk_digest);
23 void authority_cert_get_all(smartlist_t *certs_out);
24 void authority_cert_dl_failed(const char *id_digest, int status);
25 void authority_certs_fetch_missing(networkstatus_t *status, time_t now);
26 int router_reload_router_list(void);
27 int authority_cert_dl_looks_uncertain(const char *id_digest);
28 smartlist_t *router_get_trusted_dir_servers(void);
30 const routerstatus_t *router_pick_directory_server(dirinfo_type_t type,
31 int flags);
32 trusted_dir_server_t *router_get_trusteddirserver_by_digest(const char *d);
33 trusted_dir_server_t *trusteddirserver_get_by_v3_auth_digest(const char *d);
34 const routerstatus_t *router_pick_trusteddirserver(dirinfo_type_t type,
35 int flags);
36 int router_get_my_share_of_directory_requests(double *v2_share_out,
37 double *v3_share_out);
38 void router_reset_status_download_failures(void);
39 int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2);
40 int router_nickname_is_in_list(const routerinfo_t *router, const char *list);
41 const routerinfo_t *routerlist_find_my_routerinfo(void);
42 const node_t *router_find_exact_exit_enclave(const char *address,
43 uint16_t port);
44 int node_is_unreliable(const node_t *router, int need_uptime,
45 int need_capacity, int need_guard);
46 uint32_t router_get_advertised_bandwidth(const routerinfo_t *router);
47 uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router);
49 const node_t *node_sl_choose_by_bandwidth(smartlist_t *sl,
50 bandwidth_weight_rule_t rule);
52 const node_t *router_choose_random_node(smartlist_t *excludedsmartlist,
53 struct routerset_t *excludedset,
54 router_crn_flags_t flags);
56 const routerinfo_t *router_get_by_nickname(const char *nickname,
57 int warn_if_unnamed);
58 int router_is_named(const routerinfo_t *router);
59 int router_digest_is_trusted_dir_type(const char *digest,
60 dirinfo_type_t type);
61 #define router_digest_is_trusted_dir(d) \
62 router_digest_is_trusted_dir_type((d), NO_DIRINFO)
64 int router_addr_is_trusted_dir(uint32_t addr);
65 int hexdigest_to_digest(const char *hexdigest, char *digest);
66 const routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
67 const routerinfo_t *router_get_by_id_digest(const char *digest);
68 routerinfo_t *router_get_mutable_by_digest(const char *digest);
69 signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
70 signed_descriptor_t *router_get_by_extrainfo_digest(const char *digest);
71 signed_descriptor_t *extrainfo_get_by_descriptor_digest(const char *digest);
72 const char *signed_descriptor_get_body(const signed_descriptor_t *desc);
73 const char *signed_descriptor_get_annotations(const signed_descriptor_t *desc);
74 routerlist_t *router_get_routerlist(void);
75 void routerinfo_free(routerinfo_t *router);
76 void extrainfo_free(extrainfo_t *extrainfo);
77 void routerlist_free(routerlist_t *rl);
78 void dump_routerlist_mem_usage(int severity);
79 void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old,
80 time_t now);
81 void routerlist_free_all(void);
82 void routerlist_reset_warnings(void);
83 void router_set_status(const char *digest, int up);
85 static int WRA_WAS_ADDED(was_router_added_t s);
86 static int WRA_WAS_OUTDATED(was_router_added_t s);
87 static int WRA_WAS_REJECTED(was_router_added_t s);
88 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
89 * was added. It might still be necessary to check whether the descriptor
90 * generator should be notified.
92 static INLINE int
93 WRA_WAS_ADDED(was_router_added_t s) {
94 return s == ROUTER_ADDED_SUCCESSFULLY || s == ROUTER_ADDED_NOTIFY_GENERATOR;
96 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
97 * was not added because it was either:
98 * - not in the consensus
99 * - neither in the consensus nor in any networkstatus document
100 * - it was outdated.
102 static INLINE int WRA_WAS_OUTDATED(was_router_added_t s)
104 return (s == ROUTER_WAS_NOT_NEW ||
105 s == ROUTER_NOT_IN_CONSENSUS ||
106 s == ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS);
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)
112 return (s == ROUTER_AUTHDIR_REJECTS);
114 was_router_added_t router_add_to_routerlist(routerinfo_t *router,
115 const char **msg,
116 int from_cache,
117 int from_fetch);
118 was_router_added_t router_add_extrainfo_to_routerlist(
119 extrainfo_t *ei, const char **msg,
120 int from_cache, int from_fetch);
121 void routerlist_descriptors_added(smartlist_t *sl, int from_cache);
122 void routerlist_remove_old_routers(void);
123 int router_load_single_router(const char *s, uint8_t purpose, int cache,
124 const char **msg);
125 int router_load_routers_from_string(const char *s, const char *eos,
126 saved_location_t saved_location,
127 smartlist_t *requested_fingerprints,
128 int descriptor_digests,
129 const char *prepend_annotations);
130 void router_load_extrainfo_from_string(const char *s, const char *eos,
131 saved_location_t saved_location,
132 smartlist_t *requested_fingerprints,
133 int descriptor_digests);
135 void routerlist_retry_directory_downloads(time_t now);
136 int router_exit_policy_all_nodes_reject(const tor_addr_t *addr, uint16_t port,
137 int need_uptime);
139 int router_exit_policy_rejects_all(const routerinfo_t *router);
140 trusted_dir_server_t *add_trusted_dir_server(const char *nickname,
141 const char *address,
142 uint16_t dir_port, uint16_t or_port,
143 const char *digest, const char *v3_auth_digest,
144 dirinfo_type_t type);
145 void authority_cert_free(authority_cert_t *cert);
146 void clear_trusted_dir_servers(void);
147 int any_trusted_dir_is_v1_authority(void);
148 void update_consensus_router_descriptor_downloads(time_t now, int is_vote,
149 networkstatus_t *consensus);
150 void update_router_descriptor_downloads(time_t now);
151 void update_all_descriptor_downloads(time_t now);
152 void update_extrainfo_downloads(time_t now);
153 int router_have_minimum_dir_info(void);
154 void router_dir_info_changed(void);
155 const char *get_dir_info_status_string(void);
156 int count_loading_descriptors_progress(void);
157 void router_reset_descriptor_download_failures(void);
158 int router_differences_are_cosmetic(const routerinfo_t *r1,
159 const routerinfo_t *r2);
160 int routerinfo_incompatible_with_extrainfo(const routerinfo_t *ri,
161 extrainfo_t *ei,
162 signed_descriptor_t *sd,
163 const char **msg);
165 void routerlist_assert_ok(const routerlist_t *rl);
166 const char *esc_router_info(const routerinfo_t *router);
167 void routers_sort_by_identity(smartlist_t *routers);
169 routerset_t *routerset_new(void);
170 void routerset_refresh_countries(routerset_t *rs);
171 int routerset_parse(routerset_t *target, const char *s,
172 const char *description);
173 void routerset_union(routerset_t *target, const routerset_t *source);
174 int routerset_is_list(const routerset_t *set);
175 int routerset_needs_geoip(const routerset_t *set);
176 int routerset_is_empty(const routerset_t *set);
177 int routerset_contains_router(const routerset_t *set, const routerinfo_t *ri,
178 country_t country);
179 int routerset_contains_routerstatus(const routerset_t *set,
180 const routerstatus_t *rs,
181 country_t country);
182 int routerset_contains_extendinfo(const routerset_t *set,
183 const extend_info_t *ei);
185 int routerset_contains_node(const routerset_t *set, const node_t *node);
186 void routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset,
187 const routerset_t *excludeset,
188 int running_only);
189 #if 0
190 void routersets_get_node_disjunction(smartlist_t *target,
191 const smartlist_t *source,
192 const routerset_t *include,
193 const routerset_t *exclude, int running_only);
194 #endif
195 void routerset_subtract_nodes(smartlist_t *out,
196 const routerset_t *routerset);
198 char *routerset_to_string(const routerset_t *routerset);
199 int routerset_equal(const routerset_t *old, const routerset_t *new);
200 void routerset_free(routerset_t *routerset);
201 void refresh_all_country_info(void);
203 int hid_serv_get_responsible_directories(smartlist_t *responsible_dirs,
204 const char *id);
205 int hid_serv_acting_as_directory(void);
206 int hid_serv_responsible_for_desc_id(const char *id);
208 void list_pending_microdesc_downloads(digestmap_t *result);
209 void launch_descriptor_downloads(int purpose,
210 smartlist_t *downloadable,
211 const routerstatus_t *source,
212 time_t now);
214 int hex_digest_nickname_decode(const char *hexdigest,
215 char *digest_out,
216 char *nickname_qualifier_out,
217 char *nickname_out);
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 int choose_array_element_by_weight(const u64_dbl_t *entries, int n_entries);
229 void scale_array_elements_to_u64(u64_dbl_t *entries, int n_entries,
230 uint64_t *total_out);
231 #endif
233 #endif