diff --git a/src/node/application.cc b/src/node/application.cc index 4cdb8d6fa..ce98cb5ae 100644 --- a/src/node/application.cc +++ b/src/node/application.cc @@ -37,8 +37,10 @@ namespace ns3 { // Application Methods // \brief Application Constructor - Application::Application(const Node& n) : m_startVar(nil), m_stopVar(nil), - m_start(false), m_stop(false) + Application::Application(const Node& n) + : m_node (0), + m_startVar(nil), m_stopVar(nil), + m_start(false), m_stop(false) { SetNode(n); } diff --git a/src/node/arp-cache.cc b/src/node/arp-cache.cc index d1d477b5c..6a6f71bc9 100644 --- a/src/node/arp-cache.cc +++ b/src/node/arp-cache.cc @@ -34,15 +34,18 @@ ArpCache::ArpCache (NetDevice *device, Ipv4Interface *interface) m_aliveTimeout (Seconds (120)), m_deadTimeout (Seconds (100)), m_waitReplyTimeout (Seconds (1)) -{} +{ + m_device->Ref (); +} ArpCache::~ArpCache () { + m_device->Unref (); Flush (); } NetDevice * -ArpCache::GetDevice (void) const +ArpCache::PeekDevice (void) const { return m_device; } diff --git a/src/node/arp-cache.h b/src/node/arp-cache.h index f71e9ce9a..ddee3b840 100644 --- a/src/node/arp-cache.h +++ b/src/node/arp-cache.h @@ -24,6 +24,7 @@ #include #include "ns3/packet.h" #include "ns3/nstime.h" +#include "net-device.h" #include "ipv4-address.h" #include "mac-address.h" #include "sgi-hashmap.h" @@ -40,7 +41,7 @@ public: ArpCache (NetDevice *device, Ipv4Interface *interface); ~ArpCache (); - NetDevice *GetDevice (void) const; + NetDevice *PeekDevice (void) const; Ipv4Interface *GetInterface (void) const; void SetAliveTimeout (Time aliveTimeout); diff --git a/src/node/arp-ipv4-interface.cc b/src/node/arp-ipv4-interface.cc index 00e8554e6..9c7ce8215 100644 --- a/src/node/arp-ipv4-interface.cc +++ b/src/node/arp-ipv4-interface.cc @@ -42,10 +42,10 @@ TraceResolver * ArpIpv4Interface::DoCreateTraceResolver (TraceContext const &context) { CompositeTraceResolver *resolver = new CompositeTraceResolver (context); - if (GetDevice () != 0) + if (PeekDevice () != 0) { resolver->Add ("netdevice", - MakeCallback (&NetDevice::CreateTraceResolver, GetDevice ()), + MakeCallback (&NetDevice::CreateTraceResolver, PeekDevice ()), ArpIpv4Interface::NETDEVICE); } @@ -55,20 +55,20 @@ ArpIpv4Interface::DoCreateTraceResolver (TraceContext const &context) void ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest) { - NS_ASSERT (GetDevice () != 0); - if (GetDevice ()->NeedsArp ()) + NS_ASSERT (PeekDevice () != 0); + if (PeekDevice ()->NeedsArp ()) { Arp * arp = m_node->GetArp (); MacAddress hardwareDestination; - bool found = arp->Lookup (p, dest, GetDevice (), &hardwareDestination); + bool found = arp->Lookup (p, dest, PeekDevice (), &hardwareDestination); if (found) { - GetDevice ()->Send (p, hardwareDestination, Ipv4::PROT_NUMBER); + PeekDevice ()->Send (p, hardwareDestination, Ipv4::PROT_NUMBER); } } else { - GetDevice ()->Send (p, GetDevice ()->GetBroadcast (), Ipv4::PROT_NUMBER); + PeekDevice ()->Send (p, PeekDevice ()->GetBroadcast (), Ipv4::PROT_NUMBER); } } diff --git a/src/node/arp.cc b/src/node/arp.cc index 3f4c50c06..c3918fafb 100644 --- a/src/node/arp.cc +++ b/src/node/arp.cc @@ -65,7 +65,7 @@ Arp::FindCache (NetDevice *device) { for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++) { - if ((*i)->GetDevice () == device) + if ((*i)->PeekDevice () == device) { return *i; } @@ -79,9 +79,9 @@ Arp::FindCache (NetDevice *device) } void -Arp::Receive(Packet& packet, NetDevice &device) +Arp::Receive(Packet& packet, NetDevice *device) { - ArpCache *cache = FindCache (&device); + ArpCache *cache = FindCache (device); ArpHeader arp; packet.RemoveHeader (arp); if (arp.IsRequest () && @@ -94,7 +94,7 @@ Arp::Receive(Packet& packet, NetDevice &device) } else if (arp.IsReply () && arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress ()) && - arp.GetDestinationHardwareAddress ().IsEqual (device.GetAddress ())) + arp.GetDestinationHardwareAddress ().IsEqual (device->GetAddress ())) { Ipv4Address from = arp.GetSourceIpv4Address (); ArpCache::Entry *entry = cache->Lookup (from); @@ -200,25 +200,25 @@ void Arp::SendArpRequest (ArpCache const *cache, Ipv4Address to) { ArpHeader arp; - arp.SetRequest (cache->GetDevice ()->GetAddress (), + arp.SetRequest (cache->PeekDevice ()->GetAddress (), cache->GetInterface ()->GetAddress (), - cache->GetDevice ()->GetBroadcast (), + cache->PeekDevice ()->GetBroadcast (), to); Packet packet; packet.AddHeader (arp); - cache->GetDevice ()->Send (packet, cache->GetDevice ()->GetBroadcast (), PROT_NUMBER); + cache->PeekDevice ()->Send (packet, cache->PeekDevice ()->GetBroadcast (), PROT_NUMBER); } void Arp::SendArpReply (ArpCache const *cache, Ipv4Address toIp, MacAddress toMac) { ArpHeader arp; - arp.SetReply (cache->GetDevice ()->GetAddress (), + arp.SetReply (cache->PeekDevice ()->GetAddress (), cache->GetInterface ()->GetAddress (), toMac, toIp); Packet packet; packet.AddHeader (arp); - cache->GetDevice ()->Send (packet, toMac, PROT_NUMBER); + cache->PeekDevice ()->Send (packet, toMac, PROT_NUMBER); } }//namespace ns3 diff --git a/src/node/arp.h b/src/node/arp.h index c940de43f..80aa68623 100644 --- a/src/node/arp.h +++ b/src/node/arp.h @@ -46,7 +46,7 @@ public: virtual TraceResolver *CreateTraceResolver (TraceContext const &context); - virtual void Receive(Packet& p, NetDevice &device); + virtual void Receive(Packet& p, NetDevice *device); bool Lookup (Packet &p, Ipv4Address destination, NetDevice *device, MacAddress *hardwareDestination); diff --git a/src/node/internet-node.cc b/src/node/internet-node.cc index 1f58a35cd..af6adfcff 100644 --- a/src/node/internet-node.cc +++ b/src/node/internet-node.cc @@ -160,7 +160,7 @@ InternetNode::ReceiveFromDevice (NetDevice *device, const Packet &p, uint16_t pr if (target != 0) { Packet packet = p; - target->Receive(packet, *device); + target->Receive(packet, device); return true; } return false; diff --git a/src/node/ipv4-interface.cc b/src/node/ipv4-interface.cc index 98311a2ae..6fc5aacbe 100644 --- a/src/node/ipv4-interface.cc +++ b/src/node/ipv4-interface.cc @@ -34,13 +34,23 @@ namespace ns3 { Ipv4Interface::Ipv4Interface (NetDevice *nd) : m_netdevice (nd), m_ifup(false) -{} +{ + if (m_netdevice != 0) + { + m_netdevice->Ref (); + } +} Ipv4Interface::~Ipv4Interface () -{} +{ + if (m_netdevice != 0) + { + m_netdevice->Unref (); + } +} NetDevice* -Ipv4Interface::GetDevice (void) const +Ipv4Interface::PeekDevice (void) const { return m_netdevice; } diff --git a/src/node/ipv4-interface.h b/src/node/ipv4-interface.h index 173fc0f69..31948f247 100644 --- a/src/node/ipv4-interface.h +++ b/src/node/ipv4-interface.h @@ -87,7 +87,7 @@ public: * \returns the underlying NetDevice. This method can return * zero if this interface has no associated NetDevice. */ - NetDevice *GetDevice (void) const; + NetDevice *PeekDevice (void) const; /** * \param a set the ipv4 address of this interface. diff --git a/src/node/ipv4-loopback-interface.cc b/src/node/ipv4-loopback-interface.cc index 3700e6178..6c0ec5f53 100644 --- a/src/node/ipv4-loopback-interface.cc +++ b/src/node/ipv4-loopback-interface.cc @@ -33,9 +33,7 @@ Ipv4LoopbackInterface::Ipv4LoopbackInterface (Node *node) { } Ipv4LoopbackInterface::~Ipv4LoopbackInterface () -{ - delete GetDevice (); -} +{} Node * Ipv4LoopbackInterface::GetNode (void) const @@ -52,7 +50,7 @@ Ipv4LoopbackInterface::DoCreateTraceResolver (TraceContext const &context) void Ipv4LoopbackInterface::SendTo (Packet packet, Ipv4Address dest) { - m_node->GetIpv4 ()->Receive (packet, *(this->GetDevice ())); + m_node->GetIpv4 ()->Receive (packet, PeekDevice ()); } }//namespace ns3 diff --git a/src/node/ipv4.cc b/src/node/ipv4.cc index 05d22e96b..8b8c2516b 100644 --- a/src/node/ipv4.cc +++ b/src/node/ipv4.cc @@ -338,7 +338,7 @@ Ipv4::FindInterfaceForDevice (NetDevice const*device) { for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++) { - if ((*i)->GetDevice () == device) + if ((*i)->PeekDevice () == device) { return *i; } @@ -354,12 +354,12 @@ Ipv4::Copy(Node *node) const return ipv4; } void -Ipv4::Receive(Packet& packet, NetDevice &device) +Ipv4::Receive(Packet& packet, NetDevice *device) { uint32_t index = 0; for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++) { - if ((*i)->GetDevice () == &device) + if ((*i)->PeekDevice () == device) { m_rxTrace (packet, index); break; @@ -439,7 +439,7 @@ Ipv4::SendRealOut (Packet const &p, Ipv4Header const &ip, Ipv4Route const &route bool -Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice &device) +Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice *device) { for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++) @@ -455,7 +455,7 @@ Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice &device) i != m_interfaces.end (); i++) { Ipv4Interface *interface = *i; - if (interface->GetDevice () == &device) + if (interface->PeekDevice () == device) { if (ipHeader.GetDestination ().IsEqual (interface->GetBroadcast ())) { diff --git a/src/node/ipv4.h b/src/node/ipv4.h index d0c52d694..cda60fa64 100644 --- a/src/node/ipv4.h +++ b/src/node/ipv4.h @@ -193,7 +193,7 @@ public: * - implement a per-NetDevice ARP cache * - send back arp replies on the right device */ - virtual void Receive(Packet& p, NetDevice &device); + virtual void Receive(Packet& p, NetDevice *device); /** * \param packet packet to send @@ -219,7 +219,7 @@ public: private: void SendRealOut (Packet const &packet, Ipv4Header const &ip, Ipv4Route const &route); - bool Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice &device); + bool Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice *device); void ForwardUp (Packet p, Ipv4Header const&ip); uint32_t AddIpv4Interface (Ipv4Interface *interface); void SetupLoopback (void); diff --git a/src/node/l3-protocol.h b/src/node/l3-protocol.h index 59b41e8f2..2d690792f 100644 --- a/src/node/l3-protocol.h +++ b/src/node/l3-protocol.h @@ -54,7 +54,7 @@ public: * - implement a per-NetDevice ARP cache * - send back arp replies on the right device */ - virtual void Receive(Packet& p, NetDevice &device) = 0; + virtual void Receive(Packet& p, NetDevice *device) = 0; private: int m_protocolNumber; diff --git a/src/node/node.cc b/src/node/node.cc index 35170df38..a85fbc714 100644 --- a/src/node/node.cc +++ b/src/node/node.cc @@ -72,6 +72,7 @@ Node::SetSystemId(uint32_t s ) uint32_t Node::AddDevice (NetDevice *device) { + device->Ref (); uint32_t index = m_devices.size (); m_devices.push_back (device); DoAddDevice (device);