1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2013, 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 const routerinfo_t
*routerlist_find_my_routerinfo(void);
62 uint32_t router_get_advertised_bandwidth(const routerinfo_t
*router
);
63 uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t
*router
);
65 const node_t
*node_sl_choose_by_bandwidth(const smartlist_t
*sl
,
66 bandwidth_weight_rule_t rule
);
67 double frac_nodes_with_descriptors(const smartlist_t
*sl
,
68 bandwidth_weight_rule_t rule
);
70 const node_t
*router_choose_random_node(smartlist_t
*excludedsmartlist
,
71 struct routerset_t
*excludedset
,
72 router_crn_flags_t flags
);
74 int router_is_named(const routerinfo_t
*router
);
75 int router_digest_is_trusted_dir_type(const char *digest
,
77 #define router_digest_is_trusted_dir(d) \
78 router_digest_is_trusted_dir_type((d), NO_DIRINFO)
80 int router_addr_is_trusted_dir(uint32_t addr
);
81 int hexdigest_to_digest(const char *hexdigest
, char *digest
);
82 const routerinfo_t
*router_get_by_id_digest(const char *digest
);
83 routerinfo_t
*router_get_mutable_by_digest(const char *digest
);
84 signed_descriptor_t
*router_get_by_descriptor_digest(const char *digest
);
85 signed_descriptor_t
*router_get_by_extrainfo_digest(const char *digest
);
86 signed_descriptor_t
*extrainfo_get_by_descriptor_digest(const char *digest
);
87 const char *signed_descriptor_get_body(const signed_descriptor_t
*desc
);
88 const char *signed_descriptor_get_annotations(const signed_descriptor_t
*desc
);
89 routerlist_t
*router_get_routerlist(void);
90 void routerinfo_free(routerinfo_t
*router
);
91 void extrainfo_free(extrainfo_t
*extrainfo
);
92 void routerlist_free(routerlist_t
*rl
);
93 void dump_routerlist_mem_usage(int severity
);
94 void routerlist_remove(routerlist_t
*rl
, routerinfo_t
*ri
, int make_old
,
96 void routerlist_free_all(void);
97 void routerlist_reset_warnings(void);
99 static int WRA_WAS_ADDED(was_router_added_t s
);
100 static int WRA_WAS_OUTDATED(was_router_added_t s
);
101 static int WRA_WAS_REJECTED(was_router_added_t s
);
102 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
103 * was added. It might still be necessary to check whether the descriptor
104 * generator should be notified.
107 WRA_WAS_ADDED(was_router_added_t s
) {
108 return s
== ROUTER_ADDED_SUCCESSFULLY
|| s
== ROUTER_ADDED_NOTIFY_GENERATOR
;
110 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
111 * was not added because it was either:
112 * - not in the consensus
113 * - neither in the consensus nor in any networkstatus document
116 static INLINE
int WRA_WAS_OUTDATED(was_router_added_t s
)
118 return (s
== ROUTER_WAS_NOT_NEW
||
119 s
== ROUTER_NOT_IN_CONSENSUS
||
120 s
== ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS
);
122 /** Return true iff the outcome code in <b>s</b> indicates that the descriptor
123 * was flat-out rejected. */
124 static INLINE
int WRA_WAS_REJECTED(was_router_added_t s
)
126 return (s
== ROUTER_AUTHDIR_REJECTS
);
128 was_router_added_t
router_add_to_routerlist(routerinfo_t
*router
,
132 was_router_added_t
router_add_extrainfo_to_routerlist(
133 extrainfo_t
*ei
, const char **msg
,
134 int from_cache
, int from_fetch
);
135 void routerlist_descriptors_added(smartlist_t
*sl
, int from_cache
);
136 void routerlist_remove_old_routers(void);
137 int router_load_single_router(const char *s
, uint8_t purpose
, int cache
,
139 int router_load_routers_from_string(const char *s
, const char *eos
,
140 saved_location_t saved_location
,
141 smartlist_t
*requested_fingerprints
,
142 int descriptor_digests
,
143 const char *prepend_annotations
);
144 void router_load_extrainfo_from_string(const char *s
, const char *eos
,
145 saved_location_t saved_location
,
146 smartlist_t
*requested_fingerprints
,
147 int descriptor_digests
);
149 void routerlist_retry_directory_downloads(time_t now
);
151 int router_exit_policy_rejects_all(const routerinfo_t
*router
);
153 dir_server_t
*trusted_dir_server_new(const char *nickname
, const char *address
,
154 uint16_t dir_port
, uint16_t or_port
,
155 const char *digest
, const char *v3_auth_digest
,
156 dirinfo_type_t type
, double weight
);
157 dir_server_t
*fallback_dir_server_new(const tor_addr_t
*addr
,
158 uint16_t dir_port
, uint16_t or_port
,
159 const char *id_digest
, double weight
);
160 void dir_server_add(dir_server_t
*ent
);
162 void authority_cert_free(authority_cert_t
*cert
);
163 void clear_dir_servers(void);
164 void update_consensus_router_descriptor_downloads(time_t now
, int is_vote
,
165 networkstatus_t
*consensus
);
166 void update_router_descriptor_downloads(time_t now
);
167 void update_all_descriptor_downloads(time_t now
);
168 void update_extrainfo_downloads(time_t now
);
169 void router_reset_descriptor_download_failures(void);
170 int router_differences_are_cosmetic(const routerinfo_t
*r1
,
171 const routerinfo_t
*r2
);
172 int routerinfo_incompatible_with_extrainfo(const routerinfo_t
*ri
,
174 signed_descriptor_t
*sd
,
177 void routerlist_assert_ok(const routerlist_t
*rl
);
178 const char *esc_router_info(const routerinfo_t
*router
);
179 void routers_sort_by_identity(smartlist_t
*routers
);
181 void refresh_all_country_info(void);
183 int hid_serv_get_responsible_directories(smartlist_t
*responsible_dirs
,
185 int hid_serv_acting_as_directory(void);
186 int hid_serv_responsible_for_desc_id(const char *id
);
188 void list_pending_microdesc_downloads(digestmap_t
*result
);
189 void launch_descriptor_downloads(int purpose
,
190 smartlist_t
*downloadable
,
191 const routerstatus_t
*source
,
194 int hex_digest_nickname_decode(const char *hexdigest
,
196 char *nickname_qualifier_out
,
198 int hex_digest_nickname_matches(const char *hexdigest
,
199 const char *identity_digest
,
200 const char *nickname
, int is_named
);
202 #ifdef ROUTERLIST_PRIVATE
203 /** Helper type for choosing routers by bandwidth: contains a union of
204 * double and uint64_t. Before we call scale_array_elements_to_u64, it holds
205 * a double; after, it holds a uint64_t. */
206 typedef union u64_dbl_t
{
211 STATIC
int choose_array_element_by_weight(const u64_dbl_t
*entries
,
213 STATIC
void scale_array_elements_to_u64(u64_dbl_t
*entries
, int n_entries
,
214 uint64_t *total_out
);