directory.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file directory.h
  8. * \brief Header file for directory.c.
  9. **/
  10. #ifndef TOR_DIRECTORY_H
  11. #define TOR_DIRECTORY_H
  12. #include "hs_ident.h"
  13. int directories_have_accepted_server_descriptor(void);
  14. void directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose,
  15. dirinfo_type_t type, const char *payload,
  16. size_t payload_len, size_t extrainfo_len);
  17. MOCK_DECL(void, directory_get_from_dirserver, (
  18. uint8_t dir_purpose,
  19. uint8_t router_purpose,
  20. const char *resource,
  21. int pds_flags,
  22. download_want_authority_t want_authority));
  23. void directory_get_from_all_authorities(uint8_t dir_purpose,
  24. uint8_t router_purpose,
  25. const char *resource);
  26. /** Enumeration of ways to connect to a directory server */
  27. typedef enum {
  28. /** Default: connect over a one-hop Tor circuit. Relays fall back to direct
  29. * DirPort connections, clients, onion services, and bridges do not */
  30. DIRIND_ONEHOP=0,
  31. /** Connect over a multi-hop anonymizing Tor circuit */
  32. DIRIND_ANONYMOUS=1,
  33. /** Connect to the DirPort directly */
  34. DIRIND_DIRECT_CONN,
  35. /** Connect over a multi-hop anonymizing Tor circuit to our dirport */
  36. DIRIND_ANON_DIRPORT,
  37. } dir_indirection_t;
  38. int directory_must_use_begindir(const or_options_t *options);
  39. /**
  40. * A directory_request_t describes the information about a directory request
  41. * at the client side. It describes what we're going to ask for, which
  42. * directory we're going to ask for it, how we're going to contact that
  43. * directory, and (in some cases) what to do with it when we're done.
  44. */
  45. typedef struct directory_request_t directory_request_t;
  46. directory_request_t *directory_request_new(uint8_t dir_purpose);
  47. void directory_request_free_(directory_request_t *req);
  48. #define directory_request_free(req) \
  49. FREE_AND_NULL(directory_request_t, directory_request_free_, (req))
  50. void directory_request_set_or_addr_port(directory_request_t *req,
  51. const tor_addr_port_t *p);
  52. void directory_request_set_dir_addr_port(directory_request_t *req,
  53. const tor_addr_port_t *p);
  54. void directory_request_set_directory_id_digest(directory_request_t *req,
  55. const char *digest);
  56. void directory_request_set_guard_state(directory_request_t *req,
  57. struct circuit_guard_state_t *state);
  58. void directory_request_set_router_purpose(directory_request_t *req,
  59. uint8_t router_purpose);
  60. void directory_request_set_indirection(directory_request_t *req,
  61. dir_indirection_t indirection);
  62. void directory_request_set_resource(directory_request_t *req,
  63. const char *resource);
  64. void directory_request_set_payload(directory_request_t *req,
  65. const char *payload,
  66. size_t payload_len);
  67. void directory_request_set_if_modified_since(directory_request_t *req,
  68. time_t if_modified_since);
  69. void directory_request_set_rend_query(directory_request_t *req,
  70. const rend_data_t *query);
  71. void directory_request_upload_set_hs_ident(directory_request_t *req,
  72. const hs_ident_dir_conn_t *ident);
  73. void directory_request_fetch_set_hs_ident(directory_request_t *req,
  74. const hs_ident_dir_conn_t *ident);
  75. void directory_request_set_routerstatus(directory_request_t *req,
  76. const routerstatus_t *rs);
  77. void directory_request_add_header(directory_request_t *req,
  78. const char *key,
  79. const char *val);
  80. MOCK_DECL(void, directory_initiate_request, (directory_request_t *request));
  81. int parse_http_response(const char *headers, int *code, time_t *date,
  82. compress_method_t *compression, char **response);
  83. int parse_http_command(const char *headers,
  84. char **command_out, char **url_out);
  85. char *http_get_header(const char *headers, const char *which);
  86. int connection_dir_is_encrypted(const dir_connection_t *conn);
  87. int connection_dir_reached_eof(dir_connection_t *conn);
  88. int connection_dir_process_inbuf(dir_connection_t *conn);
  89. int connection_dir_finished_flushing(dir_connection_t *conn);
  90. int connection_dir_finished_connecting(dir_connection_t *conn);
  91. void connection_dir_about_to_close(dir_connection_t *dir_conn);
  92. #define DSR_HEX (1<<0)
  93. #define DSR_BASE64 (1<<1)
  94. #define DSR_DIGEST256 (1<<2)
  95. #define DSR_SORT_UNIQ (1<<3)
  96. int dir_split_resource_into_fingerprints(const char *resource,
  97. smartlist_t *fp_out, int *compressed_out,
  98. int flags);
  99. enum dir_spool_source_t;
  100. int dir_split_resource_into_spoolable(const char *resource,
  101. enum dir_spool_source_t source,
  102. smartlist_t *spool_out,
  103. int *compressed_out,
  104. int flags);
  105. int dir_split_resource_into_fingerprint_pairs(const char *res,
  106. smartlist_t *pairs_out);
  107. char *directory_dump_request_log(void);
  108. void note_request(const char *key, size_t bytes);
  109. int router_supports_extrainfo(const char *identity_digest, int is_authority);
  110. time_t download_status_increment_failure(download_status_t *dls,
  111. int status_code, const char *item,
  112. int server, time_t now);
  113. time_t download_status_increment_attempt(download_status_t *dls,
  114. const char *item, time_t now);
  115. /** Increment the failure count of the download_status_t <b>dls</b>, with
  116. * the optional status code <b>sc</b>. */
  117. #define download_status_failed(dls, sc) \
  118. download_status_increment_failure((dls), (sc), NULL, \
  119. dir_server_mode(get_options()), \
  120. time(NULL))
  121. void download_status_reset(download_status_t *dls);
  122. static int download_status_is_ready(download_status_t *dls, time_t now,
  123. int max_failures);
  124. time_t download_status_get_next_attempt_at(const download_status_t *dls);
  125. /** Return true iff, as of <b>now</b>, the resource tracked by <b>dls</b> is
  126. * ready to get its download reattempted. */
  127. static inline int
  128. download_status_is_ready(download_status_t *dls, time_t now,
  129. int max_failures)
  130. {
  131. /* dls wasn't reset before it was used */
  132. if (dls->next_attempt_at == 0) {
  133. download_status_reset(dls);
  134. }
  135. if (dls->backoff == DL_SCHED_DETERMINISTIC) {
  136. /* Deterministic schedules can hit an endpoint; exponential backoff
  137. * schedules just wait longer and longer. */
  138. int under_failure_limit = (dls->n_download_failures <= max_failures
  139. && dls->n_download_attempts <= max_failures);
  140. if (!under_failure_limit)
  141. return 0;
  142. }
  143. return download_status_get_next_attempt_at(dls) <= now;
  144. }
  145. static void download_status_mark_impossible(download_status_t *dl);
  146. /** Mark <b>dl</b> as never downloadable. */
  147. static inline void
  148. download_status_mark_impossible(download_status_t *dl)
  149. {
  150. dl->n_download_failures = IMPOSSIBLE_TO_DOWNLOAD;
  151. dl->n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD;
  152. }
  153. int download_status_get_n_failures(const download_status_t *dls);
  154. int download_status_get_n_attempts(const download_status_t *dls);
  155. int purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose,
  156. const char *resource);
  157. #ifdef DIRECTORY_PRIVATE
  158. /** A structure to hold arguments passed into each directory response
  159. * handler */
  160. typedef struct response_handler_args_t {
  161. int status_code;
  162. const char *reason;
  163. const char *body;
  164. size_t body_len;
  165. const char *headers;
  166. } response_handler_args_t;
  167. struct directory_request_t {
  168. /**
  169. * These fields specify which directory we're contacting. Routerstatus,
  170. * if present, overrides the other fields.
  171. *
  172. * @{ */
  173. tor_addr_port_t or_addr_port;
  174. tor_addr_port_t dir_addr_port;
  175. char digest[DIGEST_LEN];
  176. const routerstatus_t *routerstatus;
  177. /** @} */
  178. /** One of DIR_PURPOSE_* other than DIR_PURPOSE_SERVER. Describes what
  179. * kind of operation we'll be doing (upload/download), and of what kind
  180. * of document. */
  181. uint8_t dir_purpose;
  182. /** One of ROUTER_PURPOSE_*; used for uploads and downloads of routerinfo
  183. * and extrainfo docs. */
  184. uint8_t router_purpose;
  185. /** Enum: determines whether to anonymize, and whether to use dirport or
  186. * orport. */
  187. dir_indirection_t indirection;
  188. /** Alias to the variable part of the URL for this request */
  189. const char *resource;
  190. /** Alias to the payload to upload (if any) */
  191. const char *payload;
  192. /** Number of bytes to upload from payload</b> */
  193. size_t payload_len;
  194. /** Value to send in an if-modified-since header, or 0 for none. */
  195. time_t if_modified_since;
  196. /** Hidden-service-specific information v2. */
  197. const rend_data_t *rend_query;
  198. /** Extra headers to append to the request */
  199. config_line_t *additional_headers;
  200. /** Hidden-service-specific information for v3+. */
  201. const hs_ident_dir_conn_t *hs_ident;
  202. /** Used internally to directory.c: gets informed when the attempt to
  203. * connect to the directory succeeds or fails, if that attempt bears on the
  204. * directory's usability as a directory guard. */
  205. struct circuit_guard_state_t *guard_state;
  206. };
  207. struct get_handler_args_t;
  208. STATIC int handle_get_hs_descriptor_v3(dir_connection_t *conn,
  209. const struct get_handler_args_t *args);
  210. STATIC int directory_handle_command(dir_connection_t *conn);
  211. STATIC char *accept_encoding_header(void);
  212. STATIC int allowed_anonymous_connection_compression_method(compress_method_t);
  213. STATIC void warn_disallowed_anonymous_compression_method(compress_method_t);
  214. STATIC int handle_response_fetch_hsdesc_v3(dir_connection_t *conn,
  215. const response_handler_args_t *args);
  216. STATIC int handle_response_fetch_microdesc(dir_connection_t *conn,
  217. const response_handler_args_t *args);
  218. STATIC int handle_response_fetch_consensus(dir_connection_t *conn,
  219. const response_handler_args_t *args);
  220. #endif /* defined(DIRECTORY_PRIVATE) */
  221. #ifdef TOR_UNIT_TESTS
  222. /* Used only by test_dir.c and test_hs_cache.c */
  223. STATIC int parse_http_url(const char *headers, char **url);
  224. STATIC dirinfo_type_t dir_fetch_type(int dir_purpose, int router_purpose,
  225. const char *resource);
  226. MOCK_DECL(STATIC int, directory_handle_command_get,(dir_connection_t *conn,
  227. const char *headers,
  228. const char *req_body,
  229. size_t req_body_len));
  230. MOCK_DECL(STATIC int, directory_handle_command_post,(dir_connection_t *conn,
  231. const char *headers,
  232. const char *body,
  233. size_t body_len));
  234. STATIC int download_status_schedule_get_delay(download_status_t *dls,
  235. const smartlist_t *schedule,
  236. int min_delay, int max_delay,
  237. time_t now);
  238. STATIC int handle_post_hs_descriptor(const char *url, const char *body);
  239. STATIC char* authdir_type_to_string(dirinfo_type_t auth);
  240. STATIC const char * dir_conn_purpose_to_string(int purpose);
  241. STATIC int should_use_directory_guards(const or_options_t *options);
  242. STATIC compression_level_t choose_compression_level(ssize_t n_bytes);
  243. STATIC const smartlist_t *find_dl_schedule(const download_status_t *dls,
  244. const or_options_t *options);
  245. STATIC void find_dl_min_and_max_delay(download_status_t *dls,
  246. const or_options_t *options,
  247. int *min, int *max);
  248. STATIC int next_random_exponential_delay(int delay,
  249. int base_delay,
  250. int max_delay);
  251. STATIC void next_random_exponential_delay_range(int *low_bound_out,
  252. int *high_bound_out,
  253. int delay,
  254. int base_delay);
  255. STATIC int parse_hs_version_from_post(const char *url, const char *prefix,
  256. const char **end_pos);
  257. STATIC unsigned parse_accept_encoding_header(const char *h);
  258. #endif /* defined(TOR_UNIT_TESTS) */
  259. #if defined(TOR_UNIT_TESTS) || defined(DIRECTORY_PRIVATE)
  260. /* Used only by directory.c and test_dir.c */
  261. /* no more than quadruple the previous delay (multiplier + 1) */
  262. #define DIR_DEFAULT_RANDOM_MULTIPLIER (3)
  263. /* no more than triple the previous delay */
  264. #define DIR_TEST_NET_RANDOM_MULTIPLIER (2)
  265. #endif /* defined(TOR_UNIT_TESTS) || defined(DIRECTORY_PRIVATE) */
  266. #endif /* !defined(TOR_DIRECTORY_H) */