Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement TORA protocol in ns3

Implementation of  TORA (Temporally Ordered Routing Algorithm) in ns3 are discussed where , we create a custom module because TORA will not directly support ns3. It is a highly adaptive, loop-free, distributed routing protocol specially designed for mobile ad-hoc networks (MANETs). Here’s the steps given to implement a simplified version of TORA in ns-3.

Step-by-Step Guide to Implementing TORA in ns-3

  1. Set Up Your Environment

Ensure that ns3 is installed.

  1. Create Custom TORA Protocol

First, create a custom protocol for TORA. This involves creating header files and source files for TORA.

Create TORA Protocol Files

Create the directory structure for your custom TORA module.

cd ns-3.xx/src

mkdir -p tora/model

mkdir -p tora/helper

cd tora

3. Define TORA Header File

Create tora-routing-protocol.h in the model directory.

#ifndef TORA_ROUTING_PROTOCOL_H

#define TORA_ROUTING_PROTOCOL_H

#include “ns3/ipv4-routing-protocol.h”

#include “ns3/ipv4.h”

#include “ns3/timer.h”

#include “ns3/ipv4-address.h”

#include “ns3/ipv4-routing-table-entry.h”

#include “ns3/node-container.h”

namespace ns3 {

class ToraRoutingProtocol : public Ipv4RoutingProtocol

{

public:

  static TypeId GetTypeId (void);

  ToraRoutingProtocol ();

  virtual ~ToraRoutingProtocol ();

  void Setup (Ptr<Ipv4> ipv4, NodeContainer nodes);

  // 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);

  virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const;

private:

  Ptr<Ipv4> m_ipv4;

  NodeContainer m_nodes;

  std::map<Ipv4Address, Ptr<Ipv4Route>> m_routingTable;

  void UpdateRoutingTable ();

  Ipv4Address GetNetworkAddress (Ipv4Address ip);

  void SendToraMessages ();

  void HandleToraMessages (Ptr<Socket> socket);

};

} // namespace ns3

#endif /* TORA_ROUTING_PROTOCOL_H */

4. Define TORA Source File

Create tora-routing-protocol.cc in the model directory.

#include “tora-routing-protocol.h”

#include “ns3/log.h”

#include “ns3/ipv4-route.h”

#include “ns3/simulator.h”

#include “ns3/socket-factory.h”

#include “ns3/inet-socket-address.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 ()

{

}

 

ToraRoutingProtocol::~ToraRoutingProtocol ()

{

}

void

ToraRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)

{

  m_ipv4 = ipv4;

  UpdateRoutingTable ();

}

void

ToraRoutingProtocol::Setup (Ptr<Ipv4> ipv4, NodeContainer nodes)

{

  m_ipv4 = ipv4;

  m_nodes = nodes;

  UpdateRoutingTable ();

}

void

ToraRoutingProtocol::NotifyInterfaceUp (uint32_t interface)

{

  UpdateRoutingTable ();

}

void

ToraRoutingProtocol::NotifyInterfaceDown (uint32_t interface)

{

  UpdateRoutingTable ();

}

void

ToraRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

  UpdateRoutingTable ();

}

void

ToraRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

  UpdateRoutingTable ();

}

Ptr<Ipv4Route>

ToraRoutingProtocol::RouteOutput (Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)

{

  Ipv4Address dest = header.GetDestination ();

  auto it = m_routingTable.find (GetNetworkAddress (dest));

  if (it != m_routingTable.end ())

    {

      return it->second;

    }

  sockerr = Socket::ERROR_NOROUTETOHOST;

  return nullptr;

}

bool

ToraRoutingProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,  UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb)

{

  Ipv4Address dest = header.GetDestination ();

  auto it = m_routingTable.find (GetNetworkAddress (dest));

  if (it != m_routingTable.end ())

    {

      Ptr<Ipv4Route> route = it->second;

      if (route->GetOutputDevice () == idev)

        {

          lcb (p, header, idev);

          return true;

        }

      else

        {

          ucb (route, p, header);

          return true;

        }

    }

  ecb (p, header, Socket::ERROR_NOROUTETOHOST);

  return false;

}

void

ToraRoutingProtocol::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const

{

  *stream->GetStream () << “TORA Routing Table” << std::endl;

  for (auto it = m_routingTable.begin (); it != m_routingTable.end (); ++it)

    {

      *stream->GetStream () << it->first << ” -> ” << it->second->GetGateway () << ” via ” << it->second->GetOutputDevice ()->GetIfIndex () << std::endl;

    }

}

Ipv4Address

ToraRoutingProtocol::GetNetworkAddress (Ipv4Address ip)

{

  uint32_t address = ip.Get ();

  if ((address & 0x80000000) == 0)

    {

      return Ipv4Address (address & 0xff000000); // Class A

    }

  else if ((address & 0xc0000000) == 0x80000000)

    {

      return Ipv4Address (address & 0xffff0000); // Class B

    }

  else

    {

      return Ipv4Address (address & 0xffffff00); // Class C

    }

}

void

ToraRoutingProtocol::UpdateRoutingTable ()

{

  m_routingTable.clear ();

  for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); ++i)

    {

      for (uint32_t j = 0; j < m_ipv4->GetNAddresses (i); ++j)

        {

          Ipv4InterfaceAddress ifAddr = m_ipv4->GetAddress (i, j);

          Ipv4Address netAddr = GetNetworkAddress (ifAddr.GetLocal ());

          Ptr<Ipv4Route> route = Create<Ipv4Route> ();

          route->SetDestination (Ipv4Address::GetBroadcast ());

          route->SetGateway (Ipv4Address (“0.0.0.0”));

          route->SetOutputDevice (m_ipv4->GetNetDevice (i));

          m_routingTable[netAddr] = route;

        }

    }

}

void

ToraRoutingProtocol::SendToraMessages ()

{

  // Implement TORA message sending logic here

}

void

ToraRoutingProtocol::HandleToraMessages (Ptr<Socket> socket)

{

  // Implement TORA message handling logic here

}

} // namespace ns3

5. Define TORA Helper

Create tora-helper.h in the helper directory.

#ifndef TORA_HELPER_H

#define TORA_HELPER_H

#include “ns3/ipv4-routing-helper.h”

#include “ns3/tora-routing-protocol.h”

namespace ns3 {

class ToraHelper : public Ipv4RoutingHelper

{

public:

  ToraHelper ();

  virtual ~ToraHelper ();

  ToraHelper* Copy (void) const;

  virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;

};

} // namespace ns3

#endif /* TORA_HELPER_H */

Create tora-helper.cc in the helper directory.

#include “tora-helper.h”

#include “ns3/tora-routing-protocol.h”

#include “ns3/node.h”

#include “ns3/ipv4.h”

namespace ns3 {

ToraHelper::ToraHelper ()

{

}

ToraHelper::~ToraHelper ()

{

}

 

ToraHelper*

ToraHelper::Copy (void) const

{

  return new ToraHelper (*this);

}

Ptr<Ipv4RoutingProtocol>

ToraHelper::Create (Ptr<Node> node) const

{

  Ptr<ToraRoutingProtocol> toraRouting = CreateObject<ToraRoutingProtocol> ();

  node->AggregateObject (toraRouting);

  return toraRouting;

}

} // namespace ns3

6. Update CMakeLists.txt

Add the new TORA module to the ns-3 build system. Edit src/CMakeLists.txt and add the following line:

add_subdirectory (tora)

Create src/tora/CMakeLists.txt with the following content:

ns3_add_library (tora

    model/tora-routing-protocol.cc

    helper/tora-helper.cc

)

target_link_libraries (tora)

7. Set Up the Network Topology

Create a simulation script in the scratch directory to use the TORA protocol.

#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/mobility-module.h”

#include “ns3/wifi-module.h”

#include “ns3/tora-helper.h”

using namespace ns3;

int main (int argc, char *argv[])

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  NodeContainer nodes;

  nodes.Create (10);

  // Set up WiFi

  WifiHelper wifi;

  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

  wifiPhy.SetChannel (wifiChannel.Create ());

  WifiMacHelper wifiMac;

  wifiMac.SetType (“ns3::AdhocWifiMac”);

  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

  // Set up mobility model

  MobilityHelper mobility;

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (5.0),

                                 “DeltaY”, DoubleValue (5.0),

                                 “GridWidth”, UintegerValue (5),

                                 “LayoutType”, StringValue (“RowFirst”));

  mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);

  mobility.Install (nodes);

  // Install the internet stack on nodes

  InternetStackHelper stack;

  ToraHelper tora;

  stack.SetRoutingHelper (tora);

  stack.Install (nodes);

  // Assign IP addresses to the devices

  Ipv4AddressHelper address;

  address.SetBase (“10.1.1.0”, “255.255.255.0”);

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  // Set up applications (e.g., a UDP echo server and client)

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (9));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 9);

  echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

  echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

  echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

  // Enable tracing

  AsciiTraceHelper ascii;

  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“tora-simulation.tr”));

  wifiPhy.EnablePcapAll (“tora-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

8. Build and Run the Simulation

After writing your script and creating the necessary files, build and run it.

./waf build

./waf –run scratch/tora-simulation

We have learnt to implement TORA protocol in ns3 which is a highly adaptive, loop-free protocol that will not directly support ns3 so, we had created a custom module for implementation process. Distributed Routing Protocol related to your project with comparative analysis are guided by us.