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

How to Implement Route by Hybrid Protocols in ns3

To implement the Route by hybrid protocols in ns3 have includes to an integrating the various routing techniques that takes benefits of their power while preventing their weakness. This is completed by making the custom routing protocol that incorporates the behaviours from both proactive and reactive protocols.

The given below is the detailed procedure on how to implement the route by hybrid protocols in ns3:

Step-by-Step Implementation:

Step 1: Set Up the ns3 Environment

  1. Install ns3: Make sure ns3 is installed in the computer.

sudo apt-get update

sudo apt-get install ns3

Create a New ns3 Project: Create a directory for your new project within the ns3 workspace.

cd ns-3

mkdir scratch/hybrid-routing

Step 2: Define the Hybrid Routing Protocol

  1. Create the Routing Protocol Class: Define a new routing protocol class that integrates features from both proactive and reactive protocols.

// HybridRouting.h

#ifndef HYBRID_ROUTING_H

#define HYBRID_ROUTING_H

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

#include “ns3/ipv4.h”

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

#include “ns3/timer.h”

#include <map>

#include <vector>

namespace ns3 {

class HybridRouting : public Ipv4RoutingProtocol {

public:

static TypeId GetTypeId (void);

HybridRouting ();

virtual ~HybridRouting ();

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, Time::Unit unit = Time::S) const;

private:

void ProactiveRouting ();

void ReactiveRouting (Ipv4Address dst);

Ptr<Ipv4> m_ipv4;

std::map<Ipv4Address, Ipv4RoutingTableEntry> m_proactiveRoutes;

std::map<Ipv4Address, Ipv4RoutingTableEntry> m_reactiveRoutes;

Timer m_proactiveTimer;

};

} // namespace ns3

#endif // HYBRID_ROUTING_H

// HybridRouting.cc

#include “HybridRouting.h”

#include “ns3/log.h”

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

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“HybridRouting”);

NS_OBJECT_ENSURE_REGISTERED (HybridRouting);

TypeId HybridRouting::GetTypeId (void) {

static TypeId tid = TypeId (“ns3::HybridRouting”)

.SetParent<Ipv4RoutingProtocol> ()

.SetGroupName (“Internet”)

.AddConstructor<HybridRouting> ();

return tid;

}

HybridRouting::HybridRouting () {

m_proactiveTimer.SetFunction (&HybridRouting::ProactiveRouting, this);

m_proactiveTimer.Schedule (Seconds (5.0));  // Example periodic interval for proactive updates

}

HybridRouting::~HybridRouting () {}

void HybridRouting::SetIpv4 (Ptr<Ipv4> ipv4) {

m_ipv4 = ipv4;

}

void HybridRouting::NotifyInterfaceUp (uint32_t interface) {}

void HybridRouting::NotifyInterfaceDown (uint32_t interface) {}

void HybridRouting::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {}

void HybridRouting::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {}

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

Ipv4Address dst = header.GetDestination ();

// Check if a proactive route exists

if (m_proactiveRoutes.find (dst) != m_proactiveRoutes.end ()) {

Ipv4RoutingTableEntry route = m_proactiveRoutes[dst];

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

ipv4Route->SetDestination (route.GetDest ());

ipv4Route->SetGateway (route.GetGateway ());

ipv4Route->SetOutputDevice (m_ipv4->GetNetDevice (route.GetInterface ()));

ipv4Route->SetSource (route.GetSource ());

return ipv4Route;

}

// Use reactive routing if no proactive route is found

ReactiveRouting (dst);

if (m_reactiveRoutes.find (dst) != m_reactiveRoutes.end ()) {

Ipv4RoutingTableEntry route = m_reactiveRoutes[dst];

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

ipv4Route->SetDestination (route.GetDest ());

ipv4Route->SetGateway (route.GetGateway ());

ipv4Route->SetOutputDevice (m_ipv4->GetNetDevice (route.GetInterface ()));

ipv4Route->SetSource (route.GetSource ());

return ipv4Route;

}

sockerr = Socket::ERROR_NOROUTETOHOST;

return 0;

}

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

Ipv4Address dst = header.GetDestination ();

// Check if the packet is for this node

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

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

if (m_ipv4->GetAddress (i, j).GetLocal () == dst) {

lcb (p, header, i);

return true;

}

}

}

// Check if a proactive route exists

if (m_proactiveRoutes.find (dst) != m_proactiveRoutes.end ()) {

Ipv4RoutingTableEntry route = m_proactiveRoutes[dst];

ucb (route, p, header);

return true;

}

// Use reactive routing if no proactive route is found

ReactiveRouting (dst);

if (m_reactiveRoutes.find (dst) != m_reactiveRoutes.end ()) {

Ipv4RoutingTableEntry route = m_reactiveRoutes[dst];

ucb (route, p, header);

return true;

}

return false;

}

void HybridRouting::PrintRoutingTable (Ptr<OutputStreamWrapper> stream, Time::Unit unit) const {

*stream->GetStream () << “Proactive Routes:\n”;

for (auto &route : m_proactiveRoutes) {

route.second.Print (stream);

}

*stream->GetStream () << “Reactive Routes:\n”;

for (auto &route : m_reactiveRoutes) {

route.second.Print (stream);

}

}

void HybridRouting::ProactiveRouting () {

// Implement proactive routing updates (e.g., periodically broadcast routing tables)

m_proactiveTimer.Schedule (Seconds (5.0));

}

void HybridRouting::ReactiveRouting (Ipv4Address dst) {

// Implement reactive routing (e.g., route discovery on demand)

}

} // namespace ns3

Step 3: Integrate the Routing Protocol in a Simulation

  1. Create a New Simulation Script: Create a new script in your scratch directory to use the hybrid routing protocol.

// hybrid-routing.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 “HybridRouting.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“HybridRoutingExample”);

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

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (4);

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

NetDeviceContainer devices;

devices = pointToPoint.Install (nodes);

InternetStackHelper stack;

Ptr<HybridRouting> hybridRouting = CreateObject<HybridRouting> ();

stack.SetRoutingHelper (hybridRouting);

stack.Install (nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign (devices);

UdpEchoServerHelper echoServer (9);

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

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (3), 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));

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 4: Compile and Run the Simulation

  1. Compile the Script: Compile your script using the waf build system.

./waf build

Run the Simulation: Run your simulation script and observe the results.

./waf –run scratch/hybrid-routing

Step 5: Enable Tracing and Analyse Results

  1. Enable Tracing: Add tracing to collect data for analysis.

AsciiTraceHelper ascii;

pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“hybrid-routing.tr”));

Run the Simulation: Set the simulation stop time and run it.

Simulator::Stop (Seconds (10.0));

Simulator::Run ();

Simulator::Destroy ();

Step 6: Analyse the Results

After running the simulation, analyse the generated trace files (hybrid-routing.tr) to study the routing behaviour and performance.

Obtain project concepts and performance evaluations concerning Network Routing through Hybrid Protocols utilizing the ns3 tool. We conduct performance analyses, so feel free to contact us for further guidance. If you are looking for top project ideas on proactive and reactive protocols within Hybrid Protocols using the ns3 tool, we are here to assist you.