To implement Temporary Ordered Routing Algorithm (TORA) in ns3, we have to incorporate several steps. TORA is a reactive routing protocol that is especially designed for mobile ad hoc networks (MANETs) which works by establishing routes on-demand and maintaining routes only when they are essential.
Here are the steps to implement TORA in ns3.
Step-by-step guide on implementing TORA in ns3
- Set up your ns3 :
- Make sure that ns3 is installed in the computer. If not, install it.
- Create a New Module for TORA:
- Go to the src directory in ns3 and create a new directory named TORA. Create subdirectories named model, helper, and examples, inside the src directory.
- Define the TORA protocol:
- create .cc and .h files for the TORA protocol in the model directory. Define the TORA class that inherits from the Ipv4RoutingProtocol class.
- Implement the TORA class:
- Implement the key components of TORA such as neighbor discovery, route creation, route maintenance, and route erasure. For the TORA control messages, create classes: Query (QRY), Update (UPD), Clear (CLR), and Route Error (RERR).
- Integrate the TORA with ns3 routing system:
- Modify the RoutingHelper class to include TORA.
- Create simulation script:
- Set up a network topology.
- Install the Internet stack.
- Use the TORA helper to enable TORA.
- Set up applications and run the simulation.
Example Code Structure
Below is the example code structure for the implementation of TORA in ns3.
Define TORA Protocol
// src/tora/model/tora-routing-protocol.h
#ifndef TORA_ROUTING_PROTOCOL_H
#define TORA_ROUTING_PROTOCOL_H
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/ipv4.h”
#include “ns3/ipv4-l3-protocol.h”
#include “ns3/ipv4-route.h”
#include “ns3/node.h”
#include “ns3/net-device.h”
#include “ns3/socket.h”
#include “ns3/timer.h”
#include <vector>
#include <map>
namespace ns3 {
class ToraRoutingProtocol : public Ipv4RoutingProtocol
{
public:
static TypeId GetTypeId (void);
ToraRoutingProtocol ();
virtual ~ToraRoutingProtocol ();
// Inherited from Ipv4RoutingProtocol
virtual Ptr<Ipv4Route> RouteOutput (Ptr<Packet> p, const Ipv4Header &header,
Ptr<NetDevice> 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);
void RecvTora (Ptr<Socket> socket);
private:
void Start ();
void Stop ();
Ptr<Ipv4> m_ipv4;
std::map<Ptr<Socket>, Ipv4InterfaceAddress> m_socketAddresses;
};
} // namespace ns3
#endif // TORA_ROUTING_PROTOCOL_H
Implement TORA Routing Protocol
// src/tora/model/tora-routing-protocol.cc
#include “tora-routing-protocol.h”
#include “ns3/log.h”
#include “ns3/ipv4-static-routing.h”
#include “ns3/ipv4-static-routing-helper.h”
namespace ns3 {
NS_LOG_COMPONENT_DEFINE (“ToraRoutingProtocol”);
NS_OBJECT_ENSURE_REGISTERED (ToraRoutingProtocol);
TypeId
ToraRoutingProtocol::GetTypeId (void)
{
static TypeId tid = TypeId (“ns3::ToraRoutingProtocol”)
.SetParent<Ipv4RoutingProtocol> ()
.SetGroupName (“Internet”)
.AddConstructor<ToraRoutingProtocol> ();
return tid;
}
ToraRoutingProtocol::ToraRoutingProtocol ()
{
NS_LOG_FUNCTION (this);
}
ToraRoutingProtocol::~ToraRoutingProtocol ()
{
NS_LOG_FUNCTION (this);
}
void
ToraRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)
{
NS_LOG_FUNCTION (this << ipv4);
m_ipv4 = ipv4;
}
void
ToraRoutingProtocol::NotifyInterfaceUp (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
}
void
ToraRoutingProtocol::NotifyInterfaceDown (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
}
void
ToraRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION (this << interface << address);
}
void
ToraRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION (this << interface << address);
}
void
ToraRoutingProtocol::Start ()
{
NS_LOG_FUNCTION (this);
}
void
ToraRoutingProtocol::Stop ()
{
NS_LOG_FUNCTION (this);
}
Ptr<Ipv4Route>
ToraRoutingProtocol::RouteOutput (Ptr<Packet> p, const Ipv4Header &header,
Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)
{
NS_LOG_FUNCTION (this << p << header << oif << sockerr);
Ipv4Address dest = header.GetDestination ();
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
// Implement TORA route finding logic
// Example: Find the route to the destination address
// For simplicity, the example uses a static route
route->SetDestination (dest);
route->SetGateway (Ipv4Address (“1.1.1.1”)); // Example gateway
route->SetOutputDevice (m_ipv4->GetNetDevice (0)); // Example output device
return route;
}
bool
ToraRoutingProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,
UnicastForwardCallback ucb, MulticastForwardCallback mcb,
LocalDeliverCallback lcb, ErrorCallback ecb)
{
NS_LOG_FUNCTION (this << p << header << idev << ucb << mcb << lcb << ecb);
Ipv4Address dest = header.GetDestination ();
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
// Implement TORA route finding logic
// Example: Find the route to the destination address
// For simplicity, the example uses a static route
route->SetDestination (dest);
route->SetGateway (Ipv4Address (“1.1.1.1”)); // Example gateway
route->SetOutputDevice (m_ipv4->GetNetDevice (0)); // Example output device
ucb (route, p, header);
return true;
}
void
ToraRoutingProtocol::RecvTora (Ptr<Socket> socket)
{
NS_LOG_FUNCTION (this << socket);
// Implement TORA message reception logic
}
} // namespace ns3
Implement TORA Routing Protocol
// src/tora/model/tora-routing-protocol.cc
#include “tora-routing-protocol.h”
#include “ns3/log.h”
#include “ns3/ipv4-static-routing.h”
#include “ns3/ipv4-static-routing-helper.h”
namespace ns3 {
NS_LOG_COMPONENT_DEFINE (“ToraRoutingProtocol”);
NS_OBJECT_ENSURE_REGISTERED (ToraRoutingProtocol);
TypeId
ToraRoutingProtocol::GetTypeId (void)
{
static TypeId tid = TypeId (“ns3::ToraRoutingProtocol”)
.SetParent<Ipv4RoutingProtocol> ()
.SetGroupName (“Internet”)
.AddConstructor<ToraRoutingProtocol> ();
return tid;
}
ToraRoutingProtocol::ToraRoutingProtocol ()
{
NS_LOG_FUNCTION (this);
}
ToraRoutingProtocol::~ToraRoutingProtocol ()
{
NS_LOG_FUNCTION (this);
}
void
ToraRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)
{
NS_LOG_FUNCTION (this << ipv4);
m_ipv4 = ipv4;
}
void
ToraRoutingProtocol::NotifyInterfaceUp (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
}
void
ToraRoutingProtocol::NotifyInterfaceDown (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
}
void
ToraRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION (this << interface << address);
}
void
ToraRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION (this << interface << address);
}
void
ToraRoutingProtocol::Start ()
{
NS_LOG_FUNCTION (this);
}
void
ToraRoutingProtocol::Stop ()
{
NS_LOG_FUNCTION (this);
}
Ptr<Ipv4Route>
ToraRoutingProtocol::RouteOutput (Ptr<Packet> p, const Ipv4Header &header,
Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)
{
NS_LOG_FUNCTION (this << p << header << oif << sockerr);
Ipv4Address dest = header.GetDestination ();
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
// Implement TORA route finding logic
// Example: Find the route to the destination address
// For simplicity, the example uses a static route
route->SetDestination (dest);
route->SetGateway (Ipv4Address (“1.1.1.1”)); // Example gateway
route->SetOutputDevice (m_ipv4->GetNetDevice (0)); // Example output device
return route;
}
bool
ToraRoutingProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,
UnicastForwardCallback ucb, MulticastForwardCallback mcb,
LocalDeliverCallback lcb, ErrorCallback ecb)
{
NS_LOG_FUNCTION (this << p << header << idev << ucb << mcb << lcb << ecb);
Ipv4Address dest = header.GetDestination ();
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
// Implement TORA route finding logic
// Example: Find the route to the destination address
// For simplicity, the example uses a static route
route->SetDestination (dest);
route->SetGateway (Ipv4Address (“1.1.1.1”)); // Example gateway
route->SetOutputDevice (m_ipv4->GetNetDevice (0)); // Example output device
ucb (route, p, header);
return true;
}
void
ToraRoutingProtocol::RecvTora (Ptr<Socket> socket)
{
NS_LOG_FUNCTION (this << socket);
// Implement TORA message reception logic
}
} // namespace ns3
Helper Class
// src/tora/helper/tora-helper.h
#ifndef TORA_HELPER_H
#define TORA_HELPER_H
#include “ns3/ipv4-routing-helper.h”
#include “ns3/object-factory.h”
#include “ns3/tora-routing-protocol.h”
namespace ns3 {
class ToraHelper : public Ipv4RoutingHelper
{
public:
ToraHelper ();
ToraHelper (const ToraHelper &);
ToraHelper &operator= (const ToraHelper &);
virtual ~ToraHelper ();
virtual ToraHelper* Copy (void) const;
virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;
};
} // namespace ns3
#endif // TORA_HELPER_H
Implement Helper Class
// src/tora/helper/tora-helper.cc
#include “tora-helper.h”
#include “ns3/tora-routing-protocol.h”
#include “ns3/node.h”
namespace ns3 {
ToraHelper::ToraHelper ()
{
}
ToraHelper::ToraHelper (const ToraHelper &o)
{
}
ToraHelper&
ToraHelper::operator= (const ToraHelper &o)
{
return *this;
}
ToraHelper::~ToraHelper ()
{
}
ToraHelper*
ToraHelper::Copy (void) const
{
return new ToraHelper (*this);
}
Ptr<Ipv4RoutingProtocol>
ToraHelper::Create (Ptr<Node> node) const
{
Ptr<ToraRoutingProtocol> protocol = CreateObject<ToraRoutingProtocol> ();
node->AggregateObject (protocol);
return protocol;
}
} // namespace ns3
Example Simulation Script
// examples/tora-simulation.cc
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/tora-helper.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“ToraSimulation”);
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create (4);
// Create point-to-point links
PointToPointHelper p2p;
p2p.SetDeviceAttribute (“DataRate”, StringValue (“1Mbps”));
p2p.SetChannelAttribute (“Delay”, StringValue (“10ms”));
NetDeviceContainer devices01 = p2p.Install (nodes.Get (0), nodes.Get (1));
NetDeviceContainer devices12 = p2p.Install (nodes.Get (1), nodes.Get (2));
NetDeviceContainer devices23 = p2p.Install (nodes.Get (2), nodes.Get (3));
NetDeviceContainer devices30 = p2p.Install (nodes.Get (3), nodes.Get (0));
// Install the Internet stack
InternetStackHelper stack;
ToraHelper tora;
stack.SetRoutingHelper (tora);
stack.Install (nodes);
// Assign IP addresses
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces01 = address.Assign (devices01);
address.SetBase (“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces12 = address.Assign (devices12);
address.SetBase (“10.1.3.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces23 = address.Assign (devices23);
address.SetBase (“10.1.4.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces30 = address.Assign (devices30);
// Create a packet sink to receive packets
uint16_t sinkPort = 8080;
Address sinkAddress (InetSocketAddress (Ipv4Address (“10.1.4.1”), sinkPort));
PacketSinkHelper packetSinkHelper (“ns3::TcpSocketFactory”, sinkAddress);
ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (3));
sinkApps.Start (Seconds (1.0));
sinkApps.Stop (Seconds (10.0));
// Create a TCP client to send packets
OnOffHelper clientHelper (“ns3::TcpSocketFactory”, sinkAddress);
clientHelper.SetAttribute (“OnTime”, StringValue (“ns3::ConstantRandomVariable[Constant=1]”));
clientHelper.SetAttribute (“OffTime”, StringValue (“ns3::ConstantRandomVariable[Constant=0]”));
clientHelper.SetAttribute (“DataRate”, DataRateValue (DataRate (“1Mbps”)));
clientHelper.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = clientHelper.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
p2p.EnableAsciiAll (ascii.CreateFileStream (“tora-routing.tr”));
p2p.EnablePcapAll (“tora-routing”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Running the Simulation
To run the simulation, compile the script and execute it:
./waf configure –enable-examples
./waf build
./waf –run tora-simulation
Overall, we had successfully learned on implementing Temporary Ordered Routing Algorithm (TORA) in ns3 by defining the protocol and running the simulation. Temporary Ordered Routing Algorithm (TORA) projects are worked by us stay in contact with us for more details.