How to Simulate Ad Hoc Protocols Projects Using NS3

To simulate the ad-hoc routing protocols within NS3 that normally contains utilising protocols are created for mobile or dynamic networks in which nodes are move and ascertain links dynamically. This simulation environment NS3 contains executions of numerous general ad-hoc protocols such as AODV (Ad-hoc On-demand Distance Vector), DSDV (Destination-Sequenced Distance-Vector), and OLSR (Optimized Link State Routing). We present basic method to replicate the ad-hoc protocol in NS3:

Steps to Simulate Ad-Hoc Protocols in NS3

  1. Install NS3

Initially, make certain we have NS3 installed. We can download it from the NS3 website. Then we follow the instructions for installing on the operating system.

  1. Supported Ad-Hoc Protocols in NS3

NS3 supports numerous ad-hoc protocols, containing:

  • AODV (Ad-hoc On-demand Distance Vector)
  • DSDV (Destination-Sequenced Distance-Vector)
  • OLSR (Optimized Link State Routing)
  • DSR (Dynamic Source Routing)

In this guide, we will concentrate on AODV and DSDV that are among the most usually used ad-hoc protocols.

  1. Create a Simulation Script

Example Simulation with AODV Protocol

  1. Include Necessary Headers

We can require to contain the suitable headers for network stack, routing, mobility, applications, and wireless communication.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/mobility-module.h”

#include “ns3/aodv-module.h”      // For AODV

#include “ns3/wifi-module.h”      // For wireless communication

#include “ns3/applications-module.h”

#include “ns3/netanim-module.h”   // For animation

  1. Create and Install Nodes

Make the nodes, which denote the mobile devices within an ad-hoc network. Here, we make 10 nodes.

NodeContainer nodes;

nodes.Create(10);  // Create 10 nodes

  1. Configure Wireless (Wi-Fi) Communication

We can use the WifiHelper to set up the wireless channel and devices, which enable communication among the nodes in an ad-hoc manner.

// Create Wi-Fi channel

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();

wifiPhy.SetChannel(wifiChannel.Create());

// Setup MAC and install Wi-Fi devices

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211g);

wifi.SetRemoteStationManager(“ns3::AarfWifiManager”);

WifiMacHelper wifiMac;

wifiMac.SetType(“ns3::AdhocWifiMac”);  // Ad-hoc mode

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

  1. Install Internet Stack with AODV

Install the internet stack (IP, TCP, UDP) and set up the routing protocol (AODV in this case) for each node.

AodvHelper aodv;  // AODV protocol

InternetStackHelper internet;

internet.SetRoutingHelper(aodv);  // Set AODV as the routing protocol

internet.Install(nodes);  // Install internet stack on all nodes

  1. Assign IP Addresses

Allocate an IP addresses to each wireless device. It will permit the nodes to communicate through IP.

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

  1. Set Up Mobility Models

Ad-hoc networks are normally dynamic, thus it’s general to allocate the mobility to nodes. In this instance, we utilise a RandomWaypointMobilityModel to replicate the node movement.

MobilityHelper mobility;

mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,

“MinX”, DoubleValue(0.0),

“MinY”, DoubleValue(0.0),

“DeltaX”, DoubleValue(5.0),

“DeltaY”, DoubleValue(10.0),

“GridWidth”, UintegerValue(3),

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

mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,

“Speed”, StringValue(“ns3::ConstantRandomVariable[Constant=20]”),

“Pause”, StringValue(“ns3::ConstantRandomVariable[Constant=0.2]”),

“PositionAllocator”, “ns3::GridPositionAllocator”);

mobility.Install(nodes);

  1. Set Up Applications

Configure applications like UDP Echo to replicate traffic and we calculate the network performance. In this case, we use a UDP server and client to transmit and receive data.

UdpEchoServerHelper echoServer(9);  // Port 9

ApplicationContainer serverApps = echoServer.Install(nodes.Get(9));  // Server on node 9

serverApps.Start(Seconds(1.0));

serverApps.Stop(Seconds(10.0));

UdpEchoClientHelper echoClient(interfaces.GetAddress(9), 9);  // Client on node 0 communicating with server on node 9

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

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

  1. Enable Tracing and Animation

We can allow packet capture (pcap) to investigate network traffic and NetAnim for visualizing the simulation.

wifiPhy.EnablePcapAll(“aodv_ad_hoc_simulation”);  // Enable pcap tracing for all nodes

AnimationInterface anim(“aodv_ad_hoc_simulation.xml”);  // For NetAnim visualization

  1. Run the Simulation

Set the simulation time, begin it, and then destroy the simulator instance at the end.

Simulator::Stop(Seconds(10.0));

Simulator::Run();

Simulator::Destroy();

Example Full Script for AODV Protocol

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/aodv-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/applications-module.h”

#include “ns3/netanim-module.h”

using namespace ns3;

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

NodeContainer nodes;

nodes.Create(10);  // Create 10 nodes

// Configure wireless (Wi-Fi) communication

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();

wifiPhy.SetChannel(wifiChannel.Create());

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211g);

wifi.SetRemoteStationManager(“ns3::AarfWifiManager”);

WifiMacHelper wifiMac;

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

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

// Install AODV routing protocol

AodvHelper aodv;

InternetStackHelper internet;

internet.SetRoutingHelper(aodv);

internet.Install(nodes);

// Assign IP addresses

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

// Set up mobility model

MobilityHelper mobility;

mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,

“MinX”, DoubleValue(0.0),

“MinY”, DoubleValue(0.0),

“DeltaX”, DoubleValue(5.0),

“DeltaY”, DoubleValue(10.0),

“GridWidth”, UintegerValue(3),

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

mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,

“Speed”, StringValue(“ns3::ConstantRandomVariable[Constant=20]”),

“Pause”, StringValue(“ns3::ConstantRandomVariable[Constant=0.2]”),

“PositionAllocator”, “ns3::GridPositionAllocator”);

mobility.Install(nodes);

// Set up UDP Echo application

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(100));

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 and animation

wifiPhy.EnablePcapAll(“aodv_ad_hoc_simulation”);

AnimationInterface anim(“aodv_ad_hoc_simulation.xml”);

Simulator::Stop(Seconds(10.0));

Simulator::Run();

Simulator::Destroy();

return 0;

}

  1. Running the Simulation
  • Build and run the script:

./waf build

./waf –run <your_script>

  1. Analyse the Results
  • Packet Capture (PCAP): The pcap files are generated can be investigated using tools such as Wireshark.
  • NetAnim: We can open the generated XML file in NetAnim to envision the simulation and we observe how packets are move among the nodes.
  1. Simulate Other Ad-Hoc Protocols

To mimic other ad-hoc protocols, like DSDV or OLSR, simply substitute the AODV helper in the script with the respective helper:

  • For DSDV:

#include “ns3/dsdv-helper.h”  // Include the DSDV module

DsdvHelper dsdv;

InternetStackHelper internet;

internet.SetRoutingHelper(dsdv);  // Set DSDV as the routing protocol

internet.Install(nodes);

  • For OLSR:

#include “ns3/olsr-helper.h”  // Include the OLSR module

OlsrHelper olsr;

InternetStackHelper internet;

internet.SetRoutingHelper(olsr);  // Set OLSR as the routing protocol

internet.Install(nodes);

We illustrated simple technique with example code to know on how to simulate and analyse the ad hoc protocols projects through NS3 simulator which includes executions of AODV, DSDV, and OLSR. If you know any more details on any of these project ideas, we will provide.

To simulate ad hoc protocol projects using the NS3 tool, share your research details with us. We offer tailored solutions and assist you in performance evaluation. Our expertise includes AODV (Ad-hoc On-demand Distance Vector), DSDV (Destination-Sequenced Distance-Vector), and OLSR (Optimized Link State Routing). By providing your research specifics, we can guide you toward the most effective outcomes.

Opening Time

9:00am

Lunch Time

12:30pm

Break Time

4:00pm

Closing Time

6:30pm

  • award1
  • award2