Wednesday 22 March 2017

How to print Routing table in AODV ns2

In this post we will discuss how we can print the routing table of each node in ns2 AODV routing protocol.

Step 1: First we need to write a function which prints the routing table contents,


void aodv_rtable::rt_print() { 

   aodv_rt_entry *rt = rthead.lh_first; 
   for(; rt; rt = rt->rt_link.le_next) 
   {
         printf("Destination : %d , No of Hops : %d , Next Hop : %d \n ",rt->rt_dst, rt->rt_hops , rt->rt_nexthop );
   }
      printf("Finished Printing Routing table");

 }


You need to add the above function to  ns-allinone-2.35/ns-2.35/aodv/aodv_rtable.cc
after rt_add() function in aodv_rtable.cc, 

Step-2 :  Add the function declaration to 
ns-allinone-2.35/ns-2.35/aodv/aodv_rtable.h, after line

 aodv_rt_entry* rt_lookup(nsaddr_t id); // this is rt_lookup
 void aodv_rtable::rt_print(); // Add this line to aodv_rtable.h 

Step 3:  Next we need to add the function call in ns-allinone-2.35/ns-2.35/aodv/aodv.cc,  wherever you require,

Suppose if you want to print the table when the rt_resolve function is called.
You can paste the below code inside rt_resolve function.

printf("Routing Table of %d" ,index );
rtable.rt_print();

Like below

void
AODV::rt_resolve(Packet *p) {
struct hdr_cmn *ch = HDR_CMN(p);
struct hdr_ip *ih = HDR_IP(p);
aodv_rt_entry *rt;

printf("Routing Table of %d" ,index );
rtable.rt_print();

 /*
  *  Set the transmit failure callback.  That
  *  won't change.
  */
 ch->xmit_failure_ = aodv_rt_failed_callback;
 ch->xmit_failure_data_ = (void*) this;
rt = rtable.rt_lookup(ih->daddr());

 if(rt == 0) {

once the files are modified and saved , type the following commands, suppose you installed ns2 inside home directory ns-allinone-2.35/ns-2.35/   folder then goto ns2.35 folder.


cd ns-allinone-2.35/ns-2.35/  
sudo make
sudo make install

Now run the tcl script which uses AODV as the Routing protocol. Now you will be able to see routing table in terminal.

If any errors please comment





Monday 20 March 2017

Introduction to ns2

Network Simulator (Version 2), widely known as NS2, is simply an event-driven
simulation tool that has proved useful in studying the dynamic nature of commu-
nication networks. Simulation of wired as well as wireless network functions and
protocols (e.g., routing algorithms, TCP, UDP) can be done using NS2. In general,
NS2 provides users with a way of specifying such network protocols and simulating
their corresponding behaviors.
     NS2 provides users with an
executable command “ns” which takes one input argument, the name of a Tcl
simulation scripting file. In most cases, a simulation trace file is created and is used
to plot graph and/or to create animation.
NS2 consists of two key languages: CCC and Object-oriented Tool Command
Language (OTcl). While the CCC defines the internal mechanism (i.e., a backend)
of the simulation, the OTcl sets up simulation by assembling and configuring the
objects as well as scheduling discrete events (i.e., a frontend). The CCC and the
OTcl are linked together using TclCL. Mapped to a CCC object, variables in
the OTcl domains are sometimes referred to as handles. Conceptually, a handle
is just a string (e.g., “_o10”) in the OTcl domain and does not contain any
functionality. Instead, the functionality (e.g., receiving a packet) is defined in the
mapped CCC object (e.g., of class Connector). In the OTcl domain, a handle
acts as a frontend which interacts with users and other OTcl objects. It may
define its own procedures and variables to facilitate the interaction. Note that the
member procedures and variables in the OTcl domain are called instance procedures
(instprocs) and instance variables (instvars), respectively

      After simulation, NS2 outputs either text-based simulation results. To interpret
these results graphically and interactively, tools such as NAM (Network AniMator)
and XGraph are used. To analyze a particular behavior of the network, users can
extract a relevant subset of text-based data and transform it to a more conceivable
presentation.

Installation of ns 2.35

Download ns2.35 all in one package

 We are going to install ns2.35 on ubuntu 14.04 or later versions.
 NS2.35 all in one package can be downloaded from http://sourceforge.net/projects/nsnam/files/latest/download
The package ns-allinone-2.35.tar.gz downloaded has to be moved to home directory( Home folder) /home/user_name

Extracting the package 

Open terminal and type the following commands,
cd ~/
tar -xvzf ns-allinone-2.35.tar.gz


Now All the files will be extracted into a folder called "ns-allinone-2.35" in home directory


Install Dependent packages for ns 2.35

Ns2 requires a few packages to be pre installed. It also requires the GCC- version 4.3 to work correctly. So install all of them by using the following command:
sudo apt-get install build-essential autoconf automake libxmu-dev
One of the dependencies mentioned is the compiler GCC-4.3, which is no longer available, and thus we have to install GCC-4.4 version. The version 4.4 is the oldest we can get. To do that, use the follwoing command:
sudo apt-get install gcc-4.4
Once the installation is over , we have to make a change in the "ls.h" file. Use the following steps to make the changes:
Navigate to the folder "linkstate", use the following command. Here it is assumed that the ns folder extracted is in the home folder of your system.
cd ~/ns-allinone-2.35/ns-2.35/linkstate
Now open the file named "ls.h" and scroll to the 137th line. In that change the word "error" to "this->error". The image below shows the line 137 (highlighted in the image below) after making the changes to the ls.h file.To open the file use the following command:
gedit ls.h
Now there is one more step that has to be done. We have to tell the ns which version of GCC will be used. To do so, go to your ns folder and type the following command:
sudo gedit ns-allinone-2.35/otcl-1.14/Makefile.in
In the file, change Change CC= @CC@ to CC=gcc-4.4 (NOTE : If you are using ubuntu 16.04 do not modify this )


Installation

Now we are ready to install ns2. To do so we first require root privileges and then we can run the install script. Use the following two commands:
cd ~/ns-allinone-2.35/
sudo ./install
Installation will take 10- 15 minutes, after successfull installation the last messages will tell you how to add path variable to execute ns2 correctly.
Adding the Path variables
The final step is to tell the system, where the files for ns2 are installed or present. To do that, we have to set the environment path using the ".bashrc" file. In that file, we need to add a few lines at the bottom. The things to be added are given below. But for the path indicated below, many of those lines have "/home/sharvin/ns-allinone-2.35/...." , but that is where I have my extracted folder. Make sure you replace them with your path. For example, if you have installed it in a folder "/home/abc", then replace "/home/akshay/ns-allinone-2.35/otcl-1.14" with "/home/abc/ns-allinone-2.35/otcl-1.14".
my home directory is /home/sharvin
to get your home directory, open terminal and type pwd , you get result like /home/sharvin/
so your user directory is sharvin.
Do this for all the required lines.
sudo gedit ~/.bashrc
Lines to be added:
# LD_LIBRARY_PATH
OTCL_LIB=/home/sharvin/ns-allinone-2.35/otcl-1.14
NS2_LIB=/home/sharvin/ns-allinone-2.35/lib
X11_LIB=/usr/X11R6/lib
USR_LOCAL_LIB=/usr/local/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OTCL_LIB:$NS2_LIB:$X11_LIB:$USR_LOCAL_LIB
# TCL_LIBRARY
TCL_LIB=/home/sharvin/ns-allinone-2.35/tcl8.5.10/library
USR_LIB=/usr/lib
export TCL_LIBRARY=$TCL_LIB:$USR_LIB
# PATH
XGRAPH=/home/sharvin/ns-allinone-2.35/bin:/home/sharvin/ns-allinone-2.35/tcl8.5.10/unix:/home/sharvin/ns-allinone-2.35/tk8.5.10/unix
#the above two lines beginning from xgraph and ending with unix should come on the same line
NS=/home/sharvin/ns-allinone-2.35/ns-2.35/ 
NAM=/home/sharvin/ns-allinone-2.35/nam-1.15/ 
PATH=$PATH:$XGRAPH:$NS:$NAM
Once the changes have been made, save the file and restart the system.
Running Ns2 
To check whether the installation was successfull type ns in terminal, if a % symbol appears , then the installation is successfully done 

Introductory post

Hi dear visitor,
    This Blog will be talking about network simulator 2.35( ns2.35 ). Ubuntu 14.04 will be the platform we will be using, so that it easy to get expertize on any errors. Initially we will discuss about installation and running of sample programs for wired and wireless network. later we will go to the addition and modification of Different protocols. I mainly concentrate on AODV routing protocol. The discussions and posts can help you to modify protocol to implement your own protocol for your research purposes. Thank you  all stay tuned with the site. For any queries regarding AODV routing protocol Contact me at sharvinarsh@gmail.com,