connection.h 13 KB

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