control_events.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file control_events.h
  8. * \brief Header file for control_events.c.
  9. **/
  10. #ifndef TOR_CONTROL_EVENTS_H
  11. #define TOR_CONTROL_EVENTS_H
  12. #include "core/or/ocirc_event.h"
  13. /** Used to indicate the type of a CIRC_MINOR event passed to the controller.
  14. * The various types are defined in control-spec.txt . */
  15. typedef enum circuit_status_minor_event_t {
  16. CIRC_MINOR_EVENT_PURPOSE_CHANGED,
  17. CIRC_MINOR_EVENT_CANNIBALIZED,
  18. } circuit_status_minor_event_t;
  19. #include "core/or/orconn_event.h"
  20. /** Used to indicate the type of a stream event passed to the controller.
  21. * The various types are defined in control-spec.txt */
  22. typedef enum stream_status_event_t {
  23. STREAM_EVENT_SENT_CONNECT = 0,
  24. STREAM_EVENT_SENT_RESOLVE = 1,
  25. STREAM_EVENT_SUCCEEDED = 2,
  26. STREAM_EVENT_FAILED = 3,
  27. STREAM_EVENT_CLOSED = 4,
  28. STREAM_EVENT_NEW = 5,
  29. STREAM_EVENT_NEW_RESOLVE = 6,
  30. STREAM_EVENT_FAILED_RETRIABLE = 7,
  31. STREAM_EVENT_REMAP = 8
  32. } stream_status_event_t;
  33. /** Used to indicate the type of a buildtime event */
  34. typedef enum buildtimeout_set_event_t {
  35. BUILDTIMEOUT_SET_EVENT_COMPUTED = 0,
  36. BUILDTIMEOUT_SET_EVENT_RESET = 1,
  37. BUILDTIMEOUT_SET_EVENT_SUSPENDED = 2,
  38. BUILDTIMEOUT_SET_EVENT_DISCARD = 3,
  39. BUILDTIMEOUT_SET_EVENT_RESUME = 4
  40. } buildtimeout_set_event_t;
  41. /** Enum describing various stages of bootstrapping, for use with controller
  42. * bootstrap status events. The values range from 0 to 100. */
  43. typedef enum {
  44. BOOTSTRAP_STATUS_UNDEF=-1,
  45. BOOTSTRAP_STATUS_STARTING=0,
  46. /* Initial connection to any relay */
  47. BOOTSTRAP_STATUS_CONN_PT=1,
  48. BOOTSTRAP_STATUS_CONN_DONE_PT=2,
  49. BOOTSTRAP_STATUS_CONN_PROXY=3,
  50. BOOTSTRAP_STATUS_CONN_DONE_PROXY=4,
  51. BOOTSTRAP_STATUS_CONN=5,
  52. BOOTSTRAP_STATUS_CONN_DONE=10,
  53. BOOTSTRAP_STATUS_HANDSHAKE=14,
  54. BOOTSTRAP_STATUS_HANDSHAKE_DONE=15,
  55. /* Loading directory info */
  56. BOOTSTRAP_STATUS_ONEHOP_CREATE=20,
  57. BOOTSTRAP_STATUS_REQUESTING_STATUS=25,
  58. BOOTSTRAP_STATUS_LOADING_STATUS=30,
  59. BOOTSTRAP_STATUS_LOADING_KEYS=40,
  60. BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS=45,
  61. BOOTSTRAP_STATUS_LOADING_DESCRIPTORS=50,
  62. BOOTSTRAP_STATUS_ENOUGH_DIRINFO=75,
  63. /* Connecting to a relay for AP circuits */
  64. BOOTSTRAP_STATUS_AP_CONN_PT=76,
  65. BOOTSTRAP_STATUS_AP_CONN_DONE_PT=77,
  66. BOOTSTRAP_STATUS_AP_CONN_PROXY=78,
  67. BOOTSTRAP_STATUS_AP_CONN_DONE_PROXY=79,
  68. BOOTSTRAP_STATUS_AP_CONN=80,
  69. BOOTSTRAP_STATUS_AP_CONN_DONE=85,
  70. BOOTSTRAP_STATUS_AP_HANDSHAKE=89,
  71. BOOTSTRAP_STATUS_AP_HANDSHAKE_DONE=90,
  72. /* Creating AP circuits */
  73. BOOTSTRAP_STATUS_CIRCUIT_CREATE=95,
  74. BOOTSTRAP_STATUS_DONE=100
  75. } bootstrap_status_t;
  76. /** Reason for remapping an AP connection's address: we have a cached
  77. * answer. */
  78. #define REMAP_STREAM_SOURCE_CACHE 1
  79. /** Reason for remapping an AP connection's address: the exit node told us an
  80. * answer. */
  81. #define REMAP_STREAM_SOURCE_EXIT 2
  82. void control_initialize_event_queue(void);
  83. void control_update_global_event_mask(void);
  84. void control_adjust_event_log_severity(void);
  85. #define EVENT_NS 0x000F
  86. int control_event_is_interesting(int event);
  87. void control_per_second_events(void);
  88. int control_any_per_second_event_enabled(void);
  89. int control_event_circuit_status(origin_circuit_t *circ,
  90. circuit_status_event_t e, int reason);
  91. int control_event_circuit_purpose_changed(origin_circuit_t *circ,
  92. int old_purpose);
  93. int control_event_circuit_cannibalized(origin_circuit_t *circ,
  94. int old_purpose,
  95. const struct timeval *old_tv_created);
  96. int control_event_stream_status(entry_connection_t *conn,
  97. stream_status_event_t e,
  98. int reason);
  99. int control_event_or_conn_status(or_connection_t *conn,
  100. or_conn_status_event_t e, int reason);
  101. int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
  102. int control_event_stream_bandwidth(edge_connection_t *edge_conn);
  103. int control_event_stream_bandwidth_used(void);
  104. int control_event_circ_bandwidth_used(void);
  105. int control_event_circ_bandwidth_used_for_circ(origin_circuit_t *ocirc);
  106. int control_event_conn_bandwidth(connection_t *conn);
  107. int control_event_conn_bandwidth_used(void);
  108. int control_event_circuit_cell_stats(void);
  109. void control_event_logmsg(int severity, log_domain_mask_t domain,
  110. const char *msg);
  111. void control_event_logmsg_pending(void);
  112. int control_event_descriptors_changed(smartlist_t *routers);
  113. int control_event_address_mapped(const char *from, const char *to,
  114. time_t expires, const char *error,
  115. const int cached);
  116. int control_event_my_descriptor_changed(void);
  117. int control_event_network_liveness_update(int liveness);
  118. int control_event_networkstatus_changed(smartlist_t *statuses);
  119. int control_event_newconsensus(const networkstatus_t *consensus);
  120. int control_event_networkstatus_changed_single(const routerstatus_t *rs);
  121. int control_event_general_status(int severity, const char *format, ...)
  122. CHECK_PRINTF(2,3);
  123. int control_event_client_status(int severity, const char *format, ...)
  124. CHECK_PRINTF(2,3);
  125. int control_event_server_status(int severity, const char *format, ...)
  126. CHECK_PRINTF(2,3);
  127. int control_event_general_error(const char *format, ...)
  128. CHECK_PRINTF(1,2);
  129. int control_event_client_error(const char *format, ...)
  130. CHECK_PRINTF(1,2);
  131. int control_event_server_error(const char *format, ...)
  132. CHECK_PRINTF(1,2);
  133. int control_event_guard(const char *nickname, const char *digest,
  134. const char *status);
  135. int control_event_conf_changed(const smartlist_t *elements);
  136. int control_event_buildtimeout_set(buildtimeout_set_event_t type,
  137. const char *args);
  138. int control_event_signal(uintptr_t signal);
  139. void control_event_bootstrap(bootstrap_status_t status, int progress);
  140. MOCK_DECL(void, control_event_bootstrap_prob_or,(const char *warn,
  141. int reason,
  142. or_connection_t *or_conn));
  143. void control_event_boot_dir(bootstrap_status_t status, int progress);
  144. void control_event_boot_first_orconn(void);
  145. void control_event_bootstrap_problem(const char *warn, const char *reason,
  146. const connection_t *conn, int dowarn);
  147. char *control_event_boot_last_msg(void);
  148. void control_event_bootstrap_reset(void);
  149. void control_event_clients_seen(const char *controller_str);
  150. void control_event_transport_launched(const char *mode,
  151. const char *transport_name,
  152. tor_addr_t *addr, uint16_t port);
  153. void control_event_pt_log(const char *log);
  154. void control_event_pt_status(const char *status);
  155. void control_event_hs_descriptor_requested(const char *onion_address,
  156. rend_auth_type_t auth_type,
  157. const char *id_digest,
  158. const char *desc_id,
  159. const char *hsdir_index);
  160. void control_event_hs_descriptor_created(const char *onion_address,
  161. const char *desc_id,
  162. int replica);
  163. void control_event_hs_descriptor_upload(const char *onion_address,
  164. const char *desc_id,
  165. const char *hs_dir,
  166. const char *hsdir_index);
  167. void control_event_hs_descriptor_upload_end(const char *action,
  168. const char *onion_address,
  169. const char *hs_dir,
  170. const char *reason);
  171. void control_event_hs_descriptor_uploaded(const char *hs_dir,
  172. const char *onion_address);
  173. /* Hidden service v2 HS_DESC specific. */
  174. void control_event_hsv2_descriptor_failed(const rend_data_t *rend_data,
  175. const char *id_digest,
  176. const char *reason);
  177. void control_event_hsv2_descriptor_received(const char *onion_address,
  178. const rend_data_t *rend_data,
  179. const char *id_digest);
  180. /* Hidden service v3 HS_DESC specific. */
  181. void control_event_hsv3_descriptor_failed(const char *onion_address,
  182. const char *desc_id,
  183. const char *hsdir_id_digest,
  184. const char *reason);
  185. void control_event_hsv3_descriptor_received(const char *onion_address,
  186. const char *desc_id,
  187. const char *hsdir_id_digest);
  188. void control_event_hs_descriptor_upload_failed(const char *hs_dir,
  189. const char *onion_address,
  190. const char *reason);
  191. void control_event_hs_descriptor_content(const char *onion_address,
  192. const char *desc_id,
  193. const char *hsdir_fp,
  194. const char *content);
  195. void control_events_free_all(void);
  196. #ifdef CONTROL_MODULE_PRIVATE
  197. char *get_bw_samples(void);
  198. #endif /* defined(CONTROL_MODULE_PRIVATE) */
  199. #ifdef CONTROL_EVENTS_PRIVATE
  200. /** Bitfield: The bit 1&lt;&lt;e is set if <b>any</b> open control
  201. * connection is interested in events of type <b>e</b>. We use this
  202. * so that we can decide to skip generating event messages that nobody
  203. * has interest in without having to walk over the global connection
  204. * list to find out.
  205. **/
  206. typedef uint64_t event_mask_t;
  207. /* Recognized asynchronous event types. It's okay to expand this list
  208. * because it is used both as a list of v0 event types, and as indices
  209. * into the bitfield to determine which controllers want which events.
  210. */
  211. /* This bitfield has no event zero 0x0000 */
  212. #define EVENT_MIN_ 0x0001
  213. #define EVENT_CIRCUIT_STATUS 0x0001
  214. #define EVENT_STREAM_STATUS 0x0002
  215. #define EVENT_OR_CONN_STATUS 0x0003
  216. #define EVENT_BANDWIDTH_USED 0x0004
  217. #define EVENT_CIRCUIT_STATUS_MINOR 0x0005
  218. #define EVENT_NEW_DESC 0x0006
  219. #define EVENT_DEBUG_MSG 0x0007
  220. #define EVENT_INFO_MSG 0x0008
  221. #define EVENT_NOTICE_MSG 0x0009
  222. #define EVENT_WARN_MSG 0x000A
  223. #define EVENT_ERR_MSG 0x000B
  224. #define EVENT_ADDRMAP 0x000C
  225. /* There was an AUTHDIR_NEWDESCS event, but it no longer exists. We
  226. can reclaim 0x000D. */
  227. #define EVENT_DESCCHANGED 0x000E
  228. /* Exposed above */
  229. // #define EVENT_NS 0x000F
  230. #define EVENT_STATUS_CLIENT 0x0010
  231. #define EVENT_STATUS_SERVER 0x0011
  232. #define EVENT_STATUS_GENERAL 0x0012
  233. #define EVENT_GUARD 0x0013
  234. #define EVENT_STREAM_BANDWIDTH_USED 0x0014
  235. #define EVENT_CLIENTS_SEEN 0x0015
  236. #define EVENT_NEWCONSENSUS 0x0016
  237. #define EVENT_BUILDTIMEOUT_SET 0x0017
  238. #define EVENT_GOT_SIGNAL 0x0018
  239. #define EVENT_CONF_CHANGED 0x0019
  240. #define EVENT_CONN_BW 0x001A
  241. #define EVENT_CELL_STATS 0x001B
  242. /* UNUSED : 0x001C */
  243. #define EVENT_CIRC_BANDWIDTH_USED 0x001D
  244. #define EVENT_TRANSPORT_LAUNCHED 0x0020
  245. #define EVENT_HS_DESC 0x0021
  246. #define EVENT_HS_DESC_CONTENT 0x0022
  247. #define EVENT_NETWORK_LIVENESS 0x0023
  248. #define EVENT_PT_LOG 0x0024
  249. #define EVENT_PT_STATUS 0x0025
  250. #define EVENT_MAX_ 0x0025
  251. /* sizeof(control_connection_t.event_mask) in bits, currently a uint64_t */
  252. #define EVENT_CAPACITY_ 0x0040
  253. /* If EVENT_MAX_ ever hits 0x0040, we need to make the mask into a
  254. * different structure, as it can only handle a maximum left shift of 1<<63. */
  255. #if EVENT_MAX_ >= EVENT_CAPACITY_
  256. #error control_connection_t.event_mask has an event greater than its capacity
  257. #endif
  258. #define EVENT_MASK_(e) (((uint64_t)1)<<(e))
  259. #define EVENT_MASK_NONE_ ((uint64_t)0x0)
  260. #define EVENT_MASK_ABOVE_MIN_ ((~((uint64_t)0x0)) << EVENT_MIN_)
  261. #define EVENT_MASK_BELOW_MAX_ ((~((uint64_t)0x0)) \
  262. >> (EVENT_CAPACITY_ - EVENT_MAX_ \
  263. - EVENT_MIN_))
  264. #define EVENT_MASK_ALL_ (EVENT_MASK_ABOVE_MIN_ \
  265. & EVENT_MASK_BELOW_MAX_)
  266. /** Helper structure: temporarily stores cell statistics for a circuit. */
  267. typedef struct cell_stats_t {
  268. /** Number of cells added in app-ward direction by command. */
  269. uint64_t added_cells_appward[CELL_COMMAND_MAX_ + 1];
  270. /** Number of cells added in exit-ward direction by command. */
  271. uint64_t added_cells_exitward[CELL_COMMAND_MAX_ + 1];
  272. /** Number of cells removed in app-ward direction by command. */
  273. uint64_t removed_cells_appward[CELL_COMMAND_MAX_ + 1];
  274. /** Number of cells removed in exit-ward direction by command. */
  275. uint64_t removed_cells_exitward[CELL_COMMAND_MAX_ + 1];
  276. /** Total waiting time of cells in app-ward direction by command. */
  277. uint64_t total_time_appward[CELL_COMMAND_MAX_ + 1];
  278. /** Total waiting time of cells in exit-ward direction by command. */
  279. uint64_t total_time_exitward[CELL_COMMAND_MAX_ + 1];
  280. } cell_stats_t;
  281. void sum_up_cell_stats_by_command(circuit_t *circ,
  282. cell_stats_t *cell_stats);
  283. void append_cell_stats_by_command(smartlist_t *event_parts,
  284. const char *key,
  285. const uint64_t *include_if_non_zero,
  286. const uint64_t *number_to_include);
  287. void format_cell_stats(char **event_string, circuit_t *circ,
  288. cell_stats_t *cell_stats);
  289. /** Helper structure: maps event values to their names. */
  290. struct control_event_t {
  291. uint16_t event_code;
  292. const char *event_name;
  293. };
  294. extern const struct control_event_t control_event_table[];
  295. #ifdef TOR_UNIT_TESTS
  296. MOCK_DECL(STATIC void,
  297. send_control_event_string,(uint16_t event, const char *msg));
  298. MOCK_DECL(STATIC void,
  299. queue_control_event_string,(uint16_t event, char *msg));
  300. void control_testing_set_global_event_mask(uint64_t mask);
  301. #endif /* defined(TOR_UNIT_TESTS) */
  302. #endif /* defined(CONTROL_EVENTS_PRIVATE) */
  303. #endif /* !defined(TOR_CONTROL_EVENTS_H) */