Skip to main content


Wired Cum Wireless Network Simulation - NS2 Tutorial 22

Today Session on
Wired Cum Wireless Networks using ns2.

You can watch the following lecture before understanding this lecture.
Watch Wireless Networks -
Watch Wired Networks -

Number of Wireles snodes is 7
Wired nodes - 2
Accesspoint or Base Stations - 2

Watch the full video for this example

this example is already avaialble in tcl/ex folder.
The same example I have taken but modified to my design...

How to generate Scenario and Traffic generation (connection Pattern)

To generate the connection pattern, here is the steps

Open the terminal and go to the folder
$] cd /home/pradeepkumar/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen
and run this command
$] ns cbrgen.tcl -type tcp -nn 7 -seed 1 -mc 20 -rate 0.001 

for 7 nodes, type of traffic is TCP, random seed is 1, maximum connection is 20 and the rate is 1ms or 0.001 and the output is sent to tcpgen.tcl


Ns2 and Ns3 Video Tutorials with Free Source Code

This post shows you the way to download, learn and watch source  codes and their working in ns3 and ns2.
Everyday thousands of visitor visits this blog and search for source code and explanation. Since 2011, the blog is delivering information across the globe.

Now this era is on Videos and online tutorials. nsnam is happily created a Video Channel called "Engineering Clinic" in Youtube and deliver video lectures with free source codes for all applications.

Subscribers of this blog are requested to subscribe to the video channel given below 

Here are some Playlists that are important to the viewers of this blog.

NS2 Tutorials -

NS3 Tutorials -

IoT Tutorials -

MOODLE Tutorials - 

Wormhole Attack in ns2 with full source code

Wormhole attack in ns2 - with full source code.

OS Used: Ubuntu 18.04 LTS
Ns2 version - 2.35

Just three files are to be modified


Download link for all the files .cc, .h and .tcl files

All these files are located in the ~ns-2.35/mac/

How to do?
Copy all the above three files in the mac/ folder and open the terminal

$] cd ns-allinone-2.35/ns-2.35

$] make 

You should not get any error. Once done, open the tcl files given in the folder ex-wormhole and run the examples and predict the performance as per the video given above.

What is wormhole?

How to download the files. I will give the copy of the above three files in my website

Very soon, I will move to github.

Subscribe to my youtube channel and share.

Explanation of Wormhole Attack

Hope you would have enjoyed this worm hole attack in ns2.

Please download the full source code from my website

If you need more informa…

How to add a Protocol in ns2 - NS2 Tutorial #13

How to add a new protocol in ns2

I am going to use the version ns-2.35
protoname was one of the best unicast routing protocol for ns2.


Protoname -> Simp


protoname -> simp

you can download the  source code from

There are totally 5 files in the source code converted to 

protoname.h -> simp.h 

protoname_pkt.h -> simp_pkt.h -> 

protoname_rtable.h -> simp_rtable.h

New Protocol Implementation

In this new protocol,  we have to

1. Add the source code (the 5 files given above)

2. existing code modification (within ns2)

3. Make an entry in the

4. Recompile ns2

5. Test the protocol using a TCL file.

Addition of source code is done.

Existing code modification - Very important.

in ns2, inforamtion about compilation is scattered... we need to make modifications at multiple locations. What are the files that are needed for modification.

all the file locations are he…

Blackhole attack in ns2 - NS2 Tutorial # 12

How to add a black hole attack in AODV protocol in ns2.  You can download the source codes. 
Black hole is the basic attack in a network, here we are going mitigate this attack in ns2.
Trust value, based on the trust value, if the value is less, then we can make it as an attacker node.
High trust, medium trust and low trust (integer)
Trusted or not (boolean)
To make a node attacker node (preferanly a black hole attack, how to do that?)
AODV protocol as it is easy to implement in ns2.

Step 1: Declare the attacker variable We will two files aodv.h and files  in aodv.h file,  create a variable name as given below in the protected scope

bool attacker;
Step 2: initialize the attacker variable as false. In file, initialize the attacker to be false inside the constructor.

Step 3: if a node is a blackhole, what happens to attacker value in the command() function of file inside the block  if(argc==2), include the following lines
if(strncasecmp(argv[1], "blackhol…

Finding Energy, node position and speed in AODV - NS2 Tutorial # 11.1

AODV - Part 2

Finding the node position, energy and speed of the node while it is forwarding a packet.

By default, these codes are not there in AODV, so we are going to modify or append in AODV protocol..

Step 1:

Open aodv.h and, these two files will be modified

Step 2:

Declare variables in aodv.h file in protected mode

double xpos,ypos,zpos,energy_t;

int n_speed;

MobileNode *t_node;

FILE *fp;

Step 3:

initialize these values inside the constrctor of file





Step 4:

add the following statements to the forward() function of

fprintf(fp,"Position is X, Y, Z, Node Speed,Energy\n");


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

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




AODV simulation in NS2 (Part 1) - NS2 Tutorial # 11

Lecture 11 - AODV - Part 1
Todays topic on AODV protocol

AODV - Adhoc Ondemand Distance Vector
the location of the source code for Aodv is in the folder


My suggestion is
Always try to understand the header file first.. later you can move on to source file (.cc files)
I will go through aodv.h file first and then file next....
let me explain....

I should know where my work is residing, for example in AODV

should I work on

Routing TableEnergy ManagementQuality oif ServiceLink FailureLogs or tracingNeighbour managementAttacks (Black hole, gray hole, etc...)Security
so prefer to stay at the header files first...
Now the action begins.
how do i modify the AODV protocol as per my small piece of algorithm...
I would like to print the nodes that are forwarding the packets.
Two information will be recorded in a text file separately..

Name of the file: aodv_output.dat data recorded will be: forwarded packet, node index (node number).

For this, please open the aodv.h and aod…