Tor  0.4.3.1-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-2020, 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 #include "lib/log/log.h"
17 
18 #ifdef HAVE_SYS_SOCKET_H
19 #include <sys/socket.h>
20 #endif
21 
23 struct connection_t;
24 struct dir_connection_t;
25 struct or_connection_t;
26 struct edge_connection_t;
27 struct entry_connection_t;
29 struct port_cfg_t;
30 struct tor_addr_t;
31 struct or_options_t;
32 
34 
35 struct buf_t;
36 
37 #define CONN_TYPE_MIN_ 3
38 /** Type for sockets listening for OR connections. */
39 #define CONN_TYPE_OR_LISTENER 3
40 /** A bidirectional TLS connection transmitting a sequence of cells.
41  * May be from an OR to an OR, or from an OP to an OR. */
42 #define CONN_TYPE_OR 4
43 /** A TCP connection from an onion router to a stream's destination. */
44 #define CONN_TYPE_EXIT 5
45 /** Type for sockets listening for SOCKS connections. */
46 #define CONN_TYPE_AP_LISTENER 6
47 /** A SOCKS proxy connection from the user application to the onion
48  * proxy. */
49 #define CONN_TYPE_AP 7
50 /** Type for sockets listening for HTTP connections to the directory server. */
51 #define CONN_TYPE_DIR_LISTENER 8
52 /** Type for HTTP connections to the directory server. */
53 #define CONN_TYPE_DIR 9
54 /* Type 10 is unused. */
55 /** Type for listening for connections from user interface process. */
56 #define CONN_TYPE_CONTROL_LISTENER 11
57 /** Type for connections from user interface process. */
58 #define CONN_TYPE_CONTROL 12
59 /** Type for sockets listening for transparent connections redirected by pf or
60  * netfilter. */
61 #define CONN_TYPE_AP_TRANS_LISTENER 13
62 /** Type for sockets listening for transparent connections redirected by
63  * natd. */
64 #define CONN_TYPE_AP_NATD_LISTENER 14
65 /** Type for sockets listening for DNS requests. */
66 #define CONN_TYPE_AP_DNS_LISTENER 15
67 
68 /** Type for connections from the Extended ORPort. */
69 #define CONN_TYPE_EXT_OR 16
70 /** Type for sockets listening for Extended ORPort connections. */
71 #define CONN_TYPE_EXT_OR_LISTENER 17
72 /** Type for sockets listening for HTTP CONNECT tunnel connections. */
73 #define CONN_TYPE_AP_HTTP_CONNECT_LISTENER 18
74 
75 #define CONN_TYPE_MAX_ 19
76 /* !!!! If _CONN_TYPE_MAX is ever over 31, we must grow the type field in
77  * struct connection_t. */
78 
79 /* Proxy client handshake states */
80 /* We use a proxy but we haven't even connected to it yet. */
81 #define PROXY_INFANT 1
82 /* We use an HTTP proxy and we've sent the CONNECT command. */
83 #define PROXY_HTTPS_WANT_CONNECT_OK 2
84 /* We use a SOCKS4 proxy and we've sent the CONNECT command. */
85 #define PROXY_SOCKS4_WANT_CONNECT_OK 3
86 /* We use a SOCKS5 proxy and we try to negotiate without
87  any authentication . */
88 #define PROXY_SOCKS5_WANT_AUTH_METHOD_NONE 4
89 /* We use a SOCKS5 proxy and we try to negotiate with
90  Username/Password authentication . */
91 #define PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929 5
92 /* We use a SOCKS5 proxy and we just sent our credentials. */
93 #define PROXY_SOCKS5_WANT_AUTH_RFC1929_OK 6
94 /* We use a SOCKS5 proxy and we just sent our CONNECT command. */
95 #define PROXY_SOCKS5_WANT_CONNECT_OK 7
96 /* We use an HAPROXY proxy and we just sent the proxy header. */
97 #define PROXY_HAPROXY_WAIT_FOR_FLUSH 8
98 /* We use a proxy and we CONNECTed successfully!. */
99 #define PROXY_CONNECTED 9
100 
101 /** State for any listener connection. */
102 #define LISTENER_STATE_READY 0
103 
104 /**
105  * This struct associates an old listener connection to be replaced
106  * by new connection described by port configuration. Only used when
107  * moving listeners to/from wildcard IP address.
108  */
109 typedef struct
110 {
111  struct connection_t *old_conn; /* Old listener connection to be replaced */
112  const struct port_cfg_t *new_port; /* New port configuration */
114 
115 const char *conn_type_to_string(int type);
116 const char *conn_state_to_string(int type, int state);
118 
119 struct dir_connection_t *dir_connection_new(int socket_family);
120 struct or_connection_t *or_connection_new(int type, int socket_family);
121 struct edge_connection_t *edge_connection_new(int type, int socket_family);
122 struct entry_connection_t *entry_connection_new(int type, int socket_family);
123 struct control_connection_t *control_connection_new(int socket_family);
125  int socket_family);
128  const struct listener_connection_t *listener);
129 void connection_link_connections(struct connection_t *conn_a,
130  struct connection_t *conn_b);
131 MOCK_DECL(void,connection_free_,(struct connection_t *conn));
132 #define connection_free(conn) \
133  FREE_AND_NULL(struct connection_t, connection_free_, (conn))
134 void connection_free_all(void);
136 void connection_close_immediate(struct connection_t *conn);
137 void connection_mark_for_close_(struct connection_t *conn,
138  int line, const char *file);
140  (struct connection_t *conn, int line, const char *file));
141 
142 #define connection_mark_for_close(c) \
143  connection_mark_for_close_((c), __LINE__, SHORT_FILE__)
144 #define connection_mark_for_close_internal(c) \
145  connection_mark_for_close_internal_((c), __LINE__, SHORT_FILE__)
146 
147 /**
148  * Mark 'c' for close, but try to hold it open until all the data is written.
149  * Use the _internal versions of connection_mark_for_close; this should be
150  * called when you either are sure that if this is an or_connection_t the
151  * controlling channel has been notified (e.g. with
152  * connection_or_notify_error()), or you actually are the
153  * connection_or_close_for_error() or connection_or_close_normally function.
154  * For all other cases, use connection_mark_and_flush() instead, which
155  * checks for struct or_connection_t properly, instead. See below.
156  */
157 #define connection_mark_and_flush_internal_(c,line,file) \
158  do { \
159  struct connection_t *tmp_conn__ = (c); \
160  connection_mark_for_close_internal_(tmp_conn__, (line), (file)); \
161  tmp_conn__->hold_open_until_flushed = 1; \
162  } while (0)
163 
164 #define connection_mark_and_flush_internal(c) \
165  connection_mark_and_flush_internal_((c), __LINE__, SHORT_FILE__)
166 
167 /**
168  * Mark 'c' for close, but try to hold it open until all the data is written.
169  */
170 #define connection_mark_and_flush_(c,line,file) \
171  do { \
172  struct connection_t *tmp_conn_ = (c); \
173  if (tmp_conn_->type == CONN_TYPE_OR) { \
174  log_warn(LD_CHANNEL | LD_BUG, \
175  "Something tried to close (and flush) an or_connection_t" \
176  " without going through channels at %s:%d", \
177  file, line); \
178  connection_or_close_for_error(TO_OR_CONN(tmp_conn_), 1); \
179  } else { \
180  connection_mark_and_flush_internal_(c, line, file); \
181  } \
182  } while (0)
183 
184 #define connection_mark_and_flush(c) \
185  connection_mark_and_flush_((c), __LINE__, SHORT_FILE__)
186 
187 void connection_expire_held_open(void);
188 
189 int connection_connect(struct connection_t *conn, const char *address,
190  const struct tor_addr_t *addr,
191  uint16_t port, int *socket_error);
192 
193 #ifdef HAVE_SYS_UN_H
194 
195 int connection_connect_unix(struct connection_t *conn, const char *socket_path,
196  int *socket_error);
197 
198 #endif /* defined(HAVE_SYS_UN_H) */
199 
200 /** Maximum size of information that we can fit into SOCKS5 username
201  or password fields. */
202 #define MAX_SOCKS5_AUTH_FIELD_SIZE 255
203 
204 /** Total maximum size of information that we can fit into SOCKS5
205  username and password fields. */
206 #define MAX_SOCKS5_AUTH_SIZE_TOTAL 2*MAX_SOCKS5_AUTH_FIELD_SIZE
207 
208 int connection_proxy_connect(struct connection_t *conn, int type);
210 void log_failed_proxy_connection(struct connection_t *conn);
211 int get_proxy_addrport(struct tor_addr_t *addr, uint16_t *port,
212  int *proxy_type,
213  int *is_pt_out, const struct connection_t *conn);
214 
215 int retry_all_listeners(struct smartlist_t *new_conns,
216  int close_all_noncontrol);
217 
220 
221 ssize_t connection_bucket_write_limit(struct connection_t *conn, time_t now);
222 int global_write_bucket_low(struct connection_t *conn,
223  size_t attempt, int priority);
224 void connection_bucket_init(void);
225 void connection_bucket_adjust(const struct or_options_t *options);
226 void connection_bucket_refill_all(time_t now,
227  uint32_t now_ts);
229  bool is_global_bw);
231  bool is_global_bw);
234 
235 int connection_handle_read(struct connection_t *conn);
236 
237 int connection_buf_get_bytes(char *string, size_t len,
238  struct connection_t *conn);
239 int connection_buf_get_line(struct connection_t *conn, char *data,
240  size_t *data_len);
242  char **headers_out, size_t max_headerlen,
243  char **body_out, size_t *body_used,
244  size_t max_bodylen, int force_complete);
245 
246 int connection_wants_to_flush(struct connection_t *conn);
247 int connection_outbuf_too_full(struct connection_t *conn);
248 int connection_handle_write(struct connection_t *conn, int force);
249 int connection_flush(struct connection_t *conn);
250 
252  (const char *string, size_t len, struct connection_t *conn,
253  int zlib));
254 /* DOCDOC connection_write_to_buf */
255 static void connection_buf_add(const char *string, size_t len,
256  struct connection_t *conn);
257 void connection_dir_buf_add(const char *string, size_t len,
258  struct dir_connection_t *dir_conn, int done);
259 static inline void
260 connection_buf_add(const char *string, size_t len, struct connection_t *conn)
261 {
262  connection_write_to_buf_impl_(string, len, conn, 0);
263 }
264 void connection_buf_add_compress(const char *string, size_t len,
265  struct dir_connection_t *conn, int done);
266 void connection_buf_add_buf(struct connection_t *conn, struct buf_t *buf);
267 
268 size_t connection_get_inbuf_len(struct connection_t *conn);
269 size_t connection_get_outbuf_len(struct connection_t *conn);
270 struct connection_t *connection_get_by_global_id(uint64_t id);
271 
275  (int type,
276  const struct tor_addr_t *addr,
277  uint16_t port, int purpose));
280  int type, int state,
281  const char *rendquery);
282 struct smartlist_t *connection_list_by_type_state(int type, int state);
283 struct smartlist_t *connection_list_by_type_purpose(int type, int purpose);
285  int purpose,
286  const char *resource);
288  int purpose,
289  const char *resource,
290  int state);
291 
292 #define CONN_LEN_AND_FREE_TEMPLATE(sl) \
293  STMT_BEGIN \
294  int len = smartlist_len(sl); \
295  smartlist_free(sl); \
296  return len; \
297  STMT_END
298 
299 /** Return a count of directory connections that are fetching the item
300  * described by <b>purpose</b>/<b>resource</b>. */
301 static inline int
303  int purpose,
304  const char *resource)
305 {
307  purpose,
308  resource);
309  CONN_LEN_AND_FREE_TEMPLATE(conns);
310 }
311 
312 /** Return a count of directory connections that are fetching the item
313  * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. */
314 static inline int
316  int purpose,
317  const char *resource,
318  int state)
319 {
320  struct smartlist_t *conns =
322  purpose,
323  resource,
324  state);
325  CONN_LEN_AND_FREE_TEMPLATE(conns);
326 }
327 
328 #undef CONN_LEN_AND_FREE_TEMPLATE
329 
330 int any_other_active_or_conns(const struct or_connection_t *this_conn);
331 
332 /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
333 #define connection_speaks_cells(conn) (((conn)->type == CONN_TYPE_OR) || 0)
334 int connection_is_listener(struct connection_t *conn);
335 int connection_state_is_open(struct connection_t *conn);
337 
338 char *alloc_http_authenticator(const char *authenticator);
339 
340 void assert_connection_ok(struct connection_t *conn, time_t now);
342 void connection_dump_buffer_mem_stats(int severity);
343 
345  (const struct connection_t *conn, long apparent_skew, int trusted,
346  log_domain_mask_t domain, const char *received,
347  const char *source));
348 
349 int connection_is_moribund(struct connection_t *conn);
350 void connection_check_oos(int n_socks, int failed);
351 
352 /** Execute the statement <b>stmt</b>, which may log events concerning the
353  * connection <b>conn</b>. To prevent infinite loops, disable log messages
354  * being sent to controllers if <b>conn</b> is a control connection.
355  *
356  * Stmt must not contain any return or goto statements.
357  */
358 #define CONN_LOG_PROTECT(conn, stmt) \
359  STMT_BEGIN \
360  int _log_conn_is_control; \
361  tor_assert(conn); \
362  _log_conn_is_control = (conn->type == CONN_TYPE_CONTROL); \
363  if (_log_conn_is_control) \
364  disable_control_logging(); \
365  STMT_BEGIN stmt; STMT_END; \
366  if (_log_conn_is_control) \
367  enable_control_logging(); \
368  STMT_END
369 
370 #ifdef CONNECTION_PRIVATE
371 STATIC void connection_free_minimal(struct connection_t *conn);
372 
373 /* Used only by connection.c and test*.c */
375  (struct connection_t *conn,
376  const struct sockaddr *sa,
377  socklen_t sa_len,
378  const struct sockaddr *bindaddr,
379  socklen_t bindaddr_len,
380  int *socket_error));
381 MOCK_DECL(STATIC void, kill_conn_list_for_oos, (struct smartlist_t *conns));
382 MOCK_DECL(STATIC struct smartlist_t *, pick_oos_victims, (int n));
383 
384 #endif /* defined(CONNECTION_PRIVATE) */
385 
386 #endif /* !defined(TOR_CONNECTION_H) */
STATIC void kill_conn_list_for_oos(smartlist_t *conns)
Definition: connection.c:5172
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:1944
struct connection_t * connection_get_by_type_nonlinked(int type)
Definition: connection.c:4588
void connection_read_bw_exhausted(struct connection_t *conn, bool is_global_bw)
Definition: connection.c:3425
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:5497
const char * conn_state_to_string(int type, int state)
Definition: connection.c:276
struct connection_t * connection_get_by_global_id(uint64_t id)
Definition: connection.c:4561
uint8_t type
Definition: port_cfg_st.h:23
int connection_state_is_open(struct connection_t *conn)
Definition: connection.c:4734
struct connection_t * connection_get_by_type(int type)
Definition: connection.c:4569
static int connection_dir_count_by_purpose_resource_and_state(int purpose, const char *resource, int state)
Definition: connection.h:315
int connection_flush(struct connection_t *conn)
Definition: connection.c:4344
unsigned int purpose
Definition: connection_st.h:51
void connection_close_immediate(struct connection_t *conn)
Definition: connection.c:841
struct smartlist_t * connection_dir_list_by_purpose_resource_and_state(int purpose, const char *resource, int state)
Definition: connection.c:4668
void connection_mark_all_noncontrol_connections(void)
Definition: connection.c:3099
void connection_dump_buffer_mem_stats(int severity)
Definition: connection.c:5294
void log_failed_proxy_connection(struct connection_t *conn)
Definition: connection.c:5558
int retry_all_listeners(struct smartlist_t *new_conns, int close_all_noncontrol)
Definition: connection.c:2991
struct or_connection_t * or_connection_new(int type, int socket_family)
Definition: connection.c:376
void connection_write_to_buf_impl_(const char *string, size_t len, struct connection_t *conn, int zlib)
Definition: connection.c:4421
Top-level declarations for the smartlist_t dynamic array type.
struct connection_t * connection_new(int type, int socket_family)
Definition: connection.c:451
struct connection_t * connection_get_by_type_state_rendquery(int type, int state, const char *rendquery)
Definition: connection.c:4598
STATIC smartlist_t * pick_oos_victims(int n)
Definition: connection.c:5079
#define STATIC
Definition: testsupport.h:32
void connection_consider_empty_write_buckets(struct connection_t *conn)
Definition: connection.c:3480
STATIC void connection_free_minimal(connection_t *conn)
Definition: connection.c:572
void connection_check_oos(int n_socks, int failed)
Definition: connection.c:5210
struct listener_connection_t * TO_LISTENER_CONN(struct connection_t *)
Definition: connection.c:222
int connection_is_listener(struct connection_t *conn)
Definition: connection.c:4715
int global_write_bucket_low(struct connection_t *conn, size_t attempt, int priority)
Definition: connection.c:3295
void connection_dir_buf_add(const char *string, size_t len, struct dir_connection_t *dir_conn, int done)
Definition: connection.c:4458
struct dir_connection_t * dir_connection_new(int socket_family)
Definition: connection.c:361
void assert_connection_ok(struct connection_t *conn, time_t now)
Definition: connection.c:5343
const char * conn_type_to_string(int type)
Definition: connection.c:244
struct edge_connection_t * edge_connection_new(int type, int socket_family)
Definition: connection.c:416
void connection_free_all(void)
Definition: connection.c:5599
int connection_init_accepted_conn(connection_t *conn, const listener_connection_t *listener)
Definition: connection.c:1872
int connection_is_moribund(struct connection_t *conn)
Definition: connection.c:5193
void connection_free_(struct connection_t *conn)
Definition: connection.c:758
void connection_about_to_close_connection(struct connection_t *conn)
Definition: connection.c:810
int connection_connect(connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
Definition: connection.c:2184
void connection_link_connections(struct connection_t *conn_a, struct connection_t *conn_b)
Definition: connection.c:540
void connection_consider_empty_read_buckets(struct connection_t *conn)
Definition: connection.c:3451
int conn_listener_type_supports_af_unix(int type)
Definition: connection.c:554
void connection_mark_for_close_(struct connection_t *conn, int line, const char *file)
Definition: connection.c:875
unsigned int type
Definition: connection_st.h:50
struct entry_connection_t * entry_connection_new(int type, int socket_family)
Definition: connection.c:397
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:5643
struct connection_t * connection_get_by_type_state(int type, int state)
Definition: connection.c:4578
uint64_t log_domain_mask_t
Definition: logging_types.h:21
uint16_t port
struct listener_connection_t * listener_connection_new(int type, int socket_family)
Definition: connection.c:439
int connection_wants_to_flush(struct connection_t *conn)
Definition: connection.c:4005
connection_t * connection_get_by_type_addr_port_purpose(int type, const tor_addr_t *addr, uint16_t port, int purpose)
Definition: connection.c:4548
int connection_read_proxy_handshake(struct connection_t *conn)
Definition: connection.c:2691
int connection_state_is_connecting(struct connection_t *conn)
Definition: connection.c:4754
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
int any_other_active_or_conns(const or_connection_t *this_conn)
Definition: connection.c:4699
struct smartlist_t * connection_dir_list_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.c:4650
char * alloc_http_authenticator(const char *authenticator)
Definition: connection.c:4777
int connection_proxy_connect(struct connection_t *conn, int type)
Definition: connection.c:2538
int connection_or_nonopen_was_started_here(struct or_connection_t *conn)
void connection_mark_for_close_internal_(struct connection_t *conn, int line, const char *file)
Definition: connection.c:916
int connection_fetch_from_buf_http(struct 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:3993
void connection_bucket_adjust(const or_options_t *options)
Definition: connection.c:3533
tor_addr_t addr
void connection_write_bw_exhausted(struct connection_t *conn, bool is_global_bw)
Definition: connection.c:3440
Headers for log.c.
void connection_bucket_init(void)
Definition: connection.c:3508
void connection_mark_all_noncontrol_listeners(void)
Definition: connection.c:3085
int connection_outbuf_too_full(struct connection_t *conn)
Definition: connection.c:4015
static int connection_dir_count_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.h:302
void connection_expire_held_open(void)
Definition: connection.c:958
int connection_buf_get_bytes(char *string, size_t len, struct connection_t *conn)
Definition: connection.c:3977
void connection_buf_add_buf(struct connection_t *conn, struct buf_t *buf)
Definition: connection.c:4482
int connection_buf_get_line(struct connection_t *conn, char *data, size_t *data_len)
Definition: connection.c:3984
ssize_t connection_bucket_write_limit(struct connection_t *conn, time_t now)
Definition: connection.c:3245
uint8_t state
Definition: connection_st.h:49
struct control_connection_t * control_connection_new(int socket_family)
Definition: connection.c:427