connection.h 9.1 KB

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