TOR: update to v0.2.5.12
[tomato.git] / release / src / router / tor / src / or / connection.h
blob13dcbcd919e6c5419355dc7568638f81b35dadca
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 */
7 /**
8 * \file connection.h
9 * \brief Header file for connection.c.
10 **/
12 #ifndef TOR_CONNECTION_H
13 #define TOR_CONNECTION_H
15 /* XXXX For buf_datalen in inline function */
16 #include "buffers.h"
18 const char *conn_type_to_string(int type);
19 const char *conn_state_to_string(int type, int state);
21 dir_connection_t *dir_connection_new(int socket_family);
22 or_connection_t *or_connection_new(int type, int socket_family);
23 edge_connection_t *edge_connection_new(int type, int socket_family);
24 entry_connection_t *entry_connection_new(int type, int socket_family);
25 control_connection_t *control_connection_new(int socket_family);
26 listener_connection_t *listener_connection_new(int type, int socket_family);
27 connection_t *connection_new(int type, int socket_family);
29 void connection_link_connections(connection_t *conn_a, connection_t *conn_b);
30 void connection_free(connection_t *conn);
31 void connection_free_all(void);
32 void connection_about_to_close_connection(connection_t *conn);
33 void connection_close_immediate(connection_t *conn);
34 void connection_mark_for_close_(connection_t *conn,
35 int line, const char *file);
36 void connection_mark_for_close_internal_(connection_t *conn,
37 int line, const char *file);
39 #define connection_mark_for_close(c) \
40 connection_mark_for_close_((c), __LINE__, SHORT_FILE__)
41 #define connection_mark_for_close_internal(c) \
42 connection_mark_for_close_internal_((c), __LINE__, SHORT_FILE__)
44 /**
45 * Mark 'c' for close, but try to hold it open until all the data is written.
46 * Use the _internal versions of connection_mark_for_close; this should be
47 * called when you either are sure that if this is an or_connection_t the
48 * controlling channel has been notified (e.g. with
49 * connection_or_notify_error()), or you actually are the
50 * connection_or_close_for_error() or connection_or_close_normally function.
51 * For all other cases, use connection_mark_and_flush() instead, which
52 * checks for or_connection_t properly, instead. See below.
54 #define connection_mark_and_flush_internal_(c,line,file) \
55 do { \
56 connection_t *tmp_conn_ = (c); \
57 connection_mark_for_close_internal_(tmp_conn_, (line), (file)); \
58 tmp_conn_->hold_open_until_flushed = 1; \
59 IF_HAS_BUFFEREVENT(tmp_conn_, \
60 connection_start_writing(tmp_conn_)); \
61 } while (0)
63 #define connection_mark_and_flush_internal(c) \
64 connection_mark_and_flush_internal_((c), __LINE__, SHORT_FILE__)
66 /**
67 * Mark 'c' for close, but try to hold it open until all the data is written.
69 #define connection_mark_and_flush_(c,line,file) \
70 do { \
71 connection_t *tmp_conn_ = (c); \
72 if (tmp_conn_->type == CONN_TYPE_OR) { \
73 log_warn(LD_CHANNEL | LD_BUG, \
74 "Something tried to close (and flush) an or_connection_t" \
75 " without going through channels at %s:%d", \
76 file, line); \
77 connection_or_close_for_error(TO_OR_CONN(tmp_conn_), 1); \
78 } else { \
79 connection_mark_and_flush_internal_(c, line, file); \
80 } \
81 } while (0)
83 #define connection_mark_and_flush(c) \
84 connection_mark_and_flush_((c), __LINE__, SHORT_FILE__)
86 void connection_expire_held_open(void);
88 int connection_connect(connection_t *conn, const char *address,
89 const tor_addr_t *addr,
90 uint16_t port, int *socket_error);
92 /** Maximum size of information that we can fit into SOCKS5 username
93 or password fields. */
94 #define MAX_SOCKS5_AUTH_FIELD_SIZE 255
96 /** Total maximum size of information that we can fit into SOCKS5
97 username and password fields. */
98 #define MAX_SOCKS5_AUTH_SIZE_TOTAL 2*MAX_SOCKS5_AUTH_FIELD_SIZE
100 int connection_proxy_connect(connection_t *conn, int type);
101 int connection_read_proxy_handshake(connection_t *conn);
102 void log_failed_proxy_connection(connection_t *conn);
103 int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
104 const connection_t *conn);
106 int retry_all_listeners(smartlist_t *replaced_conns,
107 smartlist_t *new_conns,
108 int close_all_noncontrol);
110 void connection_mark_all_noncontrol_listeners(void);
111 void connection_mark_all_noncontrol_connections(void);
113 ssize_t connection_bucket_write_limit(connection_t *conn, time_t now);
114 int global_write_bucket_low(connection_t *conn, size_t attempt, int priority);
115 void connection_bucket_init(void);
116 void connection_bucket_refill(int seconds_elapsed, time_t now);
118 int connection_handle_read(connection_t *conn);
120 int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
121 int connection_fetch_from_buf_line(connection_t *conn, char *data,
122 size_t *data_len);
123 int connection_fetch_from_buf_http(connection_t *conn,
124 char **headers_out, size_t max_headerlen,
125 char **body_out, size_t *body_used,
126 size_t max_bodylen, int force_complete);
128 int connection_wants_to_flush(connection_t *conn);
129 int connection_outbuf_too_full(connection_t *conn);
130 int connection_handle_write(connection_t *conn, int force);
131 int connection_flush(connection_t *conn);
133 MOCK_DECL(void, connection_write_to_buf_impl_,
134 (const char *string, size_t len, connection_t *conn, int zlib));
135 /* DOCDOC connection_write_to_buf */
136 static void connection_write_to_buf(const char *string, size_t len,
137 connection_t *conn);
138 /* DOCDOC connection_write_to_buf_zlib */
139 static void connection_write_to_buf_zlib(const char *string, size_t len,
140 dir_connection_t *conn, int done);
141 static INLINE void
142 connection_write_to_buf(const char *string, size_t len, connection_t *conn)
144 connection_write_to_buf_impl_(string, len, conn, 0);
146 static INLINE void
147 connection_write_to_buf_zlib(const char *string, size_t len,
148 dir_connection_t *conn, int done)
150 connection_write_to_buf_impl_(string, len, TO_CONN(conn), done ? -1 : 1);
153 /* DOCDOC connection_get_inbuf_len */
154 static size_t connection_get_inbuf_len(connection_t *conn);
155 /* DOCDOC connection_get_outbuf_len */
156 static size_t connection_get_outbuf_len(connection_t *conn);
158 static INLINE size_t
159 connection_get_inbuf_len(connection_t *conn)
161 IF_HAS_BUFFEREVENT(conn, {
162 return evbuffer_get_length(bufferevent_get_input(conn->bufev));
163 }) ELSE_IF_NO_BUFFEREVENT {
164 return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
168 static INLINE size_t
169 connection_get_outbuf_len(connection_t *conn)
171 IF_HAS_BUFFEREVENT(conn, {
172 return evbuffer_get_length(bufferevent_get_output(conn->bufev));
173 }) ELSE_IF_NO_BUFFEREVENT {
174 return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
178 connection_t *connection_get_by_global_id(uint64_t id);
180 connection_t *connection_get_by_type(int type);
181 connection_t *connection_get_by_type_addr_port_purpose(int type,
182 const tor_addr_t *addr,
183 uint16_t port, int purpose);
184 connection_t *connection_get_by_type_state(int type, int state);
185 connection_t *connection_get_by_type_state_rendquery(int type, int state,
186 const char *rendquery);
187 dir_connection_t *connection_dir_get_by_purpose_and_resource(
188 int state, const char *resource);
190 int any_other_active_or_conns(const or_connection_t *this_conn);
192 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
193 int connection_is_listener(connection_t *conn);
194 int connection_state_is_open(connection_t *conn);
195 int connection_state_is_connecting(connection_t *conn);
197 char *alloc_http_authenticator(const char *authenticator);
199 void assert_connection_ok(connection_t *conn, time_t now);
200 int connection_or_nonopen_was_started_here(or_connection_t *conn);
201 void connection_dump_buffer_mem_stats(int severity);
202 void remove_file_if_very_old(const char *fname, time_t now);
204 #ifdef USE_BUFFEREVENTS
205 int connection_type_uses_bufferevent(connection_t *conn);
206 void connection_configure_bufferevent_callbacks(connection_t *conn);
207 void connection_handle_read_cb(struct bufferevent *bufev, void *arg);
208 void connection_handle_write_cb(struct bufferevent *bufev, void *arg);
209 void connection_handle_event_cb(struct bufferevent *bufev, short event,
210 void *arg);
211 void connection_get_rate_limit_totals(uint64_t *read_out,
212 uint64_t *written_out);
213 void connection_enable_rate_limiting(connection_t *conn);
214 #else
215 #define connection_type_uses_bufferevent(c) (0)
216 #endif
218 #ifdef CONNECTION_PRIVATE
219 STATIC void connection_free_(connection_t *conn);
221 /* Used only by connection.c and test*.c */
222 uint32_t bucket_millis_empty(int tokens_before, uint32_t last_empty_time,
223 int tokens_after, int milliseconds_elapsed,
224 const struct timeval *tvnow);
225 void connection_buckets_note_empty_ts(uint32_t *timestamp_var,
226 int tokens_before,
227 size_t tokens_removed,
228 const struct timeval *tvnow);
229 #endif
231 #endif