A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
traceroute-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Ritsumeikan University, Shiga, Japan
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
7 *
8 *
9 * TraceRoute application example using AODV routing protocol.
10 */
11
12#include "ns3/aodv-module.h"
13#include "ns3/core-module.h"
14#include "ns3/internet-module.h"
15#include "ns3/mobility-module.h"
16#include "ns3/network-module.h"
17#include "ns3/point-to-point-module.h"
18#include "ns3/v4traceroute-helper.h"
19#include "ns3/wifi-module.h"
20
21#include <cmath>
22#include <iostream>
23
24using namespace ns3;
25
26/**
27 * \ingroup aodv-examples
28 * \ingroup examples
29 * \brief Test script.
30 *
31 * This script creates 1-dimensional grid topology and Traceroute the last node from the first one:
32 *
33 * [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.0.4]
34 *
35 * The results should be all the intermediate hops all the way to 10.0.0.10
36 *
37 * Usage:
38 *
39 * traceroute 10.0.0.10
40 */
42{
43 public:
45 /**
46 * \brief Configure script parameters
47 * \param argc is the command line argument count
48 * \param argv is the command line arguments
49 * \return true on successful configuration
50 */
51 bool Configure(int argc, char** argv);
52 /// Run simulation
53 void Run();
54 /**
55 * Report results
56 * \param os the output stream
57 */
58 void Report(std::ostream& os);
59
60 private:
61 // parameters
62 /// Number of nodes
64 /// Distance between nodes, meters
65 double step;
66 /// Simulation time, seconds
67 double totalTime;
68 /// Write per-device PCAP traces if true
69 bool pcap;
70 /// Print aodv routes if true
72 /// nodes used in the example
74 /// devices used in the example
76 /// interfaces used in the example
78
79 private:
80 /// Create the nodes
81 void CreateNodes();
82 /// Create the devices
83 void CreateDevices();
84 /// Create the network
86 /// Create the simulation applications
87
89};
90
91int
92main(int argc, char** argv)
93{
95 if (!test.Configure(argc, argv))
96 {
97 NS_FATAL_ERROR("Configuration failed. Aborted.");
98 }
99
100 test.Run();
101 test.Report(std::cout);
102 return 0;
103}
104
105//-----------------------------------------------------------------------------
107 : size(10),
108 step(50),
109 totalTime(100),
110 pcap(false),
111 printRoutes(false)
112{
113}
114
115bool
116TracerouteExample::Configure(int argc, char** argv)
117{
118 // Enable AODV logs by default. Comment this if too noisy
119 // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
120
122 CommandLine cmd(__FILE__);
123
124 cmd.AddValue("pcap", "Write PCAP traces.", pcap);
125 cmd.AddValue("printRoutes", "Print routing table dumps.", printRoutes);
126 cmd.AddValue("size", "Number of nodes.", size);
127 cmd.AddValue("time", "Simulation time, s.", totalTime);
128 cmd.AddValue("step", "Grid step, m", step);
129
130 cmd.Parse(argc, argv);
131 return true;
132}
133
134void
136{
137 CreateNodes();
138
140
142
144
145 std::cout << "Starting simulation for " << totalTime << " s ...\n";
146
150}
151
152void
154{
155}
156
157void
159{
160 std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
162 // Name nodes
163 for (uint32_t i = 0; i < size; ++i)
164 {
165 std::ostringstream os;
166 os << "node-" << i;
167 Names::Add(os.str(), nodes.Get(i));
168 }
169 // Create static grid
170 MobilityHelper mobility;
171 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
172 "MinX",
173 DoubleValue(0.0),
174 "MinY",
175 DoubleValue(0.0),
176 "DeltaX",
178 "DeltaY",
179 DoubleValue(0),
180 "GridWidth",
182 "LayoutType",
183 StringValue("RowFirst"));
184 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
185 mobility.Install(nodes);
186}
187
188void
190{
191 WifiMacHelper wifiMac;
192 wifiMac.SetType("ns3::AdhocWifiMac");
193 YansWifiPhyHelper wifiPhy;
195 wifiPhy.SetChannel(wifiChannel.Create());
196 WifiHelper wifi;
197 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
198 "DataMode",
199 StringValue("OfdmRate6Mbps"),
200 "RtsCtsThreshold",
201 UintegerValue(0));
202 devices = wifi.Install(wifiPhy, wifiMac, nodes);
203
204 if (pcap)
205 {
206 wifiPhy.EnablePcapAll(std::string("aodv"));
207 }
208}
209
210void
212{
213 AodvHelper aodv;
214 // you can configure AODV attributes here using aodv.Set(name, value)
216 stack.SetRoutingHelper(aodv); // has effect on the next Install ()
217 stack.Install(nodes);
218 Ipv4AddressHelper address;
219 address.SetBase("10.0.0.0", "255.0.0.0");
220 interfaces = address.Assign(devices);
221
222 if (printRoutes)
223 {
224 Ptr<OutputStreamWrapper> routingStream =
225 Create<OutputStreamWrapper>("aodv.routes", std::ios::out);
227 }
228}
229
230void
232{
233 V4TraceRouteHelper traceroute(Ipv4Address("10.0.0.10")); // size - 1
234 traceroute.SetAttribute("Verbose", BooleanValue(true));
235 ApplicationContainer p = traceroute.Install(nodes.Get(0));
236
237 // Used when we wish to dump the traceroute results into a file
238
239 // Ptr<OutputStreamWrapper> printstrm = Create<OutputStreamWrapper> ("mytrace", std::ios::out);
240 // traceroute.PrintTraceRouteAt(nodes.Get(0),printstrm);
241
242 p.Start(Seconds(0));
243 p.Stop(Seconds(totalTime) - Seconds(0.001));
244}
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:25
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.
ApplicationContainer Install(NodeContainer c)
Install an application on each node of the input container configured with all the attributes set wit...
void SetAttribute(const std::string &name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
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.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
static void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of all nodes at a particular time.
Helper class used to assign positions and mobility models to nodes.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
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 ...
Smart pointer class similar to boost::intrusive_ptr.
static void SetSeed(uint32_t seed)
Set the seed.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
Create a IPv4 traceroute application and associate it to a node.
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
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.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
-ns3 Test suite for the ns3 wrapper script