directory.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. void directory_request_set_or_addr_port(directory_request_t *req,
  49. const tor_addr_port_t *p);
  50. void directory_request_set_dir_addr_port(directory_request_t *req,
  51. const tor_addr_port_t *p);
  52. void directory_request_set_directory_id_digest(directory_request_t *req,
  53. const char *digest);
  54. void directory_request_set_guard_state(directory_request_t *req,
  55. struct circuit_guard_state_t *state);
  56. void directory_request_set_router_purpose(directory_request_t *req,
  57. uint8_t router_purpose);
  58. void directory_request_set_indirection(directory_request_t *req,
  59. dir_indirection_t indirection);
  60. void directory_request_set_resource(directory_request_t *req,
  61. const char *resource);
  62. void directory_request_set_payload(directory_request_t *req,
  63. const char *payload,
  64. size_t payload_len);
  65. void directory_request_set_if_modified_since(directory_request_t *req,
  66. time_t if_modified_since);
  67. void directory_request_set_rend_query(directory_request_t *req,
  68. const rend_data_t *query);
  69. void directory_request_upload_set_hs_ident(directory_request_t *req,
  70. const hs_ident_dir_conn_t *ident);
  71. void directory_request_fetch_set_hs_ident(directory_request_t *req,
  72. const hs_ident_dir_conn_t *ident);
  73. void directory_request_set_routerstatus(directory_request_t *req,
  74. const routerstatus_t *rs);
  75. void directory_request_add_header(directory_request_t *req,
  76. const char *key,
  77. const char *val);
  78. MOCK_DECL(void, directory_initiate_request, (directory_request_t *request));
  79. int parse_http_response(const char *headers, int *code, time_t *date,
  80. compress_method_t *compression, char **response);
  81. int parse_http_command(const char *headers,
  82. char **command_out, char **url_out);
  83. char *http_get_header(const char *headers, const char *which);
  84. int connection_dir_is_encrypted(const dir_connection_t *conn);
  85. int connection_dir_reached_eof(dir_connection_t *conn);
  86. int connection_dir_process_inbuf(dir_connection_t *conn);
  87. int connection_dir_finished_flushing(dir_connection_t *conn);
  88. int connection_dir_finished_connecting(dir_connection_t *conn);
  89. void connection_dir_about_to_close(dir_connection_t *dir_conn);
  90. #define DSR_HEX (1<<0)
  91. #define DSR_BASE64 (1<<1)
  92. #define DSR_DIGEST256 (1<<2)
  93. #define DSR_SORT_UNIQ (1<<3)
  94. int dir_split_resource_into_fingerprints(const char *resource,
  95. smartlist_t *fp_out, int *compressed_out,
  96. int flags);
  97. enum dir_spool_source_t;
  98. int dir_split_resource_into_spoolable(const char *resource,
  99. enum dir_spool_source_t source,
  100. smartlist_t *spool_out,
  101. int *compressed_out,
  102. int flags);
  103. int dir_split_resource_into_fingerprint_pairs(const char *res,
  104. smartlist_t *pairs_out);
  105. char *directory_dump_request_log(void);
  106. void note_request(const char *key, size_t bytes);
  107. int router_supports_extrainfo(const char *identity_digest, int is_authority);
  108. time_t download_status_increment_failure(download_status_t *dls,
  109. int status_code, const char *item,
  110. int server, time_t now);
  111. time_t download_status_increment_attempt(download_status_t *dls,
  112. const char *item, time_t now);
  113. /** Increment the failure count of the download_status_t <b>dls</b>, with
  114. * the optional status code <b>sc</b>. */
  115. #define download_status_failed(dls, sc) \
  116. download_status_increment_failure((dls), (sc), NULL, \
  117. dir_server_mode(get_options()), \
  118. time(NULL))
  119. void download_status_reset(download_status_t *dls);
  120. static int download_status_is_ready(download_status_t *dls, time_t now,
  121. int max_failures);
  122. time_t download_status_get_next_attempt_at(const download_status_t *dls);
  123. /** Return true iff, as of <b>now</b>, the resource tracked by <b>dls</b> is
  124. * ready to get its download reattempted. */
  125. static inline int
  126. download_status_is_ready(download_status_t *dls, time_t now,
  127. int max_failures)
  128. {
  129. /* dls wasn't reset before it was used */
  130. if (dls->next_attempt_at == 0) {
  131. download_status_reset(dls);
  132. }
  133. if (dls->backoff == DL_SCHED_DETERMINISTIC) {
  134. /* Deterministic schedules can hit an endpoint; exponential backoff
  135. * schedules just wait longer and longer. */
  136. int under_failure_limit = (dls->n_download_failures <= max_failures
  137. && dls->n_download_attempts <= max_failures);
  138. if (!under_failure_limit)
  139. return 0;
  140. }
  141. return download_status_get_next_attempt_at(dls) <= now;
  142. }
  143. static void download_status_mark_impossible(download_status_t *dl);
  144. /** Mark <b>dl</b> as never downloadable. */
  145. static inline void
  146. download_status_mark_impossible(download_status_t *dl)
  147. {
  148. dl->n_download_failures = IMPOSSIBLE_TO_DOWNLOAD;
  149. dl->n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD;
  150. }
  151. int download_status_get_n_failures(const download_status_t *dls);
  152. int download_status_get_n_attempts(const download_status_t *dls);
  153. int purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose,
  154. const char *resource);
  155. #ifdef DIRECTORY_PRIVATE
  156. /** A structure to hold arguments passed into each directory response
  157. * handler */
  158. typedef struct response_handler_args_t {
  159. int status_code;
  160. const char *reason;
  161. const char *body;
  162. size_t body_len;
  163. const char *headers;
  164. } response_handler_args_t;
  165. struct get_handler_args_t;
  166. STATIC int handle_get_hs_descriptor_v3(dir_connection_t *conn,
  167. const struct get_handler_args_t *args);
  168. STATIC int directory_handle_command(dir_connection_t *conn);
  169. STATIC char *accept_encoding_header(void);
  170. STATIC int allowed_anonymous_connection_compression_method(compress_method_t);
  171. STATIC void warn_disallowed_anonymous_compression_method(compress_method_t);
  172. STATIC int handle_response_fetch_hsdesc_v3(dir_connection_t *conn,
  173. const response_handler_args_t *args);
  174. #endif
  175. #ifdef TOR_UNIT_TESTS
  176. /* Used only by test_dir.c and test_hs_cache.c */
  177. STATIC int parse_http_url(const char *headers, char **url);
  178. STATIC dirinfo_type_t dir_fetch_type(int dir_purpose, int router_purpose,
  179. const char *resource);
  180. MOCK_DECL(STATIC int, directory_handle_command_get,(dir_connection_t *conn,
  181. const char *headers,
  182. const char *req_body,
  183. size_t req_body_len));
  184. MOCK_DECL(STATIC int, directory_handle_command_post,(dir_connection_t *conn,
  185. const char *headers,
  186. const char *body,
  187. size_t body_len));
  188. STATIC int download_status_schedule_get_delay(download_status_t *dls,
  189. const smartlist_t *schedule,
  190. int min_delay, int max_delay,
  191. time_t now);
  192. STATIC int handle_post_hs_descriptor(const char *url, const char *body);
  193. STATIC char* authdir_type_to_string(dirinfo_type_t auth);
  194. STATIC const char * dir_conn_purpose_to_string(int purpose);
  195. STATIC int should_use_directory_guards(const or_options_t *options);
  196. STATIC compression_level_t choose_compression_level(ssize_t n_bytes);
  197. STATIC const smartlist_t *find_dl_schedule(const download_status_t *dls,
  198. const or_options_t *options);
  199. STATIC void find_dl_min_and_max_delay(download_status_t *dls,
  200. const or_options_t *options,
  201. int *min, int *max);
  202. STATIC int next_random_exponential_delay(int delay, int max_delay);
  203. STATIC int parse_hs_version_from_post(const char *url, const char *prefix,
  204. const char **end_pos);
  205. STATIC unsigned parse_accept_encoding_header(const char *h);
  206. #endif
  207. #if defined(TOR_UNIT_TESTS) || defined(DIRECTORY_PRIVATE)
  208. /* Used only by directory.c and test_dir.c */
  209. /* no more than quadruple the previous delay (multiplier + 1) */
  210. #define DIR_DEFAULT_RANDOM_MULTIPLIER (3)
  211. /* no more than triple the previous delay */
  212. #define DIR_TEST_NET_RANDOM_MULTIPLIER (2)
  213. #endif
  214. #endif