control.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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-2015, 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. void control_update_global_event_mask(void);
  13. void control_adjust_event_log_severity(void);
  14. void control_ports_write_to_file(void);
  15. /** Log information about the connection <b>conn</b>, protecting it as with
  16. * CONN_LOG_PROTECT. Example:
  17. *
  18. * LOG_FN_CONN(conn, (LOG_DEBUG, "Socket %d wants to write", conn->s));
  19. **/
  20. #define LOG_FN_CONN(conn, args) \
  21. CONN_LOG_PROTECT(conn, log_fn args)
  22. int connection_control_finished_flushing(control_connection_t *conn);
  23. int connection_control_reached_eof(control_connection_t *conn);
  24. void connection_control_closed(control_connection_t *conn);
  25. int connection_control_process_inbuf(control_connection_t *conn);
  26. #define EVENT_AUTHDIR_NEWDESCS 0x000D
  27. #define EVENT_NS 0x000F
  28. int control_event_is_interesting(int event);
  29. int control_event_circuit_status(origin_circuit_t *circ,
  30. circuit_status_event_t e, int reason);
  31. int control_event_circuit_purpose_changed(origin_circuit_t *circ,
  32. int old_purpose);
  33. int control_event_circuit_cannibalized(origin_circuit_t *circ,
  34. int old_purpose,
  35. const struct timeval *old_tv_created);
  36. int control_event_stream_status(entry_connection_t *conn,
  37. stream_status_event_t e,
  38. int reason);
  39. int control_event_or_conn_status(or_connection_t *conn,
  40. or_conn_status_event_t e, int reason);
  41. int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
  42. int control_event_stream_bandwidth(edge_connection_t *edge_conn);
  43. int control_event_stream_bandwidth_used(void);
  44. int control_event_circ_bandwidth_used(void);
  45. int control_event_conn_bandwidth(connection_t *conn);
  46. int control_event_conn_bandwidth_used(void);
  47. int control_event_circuit_cell_stats(void);
  48. int control_event_tb_empty(const char *bucket, uint32_t read_empty_time,
  49. uint32_t write_empty_time,
  50. int milliseconds_elapsed);
  51. void control_event_logmsg(int severity, uint32_t domain, const char *msg);
  52. int control_event_descriptors_changed(smartlist_t *routers);
  53. int control_event_address_mapped(const char *from, const char *to,
  54. time_t expires, const char *error,
  55. const int cached);
  56. int control_event_or_authdir_new_descriptor(const char *action,
  57. const char *desc,
  58. size_t desclen,
  59. const char *msg);
  60. int control_event_my_descriptor_changed(void);
  61. int control_event_networkstatus_changed(smartlist_t *statuses);
  62. int control_event_newconsensus(const networkstatus_t *consensus);
  63. int control_event_networkstatus_changed_single(const routerstatus_t *rs);
  64. int control_event_general_status(int severity, const char *format, ...)
  65. CHECK_PRINTF(2,3);
  66. int control_event_client_status(int severity, const char *format, ...)
  67. CHECK_PRINTF(2,3);
  68. int control_event_server_status(int severity, const char *format, ...)
  69. CHECK_PRINTF(2,3);
  70. int control_event_guard(const char *nickname, const char *digest,
  71. const char *status);
  72. int control_event_conf_changed(const smartlist_t *elements);
  73. int control_event_buildtimeout_set(buildtimeout_set_event_t type,
  74. const char *args);
  75. int control_event_signal(uintptr_t signal);
  76. int init_control_cookie_authentication(int enabled);
  77. char *get_controller_cookie_file_name(void);
  78. smartlist_t *decode_hashed_passwords(config_line_t *passwords);
  79. void disable_control_logging(void);
  80. void enable_control_logging(void);
  81. void monitor_owning_controller_process(const char *process_spec);
  82. int control_event_bootstrap(bootstrap_status_t status, int progress);
  83. MOCK_DECL(void, control_event_bootstrap_problem,(const char *warn,
  84. int reason,
  85. or_connection_t *or_conn));
  86. void control_event_clients_seen(const char *controller_str);
  87. void control_event_transport_launched(const char *mode,
  88. const char *transport_name,
  89. tor_addr_t *addr, uint16_t port);
  90. const char *rend_auth_type_to_string(rend_auth_type_t auth_type);
  91. MOCK_DECL(const char *, node_describe_longname_by_id,(const char *id_digest));
  92. void control_event_hs_descriptor_requested(const rend_data_t *rend_query,
  93. const char *desc_id_base32,
  94. const char *hs_dir);
  95. void control_event_hs_descriptor_receive_end(const char *action,
  96. const rend_data_t *rend_query,
  97. const char *hs_dir,
  98. const char *reason);
  99. void control_event_hs_descriptor_received(const rend_data_t *rend_query,
  100. const char *hs_dir);
  101. void control_event_hs_descriptor_failed(const rend_data_t *rend_query,
  102. const char *hs_dir,
  103. const char *reason);
  104. void control_free_all(void);
  105. #ifdef CONTROL_PRIVATE
  106. /* Recognized asynchronous event types. It's okay to expand this list
  107. * because it is used both as a list of v0 event types, and as indices
  108. * into the bitfield to determine which controllers want which events.
  109. */
  110. /* This bitfield has no event zero 0x0000 */
  111. #define EVENT_MIN_ 0x0001
  112. #define EVENT_CIRCUIT_STATUS 0x0001
  113. #define EVENT_STREAM_STATUS 0x0002
  114. #define EVENT_OR_CONN_STATUS 0x0003
  115. #define EVENT_BANDWIDTH_USED 0x0004
  116. #define EVENT_CIRCUIT_STATUS_MINOR 0x0005
  117. #define EVENT_NEW_DESC 0x0006
  118. #define EVENT_DEBUG_MSG 0x0007
  119. #define EVENT_INFO_MSG 0x0008
  120. #define EVENT_NOTICE_MSG 0x0009
  121. #define EVENT_WARN_MSG 0x000A
  122. #define EVENT_ERR_MSG 0x000B
  123. #define EVENT_ADDRMAP 0x000C
  124. /* Exposed above */
  125. // #define EVENT_AUTHDIR_NEWDESCS 0x000D
  126. #define EVENT_DESCCHANGED 0x000E
  127. /* Exposed above */
  128. // #define EVENT_NS 0x000F
  129. #define EVENT_STATUS_CLIENT 0x0010
  130. #define EVENT_STATUS_SERVER 0x0011
  131. #define EVENT_STATUS_GENERAL 0x0012
  132. #define EVENT_GUARD 0x0013
  133. #define EVENT_STREAM_BANDWIDTH_USED 0x0014
  134. #define EVENT_CLIENTS_SEEN 0x0015
  135. #define EVENT_NEWCONSENSUS 0x0016
  136. #define EVENT_BUILDTIMEOUT_SET 0x0017
  137. #define EVENT_SIGNAL 0x0018
  138. #define EVENT_CONF_CHANGED 0x0019
  139. #define EVENT_CONN_BW 0x001A
  140. #define EVENT_CELL_STATS 0x001B
  141. #define EVENT_TB_EMPTY 0x001C
  142. #define EVENT_CIRC_BANDWIDTH_USED 0x001D
  143. #define EVENT_TRANSPORT_LAUNCHED 0x0020
  144. #define EVENT_HS_DESC 0x0021
  145. #define EVENT_MAX_ 0x0021
  146. /* sizeof(control_connection_t.event_mask) in bits, currently a uint64_t */
  147. #define EVENT_CAPACITY_ 0x0040
  148. /* If EVENT_MAX_ ever hits 0x0040, we need to make the mask into a
  149. * different structure, as it can only handle a maximum left shift of 1<<63. */
  150. #define EVENT_MASK_(e) (((uint64_t)1)<<(e))
  151. #define EVENT_MASK_NONE_ ((uint64_t)0x0)
  152. #define EVENT_MASK_ABOVE_MIN_ ((~((uint64_t)0x0)) << EVENT_MIN_)
  153. #define EVENT_MASK_BELOW_MAX_ ((~((uint64_t)0x0)) \
  154. >> (EVENT_CAPACITY_ - EVENT_MAX_ \
  155. - EVENT_MIN_))
  156. #define EVENT_MASK_ALL_ (EVENT_MASK_ABOVE_MIN_ \
  157. & EVENT_MASK_BELOW_MAX_)
  158. /* Used only by control.c and test.c */
  159. STATIC size_t write_escaped_data(const char *data, size_t len, char **out);
  160. STATIC size_t read_escaped_data(const char *data, size_t len, char **out);
  161. /** Flag for event_format_t. Indicates that we should use the one standard
  162. format. (Other formats previous existed, and are now deprecated)
  163. */
  164. #define ALL_FORMATS 1
  165. /** Bit field of flags to select how to format a controller event. Recognized
  166. * flag is ALL_FORMATS. */
  167. typedef int event_format_t;
  168. #ifdef TOR_UNIT_TESTS
  169. MOCK_DECL(STATIC void,
  170. send_control_event_string,(uint16_t event, event_format_t which,
  171. const char *msg));
  172. void control_testing_set_global_event_mask(uint64_t mask);
  173. #endif
  174. /** Helper structure: temporarily stores cell statistics for a circuit. */
  175. typedef struct cell_stats_t {
  176. /** Number of cells added in app-ward direction by command. */
  177. uint64_t added_cells_appward[CELL_COMMAND_MAX_ + 1];
  178. /** Number of cells added in exit-ward direction by command. */
  179. uint64_t added_cells_exitward[CELL_COMMAND_MAX_ + 1];
  180. /** Number of cells removed in app-ward direction by command. */
  181. uint64_t removed_cells_appward[CELL_COMMAND_MAX_ + 1];
  182. /** Number of cells removed in exit-ward direction by command. */
  183. uint64_t removed_cells_exitward[CELL_COMMAND_MAX_ + 1];
  184. /** Total waiting time of cells in app-ward direction by command. */
  185. uint64_t total_time_appward[CELL_COMMAND_MAX_ + 1];
  186. /** Total waiting time of cells in exit-ward direction by command. */
  187. uint64_t total_time_exitward[CELL_COMMAND_MAX_ + 1];
  188. } cell_stats_t;
  189. void sum_up_cell_stats_by_command(circuit_t *circ,
  190. cell_stats_t *cell_stats);
  191. void append_cell_stats_by_command(smartlist_t *event_parts,
  192. const char *key,
  193. const uint64_t *include_if_non_zero,
  194. const uint64_t *number_to_include);
  195. void format_cell_stats(char **event_string, circuit_t *circ,
  196. cell_stats_t *cell_stats);
  197. STATIC char *get_bw_samples(void);
  198. #endif
  199. #endif