dirserv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 dirserv.h
  8. * \brief Header file for dirserv.c.
  9. **/
  10. #ifndef TOR_DIRSERV_H
  11. #define TOR_DIRSERV_H
  12. struct ed25519_public_key_t;
  13. #include "lib/testsupport/testsupport.h"
  14. /** An enum to describe what format we're generating a routerstatus line in.
  15. */
  16. typedef enum {
  17. /** For use in a v2 opinion */
  18. NS_V2,
  19. /** For use in a consensus networkstatus document (ns flavor) */
  20. NS_V3_CONSENSUS,
  21. /** For use in a vote networkstatus document */
  22. NS_V3_VOTE,
  23. /** For passing to the controlport in response to a GETINFO request */
  24. NS_CONTROL_PORT,
  25. /** For use in a consensus networkstatus document (microdesc flavor) */
  26. NS_V3_CONSENSUS_MICRODESC
  27. } routerstatus_format_type_t;
  28. /** What fraction (1 over this number) of the relay ID space do we
  29. * (as a directory authority) launch connections to at each reachability
  30. * test? */
  31. #define REACHABILITY_MODULO_PER_TEST 128
  32. /** How often (in seconds) do we launch reachability tests? */
  33. #define REACHABILITY_TEST_INTERVAL 10
  34. /** How many seconds apart are the reachability tests for a given relay? */
  35. #define REACHABILITY_TEST_CYCLE_PERIOD \
  36. (REACHABILITY_TEST_INTERVAL*REACHABILITY_MODULO_PER_TEST)
  37. /** Maximum length of an exit policy summary. */
  38. #define MAX_EXITPOLICY_SUMMARY_LEN 1000
  39. /** Maximum allowable length of a version line in a networkstatus. */
  40. #define MAX_V_LINE_LEN 128
  41. /** Maximum allowable length of bandwidth headers in a bandwidth file */
  42. #define MAX_BW_FILE_HEADER_COUNT_IN_VOTE 50
  43. /** Terminatore that separates bandwidth file headers from bandwidth file
  44. * relay lines */
  45. #define BW_FILE_HEADERS_TERMINATOR "=====\n"
  46. /** Ways to convert a spoolable_resource_t to a bunch of bytes. */
  47. typedef enum dir_spool_source_t {
  48. DIR_SPOOL_SERVER_BY_DIGEST=1, DIR_SPOOL_SERVER_BY_FP,
  49. DIR_SPOOL_EXTRA_BY_DIGEST, DIR_SPOOL_EXTRA_BY_FP,
  50. DIR_SPOOL_MICRODESC,
  51. DIR_SPOOL_NETWORKSTATUS,
  52. DIR_SPOOL_CONSENSUS_CACHE_ENTRY,
  53. } dir_spool_source_t;
  54. #define dir_spool_source_bitfield_t ENUM_BF(dir_spool_source_t)
  55. /** Object to remember the identity of an object that we are spooling,
  56. * or about to spool, in response to a directory request.
  57. *
  58. * (Why do we spool? Because some directory responses are very large,
  59. * and we don't want to just shove the complete answer into the output
  60. * buffer: that would take a ridiculous amount of RAM.)
  61. *
  62. * If the spooled resource is relatively small (like microdescriptors,
  63. * descriptors, etc), we look them up by ID as needed, and add the whole
  64. * thing onto the output buffer at once. If the spooled reseource is
  65. * big (like networkstatus documents), we reference-count it, and add it
  66. * a few K at a time.
  67. */
  68. typedef struct spooled_resource_t {
  69. /**
  70. * If true, we add the entire object to the outbuf. If false,
  71. * we spool the object a few K at a time.
  72. */
  73. unsigned spool_eagerly : 1;
  74. /**
  75. * Tells us what kind of object to get, and how to look it up.
  76. */
  77. dir_spool_source_bitfield_t spool_source : 7;
  78. /**
  79. * Tells us the specific object to spool.
  80. */
  81. uint8_t digest[DIGEST256_LEN];
  82. /**
  83. * A large object that we're spooling. Holds a reference count. Only
  84. * used when spool_eagerly is false.
  85. */
  86. struct cached_dir_t *cached_dir_ref;
  87. /**
  88. * A different kind of large object that we might be spooling. Also
  89. * reference-counted. Also only used when spool_eagerly is false.
  90. */
  91. struct consensus_cache_entry_t *consensus_cache_entry;
  92. const uint8_t *cce_body;
  93. size_t cce_len;
  94. /**
  95. * The current offset into cached_dir or cce_body. Only used when
  96. * spool_eagerly is false */
  97. off_t cached_dir_offset;
  98. } spooled_resource_t;
  99. #ifdef DIRSERV_PRIVATE
  100. typedef struct measured_bw_line_t {
  101. char node_id[DIGEST_LEN];
  102. char node_hex[MAX_HEX_NICKNAME_LEN+1];
  103. long int bw_kb;
  104. } measured_bw_line_t;
  105. #endif /* defined(DIRSERV_PRIVATE) */
  106. int connection_dirserv_flushed_some(dir_connection_t *conn);
  107. int dirserv_add_own_fingerprint(crypto_pk_t *pk);
  108. int dirserv_load_fingerprint_file(void);
  109. void dirserv_free_fingerprint_list(void);
  110. enum was_router_added_t dirserv_add_multiple_descriptors(
  111. const char *desc, uint8_t purpose,
  112. const char *source,
  113. const char **msg);
  114. enum was_router_added_t dirserv_add_descriptor(routerinfo_t *ri,
  115. const char **msg,
  116. const char *source);
  117. void dirserv_set_router_is_running(routerinfo_t *router, time_t now);
  118. int list_server_status_v1(smartlist_t *routers, char **router_status_out,
  119. int for_controller);
  120. char *dirserv_get_flag_thresholds_line(void);
  121. void dirserv_compute_bridge_flag_thresholds(void);
  122. int directory_fetches_from_authorities(const or_options_t *options);
  123. int directory_fetches_dir_info_early(const or_options_t *options);
  124. int directory_fetches_dir_info_later(const or_options_t *options);
  125. int directory_caches_unknown_auth_certs(const or_options_t *options);
  126. int directory_caches_dir_info(const or_options_t *options);
  127. int directory_permits_begindir_requests(const or_options_t *options);
  128. int directory_too_idle_to_fetch_descriptors(const or_options_t *options,
  129. time_t now);
  130. cached_dir_t *dirserv_get_consensus(const char *flavor_name);
  131. void dirserv_set_cached_consensus_networkstatus(const char *consensus,
  132. const char *flavor_name,
  133. const common_digests_t *digests,
  134. const uint8_t *sha3_as_signed,
  135. time_t published);
  136. void dirserv_clear_old_networkstatuses(time_t cutoff);
  137. int dirserv_get_routerdesc_spool(smartlist_t *spools_out, const char *key,
  138. dir_spool_source_t source,
  139. int conn_is_encrypted,
  140. const char **msg_out);
  141. int dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
  142. const char **msg);
  143. void dirserv_orconn_tls_done(const tor_addr_t *addr,
  144. uint16_t or_port,
  145. const char *digest_rcvd,
  146. const struct ed25519_public_key_t *ed_id_rcvd);
  147. int dirserv_should_launch_reachability_test(const routerinfo_t *ri,
  148. const routerinfo_t *ri_old);
  149. void dirserv_single_reachability_test(time_t now, routerinfo_t *router);
  150. void dirserv_test_reachability(time_t now);
  151. int authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg,
  152. int complain,
  153. int *valid_out);
  154. uint32_t dirserv_router_get_status(const routerinfo_t *router,
  155. const char **msg,
  156. int severity);
  157. void dirserv_set_node_flags_from_authoritative_status(node_t *node,
  158. uint32_t authstatus);
  159. int dirserv_would_reject_router(const routerstatus_t *rs);
  160. char *routerstatus_format_entry(
  161. const routerstatus_t *rs,
  162. const char *version,
  163. const char *protocols,
  164. routerstatus_format_type_t format,
  165. int consensus_method,
  166. const vote_routerstatus_t *vrs);
  167. void dirserv_free_all(void);
  168. void cached_dir_decref(cached_dir_t *d);
  169. cached_dir_t *new_cached_dir(char *s, time_t published);
  170. struct config_line_t;
  171. char *format_recommended_version_list(const struct config_line_t *line,
  172. int warn);
  173. int validate_recommended_package_line(const char *line);
  174. int dirserv_query_measured_bw_cache_kb(const char *node_id,
  175. long *bw_out,
  176. time_t *as_of_out);
  177. void dirserv_clear_measured_bw_cache(void);
  178. int dirserv_has_measured_bw(const char *node_id);
  179. int dirserv_get_measured_bw_cache_size(void);
  180. void dirserv_count_measured_bws(const smartlist_t *routers);
  181. int running_long_enough_to_decide_unreachable(void);
  182. void dirserv_compute_performance_thresholds(digestmap_t *omit_as_sybil);
  183. #ifdef DIRSERV_PRIVATE
  184. STATIC void dirserv_set_routerstatus_testing(routerstatus_t *rs);
  185. /* Put the MAX_MEASUREMENT_AGE #define here so unit tests can see it */
  186. #define MAX_MEASUREMENT_AGE (3*24*60*60) /* 3 days */
  187. STATIC int measured_bw_line_parse(measured_bw_line_t *out, const char *line,
  188. int line_is_after_headers);
  189. STATIC int measured_bw_line_apply(measured_bw_line_t *parsed_line,
  190. smartlist_t *routerstatuses);
  191. STATIC void dirserv_cache_measured_bw(const measured_bw_line_t *parsed_line,
  192. time_t as_of);
  193. STATIC void dirserv_expire_measured_bw_cache(time_t now);
  194. STATIC int
  195. dirserv_read_guardfraction_file_from_str(const char *guardfraction_file_str,
  196. smartlist_t *vote_routerstatuses);
  197. #endif /* defined(DIRSERV_PRIVATE) */
  198. int dirserv_read_measured_bandwidths(const char *from_file,
  199. smartlist_t *routerstatuses,
  200. smartlist_t *bw_file_headers);
  201. int dirserv_read_guardfraction_file(const char *fname,
  202. smartlist_t *vote_routerstatuses);
  203. spooled_resource_t *spooled_resource_new(dir_spool_source_t source,
  204. const uint8_t *digest,
  205. size_t digestlen);
  206. spooled_resource_t *spooled_resource_new_from_cache_entry(
  207. struct consensus_cache_entry_t *entry);
  208. void spooled_resource_free_(spooled_resource_t *spooled);
  209. #define spooled_resource_free(sp) \
  210. FREE_AND_NULL(spooled_resource_t, spooled_resource_free_, (sp))
  211. void dirserv_spool_remove_missing_and_guess_size(dir_connection_t *conn,
  212. time_t cutoff,
  213. int compression,
  214. size_t *size_out,
  215. int *n_expired_out);
  216. void dirserv_spool_sort(dir_connection_t *conn);
  217. void dir_conn_clear_spool(dir_connection_t *conn);
  218. #endif /* !defined(TOR_DIRSERV_H) */