[关闭]
@SmashStack 2017-05-03T18:10:48.000000Z 字数 34292 阅读 3413

无线网络攻防实验平台实验报告

Chore


实验人员: 张 倬
报告作者: 张 倬
学  号: 5140369057
完成时间: 2017 年 5 月 3 日

NS2 安装与配置

一、实验目的

  1. 掌握 Linux 环境下 NS2 软件的安装工作;
  2. 熟悉 NS2 环境变量的设置。

二、实验内容

1. Linux 上 NS2 必要工具和库文件的安装;
2. Linux 上 NS2 软件的安装;
3. NS2 环境变量的设置;
4. 安装结果的验证。

三、实验步骤及测试结果

  1. NS2 必要工具和库文件的安装
    • sudo apt install build-essential
    • sudo apt install tcl8.5 tcl8.5-dev tk8.5 tk8.5-dev
    • sudo apt install libxmu-dev libxmu-headers
      ns_apt.png-320.9kB
  2. NS2 的安装
  3. 设置环境变量
    • 打开~/.zshrc文件进行编辑,如下图所示(我 Ubuntu 使用的是 zsh,所以修改 .zshrc 文件):
      ns_zshrc.png-199kB
  4. 验证环境变量配置是否正确
    • ns2安装成功
      ns_execute.png-353.7kB
    • nam安装成功
      ns_nam_success.png-321.8kB

四、实验心得与体会

这次实验特别锻炼了我独立解决问题的能力,在编译 ns2 的过程中,我曾经遇到了如下的报错信息:
ns_error.png-332.5kB
在一筹莫展之际,我在网络上通过 Google 查找了相关文档的信息,最后发现问题是因为 c++ 语法新版和旧版语法不兼容,导致编译出错。考虑到我自身机器就是 Ubuntu,修改 GCC 显然不是明智的决定,于是我对源码进行了修改,经过多次测试以后解决了问题。这也正提醒了我对现实中遇到问题,更应该深入到细节,从根本上发现问题原因并解决。同时也让我更加深信网络资源合理运用的必要性与重要性。

Tcl 语言言网网络环境配置

一、实验目的

利用 tcl 语言建立简单的场景,满足:

二、实验内容

  1. 阅读和理解参考代码;
  2. 根据参考代码完成 circle.tcl 文件,接受用户输入 n ,建立通信场景,满足通信节点为圆上等距分布的 n 个点,且对角节点之间传送简单数据流。

三、实验步骤及测试结果

  1. # editing
  2. set val(stop) 100
  3. set val(tr) out.tr
  4. #设定模拟需要的一些属性
  5. set val(chan) Channel/WirelessChannel
  6. set val(prop) Propagation/TwoRayGround
  7. set val(netif) Phy/WirelessPhy
  8. set val(mac) Mac/802_11
  9. #将协议设置为 DSR 后,同时将队列设置为 CMUPriQueue
  10. set val(ifq) CMUPriQueue
  11. set val(ll) LL
  12. set val(ant) Antenna/OmniAntenna
  13. set val(ifqlen) 50
  14. #将结点个数预设为 0,待用户输入。此项要求用户一定输入,否则不执行模拟。
  15. set val(nn) 0
  16. set val(rp) DSR
  17. #场景大小缺省值为 1000*1000
  18. set val(x) 1000
  19. set val(y) 1000
  20. #圆的半径缺省值为 400
  21. set val(r) 300
  22. #该过程用于在屏幕上打印在终端输入 ns circle.tcl 后添加参数的格式
  23. proc usage {} {
  24. global argv0
  25. puts "\nusage: $argv0 \[-nn nodes\] \[-r r\] \[-x x\] \[-y y\]\n"
  26. puts "note: \[-nn nodes\] is essential, and the others are optional.\n"
  27. }
  28. #该过程用来根据用户的输入更改一些预设参数的值
  29. proc getval {argc argv} {
  30. # editing
  31. global val
  32. lappend vallist r x y z
  33. #argc 为参数的个数,argv 为整条参数构成的字符串
  34. for {set i 0} {$i < $argc} {incr i} {
  35. #变量 arg 为 argv 的第 i 部分,以空格为分界
  36. set arg [lindex $argv $i]
  37. #略过无字符“-”的字符串,一般是用户键入的数字
  38. #string range $arg m n 表示取字符串$arg 的第 m 个字符到第 n 个字符
  39. if {[string range $arg 0 0] != "-"} continue
  40. set name [string range $arg 1 end]
  41. #更改预设变量(结点个数,半径,场景大小)
  42. set val($name) [lindex $argv [expr $i+1]]
  43. }
  44. }
  45. #调用 getval 过程
  46. getval $argc $argv
  47. #用户没有输入参数,只键入了 ns circle.tcl,则结点个数认为 0
  48. if { $val(nn) == 0 } {
  49. #打印用法
  50. usage
  51. exit
  52. }
  53. #初始化全局变量
  54. set ns [new Simulator]
  55. #打开 Trace 文件
  56. set namfd [open ns1.nam w]
  57. $ns namtrace-all-wireless $namfd $val(x) $val(y)
  58. set tracefd [open $val(tr) w]
  59. $ns trace-all $tracefd
  60. #建立一个拓扑对象,以记录移动节点在拓扑内移动的情况
  61. set topo [new Topography]
  62. #拓扑范围为 1000m*1000m
  63. $topo load_flatgrid $val(x) $val(y)
  64. #创建物理信道对象
  65. set chan [new $val(chan)]
  66. #创建 God 对象
  67. set god [create-god $val(nn)]
  68. #设置移动节点属性
  69. $ns node-config -adhocRouting $val(rp) \
  70. -llType $val(ll) \
  71. -macType $val(mac)\
  72. -ifqType $val(ifq) \
  73. -ifqLen $val(ifqlen) \
  74. -antType $val(ant) \
  75. -propType $val(prop) \
  76. -phyType $val(netif)\
  77. -channel $chan \
  78. -topoInstance $topo \
  79. -agentTrace ON \
  80. -routerTrace ON \
  81. -macTrace OFF \
  82. -movementTrace ON
  83. for {set i 0} {$i < $val(nn)} {incr i} {
  84. set node_($i) [$ns node]
  85. }
  86. #设定移动节点的初始位置(提示:通过三角函数实现节点数未知情况下的节点位置设置)
  87. # editing
  88. set angle [expr 3.1415926535897932384626433832795021141979 * 2 / $val(nn)]
  89. for {set i 0} {$i < $val(nn)} {incr i} {
  90. $node_($i) set X_ [expr cos($angle * $i) * $val(r) + $val(r)]
  91. $node_($i) set Y_ [expr sin($angle * $i) * $val(r) + $val(r)]
  92. $node_($i) set Z_ 0
  93. }
  94. #新建一个 UDP Agent 并把它绑定到节点 1 上
  95. # editing
  96. set udp0 [new Agent/UDP]
  97. $ns attach-agent $node_(1) $udp0
  98. ##新建一个 CBR 流量发生器,设定分组大小为 500Byte,发送间隔为 5ms,然后绑定到 udp0 上
  99. # editing
  100. set cbr0 [new Application/Traffic/CBR]
  101. $cbr0 set packetSize_ 500
  102. $cbr0 set interval_ 0.005
  103. $cbr0 attach-agent $udp0
  104. #在 node_(1)节点沿直径对面的节点上建立一个数据接受器(对面节点号需要计算)
  105. # editing
  106. set faceid [expr ($val(nn) / 2 + 1) % $val(nn)]
  107. set null0 [new Agent/Null]
  108. $ns attach-agent $node_($faceid) $null0
  109. #将这两个 Agent 连接起来
  110. # editing
  111. $ns connect $udp0 $null0
  112. $ns at 0.5 "$cbr0 start"
  113. $ns at 4.5 "$cbr0 stop"
  114. #在 Nam 中定义节点初始所在位置
  115. for {set i 0 } {$i<$val(nn)} {incr i} {
  116. #只有定义了移动模型后,这个函数才能被调用
  117. $ns initial_node_pos $node_($i) 30
  118. }
  119. #定义节点模拟的结束时间
  120. # editing
  121. for {set i 0 } {$i<$val(nn)} {incr i} {
  122. $ns at $val(stop) "$node_($i) reset";
  123. }
  124. $ns at $val(stop) "stop"
  125. $ns at $val(stop) "puts \"NS EXITING...\"; $ns halt"
  126. #stop 函数
  127. proc stop {} {
  128. global ns tracefd namfd
  129. $ns flush-trace
  130. close $tracefd
  131. close $namfd
  132. exec nam ns1.nam &
  133. }
  134. $ns run

四、实验心得与体会

在实验过程中,首先我体会到了 Tcl 语言和我熟悉的语言的不同之处。Tcl 语言和 C 语言在语法上有着很大的差异性,有时候在短时间内无法习惯过来,不过后来发现和 Bash 脚本语言很相像,渐渐找到了书写的感觉。另一方面,模拟实验也让我生动地感受到了节点数据传输的原理,在通信数据的传输过程中,节点间都倾向于直接传递数据,所以在半径为五十的情况下节点一和节点五可以直接交互。但是如果节点间的距离过大,数据传输就会经过其他节点,正如半径为两百时的情况。而最后,像半径为五百时,当节点距离达到相邻节点都无法传输数据时,传输就会失败。

使用用CMU工工具配置一一个随机场景

一、实验目的

  1. 学习使用 CMU 工具生成随机节点场景及 CBR 数据流。
  2. 学习使用 gawk 工具分析模拟结果。

二、实验内容

  1. setdest 和 cbrgen 是 CMU(卡内基梅隆大学)开发的用于生成无线节点移动场景和随机 cbr 数据流的工具。例如 ./setdest -n 100 -p 5 -M 2 -t 3600 -x 1000 -y 1000 > my_scene.txt 的含义是生成一个 100 个节点以最大速度 2m/s 的速度在大小为 1000m*1000m 的区域内随机运动 1 小时(3600 秒)每到达一个地方停留 5 秒的无线节点移动的场景,场景文件重定向到文件 my_scene.txt 中。

  2. gawk 是用来分析 Trace 文件的工具。而使用 gawk 非常简单只需要将用 gawk 语言写好
    的用以分析对应的 Trace 文件的脚本放在 Trace 文件所在的目录下,通过命令“gawk –f xxx.awk xxx.tr”就可以使用 gawk 来分析 Trace 文件。其中,xxx.awk 就是用以分析 Trace 文件的 gawk 脚本。xxx.tr 就是所需要分析的 Trace 文件。

  3. 本次实验需要完成 random.tcl 文件和 RandomScene.tcl 文件,使用gawk对模拟结果进行分析。

三、实验步骤及测试结果

  1. set seed [exec ./RandomNumber] ;#get a random number as seed
  2. puts "seed is $seed"
  3. puts "creating 50 random nodes"
  4. # editing
  5. exec /home/izhuer/Tool/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen/setdest/setdest -n 50 -p 5 -M 2 -t 3600 -x 900 -y 900 > RandomDest.txt
  6. #create 50 random nodes by setdest, output to file RandomDest.txt; The route depends on different computers.
  7. puts "creatingnodes done"
  8. puts "creating random cbr stream"
  9. # editing
  10. exec ns /home/izhuer/Tool/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen/cbrgen.tcl -type cbr -nn 50 -seed $seed -mc 25 -rate 1.0 > RandomCbr.txt
  11. #create 50 nodes' random cbr stream by cbrgen.tcl, output to file RandomCbr.txt; The route depends on different computers.
  12. puts "creating cbr stream done\n"
  13. puts "-------------Simulation-------------"
  14. source RandomScene.tcl ; #run simulatior
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5. int main()
  6. {
  7. unsigned int num = (unsigned)time(NULL);
  8. cout<<num<<endl;
  9. return 0;
  10. }
  1. Phy/WirelessPhy set RXThresh_ 2.28289e-11 ;# set valid wirless connection distance is 500m
  2. set val(chan) Channel/WirelessChannel ;# channel type
  3. set val(prop) Propagation/TwoRayGround ;# radio-propagation model
  4. set val(netif) Phy/WirelessPhy ;# network interface type
  5. set val(mac) Mac/802_11 ;# MAC type
  6. set val(ifq) CMUPriQueue ;# interface queue type
  7. set val(ll) LL ;# link layer type
  8. set val(ant) Antenna/OmniAntenna ;# antenna model
  9. set val(ifqlen) 50 ;# max packet in ifq
  10. set val(nn) 50 ;# number of mobile nodes
  11. set val(rp) DSR ;# routing protocol
  12. set val(x) 900 ;# X dimensions of the topography
  13. set val(y) 900 ;# X dimensions of the topography
  14. set val(ncbr) 25 ;# number of cbr streams
  15. #set up a global instance of Simulator
  16. set ns_ [new Simulator]
  17. #open files to record the simulation results
  18. set tracefd [open RandomScene.tr w]
  19. $ns_ trace-all $tracefd
  20. set namtracefd [open RandomScene.nam w]
  21. $ns_ namtrace-all-wireless $namtracefd $val(x) $val(y)
  22. #a function called in proc finish to analyse trace and print result
  23. # editing
  24. proc analysis {} {
  25. puts ""
  26. puts "-------------Analysis-------------"
  27. puts [exec gawk -f three.awk RandomScene.tr]
  28. }
  29. #a function called at the end of the simulation to close the recording files and show the animation
  30. # editing
  31. proc finish {} {
  32. global ns_ tracefd namtracefd
  33. $ns_ flush-trace
  34. close $tracefd
  35. close $namtracefd
  36. analysis
  37. exec nam RandomScene.nam &
  38. $ns_ halt
  39. }
  40. #set up the topography of the scene
  41. set topo [new Topography]
  42. $topo load_flatgrid $val(x) $val(y)
  43. #set up a global instance of God
  44. set god_ [create-god $val(nn)]
  45. #configure all the nodes in the simulation using variables initialized at the beginning
  46. $ns_ node-config -addressType def \
  47. -adhocRouting $val(rp) \
  48. -llType $val(ll) \
  49. -macType $val(mac) \
  50. -ifqType $val(ifq) \
  51. -ifqLen $val(ifqlen) \
  52. -antType $val(ant) \
  53. -propType $val(prop) \
  54. -phyType $val(netif) \
  55. -channelType $val(chan) \
  56. -topoInstance $topo \
  57. -agentTrace ON \
  58. -routerTrace ON \
  59. -macTrace OFF \
  60. -movementTrace OFF
  61. #set up nodes and disable their functions of random motion
  62. for {set i 0} {$i<$val(nn)} {incr i} {
  63. set node_($i) [$ns_ node]
  64. $node_($i) random-motion 0
  65. }
  66. source RandomDest.txt
  67. source RandomCbr.txt
  68. #stop all the cbr streams from generating packets
  69. for {set i 0} {$i < $val(ncbr)} {incr i} {
  70. $ns_ at 50.0 "$cbr_($i) stop"
  71. }
  72. #reset all the nodes
  73. for {set i 0} {$i<$val(nn)} {incr i} {
  74. $ns_ at 50.0 "$node_($i) reset"
  75. }
  76. #call function "finish" to close files and show the animation
  77. $ns_ at 60.0 "finish"
  78. #run the simulation
  79. $ns_ run
  1. BEGIN {
  2. #设置初始变量
  3. num_D = 0; #丢包数
  4. num_s = 0; #发送包数
  5. num_r = 0; #收到包数
  6. rate_drop=0; #丢包率
  7. sum_delay=0; #总延迟时间
  8. average_delay=0; #平均延迟时间
  9. }
  10. {
  11. #读取 trace 文件记录
  12. event = $1; #第一列为包的操作(s 为发送包,r 为接受包)
  13. time = $2; #第二列为操作时间
  14. node=$3; #第三列为节点号
  15. trace_type = $4; #第四列为操作层
  16. flag = $5; #第五列为标志位
  17. uid = $6; #第六列为
  18. pkt_type = $7; #第七列为包类型
  19. pkt_size = $8; #第八列为包的大小
  20. #操作
  21. if (event== "s" && trace_type== "AGT" && pkt_type== "cbr")
  22. { send_time[uid]=time; #创建数组记录发包时间
  23. num_s++;#记录发送包总数
  24. }
  25. if (event== "r" && trace_type== "AGT" && pkt_type== "cbr")
  26. { delay[uid]=time-send_time[uid]; #创建数组记录延迟时间
  27. num_r++; #记录收到包总数
  28. }
  29. if (event== "D" && pkt_type== "cbr")
  30. delay[uid]=-1; #-1 表示包丢失, 该包不会记入延迟时间
  31. }
  32. END {
  33. #计算丢包数和丢包率
  34. num_D = num_s-num_r; #丢包总数
  35. rate_drop=num_D/num_s*100.0; #计算丢包率
  36. #计算延迟
  37. for(i=0;i<num_s;i++)
  38. {if(delay[i]>=0)
  39. sum_delay+=delay[i];
  40. }#总延迟时间
  41. average_delay=sum_delay/num_r;#平均延迟时间
  42. #打印结果
  43. printf("number of packets droped:%d \n",num_D);
  44. printf("number of packets sent:%d \n",num_s);
  45. printf("drop rate:%.3f%% \n",rate_drop );
  46. printf("average delay time:%.9f \n",average_delay );
  47. }
  1. seed is 1492075896
  2. creating 50 random nodes
  3. creatingnodes done
  4. creating random cbr stream
  5. creating cbr stream done
  6. -------------Simulation-------------
  7. num_nodes is set 50
  8. warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl
  9. INITIALIZE THE LIST xListHead
  10. channel.cc:sendUp - Calc highestAntennaZ_ and distCST_
  11. highestAntennaZ_ = 1.5, distCST_ = 550.0
  12. SORTING LISTS ...DONE!
  13. -------------Analysis-------------
  14. number of packets droped:0
  15. number of packets sent:184
  16. drop rate:0.000%
  17. average delay time:0.008996461

四、实验心得与体会

在这次实验的过程中,我发现丢包率与分组的发送率还是有着一定的关系的。在我们的实验数据中,我设置分组发送率为 1.0 时,丢包率为 0,而当我设置分组发送率为 100.0 时,我们得到了如下的实验结果:
three_100.0_loss.png-110kB
我们可以看到,这里有着大量的数据包丢失,然后我们在从 gawk 的数据中去分析:

  1. seed is 1492075746
  2. creating 50 random nodes
  3. creatingnodes done
  4. creating random cbr stream
  5. creating cbr stream done
  6. -------------Simulation-------------
  7. num_nodes is set 50
  8. warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl
  9. INITIALIZE THE LIST xListHead
  10. channel.cc:sendUp - Calc highestAntennaZ_ and distCST_
  11. highestAntennaZ_ = 1.5, distCST_ = 550.0
  12. SORTING LISTS ...DONE!
  13. -------------Analysis-------------
  14. number of packets droped:17766
  15. number of packets sent:23496
  16. drop rate:75.613%
  17. average delay time:0.719766833

我们会发现,丢包率已经达到了 76% 之高,这也从实验上证明了我们的猜测。也就是说,在显示生活中,我们移动通信还需要着重关注负载平衡,不能一味提高发送率。

在NS2中移植实现MFLood协议

一、实验目的

  1. 将 MFlood 路由协议添加到 NS-2 中(MFlood 协议是一个很简单的路由协议,每个节点在收到目的不是自己的包后就将其广播出去,但不会重复广播同一个包);
  2. 使用随机场景脚本进行测试

二、实验原理

一般而言, 在 NS-2 中移植协议主要有以下几步:

  1. 添加新的代码文件。这些文件就是对协议进行描述的一些都文件和源文件,录入 protocol.h 和 protocol.cc 等。我们可以在 ns-allinone-~ns/~ns 目录下新建一个 protocol 的文件夹来存放这些文件,有时我们也会将他们放置在已有的文件夹下。
  2. 修改 NS-2 原有的一些文件。由于新添加的协议会用到一些 NS-2 已有的类,结构或者变量,可能会对 NS-2 中一些原有的信息进行更改或者扩。例如,新的协议可能会有它自己的协议头和消息类型,那么需要对 NS-2 定义协议头和消息类型的那些文件进行修改,添加必要的内容。不同的协议需要修改的文件不同,这是由新协议需要描述的内容决定的。通常需要修改的文件有:定义消息类型和普通头部的文件 packet.h 、定义分组头部的文件 ns-packet.tcl 、定义默认参数的文件 ns-default.tcl 等。
  3. 修改 NS-2 的 Makefile 文件。我们编写完描述协议的代码以后,需要将它们整合到 NS-2 中,称为 NS-2 中的一部分。而修改 Makefile 文件就是整合的过程。

三、实验内容

  1. 添加新的代码文件
  2. 修改 NS-2 原有的一些文件
  3. 修改 NS-2 的 Makefile 文件
  4. 验证移植结果

四、实验步骤及测试结果

  1. ......
  2. static const packet_t PT_MFLOOD = 73;
  3. static packet_t PT_NTYPE = 74; // This MUST be the LAST one
  4. .....
  5. name_[PT_MFLOOD] = "MFlood";
  6. name_[PT_NTYPE]= "undefined";
  1. # Mobility, Ad-Hoc Networks, Sensor Nets:
  2. AODV # routing protocol for ad-hoc networks
  3. MFlood
  4. Diffusion # diffusion/diffusion.cc
  5. IMEP # Internet MANET Encapsulation Protocol, for ad-hoc networks
  6. MIP # Mobile IP, mobile/mip-reg.cc
  7. Smac # Sensor-MAC
  8. TORA # routing protocol for ad-hoc networks
  9. MDART # routing protocol for ad-hoc networks
  10. # AOMDV patch
  11. AOMDV
  1. ......
  2. AODV {
  3. set ragent [$self create-aodv-agent $node]
  4. }
  5. MFlood {
  6. set ragent [$self create-mflood-agent $node]
  7. }
  8. AOMDV {
  9. set ragent [$self create-aomdv-agent $node]
  10. }
  11. ......
  12. Simulator instproc create-aodv-agent { node } {
  13. # Create AODV routing agent
  14. set ragent [new Agent/AODV [$node node-addr]]
  15. $self at 0.0 "$ragent start" ;# start BEACON/HELLO Messages
  16. $node set ragent_ $ragent
  17. return $ragent
  18. }
  19. Simulator instproc create-mflood-agent { node } {
  20. set ragent [new Agent/MFlood [$node id]]
  21. $node set ragent_ $ragent
  22. return $ragent
  23. }
  1. ......
  2. aodv/aodv_logs.o aodv/aodv.o \
  3. aodv/aodv_rtable.o aodv/aodv_rqueue.o \
  4. mflood/mflood.o mflood/mflood-seqtable.o \
  5. aomdv/aomdv_logs.o aomdv/aomdv.o \
  6. aomdv/aomdv_rtable.o aomdv/aomdv_rqueue.o \
  7. ......
  1. # editing
  2. set val(stop) 100
  3. set val(tr) out.tr
  4. #设定模拟需要的一些属性
  5. set val(chan) Channel/WirelessChannel
  6. set val(prop) Propagation/TwoRayGround
  7. set val(netif) Phy/WirelessPhy
  8. set val(mac) Mac/802_11
  9. #将协议设置为 DSR 后,同时将队列设置为 CMUPriQueue
  10. set val(ifq) CMUPriQueue
  11. set val(ll) LL
  12. set val(ant) Antenna/OmniAntenna
  13. set val(ifqlen) 50
  14. #将结点个数预设为 0,待用户输入。此项要求用户一定输入,否则不执行模拟。
  15. set val(nn) 0
  16. set val(rp) MFlood
  17. ......

输出信息:

  1. ns two_circle.tcl -nn 8 -r 50 -x 100 -y 100
  2. num_nodes is set 8
  3. INITIALIZE THE LIST xListHead
  4. 5
  5. channel.cc:sendUp - Calc highestAntennaZ_ and distCST_
  6. highestAntennaZ_ = 1.5, distCST_ = 550.0
  7. SORTING LISTS ...DONE!
  8. NS EXITING...

弹出运行截图:
four.png-23.9kB

为了更加清晰地展示输出信息,我查看了节点 3 的 trace:

  1. grep "^[fr]*_3" out.tr
  2. ......
  3. r 5.598748669 _3_ RTR --- 430 cbr 520 [0 ffffffff 6 800] ------- [1:0 5:0 29 0] [430] 2 0
  4. f 5.599054615 _3_ RTR --- 431 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 29 0] [431] 1 0
  5. r 5.608621336 _3_ RTR --- 426 cbr 520 [0 ffffffff 7 800] ------- [1:0 5:0 29 0] [426] 2 0
  6. r 5.613315438 _3_ RTR --- 430 cbr 520 [0 ffffffff 0 800] ------- [1:0 5:0 29 0] [430] 2 0
  7. r 5.618009591 _3_ RTR --- 429 cbr 520 [0 ffffffff 4 800] ------- [1:0 5:0 29 0] [429] 2 0
  8. r 5.622763827 _3_ RTR --- 430 cbr 520 [0 ffffffff 2 800] ------- [1:0 5:0 29 0] [430] 2 0
  9. r 5.632212298 _3_ RTR --- 432 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 30 0] [432] 1 0
  10. r 5.636946498 _3_ RTR --- 430 cbr 520 [0 ffffffff 4 800] ------- [1:0 5:0 29 0] [430] 2 0
  11. r 5.641661012 _3_ RTR --- 429 cbr 520 [0 ffffffff 7 800] ------- [1:0 5:0 29 0] [429] 2 0
  12. f 5.642938569 _3_ RTR --- 432 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 29 0] [432] 1 0
  13. r 5.646375114 _3_ RTR --- 431 cbr 520 [0 ffffffff 0 800] ------- [1:0 5:0 29 0] [431] 2 0
  14. r 5.651109169 _3_ RTR --- 431 cbr 520 [0 ffffffff 2 800] ------- [1:0 5:0 29 0] [431] 2 0
  15. r 5.655803683 _3_ RTR --- 431 cbr 520 [0 ffffffff 6 800] ------- [1:0 5:0 29 0] [431] 2 0
  16. r 5.660557919 _3_ RTR --- 433 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 30 0] [433] 1 0
  17. r 5.670330585 _3_ RTR --- 431 cbr 520 [0 ffffffff 7 800] ------- [1:0 5:0 29 0] [431] 2 0
  18. r 5.675024688 _3_ RTR --- 432 cbr 520 [0 ffffffff 0 800] ------- [1:0 5:0 29 0] [432] 2 0
  19. f 5.675377768 _3_ RTR --- 433 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 29 0] [433] 1 0
  20. r 5.679718841 _3_ RTR --- 432 cbr 520 [0 ffffffff 7 800] ------- [1:0 5:0 29 0] [432] 2 0
  21. r 5.684492943 _3_ RTR --- 431 cbr 520 [0 ffffffff 4 800] ------- [1:0 5:0 29 0] [431] 2 0
  22. r 5.689307457 _3_ RTR --- 433 cbr 520 [0 ffffffff 0 800] ------- [1:0 5:0 29 0] [433] 2 0
  23. r 5.694021512 _3_ RTR --- 432 cbr 520 [0 ffffffff 2 800] ------- [1:0 5:0 29 0] [432] 2 0
  24. r 5.698755748 _3_ RTR --- 432 cbr 520 [0 ffffffff 4 800] ------- [1:0 5:0 29 0] [432] 2 0
  25. r 5.703450164 _3_ RTR --- 437 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 30 0] [437] 1 0
  26. r 5.713262830 _3_ RTR --- 433 cbr 520 [0 ffffffff 7 800] ------- [1:0 5:0 29 0] [433] 2 0
  27. r 5.717976932 _3_ RTR --- 433 cbr 520 [0 ffffffff 4 800] ------- [1:0 5:0 29 0] [433] 2 0
  28. f 5.718399492 _3_ RTR --- 437 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 29 0] [437] 1 0
  29. r 5.722771446 _3_ RTR --- 437 cbr 520 [0 ffffffff 0 800] ------- [1:0 5:0 29 0] [437] 2 0
  30. r 5.727465502 _3_ RTR --- 443 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 30 0] [443] 1 0
  31. f 5.729130036 _3_ RTR --- 443 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 29 0] [443] 1 0
  32. r 5.732179882 _3_ RTR --- 437 cbr 520 [0 ffffffff 6 800] ------- [1:0 5:0 29 0] [437] 2 0
  33. r 5.741588548 _3_ RTR --- 437 cbr 520 [0 ffffffff 7 800] ------- [1:0 5:0 29 0] [437] 2 0
  34. r 5.746282651 _3_ RTR --- 437 cbr 520 [0 ffffffff 4 800] ------- [1:0 5:0 29 0] [437] 2 0
  35. r 5.750997067 _3_ RTR --- 443 cbr 520 [0 ffffffff 6 800] ------- [1:0 5:0 29 0] [443] 2 0
  36. r 5.755711220 _3_ RTR --- 433 cbr 520 [0 ffffffff 2 800] ------- [1:0 5:0 29 0] [433] 2 0
  37. r 5.760505455 _3_ RTR --- 444 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 30 0] [444] 1 0
  38. f 5.763578769 _3_ RTR --- 444 cbr 520 [0 ffffffff 1 800] ------- [1:0 5:0 29 0] [444] 1 0
  39. r 5.765319655 _3_ RTR --- 443 cbr 520 [0 ffffffff 0 800] ------- [1:0 5:0 29 0] [443] 2 0
  40. r 5.770033891 _3_ RTR --- 444 cbr 520 [0 ffffffff 6 800] ------- [1:0 5:0 29 0] [444] 2 0
  41. r 5.774788044 _3_ RTR --- 443 cbr 520 [0 ffffffff 7 800] ------- [1:0 5:0 29 0] [443] 2 0
  42. r 5.794816998 _3_ RTR --- 444 cbr 520 [0 ffffffff 4 800] ------- [1:0 5:0 29 0] [444] 2 0
  43. r 5.804305614 _3_ RTR --- 444 cbr 520 [0 ffffffff 0 800] ------- [1:0 5:0 29 0] [444] 2 0
  44. r 5.809059669 _3_ RTR --- 444 cbr 520 [0 ffffffff 2 800] ------- [1:0 5:0 29 0] [444] 2 0

我们可以看到,无论是从模拟开始到结束,当模拟场景中的节点收到每个节点在收到目的不是自己的包后就将其广播出去,但同时,它们不会重复广播同一个包。说明 MFlood 协议的移植成功。

五、实验心得与体会

MFlood 洪泛协议是一个简单的无线路由协议,其中基本的思想是:节点根据一定的规则转发自己收到的数据包。协议很简单,协议的移植也不是一个复杂的过程,但是却让我体会到了每一种协议有着各自的长处:洪泛协议虽然简单,看似对各种网络都很有用,但是试考虑对于对于洪泛协议,如果我伪造了一个目的地不存在的报文,那么是不是这个报文会在网络中不停的传递,造成整个网络负载的增大。这只是我的一个猜测,但正是因为这样思维的过程让我对整个洪泛协议有了进一步深入的了解。感谢老师为我们提供这样一个实验机会,也正是因为亲身实践了,我才对这个协议的整个流程有了一些概念性的了解。

在 NS2 协议栈中添加 mac 协议

一、实验目的

  1. 学习在 ns2 上如何添加一个新的协议(此处定义为 lmac,实现 smac 协议的全部功能)
  2. 通过在 ns2 仿真我们添加的 lmac 协议,深入理解无线传感网络 smac 协议

二、实验原理

smac 协议简介:

  1. 什么是 MAC?即 medium access control,对信道访问的控制协议。 MAC 可以大体分为两大类,scheduled‐based and contention‐based,基于调度的和基于竞争的。scheduled‐based protocol由中心节点分配信道访问,每个节点只能在分得的时刻或者空间访问信道,如 TDMA,CDMA 等就是这样一种协议。contention‐based protocols 是各个节点竞争信道的访问权,竞争胜出者方可访问信道,CSMA 是其典型代表。这两种协议方式各有优缺点,花开两朵,各表一枝。基于调度的以后再论,先说这基于竞争的。主要特点有:

    • 拓扑变化具有适应性,具有可扩展性。
    • 用于 multi‐hop 网络。
    • 不需要严格同步。
    • 会发生碰撞。
    • 存在较为严重的隐藏终端问题(idden terminal problems)。
  2. SMAC 就是一种基于竞争的协议,但是又因为它适应于 WSN,与传统的无线网络协议有所不同。Energy conservation and self‐configuation are primary goals,while per‐node fairness and latency are less important。要节约能耗,必须先了解能量浪费的来源:

    • ollision,碰撞, 当节点 B 在和节点 A 通信时,不可以和节点 C 同时通信,否则和节点 A 的通信也会遭到破坏,这就是碰撞,碰撞要求重传,重传会引发能耗消耗。
    • overheading,串音。当节点 A收到发往其他节点发往节点 B 的数据包时,做了无谓的接受工作。
    • control packet overhead,控制包的消耗,由于需要大量 RTS,CTS,ACK 等控制包来解决隐藏终端和保证服务质量,所以这些控制包的能量消耗也是很重要的一部分。
    • Idle listening,信道监听。
  3. SMAC 采用的机制有周期性的监听和休眠。摒弃传统无线网络节点要么在传输数据要么在监听的特点,采用周期性的休眠机制,使得节点可以减少无谓的监听,在监听时间段内若有数据需要传输则进行数据传输,否则监听时间到后进入休眠这大大节约了能耗,但是也造成了延迟 latency,并且需要同步。

  4. 休眠机制的选择。当一个节点广播一个休眠机制时寄希望全网都会采用这个机制,然而由于不同节点可能同时发出机制,所以网内存在几种不同的休眠机制,可采用时间戳和优先级的机制尽可能使得网络同步于一种机制,当然在边界节点(border nodes)可以采用多种机制,可是这样会加快边界节点的能量消耗,不过边界节点的存在对网络拓扑变化有积极的影响,这是因为他们能比其他节点相对更快的检测到新节点的加入。

  5. collision and overheading avoidance,碰撞和串音避免。首先,节点是如何知道网络空闲与否,这里有两种机制,一种是收到其他节点通信的信息,从中可以判断他们通信的时间,故可以设置 NAV,每过一个时间,NAV 减 1,直到为 0,就可判断信道可能空闲。另一种是物理层检测信道信号的强弱来判定,有研究发现检测噪声也可以判断信道的情况。SMAC 采用了碰撞窗口 contention window 来解决碰撞,采用 RTS/CTS 机制来缓解隐藏终端问题和解决串音。有这样原则:相互通信节点 AB 的一跳邻居都不可以在 AB 通信时进行通信。

  6. message passing,数据传输。当需要传输的数据较长时,如果一旦丢失,则需要重传浪费大量能耗。若将其分成小段传输,则需要加入大量控制包。SMAC 采用把数据分成小段并采用一个 RTS /CTS 的方式集中传输数据,当数据丢失时,不同于 802.11 放弃信道重新竞争以保证用户公平性的方法,而是继续占有信道进行重传。这虽然破坏了用户的公平性,但是维护了整体的公平性。

三、实验内容

  1. 添加新的协议;
  2. 验证协议是否正确添加成功。

四、实验步骤及测试结果

  1. cp smac.cc lmac.cc
  2. cp smac.h lmac.h
  1. vim 打开两个文件,在 vim 中执行命令
  2. :%s/smac/lmac/g
  3. :%s/Smac/Lmac/g
  1. ......
  2. #define HDR_SMAC(p) ((hdr_smac *)hdr_mac::access(p))
  3. #define HDR_LMAC(p) ((hdr_lmac *)hdr_mac::access(p))
  4. ....
  5. // SMAC packet
  6. PT_SMAC,
  7. // LMAC packet
  8. PT_LMAC,
  9. ...
  1. .....
  2. # Turning on/off sleep‐wakeup cycles for SMAC
  3. Mac/SMAC set syncFlag_ 1
  4. # Nodes synchronize their schedules in SMAC
  5. Mac/SMAC set selfConfigFlag_ 1
  6. # Default duty cycle in SMAC
  7. Mac/SMAC set dutyCycle_ 10
  8. #add LMAC here
  9. # Turning on/off sleep‐wakeup cycles for LMAC
  10. Mac/LMAC set syncFlag_ 1
  11. # Nodes synchronize their schedules in LMAC
  12. Mac/LMAC set selfConfigFlag_ 1
  13. # Default duty cycle in LMAC
  14. Mac/LMAC set dutyCycle_ 10
  15. .....
  16. # Turning on/off sleep‐wakeup cycles for SMAC
  17. Mac/SMAC set syncFlag_ 0
  18. # Turning on/off sleep‐wakeup cycles for LMAC
  19. Mac/LMAC set syncFlag_ 0
  1. .....
  2. Lmac # A new Sensor‐MAC
  3. ....
  1. ......
  2. void format_smac(Packet *p, int offset);
  3. void format_lmac(Packet *p, int offset);
  4. ......
  1. #include<smac.h>
  2. #include<lamc.h> // 新加语句
  3. ......
  4. struct hdr_cmn *ch = HDR_CMN(p);
  5. struct hdr_ip *ih = HDR_IP(p);
  6. struct hdr_mac802_11 *mh;
  7. struct hdr_smac *sh;
  8. struct hdr_lmac *ph; // 新添加一个指向 lamc 包的指针
  9. ......
  10. char mactype[SMALL_LEN];
  11. strcpy(mactype, Simulator::instance().macType());
  12. if (strcmp (mactype, "Mac/SMAC") == 0)
  13. sh = HDR_SMAC(p);
  14. else if (strcmp (mactype,"Mac/LMAC") == 0) // 判断是不是 LMC 包
  15. ph = HDR_LMAC(p);
  16. else
  17. mh = HDR_MAC802_11(p);
  18. ......
  19. if (strcmp (mactype, "Mac/SMAC") == 0) {
  20. format_smac(p, offset);
  21. }
  22. else if (strcmp (mactype, "Mac/LMAC") == 0) { //新添语句
  23. format_lmac(p, offset);
  24. }
  25. else {
  26. format_mac(p, offset);
  27. }
  28. return;
  29. ......
  30. // mac layer extension
  31. offset = strlen(pt_‐>buffer());
  32. if (strcmp(mactype, "Mac/SMAC") == 0) {
  33. format_smac(p, offset);
  34. }
  35. else if (strcmp(mactype, "Mac/LMAC") == 0) { //新添语句
  36. format_lmac(p, offset);
  37. }
  38. else {
  39. format_mac(p, offset);
  40. }
  41. ......
  42. (ch‐>ptype() == PT_SMAC) ? (
  43. (sh‐>type == RTS_PKT) ? "RTS" : //新添语句
  44. (sh‐>type == RTS_PKT) ? "RTS" :
  45. (sh‐>type == CTS_PKT) ? "CTS" :
  46. (sh‐>type == ACK_PKT) ? "ACK" :
  47. (sh‐>type == SYNC_PKT) ? "SYNC" : "UNKN") :
  48. p acket_info.name(ch‐>ptype())), ch‐>size());
  49. ......
  50. void CMUTrace::format_lmac(Packet *p, int offset) //加入函数
  51. {
  52. struct hdr_lmac *ph = HDR_LMAC(p);
  53. sprintf(pt_‐>buffer() + offset, " [%.2f %d %d] ",
  54. ph‐>duration,
  55. ph‐>dstAddr,
  56. ph‐>srcAddr);
  57. }
  58. .....
  59. switch(ch‐>ptype()) {
  60. case PT_MAC:
  61. case PT_SMAC:
  62. case PT_LMAC: //这是新添加的 LMAC 协议
  63. break;
  64. case PT_ARP:
  65. format_arp(p, offset);
  66. break;
  1. ......
  2. mac/mac802_3.o mac/mactdma.o mac/smac.o mac/lmac.o\
  3. ......
  1. cd ~/Tools/ns-allinone-2.35/
  2. sudo ./install

用新编译出来的 ns 对 lmac.tcl 进行运行,运行 nam 如下:
five.png-24kB

运行的命令行结果如下:
five_0.png-86.5kB

另外,运行 smac.tcl,将生成的两个 trace 文件进行 diff 比对,发现文件一致,说明 lmac 协议添加成功:
five_diff.png-35.6kB

五、实验心得与体会

这次实验我遇到了不小的问题,主要在于 Lmac 和 Smac 这两个代码容易混淆,经常编译通过以后不能正常运行 lamc.tcl 文件,反反复复很多次以后终于通过了编译。另外一个遇到的问题是按照教材里说的:

  1. make clean
  2. make depend
  3. make

每次都无法正常通过编译,最后我是通过直接 install 整个文件去解决的这个问题的。好在有了前面移植 Mflood 协议的经验,我才顺利的完成了这个实验。

另外,通过 Smac 的协议讲解,我也意识到了这是一个非常节省网络通讯压力的协议。在 Smac 下,每个节点不必一直处于监听状态,就从监听这个方面减轻了网路的负载。但是反过来,这也导致了网络的延迟,有利必有弊,我从 MFlood 到 Smac 中深刻认识到网络协议的发展就是不断地在性能和效率中权衡取最优值,最终目标就是达到一个适合整个网络的平衡态。

无线自组织网络 AODV 协议的仿真

一、实验目的

在 NS-2 仿真 AODV 协议,并在 trace 文件中了解 AODV 协议的原理。

二、实验原理

AODV 是由 Nokia 研究中心的 CharlesE.Perkins 和加利福尼亚大学 SantaBarbara的 ElizabethM.Belding-Roryer 以及 Cincinnati 大学 SamirR.Das 等共同开发,已经被IETFMANET 工作组于 2003 年 7 月正式公布为自组网路由协议的 RFc 标准。AODV实质上就是 DSR 和 DSDV 的综合,它借用了 DSR 中路由发现和路由维护的基础程序,及 DSDV 的逐跳 (Hop-by-Hop)路由、目的节点序列号和路由维护阶段的周期更新机制,以 DSDV 为基础,结 合 DSR 中的按需路由思想并加以改进。

AODV 在每个中间节点隐式保存了路由请求和应答的结果,并利用扩展环搜索的办法 来限制搜索发现过的目的节点的范围。AODV 支持组播功能,支持 QoS,而且 AODV 中可 以使用 IP地址,实现同 Internet 连接,但是不支持单向信道。和 DSDV 保存完整的路由表 不同的是,AODV 通过建立基于按需路由来减少路由广播的次数,这是 AODV 对 DSDV的 重要改进。和 DSR 相比,AODV 的好处在于源路由并不需要包括在每一个数据分组中,这样会使路由协议的开销有所降低。AODV 是一个纯粹的按需路由系统,那些不在路径内的 节点不保存路由信息,也不参与路由表的交换。AODV 协议可以实现在移动终端间动态的、 自发的路由,使移动终端很快获得通向所需目的的路由,同时又不用维护当前没有使用的路 由信息,并且还能很快对断链的拓扑变化做出反应。AODV 的操作是无环路的,在避免了 通常 Bellman-ford 算法的无穷计数问题的同时,还提供了很快的收敛速度。AODV 的路由表 中每个项都使用了目的序列号(DestinationSequenceNumber)。目的序列号是目的节点创建, 并在发给发起节点的路由信息中使用的。使用目的序列号可以避免环路的发生。

AODV 使用 3 种消息作为控制信息:RouteRequest(RREQ),RouteReply(RREP)和RouteError(RERR)。这些消息都在 UDP 上使用 654 端口号。

当源节点需要和目的节点通信时,如果在路由表中已经存在了对应的路由时,AODV 不会进行任何操作。当源节点需要和新的目的通信时,它就会发起路由发现过程,通过广播 RREQ 信息来查找相应路由。当这个 RREQ 到达目的节点本身,或者是一个拥有足够新的 到目的节点路由的中间节点时,路由就可以确定了。所谓“足够新”就是通过目的序列号来 判断的。目的节点或中间节点通过原路返回一个 RREP 信息来向源节点确定路由的可用性。在维护路由表的过程中,当路由不再被使用时,节点就会从路由表中删除相应的项。同时, 节点会监视一个活动路由(activeroute,有限跳的,可用于数据转发的路由表)中,下一跳节点的状况。当发现有链路断开的情况时,节点就会使用 RERR 通知上游的节点,而上游的 节点就会使用该 RERR 分组拷贝通知更上游的节点。在 RERR消息中,指明了由于断链而导致无法达到目的节点。每个节点都保留了一个“前驱列表”(precursorlist)来帮助完成错误 报告的功能,这个列表中保存了把自己作为到当前不可达节点的下一跳的相邻节点(可以通 过记录 RERR 很容易地获得)。在路由表中,针对每一个表项,需要记录相应的的特征内容。 其中,序列号是防止路由环路的关键所在。当发生断链时,通过增加序列号和度量值(跳数) 来使路由表项无效。

三、实验内容

四、实验步骤及测试结果

run.tcl

  1. set seed [exec ./RandomNumber] ;#get a random number as seed
  2. puts "seed is $seed"
  3. puts "creating 50 random nodes"
  4. # editing, 更改了节点数量和场景大小
  5. exec /home/izhuer/Tool/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen/setdest/setdest -n 50 -p 5 -M 2 -t 3600 -x 100 -y 100 > RandomDest.txt
  6. #create 50 random nodes by setdest, output to file RandomDest.txt; The route depends on different computers.
  7. puts "creatingnodes done"
  8. puts "creating random cbr stream"
  9. # editing
  10. exec ns /home/izhuer/Tool/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen/cbrgen.tcl -type cbr -nn 50 -seed $seed -mc 25 -rate 1.0 > RandomCbr.txt
  11. #create 50 nodes' random cbr stream by cbrgen.tcl, output to file RandomCbr.txt; The route depends on different computers.
  12. puts "creating cbr stream done\n"
  13. puts "-------------Simulation-------------"
  14. source RandomScene.tcl ; #run simulatior

RandomScene.tcl

  1. Phy/WirelessPhy set RXThresh_ 2.28289e-11 ;# set valid wirless connection distance is 500m
  2. set val(chan) Channel/WirelessChannel ;# channel type
  3. set val(prop) Propagation/TwoRayGround ;# radio-propagation model
  4. set val(netif) Phy/WirelessPhy ;# network interface type
  5. set val(mac) Mac/802_11 ;# MAC type
  6. set val(ifq) CMUPriQueue ;# interface queue type
  7. set val(ll) LL ;# link layer type
  8. set val(ant) Antenna/OmniAntenna ;# antenna model
  9. set val(ifqlen) 50 ;# max packet in ifq
  10. set val(nn) 50 ;# number of mobile nodes
  11. set val(rp) AODV ;# routing protocol 添加 AODV 协议
  12. set val(x) 100 ;# X dimensions of the topography 更改大小
  13. set val(y) 100 ;# X dimensions of the topography 更改大小
  14. set val(ncbr) 25 ;# number of cbr streams
  15. #set up a global instance of Simulator
  16. set ns_ [new Simulator]
  17. #open files to record the simulation results
  18. set tracefd [open RandomScene.tr w]
  19. $ns_ trace-all $tracefd
  20. set namtracefd [open RandomScene.nam w]
  21. $ns_ namtrace-all-wireless $namtracefd $val(x) $val(y)
  22. #a function called in proc finish to analyse trace and print result
  23. # editing
  24. proc analysis {} {
  25. puts ""
  26. puts "-------------Analysis-------------"
  27. puts [exec gawk -f three.awk RandomScene.tr]
  28. }
  29. #a function called at the end of the simulation to close the recording files and show the animation
  30. # editing
  31. proc finish {} {
  32. global ns_ tracefd namtracefd
  33. $ns_ flush-trace
  34. close $tracefd
  35. close $namtracefd
  36. analysis
  37. exec nam RandomScene.nam &
  38. $ns_ halt
  39. }
  40. #set up the topography of the scene
  41. set topo [new Topography]
  42. $topo load_flatgrid $val(x) $val(y)
  43. #set up a global instance of God
  44. set god_ [create-god $val(nn)]
  45. #configure all the nodes in the simulation using variables initialized at the beginning
  46. $ns_ node-config -addressType def \
  47. -adhocRouting $val(rp) \
  48. -llType $val(ll) \
  49. -macType $val(mac) \
  50. -ifqType $val(ifq) \
  51. -ifqLen $val(ifqlen) \
  52. -antType $val(ant) \
  53. -propType $val(prop) \
  54. -phyType $val(netif) \
  55. -channelType $val(chan) \
  56. -topoInstance $topo \
  57. -agentTrace ON \
  58. -routerTrace ON \
  59. -macTrace OFF \
  60. -movementTrace OFF
  61. #set up nodes and disable their functions of random motion
  62. for {set i 0} {$i<$val(nn)} {incr i} {
  63. set node_($i) [$ns_ node]
  64. $node_($i) random-motion 0
  65. }
  66. source RandomDest.txt
  67. source RandomCbr.txt
  68. #stop all the cbr streams from generating packets
  69. for {set i 0} {$i < $val(ncbr)} {incr i} {
  70. $ns_ at 50.0 "$cbr_($i) stop"
  71. }
  72. #reset all the nodes
  73. for {set i 0} {$i<$val(nn)} {incr i} {
  74. $ns_ at 50.0 "$node_($i) reset"
  75. }
  76. #call function "finish" to close files and show the animation
  77. $ns_ at 60.0 "finish"
  78. #run the simulation
  79. $ns_ run
序号 数据名称 数据内容
1 事件类型 s(发送事件) r(接收事件) d(丢弃事件) f(转发事件)
2 时间 产生的时间
3 处理该事件的节点 ID
4 Trace 种类 RTR:路由器 / AGT:代理 / MAC:MAC 层
5:6 分隔符 -
7 分组 ID
8 分组类型 -
9 分组大小 -
10 发送节点在无线信道上发送该分组所期望的时间值 -
11 接收节点的 MAC 地址 MAC 地址
12 发送节点的 MAC 地址 MAC 地址
13 MAC 层封装的分组类型 0x800:IP 分组 / 0x806:ARP 分组
14:16 分隔符 -
17 分组发送的源 IP 地址 节点号.端口号
18 分组发送的目的 IP 地址 节点号.端口号
19 分组的 TTL 值 TTL
20 源节点到目的节点的跳数 跳数

对某一个固定分组的消息进行追踪,可以通过 linux 下的 grep 正则匹配来识别,经过查看分析以及查阅网上的资料,可以看到 AODV 有如下特点:

  1. 使用目的序列号来避免路由环路,避免了无限计数的问题
  2. 网络具有可拓展性;
  3. 支持中间节点应答,能使源节点快速获得路由,有效减少了广播数;
  4. 快速响应活跃路径上断链;需要周期性地广播分组;
  5. 不活跃的路由在一定时间后会被删除。

五、实验心得与体会

首先想要说的是这个实验其实我投机取巧了,本来 adov.tcl 应该是一个蛮大的编写量的,但是想到实验三有写过极为类似的脚本就重用了之前的代码,着实减轻了不少的负担。

另一个方面,这个实验也让我对 trace 文件有了更深入的了解。在实验三中我只是依样画葫芦地使用 gawk 脚本对 trace 文件进行分析,在这个实验中我自己去理解并阅读了 trace 文件,对整个文件结构也就有了更深入的理解,对未来我更进一步地学习 ns-2 打了一个很好的基础。

同时,这个实验也让我了解到了 AODV 路由协议的特点,通过距离矢量路由协议,建立起了虚电路,从而搭建起整个路由网络,虽然开始建立的时候会花一些时间,但是后面效率很高,正如我之前说的一样,网络协议的追求是平衡性的越来越完善。也正是这个实验,给了我一个机会去温习并回忆计算机网络当时学习到的知识,感觉收益颇丰。

黑洞攻击实验

一、实验目的

通过本实验,掌握主动黑洞、被动攻击的原理与实现方法。

二、实验原理

  1. 被动型黑洞:

我们将一般意义上的黑洞节点称为被动型黑洞,这种攻击者转发经过自己的路由报文,但丢弃所有的数据报文,从而达到路由欺骗的目的。由于没有向网络注入虚假报文,仅仅对网络拓扑进行攻击,因此,这种在其它文献中经常提到的黑洞攻击是一种被动的路由扰乱型攻击。

  1. 主动型黑洞:

相对应地,我们定义了主动型黑洞,这是一种“改良”的黑洞节点,在被动黑洞的基础上,冒充目的结点,提前回复 RREQ 报文,对外界宣称自己就是目的节点,以达到“主动出击”的目的,从而吸引更多的数据包。因此,这种攻击属于主动的路由扰乱型攻击。

三、实验内容

因为主动型黑洞是在被动黑洞的基础上构造出来的,于是我决定实现主动型黑洞,并自己构建出一个场景证明黑洞的成功构建。

四、实验步骤及测试结果

我主要对 ns-2.35/aodv/aodv.cc 中的文件进行了修改
首先加入对攻击节点的判断,通过引入一个 hacker 字段定义一个黑洞攻击节点:
seven_code1.png-70.8kB

接下来在 AODV 的构造函数中加入一个 malicious 变量标志恶意报文:
seven_code2.png-50.8kB

在 ADOV::rt_resolve 中,对黑洞攻击的恶意报文进行处理丢弃:
image_1bf7m08jf1t941767kb11a6gvu21k.png-71.2kB

在 ADOV::recvRequest 中对恶意报文的源头进行混淆:
image_1bf7mar9p1goeaqe99e16dd19nt21.png-82.1kB

最后,更新 aodv.h:
image_1bf7mdpov18pf1ttbgrctrq892e.png-82.1kB

这样一来,我们就实现了对一个黑洞节点的定义和黑洞报文的实现,接下来我们需要完善一个 balckhole.tcl 去对攻击场景进行模拟:

  1. #===================================
  2. # Simulation parameters setup
  3. #===================================
  4. set val(chan) Channel/WirelessChannel ;# channel type
  5. set val(prop) Propagation/TwoRayGround ;# radio-propagation model
  6. set val(netif) Phy/WirelessPhy ;# network interface type
  7. set val(mac) Mac/802_11 ;# MAC type
  8. set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
  9. set val(ll) LL ;# link layer type
  10. set val(ant) Antenna/OmniAntenna ;# antenna model
  11. set val(ifqlen) 50 ;# max packet in ifq
  12. set val(nn) 7 ;# number of mobilenodes
  13. set val(rp) AODV ;# routing protocol
  14. set val(x) 800 ;# X dimension of topography
  15. set val(y) 541 ;# Y dimension of topography
  16. set val(stop) 100.0 ;# time of simulation end
  17. #===================================
  18. # Initialization
  19. #===================================
  20. #Create a ns simulator
  21. set ns [new Simulator]
  22. #Setup topography object
  23. set topo [new Topography]
  24. $topo load_flatgrid $val(x) $val(y)
  25. create-god $val(nn)
  26. #Open the NS trace file
  27. set tracefile [open blackhole.tr w]
  28. $ns trace-all $tracefile
  29. #Open the NAM trace file
  30. set namfile [open blackhole.nam w]
  31. $ns namtrace-all $namfile
  32. $ns namtrace-all-wireless $namfile $val(x) $val(y)
  33. set chan [new $val(chan)];#Create wireless channel
  34. #===================================
  35. # Mobile node parameter setup
  36. #===================================
  37. $ns node-config -adhocRouting $val(rp) \
  38. -llType $val(ll) \
  39. -macType $val(mac) \
  40. -ifqType $val(ifq) \
  41. -ifqLen $val(ifqlen) \
  42. -antType $val(ant) \
  43. -propType $val(prop) \
  44. -phyType $val(netif) \
  45. -channel $chan \
  46. -topoInstance $topo \
  47. -agentTrace ON \
  48. -routerTrace ON \
  49. -macTrace OFF \
  50. -movementTrace ON
  51. #===================================
  52. # Nodes Definition
  53. #===================================
  54. #Create 7 nodes
  55. set n0 [$ns node]
  56. $n0 set X_ 99
  57. $n0 set Y_ 299
  58. $n0 set Z_ 0.0
  59. $ns initial_node_pos $n0 20
  60. set n1 [$ns node]
  61. $n1 set X_ 299
  62. $n1 set Y_ 297
  63. $n1 set Z_ 0.0
  64. $ns initial_node_pos $n1 20
  65. set n2 [$ns node]
  66. $n2 set X_ 499
  67. $n2 set Y_ 298
  68. $n2 set Z_ 0.0
  69. $ns initial_node_pos $n2 20
  70. set n3 [$ns node]
  71. $n3 set X_ 700
  72. $n3 set Y_ 299
  73. $n3 set Z_ 0.0
  74. $ns initial_node_pos $n3 20
  75. set n4 [$ns node]
  76. $n4 set X_ 199
  77. $n4 set Y_ 350
  78. $n4 set Z_ 0.0
  79. $ns initial_node_pos $n4 20
  80. set n5 [$ns node]
  81. $n5 set X_ 599
  82. $n5 set Y_ 350
  83. $n5 set Z_ 0.0
  84. $ns initial_node_pos $n5 20
  85. set n6 [$ns node]
  86. $n6 set X_ 600
  87. $n6 set Y_ 200
  88. $n6 set Z_ 0.0
  89. $ns initial_node_pos $n6 20
  90. # Node 5 is given RED Color and a label- indicating it is a Blackhole Attacker
  91. $n5 color red
  92. $ns at 0.0 "$n5 color red"
  93. $ns at 0.0 "$n5 label Attacker"
  94. # Node 0 is given GREEN Color and a label - acts as a Source Node
  95. $n0 color green
  96. $ns at 0.0 "$n0 color green"
  97. $ns at 0.0 "$n0 label Source"
  98. # Node 3 is given BLUE Color and a label- acts as a Destination Node
  99. $n3 color blue
  100. $ns at 0.0 "$n3 color blue"
  101. $ns at 0.0 "$n3 label Destination"
  102. #===================================
  103. # Set node 5 as attacker
  104. #===================================
  105. $ns at 0.0 "[$n5 set ragent_] hacker"
  106. #===================================
  107. # Agents Definition
  108. #===================================
  109. #Setup a UDP connection
  110. set udp0 [new Agent/UDP]
  111. $ns attach-agent $n0 $udp0
  112. set null1 [new Agent/Null]
  113. $ns attach-agent $n3 $null1
  114. $ns connect $udp0 $null1
  115. $udp0 set packetSize_ 1500
  116. #===================================
  117. # Applications Definition
  118. #===================================
  119. #Setup a CBR Application over UDP connection
  120. set cbr0 [new Application/Traffic/CBR]
  121. $cbr0 attach-agent $udp0
  122. $cbr0 set packetSize_ 1000
  123. $cbr0 set rate_ 0.1Mb
  124. $cbr0 set random_ null
  125. $ns at 1.0 "$cbr0 start"
  126. $ns at 100.0 "$cbr0 stop"
  127. #===================================
  128. # Termination
  129. #===================================
  130. #Define a 'finish' procedure
  131. proc finish {} {
  132. global ns tracefile namfile
  133. $ns flush-trace
  134. close $tracefile
  135. close $namfile
  136. exec nam blackhole.nam &
  137. exit 0
  138. }
  139. for {set i 0} {$i < $val(nn) } { incr i } {
  140. $ns at $val(stop) "\$n$i reset"
  141. }
  142. $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
  143. $ns at $val(stop) "finish"
  144. $ns at $val(stop) "puts \"done\" ; $ns halt"
  145. $ns run

颜色非常好定义,通过 color 就可以实现了,然后我的实验思路是:将 116 行的将 n5 定义为 hacker 的语句注释掉,就可以得到一个正常的网络,随后我们可以在将注释取消,得到一个带有黑洞节点的攻击网络,通过比对两个网络的模拟图像,同时分析 trace 文件我们可以得到相关的数据,并得出结论。这样我们就需要一个合适的 gawk 脚本:

  1. BEGIN {
  2. sendLine = 0;
  3. recvLine = 0;
  4. fowardLine = 0;
  5. }
  6. $0 ~/^s.* AGT/ {
  7. sendLine ++ ;
  8. }
  9. $0 ~/^r.* AGT/ {
  10. recvLine ++ ;
  11. }
  12. $0 ~/^f.* RTR/ {
  13. fowardLine ++ ;
  14. }
  15. END {
  16. printf "cbr s:%d r:%d, r/s Ratio:%.4f, f:%d \n", sendLine, recvLine, (recvLine/sendLine),fowardLine;
  17. }

同样,我们也可以通过设置发包速率来横向比对各种数据。

可以看到,明显黑洞节点吸引了报文,一下是两个 trace 对比情况的截图:
image_1bf7nc2b3sch12qad8a1b4trjq3l.png-137.5kB

有黑洞节点的丢包率是是百分之百,可见攻击效果多明显。

五、实验心得与体会

在这个实验中,我并没有按照实验的要求去绘制攻击者数量和丢包率的关系图,主要原因在于当我把大量的节点放置在网络中时,我发现我的 ns 出现了段错误,这个问题一直没有解决,个人猜测是因为对黑洞信息的处理不得当的原因,但是不得不说的是,这个黑洞实验着实锻炼了我不少。不像之前的虫洞试验,深入 ns-2 源码中对错误进行了很久的分析也没有找到失败的原因。这次黑洞实验,可能是因为有了虫洞实验的失败教训,我着实效率很高。深入思考了一下黑洞试验和虫洞实验对代码修改的难度差异,我觉得可能下面的主要愿意造成的:

  1. 黑洞实验中不需要对接收和发送同时处理,这样一来报文错误处理的可能性就大大减少了
  2. 而虫洞实验需要对接收和发送同时处理,而且代码中的 send_upsend_down 不是简单的发出接收的函数,而是在网络模型的上下层传递的过程,这个就导致了牵一发动全身,使得冲动实验中有很多很多需要修改的部分。

另外的一点是,除了虫洞攻击实验,黑洞实验是我所有实验中难度最大的实验,我从一次次失败,一次次克服困难的过程中也学到了很多除了课程之外的更多的东西。谢谢老师和助教给我这样一个机会去提升自我。

虫洞攻击实验(失败)

见报告 PPT

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注