12#ifndef TOR_CONNECTION_H
13#define TOR_CONNECTION_H
18#ifdef HAVE_SYS_SOCKET_H
19#include <sys/socket.h>
39#define CONN_TYPE_MIN_ 3
41#define CONN_TYPE_OR_LISTENER 3
46#define CONN_TYPE_EXIT 5
48#define CONN_TYPE_AP_LISTENER 6
53#define CONN_TYPE_DIR_LISTENER 8
55#define CONN_TYPE_DIR 9
58#define CONN_TYPE_CONTROL_LISTENER 11
60#define CONN_TYPE_CONTROL 12
63#define CONN_TYPE_AP_TRANS_LISTENER 13
66#define CONN_TYPE_AP_NATD_LISTENER 14
68#define CONN_TYPE_AP_DNS_LISTENER 15
71#define CONN_TYPE_EXT_OR 16
73#define CONN_TYPE_EXT_OR_LISTENER 17
75#define CONN_TYPE_AP_HTTP_CONNECT_LISTENER 18
77#define CONN_TYPE_METRICS_LISTENER 19
79#define CONN_TYPE_METRICS 20
81#define CONN_TYPE_MAX_ 21
89#define PROXY_HTTPS_WANT_CONNECT_OK 2
91#define PROXY_SOCKS4_WANT_CONNECT_OK 3
94#define PROXY_SOCKS5_WANT_AUTH_METHOD_NONE 4
97#define PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929 5
99#define PROXY_SOCKS5_WANT_AUTH_RFC1929_OK 6
101#define PROXY_SOCKS5_WANT_CONNECT_OK 7
103#define PROXY_HAPROXY_WAIT_FOR_FLUSH 8
105#define PROXY_CONNECTED 9
108#define LISTENER_STATE_READY 0
141#define connection_free(conn) \
142 FREE_AND_NULL(struct connection_t, connection_free_, (conn))
147 int line,
const char *file);
149 (
struct connection_t *conn,
int line,
const char *file));
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__)
166#define connection_mark_and_flush_internal_(c,line,file) \
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; \
173#define connection_mark_and_flush_internal(c) \
174 connection_mark_and_flush_internal_((c), __LINE__, SHORT_FILE__)
179#define connection_mark_and_flush_(c,line,file) \
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", \
187 connection_or_close_for_error(TO_OR_CONN(tmp_conn_), 1); \
189 connection_mark_and_flush_internal_(c, line, file); \
193#define connection_mark_and_flush(c) \
194 connection_mark_and_flush_((c), __LINE__, SHORT_FILE__)
200 uint16_t
port,
int *socket_error);
204int connection_connect_unix(
struct connection_t *conn,
const char *socket_path,
211#define MAX_SOCKS5_AUTH_FIELD_SIZE 255
215#define MAX_SOCKS5_AUTH_SIZE_TOTAL 2*MAX_SOCKS5_AUTH_FIELD_SIZE
225 int close_all_noncontrol);
235void connection_bucket_refill_all(time_t now,
251 char **headers_out,
size_t max_headerlen,
252 char **body_out,
size_t *body_used,
253 size_t max_bodylen,
int force_complete);
257int connection_handle_write(
struct connection_t *conn,
int force);
262 (
const char *
string,
size_t len,
struct connection_t *conn,
265static void connection_buf_add(
const char *
string,
size_t len,
270connection_buf_add(
const char *
string,
size_t len,
struct connection_t *conn)
274void connection_buf_add_compress(
const char *
string,
size_t len,
278size_t connection_get_inbuf_len(
const struct connection_t *conn);
279size_t connection_get_outbuf_len(
const struct connection_t *conn);
289struct connection_t *connection_get_by_type_state_rendquery(
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);
296 const char *resource);
299 const char *resource,
302#define CONN_LEN_AND_FREE_TEMPLATE(sl) \
304 int len = smartlist_len(sl); \
305 smartlist_free(sl); \
314 const char *resource)
319 CONN_LEN_AND_FREE_TEMPLATE(conns);
327 const char *resource,
335 CONN_LEN_AND_FREE_TEMPLATE(conns);
338#undef CONN_LEN_AND_FREE_TEMPLATE
343#define connection_speaks_cells(conn) (((conn)->type == CONN_TYPE_OR) || 0)
355 (
const struct connection_t *conn,
long apparent_skew,
int trusted,
357 const char *source));
368#define CONN_LOG_PROTECT(conn, stmt) \
370 int _log_conn_is_control; \
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(); \
380#ifdef CONNECTION_PRIVATE
386 const struct sockaddr *sa,
388 const struct sockaddr *bindaddr,
389 socklen_t bindaddr_len,
void clock_skew_warning(const connection_t *conn, long apparent_skew, int trusted, log_domain_mask_t domain, const char *received, const char *source)
bool connection_dir_is_global_write_low(const connection_t *conn, size_t attempt)
int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type, int *is_pt_out, const connection_t *conn)
STATIC smartlist_t * pick_oos_victims(int n)
connection_t * connection_get_by_type_addr_port_purpose(int type, const tor_addr_t *addr, uint16_t port, int purpose)
const listener_connection_t * CONST_TO_LISTENER_CONN(const connection_t *c)
STATIC void connection_free_minimal(connection_t *conn)
void connection_bucket_adjust(const or_options_t *options)
int any_other_active_or_conns(const or_connection_t *this_conn)
int connection_init_accepted_conn(connection_t *conn, const listener_connection_t *listener)
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)
STATIC void kill_conn_list_for_oos(smartlist_t *conns)
int connection_connect(connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
void connection_mark_all_noncontrol_listeners(void)
void connection_dir_buf_add(const char *string, size_t len, struct dir_connection_t *dir_conn, int done)
struct listener_connection_t * listener_connection_new(int type, int socket_family)
void connection_mark_for_close_internal_(struct connection_t *conn, int line, const char *file)
void assert_connection_ok(struct connection_t *conn, time_t now)
int connection_is_listener(struct connection_t *conn)
void connection_close_immediate(struct connection_t *conn)
void connection_write_to_buf_impl_(const char *string, size_t len, struct connection_t *conn, int zlib)
void log_failed_proxy_connection(struct connection_t *conn)
struct listener_connection_t * TO_LISTENER_CONN(struct connection_t *)
void connection_consider_empty_write_buckets(struct connection_t *conn)
struct control_connection_t * control_connection_new(int socket_family)
static int connection_dir_count_by_purpose_and_resource(int purpose, const char *resource)
struct or_connection_t * or_connection_new(int type, int socket_family)
void connection_dump_buffer_mem_stats(int severity)
char * alloc_http_authenticator(const char *authenticator)
int connection_wants_to_flush(struct connection_t *conn)
void connection_bucket_init(void)
int connection_flush(struct connection_t *conn)
void connection_free_(struct connection_t *conn)
const char * conn_type_to_string(int type)
struct dir_connection_t * dir_connection_new(int socket_family)
int connection_buf_get_line(struct connection_t *conn, char *data, size_t *data_len)
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)
struct edge_connection_t * edge_connection_new(int type, int socket_family)
void connection_link_connections(struct connection_t *conn_a, struct connection_t *conn_b)
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)
int connection_read_proxy_handshake(struct connection_t *conn)
struct connection_t * connection_new(int type, int socket_family)
void connection_write_bw_exhausted(struct connection_t *conn, bool is_global_bw)
const char * connection_describe_peer(const connection_t *conn)
void connection_about_to_close_connection(struct connection_t *conn)
int conn_listener_type_supports_af_unix(int type)
void connection_free_all(void)
void connection_consider_empty_read_buckets(struct connection_t *conn)
void connection_mark_for_close_(struct connection_t *conn, int line, const char *file)
int connection_process_inbuf(struct connection_t *conn, int package_partial)
ssize_t connection_bucket_write_limit(struct connection_t *conn, time_t now)
struct smartlist_t * connection_dir_list_by_purpose_and_resource(int purpose, const char *resource)
int connection_is_moribund(struct connection_t *conn)
void connection_read_bw_exhausted(struct connection_t *conn, bool is_global_bw)
int retry_all_listeners(struct smartlist_t *new_conns, int close_all_noncontrol)
int connection_state_is_open(struct connection_t *conn)
int connection_state_is_connecting(struct connection_t *conn)
struct connection_t * connection_get_by_type(int type)
void connection_buf_add_buf(struct connection_t *conn, struct buf_t *buf)
const char * connection_describe(const connection_t *conn)
int connection_proxy_connect(struct connection_t *conn, int type)
int connection_outbuf_too_full(struct connection_t *conn)
void connection_mark_all_noncontrol_connections(void)
static int connection_dir_count_by_purpose_resource_and_state(int purpose, const char *resource, int state)
struct connection_t * connection_get_by_type_nonlinked(int type)
struct entry_connection_t * entry_connection_new(int type, int socket_family)
struct connection_t * connection_get_by_type_state(int type, int state)
struct connection_t * connection_get_by_global_id(uint64_t id)
void connection_expire_held_open(void)
const char * conn_state_to_string(int type, int state)
struct smartlist_t * connection_dir_list_by_purpose_resource_and_state(int purpose, const char *resource, int state)
void connection_check_oos(int n_socks, int failed)
uint64_t log_domain_mask_t
Top-level declarations for the smartlist_t dynamic array type.
#define MOCK_DECL(rv, funcname, arglist)