Skip to main content

MANET Routing Protocols using ns3


Download the Code here:

Check the Video for Detailed instruction and how to use the plots, graphs and source code.

Comparison of MANET routing Protocols 1. AODV
3. DSR
using NS3 (Network Simulator 3)
B.Tech, M.Tech, PhD...
1. Reactive Vs Proactive routing
2. PErformance comparison of MANET protocols
3. AODV Vs DSDV Comparison and also at my channel.

What Version: ns-3.29
My Ubunut OS is: Ubuntu 18.04

This file we are going to use for our simulation:

Once you under stand the code, now lets run this example

Step 1: Copy the above file in to ~ns-3.29/scratch/ folder

Step 2: Understand this code.
Step 3: Run this code
Open the terminal, Go to ns-3.29 and run the following command

$] ./waf --run scratch/manet-routing-compare

enable the following header file

#include "ns3/flo…

OLSR protocol in NS-2.35 (ns2)

OLSR protocol is called as Optimized link State routing protocol and is defined by
The OLSR is available as a patch for ns2 for various versions. There is a new version UM-OLSR patch in the given link below.

OS Used: Linux Mint 14 and Ubuntu 12.10

The following files are modified in ns-2.35 for OLSR

Download the OLSR File here (the original OLSR may contain some bug, so download from this location for bug free OLSR Code)
Copy the file from ns-modified files/ to the corresponding location

Ensure that you have a fresh installation of ns-2.35 (else pasting these files will overwrite the information what you have earlier)

The zip file also has a patch file that can be patched for various ns2 versions. if you encounter any errors, go for manual patching.

You can also see: SUMO and MOVE Installation for VANETs

OLSR Installation

Once all the files were copied to their location. Open the terminal and go to the ~ns-2.35/ folder and execute the commands one by one.


Thats it, the code gets recompiled and download this source code to test the OLSR protocol
here is the code
# ======================================================================
# Define options
# ======================================================================
set opt(chan) Channel/WirelessChannel ;# channel type
set opt(prop) Propagation/TwoRayGround ;# radio-propagation model
set opt(netif) Phy/WirelessPhy ;# network interface type
set opt(mac) Mac/802_11 ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type
set opt(ll) LL ;# link layer type
set opt(ant) Antenna/OmniAntenna ;# antenna model
set opt(ifqlen) 50 ;# max packet in ifq
set opt(nn) 5 ;# number of mobilenodes
set opt(adhocRouting) OLSR ;# routing protocol
set opt(cp) "" ;# connection pattern file
set opt(sc) "" ;# node movement file.
set opt(x) 400 ;#
set opt(y) 600 ;#
set opt(seed) 0.0 ;#
set opt(stop) 45 ;#
set opt(cbr-start) 30.0
# ============================================================================
# check for random seed
if {$opt(seed) > 0} {
puts "Seeding Random number generator with $opt(seed)\n"
ns-random $opt(seed)
# create simulator instance
set ns_ [new Simulator]
# control OLSR behaviour from this script -
# commented lines are not needed because
# those are default values
Agent/OLSR set use_mac_ true
#Agent/OLSR set debug_ false
#Agent/OLSR set willingness 3
#Agent/OLSR set hello_ival_ 2
#Agent/OLSR set tc_ival_ 5
# open traces
set tracefd [open w]
set namtrace [open olsr_example.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)
# create topography object
set topo [new Topography]
# define topology
$topo load_flatgrid $opt(x) $opt(y)
# create God
create-god $opt(nn)

set chan_1_ [new $opt(chan)]

# configure mobile nodes
$ns_ node-config -adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channel $chan_1_ \
-topoInstance $topo \
-wiredRouting OFF \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF
for {set i 0} {$i < $opt(nn)} {incr i} {
set node_($i) [$ns_ node]

# define initial node position in nam
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns_ initial_node_pos $node_($i) 20

# positions
$node_(0) set X_ 350.0
$node_(0) set Y_ 200.0
$node_(1) set Z_ 0.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 350.0
$node_(2) set X_ 200.0
$node_(2) set Y_ 550.0
$node_(2) set Z_ 0.0
$node_(3) set X_ 50.0
$node_(3) set Y_ 200.0
$node_(3) set Z_ 0.0
$node_(4) set X_ 200.0
$node_(4) set Y_ 50.0
$node_(4) set Z_ 0.0
# setup UDP connection
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns_ attach-agent $node_(0) $udp
$ns_ attach-agent $node_(2) $null
$ns_ connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 512
$cbr set rate_ 20Kb
$cbr attach-agent $udp
$ns_ at $opt(cbr-start) "$cbr start"
# print (in the trace file) routing table and other
# internal data structures on a per-node basis

$ns_ at 10.0 "[$node_(0) agent 255] print_rtable"
$ns_ at 15.0 "[$node_(0) agent 255] print_linkset"
$ns_ at 20.0 "[$node_(0) agent 255] print_nbset"
$ns_ at 25.0 "[$node_(0) agent 255] print_nb2hopset"
$ns_ at 30.0 "[$node_(0) agent 255] print_mprset"
$ns_ at 35.0 "[$node_(0) agent 255] print_mprselset"
$ns_ at 40.0 "[$node_(0) agent 255] print_topologyset"
# source connection-pattern and node-movement scripts
if { $opt(cp) == "" } {
puts "*** NOTE: no connection pattern specified."
set opt(cp) "none"
} else {
puts "Loading connection pattern..."
source $opt(cp)
if { $opt(sc) == "" } {
puts "*** NOTE: no scenario file specified."
set opt(sc) "none"
} else {
puts "Loading scenario file..."
source $opt(sc)
puts "Load complete..."
# tell all nodes when the simulation ends
for {set i 0} {$i < $opt(nn) } {incr i} {
$ns_ at $opt(stop) "$node_($i) reset";
$ns_ at $opt(stop) "puts \"NS EXITING...\" ; $ns_ halt"
$ns_ at $opt(stop) "stop"

proc stop {} {
global ns_ tracefd namtrace
$ns_ flush-trace
close $tracefd
close $namtrace
puts "Starting Simulation..."
$ns_ run

The above code tells the protocol to print the routing table, neighbour list, etc to the trace file. So open the trace file and see the routing table printing as various times 10.0, 15.0, 20.0, etc
Here is the sample output for the node 1 with various printing.
OLSR Trace

P 10.000000 _1_ Routing Table
P dest next iface dist
P 0 0 1 1
P 2 2 1 1
P 3 3 1 1
P 4 3 1 2

P 15.000000 _1_ Link Set
P local nb sym asym lost time
P 1 0 20.563653 20.563653 0.000000 26.563653
P 1 2 20.457948 20.457948 0.000000 26.457948
P 1 3 20.511131 20.511131 0.000000 26.511131

P 20.000000 _1_ Neighbor Set
P nb status willingness
P 0 1 3
P 2 1 3
P 3 1 3

P 25.000000 _1_ Neighbor2hop Set
P nb nb2hop time
P 0 4 29.878606
P 3 4 30.756880

P 30.000000 _1_ MPR Set
P nb
P 0

P 35.000000 _1_ MPR Selector Set
P nb time
P 0 40.819322
P 2 39.935872
P 3 39.741881

P 40.000000 _1_ Topology Set
P dest last seq time
P 1 0 1 54.025467
P 4 0 1 54.025467

This shows the successful installation of OLSR in ns-2.35.  Comment on if you have any other queries.


  1. sir please help me in installing OSLR in ns-2.35 even though i tried what u said it doesn't work to me please kindly mail me more details sir my email is


  2. Sir I tired you method but it showing me problem on ns 2.34 and ns 2.35.
    some for loop problem

    can't read "node_(0)": no such variable
    while executing
    "$ns_ initial_node_pos $node_($i) 20"
    ("for" body line 2)
    invoked from within
    "for {set i 0} {$i < $opt(nn)} {incr i} {
    $ns_ initial_node_pos $node_($i) 20
    (file "2.tcl" line 88)

  3. hello sir
    I'm not able to download the olsr code and the program written here has some errors could you please share the program


Post a Comment

Popular posts from this blog

Routing in VANETs using ns3

Part 1
WAVE - Wireless Access for Vehicular environments. It might take more than an hour. The readers are requested to be patient. SUMO, VANETs, routing comparison 1. Explain the full source code (1550 lines of code) 2. Creating a real scenario using osm (Open Street Map Web Wizard) 3. Performance analysis for various vanet protocols. Location of the source code /home/pradeepkumar/ns-allinone-3.27/ns-3.27/src/wave/examples
Move this file to the scratch/ for inclusion of all modules. Step 1: Explanation of source code. Copy the file to scratch folder. This is just Part 1 of the VANET comparison

Part 2 Please watch the First Part before watching this video
Part 2 - Analysis of the results.
Please go through the first video (Part 1) and then watch this video (PArt 2)
#VANETs #NS3 #Routing
1. SUMO for web traffic ( 2. Convert this into mobility.tcl file and t…

ns3 installation in Ubuntu 16.04

This post serves the installation instructions of ns3 in ubuntu 16.04 version. Some of my students are working in ns3, this post will benefit them in installing ns3.

OS Used: Ubuntu 16.04.4
ns3 version: ns3 version 3.27

The same procedure will be applied for OS like Debian, Linux Mint.

Fresh installation of Ubuntu 16.04 Let you try the fresh installation of ubuntu in your hard disk along with windows.
Installation of ns3 dependencies ns3 needs so many dependencies, developmental libraries, drivers, etc. so install all those
$] sudo apt update  $] sudo apt upgrade
$] sudo apt-get install build-essential autoconf automake libxmu-dev python-pygoocanvas python-pygraphviz cvs mercurial bzr git cmake p7zip-full python-matplotlib python-tk python-dev python-kiwi python-gnome2 python-gnome2-desktop-dev python-rsvg qt4-dev-tools qt4-qmake qt4-qmake qt4-default gnuplot-x11 wireshark
The above command make take some time to download, compile and install it, Be Patient. 
Installing ns3 Download the…

TORA Protocol in NS-2.35 (NS2)

This post tells you how to enable the TORA (Temporally ordered routing Algorithm) protocol in Network Simulator 2 (ns-2.35)

TORA is a protocol in wireless adhoc networks that works with timing parameters. NS-2.35 comes with the TORA protocol by default but it has to be tweaked manually to make it run.
This post will help you to do that.

You can watch this video for detailed instructions:

Step 1: Generate a Scenario for TORA protoco using NS2 Scenario Generator NSG Software.
We have created a tcl file using NSG2.1.jar

$] java -jar NSG2.1.jar

Three files have to be modified
~ns-2.35/tora/ There are various websites that tells you how to configure TORA by making changes to the above three files.  Change 1: tora.h In the tora.h file, go to the end of the File before the agent completes, include these two lines

#include <classifier/classifier-port.h>
protected: PortClassifier *dmux_;

Change 2: Open the and include the follow…