A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ping.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Chandrakant Jena
3 * Copyright (c) 2007-2009 Strasbourg University (original Ping6 code)
4 * Copyright (c) 2008 INRIA (original v4Ping code)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation;
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Author: Chandrakant Jena <chandrakant.barcelona@gmail.com>
20 *
21 * Derived from original v4Ping application (author: Mathieu Lacage)
22 * Derived from original ping6 application (author: Sebastien Vincent)
23 */
24
25#ifndef PING_H
26#define PING_H
27
28#include "ns3/application.h"
29#include "ns3/average.h"
30#include "ns3/traced-callback.h"
31
32#include <map>
33
34namespace ns3
35{
36
37class Socket;
38
39/**
40 * \ingroup internet-apps
41 * \defgroup ping Ping
42 */
43
44/**
45 * \ingroup ping
46 *
47 * This application behaves similarly to the Unix ping application, although
48 * with fewer options supported. The application can be used to send
49 * ICMP echo requests to unicast IPv4 and IPv6 addresses.
50 * The application can produce a verbose output similar to the real
51 * application, and can also export statistics via a trace source.
52 * The ping packet count, packet size, and interval between pings can
53 * be controlled via attributes of this class.
54 */
55class Ping : public Application
56{
57 public:
58 /**
59 * \brief Get the type ID.
60 * \return the object TypeId
61 */
62 static TypeId GetTypeId();
63
64 /**
65 * \enum VerboseMode
66 * \brief Encode three possible levels of verbose output
67 */
69 {
70 VERBOSE = 0, //!< Verbose output (similar to real ping output)
71 QUIET = 1, //!< Quiet output (similar to real 'ping -q' output)
72 SILENT = 2, //!< Silent output (no terminal output at all)
73 };
74
75 /**
76 * \enum DropReason
77 * \brief Reason why a ping was dropped
78 */
80 {
81 DROP_TIMEOUT = 0, //!< Response timed out
82 DROP_HOST_UNREACHABLE, //!< Received ICMP Destination Host Unreachable
83 DROP_NET_UNREACHABLE, //!< Received ICMP Destination Network Unreachable
84 };
85
86 /**
87 * A ping report provides all of the data that is typically output to the
88 * terminal when the application stops, including number sent and received
89 * and the RTT statistics.
90 */
92 {
93 uint32_t m_transmitted{0}; //!< Number of echo requests sent
94 uint32_t m_received{0}; //!< Number of echo replies received
95 uint16_t m_loss{0}; //!< Percentage of lost packets (decimal value 0-100)
96 Time m_duration{0}; //!< Duration of the application
97 double m_rttMin{0}; //!< rtt min value
98 double m_rttAvg{0}; //!< rtt avg value
99 double m_rttMax{0}; //!< rtt max value
100 double m_rttMdev{0}; //!< rtt mdev value
101 };
102
103 /**
104 * Constructor
105 */
106 Ping();
107
108 /**
109 * Destructor
110 */
111 ~Ping() override;
112
113 /**
114 * \brief Set routers for IPv6 routing type 0 (loose routing).
115 * \param routers routers addresses
116 */
117 void SetRouters(const std::vector<Ipv6Address>& routers);
118
119 /**
120 * TracedCallback signature for Rtt trace
121 *
122 * \param [in] seq The ICMP sequence number
123 * \param [in] p The ICMP echo request packet (including ICMP header)
124 */
125 typedef void (*TxTrace)(uint16_t seq, Ptr<const Packet> p);
126
127 /**
128 * TracedCallback signature for Rtt trace
129 *
130 * \param [in] seq The ICMP sequence number
131 * \param [in] rtt The reported RTT
132 */
133 typedef void (*RttTrace)(uint16_t seq, Time rtt);
134
135 /**
136 * TracedCallback signature for Drop trace
137 *
138 * \param [in] seq The ICMP sequence number
139 * \param [in] reason The reason for the reported drop
140 */
141 typedef void (*DropTrace)(uint16_t seq, DropReason reason);
142
143 /**
144 * TracedCallback signature for Report trace
145 *
146 * \param [in] report The report information
147 */
148 typedef void (*ReportTrace)(const PingReport& report);
149
150 protected:
151 void DoDispose() override;
152
153 private:
154 void StartApplication() override;
155 void StopApplication() override;
156
157 /**
158 * \brief Writes data to buffer in little-endian format.
159 *
160 * Least significant byte of data is at lowest buffer address
161 *
162 * \param[out] buffer the buffer to write to
163 * \param[in] data the data to write
164 */
165 void Write64(uint8_t* buffer, const uint64_t data);
166
167 /**
168 * \brief Writes data from a little-endian formatted buffer to data.
169 *
170 * \param buffer the buffer to read from
171 * \return the read data
172 */
173 uint64_t Read64(const uint8_t* buffer);
174
175 /**
176 * \brief Return the application signatiure.
177 * \returns the application signature.
178 *
179 * The application signature is the NodeId concatenated with the
180 * application index in the node.
181 */
182 uint64_t GetApplicationSignature() const;
183
184 /**
185 * \brief Receive an ICMPv4 or an ICMPv6 Echo reply
186 * \param socket the receiving socket
187 *
188 * This function is called by lower layers through a callback.
189 */
190 void Receive(Ptr<Socket> socket);
191
192 /**
193 * \brief Send one Ping (ICMPv4 ECHO or ICMPv6 ECHO) to the destination
194 */
195 void Send();
196
197 /**
198 * Print the report
199 */
200 void PrintReport();
201
202 /// Sender Local Address
204 /// Remote address
206 /// Wait interval between ECHO requests
208
209 /**
210 * Specifies the number of data bytes to be sent.
211 * The default is 56, which translates into 64 ICMP data bytes when combined with the 8 bytes of
212 * ICMP header data.
213 */
215 /// The socket we send packets from
217 /// The Type of Service carried by ICMP ECHOs
218 uint8_t m_tos;
219 /// ICMP ECHO sequence number
220 uint16_t m_seq{0};
221 /// Callbacks for tracing the packet Tx events
223 /// TracedCallback for RTT samples
225 /// TracedCallback for drop events
227 /// TracedCallback for final ping report
229 /// Variable to stor verbose mode
231 /// Received packets counter
233 /// Duplicate packets counter
235 /// Start time to report total ping time
237 /// Average rtt is ms
239 /// Next packet will be sent
241
242 /**
243 * \brief Sent echo request data.
244 */
246 {
247 public:
248 /**
249 * Constructor.
250 * \param txTimePar Echo request Tx time.
251 * \param ackedPar True if the Echo request has been acknowledged at least once.
252 */
253 EchoRequestData(Time txTimePar, bool ackedPar)
254 : txTime(txTimePar),
255 acked(ackedPar)
256 {
257 }
258
259 Time txTime; //!< Tx time
260 bool acked{false}; //!< True if packet has been acknowledged
261 };
262
263 /// All sent but not answered packets. Map icmp seqno -> when sent, acked at least once.
264 std::vector<EchoRequestData> m_sent;
265 /// Number of packets to be sent.
267 /// Time to wait for a response, in seconds. The option affects only timeout in absence of any
268 /// responses, otherwise ping waits for two RTTs
270 /// True if the report has been printed already.
271 bool m_reportPrinted{false};
272 /// Use IPv4 (false) or IPv6 (true)
273 bool m_useIpv6{false};
274 /// Destination is Broadcast or Multicast
276
277 /// Routers addresses for IPv6 routing type 0.
278 std::vector<Ipv6Address> m_routers;
279
280 /// App signature: ID of the node where the app is installed || ID of the Application.
281 uint64_t m_appSignature{0};
282};
283
284} // namespace ns3
285
286#endif /* PING_H */
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
Simple average, min, max and std.
Definition: average.h:43
An identifier for simulation events.
Definition: event-id.h:55
Sent echo request data.
Definition: ping.h:246
EchoRequestData(Time txTimePar, bool ackedPar)
Constructor.
Definition: ping.h:253
bool acked
True if packet has been acknowledged.
Definition: ping.h:260
Time txTime
Tx time.
Definition: ping.h:259
This application behaves similarly to the Unix ping application, although with fewer options supporte...
Definition: ping.h:56
uint32_t m_size
Specifies the number of data bytes to be sent.
Definition: ping.h:214
uint32_t m_recv
Received packets counter.
Definition: ping.h:232
Time m_started
Start time to report total ping time.
Definition: ping.h:236
bool m_reportPrinted
True if the report has been printed already.
Definition: ping.h:271
std::vector< EchoRequestData > m_sent
All sent but not answered packets. Map icmp seqno -> when sent, acked at least once.
Definition: ping.h:264
uint64_t Read64(const uint8_t *buffer)
Writes data from a little-endian formatted buffer to data.
Definition: ping.cc:400
void(* RttTrace)(uint16_t seq, Time rtt)
TracedCallback signature for Rtt trace.
Definition: ping.h:133
void(* TxTrace)(uint16_t seq, Ptr< const Packet > p)
TracedCallback signature for Rtt trace.
Definition: ping.h:125
bool m_useIpv6
Use IPv4 (false) or IPv6 (true)
Definition: ping.h:273
VerboseMode
Encode three possible levels of verbose output.
Definition: ping.h:69
@ SILENT
Silent output (no terminal output at all)
Definition: ping.h:72
@ VERBOSE
Verbose output (similar to real ping output)
Definition: ping.h:70
@ QUIET
Quiet output (similar to real 'ping -q' output)
Definition: ping.h:71
void SetRouters(const std::vector< Ipv6Address > &routers)
Set routers for IPv6 routing type 0 (loose routing).
Definition: ping.cc:698
std::vector< Ipv6Address > m_routers
Routers addresses for IPv6 routing type 0.
Definition: ping.h:278
~Ping() override
Destructor.
Definition: ping.cc:136
TracedCallback< uint16_t, Ptr< Packet > > m_txTrace
Callbacks for tracing the packet Tx events.
Definition: ping.h:222
uint64_t m_appSignature
App signature: ID of the node where the app is installed || ID of the Application.
Definition: ping.h:281
uint32_t m_count
Number of packets to be sent.
Definition: ping.h:266
uint64_t GetApplicationSignature() const
Return the application signatiure.
Definition: ping.cc:151
void Send()
Send one Ping (ICMPv4 ECHO or ICMPv6 ECHO) to the destination.
Definition: ping.cc:423
void DoDispose() override
Destructor implementation.
Definition: ping.cc:142
void(* DropTrace)(uint16_t seq, DropReason reason)
TracedCallback signature for Drop trace.
Definition: ping.h:141
void StartApplication() override
Application specific startup code.
Definition: ping.cc:525
TracedCallback< const PingReport & > m_reportTrace
TracedCallback for final ping report.
Definition: ping.h:228
void StopApplication() override
Application specific shutdown code.
Definition: ping.cc:626
DropReason
Reason why a ping was dropped.
Definition: ping.h:80
@ DROP_TIMEOUT
Response timed out.
Definition: ping.h:81
@ DROP_NET_UNREACHABLE
Received ICMP Destination Network Unreachable.
Definition: ping.h:83
@ DROP_HOST_UNREACHABLE
Received ICMP Destination Host Unreachable.
Definition: ping.h:82
Average< double > m_avgRtt
Average rtt is ms.
Definition: ping.h:238
void PrintReport()
Print the report.
Definition: ping.cc:645
Ping()
Constructor.
Definition: ping.cc:131
Time m_interval
Wait interval between ECHO requests.
Definition: ping.h:207
static TypeId GetTypeId()
Get the type ID.
Definition: ping.cc:57
bool m_multipleDestinations
Destination is Broadcast or Multicast.
Definition: ping.h:275
uint16_t m_seq
ICMP ECHO sequence number.
Definition: ping.h:220
uint32_t m_duplicate
Duplicate packets counter.
Definition: ping.h:234
Address m_interfaceAddress
Sender Local Address.
Definition: ping.h:203
void Receive(Ptr< Socket > socket)
Receive an ICMPv4 or an ICMPv6 Echo reply.
Definition: ping.cc:172
uint8_t m_tos
The Type of Service carried by ICMP ECHOs.
Definition: ping.h:218
void Write64(uint8_t *buffer, const uint64_t data)
Writes data to buffer in little-endian format.
Definition: ping.cc:385
Address m_destination
Remote address.
Definition: ping.h:205
EventId m_next
Next packet will be sent.
Definition: ping.h:240
Ptr< Socket > m_socket
The socket we send packets from.
Definition: ping.h:216
VerboseMode m_verbose
Variable to stor verbose mode.
Definition: ping.h:230
TracedCallback< uint16_t, DropReason > m_dropTrace
TracedCallback for drop events.
Definition: ping.h:226
Time m_timeout
Time to wait for a response, in seconds.
Definition: ping.h:269
void(* ReportTrace)(const PingReport &report)
TracedCallback signature for Report trace.
Definition: ping.h:148
TracedCallback< uint16_t, Time > m_rttTrace
TracedCallback for RTT samples.
Definition: ping.h:224
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
A ping report provides all of the data that is typically output to the terminal when the application ...
Definition: ping.h:92
Time m_duration
Duration of the application.
Definition: ping.h:96
uint16_t m_loss
Percentage of lost packets (decimal value 0-100)
Definition: ping.h:95
double m_rttAvg
rtt avg value
Definition: ping.h:98
double m_rttMdev
rtt mdev value
Definition: ping.h:100
uint32_t m_received
Number of echo replies received.
Definition: ping.h:94
double m_rttMin
rtt min value
Definition: ping.h:97
uint32_t m_transmitted
Number of echo requests sent.
Definition: ping.h:93
double m_rttMax
rtt max value
Definition: ping.h:99