connection.c File Reference

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

#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_tconnection_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_tdir_connection_new (int socket_family)
or_connection_tor_connection_new (int socket_family)
edge_connection_tedge_connection_new (int type, int socket_family)
control_connection_tcontrol_connection_new (int socket_family)
connection_tconnection_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_tconnection_get_by_type_addr_port_purpose (int type, const tor_addr_t *addr, uint16_t port, int purpose)
connection_tconnection_get_by_global_id (uint64_t id)
connection_tconnection_get_by_type (int type)
connection_tconnection_get_by_type_state (int type, int state)
connection_tconnection_get_by_type_state_rendquery (int type, int state, const char *rendquery)
connection_tconnection_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_toutgoing_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


Detailed Description

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


Define Documentation

#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)->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().


Function Documentation

static void _connection_free ( connection_t conn  )  [static]

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

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  ) 

void connection_about_to_close_connection ( connection_t conn  ) 

Do any cleanup needed:

  • 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.

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]

void connection_bucket_refill ( int  seconds_elapsed,
time_t  now 
)

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 
)

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

void connection_close_immediate ( connection_t conn  ) 

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]

static void connection_consider_empty_write_buckets ( connection_t conn  )  [static]

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]

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 
)

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]

static int connection_flushed_some ( connection_t conn  )  [static]

void connection_free ( connection_t conn  ) 

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]

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]

int connection_is_listener ( connection_t conn  ) 

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 
)

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]

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]

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  ) 

int connection_state_is_open ( connection_t conn  ) 

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  ) 

edge_connection_t* edge_connection_new ( int  type,
int  socket_family 
)

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:

  • 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.

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  ) 

int retry_all_listeners ( smartlist_t replaced_conns,
smartlist_t new_conns 
)

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().


Variable Documentation

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.

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.


Generated on Tue May 25 00:30:41 2010 for tor by  doxygen 1.5.6