#include "or.h"
Defines | |
#define | CONN_IS_CLOSED(c) ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0)) |
#define | WARN_TOO_MANY_CONNS_INTERVAL (6*60*60) |
#define | CLIENT_IDLE_TIME_FOR_PRIORITY 30 |
#define | MIN_TLS_FLUSHLEN 15872 |
Functions | |
static connection_t * | connection_create_listener (struct sockaddr *listensockaddr, socklen_t listensocklen, int type, char *address) |
static void | connection_init (time_t now, connection_t *conn, int type, int socket_family) |
static int | connection_init_accepted_conn (connection_t *conn, uint8_t listener_type) |
static int | connection_handle_listener_read (connection_t *conn, int new_type) |
static int | connection_bucket_should_increase (int bucket, or_connection_t *conn) |
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_read_to_buf (connection_t *conn, int *max_to_read, int *socket_error) |
static int | connection_process_inbuf (connection_t *conn, int package_partial) |
static void | client_check_address_changed (int sock) |
static void | set_constrained_socket_buffers (int 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) |
const char * | conn_type_to_string (int type) |
const char * | conn_state_to_string (int type, int state) |
dir_connection_t * | dir_connection_new (int socket_family) |
or_connection_t * | or_connection_new (int socket_family) |
edge_connection_t * | edge_connection_new (int type, int socket_family) |
control_connection_t * | control_connection_new (int socket_family) |
connection_t * | connection_new (int type, int socket_family) |
void | connection_link_connections (connection_t *conn_a, connection_t *conn_b) |
static void | _connection_free (connection_t *conn) |
void | connection_free (connection_t *conn) |
void | connection_free_all (void) |
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_expire_held_open (void) |
static struct sockaddr_in * | create_inet_sockaddr (const char *listenaddress, uint16_t listenport, char **readable_address, socklen_t *socklen_out) |
static struct sockaddr_un * | create_unix_sockaddr (const char *listenaddress, char **readable_address, socklen_t *len_out) |
static void | warn_too_many_conns (void) |
static int | check_sockaddr (struct sockaddr *sa, int len, int level) |
static int | check_sockaddr_family_match (sa_family_t got, connection_t *listener) |
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) |
int | connection_read_proxy_handshake (connection_t *conn) |
static int | retry_listeners (int type, config_line_t *cfg, int port_option, const char *default_addr, smartlist_t *replaced_conns, smartlist_t *new_conns, int disable_all_conns, int socket_family) |
int | retry_all_listeners (smartlist_t *replaced_conns, smartlist_t *new_conns) |
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_round_robin (int base, int priority, ssize_t global_bucket, 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 | connection_buckets_decrement (connection_t *conn, time_t now, size_t num_read, size_t num_written) |
static void | connection_consider_empty_read_buckets (connection_t *conn) |
static void | connection_consider_empty_write_buckets (connection_t *conn) |
void | connection_bucket_init (void) |
static void | connection_bucket_refill_helper (int *bucket, int rate, int burst, int seconds_elapsed, const char *name) |
void | connection_bucket_refill (int seconds_elapsed, time_t now) |
static int | connection_handle_read_impl (connection_t *conn) |
int | connection_handle_read (connection_t *conn) |
int | connection_fetch_from_buf (char *string, size_t len, connection_t *conn) |
int | connection_wants_to_flush (connection_t *conn) |
int | connection_outbuf_too_full (connection_t *conn) |
static int | connection_handle_write_impl (connection_t *conn, int force) |
int | connection_handle_write (connection_t *conn, int force) |
void | _connection_write_to_buf_impl (const char *string, size_t len, connection_t *conn, int zlib) |
connection_t * | connection_get_by_type_addr_port_purpose (int type, const tor_addr_t *addr, uint16_t port, int purpose) |
connection_t * | connection_get_by_global_id (uint64_t id) |
connection_t * | connection_get_by_type (int type) |
connection_t * | connection_get_by_type_state (int type, int state) |
connection_t * | connection_get_by_type_state_rendquery (int type, int state, const char *rendquery) |
connection_t * | connection_get_by_type_purpose (int type, int purpose) |
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) |
void | connection_dump_buffer_mem_stats (int severity) |
void | assert_connection_ok (connection_t *conn, time_t now) |
Variables | |
static uint32_t | last_interface_ip = 0 |
static smartlist_t * | outgoing_addrs = NULL |
int | global_read_bucket |
int | global_write_bucket |
int | global_relayed_read_bucket |
int | global_relayed_write_bucket |
static int | write_buckets_empty_last_second = 0 |
#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?
Referenced by connection_counts_as_relayed_traffic().
#define CONN_IS_CLOSED | ( | c | ) | ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0)) |
Return true iff connection_close_immediate() has been called on this connection.
Referenced by connection_close_immediate(), and connection_finished_flushing().
#define MIN_TLS_FLUSHLEN 15872 |
OpenSSL TLS record size is 16383; this is close. The goal here is to push data out as soon as we know there's enough for a TLS record, so during periods of high load we won't read entire megabytes from input before pushing any data out. It also has the feature of not growing huge outbufs unless something is slow.
Referenced by _connection_write_to_buf_impl().
static void _connection_free | ( | connection_t * | conn | ) | [static] |
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.
References or_connection_t::active_circuit_pqueue, connection_t::address, buf_datalen(), buf_free(), dir_connection_t::cached_dir, cached_dir_decref(), edge_connection_t::chosen_exit_name, CONN_IS_EDGE, conn_state_to_string(), CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_CONTROL_LISTENER, CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, conn_type_to_string(), connection_is_listener(), connection_or_remove_from_identity_map(), dir_connection_t::fingerprint_stack, or_connection_t::handshake_state, connection_t::inbuf, control_connection_t::incoming_cmd, LD_BUG, LD_GENERAL, LD_NET, connection_t::linked, connection_t::magic, or_connection_t::nickname, or_handshake_state_free(), connection_t::outbuf, connection_t::read_event, dir_connection_t::rend_data, edge_connection_t::rend_data, rend_data_free(), dir_connection_t::requested_resource, connection_t::s, smartlist_free(), connection_t::socket_family, edge_connection_t::socks_request, connection_t::state, or_connection_t::tls, TO_CONTROL_CONN(), TO_DIR_CONN(), TO_EDGE_CONN(), TO_OR_CONN(), tor_assert, tor_close_socket(), tor_digest_is_zero(), tor_free, tor_tls_free(), tor_zlib_free(), connection_t::type, connection_t::write_event, and dir_connection_t::zlib_state.
Referenced by connection_free(), and connection_free_all().
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.
References add_connection_to_closeable_list(), assert_connection_ok(), LD_BUG, connection_t::marked_for_close, connection_t::marked_for_close_file, connection_t::timestamp_lastwritten, tor_assert, and tor_fragile_assert.
Referenced by _connection_mark_unattached_ap().
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.
If it's an OR conn and an entire TLS record is ready, then try to flush the record now. Similarly, if it's a local control connection and a 64k chunk is ready, try to flush it all, so we don't end up with many megabytes of controller info queued at once.
References buf_datalen(), circuit_get_by_edge_conn(), CONN_IS_EDGE, CONN_LOG_PROTECT, CONN_TYPE_CONTROL, CONN_TYPE_OR, connection_close_immediate(), connection_is_rate_limited(), connection_start_writing(), connection_t::hold_open_until_flushed, connection_t::in_flushed_some, LD_BUG, LD_NET, connection_t::marked_for_close, MIN_TLS_FLUSHLEN, connection_t::outbuf, connection_t::outbuf_flushlen, connection_t::s, TO_DIR_CONN(), TO_EDGE_CONN(), tor_fragile_assert, connection_t::type, write_to_buf(), write_to_buf_zlib(), and dir_connection_t::zlib_state.
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.
References base64_encode(), and tor_free.
Referenced by connection_proxy_connect(), directory_handle_command_get(), and directory_send_command().
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.
References connection_t::address, AP_CONN_STATE_OPEN, assert_buf_ok(), assert_cpath_layer_ok(), edge_connection_t::chosen_exit_name, edge_connection_t::chosen_exit_optional, edge_connection_t::chosen_exit_retries, CONN_IS_EDGE, CONN_TYPE_AP, CONN_TYPE_AP_DNS_LISTENER, CONN_TYPE_AP_LISTENER, CONN_TYPE_AP_NATD_LISTENER, CONN_TYPE_AP_TRANS_LISTENER, CONN_TYPE_CONTROL, CONN_TYPE_CONTROL_LISTENER, CONN_TYPE_CPUWORKER, CONN_TYPE_DIR, CONN_TYPE_DIR_LISTENER, CONN_TYPE_EXIT, CONN_TYPE_OR, CONN_TYPE_OR_LISTENER, connection_is_listener(), connection_is_writing(), edge_connection_t::cpath_layer, EXIT_PURPOSE_CONNECT, EXIT_PURPOSE_RESOLVE, socks_request_t::has_finished, connection_t::hold_open_until_flushed, connection_t::inbuf, connection_t::linked, connection_t::linked_conn, LISTENER_STATE_READY, connection_t::magic, connection_t::marked_for_close, OR_CONN_STATE_OPEN, OR_CONN_STATE_PROXY_HANDSHAKING, connection_t::outbuf, connection_t::outbuf_flushlen, connection_t::purpose, connection_t::s, edge_connection_t::socks_request, connection_t::state, or_connection_t::tls, TO_EDGE_CONN(), TO_OR_CONN(), tor_assert, connection_t::type, and connection_t::write_blocked_on_bw.
Referenced by _connection_mark_for_close(), add_connection_to_closeable_list(), assert_all_pending_dns_resolves_ok(), command_process_netinfo_cell(), conn_close_if_marked(), conn_read_callback(), conn_write_callback(), connection_close_immediate(), connection_dns_remove(), connection_or_finished_flushing(), dns_cancel_pending_resolve(), dns_found_answer(), dns_resolve_impl(), and rend_client_desc_trynow().
static int check_sockaddr | ( | 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.
References LD_NET, and tor_mem_is_zero().
Referenced by connection_handle_listener_read().
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.
References conn_type_to_string(), LD_BUG, connection_t::socket_family, and connection_t::type.
Referenced by connection_handle_listener_read().
static void client_check_address_changed | ( | int | 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().
References get_interface_address(), ip_address_changed(), last_interface_ip, LD_NET, smartlist_add(), smartlist_clear(), smartlist_create(), and tor_free.
Referenced by connection_connect().
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
References AP_CONN_STATE_CIRCUIT_WAIT, AP_CONN_STATE_CONNECT_WAIT, AP_CONN_STATE_CONTROLLER_WAIT, AP_CONN_STATE_NATD_WAIT, AP_CONN_STATE_OPEN, AP_CONN_STATE_RENDDESC_WAIT, AP_CONN_STATE_RESOLVE_WAIT, AP_CONN_STATE_SOCKS_WAIT, CONN_TYPE_AP, CONN_TYPE_AP_DNS_LISTENER, CONN_TYPE_AP_LISTENER, CONN_TYPE_AP_NATD_LISTENER, CONN_TYPE_AP_TRANS_LISTENER, CONN_TYPE_CONTROL, CONN_TYPE_CONTROL_LISTENER, CONN_TYPE_CPUWORKER, CONN_TYPE_DIR, CONN_TYPE_DIR_LISTENER, CONN_TYPE_EXIT, CONN_TYPE_OR, CONN_TYPE_OR_LISTENER, conn_type_to_string(), CONTROL_CONN_STATE_NEEDAUTH, CONTROL_CONN_STATE_OPEN, CPUWORKER_STATE_BUSY_ONION, CPUWORKER_STATE_IDLE, DIR_CONN_STATE_CLIENT_FINISHED, DIR_CONN_STATE_CLIENT_READING, DIR_CONN_STATE_CLIENT_SENDING, DIR_CONN_STATE_CONNECTING, DIR_CONN_STATE_SERVER_COMMAND_WAIT, DIR_CONN_STATE_SERVER_WRITING, EXIT_CONN_STATE_CONNECTING, EXIT_CONN_STATE_OPEN, EXIT_CONN_STATE_RESOLVEFAILED, EXIT_CONN_STATE_RESOLVING, LD_BUG, LISTENER_STATE_READY, OR_CONN_STATE_CONNECTING, OR_CONN_STATE_OPEN, OR_CONN_STATE_OR_HANDSHAKING, OR_CONN_STATE_PROXY_HANDSHAKING, OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING, OR_CONN_STATE_TLS_HANDSHAKING, OR_CONN_STATE_TLS_SERVER_RENEGOTIATING, and tor_snprintf().
Referenced by _connection_free(), connection_ap_expire_beginning(), connection_buckets_decrement(), connection_close_immediate(), connection_edge_process_inbuf(), connection_edge_process_relay_cell_not_open(), connection_expire_held_open(), and dumpstats().
const char* conn_type_to_string | ( | int | type | ) |
Return the human-readable name for the connection type type
References CONN_TYPE_AP, CONN_TYPE_AP_DNS_LISTENER, CONN_TYPE_AP_LISTENER, CONN_TYPE_AP_NATD_LISTENER, CONN_TYPE_AP_TRANS_LISTENER, CONN_TYPE_CONTROL, CONN_TYPE_CONTROL_LISTENER, CONN_TYPE_CPUWORKER, CONN_TYPE_DIR, CONN_TYPE_DIR_LISTENER, CONN_TYPE_EXIT, CONN_TYPE_OR, CONN_TYPE_OR_LISTENER, LD_BUG, and tor_snprintf().
Referenced by _connection_free(), check_sockaddr_family_match(), conn_close_if_marked(), conn_read_callback(), conn_state_to_string(), conn_write_callback(), connection_add(), connection_buckets_decrement(), connection_close_immediate(), connection_create_listener(), connection_expire_held_open(), connection_remove(), dumpstats(), and options_act_reversible().
void connection_about_to_close_connection | ( | connection_t * | conn | ) |
Do any cleanup needed:
References authdir_mode_tests_reachability(), circuit_detach_stream(), circuit_get_by_edge_conn(), circuit_n_conn_done(), circuit_unlink_all_from_or_conn(), CONN_IS_EDGE, CONN_TYPE_AP, CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_dir_request_failed(), connection_dns_remove(), connection_or_nonopen_was_started_here(), control_event_bootstrap_problem(), control_event_or_conn_status(), control_event_stream_bandwidth(), control_event_stream_status(), DIR_CONN_STATE_CLIENT_FINISHED, DIR_PURPOSE_FETCH_RENDDESC_V2, edge_connection_t::dns_server_request, dnsserv_reject_request(), edge_connection_t::edge_has_sent_end, edge_connection_t::end_reason, entry_guard_register_connect_status(), EXIT_CONN_STATE_RESOLVING, get_options(), socks_request_t::has_finished, connection_t::hold_open_until_flushed, or_options_t::HttpsProxy, or_connection_t::identity_digest, LD_BUG, connection_t::marked_for_close, connection_t::marked_for_close_file, rend_data_t::onion_address, OR_CONN_STATE_OPEN, OR_CONN_STATE_TLS_HANDSHAKING, orconn_end_reason_to_control_string(), connection_t::purpose, rend_client_refetch_v2_renddesc(), dir_connection_t::rend_data, REND_SERVICE_ID_LEN_BASE32, rep_hist_note_connect_failed(), rep_hist_note_connection_died(), rep_hist_note_disconnect(), edge_connection_t::socks_request, connection_t::state, or_connection_t::tls_error, tls_error_to_orconn_end_reason(), TO_DIR_CONN(), TO_EDGE_CONN(), TO_OR_CONN(), tor_assert, tor_digest_is_zero(), tor_fragile_assert, and connection_t::type.
Referenced by connection_unlink().
void connection_bucket_init | ( | void | ) |
Initialize the global read bucket to options->BandwidthBurst.
References or_options_t::BandwidthBurst, get_options(), or_options_t::RelayBandwidthBurst, and or_options_t::RelayBandwidthRate.
Referenced by do_main_loop().
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?
References CELL_NETWORK_SIZE, CONN_TYPE_DIR, connection_bucket_round_robin(), connection_counts_as_relayed_traffic(), connection_is_rate_limited(), OR_CONN_STATE_OPEN, or_connection_t::read_bucket, RELAY_PAYLOAD_SIZE, connection_t::state, TO_OR_CONN(), and connection_t::type.
Referenced by connection_read_to_buf().
void connection_bucket_refill | ( | int | seconds_elapsed, | |
time_t | now | |||
) |
A second has rolled over; increment buckets appropriately.
References or_connection_t::bandwidthburst, or_options_t::BandwidthBurst, or_connection_t::bandwidthrate, or_options_t::BandwidthRate, connection_bucket_refill_helper(), connection_bucket_should_increase(), connection_counts_as_relayed_traffic(), connection_start_reading(), connection_start_writing(), get_connection_array(), get_options(), LD_NET, LOG_FN_CONN, OR_CONN_STATE_OPEN, or_connection_t::read_bucket, or_options_t::RelayBandwidthBurst, or_options_t::RelayBandwidthRate, TO_OR_CONN(), tor_assert, and or_connection_t::write_bucket.
Referenced by second_elapsed_callback().
static void connection_bucket_refill_helper | ( | int * | bucket, | |
int | rate, | |||
int | burst, | |||
int | seconds_elapsed, | |||
const char * | name | |||
) | [static] |
Refill a single bucket called name with bandwidth rate rate and bandwidth burst burst, assuming that seconds_elapsed seconds have passed since the last call.
References LD_NET.
Referenced by connection_bucket_refill().
static ssize_t connection_bucket_round_robin | ( | int | base, | |
int | priority, | |||
ssize_t | global_bucket, | |||
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.
Referenced by connection_bucket_read_limit(), and connection_bucket_write_limit().
static int connection_bucket_should_increase | ( | int | bucket, | |
or_connection_t * | conn | |||
) | [static] |
Is the bucket for connection conn low enough that we should add another pile of tokens to it?
References or_connection_t::_base, or_connection_t::bandwidthburst, OR_CONN_STATE_OPEN, connection_t::state, and tor_assert.
Referenced by connection_bucket_refill().
ssize_t connection_bucket_write_limit | ( | connection_t * | conn, | |
time_t | now | |||
) |
How many bytes at most can we write onto this connection?
References CELL_NETWORK_SIZE, CONN_TYPE_DIR, connection_bucket_round_robin(), connection_counts_as_relayed_traffic(), connection_is_rate_limited(), OR_CONN_STATE_OPEN, connection_t::outbuf_flushlen, RELAY_PAYLOAD_SIZE, connection_t::state, TO_OR_CONN(), connection_t::type, and or_connection_t::write_bucket.
Referenced by conn_close_if_marked(), and connection_handle_write_impl().
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.
References conn_state_to_string(), CONN_TYPE_EXIT, conn_type_to_string(), connection_counts_as_relayed_traffic(), connection_is_rate_limited(), LD_BUG, OR_CONN_STATE_OPEN, connection_t::port, or_connection_t::read_bucket, rep_hist_note_bytes_read(), rep_hist_note_bytes_written(), rep_hist_note_exit_bytes_read(), rep_hist_note_exit_bytes_written(), connection_t::state, TO_OR_CONN(), tor_fragile_assert, connection_t::type, and or_connection_t::write_bucket.
Referenced by connection_handle_read_impl(), connection_handle_write_impl(), and connection_read_to_buf().
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().
References assert_connection_ok(), buf_clear(), CONN_IS_CLOSED, conn_state_to_string(), conn_type_to_string(), connection_is_listener(), connection_unregister_events(), LD_BUG, LD_NET, connection_t::linked, connection_t::linked_conn_is_closed, connection_t::outbuf, connection_t::outbuf_flushlen, connection_t::s, connection_t::state, tor_close_socket(), tor_fragile_assert, and connection_t::type.
Referenced by _connection_write_to_buf_impl(), conn_write_callback(), connection_dir_reached_eof(), connection_exit_connect_dir(), connection_handle_read_impl(), connection_handle_write_impl(), and options_act_reversible().
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 (they arrive 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.
address is used to make the logs useful.
On success, add conn to the list of polled connections.
References client_check_address_changed(), connection_add(), or_options_t::ConstrainedSockets, or_options_t::ConstrainedSockSize, escaped_safe_str_client(), get_n_open_sockets(), get_options(), LD_CONFIG, LD_NET, or_options_t::OutboundBindAddress, connection_t::s, server_mode(), set_constrained_socket_buffers(), set_socket_nonblocking(), tor_addr_family(), tor_addr_is_loopback(), tor_addr_to_sockaddr(), tor_assert, tor_close_socket(), tor_inet_aton(), tor_open_socket(), and warn_too_many_conns().
Referenced by connection_exit_connect(), connection_or_connect(), and directory_initiate_command_rend().
static void connection_consider_empty_read_buckets | ( | connection_t * | conn | ) | [static] |
If we have exhausted our global buckets, or the buckets for conn, stop reading.
References approx_time(), connection_counts_as_relayed_traffic(), connection_stop_reading(), LD_NET, LOG_FN_CONN, OR_CONN_STATE_OPEN, connection_t::read_blocked_on_bw, connection_t::state, and TO_OR_CONN().
Referenced by connection_handle_write_impl(), and connection_read_to_buf().
static void connection_consider_empty_write_buckets | ( | connection_t * | conn | ) | [static] |
If we have exhausted our global buckets, or the buckets for conn, stop writing.
References approx_time(), connection_counts_as_relayed_traffic(), connection_stop_writing(), LD_NET, LOG_FN_CONN, OR_CONN_STATE_OPEN, connection_t::state, TO_OR_CONN(), and connection_t::write_blocked_on_bw.
Referenced by connection_handle_write_impl(), and connection_read_to_buf().
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.
References CLIENT_IDLE_TIME_FOR_PRIORITY, CONN_TYPE_DIR, CONN_TYPE_OR, DIR_CONN_IS_SERVER, TO_OR_CONN(), and connection_t::type.
Referenced by connection_bucket_read_limit(), connection_bucket_refill(), connection_bucket_write_limit(), connection_buckets_decrement(), connection_consider_empty_read_buckets(), and connection_consider_empty_write_buckets().
static connection_t * connection_create_listener | ( | struct sockaddr * | listensockaddr, | |
socklen_t | socklen, | |||
int | type, | |||
char * | address | |||
) | [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.
References connection_t::address, CONN_TYPE_AP_DNS_LISTENER, CONN_TYPE_CONTROL_LISTENER, conn_type_to_string(), connection_add(), connection_free(), connection_new(), connection_start_reading(), dnsserv_configure_listener(), get_n_open_sockets(), get_options(), LD_BUG, LD_NET, LISTENER_STATE_READY, connection_t::port, connection_t::s, set_socket_nonblocking(), connection_t::socket_family, connection_t::state, tor_assert, tor_close_socket(), tor_open_socket(), and warn_too_many_conns().
void connection_dump_buffer_mem_stats | ( | int | severity | ) |
Log how many bytes are used by buffers of different kinds and sizes.
References buf_allocation(), buf_datalen(), and get_connection_array().
Referenced by dumpmemusage().
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.
References conn_state_to_string(), CONN_TYPE_DIR, CONN_TYPE_EXIT, conn_type_to_string(), DIR_PURPOSE_SERVER, get_connection_array(), LD_NET, and tor_assert.
Referenced by run_scheduled_events().
int connection_fetch_from_buf | ( | char * | string, | |
size_t | len, | |||
connection_t * | conn | |||
) |
A pass-through to fetch_from_buf.
References fetch_from_buf(), and connection_t::inbuf.
Referenced by connection_cpu_process_inbuf(), connection_edge_package_raw_inbuf(), and connection_or_process_cells_from_inbuf().
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.
References CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_dir_finished_connecting(), connection_edge_finished_connecting(), connection_or_finished_connecting(), LD_BUG, TO_DIR_CONN(), TO_EDGE_CONN(), TO_OR_CONN(), tor_assert, tor_fragile_assert, and connection_t::type.
Referenced by connection_handle_write_impl().
static int connection_finished_flushing | ( | connection_t * | conn | ) | [static] |
We just finished flushing bytes from conn->outbuf, and there are no more bytes remaining.
This function just passes conn to the connection-specific connection_*_finished_flushing() function.
References CONN_IS_CLOSED, CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_CPUWORKER, CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_control_finished_flushing(), connection_cpu_finished_flushing(), connection_dir_finished_flushing(), connection_edge_finished_flushing(), connection_or_finished_flushing(), LD_BUG, TO_CONTROL_CONN(), TO_DIR_CONN(), TO_EDGE_CONN(), TO_OR_CONN(), tor_assert, tor_fragile_assert, and connection_t::type.
Referenced by connection_handle_read_impl(), and connection_handle_write_impl().
static int connection_flushed_some | ( | connection_t * | conn | ) | [static] |
Called whenever we've written data on a connection.
References CONN_TYPE_DIR, CONN_TYPE_OR, connection_dirserv_flushed_some(), connection_or_flushed_some(), DIR_CONN_STATE_SERVER_WRITING, connection_t::in_flushed_some, connection_t::state, TO_DIR_CONN(), TO_OR_CONN(), tor_assert, and connection_t::type.
Referenced by connection_handle_read_impl(), and connection_handle_write_impl().
void connection_free | ( | connection_t * | conn | ) |
Make sure conn isn't in any of the global conn lists; then free it.
References _connection_free(), CONN_TYPE_CONTROL, connection_in_array(), connection_is_on_closeable_list(), connection_or_remove_from_identity_map(), connection_start_reading(), connection_unregister_events(), control_update_global_event_mask(), control_connection_t::event_mask, LD_BUG, connection_t::linked_conn, connection_t::marked_for_close, connection_t::reading_from_linked_conn, TO_CONTROL_CONN(), TO_OR_CONN(), tor_assert, tor_digest_is_zero(), tor_fragile_assert, and connection_t::type.
Referenced by circuit_free_all(), connection_ap_make_link(), connection_create_listener(), connection_exit_begin_conn(), connection_exit_begin_resolve(), connection_exit_connect(), connection_exit_connect_dir(), connection_handle_listener_read(), connection_or_connect(), connection_unlink(), directory_initiate_command_rend(), dns_cancel_pending_resolve(), dns_found_answer(), dns_resolve(), dnsserv_launch_request(), evdns_server_callback(), purge_expired_resolves(), and spawn_cpuworker().
void connection_free_all | ( | void | ) |
Call _connection_free() on every connection in our array, and release all storage held by connection.c. This is used by cpuworkers and dnsworkers when they fork, so they don't keep resources held open (especially sockets).
Don't do the checks in connection_free(), because they will fail.
References _connection_free(), CONN_TYPE_CONTROL, connection_or_clear_identity_map(), control_update_global_event_mask(), get_connection_array(), smartlist_free(), TO_CONTROL_CONN(), and tor_free.
Referenced by tor_free_all().
connection_t* connection_get_by_global_id | ( | uint64_t | id | ) |
Return the stream with id id if it is not already marked for close.
References get_connection_array().
Referenced by connection_cpu_process_inbuf(), and get_stream().
connection_t* connection_get_by_type | ( | int | type | ) |
Return a connection of type type that is not marked for close.
References get_connection_array().
Referenced by circuit_get_open_circ_or_launch(), hibernate_begin(), and hibernate_go_dormant().
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.
References get_connection_array(), and tor_addr_eq.
Referenced by consider_testing_reachability(), launch_direct_bridge_descriptor_fetch(), router_pick_trusteddirserver_impl(), and update_v2_networkstatus_cache_downloads().
connection_t* connection_get_by_type_purpose | ( | int | type, | |
int | purpose | |||
) |
Return an open, non-marked connection of a given type and purpose, or NULL if no such connection exists.
References get_connection_array().
Referenced by update_consensus_networkstatus_downloads(), and update_v2_networkstatus_cache_downloads().
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.
References get_connection_array().
Referenced by assign_onionskin_to_cpuworker(), cpuworkers_rotate(), and directory_all_unreachable().
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.
References CONN_IS_EDGE, CONN_TYPE_AP, CONN_TYPE_DIR, CONN_TYPE_EXIT, get_connection_array(), rend_cmp_service_ids(), TO_DIR_CONN(), TO_EDGE_CONN(), and tor_assert.
Referenced by rend_client_remove_intro_point().
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.
References connection_t::addr, connection_t::address, check_sockaddr(), check_sockaddr_family_match(), CONN_TYPE_AP, CONN_TYPE_CONTROL_LISTENER, CONN_TYPE_DIR, connection_add(), connection_free(), connection_init_accepted_conn(), connection_new(), or_options_t::ConstrainedSockets, or_options_t::ConstrainedSockSize, dir_policy_permits_address(), fmt_addr(), get_options(), LD_APP, LD_DIRSERV, LD_NET, connection_t::port, connection_t::s, set_constrained_socket_buffers(), set_socket_nonblocking(), connection_t::socket_family, socks_policy_permits_address(), tor_accept_socket(), tor_addr_copy(), tor_addr_from_sockaddr(), tor_addr_make_unspec(), tor_assert, tor_close_socket(), tor_dup_addr(), connection_t::type, and warn_too_many_conns().
Referenced by connection_handle_read_impl().
static int connection_handle_read_impl | ( | connection_t * | conn | ) | [static] |
Read bytes from conn->s and process them.
This function gets called from conn_read() in main.c, either when poll() has declared that conn wants to read, or (for OR conns) when there are pending TLS bytes.
It calls connection_read_to_buf() 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.
References connection_t::active_on_link, approx_time(), buf_datalen(), CONN_IS_EDGE, CONN_TYPE_AP, CONN_TYPE_AP_DNS_LISTENER, CONN_TYPE_AP_LISTENER, CONN_TYPE_AP_NATD_LISTENER, CONN_TYPE_AP_TRANS_LISTENER, CONN_TYPE_CONTROL, CONN_TYPE_CONTROL_LISTENER, CONN_TYPE_DIR, CONN_TYPE_DIR_LISTENER, CONN_TYPE_OR, CONN_TYPE_OR_LISTENER, connection_buckets_decrement(), connection_close_immediate(), connection_edge_end_errno(), connection_finished_flushing(), connection_flushed_some(), connection_handle_listener_read(), connection_is_reading(), connection_or_connect_failed(), connection_process_inbuf(), connection_reached_eof(), connection_read_to_buf(), connection_stop_reading_from_linked_conn(), connection_wants_to_flush(), errno_to_orconn_end_reason(), socks_request_t::has_finished, connection_t::inbuf, connection_t::inbuf_reached_eof, connection_t::linked_conn, connection_t::marked_for_close, OR_CONN_STATE_CONNECTING, connection_t::outbuf, edge_connection_t::socks_request, connection_t::state, connection_t::timestamp_lastread, TO_EDGE_CONN(), TO_OR_CONN(), tor_assert, tor_fragile_assert, and connection_t::type.
static int connection_handle_write_impl | ( | connection_t * | conn, | |
int | force | |||
) | [static] |
Try to flush more bytes onto conn->s.
This function gets called either from conn_write() in main.c when poll() has declared that conn wants to write, or below from connection_write_to_buf() when an entire TLS record is ready.
Update conn->timestamp_lastwritten 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.
References approx_time(), buf_datalen(), CASE_TOR_TLS_ERROR_ANY, CONN_IS_EDGE, CONN_LOG_PROTECT, CONN_TYPE_AP, CONN_TYPE_OR, connection_bucket_write_limit(), connection_buckets_decrement(), connection_close_immediate(), connection_consider_empty_read_buckets(), connection_consider_empty_write_buckets(), connection_edge_end_errno(), connection_finished_connecting(), connection_finished_flushing(), connection_flushed_some(), connection_is_listener(), connection_is_reading(), connection_or_connect_failed(), connection_state_is_connecting(), connection_stop_writing(), connection_tls_continue_handshake(), connection_wants_to_flush(), connection_t::dirreq_id, DIRREQ_OR_CONN_BUFFER_FLUSHED, errno_to_orconn_end_reason(), flush_buf(), flush_buf_tls(), geoip_change_dirreq_state(), connection_t::in_flushed_some, LD_BUG, LD_GENERAL, LD_NET, connection_t::marked_for_close, edge_connection_t::n_written, OR_CONN_STATE_PROXY_HANDSHAKING, OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING, OR_CONN_STATE_TLS_HANDSHAKING, OR_CONN_STATE_TLS_SERVER_RENEGOTIATING, connection_t::outbuf, connection_t::outbuf_flushlen, connection_t::s, connection_t::state, connection_t::timestamp_lastwritten, or_connection_t::tls, TO_EDGE_CONN(), TO_OR_CONN(), tor_assert, tor_tls_get_n_raw_bytes(), connection_t::type, and connection_t::write_blocked_on_bw.
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->type to type. Set conn->s and conn->conn_array_index to -1 to signify they are not yet assigned.
If conn is not a listener type, allocate buffers for it. If it's an AP type, allocate space to store the socks_request.
Assign a pseudorandom next_circ_id between 0 and 2**15.
Initialize conn's timestamps to now.
References buf_new(), connection_t::conn_array_index, CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_is_listener(), connection_t::global_identifier, connection_t::inbuf, connection_t::magic, connection_t::outbuf, connection_t::s, connection_t::socket_family, connection_t::timestamp_created, connection_t::timestamp_lastread, connection_t::timestamp_lastwritten, and connection_t::type.
Referenced by connection_new(), control_connection_new(), dir_connection_new(), edge_connection_new(), and or_connection_new().
static int connection_init_accepted_conn | ( | connection_t * | conn, | |
uint8_t | listener_type | |||
) | [static] |
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.
References AP_CONN_STATE_CIRCUIT_WAIT, AP_CONN_STATE_NATD_WAIT, AP_CONN_STATE_SOCKS_WAIT, CONN_TYPE_AP, CONN_TYPE_AP_LISTENER, CONN_TYPE_AP_NATD_LISTENER, CONN_TYPE_AP_TRANS_LISTENER, CONN_TYPE_CONTROL, CONN_TYPE_DIR, CONN_TYPE_OR, connection_ap_process_transparent(), connection_start_reading(), connection_tls_start_handshake(), CONTROL_CONN_STATE_NEEDAUTH, control_event_or_conn_status(), DIR_CONN_STATE_SERVER_COMMAND_WAIT, DIR_PURPOSE_SERVER, connection_t::purpose, connection_t::state, TO_EDGE_CONN(), TO_OR_CONN(), and connection_t::type.
Referenced by connection_handle_listener_read().
int connection_is_listener | ( | connection_t * | conn | ) |
Return 1 if conn is a listener conn, else return 0.
References CONN_TYPE_AP_DNS_LISTENER, CONN_TYPE_AP_LISTENER, CONN_TYPE_AP_NATD_LISTENER, CONN_TYPE_AP_TRANS_LISTENER, CONN_TYPE_CONTROL_LISTENER, CONN_TYPE_DIR_LISTENER, CONN_TYPE_OR_LISTENER, and connection_t::type.
Referenced by _connection_free(), assert_connection_ok(), connection_close_immediate(), connection_handle_write_impl(), connection_init(), and dumpstats().
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.
References connection_t::addr, connection_t::linked, tor_addr_family(), and tor_addr_is_internal().
Referenced by _connection_write_to_buf_impl(), connection_bucket_read_limit(), connection_bucket_write_limit(), connection_buckets_decrement(), and global_write_bucket_low().
void connection_link_connections | ( | connection_t * | conn_a, | |
connection_t * | conn_b | |||
) |
Create a link between conn_a and conn_b.
References connection_t::linked, connection_t::linked_conn, connection_t::s, and tor_assert.
Referenced by connection_exit_connect_dir(), and directory_initiate_command_rend().
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.
References CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_init(), control_connection_new(), dir_connection_new(), edge_connection_new(), or_connection_new(), and TO_CONN.
Referenced by connection_create_listener(), connection_handle_listener_read(), and spawn_cpuworker().
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().
References CELL_PAYLOAD_SIZE, and connection_t::outbuf_flushlen.
Referenced by connection_edge_consider_sending_sendme().
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.
References CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_CPUWORKER, CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_control_process_inbuf(), connection_cpu_process_inbuf(), connection_dir_process_inbuf(), connection_edge_process_inbuf(), connection_or_process_inbuf(), LD_BUG, TO_CONTROL_CONN(), TO_DIR_CONN(), TO_EDGE_CONN(), TO_OR_CONN(), tor_assert, tor_fragile_assert, and connection_t::type.
Referenced by connection_handle_read_impl().
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.
References connection_t::addr, alloc_http_authenticator(), connection_proxy_state_to_string(), fmt_addr(), get_options(), or_options_t::HttpsProxyAuthenticator, LD_BUG, LD_NET, LD_OR, connection_t::port, connection_t::proxy_state, or_options_t::Socks5ProxyUsername, SOCKS_COMMAND_CONNECT, tor_addr_family(), tor_addr_to_ipv4n(), tor_assert, tor_fragile_assert, tor_free, and tor_snprintf().
Referenced by connection_or_finished_connecting().
static const char * connection_proxy_state_to_string | ( | int | state | ) | [static] |
Convert state number to string representation for logging purposes.
Referenced by connection_proxy_connect(), and connection_read_proxy_handshake().
static int connection_reached_eof | ( | connection_t * | conn | ) | [static] |
Callback: invoked when a connection reaches an EOF event.
References CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_CPUWORKER, CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_control_reached_eof(), connection_cpu_reached_eof(), connection_dir_reached_eof(), connection_edge_reached_eof(), connection_or_reached_eof(), LD_BUG, TO_CONTROL_CONN(), TO_DIR_CONN(), TO_EDGE_CONN(), TO_OR_CONN(), tor_fragile_assert, and connection_t::type.
Referenced by connection_handle_read_impl().
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.
References connection_t::address, escaped(), fetch_from_buf_http(), connection_t::inbuf, LD_NET, LD_PROTOCOL, MAX_HEADERS_SIZE, parse_http_response(), and tor_free.
Referenced by 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.
References connection_t::address, connection_proxy_state_to_string(), connection_read_https_proxy_response(), connection_send_socks5_connect(), escaped(), fetch_from_buf_socks_client(), get_options(), connection_t::inbuf, LD_BUG, LD_NET, connection_t::port, connection_t::proxy_state, or_options_t::Socks5ProxyPassword, or_options_t::Socks5ProxyUsername, tor_assert, tor_fragile_assert, and tor_free.
Referenced by connection_or_process_inbuf().
static int connection_read_to_buf | ( | connection_t * | conn, | |
int * | 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.
References connection_t::address, approx_time(), buf_datalen(), buf_slack(), CASE_TOR_TLS_ERROR_ANY_NONIO, CONN_LOG_PROTECT, CONN_TYPE_AP, connection_bucket_read_limit(), connection_buckets_decrement(), connection_consider_empty_read_buckets(), connection_consider_empty_write_buckets(), connection_is_writing(), connection_start_writing(), connection_tls_continue_handshake(), connection_t::inbuf, connection_t::inbuf_reached_eof, LD_BUG, LD_GENERAL, LD_NET, connection_t::linked, connection_t::linked_conn, connection_t::marked_for_close, move_buf_to_buf(), edge_connection_t::n_read, or_connection_t::nickname, OR_CONN_STATE_PROXY_HANDSHAKING, OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING, OR_CONN_STATE_TLS_HANDSHAKING, connection_t::outbuf, connection_t::outbuf_flushlen, read_to_buf(), read_to_buf_tls(), connection_t::s, connection_t::state, or_connection_t::tls, or_connection_t::tls_error, TO_EDGE_CONN(), TO_OR_CONN(), tor_tls_err_to_string(), tor_tls_get_n_raw_bytes(), tor_tls_get_pending_bytes(), and connection_t::type.
Referenced by connection_handle_read_impl().
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.
References connection_t::addr, connection_t::port, connection_t::proxy_state, SOCKS_COMMAND_CONNECT, tor_addr_family(), tor_addr_to_in6(), and tor_addr_to_ipv4n().
Referenced by connection_read_proxy_handshake().
int connection_state_is_connecting | ( | connection_t * | conn | ) |
Return 1 if conn is in 'connecting' state, else return 0.
References CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, DIR_CONN_STATE_CONNECTING, EXIT_CONN_STATE_CONNECTING, connection_t::marked_for_close, OR_CONN_STATE_CONNECTING, connection_t::state, tor_assert, and connection_t::type.
Referenced by connection_handle_write_impl().
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.
References AP_CONN_STATE_OPEN, CONN_TYPE_AP, CONN_TYPE_CONTROL, CONN_TYPE_EXIT, CONN_TYPE_OR, CONTROL_CONN_STATE_OPEN, EXIT_CONN_STATE_OPEN, connection_t::marked_for_close, OR_CONN_STATE_OPEN, connection_t::state, tor_assert, and connection_t::type.
Referenced by connection_edge_process_relay_cell(), connection_edge_reached_eof(), and run_connection_housekeeping().
int connection_wants_to_flush | ( | connection_t * | conn | ) |
Return conn->outbuf_flushlen: how many bytes conn wants to flush from its outbuf.
References connection_t::outbuf_flushlen.
Referenced by conn_close_if_marked(), connection_edge_finished_connecting(), connection_exit_connect(), connection_handle_read_impl(), and connection_handle_write_impl().
control_connection_t* control_connection_new | ( | int | socket_family | ) |
Allocate and return a new control_connection_t, initialized as by connection_init().
References CONN_TYPE_CONTROL, connection_init(), LD_CONTROL, and TO_CONN.
Referenced by connection_new().
static struct sockaddr_in* create_inet_sockaddr | ( | const char * | listenaddress, | |
uint16_t | listenport, | |||
char ** | readable_address, | |||
socklen_t * | socklen_out | |||
) | [static, read] |
Create an AF_INET listenaddr struct. listenaddress provides the host and optionally the port information for the new structure. If no port is provided in listenaddress then listenport is used.
If not NULL readable_address will contain a copy of the host part of listenaddress.
The listenaddr struct has to be freed by the caller.
References LD_CONFIG, parse_addr_port(), and tor_free.
static struct sockaddr_un* create_unix_sockaddr | ( | const char * | listenaddress, | |
char ** | readable_address, | |||
socklen_t * | len_out | |||
) | [static, read] |
Create an AF_UNIX listenaddr struct. listenaddress provides the path to the Unix socket.
Eventually listenaddress will also optionally contain user, group, and file permissions for the new socket. But not yet. XXX Also, since we do not create the socket here the information doesn't help here.
If not NULL readable_address will contain a copy of the path part of listenaddress.
The listenaddr struct has to be freed by the caller.
dir_connection_t* dir_connection_new | ( | int | socket_family | ) |
Allocate and return a new dir_connection_t, initialized as by connection_init().
References CONN_TYPE_DIR, connection_init(), and TO_CONN.
Referenced by connection_exit_connect_dir(), connection_new(), and directory_initiate_command_rend().
edge_connection_t* edge_connection_new | ( | int | type, | |
int | socket_family | |||
) |
Allocate and return a new edge_connection_t, initialized as by connection_init().
References CONN_TYPE_AP, CONN_TYPE_EXIT, connection_init(), edge_connection_t::socks_request, TO_CONN, and tor_assert.
Referenced by connection_ap_make_link(), connection_exit_begin_conn(), connection_exit_begin_resolve(), connection_new(), dnsserv_launch_request(), and evdns_server_callback().
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 is 1 for v1 requests (directories and running-routers), and 2 for v2 requests (statuses and descriptors). But see FFFF in directory_handle_command_get() for why we don't use priority 2 yet.
There are a lot of parameters we could use here:
References authdir_mode(), or_options_t::BandwidthRate, connection_is_rate_limited(), get_options(), and or_options_t::RelayBandwidthRate.
Referenced by directory_handle_command_get().
or_connection_t* or_connection_new | ( | int | socket_family | ) |
Allocate and return a new or_connection_t, initialized as by connection_init().
References or_connection_t::active_circuit_pqueue, or_connection_t::active_circuit_pqueue_last_recalibrated, cell_ewma_get_tick(), CONN_TYPE_OR, connection_init(), crypto_rand_int(), or_connection_t::next_circ_id, smartlist_create(), or_connection_t::timestamp_last_added_nonpadding, and TO_CONN.
Referenced by connection_new(), and connection_or_connect().
int retry_all_listeners | ( | smartlist_t * | replaced_conns, | |
smartlist_t * | new_conns | |||
) |
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 old conns that should be closed to replaced_conns. Add all new connections to new_conns.
References or_options_t::ClientOnly, CONN_TYPE_AP_DNS_LISTENER, CONN_TYPE_AP_LISTENER, CONN_TYPE_AP_NATD_LISTENER, CONN_TYPE_AP_TRANS_LISTENER, CONN_TYPE_CONTROL_LISTENER, CONN_TYPE_DIR_LISTENER, CONN_TYPE_OR_LISTENER, or_options_t::ControlListenAddress, or_options_t::ControlPort, or_options_t::ControlSocket, or_options_t::DirListenAddress, or_options_t::DirPort, or_options_t::DNSListenAddress, or_options_t::DNSPort, get_options(), or_options_t::NatdListenAddress, or_options_t::NatdPort, or_options_t::ORListenAddress, or_options_t::ORPort, retry_listeners(), or_options_t::SocksListenAddress, or_options_t::SocksPort, or_options_t::TransListenAddress, and or_options_t::TransPort.
Referenced by options_act_reversible(), and run_scheduled_events().
static int retry_listeners | ( | int | type, | |
config_line_t * | cfg, | |||
int | port_option, | |||
const char * | default_addr, | |||
smartlist_t * | replaced_conns, | |||
smartlist_t * | new_conns, | |||
int | disable_all_conns, | |||
int | socket_family | |||
) | [static] |
Launch any configured listener connections of type type. (A listener is configured if port_option is non-zero. If any ListenAddress configuration options are given in cfg, create a connection binding to each one. Otherwise, create a single connection binding to the address default_addr.)
Only launch the listeners of this type that are not already open, and only close listeners that are no longer wanted. Existing listeners that are still configured are not touched.
If disable_all_conns is set, then never open new conns, and close the existing ones.
Add all old conns that should be closed to replaced_conns. Add all new connections to new_conns.
References connection_t::address, get_connection_array(), config_line_t::key, connection_t::marked_for_close, config_line_t::next, parse_addr_port(), connection_t::port, smartlist_add(), smartlist_create(), connection_t::socket_family, tor_assert, tor_free, connection_t::type, and config_line_t::value.
Referenced by retry_all_listeners().
static void set_constrained_socket_buffers | ( | int | 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.
References LD_NET.
Referenced by connection_connect(), and connection_handle_listener_read().
static void warn_too_many_conns | ( | void | ) | [static] |
Warn that an accept or a connect has failed because we're running up against our ulimit. Rate-limit these warnings so that we don't spam the log.
References control_event_general_status(), get_n_open_sockets(), and LD_NET.
Referenced by connection_connect(), connection_create_listener(), and connection_handle_listener_read().
Max number of bytes I can read this second.
Referenced by do_main_loop(), and second_elapsed_callback().
Max number of relayed (bandwidth class 1) bytes I can read this second.
Max number of relayed (bandwidth class 1) bytes I can write this second.
Max number of bytes I can write this second.
Referenced by do_main_loop(), and second_elapsed_callback().
uint32_t last_interface_ip = 0 [static] |
The last IPv4 address that our network interface seemed to have been binding to, in host order. We use this to detect when our IP changes.
Referenced by client_check_address_changed().
smartlist_t* outgoing_addrs = NULL [static] |
A list of uint32_ts for addresses we've used in outgoing connections. Used to detect IP address changes.
int write_buckets_empty_last_second = 0 [static] |
Did either global write bucket run dry last second? If so, we are likely to run dry again this second, so be stingy with the tokens we just put in.