To implement Mobile Ad hoc Network (MANET) in ns-3 we create a network of mobile nodes that communicate with each other without relying on any fixed infrastructure. Some steps were given below to create MANET using the AODV (Ad hoc On-demand Distance Vector) routing protocol in ns-3.
Step-by-Step Guide to Implement MANET in ns-3
- Install ns-3:
- Ensure ns-3 is installed. Follow the installation instructions for ns-3 if you haven’t done so already.
- Create a Simulation Script:
- Create a new script file, e.g., manet-simulation.cc.
- Include Necessary Headers:
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/mobility-module.h”
#include “ns3/wifi-module.h”
#include “ns3/applications-module.h”
#include “ns3/aodv-module.h”
Define the Main Function:
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
Set Up the Network Topology:
- Create nodes and configure Wi-Fi settings
NodeContainer nodes;
nodes.Create (10); // Create 10 nodes
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiHelper wifi;
wifi.SetRemoteStationManager (“ns3::AarfWifiManager”);
WifiMacHelper wifiMac;
Ssid ssid = Ssid (“ns-3-ssid”);
wifiMac.SetType (“ns3::StaWifiMac”, “Ssid”, SsidValue (ssid), “ActiveProbing”, BooleanValue (false));
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
Install Internet Stack and AODV Routing Protocol:
- Install the Internet stack on all nodes and enable AODV routing protocol:
InternetStackHelper stack;
AodvHelper aodv;
stack.SetRoutingHelper (aodv);
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
Configure Mobility:
- Set up mobility models for the nodes
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::RandomRectanglePositionAllocator”,
“X”, StringValue (“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”),
“Y”, StringValue (“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”));
mobility.SetMobilityModel (“ns3::RandomWaypointMobilityModel”,
“Speed”, StringValue (“ns3::UniformRandomVariable[Min=1.0|Max=5.0]”),
“Pause”, StringValue (“ns3::ConstantRandomVariable[Constant=2.0]”), “PositionAllocator”, StringValue (“ns3::RandomRectanglePositionAllocator”));
mobility.Install (nodes);
Install Applications:
- Set up a UDP echo server on one node and a UDP echo client on another node
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (20.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (10));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (2.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (9));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (20.0));
Run the Simulation:
- Define the simulation stop time and start the simulator:
Simulator::Stop (Seconds (20.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
Example Complete Script (manet-simulation.cc):
Some examples for simulating the MANET in ns-3
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/mobility-module.h”
#include “ns3/wifi-module.h”
#include “ns3/applications-module.h”
#include “ns3/aodv-module.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (10); // Create 10 nodes
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiHelper wifi; wifi.SetRemoteStationManager (“ns3::AarfWifiManager”);
WifiMacHelper wifiMac;
Ssid ssid = Ssid (“ns-3-ssid”);
wifiMac.SetType (“ns3::StaWifiMac”, “Ssid”, SsidValue (ssid), “ActiveProbing”, BooleanValue (false));
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
// Install the Internet stack on all nodes and enable AODV routing protocol
InternetStackHelper stack;
AodvHelper aodv;
stack.SetRoutingHelper (aodv);
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Configure mobility
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::RandomRectanglePositionAllocator”,
“X”, StringValue (“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”),
“Y”, StringValue (“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”));
mobility.SetMobilityModel (“ns3::RandomWaypointMobilityModel”,
“Speed”, StringValue (“ns3::UniformRandomVariable[Min=1.0|Max=5.0]”),
“Pause”, StringValue (“ns3::ConstantRandomVariable[Constant=2.0]”),
“PositionAllocator”, StringValue (“ns3::RandomRectanglePositionAllocator”));
mobility.Install (nodes);
// Install Applications
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (20.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (10));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (2.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (9));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (20.0));
Simulator::Stop (Seconds (20.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Explanation:
Here we have explained about the process of implementing MANET in ns-3
- Network Configuration:
- Nodes are created, and Wi-Fi settings are configured using YansWifiChannelHelper and YansWifiPhyHelper.
- The Wi-Fi MAC layer is set up for all nodes, and Wi-Fi devices are installed on the nodes.
- Routing Protocol:
- The Internet stack is installed on all nodes, and the AODV routing protocol is enabled.
- Mobility:
- The RandomWaypointMobilityModel is used to simulate mobile nodes moving randomly within a specified area.
- Applications:
- A UDP echo server application is installed on one node.
- A UDP echo client application is installed on another node to simulate communication.
- Running the Simulation:
- The simulation is run for a specified duration, and then the simulator is destroyed to clean up.
Finally, we have discussed that how the MANET in ns-3 we do implement and simulation support for your project.