forked from schneiderl/redes-ii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopologia-i.cc
165 lines (137 loc) · 5.68 KB
/
topologia-i.cc
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
#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/netanim-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("RipSimpleRouting");
int main (int argc, char **argv)
{
bool verbose = false;
bool printRoutingTables = false;
bool showPings = false;
std::string SplitHorizon ("PoisonReverse");
CommandLine cmd;
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 ("Exemplo-RIp", 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> pcT = CreateObject<Node> ();
Names::Add ("TNode", pcT);
Ptr<Node> pcR = CreateObject<Node> ();
Names::Add ("RNode", pcR);
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);
NodeContainer net1 (pcT, a);
NodeContainer net2 (a, b);
NodeContainer net3 (b, c);
NodeContainer net4 (c, pcR);
NodeContainer routers (a, b, c);
NodeContainer nodes (pcT, pcR);
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);
NS_LOG_INFO ("Create IPv4 and routing");
RipHelper ripRouting;
// Rule of thumb:
// Interfaces are added sequentially, starting from 0
// However, interface 0 is always the loopback...
ripRouting.ExcludeInterface (a, 1);
ripRouting.ExcludeInterface (b, 2);
ripRouting.ExcludeInterface (c, 3);
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);
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);
Ptr<Ipv4StaticRouting> staticRouting;
staticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting> (pcT->GetObject<Ipv4> ()->GetRoutingProtocol ());
staticRouting->SetDefaultRoute ("10.0.0.2", 1 );
staticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting> (pcR->GetObject<Ipv4> ()->GetRoutingProtocol ());
staticRouting->SetDefaultRoute ("10.0.2.1", 1 );
if (printRoutingTables)
{
RipHelper routingHelper;
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout);
routingHelper.PrintRoutingTableAt (Seconds (30.0), a, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (30.0), b, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (60.0), a, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (60.0), b, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (90.0), a, routingStream);
routingHelper.PrintRoutingTableAt (Seconds (90.0), b, routingStream);
}
NS_LOG_INFO ("Create Applications.");
uint32_t packetSize = 1024;
Time interPacketInterval = Seconds (1.0);
V4PingHelper ping ("10.0.2.2");
ping.SetAttribute ("Interval", TimeValue (interPacketInterval));
ping.SetAttribute ("Size", UintegerValue (packetSize));
if (showPings)
{
ping.SetAttribute ("Verbose", BooleanValue (true));
}
ApplicationContainer apps = ping.Install (pcT);
apps.Start (Seconds (1.0));
apps.Stop (Seconds (110.0));
AsciiTraceHelper ascii;
csma.EnableAsciiAll (ascii.CreateFileStream ("exemplo-rip.tr"));
csma.EnablePcapAll ("exemplo-rip", true);
/* Now, do the actual simulation. */
NS_LOG_INFO ("Run Simulation.");
Simulator::Stop (Seconds (131.0));
AnimationInterface anim ("animation.xml");
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
}