tor  0.4.0.1-alpha
connection.h
Go to the documentation of this file.
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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
12 #ifndef TOR_CONNECTION_H
13 #define TOR_CONNECTION_H
14 
16 
17 struct buf_t;
18 
19 #define CONN_TYPE_MIN_ 3
20 
21 #define CONN_TYPE_OR_LISTENER 3
22 
24 #define CONN_TYPE_OR 4
25 
26 #define CONN_TYPE_EXIT 5
27 
28 #define CONN_TYPE_AP_LISTENER 6
29 
31 #define CONN_TYPE_AP 7
32 
33 #define CONN_TYPE_DIR_LISTENER 8
34 
35 #define CONN_TYPE_DIR 9
36 /* Type 10 is unused. */
38 #define CONN_TYPE_CONTROL_LISTENER 11
39 
40 #define CONN_TYPE_CONTROL 12
41 
43 #define CONN_TYPE_AP_TRANS_LISTENER 13
44 
46 #define CONN_TYPE_AP_NATD_LISTENER 14
47 
48 #define CONN_TYPE_AP_DNS_LISTENER 15
49 
51 #define CONN_TYPE_EXT_OR 16
52 
53 #define CONN_TYPE_EXT_OR_LISTENER 17
54 
55 #define CONN_TYPE_AP_HTTP_CONNECT_LISTENER 18
56 
57 #define CONN_TYPE_MAX_ 19
58 /* !!!! If _CONN_TYPE_MAX is ever over 31, we must grow the type field in
59  * connection_t. */
60 
61 /* Proxy client handshake states */
62 /* We use a proxy but we haven't even connected to it yet. */
63 #define PROXY_INFANT 1
64 /* We use an HTTP proxy and we've sent the CONNECT command. */
65 #define PROXY_HTTPS_WANT_CONNECT_OK 2
66 /* We use a SOCKS4 proxy and we've sent the CONNECT command. */
67 #define PROXY_SOCKS4_WANT_CONNECT_OK 3
68 /* We use a SOCKS5 proxy and we try to negotiate without
69  any authentication . */
70 #define PROXY_SOCKS5_WANT_AUTH_METHOD_NONE 4
71 /* We use a SOCKS5 proxy and we try to negotiate with
72  Username/Password authentication . */
73 #define PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929 5
74 /* We use a SOCKS5 proxy and we just sent our credentials. */
75 #define PROXY_SOCKS5_WANT_AUTH_RFC1929_OK 6
76 /* We use a SOCKS5 proxy and we just sent our CONNECT command. */
77 #define PROXY_SOCKS5_WANT_CONNECT_OK 7
78 /* We use a proxy and we CONNECTed successfully!. */
79 #define PROXY_CONNECTED 8
80 
82 #define LISTENER_STATE_READY 0
83 
89 typedef struct
90 {
91  connection_t *old_conn; /* Old listener connection to be replaced */
92  const port_cfg_t *new_port; /* New port configuration */
94 
95 const char *conn_type_to_string(int type);
96 const char *conn_state_to_string(int type, int state);
98 
99 dir_connection_t *dir_connection_new(int socket_family);
100 or_connection_t *or_connection_new(int type, int socket_family);
101 edge_connection_t *edge_connection_new(int type, int socket_family);
102 entry_connection_t *entry_connection_new(int type, int socket_family);
103 control_connection_t *control_connection_new(int socket_family);
104 listener_connection_t *listener_connection_new(int type, int socket_family);
105 connection_t *connection_new(int type, int socket_family);
107  const listener_connection_t *listener);
109 MOCK_DECL(void,connection_free_,(connection_t *conn));
110 #define connection_free(conn) \
111  FREE_AND_NULL(connection_t, connection_free_, (conn))
112 void connection_free_all(void);
116  int line, const char *file);
117 MOCK_DECL(void, connection_mark_for_close_internal_,
118  (connection_t *conn, int line, const char *file));
119 
120 #define connection_mark_for_close(c) \
121  connection_mark_for_close_((c), __LINE__, SHORT_FILE__)
122 #define connection_mark_for_close_internal(c) \
123  connection_mark_for_close_internal_((c), __LINE__, SHORT_FILE__)
124 
135 #define connection_mark_and_flush_internal_(c,line,file) \
136  do { \
137  connection_t *tmp_conn__ = (c); \
138  connection_mark_for_close_internal_(tmp_conn__, (line), (file)); \
139  tmp_conn__->hold_open_until_flushed = 1; \
140  } while (0)
141 
142 #define connection_mark_and_flush_internal(c) \
143  connection_mark_and_flush_internal_((c), __LINE__, SHORT_FILE__)
144 
148 #define connection_mark_and_flush_(c,line,file) \
149  do { \
150  connection_t *tmp_conn_ = (c); \
151  if (tmp_conn_->type == CONN_TYPE_OR) { \
152  log_warn(LD_CHANNEL | LD_BUG, \
153  "Something tried to close (and flush) an or_connection_t" \
154  " without going through channels at %s:%d", \
155  file, line); \
156  connection_or_close_for_error(TO_OR_CONN(tmp_conn_), 1); \
157  } else { \
158  connection_mark_and_flush_internal_(c, line, file); \
159  } \
160  } while (0)
161 
162 #define connection_mark_and_flush(c) \
163  connection_mark_and_flush_((c), __LINE__, SHORT_FILE__)
164 
165 void connection_expire_held_open(void);
166 
167 int connection_connect(connection_t *conn, const char *address,
168  const tor_addr_t *addr,
169  uint16_t port, int *socket_error);
170 
171 #ifdef HAVE_SYS_UN_H
172 
173 int connection_connect_unix(connection_t *conn, const char *socket_path,
174  int *socket_error);
175 
176 #endif /* defined(HAVE_SYS_UN_H) */
177 
180 #define MAX_SOCKS5_AUTH_FIELD_SIZE 255
181 
184 #define MAX_SOCKS5_AUTH_SIZE_TOTAL 2*MAX_SOCKS5_AUTH_FIELD_SIZE
185 
186 int connection_proxy_connect(connection_t *conn, int type);
189 int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
190  const connection_t *conn);
191 
192 int retry_all_listeners(smartlist_t *new_conns,
193  int close_all_noncontrol);
194 
197 
198 ssize_t connection_bucket_write_limit(connection_t *conn, time_t now);
199 int global_write_bucket_low(connection_t *conn, size_t attempt, int priority);
200 void connection_bucket_init(void);
201 void connection_bucket_adjust(const or_options_t *options);
202 void connection_bucket_refill_all(time_t now,
203  uint32_t now_ts);
204 void connection_read_bw_exhausted(connection_t *conn, bool is_global_bw);
205 void connection_write_bw_exhausted(connection_t *conn, bool is_global_bw);
208 
209 int connection_handle_read(connection_t *conn);
210 
211 int connection_buf_get_bytes(char *string, size_t len, connection_t *conn);
212 int connection_buf_get_line(connection_t *conn, char *data,
213  size_t *data_len);
215  char **headers_out, size_t max_headerlen,
216  char **body_out, size_t *body_used,
217  size_t max_bodylen, int force_complete);
218 
221 int connection_handle_write(connection_t *conn, int force);
222 int connection_flush(connection_t *conn);
223 
224 MOCK_DECL(void, connection_write_to_buf_impl_,
225  (const char *string, size_t len, connection_t *conn, int zlib));
226 /* DOCDOC connection_write_to_buf */
227 static void connection_buf_add(const char *string, size_t len,
228  connection_t *conn);
229 static inline void
230 connection_buf_add(const char *string, size_t len, connection_t *conn)
231 {
232  connection_write_to_buf_impl_(string, len, conn, 0);
233 }
234 void connection_buf_add_compress(const char *string, size_t len,
235  dir_connection_t *conn, int done);
236 void connection_buf_add_buf(connection_t *conn, struct buf_t *buf);
237 
238 size_t connection_get_inbuf_len(connection_t *conn);
239 size_t connection_get_outbuf_len(connection_t *conn);
241 
243 MOCK_DECL(connection_t *,connection_get_by_type_nonlinked,(int type));
244 MOCK_DECL(connection_t *,connection_get_by_type_addr_port_purpose,(int type,
245  const tor_addr_t *addr,
246  uint16_t port, int purpose));
247 connection_t *connection_get_by_type_state(int type, int state);
249  const char *rendquery);
250 smartlist_t *connection_list_by_type_state(int type, int state);
251 smartlist_t *connection_list_by_type_purpose(int type, int purpose);
253  int purpose,
254  const char *resource);
256  int purpose,
257  const char *resource,
258  int state);
259 
260 #define CONN_LEN_AND_FREE_TEMPLATE(sl) \
261  STMT_BEGIN \
262  int len = smartlist_len(sl); \
263  smartlist_free(sl); \
264  return len; \
265  STMT_END
266 
269 static inline int
271  int purpose,
272  const char *resource)
273 {
275  purpose,
276  resource);
277  CONN_LEN_AND_FREE_TEMPLATE(conns);
278 }
279 
282 static inline int
284  int purpose,
285  const char *resource,
286  int state)
287 {
288  smartlist_t *conns =
290  purpose,
291  resource,
292  state);
293  CONN_LEN_AND_FREE_TEMPLATE(conns);
294 }
295 
296 #undef CONN_LEN_AND_FREE_TEMPLATE
297 
298 int any_other_active_or_conns(const or_connection_t *this_conn);
299 
300 /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
301 #define connection_speaks_cells(conn) (((conn)->type == CONN_TYPE_OR) || 0)
305 
306 char *alloc_http_authenticator(const char *authenticator);
307 
308 void assert_connection_ok(connection_t *conn, time_t now);
310 void connection_dump_buffer_mem_stats(int severity);
311 
312 MOCK_DECL(void, clock_skew_warning,
313  (const connection_t *conn, long apparent_skew, int trusted,
314  log_domain_mask_t domain, const char *received,
315  const char *source));
316 
318 void connection_check_oos(int n_socks, int failed);
319 
326 #define CONN_LOG_PROTECT(conn, stmt) \
327  STMT_BEGIN \
328  int _log_conn_is_control; \
329  tor_assert(conn); \
330  _log_conn_is_control = (conn->type == CONN_TYPE_CONTROL); \
331  if (_log_conn_is_control) \
332  disable_control_logging(); \
333  STMT_BEGIN stmt; STMT_END; \
334  if (_log_conn_is_control) \
335  enable_control_logging(); \
336  STMT_END
337 
338 #ifdef CONNECTION_PRIVATE
339 STATIC void connection_free_minimal(connection_t *conn);
340 
341 /* Used only by connection.c and test*.c */
342 MOCK_DECL(STATIC int,connection_connect_sockaddr,
343  (connection_t *conn,
344  const struct sockaddr *sa,
345  socklen_t sa_len,
346  const struct sockaddr *bindaddr,
347  socklen_t bindaddr_len,
348  int *socket_error));
349 MOCK_DECL(STATIC void, kill_conn_list_for_oos, (smartlist_t *conns));
350 MOCK_DECL(STATIC smartlist_t *, pick_oos_victims, (int n));
351 
352 #endif /* defined(CONNECTION_PRIVATE) */
353 
354 #endif /* !defined(TOR_CONNECTION_H) */
listener_connection_t * TO_LISTENER_CONN(connection_t *)
Definition: connection.c:219
int connection_is_listener(connection_t *conn)
Definition: connection.c:4590
const char * conn_state_to_string(int type, int state)
Definition: connection.c:273
int connection_outbuf_too_full(connection_t *conn)
Definition: connection.c:3907
uint32_t log_domain_mask_t
Definition: log.h:132
void connection_close_immediate(connection_t *conn)
Definition: connection.c:841
connection_t * connection_get_by_type(int type)
Definition: connection.c:4444
static int connection_dir_count_by_purpose_resource_and_state(int purpose, const char *resource, int state)
Definition: connection.h:283
entry_connection_t * entry_connection_new(int type, int socket_family)
Definition: connection.c:394
listener_connection_t * listener_connection_new(int type, int socket_family)
Definition: connection.c:436
void connection_mark_all_noncontrol_connections(void)
Definition: connection.c:2995
void connection_dump_buffer_mem_stats(int severity)
Definition: connection.c:5169
void connection_read_bw_exhausted(connection_t *conn, bool is_global_bw)
Definition: connection.c:3321
int retry_all_listeners(smartlist_t *new_conns, int close_all_noncontrol)
Definition: connection.c:2887
connection_t * connection_get_by_type_state_rendquery(int type, int state, const char *rendquery)
Definition: connection.c:4473
edge_connection_t * edge_connection_new(int type, int socket_family)
Definition: connection.c:413
STATIC void connection_free_minimal(connection_t *conn)
Definition: connection.c:569
void connection_check_oos(int n_socks, int failed)
Definition: connection.c:5085
int connection_state_is_open(connection_t *conn)
Definition: connection.c:4609
int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type, const connection_t *conn)
Definition: connection.c:5370
void connection_write_bw_exhausted(connection_t *conn, bool is_global_bw)
Definition: connection.c:3336
void connection_buf_add_buf(connection_t *conn, struct buf_t *buf)
Definition: connection.c:4357
int global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
Definition: connection.c:3191
void connection_about_to_close_connection(connection_t *conn)
Definition: connection.c:810
const char * conn_type_to_string(int type)
Definition: connection.c:241
void connection_free_all(void)
Definition: connection.c:5460
int connection_buf_get_line(connection_t *conn, char *data, size_t *data_len)
Definition: connection.c:3876
int connection_state_is_connecting(connection_t *conn)
Definition: connection.c:4629
int connection_connect(connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
Definition: connection.c:2176
dir_connection_t * dir_connection_new(int socket_family)
Definition: connection.c:358
int connection_wants_to_flush(connection_t *conn)
Definition: connection.c:3897
void connection_bucket_adjust(const or_options_t *options)
Definition: connection.c:3429
int connection_or_nonopen_was_started_here(or_connection_t *conn)
int conn_listener_type_supports_af_unix(int type)
Definition: connection.c:551
int connection_buf_get_bytes(char *string, size_t len, connection_t *conn)
Definition: connection.c:3869
connection_t * connection_new(int type, int socket_family)
Definition: connection.c:448
int connection_flush(connection_t *conn)
Definition: connection.c:4236
int connection_fetch_from_buf_http(connection_t *conn, char **headers_out, size_t max_headerlen, char **body_out, size_t *body_used, size_t max_bodylen, int force_complete)
Definition: connection.c:3885
int connection_init_accepted_conn(connection_t *conn, const listener_connection_t *listener)
Definition: connection.c:1866
void assert_connection_ok(connection_t *conn, time_t now)
Definition: connection.c:5218
smartlist_t * connection_dir_list_by_purpose_resource_and_state(int purpose, const char *resource, int state)
Definition: connection.c:4543
or_connection_t * or_connection_new(int type, int socket_family)
Definition: connection.c:373
int any_other_active_or_conns(const or_connection_t *this_conn)
Definition: connection.c:4574
control_connection_t * control_connection_new(int socket_family)
Definition: connection.c:424
void connection_consider_empty_write_buckets(connection_t *conn)
Definition: connection.c:3376
int connection_read_proxy_handshake(connection_t *conn)
Definition: connection.c:2587
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
smartlist_t * connection_dir_list_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.c:4525
char * alloc_http_authenticator(const char *authenticator)
Definition: connection.c:4652
void connection_mark_for_close_(connection_t *conn, int line, const char *file)
Definition: connection.c:875
int connection_is_moribund(connection_t *conn)
Definition: connection.c:5068
ssize_t connection_bucket_write_limit(connection_t *conn, time_t now)
Definition: connection.c:3141
void connection_bucket_init(void)
Definition: connection.c:3404
void connection_mark_all_noncontrol_listeners(void)
Definition: connection.c:2981
void log_failed_proxy_connection(connection_t *conn)
Definition: connection.c:5422
void connection_consider_empty_read_buckets(connection_t *conn)
Definition: connection.c:3347
static int connection_dir_count_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.h:270
void connection_expire_held_open(void)
Definition: connection.c:952
void connection_link_connections(connection_t *conn_a, connection_t *conn_b)
Definition: connection.c:537
connection_t * connection_get_by_global_id(uint64_t id)
Definition: connection.c:4436
int connection_proxy_connect(connection_t *conn, int type)
Definition: connection.c:2322
connection_t * connection_get_by_type_state(int type, int state)
Definition: connection.c:4453