A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
v4traceroute.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
18 *
19 * Traceroute uses ICMPV4 echo messages to trace all the middle hops to a given destination.
20 * It also shows the delay time it takes for a round trip to complete for each
21 * set probe (default 3).
22 *
23 */
24
25#include "v4traceroute.h"
26
27#include "ns3/assert.h"
28#include "ns3/boolean.h"
29#include "ns3/icmpv4-l4-protocol.h"
30#include "ns3/icmpv4.h"
31#include "ns3/inet-socket-address.h"
32#include "ns3/ipv4-address.h"
33#include "ns3/log.h"
34#include "ns3/packet.h"
35#include "ns3/socket.h"
36#include "ns3/trace-source-accessor.h"
37#include "ns3/uinteger.h"
38
39namespace ns3
40{
41
42NS_LOG_COMPONENT_DEFINE("V4TraceRoute");
43NS_OBJECT_ENSURE_REGISTERED(V4TraceRoute);
44
45TypeId
47{
48 static TypeId tid =
49 TypeId("ns3::V4TraceRoute")
51 .SetGroupName("Internet-Apps")
52 .AddConstructor<V4TraceRoute>()
53 .AddAttribute("Remote",
54 "The address of the machine we want to trace.",
58 .AddAttribute("Tos",
59 "The Type of Service used to send IPv4 packets. "
60 "All 8 bits of the TOS byte are set (including ECN bits).",
63 MakeUintegerChecker<uint8_t>())
64 .AddAttribute("Verbose",
65 "Produce usual output.",
66 BooleanValue(true),
69 .AddAttribute("Interval",
70 "Wait interval between sent packets.",
74 .AddAttribute("Size",
75 "The number of data bytes to be sent, real packet will "
76 "be 8 (ICMP) + 20 (IP) bytes longer.",
77 UintegerValue(56),
79 MakeUintegerChecker<uint32_t>())
80 .AddAttribute("MaxHop",
81 "The maximum number of hops to trace.",
82 UintegerValue(30),
84 MakeUintegerChecker<uint32_t>())
85 .AddAttribute("ProbeNum",
86 "The number of packets send to each hop.",
89 MakeUintegerChecker<uint16_t>())
90 .AddAttribute("Timeout",
91 "The waiting time for a route response before a timeout.",
95 return tid;
96}
97
99 : m_interval(Seconds(0)),
100 m_size(56),
101 m_socket(nullptr),
102 m_seq(0),
103 m_verbose(true),
104 m_probeCount(0),
105 m_maxProbes(3),
106 m_ttl(1),
107 m_maxTtl(30),
108 m_waitIcmpReplyTimeout(Seconds(5))
109{
110 m_osRoute.clear();
111 m_routeIpv4.clear();
112}
113
115{
116}
117
118void
120{
121 m_printStream = stream;
122}
123
124void
126{
127 NS_LOG_FUNCTION(this);
128 NS_LOG_LOGIC("Application started");
130
131 NS_ABORT_MSG_IF(!m_remote.IsInitialized(), "'Remote' attribute not properly set");
132
133 if (m_verbose)
134 {
135 NS_LOG_UNCOND("Traceroute to " << m_remote << ", " << m_maxTtl << " hops Max, " << m_size
136 << " bytes of data.");
137 }
138
139 if (m_printStream)
140 {
141 *m_printStream->GetStream() << "Traceroute to " << m_remote << ", " << m_maxTtl
142 << " hops Max, " << m_size << " bytes of data.\n";
143 }
144
145 m_socket = Socket::CreateSocket(GetNode(), TypeId::LookupByName("ns3::Ipv4RawSocketFactory"));
147 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
148
151
153 int status;
154 status = m_socket->Bind(src);
155 NS_ASSERT(status != -1);
156
158}
159
160void
162{
163 NS_LOG_FUNCTION(this);
164
165 if (m_next.IsRunning())
166 {
167 m_next.Cancel();
168 }
169
171 {
173 }
174
175 if (m_socket)
176 {
177 m_socket->Close();
178 }
179
180 if (m_verbose)
181 {
182 NS_LOG_UNCOND("\nTrace Complete");
183 }
184
185 if (m_printStream)
186 {
187 *m_printStream->GetStream() << "Trace Complete\n" << std::endl;
188 }
189}
190
191void
193{
194 NS_LOG_FUNCTION(this);
195
197 {
199 }
200
201 m_socket = nullptr;
203}
204
207{
208 NS_LOG_FUNCTION(this);
209 Ptr<Node> node = GetNode();
210 for (uint32_t i = 0; i < node->GetNApplications(); ++i)
211 {
212 if (node->GetApplication(i) == this)
213 {
214 return i;
215 }
216 }
217 NS_ASSERT_MSG(false, "forgot to add application to node");
218 return 0;
219}
220
221void
223{
224 NS_LOG_FUNCTION(this << socket);
225
226 while (m_socket->GetRxAvailable() > 0)
227 {
228 Address from;
229 Ptr<Packet> p = m_socket->RecvFrom(0xffffffff, 0, from);
230 NS_LOG_DEBUG("recv " << p->GetSize() << " bytes");
233 NS_ASSERT(realFrom.GetPort() == 1);
234 Ipv4Header ipv4;
235 p->RemoveHeader(ipv4);
236 NS_ASSERT(ipv4.GetProtocol() == Icmpv4L4Protocol::PROT_NUMBER);
237 Icmpv4Header icmp;
238 p->RemoveHeader(icmp);
239
241 {
242 Icmpv4TimeExceeded timeoutResp;
243 p->RemoveHeader(timeoutResp);
244
245 // GetData () gets 64 bits of data, but the received packet
246 // only contains 32 bits of data.
247 uint8_t data[8];
248 timeoutResp.GetData(data);
249
250 // Get the 7th and 8th Octet to obtain the Sequence number from
251 // the original packet.
252 uint16_t recvSeq;
253 recvSeq = (uint16_t)data[7] << 0;
254 recvSeq |= (uint16_t)data[6] << 8;
255
256 auto i = m_sent.find(recvSeq);
257 if (i != m_sent.end())
258 {
259 Time sendTime = i->second;
260 NS_ASSERT(Simulator::Now() >= sendTime);
261 Time delta = Simulator::Now() - sendTime;
262
263 m_routeIpv4.str("");
264 m_routeIpv4.clear();
265 m_routeIpv4 << realFrom.GetIpv4();
266 m_osRoute << delta.As(Time::MS);
268 {
269 if (m_verbose)
270 {
271 NS_LOG_UNCOND(m_ttl << " " << m_routeIpv4.str() << " " << m_osRoute.str());
272 }
273
274 if (m_printStream)
275 {
277 << m_ttl << " " << m_routeIpv4.str() << " " << m_osRoute.str() << "\n";
278 }
279 m_osRoute.str("");
280 m_osRoute.clear();
281 m_routeIpv4.str("");
282 m_routeIpv4.clear();
283 }
284 else
285 {
286 m_osRoute << " ";
287 }
288
290
291 if (m_ttl < m_maxTtl + 1)
292 {
293 m_next =
295 }
296 }
297 }
298 else if (icmp.GetType() == Icmpv4Header::ICMPV4_ECHO_REPLY &&
299 m_remote == realFrom.GetIpv4())
300 {
301 // When UDP is used, TraceRoute should stop until ICMPV4_DEST_UNREACH
302 // (with code (3) PORT_UNREACH) is received, however, the current
303 // ns-3 implementation does not include the UDP version of traceroute.
304 // The traceroute ICMP version (the current version) stops until max_ttl is reached
305 // or until an ICMP ECHO REPLY is received m_maxProbes times.
306
307 Icmpv4Echo echo;
308 p->RemoveHeader(echo);
309 auto i = m_sent.find(echo.GetSequenceNumber());
310
311 if (i != m_sent.end() && echo.GetIdentifier() == 0)
312 {
313 uint32_t dataSize = echo.GetDataSize();
314
315 if (dataSize == m_size)
316 {
317 Time sendTime = i->second;
318 NS_ASSERT(Simulator::Now() >= sendTime);
319 Time delta = Simulator::Now() - sendTime;
320
321 m_sent.erase(i);
322
323 if (m_verbose)
324 {
325 m_routeIpv4.str("");
326 m_routeIpv4.clear();
327 m_routeIpv4 << realFrom.GetIpv4();
328 m_osRoute << delta.As(Time::MS);
329
331 {
332 NS_LOG_UNCOND(m_ttl << " " << m_routeIpv4.str() << " "
333 << m_osRoute.str());
334 if (m_printStream)
335 {
336 *m_printStream->GetStream() << m_ttl << " " << m_routeIpv4.str()
337 << " " << m_osRoute.str() << "\n";
338 }
339
340 m_osRoute.clear();
341 m_routeIpv4.clear();
342 }
343 else
344 {
345 m_osRoute << " ";
346 }
347 }
348 }
349 }
350
353 {
354 if (m_verbose)
355 {
356 NS_LOG_UNCOND("\nTrace Complete");
357 }
358
359 if (m_printStream)
360 {
361 *m_printStream->GetStream() << "Trace Complete\n" << std::endl;
362 }
364 }
365 else if (m_ttl < m_maxTtl + 1)
366 {
368 }
369 }
370 }
371}
372
373void
375{
376 NS_LOG_INFO("m_seq=" << m_seq);
377 Ptr<Packet> p = Create<Packet>();
378 Icmpv4Echo echo;
380 m_seq++;
381 echo.SetIdentifier(0);
382
383 //
384 // We must write quantities out in some form of network order. Since there
385 // isn't an htonl to work with we just follow the convention in pcap traces
386 // (where any difference would show up anyway) and borrow that code. Don't
387 // be too surprised when you see that this is a little endian convention.
388 //
389 NS_ASSERT(m_size >= 16);
390
391 Ptr<Packet> dataPacket = Create<Packet>(m_size);
392 echo.SetData(dataPacket);
393 p->AddHeader(echo);
394 Icmpv4Header header;
396 header.SetCode(0);
398 {
399 header.EnableChecksum();
400 }
401
402 p->AddHeader(header);
403
405 {
406 m_probeCount++;
407 }
408 else
409 {
410 m_probeCount = 1;
411 m_ttl++;
412 }
413
414 m_sent.insert(std::make_pair(m_seq - 1, Simulator::Now()));
416
418 m_socket->SendTo(p, 0, dst);
419}
420
421void
423{
424 NS_LOG_FUNCTION(this);
426 {
427 NS_LOG_LOGIC("Starting WaitIcmpReplyTimer at " << Simulator::Now() << " for "
429
432 this);
433 Send();
434 }
435}
436
437void
439{
440 if (m_ttl < m_maxTtl + 1)
441 {
443 }
444
445 m_osRoute << "* ";
447 {
448 if (m_verbose)
449 {
450 NS_LOG_UNCOND(m_ttl << " " << m_routeIpv4.str() << " " << m_osRoute.str());
451 }
452
453 if (m_printStream)
454 {
456 << m_ttl << " " << m_routeIpv4.str() << " " << m_osRoute.str() << "\n";
457 }
458 m_osRoute.str("");
459 m_osRoute.clear();
460 m_routeIpv4.str("");
461 m_routeIpv4.clear();
462 }
463}
464
465} // namespace ns3
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
void DoDispose() override
Destructor implementation.
Definition: application.cc:86
Ptr< Node > GetNode() const
Definition: application.cc:108
AttributeValue implementation for Boolean.
Definition: boolean.h:37
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
ICMP Echo header.
Definition: icmpv4.h:110
void SetIdentifier(uint16_t id)
Set the Echo identifier.
Definition: icmpv4.cc:150
void SetData(Ptr< const Packet > data)
Set the Echo data.
Definition: icmpv4.cc:164
uint16_t GetIdentifier() const
Get the Echo identifier.
Definition: icmpv4.cc:186
void SetSequenceNumber(uint16_t seq)
Set the Echo sequence number.
Definition: icmpv4.cc:157
uint32_t GetDataSize() const
Get the Echo data size.
Definition: icmpv4.cc:200
uint16_t GetSequenceNumber() const
Get the Echo sequence number.
Definition: icmpv4.cc:193
Base class for all the ICMP packet headers.
Definition: icmpv4.h:43
@ ICMPV4_TIME_EXCEEDED
Definition: icmpv4.h:53
void SetCode(uint8_t code)
Set ICMP code.
Definition: icmpv4.cc:123
void SetType(uint8_t type)
Set ICMP type.
Definition: icmpv4.cc:116
void EnableChecksum()
Enables ICMP Checksum calculation.
Definition: icmpv4.cc:60
uint8_t GetType() const
Get ICMP type.
Definition: icmpv4.cc:130
static const uint8_t PROT_NUMBER
ICMP protocol number (0x1)
ICMP Time Exceeded header.
Definition: icmpv4.h:250
void GetData(uint8_t payload[8]) const
Get the ICMP carried data.
Definition: icmpv4.cc:469
an Inet address class
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
static Ipv4Address GetAny()
bool IsInitialized() const
AttributeValue implementation for Ipv4Address.
Definition: ipv4-address.h:341
Packet header for IPv4.
Definition: ipv4-header.h:34
static bool ChecksumEnabled()
Definition: node.cc:278
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:211
std::ostream * GetStream()
Return a pointer to an ostream previously set in the wrapper.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
virtual void SetIpTtl(uint8_t ipTtl)
Manually set IP Time to Live field.
Definition: socket.cc:510
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
Definition: socket.cc:434
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
virtual uint32_t GetRxAvailable() const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:72
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
@ MS
millisecond
Definition: nstime.h:117
AttributeValue implementation for Time.
Definition: nstime.h:1406
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Hold an unsigned integer type.
Definition: uinteger.h:45
Traceroute application sends one ICMP ECHO request with TTL=1, and after receiving an ICMP TIME EXCEE...
Definition: v4traceroute.h:51
~V4TraceRoute() override
Ipv4Address m_remote
Remote address.
Definition: v4traceroute.h:98
void HandleWaitReplyTimeout()
Triggers an action if an ICMP TIME EXCEED have not being received in the time defined by StartWaitRep...
void StopApplication() override
Application specific shutdown code.
EventId m_next
Next packet will be sent.
Definition: v4traceroute.h:117
std::ostringstream m_routeIpv4
The Ipv4 address of the latest hop found.
Definition: v4traceroute.h:138
void StartApplication() override
Application specific startup code.
Ptr< OutputStreamWrapper > m_printStream
Stream of the traceroute used for the output file.
Definition: v4traceroute.h:140
uint32_t m_probeCount
The Current probe value.
Definition: v4traceroute.h:119
uint16_t m_seq
ICMP ECHO sequence number.
Definition: v4traceroute.h:111
uint32_t GetApplicationId() const
Return the application ID in the node.
uint16_t m_ttl
The current TTL value.
Definition: v4traceroute.h:123
std::map< uint16_t, Time > m_sent
All sent but not answered packets. Map icmp seqno -> when sent.
Definition: v4traceroute.h:133
void DoDispose() override
Destructor implementation.
Ptr< Socket > m_socket
The socket we send packets from.
Definition: v4traceroute.h:109
uint32_t m_maxTtl
The maximum Ttl (Max number of hops to trace)
Definition: v4traceroute.h:127
Time m_interval
Wait interval seconds between sending each packet.
Definition: v4traceroute.h:101
void Print(Ptr< OutputStreamWrapper > stream)
Prints the application traced routes into a given OutputStream.
std::ostringstream m_osRoute
Stream of characters used for printing a single route.
Definition: v4traceroute.h:136
Time m_started
Start time to report total ping time.
Definition: v4traceroute.h:115
EventId m_waitIcmpReplyTimer
The timer used to wait for the probes ICMP replies.
Definition: v4traceroute.h:131
uint32_t m_size
Specifies the number of data bytes to be sent.
Definition: v4traceroute.h:107
uint8_t m_tos
The packets Type of Service.
Definition: v4traceroute.h:125
uint16_t m_maxProbes
The maximum number of probe packets per hop.
Definition: v4traceroute.h:121
Time m_waitIcmpReplyTimeout
The wait time until the response is considered lost.
Definition: v4traceroute.h:129
void Receive(Ptr< Socket > socket)
Receive an ICMP Echo.
static TypeId GetTypeId()
Get the type ID.
Definition: v4traceroute.cc:46
void StartWaitReplyTimer()
Starts a timer after sending an ICMP ECHO.
bool m_verbose
produce traceroute style output if true
Definition: v4traceroute.h:113
void Send()
Send one (ICMP ECHO) to the destination.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:81
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeChecker > MakeIpv4AddressChecker()
Ptr< const AttributeAccessor > MakeIpv4AddressAccessor(T1 a1)
Definition: ipv4-address.h:341
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1427
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1407
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:706
uint8_t data[writeSize]