diff -crB oldstatic-routing/ipv4-routing-table-entry.cc static-routing/ipv4-routing-table-entry.cc *** oldstatic-routing/ipv4-routing-table-entry.cc Tue Aug 11 11:42:23 2009 --- static-routing/ipv4-routing-table-entry.cc Tue Aug 11 12:33:22 2009 *************** *** 48,61 **** Ipv4Address gateway, uint32_t interface) : m_dest (dest), ! m_destNetworkMask (Ipv4Mask::GetZero ()), m_gateway (gateway), m_interface (interface) {} Ipv4RoutingTableEntry::Ipv4RoutingTableEntry (Ipv4Address dest, uint32_t interface) : m_dest (dest), ! m_destNetworkMask (Ipv4Mask::GetZero ()), m_gateway (Ipv4Address::GetZero ()), m_interface (interface) {} --- 48,61 ---- Ipv4Address gateway, uint32_t interface) : m_dest (dest), ! m_destNetworkMask (Ipv4Mask::GetOnes ()), m_gateway (gateway), m_interface (interface) {} Ipv4RoutingTableEntry::Ipv4RoutingTableEntry (Ipv4Address dest, uint32_t interface) : m_dest (dest), ! m_destNetworkMask (Ipv4Mask::GetOnes ()), m_gateway (Ipv4Address::GetZero ()), m_interface (interface) {} *************** *** 80,86 **** bool Ipv4RoutingTableEntry::IsHost (void) const { ! if (m_destNetworkMask.IsEqual (Ipv4Mask::GetZero ())) { return true; } --- 80,86 ---- bool Ipv4RoutingTableEntry::IsHost (void) const { ! if (m_destNetworkMask.IsEqual (Ipv4Mask::GetOnes ())) { return true; } diff -crB oldstatic-routing/ipv4-static-routing.cc static-routing/ipv4-static-routing.cc *** oldstatic-routing/ipv4-static-routing.cc Tue Aug 11 11:42:23 2009 --- static-routing/ipv4-static-routing.cc Tue Aug 11 14:48:23 2009 *************** *** 27,32 **** --- 27,34 ---- NS_LOG_COMPONENT_DEFINE ("Ipv4StaticRouting"); + using std::make_pair; + namespace ns3 { NS_OBJECT_ENSURE_REGISTERED (Ipv4StaticRouting); *************** *** 42,78 **** } Ipv4StaticRouting::Ipv4StaticRouting () ! : m_defaultRoute (0), m_ipv4 (0) ! { ! NS_LOG_FUNCTION_NOARGS (); ! } ! ! void ! Ipv4StaticRouting::AddHostRouteTo (Ipv4Address dest, ! Ipv4Address nextHop, ! uint32_t interface) ! { ! NS_LOG_FUNCTION_NOARGS (); ! Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); ! *route = Ipv4RoutingTableEntry::CreateHostRouteTo (dest, nextHop, interface); ! m_hostRoutes.push_back (route); ! } ! ! void ! Ipv4StaticRouting::AddHostRouteTo (Ipv4Address dest, ! uint32_t interface) { NS_LOG_FUNCTION_NOARGS (); - Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); - *route = Ipv4RoutingTableEntry::CreateHostRouteTo (dest, interface); - m_hostRoutes.push_back (route); } void Ipv4StaticRouting::AddNetworkRouteTo (Ipv4Address network, Ipv4Mask networkMask, Ipv4Address nextHop, ! uint32_t interface) { NS_LOG_FUNCTION_NOARGS (); Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); --- 44,60 ---- } Ipv4StaticRouting::Ipv4StaticRouting () ! : m_defaultRoute(0),m_defaultMetric(0xffff),m_ipv4 (0) { NS_LOG_FUNCTION_NOARGS (); } void Ipv4StaticRouting::AddNetworkRouteTo (Ipv4Address network, Ipv4Mask networkMask, Ipv4Address nextHop, ! uint32_t interface, ! uint32_t metric) { NS_LOG_FUNCTION_NOARGS (); Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); *************** *** 80,110 **** networkMask, nextHop, interface); ! m_networkRoutes.push_back (route); } void Ipv4StaticRouting::AddNetworkRouteTo (Ipv4Address network, Ipv4Mask networkMask, ! uint32_t interface) { NS_LOG_FUNCTION_NOARGS (); Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); *route = Ipv4RoutingTableEntry::CreateNetworkRouteTo (network, networkMask, interface); ! m_networkRoutes.push_back (route); } void Ipv4StaticRouting::SetDefaultRoute (Ipv4Address nextHop, ! uint32_t interface) { NS_LOG_FUNCTION_NOARGS (); ! Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); ! *route = Ipv4RoutingTableEntry::CreateDefaultRoute (nextHop, interface); ! delete m_defaultRoute; ! m_defaultRoute = route; } void --- 62,115 ---- networkMask, nextHop, interface); ! m_networkRoutes.push_back (make_pair(route,metric)); } void Ipv4StaticRouting::AddNetworkRouteTo (Ipv4Address network, Ipv4Mask networkMask, ! uint32_t interface, ! uint32_t metric) { NS_LOG_FUNCTION_NOARGS (); Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry (); *route = Ipv4RoutingTableEntry::CreateNetworkRouteTo (network, networkMask, interface); ! m_networkRoutes.push_back (make_pair (route,metric)); ! } ! ! void ! Ipv4StaticRouting::AddHostRouteTo (Ipv4Address dest, ! Ipv4Address nextHop, ! uint32_t interface, ! uint32_t metric) ! { ! NS_LOG_FUNCTION_NOARGS (); ! AddNetworkRouteTo(dest, Ipv4Mask::GetOnes(), nextHop, interface, metric); ! if (metric <= m_defaultMetric) ! { ! m_defaultRoute = m_networkRoutes.back().first; ! m_defaultMetric = metric; ! } ! } ! ! void ! Ipv4StaticRouting::AddHostRouteTo (Ipv4Address dest, ! uint32_t interface, ! uint32_t metric) ! { ! NS_LOG_FUNCTION_NOARGS (); ! AddNetworkRouteTo(dest, Ipv4Mask::GetOnes(), interface, metric); } void Ipv4StaticRouting::SetDefaultRoute (Ipv4Address nextHop, ! uint32_t interface, ! uint32_t metric) { NS_LOG_FUNCTION_NOARGS (); ! AddNetworkRouteTo(Ipv4Address("0.0.0.0"), Ipv4Mask::GetZero(), nextHop, interface, metric); } void *************** *** 133,139 **** *route = Ipv4RoutingTableEntry::CreateNetworkRouteTo (network, networkMask, outputInterface); ! m_networkRoutes.push_back (route); } uint32_t --- 138,144 ---- *route = Ipv4RoutingTableEntry::CreateNetworkRouteTo (network, networkMask, outputInterface); ! m_networkRoutes.push_back (make_pair(route,0)); } uint32_t *************** *** 214,271 **** { NS_LOG_FUNCTION_NOARGS (); Ptr rtentry = 0; ! for (HostRoutesCI i = m_hostRoutes.begin (); ! i != m_hostRoutes.end (); i++) { ! NS_ASSERT ((*i)->IsHost ()); ! if ((*i)->GetDest ().IsEqual (dest)) ! { ! NS_LOG_LOGIC ("Found global host route" << *i); ! Ipv4RoutingTableEntry* route = (*i); ! rtentry = Create (); ! uint32_t interfaceIdx = route->GetInterface (); ! rtentry->SetDestination (route->GetDest ()); ! rtentry->SetSource (SourceAddressSelection (interfaceIdx, route->GetDest ())); ! rtentry->SetGateway (route->GetGateway ()); ! rtentry->SetOutputDevice (m_ipv4->GetNetDevice (interfaceIdx)); ! return rtentry; ! } ! } ! for (NetworkRoutesI j = m_networkRoutes.begin (); ! j != m_networkRoutes.end (); ! j++) ! { ! NS_ASSERT ((*j)->IsNetwork ()); ! Ipv4Mask mask = (*j)->GetDestNetworkMask (); ! Ipv4Address entry = (*j)->GetDestNetwork (); if (mask.IsMatch (dest, entry)) { ! NS_LOG_LOGIC ("Found global network route" << *j); ! Ipv4RoutingTableEntry* route = (*j); ! rtentry = Create (); uint32_t interfaceIdx = route->GetInterface (); rtentry->SetDestination (route->GetDest ()); rtentry->SetSource (SourceAddressSelection (interfaceIdx, route->GetDest ())); rtentry->SetGateway (route->GetGateway ()); rtentry->SetOutputDevice (m_ipv4->GetNetDevice (interfaceIdx)); - return rtentry; } } ! if (m_defaultRoute != 0) ! { ! NS_ASSERT (m_defaultRoute->IsDefault ()); ! NS_LOG_LOGIC ("Found global network route" << m_defaultRoute); ! Ipv4RoutingTableEntry* route = m_defaultRoute; ! rtentry = Create (); ! uint32_t interfaceIdx = route->GetInterface (); ! rtentry->SetDestination (route->GetDest ()); ! rtentry->SetSource (SourceAddressSelection (interfaceIdx, route->GetDest ())); ! rtentry->SetGateway (route->GetGateway ()); ! rtentry->SetOutputDevice (m_ipv4->GetNetDevice (interfaceIdx)); ! return rtentry; ! } ! return 0; } Ptr --- 219,265 ---- { NS_LOG_FUNCTION_NOARGS (); Ptr rtentry = 0; ! uint16_t longest_mask = 0; ! uint32_t shortest_metric = 0xffffffff; ! for (NetworkRoutesI i = m_networkRoutes.begin (); ! i != m_networkRoutes.end (); i++) { ! Ipv4RoutingTableEntry *j=i->first; ! uint32_t metric =i->second; ! Ipv4Mask mask = (j)->GetDestNetworkMask (); ! uint16_t masklen = mask.GetMaskLength(); ! Ipv4Address entry = (j)->GetDestNetwork (); ! NS_LOG_LOGIC ("Searching for route to " << dest << ", checking against route to " << entry << "/" << masklen); if (mask.IsMatch (dest, entry)) { ! NS_LOG_LOGIC ("Found global network route " << j << ", mask length " << masklen << ", metric " << metric); ! if (masklen < longest_mask) // Not interested if got shorter mask ! { ! NS_LOG_LOGIC ("Previous match longer, skipping"); ! continue; ! } ! if (masklen > longest_mask) // Reset metric if longer masklen ! { ! shortest_metric = 0xffffffff; ! } ! if (metric > shortest_metric) ! { ! NS_LOG_LOGIC ("Equal mask length, but previous metric shorter, skipping"); ! continue; ! } ! shortest_metric = metric; ! Ipv4RoutingTableEntry* route = (j); uint32_t interfaceIdx = route->GetInterface (); + rtentry = Create (); rtentry->SetDestination (route->GetDest ()); rtentry->SetSource (SourceAddressSelection (interfaceIdx, route->GetDest ())); rtentry->SetGateway (route->GetGateway ()); rtentry->SetOutputDevice (m_ipv4->GetNetDevice (interfaceIdx)); } } ! NS_LOG_LOGIC ("Matching route via " << rtentry << " at the end"); ! return rtentry; } Ptr *************** *** 324,337 **** Ipv4StaticRouting::GetNRoutes (void) { NS_LOG_FUNCTION_NOARGS (); ! uint32_t n = 0; ! if (m_defaultRoute != 0) ! { ! n++; ! } ! n += m_hostRoutes.size (); ! n += m_networkRoutes.size (); ! return n; } Ipv4RoutingTableEntry --- 318,324 ---- Ipv4StaticRouting::GetNRoutes (void) { NS_LOG_FUNCTION_NOARGS (); ! return m_networkRoutes.size ();; } Ipv4RoutingTableEntry *************** *** 340,346 **** NS_LOG_FUNCTION_NOARGS (); if (m_defaultRoute != 0) { ! return *m_defaultRoute; } else { --- 327,333 ---- NS_LOG_FUNCTION_NOARGS (); if (m_defaultRoute != 0) { ! return m_defaultRoute; } else { *************** *** 352,380 **** Ipv4StaticRouting::GetRoute (uint32_t index) { NS_LOG_FUNCTION_NOARGS (); ! if (index == 0 && m_defaultRoute != 0) ! { ! return *m_defaultRoute; ! } ! if (index > 0 && m_defaultRoute != 0) ! { ! index--; ! } ! if (index < m_hostRoutes.size ()) { ! uint32_t tmp = 0; ! for (HostRoutesCI i = m_hostRoutes.begin (); ! i != m_hostRoutes.end (); ! i++) { ! if (tmp == index) ! { ! return *i; ! } ! tmp++; } } ! index -= m_hostRoutes.size (); uint32_t tmp = 0; for (NetworkRoutesI j = m_networkRoutes.begin (); j != m_networkRoutes.end (); --- 339,364 ---- Ipv4StaticRouting::GetRoute (uint32_t index) { NS_LOG_FUNCTION_NOARGS (); ! uint32_t tmp = 0; ! for (NetworkRoutesI j = m_networkRoutes.begin (); ! j != m_networkRoutes.end (); ! j++) { ! if (tmp == index) { ! return j->first; } + tmp++; } ! NS_ASSERT (false); ! // quiet compiler. ! return 0; ! } ! ! uint32_t ! Ipv4StaticRouting::GetMetric (uint32_t index) ! { ! NS_LOG_FUNCTION_NOARGS (); uint32_t tmp = 0; for (NetworkRoutesI j = m_networkRoutes.begin (); j != m_networkRoutes.end (); *************** *** 382,388 **** { if (tmp == index) { ! return *j; } tmp++; } --- 366,372 ---- { if (tmp == index) { ! return j->second; } tmp++; } *************** *** 394,425 **** Ipv4StaticRouting::RemoveRoute (uint32_t index) { NS_LOG_FUNCTION_NOARGS (); - if (index == 0 && m_defaultRoute != 0) - { - delete m_defaultRoute; - m_defaultRoute = 0; - } - if (index > 0 && m_defaultRoute != 0) - { - index--; - } - if (index < m_hostRoutes.size ()) - { - uint32_t tmp = 0; - for (HostRoutesI i = m_hostRoutes.begin (); - i != m_hostRoutes.end (); - i++) - { - if (tmp == index) - { - delete *i; - m_hostRoutes.erase (i); - return; - } - tmp++; - } - } - index -= m_hostRoutes.size (); uint32_t tmp = 0; for (NetworkRoutesI j = m_networkRoutes.begin (); j != m_networkRoutes.end (); --- 379,384 ---- *************** *** 427,433 **** { if (tmp == index) { ! delete *j; m_networkRoutes.erase (j); return; } --- 386,392 ---- { if (tmp == index) { ! delete j->first; m_networkRoutes.erase (j); return; } *************** *** 520,541 **** Ipv4StaticRouting::DoDispose (void) { NS_LOG_FUNCTION_NOARGS (); - for (HostRoutesI i = m_hostRoutes.begin (); - i != m_hostRoutes.end (); - i = m_hostRoutes.erase (i)) - { - delete (*i); - } for (NetworkRoutesI j = m_networkRoutes.begin (); j != m_networkRoutes.end (); j = m_networkRoutes.erase (j)) { ! delete (*j); ! } ! if (m_defaultRoute != 0) ! { ! delete m_defaultRoute; ! m_defaultRoute = 0; } for (MulticastRoutesI i = m_multicastRoutes.begin (); i != m_multicastRoutes.end (); --- 479,489 ---- Ipv4StaticRouting::DoDispose (void) { NS_LOG_FUNCTION_NOARGS (); for (NetworkRoutesI j = m_networkRoutes.begin (); j != m_networkRoutes.end (); j = m_networkRoutes.erase (j)) { ! delete (j->first); } for (MulticastRoutesI i = m_multicastRoutes.begin (); i != m_multicastRoutes.end (); diff -crB oldstatic-routing/ipv4-static-routing.h static-routing/ipv4-static-routing.h *** oldstatic-routing/ipv4-static-routing.h Tue Aug 11 11:42:23 2009 --- static-routing/ipv4-static-routing.h Tue Aug 11 12:52:39 2009 *************** *** 23,28 **** --- 23,29 ---- #define IPV4_STATIC_ROUTING_H #include + #include #include #include "ns3/ipv4-address.h" #include "ns3/ipv4-header.h" *************** *** 87,146 **** virtual void SetIpv4 (Ptr ipv4); /** ! * \brief Add a host route to the static routing table. * ! * \param dest The Ipv4Address destination for this route. ! * \param nextHop The Ipv4Address of the next hop in the route. * \param interface The network interface index used to send packets to the * destination. * * \see Ipv4Address */ ! void AddHostRouteTo (Ipv4Address dest, ! Ipv4Address nextHop, ! uint32_t interface); /** ! * \brief Add a host route to the static routing table. * ! * \param dest The Ipv4Address destination for this route. * \param interface The network interface index used to send packets to the * destination. * * \see Ipv4Address */ ! void AddHostRouteTo (Ipv4Address dest, ! uint32_t interface); /** ! * \brief Add a network route to the static routing table. * ! * \param network The Ipv4Address network for this route. ! * \param networkMask The Ipv4Mask to extract the network. ! * \param nextHop The next hop in the route to the destination network. * \param interface The network interface index used to send packets to the * destination. * * \see Ipv4Address */ ! void AddNetworkRouteTo (Ipv4Address network, ! Ipv4Mask networkMask, ! Ipv4Address nextHop, ! uint32_t interface); ! /** ! * \brief Add a network route to the static routing table. * ! * \param network The Ipv4Address network for this route. ! * \param networkMask The Ipv4Mask to extract the network. * \param interface The network interface index used to send packets to the * destination. * * \see Ipv4Address */ ! void AddNetworkRouteTo (Ipv4Address network, ! Ipv4Mask networkMask, ! uint32_t interface); ! /** * \brief Add a default route to the static routing table. * --- 88,154 ---- virtual void SetIpv4 (Ptr ipv4); /** ! * \brief Add a network route to the static routing table. * ! * \param network The Ipv4Address network for this route. ! * \param networkMask The Ipv4Mask to extract the network. ! * \param nextHop The next hop in the route to the destination network. * \param interface The network interface index used to send packets to the * destination. + * \param metric Metric of route in case of multiple routes to same destination * * \see Ipv4Address */ ! void AddNetworkRouteTo (Ipv4Address network, ! Ipv4Mask networkMask, ! Ipv4Address nextHop, ! uint32_t interface, ! uint32_t metric=0); ! /** ! * \brief Add a network route to the static routing table. * ! * \param network The Ipv4Address network for this route. ! * \param networkMask The Ipv4Mask to extract the network. * \param interface The network interface index used to send packets to the * destination. + * \param metric Metric of route in case of multiple routes to same destination * * \see Ipv4Address */ ! void AddNetworkRouteTo (Ipv4Address network, ! Ipv4Mask networkMask, ! uint32_t interface, ! uint32_t metric=0); /** ! * \brief Add a host route to the static routing table. * ! * \param dest The Ipv4Address destination for this route. ! * \param nextHop The Ipv4Address of the next hop in the route. * \param interface The network interface index used to send packets to the * destination. + * \param metric Metric of route in case of multiple routes to same destination * * \see Ipv4Address */ ! void AddHostRouteTo (Ipv4Address dest, ! Ipv4Address nextHop, ! uint32_t interface, ! uint32_t metric=0); /** ! * \brief Add a host route to the static routing table. * ! * \param dest The Ipv4Address destination for this route. * \param interface The network interface index used to send packets to the * destination. + * \param metric Metric of route in case of multiple routes to same destination * * \see Ipv4Address */ ! void AddHostRouteTo (Ipv4Address dest, ! uint32_t interface, ! uint32_t metric=0); /** * \brief Add a default route to the static routing table. * *************** *** 155,166 **** * \param nextHop The Ipv4Address to send packets to in the hope that they * will be forwarded correctly. * \param interface The network interface index used to send packets. * * \see Ipv4Address * \see Ipv4StaticRouting::Lookup */ void SetDefaultRoute (Ipv4Address nextHop, ! uint32_t interface); /** * \brief Get the number of individual unicast routes that have been added --- 163,176 ---- * \param nextHop The Ipv4Address to send packets to in the hope that they * will be forwarded correctly. * \param interface The network interface index used to send packets. + * \param metric Metric of route in case of multiple routes to same destination * * \see Ipv4Address * \see Ipv4StaticRouting::Lookup */ void SetDefaultRoute (Ipv4Address nextHop, ! uint32_t interface, ! uint32_t metric=0); /** * \brief Get the number of individual unicast routes that have been added *************** *** 171,180 **** uint32_t GetNRoutes (void); /** ! * \brief Get the default route from the static routing table. * * \return If the default route is set, a pointer to that Ipv4RoutingTableEntry is ! * returned, otherwise a zero pointer is returned. * * \see Ipv4RoutingTableEntry */ --- 181,191 ---- uint32_t GetNRoutes (void); /** ! * \brief Get the default route with lowest metric from the static routing table. * * \return If the default route is set, a pointer to that Ipv4RoutingTableEntry is ! * returned, otherwise a zero pointer is returned. If multiple default routes exist, ! * one with lowest metric is returned. * * \see Ipv4RoutingTableEntry */ *************** *** 203,208 **** --- 214,229 ---- Ipv4RoutingTableEntry GetRoute (uint32_t i); /** + * \brief Get a metric for route from the static unicast routing table. + * + * \param i The index (into the routing table) of the route to retrieve. If + * the default route has been set, it will occupy index zero. + * \return If route is set, the metric is returned. If not, an infinity metric (0xffffffff) is returned + * + */ + uint32_t GetMetric (uint32_t index); + + /** * \brief Remove a route from the static unicast routing table. * * Externally, the unicast static routing table appears simply as a table with *************** *** 366,377 **** virtual void DoDispose (void); private: ! typedef std::list HostRoutes; ! typedef std::list::const_iterator HostRoutesCI; ! typedef std::list::iterator HostRoutesI; ! typedef std::list NetworkRoutes; ! typedef std::list::const_iterator NetworkRoutesCI; ! typedef std::list::iterator NetworkRoutesI; typedef std::list MulticastRoutes; typedef std::list::const_iterator MulticastRoutesCI; --- 387,395 ---- virtual void DoDispose (void); private: ! typedef std::list > NetworkRoutes; ! typedef std::list >::const_iterator NetworkRoutesCI; ! typedef std::list >::iterator NetworkRoutesI; typedef std::list MulticastRoutes; typedef std::list::const_iterator MulticastRoutesCI; *************** *** 383,392 **** Ipv4Address SourceAddressSelection (uint32_t interface, Ipv4Address dest); - HostRoutes m_hostRoutes; NetworkRoutes m_networkRoutes; - Ipv4RoutingTableEntry *m_defaultRoute; MulticastRoutes m_multicastRoutes; Ptr m_ipv4; }; --- 401,410 ---- Ipv4Address SourceAddressSelection (uint32_t interface, Ipv4Address dest); NetworkRoutes m_networkRoutes; MulticastRoutes m_multicastRoutes; + Ipv4RoutingTableEntry *m_defaultRoute; + uint32_t m_defaultMetric; Ptr m_ipv4; };