A Discrete-Event Network Simulator
API
traceroute-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Ritsumeikan University, Shiga, Japan
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
18  *
19  *
20  * TraceRoute application example using AODV routing protocol.
21  *
22  *
23  */
24 
25 #include "ns3/aodv-module.h"
26 #include "ns3/core-module.h"
27 #include "ns3/internet-module.h"
28 #include "ns3/mobility-module.h"
29 #include "ns3/network-module.h"
30 #include "ns3/point-to-point-module.h"
31 #include "ns3/v4traceroute-helper.h"
32 #include "ns3/wifi-module.h"
33 
34 #include <cmath>
35 #include <iostream>
36 
37 using namespace ns3;
38 
55 {
56  public:
64  bool Configure(int argc, char** argv);
66  void Run();
71  void Report(std::ostream& os);
72 
73  private:
74  // parameters
76  uint32_t size;
78  double step;
80  double totalTime;
82  bool pcap;
91 
92  private:
94  void CreateNodes();
96  void CreateDevices();
98  void InstallInternetStack();
100 
101  void InstallApplications();
102 };
103 
104 int
105 main(int argc, char** argv)
106 {
107  TracerouteExample test;
108  if (!test.Configure(argc, argv))
109  {
110  NS_FATAL_ERROR("Configuration failed. Aborted.");
111  }
112 
113  test.Run();
114  test.Report(std::cout);
115  return 0;
116 }
117 
118 //-----------------------------------------------------------------------------
120  : size(10),
121  step(50),
122  totalTime(100),
123  pcap(false),
124  printRoutes(false)
125 {
126 }
127 
128 bool
129 TracerouteExample::Configure(int argc, char** argv)
130 {
131  // Enable AODV logs by default. Comment this if too noisy
132  // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
133 
134  SeedManager::SetSeed(12345);
135  CommandLine cmd(__FILE__);
136 
137  cmd.AddValue("pcap", "Write PCAP traces.", pcap);
138  cmd.AddValue("printRoutes", "Print routing table dumps.", printRoutes);
139  cmd.AddValue("size", "Number of nodes.", size);
140  cmd.AddValue("time", "Simulation time, s.", totalTime);
141  cmd.AddValue("step", "Grid step, m", step);
142 
143  cmd.Parse(argc, argv);
144  return true;
145 }
146 
147 void
149 {
150  CreateNodes();
151 
152  CreateDevices();
153 
155 
157 
158  std::cout << "Starting simulation for " << totalTime << " s ...\n";
159 
160  Simulator::Stop(Seconds(totalTime));
161  Simulator::Run();
162  Simulator::Destroy();
163 }
164 
165 void
167 {
168 }
169 
170 void
172 {
173  std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
174  nodes.Create(size);
175  // Name nodes
176  for (uint32_t i = 0; i < size; ++i)
177  {
178  std::ostringstream os;
179  os << "node-" << i;
180  Names::Add(os.str(), nodes.Get(i));
181  }
182  // Create static grid
184  mobility.SetPositionAllocator("ns3::GridPositionAllocator",
185  "MinX",
186  DoubleValue(0.0),
187  "MinY",
188  DoubleValue(0.0),
189  "DeltaX",
190  DoubleValue(step),
191  "DeltaY",
192  DoubleValue(0),
193  "GridWidth",
195  "LayoutType",
196  StringValue("RowFirst"));
197  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
198  mobility.Install(nodes);
199 }
200 
201 void
203 {
204  WifiMacHelper wifiMac;
205  wifiMac.SetType("ns3::AdhocWifiMac");
206  YansWifiPhyHelper wifiPhy;
207  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
208  wifiPhy.SetChannel(wifiChannel.Create());
210  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
211  "DataMode",
212  StringValue("OfdmRate6Mbps"),
213  "RtsCtsThreshold",
214  UintegerValue(0));
215  devices = wifi.Install(wifiPhy, wifiMac, nodes);
216 
217  if (pcap)
218  {
219  wifiPhy.EnablePcapAll(std::string("aodv"));
220  }
221 }
222 
223 void
225 {
226  AodvHelper aodv;
227  // you can configure AODV attributes here using aodv.Set(name, value)
229  stack.SetRoutingHelper(aodv); // has effect on the next Install ()
230  stack.Install(nodes);
232  address.SetBase("10.0.0.0", "255.0.0.0");
233  interfaces = address.Assign(devices);
234 
235  if (printRoutes)
236  {
237  Ptr<OutputStreamWrapper> routingStream =
238  Create<OutputStreamWrapper>("aodv.routes", std::ios::out);
239  Ipv4RoutingHelper::PrintRoutingTableAllAt(Seconds(8), routingStream);
240  }
241 }
242 
243 void
245 {
246  V4TraceRouteHelper traceroute(Ipv4Address("10.0.0.10")); // size - 1
247  traceroute.SetAttribute("Verbose", BooleanValue(true));
248  ApplicationContainer p = traceroute.Install(nodes.Get(0));
249 
250  // Used when we wish to dump the traceroute results into a file
251 
252  // Ptr<OutputStreamWrapper> printstrm = Create<OutputStreamWrapper> ("mytrace", std::ios::out);
253  // traceroute.PrintTraceRouteAt(nodes.Get(0),printstrm);
254 
255  p.Start(Seconds(0));
256  p.Stop(Seconds(totalTime) - Seconds(0.001));
257 }
void InstallApplications()
Create the simulation applications.
double totalTime
Simulation time, seconds.
void CreateDevices()
Create the devices.
void CreateNodes()
Create the nodes.
void Report(std::ostream &os)
Report results.
uint32_t size
Number of nodes.
void Run()
Run simulation.
Ipv4InterfaceContainer interfaces
interfaces used in the example
bool Configure(int argc, char **argv)
Configure script parameters.
NodeContainer nodes
nodes used in the example
NetDeviceContainer devices
devices used in the example
void InstallInternetStack()
Create the network.
bool printRoutes
Print aodv routes if true.
bool pcap
Write per-device PCAP traces if true.
double step
Distance between nodes, meters.
Helper class that adds AODV routing to nodes.
Definition: aodv-helper.h:36
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Hold variables of type string.
Definition: string.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
Create a IPv4 traceroute application and associate it to a node.
ApplicationContainer Install(NodeContainer nodes) const
Install a TraceRoute application on each Node in the provided NodeContainer.
void SetAttribute(std::string name, const AttributeValue &value)
Configure traceRoute applications attribute.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
address
Definition: first.py:47
stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
wifi
Definition: third.py:95
mobility
Definition: third.py:105