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

How to Implement Classful Protocol in ns3

To implement a classful routing protocol in ns3 consists to make a custom routing protocol that observes the norms of classful addressing the thoughts from older IP networking standards we provide you some guidance. Classful routing protocols don’t get subnet mask data with routing updates and they depend on the IP address class (A, B, or C) to regulate the network phase to address.

Below are the procedures to implement the basic classful routing protocol in ns3 environment:

Step-by-Step Implementation

  1. Set Up Your Environment

Make sure ns3 installed. If not installed, download it from the official website.

  1. Create a Custom Routing Protocol

First, create a new directory for your custom module in the src directory of your ns-3 installation.

cd ns-3.xx

mkdir -p src/classful-routing

cd src/classful-routing

touch CMakeLists.txt classful-routing.h classful-routing.cc classful-routing-helper.h classful-routing-helper.cc

3.      Define the Protocol Headers

classful-routing.h

#ifndef CLASSFUL_ROUTING_H

#define CLASSFUL_ROUTING_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”

namespace ns3 {

class ClassfulRouting : public Ipv4RoutingProtocol

{

public:

  static TypeId GetTypeId (void);

  ClassfulRouting ();

  virtual ~ClassfulRouting ();

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

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

  void UpdateRoutingTable ();

  Ipv4Address GetNetworkAddress (Ipv4Address ip);

};

} // namespace ns3

#endif /* CLASSFUL_ROUTING_H */

classful-routing-helper.h

#ifndef CLASSFUL_ROUTING_HELPER_H

#define CLASSFUL_ROUTING_HELPER_H

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

#include “ns3/classful-routing.h”

namespace ns3 {

class ClassfulRoutingHelper : public Ipv4RoutingHelper

{

public:

  ClassfulRoutingHelper ();

  virtual ~ClassfulRoutingHelper ();

  ClassfulRoutingHelper* Copy (void) const;

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

};

} // namespace ns3

#endif /* CLASSFUL_ROUTING_HELPER_H */

4.      Implement the Protocol

classful-routing.cc

#include “classful-routing.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 (“ClassfulRouting”);

NS_OBJECT_ENSURE_REGISTERED (ClassfulRouting);

TypeId

ClassfulRouting::GetTypeId (void)

{

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

    .SetParent<Ipv4RoutingProtocol> ()

    .SetGroupName (“Internet”)

    .AddConstructor<ClassfulRouting> ();

  return tid;

}

ClassfulRouting::ClassfulRouting ()

{

}

ClassfulRouting::~ClassfulRouting ()

{

}

void

ClassfulRouting::SetIpv4 (Ptr<Ipv4> ipv4)

{

  m_ipv4 = ipv4;

  UpdateRoutingTable ();

}

void

ClassfulRouting::NotifyInterfaceUp (uint32_t interface)

{

  UpdateRoutingTable ();

}

void

ClassfulRouting::NotifyInterfaceDown (uint32_t interface)

{

  UpdateRoutingTable ();

}

void

ClassfulRouting::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

  UpdateRoutingTable ();

}

void

ClassfulRouting::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

  UpdateRoutingTable ();

}

Ptr<Ipv4Route>

ClassfulRouting::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

ClassfulRouting::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

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

{

  *stream->GetStream () << “Classful 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

ClassfulRouting::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

ClassfulRouting::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;

        }

    }

}

} // namespace ns3

classful-routing-helper.cc

#include “classful-routing-helper.h”

#include “ns3/classful-routing.h”

#include “ns3/node.h”

#include “ns3/ipv4.h”

namespace ns3 {

ClassfulRoutingHelper::ClassfulRoutingHelper ()

{

}

ClassfulRoutingHelper::~ClassfulRoutingHelper ()

{

}

ClassfulRoutingHelper*

ClassfulRoutingHelper::Copy (void) const

{

  return new ClassfulRoutingHelper (*this);

}

Ptr<Ipv4RoutingProtocol>

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

{

  Ptr<ClassfulRouting> routing = CreateObject<ClassfulRouting> ();

  node->AggregateObject (routing);

  return routing;

}

} // namespace ns3

5.      Set Up the Network Topology

In your scratch directory, create a new simulation script to use your custom classful routing protocol.

touch scratch/classful-simulation.cc

Include the necessary headers and set up the network topology:

#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/classful-routing-helper.h”

using namespace ns3;

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

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  NodeContainer nodes;

  nodes.Create (3);

  PointToPointHelper pointToPoint;

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

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

  NetDeviceContainer devices;

  devices = pointToPoint.Install (nodes.Get (0), nodes.Get (1));

  devices.Add (pointToPoint.Install (nodes.Get (1), nodes.Get (2)));

  InternetStackHelper internet;

  ClassfulRoutingHelper classfulRouting;

  internet.SetRoutingHelper (classfulRouting);

  internet.Install (nodes);

  Ipv4AddressHelper address;

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

  Ipv4InterfaceContainer interfaces = address.Assign (devices.Get (0));

  interfaces.Add (address.Assign (devices.Get (1)));

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

  interfaces.Add (address.Assign (devices.Get (2)));

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

  UdpEchoServerHelper echoServer (9);

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

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

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

}

6.      Build and Run the Simulation

After writing your script, you need to build and run it.

./waf build

./waf –run scratch/classful-simulation

7.      Analyze the Results

Simulation part is over then we need analyse the outcomes based on our requirements and also want to add logging or tracing to see the characteristics of the custom classful routing protocol. Overall we had seen how to simulate the classful routing protocol to analyse the outcome using the ns3 simulation tool and we also provide further information about classful routing protocol. Implementation of Classful Protocol in ns3 will be carried on as per your topic by our developers.