Make olsr::RoutingTable public, add a method GetEntries to it, and add a method GetRoutingTable to OlsrAgent. This way it is possible to read the actual routing table that OLSR has discovered, for debugging/visualization purposes.
--- a/bindings/python/ns3_module_olsr.py Fri Nov 07 11:36:15 2008 -0800
+++ b/bindings/python/ns3_module_olsr.py Fri Nov 07 19:51:00 2008 +0000
@@ -63,8 +63,13 @@
module.add_class('Tc', outer_class=root_module['ns3::olsr::MessageHeader'])
## olsr-header.h: ns3::olsr::PacketHeader [class]
module.add_class('PacketHeader', parent=root_module['ns3::Header'])
+ ## olsr-routing-table.h: ns3::olsr::RoutingTable [class]
+ module.add_class('RoutingTable', parent=root_module['ns3::Ipv4RoutingProtocol'])
+ ## olsr-routing-table.h: ns3::olsr::RoutingTableEntry [struct]
+ module.add_class('RoutingTableEntry')
module.add_container('std::vector< ns3::olsr::MessageHeader::Hello::LinkMessage >', 'ns3::olsr::MessageHeader::Hello::LinkMessage', container_type='vector')
module.add_container('std::vector< ns3::olsr::MessageHeader::Hna::Association >', 'ns3::olsr::MessageHeader::Hna::Association', container_type='vector')
+ module.add_container('std::vector< ns3::olsr::RoutingTableEntry >', 'ns3::olsr::RoutingTableEntry', container_type='vector')
def register_methods(root_module):
register_Ns3OlsrAgent_methods(root_module, root_module['ns3::olsr::Agent'])
@@ -76,6 +81,8 @@
register_Ns3OlsrMessageHeaderMid_methods(root_module, root_module['ns3::olsr::MessageHeader::Mid'])
register_Ns3OlsrMessageHeaderTc_methods(root_module, root_module['ns3::olsr::MessageHeader::Tc'])
register_Ns3OlsrPacketHeader_methods(root_module, root_module['ns3::olsr::PacketHeader'])
+ register_Ns3OlsrRoutingTable_methods(root_module, root_module['ns3::olsr::RoutingTable'])
+ register_Ns3OlsrRoutingTableEntry_methods(root_module, root_module['ns3::olsr::RoutingTableEntry'])
return
def register_Ns3OlsrAgent_methods(root_module, cls):
@@ -103,6 +110,11 @@
'void',
[],
is_pure_virtual=True, is_virtual=True)
+ ## olsr-agent.h: ns3::Ptr<const ns3::olsr::RoutingTable> ns3::olsr::Agent::GetRoutingTable() const [member function]
+ cls.add_method('GetRoutingTable',
+ 'ns3::Ptr< ns3::olsr::RoutingTable const >',
+ [],
+ is_pure_virtual=True, is_const=True, is_virtual=True)
return
def register_Ns3OlsrMessageHeader_methods(root_module, cls):
@@ -438,6 +450,87 @@
[param('uint16_t', 'seqnum')])
return
+def register_Ns3OlsrRoutingTable_methods(root_module, cls):
+ ## olsr-routing-table.h: ns3::olsr::RoutingTable::RoutingTable(ns3::olsr::RoutingTable const & arg0) [copy constructor]
+ cls.add_constructor([param('ns3::olsr::RoutingTable const &', 'arg0')])
+ ## olsr-routing-table.h: ns3::olsr::RoutingTable::RoutingTable() [constructor]
+ cls.add_constructor([])
+ ## olsr-routing-table.h: void ns3::olsr::RoutingTable::SetIpv4(ns3::Ptr<ns3::Ipv4> ipv4) [member function]
+ cls.add_method('SetIpv4',
+ 'void',
+ [param('ns3::Ptr< ns3::Ipv4 >', 'ipv4')])
+ ## olsr-routing-table.h: void ns3::olsr::RoutingTable::SetMainAddress(ns3::Ipv4Address mainAddress) [member function]
+ cls.add_method('SetMainAddress',
+ 'void',
+ [param('ns3::Ipv4Address', 'mainAddress')])
+ ## olsr-routing-table.h: void ns3::olsr::RoutingTable::Clear() [member function]
+ cls.add_method('Clear',
+ 'void',
+ [])
+ ## olsr-routing-table.h: uint32_t ns3::olsr::RoutingTable::GetSize() const [member function]
+ cls.add_method('GetSize',
+ 'uint32_t',
+ [],
+ is_const=True)
+ ## olsr-routing-table.h: std::vector<ns3::olsr::RoutingTableEntry,std::allocator<ns3::olsr::RoutingTableEntry> > ns3::olsr::RoutingTable::GetEntries() const [member function]
+ cls.add_method('GetEntries',
+ 'std::vector< ns3::olsr::RoutingTableEntry >',
+ [],
+ is_const=True)
+ ## olsr-routing-table.h: void ns3::olsr::RoutingTable::RemoveEntry(ns3::Ipv4Address const & dest) [member function]
+ cls.add_method('RemoveEntry',
+ 'void',
+ [param('ns3::Ipv4Address const &', 'dest')])
+ ## olsr-routing-table.h: void ns3::olsr::RoutingTable::AddEntry(ns3::Ipv4Address const & dest, ns3::Ipv4Address const & next, uint32_t interface, uint32_t distance) [member function]
+ cls.add_method('AddEntry',
+ 'void',
+ [param('ns3::Ipv4Address const &', 'dest'), param('ns3::Ipv4Address const &', 'next'), param('uint32_t', 'interface'), param('uint32_t', 'distance')])
+ ## olsr-routing-table.h: void ns3::olsr::RoutingTable::AddEntry(ns3::Ipv4Address const & dest, ns3::Ipv4Address const & next, ns3::Ipv4Address const & interfaceAddress, uint32_t distance) [member function]
+ cls.add_method('AddEntry',
+ 'void',
+ [param('ns3::Ipv4Address const &', 'dest'), param('ns3::Ipv4Address const &', 'next'), param('ns3::Ipv4Address const &', 'interfaceAddress'), param('uint32_t', 'distance')])
+ ## olsr-routing-table.h: bool ns3::olsr::RoutingTable::Lookup(ns3::Ipv4Address const & dest, ns3::olsr::RoutingTableEntry & outEntry) const [member function]
+ cls.add_method('Lookup',
+ 'bool',
+ [param('ns3::Ipv4Address const &', 'dest'), param('ns3::olsr::RoutingTableEntry &', 'outEntry')],
+ is_const=True)
+ ## olsr-routing-table.h: bool ns3::olsr::RoutingTable::FindSendEntry(ns3::olsr::RoutingTableEntry const & entry, ns3::olsr::RoutingTableEntry & outEntry) const [member function]
+ cls.add_method('FindSendEntry',
+ 'bool',
+ [param('ns3::olsr::RoutingTableEntry const &', 'entry'), param('ns3::olsr::RoutingTableEntry &', 'outEntry')],
+ is_const=True)
+ ## olsr-routing-table.h: bool ns3::olsr::RoutingTable::RequestRoute(uint32_t ifIndex, ns3::Ipv4Header const & ipHeader, ns3::Ptr<ns3::Packet> packet, ns3::Callback<void,bool,const ns3::Ipv4Route&,ns3::Ptr<ns3::Packet>,const ns3::Ipv4Header&,ns3::empty,ns3::empty> routeReply) [member function]
+ cls.add_method('RequestRoute',
+ 'bool',
+ [param('uint32_t', 'ifIndex'), param('ns3::Ipv4Header const &', 'ipHeader'), param('ns3::Ptr< ns3::Packet >', 'packet'), param('ns3::Callback< void, bool, ns3::Ipv4Route const &, ns3::Ptr< ns3::Packet >, ns3::Ipv4Header const &, ns3::empty, ns3::empty >', 'routeReply')],
+ is_virtual=True)
+ ## olsr-routing-table.h: bool ns3::olsr::RoutingTable::RequestIfIndex(ns3::Ipv4Address destination, uint32_t & ifIndex) [member function]
+ cls.add_method('RequestIfIndex',
+ 'bool',
+ [param('ns3::Ipv4Address', 'destination'), param('uint32_t &', 'ifIndex')],
+ is_virtual=True)
+ ## olsr-routing-table.h: void ns3::olsr::RoutingTable::DoDispose() [member function]
+ cls.add_method('DoDispose',
+ 'void',
+ [],
+ visibility='private', is_virtual=True)
+ return
+
+def register_Ns3OlsrRoutingTableEntry_methods(root_module, cls):
+ ## olsr-routing-table.h: ns3::olsr::RoutingTableEntry::destAddr [variable]
+ cls.add_instance_attribute('destAddr', 'ns3::Ipv4Address', is_const=False)
+ ## olsr-routing-table.h: ns3::olsr::RoutingTableEntry::nextAddr [variable]
+ cls.add_instance_attribute('nextAddr', 'ns3::Ipv4Address', is_const=False)
+ ## olsr-routing-table.h: ns3::olsr::RoutingTableEntry::interface [variable]
+ cls.add_instance_attribute('interface', 'uint32_t', is_const=False)
+ ## olsr-routing-table.h: ns3::olsr::RoutingTableEntry::distance [variable]
+ cls.add_instance_attribute('distance', 'uint32_t', is_const=False)
+ ## olsr-routing-table.h: ns3::olsr::RoutingTableEntry::RoutingTableEntry(ns3::olsr::RoutingTableEntry const & arg0) [copy constructor]
+ cls.add_constructor([param('ns3::olsr::RoutingTableEntry const &', 'arg0')])
+ ## olsr-routing-table.h: ns3::olsr::RoutingTableEntry::RoutingTableEntry() [constructor]
+ cls.add_constructor([])
+ return
+
def register_functions(root_module):
module = root_module
register_functions_ns3_internal(module.get_submodule('internal'), root_module)
--- a/src/routing/olsr/olsr-agent-impl.cc Fri Nov 07 11:36:15 2008 -0800
+++ b/src/routing/olsr/olsr-agent-impl.cc Fri Nov 07 19:51:00 2008 +0000
@@ -2435,7 +2435,11 @@
}
}
-
+Ptr<const olsr::RoutingTable>
+AgentImpl::GetRoutingTable () const
+{
+ return m_routingTable;
+}
}} // namespace olsr, ns3
--- a/src/routing/olsr/olsr-agent-impl.h Fri Nov 07 11:36:15 2008 -0800
+++ b/src/routing/olsr/olsr-agent-impl.h Fri Nov 07 19:51:00 2008 +0000
@@ -30,7 +30,7 @@
#include "olsr-header.h"
#include "olsr-state.h"
-#include "routing-table.h"
+#include "olsr-routing-table.h"
#include "repositories.h"
#include "ns3/object.h"
@@ -58,6 +58,7 @@
virtual void Start ();
virtual void SetMainInterface (uint32_t interface);
+ virtual Ptr<const olsr::RoutingTable> GetRoutingTable () const;
private:
EventGarbageCollector m_events;
--- a/src/routing/olsr/olsr-agent.h Fri Nov 07 11:36:15 2008 -0800
+++ b/src/routing/olsr/olsr-agent.h Fri Nov 07 19:51:00 2008 +0000
@@ -22,6 +22,7 @@
#define OLSR_AGENT_H
#include "ns3/node.h"
+#include "ns3/olsr-routing-table.h"
namespace ns3 {
namespace olsr {
@@ -72,6 +73,8 @@
* neighbors, as well start listening to messages from neighbors.
*/
virtual void Start () = 0;
+
+ virtual Ptr<const olsr::RoutingTable> GetRoutingTable () const = 0;
};
}} // namespace olsr, ns3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/routing/olsr/olsr-routing-table.cc Fri Nov 07 19:51:00 2008 +0000
@@ -0,0 +1,332 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2004 Francisco J. Ros
+ * Copyright (c) 2007 INESC Porto
+ *
+ * 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
+ *
+ * Authors: Francisco J. Ros <fjrm@dif.um.es>
+ * Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
+ */
+
+///
+/// \file OLSR_rtable.cc
+/// \brief Implementation of our routing table.
+///
+
+#include "olsr-routing-table.h"
+#include "ns3/packet.h"
+#include "ns3/ipv4-header.h"
+#include "ns3/log.h"
+
+namespace ns3 { namespace olsr {
+
+NS_LOG_COMPONENT_DEFINE ("OlsrRoutingTable");
+
+///
+/// \brief Clears the routing table and frees the memory assigned to each one of its entries.
+///
+void
+RoutingTable::Clear ()
+{
+ NS_LOG_FUNCTION_NOARGS ();
+ m_table.clear ();
+}
+
+///
+/// \brief Deletes the entry whose destination address is given.
+/// \param dest address of the destination node.
+///
+void
+RoutingTable::RemoveEntry (Ipv4Address const &dest)
+{
+ m_table.erase (dest);
+}
+
+///
+/// \brief Looks up an entry for the specified destination address.
+/// \param dest destination address.
+/// \param outEntry output parameter to hold the routing entry result, if fuond
+/// \return true if found, false if not found
+///
+bool
+RoutingTable::Lookup (Ipv4Address const &dest,
+ RoutingTableEntry &outEntry) const
+{
+ // Get the iterator at "dest" position
+ std::map<Ipv4Address, RoutingTableEntry>::const_iterator it =
+ m_table.find (dest);
+ // If there is no route to "dest", return NULL
+ if (it == m_table.end ())
+ return false;
+ outEntry = it->second;
+ return true;
+}
+
+///
+/// \brief Finds the appropiate entry which must be used in order to forward
+/// a data packet to a next hop (given a destination).
+///
+/// Imagine a routing table like this: [A,B] [B,C] [C,C]; being each pair of the
+/// form [dest addr,next-hop addr]. In this case, if this function is invoked with
+/// [A,B] then pair [C,C] is returned because C is the next hop that must be used
+/// to forward a data packet destined to A. That is, C is a neighbor of this node,
+/// but B isn't. This function finds the appropiate neighbor for forwarding a packet.
+///
+/// \param entry the routing table entry which indicates the destination node
+/// we are interested in.
+/// \return the appropiate routing table entry which indicates the next
+/// hop which must be used for forwarding a data packet, or NULL
+/// if there is no such entry.
+///
+bool
+RoutingTable::FindSendEntry (RoutingTableEntry const &entry,
+ RoutingTableEntry &outEntry) const
+{
+ outEntry = entry;
+ while (outEntry.destAddr != outEntry.nextAddr)
+ {
+ if (not Lookup(outEntry.nextAddr, outEntry))
+ return false;
+ }
+ return true;
+}
+
+
+bool
+RoutingTable::RequestRoute (uint32_t ifIndex,
+ const Ipv4Header &ipHeader,
+ Ptr<Packet> packet,
+ RouteReplyCallback routeReply)
+{
+ RoutingTableEntry entry1, entry2;
+ if (Lookup (ipHeader.GetDestination (), entry1))
+ {
+ bool foundSendEntry = FindSendEntry (entry1, entry2);
+ if (!foundSendEntry)
+ NS_FATAL_ERROR ("FindSendEntry failure");
+
+ Ipv4Route route = Ipv4Route::CreateHostRouteTo
+ (ipHeader.GetDestination (), entry2.nextAddr, entry2.interface);
+
+ NS_LOG_DEBUG ("Olsr node " << m_mainAddress
+ << ": RouteRequest for dest=" << ipHeader.GetDestination ()
+ << " --> nestHop=" << entry2.nextAddr
+ << " interface=" << entry2.interface);
+
+ routeReply (true, route, packet, ipHeader);
+ return true;
+ }
+ else
+ {
+#ifdef NS3_LOG_ENABLE
+ NS_LOG_DEBUG ("Olsr node " << m_mainAddress
+ << ": RouteRequest for dest=" << ipHeader.GetDestination ()
+ << " --> NOT FOUND; ** Dumping routing table...");
+ for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator iter = m_table.begin ();
+ iter != m_table.end (); iter++)
+ {
+ NS_LOG_DEBUG ("dest=" << iter->first << " --> next=" << iter->second.nextAddr
+ << " via interface " << iter->second.interface);
+ }
+
+ NS_LOG_DEBUG ("** Routing table dump end.");
+#endif
+ return false;
+ }
+}
+
+bool
+RoutingTable::RequestIfIndex (Ipv4Address destination,
+ uint32_t& ifIndex)
+{
+ RoutingTableEntry entry1, entry2;
+ if (Lookup (destination, entry1))
+ {
+ bool foundSendEntry = FindSendEntry (entry1, entry2);
+ if (!foundSendEntry)
+ NS_FATAL_ERROR ("FindSendEntry failure");
+ ifIndex = entry2.interface;
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+
+///
+/// \brief Adds a new entry into the routing table.
+///
+/// If an entry for the given destination existed, it is deleted and freed.
+///
+/// \param dest address of the destination node.
+/// \param next address of the next hop node.
+/// \param iface address of the local interface.
+/// \param dist distance to the destination node.
+///
+void
+RoutingTable::AddEntry (Ipv4Address const &dest,
+ Ipv4Address const &next,
+ uint32_t interface,
+ uint32_t distance)
+{
+ NS_LOG_FUNCTION (this << dest << next << interface << distance << m_mainAddress);
+
+ NS_ASSERT (distance > 0);
+
+ // Creates a new rt entry with specified values
+ RoutingTableEntry &entry = m_table[dest];
+
+ entry.destAddr = dest;
+ entry.nextAddr = next;
+ entry.interface = interface;
+ entry.distance = distance;
+}
+
+void
+RoutingTable::AddEntry (Ipv4Address const &dest,
+ Ipv4Address const &next,
+ Ipv4Address const &interfaceAddress,
+ uint32_t distance)
+{
+ NS_LOG_FUNCTION (this << dest << next << interfaceAddress << distance << m_mainAddress);
+
+ NS_ASSERT (distance > 0);
+ NS_ASSERT (m_ipv4);
+
+ RoutingTableEntry entry;
+ for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
+ {
+ if (m_ipv4->GetAddress (i) == interfaceAddress)
+ {
+ AddEntry (dest, next, i, distance);
+ return;
+ }
+ }
+ NS_ASSERT (false); // should not be reached
+ AddEntry (dest, next, 0, distance);
+}
+
+void
+RoutingTable::SetMainAddress (Ipv4Address mainAddress)
+{
+ m_mainAddress = mainAddress;
+}
+
+void
+RoutingTable::SetIpv4 (Ptr<Ipv4> ipv4)
+{
+ m_ipv4 = ipv4;
+}
+
+std::vector<RoutingTableEntry>
+RoutingTable::GetEntries () const
+{
+ std::vector<RoutingTableEntry> retval;
+ for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator iter = m_table.begin ();
+ iter != m_table.end (); iter++)
+ {
+ retval.push_back (iter->second);
+ }
+ return retval;
+}
+
+///
+/// \brief Returns the number of entries in the routing table.
+/// \return the number of entries in the routing table.
+///
+// u_int32_t
+// RoutingTable::size() {
+// return rt_.size();
+// }
+
+}}; // namespace ns3, olsr
+
+
+
+#ifdef RUN_SELF_TESTS
+
+
+#include "ns3/test.h"
+
+
+namespace ns3 { namespace olsr {
+
+class OlsrRoutingTableTest : public ns3::Test {
+private:
+public:
+ OlsrRoutingTableTest ();
+ virtual bool RunTests (void);
+
+
+};
+
+OlsrRoutingTableTest::OlsrRoutingTableTest ()
+ : ns3::Test ("OlsrRoutingTable")
+{}
+
+
+bool
+OlsrRoutingTableTest::RunTests (void)
+{
+ bool result = true;
+
+ RoutingTable table;
+
+ table.AddEntry (Ipv4Address ("1.2.3.5"),
+ Ipv4Address ("1.2.3.4"),
+ 0,
+ 1);
+
+ table.AddEntry (Ipv4Address ("1.2.3.4"),
+ Ipv4Address ("1.2.3.4"),
+ 0,
+ 1);
+
+ RoutingTableEntry entry1;
+ NS_TEST_ASSERT (table.Lookup (Ipv4Address ("1.2.3.5"), entry1));
+ NS_TEST_ASSERT_EQUAL (entry1.destAddr, Ipv4Address ("1.2.3.5"));
+ NS_TEST_ASSERT_EQUAL (entry1.nextAddr, Ipv4Address ("1.2.3.4"));
+ NS_TEST_ASSERT_EQUAL (entry1.interface, 0);
+ NS_TEST_ASSERT_EQUAL (entry1.distance, 1);
+
+ RoutingTableEntry entry2;
+ NS_TEST_ASSERT (table.Lookup (Ipv4Address ("1.2.3.4"), entry2));
+ NS_TEST_ASSERT_EQUAL (entry2.destAddr, Ipv4Address ("1.2.3.4"));
+ NS_TEST_ASSERT_EQUAL (entry2.nextAddr, Ipv4Address ("1.2.3.4"));
+ NS_TEST_ASSERT_EQUAL (entry2.interface, 0);
+ NS_TEST_ASSERT_EQUAL (entry2.distance, 1);
+
+ RoutingTableEntry sendEntry;
+ NS_TEST_ASSERT (table.FindSendEntry (entry1, sendEntry));
+ NS_TEST_ASSERT_EQUAL (sendEntry.destAddr, Ipv4Address ("1.2.3.4"));
+ NS_TEST_ASSERT_EQUAL (sendEntry.nextAddr, Ipv4Address ("1.2.3.4"));
+ NS_TEST_ASSERT_EQUAL (sendEntry.interface, 0);
+ NS_TEST_ASSERT_EQUAL (sendEntry.distance, 1);
+
+ table.RemoveEntry (Ipv4Address ("1.2.3.5"));
+ RoutingTableEntry removedEntry;
+ NS_TEST_ASSERT (not table.Lookup (Ipv4Address ("1.2.3.5"), removedEntry));
+
+ return result;
+}
+
+static OlsrRoutingTableTest gOlsrRoutingTableTest;
+
+}}; // namespace
+
+
+#endif /* RUN_SELF_TESTS */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/routing/olsr/olsr-routing-table.h Fri Nov 07 19:51:00 2008 +0000
@@ -0,0 +1,112 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2004 Francisco J. Ros
+ * Copyright (c) 2007 INESC Porto
+ *
+ * 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
+ *
+ * Authors: Francisco J. Ros <fjrm@dif.um.es>
+ * Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
+ */
+
+///
+/// \file OLSR_rtable.h
+/// \brief Header file for routing table's related stuff.
+///
+
+#ifndef __OLSR_RTABLE_H__
+#define __OLSR_RTABLE_H__
+
+#include "ns3/ipv4.h"
+#include <map>
+
+
+namespace ns3 { namespace olsr {
+
+/// An %OLSR's routing table entry.
+struct RoutingTableEntry
+{
+ Ipv4Address destAddr; ///< Address of the destination node.
+ Ipv4Address nextAddr; ///< Address of the next hop.
+ uint32_t interface; ///< Interface index
+ uint32_t distance; ///< Distance in hops to the destination.
+
+ RoutingTableEntry () : // default values
+ destAddr (), nextAddr (),
+ interface (0), distance (0) {};
+};
+
+///
+/// \brief Defines rtable_t as a map of OLSR_rt_entry, whose key is the destination address.
+///
+/// The routing table is thus defined as pairs: [dest address, entry]. Each element
+/// of the pair can be accesed via "first" and "second" members.
+///
+//typedef std::map<Ipv4Address, RoutingTableEntry> RoutingTable;
+
+///
+/// \brief This class is a representation of the OLSR's Routing Table.
+///
+class RoutingTable : public Ipv4RoutingProtocol
+{
+ std::map<Ipv4Address, RoutingTableEntry> m_table; ///< Data structure for the routing table.
+ Ptr<Ipv4> m_ipv4;
+
+ Ipv4Address m_mainAddress; // used only for printing debug messages
+
+ void DoDispose ()
+ {
+ m_ipv4 = 0;
+ Ipv4RoutingProtocol::DoDispose ();
+ }
+
+public:
+
+ RoutingTable () {}
+
+ void SetIpv4 (Ptr<Ipv4> ipv4);
+ void SetMainAddress (Ipv4Address mainAddress);
+
+ ~RoutingTable () {}
+
+ void Clear ();
+ uint32_t GetSize () const { return m_table.size (); }
+ std::vector<RoutingTableEntry> GetEntries () const;
+ void RemoveEntry (const Ipv4Address &dest);
+ void AddEntry (const Ipv4Address &dest,
+ const Ipv4Address &next,
+ uint32_t interface,
+ uint32_t distance);
+ void AddEntry (const Ipv4Address &dest,
+ const Ipv4Address &next,
+ const Ipv4Address &interfaceAddress,
+ uint32_t distance);
+ bool Lookup (const Ipv4Address &dest,
+ RoutingTableEntry &outEntry) const;
+ bool FindSendEntry (const RoutingTableEntry &entry,
+ RoutingTableEntry &outEntry) const;
+
+ // From Ipv4RoutingProtocol
+ virtual bool RequestRoute (uint32_t ifIndex,
+ const Ipv4Header &ipHeader,
+ Ptr<Packet> packet,
+ RouteReplyCallback routeReply);
+ virtual bool RequestIfIndex (Ipv4Address destination,
+ uint32_t& ifIndex);
+
+};
+
+}}; // namespace ns3, olsr
+
+#endif
--- a/src/routing/olsr/routing-table.cc Fri Nov 07 11:36:15 2008 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,309 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2004 Francisco J. Ros
- * Copyright (c) 2007 INESC Porto
- *
- * 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
- *
- * Authors: Francisco J. Ros <fjrm@dif.um.es>
- * Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
- */
-
-///
-/// \file OLSR_rtable.cc
-/// \brief Implementation of our routing table.
-///
-
-#include "routing-table.h"
-#include "ns3/packet.h"
-#include "ns3/ipv4-header.h"
-#include "ns3/log.h"
-
-namespace ns3 { namespace olsr {
-
-NS_LOG_COMPONENT_DEFINE ("OlsrRoutingTable");
-
-///
-/// \brief Clears the routing table and frees the memory assigned to each one of its entries.
-///
-void
-RoutingTable::Clear ()
-{
- NS_LOG_FUNCTION_NOARGS ();
- m_table.clear ();
-}
-
-///
-/// \brief Deletes the entry whose destination address is given.
-/// \param dest address of the destination node.
-///
-void
-RoutingTable::RemoveEntry (Ipv4Address const &dest)
-{
- m_table.erase (dest);
-}
-
-///
-/// \brief Looks up an entry for the specified destination address.
-/// \param dest destination address.
-/// \param outEntry output parameter to hold the routing entry result, if fuond
-/// \return true if found, false if not found
-///
-bool
-RoutingTable::Lookup (Ipv4Address const &dest,
- RoutingTableEntry &outEntry) const
-{
- // Get the iterator at "dest" position
- std::map<Ipv4Address, RoutingTableEntry>::const_iterator it =
- m_table.find (dest);
- // If there is no route to "dest", return NULL
- if (it == m_table.end ())
- return false;
- outEntry = it->second;
- return true;
-}
-
-///
-/// \brief Finds the appropiate entry which must be used in order to forward
-/// a data packet to a next hop (given a destination).
-///
-/// Imagine a routing table like this: [A,B] [B,C] [C,C]; being each pair of the
-/// form [dest addr,next-hop addr]. In this case, if this function is invoked with
-/// [A,B] then pair [C,C] is returned because C is the next hop that must be used
-/// to forward a data packet destined to A. That is, C is a neighbor of this node,
-/// but B isn't. This function finds the appropiate neighbor for forwarding a packet.
-///
-/// \param entry the routing table entry which indicates the destination node
-/// we are interested in.
-/// \return the appropiate routing table entry which indicates the next
-/// hop which must be used for forwarding a data packet, or NULL
-/// if there is no such entry.
-///
-bool
-RoutingTable::FindSendEntry (RoutingTableEntry const &entry,
- RoutingTableEntry &outEntry) const
-{
- outEntry = entry;
- while (outEntry.destAddr != outEntry.nextAddr)
- {
- if (not Lookup(outEntry.nextAddr, outEntry))
- return false;
- }
- return true;
-}
-
-
-bool
-RoutingTable::RequestRoute (uint32_t ifIndex,
- const Ipv4Header &ipHeader,
- Ptr<Packet> packet,
- RouteReplyCallback routeReply)
-{
- RoutingTableEntry entry1, entry2;
- if (Lookup (ipHeader.GetDestination (), entry1))
- {
- bool foundSendEntry = FindSendEntry (entry1, entry2);
- if (!foundSendEntry)
- NS_FATAL_ERROR ("FindSendEntry failure");
-
- Ipv4Route route = Ipv4Route::CreateHostRouteTo
- (ipHeader.GetDestination (), entry2.nextAddr, entry2.interface);
-
- NS_LOG_DEBUG ("Olsr node " << m_mainAddress
- << ": RouteRequest for dest=" << ipHeader.GetDestination ()
- << " --> nestHop=" << entry2.nextAddr
- << " interface=" << entry2.interface);
-
- routeReply (true, route, packet, ipHeader);
- return true;
- }
- else
- {
-#ifdef NS3_LOG_ENABLE
- NS_LOG_DEBUG ("Olsr node " << m_mainAddress
- << ": RouteRequest for dest=" << ipHeader.GetDestination ()
- << " --> NOT FOUND; ** Dumping routing table...");
- for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator iter = m_table.begin ();
- iter != m_table.end (); iter++)
- {
- NS_LOG_DEBUG ("dest=" << iter->first << " --> next=" << iter->second.nextAddr
- << " via interface " << iter->second.interface);
- }
-
- NS_LOG_DEBUG ("** Routing table dump end.");
-#endif
- return false;
- }
-}
-
-bool
-RoutingTable::RequestIfIndex (Ipv4Address destination,
- uint32_t& ifIndex)
-{
- RoutingTableEntry entry1, entry2;
- if (Lookup (destination, entry1))
- {
- bool foundSendEntry = FindSendEntry (entry1, entry2);
- if (!foundSendEntry)
- NS_FATAL_ERROR ("FindSendEntry failure");
- ifIndex = entry2.interface;
- return true;
- }
- else
- {
- return false;
- }
-}
-
-
-///
-/// \brief Adds a new entry into the routing table.
-///
-/// If an entry for the given destination existed, it is deleted and freed.
-///
-/// \param dest address of the destination node.
-/// \param next address of the next hop node.
-/// \param iface address of the local interface.
-/// \param dist distance to the destination node.
-///
-void
-RoutingTable::AddEntry (Ipv4Address const &dest,
- Ipv4Address const &next,
- uint32_t interface,
- uint32_t distance)
-{
- NS_LOG_FUNCTION (this << dest << next << interface << distance << m_mainAddress);
-
- NS_ASSERT (distance > 0);
-
- // Creates a new rt entry with specified values
- RoutingTableEntry &entry = m_table[dest];
-
- entry.destAddr = dest;
- entry.nextAddr = next;
- entry.interface = interface;
- entry.distance = distance;
-}
-
-void
-RoutingTable::AddEntry (Ipv4Address const &dest,
- Ipv4Address const &next,
- Ipv4Address const &interfaceAddress,
- uint32_t distance)
-{
- NS_LOG_FUNCTION (this << dest << next << interfaceAddress << distance << m_mainAddress);
-
- NS_ASSERT (distance > 0);
- NS_ASSERT (m_ipv4);
-
- RoutingTableEntry entry;
- for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
- {
- if (m_ipv4->GetAddress (i) == interfaceAddress)
- {
- AddEntry (dest, next, i, distance);
- return;
- }
- }
- NS_ASSERT (false); // should not be reached
- AddEntry (dest, next, 0, distance);
-}
-
-
-///
-/// \brief Returns the number of entries in the routing table.
-/// \return the number of entries in the routing table.
-///
-// u_int32_t
-// RoutingTable::size() {
-// return rt_.size();
-// }
-
-}}; // namespace ns3, olsr
-
-
-
-#ifdef RUN_SELF_TESTS
-
-
-#include "ns3/test.h"
-
-
-namespace ns3 { namespace olsr {
-
-class OlsrRoutingTableTest : public ns3::Test {
-private:
-public:
- OlsrRoutingTableTest ();
- virtual bool RunTests (void);
-
-
-};
-
-OlsrRoutingTableTest::OlsrRoutingTableTest ()
- : ns3::Test ("OlsrRoutingTable")
-{}
-
-
-bool
-OlsrRoutingTableTest::RunTests (void)
-{
- bool result = true;
-
- RoutingTable table;
-
- table.AddEntry (Ipv4Address ("1.2.3.5"),
- Ipv4Address ("1.2.3.4"),
- 0,
- 1);
-
- table.AddEntry (Ipv4Address ("1.2.3.4"),
- Ipv4Address ("1.2.3.4"),
- 0,
- 1);
-
- RoutingTableEntry entry1;
- NS_TEST_ASSERT (table.Lookup (Ipv4Address ("1.2.3.5"), entry1));
- NS_TEST_ASSERT_EQUAL (entry1.destAddr, Ipv4Address ("1.2.3.5"));
- NS_TEST_ASSERT_EQUAL (entry1.nextAddr, Ipv4Address ("1.2.3.4"));
- NS_TEST_ASSERT_EQUAL (entry1.interface, 0);
- NS_TEST_ASSERT_EQUAL (entry1.distance, 1);
-
- RoutingTableEntry entry2;
- NS_TEST_ASSERT (table.Lookup (Ipv4Address ("1.2.3.4"), entry2));
- NS_TEST_ASSERT_EQUAL (entry2.destAddr, Ipv4Address ("1.2.3.4"));
- NS_TEST_ASSERT_EQUAL (entry2.nextAddr, Ipv4Address ("1.2.3.4"));
- NS_TEST_ASSERT_EQUAL (entry2.interface, 0);
- NS_TEST_ASSERT_EQUAL (entry2.distance, 1);
-
- RoutingTableEntry sendEntry;
- NS_TEST_ASSERT (table.FindSendEntry (entry1, sendEntry));
- NS_TEST_ASSERT_EQUAL (sendEntry.destAddr, Ipv4Address ("1.2.3.4"));
- NS_TEST_ASSERT_EQUAL (sendEntry.nextAddr, Ipv4Address ("1.2.3.4"));
- NS_TEST_ASSERT_EQUAL (sendEntry.interface, 0);
- NS_TEST_ASSERT_EQUAL (sendEntry.distance, 1);
-
- table.RemoveEntry (Ipv4Address ("1.2.3.5"));
- RoutingTableEntry removedEntry;
- NS_TEST_ASSERT (not table.Lookup (Ipv4Address ("1.2.3.5"), removedEntry));
-
- return result;
-}
-
-static OlsrRoutingTableTest gOlsrRoutingTableTest;
-
-}}; // namespace
-
-
-#endif /* RUN_SELF_TESTS */
--- a/src/routing/olsr/routing-table.h Fri Nov 07 11:36:15 2008 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,111 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2004 Francisco J. Ros
- * Copyright (c) 2007 INESC Porto
- *
- * 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
- *
- * Authors: Francisco J. Ros <fjrm@dif.um.es>
- * Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
- */
-
-///
-/// \file OLSR_rtable.h
-/// \brief Header file for routing table's related stuff.
-///
-
-#ifndef __OLSR_RTABLE_H__
-#define __OLSR_RTABLE_H__
-
-#include "ns3/ipv4.h"
-#include <map>
-
-
-namespace ns3 { namespace olsr {
-
-/// An %OLSR's routing table entry.
-struct RoutingTableEntry
-{
- Ipv4Address destAddr; ///< Address of the destination node.
- Ipv4Address nextAddr; ///< Address of the next hop.
- uint32_t interface; ///< Interface index
- uint32_t distance; ///< Distance in hops to the destination.
-
- RoutingTableEntry () : // default values
- destAddr (), nextAddr (),
- interface (0), distance (0) {};
-};
-
-///
-/// \brief Defines rtable_t as a map of OLSR_rt_entry, whose key is the destination address.
-///
-/// The routing table is thus defined as pairs: [dest address, entry]. Each element
-/// of the pair can be accesed via "first" and "second" members.
-///
-//typedef std::map<Ipv4Address, RoutingTableEntry> RoutingTable;
-
-///
-/// \brief This class is a representation of the OLSR's Routing Table.
-///
-class RoutingTable : public Ipv4RoutingProtocol
-{
- std::map<Ipv4Address, RoutingTableEntry> m_table; ///< Data structure for the routing table.
- Ptr<Ipv4> m_ipv4;
-
- Ipv4Address m_mainAddress; // used only for printing debug messages
-
- void DoDispose ()
- {
- m_ipv4 = 0;
- Ipv4RoutingProtocol::DoDispose ();
- }
-
-public:
-
- RoutingTable () {}
-
- void SetIpv4 (Ptr<Ipv4> ipv4) {m_ipv4 = ipv4;}
- void SetMainAddress (Ipv4Address mainAddress) {m_mainAddress = mainAddress;}
-
- ~RoutingTable () {}
-
- void Clear ();
- uint32_t GetSize () const { return m_table.size (); }
- void RemoveEntry (const Ipv4Address &dest);
- void AddEntry (const Ipv4Address &dest,
- const Ipv4Address &next,
- uint32_t interface,
- uint32_t distance);
- void AddEntry (const Ipv4Address &dest,
- const Ipv4Address &next,
- const Ipv4Address &interfaceAddress,
- uint32_t distance);
- bool Lookup (const Ipv4Address &dest,
- RoutingTableEntry &outEntry) const;
- bool FindSendEntry (const RoutingTableEntry &entry,
- RoutingTableEntry &outEntry) const;
-
- // From Ipv4RoutingProtocol
- virtual bool RequestRoute (uint32_t ifIndex,
- const Ipv4Header &ipHeader,
- Ptr<Packet> packet,
- RouteReplyCallback routeReply);
- virtual bool RequestIfIndex (Ipv4Address destination,
- uint32_t& ifIndex);
-
-};
-
-}}; // namespace ns3, olsr
-
-#endif
--- a/src/routing/olsr/wscript Fri Nov 07 11:36:15 2008 -0800
+++ b/src/routing/olsr/wscript Fri Nov 07 19:51:00 2008 +0000
@@ -6,7 +6,7 @@
module.source = [
'olsr-header.cc',
'olsr-state.cc',
- 'routing-table.cc',
+ 'olsr-routing-table.cc',
'olsr-agent.cc',
'olsr-agent-impl.cc',
]
@@ -16,5 +16,6 @@
headers.source = [
'olsr-agent.h',
'olsr-header.h',
+ 'olsr-routing-table.h'
]