Tor源码分析一 — 使用工具集

  Tor系统是用于匿名通信的一个系统,源代码的维护到今天已经快10年。笔者从今年3月份开始陆陆续续研究Tor系统的源码,由于源码量大,也有很多需要总结的地方,所以特地在这里开辟个Tor源码分析的系列文章,供自己总结,也供大家共同探讨。

1. 源码查看工具 — SourceInsight

  本来Tor源码在Linux下分析和调试都可以比在Windows下更专业些,但是因为笔者本身大部分时间需要在Windows下工作,所以选择了Windows下的源码分析软件。SourceInsight是个非常好的源码分析软件,用于查看各种开源代码工程都非常方便。这里选用SourceInsight作为Tor源码的基本浏览和修改工具。

2. 文档生成工具 — Doxygen

  关于Doxygen的基本介绍大家可以在网上找到很多。简单的说,Doxygen就是将源码内的信息进行总结,生成包括函数调用关系,函数介绍等信息。这对于宏观查看代码非常重要。Tor源码内很多函数都连续嵌套非常多层,靠人为记住是非常困难的。利用Doxygen的函数调用图,能帮助我们更好的查看函数之间的调用关系。这里不介绍Doxygen如何使用,百度上有许多。注:使用Doxygen时定要打开函数调用图的生成功能。

3. 编译工具 — MinGW MSYS msyDTK

  为了在Windows上完成对Tor的编译,需要使用到下面一些列套件。好在这些部分在Tor的FAQ中已经很详细地说明如何使用,这里就仅仅将Tor中关于如何在WIndows下编译Tor源码的部分复制于此,请大家自行查看。

##
## Instructions for building Tor with MinGW (http://www.mingw.org/)
##

Stage One:  Download and Install MinGW.
---------------------------------------

Download mingw:
http://prdownloads.sf.net/mingw/MinGW-5.1.6.exe?download

Download msys:
http://prdownloads.sf.net/ming/MSYS-1.0.11.exe?download

Download msysDTK:
http://sourceforge.net/projects/mingw/files/MSYS%20Supplementary%20Tools/msysDTK-1.0.1/msysDTK-1.0.1.exe/download

Install MinGW, msysDTK, and MSYS in that order.  

Make sure your PATH includes C:\MinGW\bin.  You can verify this by right
clicking on "My Computer", choose "Properties", choose "Advanced",
choose "Environment Variables", select PATH.

Start MSYS(rxvt).

Create a directory called "tor-mingw".

Stage Two:  Download, extract, compile openssl
----------------------------------------------

Download openssl:
http://www.openssl.org/source/openssl-0.9.8l.tar.gz

Extract openssl:
Copy the openssl tarball into the "tor-mingw" directory.
Type "cd tor-mingw/"
Type "tar zxf openssl-0.9.8l.tar.gz"
(Note:  There are many symlink errors because Windows doesn't support
symlinks.  You can ignore these errors.)

Make openssl libraries:
Type "cd tor-mingw/openssl-0.9.8l/"
Type "./Configure -no-idea -no-rc5 -no-mdc2 mingw"
Edit Makefile and remove the "test:" and "tests:" sections.
Type "rm -rf ./test"
Type "cd crypto/"
Type "find ./ -name "*.h" -exec cp {} ../include/openssl/ \;"
Type "cd ../ssl/"
Type "find ./ -name "*.h" -exec cp {} ../include/openssl/ \;"
Type "cd .."
Type "cp *.h include/openssl/"
Type "find ./fips -type f -name "*.h" -exec cp {} include/openssl/ \;"
# The next steps can take up to 30 minutes to complete.
Type "make"
Type "make install"


Stage Three:  Download, extract, compile zlib
---------------------------------------------

Download zlib source:
http://www.zlib.net/zlib-1.2.3.tar.gz

Extract zlib:
Copy the zlib tarball into the "tor-mingw" directory
Type "cd tor-mingw/"
Type "tar zxf zlib-1.2.3.tar.gz"

CHOICE:

Make zlib.a:
Type "cd tor-mingw/zlib-1.2.3/"
Type "./configure"
Type "make"
Type "make install"

Done.


Stage Four: Download, extract, and compile libevent
------------------------------------------------------

Download the latest libevent release:
http://www.monkey.org/~provos/libevent/

Copy the libevent tarball into the "tor-mingw" directory.
Type "cd tor-mingw"

Extract libevent.

Type "./configure --enable-static --disable-shared"
Type "make"
Type "make install"

Stage Five:  Build Tor
----------------------

Download the current Tor alpha release source code from https://torproject.org/download.html.
Copy the Tor tarball into the "tor-mingw" directory.
Extract Tor:
Type "tar zxf latest-tor-alpha.tar.gz"

cd tor-
Type "./configure"
Type "make"

You now have a tor.exe in src/or/.  This is Tor.
You now have a tor-resolve.exe in src/tools/.

Stage Six:  Build the installer
-------------------------------

Install the latest NSIS:
http://nsis.sourceforge.net/Download

Run the package script in contrib:
From the Tor build directory above, run:
"./contrib/package_nsis-mingw.sh"

The resulting Tor installer executable is in ./win_tmp/.

  上述所有的工具及源码都下载,安装完毕之后,就可以进入Tor源码的细节分析阶段。不过,在此之前,如果可能,还需要通读Tor源码的设计文档:

  https://www.torproject.org/docs/documentation.html.en

  上述网址需要翻墙才可以阅读,所以,或许还需要使用VPN或者GoAgent的方式事先翻墙。

  重点必读的设计文档包括:,