directory.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. time_t download_status_get_next_attempt_at(const download_status_t *dls);
  124. /** Return true iff, as of <b>now</b>, the resource tracked by <b>dls</b> is
  125. * ready to get its download reattempted. */
  126. static inline int
  127. download_status_is_ready(download_status_t *dls, time_t now)
  128. {
  129. /* dls wasn't reset before it was used */
  130. if (dls->next_attempt_at == 0) {
  131. download_status_reset(dls);
  132. }
  133. return download_status_get_next_attempt_at(dls) <= now;
  134. }
  135. static void download_status_mark_impossible(download_status_t *dl);
  136. /** Mark <b>dl</b> as never downloadable. */
  137. static inline void
  138. download_status_mark_impossible(download_status_t *dl)
  139. {
  140. dl->n_download_failures = IMPOSSIBLE_TO_DOWNLOAD;
  141. dl->n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD;
  142. }
  143. int download_status_get_n_failures(const download_status_t *dls);
  144. int download_status_get_n_attempts(const download_status_t *dls);
  145. int purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose,
  146. const char *resource);
  147. #ifdef DIRECTORY_PRIVATE
  148. /** A structure to hold arguments passed into each directory response
  149. * handler */
  150. typedef struct response_handler_args_t {
  151. int status_code;
  152. const char *reason;
  153. const char *body;
  154. size_t body_len;
  155. const char *headers;
  156. } response_handler_args_t;
  157. struct directory_request_t {
  158. /**
  159. * These fields specify which directory we're contacting. Routerstatus,
  160. * if present, overrides the other fields.
  161. *
  162. * @{ */
  163. tor_addr_port_t or_addr_port;
  164. tor_addr_port_t dir_addr_port;
  165. char digest[DIGEST_LEN];
  166. const routerstatus_t *routerstatus;
  167. /** @} */
  168. /** One of DIR_PURPOSE_* other than DIR_PURPOSE_SERVER. Describes what
  169. * kind of operation we'll be doing (upload/download), and of what kind
  170. * of document. */
  171. uint8_t dir_purpose;
  172. /** One of ROUTER_PURPOSE_*; used for uploads and downloads of routerinfo
  173. * and extrainfo docs. */
  174. uint8_t router_purpose;
  175. /** Enum: determines whether to anonymize, and whether to use dirport or
  176. * orport. */
  177. dir_indirection_t indirection;
  178. /** Alias to the variable part of the URL for this request */
  179. const char *resource;
  180. /** Alias to the payload to upload (if any) */
  181. const char *payload;
  182. /** Number of bytes to upload from payload</b> */
  183. size_t payload_len;
  184. /** Value to send in an if-modified-since header, or 0 for none. */
  185. time_t if_modified_since;
  186. /** Hidden-service-specific information v2. */
  187. const rend_data_t *rend_query;
  188. /** Extra headers to append to the request */
  189. config_line_t *additional_headers;
  190. /** Hidden-service-specific information for v3+. */
  191. const hs_ident_dir_conn_t *hs_ident;
  192. /** Used internally to directory.c: gets informed when the attempt to
  193. * connect to the directory succeeds or fails, if that attempt bears on the
  194. * directory's usability as a directory guard. */
  195. struct circuit_guard_state_t *guard_state;
  196. };
  197. struct get_handler_args_t;
  198. STATIC int handle_get_hs_descriptor_v3(dir_connection_t *conn,
  199. const struct get_handler_args_t *args);
  200. STATIC int directory_handle_command(dir_connection_t *conn);
  201. STATIC char *accept_encoding_header(void);
  202. STATIC int allowed_anonymous_connection_compression_method(compress_method_t);
  203. STATIC void warn_disallowed_anonymous_compression_method(compress_method_t);
  204. STATIC int handle_response_fetch_hsdesc_v3(dir_connection_t *conn,
  205. const response_handler_args_t *args);
  206. STATIC int handle_response_fetch_microdesc(dir_connection_t *conn,
  207. const response_handler_args_t *args);
  208. STATIC int handle_response_fetch_consensus(dir_connection_t *conn,
  209. const response_handler_args_t *args);
  210. #endif /* defined(DIRECTORY_PRIVATE) */
  211. #ifdef TOR_UNIT_TESTS
  212. /* Used only by test_dir.c and test_hs_cache.c */
  213. STATIC int parse_http_url(const char *headers, char **url);
  214. STATIC dirinfo_type_t dir_fetch_type(int dir_purpose, int router_purpose,
  215. const char *resource);
  216. MOCK_DECL(STATIC int, directory_handle_command_get,(dir_connection_t *conn,
  217. const char *headers,
  218. const char *req_body,
  219. size_t req_body_len));
  220. MOCK_DECL(STATIC int, directory_handle_command_post,(dir_connection_t *conn,
  221. const char *headers,
  222. const char *body,
  223. size_t body_len));
  224. STATIC int download_status_schedule_get_delay(download_status_t *dls,
  225. int min_delay,
  226. time_t now);
  227. STATIC int handle_post_hs_descriptor(const char *url, const char *body);
  228. STATIC char* authdir_type_to_string(dirinfo_type_t auth);
  229. STATIC const char * dir_conn_purpose_to_string(int purpose);
  230. STATIC int should_use_directory_guards(const or_options_t *options);
  231. STATIC compression_level_t choose_compression_level(ssize_t n_bytes);
  232. STATIC const smartlist_t *find_dl_schedule(const download_status_t *dls,
  233. const or_options_t *options);
  234. STATIC int find_dl_min_delay(download_status_t *dls,
  235. const or_options_t *options);
  236. STATIC int next_random_exponential_delay(int delay,
  237. int base_delay);
  238. STATIC void next_random_exponential_delay_range(int *low_bound_out,
  239. int *high_bound_out,
  240. int delay,
  241. int base_delay);
  242. STATIC int parse_hs_version_from_post(const char *url, const char *prefix,
  243. const char **end_pos);
  244. STATIC unsigned parse_accept_encoding_header(const char *h);
  245. #endif /* defined(TOR_UNIT_TESTS) */
  246. #if defined(TOR_UNIT_TESTS) || defined(DIRECTORY_PRIVATE)
  247. /* Used only by directory.c and test_dir.c */
  248. /* no more than quadruple the previous delay (multiplier + 1) */
  249. #define DIR_DEFAULT_RANDOM_MULTIPLIER (3)
  250. /* no more than triple the previous delay */
  251. #define DIR_TEST_NET_RANDOM_MULTIPLIER (2)
  252. #endif /* defined(TOR_UNIT_TESTS) || defined(DIRECTORY_PRIVATE) */
  253. #endif /* !defined(TOR_DIRECTORY_H) */