Tor原始碼分析二 -- 目錄結構
Tor由於經過了長年的開發,版本到現在已經有很多。筆者選用的是tor-0.2.3.25版本。關於版本變遷和更新說明,請大家自行檢視:
1. 原始碼目錄關係圖
Tor的原始碼目錄中包括doc, contrib, src等一些列很莫名的檔案。其實,在這些所有的檔案中,除了src資料夾,其他都可以在初期暫時不去理會。所以,這裡我們就談src資料夾下的目錄結構,也是Tor原始碼核心的目錄結構。
上面兩圖已經基本給出了各個子目錄之間的關係和引用次數。總的來說,src目錄下包括下列這樣的目錄:
1)win32:用於Windows的目錄,內部只含有一個orconfig.h檔案。主要用於Tor原始碼的跨平臺編譯。
2)common:Tor原始碼中基本通用函式的封裝目錄。包括:OpenSSL,Libevent的封裝,TLS的封裝,連結串列和雜湊表的實現等。
3)tools:Tor使用工具目錄。包括:金鑰驗證,證書生成,域名解析和防火牆輔助的實現。
4)or:Tor核心程式邏輯目錄。(Tor系統所有核心協議的實現目錄)包括:客戶端身份核心程式碼,路由身份核心程式碼,目錄伺服器身份核心程式碼等。
5)test:Tor原始碼各功能模組的測試程式碼目錄。
6)config:配置檔案模板目錄
2. or目錄下的各檔案
or目錄是Tor原始碼的核心,有必要簡要介紹下該目錄下的檔案的主要作用:(細節部分後期會逐個檔案分別進行分析)
以下部分同時用於OP與OR:(Onion Proxy, Onion Router)
1)Buffers:緩衝區相關結構體及處理函式;
a generic interface buffer. Buffers are fairly opaque string holders that can read to or flush from: memory, file descriptors, or TLS connections.
2)Circuitbuild:鏈路建立相關函式;
The actual details of building circuits.
3)Circuitlist:鏈路列表及鏈路控制相關函式;
Manage the global circuit list.
4)Circuituse:鏈路獲取或使用相關函式;
Launch the right sort of circuits and attach streams to them.
5)Command:Tor Cell資料包的所有命令處理函式;
Functions for processing incoming cells.
6)Config:Tor系統配置選項處理函式;
Code to parse and interpret configuration files.
7)Connection:通用連線相關函式;
General high-level functions to handle reading and writing on connections.
8)Connection_edge:邊緣連線相關函式;(邊緣連線包括應用代理連線AP和出口連線EXIT)
Handle edge streams.
9)Connection_or:OR連線相關函式;
Functions to handle OR connections, TLS handshaking, and cells on the network.
10)Control:Tor系統控制相關函式;
Implementation for Tor's control-socket interface. See doc/spec/control-spec.txt for full details on protocol.
11)Cpuworker:多執行緒(加解密)處理函式;
Implements a farm of 'CPU worker' processes to perform CPU-intensive tasks in another thread or process, to not interrupt the main thread.
12)Directory:目錄伺服器連線相關操作函式;
Code to send and fetch directories and router descriptors via HTTP. Directories usedirserv.c to generate the results; clients use routers.c to parse them.
13)Geoip:Geoip功能相關函式;(一般用於Bridge使用者來自的國家統計)
Functions related to maintaining an IP-to-country database; to summarizing client connections by country to entry guards, bridges, and directory servers; and for statistics on answering network status requests.
14)Hibernate:休眠相關處理函式;
Functions to close listeners, stop allowing new circuits, etc in preparation for closing down or going dormant; and to track bandwidth and time intervals to know when to hibernate and when to stop hibernating.
15)Main:主程式
Toplevel module. Handles signals, multiplexes between connections, implements main loop, and drives scheduled events.
16)Microdesc:微描述符處理相關函式;
17)Networkstatus:網路狀態文件處理函式;
Functions and structures for handling network status documents as a client or cache.
18)Nodelist:路由節點列表相關處理函式;
19)Ntmain:nt服務相關函式;
20)Onion:Cell封裝處理相關函式;
Functions to queue create cells, and handle onionskin parsing and creation.
21)Policies:出入口策略相關函式;
Code to parse and use address policies and exit policies.
22)Reason:差錯原因相關函式;
Convert circuit, stream, and orconn error reasons to and/or from strings and errno values.
23)Relay:Relay Cell相關處理函式;
Handle relay cell encryption/decryption, plus packaging and receiving from circuits, plus queuing on circuits.
24)Rendcommon:Hidden Service通用程式碼;
Rendezvous implementation: shared code between introducers, services, clients, and rendezvous points.
25)Routerlist:路由資訊列表相關處理函式;
Code to maintain and access the global list of routerinfos for known servers.
26)Routerparse:路由描述符與目錄的解析與驗證相關函式;
Code to parse and validate router descriptors and directories.
27)Status:狀態儲存與秒記錄函式;
Keep status information and log the heartbeat messages.
28)Tormain:主函式介面;
Stub module containing a main() function. Allows unit test binary to link againstmain.c.
29)Transports:Pluggable Transports related code.
30)Rephist:歷史資訊與資料統計相關函式;
Basic history and "reputation" functionality to remember which servers have worked in the past, how much bandwidth we've been using, which ports we tend to want, and so on; further, exit port statistics, cell statistics, and connection statistics.
以下部分用於OR:(Onion Router)
1)Dirsev:目錄伺服器核心實現,生成及管理目錄內容;
Directory server core implementation. Manages directory contents and generates directories.
2)Dirvote:目錄伺服器投票與共識相關操作函式;
Functions to compute directory consensus, and schedule voting.
3)Dns:伺服器域名解析服務相關函式;
Implements a local cache for DNS results for Tor servers. This is implemented as a wrapper around Adam Langley'seventdns.c code. (We can't just use gethostbyname() and friends because we really need to be nonblocking.)
4)Rendmid:Hidden Service中間服務點實現程式碼;
Implement introductions points and rendezvous points.
5)Rendservice:Hidden Service服務功能程式碼;
The hidden-service side of rendezvous functionality.
6)Router:Onion Router核心實現;
OR functionality, including key maintenance, generating and uploading server descriptors, retrying OR connections.
以下部分用於OP:(Onion Proxy)
1)Dnsserv:客戶端域名解析代理伺服器相關函式;
Implements client-side DNS proxy server code. Note: this is the DNS Server code, not the Server DNS code. Confused? This code runs on client-side, and acts as a DNS server. The code indns.c, on the other hand, runs on Tor servers,
and acts as a DNS client.
2)Eventdns:暫略
3)Rendclient:Hidden Service客戶端程式碼;
Client code to access location-hidden services.
筆者暫時沒有全部查閱上述程式碼,如果有差錯的地方,請大家指點。