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 * 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#include "ns3/aodv-module.h"
24#include "ns3/core-module.h"
25#include "ns3/internet-module.h"
26#include "ns3/mobility-module.h"
27#include "ns3/network-module.h"
28#include "ns3/point-to-point-module.h"
29#include "ns3/v4traceroute-helper.h"
30#include "ns3/wifi-module.h"
31
32#include <cmath>
33#include <iostream>
34
35using namespace ns3;
36
37/**
38 * \ingroup aodv-examples
39 * \ingroup examples
40 * \brief Test script.
41 *
42 * This script creates 1-dimensional grid topology and Traceroute the last node from the first one:
43 *
44 * [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.0.4]
45 *
46 * The results should be all the intermediate hops all the way to 10.0.0.10
47 *
48 * Usage:
49 *
50 * traceroute 10.0.0.10
51 */
53{
54 public:
56 /**
57 * \brief Configure script parameters
58 * \param argc is the command line argument count
59 * \param argv is the command line arguments
60 * \return true on successful configuration
61 */
62 bool Configure(int argc, char** argv);
63 /// Run simulation
64 void Run();
65 /**
66 * Report results
67 * \param os the output stream
68 */
69 void Report(std::ostream& os);
70
71 private:
72 // parameters
73 /// Number of nodes
75 /// Distance between nodes, meters
76 double step;
77 /// Simulation time, seconds
78 double totalTime;
79 /// Write per-device PCAP traces if true
80 bool pcap;
81 /// Print aodv routes if true
83 /// nodes used in the example
85 /// devices used in the example
87 /// interfaces used in the example
89
90 private:
91 /// Create the nodes
92 void CreateNodes();
93 /// Create the devices
94 void CreateDevices();
95 /// Create the network
97 /// Create the simulation applications
98
100};
101
102int
103main(int argc, char** argv)
104{
106 if (!test.Configure(argc, argv))
107 {
108 NS_FATAL_ERROR("Configuration failed. Aborted.");
109 }
110
111 test.Run();
112 test.Report(std::cout);
113 return 0;
114}
115
116//-----------------------------------------------------------------------------
118 : size(10),
119 step(50),
120 totalTime(100),
121 pcap(false),
122 printRoutes(false)
123{
124}
125
126bool
127TracerouteExample::Configure(int argc, char** argv)
128{
129 // Enable AODV logs by default. Comment this if too noisy
130 // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
131
133 CommandLine cmd(__FILE__);
134
135 cmd.AddValue("pcap", "Write PCAP traces.", pcap);
136 cmd.AddValue("printRoutes", "Print routing table dumps.", printRoutes);
137 cmd.AddValue("size", "Number of nodes.", size);
138 cmd.AddValue("time", "Simulation time, s.", totalTime);
139 cmd.AddValue("step", "Grid step, m", step);
140
141 cmd.Parse(argc, argv);
142 return true;
143}
144
145void
147{
148 CreateNodes();
149
151
153
155
156 std::cout << "Starting simulation for " << totalTime << " s ...\n";
157
161}
162
163void
165{
166}
167
168void
170{
171 std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
173 // Name nodes
174 for (uint32_t i = 0; i < size; ++i)
175 {
176 std::ostringstream os;
177 os << "node-" << i;
178 Names::Add(os.str(), nodes.Get(i));
179 }
180 // Create static grid
181 MobilityHelper mobility;
182 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
183 "MinX",
184 DoubleValue(0.0),
185 "MinY",
186 DoubleValue(0.0),
187 "DeltaX",
189 "DeltaY",
190 DoubleValue(0),
191 "GridWidth",
193 "LayoutType",
194 StringValue("RowFirst"));
195 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
196 mobility.Install(nodes);
197}
198
199void
201{
202 WifiMacHelper wifiMac;
203 wifiMac.SetType("ns3::AdhocWifiMac");
204 YansWifiPhyHelper wifiPhy;
206 wifiPhy.SetChannel(wifiChannel.Create());
207 WifiHelper wifi;
208 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
209 "DataMode",
210 StringValue("OfdmRate6Mbps"),
211 "RtsCtsThreshold",
212 UintegerValue(0));
213 devices = wifi.Install(wifiPhy, wifiMac, nodes);
214
215 if (pcap)
216 {
217 wifiPhy.EnablePcapAll(std::string("aodv"));
218 }
219}
220
221void
223{
224 AodvHelper aodv;
225 // you can configure AODV attributes here using aodv.Set(name, value)
227 stack.SetRoutingHelper(aodv); // has effect on the next Install ()
228 stack.Install(nodes);
229 Ipv4AddressHelper address;
230 address.SetBase("10.0.0.0", "255.0.0.0");
231 interfaces = address.Assign(devices);
232
233 if (printRoutes)
234 {
235 Ptr<OutputStreamWrapper> routingStream =
236 Create<OutputStreamWrapper>("aodv.routes", std::ios::out);
238 }
239}
240
241void
243{
244 V4TraceRouteHelper traceroute(Ipv4Address("10.0.0.10")); // size - 1
245 traceroute.SetAttribute("Verbose", BooleanValue(true));
246 ApplicationContainer p = traceroute.Install(nodes.Get(0));
247
248 // Used when we wish to dump the traceroute results into a file
249
250 // Ptr<OutputStreamWrapper> printstrm = Create<OutputStreamWrapper> ("mytrace", std::ios::out);
251 // traceroute.PrintTraceRouteAt(nodes.Get(0),printstrm);
252
253 p.Start(Seconds(0));
254 p.Stop(Seconds(totalTime) - Seconds(0.001));
255}
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.
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
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.
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:775
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.
Definition: ptr.h:77
static void SetSeed(uint32_t seed)
Set the seed.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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.
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.
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.
Definition: fatal-error.h:179
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Every class exported by the ns3 library is enclosed in the ns3 namespace.
-ns3 Test suite for the ns3 wrapper script