directory.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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-2016, 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. int directories_have_accepted_server_descriptor(void);
  13. void directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose,
  14. dirinfo_type_t type, const char *payload,
  15. size_t payload_len, size_t extrainfo_len);
  16. MOCK_DECL(void, directory_get_from_dirserver, (
  17. uint8_t dir_purpose,
  18. uint8_t router_purpose,
  19. const char *resource,
  20. int pds_flags,
  21. download_want_authority_t want_authority));
  22. void directory_get_from_all_authorities(uint8_t dir_purpose,
  23. uint8_t router_purpose,
  24. const char *resource);
  25. /** Enumeration of ways to connect to a directory server */
  26. typedef enum {
  27. /** Default: connect over a one-hop Tor circuit. Relays fall back to direct
  28. * DirPort connections, clients, onion services, and bridges do not */
  29. DIRIND_ONEHOP=0,
  30. /** Connect over a multi-hop anonymizing Tor circuit */
  31. DIRIND_ANONYMOUS=1,
  32. /** Connect to the DirPort directly */
  33. DIRIND_DIRECT_CONN,
  34. /** Connect over a multi-hop anonymizing Tor circuit to our dirport */
  35. DIRIND_ANON_DIRPORT,
  36. } dir_indirection_t;
  37. int directory_must_use_begindir(const or_options_t *options);
  38. MOCK_DECL(void, directory_initiate_command_routerstatus,
  39. (const routerstatus_t *status,
  40. uint8_t dir_purpose,
  41. uint8_t router_purpose,
  42. dir_indirection_t indirection,
  43. const char *resource,
  44. const char *payload,
  45. size_t payload_len,
  46. time_t if_modified_since));
  47. void directory_initiate_command_routerstatus_rend(const routerstatus_t *status,
  48. uint8_t dir_purpose,
  49. uint8_t router_purpose,
  50. dir_indirection_t indirection,
  51. const char *resource,
  52. const char *payload,
  53. size_t payload_len,
  54. time_t if_modified_since,
  55. const rend_data_t *rend_query);
  56. int parse_http_response(const char *headers, int *code, time_t *date,
  57. compress_method_t *compression, char **response);
  58. int connection_dir_is_encrypted(dir_connection_t *conn);
  59. int connection_dir_reached_eof(dir_connection_t *conn);
  60. int connection_dir_process_inbuf(dir_connection_t *conn);
  61. int connection_dir_finished_flushing(dir_connection_t *conn);
  62. int connection_dir_finished_connecting(dir_connection_t *conn);
  63. void connection_dir_about_to_close(dir_connection_t *dir_conn);
  64. void directory_initiate_command(const tor_addr_t *or_addr, uint16_t or_port,
  65. const tor_addr_t *dir_addr, uint16_t dir_port,
  66. const char *digest,
  67. uint8_t dir_purpose, uint8_t router_purpose,
  68. dir_indirection_t indirection,
  69. const char *resource,
  70. const char *payload, size_t payload_len,
  71. time_t if_modified_since);
  72. #define DSR_HEX (1<<0)
  73. #define DSR_BASE64 (1<<1)
  74. #define DSR_DIGEST256 (1<<2)
  75. #define DSR_SORT_UNIQ (1<<3)
  76. int dir_split_resource_into_fingerprints(const char *resource,
  77. smartlist_t *fp_out, int *compressed_out,
  78. int flags);
  79. int dir_split_resource_into_fingerprint_pairs(const char *res,
  80. smartlist_t *pairs_out);
  81. char *directory_dump_request_log(void);
  82. void note_request(const char *key, size_t bytes);
  83. int router_supports_extrainfo(const char *identity_digest, int is_authority);
  84. time_t download_status_increment_failure(download_status_t *dls,
  85. int status_code, const char *item,
  86. int server, time_t now);
  87. time_t download_status_increment_attempt(download_status_t *dls,
  88. const char *item, time_t now);
  89. /** Increment the failure count of the download_status_t <b>dls</b>, with
  90. * the optional status code <b>sc</b>. */
  91. #define download_status_failed(dls, sc) \
  92. download_status_increment_failure((dls), (sc), NULL, \
  93. dir_server_mode(get_options()), \
  94. time(NULL))
  95. void download_status_reset(download_status_t *dls);
  96. static int download_status_is_ready(download_status_t *dls, time_t now,
  97. int max_failures);
  98. /** Return true iff, as of <b>now</b>, the resource tracked by <b>dls</b> is
  99. * ready to get its download reattempted. */
  100. static inline int
  101. download_status_is_ready(download_status_t *dls, time_t now,
  102. int max_failures)
  103. {
  104. if (dls->backoff == DL_SCHED_DETERMINISTIC) {
  105. /* Deterministic schedules can hit an endpoint; exponential backoff
  106. * schedules just wait longer and longer. */
  107. int under_failure_limit = (dls->n_download_failures <= max_failures
  108. && dls->n_download_attempts <= max_failures);
  109. if (!under_failure_limit)
  110. return 0;
  111. }
  112. return dls->next_attempt_at <= now;
  113. }
  114. static void download_status_mark_impossible(download_status_t *dl);
  115. /** Mark <b>dl</b> as never downloadable. */
  116. static inline void
  117. download_status_mark_impossible(download_status_t *dl)
  118. {
  119. dl->n_download_failures = IMPOSSIBLE_TO_DOWNLOAD;
  120. dl->n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD;
  121. }
  122. int download_status_get_n_failures(const download_status_t *dls);
  123. int download_status_get_n_attempts(const download_status_t *dls);
  124. time_t download_status_get_next_attempt_at(const download_status_t *dls);
  125. /* Yes, these two functions are confusingly similar.
  126. * Let's sort that out in #20077. */
  127. int purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose);
  128. int is_sensitive_dir_purpose(uint8_t dir_purpose);
  129. #ifdef TOR_UNIT_TESTS
  130. /* Used only by directory.c and test_dir.c */
  131. STATIC int parse_http_url(const char *headers, char **url);
  132. STATIC dirinfo_type_t dir_fetch_type(int dir_purpose, int router_purpose,
  133. const char *resource);
  134. STATIC int directory_handle_command_get(dir_connection_t *conn,
  135. const char *headers,
  136. const char *req_body,
  137. size_t req_body_len);
  138. STATIC int download_status_schedule_get_delay(download_status_t *dls,
  139. const smartlist_t *schedule,
  140. int min_delay, int max_delay,
  141. time_t now);
  142. STATIC char* authdir_type_to_string(dirinfo_type_t auth);
  143. STATIC const char * dir_conn_purpose_to_string(int purpose);
  144. STATIC int should_use_directory_guards(const or_options_t *options);
  145. STATIC zlib_compression_level_t choose_compression_level(ssize_t n_bytes);
  146. STATIC const smartlist_t *find_dl_schedule(download_status_t *dls,
  147. const or_options_t *options);
  148. STATIC void find_dl_min_and_max_delay(download_status_t *dls,
  149. const or_options_t *options,
  150. int *min, int *max);
  151. STATIC int next_random_exponential_delay(int delay,
  152. int base_delay,
  153. int max_delay);
  154. STATIC void next_random_exponential_delay_range(int *low_bound_out,
  155. int *high_bound_out,
  156. int delay,
  157. int base_delay);
  158. #endif
  159. #endif