move list routing and static routing to src/routing directories

--HG--
rename : src/internet-stack/ipv4-list-routing.cc => src/routing/list-routing/ipv4-list-routing.cc
rename : src/internet-stack/ipv4-list-routing.h => src/routing/list-routing/ipv4-list-routing.h
rename : src/node/ipv4-routing-table-entry.cc => src/routing/static-routing/ipv4-routing-table-entry.cc
rename : src/node/ipv4-routing-table-entry.h => src/routing/static-routing/ipv4-routing-table-entry.h
rename : src/internet-stack/ipv4-static-routing-impl.cc => src/routing/static-routing/ipv4-static-routing.cc
rename : src/node/ipv4-static-routing.h => src/routing/static-routing/ipv4-static-routing.h
This commit is contained in:
Tom Henderson committed 2009-06-21 23:32:23 -07:00
1 parent ce3e645272
commit b79dacedd2
19 files changed
+154 -241

No files matched your search

-2
View File
@@ -32,13 +32,11 @@
#include "ns3/ipv4-header.h"
#include "ns3/boolean.h"
#include "ns3/ipv4-routing-table-entry.h"
#include "ns3/ipv4-static-routing.h"
#include "loopback-net-device.h"
#include "arp-l3-protocol.h"
#include "ipv4-l3-protocol.h"
#include "ipv4-l4-protocol.h"
#include "ipv4-list-routing.h"
#include "icmpv4-l4-protocol.h"
#include "ipv4-interface.h"
#include "ipv4-raw-socket-impl.h"
-1
View File
@@ -30,7 +30,6 @@
#include "ns3/traced-callback.h"
#include "ns3/ipv4-header.h"
#include "ns3/ipv4-routing-protocol.h"
#include "ipv4-static-routing-impl.h"
namespace ns3 {
-411
View File
@@ -1,411 +0,0 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2009 University of Washington
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "ns3/log.h"
#include "ns3/ipv4.h"
#include "ns3/ipv4-route.h"
#include "ns3/node.h"
#include "ns3/ipv4-static-routing.h"
#include "ipv4-list-routing.h"
NS_LOG_COMPONENT_DEFINE ("Ipv4ListRouting");
namespace ns3 {
NS_OBJECT_ENSURE_REGISTERED (Ipv4ListRouting);
TypeId
Ipv4ListRouting::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::Ipv4ListRouting")
.SetParent<Ipv4RoutingProtocol> ()
.AddConstructor<Ipv4ListRouting> ()
;
return tid;
}
Ipv4ListRouting::Ipv4ListRouting ()
: m_ipv4 (0)
{
NS_LOG_FUNCTION_NOARGS ();
}
Ipv4ListRouting::~Ipv4ListRouting ()
{
NS_LOG_FUNCTION_NOARGS ();
}
void
Ipv4ListRouting::DoDispose (void)
{
NS_LOG_FUNCTION_NOARGS ();
for (Ipv4RoutingProtocolList::iterator rprotoIter = m_routingProtocols.begin ();
rprotoIter != m_routingProtocols.end (); rprotoIter++)
{
// Note: Calling dispose on these protocols causes memory leak
// The routing protocols should not maintain a pointer to
// this object, so Dispose() shouldn't be necessary.
(*rprotoIter).second = 0;
}
m_routingProtocols.clear ();
m_ipv4 = 0;
}
Ptr<Ipv4Route>
Ipv4ListRouting::RouteOutput (const Ipv4Header &header, uint32_t oif, enum Socket::SocketErrno &sockerr)
{
NS_LOG_FUNCTION (this << header.GetDestination () << " " << header.GetSource () << " " << oif);
Ptr<Ipv4Route> route;
for (Ipv4RoutingProtocolList::const_iterator i = m_routingProtocols.begin ();
i != m_routingProtocols.end (); i++)
{
NS_LOG_LOGIC ("Checking protocol " << (*i).second->GetInstanceTypeId () << " with priority " << (*i).first);
NS_LOG_LOGIC ("Requesting source address for destination " << header.GetDestination ());
route = (*i).second->RouteOutput (header, oif, sockerr);
if (route)
{
NS_LOG_LOGIC ("Found route " << route);
sockerr = Socket::ERROR_NOTERROR;
return route;
}
}
NS_LOG_LOGIC ("Done checking " << GetTypeId ());
NS_LOG_LOGIC ("");
sockerr = Socket::ERROR_NOROUTETOHOST;
return 0;
}
// Patterned after Linux ip_route_input and ip_route_input_slow
bool
Ipv4ListRouting::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,
UnicastForwardCallback ucb, MulticastForwardCallback mcb,
LocalDeliverCallback lcb, ErrorCallback ecb)
{
bool retVal = false;
NS_LOG_FUNCTION (p << header << idev);
NS_LOG_LOGIC ("RouteInput logic for node: " << m_ipv4->GetObject<Node> ()->GetId ());
NS_ASSERT (m_ipv4 != 0);
uint32_t iif = m_ipv4->GetInterfaceForDevice (idev);
// Multicast recognition; handle local delivery here
//
if (header.GetDestination().IsMulticast ())
{
#ifdef NOTYET
if (m_ipv4->MulticastCheckGroup (iif, header.GetDestination ()))
#endif
if (true)
{
NS_LOG_LOGIC ("Multicast packet for me-- local deliver");
Ptr<Packet> packetCopy = p->Copy();
// Here may want to disable lcb callback in recursive RouteInput
// call below
lcb (packetCopy, header, iif);
// Fall through-- we may also need to forward this
retVal = true;
}
for (Ipv4RoutingProtocolList::const_iterator rprotoIter =
m_routingProtocols.begin (); rprotoIter != m_routingProtocols.end ();
rprotoIter++)
{
NS_LOG_LOGIC ("Multicast packet for me-- trying to forward");
if ((*rprotoIter).second->RouteInput (p, header, idev, ucb, mcb, lcb, ecb))
{
retVal = true;
}
}
return retVal;
}
if (header.GetDestination ().IsBroadcast ())
{
NS_LOG_LOGIC ("For me (Ipv4Addr broadcast address)");
// TODO: Local Deliver for broadcast
// TODO: Forward broadcast
}
// TODO: Configurable option to enable RFC 1222 Strong End System Model
// Right now, we will be permissive and allow a source to send us
// a packet to one of our other interface addresses; that is, the
// destination unicast address does not match one of the iif addresses,
// but we check our other interfaces. This could be an option
// (to remove the outer loop immediately below and just check iif).
for (uint32_t j = 0; j < m_ipv4->GetNInterfaces (); j++)
{
for (uint32_t i = 0; i < m_ipv4->GetNAddresses (j); i++)
{
Ipv4InterfaceAddress iaddr = m_ipv4->GetAddress (j, i);
Ipv4Address addr = iaddr.GetLocal ();
if (addr.IsEqual (header.GetDestination ()))
{
if (j == iif)
{
NS_LOG_LOGIC ("For me (destination " << addr << " match)");
}
else
{
NS_LOG_LOGIC ("For me (destination " << addr << " match) on another interface " << header.GetDestination ());
}
lcb (p, header, iif);
return true;
}
if (header.GetDestination ().IsEqual (iaddr.GetBroadcast ()))
{
NS_LOG_LOGIC ("For me (interface broadcast address)");
lcb (p, header, iif);
return true;
}
NS_LOG_LOGIC ("Address "<< addr << " not a match");
}
}
// Next, try to find a route
for (Ipv4RoutingProtocolList::const_iterator rprotoIter =
m_routingProtocols.begin ();
rprotoIter != m_routingProtocols.end ();
rprotoIter++)
{
if ((*rprotoIter).second->RouteInput (p, header, idev, ucb, mcb, lcb, ecb))
{
return true;
}
}
// No routing protocol has found a route.
return retVal;
}
void
Ipv4ListRouting::NotifyInterfaceUp (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
for (Ipv4RoutingProtocolList::const_iterator rprotoIter =
m_routingProtocols.begin ();
rprotoIter != m_routingProtocols.end ();
rprotoIter++)
{
(*rprotoIter).second->NotifyInterfaceUp (interface);
}
}
void
Ipv4ListRouting::NotifyInterfaceDown (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
for (Ipv4RoutingProtocolList::const_iterator rprotoIter =
m_routingProtocols.begin ();
rprotoIter != m_routingProtocols.end ();
rprotoIter++)
{
(*rprotoIter).second->NotifyInterfaceDown (interface);
}
}
void
Ipv4ListRouting::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION(this << interface << address);
for (Ipv4RoutingProtocolList::const_iterator rprotoIter =
m_routingProtocols.begin ();
rprotoIter != m_routingProtocols.end ();
rprotoIter++)
{
(*rprotoIter).second->NotifyAddAddress (interface, address);
}
}
void
Ipv4ListRouting::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION(this << interface << address);
for (Ipv4RoutingProtocolList::const_iterator rprotoIter =
m_routingProtocols.begin ();
rprotoIter != m_routingProtocols.end ();
rprotoIter++)
{
(*rprotoIter).second->NotifyRemoveAddress (interface, address);
}
}
void
Ipv4ListRouting::SetIpv4 (Ptr<Ipv4> ipv4)
{
NS_LOG_FUNCTION(this << ipv4);
NS_ASSERT (m_ipv4 == 0);
for (Ipv4RoutingProtocolList::const_iterator rprotoIter =
m_routingProtocols.begin ();
rprotoIter != m_routingProtocols.end ();
rprotoIter++)
{
(*rprotoIter).second->SetIpv4 (ipv4);
}
m_ipv4 = ipv4;
}
void
Ipv4ListRouting::AddRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol, int16_t priority)
{
NS_LOG_FUNCTION (this << routingProtocol->GetInstanceTypeId () << priority);
m_routingProtocols.push_back (std::make_pair (priority, routingProtocol));
m_routingProtocols.sort ( Compare );
if (m_ipv4 != 0)
{
routingProtocol->SetIpv4 (m_ipv4);
}
}
uint32_t
Ipv4ListRouting::GetNRoutingProtocols (void) const
{
NS_LOG_FUNCTION (this);
return m_routingProtocols.size ();
}
Ptr<Ipv4RoutingProtocol>
Ipv4ListRouting::GetRoutingProtocol (uint32_t index, int16_t& priority) const
{
NS_LOG_FUNCTION (index);
if (index > m_routingProtocols.size ())
{
NS_FATAL_ERROR ("Ipv4ListRouting::GetRoutingProtocol(): index " << index << " out of range");
}
uint32_t i = 0;
for (Ipv4RoutingProtocolList::const_iterator rprotoIter = m_routingProtocols.begin ();
rprotoIter != m_routingProtocols.end (); rprotoIter++, i++)
{
if (i == index)
{
priority = (*rprotoIter).first;
return (*rprotoIter).second;
}
}
return 0;
}
Ptr<Ipv4StaticRouting>
Ipv4ListRouting::GetStaticRouting (void) const
{
NS_LOG_FUNCTION (this);
Ipv4StaticRouting* srp;
for (Ipv4RoutingProtocolList::const_iterator rprotoIter = m_routingProtocols.begin ();
rprotoIter != m_routingProtocols.end (); rprotoIter++)
{
NS_LOG_LOGIC ("Searching for static routing");
srp = dynamic_cast<Ipv4StaticRouting*> (PeekPointer((*rprotoIter).second));
if (srp)
{
NS_LOG_LOGIC ("Found static routing");
return Ptr<Ipv4StaticRouting> (srp);
}
}
NS_LOG_LOGIC ("Static routing not found");
return 0;
}
bool
Ipv4ListRouting::Compare (const Ipv4RoutingProtocolEntry& a, const Ipv4RoutingProtocolEntry& b)
{
return a.first > b.first;
}
} // namespace ns3
#ifdef RUN_SELF_TESTS
#include "ns3/test.h"
#include "ipv4-list-routing.h"
#include "ns3/ipv4-routing-protocol.h"
namespace ns3 {
class Ipv4ARouting : public Ipv4RoutingProtocol {
public:
Ptr<Ipv4Route> RouteOutput (const Ipv4Header &header, uint32_t oif, Socket::SocketErrno &sockerr) { return 0;}
bool RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,
UnicastForwardCallback ucb, MulticastForwardCallback mcb,
LocalDeliverCallback lcb, ErrorCallback ecb) {return false;}
void NotifyInterfaceUp (uint32_t interface) {}
void NotifyInterfaceDown (uint32_t interface) {}
void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {}
void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {}
void SetIpv4 (Ptr<Ipv4> ipv4) {}
};
class Ipv4BRouting : public Ipv4RoutingProtocol {
public:
Ptr<Ipv4Route> RouteOutput (const Ipv4Header &header, uint32_t oif, Socket::SocketErrno &sockerr) { return 0;}
bool RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,
UnicastForwardCallback ucb, MulticastForwardCallback mcb,
LocalDeliverCallback lcb, ErrorCallback ecb) {return false;}
void NotifyInterfaceUp (uint32_t interface) {}
void NotifyInterfaceDown (uint32_t interface) {}
void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {}
void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {}
void SetIpv4 (Ptr<Ipv4> ipv4) {}
};
class Ipv4ListRoutingTest: public Test {
public:
virtual bool RunTests (void);
Ipv4ListRoutingTest ();
};
Ipv4ListRoutingTest::Ipv4ListRoutingTest ()
: Test ("Ipv4ListRouting") {}
bool
Ipv4ListRoutingTest::RunTests (void)
{
bool result = true;
Ptr<Ipv4ListRouting> lr = CreateObject<Ipv4ListRouting> ();
Ptr<Ipv4RoutingProtocol> aRouting = CreateObject<Ipv4ARouting> ();
Ptr<Ipv4RoutingProtocol> bRouting = CreateObject<Ipv4BRouting> ();
// The Ipv4ARouting should be added with higher priority (larger integer
// value) and will be fetched first below
lr->AddRoutingProtocol (aRouting, 10);
lr->AddRoutingProtocol (bRouting, 5);
int16_t first = 3;
int16_t second = 3;
uint32_t num = lr->GetNRoutingProtocols ();
NS_TEST_ASSERT_EQUAL (num, 2);
Ptr<Ipv4RoutingProtocol> firstRp = lr->GetRoutingProtocol (0, first);
NS_TEST_ASSERT_EQUAL (10, first);
NS_TEST_ASSERT_EQUAL (firstRp, aRouting);
Ptr<Ipv4RoutingProtocol> secondRp = lr->GetRoutingProtocol (1, second);
NS_TEST_ASSERT_EQUAL (5, second);
NS_TEST_ASSERT_EQUAL (secondRp, bRouting);
// Test negative values
lr = CreateObject<Ipv4ListRouting> ();
// The Ipv4BRouting should be added with higher priority (larger integer value)
lr->AddRoutingProtocol (aRouting, -10);
lr->AddRoutingProtocol (bRouting, -5);
num = lr->GetNRoutingProtocols ();
NS_TEST_ASSERT_EQUAL (num, 2);
firstRp = lr->GetRoutingProtocol (0, first);
NS_TEST_ASSERT_EQUAL (-5, first);
NS_TEST_ASSERT_EQUAL (firstRp, bRouting);
return result;
}
static Ipv4ListRoutingTest gIpv4ListRoutingTest;
} // namespace ns3
#endif /* RUN_SELF_TESTS */
-102
View File
@@ -1,102 +0,0 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2009 University of Washington
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef IPV4_LIST_ROUTING_H
#define IPV4_LIST_ROUTING_H
#include <list>
#include "ns3/ipv4-routing-protocol.h"
namespace ns3 {
class Ipv4StaticRouting;
/**
* \ingroup ipv4Routing
*
* This class is a specialization of Ipv4RoutingProtocol that allows
* other instances of Ipv4RoutingProtocol to be inserted in a
* prioritized list. Routing protocols in the list are consulted one
* by one, from highest to lowest priority, until a routing protocol
* is found that will take the packet (this corresponds to a non-zero
* return value to RouteOutput, or a return value of true to RouteInput).
* The order by which routing protocols with the same priority value
* are consulted is undefined.
*
*/
class Ipv4ListRouting : public Ipv4RoutingProtocol
{
public:
static TypeId GetTypeId (void);
Ipv4ListRouting ();
virtual ~Ipv4ListRouting ();
/**
* \brief Register a new routing protocol to be used in this IPv4 stack
*
* \param routingProtocol new routing protocol implementation object
* \param priority priority to give to this routing protocol.
* Values may range between -32768 and +32767.
*/
virtual void AddRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol, int16_t priority);
/**
* \return number of routing protocols in the list
*/
virtual uint32_t GetNRoutingProtocols (void) const;
/**
* Return pointer to routing protocol stored at index, with the
* first protocol (index 0) the highest priority, the next one (index 1)
* the second highest priority, and so on. The priority parameter is an
* output parameter and it returns the integer priority of the protocol.
*
* \return pointer to routing protocol indexed by
* \param index index of protocol to return
* \param priority output parameter, set to the priority of the protocol
being returned
*/
virtual Ptr<Ipv4RoutingProtocol> GetRoutingProtocol (uint32_t index, int16_t& priority) const;
// Below are from Ipv4RoutingProtocol
virtual Ptr<Ipv4Route> RouteOutput (const Ipv4Header &header, uint32_t oif, Socket::SocketErrno &sockerr);
virtual bool RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,
UnicastForwardCallback ucb, MulticastForwardCallback mcb,
LocalDeliverCallback lcb, ErrorCallback ecb);
virtual void NotifyInterfaceUp (uint32_t interface);
virtual void NotifyInterfaceDown (uint32_t interface);
virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address);
virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address);
virtual void SetIpv4 (Ptr<Ipv4> ipv4);
virtual Ptr<Ipv4StaticRouting> GetStaticRouting (void) const;
protected:
void DoDispose (void);
private:
typedef std::pair<int16_t, Ptr<Ipv4RoutingProtocol> > Ipv4RoutingProtocolEntry;
typedef std::list<Ipv4RoutingProtocolEntry> Ipv4RoutingProtocolList;
Ipv4RoutingProtocolList m_routingProtocols;
static bool Compare (const Ipv4RoutingProtocolEntry& a, const Ipv4RoutingProtocolEntry& b);
Ptr<Ipv4> m_ipv4;
};
} //namespace ns3
#endif /* IPV4_LIST_ROUTING_H */
@@ -1,668 +0,0 @@
// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*-
//
// Copyright (c) 2006 Georgia Tech Research Corporation
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Author: George F. Riley<riley@ece.gatech.edu>
// Gustavo Carneiro <gjc@inescporto.pt>
#include "ns3/log.h"
#include "ns3/packet.h"
#include "ns3/node.h"
#include "ns3/ipv4-route.h"
#include "ipv4-static-routing-impl.h"
#include "ns3/ipv4-routing-table-entry.h"
NS_LOG_COMPONENT_DEFINE ("Ipv4StaticRoutingImpl");
namespace ns3 {
NS_OBJECT_ENSURE_REGISTERED (Ipv4StaticRoutingImpl);
TypeId
Ipv4StaticRoutingImpl::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::Ipv4StaticRoutingImpl")
.SetParent<Ipv4StaticRouting> ()
.AddConstructor<Ipv4StaticRoutingImpl> ()
;
return tid;
}
Ipv4StaticRoutingImpl::Ipv4StaticRoutingImpl ()
: m_defaultRoute (0), m_ipv4 (0)
{
NS_LOG_FUNCTION_NOARGS ();
}
void
Ipv4StaticRoutingImpl::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
Ipv4StaticRoutingImpl::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
Ipv4StaticRoutingImpl::AddNetworkRouteTo (Ipv4Address network,
Ipv4Mask networkMask,
Ipv4Address nextHop,
uint32_t interface)
{
NS_LOG_FUNCTION_NOARGS ();
Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry ();
*route = Ipv4RoutingTableEntry::CreateNetworkRouteTo (network,
networkMask,
nextHop,
interface);
m_networkRoutes.push_back (route);
}
void
Ipv4StaticRoutingImpl::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
Ipv4StaticRoutingImpl::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
Ipv4StaticRoutingImpl::AddMulticastRoute(Ipv4Address origin,
Ipv4Address group,
uint32_t inputInterface,
std::vector<uint32_t> outputInterfaces)
{
NS_LOG_FUNCTION_NOARGS ();
Ipv4MulticastRoutingTableEntry *route = new Ipv4MulticastRoutingTableEntry ();
*route = Ipv4MulticastRoutingTableEntry::CreateMulticastRoute (origin, group,
inputInterface, outputInterfaces);
m_multicastRoutes.push_back (route);
}
// default multicast routes are stored as a network route
// these routes are _not_ consulted in the forwarding process-- only
// for originating packets
void
Ipv4StaticRoutingImpl::SetDefaultMulticastRoute(uint32_t outputInterface)
{
NS_LOG_FUNCTION_NOARGS ();
Ipv4RoutingTableEntry *route = new Ipv4RoutingTableEntry ();
Ipv4Address network = Ipv4Address ("224.0.0.0");
Ipv4Mask networkMask = Ipv4Mask ("240.0.0.0");
*route = Ipv4RoutingTableEntry::CreateNetworkRouteTo (network,
networkMask,
outputInterface);
m_networkRoutes.push_back (route);
}
uint32_t
Ipv4StaticRoutingImpl::GetNMulticastRoutes (void) const
{
NS_LOG_FUNCTION_NOARGS ();
return m_multicastRoutes.size ();
}
Ipv4MulticastRoutingTableEntry
Ipv4StaticRoutingImpl::GetMulticastRoute (uint32_t index) const
{
NS_LOG_FUNCTION_NOARGS ();
NS_ASSERT_MSG(index < m_multicastRoutes.size (),
"Ipv4StaticRoutingImpl::GetMulticastRoute (): Index out of range");
if (index < m_multicastRoutes.size ())
{
uint32_t tmp = 0;
for (MulticastRoutesCI i = m_multicastRoutes.begin ();
i != m_multicastRoutes.end ();
i++)
{
if (tmp == index)
{
return *i;
}
tmp++;
}
}
return 0;
}
bool
Ipv4StaticRoutingImpl::RemoveMulticastRoute(Ipv4Address origin,
Ipv4Address group,
uint32_t inputInterface)
{
NS_LOG_FUNCTION_NOARGS ();
for (MulticastRoutesI i = m_multicastRoutes.begin ();
i != m_multicastRoutes.end ();
i++)
{
Ipv4MulticastRoutingTableEntry *route = *i;
if (origin == route->GetOrigin () &&
group == route->GetGroup () &&
inputInterface == route->GetInputInterface ())
{
delete *i;
m_multicastRoutes.erase (i);
return true;
}
}
return false;
}
void
Ipv4StaticRoutingImpl::RemoveMulticastRoute(uint32_t index)
{
NS_LOG_FUNCTION_NOARGS ();
uint32_t tmp = 0;
for (MulticastRoutesI i = m_multicastRoutes.begin ();
i != m_multicastRoutes.end ();
i++)
{
if (tmp == index)
{
delete *i;
m_multicastRoutes.erase (i);
return;
}
tmp++;
}
}
Ptr<Ipv4Route>
Ipv4StaticRoutingImpl::LookupStatic (Ipv4Address dest)
{
NS_LOG_FUNCTION_NOARGS ();
Ptr<Ipv4Route> 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<Ipv4Route> ();
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<Ipv4Route> ();
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<Ipv4Route> ();
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<Ipv4MulticastRoute>
Ipv4StaticRoutingImpl::LookupStatic (
Ipv4Address origin,
Ipv4Address group,
uint32_t interface)
{
NS_LOG_FUNCTION_NOARGS ();
Ptr<Ipv4MulticastRoute> mrtentry = 0;
for (MulticastRoutesI i = m_multicastRoutes.begin ();
i != m_multicastRoutes.end ();
i++)
{
Ipv4MulticastRoutingTableEntry *route = *i;
//
// We've been passed an origin address, a multicast group address and an
// interface index. We have to decide if the current route in the list is
// a match.
//
// The first case is the restrictive case where the origin, group and index
// matches.
//
if (origin == route->GetOrigin () && group == route->GetGroup ())
{
// Skipping this case (SSM) for now
NS_LOG_LOGIC ("Found multicast source specific route" << *i);
}
if (group == route->GetGroup ())
{
if (interface == Ipv4::IF_ANY ||
interface == route->GetInputInterface ())
{
NS_LOG_LOGIC ("Found multicast route" << *i);
mrtentry = Create<Ipv4MulticastRoute> ();
mrtentry->SetGroup (route->GetGroup ());
mrtentry->SetOrigin (route->GetOrigin ());
mrtentry->SetParent (route->GetInputInterface ());
for (uint32_t j = 0; j < route->GetNOutputInterfaces (); j++)
{
if (route->GetOutputInterface (j))
{
NS_LOG_LOGIC ("Setting output interface index " << route->GetOutputInterface (j));
mrtentry->SetOutputTtl (route->GetOutputInterface (j), Ipv4MulticastRoute::MAX_TTL - 1);
}
}
return mrtentry;
}
}
}
return mrtentry;
}
uint32_t
Ipv4StaticRoutingImpl::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
Ipv4StaticRoutingImpl::GetDefaultRoute ()
{
NS_LOG_FUNCTION_NOARGS ();
if (m_defaultRoute != 0)
{
return *m_defaultRoute;
}
else
{
return Ipv4RoutingTableEntry ();
}
}
Ipv4RoutingTableEntry
Ipv4StaticRoutingImpl::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 ();
j++)
{
if (tmp == index)
{
return *j;
}
tmp++;
}
NS_ASSERT (false);
// quiet compiler.
return 0;
}
void
Ipv4StaticRoutingImpl::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 ();
j++)
{
if (tmp == index)
{
delete *j;
m_networkRoutes.erase (j);
return;
}
tmp++;
}
NS_ASSERT (false);
}
Ptr<Ipv4Route>
Ipv4StaticRoutingImpl::RouteOutput (const Ipv4Header &header, uint32_t oif, Socket::SocketErrno &sockerr)
{
NS_LOG_FUNCTION (this << header << oif);
Ipv4Address destination = header.GetDestination ();
Ptr<Ipv4Route> rtentry = 0;
// Multicast goes here
if (destination.IsMulticast ())
{
// Note: Multicast routes for outbound packets are stored in the
// normal unicast table. An implication of this is that it is not
// possible to source multicast datagrams on multiple interfaces.
// This is a well-known property of sockets implementation on
// many Unix variants.
// So, we just log it and fall through to LookupStatic ()
NS_LOG_LOGIC ("RouteOutput()::Multicast destination");
}
rtentry = LookupStatic (destination);
if (rtentry)
{
sockerr = Socket::ERROR_NOTERROR;
}
else
{
sockerr = Socket::ERROR_NOROUTETOHOST;
}
return rtentry;
}
// XXX this method not robust enough to work outside of ListRouting context
// because it will not perform local delivery
bool
Ipv4StaticRoutingImpl::RouteInput (Ptr<const Packet> p, const Ipv4Header &ipHeader, Ptr<const NetDevice> idev,
UnicastForwardCallback ucb, MulticastForwardCallback mcb,
LocalDeliverCallback lcb, ErrorCallback ecb)
{
NS_LOG_FUNCTION (this << p << ipHeader << ipHeader.GetSource () << ipHeader.GetDestination () << idev);
if (ipHeader.GetDestination ().IsMulticast ())
{
NS_LOG_LOGIC ("Multicast destination");
Ptr<Ipv4MulticastRoute> mrtentry = LookupStatic(ipHeader.GetSource (),
ipHeader.GetDestination (), m_ipv4->GetInterfaceForDevice (idev));
if (mrtentry)
{
NS_LOG_LOGIC ("Multicast route found");
mcb (mrtentry, p, ipHeader); // multicast forwarding callback
return true;
}
else
{
NS_LOG_LOGIC ("Multicast route not found");
return false; // Let other routing protocols try to handle this
}
}
//
// This is a unicast packet. Check to see if we have a route for it.
//
NS_LOG_LOGIC ("Unicast destination");
Ptr<Ipv4Route> rtentry = LookupStatic (ipHeader.GetDestination ());
if (rtentry != 0)
{
NS_LOG_LOGIC ("Found unicast destination- calling unicast callback");
ucb (rtentry, p, ipHeader); // unicast forwarding callback
return true;
}
else
{
NS_LOG_LOGIC ("Did not find unicast destination- returning false");
return false; // Let other routing protocols try to handle this
}
}
Ipv4StaticRoutingImpl::~Ipv4StaticRoutingImpl ()
{
NS_LOG_FUNCTION_NOARGS ();
}
void
Ipv4StaticRoutingImpl::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 ();
i = m_multicastRoutes.erase (i))
{
delete (*i);
}
m_ipv4 = 0;
Ipv4RoutingProtocol::DoDispose ();
}
void
Ipv4StaticRoutingImpl::NotifyInterfaceUp (uint32_t i)
{
// If interface address and network mask have been set, add a route
// to the network of the interface (like e.g. ifconfig does on a
// Linux box)
for (uint32_t j = 0; j < m_ipv4->GetNAddresses (i); j++)
{
if (m_ipv4->GetAddress (i,j).GetLocal () != Ipv4Address () &&
m_ipv4->GetAddress (i,j).GetMask () != Ipv4Mask ())
{
AddNetworkRouteTo (m_ipv4->GetAddress (i,j).GetLocal ().CombineMask (m_ipv4->GetAddress (i,j).GetMask ()),
m_ipv4->GetAddress (i,j).GetMask (), i);
}
}
}
void
Ipv4StaticRoutingImpl::NotifyInterfaceDown (uint32_t i)
{
// Remove all static routes that are going through this interface
for (uint32_t j = 0; j < GetNRoutes (); j++)
{
Ipv4RoutingTableEntry route = GetRoute (j);
if (route.GetInterface () == i)
{
RemoveRoute (j);
}
}
}
void
Ipv4StaticRoutingImpl::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION (this << interface);
if (!m_ipv4->IsUp (interface))
{
return;
}
Ipv4Address networkAddress = address.GetLocal ().CombineMask (address.GetMask ());
Ipv4Mask networkMask = address.GetMask ();
if (address.GetLocal () != Ipv4Address () &&
address.GetMask () != Ipv4Mask ())
{
AddNetworkRouteTo (networkAddress,
networkMask, interface);
}
}
void
Ipv4StaticRoutingImpl::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
if (!m_ipv4->IsUp (interface))
{
return;
}
Ipv4Address networkAddress = address.GetLocal ().CombineMask (address.GetMask ());
Ipv4Mask networkMask = address.GetMask ();
// Remove all static routes that are going through this interface
// which reference this network
for (uint32_t j = 0; j < GetNRoutes (); j++)
{
Ipv4RoutingTableEntry route = GetRoute (j);
if (route.GetInterface () == interface &&
route.IsNetwork () &&
route.GetDestNetwork () == networkAddress &&
route.GetDestNetworkMask () == networkMask)
{
RemoveRoute (j);
}
}
}
void
Ipv4StaticRoutingImpl::SetIpv4 (Ptr<Ipv4> ipv4)
{
NS_LOG_FUNCTION (this << ipv4);
NS_ASSERT (m_ipv4 == 0 && ipv4 != 0);
m_ipv4 = ipv4;
for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
{
if (m_ipv4->IsUp (i))
{
NotifyInterfaceUp (i);
}
else
{
NotifyInterfaceDown (i);
}
}
}
Ipv4Address
Ipv4StaticRoutingImpl::SourceAddressSelection (uint32_t interfaceIdx, Ipv4Address dest)
{
if (m_ipv4->GetNAddresses (interfaceIdx) == 1) // common case
{
return m_ipv4->GetAddress (interfaceIdx, 0).GetLocal ();
}
// no way to determine the scope of the destination, so adopt the
// following rule: pick the first available address (index 0) unless
// a subsequent address is on link (in which case, pick the primary
// address if there are multiple)
Ipv4Address candidate = m_ipv4->GetAddress (interfaceIdx, 0).GetLocal ();
for (uint32_t i = 0; i < m_ipv4->GetNAddresses (interfaceIdx); i++)
{
Ipv4InterfaceAddress test = m_ipv4->GetAddress (interfaceIdx, i);
if (test.GetLocal ().CombineMask (test.GetMask ()) == dest.CombineMask (test.GetMask ()))
{
if (test.IsSecondary () == false)
{
return test.GetLocal ();
}
}
}
return candidate;
}
}//namespace ns3
+4 -4
View File
@@ -37,8 +37,8 @@
#include "icmpv4-l4-protocol.h"
#include "udp-l4-protocol.h"
#include "tcp-l4-protocol.h"
#include "ipv4-static-routing-impl.h"
#include "ipv4-list-routing.h"
#include "ns3/ipv4-static-routing.h"
#include "ns3/ipv4-list-routing.h"
#include "ns3/node.h"
#include "ns3/inet-socket-address.h"
@@ -58,8 +58,8 @@ AddInternetStack (Ptr<Node> node)
//Routing for Ipv4
Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
ipv4->SetRoutingProtocol (ipv4Routing);
Ptr<Ipv4StaticRoutingImpl> ipv4staticRoutingImpl = CreateObject<Ipv4StaticRoutingImpl> ();
ipv4Routing->AddRoutingProtocol (ipv4staticRoutingImpl, 0);
Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
node->AggregateObject(ipv4);
//ICMP
Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
+4 -4
View File
@@ -41,8 +41,8 @@
#include "icmpv4-l4-protocol.h"
#include "udp-l4-protocol.h"
#include "tcp-l4-protocol.h"
#include "ipv4-list-routing.h"
#include "ipv4-static-routing-impl.h"
#include "ns3/ipv4-list-routing.h"
#include "ns3/ipv4-static-routing.h"
#include <string>
#include <limits>
@@ -59,8 +59,8 @@ AddInternetStack (Ptr<Node> node)
//Routing for Ipv4
Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
ipv4->SetRoutingProtocol (ipv4Routing);
Ptr<Ipv4StaticRoutingImpl> ipv4staticRoutingImpl = CreateObject<Ipv4StaticRoutingImpl> ();
ipv4Routing->AddRoutingProtocol (ipv4staticRoutingImpl, 0);
Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
node->AggregateObject(ipv4);
//ICMP
Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
-4
View File
@@ -81,8 +81,6 @@ def build(bld):
'ipv4-checksum.cc',
'ipv4-interface.cc',
'ipv4-l3-protocol.cc',
'ipv4-static-routing-impl.cc',
'ipv4-list-routing.cc',
'ipv4-end-point.cc',
'udp-l4-protocol.cc',
'tcp-l4-protocol.cc',
@@ -110,8 +108,6 @@ def build(bld):
'udp-header.h',
'tcp-header.h',
'sequence-number.h',
'ipv4-list-routing.h',
'ipv4-static-routing-impl.h',
'icmpv4.h',
]