Tor  0.4.3.0-alpha-dev
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 
7 /**
8  * \file connection.h
9  * \brief Header file for connection.c.
10  **/
11 
12 #ifndef TOR_CONNECTION_H
13 #define TOR_CONNECTION_H
14 
16 
17 struct buf_t;
18 
19 #define CONN_TYPE_MIN_ 3
20 /** Type for sockets listening for OR connections. */
21 #define CONN_TYPE_OR_LISTENER 3
22 /** A bidirectional TLS connection transmitting a sequence of cells.
23  * May be from an OR to an OR, or from an OP to an OR. */
24 #define CONN_TYPE_OR 4
25 /** A TCP connection from an onion router to a stream's destination. */
26 #define CONN_TYPE_EXIT 5
27 /** Type for sockets listening for SOCKS connections. */
28 #define CONN_TYPE_AP_LISTENER 6
29 /** A SOCKS proxy connection from the user application to the onion
30  * proxy. */
31 #define CONN_TYPE_AP 7
32 /** Type for sockets listening for HTTP connections to the directory server. */
33 #define CONN_TYPE_DIR_LISTENER 8
34 /** Type for HTTP connections to the directory server. */
35 #define CONN_TYPE_DIR 9
36 /* Type 10 is unused. */
37 /** Type for listening for connections from user interface process. */
38 #define CONN_TYPE_CONTROL_LISTENER 11
39 /** Type for connections from user interface process. */
40 #define CONN_TYPE_CONTROL 12
41 /** Type for sockets listening for transparent connections redirected by pf or
42  * netfilter. */
43 #define CONN_TYPE_AP_TRANS_LISTENER 13
44 /** Type for sockets listening for transparent connections redirected by
45  * natd. */
46 #define CONN_TYPE_AP_NATD_LISTENER 14
47 /** Type for sockets listening for DNS requests. */
48 #define CONN_TYPE_AP_DNS_LISTENER 15
49 
50 /** Type for connections from the Extended ORPort. */
51 #define CONN_TYPE_EXT_OR 16
52 /** Type for sockets listening for Extended ORPort connections. */
53 #define CONN_TYPE_EXT_OR_LISTENER 17
54 /** Type for sockets listening for HTTP CONNECT tunnel connections. */
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 
81 /** State for any listener connection. */
82 #define LISTENER_STATE_READY 0
83 
84 /**
85  * This struct associates an old listener connection to be replaced
86  * by new connection described by port configuration. Only used when
87  * moving listeners to/from wildcard IP address.
88  */
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);
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);
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 
125 /**
126  * Mark 'c' for close, but try to hold it open until all the data is written.
127  * Use the _internal versions of connection_mark_for_close; this should be
128  * called when you either are sure that if this is an or_connection_t the
129  * controlling channel has been notified (e.g. with
130  * connection_or_notify_error()), or you actually are the
131  * connection_or_close_for_error() or connection_or_close_normally function.
132  * For all other cases, use connection_mark_and_flush() instead, which
133  * checks for or_connection_t properly, instead. See below.
134  */
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 
145 /**
146  * Mark 'c' for close, but try to hold it open until all the data is written.
147  */
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 
178 /** Maximum size of information that we can fit into SOCKS5 username
179  or password fields. */
180 #define MAX_SOCKS5_AUTH_FIELD_SIZE 255
181 
182 /** Total maximum size of information that we can fit into SOCKS5
183  username and password fields. */
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  int *is_pt_out, 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 
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 void connection_dir_buf_add(const char *string, size_t len,
230  dir_connection_t *dir_conn, int done);
231 static inline void
232 connection_buf_add(const char *string, size_t len, connection_t *conn)
233 {
234  connection_write_to_buf_impl_(string, len, conn, 0);
235 }
236 void connection_buf_add_compress(const char *string, size_t len,
237  dir_connection_t *conn, int done);
238 void connection_buf_add_buf(connection_t *conn, struct buf_t *buf);
239 
240 size_t connection_get_inbuf_len(connection_t *conn);
241 size_t connection_get_outbuf_len(connection_t *conn);
243 
247  const tor_addr_t *addr,
248  uint16_t port, int purpose));
249 connection_t *connection_get_by_type_state(int type, int state);
251  const char *rendquery);
252 smartlist_t *connection_list_by_type_state(int type, int state);
253 smartlist_t *connection_list_by_type_purpose(int type, int purpose);
255  int purpose,
256  const char *resource);
258  int purpose,
259  const char *resource,
260  int state);
261 
262 #define CONN_LEN_AND_FREE_TEMPLATE(sl) \
263  STMT_BEGIN \
264  int len = smartlist_len(sl); \
265  smartlist_free(sl); \
266  return len; \
267  STMT_END
268 
269 /** Return a count of directory connections that are fetching the item
270  * described by <b>purpose</b>/<b>resource</b>. */
271 static inline int
273  int purpose,
274  const char *resource)
275 {
277  purpose,
278  resource);
279  CONN_LEN_AND_FREE_TEMPLATE(conns);
280 }
281 
282 /** Return a count of directory connections that are fetching the item
283  * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. */
284 static inline int
286  int purpose,
287  const char *resource,
288  int state)
289 {
290  smartlist_t *conns =
292  purpose,
293  resource,
294  state);
295  CONN_LEN_AND_FREE_TEMPLATE(conns);
296 }
297 
298 #undef CONN_LEN_AND_FREE_TEMPLATE
299 
300 int any_other_active_or_conns(const or_connection_t *this_conn);
301 
302 /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
303 #define connection_speaks_cells(conn) (((conn)->type == CONN_TYPE_OR) || 0)
307 
308 char *alloc_http_authenticator(const char *authenticator);
309 
310 void assert_connection_ok(connection_t *conn, time_t now);
312 void connection_dump_buffer_mem_stats(int severity);
313 
315  (const connection_t *conn, long apparent_skew, int trusted,
316  log_domain_mask_t domain, const char *received,
317  const char *source));
318 
320 void connection_check_oos(int n_socks, int failed);
321 
322 /** Execute the statement <b>stmt</b>, which may log events concerning the
323  * connection <b>conn</b>. To prevent infinite loops, disable log messages
324  * being sent to controllers if <b>conn</b> is a control connection.
325  *
326  * Stmt must not contain any return or goto statements.
327  */
328 #define CONN_LOG_PROTECT(conn, stmt) \
329  STMT_BEGIN \
330  int _log_conn_is_control; \
331  tor_assert(conn); \
332  _log_conn_is_control = (conn->type == CONN_TYPE_CONTROL); \
333  if (_log_conn_is_control) \
334  disable_control_logging(); \
335  STMT_BEGIN stmt; STMT_END; \
336  if (_log_conn_is_control) \
337  enable_control_logging(); \
338  STMT_END
339 
340 #ifdef CONNECTION_PRIVATE
342 
343 /* Used only by connection.c and test*.c */
345  (connection_t *conn,
346  const struct sockaddr *sa,
347  socklen_t sa_len,
348  const struct sockaddr *bindaddr,
349  socklen_t bindaddr_len,
350  int *socket_error));
353 
354 #endif /* defined(CONNECTION_PRIVATE) */
355 
356 #endif /* !defined(TOR_CONNECTION_H) */
listener_connection_t * TO_LISTENER_CONN(connection_t *)
Definition: connection.c:221
STATIC void kill_conn_list_for_oos(smartlist_t *conns)
Definition: connection.c:5086
STATIC int connection_connect_sockaddr(connection_t *conn, const struct sockaddr *sa, socklen_t sa_len, const struct sockaddr *bindaddr, socklen_t bindaddr_len, int *socket_error)
Definition: connection.c:1945
int connection_is_listener(connection_t *conn)
Definition: connection.c:4629
const char * conn_state_to_string(int type, int state)
Definition: connection.c:275
int connection_outbuf_too_full(connection_t *conn)
Definition: connection.c:3929
void connection_close_immediate(connection_t *conn)
Definition: connection.c:844
connection_t * connection_get_by_type(int type)
Definition: connection.c:4483
static int connection_dir_count_by_purpose_resource_and_state(int purpose, const char *resource, int state)
Definition: connection.h:285
entry_connection_t * entry_connection_new(int type, int socket_family)
Definition: connection.c:396
listener_connection_t * listener_connection_new(int type, int socket_family)
Definition: connection.c:438
void connection_mark_all_noncontrol_connections(void)
Definition: connection.c:3013
void connection_dump_buffer_mem_stats(int severity)
Definition: connection.c:5208
void connection_read_bw_exhausted(connection_t *conn, bool is_global_bw)
Definition: connection.c:3339
int retry_all_listeners(smartlist_t *new_conns, int close_all_noncontrol)
Definition: connection.c:2905
connection_t * connection_get_by_type_state_rendquery(int type, int state, const char *rendquery)
Definition: connection.c:4512
edge_connection_t * edge_connection_new(int type, int socket_family)
Definition: connection.c:415
STATIC smartlist_t * pick_oos_victims(int n)
Definition: connection.c:4993
#define STATIC
Definition: testsupport.h:32
STATIC void connection_free_minimal(connection_t *conn)
Definition: connection.c:571
connection_t * connection_get_by_type_addr_port_purpose(int type, const tor_addr_t *addr, uint16_t port, int purpose)
Definition: connection.c:4462
void connection_free_(connection_t *conn)
Definition: connection.c:761
void connection_check_oos(int n_socks, int failed)
Definition: connection.c:5124
int connection_state_is_open(connection_t *conn)
Definition: connection.c:4648
void connection_write_bw_exhausted(connection_t *conn, bool is_global_bw)
Definition: connection.c:3354
void connection_buf_add_buf(connection_t *conn, struct buf_t *buf)
Definition: connection.c:4396
int global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
Definition: connection.c:3209
void connection_about_to_close_connection(connection_t *conn)
Definition: connection.c:813
const char * conn_type_to_string(int type)
Definition: connection.c:243
void connection_free_all(void)
Definition: connection.c:5505
int connection_buf_get_line(connection_t *conn, char *data, size_t *data_len)
Definition: connection.c:3898
int connection_state_is_connecting(connection_t *conn)
Definition: connection.c:4668
int connection_connect(connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
Definition: connection.c:2185
dir_connection_t * dir_connection_new(int socket_family)
Definition: connection.c:360
void connection_dir_buf_add(const char *string, size_t len, dir_connection_t *dir_conn, int done)
Definition: connection.c:4372
int connection_wants_to_flush(connection_t *conn)
Definition: connection.c:3919
void connection_bucket_adjust(const or_options_t *options)
Definition: connection.c:3447
int connection_or_nonopen_was_started_here(or_connection_t *conn)
int conn_listener_type_supports_af_unix(int type)
Definition: connection.c:553
int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type, int *is_pt_out, const connection_t *conn)
Definition: connection.c:5411
int connection_buf_get_bytes(char *string, size_t len, connection_t *conn)
Definition: connection.c:3891
connection_t * connection_new(int type, int socket_family)
Definition: connection.c:450
int connection_flush(connection_t *conn)
Definition: connection.c:4258
void connection_write_to_buf_impl_(const char *string, size_t len, connection_t *conn, int zlib)
Definition: connection.c:4335
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:3907
void connection_mark_for_close_internal_(connection_t *conn, int line, const char *file)
Definition: connection.c:919
int connection_init_accepted_conn(connection_t *conn, const listener_connection_t *listener)
Definition: connection.c:1875
uint64_t log_domain_mask_t
Definition: logging_types.h:21
void assert_connection_ok(connection_t *conn, time_t now)
Definition: connection.c:5257
smartlist_t * connection_dir_list_by_purpose_resource_and_state(int purpose, const char *resource, int state)
Definition: connection.c:4582
or_connection_t * or_connection_new(int type, int socket_family)
Definition: connection.c:375
void clock_skew_warning(const connection_t *conn, long apparent_skew, int trusted, log_domain_mask_t domain, const char *received, const char *source)
Definition: connection.c:5549
int any_other_active_or_conns(const or_connection_t *this_conn)
Definition: connection.c:4613
control_connection_t * control_connection_new(int socket_family)
Definition: connection.c:426
void connection_consider_empty_write_buckets(connection_t *conn)
Definition: connection.c:3394
int connection_read_proxy_handshake(connection_t *conn)
Definition: connection.c:2605
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
smartlist_t * connection_dir_list_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.c:4564
char * alloc_http_authenticator(const char *authenticator)
Definition: connection.c:4691
void connection_mark_for_close_(connection_t *conn, int line, const char *file)
Definition: connection.c:878
int connection_is_moribund(connection_t *conn)
Definition: connection.c:5107
ssize_t connection_bucket_write_limit(connection_t *conn, time_t now)
Definition: connection.c:3159
void connection_bucket_init(void)
Definition: connection.c:3422
void connection_mark_all_noncontrol_listeners(void)
Definition: connection.c:2999
void log_failed_proxy_connection(connection_t *conn)
Definition: connection.c:5465
void connection_consider_empty_read_buckets(connection_t *conn)
Definition: connection.c:3365
static int connection_dir_count_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.h:272
void connection_expire_held_open(void)
Definition: connection.c:961
void connection_link_connections(connection_t *conn_a, connection_t *conn_b)
Definition: connection.c:539
connection_t * connection_get_by_global_id(uint64_t id)
Definition: connection.c:4475
int connection_proxy_connect(connection_t *conn, int type)
Definition: connection.c:2340
connection_t * connection_get_by_type_nonlinked(int type)
Definition: connection.c:4502
connection_t * connection_get_by_type_state(int type, int state)
Definition: connection.c:4492