directory.h 12 KB

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