diff --git a/src/node/arp-ipv4-interface.cc b/src/node/arp-ipv4-interface.cc index ae987b8d0..71c8165ae 100644 --- a/src/node/arp-ipv4-interface.cc +++ b/src/node/arp-ipv4-interface.cc @@ -69,6 +69,7 @@ ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest) { PeekDevice ()->Send (p, hardwareDestination, Ipv4::PROT_NUMBER); } + arp->Unref (); } else { diff --git a/src/node/arp.cc b/src/node/arp.cc index a74f539cb..7761848a0 100644 --- a/src/node/arp.cc +++ b/src/node/arp.cc @@ -84,7 +84,9 @@ Arp::FindCache (NetDevice *device) return *i; } } - Ipv4Interface *interface = m_node->GetIpv4 ()->FindInterfaceForDevice (device); + Ipv4 *ipv4 = m_node->GetIpv4 (); + Ipv4Interface *interface = ipv4->FindInterfaceForDevice (device); + ipv4->Unref (); ArpCache * cache = new ArpCache (device, interface); NS_ASSERT (device->IsBroadcast ()); device->SetLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache)); diff --git a/src/node/internet-node.cc b/src/node/internet-node.cc index 5e4201ca6..15aeb565b 100644 --- a/src/node/internet-node.cc +++ b/src/node/internet-node.cc @@ -61,15 +61,24 @@ TraceResolver * InternetNode::CreateTraceResolver (TraceContext const &context) { CompositeTraceResolver *resolver = new CompositeTraceResolver (context); + Ipv4 *ipv4 = GetIpv4 (); resolver->Add ("ipv4", - MakeCallback (&Ipv4::CreateTraceResolver, GetIpv4 ()), + MakeCallback (&Ipv4::CreateTraceResolver, ipv4), InternetNode::IPV4); + ipv4->Unref (); + + Arp *arp = GetArp (); resolver->Add ("arp", - MakeCallback (&Arp::CreateTraceResolver, GetArp ()), + MakeCallback (&Arp::CreateTraceResolver, arp), InternetNode::ARP); + arp->Unref (); + + Udp *udp = GetUdp (); resolver->Add ("udp", - MakeCallback (&Udp::CreateTraceResolver, GetUdp ()), + MakeCallback (&Udp::CreateTraceResolver, udp), InternetNode::UDP); + udp->Unref (); + return resolver; } @@ -102,36 +111,45 @@ void InternetNode::Dispose() ApplicationList* InternetNode::GetApplicationList() const { + m_applicationList->Ref (); return m_applicationList; } L3Demux* InternetNode::GetL3Demux() const { + m_l3Demux->Ref (); return m_l3Demux; } Ipv4L4Demux* InternetNode::GetIpv4L4Demux() const { + m_ipv4L4Demux->Ref (); return m_ipv4L4Demux; } Ipv4 * InternetNode::GetIpv4 (void) const { - return static_cast (m_l3Demux->Lookup (Ipv4::PROT_NUMBER)); + Ipv4 *ipv4 = static_cast (m_l3Demux->PeekProtocol (Ipv4::PROT_NUMBER)); + ipv4->Ref (); + return ipv4; } Udp * InternetNode::GetUdp (void) const { - return static_cast (m_ipv4L4Demux->Lookup (Udp::PROT_NUMBER)); + Udp *udp = static_cast (m_ipv4L4Demux->PeekProtocol (Udp::PROT_NUMBER)); + udp->Ref (); + return udp; } Arp * InternetNode::GetArp (void) const { - return static_cast (m_l3Demux->Lookup (Arp::PROT_NUMBER)); + Arp *arp = static_cast (m_l3Demux->PeekProtocol (Arp::PROT_NUMBER)); + arp->Ref (); + return arp; } void @@ -143,7 +161,7 @@ InternetNode::DoAddDevice (NetDevice *device) const bool InternetNode::ReceiveFromDevice (NetDevice *device, const Packet &p, uint16_t protocolNumber) const { - L3Protocol *target = GetL3Demux()->Lookup(protocolNumber); + L3Protocol *target = GetL3Demux()->PeekProtocol (protocolNumber); if (target != 0) { Packet packet = p; diff --git a/src/node/ipv4-l4-demux.cc b/src/node/ipv4-l4-demux.cc index 651865028..ba37dc6a3 100644 --- a/src/node/ipv4-l4-demux.cc +++ b/src/node/ipv4-l4-demux.cc @@ -91,7 +91,7 @@ Ipv4L4Demux::Insert(const Ipv4L4Protocol&protocol) return copy; } Ipv4L4Protocol* -Ipv4L4Demux::Lookup(int protocolNumber) +Ipv4L4Demux::PeekProtocol(int protocolNumber) { for (L4List_t::iterator i = m_protocols.begin(); i != m_protocols.end(); ++i) { diff --git a/src/node/ipv4-l4-demux.h b/src/node/ipv4-l4-demux.h index d1fdcf1cb..c8fba5454 100644 --- a/src/node/ipv4-l4-demux.h +++ b/src/node/ipv4-l4-demux.h @@ -81,7 +81,7 @@ public: * to forward packets up the stack to the right protocol. * It is also called from InternetNode::GetUdp for example. */ - Ipv4L4Protocol* Lookup(int protocolNumber); + Ipv4L4Protocol* PeekProtocol(int protocolNumber); /** * \param protocol protocol to remove from this demux. * diff --git a/src/node/ipv4-loopback-interface.cc b/src/node/ipv4-loopback-interface.cc index 4a2a968d0..fd506fd01 100644 --- a/src/node/ipv4-loopback-interface.cc +++ b/src/node/ipv4-loopback-interface.cc @@ -47,7 +47,9 @@ Ipv4LoopbackInterface::DoCreateTraceResolver (TraceContext const &context) void Ipv4LoopbackInterface::SendTo (Packet packet, Ipv4Address dest) { - m_node->GetIpv4 ()->Receive (packet, PeekDevice ()); + Ipv4 *ipv4 = m_node->GetIpv4 (); + ipv4->Receive (packet, PeekDevice ()); + ipv4->Unref (); } }//namespace ns3 diff --git a/src/node/ipv4.cc b/src/node/ipv4.cc index 7723210e0..d5f0a9423 100644 --- a/src/node/ipv4.cc +++ b/src/node/ipv4.cc @@ -518,7 +518,9 @@ Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice *device) void Ipv4::ForwardUp (Packet p, Ipv4Header const&ip) { - Ipv4L4Protocol *protocol = m_node->GetIpv4L4Demux ()->Lookup (ip.GetProtocol ()); + Ipv4L4Demux *demux = m_node->GetIpv4L4Demux (); + Ipv4L4Protocol *protocol = demux->PeekProtocol (ip.GetProtocol ()); + demux->Unref (); protocol->Receive (p, ip.GetSource (), ip.GetDestination ()); } diff --git a/src/node/l3-demux.cc b/src/node/l3-demux.cc index 9d34a3ab8..7bee2bffd 100644 --- a/src/node/l3-demux.cc +++ b/src/node/l3-demux.cc @@ -91,7 +91,8 @@ L3Protocol* L3Demux::Insert(const L3Protocol& l3p) return l; } -L3Protocol* L3Demux::Lookup(int p) +L3Protocol* +L3Demux::PeekProtocol (int p) { // Look up a protocol by protocol number L3Map_t::iterator i = m_protocols.find(p); if (i == m_protocols.end()) return 0; // Not found diff --git a/src/node/l3-demux.h b/src/node/l3-demux.h index 1e54d5d45..72b5adaff 100644 --- a/src/node/l3-demux.h +++ b/src/node/l3-demux.h @@ -85,7 +85,7 @@ public: * to forward packets up the stack to the right protocol. * It is also called from InternetNode::GetIpv4 for example. */ - ns3::L3Protocol* Lookup(int protocolNumber); + ns3::L3Protocol* PeekProtocol (int protocolNumber); /** * \param protocol protocol to remove from this demux. * diff --git a/src/node/onoff-application.cc b/src/node/onoff-application.cc index 19544e3f4..5a939a914 100644 --- a/src/node/onoff-application.cc +++ b/src/node/onoff-application.cc @@ -172,7 +172,10 @@ void OnOffApplication::StartApplication() // Called at time specified by Star MakeCallback(&OnOffApplication::ConnectionFailed, this)); #endif - m_socket = PeekNode ()->GetUdp ()->CreateSocket (); + + Udp *udp = PeekNode ()->GetUdp (); + m_socket = udp->CreateSocket (); + udp->Unref (); m_socket->Connect (m_peerIP, m_peerPort); } StopApplication(); // Insure no pending event diff --git a/src/node/udp-socket.cc b/src/node/udp-socket.cc index b20657174..5b41d3dd4 100644 --- a/src/node/udp-socket.cc +++ b/src/node/udp-socket.cc @@ -35,6 +35,7 @@ UdpSocket::UdpSocket (Node *node) m_connected (false) { NS_ASSERT (GetUdp () != 0); + m_udp = m_node->GetUdp (); m_node->Ref (); } UdpSocket::~UdpSocket () @@ -57,6 +58,11 @@ UdpSocket::Destroy (void) m_node = 0; } m_endPoint = 0; + if (m_udp != 0) + { + m_udp->Unref (); + m_udp = 0; + } } int UdpSocket::FinishBind (void) @@ -73,25 +79,25 @@ UdpSocket::FinishBind (void) int UdpSocket::Bind (void) { - m_endPoint = GetUdp ()->Allocate (); + m_endPoint = m_udp->Allocate (); return FinishBind (); } int UdpSocket::Bind (Ipv4Address address) { - m_endPoint = GetUdp ()->Allocate (address); + m_endPoint = m_udp->Allocate (address); return FinishBind (); } int UdpSocket::Bind (uint16_t port) { - m_endPoint = GetUdp ()->Allocate (port); + m_endPoint = m_udp->Allocate (port); return FinishBind (); } int UdpSocket::Bind (Ipv4Address address, uint16_t port) { - m_endPoint = GetUdp ()->Allocate (address, port); + m_endPoint = m_udp->Allocate (address, port); return FinishBind (); } @@ -185,7 +191,7 @@ UdpSocket::DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport, m_errno = ESHUTDOWN; return -1; } - GetUdp ()->Send (p, m_endPoint->GetLocalAddress (), daddr, + m_udp->Send (p, m_endPoint->GetLocalAddress (), daddr, m_endPoint->GetLocalPort (), dport); if (!dataSent.IsNull ()) { @@ -245,11 +251,4 @@ UdpSocket::ForwardUp (const Packet &packet, Ipv4Address saddr, uint16_t sport) } } -Udp * -UdpSocket::GetUdp (void) const -{ - return m_node->GetUdp (); -} - - }//namespace ns3 diff --git a/src/node/udp-socket.h b/src/node/udp-socket.h index d5a0ed060..d2a910aef 100644 --- a/src/node/udp-socket.h +++ b/src/node/udp-socket.h @@ -83,6 +83,7 @@ private: Ipv4EndPoint *m_endPoint; Node *m_node; + Udp *m_udp; Ipv4Address m_defaultAddress; uint16_t m_defaultPort; Callback m_dummyRxCallback; diff --git a/src/node/udp.cc b/src/node/udp.cc index e7c2c82aa..a981a12c9 100644 --- a/src/node/udp.cc +++ b/src/node/udp.cc @@ -146,6 +146,7 @@ Udp::Send (Packet packet, if (ipv4 != 0) { ipv4->Send (packet, saddr, daddr, PROT_NUMBER); + ipv4->Unref (); } }