src/internet-stack/icmpv4-l4-protocol.cc
author Tom Henderson <tomh@tomh.org>
Thu, 28 May 2009 21:37:25 -0700
changeset 4472 e20a31541404
parent 4375 db81fdcb06e7
child 4558 31e9053749bb
permissions -rw-r--r--
src/ and utils/ changes for IPv4 routing rework
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3820
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
#include "icmpv4-l4-protocol.h"
4472
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
     2
#include "ipv4-raw-socket-factory-impl.h"
3820
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
#include "ipv4-interface.h"
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
#include "ipv4-l3-protocol.h"
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
#include "ns3/assert.h"
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
#include "ns3/log.h"
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
#include "ns3/node.h"
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
#include "ns3/packet.h"
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
#include "ns3/boolean.h"
4472
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    10
#include "ns3/ipv4-route.h"
3820
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
namespace ns3 {
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
NS_LOG_COMPONENT_DEFINE ("Icmpv4L4Protocol");
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
NS_OBJECT_ENSURE_REGISTERED (Icmpv4L4Protocol);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
  // see rfc 792
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
enum {
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
 ICMP_PROTOCOL = 1
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
};
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
TypeId 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
Icmpv4L4Protocol::GetTypeId (void)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
  static TypeId tid = TypeId ("ns3::Icmpv4L4Protocol")
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
    .SetParent<Ipv4L4Protocol> ()
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
    .AddConstructor<Icmpv4L4Protocol> ()
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
    .AddAttribute ("CalcChecksum", 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
		   "Control whether the icmp header checksum is calculated and stored in outgoing icmpv4 headers",
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
		   BooleanValue (false),
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
		   MakeBooleanAccessor (&Icmpv4L4Protocol::m_calcChecksum),
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
		   MakeBooleanChecker ())
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
    ;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
  return tid;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
Icmpv4L4Protocol::Icmpv4L4Protocol ()
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
  : m_node (0)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
{}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
Icmpv4L4Protocol::~Icmpv4L4Protocol ()
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
  NS_ASSERT (m_node == 0);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
void 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
Icmpv4L4Protocol::SetNode (Ptr<Node> node)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
  m_node = node;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
4472
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    52
/*
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    53
 * This method is called by AddAgregate and completes the aggregation
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    54
 * by setting the node in the ICMP stack and adding ICMP factory to
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    55
 * IPv4 stack connected to the node
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    56
 */
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    57
void
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    58
Icmpv4L4Protocol::NotifyNewAggregate ()
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    59
{
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    60
  bool is_not_initialized = (m_node == 0);
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    61
  Ptr<Node>node = this->GetObject<Node> ();
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    62
  Ptr<Ipv4L3Protocol> ipv4 = this->GetObject<Ipv4L3Protocol> ();
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    63
  if (is_not_initialized && node!= 0 && ipv4 != 0)
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    64
    {
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    65
      this->SetNode (node);
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    66
      ipv4->Insert (this);
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    67
      Ptr<Ipv4RawSocketFactoryImpl> rawFactory = CreateObject<Ipv4RawSocketFactoryImpl> ();
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    68
      ipv4->AggregateObject (rawFactory);
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    69
    }
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    70
  Object::NotifyNewAggregate ();
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    71
}
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    72
3820
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
uint16_t 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
Icmpv4L4Protocol::GetStaticProtocolNumber (void)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
  return ICMP_PROTOCOL;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
int 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
Icmpv4L4Protocol::GetProtocolNumber (void) const
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
  return ICMP_PROTOCOL;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
void
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
Icmpv4L4Protocol::SendMessage (Ptr<Packet> packet, Ipv4Address dest, uint8_t type, uint8_t code)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
  Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
4472
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    88
  NS_ASSERT (ipv4 != 0 && ipv4->GetRoutingProtocol () != 0);
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    89
  Ipv4Header header;
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    90
  header.SetDestination (dest);
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    91
  Socket::SocketErrno errno;
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    92
  Ptr<Ipv4Route> route;
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    93
  uint32_t oif = 0; //specify non-zero if bound to a source address
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    94
  route = ipv4->GetRoutingProtocol ()->RouteOutput (header, oif, errno);
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    95
  if (route != 0)
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    96
    {
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    97
      NS_LOG_LOGIC ("Route exists");
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    98
     Ipv4Address source = route->GetSource ();
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
    99
     SendMessage (packet, source, dest, type, code, route);
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
   100
    }
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
   101
  else
3820
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
    {
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
      NS_LOG_WARN ("drop icmp message");
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
    }
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
void
4472
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
   108
Icmpv4L4Protocol::SendMessage (Ptr<Packet> packet, Ipv4Address source, Ipv4Address dest, uint8_t type, uint8_t code, Ptr<Ipv4Route> route)
3820
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
  Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
  Icmpv4Header icmp;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  icmp.SetType (type);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
  icmp.SetCode (code);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
  if (m_calcChecksum)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
    {
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
      icmp.EnableChecksum ();
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
    }
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
  packet->AddHeader (icmp);
4472
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
   119
  ipv4->Send (packet, source, dest, ICMP_PROTOCOL, route);
3820
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
void 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
Icmpv4L4Protocol::SendDestUnreachFragNeeded (Ipv4Header header, 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
					     Ptr<const Packet> orgData, 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
					     uint16_t nextHopMtu)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
  NS_LOG_FUNCTION (this << header << *orgData << nextHopMtu);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
  SendDestUnreach (header, orgData, Icmpv4DestinationUnreachable::FRAG_NEEDED, nextHopMtu);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
void 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
Icmpv4L4Protocol::SendDestUnreachPort (Ipv4Header header, 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
				       Ptr<const Packet> orgData)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   132
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
  NS_LOG_FUNCTION (this << header << *orgData);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
  SendDestUnreach (header, orgData, Icmpv4DestinationUnreachable::PORT_UNREACHABLE, 0);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
void 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
Icmpv4L4Protocol::SendDestUnreach (Ipv4Header header, Ptr<const Packet> orgData, 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
				   uint8_t code, uint16_t nextHopMtu)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
  NS_LOG_FUNCTION (this << header << *orgData << (uint32_t) code << nextHopMtu);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
  Ptr<Packet> p = Create<Packet> ();
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   142
  Icmpv4DestinationUnreachable unreach;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
  unreach.SetNextHopMtu (nextHopMtu);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
  unreach.SetHeader (header);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
  unreach.SetData (orgData);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
  p->AddHeader (unreach);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
  SendMessage (p, header.GetSource (), Icmpv4Header::DEST_UNREACH, code);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
void 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
Icmpv4L4Protocol::SendTimeExceededTtl (Ipv4Header header, Ptr<const Packet> orgData)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
  NS_LOG_FUNCTION (this << header << *orgData);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
  Ptr<Packet> p = Create<Packet> ();
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
  Icmpv4TimeExceeded time;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
  time.SetHeader (header);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
  time.SetData (orgData);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
  p->AddHeader (time);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
  SendMessage (p, header.GetSource (), Icmpv4Header::TIME_EXCEEDED, Icmpv4TimeExceeded::TIME_TO_LIVE);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
void
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
Icmpv4L4Protocol::HandleEcho (Ptr<Packet> p,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
			      Icmpv4Header header, 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
			      Ipv4Address source,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
			      Ipv4Address destination)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
  NS_LOG_FUNCTION (this << p << header << source << destination);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
  Ptr<Packet> reply = Create<Packet> ();
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   171
  Icmpv4Echo echo;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
  p->RemoveHeader (echo);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   173
  reply->AddHeader (echo);
4472
e20a31541404 src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents: 4375
diff changeset
   174
  SendMessage (reply, destination, source, Icmpv4Header::ECHO_REPLY, 0, 0);
3820
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
void
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
Icmpv4L4Protocol::Forward (Ipv4Address source, Icmpv4Header icmp,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
			   uint32_t info, Ipv4Header ipHeader,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
			   const uint8_t payload[8])
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
  Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
  Ptr<Ipv4L4Protocol> l4 = ipv4->GetProtocol (ipHeader.GetProtocol ());
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
  if (l4 != 0)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
    {
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
      l4->ReceiveIcmp (source, ipHeader.GetTtl (), icmp.GetType (), icmp.GetCode (),
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
		       info, ipHeader.GetSource (), ipHeader.GetDestination (), payload);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
    }
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
void
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
Icmpv4L4Protocol::HandleDestUnreach (Ptr<Packet> p,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
				     Icmpv4Header icmp, 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
				     Ipv4Address source,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
				     Ipv4Address destination)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
  NS_LOG_FUNCTION (this << p << icmp << source << destination);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   197
  Icmpv4DestinationUnreachable unreach;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   198
  p->PeekHeader (unreach);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
  uint8_t payload[8];
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
  unreach.GetData (payload);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   201
  Ipv4Header ipHeader = unreach.GetHeader ();
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   202
  Forward (source, icmp, unreach.GetNextHopMtu (), ipHeader, payload);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   203
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   204
void
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
Icmpv4L4Protocol::HandleTimeExceeded (Ptr<Packet> p,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   206
				      Icmpv4Header icmp, 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
				      Ipv4Address source,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
				      Ipv4Address destination)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
  NS_LOG_FUNCTION (this << p << icmp << source << destination);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
  Icmpv4TimeExceeded time;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
  p->PeekHeader (time);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
  uint8_t payload[8];
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   215
  time.GetData (payload);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   216
  Ipv4Header ipHeader = time.GetHeader ();
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   217
  // info field is zero for TimeExceeded on linux
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   218
  Forward (source, icmp, 0, ipHeader, payload);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   219
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   220
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   221
enum Ipv4L4Protocol::RxStatus
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   222
Icmpv4L4Protocol::Receive(Ptr<Packet> p, 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   223
			  Ipv4Address const &source,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   224
			  Ipv4Address const &destination,
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   225
			  Ptr<Ipv4Interface> incomingInterface)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   226
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   227
  NS_LOG_FUNCTION (this << p << source << destination << incomingInterface);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   228
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   229
  Icmpv4Header icmp;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   230
  p->RemoveHeader (icmp);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   231
  switch (icmp.GetType ()) {
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   232
  case Icmpv4Header::ECHO:
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   233
    HandleEcho (p, icmp, source, destination);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   234
    break;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   235
  case Icmpv4Header::DEST_UNREACH:
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   236
    HandleDestUnreach (p, icmp, source, destination);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   237
    break;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   238
  case Icmpv4Header::TIME_EXCEEDED:
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   239
    HandleTimeExceeded (p, icmp, source, destination);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   240
    break;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   241
  default:
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   242
    NS_LOG_DEBUG (icmp << " " << *p);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   243
    break;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   244
  }
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   245
  return Ipv4L4Protocol::RX_OK;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   246
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   247
void 
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   248
Icmpv4L4Protocol::DoDispose (void)
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   249
{
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   250
  NS_LOG_FUNCTION (this);
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   251
  m_node = 0;
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   252
  Ipv4L4Protocol::DoDispose ();
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   253
}
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   254
c04ecfdce1ef apply icmp patch
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   255
} // namespace ns3