directory.h 12 KB

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