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