Tor源码分析二 — 目录结构

  Tor由于经过了长年的开发,版本到现在已经有很多。笔者选用的是tor-0.2.3.25版本。关于版本变迁和更新说明,请大家自行查看:

  https://gitweb.torproject.org/tor.git (需翻墙)

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.

  笔者暂时没有全部查阅上述代码,如果有差错的地方,请大家指点。