connection.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 connection.h
  8. * \brief Header file for connection.c.
  9. **/
  10. #ifndef TOR_CONNECTION_H
  11. #define TOR_CONNECTION_H
  12. listener_connection_t *TO_LISTENER_CONN(connection_t *);
  13. /* XXXX For buf_datalen in inline function */
  14. #include "common/buffers.h"
  15. const char *conn_type_to_string(int type);
  16. const char *conn_state_to_string(int type, int state);
  17. int conn_listener_type_supports_af_unix(int type);
  18. dir_connection_t *dir_connection_new(int socket_family);
  19. or_connection_t *or_connection_new(int type, int socket_family);
  20. edge_connection_t *edge_connection_new(int type, int socket_family);
  21. entry_connection_t *entry_connection_new(int type, int socket_family);
  22. control_connection_t *control_connection_new(int socket_family);
  23. listener_connection_t *listener_connection_new(int type, int socket_family);
  24. connection_t *connection_new(int type, int socket_family);
  25. int connection_init_accepted_conn(connection_t *conn,
  26. const listener_connection_t *listener);
  27. void connection_link_connections(connection_t *conn_a, connection_t *conn_b);
  28. MOCK_DECL(void,connection_free_,(connection_t *conn));
  29. #define connection_free(conn) \
  30. FREE_AND_NULL(connection_t, connection_free_, (conn))
  31. void connection_free_all(void);
  32. void connection_about_to_close_connection(connection_t *conn);
  33. void connection_close_immediate(connection_t *conn);
  34. void connection_mark_for_close_(connection_t *conn,
  35. int line, const char *file);
  36. MOCK_DECL(void, connection_mark_for_close_internal_,
  37. (connection_t *conn, int line, const char *file));
  38. #define connection_mark_for_close(c) \
  39. connection_mark_for_close_((c), __LINE__, SHORT_FILE__)
  40. #define connection_mark_for_close_internal(c) \
  41. connection_mark_for_close_internal_((c), __LINE__, SHORT_FILE__)
  42. /**
  43. * Mark 'c' for close, but try to hold it open until all the data is written.
  44. * Use the _internal versions of connection_mark_for_close; this should be
  45. * called when you either are sure that if this is an or_connection_t the
  46. * controlling channel has been notified (e.g. with
  47. * connection_or_notify_error()), or you actually are the
  48. * connection_or_close_for_error() or connection_or_close_normally function.
  49. * For all other cases, use connection_mark_and_flush() instead, which
  50. * checks for or_connection_t properly, instead. See below.
  51. */
  52. #define connection_mark_and_flush_internal_(c,line,file) \
  53. do { \
  54. connection_t *tmp_conn__ = (c); \
  55. connection_mark_for_close_internal_(tmp_conn__, (line), (file)); \
  56. tmp_conn__->hold_open_until_flushed = 1; \
  57. } while (0)
  58. #define connection_mark_and_flush_internal(c) \
  59. connection_mark_and_flush_internal_((c), __LINE__, SHORT_FILE__)
  60. /**
  61. * Mark 'c' for close, but try to hold it open until all the data is written.
  62. */
  63. #define connection_mark_and_flush_(c,line,file) \
  64. do { \
  65. connection_t *tmp_conn_ = (c); \
  66. if (tmp_conn_->type == CONN_TYPE_OR) { \
  67. log_warn(LD_CHANNEL | LD_BUG, \
  68. "Something tried to close (and flush) an or_connection_t" \
  69. " without going through channels at %s:%d", \
  70. file, line); \
  71. connection_or_close_for_error(TO_OR_CONN(tmp_conn_), 1); \
  72. } else { \
  73. connection_mark_and_flush_internal_(c, line, file); \
  74. } \
  75. } while (0)
  76. #define connection_mark_and_flush(c) \
  77. connection_mark_and_flush_((c), __LINE__, SHORT_FILE__)
  78. void connection_expire_held_open(void);
  79. int connection_connect(connection_t *conn, const char *address,
  80. const tor_addr_t *addr,
  81. uint16_t port, int *socket_error);
  82. #ifdef HAVE_SYS_UN_H
  83. int connection_connect_unix(connection_t *conn, const char *socket_path,
  84. int *socket_error);
  85. #endif /* defined(HAVE_SYS_UN_H) */
  86. /** Maximum size of information that we can fit into SOCKS5 username
  87. or password fields. */
  88. #define MAX_SOCKS5_AUTH_FIELD_SIZE 255
  89. /** Total maximum size of information that we can fit into SOCKS5
  90. username and password fields. */
  91. #define MAX_SOCKS5_AUTH_SIZE_TOTAL 2*MAX_SOCKS5_AUTH_FIELD_SIZE
  92. int connection_proxy_connect(connection_t *conn, int type);
  93. int connection_read_proxy_handshake(connection_t *conn);
  94. void log_failed_proxy_connection(connection_t *conn);
  95. int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
  96. const connection_t *conn);
  97. int retry_all_listeners(smartlist_t *replaced_conns,
  98. smartlist_t *new_conns,
  99. int close_all_noncontrol);
  100. void connection_mark_all_noncontrol_listeners(void);
  101. void connection_mark_all_noncontrol_connections(void);
  102. ssize_t connection_bucket_write_limit(connection_t *conn, time_t now);
  103. int global_write_bucket_low(connection_t *conn, size_t attempt, int priority);
  104. void connection_bucket_init(void);
  105. void connection_bucket_adjust(const or_options_t *options);
  106. void connection_bucket_refill_all(time_t now,
  107. uint32_t now_ts);
  108. void connection_read_bw_exhausted(connection_t *conn, bool is_global_bw);
  109. void connection_write_bw_exhausted(connection_t *conn, bool is_global_bw);
  110. void connection_consider_empty_read_buckets(connection_t *conn);
  111. void connection_consider_empty_write_buckets(connection_t *conn);
  112. int connection_handle_read(connection_t *conn);
  113. int connection_buf_get_bytes(char *string, size_t len, connection_t *conn);
  114. int connection_buf_get_line(connection_t *conn, char *data,
  115. size_t *data_len);
  116. int connection_fetch_from_buf_http(connection_t *conn,
  117. char **headers_out, size_t max_headerlen,
  118. char **body_out, size_t *body_used,
  119. size_t max_bodylen, int force_complete);
  120. int connection_wants_to_flush(connection_t *conn);
  121. int connection_outbuf_too_full(connection_t *conn);
  122. int connection_handle_write(connection_t *conn, int force);
  123. int connection_flush(connection_t *conn);
  124. MOCK_DECL(void, connection_write_to_buf_impl_,
  125. (const char *string, size_t len, connection_t *conn, int zlib));
  126. /* DOCDOC connection_write_to_buf */
  127. static void connection_buf_add(const char *string, size_t len,
  128. connection_t *conn);
  129. static inline void
  130. connection_buf_add(const char *string, size_t len, connection_t *conn)
  131. {
  132. connection_write_to_buf_impl_(string, len, conn, 0);
  133. }
  134. void connection_buf_add_compress(const char *string, size_t len,
  135. dir_connection_t *conn, int done);
  136. void connection_buf_add_buf(connection_t *conn, buf_t *buf);
  137. size_t connection_get_inbuf_len(connection_t *conn);
  138. size_t connection_get_outbuf_len(connection_t *conn);
  139. connection_t *connection_get_by_global_id(uint64_t id);
  140. connection_t *connection_get_by_type(int type);
  141. MOCK_DECL(connection_t *,connection_get_by_type_addr_port_purpose,(int type,
  142. const tor_addr_t *addr,
  143. uint16_t port, int purpose));
  144. connection_t *connection_get_by_type_state(int type, int state);
  145. connection_t *connection_get_by_type_state_rendquery(int type, int state,
  146. const char *rendquery);
  147. smartlist_t *connection_list_by_type_state(int type, int state);
  148. smartlist_t *connection_list_by_type_purpose(int type, int purpose);
  149. smartlist_t *connection_dir_list_by_purpose_and_resource(
  150. int purpose,
  151. const char *resource);
  152. smartlist_t *connection_dir_list_by_purpose_resource_and_state(
  153. int purpose,
  154. const char *resource,
  155. int state);
  156. #define CONN_LEN_AND_FREE_TEMPLATE(sl) \
  157. STMT_BEGIN \
  158. int len = smartlist_len(sl); \
  159. smartlist_free(sl); \
  160. return len; \
  161. STMT_END
  162. /** Return a count of directory connections that are fetching the item
  163. * described by <b>purpose</b>/<b>resource</b>. */
  164. static inline int
  165. connection_dir_count_by_purpose_and_resource(
  166. int purpose,
  167. const char *resource)
  168. {
  169. smartlist_t *conns = connection_dir_list_by_purpose_and_resource(
  170. purpose,
  171. resource);
  172. CONN_LEN_AND_FREE_TEMPLATE(conns);
  173. }
  174. /** Return a count of directory connections that are fetching the item
  175. * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. */
  176. static inline int
  177. connection_dir_count_by_purpose_resource_and_state(
  178. int purpose,
  179. const char *resource,
  180. int state)
  181. {
  182. smartlist_t *conns =
  183. connection_dir_list_by_purpose_resource_and_state(
  184. purpose,
  185. resource,
  186. state);
  187. CONN_LEN_AND_FREE_TEMPLATE(conns);
  188. }
  189. #undef CONN_LEN_AND_FREE_TEMPLATE
  190. int any_other_active_or_conns(const or_connection_t *this_conn);
  191. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  192. #define connection_speaks_cells(conn) (((conn)->type == CONN_TYPE_OR) || 0)
  193. int connection_is_listener(connection_t *conn);
  194. int connection_state_is_open(connection_t *conn);
  195. int connection_state_is_connecting(connection_t *conn);
  196. char *alloc_http_authenticator(const char *authenticator);
  197. void assert_connection_ok(connection_t *conn, time_t now);
  198. int connection_or_nonopen_was_started_here(or_connection_t *conn);
  199. void connection_dump_buffer_mem_stats(int severity);
  200. MOCK_DECL(void, clock_skew_warning,
  201. (const connection_t *conn, long apparent_skew, int trusted,
  202. log_domain_mask_t domain, const char *received,
  203. const char *source));
  204. int connection_is_moribund(connection_t *conn);
  205. void connection_check_oos(int n_socks, int failed);
  206. #ifdef CONNECTION_PRIVATE
  207. STATIC void connection_free_minimal(connection_t *conn);
  208. /* Used only by connection.c and test*.c */
  209. MOCK_DECL(STATIC int,connection_connect_sockaddr,
  210. (connection_t *conn,
  211. const struct sockaddr *sa,
  212. socklen_t sa_len,
  213. const struct sockaddr *bindaddr,
  214. socklen_t bindaddr_len,
  215. int *socket_error));
  216. MOCK_DECL(STATIC void, kill_conn_list_for_oos, (smartlist_t *conns));
  217. MOCK_DECL(STATIC smartlist_t *, pick_oos_victims, (int n));
  218. #endif /* defined(CONNECTION_PRIVATE) */
  219. #endif /* !defined(TOR_CONNECTION_H) */