connection.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 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. void connection_link_connections(connection_t *conn_a, connection_t *conn_b);
  25. void connection_free(connection_t *conn);
  26. void connection_free_all(void);
  27. void connection_about_to_close_connection(connection_t *conn);
  28. void connection_close_immediate(connection_t *conn);
  29. void connection_mark_for_close_(connection_t *conn,
  30. int line, const char *file);
  31. void connection_mark_for_close_internal_(connection_t *conn,
  32. int line, const char *file);
  33. #define connection_mark_for_close(c) \
  34. connection_mark_for_close_((c), __LINE__, SHORT_FILE__)
  35. #define connection_mark_for_close_internal(c) \
  36. connection_mark_for_close_internal_((c), __LINE__, SHORT_FILE__)
  37. /**
  38. * Mark 'c' for close, but try to hold it open until all the data is written.
  39. * Use the _internal versions of connection_mark_for_close; this should be
  40. * called when you either are sure that if this is an or_connection_t the
  41. * controlling channel has been notified (e.g. with
  42. * connection_or_notify_error()), or you actually are the
  43. * connection_or_close_for_error() or connection_or_close_normally function.
  44. * For all other cases, use connection_mark_and_flush() instead, which
  45. * checks for or_connection_t properly, instead. See below.
  46. */
  47. #define connection_mark_and_flush_internal_(c,line,file) \
  48. do { \
  49. connection_t *tmp_conn_ = (c); \
  50. connection_mark_for_close_internal_(tmp_conn_, (line), (file)); \
  51. tmp_conn_->hold_open_until_flushed = 1; \
  52. IF_HAS_BUFFEREVENT(tmp_conn_, \
  53. connection_start_writing(tmp_conn_)); \
  54. } while (0)
  55. #define connection_mark_and_flush_internal(c) \
  56. connection_mark_and_flush_internal_((c), __LINE__, SHORT_FILE__)
  57. /**
  58. * Mark 'c' for close, but try to hold it open until all the data is written.
  59. */
  60. #define connection_mark_and_flush_(c,line,file) \
  61. do { \
  62. connection_t *tmp_conn_ = (c); \
  63. if (tmp_conn_->type == CONN_TYPE_OR) { \
  64. log_warn(LD_CHANNEL | LD_BUG, \
  65. "Something tried to close (and flush) an or_connection_t" \
  66. " without going through channels at %s:%d", \
  67. file, line); \
  68. connection_or_close_for_error(TO_OR_CONN(tmp_conn_), 1); \
  69. } else { \
  70. connection_mark_and_flush_internal_(c, line, file); \
  71. } \
  72. } while (0)
  73. #define connection_mark_and_flush(c) \
  74. connection_mark_and_flush_((c), __LINE__, SHORT_FILE__)
  75. void connection_expire_held_open(void);
  76. int connection_connect(connection_t *conn, const char *address,
  77. const tor_addr_t *addr,
  78. uint16_t port, int *socket_error);
  79. #ifdef HAVE_SYS_UN_H
  80. int connection_connect_unix(connection_t *conn, const char *socket_path,
  81. int *socket_error);
  82. #endif /* defined(HAVE_SYS_UN_H) */
  83. /** Maximum size of information that we can fit into SOCKS5 username
  84. or password fields. */
  85. #define MAX_SOCKS5_AUTH_FIELD_SIZE 255
  86. /** Total maximum size of information that we can fit into SOCKS5
  87. username and password fields. */
  88. #define MAX_SOCKS5_AUTH_SIZE_TOTAL 2*MAX_SOCKS5_AUTH_FIELD_SIZE
  89. int connection_proxy_connect(connection_t *conn, int type);
  90. int connection_read_proxy_handshake(connection_t *conn);
  91. void log_failed_proxy_connection(connection_t *conn);
  92. int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
  93. const connection_t *conn);
  94. int retry_all_listeners(smartlist_t *replaced_conns,
  95. smartlist_t *new_conns,
  96. int close_all_noncontrol);
  97. void connection_mark_all_noncontrol_listeners(void);
  98. void connection_mark_all_noncontrol_connections(void);
  99. ssize_t connection_bucket_write_limit(connection_t *conn, time_t now);
  100. int global_write_bucket_low(connection_t *conn, size_t attempt, int priority);
  101. void connection_bucket_init(void);
  102. void connection_bucket_refill(int seconds_elapsed, time_t now);
  103. int connection_handle_read(connection_t *conn);
  104. int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
  105. int connection_fetch_from_buf_line(connection_t *conn, char *data,
  106. size_t *data_len);
  107. int connection_fetch_from_buf_http(connection_t *conn,
  108. char **headers_out, size_t max_headerlen,
  109. char **body_out, size_t *body_used,
  110. size_t max_bodylen, int force_complete);
  111. int connection_wants_to_flush(connection_t *conn);
  112. int connection_outbuf_too_full(connection_t *conn);
  113. int connection_handle_write(connection_t *conn, int force);
  114. int connection_flush(connection_t *conn);
  115. MOCK_DECL(void, connection_write_to_buf_impl_,
  116. (const char *string, size_t len, connection_t *conn, int zlib));
  117. /* DOCDOC connection_write_to_buf */
  118. static void connection_write_to_buf(const char *string, size_t len,
  119. connection_t *conn);
  120. /* DOCDOC connection_write_to_buf_zlib */
  121. static void connection_write_to_buf_zlib(const char *string, size_t len,
  122. dir_connection_t *conn, int done);
  123. static INLINE void
  124. connection_write_to_buf(const char *string, size_t len, connection_t *conn)
  125. {
  126. connection_write_to_buf_impl_(string, len, conn, 0);
  127. }
  128. static INLINE void
  129. connection_write_to_buf_zlib(const char *string, size_t len,
  130. dir_connection_t *conn, int done)
  131. {
  132. connection_write_to_buf_impl_(string, len, TO_CONN(conn), done ? -1 : 1);
  133. }
  134. /* DOCDOC connection_get_inbuf_len */
  135. static size_t connection_get_inbuf_len(connection_t *conn);
  136. /* DOCDOC connection_get_outbuf_len */
  137. static size_t connection_get_outbuf_len(connection_t *conn);
  138. static INLINE size_t
  139. connection_get_inbuf_len(connection_t *conn)
  140. {
  141. IF_HAS_BUFFEREVENT(conn, {
  142. return evbuffer_get_length(bufferevent_get_input(conn->bufev));
  143. }) ELSE_IF_NO_BUFFEREVENT {
  144. return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
  145. }
  146. }
  147. static INLINE size_t
  148. connection_get_outbuf_len(connection_t *conn)
  149. {
  150. IF_HAS_BUFFEREVENT(conn, {
  151. return evbuffer_get_length(bufferevent_get_output(conn->bufev));
  152. }) ELSE_IF_NO_BUFFEREVENT {
  153. return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
  154. }
  155. }
  156. connection_t *connection_get_by_global_id(uint64_t id);
  157. connection_t *connection_get_by_type(int type);
  158. connection_t *connection_get_by_type_addr_port_purpose(int type,
  159. const tor_addr_t *addr,
  160. uint16_t port, int purpose);
  161. connection_t *connection_get_by_type_state(int type, int state);
  162. connection_t *connection_get_by_type_state_rendquery(int type, int state,
  163. const char *rendquery);
  164. dir_connection_t *connection_dir_get_by_purpose_and_resource(
  165. int state, const char *resource);
  166. int any_other_active_or_conns(const or_connection_t *this_conn);
  167. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  168. #define connection_speaks_cells(conn) (((conn)->type == CONN_TYPE_OR) || 0)
  169. int connection_is_listener(connection_t *conn);
  170. int connection_state_is_open(connection_t *conn);
  171. int connection_state_is_connecting(connection_t *conn);
  172. char *alloc_http_authenticator(const char *authenticator);
  173. void assert_connection_ok(connection_t *conn, time_t now);
  174. int connection_or_nonopen_was_started_here(or_connection_t *conn);
  175. void connection_dump_buffer_mem_stats(int severity);
  176. void remove_file_if_very_old(const char *fname, time_t now);
  177. #ifdef USE_BUFFEREVENTS
  178. int connection_type_uses_bufferevent(connection_t *conn);
  179. void connection_configure_bufferevent_callbacks(connection_t *conn);
  180. void connection_handle_read_cb(struct bufferevent *bufev, void *arg);
  181. void connection_handle_write_cb(struct bufferevent *bufev, void *arg);
  182. void connection_handle_event_cb(struct bufferevent *bufev, short event,
  183. void *arg);
  184. void connection_get_rate_limit_totals(uint64_t *read_out,
  185. uint64_t *written_out);
  186. void connection_enable_rate_limiting(connection_t *conn);
  187. #else
  188. #define connection_type_uses_bufferevent(c) (0)
  189. #endif
  190. #ifdef CONNECTION_PRIVATE
  191. STATIC void connection_free_(connection_t *conn);
  192. /* Used only by connection.c and test*.c */
  193. uint32_t bucket_millis_empty(int tokens_before, uint32_t last_empty_time,
  194. int tokens_after, int milliseconds_elapsed,
  195. const struct timeval *tvnow);
  196. void connection_buckets_note_empty_ts(uint32_t *timestamp_var,
  197. int tokens_before,
  198. size_t tokens_removed,
  199. const struct timeval *tvnow);
  200. #endif
  201. #endif