A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ping-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Chandrakant Jena
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Chandrakant Jena <chandrakant.barcelona@gmail.com>
7 */
8
9// Example to demonstrate the working of the Ping Application
10// Network topology:
11// A ------------------------------ B ------------------------------ C
12// 100 Mbps 100 Mbps
13// 5 ms (one way) 5 ms (one way)
14// IPv4 addresses:
15// 10.1.1.1 <-> 10.1.1.2 / 10.1.2.1 <-> 10.1.2.2
16//
17// IPv6 addresses:
18// 2001:1::200:ff:fe00:1
19// <-> 2001:1::200:ff:fe00:2 / 2001:1:0:1:200:ff:fe00:3
20// <-> 2001:1:0:1:200:ff:fe00:4
21//
22// The topology has three nodes interconnected by two point-to-point links.
23// Each link has 5 ms one-way delay, for a round-trip propagation delay
24// of 20 ms. The transmission rate on each link is 100 Mbps. The routing
25// between links is enabled by ns-3's NixVector routing.
26//
27// By default, this program will send 5 pings from node A to node C using IPv6.
28// When using IPv6, the output will look like this:
29//
30// PING 2001:1:0:1:200:ff:fe00:4 - 56 bytes of data; 104 bytes including ICMP and IPv6 headers.
31// 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=0 ttl=63 time=20.033 ms
32// 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=1 ttl=63 time=20.033 ms
33// 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=2 ttl=63 time=20.033 ms
34// 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=3 ttl=63 time=20.033 ms
35// 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=4 ttl=63 time=20.033 ms
36//
37// --- 2001:1:0:1:200:ff:fe00:4 ping statistics ---
38// 5 packets transmitted, 5 received, 0% packet loss, time 4020ms
39// rtt min/avg/max/mdev = 20/20/20/0 ms
40//
41// When using IPv4, the output will look like this:
42//
43// PING 10.1.2.2 - 56 bytes of data; 84 bytes including ICMP and IPv4 headers.
44// 64 bytes from (10.1.2.2): icmp_seq=0 ttl=63 time=20.027 ms
45// 64 bytes from (10.1.2.2): icmp_seq=1 ttl=63 time=20.027 ms
46// 64 bytes from (10.1.2.2): icmp_seq=2 ttl=63 time=20.027 ms
47// 64 bytes from (10.1.2.2): icmp_seq=3 ttl=63 time=20.027 ms
48// 64 bytes from (10.1.2.2): icmp_seq=4 ttl=63 time=20.027 ms
49// --- 10.1.2.2 ping statistics ---
50// 5 packets transmitted, 5 received, 0% packet loss, time 4020ms
51// rtt min/avg/max/mdev = 20/20/20/0 ms
52//
53// The example program will also produce four pcap traces (one for each
54// NetDevice in the scenario) that can be viewed using tcpdump or Wireshark.
55//
56// Other program options include options to change the destination and
57// source addresses, number of packets (count), packet size, interval,
58// and whether to enable logging (if logging is enabled in the build).
59//
60// The Ping application in this example starts at simulation time 1 and will
61// stop either at simulation time 50 or once 'Count' pings have been responded
62// to, whichever comes first.
63
64#include "ns3/core-module.h"
65#include "ns3/internet-apps-module.h"
66#include "ns3/internet-module.h"
67#include "ns3/network-module.h"
68#include "ns3/nix-vector-routing-module.h"
69#include "ns3/point-to-point-module.h"
70
71#include <fstream>
72
73using namespace ns3;
74
75NS_LOG_COMPONENT_DEFINE("PingExample");
76
77int
78main(int argc, char* argv[])
79{
80 bool logging{false};
81 Time interPacketInterval{Seconds(1)};
82 uint32_t size{56};
83 uint32_t count{5};
84 std::string destinationStr;
85 Address destination;
86 std::string sourceStr;
88 bool useIpv6{true};
89
90 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
91
93 cmd.AddValue("logging", "Tell application to log if true", logging);
94 cmd.AddValue("interval", "The time to wait between two packets", interPacketInterval);
95 cmd.AddValue("size", "Data bytes to be sent, per-packet", size);
96 cmd.AddValue("count", "Number of packets to be sent", count);
97 cmd.AddValue("destination",
98 "Destination IPv4 or IPv6 address, e.g., \"10.1.2.2\"",
100 cmd.AddValue("source",
101 "Source address, needed only for multicast or broadcast destinations",
102 sourceStr);
103 cmd.Parse(argc, argv);
104
105 if (!destinationStr.empty())
106 {
109 if (v4Dst.IsInitialized())
110 {
111 useIpv6 = false;
112 destination = v4Dst;
113 }
114 else if (v6Dst.IsInitialized())
115 {
116 useIpv6 = true;
117 destination = v6Dst;
118 }
119 }
120
121 if (!sourceStr.empty())
122 {
123 Ipv4Address v4Src(sourceStr.c_str());
124 Ipv6Address v6Src(sourceStr.c_str());
125 if (v4Src.IsInitialized())
126 {
127 source = v4Src;
128 }
129 else if (v6Src.IsInitialized())
130 {
131 source = v6Src;
132 }
133 }
134 if (sourceStr.empty())
135 {
136 if (useIpv6)
137 {
139 if (v6Dst.IsInitialized() && v6Dst.IsMulticast())
140 {
141 std::cout << "Specify a source address to use when pinging multicast addresses"
142 << std::endl;
143 std::cout << "Program exiting..." << std::endl;
144 return 0;
145 }
146 }
147 else
148 {
150 if (v4Dst.IsInitialized() && (v4Dst.IsBroadcast() || v4Dst.IsMulticast()))
151 {
152 std::cout << "Specify a source address to use when pinging broadcast or multicast "
153 "addresses"
154 << std::endl;
155 std::cout << "Program exiting..." << std::endl;
156 return 0;
157 }
158 }
159 }
160
161 if (logging)
162 {
165 }
166
168 nodes.Create(3);
171 link1Nodes.Add(nodes.Get(1));
174 link2Nodes.Add(nodes.Get(2));
175
177 pointToPoint.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
178 pointToPoint.SetChannelAttribute("Delay", StringValue("5ms"));
179
184
185 // The following block of code inserts an optional packet loss model
188 std::list<uint32_t> dropList;
189 // Enable one or more of the below lines to force specific packet losses
190 // dropList.push_back (0);
191 // dropList.push_back (1);
192 // dropList.push_back (2);
193 // dropList.push_back (3);
194 // dropList.push_back (4);
195 // etc. (other lines may be added)
196 errorModel->SetList(dropList);
197 p2pSender->SetReceiveErrorModel(errorModel);
198
199 if (!useIpv6)
200 {
203 stack.SetRoutingHelper(nixRouting);
204 stack.SetIpv6StackInstall(false);
205 stack.Install(nodes);
206
208 addressV4.SetBase("10.1.1.0", "255.255.255.0");
209 addressV4.Assign(link1Devices);
210 addressV4.NewNetwork();
212
213 if (destination.IsInvalid())
214 {
215 destination = link2InterfacesV4.GetAddress(1, 0);
216 }
217 }
218 else
219 {
222 stack.SetRoutingHelper(nixRouting);
223 stack.SetIpv4StackInstall(false);
224 stack.Install(nodes);
225
227 addressV6.SetBase("2001:1::", 64);
228 addressV6.Assign(link1Devices);
229 addressV6.NewNetwork();
231
232 if (destination.IsInvalid())
233 {
234 destination = link2InterfacesV6.GetAddress(1, 1);
235 }
236 }
237
238 // Create Ping application and installing on node A
239 PingHelper pingHelper(destination, source);
240 pingHelper.SetAttribute("Interval", TimeValue(interPacketInterval));
241 pingHelper.SetAttribute("Size", UintegerValue(size));
242 pingHelper.SetAttribute("Count", UintegerValue(count));
244 apps.Start(Seconds(1));
245 apps.Stop(Seconds(50));
246
247 pointToPoint.EnablePcapAll("ping-example");
248
252 return 0;
253}
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
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.
AttributeValue implementation for Boolean.
Definition boolean.h:26
Parse command-line arguments.
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4 addresses are stored in host order in this class.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Describes an IPv6 address.
Keep track of a set of IPv6 interfaces.
holds a vector of ns3::NetDevice pointers
Helper class that adds Nix-vector routing to nodes.
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.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:66
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
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
AttributeValue implementation for Time.
Definition nstime.h:1431
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
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:1344
NodeContainer nodes
pointToPoint
Definition first.py:27
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
LogLevel
Logging severity classes and levels.
Definition log.h:83
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:109
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309