How to Simulate Classless Protocol Projects Using NS3

To simulate classless protocols projects using NS3 that mainly refers to operating with Classless Inter-Domain Routing (CIDR), which is generally used with routing protocols that don’t based on classful IP addressing. These protocols can be actively estimated routes rely on IP prefixes and subnet masks, instead of the predefined IP address classes (A, B, C).

In this instruction, we will be replicated a classless routing protocol project within NS3 by:

  • Setting up IP networks using CIDR (Classless Inter-Domain Routing).
  • Configuring routing protocols, which support CIDR (such as static routing or a dynamic protocol like OSPF).

We will illustrate how to:

  1. Make a network using CIDR (subnet masks and prefixes).
  2. Execute the static routing (or dynamic routing) with classless IP address configurations.
  3. Replicate the network using NS3.

Steps to Simulate Classless Protocols (CIDR) in NS3

  1. Install NS3

Make certain we install NS3 on the computer. We can download it from the NS3 website. If NS3 is already installed then we proceed to the next step.

  1. Create a Simulation Script

We will replicate a basic network using CIDR with static routing, however we can also be expanded it to use dynamic routing protocols like OSPF, RIP, or BGP.

Example Simulation Script with CIDR

  1. Include Necessary Headers

We will require the following headers for network setup, internet stack, static routing, and applications.

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

  1. Create and Install Nodes

Make a set of nodes are signifying the routers and hosts. For simplicity, we will make a network with 3 routers and 2 hosts.

NodeContainer routers;

routers.Create(3);  // Create 3 routers

NodeContainer hosts;

hosts.Create(2);    // Create 2 hosts

  1. Configure Point-to-Point Links

We will configure point-to-point links between routers and among routers and hosts.

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices1, devices2, devices3;

devices1 = pointToPoint.Install(routers.Get(0), routers.Get(1));  // Router 0 to Router 1

devices2 = pointToPoint.Install(routers.Get(1), routers.Get(2));  // Router 1 to Router 2

devices3 = pointToPoint.Install(routers.Get(1), hosts.Get(0));    // Router 1 to Host 0

  1. Install Internet Stack with CIDR

Install the internet stack (which includes TCP/IP protocols) on the routers and hosts.

InternetStackHelper internet;

internet.Install(routers);

internet.Install(hosts);

  1. Assign IP Addresses Using CIDR

Allocate an IP addresses to the devices using CIDR. We will use various subnet masks for the networks, and signifying classless addressing.

// Set up the first network with /30 (255.255.255.252) subnet mask

Ipv4AddressHelper address1;

address1.SetBase(“10.1.1.0”, “255.255.255.252”);

Ipv4InterfaceContainer interfaces1 = address1.Assign(devices1);  // Router 0 to Router 1

// Set up the second network with /28 (255.255.255.240) subnet mask

Ipv4AddressHelper address2;

address2.SetBase(“10.1.2.0”, “255.255.255.240”);

Ipv4InterfaceContainer interfaces2 = address2.Assign(devices2);  // Router 1 to Router 2

// Set up the third network with /24 (255.255.255.0) subnet mask

Ipv4AddressHelper address3;

address3.SetBase(“10.1.3.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces3 = address3.Assign(devices3);  // Router 1 to Host 0

  1. Set Up Static Routing

We will set up static routing manually for this instance. Later we can expand it to utilise dynamic routing protocols such as OSPF or RIP.

Ipv4StaticRoutingHelper staticRoutingHelper;

// Get the static routing object for Router 0

Ptr<Ipv4StaticRouting> staticRouting0 = staticRoutingHelper.GetStaticRouting(routers.Get(0)->GetObject<Ipv4>());

// Add a route to Router 2 via Router 1

staticRouting0->AddNetworkRouteTo(Ipv4Address(“10.1.2.0”), Ipv4Mask(“255.255.255.240”), Ipv4Address(“10.1.1.2”), 1);

// Get the static routing object for Router 1

Ptr<Ipv4StaticRouting> staticRouting1 = staticRoutingHelper.GetStaticRouting(routers.Get(1)->GetObject<Ipv4>());

// Route from Router 1 to Router 2

staticRouting1->AddNetworkRouteTo(Ipv4Address(“10.1.2.0”), Ipv4Mask(“255.255.255.240”), Ipv4Address(“10.1.2.1”), 2);

// Get the static routing object for Router 2

Ptr<Ipv4StaticRouting> staticRouting2 = staticRoutingHelper.GetStaticRouting(routers.Get(2)->GetObject<Ipv4>());

// Route to Host 0 network via Router 1

staticRouting2->AddNetworkRouteTo(Ipv4Address(“10.1.3.0”), Ipv4Mask(“255.255.255.0”), Ipv4Address(“10.1.2.1”), 1);

  1. Set Up Applications

We will set up a TCP server and TCP client to generate the traffic among the two hosts over routers.

  • Server (Sink Application):

uint16_t port = 8080;

Address sinkAddress(InetSocketAddress(interfaces3.GetAddress(1), port));  // Host 0 IP

PacketSinkHelper packetSinkHelper(“ns3::TcpSocketFactory”, sinkAddress);

ApplicationContainer sinkApp = packetSinkHelper.Install(hosts.Get(0));  // Install on Host 0

sinkApp.Start(Seconds(0.0));

sinkApp.Stop(Seconds(20.0));

  • Client (OnOff Application):

OnOffHelper clientHelper(“ns3::TcpSocketFactory”, sinkAddress);

clientHelper.SetAttribute(“OnTime”, StringValue(“ns3::ConstantRandomVariable[Constant=1]”));

clientHelper.SetAttribute(“OffTime”, StringValue(“ns3::ConstantRandomVariable[Constant=0]”));

clientHelper.SetAttribute(“DataRate”, StringValue(“2Mbps”));

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

ApplicationContainer clientApp = clientHelper.Install(routers.Get(2));  // Client is in Router 2

clientApp.Start(Seconds(1.0));

clientApp.Stop(Seconds(20.0));

  1. Run the Simulation

To end, we set the simulation duration and run the simulation.

Simulator::Stop(Seconds(20.0));

Simulator::Run();

Simulator::Destroy();

  1. Example Full Script

The following is the comprehensive NS3 simulation script using CIDR with static routing:

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

using namespace ns3;

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

// Step 1: Create nodes

NodeContainer routers, hosts;

routers.Create(3);  // 3 routers

hosts.Create(2);    // 2 hosts

// Step 2: Configure point-to-point links

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices1, devices2, devices3;

devices1 = pointToPoint.Install(routers.Get(0), routers.Get(1));

devices2 = pointToPoint.Install(routers.Get(1), routers.Get(2));

devices3 = pointToPoint.Install(routers.Get(1), hosts.Get(0));

// Step 3: Install internet stack

InternetStackHelper internet;

internet.Install(routers);

internet.Install(hosts);

// Step 4: Assign IP addresses using CIDR

Ipv4AddressHelper address1, address2, address3;

address1.SetBase(“10.1.1.0”, “255.255.255.252”);

Ipv4InterfaceContainer interfaces1 = address1.Assign(devices1);

address2.SetBase(“10.1.2.0”, “255.255.255.240”);

Ipv4InterfaceContainer interfaces2 = address2.Assign(devices2);

address3.SetBase(“10.1.3.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces3 = address3.Assign(devices3);

// Step 5: Set up static routing

Ipv4StaticRoutingHelper staticRoutingHelper;

Ptr<Ipv4StaticRouting> staticRouting0 = staticRoutingHelper.GetStaticRouting(routers.Get(0)->GetObject<Ipv4>());

staticRouting0->AddNetworkRouteTo(Ipv4Address(“10.1.2.0”), Ipv4Mask(“255.255.255.240”), Ipv4Address(“10.1.1.2”), 1);

Ptr<Ipv4StaticRouting> staticRouting1 = staticRoutingHelper.GetStaticRouting(routers.Get(1)->GetObject<Ipv4>());

staticRouting1->AddNetworkRouteTo(Ipv4Address(“10.1.2.0”), Ipv4Mask(“255.255.255.240”), Ipv4Address(“10.1.2.1”), 2);

Ptr<Ipv4StaticRouting> staticRouting2 = staticRoutingHelper.GetStaticRouting(routers.Get(2)->GetObject<Ipv4>());

staticRouting2->AddNetworkRouteTo(Ipv4Address(“10.1.3.0”), Ipv4Mask(“255.255.255.0”), Ipv4Address(“10.1.2.1”), 1);

// Step 6: Set up TCP traffic (Sink on Host 0, Client on Router 2)

uint16_t port = 8080;

Address sinkAddress(InetSocketAddress(interfaces3.GetAddress(1), port));

PacketSinkHelper packetSinkHelper(“ns3::TcpSocketFactory”, sinkAddress);

ApplicationContainer sinkApp = packetSinkHelper.Install(hosts.Get(0));

sinkApp.Start(Seconds(0.0));

sinkApp.Stop(Seconds(20.0));

OnOffHelper clientHelper(“ns3::TcpSocketFactory”, sinkAddress);

clientHelper.SetAttribute(“OnTime”, StringValue(“ns3::ConstantRandomVariable[Constant=1]”));

clientHelper.SetAttribute(“OffTime”, StringValue(“ns3::ConstantRandomVariable[Constant=0]”));

clientHelper.SetAttribute(“DataRate”, StringValue(“2Mbps”));

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

ApplicationContainer clientApp = clientHelper.Install(routers.Get(2));

clientApp.Start(Seconds(1.0));

clientApp.Stop(Seconds(20.0));

// Step 7: Run the simulation

Simulator::Stop(Seconds(20.0));

Simulator::Run();

Simulator::Destroy();

return 0;

}

  1. Run the Simulation
  • Now, we compile and run the script using NS3’s waf build system:

./waf build

./waf –run <your_script_name>

  1. Analyze Results
  • PCAP Tracing: We can allow packet capture (pcap) to log all packets for analysis with Wireshark:

pointToPoint.EnablePcapAll(“cidr_simulation”);  // Save pcap trace for all nodes

  • Flow Monitor: Also we can utilise the NS3’s Flow Monitor to trace the performance parameters such as throughput, delay, and packet loss.

The Classless protocol projects was addressed through a sequential process, simulated and analysed with the help of the simulation tool NS3. We will present comprehensive details using various simulation tool according to your needs.

To replicate the Classless Protocol Projects Using NS3 is send us an email with all the research details you have. We will respond to you right away and provide you with the finest possible results. We will support you with your network evaluation.

Opening Time

9:00am

Lunch Time

12:30pm

Break Time

4:00pm

Closing Time

6:30pm

  • award1
  • award2