A Discrete-Event Network Simulator
API
ping6.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007-2009 Strasbourg University
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: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
18 */
19
20#include "ping6.h"
21
22#include "ns3/icmpv6-header.h"
23#include "ns3/inet6-socket-address.h"
24#include "ns3/ipv6-extension-header.h"
25#include "ns3/ipv6-header.h"
26#include "ns3/ipv6-raw-socket-factory.h"
27#include "ns3/log.h"
28#include "ns3/nstime.h"
29#include "ns3/packet.h"
30#include "ns3/simulator.h"
31#include "ns3/socket-factory.h"
32#include "ns3/socket.h"
33#include "ns3/uinteger.h"
34
35namespace ns3
36{
37
38NS_LOG_COMPONENT_DEFINE("Ping6Application");
39
41
42TypeId
44{
45 static TypeId tid = TypeId("ns3::Ping6")
47 .SetGroupName("Internet-Apps")
48 .AddConstructor<Ping6>()
49 .AddAttribute("MaxPackets",
50 "The maximum number of packets the application will send",
51 UintegerValue(100),
53 MakeUintegerChecker<uint32_t>())
54 .AddAttribute("Interval",
55 "The time to wait between packets",
56 TimeValue(Seconds(1.0)),
59 .AddAttribute("RemoteIpv6",
60 "The Ipv6Address of the outbound packets",
62 MakeIpv6AddressAccessor(&Ping6::m_peerAddress),
63 MakeIpv6AddressChecker())
64 .AddAttribute("LocalIpv6",
65 "Local Ipv6Address of the sender",
67 MakeIpv6AddressAccessor(&Ping6::m_localAddress),
68 MakeIpv6AddressChecker())
69 .AddAttribute("PacketSize",
70 "Size of packets generated",
71 UintegerValue(100),
73 MakeUintegerChecker<uint32_t>());
74 return tid;
75}
76
78{
79 NS_LOG_FUNCTION(this);
80 m_sent = 0;
81 m_socket = nullptr;
82 m_seq = 0;
84 m_ifIndex = 0;
86}
87
89{
90 NS_LOG_FUNCTION(this);
91 m_socket = nullptr;
92}
93
94void
96{
97 NS_LOG_FUNCTION(this);
99}
100
101void
103{
104 NS_LOG_FUNCTION(this);
105
106 if (!m_socket)
107 {
108 TypeId tid = TypeId::LookupByName("ns3::Ipv6RawSocketFactory");
110
112
117
118 if (!m_localAddress.IsAny())
119 {
120 m_ipInterfaceIndex = m_ipv6Protocol->GetInterfaceForAddress(m_localAddress);
121 }
122 }
123
125}
126
127void
129{
130 NS_LOG_FUNCTION(this << ipv6);
131 m_localAddress = ipv6;
132}
133
134void
136{
137 NS_LOG_FUNCTION(this << ipv6);
138 m_peerAddress = ipv6;
139}
140
141void
143{
144 NS_LOG_FUNCTION(this);
145
146 if (m_socket)
147 {
149 }
150
152}
153
154void
156{
157 m_ifIndex = ifIndex;
158}
159
160void
162{
163 NS_LOG_FUNCTION(this << dt);
165}
166
167void
168Ping6::SetRouters(std::vector<Ipv6Address> routers)
169{
170 m_routers = routers;
171}
172
173void
175{
176 NS_LOG_FUNCTION(this);
178
179 Ipv6Address src;
180 Ptr<Ipv6> ipv6 = GetNode()->GetObject<Ipv6>();
181
182 if (m_ifIndex > 0)
183 {
184 /* hack to have ifIndex in Ipv6RawSocketImpl
185 * maybe add a SetIfIndex in Ipv6RawSocketImpl directly
186 */
187 for (uint32_t i = 0; i < GetNode()->GetObject<Ipv6>()->GetNAddresses(m_ifIndex); i++)
188 {
190 srcIa = GetNode()->GetObject<Ipv6>()->GetAddress(m_ifIndex, i);
191
192 if (srcIa.IsInSameSubnet(m_peerAddress))
193 {
194 src = srcIa.GetAddress();
195 break;
196 }
197 }
198 }
199 else
200 {
201 src = m_localAddress;
202 }
203
204 uint32_t size = m_size;
205 if (m_size < 4)
206 {
207 NS_LOG_WARN("ICMPv6 echo request payload size must be >= 4");
208 size = 4;
209 }
210
211 uint8_t* data = new uint8_t[size];
212 memset(data, 0, size);
213 data[0] = 0xDE;
214 data[1] = 0xAD;
215 data[2] = 0xBE;
216 data[3] = 0xEF;
217
218 Ptr<Packet> p = Create<Packet>(data, size);
219 Icmpv6Echo req(1);
220
221 req.SetId(0xBEEF);
222 req.SetSeq(m_seq);
223 m_seq++;
224
225 /* we do not calculate pseudo header checksum here, because we are not sure about
226 * source IPv6 address. Checksum is calculated in Ipv6RawSocketImpl.
227 */
228
229 p->AddHeader(req);
231
232 /* use Loose Routing (routing type 0) */
233 if (m_routers.size())
234 {
237 routingHeader.SetTypeRouting(0);
238 routingHeader.SetSegmentsLeft(m_routers.size());
239 routingHeader.SetRoutersAddress(m_routers);
240 p->AddHeader(routingHeader);
242 }
243
245 ++m_sent;
246
247 NS_LOG_INFO("Sent " << p->GetSize() << " bytes to " << m_peerAddress);
248
249 if (m_sent < m_count)
250 {
252 }
253
254 delete[] data;
255}
256
257void
259{
260 NS_LOG_FUNCTION(this << socket);
261
262 Ptr<Packet> packet = nullptr;
263 Address from;
264 while ((packet = socket->RecvFrom(from)))
265 {
267 {
268 Ipv6Header hdr;
269 Icmpv6Echo reply(0);
271 Icmpv6TimeExceeded timeExceeded;
273
274 packet->RemoveHeader(hdr);
275
276 uint8_t type;
277 packet->CopyData(&type, sizeof(type));
278
279 switch (type)
280 {
282 packet->RemoveHeader(reply);
283
284 NS_LOG_INFO("Received Echo Reply size = "
285 << std::dec << packet->GetSize() << " bytes from " << address.GetIpv6()
286 << " id = " << (uint16_t)reply.GetId()
287 << " seq = " << (uint16_t)reply.GetSeq()
288 << " Hop Count = " << (uint16_t)(64 - hdr.GetHopLimit()));
289
290 if (m_ifIndex)
291 {
292 m_ipv6Protocol->ReachabilityHint(m_ifIndex, address.GetIpv6());
293 }
294 else
295 {
296 m_ipv6Protocol->ReachabilityHint(m_ipInterfaceIndex, address.GetIpv6());
297 }
298
299 break;
301 packet->RemoveHeader(destUnreach);
302
303 NS_LOG_INFO("Received Destination Unreachable from " << address.GetIpv6());
304 break;
306 packet->RemoveHeader(timeExceeded);
307
308 NS_LOG_INFO("Received Time Exceeded from " << address.GetIpv6());
309 break;
310 default:
311 break;
312 }
313 }
314 }
315}
316
317} /* namespace ns3 */
a polymophic address class
Definition: address.h:92
The base class for all ns3 applications.
Definition: application.h:61
void DoDispose() override
Destructor implementation.
Definition: application.cc:85
Ptr< Node > GetNode() const
Definition: application.cc:107
Ptr< Node > m_node
The node that this application is installed on.
Definition: application.h:148
An identifier for simulation events.
Definition: event-id.h:55
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
ICMPv6 Error Destination Unreachable header.
ICMPv6 Echo message.
void SetId(uint16_t id)
Set the ID of the packet.
uint16_t GetId() const
Get the ID of the packet.
void SetSeq(uint16_t seq)
Set the sequence number.
uint16_t GetSeq() const
Get the sequence number.
@ ICMPV6_ERROR_DESTINATION_UNREACHABLE
Definition: icmpv6-header.h:45
ICMPv6 Error Time Exceeded header.
An Inet6 address class.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
Describes an IPv6 address.
Definition: ipv6-address.h:50
bool IsAny() const
If the IPv6 address is the "Any" address.
AttributeValue implementation for Ipv6Address.
void SetNextHeader(uint8_t nextHeader)
Set the "Next header" field.
Header of IPv6 Extension Routing : Type 0 (Loose Routing)
void SetRoutersAddress(std::vector< Ipv6Address > routersAddress)
Set the vector of routers' address.
void SetTypeRouting(uint8_t typeRouting)
Set the "Type of Routing" field.
void SetSegmentsLeft(uint8_t segmentsLeft)
Set the "Segments left" field.
Packet header for IPv6.
Definition: ipv6-header.h:36
uint8_t GetHopLimit() const
Get the "Hop limit" field (TTL).
Definition: ipv6-header.cc:100
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
IPv6 address associated with an interface.
Ipv6Address GetAddress() const
Get the IPv6 address.
bool IsInSameSubnet(Ipv6Address b) const
Checks if the address is in the same subnet.
IPv6 layer implementation.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:258
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:400
A ping6 application.
Definition: ping6.h:43
void DoDispose() override
Dispose this object;.
Definition: ping6.cc:95
uint32_t m_ifIndex
Out interface (i.e.
Definition: ping6.h:184
void SetRemote(Ipv6Address ipv6)
Set the remote peer.
Definition: ping6.cc:135
void SetLocal(Ipv6Address ipv6)
Set the local address.
Definition: ping6.cc:128
Ipv6Address m_localAddress
Local address.
Definition: ping6.h:149
EventId m_sendEvent
Event ID.
Definition: ping6.h:179
void HandleRead(Ptr< Socket > socket)
Receive method.
Definition: ping6.cc:258
std::vector< Ipv6Address > m_routers
Routers addresses for routing type 0.
Definition: ping6.h:189
void StopApplication() override
Stop the application.
Definition: ping6.cc:142
uint32_t m_count
Number of "Echo request" packets that will be sent.
Definition: ping6.h:129
void SetIfIndex(uint32_t ifIndex)
Set the out interface index.
Definition: ping6.cc:155
Ptr< Socket > m_socket
Local socket.
Definition: ping6.h:169
void Send()
Send a packet.
Definition: ping6.cc:174
void SetRouters(std::vector< Ipv6Address > routers)
Set routers for routing type 0 (loose routing).
Definition: ping6.cc:168
uint16_t m_seq
Sequence number.
Definition: ping6.h:174
Time m_interval
Interval between packets sent.
Definition: ping6.h:144
uint32_t m_ipInterfaceIndex
IP interface index relative to the local address.
Definition: ping6.h:154
Ptr< Ipv6L3Protocol > m_ipv6Protocol
IP interface index relative to the local address.
Definition: ping6.h:159
uint32_t m_sent
Number of packets sent.
Definition: ping6.h:134
void StartApplication() override
Start the application.
Definition: ping6.cc:102
Ipv6Address m_peerAddress
Peer address.
Definition: ping6.h:164
uint32_t m_size
Size of the packet.
Definition: ping6.h:139
~Ping6() override
Destructor.
Definition: ping6.cc:88
Ping6()
Constructor.
Definition: ping6.cc:77
static TypeId GetTypeId()
Get the type ID.
Definition: ping6.cc:43
void ScheduleTransmit(Time dt)
Schedule sending a packet.
Definition: ping6.cc:161
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:276
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.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
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 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
AttributeValue implementation for Time.
Definition: nstime.h:1425
a unique identifier for an interface.
Definition: type-id.h:60
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:839
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Hold an unsigned integer type.
Definition: uinteger.h:45
#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
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1426
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:734
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
#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:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
address
Definition: first.py:40
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:691
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:535
uint8_t data[writeSize]