1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2016, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Header file for directory.c.
12 #ifndef TOR_DIRECTORY_H
13 #define TOR_DIRECTORY_H
15 int directories_have_accepted_server_descriptor(void);
16 void directory_post_to_dirservers(uint8_t dir_purpose
, uint8_t router_purpose
,
17 dirinfo_type_t type
, const char *payload
,
18 size_t payload_len
, size_t extrainfo_len
);
19 MOCK_DECL(void, directory_get_from_dirserver
, (
21 uint8_t router_purpose
,
24 download_want_authority_t want_authority
));
25 void directory_get_from_all_authorities(uint8_t dir_purpose
,
26 uint8_t router_purpose
,
27 const char *resource
);
29 /** Enumeration of ways to connect to a directory server */
31 /** Default: connect over a one-hop Tor circuit. Relays fall back to direct
32 * DirPort connections, clients, onion services, and bridges do not */
34 /** Connect over a multi-hop anonymizing Tor circuit */
36 /** Connect to the DirPort directly */
38 /** Connect over a multi-hop anonymizing Tor circuit to our dirport */
42 int directory_must_use_begindir(const or_options_t
*options
);
44 MOCK_DECL(void, directory_initiate_command_routerstatus
,
45 (const routerstatus_t
*status
,
47 uint8_t router_purpose
,
48 dir_indirection_t indirection
,
52 time_t if_modified_since
));
54 void directory_initiate_command_routerstatus_rend(const routerstatus_t
*status
,
56 uint8_t router_purpose
,
57 dir_indirection_t indirection
,
61 time_t if_modified_since
,
62 const rend_data_t
*rend_query
);
64 int parse_http_response(const char *headers
, int *code
, time_t *date
,
65 compress_method_t
*compression
, char **response
);
67 int connection_dir_is_encrypted(dir_connection_t
*conn
);
68 int connection_dir_reached_eof(dir_connection_t
*conn
);
69 int connection_dir_process_inbuf(dir_connection_t
*conn
);
70 int connection_dir_finished_flushing(dir_connection_t
*conn
);
71 int connection_dir_finished_connecting(dir_connection_t
*conn
);
72 void connection_dir_about_to_close(dir_connection_t
*dir_conn
);
73 void directory_initiate_command(const tor_addr_t
*or_addr
, uint16_t or_port
,
74 const tor_addr_t
*dir_addr
, uint16_t dir_port
,
76 uint8_t dir_purpose
, uint8_t router_purpose
,
77 dir_indirection_t indirection
,
79 const char *payload
, size_t payload_len
,
80 time_t if_modified_since
);
82 #define DSR_HEX (1<<0)
83 #define DSR_BASE64 (1<<1)
84 #define DSR_DIGEST256 (1<<2)
85 #define DSR_SORT_UNIQ (1<<3)
86 int dir_split_resource_into_fingerprints(const char *resource
,
87 smartlist_t
*fp_out
, int *compressed_out
,
90 int dir_split_resource_into_fingerprint_pairs(const char *res
,
91 smartlist_t
*pairs_out
);
92 char *directory_dump_request_log(void);
93 void note_request(const char *key
, size_t bytes
);
94 int router_supports_extrainfo(const char *identity_digest
, int is_authority
);
96 time_t download_status_increment_failure(download_status_t
*dls
,
97 int status_code
, const char *item
,
98 int server
, time_t now
);
99 time_t download_status_increment_attempt(download_status_t
*dls
,
100 const char *item
, time_t now
);
101 /** Increment the failure count of the download_status_t <b>dls</b>, with
102 * the optional status code <b>sc</b>. */
103 #define download_status_failed(dls, sc) \
104 download_status_increment_failure((dls), (sc), NULL, \
105 dir_server_mode(get_options()), \
108 void download_status_reset(download_status_t
*dls
);
109 static int download_status_is_ready(download_status_t
*dls
, time_t now
,
111 /** Return true iff, as of <b>now</b>, the resource tracked by <b>dls</b> is
112 * ready to get its download reattempted. */
114 download_status_is_ready(download_status_t
*dls
, time_t now
,
117 int under_failure_limit
= (dls
->n_download_failures
<= max_failures
118 && dls
->n_download_attempts
<= max_failures
);
119 return (under_failure_limit
&& dls
->next_attempt_at
<= now
);
122 static void download_status_mark_impossible(download_status_t
*dl
);
123 /** Mark <b>dl</b> as never downloadable. */
125 download_status_mark_impossible(download_status_t
*dl
)
127 dl
->n_download_failures
= IMPOSSIBLE_TO_DOWNLOAD
;
128 dl
->n_download_attempts
= IMPOSSIBLE_TO_DOWNLOAD
;
131 int download_status_get_n_failures(const download_status_t
*dls
);
132 int download_status_get_n_attempts(const download_status_t
*dls
);
133 time_t download_status_get_next_attempt_at(const download_status_t
*dls
);
135 #ifdef TOR_UNIT_TESTS
136 /* Used only by directory.c and test_dir.c */
138 STATIC
int parse_http_url(const char *headers
, char **url
);
139 STATIC
int purpose_needs_anonymity(uint8_t dir_purpose
,
140 uint8_t router_purpose
);
141 STATIC dirinfo_type_t
dir_fetch_type(int dir_purpose
, int router_purpose
,
142 const char *resource
);
143 STATIC
int directory_handle_command_get(dir_connection_t
*conn
,
145 const char *req_body
,
146 size_t req_body_len
);
147 STATIC
int download_status_schedule_get_delay(download_status_t
*dls
,
148 const smartlist_t
*schedule
,
149 int min_delay
, int max_delay
,
152 STATIC
char* authdir_type_to_string(dirinfo_type_t auth
);
153 STATIC
const char * dir_conn_purpose_to_string(int purpose
);
154 STATIC
int should_use_directory_guards(const or_options_t
*options
);
155 STATIC zlib_compression_level_t
choose_compression_level(ssize_t n_bytes
);
156 STATIC
const smartlist_t
*find_dl_schedule(download_status_t
*dls
,
157 const or_options_t
*options
);
158 STATIC
void find_dl_min_and_max_delay(download_status_t
*dls
,
159 const or_options_t
*options
,
161 STATIC
int next_random_exponential_delay(int delay
, int max_delay
);