diff --git a/src/internet-node/arp-ipv4-interface.cc b/src/internet-node/arp-ipv4-interface.cc index 5a8e19514..e8be25ea1 100644 --- a/src/internet-node/arp-ipv4-interface.cc +++ b/src/internet-node/arp-ipv4-interface.cc @@ -24,16 +24,34 @@ #include "ns3/node.h" #include "ns3/net-device.h" #include "ns3/address.h" +#include "ns3/pointer.h" #include "arp-ipv4-interface.h" #include "ipv4-l3-protocol.h" #include "arp-l3-protocol.h" +#include "arp-cache.h" NS_LOG_COMPONENT_DEFINE ("ArpIpv4Interface"); namespace ns3 { +TypeId +ArpIpv4Interface::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::ArpIpv4Interface") + .SetParent () + .AddAttribute ("ArpCache", + "The arp cache for this ipv4 interface", + PointerValue (0), + MakePointerAccessor (&ArpIpv4Interface::m_cache), + MakePointerChecker ()) + ; + return tid; +} + ArpIpv4Interface::ArpIpv4Interface () + : m_node (0), + m_device (0) { NS_LOG_FUNCTION_NOARGS (); } @@ -48,17 +66,20 @@ ArpIpv4Interface::DoDispose (void) { m_node = 0; m_device = 0; + m_cache = 0; } void ArpIpv4Interface::SetNode (Ptr node) { m_node = node; + DoSetup (); } void ArpIpv4Interface::SetDevice (Ptr device) { m_device = device; + DoSetup (); } Ptr @@ -67,13 +88,24 @@ ArpIpv4Interface::GetDevice (void) const return m_device; } +void +ArpIpv4Interface::DoSetup (void) +{ + if (m_node == 0 || m_device == 0) + { + return; + } + Ptr arp = m_node->GetObject (); + m_cache = arp->CreateCache (m_device, this); +} + void ArpIpv4Interface::SendTo (Ptr p, Ipv4Address dest) { NS_LOG_FUNCTION (this << p << dest); NS_ASSERT (GetDevice () != 0); - if (GetDevice ()->NeedsArp ()) + if (m_device->NeedsArp ()) { NS_LOG_LOGIC ("Needs ARP"); Ptr arp = @@ -101,7 +133,7 @@ ArpIpv4Interface::SendTo (Ptr p, Ipv4Address dest) else { NS_LOG_LOGIC ("ARP Lookup"); - found = arp->Lookup (p, dest, GetDevice (), &hardwareDestination); + found = arp->Lookup (p, dest, GetDevice (), m_cache, &hardwareDestination); } if (found) diff --git a/src/internet-node/arp-ipv4-interface.h b/src/internet-node/arp-ipv4-interface.h index 8f7f080d9..2c5300e36 100644 --- a/src/internet-node/arp-ipv4-interface.h +++ b/src/internet-node/arp-ipv4-interface.h @@ -27,6 +27,7 @@ namespace ns3 { class Node; +class ArpCache; /** * \brief an Ipv4 Interface which uses ARP @@ -37,7 +38,9 @@ class Node; */ class ArpIpv4Interface : public Ipv4Interface { - public: +public: + static TypeId GetTypeId (void); + ArpIpv4Interface (); virtual ~ArpIpv4Interface (); @@ -49,8 +52,10 @@ class ArpIpv4Interface : public Ipv4Interface private: virtual void SendTo (Ptr p, Ipv4Address dest); virtual void DoDispose (void); + void DoSetup (void); Ptr m_node; Ptr m_device; + Ptr m_cache; }; }//namespace ns3 diff --git a/src/internet-node/arp-l3-protocol.cc b/src/internet-node/arp-l3-protocol.cc index d8c824c69..60a80bd86 100644 --- a/src/internet-node/arp-l3-protocol.cc +++ b/src/internet-node/arp-l3-protocol.cc @@ -76,6 +76,18 @@ ArpL3Protocol::DoDispose (void) Object::DoDispose (); } +Ptr +ArpL3Protocol::CreateCache (Ptr device, Ptr interface) +{ + Ptr ipv4 = m_node->GetObject (); + Ptr cache = CreateObject (); + cache->SetDevice (device, interface); + NS_ASSERT (device->IsBroadcast ()); + device->SetLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache)); + m_cacheList.push_back (cache); + return cache; +} + Ptr ArpL3Protocol::FindCache (Ptr device) { @@ -87,14 +99,9 @@ ArpL3Protocol::FindCache (Ptr device) return *i; } } - Ptr ipv4 = m_node->GetObject (); - Ptr interface = ipv4->FindInterfaceForDevice (device); - Ptr cache = CreateObject (); - cache->SetDevice (device, interface); - NS_ASSERT (device->IsBroadcast ()); - device->SetLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache)); - m_cacheList.push_back (cache); - return cache; + NS_ASSERT (false); + // quiet compiler + return 0; } void @@ -167,10 +174,10 @@ ArpL3Protocol::Receive(Ptr device, Ptr packet, uint16_t proto bool ArpL3Protocol::Lookup (Ptr packet, Ipv4Address destination, Ptr device, + Ptr cache, Address *hardwareDestination) { NS_LOG_FUNCTION_NOARGS (); - Ptr cache = FindCache (device); ArpCache::Entry *entry = cache->Lookup (destination); if (entry != 0) { diff --git a/src/internet-node/arp-l3-protocol.h b/src/internet-node/arp-l3-protocol.h index daa5245a4..efd61966b 100644 --- a/src/internet-node/arp-l3-protocol.h +++ b/src/internet-node/arp-l3-protocol.h @@ -46,6 +46,8 @@ public: void SetNode (Ptr node); + Ptr CreateCache (Ptr device, Ptr interface); + /** * \brief Recieve a packet */ @@ -60,6 +62,7 @@ public: */ bool Lookup (Ptr p, Ipv4Address destination, Ptr device, + Ptr cache, Address *hardwareDestination); protected: virtual void DoDispose (void); diff --git a/src/internet-node/ipv4-loopback-interface.cc b/src/internet-node/ipv4-loopback-interface.cc index 68acda205..c4e69dece 100644 --- a/src/internet-node/ipv4-loopback-interface.cc +++ b/src/internet-node/ipv4-loopback-interface.cc @@ -31,6 +31,15 @@ NS_LOG_COMPONENT_DEFINE ("Ipv4LoopbackInterface"); namespace ns3 { +TypeId +Ipv4LoopbackInterface::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::Ipv4LoopbackInterface") + .SetParent () + ; + return tid; +} + Ipv4LoopbackInterface::Ipv4LoopbackInterface () : m_node (0) { diff --git a/src/internet-node/ipv4-loopback-interface.h b/src/internet-node/ipv4-loopback-interface.h index 098de027b..23255386a 100644 --- a/src/internet-node/ipv4-loopback-interface.h +++ b/src/internet-node/ipv4-loopback-interface.h @@ -32,7 +32,8 @@ class Node; */ class Ipv4LoopbackInterface : public Ipv4Interface { - public: +public: + static TypeId GetTypeId (void); Ipv4LoopbackInterface (); virtual ~Ipv4LoopbackInterface ();