A Discrete-Event Network Simulator
API
ping-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Chandrakant Jena
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: Chandrakant Jena <chandrakant.barcelona@gmail.com>
18  */
19 
20 // Example to demonstrate the working of the Ping Application
21 // Network topology:
22 // A ------------------------------ B ------------------------------ C
23 // 100 Mbps 100 Mbps
24 // 5 ms (one way) 5 ms (one way)
25 // IPv4 addresses:
26 // 10.1.1.1 <-> 10.1.1.2 / 10.1.2.1 <-> 10.1.2.2
27 //
28 // IPv6 addresses:
29 // 2001:1::200:ff:fe00:1
30 // <-> 2001:1::200:ff:fe00:2 / 2001:1:0:1:200:ff:fe00:3
31 // <-> 2001:1:0:1:200:ff:fe00:4
32 //
33 // The topology has three nodes interconnected by two point-to-point links.
34 // Each link has 5 ms one-way delay, for a round-trip propagation delay
35 // of 20 ms. The transmission rate on each link is 100 Mbps. The routing
36 // between links is enabled by ns-3's NixVector routing.
37 //
38 // By default, this program will send 5 pings from node A to node C using IPv6.
39 // When using IPv6, the output will look like this:
40 //
41 // PING 2001:1:0:1:200:ff:fe00:4 - 56 bytes of data; 104 bytes including ICMP and IPv6 headers.
42 // 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=0 ttl=63 time=20.033 ms
43 // 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=1 ttl=63 time=20.033 ms
44 // 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=2 ttl=63 time=20.033 ms
45 // 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=3 ttl=63 time=20.033 ms
46 // 64 bytes from (2001:1:0:1:200:ff:fe00:4): icmp_seq=4 ttl=63 time=20.033 ms
47 //
48 // --- 2001:1:0:1:200:ff:fe00:4 ping statistics ---
49 // 5 packets transmitted, 5 received, 0% packet loss, time 4020ms
50 // rtt min/avg/max/mdev = 20/20/20/0 ms
51 //
52 // When using IPv4, the output will look like this:
53 //
54 // PING 10.1.2.2 - 56 bytes of data; 84 bytes including ICMP and IPv4 headers.
55 // 64 bytes from (10.1.2.2): icmp_seq=0 ttl=63 time=20.027 ms
56 // 64 bytes from (10.1.2.2): icmp_seq=1 ttl=63 time=20.027 ms
57 // 64 bytes from (10.1.2.2): icmp_seq=2 ttl=63 time=20.027 ms
58 // 64 bytes from (10.1.2.2): icmp_seq=3 ttl=63 time=20.027 ms
59 // 64 bytes from (10.1.2.2): icmp_seq=4 ttl=63 time=20.027 ms
60 // --- 10.1.2.2 ping statistics ---
61 // 5 packets transmitted, 5 received, 0% packet loss, time 4020ms
62 // rtt min/avg/max/mdev = 20/20/20/0 ms
63 //
64 // The example program will also produce four pcap traces (one for each
65 // NetDevice in the scenario) that can be viewed using tcpdump or Wireshark.
66 //
67 // Other program options include options to change the destination and
68 // source addresses, number of packets (count), packet size, interval,
69 // and whether to enable logging (if logging is enabled in the build).
70 //
71 // The Ping application in this example starts at simulation time 1 and will
72 // stop either at simulation time 50 or once 'Count' pings have been responded
73 // to, whichever comes first.
74 
75 #include "ns3/core-module.h"
76 #include "ns3/internet-apps-module.h"
77 #include "ns3/internet-module.h"
78 #include "ns3/network-module.h"
79 #include "ns3/nix-vector-routing-module.h"
80 #include "ns3/point-to-point-module.h"
81 
82 #include <fstream>
83 
84 using namespace ns3;
85 
86 NS_LOG_COMPONENT_DEFINE("PingExample");
87 
88 int
89 main(int argc, char* argv[])
90 {
91  bool logging{false};
92  Time interPacketInterval{Seconds(1.0)};
93  uint32_t size{56};
94  uint32_t count{5};
95  std::string destinationStr;
96  Address destination;
97  std::string sourceStr;
98  Address source;
99  bool useIpv6{true};
100 
101  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
102 
103  CommandLine cmd(__FILE__);
104  cmd.AddValue("logging", "Tell application to log if true", logging);
105  cmd.AddValue("interval", "The time to wait between two packets", interPacketInterval);
106  cmd.AddValue("size", "Data bytes to be sent, per-packet", size);
107  cmd.AddValue("count", "Number of packets to be sent", count);
108  cmd.AddValue("destination",
109  "Destination IPv4 or IPv6 address, e.g., \"10.1.2.2\"",
110  destinationStr);
111  cmd.AddValue("source",
112  "Source address, needed only for multicast or broadcast destinations",
113  sourceStr);
114  cmd.Parse(argc, argv);
115 
116  if (!destinationStr.empty())
117  {
118  Ipv4Address v4Dst(destinationStr.c_str());
119  Ipv6Address v6Dst(destinationStr.c_str());
120  if (v4Dst.IsInitialized())
121  {
122  useIpv6 = false;
123  destination = v4Dst;
124  }
125  else if (v6Dst.IsInitialized())
126  {
127  useIpv6 = true;
128  destination = v6Dst;
129  }
130  }
131 
132  if (!sourceStr.empty())
133  {
134  Ipv4Address v4Src(sourceStr.c_str());
135  Ipv6Address v6Src(sourceStr.c_str());
136  if (v4Src.IsInitialized())
137  {
138  source = v4Src;
139  }
140  else if (v6Src.IsInitialized())
141  {
142  source = v6Src;
143  }
144  }
145  if (sourceStr.empty())
146  {
147  if (useIpv6)
148  {
149  Ipv6Address v6Dst = Ipv6Address(destinationStr.c_str());
150  if (v6Dst.IsInitialized() && v6Dst.IsMulticast())
151  {
152  std::cout << "Specify a source address to use when pinging multicast addresses"
153  << std::endl;
154  std::cout << "Program exiting..." << std::endl;
155  return 0;
156  }
157  }
158  else
159  {
160  Ipv4Address v4Dst(destinationStr.c_str());
161  if (v4Dst.IsInitialized() && (v4Dst.IsBroadcast() || v4Dst.IsMulticast()))
162  {
163  std::cout << "Specify a source address to use when pinging broadcast or multicast "
164  "addresses"
165  << std::endl;
166  std::cout << "Program exiting..." << std::endl;
167  return 0;
168  }
169  }
170  }
171 
172  if (logging)
173  {
176  }
177 
179  nodes.Create(3);
180  NodeContainer link1Nodes;
181  link1Nodes.Add(nodes.Get(0));
182  link1Nodes.Add(nodes.Get(1));
183  NodeContainer link2Nodes;
184  link2Nodes.Add(nodes.Get(1));
185  link2Nodes.Add(nodes.Get(2));
186 
188  pointToPoint.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
189  pointToPoint.SetChannelAttribute("Delay", StringValue("5ms"));
190 
191  NetDeviceContainer link1Devices;
192  link1Devices = pointToPoint.Install(link1Nodes);
193  NetDeviceContainer link2Devices;
194  link2Devices = pointToPoint.Install(link2Nodes);
195 
196  // The following block of code inserts an optional packet loss model
197  Ptr<PointToPointNetDevice> p2pSender = DynamicCast<PointToPointNetDevice>(link1Devices.Get(0));
198  Ptr<ReceiveListErrorModel> errorModel = CreateObject<ReceiveListErrorModel>();
199  std::list<uint32_t> dropList;
200  // Enable one or more of the below lines to force specific packet losses
201  // dropList.push_back (0);
202  // dropList.push_back (1);
203  // dropList.push_back (2);
204  // dropList.push_back (3);
205  // dropList.push_back (4);
206  // etc. (other lines may be added)
207  errorModel->SetList(dropList);
208  p2pSender->SetReceiveErrorModel(errorModel);
209 
210  if (!useIpv6)
211  {
214  stack.SetRoutingHelper(nixRouting);
215  stack.SetIpv6StackInstall(false);
216  stack.Install(nodes);
217 
218  Ipv4AddressHelper addressV4;
219  addressV4.SetBase("10.1.1.0", "255.255.255.0");
220  addressV4.Assign(link1Devices);
221  addressV4.NewNetwork();
222  Ipv4InterfaceContainer link2InterfacesV4 = addressV4.Assign(link2Devices);
223 
224  if (destination.IsInvalid())
225  {
226  destination = link2InterfacesV4.GetAddress(1, 0);
227  }
228  }
229  else
230  {
233  stack.SetRoutingHelper(nixRouting);
234  stack.SetIpv4StackInstall(false);
235  stack.Install(nodes);
236 
237  Ipv6AddressHelper addressV6;
238  addressV6.SetBase("2001:1::", 64);
239  addressV6.Assign(link1Devices);
240  addressV6.NewNetwork();
241  Ipv6InterfaceContainer link2InterfacesV6 = addressV6.Assign(link2Devices);
242 
243  if (destination.IsInvalid())
244  {
245  destination = link2InterfacesV6.GetAddress(1, 1);
246  }
247  }
248 
249  // Create Ping application and installing on node A
250  PingHelper pingHelper(destination, source);
251  pingHelper.SetAttribute("Interval", TimeValue(interPacketInterval));
252  pingHelper.SetAttribute("Size", UintegerValue(size));
253  pingHelper.SetAttribute("Count", UintegerValue(count));
254  ApplicationContainer apps = pingHelper.Install(nodes.Get(0));
255  apps.Start(Seconds(1));
256  apps.Stop(Seconds(50));
257 
258  pointToPoint.EnablePcapAll("ping-example");
259 
260  Simulator::Stop(Seconds(60.0));
261  Simulator::Run();
263  return 0;
264 }
a polymophic address class
Definition: address.h:101
bool IsInvalid() const
Definition: address.cc:71
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
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.
Ipv4Address NewNetwork()
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
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.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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.
void NewNetwork()
Allocate a new network.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:49
bool IsMulticast() const
If the IPv6 address is multicast (ff00::/8).
bool IsInitialized() const
Keep track of a set of IPv6 interfaces.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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:48
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
pointToPoint
Definition: first.py:38
stack
Definition: first.py:44
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:302
LogLevel
Logging severity classes and levels.
Definition: log.h:94
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:320
cmd
Definition: second.py:40