diff --git a/src/node/ipv4.cc b/src/node/ipv4.cc index f99675e25..9578d4c8a 100644 --- a/src/node/ipv4.cc +++ b/src/node/ipv4.cc @@ -18,6 +18,9 @@ * * Author: Mathieu Lacage */ + +#include "ns3/assert.h" +#include "ns3/node.h" #include "ipv4.h" namespace ns3 { @@ -32,4 +35,48 @@ Ipv4::Ipv4 () Ipv4::~Ipv4 () {} +uint32_t +GetIfIndexByIpv4Address (Ptr node, Ipv4Address a, Ipv4Mask amask) +{ + Ptr ipv4 = node->QueryInterface (Ipv4::iid); + NS_ASSERT_MSG (ipv4, "GetIfIndexByIpv4Address: No Ipv4 interface"); + for (uint32_t i = 0; i < ipv4->GetNInterfaces (); i++) + { + if (ipv4->GetAddress (i).CombineMask(amask) == a.CombineMask(amask) ) + { + return i; + } + } + // Mapping not found + NS_ASSERT_MSG (false, "GetIfIndexByIpv4Address failed"); + return 0; +} + +bool +GetIpv4RouteToDestination (Ptr node, Ipv4Route& route, + Ipv4Address a, Ipv4Mask amask) +{ + Ipv4Route tempRoute; + Ptr ipv4 = node->QueryInterface (Ipv4::iid); + NS_ASSERT_MSG (ipv4, "GetIpv4RouteToDestination: No Ipv4 interface"); + for (uint32_t i = 0; i < ipv4->GetNRoutes (); i++) + { + tempRoute = ipv4->GetRoute (i); + // Host route found + if ( tempRoute.IsNetwork () == false && tempRoute.GetDest () == a ) + { + route = tempRoute; + return true; + } + else if ( tempRoute.IsNetwork () && + tempRoute.GetDestNetwork () == a.CombineMask(amask) ) + { + route = tempRoute; + return true; + } + } + return false; +} + + } // namespace ns3 diff --git a/src/node/ipv4.h b/src/node/ipv4.h index 76d88faea..dc5e146bd 100644 --- a/src/node/ipv4.h +++ b/src/node/ipv4.h @@ -29,6 +29,7 @@ namespace ns3 { +class Node; class NetDevice; class Packet; class Ipv4Route; @@ -273,6 +274,19 @@ public: }; +/** + * Convenience functions (Doxygen still needed) + * + * Return the ifIndex corresponding to the Ipv4Address provided. + */ +uint32_t GetIfIndexByIpv4Address (Ptr node, + Ipv4Address a, + Ipv4Mask amask = Ipv4Mask("255.255.255.255")); + +bool GetIpv4RouteToDestination (Ptr node, Ipv4Route& route, + Ipv4Address a, + Ipv4Mask amask = Ipv4Mask("255.255.255.255")); + } // namespace ns3 #endif /* IPV4_H */ diff --git a/src/routing/global-routing/global-route-manager-impl.cc b/src/routing/global-routing/global-route-manager-impl.cc index 42c63b9b9..c2b7021c9 100644 --- a/src/routing/global-routing/global-route-manager-impl.cc +++ b/src/routing/global-routing/global-route-manager-impl.cc @@ -1088,83 +1088,10 @@ GlobalRouteManagerImpl::SPFCalculate (Ipv4Address root) m_spfroot = 0; } -// -// XXX This should probably be a method on Ipv4 -// -// Return the interface index corresponding to a given IP address -// - uint32_t -GlobalRouteManagerImpl::FindOutgoingInterfaceId (Ipv4Address a) -{ -// -// We have an IP address and a vertex ID of the root of the SPF tree. -// The question is what interface index does this address correspond to. -// The answer is a little complicated since we have to find a pointer to -// the node corresponding to the vertex ID, find the Ipv4 interface on that -// node in order to iterate the interfaces and find the one corresponding to -// the address in question. -// - Ipv4Address routerId = m_spfroot->GetVertexId (); -// -// Walk the list of nodes in the system looking for the one corresponding to -// the node at the root of the SPF tree. This is the node for which we are -// building the routing table. -// - NodeList::Iterator i = NodeList::Begin (); - for (; i != NodeList::End (); i++) - { - Ptr node = *i; - - Ptr rtr = - node->QueryInterface (GlobalRouter::iid); -// -// If the node doesn't have a GlobalRouter interface it can't be the one -// we're interested in. -// - if (rtr == 0) - { - continue; - } - - if (rtr->GetRouterId () == routerId) - { -// -// This is the node we're building the routing table for. We're going to need -// the Ipv4 interface to look for the ipv4 interface index. Since this node -// is participating in routing IP version 4 packets, it certainly must have -// an Ipv4 interface. -// - Ptr ipv4 = node->QueryInterface (Ipv4::iid); - NS_ASSERT_MSG (ipv4, - "GlobalRouteManagerImpl::FindOutgoingInterfaceId (): " - "QI for interface failed"); -// -// Look through the interfaces on this node for one that has the IP address -// we're looking for. If we find one, return the corresponding interface -// index. -// - for (uint32_t i = 0; i < ipv4->GetNInterfaces (); i++) - { - if (ipv4->GetAddress (i) == a) - { - NS_DEBUG ( - "GlobalRouteManagerImpl::FindOutgoingInterfaceId (): " - "Interface match for " << a); - return i; - } - } - } - } -// -// Couldn't find it. -// - return 0; -} - -// -// XXX This should probably be a method on Ipv4 // // Return the interface index corresponding to a given IP address +// This is a wrapper around GetIfIndexByIpv4Address(), but we first +// have to find the right node pointer to pass to that function. // uint32_t GlobalRouteManagerImpl::FindOutgoingInterfaceId (Ipv4Address a, Ipv4Mask amask) @@ -1216,17 +1143,7 @@ GlobalRouteManagerImpl::FindOutgoingInterfaceId (Ipv4Address a, Ipv4Mask amask) // we're looking for. If we find one, return the corresponding interface // index. // - for (uint32_t i = 0; i < ipv4->GetNInterfaces (); i++) - { - if (ipv4->GetAddress (i).CombineMask(amask) == - a.CombineMask(amask) ) - { - NS_DEBUG ( - "GlobalRouteManagerImpl::FindOutgoingInterfaceId (): " - "Interface match for " << a); - return i; - } - } + return (GetIfIndexByIpv4Address (node, a, amask) ); } } // @@ -1693,6 +1610,9 @@ GlobalRouteManagerImplTest::RunTests (void) srm->DebugSPFCalculate (lsa0->GetLinkStateId ()); // node n0 Simulator::Run (); + +// XXX here we should do some verification of the routes built + Simulator::Destroy (); // This delete clears the srm, which deletes the LSDB, which clears @@ -1703,7 +1623,6 @@ GlobalRouteManagerImplTest::RunTests (void) } // Instantiate this class for the unit tests -// XXX here we should do some verification of the routes built static GlobalRouteManagerImplTest g_globalRouteManagerTest; } // namespace ns3 diff --git a/src/routing/global-routing/global-route-manager-impl.h b/src/routing/global-routing/global-route-manager-impl.h index 739e20592..923caf400 100644 --- a/src/routing/global-routing/global-route-manager-impl.h +++ b/src/routing/global-routing/global-route-manager-impl.h @@ -754,8 +754,8 @@ private: GlobalRoutingLinkRecord* prev_link); void SPFIntraAddRouter (SPFVertex* v); void SPFIntraAddTransit (SPFVertex* v); - uint32_t FindOutgoingInterfaceId (Ipv4Address a); - uint32_t FindOutgoingInterfaceId (Ipv4Address a, Ipv4Mask amask); + uint32_t FindOutgoingInterfaceId (Ipv4Address a, + Ipv4Mask amask = Ipv4Mask("255.255.255.255")); }; } // namespace ns3