Security Protocol (packet) in NS2

Security is not my area of work, but most of the places, I have been bombarded with questions like , is it possible to implement security protocol in ns2.
The answer is yes, here is the solution for that.  Here is the document to read
This work is done in 2005 and developed for ns version 2.26 or 2.27. I have done the compilation for ns-2.35
The files to be modified
1. Create a new agent called "Security_packet" and two files Security_packet.h and are created.
2. Add a new packet type in ~ns-2.35/common/packet.h
3. Add a new packet type in ~ns-2.35/tcl/lib/ns-default.tcl
4. Add a new packet type in ~ns-2.35/tcl/lib/ns-packet.tcl
5. modify the ~ns-2.35/

1. Download the two files here
2. Copy the and security.h file in ~ns-2.35/security folder (create the security/ folder and paste both the files)
3. Open ~ns-2.35/ and make an entry as shown below,

4. Open the ~ns-2.35/common/packet.h file
There will be two changes
Change one
static const packet_t PT_SECURITY_PACKET=74;
as shown below
and another change
name_[PT_SECURITY_PACKET]= "Security_packet";
as shown below


5. Open the file ~ns-2.35/tcl/lib/ns-default.tcl, and paste the following line at the end of the file
Agent/Security_packet set packetSize_ 0

See below
6.  open the file ~ns-2.35/tcl/lib/ns-packet.tcl, Input the following line 
as shown below

To Compile

Open the terminal, 
$] cd ~ns-allinone-2.35/ns-2.35/
$] ./configure
$] make

Run the security.tcl file using the ns command 
$] ns security.tcl

See the output, Since my area is not on the security, read the document/source code yourself

Thats it!!!

T S Pradeep Kumar

Adding a New Agent and Application in Network Simulator 2 (NS2)

 This post is reproduced from this website  But the above code is compiled for older ns2 version. But showcases the code for ns-2.35 version. You can read the theory about the agent from this weblink.
A Simple theory about Application and Agent
In Ns2, Nodes are individual entities where in...

Nodes -> Agent -> Application
Nodes is attached with an Agent and the Agent carries the Application. You can see the following image where Node is attached with the Agent and the Agent carries the FTP or CBR Application

Here is an Agent and Application is implemented
Agent - UDPmm
Application - mmApp

node -> Agent -> Application
To compile it 
Method 1: Easy method

Please download the code here
  • Copy the and in the -ns-2.35/multimedia directory
  • there is another folder called ns-modified-files/folder, copy the files which are there in to the corresponding location as specified below (Replace the existing files, Also take backup of your old files)
Following Files are modified

Then from the ns-2.35/folder 
$] ./configure && make
Test the testmm.tcl file and see the output, you can create any tcl file with udpmm as an agent and mmapp as application layer. 

Method 2: For advanced users...

  1. Copy "mm-app.h", "", "udp-mm.h" and "" to the "~ns-2.35/multimedia" directory.
  1. Add the entry to as shown below
multimedia/mm-app.o multimedia/udp-mm.o \
  1. Register the new application header by modifying "packet.h" (~ns-2.35/common/packet.h) and "ns-packet.tcl" (~ns-2.35/tcl/lib/ns-packet.tcl) as shown below.
Change the following lines to
// AOMDV packet

static const packet_t PT_AOMDV = 72;

// insert new packet types here
static packet_t PT_NTYPE = 73; // This MUST be the LAST one

// AOMDV packet

static const packet_t PT_AOMDV = 72;

static const packet_t PT_Multimedia = 73;
// insert new packet types here

static packet_t PT_NTYPE = 74; // This MUST be the LAST one

Change the following lines to
name_[PT_AOMDV]= "AOMDV";
name_[PT_NTYPE]= "undefined";

name_[PT_AOMDV]= "AOMDV";
name_[PT_NTYPE]= "undefined";

change the following lines in the ns-packet.tcl
# Other:

Encap # common/

IPinIP # IP encapsulation

HDLC # High Level Data Link Control

# Other:

Encap # common/

IPinIP # IP encapsulation

HDLC # High Level Data Link Control


  1. Add supportMM() and enableMM() methods to the "Agent" class in "agent.h" (~ns-2.35/common/agent.h) as shown below.

Add the two yellow colored lines in agent.h in Agent Class
class Agent : public Connector {



virtual void sendmsg(int sz, AppData*, const char* flags = 0);

virtual void send(int sz, AppData *data) { sendmsg(sz, data, 0); }

virtual int supportMM() { return 0; }
virtual void enableMM(){}

virtual void attachApp(Application* app);

virtual int& size() { return size_; }

  1. Add recv_msg() method to the "Application" class in "app.h" (~ns-2.35/apps/app.h) as shown below.

Add the yellow line to app.h in Application Class.
class Application : public Process {



virtual void send(int nbytes);

virtual void recv(int nbytes);

virtual void recv_msg(int nbytes, const char *msg=0){}

virtual void resume();

  1. Set default values for the newly introduced configurable parameters in "ns-default.tcl" as shown below.
# There are five rates in which the application layer is supporting..
Application/MmApp set rate0_ 0.3mb
Application/MmApp set rate1_ 0.6mb
Application/MmApp set rate2_ 0.9mb
Application/MmApp set rate3_ 1.2mb
Application/MmApp set rate4_ 1.5mb

Application/MmApp set pktsize_ 1000
Application/MmApp set random_ false

  1. Be SURE to complete this last step. Otherwise, all five-scale rates are initialized to zero unless specified in the input simulation script (i.e., the test simulation script given below will not transmit any frames).
  1. Go to ns-2.35 using shell prompt and enter the commands
  1. Run the file using “ns testmm.tcl” (this is to test your multimedia using applications) 

     Here is a sample output
    Multimedia Agent

 T S Pradeep Kumar

Tracegraph installation in Fedora 21

To install tracegraph (a software that handles the tracing automatically by specifying the trace file generated from ns2)

OS Used: Fedora 21 (64 bit)
Tracegraph version: 2.02

Download the tracegraph software from this link

Since tracegraph is a 32 bit software, you need to install all the 32bit libraries. The following packages that ends with .i686 are related to 32bit libraries

Steps to install
$] su
and press the root password
$] yum update
$] yum install libXext.i686 libXt.i686 libXp-devel.i686 
Still if tracegraph is not working, install these softwares too (this will also help in installing ns-2.35)
$] yum install tcl tk gcc-c++ libX11-devel libXt-devel libXmu-devel

To install Tracegraph
untar the software after copying it to your home folder (/home/pradeepkumar) in my case.
$] tar zxvf tracegraph202.tar.gz
$] cd tracegraph202
After the above step you need to set the LD_LIBRARY_PATH
Open the file /home/pradeepkumar/.bash_profile and give this line in the file
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/pradeepkumar/tracegraph202/bin/glnx86
and save the file and logout and login back.
$] ./trgraph 
This should pop out three windows, then tracegraph is installed successfully.

Since tracegraph is a very old software, there could be some bugs in the recent Operating Systems. So whenever, you open the tracefile, use the following command to open the files (Rather than using the File menu within tracegraph)

$] ./trgraph /path/to/


T S Pradeep Kumar

BonnMotion - A mobile scenario Generator Tool

BonnMotion is a tool that generates scenario for ns2, ns3, qualnet, etc. This will comfortably work with Linux. And in windows, there could be some bugs.

Please refer the documentation here:

To use this tool for ns2, here are the steps.
Step1 : Download the BonnMotion tool from this website
Step 2: Unzip it to home folder (/home/pradeepkumar in my case)
step 3: Here are the commands to install
$] unzip -d
$] cd bonnmotion-2.1.3/
$] ./install 
The above command will install, if any packages are missing for dependencies, please install it.

$] Once installed, copy the bonnmotion-2.1.3/bin/bm file to /usr/bin/ for running it anywhere. if not go to bin folder using the command given below

$] cd bonnmotion-2.1.3/bin/

To run BonnMotion
$bonnmotion-2.1.3/bin] ./bm -hm 
(this will list all the models)

$bonnmotion-2.1.3/bin] ./bm -hm RandomWaypoint
Will display all the parameters for RandomWaypoint like number of nodes, x, y, random seed, etc.

Assuming RandomWaypoint is the model

$bonnmotion-2.1.3/bin] ./bm -f scenario RandomWaypoint -n 10 -x 500 -y 500
The above command will create a file called scenario with 10 number of nodes with x and y as 500x500)

$bonnmotion-2.1.3/bin]  ./bm NSFile -f scenario
This will create two files called scenario.ns_movements, scenario.ns_params

The above two files can be used inside tcl script for ns2 using the source command

source scenario.ns_params
source scenario.ns_movements

(NB: Please note that the above two files represents nodes like $node_(0), etc and $ns_ as simulator object, either you change these variables within your tcl file or edit the above two files.  Also scenario.ns_params file shows the stop script as set val(duration) 600.0, but most of the tcl scripts may have val(stop), so change it to val(duration) in all cases) 

The same will be applied to other simulators also, refer the documentation given in the beginning of the slide.

Some screenshots for you.:

Installing Bonnmotion

bm command with options

A Sample Tcl file using the script generated by BonnMotion

Download the sample files here: put all the files in a single folder and run the tcl file using
$] ns ex.tcl

Provide your comments below, if you need any queries.

Credits to:

 T S Pradeep Kumar

Ant Colony Algorithm (antnet) in NS-2.35

The source code of antnet is taken from this website. If anyone claims as the owner of this software, let me know so that I will give credits to him/her.

(NB: This topic does not have any relevance to me and I have just reproduced what was available in the above website). And of Course, corrected some bugs. 

Important: These codes will work well with fresh installation of ns-allinone-2.35. If you have already compiled any other simulation in ns-2.35, kindly take a back up before you proceed.

These are the files to be modified. Download the entire folder here
rtable.txt (This file is created for my network with 2 nodes)
simpleant.tcl (This is the tcl file can be copied anywhere)
antnet_281_29.tar.gz (this will be original algorithm) 

Step 1: Download all the files

Step 2: Copy each file to the original location as given above (One main problem when you copy paste from the web is, some symbols like -, ", ->,_, are formatted automatically for web standards, but C programming or any programming language accepts only ASCII Codes. So whenever you copy paste codes in C programming, ensure that all symbols are of ASCII.  Else the compiler may throw as \364, etc stray program error.
(So all the codes that you download from my link are ASCII complaint codes, Yes I have modified, You can compile the codes with minimal effort)

Step 3: Unzip the antnet_281_29.tar.gz and paste the antnet/ folder to ..ns-2.35/
(This will be main algorithm, Open the code and understand)

Step 4: Open the Terminal and go to ns-2.35  (in my case it is /home/pradeepkumar/ns-allinone-2.35/ns-2.35/)
$]  cd /home/pradeepkumar/ns-allinone-2.35/ns-2.35/
$] ./configure 
$] make clean  (This command may be used if you have any problem in creating the .o files)
$] make 

Step 5: Run the simpleant.tcl using
$] ns simpleant.tcl 
This will create three files,
rtable.txt (The routing table of all the nodes)
simple.nam ( A network animation file)
simple.out (A Trace file)

Here is the sample output
contents of rtable.txt
Routing table at node 0
dest next phvalue
1 1 1.000000
2 1 1.000000
3 1 1.000000
4 1 1.000000
5 1 1.000000
6 1 1.000000
7 1 1.000000
8 1 1.000000
9 1 1.000000

Routing table at node 1
dest next phvalue
0 0 1.000000
2 0 1.000000
3 0 1.000000
4 0 1.000000
5 0 1.000000
6 0 1.000000
7 0 1.000000
8 0 1.000000
9 0 1.000000

Contents of Simple.out (First Few lines of the the Trace file)
+ 1 0 1 Ant 27 ------- 0 0.0 1.0 -1 8
- 1 0 1 Ant 27 ------- 0 0.0 1.0 -1 8
+ 1 1 0 Ant 27 ------- 0 1.0 0.0 -1 8
- 1 1 0 Ant 27 ------- 0 1.0 0.0 -1 8
+ 1.03 0 1 Ant 27 ------- 0 0.0 1.0 -1 3
- 1.03 0 1 Ant 27 ------- 0 0.0 1.0 -1 3
+ 1.03 1 0 Ant 27 ------- 0 1.0 0.0 -1 6
- 1.03 1 0 Ant 27 ------- 0 1.0 0.0 -1 6
T S Pradeep Kumar