src/internet-node/arp-ipv4-interface.cc
changeset 3894 6b3415c550e1
parent 3893 94f771c1373a
parent 3261 b0987a6a74c8
child 3895 b584563a7781
equal deleted inserted replaced
3893:94f771c1373a 3894:6b3415c550e1
     1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2007 INRIA
       
     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  * Authors: 
       
    19  *  Mathieu Lacage <mathieu.lacage@sophia.inria.fr>,
       
    20  */
       
    21 
       
    22 #include "ns3/packet.h"
       
    23 #include "ns3/log.h"
       
    24 #include "ns3/node.h"
       
    25 #include "ns3/net-device.h"
       
    26 #include "ns3/address.h"
       
    27 #include "ns3/pointer.h"
       
    28 
       
    29 #include "arp-ipv4-interface.h"
       
    30 #include "ipv4-l3-protocol.h"
       
    31 #include "arp-l3-protocol.h"
       
    32 #include "arp-cache.h"
       
    33 
       
    34 NS_LOG_COMPONENT_DEFINE ("ArpIpv4Interface");
       
    35 
       
    36 namespace ns3 {
       
    37 
       
    38 TypeId 
       
    39 ArpIpv4Interface::GetTypeId (void)
       
    40 {
       
    41   static TypeId tid = TypeId ("ns3::ArpIpv4Interface")
       
    42     .SetParent<Ipv4Interface> ()
       
    43     .AddAttribute ("ArpCache",
       
    44                    "The arp cache for this ipv4 interface",
       
    45                    PointerValue (0),
       
    46                    MakePointerAccessor (&ArpIpv4Interface::m_cache),
       
    47                    MakePointerChecker<ArpIpv4Interface> ())
       
    48     ;
       
    49   return tid;
       
    50 }
       
    51 
       
    52 ArpIpv4Interface::ArpIpv4Interface ()
       
    53   : m_node (0),
       
    54     m_device (0)
       
    55 {
       
    56   NS_LOG_FUNCTION (this);
       
    57 }
       
    58 
       
    59 ArpIpv4Interface::~ArpIpv4Interface ()
       
    60 {
       
    61   NS_LOG_FUNCTION (this);
       
    62 }
       
    63 
       
    64 void 
       
    65 ArpIpv4Interface::DoDispose (void)
       
    66 {
       
    67   NS_LOG_FUNCTION (this);
       
    68   m_node = 0;
       
    69   m_device = 0;
       
    70   m_cache = 0;
       
    71   Ipv4Interface::DoDispose ();
       
    72 }
       
    73 
       
    74 void 
       
    75 ArpIpv4Interface::SetNode (Ptr<Node> node)
       
    76 {
       
    77   m_node = node;
       
    78   DoSetup ();
       
    79 }
       
    80 void 
       
    81 ArpIpv4Interface::SetDevice (Ptr<NetDevice> device)
       
    82 {
       
    83   m_device = device;
       
    84   DoSetup ();
       
    85 }
       
    86 
       
    87 Ptr<NetDevice> 
       
    88 ArpIpv4Interface::GetDevice (void) const
       
    89 {
       
    90   return m_device;
       
    91 }
       
    92 
       
    93 void
       
    94 ArpIpv4Interface::DoSetup (void)
       
    95 {
       
    96   if (m_node == 0 || m_device == 0)
       
    97     {
       
    98       return;
       
    99     }
       
   100   Ptr<ArpL3Protocol> arp = m_node->GetObject<ArpL3Protocol> ();
       
   101   m_cache = arp->CreateCache (m_device, this);
       
   102 }
       
   103 
       
   104 void 
       
   105 ArpIpv4Interface::SendTo (Ptr<Packet> p, Ipv4Address dest)
       
   106 {
       
   107   NS_LOG_FUNCTION (this << p << dest);
       
   108 
       
   109   NS_ASSERT (GetDevice () != 0);
       
   110   if (m_device->NeedsArp ())
       
   111     {
       
   112       NS_LOG_LOGIC ("Needs ARP");
       
   113       Ptr<ArpL3Protocol> arp = 
       
   114         m_node->GetObject<ArpL3Protocol> ();
       
   115       Address hardwareDestination;
       
   116       bool found;
       
   117       
       
   118       if (dest.IsBroadcast () || 
       
   119           dest.IsSubnetDirectedBroadcast (GetNetworkMask ()) )
       
   120         {
       
   121           NS_LOG_LOGIC ("IsBroadcast");
       
   122           hardwareDestination = GetDevice ()->GetBroadcast ();
       
   123           found = true;
       
   124         }
       
   125       else if (dest.IsMulticast ())
       
   126         {
       
   127           NS_LOG_LOGIC ("IsMulticast");
       
   128           NS_ASSERT_MSG(GetDevice ()->IsMulticast (),
       
   129             "ArpIpv4Interface::SendTo (): Sending multicast packet over "
       
   130             "non-multicast device");
       
   131 
       
   132           hardwareDestination = GetDevice ()->MakeMulticastAddress(dest);
       
   133           found = true;
       
   134         }
       
   135       else
       
   136         {
       
   137           NS_LOG_LOGIC ("ARP Lookup");
       
   138           found = arp->Lookup (p, dest, GetDevice (), m_cache, &hardwareDestination);
       
   139         }
       
   140 
       
   141       if (found)
       
   142         {
       
   143           NS_LOG_LOGIC ("Address Resolved.  Send.");
       
   144           GetDevice ()->Send (p, hardwareDestination, 
       
   145             Ipv4L3Protocol::PROT_NUMBER);
       
   146         }
       
   147     }
       
   148   else
       
   149     {
       
   150       NS_LOG_LOGIC ("Doesn't need ARP");
       
   151       GetDevice ()->Send (p, GetDevice ()->GetBroadcast (), 
       
   152         Ipv4L3Protocol::PROT_NUMBER);
       
   153     }
       
   154 }
       
   155 
       
   156 }//namespace ns3