connection.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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-2011, 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,int line, const char *file);
  29. #define connection_mark_for_close(c) \
  30. _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
  31. /**
  32. * Mark 'c' for close, but try to hold it open until all the data is written.
  33. */
  34. #define _connection_mark_and_flush(c,line,file) \
  35. do { \
  36. connection_t *tmp_conn_ = (c); \
  37. _connection_mark_for_close(tmp_conn_, (line), (file)); \
  38. tmp_conn_->hold_open_until_flushed = 1; \
  39. IF_HAS_BUFFEREVENT(tmp_conn_, \
  40. connection_start_writing(tmp_conn_)); \
  41. } while (0)
  42. #define connection_mark_and_flush(c) \
  43. _connection_mark_and_flush((c), __LINE__, _SHORT_FILE_)
  44. void connection_expire_held_open(void);
  45. int connection_connect(connection_t *conn, const char *address,
  46. const tor_addr_t *addr,
  47. uint16_t port, int *socket_error);
  48. int connection_proxy_connect(connection_t *conn, int type);
  49. int connection_read_proxy_handshake(connection_t *conn);
  50. void log_failed_proxy_connection(connection_t *conn);
  51. int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
  52. const connection_t *conn);
  53. int retry_all_listeners(smartlist_t *replaced_conns,
  54. smartlist_t *new_conns);
  55. void connection_mark_all_noncontrol_listeners(void);
  56. void connection_mark_all_noncontrol_connections(void);
  57. ssize_t connection_bucket_write_limit(connection_t *conn, time_t now);
  58. int global_write_bucket_low(connection_t *conn, size_t attempt, int priority);
  59. void connection_bucket_init(void);
  60. void connection_bucket_refill(int seconds_elapsed, time_t now);
  61. int connection_handle_read(connection_t *conn);
  62. int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
  63. int connection_fetch_from_buf_line(connection_t *conn, char *data,
  64. size_t *data_len);
  65. int connection_fetch_from_buf_http(connection_t *conn,
  66. char **headers_out, size_t max_headerlen,
  67. char **body_out, size_t *body_used,
  68. size_t max_bodylen, int force_complete);
  69. int connection_wants_to_flush(connection_t *conn);
  70. int connection_outbuf_too_full(connection_t *conn);
  71. int connection_handle_write(connection_t *conn, int force);
  72. int connection_flush(connection_t *conn);
  73. void _connection_write_to_buf_impl(const char *string, size_t len,
  74. connection_t *conn, int zlib);
  75. static void connection_write_to_buf(const char *string, size_t len,
  76. connection_t *conn);
  77. static void connection_write_to_buf_zlib(const char *string, size_t len,
  78. dir_connection_t *conn, int done);
  79. static INLINE void
  80. connection_write_to_buf(const char *string, size_t len, connection_t *conn)
  81. {
  82. _connection_write_to_buf_impl(string, len, conn, 0);
  83. }
  84. static INLINE void
  85. connection_write_to_buf_zlib(const char *string, size_t len,
  86. dir_connection_t *conn, int done)
  87. {
  88. _connection_write_to_buf_impl(string, len, TO_CONN(conn), done ? -1 : 1);
  89. }
  90. static size_t connection_get_inbuf_len(connection_t *conn);
  91. static size_t connection_get_outbuf_len(connection_t *conn);
  92. static INLINE size_t
  93. connection_get_inbuf_len(connection_t *conn)
  94. {
  95. IF_HAS_BUFFEREVENT(conn, {
  96. return evbuffer_get_length(bufferevent_get_input(conn->bufev));
  97. }) ELSE_IF_NO_BUFFEREVENT {
  98. return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
  99. }
  100. }
  101. static INLINE size_t
  102. connection_get_outbuf_len(connection_t *conn)
  103. {
  104. IF_HAS_BUFFEREVENT(conn, {
  105. return evbuffer_get_length(bufferevent_get_output(conn->bufev));
  106. }) ELSE_IF_NO_BUFFEREVENT {
  107. return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
  108. }
  109. }
  110. connection_t *connection_get_by_global_id(uint64_t id);
  111. connection_t *connection_get_by_type(int type);
  112. connection_t *connection_get_by_type_purpose(int type, int purpose);
  113. connection_t *connection_get_by_type_addr_port_purpose(int type,
  114. const tor_addr_t *addr,
  115. uint16_t port, int purpose);
  116. connection_t *connection_get_by_type_state(int type, int state);
  117. connection_t *connection_get_by_type_state_rendquery(int type, int state,
  118. const char *rendquery);
  119. dir_connection_t *connection_dir_get_by_purpose_and_resource(
  120. int state, const char *resource);
  121. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  122. int connection_is_listener(connection_t *conn);
  123. int connection_state_is_open(connection_t *conn);
  124. int connection_state_is_connecting(connection_t *conn);
  125. char *alloc_http_authenticator(const char *authenticator);
  126. void assert_connection_ok(connection_t *conn, time_t now);
  127. int connection_or_nonopen_was_started_here(or_connection_t *conn);
  128. void connection_dump_buffer_mem_stats(int severity);
  129. void remove_file_if_very_old(const char *fname, time_t now);
  130. #ifdef USE_BUFFEREVENTS
  131. int connection_type_uses_bufferevent(connection_t *conn);
  132. void connection_configure_bufferevent_callbacks(connection_t *conn);
  133. void connection_handle_read_cb(struct bufferevent *bufev, void *arg);
  134. void connection_handle_write_cb(struct bufferevent *bufev, void *arg);
  135. void connection_handle_event_cb(struct bufferevent *bufev, short event,
  136. void *arg);
  137. void connection_get_rate_limit_totals(uint64_t *read_out,
  138. uint64_t *written_out);
  139. void connection_enable_rate_limiting(connection_t *conn);
  140. #else
  141. #define connection_type_uses_bufferevent(c) (0)
  142. #endif
  143. #endif