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/ipv6.h"
30 #include "ns3/inet6-socket-address.h"
31 #include "ns3/icmpv6-header.h"
32 #include "ns3/ipv6-raw-socket-factory.h"
33 #include "ns3/ipv6-header.h"
34 #include "ns3/ipv6-extension-header.h"
35 
36 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE ("Ping6Application");
41 
43 
45 {
46  static TypeId tid = TypeId ("ns3::Ping6")
48  .SetGroupName("Internet-Apps")
49  .AddConstructor<Ping6>()
50  .AddAttribute ("MaxPackets",
51  "The maximum number of packets the application will send",
52  UintegerValue (100),
54  MakeUintegerChecker<uint32_t>())
55  .AddAttribute ("Interval",
56  "The time to wait between packets",
57  TimeValue (Seconds (1.0)),
59  MakeTimeChecker ())
60  .AddAttribute ("RemoteIpv6",
61  "The Ipv6Address of the outbound packets",
65  .AddAttribute ("LocalIpv6",
66  "Local Ipv6Address of the sender",
70  .AddAttribute ("PacketSize",
71  "Size of packets generated",
72  UintegerValue (100),
74  MakeUintegerChecker<uint32_t>())
75  ;
76  return tid;
77 }
78 
80 {
81  NS_LOG_FUNCTION (this);
82  m_sent = 0;
83  m_socket = 0;
84  m_seq = 0;
85  m_sendEvent = EventId ();
86 }
87 
89 {
90  NS_LOG_FUNCTION (this);
91  m_socket = 0;
92 }
93 
95 {
96  NS_LOG_FUNCTION (this);
98 }
99 
101 {
102  NS_LOG_FUNCTION (this);
103 
104  if (!m_socket)
105  {
106  TypeId tid = TypeId::LookupByName ("ns3::Ipv6RawSocketFactory");
108 
110 
114  }
115 
116  ScheduleTransmit (Seconds (0.));
117 }
118 
120 {
121  NS_LOG_FUNCTION (this << ipv6);
122  m_localAddress = ipv6;
123 }
124 
126 {
127  NS_LOG_FUNCTION (this << ipv6);
128  m_peerAddress = ipv6;
129 }
130 
132 {
133  NS_LOG_FUNCTION (this);
134 
135  if (m_socket)
136  {
138  }
139 
141 }
142 
143 void Ping6::SetIfIndex (uint32_t ifIndex)
144 {
145  m_ifIndex = ifIndex;
146 }
147 
149 {
150  NS_LOG_FUNCTION (this << dt);
152 }
153 
154 void Ping6::SetRouters (std::vector<Ipv6Address> routers)
155 {
156  m_routers = routers;
157 }
158 
159 void Ping6::Send ()
160 {
161  NS_LOG_FUNCTION (this);
163 
164  Ipv6Address src;
165  Ptr<Ipv6> ipv6 = GetNode ()->GetObject<Ipv6> ();
166 
167  if (m_ifIndex > 0)
168  {
169  /* hack to have ifIndex in Ipv6RawSocketImpl
170  * maybe add a SetIfIndex in Ipv6RawSocketImpl directly
171  */
172  for (uint32_t i = 0; i < GetNode ()->GetObject<Ipv6> ()->GetNAddresses (m_ifIndex); i++)
173  {
174  Ipv6InterfaceAddress srcIa;
175  srcIa = GetNode ()->GetObject<Ipv6> ()->GetAddress (m_ifIndex, i);
176 
177  if (srcIa.IsInSameSubnet (m_peerAddress))
178  {
179  src = srcIa.GetAddress ();
180  break;
181  }
182  }
183  }
184  else
185  {
186  src = m_localAddress;
187  }
188 
189  uint32_t size = m_size;
190  if (m_size<4)
191  {
192  NS_LOG_WARN ("ICMPv6 echo request payload size must be >= 4");
193  size = 4;
194  }
195 
196  uint8_t* data = new uint8_t[size];
197  memset(data, 0, size);
198  data[0] = 0xDE;
199  data[1] = 0xAD;
200  data[2] = 0xBE;
201  data[3] = 0xEF;
202 
203  Ptr<Packet> p = Create<Packet> (data, size);
204  Icmpv6Echo req (1);
205 
206  req.SetId (0xBEEF);
207  req.SetSeq (m_seq);
208  m_seq++;
209 
210  /* we do not calculate pseudo header checksum here, because we are not sure about
211  * source IPv6 address. Checksum is calculated in Ipv6RawSocketImpl.
212  */
213 
214  p->AddHeader (req);
215  m_socket->Bind (Inet6SocketAddress (src, 0));
216 
217  /* use Loose Routing (routing type 0) */
218  if (m_routers.size ())
219  {
220  Ipv6ExtensionLooseRoutingHeader routingHeader;
221  routingHeader.SetNextHeader (Ipv6Header::IPV6_ICMPV6);
222  routingHeader.SetTypeRouting (0);
223  routingHeader.SetSegmentsLeft (m_routers.size ());
224  routingHeader.SetRoutersAddress (m_routers);
225  p->AddHeader (routingHeader);
227  }
228 
230  ++m_sent;
231 
232  NS_LOG_INFO ("Sent " << p->GetSize () << " bytes to " << m_peerAddress);
233 
234  if (m_sent < m_count)
235  {
237  }
238 
239  delete[] data;
240 }
241 
243 {
244  NS_LOG_FUNCTION (this << socket);
245 
246  Ptr<Packet> packet=0;
247  Address from;
248  while ((packet = socket->RecvFrom (from)))
249  {
251  {
252  Ipv6Header hdr;
253  Icmpv6Echo reply (0);
254  Icmpv6DestinationUnreachable destUnreach;
255  Icmpv6TimeExceeded timeExceeded;
257 
258  packet->RemoveHeader (hdr);
259 
260  uint8_t type;
261  packet->CopyData (&type, sizeof(type));
262 
263  switch (type)
264  {
266  packet->RemoveHeader (reply);
267 
268  NS_LOG_INFO ("Received Echo Reply size = " << std::dec << packet->GetSize () <<
269  " bytes from " << address.GetIpv6 () <<
270  " id = " << (uint16_t)reply.GetId () <<
271  " seq = " << (uint16_t)reply.GetSeq () <<
272  " Hop Count = " << (uint16_t) (64 - hdr.GetHopLimit ()));
273  break;
275  packet->RemoveHeader (destUnreach);
276 
277  NS_LOG_INFO ("Received Destination Unreachable from " << address.GetIpv6 ());
278  break;
280  packet->RemoveHeader (timeExceeded);
281 
282  NS_LOG_INFO ("Received Time Exceeded from " << address.GetIpv6 ());
283  break;
284  default:
285  break;
286  }
287  }
288  }
289 }
290 
291 } /* namespace ns3 */
292 
uint16_t GetSeq() const
Get the sequence number.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
virtual ~Ping6()
Destructor.
Definition: ping6.cc:88
Ipv6Address GetAddress() const
Get the IPv6 address.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Packet header for IPv6.
Definition: ipv6-header.h:34
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual void StopApplication()
Stop the application.
Definition: ping6.cc:131
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
void SetTypeRouting(uint8_t typeRouting)
Set the "Type of Routing" field.
bool IsInSameSubnet(Ipv6Address b) const
Checks if the address is in the same subnet.
ICMPv6 Error Time Exceeded header.
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
IPv6 address associated with an interface.
void SetRouters(std::vector< Ipv6Address > routers)
Set routers for routing type 0 (loose routing).
Definition: ping6.cc:154
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:268
Time m_interval
Intervall between packets sent.
Definition: ping6.h:144
void Send()
Send a packet.
Definition: ping6.cc:159
Callback< R, Ts... > MakeNullCallback(void)
Definition: callback.h:1682
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:65
a polymophic address class
Definition: address.h:90
void SetIfIndex(uint32_t ifIndex)
Set the out interface index.
Definition: ping6.cc:143
ICMPv6 Error Destination Unreachable header.
Header of IPv6 Extension Routing : Type 0 (Loose Routing)
The base class for all ns3 applications.
Definition: application.h:60
AttributeValue implementation for Time.
Definition: nstime.h:1342
Ptr< const AttributeChecker > MakeIpv6AddressChecker(void)
Hold an unsigned integer type.
Definition: uinteger.h:44
uint8_t data[writeSize]
uint32_t m_sent
Number of packets sent.
Definition: ping6.h:134
An Inet6 address class.
virtual void StartApplication()
Start the application.
Definition: ping6.cc:100
void SetRecvCallback(Callback< void, Ptr< Socket > >)
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:71
std::vector< Ipv6Address > m_routers
Routers addresses for routing type 0.
Definition: ping6.h:179
A ping6 application.
Definition: ping6.h:42
void SetLocal(Ipv6Address ipv6)
Set the local address.
Definition: ping6.cc:119
Ptr< Node > GetNode() const
Definition: application.cc:104
uint32_t m_size
Size of the packet.
Definition: ping6.h:139
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
void SetNextHeader(uint8_t nextHeader)
Set the "Next header" field.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< Socket > m_socket
Local socket.
Definition: ping6.h:159
address
Definition: first.py:44
Ping6()
Constructor.
Definition: ping6.cc:79
AttributeValue implementation for Ipv6Address.
Definition: ipv6-address.h:620
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:1343
ICMPv6 Echo message.
Describes an IPv6 address.
Definition: ipv6-address.h:49
void SetRoutersAddress(std::vector< Ipv6Address > routersAddress)
Set the vector of routers&#39; address.
void SetRemote(Ipv6Address ipv6)
Set the remote peer.
Definition: ping6.cc:125
An identifier for simulation events.
Definition: event-id.h:53
uint16_t m_seq
Sequence number.
Definition: ping6.h:164
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:620
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
uint32_t m_ifIndex
Out interface (i.e.
Definition: ping6.h:174
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
Ipv6Address m_localAddress
Local address.
Definition: ping6.h:149
static bool IsMatchingType(const Address &addr)
If the address match.
void ScheduleTransmit(Time dt)
Schedule sending a packet.
Definition: ping6.cc:148
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:449
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
virtual void DoDispose()
Dispose this object;.
Definition: ping6.cc:94
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.
EventId m_sendEvent
Event ID.
Definition: ping6.h:169
uint8_t GetHopLimit(void) const
Get the "Hop limit" field (TTL).
Definition: ipv6-header.cc:90
static TypeId GetTypeId()
Get the type ID.
Definition: ping6.cc:44
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
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
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
Ipv6Address m_peerAddress
Peer address.
Definition: ping6.h:154
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
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
void HandleRead(Ptr< Socket > socket)
Receive method.
Definition: ping6.cc:242
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830
uint32_t m_count
Number of "Echo request" packets that will be sent.
Definition: ping6.h:129
uint16_t GetId() const
Get the ID of the packet.
void SetSegmentsLeft(uint8_t segmentsLeft)
Set the "Segments left" field.