ns3分析RIP协议

实现步骤

  1. 构建拓扑

  2. 配置结点并运行PING

    不断从PC1向PC2进行PING

  3. 构造坏消息

    一定时间后将C和E之间的连接断开

C++代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <fstream>
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-routing-table-entry.h"
#include "ns3/animation-interface.h"
#include "ns3/netanim-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("RipSimpleRouting");

// 断开两个结点间的连接
void TearDownLink (Ptr<Node> nodeA, Ptr<Node> nodeB, uint32_t interfaceA, uint32_t interfaceB)
{
nodeA->GetObject<Ipv4> ()->SetDown (interfaceA);
nodeB->GetObject<Ipv4> ()->SetDown (interfaceB);
}

int main(int argc, char** argv)
{
bool verbose = false;
bool printRoutingTables = false; // 打印路由表
bool showPings = false;
std::string SplitHorizon ("NoSplitHorizon"); // 水平分隔策略

CommandLine cmd(__FILE__);
cmd.AddValue ("verbose", "turn on log components", verbose);
cmd.AddValue ("printRoutingTables", "Print routing tables at 30, 60 and 90 seconds", printRoutingTables);
cmd.AddValue ("showPings", "Show Ping6 reception", showPings);
cmd.AddValue ("splitHorizonStrategy", "Split Horizon strategy to use (NoSplitHorizon, SplitHorizon, PoisonReverse)", SplitHorizon);
cmd.Parse (argc, argv);

if (verbose)
{
LogComponentEnableAll (LogLevel (LOG_PREFIX_TIME | LOG_PREFIX_NODE));
LogComponentEnable ("RipSimpleRouting", LOG_LEVEL_INFO);
LogComponentEnable ("Rip", LOG_LEVEL_ALL);
LogComponentEnable ("Ipv4Interface", LOG_LEVEL_ALL);
LogComponentEnable ("Icmpv4L4Protocol", LOG_LEVEL_ALL);
LogComponentEnable ("Ipv4L3Protocol", LOG_LEVEL_ALL);
LogComponentEnable ("ArpCache", LOG_LEVEL_ALL);
LogComponentEnable ("V4Ping", LOG_LEVEL_ALL);
}

if (SplitHorizon == "NoSplitHorizon")
{
Config::SetDefault ("ns3::Rip::SplitHorizon", EnumValue (RipNg::NO_SPLIT_HORIZON));
}
else if (SplitHorizon == "SplitHorizon")
{
Config::SetDefault ("ns3::Rip::SplitHorizon", EnumValue (RipNg::SPLIT_HORIZON));
}
else
{
Config::SetDefault ("ns3::Rip::SplitHorizon", EnumValue (RipNg::POISON_REVERSE));
}

// 创建各结点
NS_LOG_INFO("Create nodes.");
Ptr<Node> pc1 = CreateObject<Node>();
Names::Add("Pc1Node", pc1);
Ptr<Node> pc2 = CreateObject<Node>();
Names::Add("Pc2Node", pc2);
Ptr<Node> a = CreateObject<Node> ();
Names::Add ("RouterA", a);
Ptr<Node> b = CreateObject<Node> ();
Names::Add ("RouterB", b);
Ptr<Node> c = CreateObject<Node> ();
Names::Add ("RouterC", c);
Ptr<Node> d = CreateObject<Node> ();
Names::Add ("RouterD", d);
Ptr<Node> e = CreateObject<Node> ();
Names::Add ("RouterE", e);
Ptr<Node> f = CreateObject<Node> ();
Names::Add ("RouterF", f);

// 添加各网络连接
NodeContainer net1(pc1, b);
NodeContainer net2(b, c);
NodeContainer net3(a, b);
NodeContainer net4(a, c);
NodeContainer net5(a, d);
NodeContainer net6(d, e);
NodeContainer net7(c, e);
NodeContainer net8(e, f);
NodeContainer net9(f, pc2);
NodeContainer routers(a, b, c, d, e);
routers.Add(f);
NodeContainer nodes(pc1, pc2);

NS_LOG_INFO("Create channels.");
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
NetDeviceContainer ndc1 = csma.Install (net1);
NetDeviceContainer ndc2 = csma.Install (net2);
NetDeviceContainer ndc3 = csma.Install (net3);
NetDeviceContainer ndc4 = csma.Install (net4);
NetDeviceContainer ndc5 = csma.Install (net5);
NetDeviceContainer ndc6 = csma.Install (net6);
NetDeviceContainer ndc7 = csma.Install (net7);
NetDeviceContainer ndc8 = csma.Install (net8);
NetDeviceContainer ndc9 = csma.Install (net9);

NS_LOG_INFO("Create IPv4 and routing");
RipHelper ripRouting;
// 路由器B和F与主机PC1和PC2相连的接口不参与RIP报文交换
ripRouting.ExcludeInterface(b, 1);
ripRouting.ExcludeInterface(f, 2);
// 设置接口度量值
ripRouting.SetInterfaceMetric(b, 2, 2);
ripRouting.SetInterfaceMetric(c, 1, 2);
ripRouting.SetInterfaceMetric(a, 2, 4);
ripRouting.SetInterfaceMetric(c, 2, 4);
ripRouting.SetInterfaceMetric(d, 2, 7);
ripRouting.SetInterfaceMetric(e, 1, 7);
ripRouting.SetInterfaceMetric(c, 3, 3);
ripRouting.SetInterfaceMetric(e, 2, 3);
ripRouting.SetInterfaceMetric(e, 3, 2);
ripRouting.SetInterfaceMetric(f, 1, 2);

Ipv4ListRoutingHelper listRH;
listRH.Add(ripRouting, 0);

InternetStackHelper internet;
internet.SetIpv6StackInstall(false);
internet.SetRoutingHelper(listRH);
internet.Install(routers);

InternetStackHelper internetNodes;
internetNodes.SetIpv6StackInstall(false);
internetNodes.Install(nodes);

// 分配IP地址,设置网络掩码
NS_LOG_INFO("Assign IPv4 Addresses.");
Ipv4AddressHelper ipv4;

ipv4.SetBase(Ipv4Address("10.0.0.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic1 = ipv4.Assign(ndc1);

ipv4.SetBase(Ipv4Address("10.0.1.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic2 = ipv4.Assign(ndc2);

ipv4.SetBase(Ipv4Address("10.0.2.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic3 = ipv4.Assign(ndc3);

ipv4.SetBase(Ipv4Address("10.0.3.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic4 = ipv4.Assign(ndc4);

ipv4.SetBase(Ipv4Address("10.0.4.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic5 = ipv4.Assign(ndc5);

ipv4.SetBase(Ipv4Address("10.0.5.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic6 = ipv4.Assign(ndc6);

ipv4.SetBase(Ipv4Address("10.0.6.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic7 = ipv4.Assign(ndc7);

ipv4.SetBase(Ipv4Address("10.0.7.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic8 = ipv4.Assign(ndc8);

ipv4.SetBase(Ipv4Address("10.0.8.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic9 = ipv4.Assign(ndc9);

Ptr<Ipv4StaticRouting> staticRouting;
staticRouting = Ipv4RoutingHelper::GetRouting<Ipv4StaticRouting>(pc1->GetObject<Ipv4>()->GetRoutingProtocol());
staticRouting->SetDefaultRoute("10.0.0.2", 1);
staticRouting = Ipv4RoutingHelper::GetRouting<Ipv4StaticRouting>(pc2->GetObject<Ipv4>()->GetRoutingProtocol());
staticRouting->SetDefaultRoute("10.0.8.1", 1);

// 打印路由表
if (printRoutingTables)
{
RipHelper routingHelper;
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout);
routingHelper.PrintRoutingTableAt (Seconds (2.0), a, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (2.0), b, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (2.0), c, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (2.0), d, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (2.0), e, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (2.0), f, routingStream);

routingHelper.PrintRoutingTableAt (Seconds (3.0), a, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (3.0), b, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (3.0), c, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (3.0), d, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (3.0), e, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (3.0), f, routingStream);

routingHelper.PrintRoutingTableAt (Seconds (4.0), a, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (4.0), b, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (4.0), c, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (4.0), d, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (4.0), e, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (4.0), f, routingStream);

routingHelper.PrintRoutingTableAt (Seconds (5.0), a, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (5.0), b, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (5.0), c, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (5.0), d, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (5.0), e, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (5.0), f, routingStream);

routingHelper.PrintRoutingTableAt (Seconds (6.0), a, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (6.0), b, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (6.0), c, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (6.0), d, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (6.0), e, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (6.0), f, routingStream);

routingHelper.PrintRoutingTableAt (Seconds (7.0), a, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (7.0), b, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (7.0), c, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (7.0), d, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (7.0), e, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (7.0), f, routingStream);
}

NS_LOG_INFO("Create Applications.");
uint32_t packetSize = 1024;
Time interPacketInterval = Seconds(1.0);
// ping的远程IP is 10.0.8.2 (pc2所在网络)
V4PingHelper ping("10.0.8.2");

ping.SetAttribute ("Interval", TimeValue (interPacketInterval));
ping.SetAttribute ("Size", UintegerValue (packetSize));
if (showPings)
{
ping.SetAttribute ("Verbose", BooleanValue (true));
}
// ping的发起者pc1
ApplicationContainer apps = ping.Install(pc1);
apps.Start(Seconds(1.0));
apps.Stop(Seconds(1200.0));

// 打开跟踪数据
AsciiTraceHelper ascii;
csma.EnableAsciiAll (ascii.CreateFileStream ("MyRip.tr"));
csma.EnablePcapAll ("MyRip", true);

// 定时断开C和E之间的连接
Simulator::Schedule (Seconds (30), &TearDownLink, c, e, 3, 2);

// 固定结点
AnimationInterface::SetConstantPosition(b, 10.0, 10.0);
AnimationInterface::SetConstantPosition(c, 20.0, 10.0);
AnimationInterface::SetConstantPosition(a, 10.0, 20.0);
AnimationInterface::SetConstantPosition(d, 10.0, 30.0);
AnimationInterface::SetConstantPosition(e, 20.0, 30.0);
AnimationInterface::SetConstantPosition(f, 30.0, 30.0);
AnimationInterface::SetConstantPosition(pc1, 1.0, 1.0);
AnimationInterface::SetConstantPosition(pc2, 40.0, 20.0);
// NetAnim可视化
// AnimationInterface anim("MyRip.xml");

/* Now, do the actual simulation. */
NS_LOG_INFO ("Run Simulation.");
Simulator::Stop (Seconds (1200.0));
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");

}

运行效果

  1. 构建的拓扑

  2. 未建立连接

  3. 连接建立

  4. 坏消息

  5. 新的连接