Back to index

tor  0.2.3.19-rc
connection.h
Go to the documentation of this file.
00001 /* Copyright (c) 2001 Matej Pfajfar.
00002  * Copyright (c) 2001-2004, Roger Dingledine.
00003  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00004  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00005 /* See LICENSE for licensing information */
00006 
00012 #ifndef _TOR_CONNECTION_H
00013 #define _TOR_CONNECTION_H
00014 
00015 /* XXXX For buf_datalen in inline function */
00016 #include "buffers.h"
00017 
00018 const char *conn_type_to_string(int type);
00019 const char *conn_state_to_string(int type, int state);
00020 
00021 dir_connection_t *dir_connection_new(int socket_family);
00022 or_connection_t *or_connection_new(int socket_family);
00023 edge_connection_t *edge_connection_new(int type, int socket_family);
00024 entry_connection_t *entry_connection_new(int type, int socket_family);
00025 control_connection_t *control_connection_new(int socket_family);
00026 listener_connection_t *listener_connection_new(int type, int socket_family);
00027 connection_t *connection_new(int type, int socket_family);
00028 
00029 void connection_link_connections(connection_t *conn_a, connection_t *conn_b);
00030 void connection_free(connection_t *conn);
00031 void connection_free_all(void);
00032 void connection_about_to_close_connection(connection_t *conn);
00033 void connection_close_immediate(connection_t *conn);
00034 void _connection_mark_for_close(connection_t *conn,int line, const char *file);
00035 
00036 #define connection_mark_for_close(c) \
00037   _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
00038 
00042 #define _connection_mark_and_flush(c,line,file)                         \
00043   do {                                                                  \
00044     connection_t *tmp_conn_ = (c);                                      \
00045     _connection_mark_for_close(tmp_conn_, (line), (file));              \
00046     tmp_conn_->hold_open_until_flushed = 1;                             \
00047     IF_HAS_BUFFEREVENT(tmp_conn_,                                       \
00048                        connection_start_writing(tmp_conn_));            \
00049   } while (0)
00050 
00051 #define connection_mark_and_flush(c)            \
00052   _connection_mark_and_flush((c), __LINE__, _SHORT_FILE_)
00053 
00054 void connection_expire_held_open(void);
00055 
00056 int connection_connect(connection_t *conn, const char *address,
00057                        const tor_addr_t *addr,
00058                        uint16_t port, int *socket_error);
00059 
00060 int connection_proxy_connect(connection_t *conn, int type);
00061 int connection_read_proxy_handshake(connection_t *conn);
00062 void log_failed_proxy_connection(connection_t *conn);
00063 int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
00064                        const connection_t *conn);
00065 
00066 int retry_all_listeners(smartlist_t *replaced_conns,
00067                         smartlist_t *new_conns,
00068                         int close_all_noncontrol);
00069 
00070 void connection_mark_all_noncontrol_listeners(void);
00071 void connection_mark_all_noncontrol_connections(void);
00072 
00073 ssize_t connection_bucket_write_limit(connection_t *conn, time_t now);
00074 int global_write_bucket_low(connection_t *conn, size_t attempt, int priority);
00075 void connection_bucket_init(void);
00076 void connection_bucket_refill(int seconds_elapsed, time_t now);
00077 
00078 int connection_handle_read(connection_t *conn);
00079 
00080 int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
00081 int connection_fetch_from_buf_line(connection_t *conn, char *data,
00082                                    size_t *data_len);
00083 int connection_fetch_from_buf_http(connection_t *conn,
00084                                char **headers_out, size_t max_headerlen,
00085                                char **body_out, size_t *body_used,
00086                                size_t max_bodylen, int force_complete);
00087 
00088 int connection_wants_to_flush(connection_t *conn);
00089 int connection_outbuf_too_full(connection_t *conn);
00090 int connection_handle_write(connection_t *conn, int force);
00091 int connection_flush(connection_t *conn);
00092 
00093 void _connection_write_to_buf_impl(const char *string, size_t len,
00094                                    connection_t *conn, int zlib);
00095 /* DOCDOC connection_write_to_buf */
00096 static void connection_write_to_buf(const char *string, size_t len,
00097                                     connection_t *conn);
00098 /* DOCDOC connection_write_to_buf_zlib */
00099 static void connection_write_to_buf_zlib(const char *string, size_t len,
00100                                          dir_connection_t *conn, int done);
00101 static INLINE void
00102 connection_write_to_buf(const char *string, size_t len, connection_t *conn)
00103 {
00104   _connection_write_to_buf_impl(string, len, conn, 0);
00105 }
00106 static INLINE void
00107 connection_write_to_buf_zlib(const char *string, size_t len,
00108                              dir_connection_t *conn, int done)
00109 {
00110   _connection_write_to_buf_impl(string, len, TO_CONN(conn), done ? -1 : 1);
00111 }
00112 
00113 /* DOCDOC connection_get_inbuf_len */
00114 static size_t connection_get_inbuf_len(connection_t *conn);
00115 /* DOCDOC connection_get_outbuf_len */
00116 static size_t connection_get_outbuf_len(connection_t *conn);
00117 
00118 static INLINE size_t
00119 connection_get_inbuf_len(connection_t *conn)
00120 {
00121   IF_HAS_BUFFEREVENT(conn, {
00122     return evbuffer_get_length(bufferevent_get_input(conn->bufev));
00123   }) ELSE_IF_NO_BUFFEREVENT {
00124     return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
00125   }
00126 }
00127 
00128 static INLINE size_t
00129 connection_get_outbuf_len(connection_t *conn)
00130 {
00131   IF_HAS_BUFFEREVENT(conn, {
00132     return evbuffer_get_length(bufferevent_get_output(conn->bufev));
00133   }) ELSE_IF_NO_BUFFEREVENT {
00134     return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
00135   }
00136 }
00137 
00138 connection_t *connection_get_by_global_id(uint64_t id);
00139 
00140 connection_t *connection_get_by_type(int type);
00141 connection_t *connection_get_by_type_purpose(int type, int purpose);
00142 connection_t *connection_get_by_type_addr_port_purpose(int type,
00143                                                    const tor_addr_t *addr,
00144                                                    uint16_t port, int purpose);
00145 connection_t *connection_get_by_type_state(int type, int state);
00146 connection_t *connection_get_by_type_state_rendquery(int type, int state,
00147                                                      const char *rendquery);
00148 dir_connection_t *connection_dir_get_by_purpose_and_resource(
00149                                            int state, const char *resource);
00150 
00151 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
00152 int connection_is_listener(connection_t *conn);
00153 int connection_state_is_open(connection_t *conn);
00154 int connection_state_is_connecting(connection_t *conn);
00155 
00156 char *alloc_http_authenticator(const char *authenticator);
00157 
00158 void assert_connection_ok(connection_t *conn, time_t now);
00159 int connection_or_nonopen_was_started_here(or_connection_t *conn);
00160 void connection_dump_buffer_mem_stats(int severity);
00161 void remove_file_if_very_old(const char *fname, time_t now);
00162 
00163 #ifdef USE_BUFFEREVENTS
00164 int connection_type_uses_bufferevent(connection_t *conn);
00165 void connection_configure_bufferevent_callbacks(connection_t *conn);
00166 void connection_handle_read_cb(struct bufferevent *bufev, void *arg);
00167 void connection_handle_write_cb(struct bufferevent *bufev, void *arg);
00168 void connection_handle_event_cb(struct bufferevent *bufev, short event,
00169                                  void *arg);
00170 void connection_get_rate_limit_totals(uint64_t *read_out,
00171                                       uint64_t *written_out);
00172 void connection_enable_rate_limiting(connection_t *conn);
00173 #else
00174 #define connection_type_uses_bufferevent(c) (0)
00175 #endif
00176 
00177 #endif
00178