A Discrete-Event Network Simulator
API
ping6.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2009 Strasbourg University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 #include "ping6.h"
22 #include "ns3/log.h"
23 #include "ns3/nstime.h"
24 #include "ns3/simulator.h"
25 #include "ns3/socket-factory.h"
26 #include "ns3/packet.h"
27 #include "ns3/socket.h"
28 #include "ns3/uinteger.h"
29 #include "ns3/inet6-socket-address.h"
30 #include "ns3/icmpv6-header.h"
31 #include "ns3/ipv6-raw-socket-factory.h"
32 #include "ns3/ipv6-header.h"
33 #include "ns3/ipv6-extension-header.h"
34 
35 
36 namespace ns3
37 {
38 
39 NS_LOG_COMPONENT_DEFINE ("Ping6Application");
40 
42 
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)),
58  MakeTimeChecker ())
59  .AddAttribute ("RemoteIpv6",
60  "The Ipv6Address of the outbound packets",
64  .AddAttribute ("LocalIpv6",
65  "Local Ipv6Address of the sender",
69  .AddAttribute ("PacketSize",
70  "Size of packets generated",
71  UintegerValue (100),
73  MakeUintegerChecker<uint32_t>())
74  ;
75  return tid;
76 }
77 
79 {
80  NS_LOG_FUNCTION (this);
81  m_sent = 0;
82  m_socket = 0;
83  m_seq = 0;
85  m_ifIndex = 0;
86  m_sendEvent = EventId ();
87 }
88 
90 {
91  NS_LOG_FUNCTION (this);
92  m_socket = 0;
93 }
94 
96 {
97  NS_LOG_FUNCTION (this);
99 }
100 
102 {
103  NS_LOG_FUNCTION (this);
104 
105  if (!m_socket)
106  {
107  TypeId tid = TypeId::LookupByName ("ns3::Ipv6RawSocketFactory");
109 
111 
116 
117  if (!m_localAddress.IsAny ())
118  {
119  m_ipInterfaceIndex = m_ipv6Protocol->GetInterfaceForAddress (m_localAddress);
120  }
121  }
122 
123  ScheduleTransmit (Seconds (0.));
124 }
125 
127 {
128  NS_LOG_FUNCTION (this << ipv6);
129  m_localAddress = ipv6;
130 }
131 
133 {
134  NS_LOG_FUNCTION (this << ipv6);
135  m_peerAddress = ipv6;
136 }
137 
139 {
140  NS_LOG_FUNCTION (this);
141 
142  if (m_socket)
143  {
145  }
146 
148 }
149 
150 void Ping6::SetIfIndex (uint32_t ifIndex)
151 {
152  m_ifIndex = ifIndex;
153 }
154 
156 {
157  NS_LOG_FUNCTION (this << dt);
159 }
160 
161 void Ping6::SetRouters (std::vector<Ipv6Address> routers)
162 {
163  m_routers = routers;
164 }
165 
166 void Ping6::Send ()
167 {
168  NS_LOG_FUNCTION (this);
170 
171  Ipv6Address src;
172  Ptr<Ipv6> ipv6 = GetNode ()->GetObject<Ipv6> ();
173 
174  if (m_ifIndex > 0)
175  {
176  /* hack to have ifIndex in Ipv6RawSocketImpl
177  * maybe add a SetIfIndex in Ipv6RawSocketImpl directly
178  */
179  for (uint32_t i = 0; i < GetNode ()->GetObject<Ipv6> ()->GetNAddresses (m_ifIndex); i++)
180  {
181  Ipv6InterfaceAddress srcIa;
182  srcIa = GetNode ()->GetObject<Ipv6> ()->GetAddress (m_ifIndex, i);
183 
184  if (srcIa.IsInSameSubnet (m_peerAddress))
185  {
186  src = srcIa.GetAddress ();
187  break;
188  }
189  }
190  }
191  else
192  {
193  src = m_localAddress;
194  }
195 
196  uint32_t size = m_size;
197  if (m_size<4)
198  {
199  NS_LOG_WARN ("ICMPv6 echo request payload size must be >= 4");
200  size = 4;
201  }
202 
203  uint8_t* data = new uint8_t[size];
204  memset(data, 0, size);
205  data[0] = 0xDE;
206  data[1] = 0xAD;
207  data[2] = 0xBE;
208  data[3] = 0xEF;
209 
210  Ptr<Packet> p = Create<Packet> (data, size);
211  Icmpv6Echo req (1);
212 
213  req.SetId (0xBEEF);
214  req.SetSeq (m_seq);
215  m_seq++;
216 
217  /* we do not calculate pseudo header checksum here, because we are not sure about
218  * source IPv6 address. Checksum is calculated in Ipv6RawSocketImpl.
219  */
220 
221  p->AddHeader (req);
222  m_socket->Bind (Inet6SocketAddress (src, 0));
223 
224  /* use Loose Routing (routing type 0) */
225  if (m_routers.size ())
226  {
227  Ipv6ExtensionLooseRoutingHeader routingHeader;
228  routingHeader.SetNextHeader (Ipv6Header::IPV6_ICMPV6);
229  routingHeader.SetTypeRouting (0);
230  routingHeader.SetSegmentsLeft (m_routers.size ());
231  routingHeader.SetRoutersAddress (m_routers);
232  p->AddHeader (routingHeader);
234  }
235 
237  ++m_sent;
238 
239  NS_LOG_INFO ("Sent " << p->GetSize () << " bytes to " << m_peerAddress);
240 
241  if (m_sent < m_count)
242  {
244  }
245 
246  delete[] data;
247 }
248 
250 {
251  NS_LOG_FUNCTION (this << socket);
252 
253  Ptr<Packet> packet=0;
254  Address from;
255  while ((packet = socket->RecvFrom (from)))
256  {
258  {
259  Ipv6Header hdr;
260  Icmpv6Echo reply (0);
261  Icmpv6DestinationUnreachable destUnreach;
262  Icmpv6TimeExceeded timeExceeded;
264 
265  packet->RemoveHeader (hdr);
266 
267  uint8_t type;
268  packet->CopyData (&type, sizeof(type));
269 
270  switch (type)
271  {
273  packet->RemoveHeader (reply);
274 
275  NS_LOG_INFO ("Received Echo Reply size = " << std::dec << packet->GetSize () <<
276  " bytes from " << address.GetIpv6 () <<
277  " id = " << (uint16_t)reply.GetId () <<
278  " seq = " << (uint16_t)reply.GetSeq () <<
279  " Hop Count = " << (uint16_t) (64 - hdr.GetHopLimit ()));
280 
281  if (m_ifIndex)
282  {
283  m_ipv6Protocol->ReachabilityHint (m_ifIndex, address.GetIpv6 ());
284  }
285  else
286  {
287  m_ipv6Protocol->ReachabilityHint (m_ipInterfaceIndex, address.GetIpv6 ());
288  }
289 
290  break;
292  packet->RemoveHeader (destUnreach);
293 
294  NS_LOG_INFO ("Received Destination Unreachable from " << address.GetIpv6 ());
295  break;
297  packet->RemoveHeader (timeExceeded);
298 
299  NS_LOG_INFO ("Received Time Exceeded from " << address.GetIpv6 ());
300  break;
301  default:
302  break;
303  }
304  }
305  }
306 }
307 
308 } /* namespace ns3 */
309 
ns3::Ipv6AddressValue
AttributeValue implementation for Ipv6Address.
Definition: ipv6-address.h:615
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::Application::m_node
Ptr< Node > m_node
The node that this application is installed on.
Definition: application.h:148
ns3::Inet6SocketAddress::IsMatchingType
static bool IsMatchingType(const Address &addr)
If the address match.
Definition: inet6-socket-address.cc:89
ns3::Ipv6InterfaceAddress
IPv6 address associated with an interface.
Definition: ipv6-interface-address.h:38
ns3::Ping6::HandleRead
void HandleRead(Ptr< Socket > socket)
Receive method.
Definition: ping6.cc:249
ns3::Ping6::SetRouters
void SetRouters(std::vector< Ipv6Address > routers)
Set routers for routing type 0 (loose routing).
Definition: ping6.cc:161
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
ns3::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
ns3::Ipv6Header::IPV6_ICMPV6
@ IPV6_ICMPV6
Definition: ipv6-header.h:94
ns3::Socket::Bind
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
ns3::Ping6::m_ifIndex
uint32_t m_ifIndex
Out interface (i.e.
Definition: ping6.h:185
ns3::EventId
An identifier for simulation events.
Definition: event-id.h:54
ns3::Packet::GetSize
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
ns3::Icmpv6TimeExceeded
ICMPv6 Error Time Exceeded header.
Definition: icmpv6-header.h:1193
ns3::Packet::AddHeader
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
ns3::Ping6::Ping6
Ping6()
Constructor.
Definition: ping6.cc:78
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv6InterfaceAddress::GetAddress
Ipv6Address GetAddress() const
Get the IPv6 address.
Definition: ipv6-interface-address.cc:89
ns3::Ipv6ExtensionLooseRoutingHeader::SetRoutersAddress
void SetRoutersAddress(std::vector< Ipv6Address > routersAddress)
Set the vector of routers' address.
Definition: ipv6-extension-header.cc:526
ns3::Ping6::m_sent
uint32_t m_sent
Number of packets sent.
Definition: ping6.h:135
ns3::Ping6::~Ping6
virtual ~Ping6()
Destructor.
Definition: ping6.cc:89
ns3::Icmpv6Echo::GetId
uint16_t GetId() const
Get the ID of the packet.
Definition: icmpv6-header.cc:966
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
ns3::Ipv6L3Protocol
IPv6 layer implementation.
Definition: ipv6-l3-protocol.h:63
NS_LOG_WARN
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
ns3::Packet::CopyData
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
ns3::Ping6::m_peerAddress
Ipv6Address m_peerAddress
Peer address.
Definition: ping6.h:165
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
ns3::Ping6::SetIfIndex
void SetIfIndex(uint32_t ifIndex)
Set the out interface index.
Definition: ping6.cc:150
ns3::Ping6::m_seq
uint16_t m_seq
Sequence number.
Definition: ping6.h:175
ns3::ObjectBase::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
ns3::Application::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::MakeIpv6AddressAccessor
Ptr< const AttributeAccessor > MakeIpv6AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: ipv6-address.h:615
ns3::Socket::SendTo
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
ns3::Icmpv6Echo::SetSeq
void SetSeq(uint16_t seq)
Set the sequence number.
Definition: icmpv6-header.cc:984
ns3::Ping6::m_ipv6Protocol
Ptr< Ipv6L3Protocol > m_ipv6Protocol
IP interface index relative to the local address.
Definition: ping6.h:160
ns3::Ipv6Header
Packet header for IPv6.
Definition: ipv6-header.h:36
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::MakeNullCallback
Callback< R, Ts... > MakeNullCallback(void)
Definition: callback.h:1682
ns3::Ping6::m_interval
Time m_interval
Interval between packets sent.
Definition: ping6.h:145
ns3::Inet6SocketAddress::ConvertFrom
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
Definition: inet6-socket-address.cc:110
ns3::MakeIpv6AddressChecker
Ptr< const AttributeChecker > MakeIpv6AddressChecker(void)
Definition: ipv6-address.cc:1172
ns3::Ping6::m_sendEvent
EventId m_sendEvent
Event ID.
Definition: ping6.h:180
ns3::Ptr< Socket >
ns3::Socket::SetRecvCallback
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
ns3::Ping6::SetRemote
void SetRemote(Ipv6Address ipv6)
Set the remote peer.
Definition: ping6.cc:132
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::Ping6::m_socket
Ptr< Socket > m_socket
Local socket.
Definition: ping6.h:170
data
uint8_t data[writeSize]
Definition: socket-bound-tcp-static-routing.cc:53
ns3::Simulator::Cancel
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:268
ns3::Ipv6ExtensionLooseRoutingHeader
Header of IPv6 Extension Routing : Type 0 (Loose Routing)
Definition: ipv6-extension-header.h:529
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::Socket::RecvFrom
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.
ns3::Ping6::ScheduleTransmit
void ScheduleTransmit(Time dt)
Schedule sending a packet.
Definition: ping6.cc:155
ns3::Packet::RemoveHeader
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
first.address
address
Definition: first.py:44
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::Ping6
A ping6 application.
Definition: ping6.h:44
ns3::Ipv6ExtensionRoutingHeader::SetSegmentsLeft
void SetSegmentsLeft(uint8_t segmentsLeft)
Set the "Segments left" field.
Definition: ipv6-extension-header.cc:451
ns3::Ping6::m_size
uint32_t m_size
Size of the packet.
Definition: ping6.h:140
ns3::Ping6::SetLocal
void SetLocal(Ipv6Address ipv6)
Set the local address.
Definition: ping6.cc:126
ns3::Ping6::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition: ping6.cc:43
ns3::Icmpv6Header::ICMPV6_ECHO_REPLY
@ ICMPV6_ECHO_REPLY
Definition: icmpv6-header.h:51
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
ping6.h
ns3::Icmpv6Header::ICMPV6_ERROR_DESTINATION_UNREACHABLE
@ ICMPV6_ERROR_DESTINATION_UNREACHABLE
Definition: icmpv6-header.h:46
ns3::Ipv6Header::GetHopLimit
uint8_t GetHopLimit(void) const
Get the "Hop limit" field (TTL).
Definition: ipv6-header.cc:90
ns3::Ipv6InterfaceAddress::IsInSameSubnet
bool IsInSameSubnet(Ipv6Address b) const
Checks if the address is in the same subnet.
Definition: ipv6-interface-address.cc:154
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Inet6SocketAddress
An Inet6 address class.
Definition: inet6-socket-address.h:37
ns3::Ping6::m_ipInterfaceIndex
uint32_t m_ipInterfaceIndex
IP interface index relative to the local address.
Definition: ping6.h:155
ns3::Ping6::Send
void Send()
Send a packet.
Definition: ping6.cc:166
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::Ipv6ExtensionRoutingHeader::SetTypeRouting
void SetTypeRouting(uint8_t typeRouting)
Set the "Type of Routing" field.
Definition: ipv6-extension-header.cc:441
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::Ping6::m_routers
std::vector< Ipv6Address > m_routers
Routers addresses for routing type 0.
Definition: ping6.h:190
ns3::Ipv6
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
ns3::Icmpv6Echo::GetSeq
uint16_t GetSeq() const
Get the sequence number.
Definition: icmpv6-header.cc:978
ns3::Icmpv6Echo::SetId
void SetId(uint16_t id)
Set the ID of the packet.
Definition: icmpv6-header.cc:972
ns3::Icmpv6Echo
ICMPv6 Echo message.
Definition: icmpv6-header.h:942
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::Application::GetNode
Ptr< Node > GetNode() const
Definition: application.cc:104
ns3::Socket::CreateSocket
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:71
ns3::Ipv6Address::IsAny
bool IsAny() const
If the IPv6 address is the "Any" address.
Definition: ipv6-address.cc:797
ns3::MakeUintegerAccessor
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: uinteger.h:45
ns3::Application
The base class for all ns3 applications.
Definition: application.h:61
ns3::Ping6::m_localAddress
Ipv6Address m_localAddress
Local address.
Definition: ping6.h:150
ns3::TypeId::LookupByName
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830
ns3::Ping6::m_count
uint32_t m_count
Number of "Echo request" packets that will be sent.
Definition: ping6.h:130
ns3::Ipv6Header::IPV6_EXT_ROUTING
@ IPV6_EXT_ROUTING
Definition: ipv6-header.h:90
ns3::Ipv6ExtensionHeader::SetNextHeader
void SetNextHeader(uint8_t nextHeader)
Set the "Next header" field.
Definition: ipv6-extension-header.cc:59
ns3::MakeTimeAccessor
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1354
ns3::EventId::IsExpired
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:65
ns3::Ping6::DoDispose
virtual void DoDispose()
Dispose this object;.
Definition: ping6.cc:95
ns3::Icmpv6Header::ICMPV6_ERROR_TIME_EXCEEDED
@ ICMPV6_ERROR_TIME_EXCEEDED
Definition: icmpv6-header.h:48
ns3::Ping6::StartApplication
virtual void StartApplication()
Start the application.
Definition: ping6.cc:101
ns3::Ping6::StopApplication
virtual void StopApplication()
Stop the application.
Definition: ping6.cc:138
ns3::Icmpv6DestinationUnreachable
ICMPv6 Error Destination Unreachable header.
Definition: icmpv6-header.h:1039