Tor source code file analysis-circuits global variables

Source: Internet
Author: User

In Tor system source code, there are three files used to control the link circuit: circuitlist. C, circuituse. C, and circuitbuild. C. The processing functions of these three files are similar to those of their names, namely, the Organization, use, and creation of the processing links respectively. In this article, we will only introduce global variables mainly used for links, that is, the use of global variables in these three files. The link functions are not listed here, because there are a lot of functions in the Link part, especially the link creation part. It may take a special time to describe link establishment and describe the functions of link establishment later in this article, but this is not the main content of this article.

0. global variable circuitlist. c

Circuitlist. c files are mainly used to process the organization, sorting, and counting of link lists. The first two global variables in the file are as follows:

/** A global list of all circuits at this hop. */circuit_t *global_circuitlist=NULL;/** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */static smartlist_t *circuits_pending_or_conns=NULL;

The first global variable is used to maintain the system's Link List. The system link list is the global link list maintained on each tor host. That is to say, all links are attached to this link list. The second global variable is used to maintain the list of system wait links. The list of system wait links is the list of links in the system waiting for or connection completion.

The operations on these two lists are simple, that is, the insert and delete of the linked list. Its code can also be easily searched and analyzed using sourceinsight, Which is omitted here. Before carefully studying the code, we can speculate that the global link list must be used when the link is established. Once the link structure is created, the start state of the link is set, is added to the global link list. Of course, if the link starts to be in the waiting or connection status, it will also be added to the waiting link list. The relationship between the two lists should not be so important, but ensure the consistency of the two lists: the links in the global link list that are in the waiting state must be in the waiting list; all links in the wait link list should exist in the global link list.

The last two global variables in the file are as follows: (view the source code for the use of the hash table)

/** Map from [orconn,circid] to circuit. */static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)     orconn_circid_circuit_map = HT_INITIALIZER();HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,             _orconn_circid_entry_hash, _orconn_circid_entries_eq)HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,            _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,            malloc, realloc, free)/** The most recently returned entry from circuit_get_by_circid_orconn; * used to improve performance when many cells arrive in a row from the * same circuit. */orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;

The data flow diagram of the ToR system is shown in the subsequent analysis. In the analysis of the data block diagram, the link-to-or connection direction can be clearly found, but the search from or to the link is not so easy. The TOR system uses a hash table to map link IDs from or to a specified link. The two global variables above define the global hash table. The main function of this hash table is to quickly complete the ing from the link ID and or to the specified link, in order not to query every time, the second global variable stores the latest search results for quick return to improve efficiency. This part of the global variable can be seen in the detailed use process in the function circuit_get_by_circid_orconn. Here, the code is omitted.

1. Global variable circuitbuild. c

When a link in the TOR system is used, the creation timeout occurs: How long does it take to try to establish a link? If the link is completed within the timeout period, the link is successful. If the link times out, the creation of the link is abandoned. The learncircuitbuildtimeout and circuitbuildtimeout options in the configuration file are used to control the system timeout. The former sets whether the system selects the time-out time in an adaptive manner. The latter sets an initial value for the time-out period. However, if the adaptive option is disabled, the time-out value remains unchanged at the initial value. In the circuitbuild. c file, a large number of functions are used to deal with timeout issues. Later, we will introduce them as a sub-functional component, which will be omitted here.

/** Global list of circuit build times */circuit_build_times_t circ_times;

In the TOR system, select the nodes that constitute the link before the link is established. The most important node is the entry node. The entry node is called entry guard in the document. The system cannot use the entry node at will. Instead, it selects several fixed entry nodes from all nodes. After that, the establishment of each link starts with the entry node. The purpose of this operation is to reduce the probability that the competitor controls the TOR node to grasp user information. Therefore, the system needs a Global Entry node list. After the entry node is selected, the node is added to this list so that subsequent link creation can select the entry node from the list. The following are global variables related to the entry node:

/** A list of our chosen entry guards. */static smartlist_t * entry_guards = NULL;/** a value of 1 means that the entry_guards list has changed * and those changes need to be flushed to disk. */static int entry_guards_dirty = 0; // indicates whether to add the entry node/** when selecting the entry node, we try to choose an entry guard from the configuration options, shocould We parse and add * config's entrynodes first? */Static int should_add_entry_nodes = 0;

The general meaning of the global variables in the center is to check whether the entry node list is changed. If the entry node list is changed, mark it to write the entry node information back to the hard disk when appropriate.

Indicates the global variables of unit test, which are less useful:

/** If set, we're running the unit tests: we should avoid clobbering * our state file or accessing get_options() or get_or_state() */static int unit_tests = 0;

The following is a global list of bridge and Transport read in the configuration file:

/** A list of configured bridges. Whenever we actually get a descriptor * for one, we add it as an entry guard.  Note that the order of bridges * in this list does not necessarily correspond to the order of bridges * in the torrc. */static smartlist_t *bridge_list = NULL;/** A list of pluggable transports found in torrc. */static smartlist_t *transport_list = NULL;

The specific use of the two is to be detailed when analyzing the System Identity Logic.

2. Global variable circuituse. c

There are few global variables used in this file. The following is a simple list:

/** True iff we've ever had enough testing circuits open to test our * bandwidth. */static int have_performed_bandwidth_test = 0;/** Number of consecutive failures so far; should only be touched by * circuit_launch_new and circuit_*_failure_count. */static int n_circuit_failures = 0;/** Before the last time we called circuit_reset_failure_count(), were * there a lot of failures? */static int did_circs_fail_last_period = 0;

The first global variable is used to indicate whether a bandwidth test is required. The main Execution Code of the bandwidth test is carried out by the function in the relay. c file. The instructions here are mainly used to determine whether more links need to be established to support bandwidth testing. If the indicator is 1, it indicates that the test has been performed; if the indicator is 0, it indicates that the bandwidth test is required.

The last two global variables are mainly used to mark and record Link errors. When a link error occurs, you need to increase the error count. When the error count increases to a certain number, the system stops re-creating the link. After a certain period of time, the system will re-set the error count, mark the number of error records, and then try to create a link.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.