Skip to main content

AODV Protocol Modification in NS2 - NS2 Tutorial 7

AODV Protocol Modification

In this post, you can learn

·       What is AODV protocol

·       How to measure the energy, node position and node speed during AODV transmission

·       To understand and implement the promiscuous mode in AODV


A mobile Adhoc network is self-configurable multi-hop wireless network that will not depend on pre- existing infrastructure such as access points. A MANET contains several wireless nodes where each node may move randomly, chose to communicate with any node in its range directly. In order to communicate with any node not in its range, intermediate nodes help as routers to forward the packet to the destination. The characteristics of MANETs are energy constrained, multi-hop, dynamic topology, no central authority and device heterogeneity. A node can join any network and can leave the network anytime. Due to this higher mobility, MANETs exhibit dynamic nature in forming the topology. Basically, a MANET is self-organized, self-managed decentralized network with no trusted third parties. Some of the applications where MANET is showing the competence are military operations, emergency services, commercial environments, context aware services, linking up the internet and intranet etc [6]. But lack of central administration, security is a major concern in MANETs like nodes can behave mischievously, they may be selfish or they try intercepting the communication. Some of the Attacks on MANETs include gray-hole attack, black-hole attack, worm-hole attack etc. The routing protocols of the network layer should address the above stated issues. Several security methods have been designed for MANETs but most of them need external entity to perform authentication mechanisms [13]. Many approaches have been proposed to provide security in MANETs for sensitive information.

Similarly, many routing protocols on different trust variants like bandwidth, energy utilized, processing speed, end to end delay etc are considered in many algorithms.  Our approach is to route a packet to a trusted node via a trusted path using packet forwarding ratio. We are coupling our trust model to AODV routing protocol of MANETs. The following sections explain fundamentals in MANETs routing, our trust model and experimental results.

MANET Protocols
MANET Protocols

Routing protocols in MANETs can be classified into reactive, proactive and hybrid protocols [2]. Proactive routing protocols are table-driven and in contrary reactive protocols are on demand, routing information is updated only when needed. Hybrid approach is a mix of both versions [9]. The following figure lists out the classification.

Finding the node speed, Position and energy of a node

In this Chapter, the runtime information like energy, position of the nodes and speed of the nodes are computed/calculated at a particular instant of time. We have taken AODV protocol as it is easier to configure and it is simple to change the parameters.

When decided to use the AODV protocol, then the first step is to understand the protocol by learning it through the C++ source files that were available with the ns2 source.

Here are the locations of the AODV source codes



|-- aodv.h


|-- aodv_logs.o

|-- aodv.o

|-- aodv_packet.h


|-- aodv_rqueue.h

|-- aodv_rqueue.o


|-- aodv_rtable.h

`-- aodv_rtable.o

There are .cc, .h and .o files. The .cc and .h are the source files and .o is the object file.

The header (.h) files are provided to know the functionalities of a given protocol or algorithm by specifying the data members and member functions of a given class with only definitions. The C Source files implements the functions defined in the header files.

The runtime information for a mobile node is specified in ~/ns-allinone-2.35/ns-2.35/common/mobilenode.h which has some list of functions as specified below. These functions may be included inside the aodv source files and find the required information.

Some functions of mobilenode.h

inline MobileNode*& next() { return next_; }

     inline double X() { return X_; }

     inline double Y() { return Y_; }

     inline double Z() { return Z_; }

     inline double speed() { return speed_; }

     inline double dX() { return dX_; }

     inline double dY() { return dY_; }

     inline double dZ() { return dZ_; }

     inline double destX() { return destX_; }

     inline double destY() { return destY_; }

     inline double radius() { return radius_; }

     inline double getUpdateTime() { return position_update_time_; }

For our case1, we are going to modify two files in the ~ns-2.35/aodv/ folder.


Step 1 : Make an entry in aodv.h

Source File : ~ns-2.35/aodv/aodv.h

include the following header line in aodv.h in the beginning section of the file.

#include <mobilenode.h>

 In the aodv class, declare the following variables (in protected scope)

double xpos; // To define the x,y and z position

double ypos;

double zpos;

double iEnergy; //to get the energy at a given instant of time

int node_speed; //to find the node speed

MobileNode  *iNode; //to create a node

FILE *fp; //to get all the above information in a file for plotting


Step 2: Make an entry in constructor

Source File :

Declare the following variables inside the aodv constructor, these following variables were initialized in the aodv.h

xpos = 0.0;

ypos = 0.0;

zpos = 0.0;

node_speed = 0;


fp=fopen("pradeep.csv","w");//CSV files are easily handled by spreadsheets

MobileNode *iNode;


Step 3: Make an entry in

In AODV protocol, the run time information is required during a forward of packet. So AODV has a forward() function that handles the code related to the forwarding of packets. So the function from the mobilenode.h will be called or used inside the forward() function of AODV. Locate the forward() function implementation and paste the following codes

Finally access the required functions from mobilenode.h. Paste the following lines in the void AODV::forward(aodv_rt_entry *rt, Packet *p, double delay) {

//Code by pradeepkumar

/***This code retrieves node position*****/

iNode = (MobileNode*) (Node::get_node_by_address(index));

((MobileNode *) iNode)->getLoc(&xpos,&ypos,&zpos);

//Position of %d , X, Y, Z

fprintf(fp,"%d,%f,%f,%f,", index,  xpos, ypos, zpos);


/***This code retrieves the nodes velocity*****/

iNode = (MobileNode*) (Node::get_node_by_address(index));

((MobileNode *) iNode)->getVelo(&xpos, &ypos, &zpos);

//Velocity of %d , X, Y, Z

fprintf(fp,"%d,%f,%f,%f,", index, xpos,  ypos, zpos);


/***This code retrieves the nodes Energy Value*****/

iNode = (MobileNode*) (Node::get_node_by_address(index));

node_speed = ((MobileNode *) iNode)->speed();


//Velocity of %d , Node Speed in m/s Energy in joules

fprintf(fp,"%d,%d,%f,", index, node_speed,iEnergy);


Step 4:

Recompile ns2 to reflect the changes done above.  To recompile ns2, the detailed steps are given in Appendix A.

The simple step to recompile is,

1. Go to terminal and go to the folder ~ns-2.35/

2.     Give the command make

To verify these changes, run any Wireless tcl file with AODV as the protocol and to be included with energy model codes.

Here is a simple example to test the above modification.

Listing 7.1 – Testing the AODV protocol for printing the runtime information

set val(chan)        Channel/WirelessChannel    ;#Channel Type

set val(prop)        Propagation/TwoRayGround   ;# radio-propagation model

set val(netif)       Phy/WirelessPhy            ;# network interface type

set val(mac)         Mac/Simple                 ;# MAC type

set val(ifq)         Queue/DropTail/PriQueue    ;# interface queue type

set val(ll)          LL                         ;# link layer type

set val(ant)         Antenna/OmniAntenna        ;# antenna model

set val(ifqlen)      50                         ;# max packet in ifq

set val(nn)          2                          ;# number of mobilenodes

set val(rp)          AODV                    

set val(x)        250

set val(y)        250


# Initialize Global Variables

set ns            [new Simulator]

set tracef     [open w]

$ns trace-all $tracef


set namf [open casestudy1.nam w]

$ns namf-all-wireless $namf $val(x) $val(y)


# set up topography object

set topo       [new Topography]


$topo load_flatgrid $val(x) $val(y)


# Create God

create-god $val(nn)


# Create channel

set chan_ [new $val(chan)]


# Create node(0) and node(1)


# configure node, please note the change below.

$ns node-config -adhocRouting $val(rp) \

            -llType $val(ll) \

            -macType $val(mac) \

            -ifqType $val(ifq) \

            -ifqLen $val(ifqlen) \

            -antType $val(ant) \

            -propType $val(prop) \

            -phyType $val(netif) \

            -topoInstance $topo \

            -energyModel "EnergyModel" \

            -initialEnergy 2.0 \

            -txPower 0.4 \

            -rxPower 0.1 \

            -agentTrace ON \

            -routerTrace ON \

            -macTrace ON \

            -movementTrace ON \

            -channel $chan_


for {set i 0} {$i < $val(nn)} {incr i} {

    set node_($i) [$ns node]

    $node_($i) random-motion 0

    $ns initial_node_pos $node_($i) 20




# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes


$node_(0) set X_ 15.0

$node_(0) set Y_ 15.0

$node_(0) set Z_ 0.0


$node_(1) set X_ 150.0

$node_(1) set Y_ 150.0

$node_(1) set Z_ 0.0



# Now produce some simple node movements

# Node_(1) starts to move towards node_(0)


$ns at 0.0 "$node_(0) setdest 50.0 50.0 5.0"

$ns at 0.0 "$node_(1) setdest 60.0 40.0 10.0"



# Node_(1) then starts to move away from node_(0)

$ns at 3.0 "$node_(1) setdest 240.0 240.0 30.0"


# Setup traffic flow between nodes

# TCP connections between node_(0) and node_(1)


set tcp [new Agent/TCP]

$tcp set class_ 2

set sink [new Agent/TCPSink]

$ns attach-agent $node_(0) $tcp

$ns attach-agent $node_(1) $sink

$ns connect $tcp $sink

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ns at 0.5 "$ftp start"



# Tell nodes when the simulation ends


for {set i 0} {$i < $val(nn) } {incr i} {

    $ns at 6.0 "$node_($i) reset";


$ns at 6.0 "stop"

$ns at 6.01 "puts \"NS EXITING...\" ; $ns halt"

proc stop {} {

    global ns tracef

    $ns flush-trace

    close $tracef



puts "Starting Simulation..."

$ns run


Once the above script runs, a file “pradeep.csv” would be created in the folder where this tcl file was interpreted.

The content of pradeep.csv is given below. All the values are separated by comma,

1.     The first column is the index of the node, since there are 2 nodes (0 and 1 is the index)

2.     2nd, 3rd and 4th column represents the position of the nodes

3.     5th, 6th and 7th column represents the velocity of the node with position

4.     8th column represents the speed in m/s

5.     9th columns represents the energy.










7.3 Promiscuous Mode in AODV protocol

Promiscuous Mode in AODV

In a network, promiscuous mode allows a network device to intercept and read each network packet that arrives in its entirety. This mode of operation is sometimes given to a network snoop server that captures and saves all packets for analysis (for example, for monitoring network usage). Its often used to monitor network activity.  Promiscuous mode is happening at the Mac layer and with the help of mac only, the promiscuous mode can be achieved in AODV protocol.

Files to be modified are

1.  ~ns-2.35/aodv/aodv.h

2.  ~ns-2.35/aodv/

3.  ~ns-2.35/lib/tcl/ns-mobilenode.tcl

Step 1:

Source File: ~ns-2.35/aodv/aodv.h

Tap is a class that belongs to the mac and it is used to tap a packet when it is forwarded or transmitted. To make it happen with AODV, the aodv class should be the child class of Tap

Make AODV agent a child class of Tap (you should have a member function tap), and define the Mac variable

#include <mac.h> //added at the beginning of the aodv.h

class AODV: public Tap, public Agent {


void tap(const Packet *p);



Mac *mac_;

FILE *fs; //to store the tapped packet information for analysis

int count;




Step 2:

Source File: ~ns-2.35/aodv/

In the constructor of AODV protocol, add these two lines

fs = fopen(“promis.csv”,”w”); //already the new FILE is added in the aodv.h file

count =0; //initialize the count value to 0

Define TCL command "install-tap" and implement AODV::tap()

Only when the ‘install-tap’ command is implemented, the tapping of packets will happen. To know more about, how to implement a command, refer Chapter 3 (Creation of a new agent). Already AODV command() is implemented with various commands, the new command have to be appended along with it as given below

int AODV::command(int argc, const char*const* argv) {




else if(argc == 3) {



//addition of install-tap command

else if (strcmp(argv[1], "install-tap") == 0) {

mac_ = (Mac*)TclObject::lookup(argv[2]);

if (mac_ == 0) return TCL_ERROR;


return TCL_OK;



return Agent::command(argc, argv);



void AODV::tap(const Packet *p) {

//The following statement will print the node_id and the count packets. The last value of the corresponding node_id will be taken as the node’s tapped value.

fprintf(fs,”Node id-%d, %d”, index,count++);



Step 3:

Source File: ~ns-2.35/lib/tcl/ns-mobilenode.tcl

Node/MobileNode instproc add-target { agent port } {

$self instvar dmux_ imep_ toraDebug_ mac_


# Special processing for AODV

set aodvonly [string first "AODV" [$agent info class]]

if {$aodvonly != -1 } {

$agent if-queue [$self set ifq_(0)] ; # ifq between LL and MAC

$agent install-tap $mac_(0); # this is the place where we call the command install-tap for tapping the packets, the same line can be overridden inside the Tcl source file also.



Step 4:

Recompile NS2 by typing the command make at the ~ns-2.35/ folder and test the mode.

For testing the above, a Tcl script with AODV protocol have to be developed and to be plotted. The Listing 6.1 – Wireless network with 2 nodes was tried for this and here is the sample output for this.

In this the Node 0 consumes 1117 packets and Node 1 consumes (1133-1118) 16 packets as the node 0 is the sender node and node1 is the receiver node.

Node id-> 0, 1115

Node id-> 0, 1116

Node id-> 0, 1117

Node id-> 1, 1118

Node id-> 1, 1119

Node id-> 1, 1120

Node id-> 1, 1121

Node id-> 1, 1122

Node id-> 1, 1123

Node id-> 1, 1124

Node id-> 1, 1125

Node id-> 1, 1126

Node id-> 1, 1127

Node id-> 1, 1128

Node id-> 1, 1129

Node id-> 1, 1130

Node id-> 1, 1131

Node id-> 1, 1132

Node id-> 1, 1133

Using this promiscuous code, the network usage can be calculated.


This chapter deals with the two different modifications of existing protocol in ns2. The AODV protocol is a simple and efficient in handling the energy and easy to customize, AODV protocol is chosen for these two case studies. However the readers are advised to try these two case studies for other protocols like DSR, DSDV and TORA. The results of all these can also be compared.


Popular posts from this blog

Installing ns-3.34 in Ubuntu 20.04

This post shows how to install ns 3.34 in Ubuntu 20.04 LTS Prerequisites: Fresh installation of Ubuntu Version 20.04 LTS  ns3.34 can be downloaded from here Follow the video link for complete step by step instructions on the installation.  This version fixes the compilation issues of (bug in ns3.33)  Issue the following commands after opening a terminal  $ sudo apt update $ sudo apt install g++ python3 python3-dev python-dev pkg-config sqlite3 python3-setuptools git qt5-default gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar openmpi-bin openmpi-common openmpi-doc libopenmpi-dev tcpdump wireshark libxml2 libxml2-dev Unzip or untar the ns-allinone-3.34.tar.bz2 in the home folder (in my case its /home/pradeepkumar) $ cd ns-allinone-3.34/ $ ./ --enable-examples --enable-tests  Once the installation is completed, you may get an output show

Installation of NS2 (ns-2.35) in Ubuntu 20.04

Installation of NS2 (ns-2.35) in Ubuntu 20.04 LTS Step 1: Install the basic libraries like      $] sudo apt install build-essential autoconf automake libxmu-dev Step 2: install gcc-4.8 and g++-4.8 open the file using sudo mode $] sudo nano /etc/apt/sources.list Include the following line deb bionic main universe $] sudo apt update $] sudo apt install gcc-4.8 g++-4.8 Step 3:  Unzip the ns2 packages to home folder $] tar zxvf ns-allinone-2.35.tar.gz $] cd ns-allinone-2.35/ns-2.35 Modify the following make files. ~ns-2.35/ Change @CC@ to gcc-4.8 change @CXX@ to g++-4.8 ~nam-1.15/ ~xgraph-12.2/ ~otcl-1.14/ Change in all places  @CC@ to gcc-4.8 @CPP@ or @CXX@ to g++-4.8 open the file: ~ns-2.35/linkstate/ls.h Change at the Line no 137  void eraseAll() { erase(baseMap::begin(), baseMap::end()); } to This void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); } All changes made Step 4: Open a new termi

Installation of ns3 in Windows 10 and Windows 11 OS using WSL (Windows Subsystem for Linux)

This post shows how to install ns-3.33 in Windows 10 through WSL (Windows Subsystem for Linux) This posts works for Windows 11 also (I have tested it on a Windows 11 ISO and it works the Same way as mentioned in the following post.) This post will work for ns-3.3x version. Prerequisites : Install Windows Subsystem for Linux with GUI: Please refer the following video  System Information: OS used: Windows 10 and WSL (Ubuntu 20.04) GUI: XServer for Windows NS3 Version: ns-3.33 See the following complete video on how to install ns3 in Windows 10 Step 0 : Open XLaunch Step 1 :  Open WSL using PowerShell and open it as Administrator Command:/  wsl $ xfce4-session The GUI of Ubuntu Opens within Windows 10 OS. Step 2 : Download ns3 from website through Mozilla Firefox browser Step 3: Open a Terminal  $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-q