Tor  0.4.7.0-alpha-dev
Macros | Functions | Variables
connection.c File Reference

General high-level functions to handle reading and writing on connections. More...

#include "core/or/or.h"
#include "feature/client/bridges.h"
#include "lib/buf/buffers.h"
#include "lib/tls/buffers_tls.h"
#include "lib/err/backtrace.h"
#include "app/config/config.h"
#include "app/config/resolve_addr.h"
#include "core/mainloop/connection.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/netstatus.h"
#include "core/or/channel.h"
#include "core/or/channeltls.h"
#include "core/or/circuitbuild.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "core/or/connection_edge.h"
#include "core/or/connection_or.h"
#include "core/or/dos.h"
#include "core/or/policies.h"
#include "core/or/reasons.h"
#include "core/or/relay.h"
#include "core/or/status.h"
#include "core/or/crypt_path.h"
#include "core/proto/proto_haproxy.h"
#include "core/proto/proto_http.h"
#include "core/proto/proto_socks.h"
#include "feature/client/dnsserv.h"
#include "feature/client/entrynodes.h"
#include "feature/client/transports.h"
#include "feature/control/control.h"
#include "feature/control/control_events.h"
#include "feature/dirauth/authmode.h"
#include "feature/dirauth/dirauth_config.h"
#include "feature/dircache/dirserv.h"
#include "feature/dircommon/directory.h"
#include "feature/hibernate/hibernate.h"
#include "feature/hs/hs_common.h"
#include "feature/hs/hs_ident.h"
#include "feature/hs/hs_metrics.h"
#include "feature/metrics/metrics.h"
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerlist.h"
#include "feature/relay/dns.h"
#include "feature/relay/ext_orport.h"
#include "feature/relay/routermode.h"
#include "feature/rend/rendcommon.h"
#include "feature/stats/connstats.h"
#include "feature/stats/rephist.h"
#include "feature/stats/bwhist.h"
#include "lib/crypt_ops/crypto_util.h"
#include "lib/crypt_ops/crypto_format.h"
#include "lib/geoip/geoip.h"
#include "lib/cc/ctassert.h"
#include "lib/sandbox/sandbox.h"
#include "lib/net/buffers_net.h"
#include "lib/tls/tortls.h"
#include "lib/evloop/compat_libevent.h"
#include "lib/compress/compress.h"
#include "feature/dircommon/dir_connection_st.h"
#include "feature/control/control_connection_st.h"
#include "core/or/entry_connection_st.h"
#include "core/or/listener_connection_st.h"
#include "core/or/or_connection_st.h"
#include "core/or/port_cfg_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "core/or/socks_request_st.h"

Go to the source code of this file.

Macros

#define CONNECTION_PRIVATE
 
#define CHANNEL_OBJECT_PRIVATE
 
#define ENABLE_LISTENER_REBIND
 
#define CASE_ANY_LISTENER_TYPE
 
#define CONN_IS_CLOSED(c)    ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
 
#define WARN_TOO_MANY_CONNS_INTERVAL   (6*60*60)
 
#define SOCKS4_STANDARD_BUFFER_SIZE   (1 + 1 + 2 + 4 + 1)
 
#define CLIENT_IDLE_TIME_FOR_PRIORITY   30
 
#define CONN_GET_ALL_TEMPLATE(var, test)
 
#define CONN_GET_TEMPLATE(var, test)
 
#define DIR_CONN_LIST_TEMPLATE(conn_var, conn_test, dirconn_var, dirconn_test)
 

Functions

static connection_tconnection_listener_new (const struct sockaddr *listensockaddr, socklen_t listensocklen, int type, const char *address, const port_cfg_t *portcfg, int *addr_in_use)
 
static connection_tconnection_listener_new_for_port (const port_cfg_t *port, int *defer, int *addr_in_use)
 
static void connection_init (time_t now, connection_t *conn, int type, int socket_family)
 
static int connection_handle_listener_read (connection_t *conn, int new_type)
 
static int connection_finished_flushing (connection_t *conn)
 
static int connection_flushed_some (connection_t *conn)
 
static int connection_finished_connecting (connection_t *conn)
 
static int connection_reached_eof (connection_t *conn)
 
static int connection_buf_read_from_socket (connection_t *conn, ssize_t *max_to_read, int *socket_error)
 
static int connection_process_inbuf (connection_t *conn, int package_partial)
 
static void client_check_address_changed (tor_socket_t sock)
 
static void set_constrained_socket_buffers (tor_socket_t sock, int size)
 
static const char * connection_proxy_state_to_string (int state)
 
static int connection_read_https_proxy_response (connection_t *conn)
 
static void connection_send_socks5_connect (connection_t *conn)
 
static const char * proxy_type_to_string (int proxy_type)
 
static int conn_get_proxy_type (const connection_t *conn)
 
const tor_addr_tconn_get_outbound_address (sa_family_t family, const or_options_t *options, unsigned int conn_type)
 
static void reenable_blocked_connection_init (const or_options_t *options)
 
static void reenable_blocked_connection_schedule (void)
 
listener_connection_tTO_LISTENER_CONN (connection_t *c)
 
const listener_connection_tCONST_TO_LISTENER_CONN (const connection_t *c)
 
size_t connection_get_inbuf_len (connection_t *conn)
 
size_t connection_get_outbuf_len (connection_t *conn)
 
const char * conn_type_to_string (int type)
 
const char * conn_state_to_string (int type, int state)
 
static const char * connection_describe_peer_internal (const connection_t *conn, bool include_preposition)
 
const char * connection_describe_peer (const connection_t *conn)
 
const char * connection_describe (const connection_t *conn)
 
dir_connection_tdir_connection_new (int socket_family)
 
or_connection_tor_connection_new (int type, int socket_family)
 
entry_connection_tentry_connection_new (int type, int socket_family)
 
edge_connection_tedge_connection_new (int type, int socket_family)
 
control_connection_tcontrol_connection_new (int socket_family)
 
listener_connection_tlistener_connection_new (int type, int socket_family)
 
connection_tconnection_new (int type, int socket_family)
 
void connection_link_connections (connection_t *conn_a, connection_t *conn_b)
 
int conn_listener_type_supports_af_unix (int type)
 
STATIC void connection_free_minimal (connection_t *conn)
 
void connection_free_ (connection_t *conn)
 
void connection_about_to_close_connection (connection_t *conn)
 
void connection_close_immediate (connection_t *conn)
 
void connection_mark_for_close_ (connection_t *conn, int line, const char *file)
 
void connection_mark_for_close_internal_ (connection_t *conn, int line, const char *file)
 
void connection_expire_held_open (void)
 
static struct sockaddr * create_unix_sockaddr (const char *listenaddress, char **readable_address, socklen_t *len_out)
 
static void socket_failed_from_resource_exhaustion (void)
 
static int make_socket_reuseable (tor_socket_t sock)
 
static int tor_listen (tor_socket_t fd)
 
static int check_sockaddr (const struct sockaddr *sa, int len, int level)
 
static int check_sockaddr_family_match (sa_family_t got, connection_t *listener)
 
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 connection_connect_log_client_use_ip_version (const connection_t *conn)
 
int connection_connect (connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
 
static int connection_https_proxy_connect (connection_t *conn)
 
static int connection_socks4_proxy_connect (connection_t *conn)
 
static int connection_socks5_proxy_connect (connection_t *conn)
 
static int connection_haproxy_proxy_connect (connection_t *conn)
 
int connection_proxy_connect (connection_t *conn, int type)
 
static int connection_fetch_from_buf_socks_client (connection_t *conn, int state, char **reason)
 
int connection_read_proxy_handshake (connection_t *conn)
 
static int retry_listener_ports (smartlist_t *old_conns, const smartlist_t *ports, smartlist_t *new_conns, smartlist_t *replacements, int control_listeners_only)
 
int retry_all_listeners (smartlist_t *new_conns, int close_all_noncontrol)
 
void connection_mark_all_noncontrol_listeners (void)
 
void connection_mark_all_noncontrol_connections (void)
 
static int connection_is_rate_limited (const connection_t *conn)
 
static int connection_counts_as_relayed_traffic (connection_t *conn, time_t now)
 
static ssize_t connection_bucket_get_share (int base, int priority, ssize_t global_bucket_val, ssize_t conn_bucket)
 
static ssize_t connection_bucket_read_limit (connection_t *conn, time_t now)
 
ssize_t connection_bucket_write_limit (connection_t *conn, time_t now)
 
bool connection_dir_is_global_write_low (const connection_t *conn, size_t attempt)
 
static void record_num_bytes_transferred_impl (connection_t *conn, time_t now, size_t num_read, size_t num_written)
 
static void connection_buckets_decrement (connection_t *conn, time_t now, size_t num_read, size_t num_written)
 
void connection_read_bw_exhausted (connection_t *conn, bool is_global_bw)
 
void connection_write_bw_exhausted (connection_t *conn, bool is_global_bw)
 
void connection_consider_empty_read_buckets (connection_t *conn)
 
void connection_consider_empty_write_buckets (connection_t *conn)
 
void connection_bucket_init (void)
 
void connection_bucket_adjust (const or_options_t *options)
 
static void connection_bucket_refill_single (connection_t *conn, uint32_t now_ts)
 
static void reenable_blocked_connections_cb (mainloop_event_t *ev, void *arg)
 
static int connection_handle_read_impl (connection_t *conn)
 
int connection_handle_read (connection_t *conn)
 
int connection_buf_get_bytes (char *string, size_t len, connection_t *conn)
 
int connection_buf_get_line (connection_t *conn, char *data, size_t *data_len)
 
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)
 
int connection_wants_to_flush (connection_t *conn)
 
int connection_outbuf_too_full (connection_t *conn)
 
static void update_send_buffer_size (tor_socket_t sock)
 
static int connection_handle_write_impl (connection_t *conn, int force)
 
int connection_handle_write (connection_t *conn, int force)
 
int connection_flush (connection_t *conn)
 
static int connection_may_write_to_buf (connection_t *conn)
 
static void connection_write_to_buf_failed (connection_t *conn)
 
static void connection_write_to_buf_commit (connection_t *conn)
 
void connection_write_to_buf_impl_ (const char *string, size_t len, connection_t *conn, int zlib)
 
void connection_dir_buf_add (const char *string, size_t len, dir_connection_t *dir_conn, int done)
 
void connection_buf_add_compress (const char *string, size_t len, dir_connection_t *conn, int done)
 
void connection_buf_add_buf (connection_t *conn, buf_t *buf)
 
smartlist_tconnection_list_by_type_state (int type, int state)
 
smartlist_tconnection_list_by_type_purpose (int type, int purpose)
 
connection_tconnection_get_by_type_addr_port_purpose (int type, const tor_addr_t *addr, uint16_t port, int purpose)
 
connection_tconnection_get_by_global_id (uint64_t id)
 
connection_tconnection_get_by_type (int type)
 
connection_tconnection_get_by_type_state (int type, int state)
 
connection_tconnection_get_by_type_nonlinked (int type)
 
smartlist_tconnection_dir_list_by_purpose_and_resource (int purpose, const char *resource)
 
smartlist_tconnection_dir_list_by_purpose_resource_and_state (int purpose, const char *resource, int state)
 
static connection_tconnection_get_another_active_or_conn (const or_connection_t *this_conn)
 
int any_other_active_or_conns (const or_connection_t *this_conn)
 
int connection_is_listener (connection_t *conn)
 
int connection_state_is_open (connection_t *conn)
 
int connection_state_is_connecting (connection_t *conn)
 
char * alloc_http_authenticator (const char *authenticator)
 
static int oos_victim_comparator_for_orconns (or_connection_t *a, or_connection_t *b)
 
static int oos_victim_comparator (const void **a_v, const void **b_v)
 
STATIC smartlist_tpick_oos_victims (int n)
 
STATIC void kill_conn_list_for_oos (smartlist_t *conns)
 
int connection_is_moribund (connection_t *conn)
 
void connection_check_oos (int n_socks, int failed)
 
void connection_dump_buffer_mem_stats (int severity)
 
void assert_connection_ok (connection_t *conn, time_t now)
 
int get_proxy_addrport (tor_addr_t *addr, uint16_t *port, int *proxy_type, int *is_pt_out, const connection_t *conn)
 
void log_failed_proxy_connection (connection_t *conn)
 
void connection_free_all (void)
 
void clock_skew_warning (const connection_t *conn, long apparent_skew, int trusted, log_domain_mask_t domain, const char *received, const char *source)
 

Variables

static tor_addr_tlast_interface_ipv4 = NULL
 
static tor_addr_tlast_interface_ipv6 = NULL
 
static smartlist_toutgoing_addrs = NULL
 
static int listen_limit = INT_MAX
 
static time_t write_buckets_last_empty_at = -100
 
static time_t last_recorded_accounting_at = 0
 
static uint32_t last_refilled_global_buckets_ts =0
 
static mainloop_event_treenable_blocked_connections_ev = NULL
 
static int reenable_blocked_connections_is_scheduled = 0
 
static struct timeval reenable_blocked_connections_delay
 

Detailed Description

General high-level functions to handle reading and writing on connections.

Each connection (ideally) represents a TLS connection, a TCP socket, a unix socket, or a UDP socket on which reads and writes can occur. (But see connection_edge.c for cases where connections can also represent streams that do not have a corresponding socket.)

The module implements the abstract type, connection_t. The subtypes are:

The base type implemented in this module is responsible for basic rate limiting, flow control, and marshalling bytes onto and off of the network (either directly or via TLS).

Connections are registered with the main loop with connection_add(). As they become able to read or write register the fact with the event main loop by calling connection_watch_events(), connection_start_reading(), or connection_start_writing(). When they no longer want to read or write, they call connection_stop_reading() or connection_stop_writing().

To queue data to be written on a connection, call connection_buf_add(). When data arrives, the connection_process_inbuf() callback is invoked, which dispatches to a type-specific function (such as connection_edge_process_inbuf() for example). Connection types that need notice of when data has been written receive notification via connection_flushed_some() and connection_finished_flushing(). These functions all delegate to type-specific implementations.

Additionally, beyond the core of connection_t, this module also implements:

Definition in file connection.c.

Macro Definition Documentation

◆ CASE_ANY_LISTENER_TYPE

#define CASE_ANY_LISTENER_TYPE
Value:
#define CONN_TYPE_AP_HTTP_CONNECT_LISTENER
Definition: connection.h:75
#define CONN_TYPE_DIR_LISTENER
Definition: connection.h:53
#define CONN_TYPE_OR_LISTENER
Definition: connection.h:41
#define CONN_TYPE_METRICS_LISTENER
Definition: connection.h:77
#define CONN_TYPE_CONTROL_LISTENER
Definition: connection.h:58
#define CONN_TYPE_EXT_OR_LISTENER
Definition: connection.h:73
#define CONN_TYPE_AP_NATD_LISTENER
Definition: connection.h:66
#define CONN_TYPE_AP_LISTENER
Definition: connection.h:48
#define CONN_TYPE_AP_DNS_LISTENER
Definition: connection.h:68
#define CONN_TYPE_AP_TRANS_LISTENER
Definition: connection.h:63

Definition at line 213 of file connection.c.

◆ CLIENT_IDLE_TIME_FOR_PRIORITY

#define CLIENT_IDLE_TIME_FOR_PRIORITY   30

How many seconds of no active local circuits will make the connection revert to the "relayed" bandwidth class?

Definition at line 3382 of file connection.c.

◆ CONN_GET_ALL_TEMPLATE

#define CONN_GET_ALL_TEMPLATE (   var,
  test 
)
Value:
STMT_BEGIN \
smartlist_t *conns = get_connection_array(); \
smartlist_t *ret_conns = smartlist_new(); \
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, var) { \
if (var && (test) && !var->marked_for_close) \
smartlist_add(ret_conns, var); \
} SMARTLIST_FOREACH_END(var); \
return ret_conns; \
STMT_END
smartlist_t * get_connection_array(void)
Definition: mainloop.c:451
smartlist_t * smartlist_new(void)

Definition at line 4746 of file connection.c.

◆ CONN_GET_TEMPLATE

#define CONN_GET_TEMPLATE (   var,
  test 
)
Value:
STMT_BEGIN \
smartlist_t *conns = get_connection_array(); \
SMARTLIST_FOREACH(conns, connection_t *, var, \
{ \
if (var && (test) && !var->marked_for_close) \
return var; \
}); \
return NULL; \
STMT_END

Return a connection_t * from get_connection_array() that satisfies test on var, and that is not marked for close.

Definition at line 4780 of file connection.c.

◆ CONN_IS_CLOSED

#define CONN_IS_CLOSED (   c)     ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))

Return true iff connection_close_immediate() has been called on this connection.

Definition at line 1035 of file connection.c.

◆ DIR_CONN_LIST_TEMPLATE

#define DIR_CONN_LIST_TEMPLATE (   conn_var,
  conn_test,
  dirconn_var,
  dirconn_test 
)
Value:
STMT_BEGIN \
smartlist_t *conns = get_connection_array(); \
smartlist_t *dir_conns = smartlist_new(); \
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn_var) { \
if (conn_var && (conn_test) \
&& conn_var->type == CONN_TYPE_DIR \
&& !conn_var->marked_for_close) { \
dir_connection_t *dirconn_var = TO_DIR_CONN(conn_var); \
if (dirconn_var && (dirconn_test)) { \
smartlist_add(dir_conns, dirconn_var); \
} \
} \
} SMARTLIST_FOREACH_END(conn_var); \
return dir_conns; \
STMT_END
#define CONN_TYPE_DIR
Definition: connection.h:55
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:88

Return a new smartlist of dir_connection_t * from get_connection_array() that satisfy conn_test on connection_t *conn_var, and dirconn_test on dir_connection_t *dirconn_var. conn_var must be of CONN_TYPE_DIR and not marked for close to be included in the list.

Definition at line 4846 of file connection.c.

◆ ENABLE_LISTENER_REBIND

#define ENABLE_LISTENER_REBIND

On Windows and Linux we cannot reliably bind() a socket to an address and port if: 1) There's already a socket bound to wildcard address (0.0.0.0 or ::) with the same port; 2) We try to bind() to wildcard address and there's another socket bound to a specific address and the same port.

To address this problem on these two platforms we implement a routine that: 1) Checks if first attempt to bind() a new socket failed with EADDRINUSE. 2) If so, it will close the appropriate old listener connection and 3) Attempts bind()'ing the new listener socket again.

Just to be safe, we are enabling listener rebind code on all platforms, to account for unexpected cases where it may be needed.

Definition at line 166 of file connection.c.

Function Documentation

◆ alloc_http_authenticator()

char* alloc_http_authenticator ( const char *  authenticator)

Allocates a base64'ed authenticator for use in http or https auth, based on the input string authenticator. Returns it if success, else returns NULL.

Definition at line 4999 of file connection.c.

Referenced by connection_https_proxy_connect().

◆ any_other_active_or_conns()

int any_other_active_or_conns ( const or_connection_t this_conn)

Return 1 if there are any active OR connections apart from this_conn.

We use this to guess if we should tell the controller that we didn't manage to connect to any of our bridges.

Definition at line 4920 of file connection.c.

◆ assert_connection_ok()

void assert_connection_ok ( connection_t conn,
time_t  now 
)

Verify that connection conn has all of its invariants correct. Trigger an assert if anything is invalid.

Definition at line 5571 of file connection.c.

Referenced by add_connection_to_closeable_list(), connection_close_immediate(), connection_mark_for_close_(), connection_mark_for_close_internal_(), connection_or_finished_flushing(), dns_resolve_impl(), and inform_pending_connections().

◆ check_sockaddr()

static int check_sockaddr ( const struct sockaddr *  sa,
int  len,
int  level 
)
static

Do basic sanity checking on a newly received socket. Return 0 if it looks ok, else return -1.

Notably, some TCP stacks can erroneously have accept() return successfully with socklen 0, when the client sends an RST before the accept call (as nmap does). We want to detect that, and not go on with the connection.

Definition at line 1850 of file connection.c.

◆ check_sockaddr_family_match()

static int check_sockaddr_family_match ( sa_family_t  got,
connection_t listener 
)
static

Check whether the socket family from an accepted socket got is the same as the one that listener is waiting for. If it isn't, log a useful message and return -1. Else return 0.

This is annoying, but can apparently happen on some Darwins.

Definition at line 1893 of file connection.c.

◆ client_check_address_changed()

static void client_check_address_changed ( tor_socket_t  sock)
static

Given a socket handle, check whether the local address (sockname) of the socket is one that we've connected from before. If so, double-check whether our address has changed and we need to generate keys. If we do, call init_keys().

Definition at line 5020 of file connection.c.

Referenced by connection_finished_connecting().

◆ clock_skew_warning()

void clock_skew_warning ( const connection_t conn,
long  apparent_skew,
int  trusted,
log_domain_mask_t  domain,
const char *  received,
const char *  source 
)

Log a warning, and possibly emit a control event, that received came at a skewed time. trusted indicates that the source was one that we had more faith in and therefore the warning level should have higher severity.

Definition at line 5861 of file connection.c.

◆ conn_get_outbound_address()

const tor_addr_t * conn_get_outbound_address ( sa_family_t  family,
const or_options_t options,
unsigned int  conn_type 
)

Retrieve the outbound address depending on the protocol (IPv4 or IPv6) and the connection type (relay, exit, ...) Return a socket address or NULL in case nothing is configured.

Definition at line 2339 of file connection.c.

◆ conn_get_proxy_type()

static int conn_get_proxy_type ( const connection_t conn)
static

Returns the proxy type used by tor for a single connection, for logging or high-level purposes. Don't use it to fill the proxy_type field of or_connection_t; use the actual proxy protocol instead.

Definition at line 2513 of file connection.c.

Referenced by connection_socks5_proxy_connect().

◆ conn_listener_type_supports_af_unix()

int conn_listener_type_supports_af_unix ( int  type)

Return true iff the provided connection listener type supports AF_UNIX sockets.

Definition at line 759 of file connection.c.

◆ conn_state_to_string()

const char* conn_state_to_string ( int  type,
int  state 
)

Return the human-readable name for the connection state state for the connection type type

Definition at line 302 of file connection.c.

Referenced by connection_describe(), connection_expire_held_open(), and connection_or_get_state_description().

◆ conn_type_to_string()

const char* conn_type_to_string ( int  type)

Return the human-readable name for the connection type type

Definition at line 268 of file connection.c.

Referenced by connection_describe(), and connection_expire_held_open().

◆ connection_about_to_close_connection()

void connection_about_to_close_connection ( connection_t conn)

Called when we're about to finally unlink and free a connection: perform necessary accounting and cleanup

  • Directory conns that failed to fetch a rendezvous descriptor need to inform pending rendezvous streams.
  • OR conns need to call rep_hist_note_*() to record status.
  • AP conns need to send a socks reject if necessary.
  • Exit conns need to call connection_dns_remove() if necessary.
  • AP and Exit conns need to send an end cell if they can.
  • DNS conns need to fail any resolves that are pending on them.
  • OR and edge connections need to be unlinked from circuits.

Definition at line 1012 of file connection.c.

Referenced by connection_unlink().

◆ connection_bucket_adjust()

void connection_bucket_adjust ( const or_options_t options)

Update the global connection bucket settings to a new value.

Definition at line 3780 of file connection.c.

Referenced by options_act_relay_bandwidth().

◆ connection_bucket_get_share()

static ssize_t connection_bucket_get_share ( int  base,
int  priority,
ssize_t  global_bucket_val,
ssize_t  conn_bucket 
)
static

Helper function to decide how many bytes out of global_bucket we're willing to use for this transaction. base is the size of a cell on the network; priority says whether we should write many of them or just a few; and conn_bucket (if non-negative) provides an upper limit for our answer.

Definition at line 3406 of file connection.c.

◆ connection_bucket_init()

void connection_bucket_init ( void  )

Initialize the global buckets to the values configured in the options

Definition at line 3755 of file connection.c.

◆ connection_bucket_read_limit()

static ssize_t connection_bucket_read_limit ( connection_t conn,
time_t  now 
)
static

How many bytes at most can we read onto this connection?

Definition at line 3436 of file connection.c.

Referenced by connection_buf_read_from_socket().

◆ connection_bucket_refill_single()

static void connection_bucket_refill_single ( connection_t conn,
uint32_t  now_ts 
)
static

Refill the token buckets for a single connection conn, and the global token buckets as appropriate. Requires that now_ts is the time in coarse timestamp units.

Definition at line 3807 of file connection.c.

Referenced by connection_handle_read_impl().

◆ connection_bucket_write_limit()

ssize_t connection_bucket_write_limit ( connection_t conn,
time_t  now 
)

How many bytes at most can we write onto this connection?

Definition at line 3476 of file connection.c.

◆ connection_buckets_decrement()

static void connection_buckets_decrement ( connection_t conn,
time_t  now,
size_t  num_read,
size_t  num_written 
)
static

We just read num_read and wrote num_written bytes onto conn. Decrement buckets appropriately.

Definition at line 3629 of file connection.c.

◆ connection_buf_add_buf()

void connection_buf_add_buf ( connection_t conn,
buf_t *  buf 
)

Add all bytes from buf to conn's outbuf, draining them from buf. (If the connection is marked and will soon be closed, nothing is drained.)

Definition at line 4731 of file connection.c.

◆ connection_buf_get_bytes()

int connection_buf_get_bytes ( char *  string,
size_t  len,
connection_t conn 
)

A pass-through to fetch_from_buf.

Definition at line 4232 of file connection.c.

◆ connection_buf_get_line()

int connection_buf_get_line ( connection_t conn,
char *  data,
size_t *  data_len 
)

As buf_get_line(), but read from a connection's input buffer.

Definition at line 4239 of file connection.c.

◆ connection_buf_read_from_socket()

static int connection_buf_read_from_socket ( connection_t conn,
ssize_t *  max_to_read,
int *  socket_error 
)
static

Pull in new bytes from conn->s or conn->linked_conn onto conn->inbuf, either directly or via TLS. Reduce the token buckets by the number of bytes read.

If *max_to_read is -1, then decide it ourselves, else go with the value passed to us. When returning, if it's changed, subtract the number of bytes we read from *max_to_read.

Return -1 if we want to break conn, else return 0.

Definition at line 4041 of file connection.c.

Referenced by connection_handle_read_impl().

◆ connection_check_oos()

void connection_check_oos ( int  n_socks,
int  failed 
)

Out-of-Sockets handler; n_socks is the current number of open sockets, and failed is non-zero if a socket exhaustion related error immediately preceded this call. This is where to do circuit-killing heuristics as needed.

Definition at line 5438 of file connection.c.

◆ connection_close_immediate()

void connection_close_immediate ( connection_t conn)

Close the underlying socket for conn, so we don't try to flush it. Must be used in conjunction with (right before) connection_mark_for_close().

Definition at line 1043 of file connection.c.

◆ connection_connect()

int connection_connect ( connection_t conn,
const char *  address,
const tor_addr_t addr,
uint16_t  port,
int *  socket_error 
)

Take conn, make a nonblocking socket; try to connect to addr:port (port arrives in host order). If fail, return -1 and if applicable put your best guess about errno into *socket_error. Else assign s to conn->s: if connected return 1, if EAGAIN return 0.

addr:port can be different to conn->addr:conn->port if connecting through a proxy.

address is used to make the logs useful.

On success, add conn to the list of polled connections.

Definition at line 2396 of file connection.c.

◆ connection_connect_sockaddr()

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 
)

Take conn, make a nonblocking socket; try to connect to sa, binding to bindaddr if sa is not localhost. If fail, return -1 and if applicable put your best guess about errno into *socket_error. If connected return 1, if EAGAIN return 0.

Definition at line 2163 of file connection.c.

◆ connection_consider_empty_read_buckets()

void connection_consider_empty_read_buckets ( connection_t conn)

If we have exhausted our global buckets, or the buckets for conn, stop reading.

Definition at line 3698 of file connection.c.

◆ connection_consider_empty_write_buckets()

void connection_consider_empty_write_buckets ( connection_t conn)

If we have exhausted our global buckets, or the buckets for conn, stop writing.

Definition at line 3727 of file connection.c.

◆ connection_counts_as_relayed_traffic()

static int connection_counts_as_relayed_traffic ( connection_t conn,
time_t  now 
)
static

Return 1 if conn should use tokens from the "relayed" bandwidth rates, else 0. Currently, only OR conns with bandwidth class 1, and directory conns that are serving data out, count.

Definition at line 3389 of file connection.c.

◆ connection_describe()

const char* connection_describe ( const connection_t conn)

Describe a connection for logging purposes.

Returns a pointer to a static buffer; future calls to connection_describe() will invalidate this buffer.

Nobody should parse the output of this function; it can and will change in future versions of tor.

Definition at line 543 of file connection.c.

Referenced by dumpstats().

◆ connection_describe_peer()

const char* connection_describe_peer ( const connection_t conn)

Describe the peer or address of connection conn in a human-readable manner.

Returns a pointer to a static buffer; future calls to connection_describe_peer() or connection_describe() will invalidate this buffer.

Nobody should parse the output of this function; it can and will change in future versions of tor.

Definition at line 528 of file connection.c.

Referenced by handle_response_fetch_consensus().

◆ connection_describe_peer_internal()

static const char* connection_describe_peer_internal ( const connection_t conn,
bool  include_preposition 
)
static

Helper: describe the peer or address of connection conn in a human-readable manner.

Returns a pointer to a static buffer; future calls to connection_describe_peer_internal() will invalidate this buffer.

If include_preposition is true, include a preposition before the peer address.

Nobody should parse the output of this function; it can and will change in future versions of tor.

Definition at line 403 of file connection.c.

Referenced by connection_describe(), and connection_describe_peer().

◆ connection_dir_buf_add()

void connection_dir_buf_add ( const char *  string,
size_t  len,
dir_connection_t dir_conn,
int  done 
)

Write a string (of size len to directory connection dir_conn. Apply compression if connection is configured to use it and finalize it if done is true.

Definition at line 4707 of file connection.c.

◆ connection_dir_is_global_write_low()

bool connection_dir_is_global_write_low ( const connection_t conn,
size_t  attempt 
)

Return true iff the global write buckets are low enough that we shouldn't send attempt bytes of low-priority directory stuff out to conn.

If we are a directory authority, always answer dir requests thus true is always returned.

Note: There are a lot of parameters we could use here:

  • global_relayed_write_bucket. Low is bad.
  • global_write_bucket. Low is bad.
  • bandwidthrate. Low is bad.
  • bandwidthburst. Not a big factor?
  • attempt. High is bad.
  • total bytes queued on outbufs. High is bad. But I'm wary of using this, since a few slow-flushing queues will pump up the number without meaning what we meant to mean. What we really mean is "total directory bytes added to outbufs recently", but that's harder to quantify and harder to keep track of.

Definition at line 3531 of file connection.c.

◆ connection_dir_list_by_purpose_and_resource()

smartlist_t* connection_dir_list_by_purpose_and_resource ( int  purpose,
const char *  resource 
)

Return a list of directory connections that are fetching the item described by purpose/resource. If there are none, return an empty list. This list must be freed using smartlist_free, but the pointers in it must not be freed. Note that this list should not be cached, as the pointers in it can be freed if their connections close.

Definition at line 4871 of file connection.c.

Referenced by connection_dir_count_by_purpose_and_resource().

◆ connection_dir_list_by_purpose_resource_and_state()

smartlist_t* connection_dir_list_by_purpose_resource_and_state ( int  purpose,
const char *  resource,
int  state 
)

Return a list of directory connections that are fetching the item described by purpose/resource/state. If there are none, return an empty list. This list must be freed using smartlist_free, but the pointers in it must not be freed. Note that this list should not be cached, as the pointers in it can be freed if their connections close.

Definition at line 4889 of file connection.c.

Referenced by connection_dir_count_by_purpose_resource_and_state().

◆ connection_dump_buffer_mem_stats()

void connection_dump_buffer_mem_stats ( int  severity)

Log how many bytes are used by buffers of different kinds and sizes.

Definition at line 5522 of file connection.c.

Referenced by dumpmemusage().

◆ connection_expire_held_open()

void connection_expire_held_open ( void  )

Find each connection that has hold_open_until_flushed set to 1 but hasn't written in the past 15 seconds, and set hold_open_until_flushed to 0. This means it will get cleaned up in the next loop through close_if_marked() in main.c.

Definition at line 1159 of file connection.c.

Referenced by second_elapsed_callback().

◆ connection_fetch_from_buf_http()

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 
)

As fetch_from_buf_http, but fetches from a connection's input buffer_t as appropriate.

Definition at line 4248 of file connection.c.

Referenced by directory_handle_command(), and metrics_connection_process_inbuf().

◆ connection_fetch_from_buf_socks_client()

static int connection_fetch_from_buf_socks_client ( connection_t conn,
int  state,
char **  reason 
)
static

Wrapper around fetch_from_buf_socks_client: see that functions for documentation of its behavior.

Definition at line 2886 of file connection.c.

◆ connection_finished_connecting()

static int connection_finished_connecting ( connection_t conn)
static

Called when our attempt to connect() to a server has just succeeded.

This function checks if the interface address has changed (clients only), and then passes conn to the connection-specific connection_*_finished_connecting() function.

Definition at line 5206 of file connection.c.

◆ connection_finished_flushing()

static int connection_finished_flushing ( connection_t conn)
static

We just finished flushing bytes to the appropriately low network layer, and there are no more bytes remaining in conn->outbuf or conn->tls to be flushed.

This function just passes conn to the connection-specific connection_*_finished_flushing() function.

Definition at line 5166 of file connection.c.

◆ connection_flush()

int connection_flush ( connection_t conn)

Try to flush data that's waiting for a write on conn. Return -1 on failure, 0 on success.

Don't use this function for regular writing; the buffers system should be good enough at scheduling writes there. Instead, this function is for cases when we're about to exit or something and we want to report it right away.

Definition at line 4599 of file connection.c.

◆ connection_flushed_some()

static int connection_flushed_some ( connection_t conn)
static

Called whenever we've written data on a connection.

Definition at line 5141 of file connection.c.

◆ connection_free_()

void connection_free_ ( connection_t conn)

Make sure conn isn't in any of the global conn lists; then free it.

Definition at line 960 of file connection.c.

Referenced by circuit_free_all().

◆ connection_free_all()

void connection_free_all ( void  )

Call connection_free_minimal() on every connection in our array, and release all storage held by connection.c.

Don't do the checks in connection_free(), because they will fail.

Definition at line 5818 of file connection.c.

◆ connection_free_minimal()

STATIC void connection_free_minimal ( connection_t conn)

Deallocate memory used by conn. Deallocate its buffers if necessary, close its socket if necessary, and mark the directory as dirty if conn is an OR or OP connection.

Definition at line 777 of file connection.c.

Referenced by connection_free_all().

◆ connection_get_another_active_or_conn()

static connection_t* connection_get_another_active_or_conn ( const or_connection_t this_conn)
static

Return an arbitrary active OR connection that isn't this_conn.

We use this to guess if we should tell the controller that we didn't manage to connect to any of our bridges.

Definition at line 4908 of file connection.c.

Referenced by any_other_active_or_conns().

◆ connection_get_by_global_id()

connection_t* connection_get_by_global_id ( uint64_t  id)

Return the stream with id id if it is not already marked for close.

Definition at line 4810 of file connection.c.

Referenced by get_stream().

◆ connection_get_by_type()

connection_t* connection_get_by_type ( int  type)

Return a connection of type type that is not marked for close.

Definition at line 4818 of file connection.c.

◆ connection_get_by_type_addr_port_purpose()

connection_t* connection_get_by_type_addr_port_purpose ( int  type,
const tor_addr_t addr,
uint16_t  port,
int  purpose 
)

Return a connection with given type, address, port, and purpose; or NULL if no such connection exists (or if all such connections are marked for close).

Definition at line 4797 of file connection.c.

Referenced by launch_direct_bridge_descriptor_fetch().

◆ connection_get_by_type_nonlinked()

connection_t* connection_get_by_type_nonlinked ( int  type)

Return a connection of type type that is not an internally linked connection, and is not marked for close.

Definition at line 4837 of file connection.c.

Referenced by check_network_participation_callback().

◆ connection_get_by_type_state()

connection_t* connection_get_by_type_state ( int  type,
int  state 
)

Return a connection of type type that is in state state, and that is not marked for close.

Definition at line 4827 of file connection.c.

Referenced by directory_all_unreachable_cb().

◆ connection_handle_listener_read()

static int connection_handle_listener_read ( connection_t conn,
int  new_type 
)
static

The listener connection conn told poll() it wanted to read. Call accept() on conn->s, and add the new connection if necessary.

Definition at line 1911 of file connection.c.

Referenced by connection_handle_read_impl().

◆ connection_handle_read_impl()

static int connection_handle_read_impl ( connection_t conn)
static

Read bytes from conn->s and process them.

It calls connection_buf_read_from_socket() to bring in any new bytes, and then calls connection_process_inbuf() to process them.

Mark the connection and return -1 if you want to close it, else return 0.

Definition at line 3905 of file connection.c.

◆ connection_handle_write_impl()

static int connection_handle_write_impl ( connection_t conn,
int  force 
)
static

Try to flush more bytes onto conn->s.

This function is called in connection_handle_write(), which gets called from conn_write_callback() in main.c when libevent tells us that conn wants to write.

Update conn->timestamp_last_write_allowed to now, and call flush_buf or flush_buf_tls appropriately. If it succeeds and there are no more more bytes on conn->outbuf, then call connection_finished_flushing on it too.

If force, then write as many bytes as possible, ignoring bandwidth limits. (Used for flushing messages to controller connections on fatal errors.)

Mark the connection and return -1 if you want to close it, else return 0.

Definition at line 4330 of file connection.c.

◆ connection_haproxy_proxy_connect()

static int connection_haproxy_proxy_connect ( connection_t conn)
static

Write a proxy request of haproxy to conn for conn->addr:conn->port.

Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.

Definition at line 2716 of file connection.c.

◆ connection_https_proxy_connect()

static int connection_https_proxy_connect ( connection_t conn)
static

Write a proxy request of https to conn for conn->addr:conn->port, authenticating with the auth details given in the configuration (if available).

Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.

Definition at line 2555 of file connection.c.

◆ connection_init()

static void connection_init ( time_t  now,
connection_t conn,
int  type,
int  socket_family 
)
static

Initializes conn. (you must call connection_add() to link it into the main array).

Set conn->magic to the correct value.

Set conn->type to type. Set conn->s and conn->conn_array_index to -1 to signify they are not yet assigned.

Initialize conn's timestamps to now.

Definition at line 697 of file connection.c.

◆ connection_init_accepted_conn()

int connection_init_accepted_conn ( connection_t conn,
const listener_connection_t listener 
)

Initialize states for newly accepted connection conn.

If conn is an OR, start the TLS handshake.

If conn is a transparent AP, get its original destination and place it in circuit_wait.

The listener parameter is only used for AP connections.

Definition at line 2091 of file connection.c.

◆ connection_is_listener()

int connection_is_listener ( connection_t conn)

Return 1 if conn is a listener conn, else return 0.

Definition at line 4936 of file connection.c.

Referenced by connection_mark_all_noncontrol_listeners(), dumpstats(), and retry_all_listeners().

◆ connection_is_moribund()

int connection_is_moribund ( connection_t conn)

Check if a connection is on the way out so the OOS handler doesn't try to kill more than it needs.

Definition at line 5421 of file connection.c.

Referenced by connection_count_moribund().

◆ connection_is_rate_limited()

static int connection_is_rate_limited ( const connection_t conn)
static

Return 1 if we should apply rate limiting to conn, and 0 otherwise. Right now this just checks if it's an internal IP address or an internal connection. We also should, but don't, check if the connection uses pluggable transports, since we should then limit it even if it comes from an internal IP address.

Definition at line 3360 of file connection.c.

Referenced by connection_consider_empty_read_buckets(), connection_consider_empty_write_buckets(), and record_num_bytes_transferred_impl().

◆ connection_link_connections()

void connection_link_connections ( connection_t conn_a,
connection_t conn_b 
)

Create a link between conn_a and conn_b.

Definition at line 745 of file connection.c.

◆ connection_listener_new()

static connection_t * connection_listener_new ( const struct sockaddr *  listensockaddr,
socklen_t  socklen,
int  type,
const char *  address,
const port_cfg_t port_cfg,
int *  addr_in_use 
)
static

Bind a new non-blocking socket listening to the socket described by listensockaddr.

address is only used for logging purposes and to add the information to the conn.

Set addr_in_use to true in case socket binding fails with EADDRINUSE.

Definition at line 1459 of file connection.c.

◆ connection_listener_new_for_port()

static connection_t * connection_listener_new_for_port ( const port_cfg_t port,
int *  defer,
int *  addr_in_use 
)
static

Create a new listener connection for a given port. In case we for a reason that is not an error condition, set defer to true. If we cannot bind listening socket because address is already in use, set addr_in_use to true.

Definition at line 1785 of file connection.c.

◆ connection_mark_all_noncontrol_connections()

void connection_mark_all_noncontrol_connections ( void  )

Mark every external connection not used for controllers for close.

Definition at line 3319 of file connection.c.

◆ connection_mark_all_noncontrol_listeners()

void connection_mark_all_noncontrol_listeners ( void  )

Mark every listener of type other than CONTROL_LISTENER to be closed.

Definition at line 3305 of file connection.c.

◆ connection_mark_for_close_()

void connection_mark_for_close_ ( connection_t conn,
int  line,
const char *  file 
)

Mark conn to be closed next time we loop through conn_close_if_marked() in main.c.

Definition at line 1076 of file connection.c.

Referenced by connection_mark_unattached_ap_().

◆ connection_mark_for_close_internal_()

void connection_mark_for_close_internal_ ( connection_t conn,
int  line,
const char *  file 
)

Mark conn to be closed next time we loop through conn_close_if_marked() in main.c.

This _internal version bypasses the CONN_TYPE_OR checks; this should be called when you either are sure that if this is an or_connection_t the controlling channel has been notified (e.g. with connection_or_notify_error()), or you actually are the connection_or_close_for_error() or connection_or_close_normally() function. For all other cases, use connection_mark_and_flush() which checks for or_connection_t properly, instead. See below.

We want to keep this function simple and quick, since it can be called from quite deep in the call chain, and hence it should avoid having side-effects that interfere with its callers view of the connection.

Definition at line 1117 of file connection.c.

◆ connection_may_write_to_buf()

static int connection_may_write_to_buf ( connection_t conn)
static

Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:

Return true iff it is okay to queue bytes on conn's outbuf for writing.

Definition at line 4610 of file connection.c.

Referenced by connection_buf_add_buf(), and connection_write_to_buf_impl_().

◆ connection_new()

connection_t* connection_new ( int  type,
int  socket_family 
)

Allocate, initialize, and return a new connection_t subtype of type to make or receive connections of address family socket_family. The type should be one of the CONN_TYPE_* constants.

Definition at line 656 of file connection.c.

◆ connection_outbuf_too_full()

int connection_outbuf_too_full ( connection_t conn)

Are there too many bytes on edge connection conn's outbuf to send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by connection_edge_consider_sending_sendme().

Definition at line 4269 of file connection.c.

◆ connection_process_inbuf()

static int connection_process_inbuf ( connection_t conn,
int  package_partial 
)
static

Process new bytes that have arrived on conn->inbuf.

This function just passes conn to the connection-specific connection_*_process_inbuf() function. It also passes in package_partial if wanted.

Definition at line 5113 of file connection.c.

◆ connection_proxy_connect()

int connection_proxy_connect ( connection_t conn,
int  type 
)

Write a proxy request of type (socks4, socks5, https, haproxy) to conn for conn->addr:conn->port, authenticating with the auth details given in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies support authentication.

Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.

Use connection_read_proxy_handshake() to complete the handshake.

Definition at line 2750 of file connection.c.

◆ connection_proxy_state_to_string()

static const char * connection_proxy_state_to_string ( int  state)
static

Convert state number to string representation for logging purposes.

Definition at line 2484 of file connection.c.

◆ connection_reached_eof()

static int connection_reached_eof ( connection_t conn)
static

Callback: invoked when a connection reaches an EOF event.

Definition at line 5234 of file connection.c.

◆ connection_read_bw_exhausted()

void connection_read_bw_exhausted ( connection_t conn,
bool  is_global_bw 
)

Mark conn as needing to stop reading because bandwidth has been exhausted. If is_global_bw, it is closing because global bandwidth limit has been exhausted. Otherwise, it is closing because its own bandwidth limit has been exhausted.

Definition at line 3672 of file connection.c.

◆ connection_read_https_proxy_response()

static int connection_read_https_proxy_response ( connection_t conn)
static

Read conn's inbuf. If the http response from the proxy is all here, make sure it's good news, then return 1. If it's bad news, return -1. Else return 0 and hope for better luck next time.

Definition at line 2793 of file connection.c.

◆ connection_read_proxy_handshake()

int connection_read_proxy_handshake ( connection_t conn)

Call this from connection_*_process_inbuf() to advance the proxy handshake.

No matter what proxy protocol is used, if this function returns 1, the handshake is complete, and the data remaining on inbuf may contain the start of the communication with the requested server.

Returns 0 if the current buffer contains an incomplete response, and -1 on error.

Definition at line 2903 of file connection.c.

Referenced by connection_or_process_inbuf().

◆ connection_send_socks5_connect()

static void connection_send_socks5_connect ( connection_t conn)
static

Send SOCKS5 CONNECT command to conn, copying conn->addr and conn->port into the request.

Definition at line 2854 of file connection.c.

◆ connection_socks4_proxy_connect()

static int connection_socks4_proxy_connect ( connection_t conn)
static

Write a proxy request of socks4 to conn for conn->addr:conn->port.

Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.

Definition at line 2599 of file connection.c.

◆ connection_socks5_proxy_connect()

static int connection_socks5_proxy_connect ( connection_t conn)
static

Write a proxy request of socks5 to conn for conn->addr:conn->port, authenticating with the auth details given in the configuration (if available).

Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.

Definition at line 2678 of file connection.c.

◆ connection_state_is_connecting()

int connection_state_is_connecting ( connection_t conn)

Return 1 if conn is in 'connecting' state, else return 0.

Definition at line 4976 of file connection.c.

◆ connection_state_is_open()

int connection_state_is_open ( connection_t conn)

Return 1 if conn is in state "open" and is not marked for close, else return 0.

Definition at line 4956 of file connection.c.

◆ connection_wants_to_flush()

int connection_wants_to_flush ( connection_t conn)

Return true if this connection has data to flush.

Definition at line 4259 of file connection.c.

◆ connection_write_bw_exhausted()

void connection_write_bw_exhausted ( connection_t conn,
bool  is_global_bw 
)

Mark conn as needing to stop reading because write bandwidth has been exhausted. If is_global_bw, it is closing because global bandwidth limit has been exhausted. Otherwise, it is closing because its own bandwidth limit has been exhausted.

Definition at line 3687 of file connection.c.

◆ connection_write_to_buf_commit()

static void connection_write_to_buf_commit ( connection_t conn)
static

Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:

Called when an attempt to add bytes on conn's outbuf has succeeded: start writing if appropriate.

Definition at line 4654 of file connection.c.

Referenced by connection_buf_add_buf(), and connection_write_to_buf_impl_().

◆ connection_write_to_buf_failed()

static void connection_write_to_buf_failed ( connection_t conn)
static

Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:

Called when an attempt to add bytes on conn's outbuf has failed; mark the connection and warn as appropriate.

Definition at line 4625 of file connection.c.

Referenced by connection_write_to_buf_impl_().

◆ connection_write_to_buf_impl_()

void connection_write_to_buf_impl_ ( const char *  string,
size_t  len,
connection_t conn,
int  zlib 
)

Append len bytes of string onto conn's outbuf, and ask it to start writing.

If zlib is nonzero, this is a directory connection that should get its contents compressed or decompressed as they're written. If zlib is negative, this is the last data to be compressed, and the connection's zlib state should be flushed.

Definition at line 4675 of file connection.c.

◆ CONST_TO_LISTENER_CONN()

const listener_connection_t* CONST_TO_LISTENER_CONN ( const connection_t c)

Cast a const connection_t * to a const listener_connection_t *.

Exit with an assertion failure if the input is not a listener_connection_t.

Definition at line 247 of file connection.c.

◆ control_connection_new()

control_connection_t* control_connection_new ( int  socket_family)

Allocate and return a new control_connection_t, initialized as by connection_init().

Definition at line 632 of file connection.c.

◆ dir_connection_new()

dir_connection_t* dir_connection_new ( int  socket_family)

Allocate and return a new dir_connection_t, initialized as by connection_init().

Definition at line 561 of file connection.c.

◆ edge_connection_new()

edge_connection_t* edge_connection_new ( int  type,
int  socket_family 
)

Allocate and return a new edge_connection_t, initialized as by connection_init().

Definition at line 621 of file connection.c.

Referenced by connection_exit_begin_resolve().

◆ entry_connection_new()

entry_connection_t* entry_connection_new ( int  type,
int  socket_family 
)

Allocate and return a new entry_connection_t, initialized as by connection_init().

Allocate space to store the socks_request.

Definition at line 602 of file connection.c.

Referenced by dnsserv_launch_request().

◆ get_proxy_addrport()

int get_proxy_addrport ( tor_addr_t addr,
uint16_t *  port,
int *  proxy_type,
int *  is_pt_out,
const connection_t conn 
)

Fills addr and port with the details of the global proxy server we are using. Store a 1 to the int pointed to by is_put_out if the connection is using a pluggable transport; store 0 otherwise. conn contains the connection we are using the proxy for.

Return 0 on success, -1 on failure.

Definition at line 5716 of file connection.c.

Referenced by log_failed_proxy_connection().

◆ kill_conn_list_for_oos()

STATIC void kill_conn_list_for_oos ( smartlist_t conns)

Kill a list of connections for the OOS handler.

Definition at line 5400 of file connection.c.

◆ listener_connection_new()

listener_connection_t* listener_connection_new ( int  type,
int  socket_family 
)

Allocate and return a new listener_connection_t, initialized as by connection_init().

Definition at line 644 of file connection.c.

◆ log_failed_proxy_connection()

void log_failed_proxy_connection ( connection_t conn)

Log a failed connection to a proxy server. conn is the connection we use the proxy server for.

Definition at line 5777 of file connection.c.

◆ make_socket_reuseable()

static int make_socket_reuseable ( tor_socket_t  sock)
static

Tell the TCP stack that it shouldn't wait for a long time after sock has closed before reusing its port. Return 0 on success, -1 on failure.

Definition at line 1383 of file connection.c.

◆ oos_victim_comparator()

static int oos_victim_comparator ( const void **  a_v,
const void **  b_v 
)
static

Sort comparator for OOS victims; better targets sort before worse ones.

Definition at line 5274 of file connection.c.

◆ oos_victim_comparator_for_orconns()

static int oos_victim_comparator_for_orconns ( or_connection_t a,
or_connection_t b 
)
static

Comparator for the two-orconn case in OOS victim sort

Definition at line 5258 of file connection.c.

Referenced by oos_victim_comparator().

◆ or_connection_new()

or_connection_t* or_connection_new ( int  type,
int  socket_family 
)

Allocate and return a new or_connection_t, initialized as by connection_init().

Initialize active_circuit_pqueue.

Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.

Definition at line 576 of file connection.c.

◆ pick_oos_victims()

STATIC smartlist_t* pick_oos_victims ( int  n)

Pick n victim connections for the OOS handler and return them in a smartlist.

Definition at line 5307 of file connection.c.

◆ proxy_type_to_string()

static const char * proxy_type_to_string ( int  proxy_type)
static

Return string representation of proxy_type.

Definition at line 5797 of file connection.c.

◆ record_num_bytes_transferred_impl()

static void record_num_bytes_transferred_impl ( connection_t conn,
time_t  now,
size_t  num_read,
size_t  num_written 
)
static

Helper: adjusts our bandwidth history and informs the controller as appropriate, given that we have just read num_read bytes and written num_written bytes on conn.

Definition at line 3574 of file connection.c.

◆ reenable_blocked_connection_init()

static void reenable_blocked_connection_init ( const or_options_t options)
static

Initialize the mainloop event that we use to wake up connections that find themselves blocked on bandwidth.

Definition at line 3865 of file connection.c.

◆ reenable_blocked_connection_schedule()

static void reenable_blocked_connection_schedule ( void  )
static

Called when we have blocked a connection for being low on bandwidth: schedule an event to reenable such connections, if it is not already scheduled.

Definition at line 3884 of file connection.c.

Referenced by connection_read_bw_exhausted(), and connection_write_bw_exhausted().

◆ reenable_blocked_connections_cb()

static void reenable_blocked_connections_cb ( mainloop_event_t ev,
void *  arg 
)
static

Re-enable all connections that were previously blocked on read or write. This event is scheduled after enough time has elapsed to be sure that the buckets will refill when the connections have something to do.

Definition at line 3842 of file connection.c.

Referenced by reenable_blocked_connection_init().

◆ retry_all_listeners()

int retry_all_listeners ( smartlist_t new_conns,
int  close_all_noncontrol 
)

Launch listeners for each port you should have open. Only launch listeners who are not already open, and only close listeners we no longer want.

Add all new connections to new_conns.

If close_all_noncontrol is true, then we only open control listeners, and we close all other listeners.

Definition at line 3203 of file connection.c.

Referenced by retry_listeners_callback().

◆ retry_listener_ports()

static int retry_listener_ports ( smartlist_t old_conns,
const smartlist_t ports,
smartlist_t new_conns,
smartlist_t replacements,
int  control_listeners_only 
)
static

Given a list of listener connections in old_conns, and list of port_cfg_t entries in ports, open a new listener for every port in ports that does not already have a listener in old_conns.

Remove from old_conns every connection that has a corresponding entry in ports. Add to new_conns new every connection we launch. If we may need to perform socket rebind when creating new listener that replaces old one, create a listener_replacement_t struct for affected pair and add it to replacements.

If control_listeners_only is true, then we only open control listeners, and we do not remove any noncontrol listeners from old_conns.

Return 0 on success, -1 on failure.

Definition at line 3075 of file connection.c.

◆ set_constrained_socket_buffers()

static void set_constrained_socket_buffers ( tor_socket_t  sock,
int  size 
)
static

Some systems have limited system buffers for recv and xmit on sockets allocated in a virtual server or similar environment. For a Tor server this can produce the "Error creating network socket: No buffer space available" error once all available TCP buffer space is consumed. This method will attempt to constrain the buffers allocated for the socket to the desired size to stay below system TCP buffer limits.

Definition at line 5090 of file connection.c.

◆ socket_failed_from_resource_exhaustion()

static void socket_failed_from_resource_exhaustion ( void  )
static

A socket failed from resource exhaustion.

Among other actions, warn that an accept or a connect has failed because we're running out of TCP sockets we can use on current system. Rate-limit these warnings so that we don't spam the log.

Definition at line 1249 of file connection.c.

◆ TO_LISTENER_CONN()

listener_connection_t* TO_LISTENER_CONN ( connection_t c)

Cast a connection_t * to a listener_connection_t *.

Exit with an assertion failure if the input is not a listener_connection_t.

Definition at line 234 of file connection.c.

Referenced by CONST_TO_LISTENER_CONN(), dnsserv_close_listener(), and dnsserv_configure_listener().

◆ update_send_buffer_size()

static void update_send_buffer_size ( tor_socket_t  sock)
static

On Windows Vista and Windows 7, tune the send buffer size according to a hint from the OS.

This should help fix slow upload rates.

Definition at line 4281 of file connection.c.

Variable Documentation

◆ last_interface_ipv4

tor_addr_t* last_interface_ipv4 = NULL
static

The last addresses that our network interface seemed to have been binding to. We use this as one way to detect when our IP changes.

XXXX+ We should really use the entire list of interfaces here.

Definition at line 206 of file connection.c.

◆ last_recorded_accounting_at

time_t last_recorded_accounting_at = 0
static

When did we last tell the accounting subsystem about transmitted bandwidth?

Definition at line 3568 of file connection.c.

Referenced by record_num_bytes_transferred_impl().

◆ last_refilled_global_buckets_ts

uint32_t last_refilled_global_buckets_ts =0
static

Cached value of the last coarse-timestamp when we refilled the global buckets.

Definition at line 3800 of file connection.c.

Referenced by connection_bucket_refill_single().

◆ listen_limit

int listen_limit = INT_MAX
static

Max backlog to pass to listen. We start at

Definition at line 1429 of file connection.c.

◆ outgoing_addrs

smartlist_t* outgoing_addrs = NULL
static

A list of tor_addr_t for addresses we've used in outgoing connections. Used to detect IP address changes.

Definition at line 211 of file connection.c.

Referenced by client_check_address_changed(), and connection_free_all().

◆ reenable_blocked_connections_delay

struct timeval reenable_blocked_connections_delay
static

Delay after which to run reenable_blocked_connections_ev.

Definition at line 3831 of file connection.c.

Referenced by reenable_blocked_connection_init().

◆ reenable_blocked_connections_ev

mainloop_event_t* reenable_blocked_connections_ev = NULL
static

Event to re-enable all connections that were previously blocked on read or write.

Definition at line 3828 of file connection.c.

Referenced by reenable_blocked_connection_init().

◆ reenable_blocked_connections_is_scheduled

int reenable_blocked_connections_is_scheduled = 0
static

True iff reenable_blocked_connections_ev is currently scheduled.

Definition at line 3831 of file connection.c.

Referenced by reenable_blocked_connection_init(), and reenable_blocked_connection_schedule().

◆ write_buckets_last_empty_at

time_t write_buckets_last_empty_at = -100
static

When was either global write bucket last empty? If this was recent, then we're probably low on bandwidth, and we should be stingy with our bandwidth usage.

Definition at line 3378 of file connection.c.