#include "or.h"
#include "memarea.h"
#include <event.h>
Defines | |
#define | MAIN_PRIVATE |
#define | MAX_SIGNEWNYM_RATE 10 |
#define | GREEDY_DESCRIPTOR_RETRY_INTERVAL (10) |
#define | LAZY_DESCRIPTOR_RETRY_INTERVAL (60) |
#define | DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60) |
#define | DIR_CONN_MAX_STALL (5*60) |
#define | TLS_HANDSHAKE_TIMEOUT (60) |
#define | IDLE_OR_CONN_TIMEOUT 180 |
#define | ENTROPY_INTERVAL (60*60) |
#define | SAVE_STABILITY_INTERVAL (30*60) |
#define | CHECK_V3_CERTIFICATE_INTERVAL (5*60) |
#define | NS_EXPIRY_SLOP (24*60*60) |
#define | CHECK_EXPIRED_NS_INTERVAL (2*60) |
#define | WRITE_STATS_INTERVAL (24*60*60) |
#define | CLEAN_CACHES_INTERVAL (30*60) |
#define | RETRY_DNS_INTERVAL (10*60) |
#define | CHECK_DESCRIPTOR_INTERVAL (60) |
#define | CHECK_IPADDRESS_INTERVAL (15*60) |
#define | FORCE_REGENERATE_DESCRIPTOR_INTERVAL (18*60*60) |
#define | BANDWIDTH_RECHECK_INTERVAL (12*60*60) |
#define | MEM_SHRINK_INTERVAL (60) |
#define | BRIDGE_STATUSFILE_INTERVAL (30*60) |
#define | NUM_JUMPED_SECONDS_BEFORE_WARN 100 |
#define | UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60) |
Functions | |
void | evdns_shutdown (int) |
static void | dumpmemusage (int severity) |
static void | dumpstats (int severity) |
static void | conn_read_callback (int fd, short event, void *_conn) |
static void | conn_write_callback (int fd, short event, void *_conn) |
static void | signal_callback (int fd, short events, void *arg) |
static void | second_elapsed_callback (int fd, short event, void *args) |
static int | conn_close_if_marked (int i) |
static void | connection_start_reading_from_linked_conn (connection_t *conn) |
static int | connection_should_read_from_linked_conn (connection_t *conn) |
int | connection_add (connection_t *conn) |
void | connection_unregister_events (connection_t *conn) |
int | connection_remove (connection_t *conn) |
static void | connection_unlink (connection_t *conn) |
void | add_connection_to_closeable_list (connection_t *conn) |
int | connection_is_on_closeable_list (connection_t *conn) |
int | connection_in_array (connection_t *conn) |
smartlist_t * | get_connection_array (void) |
void | connection_watch_events (connection_t *conn, watchable_events_t events) |
int | connection_is_reading (connection_t *conn) |
void | connection_stop_reading (connection_t *conn) |
void | connection_start_reading (connection_t *conn) |
int | connection_is_writing (connection_t *conn) |
void | connection_stop_writing (connection_t *conn) |
void | connection_start_writing (connection_t *conn) |
void | connection_stop_reading_from_linked_conn (connection_t *conn) |
static void | close_closeable_connections (void) |
void | directory_all_unreachable (time_t now) |
void | directory_info_has_arrived (time_t now, int from_cache) |
static void | run_connection_housekeeping (int i, time_t now) |
static void | signewnym_impl (time_t now) |
static void | run_scheduled_events (time_t now) |
static int | got_libevent_error (void) |
void | ip_address_changed (int at_interface) |
void | dns_servers_relaunch_checks (void) |
static int | do_hup (void) |
int | do_main_loop (void) |
void | control_signal_act (int the_signal) |
static void | exit_function (void) |
void | handle_signals (int is_parent) |
int | tor_init (int argc, char *argv[]) |
int | try_locking (or_options_t *options, int err_if_locked) |
int | have_lockfile (void) |
void | release_lockfile (void) |
void | tor_free_all (int postfork) |
void | tor_cleanup (void) |
int | do_list_fingerprint (void) |
void | do_hash_password (void) |
int | tor_main (int argc, char *argv[]) |
Variables | |
int | global_read_bucket |
int | global_write_bucket |
int | global_relayed_read_bucket |
int | global_relayed_write_bucket |
static int | stats_prev_global_read_bucket |
static int | stats_prev_global_write_bucket |
static uint64_t | stats_n_bytes_read = 0 |
static uint64_t | stats_n_bytes_written = 0 |
time_t | time_of_process_start = 0 |
long | stats_n_seconds_working = 0 |
static time_t | time_to_check_for_correct_dns = 0 |
static time_t | time_of_last_signewnym = 0 |
static int | signewnym_is_pending = 0 |
static smartlist_t * | connection_array = NULL |
static smartlist_t * | closeable_connection_lst = NULL |
static smartlist_t * | active_linked_connection_lst = NULL |
static int | called_loop_once = 0 |
int | has_completed_circuit = 0 |
static struct event * | timeout_event = NULL |
static int | n_libevent_errors = 0 |
uint64_t | rephist_total_alloc |
uint32_t | rephist_total_num |
static tor_lockfile_t * | lockfile = NULL |
#define DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60) |
How often do we 'forgive' undownloadable router descriptors and attempt to download them again?
Referenced by run_scheduled_events().
#define DIR_CONN_MAX_STALL (5*60) |
How long do we let a directory connection stall before expiring it?
Referenced by run_connection_housekeeping().
#define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10) |
How often do we check for router descriptors that we should download when we have too little directory info?
Referenced by run_scheduled_events().
#define IDLE_OR_CONN_TIMEOUT 180 |
How long do we wait before killing OR connections with no circuits? In Tor versions up to 0.2.1.25 and 0.2.2.12-alpha, we waited 15 minutes before cancelling these connections, which caused fast relays to accrue many many idle connections. Hopefully 3 minutes is low enough that it kills most idle connections, without being so low that we cause clients to bounce on and off.
Referenced by run_connection_housekeeping().
#define LAZY_DESCRIPTOR_RETRY_INTERVAL (60) |
How often do we check for router descriptors that we should download when we have enough directory info?
Referenced by run_scheduled_events().
#define MAX_SIGNEWNYM_RATE 10 |
How often will we honor SIGNEWNYM requests?
Referenced by run_scheduled_events(), and signal_callback().
#define TLS_HANDSHAKE_TIMEOUT (60) |
How long do we let OR connections handshake before we decide that they are obsolete?
void add_connection_to_closeable_list | ( | connection_t * | conn | ) |
Schedule conn to be closed.
References assert_connection_ok(), connection_t::marked_for_close, smartlist_add(), smartlist_isin(), and tor_assert.
Referenced by _connection_mark_for_close().
static void close_closeable_connections | ( | void | ) | [static] |
Close all connections that have been scheduled to get closed.
References connection_t::conn_array_index, conn_close_if_marked(), and connection_unlink().
Referenced by conn_read_callback(), conn_write_callback(), and run_scheduled_events().
static int conn_close_if_marked | ( | int | i | ) | [static] |
If the connection at connection_array[i] is marked for close, then:
References connection_t::address, assert_connection_ok(), buf_datalen(), CONN_TYPE_DIR, CONN_TYPE_EXIT, CONN_TYPE_OR, conn_type_to_string(), connection_bucket_write_limit(), connection_start_reading_from_linked_conn(), connection_unlink(), connection_wants_to_flush(), DIR_PURPOSE_SERVER, escaped_safe_str_client(), flush_buf(), flush_buf_tls(), get_options(), connection_t::hold_open_until_flushed, connection_t::inbuf, LD_GENERAL, LD_NET, connection_t::linked_conn, LOG_FN_CONN, connection_t::marked_for_close, connection_t::marked_for_close_file, move_buf_to_buf(), OR_CONN_STATE_OPEN, connection_t::outbuf, connection_t::outbuf_flushlen, connection_t::purpose, connection_t::s, server_mode(), connection_t::state, connection_t::timestamp_lastwritten, TO_OR_CONN(), and connection_t::type.
Referenced by close_closeable_connections().
static void conn_read_callback | ( | int | fd, | |
short | event, | |||
void * | _conn | |||
) | [static] |
Libevent callback: this gets invoked when (connection_t*)conn has some data to read.
References assert_connection_ok(), close_closeable_connections(), CONN_IS_EDGE, conn_type_to_string(), connection_edge_end_errno(), LD_BUG, LD_NET, connection_t::marked_for_close, connection_t::s, TO_EDGE_CONN(), tor_fragile_assert, and connection_t::type.
Referenced by connection_add().
static void conn_write_callback | ( | int | fd, | |
short | events, | |||
void * | _conn | |||
) | [static] |
Libevent callback: this gets invoked when (connection_t*)conn has some data to write.
References assert_connection_ok(), close_closeable_connections(), CONN_IS_EDGE, conn_type_to_string(), connection_close_immediate(), edge_connection_t::edge_has_sent_end, edge_connection_t::end_reason, LD_BUG, LD_NET, LOG_FN_CONN, connection_t::marked_for_close, connection_t::s, TO_EDGE_CONN(), tor_fragile_assert, and connection_t::type.
Referenced by connection_add().
int connection_add | ( | connection_t * | conn | ) |
Add conn to the array of connections that we can poll on. The connection's socket must be set; the connection starts out non-reading and non-writing.
References connection_t::address, connection_t::conn_array_index, conn_read_callback(), CONN_TYPE_AP, conn_type_to_string(), conn_write_callback(), LD_NET, connection_t::linked, connection_t::read_event, connection_t::s, smartlist_add(), TO_EDGE_CONN(), tor_assert, tor_event_new(), tor_libevent_get_base(), connection_t::type, and connection_t::write_event.
Referenced by connection_ap_make_link(), connection_connect(), connection_create_listener(), connection_exit_connect_dir(), connection_handle_listener_read(), directory_initiate_command_rend(), dnsserv_launch_request(), evdns_server_callback(), and spawn_cpuworker().
int connection_in_array | ( | connection_t * | conn | ) |
Return true iff conn is in the current poll array.
References smartlist_isin().
Referenced by assert_all_pending_dns_resolves_ok(), and connection_free().
int connection_is_on_closeable_list | ( | connection_t * | conn | ) |
Return 1 if conn is on the closeable list, else return 0.
References smartlist_isin().
Referenced by connection_free().
int connection_is_reading | ( | connection_t * | conn | ) |
Return true iff conn is listening for read events.
References connection_t::read_event, connection_t::reading_from_linked_conn, and tor_assert.
Referenced by connection_handle_read_impl(), connection_handle_write_impl(), and set_streams_blocked_on_circ().
int connection_is_writing | ( | connection_t * | conn | ) |
Return true iff conn is listening for write events.
References tor_assert, connection_t::write_event, and connection_t::writing_to_linked_conn.
Referenced by assert_connection_ok(), and connection_read_to_buf().
int connection_remove | ( | connection_t * | conn | ) |
Remove the connection from the global list, and remove the corresponding poll entry. Calling this function will shift the last connection (if any) into the position occupied by conn.
References connection_t::conn_array_index, conn_type_to_string(), connection_unregister_events(), LD_NET, connection_t::s, smartlist_del(), tor_assert, and connection_t::type.
Referenced by connection_unlink().
static int connection_should_read_from_linked_conn | ( | connection_t * | conn | ) | [static] |
Return true iff conn is linked conn, and reading from the conn linked to it would be good and feasible. (Reading is "feasible" if the other conn exists and has data in its outbuf, and is "good" if we have our reading_from_linked_conn flag set and the other conn has its writing_to_linked_conn flag set.)
References buf_datalen(), connection_t::linked, connection_t::linked_conn, connection_t::outbuf, connection_t::reading_from_linked_conn, and connection_t::writing_to_linked_conn.
Referenced by connection_start_reading(), and connection_start_writing().
void connection_start_reading | ( | connection_t * | conn | ) |
Tell the main loop to start notifying conn of any read events.
References connection_should_read_from_linked_conn(), connection_start_reading_from_linked_conn(), LD_NET, connection_t::linked, connection_t::read_event, connection_t::reading_from_linked_conn, connection_t::s, and tor_assert.
Referenced by circuit_resume_edge_reading_helper(), connection_bucket_refill(), connection_create_listener(), connection_edge_process_relay_cell(), connection_exit_connect_dir(), connection_free(), connection_init_accepted_conn(), connection_or_finished_connecting(), connection_or_set_state_open(), connection_tls_continue_handshake(), connection_tls_start_handshake(), connection_unlink(), connection_watch_events(), directory_initiate_command_rend(), set_streams_blocked_on_circ(), and spawn_cpuworker().
static void connection_start_reading_from_linked_conn | ( | connection_t * | conn | ) | [static] |
Helper: Tell the main loop to begin reading bytes into conn from its linked connection, if it is not doing so already. Called by connection_start_reading and connection_start_writing as appropriate.
References connection_t::active_on_link, called_loop_once, connection_t::linked, smartlist_add(), smartlist_isin(), tor_assert, and tor_libevent_get_base().
Referenced by conn_close_if_marked(), connection_start_reading(), and connection_start_writing().
void connection_start_writing | ( | connection_t * | conn | ) |
Tell the main loop to start notifying conn of any write events.
References connection_should_read_from_linked_conn(), connection_start_reading_from_linked_conn(), LD_NET, connection_t::linked, connection_t::linked_conn, connection_t::s, tor_assert, connection_t::write_event, and connection_t::writing_to_linked_conn.
Referenced by _connection_write_to_buf_impl(), connection_bucket_refill(), connection_edge_finished_connecting(), connection_read_to_buf(), connection_tls_continue_handshake(), and connection_watch_events().
void connection_stop_reading | ( | connection_t * | conn | ) |
Tell the main loop to stop notifying conn of any read events.
References connection_stop_reading_from_linked_conn(), LD_NET, connection_t::linked, connection_t::read_event, connection_t::reading_from_linked_conn, connection_t::s, and tor_assert.
Referenced by circuit_consider_stop_edge_reading(), connection_consider_empty_read_buckets(), connection_control_process_inbuf(), connection_edge_package_raw_inbuf(), connection_watch_events(), and set_streams_blocked_on_circ().
void connection_stop_reading_from_linked_conn | ( | connection_t * | conn | ) |
Tell the main loop to stop reading bytes into conn from its linked connection, if is currently doing so. Called by connection_stop_reading, connection_stop_writing, and connection_read.
References connection_t::active_on_link, connection_t::linked, smartlist_isin(), smartlist_remove(), and tor_assert.
Referenced by connection_handle_read_impl(), connection_stop_reading(), and connection_stop_writing().
void connection_stop_writing | ( | connection_t * | conn | ) |
Tell the main loop to stop notifying conn of any write events.
References connection_stop_reading_from_linked_conn(), LD_NET, connection_t::linked, connection_t::linked_conn, connection_t::s, tor_assert, connection_t::write_event, and connection_t::writing_to_linked_conn.
Referenced by connection_consider_empty_write_buckets(), connection_control_finished_flushing(), connection_cpu_finished_flushing(), connection_dir_finished_flushing(), connection_edge_finished_flushing(), connection_handle_write_impl(), connection_or_finished_flushing(), connection_tls_continue_handshake(), and connection_watch_events().
static void connection_unlink | ( | connection_t * | conn | ) | [static] |
If conn is an edge conn, remove it from the list of conn's on this circuit. If it's not on an edge, flush and send destroys for all circuits on this conn.
Remove it from connection_array (if applicable) and from closeable_connection_list.
Then free it.
References assert_connection_edge_not_dns_pending(), connection_t::conn_array_index, CONN_TYPE_EXIT, CONN_TYPE_OR, connection_about_to_close_connection(), connection_free(), connection_or_remove_from_identity_map(), connection_remove(), connection_start_reading(), connection_t::linked_conn, connection_t::marked_for_close, connection_t::reading_from_linked_conn, smartlist_remove(), TO_EDGE_CONN(), TO_OR_CONN(), tor_digest_is_zero(), and connection_t::type.
Referenced by close_closeable_connections(), and conn_close_if_marked().
void connection_unregister_events | ( | connection_t * | conn | ) |
Tell libevent that we don't care about conn any more.
References connection_t::dns_server_port, dnsserv_close_listener(), LD_BUG, connection_t::read_event, connection_t::s, tor_free, and connection_t::write_event.
Referenced by connection_close_immediate(), connection_free(), and connection_remove().
void connection_watch_events | ( | connection_t * | conn, | |
watchable_events_t | events | |||
) |
Set the event mask on conn to events. (The event mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
References connection_start_reading(), connection_start_writing(), connection_stop_reading(), and connection_stop_writing().
Referenced by connection_edge_finished_connecting(), connection_exit_connect(), connection_or_connect(), and directory_initiate_command_rend().
void control_signal_act | ( | int | the_signal | ) |
Used to implement the SIGNAL control command: if we accept the_signal as a remote pseudo-signal, act on it.
References LD_BUG, and signal_callback().
Referenced by handle_control_signal().
void directory_all_unreachable | ( | time_t | now | ) |
We've just tried every dirserver we know about, and none of them were reachable. Assume the network is down. Change state so next time an application connection arrives we'll delay it and try another directory fetch. Kill off all the circuit_wait streams that are waiting now, since they will all timeout anyway.
References socks_request_t::address, AP_CONN_STATE_CIRCUIT_WAIT, CONN_TYPE_AP, connection_get_by_type_state(), control_event_general_status(), END_STREAM_REASON_NET_UNREACHABLE, LD_NET, socks_request_t::port, safe_str_client(), edge_connection_t::socks_request, stats_n_seconds_working, and TO_EDGE_CONN().
Referenced by directory_get_from_dirserver().
void directory_info_has_arrived | ( | time_t | now, | |
int | from_cache | |||
) |
This function is called whenever we successfully pull down some new network statuses or server descriptors.
References any_predicted_circuits(), consider_testing_reachability(), directory_fetches_from_authorities(), directory_too_idle_to_fetch_descriptors(), or_options_t::DownloadExtraInfo, entry_guards_compute_status(), get_dir_info_status_string(), get_options(), has_completed_circuit, LD_DIR, router_have_minimum_dir_info(), server_mode(), update_extrainfo_downloads(), update_router_descriptor_downloads(), and we_are_hibernating().
Referenced by connection_dir_client_reached_eof(), and do_main_loop().
void dns_servers_relaunch_checks | ( | void | ) |
Forget what we've learned about the correctness of our DNS servers, and start learning again.
References dns_reset_correctness_checks(), get_options(), server_mode(), and time_to_check_for_correct_dns.
Referenced by configure_nameservers(), and ip_address_changed().
void do_hash_password | ( | void | ) |
Entry point for password hashing: take the desired password from the command line, and print its salted hash to stdout.
References base16_encode(), crypto_rand(), DIGEST_LEN, get_options(), S2K_SPECIFIER_LEN, and secret_to_key().
Referenced by tor_main().
static int do_hup | ( | void | ) | [static] |
Called when we get a SIGHUP: reload configuration files and keys, retry all connections, and so on.
References accounting_is_enabled(), accounting_record_bandwidth_usage(), addressmap_clear_transient(), authdir_mode_handles_descs(), circuit_expire_all_dirty_circs(), cpuworkers_rotate(), dirserv_load_fingerprint_file(), dns_reset(), get_options(), get_or_state(), LD_CONFIG, LD_GENERAL, options_init_from_torrc(), or_options_t::ReloadTorrcOnSIGHUP, router_reset_descriptor_download_failures(), router_reset_status_download_failures(), router_reset_warnings(), routerlist_reset_warnings(), server_mode(), and update_networkstatus_downloads().
Referenced by signal_callback().
int do_list_fingerprint | ( | void | ) |
Read/create keys as needed, and echo our fingerprint to stdout.
References crypto_pk_get_fingerprint(), FINGERPRINT_LEN, get_identity_key(), get_options(), init_keys(), LD_BUG, LD_GENERAL, or_options_t::Nickname, server_mode(), and tor_assert.
Referenced by tor_main().
int do_main_loop | ( | void | ) |
Tor main loop.
References called_loop_once, connection_bucket_init(), control_event_bootstrap(), cpu_init(), directory_info_has_arrived(), dns_init(), get_options(), global_read_bucket, global_write_bucket, got_libevent_error(), handle_signals(), identity_key_is_set(), init_cell_pool(), init_keys(), LD_BUG, LD_DIR, LD_GENERAL, LD_NET, router_reload_consensus_networkstatus(), router_reload_router_list(), router_reload_v2_networkstatus(), second_elapsed_callback(), server_mode(), stats_prev_global_read_bucket, stats_prev_global_write_bucket, tor_libevent_get_base(), tor_libevent_get_method(), trusted_dirs_reload_certs(), and update_approx_time().
Referenced by tor_main().
static void dumpmemusage | ( | int | severity | ) | [static] |
Write current memory usage information to the log.
References buf_dump_freelist_sizes(), connection_dump_buffer_mem_stats(), dump_cell_pool_usage(), dump_dns_mem_usage(), dump_routerlist_mem_usage(), LD_GENERAL, rephist_total_alloc, rephist_total_num, and tor_log_mallinfo().
Referenced by dumpstats().
static void dumpstats | ( | int | severity | ) | [static] |
Write all statistics to the log, with log level severity. Called in response to a SIGUSR1.
References buf_allocation(), buf_datalen(), circuit_dump_by_conn(), conn_state_to_string(), CONN_TYPE_OR, conn_type_to_string(), connection_is_listener(), dump_distinct_digest_count(), dump_pk_ops(), dumpmemusage(), LD_GENERAL, LD_NET, RELAY_PAYLOAD_SIZE, rend_service_dump_stats(), rep_hist_dump_stats(), safe_str_client(), stats_n_bytes_read, stats_n_bytes_written, stats_n_create_cells_processed, stats_n_created_cells_processed, stats_n_data_bytes_packaged, stats_n_data_bytes_received, stats_n_data_cells_packaged, stats_n_data_cells_received, stats_n_destroy_cells_processed, stats_n_padding_cells_processed, stats_n_relay_cells_delivered, stats_n_relay_cells_processed, stats_n_relay_cells_relayed, time_of_process_start, or_connection_t::tls, TO_OR_CONN(), and tor_tls_get_buffer_sizes().
Referenced by signal_callback().
static void exit_function | ( | void | ) | [static] |
Called by exit() as we shut down the process.
Referenced by tor_init().
smartlist_t* get_connection_array | ( | void | ) |
Set *array to an array of all connections, and *n to the length of the array. *array and *n must not be modified.
References smartlist_create().
Referenced by any_pending_bridge_descriptor_fetches(), choose_good_exit_server_general(), circuit_discard_optional_exit_enclaves(), connection_ap_attach_pending(), connection_ap_expire_beginning(), connection_ap_fail_onehop(), connection_bucket_refill(), connection_dump_buffer_mem_stats(), connection_expire_held_open(), connection_free_all(), connection_get_by_global_id(), connection_get_by_type(), connection_get_by_type_addr_port_purpose(), connection_get_by_type_purpose(), connection_get_by_type_state(), connection_get_by_type_state_rendquery(), connection_or_clear_identity_map(), control_event_stream_bandwidth_used(), control_update_global_event_mask(), cull_wedged_cpuworkers(), getinfo_helper_events(), list_pending_downloads(), rend_client_desc_trynow(), retry_listeners(), and send_control_event_string().
static int got_libevent_error | ( | void | ) | [static] |
Called when a possibly ignorable libevent error occurs; ensures that we don't get into an infinite loop by ignoring too many errors from libevent.
References LD_NET, and n_libevent_errors.
Referenced by do_main_loop().
void handle_signals | ( | int | is_parent | ) |
Set up the signal handlers for either parent or child.
References LD_BUG, signal_callback(), tor_evsignal_new(), and tor_libevent_get_base().
Referenced by cpuworker_main(), and do_main_loop().
int have_lockfile | ( | void | ) |
Return true iff we've successfully acquired the lock file.
Referenced by init_key_from_file(), and options_act().
void ip_address_changed | ( | int | at_interface | ) |
Called when our IP address seems to have changed. at_interface should be true if we detected a change in our interface, and false if we detected a change in our published address.
References dns_servers_relaunch_checks(), get_options(), init_keys(), mark_my_descriptor_dirty(), reset_bandwidth_test(), router_reset_reachability(), server_mode(), and stats_n_seconds_working.
Referenced by check_descriptor_ipaddress_changed(), client_check_address_changed(), options_act(), resolve_my_address(), and router_new_address_suggestion().
void release_lockfile | ( | void | ) |
If we have successfully acquired the lock file, release it.
References tor_lockfile_unlock().
Referenced by tor_free_all().
static void run_connection_housekeeping | ( | int | i, | |
time_t | now | |||
) | [static] |
Perform regular maintenance tasks for a single connection. This function gets run once per second per connection by run_scheduled_events.
References connection_t::address, buf_datalen(), cell_t::command, CONN_TYPE_DIR, CONN_TYPE_OR, connection_dir_reached_eof(), connection_or_connect_failed(), connection_or_write_cell_to_buf(), connection_state_is_open(), DIR_CONN_IS_SERVER, DIR_CONN_MAX_STALL, DIR_PURPOSE_FETCH_SERVERDESC, get_options(), connection_t::hold_open_until_flushed, IDLE_OR_CONN_TIMEOUT, connection_t::inbuf, or_connection_t::is_bad_for_new_circs, or_options_t::KeepalivePeriod, LD_DIR, LD_OR, LD_PROTOCOL, connection_t::marked_for_close, or_connection_t::n_circuits, OR_CONN_STATE_CONNECTING, connection_t::outbuf, connection_t::port, connection_t::purpose, connection_t::s, connection_t::state, or_connection_t::timestamp_last_added_nonpadding, or_connection_t::timestamp_lastempty, connection_t::timestamp_lastread, connection_t::timestamp_lastwritten, TO_DIR_CONN(), TO_OR_CONN(), tor_assert, connection_t::type, and we_are_hibernating().
Referenced by run_scheduled_events().
static void run_scheduled_events | ( | time_t | now | ) | [static] |
Perform regular maintenance tasks. This function gets run once per second by second_elapsed_callback().
0. See if we've been asked to shut down and our timeout has expired; or if our bandwidth limits are exhausted and we should hibernate; or if it's time to wake up from hibernation.
1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys, shut down and restart all cpuworkers, and update the directory if necessary.
1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context.
How often do we add more entropy to OpenSSL's RNG pool?
1c. If we have to change the accounting interval or record bandwidth used in this accounting interval, do so.
1d. Periodically, we discount older stability information so that new stability info counts more, and save the stability information to disk as appropriate.
2. Periodically, we consider force-uploading our descriptor (if we've passed our internal checks).
How often do we check whether part of our router info has changed in a way that would require an upload?
How often do we (as a router) check whether our IP address has changed?
If our router descriptor ever goes this long without being regenerated because something changed, we force an immediate regenerate-and-upload.
2c. Let directory voting happen.
3a. Every second, we examine pending circuits and prune the ones which have been pending for more than a few seconds. We do this before step 4, so it can try building more if it's not comfortable with the number of available circuits.
3b. Also look at pending streams and prune the ones that 'began' a long time ago but haven't gotten a 'connected' yet. Do this before step 4, so we can put them back into pending state to be picked up by the new circuit.
3c. And expire connections that we've held open for too long.
3d. And every 60 seconds, we relaunch listeners if any died.
4. Every second, we try a new circuit if there are no valid circuits. Every NewCircuitPeriod seconds, we expire circuits that became dirty more than MaxCircuitDirtiness seconds ago, and we make a new circ if there are no clean circuits.
5. We do housekeeping for each connection...
How often do we check buffers and pools for empty space that can be deallocated?
6. And remove any marked circuits...
7. And upload service descriptors if necessary.
8. and blow away any connections that need to die. have to do this now, because if we marked a conn for close and left its socket -1, then we'll pass it to poll/select and bad things will happen.
8b. And if anything in our state is ready to get flushed to disk, we flush it.
9. and if we're a server, check whether our DNS is telling stories to us.
10b. write bridge networkstatus file to disk
References accounting_is_enabled(), accounting_run_housekeeping(), advertised_server_mode(), any_predicted_circuits(), authdir_mode_tests_reachability(), authdir_mode_v3(), routerinfo_t::bandwidthcapacity, routerinfo_t::bandwidthrate, or_options_t::BridgeAuthoritativeDir, buf_shrink(), buf_shrink_freelists(), or_options_t::CellStatistics, check_descriptor_bandwidth_changed(), check_descriptor_ipaddress_changed(), circuit_build_needed_circs(), circuit_close_all_marked(), circuit_expire_building(), circuit_expire_old_circuits_serverside(), clean_cell_pool(), close_closeable_connections(), connection_ap_expire_beginning(), connection_expire_held_open(), connection_or_set_bad_connections(), consider_hibernation(), consider_publishable_server(), consider_testing_reachability(), cpuworkers_rotate(), crypto_rand_int(), crypto_seed_rng(), DESCRIPTOR_FAILURE_RESET_INTERVAL, or_options_t::DirReqStatistics, dirserv_test_reachability(), dirvote_act(), dns_init(), dns_launch_correctness_checks(), or_options_t::EntryStatistics, or_options_t::ExitPortStatistics, fetch_bridge_descriptors(), geoip_bridge_stats_init(), geoip_bridge_stats_write(), geoip_dirreq_stats_init(), geoip_dirreq_stats_write(), geoip_entry_stats_init(), geoip_entry_stats_write(), get_identity_key(), get_onion_key_set_at(), get_options(), GREEDY_DESCRIPTOR_RETRY_INTERVAL, has_completed_circuit, has_dns_init_failed(), LAZY_DESCRIPTOR_RETRY_INTERVAL, LD_BUG, LD_CONFIG, LD_CONTROL, LD_GENERAL, mark_my_descriptor_dirty_if_older_than(), MAX_SIGNEWNYM_RATE, MAX_SSL_KEY_LIFETIME, MIN_ONION_KEY_LIFETIME, networkstatus_dump_bridge_status_to_file(), networkstatus_get_latest_consensus(), networkstatus_v2_list_clean(), or_state_save(), rend_cache_clean(), rend_cache_clean_v2_descs_as_dir(), rend_consider_descriptor_republication(), rend_consider_services_upload(), rep_hist_buffer_stats_init(), rep_hist_buffer_stats_write(), rep_hist_downrate_old_runs(), rep_hist_exit_stats_init(), rep_hist_exit_stats_write(), rep_hist_record_mtbf_data(), rep_history_clean(), or_options_t::RephistTrackTime, reset_bandwidth_test(), retry_all_listeners(), rotate_onion_key(), router_dir_info_changed(), router_get_my_routerinfo(), router_have_minimum_dir_info(), router_rebuild_descriptor(), router_reset_descriptor_download_failures(), router_upload_dir_desc_to_dirservers(), routerlist_remove_old_routers(), run_connection_housekeeping(), server_mode(), should_record_bridge_info(), signewnym_impl(), signewnym_is_pending, stats_n_seconds_working, time_of_last_signewnym, time_to_check_for_correct_dns, TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT, tor_tls_context_new(), update_extrainfo_downloads(), update_networkstatus_downloads(), update_router_descriptor_downloads(), or_options_t::UseBridges, v3_authority_check_key_expiry(), networkstatus_t::valid_until, and we_are_hibernating().
Referenced by second_elapsed_callback().
static void second_elapsed_callback | ( | int | fd, | |
short | event, | |||
void * | args | |||
) | [static] |
Libevent callback: invoked once every second.
If more than this many seconds have elapsed, probably the clock jumped: doesn't count.
References accounting_add_bytes(), accounting_is_enabled(), routerinfo_t::address, check_whether_dirport_reachable(), check_whether_orport_reachable(), circuit_note_clock_jumped(), connection_bucket_refill(), control_event_bandwidth_used(), control_event_server_status(), control_event_stream_bandwidth_used(), routerinfo_t::dir_port, get_options(), global_read_bucket, global_write_bucket, has_completed_circuit, LD_CONFIG, LD_NET, n_libevent_errors, routerinfo_t::or_port, router_get_my_routerinfo(), run_scheduled_events(), server_mode(), stats_n_bytes_read, stats_n_bytes_written, stats_n_seconds_working, stats_prev_global_read_bucket, stats_prev_global_write_bucket, timeout_event, TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT, tor_evtimer_new(), tor_libevent_get_base(), update_approx_time(), and we_are_hibernating().
Referenced by do_main_loop().
static void signal_callback | ( | int | fd, | |
short | events, | |||
void * | arg | |||
) | [static] |
Libevent callback: invoked when we get a signal.
References addressmap_clear_transient(), do_hup(), dumpstats(), get_min_log_level(), get_options(), hibernate_begin_shutdown(), LD_CONFIG, LD_CONTROL, LD_GENERAL, MAX_SIGNEWNYM_RATE, server_mode(), signewnym_impl(), signewnym_is_pending, switch_logs_debug(), time_of_last_signewnym, and tor_cleanup().
Referenced by control_signal_act(), and handle_signals().
static void signewnym_impl | ( | time_t | now | ) | [static] |
Honor a NEWNYM request: make future requests unlinkable to past requests.
References addressmap_clear_transient(), circuit_expire_all_dirty_circs(), signewnym_is_pending, and time_of_last_signewnym.
Referenced by run_scheduled_events(), and signal_callback().
void tor_cleanup | ( | void | ) |
Do whatever cleanup is necessary before shutting Tor down.
References accounting_is_enabled(), accounting_record_bandwidth_usage(), authdir_mode_tests_reachability(), or_options_t::command, crypto_global_cleanup(), get_options(), get_or_state(), or_state_mark_dirty(), or_state_save(), or_options_t::PidFile, rep_hist_record_mtbf_data(), and tor_free_all().
Referenced by consider_hibernation(), hibernate_begin(), signal_callback(), and tor_main().
void tor_free_all | ( | int | postfork | ) |
Free all memory that we might have allocated somewhere. If postfork, we are a worker process and we want to free only the parts of memory that we won't touch. If !postfork, Tor is shutting down and we should free everything.
Helps us find the real leaks with dmalloc and the like. Also valgrind should then report 0 reachable in its leak report (in an ideal world -- in practice libevent, SSL, libc etc never quite free everything).
References addressmap_free_all(), buf_shrink_freelists(), circuit_free_all(), clear_pending_onions(), config_free_all(), connection_free_all(), dirserv_free_all(), dirvote_free_all(), dns_free_all(), entry_guards_free_all(), esc_router_info(), escaped(), free_cell_pool(), geoip_free_all(), logs_free_all(), memarea_clear_freelist(), networkstatus_free_all(), policies_free_all(), release_lockfile(), rend_cache_free_all(), rend_service_authorization_free_all(), rend_service_free_all(), rep_hist_free_all(), router_free_all(), routerlist_free_all(), smartlist_free(), timeout_event, tor_free, and tor_tls_free_all().
Referenced by cpuworker_main(), and tor_cleanup().
int tor_init | ( | int | argc, | |
char * | argv[] | |||
) |
Main entry point for the Tor command-line client.
References add_temp_log(), addressmap_init(), crypto_global_init(), exit_function(), get_options(), get_uname(), get_version(), LD_BUG, LD_CONFIG, LD_GENERAL, log_set_application_name(), network_init(), options_init_from_torrc(), rend_cache_init(), rep_hist_init(), smartlist_create(), time_of_process_start, and tor_snprintf().
Referenced by tor_main().
int tor_main | ( | int | argc, | |
char * | argv[] | |||
) |
Main entry point for the Tor process. Called from main().
References _tor_free(), _tor_malloc(), _tor_realloc(), do_hash_password(), do_list_fingerprint(), do_main_loop(), get_options(), init_logging(), LD_BUG, tor_assert, tor_cleanup(), tor_init(), tor_threads_init(), and update_approx_time().
Referenced by main().
int try_locking | ( | or_options_t * | options, | |
int | err_if_locked | |||
) |
Try to grab the lock file described in options, if we do not already have it. If err_if_locked is true, warn if somebody else is holding the lock, and exit if we can't get it after waiting. Otherwise, return -1 if we can't get the lockfile. Return 0 on success.
References LD_GENERAL, options_get_datadir_fname2_suffix(), tor_free, tor_lockfile_lock(), and try_locking().
Referenced by init_key_from_file(), options_act(), and try_locking().
smartlist_t* active_linked_connection_lst = NULL [static] |
List of linked connections that are currently reading data into their inbuf from their partner's outbuf.
int called_loop_once = 0 [static] |
Flag: Set to true iff we entered the current libevent main loop via loop_once. If so, there's no need to trigger a loopexit in order to handle linked connections.
Referenced by connection_start_reading_from_linked_conn(), and do_main_loop().
smartlist_t* closeable_connection_lst = NULL [static] |
List of connections that have been marked for close and need to be freed and removed from connection_array.
smartlist_t* connection_array = NULL [static] |
Smartlist of all open connections.
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().
int has_completed_circuit = 0 |
We set this to 1 when we've opened a circuit, so we can print a log entry to inform the user that Tor is working.
Referenced by circuit_note_clock_jumped(), circuit_send_next_onion_skin(), directory_info_has_arrived(), getinfo_helper_events(), options_act(), purpose_needs_anonymity(), run_scheduled_events(), and second_elapsed_callback().
tor_lockfile_t* lockfile = NULL [static] |
A lockfile structure, used to prevent two Tors from messing with the data directory at once. If this variable is non-NULL, we're holding the lockfile.
int n_libevent_errors = 0 [static] |
Number of libevent errors in the last second: we die if we get too many.
Referenced by got_libevent_error(), and second_elapsed_callback().
uint64_t rephist_total_alloc |
Total number of bytes currently allocated in fields used by rephist.c.
Referenced by _free_link_history(), add_predicted_port(), bw_array_new(), dumpmemusage(), free_or_history(), get_link_history(), get_or_history(), predicted_ports_free(), rep_hist_get_predicted_ports(), and rep_history_clean().
uint32_t rephist_total_num |
Number of or_history_t objects currently allocated.
Referenced by dumpmemusage(), free_or_history(), and get_or_history().
int signewnym_is_pending = 0 [static] |
Is there a signewnym request we're currently waiting to handle?
Referenced by run_scheduled_events(), signal_callback(), and signewnym_impl().
uint64_t stats_n_bytes_read = 0 [static] |
How many bytes have we read since we started the process?
Referenced by dumpstats(), and second_elapsed_callback().
uint64_t stats_n_bytes_written = 0 [static] |
How many bytes have we written since we started the process?
Referenced by dumpstats(), and second_elapsed_callback().
long stats_n_seconds_working = 0 |
How many seconds have we been running?
Referenced by directory_all_unreachable(), hibernate_end(), ip_address_changed(), router_dump_router_to_string(), run_scheduled_events(), and second_elapsed_callback().
int stats_prev_global_read_bucket [static] |
What was the read bucket before the last second_elapsed_callback() call? (used to determine how many bytes we've read).
Referenced by do_main_loop(), and second_elapsed_callback().
int stats_prev_global_write_bucket [static] |
What was the write bucket before the last second_elapsed_callback() call? (used to determine how many bytes we've written).
Referenced by do_main_loop(), and second_elapsed_callback().
time_t time_of_last_signewnym = 0 [static] |
When did we last process a SIGNEWNYM request?
Referenced by run_scheduled_events(), signal_callback(), and signewnym_impl().
time_t time_of_process_start = 0 |
What time did this process start up?
Referenced by dumpstats(), and tor_init().
time_t time_to_check_for_correct_dns = 0 [static] |
When do we next launch DNS wildcarding checks?
Referenced by dns_servers_relaunch_checks(), and run_scheduled_events().
struct event* timeout_event = NULL [static] |
Libevent timer: used to invoke second_elapsed_callback() once per second.
Referenced by second_elapsed_callback(), and tor_free_all().