tor  0.4.1.0-alpha-dev
Macros | Functions | Variables
connection.c File Reference
#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 "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/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/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/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/rendclient.h"
#include "feature/rend/rendcommon.h"
#include "feature/stats/rephist.h"
#include "lib/crypt_ops/crypto_util.h"
#include "lib/geoip/geoip.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 TOR_CHANNEL_INTERNAL_
 
#define CONNECTION_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 get_proxy_type (void)
 
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)
 
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)
 
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)
 
 MOCK_IMPL (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)
 
 MOCK_IMPL (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 warn_too_many_conns (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)
 
 MOCK_IMPL (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)
 
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 (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)
 
int global_write_bucket_low (connection_t *conn, size_t attempt, int priority)
 
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, size_t len)
 
 MOCK_IMPL (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)
 
 MOCK_IMPL (connection_t *, connection_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)
 
 MOCK_IMPL (connection_t *, connection_get_by_type_nonlinked,(int type))
 
connection_tconnection_get_by_type_state_rendquery (int type, int state, const char *rendquery)
 
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)
 
 MOCK_IMPL (STATIC smartlist_t *, pick_oos_victims,(int n))
 
 MOCK_IMPL (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)
 
 MOCK_IMPL (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_DIR_LISTENER
Definition: connection.h:33
#define CONN_TYPE_CONTROL_LISTENER
Definition: connection.h:38
#define CONN_TYPE_AP_NATD_LISTENER
Definition: connection.h:46
#define CONN_TYPE_AP_TRANS_LISTENER
Definition: connection.h:43
#define CONN_TYPE_OR_LISTENER
Definition: connection.h:21
#define CONN_TYPE_AP_HTTP_CONNECT_LISTENER
Definition: connection.h:55
#define CONN_TYPE_AP_DNS_LISTENER
Definition: connection.h:48
#define CONN_TYPE_AP_LISTENER
Definition: connection.h:28
#define CONN_TYPE_EXT_OR_LISTENER
Definition: connection.h:53

Definition at line 204 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 3058 of file connection.c.

Referenced by connection_counts_as_relayed_traffic().

◆ 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

Definition at line 4394 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 4428 of file connection.c.

Referenced by connection_get_another_active_or_conn(), and connection_get_by_global_id().

◆ 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 834 of file connection.c.

Referenced by connection_close_immediate(), and connection_finished_flushing().

◆ 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
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:77
#define CONN_TYPE_DIR
Definition: connection.h:35

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 4522 of file connection.c.

Referenced by connection_dir_list_by_purpose_and_resource(), and connection_dir_list_by_purpose_resource_and_state().

◆ 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 157 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 4674 of file connection.c.

References base64_encode_size().

◆ 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 4596 of file connection.c.

References connection_get_another_active_or_conn().

◆ 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 5240 of file connection.c.

References tor_assert(), and connection_t::type.

Referenced by add_connection_to_closeable_list(), assert_all_pending_dns_resolves_ok(), connection_close_immediate(), connection_mark_for_close_(), connection_or_finished_flushing(), inform_pending_connections(), and MOCK_IMPL().

◆ 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 1633 of file connection.c.

References LD_NET, log_fn, and tor_mem_is_zero().

◆ 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 1676 of file connection.c.

References connection_t::socket_family.

◆ 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 4695 of file connection.c.

References outgoing_addrs, and tor_addr_from_getsockname().

Referenced by connection_finished_connecting().

◆ 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 2120 of file connection.c.

References CONN_TYPE_EXIT, or_options_t::OutboundBindAddresses, and tor_addr_is_null().

◆ 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 552 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 274 of file connection.c.

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

◆ 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 242 of file connection.c.

Referenced by connection_expire_held_open(), and dumpstats().

◆ 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 811 of file connection.c.

References CONN_TYPE_AP, CONN_TYPE_DIR, CONN_TYPE_EXT_OR, CONN_TYPE_OR, connection_ap_about_to_close(), connection_dir_about_to_close(), connection_or_about_to_close(), connection_t::marked_for_close, TO_DIR_CONN(), TO_OR_CONN(), tor_assert(), and connection_t::type.

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 3430 of file connection.c.

References token_bucket_rw_adjust().

◆ 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 3082 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 3405 of file connection.c.

References monotime_coarse_get_stamp(), and token_bucket_rw_init().

◆ 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 3112 of file connection.c.

References CONN_TYPE_DIR, RELAY_PAYLOAD_SIZE, and connection_t::type.

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 3457 of file connection.c.

References last_refilled_global_buckets_ts, and token_bucket_rw_refill().

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 3142 of file connection.c.

References CONN_TYPE_DIR, connection_t::outbuf_flushlen, RELAY_PAYLOAD_SIZE, and connection_t::type.

◆ 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 3279 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 4379 of file connection.c.

References buf_move_all(), connection_may_write_to_buf(), connection_write_to_buf_commit(), connection_t::outbuf, and tor_assert().

◆ 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 3874 of file connection.c.

References buf_get_bytes(), and connection_t::inbuf.

◆ 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 3881 of file connection.c.

References buf_get_line(), and connection_t::inbuf.

◆ 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 3689 of file connection.c.

References approx_time(), buf_slack(), connection_bucket_read_limit(), and connection_t::inbuf.

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 5107 of file connection.c.

References tor_assert().

◆ 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 842 of file connection.c.

References assert_connection_ok(), and CONN_IS_CLOSED.

◆ 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 2177 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 3348 of file connection.c.

References connection_is_rate_limited().

◆ 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 3377 of file connection.c.

References connection_is_rate_limited().

◆ 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 3065 of file connection.c.

References CLIENT_IDLE_TIME_FOR_PRIORITY, CONN_TYPE_DIR, CONN_TYPE_OR, connection_or_client_used(), TO_OR_CONN(), and connection_t::type.

◆ 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 4355 of file connection.c.

References dir_connection_t::compress_state.

◆ 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 4547 of file connection.c.

References DIR_CONN_LIST_TEMPLATE, and strcmp_opt().

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 4565 of file connection.c.

References DIR_CONN_LIST_TEMPLATE, and strcmp_opt().

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 5191 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 953 of file connection.c.

References conn_state_to_string(), CONN_TYPE_DIR, CONN_TYPE_EXIT, conn_type_to_string(), DIR_PURPOSE_SERVER, connection_t::hold_open_until_flushed, LD_NET, log_fn, LOG_INFO, LOG_NOTICE, connection_t::marked_for_close, connection_t::purpose, connection_t::s, SMARTLIST_FOREACH_BEGIN, connection_t::state, connection_t::timestamp_last_write_allowed, tor_assert(), and connection_t::type.

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 3890 of file connection.c.

References connection_t::inbuf.

Referenced by directory_handle_command().

◆ 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 2571 of file connection.c.

References connection_t::inbuf.

◆ connection_finished_connecting()

static int connection_finished_connecting ( connection_t conn)
static

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

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

Definition at line 4877 of file connection.c.

References client_check_address_changed(), CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_dir_finished_connecting(), connection_edge_finished_connecting(), connection_or_finished_connecting(), connection_t::s, TO_DIR_CONN(), TO_EDGE_CONN(), TO_OR_CONN(), tor_assert(), and connection_t::type.

◆ 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 4839 of file connection.c.

References CONN_IS_CLOSED, CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_EXT_OR, CONN_TYPE_OR, connection_control_finished_flushing(), connection_dir_finished_flushing(), connection_edge_finished_flushing(), connection_ext_or_finished_flushing(), connection_or_finished_flushing(), TO_CONTROL_CONN(), TO_DIR_CONN(), TO_EDGE_CONN(), TO_OR_CONN(), tor_assert(), and connection_t::type.

◆ 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 4241 of file connection.c.

◆ connection_flushed_some()

static int connection_flushed_some ( connection_t conn)
static

◆ 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 5488 of file connection.c.

References clear_broken_connection_map(), CONN_TYPE_CONTROL, connection_free_minimal(), connection_or_clear_ext_or_id_map(), connection_or_clear_identity_map(), control_update_global_event_mask(), outgoing_addrs, SMARTLIST_FOREACH, TO_CONTROL_CONN(), and tor_free.

◆ 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 570 of file connection.c.

References CONN_TYPE_EXT_OR, CONN_TYPE_OR, connection_t::magic, TO_OR_CONN(), tor_assert(), and connection_t::type.

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 4584 of file connection.c.

References CONN_GET_TEMPLATE, CONN_TYPE_OR, and TO_CONN.

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 4458 of file connection.c.

References CONN_GET_TEMPLATE.

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 4466 of file connection.c.

◆ 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 4475 of file connection.c.

Referenced by directory_all_unreachable_cb().

◆ connection_get_by_type_state_rendquery()

connection_t* connection_get_by_type_state_rendquery ( int  type,
int  state,
const char *  rendquery 
)

Return a connection of type type that has rendquery equal to rendquery, and that is not marked for close. If state is non-zero, conn must be of that state too.

Definition at line 4495 of file connection.c.

◆ 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 1694 of file connection.c.

References tor_socket_t.

Referenced by connection_handle_read_impl().

◆ connection_handle_read_impl()

static int connection_handle_read_impl ( connection_t conn)
static

◆ 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 3973 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 490 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 1867 of file connection.c.

References CONN_TYPE_EXT_OR, CONN_TYPE_OR, connection_ext_or_start_auth(), connection_or_event_status(), TO_OR_CONN(), and connection_t::type.

◆ connection_is_listener()

int connection_is_listener ( connection_t conn)

◆ 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 5090 of file connection.c.

References connection_t::conn_array_index, and connection_t::marked_for_close.

Referenced by MOCK_IMPL().

◆ connection_is_rate_limited()

static int connection_is_rate_limited ( 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 3037 of file connection.c.

References connection_t::addr, or_options_t::CountPrivateBandwidth, connection_t::linked, and tor_addr_family().

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 538 of file connection.c.

References connection_t::linked, connection_t::linked_conn, connection_t::s, SOCKET_OK, and tor_assert().

◆ 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 1231 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 1568 of file connection.c.

References CFG_AUTO_PORT, port_cfg_t::is_unix_addr, port_cfg_t::port, tor_assert(), and or_options_t::User.

◆ 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 2996 of file connection.c.

References CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_CONTROL_LISTENER, connection_t::marked_for_close, SMARTLIST_FOREACH_BEGIN, and connection_t::type.

◆ 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 2982 of file connection.c.

References CONN_TYPE_CONTROL_LISTENER, connection_is_listener(), connection_t::marked_for_close, SMARTLIST_FOREACH_BEGIN, and connection_t::type.

◆ 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 876 of file connection.c.

References assert_connection_ok(), CONN_TYPE_OR, tor_assert(), and connection_t::type.

Referenced by MOCK_IMPL().

◆ 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 4252 of file connection.c.

References connection_t::hold_open_until_flushed, and connection_t::marked_for_close.

Referenced by connection_buf_add_buf(), and MOCK_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 449 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 3912 of file connection.c.

References CELL_PAYLOAD_SIZE, and connection_t::outbuf_flushlen.

Referenced by connection_edge_consider_sending_sendme().

◆ 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 4788 of file connection.c.

References CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_EXT_OR, CONN_TYPE_OR, connection_control_process_inbuf(), connection_dir_process_inbuf(), connection_edge_process_inbuf(), connection_ext_or_process_inbuf(), connection_or_process_inbuf(), TO_CONTROL_CONN(), TO_DIR_CONN(), TO_EDGE_CONN(), TO_OR_CONN(), tor_assert(), and connection_t::type.

◆ connection_proxy_connect()

int connection_proxy_connect ( connection_t conn,
int  type 
)

Write a proxy request of type (socks4, socks5, https) 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 2323 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 2265 of file connection.c.

◆ connection_reached_eof()

static int connection_reached_eof ( connection_t conn)
static

◆ 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 3322 of file connection.c.

References connection_t::read_blocked_on_bw, and reenable_blocked_connection_schedule().

◆ 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 2478 of file connection.c.

References connection_t::inbuf, and MAX_HEADERS_SIZE.

◆ 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 2588 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 2539 of file connection.c.

References connection_t::addr, connection_t::port, tor_addr_family(), tor_addr_to_in6_addr8, and tor_addr_to_ipv4n().

◆ connection_state_is_connecting()

int connection_state_is_connecting ( connection_t conn)

◆ connection_state_is_open()

int connection_state_is_open ( connection_t conn)

◆ connection_wants_to_flush()

int connection_wants_to_flush ( connection_t conn)

Return conn->outbuf_flushlen: how many bytes conn wants to flush from its outbuf.

Definition at line 3902 of file connection.c.

References connection_t::outbuf_flushlen.

◆ 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 3337 of file connection.c.

References reenable_blocked_connection_schedule(), and connection_t::write_blocked_on_bw.

◆ connection_write_to_buf_commit()

static void connection_write_to_buf_commit ( connection_t conn,
size_t  len 
)
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: record the number of bytes added.

Definition at line 4296 of file connection.c.

References connection_t::outbuf_flushlen, and connection_t::write_event.

Referenced by connection_buf_add_buf(), and MOCK_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 4267 of file connection.c.

Referenced by MOCK_IMPL().

◆ 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 425 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 359 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 414 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 395 of file connection.c.

◆ 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 5394 of file connection.c.

References transport_t::addr, connection_t::addr, or_options_t::ClientTransportPlugin, get_transport_by_bridge_addrport(), or_options_t::HTTPSProxy, or_options_t::HTTPSProxyAddr, or_options_t::HTTPSProxyPort, transport_t::port, connection_t::port, transport_t::socks_version, and tor_addr_copy().

Referenced by log_failed_proxy_connection().

◆ get_proxy_type()

static int get_proxy_type ( void  )
static

Returns the global proxy type used by tor. Use this function 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 2291 of file connection.c.

References or_options_t::ClientTransportPlugin.

◆ global_write_bucket_low()

int global_write_bucket_low ( connection_t conn,
size_t  attempt,
int  priority 
)

Return 1 if the global write buckets are low enough that we shouldn't send attempt bytes of low-priority directory stuff out to conn. Else return 0.

Priority was 1 for v1 requests (directories and running-routers), and 2 for v2 requests and later (statuses and descriptors).

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 3192 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 437 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 5448 of file connection.c.

References get_proxy_addrport().

◆ 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 1155 of file connection.c.

◆ MOCK_IMPL() [1/9]

MOCK_IMPL ( void  ,
connection_free_  ,
(connection_t *conn)   
)

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

Definition at line 758 of file connection.c.

References connection_in_array(), connection_is_on_closeable_list(), and tor_assert().

◆ MOCK_IMPL() [2/9]

MOCK_IMPL ( 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; the _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() instead, which checks for or_connection_t properly, instead. See below.

Definition at line 909 of file connection.c.

References assert_connection_ok(), connection_t::marked_for_close, and tor_assert().

◆ MOCK_IMPL() [3/9]

MOCK_IMPL ( 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 1931 of file connection.c.

◆ MOCK_IMPL() [4/9]

MOCK_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 4316 of file connection.c.

References buf_add(), buf_add_compress(), dir_connection_t::compress_state, CONN_LOG_PROTECT, connection_may_write_to_buf(), connection_write_to_buf_commit(), connection_write_to_buf_failed(), connection_t::outbuf, and TO_DIR_CONN().

◆ MOCK_IMPL() [5/9]

MOCK_IMPL ( 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 4442 of file connection.c.

◆ MOCK_IMPL() [6/9]

MOCK_IMPL ( 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 4484 of file connection.c.

◆ MOCK_IMPL() [7/9]

MOCK_IMPL ( 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 4975 of file connection.c.

◆ MOCK_IMPL() [8/9]

MOCK_IMPL ( STATIC  void,
kill_conn_list_for_oos  ,
(smartlist_t *conns)   
)

Kill a list of connections for the OOS handler.

Definition at line 5068 of file connection.c.

◆ MOCK_IMPL() [9/9]

MOCK_IMPL ( 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 5529 of file connection.c.

References connection_t::address, format_time_interval(), connection_t::port, and tor_asprintf().

◆ 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 4943 of file connection.c.

References CONN_TYPE_OR, oos_victim_comparator_for_orconns(), TO_OR_CONN(), tor_assert(), and connection_t::type.

◆ 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 4927 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 374 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 5468 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

◆ 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 3515 of file connection.c.

References mainloop_event_new(), reenable_blocked_connections_cb(), reenable_blocked_connections_delay, reenable_blocked_connections_ev, reenable_blocked_connections_is_scheduled, and or_options_t::TokenBucketRefillInterval.

◆ 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 3534 of file connection.c.

References reenable_blocked_connections_is_scheduled.

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 3492 of file connection.c.

References connection_t::read_blocked_on_bw, SMARTLIST_FOREACH_BEGIN, and connection_t::write_blocked_on_bw.

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 2888 of file connection.c.

References connection_is_listener(), connection_t::marked_for_close, router_get_advertised_dir_port(), router_get_advertised_or_port(), router_get_advertised_or_port_by_af(), smartlist_add(), and SMARTLIST_FOREACH_BEGIN.

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 2760 of file connection.c.

References connection_t::addr, connection_t::address, bool_neq, CFG_AUTO_PORT, CONN_TYPE_CONTROL_LISTENER, connection_t::port, smartlist_add(), smartlist_add_all(), SMARTLIST_FOREACH, SMARTLIST_FOREACH_BEGIN, connection_t::socket_family, tor_addr_eq, tor_addr_family(), tor_addr_is_null(), and connection_t::type.

◆ 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 4765 of file connection.c.

◆ TO_LISTENER_CONN()

listener_connection_t* TO_LISTENER_CONN ( connection_t c)

Convert a connection_t* to an listener_connection_t*; assert if the cast is invalid.

Definition at line 220 of file connection.c.

References DOWNCAST, connection_t::magic, and tor_assert().

Referenced by 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 3924 of file connection.c.

◆ warn_too_many_conns()

static void warn_too_many_conns ( void  )
static

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 1040 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 197 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 3228 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 3450 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 1201 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 202 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 3484 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 3478 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 3481 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 3054 of file connection.c.