A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "ns3/log.h"
22 #include "ns3/nstime.h"
23 #include "ns3/simulator.h"
24 #include "ns3/socket-factory.h"
25 #include "ns3/packet.h"
26 #include "ns3/socket.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/ipv6.h"
29 #include "ns3/ipv6-address.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 #include "ping6.h"
37 
38 namespace ns3
39 {
40 
41 NS_LOG_COMPONENT_DEFINE ("Ping6Application")
42  ;
43 
45  ;
46 
48 {
49  static TypeId tid = TypeId ("ns3::Ping6")
51  .AddConstructor<Ping6>()
52  .AddAttribute ("MaxPackets",
53  "The maximum number of packets the application will send",
54  UintegerValue (100),
55  MakeUintegerAccessor (&Ping6::m_count),
56  MakeUintegerChecker<uint32_t>())
57  .AddAttribute ("Interval",
58  "The time to wait between packets",
59  TimeValue (Seconds (1.0)),
60  MakeTimeAccessor (&Ping6::m_interval),
61  MakeTimeChecker ())
62  .AddAttribute ("RemoteIpv6",
63  "The Ipv6Address of the outbound packets",
65  MakeIpv6AddressAccessor (&Ping6::m_peerAddress),
66  MakeIpv6AddressChecker ())
67  .AddAttribute ("LocalIpv6",
68  "Local Ipv6Address of the sender",
70  MakeIpv6AddressAccessor (&Ping6::m_localAddress),
71  MakeIpv6AddressChecker ())
72  .AddAttribute ("PacketSize",
73  "Size of packets generated",
74  UintegerValue (100),
75  MakeUintegerAccessor (&Ping6::m_size),
76  MakeUintegerChecker<uint32_t>())
77  ;
78  return tid;
79 }
80 
82 {
84  m_sent = 0;
85  m_socket = 0;
86  m_seq = 0;
87  m_sendEvent = EventId ();
88 }
89 
91 {
93  m_socket = 0;
94 }
95 
97 {
100 }
101 
103 {
105 
106  if (!m_socket)
107  {
108  TypeId tid = TypeId::LookupByName ("ns3::Ipv6RawSocketFactory");
110 
112 
117  }
118 
119  ScheduleTransmit (Seconds (0.));
120 }
121 
123 {
124  NS_LOG_FUNCTION (this << ipv6);
125  m_localAddress = ipv6;
126 }
127 
129 {
130  NS_LOG_FUNCTION (this << ipv6);
131  m_peerAddress = ipv6;
132 }
133 
135 {
137 
138  if (m_socket)
139  {
141  }
142 
144 }
145 
146 void Ping6::SetIfIndex (uint32_t ifIndex)
147 {
148  m_ifIndex = ifIndex;
149 }
150 
152 {
153  NS_LOG_FUNCTION (this << dt);
155 }
156 
157 void Ping6::SetRouters (std::vector<Ipv6Address> routers)
158 {
159  m_routers = routers;
160 }
161 
162 void Ping6::Send ()
163 {
166  Ptr<Packet> p = 0;
167  uint8_t* data = new uint8_t[m_size];
168  Ipv6Address src;
169  Ptr<Ipv6> ipv6 = GetNode ()->GetObject<Ipv6> ();
170 
171  if (m_ifIndex > 0)
172  {
173  /* hack to have ifIndex in Ipv6RawSocketImpl
174  * maybe add a SetIfIndex in Ipv6RawSocketImpl directly
175  */
177  for (uint32_t i = 0; i < GetNode ()->GetObject<Ipv6> ()->GetNAddresses (m_ifIndex); i++)
178  {
179  src = GetNode ()->GetObject<Ipv6> ()->GetAddress (m_ifIndex, i).GetAddress ();
180  Ipv6InterfaceAddress srcIa (src);
181  if ( srcIa.GetScope() == dstIa.GetScope() )
182  {
183  break;
184  }
185  }
186  }
187  else
188  {
189  src = m_localAddress;
190  }
191 
192  NS_ASSERT_MSG (m_size >= 4, "ICMPv6 echo request payload size must be >= 4");
193  data[0] = 0xDE;
194  data[1] = 0xAD;
195  data[2] = 0xBE;
196  data[3] = 0xEF;
197 
198  p = Create<Packet> (data, 4);
199  p->AddAtEnd (Create<Packet> (m_size - 4));
200  Icmpv6Echo req (1);
201 
202  req.SetId (0xBEEF);
203  req.SetSeq (m_seq);
204  m_seq++;
205 
206  /* we do not calculate pseudo header checksum here, because we are not sure about
207  * source IPv6 address. Checksum is calculated in Ipv6RawSocketImpl.
208  */
209 
210  p->AddHeader (req);
211  m_socket->Bind (Inet6SocketAddress (src, 0));
212 
213  /* use Loose Routing (routing type 0) */
214  if (m_routers.size ())
215  {
216  Ipv6ExtensionLooseRoutingHeader routingHeader;
217  routingHeader.SetNextHeader (Ipv6Header::IPV6_ICMPV6);
218  routingHeader.SetLength (m_routers.size () * 16 + 8);
219  routingHeader.SetTypeRouting (0);
220  routingHeader.SetSegmentsLeft (m_routers.size ());
221  routingHeader.SetRoutersAddress (m_routers);
222  p->AddHeader (routingHeader);
224  }
225 
226  m_socket->Send (p, 0);
227  ++m_sent;
228 
229  NS_LOG_INFO ("Sent " << p->GetSize () << " bytes to " << m_peerAddress);
230 
231  if (m_sent < m_count)
232  {
234  }
235 
236  delete[] data;
237 }
238 
240 {
241  NS_LOG_FUNCTION (this << socket);
242 
243  Ptr<Packet> packet=0;
244  Address from;
245  while ((packet = socket->RecvFrom (from)))
246  {
248  {
249  Ipv6Header hdr;
250  Icmpv6Echo reply (0);
252 
253  packet->RemoveHeader (hdr);
254 
255  uint8_t type;
256  packet->CopyData (&type, sizeof(type));
257 
258  switch (type)
259  {
261  packet->RemoveHeader (reply);
262 
263  NS_LOG_INFO ("Received Echo Reply size = " << std::dec << packet->GetSize () << " bytes from " << address.GetIpv6 () << " id = " << (uint16_t)reply.GetId () << " seq = " << (uint16_t)reply.GetSeq ());
264  break;
265  default:
266  /* other type, discard */
267  break;
268  }
269  }
270  }
271 }
272 
273 } /* namespace ns3 */
274 
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
virtual ~Ping6()
Destructor.
Definition: ping6.cc:90
uint16_t GetId() const
Get the ID of the packet.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
Doxygen introspection did not find any typical Config paths.
Definition: ipv6-header.h:33
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
virtual void StopApplication()
Stop the application.
Definition: ping6.cc:134
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:79
void SetTypeRouting(uint8_t typeRouting)
Set the "Type of Routing" field.
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
IPv6 address associated with an interface.
void SetRouters(std::vector< Ipv6Address > routers)
Set routers for routing type 0 (loose routing).
Definition: ping6.cc:157
uint32_t GetSize(void) const
Definition: packet.h:650
#define NS_LOG_INFO(msg)
Definition: log.h:298
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
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
Callback< R > MakeNullCallback(void)
Definition: callback.h:1395
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
Time m_interval
Intervall between packets sent.
Definition: ping6.h:147
void Send()
Send a packet.
Definition: ping6.cc:162
a polymophic address class
Definition: address.h:86
void SetIfIndex(uint32_t ifIndex)
Set the out interface index.
Definition: ping6.cc:146
Doxygen introspection did not find any typical Config paths.
The base class for all ns3 applications.
Definition: application.h:61
hold objects of type ns3::Time
Definition: nstime.h:961
virtual Ipv6InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const =0
Get IPv6 address on specified IPv6 interface.
Hold an unsigned integer type.
Definition: uinteger.h:46
uint8_t data[writeSize]
Ptr< Node > GetNode() const
Definition: application.cc:104
uint32_t m_sent
Number of packets sent.
Definition: ping6.h:137
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
virtual void StartApplication()
Start the application.
Definition: ping6.cc:102
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:182
void SetLocal(Ipv6Address ipv6)
Set the local address.
Definition: ping6.cc:122
uint16_t GetSeq() const
Get the sequence number.
uint32_t m_size
Size of the packet.
Definition: ping6.h:142
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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.
Ptr< Socket > m_socket
Local socket.
Definition: ping6.h:162
Ping6()
Constructor.
Definition: ping6.cc:81
Hold objects of type ns3::Ipv6Address.
Doxygen introspection did not find any typical Config paths.
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Describes an IPv6 address.
Definition: ipv6-address.h:46
void SetRoutersAddress(std::vector< Ipv6Address > routersAddress)
Set the vector of routers' address.
Ipv6InterfaceAddress::Scope_e GetScope() const
Get address scope.
void SetRemote(Ipv6Address ipv6)
Set the remote peer.
Definition: ping6.cc:128
an identifier for simulation events.
Definition: event-id.h:46
uint16_t m_seq
Sequence number.
Definition: ping6.h:167
uint32_t m_ifIndex
Out interface (i.e.
Definition: ping6.h:177
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
Ipv6Address m_localAddress
Local address.
Definition: ping6.h:152
static bool IsMatchingType(const Address &addr)
If the address match.
void ScheduleTransmit(Time dt)
Schedule sending a packet.
Definition: ping6.cc:151
void SetLength(uint16_t length)
brief Set the length of the extension.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:381
virtual void DoDispose()
Dispose this object;.
Definition: ping6.cc:96
void SetSeq(uint16_t seq)
Set the sequence number.
tuple address
Definition: first.py:37
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:172
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
static TypeId GetTypeId()
Get the type ID.
Definition: ping6.cc:47
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::isExpired method.
Definition: event-id.cc:53
Ptr< T > GetObject(void) const
Definition: object.h:361
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
Ipv6Address m_peerAddress
Peer address.
Definition: ping6.h:157
void SetId(uint16_t id)
Set the ID of the packet.
void HandleRead(Ptr< Socket > socket)
Receive method.
Definition: ping6.cc:239
static TypeId LookupByName(std::string name)
Definition: type-id.cc:536
uint32_t m_count
Number of "Echo request" packets that will be sent.
Definition: ping6.h:132
void SetSegmentsLeft(uint8_t segmentsLeft)
Set the "Segments left" field.