17 #include "ns3/icmpv4.h" 
   18 #include "ns3/assert.h" 
   20 #include "ns3/ipv4-address.h" 
   21 #include "ns3/socket.h" 
   22 #include "ns3/uinteger.h" 
   23 #include "ns3/boolean.h" 
   24 #include "ns3/inet-socket-address.h" 
   25 #include "ns3/packet.h" 
   26 #include "ns3/trace-source-accessor.h" 
   31 NS_OBJECT_ENSURE_REGISTERED (V4Ping);
 
   34 V4Ping::GetTypeId (
void)
 
   36   static TypeId tid = TypeId (
"ns3::V4Ping")
 
   37     .SetParent<Application> ()
 
   38     .AddConstructor<V4Ping> ()
 
   39     .AddAttribute (
"Remote", 
 
   40                    "The address of the machine we want to ping.",
 
   43                    MakeIpv4AddressChecker ())
 
   44     .AddAttribute (
"Verbose",
 
   45                    "Produce usual output.",
 
   48                    MakeBooleanChecker ())
 
   49     .AddAttribute (
"Interval", 
"Wait  interval  seconds between sending each packet.",
 
   53     .AddAttribute (
"Size", 
"The number of data bytes to be sent, real packet will be 8 (ICMP) + 20 (IP) bytes longer.",
 
   56                    MakeUintegerChecker<uint32_t> (16))
 
   57     .AddTraceSource (
"Rtt",
 
   58                      "The rtt calculated by the ping.",
 
   88 V4Ping::GetApplicationId (
void)
 const 
  104 V4Ping::Receive (Ptr<Socket> socket)
 
  110       Ptr<Packet> p = m_socket->
RecvFrom (0xffffffff, 0, from);
 
  116       p->RemoveHeader (ipv4);
 
  117       uint32_t recvSize = p->GetSize ();
 
  120       p->RemoveHeader (icmp);
 
  121       if (icmp.GetType () == Icmpv4Header::ECHO_REPLY)
 
  124           p->RemoveHeader (echo);
 
  125           std::map<uint16_t, Time>::iterator i = 
m_sent.find (echo.GetSequenceNumber ());
 
  127           if (i != 
m_sent.end () && echo.GetIdentifier () == 0)
 
  129               uint32_t * buf = 
new uint32_t [
m_size];
 
  130               uint32_t dataSize = echo.GetDataSize ();
 
  135                   echo.GetData ((uint8_t *)buf);
 
  136                   Read32 ((
const uint8_t *) &buf[0], nodeId);
 
  137                   Read32 ((
const uint8_t *) &buf[1], appId);
 
  139                   if (nodeId == 
GetNode ()->GetId () &&
 
  140                       appId == GetApplicationId ())
 
  142                       Time sendTime = i->second;
 
  153                           std::cout << recvSize << 
" bytes from " << realFrom.GetIpv4 () << 
":" 
  154                                     << 
" icmp_seq=" << echo.GetSequenceNumber ()
 
  155                                     << 
" ttl=" << (unsigned)ipv4.GetTtl ()
 
  156                                     << 
" time=" << delta.GetMilliSeconds () << 
" ms\n";
 
  169 V4Ping::Write32 (uint8_t *buffer, 
const uint32_t data)
 
  172   buffer[0] = (data >> 0) & 0xff;
 
  173   buffer[1] = (data >> 8) & 0xff;
 
  174   buffer[2] = (data >> 16) & 0xff;
 
  175   buffer[3] = (data >> 24) & 0xff;
 
  180 V4Ping::Read32 (
const uint8_t *buffer, uint32_t &data)
 
  183   data = (buffer[3] << 24) + (buffer[2] << 16) + (buffer[1] << 8) + buffer[0];
 
  192   Ptr<Packet> p = Create<Packet> ();
 
  194   echo.SetSequenceNumber (m_seq);
 
  196   echo.SetIdentifier (0);
 
  204   uint8_t* data = 
new uint8_t[
m_size];
 
  205   for (uint32_t i = 0; i < 
m_size; ++i) data[i] = 0;
 
  209   Write32 (&data[0 * 
sizeof(uint32_t)], tmp);
 
  211   tmp = GetApplicationId ();
 
  212   Write32 (&data[1 * 
sizeof(uint32_t)], tmp);
 
  214   Ptr<Packet> dataPacket = Create<Packet> ((uint8_t *) data, m_size);
 
  215   echo.SetData (dataPacket);
 
  218   header.SetType (Icmpv4Header::ECHO);
 
  222       header.EnableChecksum ();
 
  224   p->AddHeader (header);
 
  226   m_socket->
Send (p, 0);
 
  239       std::cout << 
"PING  " << 
m_remote << 
" 56(84) bytes of data.\n";
 
  248   status = m_socket->
Bind (src);
 
  251   status = m_socket->
Connect (dst);
 
  265       std::ostringstream os;
 
  267       os << 
"--- " << 
m_remote << 
" ping statistics ---\n"  
  268          << m_seq << 
" packets transmitted, " << 
m_recv << 
" received, " 
  269          << ((m_seq - 
m_recv) * 100 / m_seq) << 
"% packet loss, " 
  276       std::cout << os.str ();
 
virtual void StartApplication(void)
Application specific startup code. 
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
uint32_t GetNApplications(void) const 
double Stddev() const 
Standard deviation. 
static bool ChecksumEnabled(void)
#define NS_ASSERT(condition)
#define NS_LOG_COMPONENT_DEFINE(name)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
double Avg() const 
Sample average. 
virtual void DoDispose(void)
Ptr< Application > GetApplication(uint32_t index) const 
Time m_started
Start time to report total ping time. 
Hold an unsigned integer type. 
Ptr< Node > GetNode() const 
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read. 
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host. 
virtual void DoDispose(void)
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket. 
std::map< uint16_t, Time > m_sent
All sent but not answered packets. Map icmp seqno -> when sent. 
static InetSocketAddress ConvertFrom(const Address &address)
bool m_verbose
produce ping-style output if true 
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
EventId m_next
Next packet will be sent. 
#define NS_ASSERT_MSG(condition, message)
Time Seconds(double seconds)
create ns3::Time instances in units of seconds. 
virtual void StopApplication(void)
Application specific shutdown code. 
uint32_t GetId(void) const 
#define NS_LOG_DEBUG(msg)
Time m_interval
Wait interval seconds between sending each packet. 
uint32_t m_recv
received packets counter 
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 int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host. 
Ipv4Address m_remote
Remote address. 
virtual int Close(void)=0
Close a socket. 
uint32_t Count() const 
Sample size. 
void SetAttribute(std::string name, const AttributeValue &value)
static bool IsMatchingType(const Address &address)
Average< double > m_avgRtt
Average rtt is ms. 
static TypeId LookupByName(std::string name)
virtual uint32_t GetRxAvailable(void) const =0
void Update(T const &x)
Add new sample.