control.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 control.h
  8. * \brief Header file for control.c.
  9. **/
  10. #ifndef TOR_CONTROL_H
  11. #define TOR_CONTROL_H
  12. control_connection_t *TO_CONTROL_CONN(connection_t *);
  13. void control_initialize_event_queue(void);
  14. void control_update_global_event_mask(void);
  15. void control_adjust_event_log_severity(void);
  16. void control_ports_write_to_file(void);
  17. /** Log information about the connection <b>conn</b>, protecting it as with
  18. * CONN_LOG_PROTECT. Example:
  19. *
  20. * LOG_FN_CONN(conn, (LOG_DEBUG, "Socket %d wants to write", conn->s));
  21. **/
  22. #define LOG_FN_CONN(conn, args) \
  23. CONN_LOG_PROTECT(conn, log_fn args)
  24. #define CC_LOCAL_FD_IS_OWNER (1u<<0)
  25. #define CC_LOCAL_FD_IS_AUTHENTICATED (1u<<1)
  26. int control_connection_add_local_fd(tor_socket_t sock, unsigned flags);
  27. int connection_control_finished_flushing(control_connection_t *conn);
  28. int connection_control_reached_eof(control_connection_t *conn);
  29. void connection_control_closed(control_connection_t *conn);
  30. int connection_control_process_inbuf(control_connection_t *conn);
  31. #define EVENT_NS 0x000F
  32. int control_event_is_interesting(int event);
  33. void control_per_second_events(void);
  34. int control_any_per_second_event_enabled(void);
  35. int control_event_circuit_status(origin_circuit_t *circ,
  36. circuit_status_event_t e, int reason);
  37. int control_event_circuit_purpose_changed(origin_circuit_t *circ,
  38. int old_purpose);
  39. int control_event_circuit_cannibalized(origin_circuit_t *circ,
  40. int old_purpose,
  41. const struct timeval *old_tv_created);
  42. int control_event_stream_status(entry_connection_t *conn,
  43. stream_status_event_t e,
  44. int reason);
  45. int control_event_or_conn_status(or_connection_t *conn,
  46. or_conn_status_event_t e, int reason);
  47. int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
  48. int control_event_stream_bandwidth(edge_connection_t *edge_conn);
  49. int control_event_stream_bandwidth_used(void);
  50. int control_event_circ_bandwidth_used(void);
  51. int control_event_conn_bandwidth(connection_t *conn);
  52. int control_event_conn_bandwidth_used(void);
  53. int control_event_circuit_cell_stats(void);
  54. void control_event_logmsg(int severity, uint32_t domain, const char *msg);
  55. void control_event_logmsg_pending(void);
  56. int control_event_descriptors_changed(smartlist_t *routers);
  57. int control_event_address_mapped(const char *from, const char *to,
  58. time_t expires, const char *error,
  59. const int cached);
  60. int control_event_my_descriptor_changed(void);
  61. int control_event_network_liveness_update(int liveness);
  62. int control_event_networkstatus_changed(smartlist_t *statuses);
  63. int control_event_newconsensus(const networkstatus_t *consensus);
  64. int control_event_networkstatus_changed_single(const routerstatus_t *rs);
  65. int control_event_general_status(int severity, const char *format, ...)
  66. CHECK_PRINTF(2,3);
  67. int control_event_client_status(int severity, const char *format, ...)
  68. CHECK_PRINTF(2,3);
  69. int control_event_server_status(int severity, const char *format, ...)
  70. CHECK_PRINTF(2,3);
  71. int control_event_general_error(const char *format, ...)
  72. CHECK_PRINTF(1,2);
  73. int control_event_client_error(const char *format, ...)
  74. CHECK_PRINTF(1,2);
  75. int control_event_server_error(const char *format, ...)
  76. CHECK_PRINTF(1,2);
  77. int control_event_guard(const char *nickname, const char *digest,
  78. const char *status);
  79. int control_event_conf_changed(const smartlist_t *elements);
  80. int control_event_buildtimeout_set(buildtimeout_set_event_t type,
  81. const char *args);
  82. int control_event_signal(uintptr_t signal);
  83. int init_control_cookie_authentication(int enabled);
  84. char *get_controller_cookie_file_name(void);
  85. smartlist_t *decode_hashed_passwords(config_line_t *passwords);
  86. void disable_control_logging(void);
  87. void enable_control_logging(void);
  88. void monitor_owning_controller_process(const char *process_spec);
  89. int control_event_bootstrap(bootstrap_status_t status, int progress);
  90. MOCK_DECL(void, control_event_bootstrap_prob_or,(const char *warn,
  91. int reason,
  92. or_connection_t *or_conn));
  93. void control_event_bootstrap_problem(const char *warn, const char *reason,
  94. const connection_t *conn, int dowarn);
  95. void control_event_clients_seen(const char *controller_str);
  96. void control_event_transport_launched(const char *mode,
  97. const char *transport_name,
  98. tor_addr_t *addr, uint16_t port);
  99. const char *rend_auth_type_to_string(rend_auth_type_t auth_type);
  100. MOCK_DECL(const char *, node_describe_longname_by_id,(const char *id_digest));
  101. void control_event_hs_descriptor_requested(const char *onion_address,
  102. rend_auth_type_t auth_type,
  103. const char *id_digest,
  104. const char *desc_id,
  105. const char *hsdir_index);
  106. void control_event_hs_descriptor_created(const char *onion_address,
  107. const char *desc_id,
  108. int replica);
  109. void control_event_hs_descriptor_upload(const char *onion_address,
  110. const char *desc_id,
  111. const char *hs_dir,
  112. const char *hsdir_index);
  113. void control_event_hs_descriptor_upload_end(const char *action,
  114. const char *onion_address,
  115. const char *hs_dir,
  116. const char *reason);
  117. void control_event_hs_descriptor_uploaded(const char *hs_dir,
  118. const char *onion_address);
  119. /* Hidden service v2 HS_DESC specific. */
  120. void control_event_hsv2_descriptor_failed(const rend_data_t *rend_data,
  121. const char *id_digest,
  122. const char *reason);
  123. void control_event_hsv2_descriptor_received(const char *onion_address,
  124. const rend_data_t *rend_data,
  125. const char *id_digest);
  126. /* Hidden service v3 HS_DESC specific. */
  127. void control_event_hsv3_descriptor_failed(const char *onion_address,
  128. const char *desc_id,
  129. const char *hsdir_id_digest,
  130. const char *reason);
  131. void control_event_hsv3_descriptor_received(const char *onion_address,
  132. const char *desc_id,
  133. const char *hsdir_id_digest);
  134. void control_event_hs_descriptor_upload_failed(const char *hs_dir,
  135. const char *onion_address,
  136. const char *reason);
  137. void control_event_hs_descriptor_content(const char *onion_address,
  138. const char *desc_id,
  139. const char *hsdir_fp,
  140. const char *content);
  141. void control_free_all(void);
  142. #ifdef CONTROL_PRIVATE
  143. /* Recognized asynchronous event types. It's okay to expand this list
  144. * because it is used both as a list of v0 event types, and as indices
  145. * into the bitfield to determine which controllers want which events.
  146. */
  147. /* This bitfield has no event zero 0x0000 */
  148. #define EVENT_MIN_ 0x0001
  149. #define EVENT_CIRCUIT_STATUS 0x0001
  150. #define EVENT_STREAM_STATUS 0x0002
  151. #define EVENT_OR_CONN_STATUS 0x0003
  152. #define EVENT_BANDWIDTH_USED 0x0004
  153. #define EVENT_CIRCUIT_STATUS_MINOR 0x0005
  154. #define EVENT_NEW_DESC 0x0006
  155. #define EVENT_DEBUG_MSG 0x0007
  156. #define EVENT_INFO_MSG 0x0008
  157. #define EVENT_NOTICE_MSG 0x0009
  158. #define EVENT_WARN_MSG 0x000A
  159. #define EVENT_ERR_MSG 0x000B
  160. #define EVENT_ADDRMAP 0x000C
  161. /* There was an AUTHDIR_NEWDESCS event, but it no longer exists. We
  162. can reclaim 0x000D. */
  163. #define EVENT_DESCCHANGED 0x000E
  164. /* Exposed above */
  165. // #define EVENT_NS 0x000F
  166. #define EVENT_STATUS_CLIENT 0x0010
  167. #define EVENT_STATUS_SERVER 0x0011
  168. #define EVENT_STATUS_GENERAL 0x0012
  169. #define EVENT_GUARD 0x0013
  170. #define EVENT_STREAM_BANDWIDTH_USED 0x0014
  171. #define EVENT_CLIENTS_SEEN 0x0015
  172. #define EVENT_NEWCONSENSUS 0x0016
  173. #define EVENT_BUILDTIMEOUT_SET 0x0017
  174. #define EVENT_GOT_SIGNAL 0x0018
  175. #define EVENT_CONF_CHANGED 0x0019
  176. #define EVENT_CONN_BW 0x001A
  177. #define EVENT_CELL_STATS 0x001B
  178. /* UNUSED : 0x001C */
  179. #define EVENT_CIRC_BANDWIDTH_USED 0x001D
  180. #define EVENT_TRANSPORT_LAUNCHED 0x0020
  181. #define EVENT_HS_DESC 0x0021
  182. #define EVENT_HS_DESC_CONTENT 0x0022
  183. #define EVENT_NETWORK_LIVENESS 0x0023
  184. #define EVENT_MAX_ 0x0023
  185. /* sizeof(control_connection_t.event_mask) in bits, currently a uint64_t */
  186. #define EVENT_CAPACITY_ 0x0040
  187. /* If EVENT_MAX_ ever hits 0x0040, we need to make the mask into a
  188. * different structure, as it can only handle a maximum left shift of 1<<63. */
  189. #if EVENT_MAX_ >= EVENT_CAPACITY_
  190. #error control_connection_t.event_mask has an event greater than its capacity
  191. #endif
  192. #define EVENT_MASK_(e) (((uint64_t)1)<<(e))
  193. #define EVENT_MASK_NONE_ ((uint64_t)0x0)
  194. #define EVENT_MASK_ABOVE_MIN_ ((~((uint64_t)0x0)) << EVENT_MIN_)
  195. #define EVENT_MASK_BELOW_MAX_ ((~((uint64_t)0x0)) \
  196. >> (EVENT_CAPACITY_ - EVENT_MAX_ \
  197. - EVENT_MIN_))
  198. #define EVENT_MASK_ALL_ (EVENT_MASK_ABOVE_MIN_ \
  199. & EVENT_MASK_BELOW_MAX_)
  200. /* Used only by control.c and test.c */
  201. STATIC size_t write_escaped_data(const char *data, size_t len, char **out);
  202. STATIC size_t read_escaped_data(const char *data, size_t len, char **out);
  203. #ifdef TOR_UNIT_TESTS
  204. MOCK_DECL(STATIC void,
  205. send_control_event_string,(uint16_t event, const char *msg));
  206. MOCK_DECL(STATIC void,
  207. queue_control_event_string,(uint16_t event, char *msg));
  208. void control_testing_set_global_event_mask(uint64_t mask);
  209. #endif /* defined(TOR_UNIT_TESTS) */
  210. /** Helper structure: temporarily stores cell statistics for a circuit. */
  211. typedef struct cell_stats_t {
  212. /** Number of cells added in app-ward direction by command. */
  213. uint64_t added_cells_appward[CELL_COMMAND_MAX_ + 1];
  214. /** Number of cells added in exit-ward direction by command. */
  215. uint64_t added_cells_exitward[CELL_COMMAND_MAX_ + 1];
  216. /** Number of cells removed in app-ward direction by command. */
  217. uint64_t removed_cells_appward[CELL_COMMAND_MAX_ + 1];
  218. /** Number of cells removed in exit-ward direction by command. */
  219. uint64_t removed_cells_exitward[CELL_COMMAND_MAX_ + 1];
  220. /** Total waiting time of cells in app-ward direction by command. */
  221. uint64_t total_time_appward[CELL_COMMAND_MAX_ + 1];
  222. /** Total waiting time of cells in exit-ward direction by command. */
  223. uint64_t total_time_exitward[CELL_COMMAND_MAX_ + 1];
  224. } cell_stats_t;
  225. void sum_up_cell_stats_by_command(circuit_t *circ,
  226. cell_stats_t *cell_stats);
  227. void append_cell_stats_by_command(smartlist_t *event_parts,
  228. const char *key,
  229. const uint64_t *include_if_non_zero,
  230. const uint64_t *number_to_include);
  231. void format_cell_stats(char **event_string, circuit_t *circ,
  232. cell_stats_t *cell_stats);
  233. STATIC char *get_bw_samples(void);
  234. /* ADD_ONION secret key to create an ephemeral service. The command supports
  235. * multiple versions so this union stores the key and passes it to the HS
  236. * subsystem depending on the requested version. */
  237. typedef union add_onion_secret_key_t {
  238. /* Hidden service v2 secret key. */
  239. crypto_pk_t *v2;
  240. /* Hidden service v3 secret key. */
  241. ed25519_secret_key_t *v3;
  242. } add_onion_secret_key_t;
  243. STATIC int add_onion_helper_keyarg(const char *arg, int discard_pk,
  244. const char **key_new_alg_out,
  245. char **key_new_blob_out,
  246. add_onion_secret_key_t *decoded_key,
  247. int *hs_version, char **err_msg_out);
  248. STATIC rend_authorized_client_t *
  249. add_onion_helper_clientauth(const char *arg, int *created, char **err_msg_out);
  250. STATIC int getinfo_helper_onions(
  251. control_connection_t *control_conn,
  252. const char *question,
  253. char **answer,
  254. const char **errmsg);
  255. STATIC void getinfo_helper_downloads_networkstatus(
  256. const char *flavor,
  257. download_status_t **dl_to_emit,
  258. const char **errmsg);
  259. STATIC void getinfo_helper_downloads_cert(
  260. const char *fp_sk_req,
  261. download_status_t **dl_to_emit,
  262. smartlist_t **digest_list,
  263. const char **errmsg);
  264. STATIC void getinfo_helper_downloads_desc(
  265. const char *desc_req,
  266. download_status_t **dl_to_emit,
  267. smartlist_t **digest_list,
  268. const char **errmsg);
  269. STATIC void getinfo_helper_downloads_bridge(
  270. const char *bridge_req,
  271. download_status_t **dl_to_emit,
  272. smartlist_t **digest_list,
  273. const char **errmsg);
  274. STATIC int getinfo_helper_downloads(
  275. control_connection_t *control_conn,
  276. const char *question, char **answer,
  277. const char **errmsg);
  278. STATIC int getinfo_helper_dir(
  279. control_connection_t *control_conn,
  280. const char *question, char **answer,
  281. const char **errmsg);
  282. STATIC int getinfo_helper_current_time(
  283. control_connection_t *control_conn,
  284. const char *question, char **answer,
  285. const char **errmsg);
  286. #endif /* defined(CONTROL_PRIVATE) */
  287. #endif /* !defined(TOR_CONTROL_H) */