Tor
0.4.7.0-alpha-dev
|
General high-level functions to handle reading and writing on connections. More...
#include "core/or/or.h"
#include "feature/client/bridges.h"
#include "lib/buf/buffers.h"
#include "lib/tls/buffers_tls.h"
#include "lib/err/backtrace.h"
#include "app/config/config.h"
#include "app/config/resolve_addr.h"
#include "core/mainloop/connection.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/netstatus.h"
#include "core/or/channel.h"
#include "core/or/channeltls.h"
#include "core/or/circuitbuild.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "core/or/connection_edge.h"
#include "core/or/connection_or.h"
#include "core/or/dos.h"
#include "core/or/policies.h"
#include "core/or/reasons.h"
#include "core/or/relay.h"
#include "core/or/status.h"
#include "core/or/crypt_path.h"
#include "core/proto/proto_haproxy.h"
#include "core/proto/proto_http.h"
#include "core/proto/proto_socks.h"
#include "feature/client/dnsserv.h"
#include "feature/client/entrynodes.h"
#include "feature/client/transports.h"
#include "feature/control/control.h"
#include "feature/control/control_events.h"
#include "feature/dirauth/authmode.h"
#include "feature/dirauth/dirauth_config.h"
#include "feature/dircache/dirserv.h"
#include "feature/dircommon/directory.h"
#include "feature/hibernate/hibernate.h"
#include "feature/hs/hs_common.h"
#include "feature/hs/hs_ident.h"
#include "feature/hs/hs_metrics.h"
#include "feature/metrics/metrics.h"
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerlist.h"
#include "feature/relay/dns.h"
#include "feature/relay/ext_orport.h"
#include "feature/relay/routermode.h"
#include "feature/rend/rendcommon.h"
#include "feature/stats/connstats.h"
#include "feature/stats/rephist.h"
#include "feature/stats/bwhist.h"
#include "lib/crypt_ops/crypto_util.h"
#include "lib/crypt_ops/crypto_format.h"
#include "lib/geoip/geoip.h"
#include "lib/cc/ctassert.h"
#include "lib/sandbox/sandbox.h"
#include "lib/net/buffers_net.h"
#include "lib/tls/tortls.h"
#include "lib/evloop/compat_libevent.h"
#include "lib/compress/compress.h"
#include "feature/dircommon/dir_connection_st.h"
#include "feature/control/control_connection_st.h"
#include "core/or/entry_connection_st.h"
#include "core/or/listener_connection_st.h"
#include "core/or/or_connection_st.h"
#include "core/or/port_cfg_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "core/or/socks_request_st.h"
Go to the source code of this file.
Macros | |
#define | CONNECTION_PRIVATE |
#define | CHANNEL_OBJECT_PRIVATE |
#define | ENABLE_LISTENER_REBIND |
#define | CASE_ANY_LISTENER_TYPE |
#define | CONN_IS_CLOSED(c) ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s))) |
#define | WARN_TOO_MANY_CONNS_INTERVAL (6*60*60) |
#define | SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1) |
#define | CLIENT_IDLE_TIME_FOR_PRIORITY 30 |
#define | CONN_GET_ALL_TEMPLATE(var, test) |
#define | CONN_GET_TEMPLATE(var, test) |
#define | DIR_CONN_LIST_TEMPLATE(conn_var, conn_test, dirconn_var, dirconn_test) |
Variables | |
static tor_addr_t * | last_interface_ipv4 = NULL |
static tor_addr_t * | last_interface_ipv6 = NULL |
static smartlist_t * | outgoing_addrs = NULL |
static int | listen_limit = INT_MAX |
static time_t | write_buckets_last_empty_at = -100 |
static time_t | last_recorded_accounting_at = 0 |
static uint32_t | last_refilled_global_buckets_ts =0 |
static mainloop_event_t * | reenable_blocked_connections_ev = NULL |
static int | reenable_blocked_connections_is_scheduled = 0 |
static struct timeval | reenable_blocked_connections_delay |
General high-level functions to handle reading and writing on connections.
Each connection (ideally) represents a TLS connection, a TCP socket, a unix socket, or a UDP socket on which reads and writes can occur. (But see connection_edge.c for cases where connections can also represent streams that do not have a corresponding socket.)
The module implements the abstract type, connection_t. The subtypes are:
The base type implemented in this module is responsible for basic rate limiting, flow control, and marshalling bytes onto and off of the network (either directly or via TLS).
Connections are registered with the main loop with connection_add(). As they become able to read or write register the fact with the event main loop by calling connection_watch_events(), connection_start_reading(), or connection_start_writing(). When they no longer want to read or write, they call connection_stop_reading() or connection_stop_writing().
To queue data to be written on a connection, call connection_buf_add(). When data arrives, the connection_process_inbuf() callback is invoked, which dispatches to a type-specific function (such as connection_edge_process_inbuf() for example). Connection types that need notice of when data has been written receive notification via connection_flushed_some() and connection_finished_flushing(). These functions all delegate to type-specific implementations.
Additionally, beyond the core of connection_t, this module also implements:
Definition in file connection.c.
#define CASE_ANY_LISTENER_TYPE |
Definition at line 213 of file connection.c.
#define CLIENT_IDLE_TIME_FOR_PRIORITY 30 |
How many seconds of no active local circuits will make the connection revert to the "relayed" bandwidth class?
Definition at line 3382 of file connection.c.
#define CONN_GET_ALL_TEMPLATE | ( | var, | |
test | |||
) |
Definition at line 4746 of file connection.c.
#define CONN_GET_TEMPLATE | ( | var, | |
test | |||
) |
Return a connection_t * from get_connection_array() that satisfies test on var, and that is not marked for close.
Definition at line 4780 of file connection.c.
#define CONN_IS_CLOSED | ( | c | ) | ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s))) |
Return true iff connection_close_immediate() has been called on this connection.
Definition at line 1035 of file connection.c.
#define DIR_CONN_LIST_TEMPLATE | ( | conn_var, | |
conn_test, | |||
dirconn_var, | |||
dirconn_test | |||
) |
Return a new smartlist of dir_connection_t * from get_connection_array() that satisfy conn_test on connection_t *conn_var, and dirconn_test on dir_connection_t *dirconn_var. conn_var must be of CONN_TYPE_DIR and not marked for close to be included in the list.
Definition at line 4846 of file connection.c.
#define ENABLE_LISTENER_REBIND |
On Windows and Linux we cannot reliably bind() a socket to an address and port if: 1) There's already a socket bound to wildcard address (0.0.0.0 or ::) with the same port; 2) We try to bind() to wildcard address and there's another socket bound to a specific address and the same port.
To address this problem on these two platforms we implement a routine that: 1) Checks if first attempt to bind() a new socket failed with EADDRINUSE. 2) If so, it will close the appropriate old listener connection and 3) Attempts bind()'ing the new listener socket again.
Just to be safe, we are enabling listener rebind code on all platforms, to account for unexpected cases where it may be needed.
Definition at line 166 of file connection.c.
char* alloc_http_authenticator | ( | const char * | authenticator | ) |
Allocates a base64'ed authenticator for use in http or https auth, based on the input string authenticator. Returns it if success, else returns NULL.
Definition at line 4999 of file connection.c.
Referenced by connection_https_proxy_connect().
int any_other_active_or_conns | ( | const or_connection_t * | this_conn | ) |
Return 1 if there are any active OR connections apart from this_conn.
We use this to guess if we should tell the controller that we didn't manage to connect to any of our bridges.
Definition at line 4920 of file connection.c.
void assert_connection_ok | ( | connection_t * | conn, |
time_t | now | ||
) |
Verify that connection conn has all of its invariants correct. Trigger an assert if anything is invalid.
Definition at line 5571 of file connection.c.
Referenced by add_connection_to_closeable_list(), connection_close_immediate(), connection_mark_for_close_(), connection_mark_for_close_internal_(), connection_or_finished_flushing(), dns_resolve_impl(), and inform_pending_connections().
|
static |
Do basic sanity checking on a newly received socket. Return 0 if it looks ok, else return -1.
Notably, some TCP stacks can erroneously have accept() return successfully with socklen 0, when the client sends an RST before the accept call (as nmap does). We want to detect that, and not go on with the connection.
Definition at line 1850 of file connection.c.
|
static |
Check whether the socket family from an accepted socket got is the same as the one that listener is waiting for. If it isn't, log a useful message and return -1. Else return 0.
This is annoying, but can apparently happen on some Darwins.
Definition at line 1893 of file connection.c.
|
static |
Given a socket handle, check whether the local address (sockname) of the socket is one that we've connected from before. If so, double-check whether our address has changed and we need to generate keys. If we do, call init_keys().
Definition at line 5020 of file connection.c.
Referenced by connection_finished_connecting().
void clock_skew_warning | ( | const connection_t * | conn, |
long | apparent_skew, | ||
int | trusted, | ||
log_domain_mask_t | domain, | ||
const char * | received, | ||
const char * | source | ||
) |
Log a warning, and possibly emit a control event, that received came at a skewed time. trusted indicates that the source was one that we had more faith in and therefore the warning level should have higher severity.
Definition at line 5861 of file connection.c.
const tor_addr_t * conn_get_outbound_address | ( | sa_family_t | family, |
const or_options_t * | options, | ||
unsigned int | conn_type | ||
) |
Retrieve the outbound address depending on the protocol (IPv4 or IPv6) and the connection type (relay, exit, ...) Return a socket address or NULL in case nothing is configured.
Definition at line 2339 of file connection.c.
|
static |
Returns the proxy type used by tor for a single connection, for logging or high-level purposes. Don't use it to fill the proxy_type field of or_connection_t; use the actual proxy protocol instead.
Definition at line 2513 of file connection.c.
Referenced by connection_socks5_proxy_connect().
int conn_listener_type_supports_af_unix | ( | int | type | ) |
Return true iff the provided connection listener type supports AF_UNIX sockets.
Definition at line 759 of file connection.c.
const char* conn_state_to_string | ( | int | type, |
int | state | ||
) |
Return the human-readable name for the connection state state for the connection type type
Definition at line 302 of file connection.c.
Referenced by connection_describe(), connection_expire_held_open(), and connection_or_get_state_description().
const char* conn_type_to_string | ( | int | type | ) |
Return the human-readable name for the connection type type
Definition at line 268 of file connection.c.
Referenced by connection_describe(), and connection_expire_held_open().
void connection_about_to_close_connection | ( | connection_t * | conn | ) |
Called when we're about to finally unlink and free a connection: perform necessary accounting and cleanup
Definition at line 1012 of file connection.c.
Referenced by connection_unlink().
void connection_bucket_adjust | ( | const or_options_t * | options | ) |
Update the global connection bucket settings to a new value.
Definition at line 3780 of file connection.c.
Referenced by options_act_relay_bandwidth().
|
static |
Helper function to decide how many bytes out of global_bucket we're willing to use for this transaction. base is the size of a cell on the network; priority says whether we should write many of them or just a few; and conn_bucket (if non-negative) provides an upper limit for our answer.
Definition at line 3406 of file connection.c.
void connection_bucket_init | ( | void | ) |
Initialize the global buckets to the values configured in the options
Definition at line 3755 of file connection.c.
|
static |
How many bytes at most can we read onto this connection?
Definition at line 3436 of file connection.c.
Referenced by connection_buf_read_from_socket().
|
static |
Refill the token buckets for a single connection conn, and the global token buckets as appropriate. Requires that now_ts is the time in coarse timestamp units.
Definition at line 3807 of file connection.c.
Referenced by connection_handle_read_impl().
ssize_t connection_bucket_write_limit | ( | connection_t * | conn, |
time_t | now | ||
) |
How many bytes at most can we write onto this connection?
Definition at line 3476 of file connection.c.
|
static |
We just read num_read and wrote num_written bytes onto conn. Decrement buckets appropriately.
Definition at line 3629 of file connection.c.
void connection_buf_add_buf | ( | connection_t * | conn, |
buf_t * | buf | ||
) |
Add all bytes from buf to conn's outbuf, draining them from buf. (If the connection is marked and will soon be closed, nothing is drained.)
Definition at line 4731 of file connection.c.
int connection_buf_get_bytes | ( | char * | string, |
size_t | len, | ||
connection_t * | conn | ||
) |
A pass-through to fetch_from_buf.
Definition at line 4232 of file connection.c.
int connection_buf_get_line | ( | connection_t * | conn, |
char * | data, | ||
size_t * | data_len | ||
) |
As buf_get_line(), but read from a connection's input buffer.
Definition at line 4239 of file connection.c.
|
static |
Pull in new bytes from conn->s or conn->linked_conn onto conn->inbuf, either directly or via TLS. Reduce the token buckets by the number of bytes read.
If *max_to_read is -1, then decide it ourselves, else go with the value passed to us. When returning, if it's changed, subtract the number of bytes we read from *max_to_read.
Return -1 if we want to break conn, else return 0.
Definition at line 4041 of file connection.c.
Referenced by connection_handle_read_impl().
void connection_check_oos | ( | int | n_socks, |
int | failed | ||
) |
Out-of-Sockets handler; n_socks is the current number of open sockets, and failed is non-zero if a socket exhaustion related error immediately preceded this call. This is where to do circuit-killing heuristics as needed.
Definition at line 5438 of file connection.c.
void connection_close_immediate | ( | connection_t * | conn | ) |
Close the underlying socket for conn, so we don't try to flush it. Must be used in conjunction with (right before) connection_mark_for_close().
Definition at line 1043 of file connection.c.
int connection_connect | ( | connection_t * | conn, |
const char * | address, | ||
const tor_addr_t * | addr, | ||
uint16_t | port, | ||
int * | socket_error | ||
) |
Take conn, make a nonblocking socket; try to connect to addr:port (port arrives in host order). If fail, return -1 and if applicable put your best guess about errno into *socket_error. Else assign s to conn->s: if connected return 1, if EAGAIN return 0.
addr:port can be different to conn->addr:conn->port if connecting through a proxy.
address is used to make the logs useful.
On success, add conn to the list of polled connections.
Definition at line 2396 of file connection.c.
STATIC int connection_connect_sockaddr | ( | connection_t * | conn, |
const struct sockaddr * | sa, | ||
socklen_t | sa_len, | ||
const struct sockaddr * | bindaddr, | ||
socklen_t | bindaddr_len, | ||
int * | socket_error | ||
) |
Take conn, make a nonblocking socket; try to connect to sa, binding to bindaddr if sa is not localhost. If fail, return -1 and if applicable put your best guess about errno into *socket_error. If connected return 1, if EAGAIN return 0.
Definition at line 2163 of file connection.c.
void connection_consider_empty_read_buckets | ( | connection_t * | conn | ) |
If we have exhausted our global buckets, or the buckets for conn, stop reading.
Definition at line 3698 of file connection.c.
void connection_consider_empty_write_buckets | ( | connection_t * | conn | ) |
If we have exhausted our global buckets, or the buckets for conn, stop writing.
Definition at line 3727 of file connection.c.
|
static |
Return 1 if conn should use tokens from the "relayed" bandwidth rates, else 0. Currently, only OR conns with bandwidth class 1, and directory conns that are serving data out, count.
Definition at line 3389 of file connection.c.
const char* connection_describe | ( | const connection_t * | conn | ) |
Describe a connection for logging purposes.
Returns a pointer to a static buffer; future calls to connection_describe() will invalidate this buffer.
Nobody should parse the output of this function; it can and will change in future versions of tor.
Definition at line 543 of file connection.c.
Referenced by dumpstats().
const char* connection_describe_peer | ( | const connection_t * | conn | ) |
Describe the peer or address of connection conn in a human-readable manner.
Returns a pointer to a static buffer; future calls to connection_describe_peer() or connection_describe() will invalidate this buffer.
Nobody should parse the output of this function; it can and will change in future versions of tor.
Definition at line 528 of file connection.c.
Referenced by handle_response_fetch_consensus().
|
static |
Helper: describe the peer or address of connection conn in a human-readable manner.
Returns a pointer to a static buffer; future calls to connection_describe_peer_internal() will invalidate this buffer.
If include_preposition is true, include a preposition before the peer address.
Nobody should parse the output of this function; it can and will change in future versions of tor.
Definition at line 403 of file connection.c.
Referenced by connection_describe(), and connection_describe_peer().
void connection_dir_buf_add | ( | const char * | string, |
size_t | len, | ||
dir_connection_t * | dir_conn, | ||
int | done | ||
) |
Write a string (of size len to directory connection dir_conn. Apply compression if connection is configured to use it and finalize it if done is true.
Definition at line 4707 of file connection.c.
bool connection_dir_is_global_write_low | ( | const connection_t * | conn, |
size_t | attempt | ||
) |
Return true iff the global write buckets are low enough that we shouldn't send attempt bytes of low-priority directory stuff out to conn.
If we are a directory authority, always answer dir requests thus true is always returned.
Note: There are a lot of parameters we could use here:
Definition at line 3531 of file connection.c.
smartlist_t* connection_dir_list_by_purpose_and_resource | ( | int | purpose, |
const char * | resource | ||
) |
Return a list of directory connections that are fetching the item described by purpose/resource. If there are none, return an empty list. This list must be freed using smartlist_free, but the pointers in it must not be freed. Note that this list should not be cached, as the pointers in it can be freed if their connections close.
Definition at line 4871 of file connection.c.
Referenced by connection_dir_count_by_purpose_and_resource().
smartlist_t* connection_dir_list_by_purpose_resource_and_state | ( | int | purpose, |
const char * | resource, | ||
int | state | ||
) |
Return a list of directory connections that are fetching the item described by purpose/resource/state. If there are none, return an empty list. This list must be freed using smartlist_free, but the pointers in it must not be freed. Note that this list should not be cached, as the pointers in it can be freed if their connections close.
Definition at line 4889 of file connection.c.
Referenced by connection_dir_count_by_purpose_resource_and_state().
void connection_dump_buffer_mem_stats | ( | int | severity | ) |
Log how many bytes are used by buffers of different kinds and sizes.
Definition at line 5522 of file connection.c.
Referenced by dumpmemusage().
void connection_expire_held_open | ( | void | ) |
Find each connection that has hold_open_until_flushed set to 1 but hasn't written in the past 15 seconds, and set hold_open_until_flushed to 0. This means it will get cleaned up in the next loop through close_if_marked() in main.c.
Definition at line 1159 of file connection.c.
Referenced by second_elapsed_callback().
int connection_fetch_from_buf_http | ( | connection_t * | conn, |
char ** | headers_out, | ||
size_t | max_headerlen, | ||
char ** | body_out, | ||
size_t * | body_used, | ||
size_t | max_bodylen, | ||
int | force_complete | ||
) |
As fetch_from_buf_http, but fetches from a connection's input buffer_t as appropriate.
Definition at line 4248 of file connection.c.
Referenced by directory_handle_command(), and metrics_connection_process_inbuf().
|
static |
Wrapper around fetch_from_buf_socks_client: see that functions for documentation of its behavior.
Definition at line 2886 of file connection.c.
|
static |
Called when our attempt to connect() to a server has just succeeded.
This function checks if the interface address has changed (clients only), and then passes conn to the connection-specific connection_*_finished_connecting() function.
Definition at line 5206 of file connection.c.
|
static |
We just finished flushing bytes to the appropriately low network layer, and there are no more bytes remaining in conn->outbuf or conn->tls to be flushed.
This function just passes conn to the connection-specific connection_*_finished_flushing() function.
Definition at line 5166 of file connection.c.
int connection_flush | ( | connection_t * | conn | ) |
Try to flush data that's waiting for a write on conn. Return -1 on failure, 0 on success.
Don't use this function for regular writing; the buffers system should be good enough at scheduling writes there. Instead, this function is for cases when we're about to exit or something and we want to report it right away.
Definition at line 4599 of file connection.c.
|
static |
Called whenever we've written data on a connection.
Definition at line 5141 of file connection.c.
void connection_free_ | ( | connection_t * | conn | ) |
Make sure conn isn't in any of the global conn lists; then free it.
Definition at line 960 of file connection.c.
Referenced by circuit_free_all().
void connection_free_all | ( | void | ) |
Call connection_free_minimal() on every connection in our array, and release all storage held by connection.c.
Don't do the checks in connection_free(), because they will fail.
Definition at line 5818 of file connection.c.
STATIC void connection_free_minimal | ( | connection_t * | conn | ) |
Deallocate memory used by conn. Deallocate its buffers if necessary, close its socket if necessary, and mark the directory as dirty if conn is an OR or OP connection.
Definition at line 777 of file connection.c.
Referenced by connection_free_all().
|
static |
Return an arbitrary active OR connection that isn't this_conn.
We use this to guess if we should tell the controller that we didn't manage to connect to any of our bridges.
Definition at line 4908 of file connection.c.
Referenced by any_other_active_or_conns().
connection_t* connection_get_by_global_id | ( | uint64_t | id | ) |
Return the stream with id id if it is not already marked for close.
Definition at line 4810 of file connection.c.
Referenced by get_stream().
connection_t* connection_get_by_type | ( | int | type | ) |
Return a connection of type type that is not marked for close.
Definition at line 4818 of file connection.c.
connection_t* connection_get_by_type_addr_port_purpose | ( | int | type, |
const tor_addr_t * | addr, | ||
uint16_t | port, | ||
int | purpose | ||
) |
Return a connection with given type, address, port, and purpose; or NULL if no such connection exists (or if all such connections are marked for close).
Definition at line 4797 of file connection.c.
Referenced by launch_direct_bridge_descriptor_fetch().
connection_t* connection_get_by_type_nonlinked | ( | int | type | ) |
Return a connection of type type that is not an internally linked connection, and is not marked for close.
Definition at line 4837 of file connection.c.
Referenced by check_network_participation_callback().
connection_t* connection_get_by_type_state | ( | int | type, |
int | state | ||
) |
Return a connection of type type that is in state state, and that is not marked for close.
Definition at line 4827 of file connection.c.
Referenced by directory_all_unreachable_cb().
|
static |
The listener connection conn told poll() it wanted to read. Call accept() on conn->s, and add the new connection if necessary.
Definition at line 1911 of file connection.c.
Referenced by connection_handle_read_impl().
|
static |
Read bytes from conn->s and process them.
It calls connection_buf_read_from_socket() to bring in any new bytes, and then calls connection_process_inbuf() to process them.
Mark the connection and return -1 if you want to close it, else return 0.
Definition at line 3905 of file connection.c.
|
static |
Try to flush more bytes onto conn->s.
This function is called in connection_handle_write(), which gets called from conn_write_callback() in main.c when libevent tells us that conn wants to write.
Update conn->timestamp_last_write_allowed to now, and call flush_buf or flush_buf_tls appropriately. If it succeeds and there are no more more bytes on conn->outbuf, then call connection_finished_flushing on it too.
If force, then write as many bytes as possible, ignoring bandwidth limits. (Used for flushing messages to controller connections on fatal errors.)
Mark the connection and return -1 if you want to close it, else return 0.
Definition at line 4330 of file connection.c.
|
static |
Write a proxy request of haproxy to conn for conn->addr:conn->port.
Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.
Definition at line 2716 of file connection.c.
|
static |
Write a proxy request of https to conn for conn->addr:conn->port, authenticating with the auth details given in the configuration (if available).
Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.
Definition at line 2555 of file connection.c.
|
static |
Initializes conn. (you must call connection_add() to link it into the main array).
Set conn->magic to the correct value.
Set conn->type to type. Set conn->s and conn->conn_array_index to -1 to signify they are not yet assigned.
Initialize conn's timestamps to now.
Definition at line 697 of file connection.c.
int connection_init_accepted_conn | ( | connection_t * | conn, |
const listener_connection_t * | listener | ||
) |
Initialize states for newly accepted connection conn.
If conn is an OR, start the TLS handshake.
If conn is a transparent AP, get its original destination and place it in circuit_wait.
The listener parameter is only used for AP connections.
Definition at line 2091 of file connection.c.
int connection_is_listener | ( | connection_t * | conn | ) |
Return 1 if conn is a listener conn, else return 0.
Definition at line 4936 of file connection.c.
Referenced by connection_mark_all_noncontrol_listeners(), dumpstats(), and retry_all_listeners().
int connection_is_moribund | ( | connection_t * | conn | ) |
Check if a connection is on the way out so the OOS handler doesn't try to kill more than it needs.
Definition at line 5421 of file connection.c.
Referenced by connection_count_moribund().
|
static |
Return 1 if we should apply rate limiting to conn, and 0 otherwise. Right now this just checks if it's an internal IP address or an internal connection. We also should, but don't, check if the connection uses pluggable transports, since we should then limit it even if it comes from an internal IP address.
Definition at line 3360 of file connection.c.
Referenced by connection_consider_empty_read_buckets(), connection_consider_empty_write_buckets(), and record_num_bytes_transferred_impl().
void connection_link_connections | ( | connection_t * | conn_a, |
connection_t * | conn_b | ||
) |
Create a link between conn_a and conn_b.
Definition at line 745 of file connection.c.
|
static |
Bind a new non-blocking socket listening to the socket described by listensockaddr.
address is only used for logging purposes and to add the information to the conn.
Set addr_in_use to true in case socket binding fails with EADDRINUSE.
Definition at line 1459 of file connection.c.
|
static |
Create a new listener connection for a given port. In case we for a reason that is not an error condition, set defer to true. If we cannot bind listening socket because address is already in use, set addr_in_use to true.
Definition at line 1785 of file connection.c.
void connection_mark_all_noncontrol_connections | ( | void | ) |
Mark every external connection not used for controllers for close.
Definition at line 3319 of file connection.c.
void connection_mark_all_noncontrol_listeners | ( | void | ) |
Mark every listener of type other than CONTROL_LISTENER to be closed.
Definition at line 3305 of file connection.c.
void connection_mark_for_close_ | ( | connection_t * | conn, |
int | line, | ||
const char * | file | ||
) |
Mark conn to be closed next time we loop through conn_close_if_marked() in main.c.
Definition at line 1076 of file connection.c.
Referenced by connection_mark_unattached_ap_().
void connection_mark_for_close_internal_ | ( | connection_t * | conn, |
int | line, | ||
const char * | file | ||
) |
Mark conn to be closed next time we loop through conn_close_if_marked() in main.c.
This _internal version bypasses the CONN_TYPE_OR checks; this should be called when you either are sure that if this is an or_connection_t the controlling channel has been notified (e.g. with connection_or_notify_error()), or you actually are the connection_or_close_for_error() or connection_or_close_normally() function. For all other cases, use connection_mark_and_flush() which checks for or_connection_t properly, instead. See below.
We want to keep this function simple and quick, since it can be called from quite deep in the call chain, and hence it should avoid having side-effects that interfere with its callers view of the connection.
Definition at line 1117 of file connection.c.
|
static |
Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
Return true iff it is okay to queue bytes on conn's outbuf for writing.
Definition at line 4610 of file connection.c.
Referenced by connection_buf_add_buf(), and connection_write_to_buf_impl_().
connection_t* connection_new | ( | int | type, |
int | socket_family | ||
) |
Allocate, initialize, and return a new connection_t subtype of type to make or receive connections of address family socket_family. The type should be one of the CONN_TYPE_* constants.
Definition at line 656 of file connection.c.
int connection_outbuf_too_full | ( | connection_t * | conn | ) |
Are there too many bytes on edge connection conn's outbuf to send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by connection_edge_consider_sending_sendme().
Definition at line 4269 of file connection.c.
|
static |
Process new bytes that have arrived on conn->inbuf.
This function just passes conn to the connection-specific connection_*_process_inbuf() function. It also passes in package_partial if wanted.
Definition at line 5113 of file connection.c.
int connection_proxy_connect | ( | connection_t * | conn, |
int | type | ||
) |
Write a proxy request of type (socks4, socks5, https, haproxy) to conn for conn->addr:conn->port, authenticating with the auth details given in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies support authentication.
Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.
Use connection_read_proxy_handshake() to complete the handshake.
Definition at line 2750 of file connection.c.
|
static |
Convert state number to string representation for logging purposes.
Definition at line 2484 of file connection.c.
|
static |
Callback: invoked when a connection reaches an EOF event.
Definition at line 5234 of file connection.c.
void connection_read_bw_exhausted | ( | connection_t * | conn, |
bool | is_global_bw | ||
) |
Mark conn as needing to stop reading because bandwidth has been exhausted. If is_global_bw, it is closing because global bandwidth limit has been exhausted. Otherwise, it is closing because its own bandwidth limit has been exhausted.
Definition at line 3672 of file connection.c.
|
static |
Read conn's inbuf. If the http response from the proxy is all here, make sure it's good news, then return 1. If it's bad news, return -1. Else return 0 and hope for better luck next time.
Definition at line 2793 of file connection.c.
int connection_read_proxy_handshake | ( | connection_t * | conn | ) |
Call this from connection_*_process_inbuf() to advance the proxy handshake.
No matter what proxy protocol is used, if this function returns 1, the handshake is complete, and the data remaining on inbuf may contain the start of the communication with the requested server.
Returns 0 if the current buffer contains an incomplete response, and -1 on error.
Definition at line 2903 of file connection.c.
Referenced by connection_or_process_inbuf().
|
static |
Send SOCKS5 CONNECT command to conn, copying conn->addr and conn->port into the request.
Definition at line 2854 of file connection.c.
|
static |
Write a proxy request of socks4 to conn for conn->addr:conn->port.
Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.
Definition at line 2599 of file connection.c.
|
static |
Write a proxy request of socks5 to conn for conn->addr:conn->port, authenticating with the auth details given in the configuration (if available).
Returns -1 if conn->addr is incompatible with the proxy protocol, and 0 otherwise.
Definition at line 2678 of file connection.c.
int connection_state_is_connecting | ( | connection_t * | conn | ) |
Return 1 if conn is in 'connecting' state, else return 0.
Definition at line 4976 of file connection.c.
int connection_state_is_open | ( | connection_t * | conn | ) |
Return 1 if conn is in state "open" and is not marked for close, else return 0.
Definition at line 4956 of file connection.c.
int connection_wants_to_flush | ( | connection_t * | conn | ) |
Return true if this connection has data to flush.
Definition at line 4259 of file connection.c.
void connection_write_bw_exhausted | ( | connection_t * | conn, |
bool | is_global_bw | ||
) |
Mark conn as needing to stop reading because write bandwidth has been exhausted. If is_global_bw, it is closing because global bandwidth limit has been exhausted. Otherwise, it is closing because its own bandwidth limit has been exhausted.
Definition at line 3687 of file connection.c.
|
static |
Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
Called when an attempt to add bytes on conn's outbuf has succeeded: start writing if appropriate.
Definition at line 4654 of file connection.c.
Referenced by connection_buf_add_buf(), and connection_write_to_buf_impl_().
|
static |
Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
Called when an attempt to add bytes on conn's outbuf has failed; mark the connection and warn as appropriate.
Definition at line 4625 of file connection.c.
Referenced by connection_write_to_buf_impl_().
void connection_write_to_buf_impl_ | ( | const char * | string, |
size_t | len, | ||
connection_t * | conn, | ||
int | zlib | ||
) |
Append len bytes of string onto conn's outbuf, and ask it to start writing.
If zlib is nonzero, this is a directory connection that should get its contents compressed or decompressed as they're written. If zlib is negative, this is the last data to be compressed, and the connection's zlib state should be flushed.
Definition at line 4675 of file connection.c.
const listener_connection_t* CONST_TO_LISTENER_CONN | ( | const connection_t * | c | ) |
Cast a const connection_t *
to a const listener_connection_t *
.
Exit with an assertion failure if the input is not a listener_connection_t
.
Definition at line 247 of file connection.c.
control_connection_t* control_connection_new | ( | int | socket_family | ) |
Allocate and return a new control_connection_t, initialized as by connection_init().
Definition at line 632 of file connection.c.
dir_connection_t* dir_connection_new | ( | int | socket_family | ) |
Allocate and return a new dir_connection_t, initialized as by connection_init().
Definition at line 561 of file connection.c.
edge_connection_t* edge_connection_new | ( | int | type, |
int | socket_family | ||
) |
Allocate and return a new edge_connection_t, initialized as by connection_init().
Definition at line 621 of file connection.c.
Referenced by connection_exit_begin_resolve().
entry_connection_t* entry_connection_new | ( | int | type, |
int | socket_family | ||
) |
Allocate and return a new entry_connection_t, initialized as by connection_init().
Allocate space to store the socks_request.
Definition at line 602 of file connection.c.
Referenced by dnsserv_launch_request().
int get_proxy_addrport | ( | tor_addr_t * | addr, |
uint16_t * | port, | ||
int * | proxy_type, | ||
int * | is_pt_out, | ||
const connection_t * | conn | ||
) |
Fills addr and port with the details of the global proxy server we are using. Store a 1 to the int pointed to by is_put_out if the connection is using a pluggable transport; store 0 otherwise. conn contains the connection we are using the proxy for.
Return 0 on success, -1 on failure.
Definition at line 5716 of file connection.c.
Referenced by log_failed_proxy_connection().
STATIC void kill_conn_list_for_oos | ( | smartlist_t * | conns | ) |
Kill a list of connections for the OOS handler.
Definition at line 5400 of file connection.c.
listener_connection_t* listener_connection_new | ( | int | type, |
int | socket_family | ||
) |
Allocate and return a new listener_connection_t, initialized as by connection_init().
Definition at line 644 of file connection.c.
void log_failed_proxy_connection | ( | connection_t * | conn | ) |
Log a failed connection to a proxy server. conn is the connection we use the proxy server for.
Definition at line 5777 of file connection.c.
|
static |
Tell the TCP stack that it shouldn't wait for a long time after sock has closed before reusing its port. Return 0 on success, -1 on failure.
Definition at line 1383 of file connection.c.
|
static |
Sort comparator for OOS victims; better targets sort before worse ones.
Definition at line 5274 of file connection.c.
|
static |
Comparator for the two-orconn case in OOS victim sort
Definition at line 5258 of file connection.c.
Referenced by oos_victim_comparator().
or_connection_t* or_connection_new | ( | int | type, |
int | socket_family | ||
) |
Allocate and return a new or_connection_t, initialized as by connection_init().
Initialize active_circuit_pqueue.
Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
Definition at line 576 of file connection.c.
STATIC smartlist_t* pick_oos_victims | ( | int | n | ) |
Pick n victim connections for the OOS handler and return them in a smartlist.
Definition at line 5307 of file connection.c.
|
static |
Return string representation of proxy_type.
Definition at line 5797 of file connection.c.
|
static |
Helper: adjusts our bandwidth history and informs the controller as appropriate, given that we have just read num_read bytes and written num_written bytes on conn.
Definition at line 3574 of file connection.c.
|
static |
Initialize the mainloop event that we use to wake up connections that find themselves blocked on bandwidth.
Definition at line 3865 of file connection.c.
|
static |
Called when we have blocked a connection for being low on bandwidth: schedule an event to reenable such connections, if it is not already scheduled.
Definition at line 3884 of file connection.c.
Referenced by connection_read_bw_exhausted(), and connection_write_bw_exhausted().
|
static |
Re-enable all connections that were previously blocked on read or write. This event is scheduled after enough time has elapsed to be sure that the buckets will refill when the connections have something to do.
Definition at line 3842 of file connection.c.
Referenced by reenable_blocked_connection_init().
int retry_all_listeners | ( | smartlist_t * | new_conns, |
int | close_all_noncontrol | ||
) |
Launch listeners for each port you should have open. Only launch listeners who are not already open, and only close listeners we no longer want.
Add all new connections to new_conns.
If close_all_noncontrol is true, then we only open control listeners, and we close all other listeners.
Definition at line 3203 of file connection.c.
Referenced by retry_listeners_callback().
|
static |
Given a list of listener connections in old_conns, and list of port_cfg_t entries in ports, open a new listener for every port in ports that does not already have a listener in old_conns.
Remove from old_conns every connection that has a corresponding entry in ports. Add to new_conns new every connection we launch. If we may need to perform socket rebind when creating new listener that replaces old one, create a listener_replacement_t struct for affected pair and add it to replacements.
If control_listeners_only is true, then we only open control listeners, and we do not remove any noncontrol listeners from old_conns.
Return 0 on success, -1 on failure.
Definition at line 3075 of file connection.c.
|
static |
Some systems have limited system buffers for recv and xmit on sockets allocated in a virtual server or similar environment. For a Tor server this can produce the "Error creating network socket: No buffer space available" error once all available TCP buffer space is consumed. This method will attempt to constrain the buffers allocated for the socket to the desired size to stay below system TCP buffer limits.
Definition at line 5090 of file connection.c.
|
static |
A socket failed from resource exhaustion.
Among other actions, warn that an accept or a connect has failed because we're running out of TCP sockets we can use on current system. Rate-limit these warnings so that we don't spam the log.
Definition at line 1249 of file connection.c.
listener_connection_t* TO_LISTENER_CONN | ( | connection_t * | c | ) |
Cast a connection_t *
to a listener_connection_t *
.
Exit with an assertion failure if the input is not a listener_connection_t
.
Definition at line 234 of file connection.c.
Referenced by CONST_TO_LISTENER_CONN(), dnsserv_close_listener(), and dnsserv_configure_listener().
|
static |
On Windows Vista and Windows 7, tune the send buffer size according to a hint from the OS.
This should help fix slow upload rates.
Definition at line 4281 of file connection.c.
|
static |
The last addresses that our network interface seemed to have been binding to. We use this as one way to detect when our IP changes.
XXXX+ We should really use the entire list of interfaces here.
Definition at line 206 of file connection.c.
|
static |
When did we last tell the accounting subsystem about transmitted bandwidth?
Definition at line 3568 of file connection.c.
Referenced by record_num_bytes_transferred_impl().
|
static |
Cached value of the last coarse-timestamp when we refilled the global buckets.
Definition at line 3800 of file connection.c.
Referenced by connection_bucket_refill_single().
|
static |
Max backlog to pass to listen. We start at
Definition at line 1429 of file connection.c.
|
static |
A list of tor_addr_t for addresses we've used in outgoing connections. Used to detect IP address changes.
Definition at line 211 of file connection.c.
Referenced by client_check_address_changed(), and connection_free_all().
|
static |
Delay after which to run reenable_blocked_connections_ev.
Definition at line 3831 of file connection.c.
Referenced by reenable_blocked_connection_init().
|
static |
Event to re-enable all connections that were previously blocked on read or write.
Definition at line 3828 of file connection.c.
Referenced by reenable_blocked_connection_init().
|
static |
True iff reenable_blocked_connections_ev is currently scheduled.
Definition at line 3831 of file connection.c.
Referenced by reenable_blocked_connection_init(), and reenable_blocked_connection_schedule().
|
static |
When was either global write bucket last empty? If this was recent, then we're probably low on bandwidth, and we should be stingy with our bandwidth usage.
Definition at line 3378 of file connection.c.