所謂的Edge串連主要是形容在鏈路兩端所使用的串連,包括AP串連與EXIT串連兩種。Edge串連的存在,主要是為了個應用程式串連提供服務,所以才將兩種Edge串連命名為Application Proxy應用代理串連和EXIT出口串連。這兩種串連主要出現於鏈路兩端,即鏈路第一跳與鏈路最後一跳,分別用於接受應用請求和處理應用請求的發出。因為Edge串連在Tor系統中也具有重要的作用,所以本篇就簡單分析關於Edge串連的源碼檔案connection_edge.c。
因為Edge串連位於鏈路兩端,需要處理的事務比中間結點單純的傳遞和記錄要繁瑣出許多,有很多部分筆者也沒有很詳細的看過或找出其應用需要,此處必定有很多講的含糊其辭和不準確的地方,請大家包涵。
0. 全域變數
/** A client-side struct to remember requests to rewrite addresses * to new addresses. These structs are stored in the hash table * "addressmap" below. * 由此處可見,addressmap全域變數的使用,只存在於用戶端,其他身份的TOR成員並不使用此全域變數; * * There are 5 ways to set an address mapping: * - A MapAddress command from the controller [permanent] * - An AddressMap directive in the torrc [permanent] * - When a TrackHostExits torrc directive is triggered [temporary] * - When a DNS resolve succeeds [temporary] * - When a DNS resolve fails [temporary] * 由此處可見,除去在設定檔中進行地址映射之外,主要還是靠DNS解析來修改address mapping; * * When an addressmap request is made but one is already registered, * the new one is replaced only if the currently registered one has * no "new_address" (that is, it's in the process of DNS resolve), * or if the new one is permanent (expires==0 or 1). * * (We overload the 'expires' field, using "0" for mappings set via * the configuration file, "1" for mappings set from the control * interface, and other values for DNS and TrackHostExit mappings that can * expire.) * * A mapping may be 'wildcarded'. If "src_wildcard" is true, then * any address that ends with a . followed by the key for this entry will * get remapped by it. If "dst_wildcard" is also true, then only the * matching suffix of such addresses will get replaced by new_address. */typedef struct { char *new_address; time_t expires; addressmap_entry_source_t source:3; unsigned src_wildcard:1; unsigned dst_wildcard:1; short num_resolve_failures;} addressmap_entry_t;/** Entry for mapping addresses to which virtual address we mapped them to. */typedef struct { char *ipv4_address; char *hostname_address;} virtaddress_entry_t;/** A hash table to store client-side address rewrite instructions. */static strmap_t *addressmap=NULL;/** * Table mapping addresses to which virtual address, if any, we * assigned them to. * * We maintain the following invariant: if [A,B] is in * virtaddress_reversemap, then B must be a virtual address, and [A,B] * must be in addressmap. We do not require that the converse hold: * if it fails, then we could end up mapping two virtual addresses to * the same address, which is no disaster. **/static strmap_t *virtaddress_reversemap=NULL;
此處兩全域變數的作用尚不明確,有待分析。
/* By default, we hand out 127.192.0.1 through 127.254.254.254. * These addresses should map to localhost, so even if the * application accidentally tried to connect to them directly (not * via Tor), it wouldn't get too far astray. * * These options are configured by parse_virtual_addr_network(). *//** Which network should we use for virtual IPv4 addresses? Only the first * bits of this value are fixed. */static uint32_t virtual_addr_network = 0x7fc00000u;/** How many bits of <b>virtual_addr_network</b> are fixed? */static maskbits_t virtual_addr_netmask_bits = 10;/** What's the next virtual address we will hand out? */static uint32_t next_virtual_addr = 0x7fc00000u;
此處三全域變數的作用尚不明確,有待分析。
1. AP與EXIT通用函數
connection_edge_reached_eof --> connection_edge_end
中斷連線,如果身份正確(AP)則發送end cell通知對應出口結點關閉資料流;
connection_edge_process_inbuf
分情況管理串連事務及串連緩衝區內部的資料;
connection_edge_destroy --> connection_mark_unattached_ap(若身份為AP)
connection_edge_end_errno --> connection_edge_end
串連因鏈路斷開而結束或因某個errno而結束,則關閉串連,發送流終止通知;
connection_edge_flushed_some
connection_edge_finished_flushing
connection_edge_finished_connecting(主要用於身份為EXIT的情況)
串連完成緩衝區輸出或完成下層串連之時調用相關函數,以完成掃尾工作;
connection_edge_is_rendezvous_stream
connection_edge_compatible_with_circuit
connection_edge_update_circuit_isolation
circuit_clear_isolation
串連的某些屬性判斷,關鍵點在於rendezvous子系統和isolation flag作用;
2. AP處理函數
connection_mark_unattached_ap
connection_ap_about_to_close
AP串連關閉前的處理以及關閉處理;
connection_ap_handshake_send_begin
connection_ap_handshake_send_resolve
AP串連向遠端發送Relay Begin以及Relay Resolve命令,用於完成開啟流與位址解析的功能;
connection_ap_make_link
一般用於內部DIR串連與AP串連相關聯之時,該函數建立一個可用的AP串連,並將其與DIR串連互聯;可用意味著AP串連找到附著的鏈路Circuit;
connection_ap_handshake_socks_reply
connection_ap_handshake_socks_resolved
向AP串連送回相關訊息:串連結束訊息,位址解析成功訊息;
connection_ap_can_use_exit
判斷出口結點是否允許相應AP串連出口;
connection_ap_expire_beginning
connection_ap_attach_pending
connection_ap_detach_retriable
重連一個請求逾時的AP串連;重新為找不到鏈路的AP串連分配鏈路;
connection_ap_fail_onehop
通知所有等待某個無法串連的結點的AP串連;
connection_ap_process_transparent --> connection_ap_rewrite_and_attach_if_allowed --> connection_ap_handshake_rewrite_and_attach
透明AP使用的函數,未細究;
3. EXIT處理函數
connection_exit_about_to_close
connection_exit_begin_conn --> connection_exit_connect
connection_exit_begin_resolve
以上四個函數包括了EXIT串連的幾乎所有操作,主要就是串連的建立與對外的串連,以及位址解析和關閉;
4. AddressMap相關處理函數(暫略)
address_is_invalid_destination
parse_virtual_addr_network
parse_extended_hostname
addressmap_init
addressmap_clear_excluded_trackexithosts
addressmap_clear_invalid_automaps
addressmap_clean
addressmap_clear_configured
addressmap_clear_transient
addressmap_free_all
addressmap_rewrite
addressmap_have_mapping
addressmap_register
addressmap_register_virtual_address
addressmap_get_mappings
client_dns_incr_failures
client_dns_clear_failures
client_dns_set_addressmap
5. 額外函數(暫略)
circuit_discard_optional_exit_enclaves
get_pf_socket