directory.h 16 KB

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