src/applications/v4ping/v4ping.cc
author Craig Dowell <craigdo@ee.washington.edu>
Mon, 22 Jun 2009 18:29:43 -0700
changeset 4576 96811f76c3e2
parent 4521 981e2f0b696b
child 5568 6e159c6c34bb
permissions -rw-r--r--
remind v4ping.cc that echo data is variable length
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4576
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
     2
/*
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
     5
 * published by the Free Software Foundation;
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
     6
 *
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
    10
 * GNU General Public License for more details.
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
    11
 *
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
    12
 * You should have received a copy of the GNU General Public License
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
    13
 * along with this program; if not, write to the Free Software
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
    15
 */
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
#include "v4ping.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
#include "ns3/icmpv4.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
#include "ns3/assert.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
#include "ns3/log.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#include "ns3/ipv4-address.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
#include "ns3/socket.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "ns3/uinteger.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
#include "ns3/inet-socket-address.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
#include "ns3/packet.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
#include "ns3/trace-source-accessor.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
#include "ns3/simulator.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
namespace ns3 {
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
NS_LOG_COMPONENT_DEFINE ("V4Ping");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
NS_OBJECT_ENSURE_REGISTERED (V4Ping);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
TypeId 
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
V4Ping::GetTypeId (void)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
  static TypeId tid = TypeId ("ns3::V4Ping")
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
    .SetParent<Application> ()
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
    .AddConstructor<V4Ping> ()
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
    .AddAttribute ("Remote", 
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
		   "The address of the machine we want to ping.",
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
		   Ipv4AddressValue (),
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
		   MakeIpv4AddressAccessor (&V4Ping::m_remote),
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
		   MakeIpv4AddressChecker ())
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
    .AddTraceSource ("Rtt",
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
		     "The rtt calculated by the ping.",
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
		     MakeTraceSourceAccessor (&V4Ping::m_traceRtt));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
    ;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
  return tid;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
V4Ping::V4Ping ()
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
  : m_socket (0),
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
    m_seq (0)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
{}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
V4Ping::~V4Ping ()
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
{}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
void
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
V4Ping::DoDispose (void)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
  NS_LOG_FUNCTION (this);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
  m_socket = 0;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
  Application::DoDispose ();
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
uint32_t
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
V4Ping::GetApplicationId (void) const
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
  Ptr<Node> node = GetNode ();
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
  for (uint32_t i = 0; i < node->GetNApplications (); ++i)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
    {
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
      if (node->GetApplication (i) == this)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
	{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
	  return i;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
	}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
    }
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
  NS_ASSERT_MSG (false, "forgot to add application to node");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
  return 0; // quiet compiler
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
void
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
V4Ping::Receive (Ptr<Socket> socket)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  NS_LOG_FUNCTION (this << socket);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
  while (m_socket->GetRxAvailable () > 0)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
    {
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
      Address from;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
      Ptr<Packet> p = m_socket->RecvFrom (0xffffffff, 0, from);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
      NS_LOG_DEBUG ("recv " << p->GetSize () << " bytes");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
      NS_ASSERT (InetSocketAddress::IsMatchingType (from));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
      InetSocketAddress realFrom = InetSocketAddress::ConvertFrom (from);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
      NS_ASSERT (realFrom.GetPort () == 1); // protocol should be icmp.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
      Ipv4Header ipv4;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
      p->RemoveHeader (ipv4);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
      NS_ASSERT (ipv4.GetProtocol () == 1); // protocol should be icmp.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
      Icmpv4Header icmp;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
      p->RemoveHeader (icmp);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
      if (icmp.GetType () == Icmpv4Header::ECHO_REPLY)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
	{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
	  Icmpv4Echo echo;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
	  p->RemoveHeader (echo);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
	  if (echo.GetSequenceNumber () == (m_seq - 1) &&
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
	      echo.GetIdentifier () == 0)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
	    {
4576
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
   105
              uint32_t buf[4];
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
   106
	      uint32_t dataSize = echo.GetDataSize ();
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
   107
	      if (dataSize == sizeof(buf))
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
		{
4576
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
   109
                  echo.GetData ((uint8_t *)buf);
96811f76c3e2 remind v4ping.cc that echo data is variable length
Craig Dowell <craigdo@ee.washington.edu>
parents: 4521
diff changeset
   110
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
		  if (buf[0] == GetNode ()->GetId () &&
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
		      buf[1] == GetApplicationId ())
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
		    {
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
		      int64_t ts = buf[3];
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
		      ts <<= 32;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
		      ts |= buf[2];
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
		      Time sendTime = TimeStep (ts);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
		      NS_ASSERT (Simulator::Now () > sendTime);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
		      Time delta = Simulator::Now () - sendTime;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
		      m_traceRtt (delta);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
		    }
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
		}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
	    }
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
	}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
    }
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
4026
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   128
void
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   129
V4Ping::Write32 (uint8_t *buffer, uint32_t data)
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   130
{
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   131
  buffer[0] = (data >> 0) & 0xff;
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   132
  buffer[1] = (data >> 8) & 0xff;
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   133
  buffer[2] = (data >> 16) & 0xff;
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   134
  buffer[3] = (data >> 24) & 0xff;
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   135
}
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   136
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
void 
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
V4Ping::StartApplication (void)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
  NS_LOG_FUNCTION (this);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
  m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::Ipv4RawSocketFactory"));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   142
  NS_ASSERT (m_socket != 0);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
  m_socket->SetAttribute ("Protocol", UintegerValue (1)); // icmp
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
  m_socket->SetRecvCallback (MakeCallback (&V4Ping::Receive, this));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
  InetSocketAddress src = InetSocketAddress (Ipv4Address::GetAny (), 0);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
  int status;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
  status = m_socket->Bind (src);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
  NS_ASSERT (status != -1);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
  InetSocketAddress dst = InetSocketAddress (m_remote, 0);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
  status = m_socket->Connect (dst);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
  NS_ASSERT (status != -1);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
  Ptr<Packet> p = Create<Packet> ();
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
  Icmpv4Echo echo;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
  echo.SetSequenceNumber (m_seq);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
  m_seq++;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
  echo.SetIdentifier (0);
4026
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   157
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   158
  //
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   159
  // We must write quantities out in some form of network order.  Since there
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   160
  // isn't an htonl to work with we just follow the convention in pcap traces
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   161
  // (where any difference would show up anyway) and borrow that code.  Don't
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   162
  // be too surprised when you see that this is a little endian convention.
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   163
  //
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   164
  uint8_t data[4 * sizeof(uint32_t)];
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   165
  uint32_t tmp = GetNode ()->GetId ();
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   166
  Write32 (&data[0 * sizeof(uint32_t)], tmp);
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   167
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   168
  tmp = GetApplicationId ();
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   169
  Write32 (&data[1 * sizeof(uint32_t)], tmp);
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   170
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   171
  int64_t now = Simulator::Now ().GetTimeStep ();
4026
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   172
  tmp = now & 0xffffffff;
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   173
  Write32 (&data[2 * sizeof(uint32_t)], tmp);
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   174
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
  now >>= 32;
4026
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   176
  tmp = now & 0xffffffff;
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   177
  Write32 (&data[3 * sizeof(uint32_t)], tmp);
58ae52c5845f bug 448: V4Ping Application Sends uint32_t Data in Host Order
Craig Dowell <craigdo@ee.washington.edu>
parents: 3932
diff changeset
   178
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
  Ptr<Packet> dataPacket = Create<Packet> ((uint8_t *) &data, 16);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
  echo.SetData (dataPacket);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
  p->AddHeader (echo);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
  Icmpv4Header header;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
  header.SetType (Icmpv4Header::ECHO);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
  header.SetCode (0);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
  p->AddHeader (header);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
  m_socket->Send (p, 0);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
  
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
void 
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
V4Ping::StopApplication (void)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
  NS_LOG_FUNCTION (this);
3932
76fabfcfd757 bug 421: memory leak in csma-ping
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3821
diff changeset
   193
  m_socket->Close ();
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   197
} // namespace ns3