Tor 0.4.9.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-2021, 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
23struct connection_t;
24struct dir_connection_t;
25struct or_connection_t;
29struct port_cfg_t;
30struct tor_addr_t;
31struct or_options_t;
32
35 const struct connection_t *);
36
37struct buf_t;
38
39#define CONN_TYPE_MIN_ 3
40/** Type for sockets listening for OR connections. */
41#define CONN_TYPE_OR_LISTENER 3
42/** A bidirectional TLS connection transmitting a sequence of cells.
43 * May be from an OR to an OR, or from an OP to an OR. */
44#define CONN_TYPE_OR 4
45/** A TCP connection from an onion router to a stream's destination. */
46#define CONN_TYPE_EXIT 5
47/** Type for sockets listening for SOCKS connections. */
48#define CONN_TYPE_AP_LISTENER 6
49/** A SOCKS proxy connection from the user application to the onion
50 * proxy. */
51#define CONN_TYPE_AP 7
52/** Type for sockets listening for HTTP connections to the directory server. */
53#define CONN_TYPE_DIR_LISTENER 8
54/** Type for HTTP connections to the directory server. */
55#define CONN_TYPE_DIR 9
56/* Type 10 is unused. */
57/** Type for listening for connections from user interface process. */
58#define CONN_TYPE_CONTROL_LISTENER 11
59/** Type for connections from user interface process. */
60#define CONN_TYPE_CONTROL 12
61/** Type for sockets listening for transparent connections redirected by pf or
62 * netfilter. */
63#define CONN_TYPE_AP_TRANS_LISTENER 13
64/** Type for sockets listening for transparent connections redirected by
65 * natd. */
66#define CONN_TYPE_AP_NATD_LISTENER 14
67/** Type for sockets listening for DNS requests. */
68#define CONN_TYPE_AP_DNS_LISTENER 15
69
70/** Type for connections from the Extended ORPort. */
71#define CONN_TYPE_EXT_OR 16
72/** Type for sockets listening for Extended ORPort connections. */
73#define CONN_TYPE_EXT_OR_LISTENER 17
74/** Type for sockets listening for HTTP CONNECT tunnel connections. */
75#define CONN_TYPE_AP_HTTP_CONNECT_LISTENER 18
76/** Type for sockets listening for Metrics query connections. */
77#define CONN_TYPE_METRICS_LISTENER 19
78/** Type for connections from metrics listener. */
79#define CONN_TYPE_METRICS 20
80
81#define CONN_TYPE_MAX_ 21
82/* !!!! If _CONN_TYPE_MAX is ever over 31, we must grow the type field in
83 * struct connection_t. */
84
85/* Proxy client handshake states */
86/* We use a proxy but we haven't even connected to it yet. */
87#define PROXY_INFANT 1
88/* We use an HTTP proxy and we've sent the CONNECT command. */
89#define PROXY_HTTPS_WANT_CONNECT_OK 2
90/* We use a SOCKS4 proxy and we've sent the CONNECT command. */
91#define PROXY_SOCKS4_WANT_CONNECT_OK 3
92/* We use a SOCKS5 proxy and we try to negotiate without
93 any authentication . */
94#define PROXY_SOCKS5_WANT_AUTH_METHOD_NONE 4
95/* We use a SOCKS5 proxy and we try to negotiate with
96 Username/Password authentication . */
97#define PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929 5
98/* We use a SOCKS5 proxy and we just sent our credentials. */
99#define PROXY_SOCKS5_WANT_AUTH_RFC1929_OK 6
100/* We use a SOCKS5 proxy and we just sent our CONNECT command. */
101#define PROXY_SOCKS5_WANT_CONNECT_OK 7
102/* We use an HAPROXY proxy and we just sent the proxy header. */
103#define PROXY_HAPROXY_WAIT_FOR_FLUSH 8
104/* We use a proxy and we CONNECTed successfully!. */
105#define PROXY_CONNECTED 9
106
107/** State for any listener connection. */
108#define LISTENER_STATE_READY 0
109
110/**
111 * This struct associates an old listener connection to be replaced
112 * by new connection described by port configuration. Only used when
113 * moving listeners to/from wildcard IP address.
114 */
115typedef struct
116{
117 struct connection_t *old_conn; /* Old listener connection to be replaced */
118 const struct port_cfg_t *new_port; /* New port configuration */
120
121const char *conn_type_to_string(int type);
122const char *conn_state_to_string(int type, int state);
124
125const char *connection_describe(const connection_t *conn);
126const char *connection_describe_peer(const connection_t *conn);
127
128struct dir_connection_t *dir_connection_new(int socket_family);
129struct or_connection_t *or_connection_new(int type, int socket_family);
130struct edge_connection_t *edge_connection_new(int type, int socket_family);
131struct entry_connection_t *entry_connection_new(int type, int socket_family);
132struct control_connection_t *control_connection_new(int socket_family);
134 int socket_family);
137 const struct listener_connection_t *listener);
138void connection_link_connections(struct connection_t *conn_a,
139 struct connection_t *conn_b);
140MOCK_DECL(void,connection_free_,(struct connection_t *conn));
141#define connection_free(conn) \
142 FREE_AND_NULL(struct connection_t, connection_free_, (conn))
143void connection_free_all(void);
147 int line, const char *file);
149 (struct connection_t *conn, int line, const char *file));
150
151#define connection_mark_for_close(c) \
152 connection_mark_for_close_((c), __LINE__, SHORT_FILE__)
153#define connection_mark_for_close_internal(c) \
154 connection_mark_for_close_internal_((c), __LINE__, SHORT_FILE__)
155
156/**
157 * Mark 'c' for close, but try to hold it open until all the data is written.
158 * Use the _internal versions of connection_mark_for_close; this should be
159 * called when you either are sure that if this is an or_connection_t the
160 * controlling channel has been notified (e.g. with
161 * connection_or_notify_error()), or you actually are the
162 * connection_or_close_for_error() or connection_or_close_normally function.
163 * For all other cases, use connection_mark_and_flush() instead, which
164 * checks for struct or_connection_t properly, instead. See below.
165 */
166#define connection_mark_and_flush_internal_(c,line,file) \
167 do { \
168 struct connection_t *tmp_conn__ = (c); \
169 connection_mark_for_close_internal_(tmp_conn__, (line), (file)); \
170 tmp_conn__->hold_open_until_flushed = 1; \
171 } while (0)
172
173#define connection_mark_and_flush_internal(c) \
174 connection_mark_and_flush_internal_((c), __LINE__, SHORT_FILE__)
175
176/**
177 * Mark 'c' for close, but try to hold it open until all the data is written.
178 */
179#define connection_mark_and_flush_(c,line,file) \
180 do { \
181 struct connection_t *tmp_conn_ = (c); \
182 if (tmp_conn_->type == CONN_TYPE_OR) { \
183 log_warn(LD_CHANNEL | LD_BUG, \
184 "Something tried to close (and flush) an or_connection_t" \
185 " without going through channels at %s:%d", \
186 file, line); \
187 connection_or_close_for_error(TO_OR_CONN(tmp_conn_), 1); \
188 } else { \
189 connection_mark_and_flush_internal_(c, line, file); \
190 } \
191 } while (0)
192
193#define connection_mark_and_flush(c) \
194 connection_mark_and_flush_((c), __LINE__, SHORT_FILE__)
195
197
198int connection_connect(struct connection_t *conn, const char *address,
199 const struct tor_addr_t *addr,
200 uint16_t port, int *socket_error);
201
202#ifdef HAVE_SYS_UN_H
203
204int connection_connect_unix(struct connection_t *conn, const char *socket_path,
205 int *socket_error);
206
207#endif /* defined(HAVE_SYS_UN_H) */
208
209/** Maximum size of information that we can fit into SOCKS5 username
210 or password fields. */
211#define MAX_SOCKS5_AUTH_FIELD_SIZE 255
212
213/** Total maximum size of information that we can fit into SOCKS5
214 username and password fields. */
215#define MAX_SOCKS5_AUTH_SIZE_TOTAL 2*MAX_SOCKS5_AUTH_FIELD_SIZE
216
217int connection_proxy_connect(struct connection_t *conn, int type);
220int get_proxy_addrport(struct tor_addr_t *addr, uint16_t *port,
221 int *proxy_type,
222 int *is_pt_out, const struct connection_t *conn);
223
224int retry_all_listeners(struct smartlist_t *new_conns,
225 int close_all_noncontrol);
226
229
230ssize_t connection_bucket_write_limit(struct connection_t *conn, time_t now);
232 size_t attempt);
233void connection_bucket_init(void);
234void connection_bucket_adjust(const struct or_options_t *options);
235void connection_bucket_refill_all(time_t now,
236 uint32_t now_ts);
238 bool is_global_bw);
240 bool is_global_bw);
243
244int connection_handle_read(struct connection_t *conn);
245
246int connection_buf_get_bytes(char *string, size_t len,
247 struct connection_t *conn);
248int connection_buf_get_line(struct connection_t *conn, char *data,
249 size_t *data_len);
251 char **headers_out, size_t max_headerlen,
252 char **body_out, size_t *body_used,
253 size_t max_bodylen, int force_complete);
254
257int connection_handle_write(struct connection_t *conn, int force);
258int connection_flush(struct connection_t *conn);
259int connection_process_inbuf(struct connection_t *conn, int package_partial);
260
262 (const char *string, size_t len, struct connection_t *conn,
263 int zlib));
264/* DOCDOC connection_write_to_buf */
265static void connection_buf_add(const char *string, size_t len,
266 struct connection_t *conn);
267void connection_dir_buf_add(const char *string, size_t len,
268 struct dir_connection_t *dir_conn, int done);
269static inline void
270connection_buf_add(const char *string, size_t len, struct connection_t *conn)
271{
272 connection_write_to_buf_impl_(string, len, conn, 0);
273}
274void connection_buf_add_compress(const char *string, size_t len,
275 struct dir_connection_t *conn, int done);
276void connection_buf_add_buf(struct connection_t *conn, struct buf_t *buf);
277
278size_t connection_get_inbuf_len(const struct connection_t *conn);
279size_t connection_get_outbuf_len(const struct connection_t *conn);
280struct connection_t *connection_get_by_global_id(uint64_t id);
281
285 (int type,
286 const struct tor_addr_t *addr,
287 uint16_t port, int purpose));
289struct connection_t *connection_get_by_type_state_rendquery(
290 int type, int state,
291 const char *rendquery);
292struct smartlist_t *connection_list_by_type_state(int type, int state);
293struct smartlist_t *connection_list_by_type_purpose(int type, int purpose);
295 int purpose,
296 const char *resource);
298 int purpose,
299 const char *resource,
300 int state);
301
302#define CONN_LEN_AND_FREE_TEMPLATE(sl) \
303 STMT_BEGIN \
304 int len = smartlist_len(sl); \
305 smartlist_free(sl); \
306 return len; \
307 STMT_END
308
309/** Return a count of directory connections that are fetching the item
310 * described by <b>purpose</b>/<b>resource</b>. */
311static inline int
313 int purpose,
314 const char *resource)
315{
317 purpose,
318 resource);
319 CONN_LEN_AND_FREE_TEMPLATE(conns);
320}
321
322/** Return a count of directory connections that are fetching the item
323 * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. */
324static inline int
326 int purpose,
327 const char *resource,
328 int state)
329{
330 struct smartlist_t *conns =
332 purpose,
333 resource,
334 state);
335 CONN_LEN_AND_FREE_TEMPLATE(conns);
336}
337
338#undef CONN_LEN_AND_FREE_TEMPLATE
339
340int any_other_active_or_conns(const struct or_connection_t *this_conn);
341
342/* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
343#define connection_speaks_cells(conn) (((conn)->type == CONN_TYPE_OR) || 0)
344int connection_is_listener(struct connection_t *conn);
345int connection_state_is_open(struct connection_t *conn);
347
348char *alloc_http_authenticator(const char *authenticator);
349
350void assert_connection_ok(struct connection_t *conn, time_t now);
352void connection_dump_buffer_mem_stats(int severity);
353
355 (const struct connection_t *conn, long apparent_skew, int trusted,
356 log_domain_mask_t domain, const char *received,
357 const char *source));
358
359int connection_is_moribund(struct connection_t *conn);
360void connection_check_oos(int n_socks, int failed);
361
362/** Execute the statement <b>stmt</b>, which may log events concerning the
363 * connection <b>conn</b>. To prevent infinite loops, disable log messages
364 * being sent to controllers if <b>conn</b> is a control connection.
365 *
366 * Stmt must not contain any return or goto statements.
367 */
368#define CONN_LOG_PROTECT(conn, stmt) \
369 STMT_BEGIN \
370 int _log_conn_is_control; \
371 tor_assert(conn); \
372 _log_conn_is_control = (conn->type == CONN_TYPE_CONTROL); \
373 if (_log_conn_is_control) \
374 disable_control_logging(); \
375 STMT_BEGIN stmt; STMT_END; \
376 if (_log_conn_is_control) \
377 enable_control_logging(); \
378 STMT_END
379
380#ifdef CONNECTION_PRIVATE
382
383/* Used only by connection.c and test*.c */
385 (struct connection_t *conn,
386 const struct sockaddr *sa,
387 socklen_t sa_len,
388 const struct sockaddr *bindaddr,
389 socklen_t bindaddr_len,
390 int *socket_error));
391MOCK_DECL(STATIC void, kill_conn_list_for_oos, (struct smartlist_t *conns));
392MOCK_DECL(STATIC struct smartlist_t *, pick_oos_victims, (int n));
393
394#endif /* defined(CONNECTION_PRIVATE) */
395
396#endif /* !defined(TOR_CONNECTION_H) */
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:5963
bool connection_dir_is_global_write_low(const connection_t *conn, size_t attempt)
Definition: connection.c:3595
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:5818
STATIC smartlist_t * pick_oos_victims(int n)
Definition: connection.c:5409
connection_t * connection_get_by_type_addr_port_purpose(int type, const tor_addr_t *addr, uint16_t port, int purpose)
Definition: connection.c:4899
const listener_connection_t * CONST_TO_LISTENER_CONN(const connection_t *c)
Definition: connection.c:249
STATIC void connection_free_minimal(connection_t *conn)
Definition: connection.c:790
void connection_bucket_adjust(const or_options_t *options)
Definition: connection.c:3861
int any_other_active_or_conns(const or_connection_t *this_conn)
Definition: connection.c:5022
int connection_init_accepted_conn(connection_t *conn, const listener_connection_t *listener)
Definition: connection.c:2111
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:2183
STATIC void kill_conn_list_for_oos(smartlist_t *conns)
Definition: connection.c:5502
int connection_connect(connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
Definition: connection.c:2446
void connection_mark_all_noncontrol_listeners(void)
Definition: connection.c:3355
void connection_dir_buf_add(const char *string, size_t len, struct dir_connection_t *dir_conn, int done)
Definition: connection.c:4809
struct listener_connection_t * listener_connection_new(int type, int socket_family)
Definition: connection.c:654
void connection_mark_for_close_internal_(struct connection_t *conn, int line, const char *file)
Definition: connection.c:1129
void assert_connection_ok(struct connection_t *conn, time_t now)
Definition: connection.c:5673
int connection_is_listener(struct connection_t *conn)
Definition: connection.c:5038
void connection_close_immediate(struct connection_t *conn)
Definition: connection.c:1055
void connection_write_to_buf_impl_(const char *string, size_t len, struct connection_t *conn, int zlib)
Definition: connection.c:4777
void log_failed_proxy_connection(struct connection_t *conn)
Definition: connection.c:5879
struct listener_connection_t * TO_LISTENER_CONN(struct connection_t *)
Definition: connection.c:236
void connection_consider_empty_write_buckets(struct connection_t *conn)
Definition: connection.c:3808
struct control_connection_t * control_connection_new(int socket_family)
Definition: connection.c:642
static int connection_dir_count_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.h:312
struct or_connection_t * or_connection_new(int type, int socket_family)
Definition: connection.c:578
void connection_dump_buffer_mem_stats(int severity)
Definition: connection.c:5624
char * alloc_http_authenticator(const char *authenticator)
Definition: connection.c:5101
int connection_wants_to_flush(struct connection_t *conn)
Definition: connection.c:4351
void connection_bucket_init(void)
Definition: connection.c:3836
int connection_flush(struct connection_t *conn)
Definition: connection.c:4701
void connection_free_(struct connection_t *conn)
Definition: connection.c:972
const char * conn_type_to_string(int type)
Definition: connection.c:270
struct dir_connection_t * dir_connection_new(int socket_family)
Definition: connection.c:563
int connection_buf_get_line(struct connection_t *conn, char *data, size_t *data_len)
Definition: connection.c:4331
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:4340
struct edge_connection_t * edge_connection_new(int type, int socket_family)
Definition: connection.c:627
void connection_link_connections(struct connection_t *conn_a, struct connection_t *conn_b)
Definition: connection.c:758
int connection_or_nonopen_was_started_here(struct or_connection_t *conn)
int connection_buf_get_bytes(char *string, size_t len, struct connection_t *conn)
Definition: connection.c:4324
int connection_read_proxy_handshake(struct connection_t *conn)
Definition: connection.c:2953
struct connection_t * connection_new(int type, int socket_family)
Definition: connection.c:669
void connection_write_bw_exhausted(struct connection_t *conn, bool is_global_bw)
Definition: connection.c:3765
const char * connection_describe_peer(const connection_t *conn)
Definition: connection.c:530
void connection_about_to_close_connection(struct connection_t *conn)
Definition: connection.c:1024
int conn_listener_type_supports_af_unix(int type)
Definition: connection.c:772
void connection_free_all(void)
Definition: connection.c:5920
void connection_consider_empty_read_buckets(struct connection_t *conn)
Definition: connection.c:3776
void connection_mark_for_close_(struct connection_t *conn, int line, const char *file)
Definition: connection.c:1088
int connection_process_inbuf(struct connection_t *conn, int package_partial)
Definition: connection.c:5215
ssize_t connection_bucket_write_limit(struct connection_t *conn, time_t now)
Definition: connection.c:3540
struct smartlist_t * connection_dir_list_by_purpose_and_resource(int purpose, const char *resource)
Definition: connection.c:4973
int connection_is_moribund(struct connection_t *conn)
Definition: connection.c:5523
void connection_read_bw_exhausted(struct connection_t *conn, bool is_global_bw)
Definition: connection.c:3743
int retry_all_listeners(struct smartlist_t *new_conns, int close_all_noncontrol)
Definition: connection.c:3253
int connection_state_is_open(struct connection_t *conn)
Definition: connection.c:5058
int connection_state_is_connecting(struct connection_t *conn)
Definition: connection.c:5078
struct connection_t * connection_get_by_type(int type)
Definition: connection.c:4920
void connection_buf_add_buf(struct connection_t *conn, struct buf_t *buf)
Definition: connection.c:4833
const char * connection_describe(const connection_t *conn)
Definition: connection.c:545
int connection_proxy_connect(struct connection_t *conn, int type)
Definition: connection.c:2800
int connection_outbuf_too_full(struct connection_t *conn)
Definition: connection.c:4361
void connection_mark_all_noncontrol_connections(void)
Definition: connection.c:3369
static int connection_dir_count_by_purpose_resource_and_state(int purpose, const char *resource, int state)
Definition: connection.h:325
struct connection_t * connection_get_by_type_nonlinked(int type)
Definition: connection.c:4939
struct entry_connection_t * entry_connection_new(int type, int socket_family)
Definition: connection.c:603
struct connection_t * connection_get_by_type_state(int type, int state)
Definition: connection.c:4929
struct connection_t * connection_get_by_global_id(uint64_t id)
Definition: connection.c:4912
void connection_expire_held_open(void)
Definition: connection.c:1175
const char * conn_state_to_string(int type, int state)
Definition: connection.c:304
struct smartlist_t * connection_dir_list_by_purpose_resource_and_state(int purpose, const char *resource, int state)
Definition: connection.c:4991
void connection_check_oos(int n_socks, int failed)
Definition: connection.c:5540
Headers for log.c.
uint64_t log_domain_mask_t
Definition: logging_types.h:21
Top-level declarations for the smartlist_t dynamic array type.
uint8_t state
Definition: connection_st.h:49
unsigned int type
Definition: connection_st.h:50
uint16_t port
unsigned int purpose
Definition: connection_st.h:51
tor_addr_t addr
uint8_t type
Definition: port_cfg_st.h:23
#define STATIC
Definition: testsupport.h:32
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127