Saturday 29 June 2013

Source codes for Blackhole attack in wireless Adhoc Networks (Manets)

Part 1

 Instructions for Installing Ns2.34 in Ubuntu versions


Instructions for Installing Ns2.34 in Ubuntu 10.04
This manual will work for NS2 allinone-2.34 with Ubuntu 10.04. As Ubuntu is a new version 10.04 need following packages before any further installation, otherwise it will not work with it.
Please follow the following steps:
1.   Install necessary tools for building:
         sudo apt-get install build-essential autoconf automake libxmu-dev   gcc-4.3
2.   It turns out that otcl does not like the version of gcc installed by default in Lucid. So go ahead and change ns-allinone-2.34/otcl-1.13/Makefile.in
Find the Line That says
CC = @CC@
                                        and change it to:
CC = gcc-4.3
3.   Now follow the following steps for installation of nsallinone 2.34 and it will work with your machine. (Copy ns-allinone-2.34.tar.gz)
Suppose that there is "ns-allinone-2.33.tar.gz" in current directory (e.g. "~/" for me is "/home/ram").
All steps are done in Terminal, so open a terminal in Accessories/Terminal.
To extract the file:
tar xvfz ./ns-allinone-2.33.tar.gz
4. Change your directory within terminal as following way
$ cd /home/ram
     $ cd /home/ram/ns-allinone-2.34
4.   Download & install some packages from repository if needed
$ sudo apt-get install build-essential autoconf automake libxmu-dev
5.   Install the ns2
$ cd ns-allinone-2.34
$ ./install
    
    6. Edit some paths
         $ gedit ~/.bashrc
Put these lines on that file in the last. Off course, you might change
/home/ram for it depends on where you extract ns-allinone-2.34.tar.
# LD_LIBRARY_PATH
OTCL_LIB=/home/ram/ns-allinone-2.34/otcl-1.13
NS2_LIB=/home/ram/ns-allinone-2.34/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/ram/ns-allinone-2.34/tcl8.4.18/library
USR_LIB=/usr/lib
export TCL_LIBRARY=$TCL_LIB:$USR_LIB
# PATH
XGRAPH=/home/ram/ns-allinone-2.34/bin:/home/ram/ns-allinone-
2.34/tcl8.4.18/unix:/home/ram/ns-allinone-2.34/tk8.4.18/unix:/home/ram/ns-
allinone-2.34/xgraph-12.1/
NS=/home/ram/ns-allinone-2.34/ns-2.34/
NAM=/home/ram/ns-allinone-2.34/nam-1.13/
export PATH=$PATH:$XGRAPH:$NS:$NAM
7.   Validate it (take very long time compare to install process)
$ cd ns-2.33
         $ ./validate
If you have any doubts please comment.. i will try to solve it my level best. Thank you.
Testing ns2 Installation

Use the following ns batch command

set ns [new Simulator]
$ns at 1 “puts \“Hello World!\””
$ns at 1.5 “exit”
$ns run

Then,

1. Copy the commands above and paste in the text editor
2. Save the file as simple.tcl
3. Type command in command prompt

$ ns simple.tcl

4. It will show
Hello World in the line below
TroubleShooting

1. Problem : The system cannot find Tcl or it tells that Tcl does not install correctly

Solution 1 : Check your ns tcl nam ... version, check folder name. If it is not correct, edit the .bashrcSolution 2 : run ./install again

2. Problem : nam does not work

Solution 1 : In fact, you need to change that particular file in your Linux manually.

Here is what you should do.

Apply the following patch :

--- tk8.4.18-orig/generic/tkBind.c 2006-07-21 08:26:54.000000000 +0200
+++ tk8.4.18/generic/tkBind.c 2008-07-05 12:17:10.000000000 +0200
@@ -586,6 +586,9 @@
/* ColormapNotify */ COLORMAP,
/* ClientMessage */ 0,
/* MappingNotify */ 0,
+#ifdef GenericEvent
+ /* GenericEvent */ 0,
+#endif
/* VirtualEvent */ VIRTUAL,
/* Activate */ ACTIVATE,
/* Deactivate */ ACTIVATE,

Create a file with the above lines and name it : abc.patch. To apply the patch navigate to ns directory and execute the following command in terminal:

patch -b < abc.patch
Then run $ ./install to compile and install ns2 again. Hope this helps, drop in your comment for HELP ! :)

Part 2

Instructions for INSTALLING NS-2.29 ON FEDORA 13 Operating System

Pleasz U can follow these steps in any Linux environment

INSTALLING NS-2.29 ON FEDORA 13
STEP 1
From terminal login as super user and do the following.(internet must also be connected in order to install the package dependencies.

yum install autoconf
yum install automake
yum install gcc-c++
yum install libX11-devel
yum install xorg-x11-proto-devel
yum install libXt-devel
And for nam installation (optional):
yum install libXmu-devel


STEP 2
From terminal go to the folder where the ns-allinone patch is placed and type the following to extract
$ tar -xzf ns-allinone-2.31.tar.gz 
$ cd ns-allinone-2.29
$. /install


STEP 3


Now go to /etc folder and type

gedit ~/.bashrc

Add the following lines to the end of it. Remember replace "/your/path" by something like "/home/ram". And accordingly also change the version numbers. This is for ns 2.31.

# LD_LIBRARY_PATH
OTCL_LIB=/your/path/ns-allinone-2.31/otcl-1.13
NS2_LIB=/your/path/ns-allinone-2.31/lib
X11_LIB=/usr/X11R6/lib
USR_LOCAL_LIB=/usr/local/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OTCL_LIB:$NS2_LI B:$X11_LIB:$USR_LOCAL_LIB

# TCL_LIBRARY
TCL_LIB=/your/path/ns-allinone-2.31/tcl8.4.14/library
USR_LIB=/usr/lib
export TCL_LIBRARY=$TCL_LIB:$USR_LIB

# PATH
XGRAPH=/your/path/ns-allinone-2.31/bin:/your/path/ns-allinone-2.31/tcl8.4.14/unix:/your/path/ns-allinone-2.31/tk8.4.14/unix
NS=/your/path/ns-allinone-2.31/ns-2.31/
NAM=/your/path/ns-allinone-2.31/nam-1.13/
PATH=$PATH:$XGRAPH:$NS:$NAM


Let it take effect immediately

source ~/.bashrc


Note: the step described above is important; otherwise, you cannot run ns successfully.
or you can restart your X windows.i.e. logout and then login, or reboot your system, to make it work.)

Now, the installation has been completed. If you try:
$ ns

Then a "%" will appear on the screen.type "exit" to quit the mode and back to "$"


Errors You May Encounter During Installation of ns-2.29 in Fedora 13


If you miss out step 1 you may get the following errors

Build XGraph-12.1
================================================== ==========
loading cache ./config.cache
checking for a BSD compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking whether make sets ${MAKE}... yes
checking for working aclocal... missing
checking for working autoconf... missing
checking for working automake... missing
checking for working autoheader... missing
checking for working makeinfo... missing
checking if malloc debugging is wanted... no
checking for gcc... no
checking for cc... no
configure: error: no acceptable cc found in $PATH
make: *** No targets specified and no makefile found. Stop.
Can not create xgraph; But xgraph is an optional package, continuing...

If you miss step 3

At the prompt when you type ns you will get 
Ns command not found



Others Errors:

No .configure file found in current directory
Continuing with default options...
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
checking for gcc... gcc
checking for compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking how to run the C preprocessor... gcc -E
checking for egrep... grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for string.h... (cached) yes
checking for main in -lXbsd... no
checking for socket in -lsocket... no
checking for gethostbyname in -lnsl... yes
checking for dcgettext in -lintl... no
checking for getnodebyname in -ldnet_stub... no
checking that g++ can handle -O2... no
checking standard STL is available... no
checking for ranlib... ranlib
checking for snprintf... yes
checking for ANSI C header files... (cached) yes
checking for strtoq... yes
checking for strtoll... yes
checking for long... yes
checking size of long... 4
checking for __int64_t... no
checking for long long... yes
checking for int64_t... yes
checking which kind of 64-bit int to use... int64_t
checking for tcl.h... -I../include
checking for tclInt.h... -I../include
checking for libtcl8.4... -L../lib -ltcl8.4
checking for init.tcl... ../lib/tcl8.4
checking for http.tcl... ../lib/tcl8.4/http1.0
checking Tcl http.tcl library... yes
checking for tclsh8.4.11... no
checking for tclsh8.4... ../bin/tclsh8.4
checking for tk.h... -I../include
checking for libtk8.4... -L../lib -ltk8.4
checking for tk.tcl... ../lib/tk8.4
checking for otcl.h... -I../otcl-1.11
checking for libotcl1.11... -L../otcl-1.11 -lotcl
checking for X11 header files
checking for X11 library archive
checking for XOpenDisplay in -lX11... yes
checking for XShmAttach in -lXext... yes
No explicit static compilation flag; setting V_STATIC to ""
checking for dlopen in -ldl... yes
checking for a BSD-compatible install... /usr/bin/install -c
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
g++ -c -DNO_TK -DNDEBUG -DUSE_SHM -DHAVE_LIBOTCL1_11 -DHAVE_OTCL_H -DHAVE_LIBTK8_4 -DHAVE_TK_H -DHAVE_LIBTCL8_4 -DHAVE_TCLINT_H -DHAVE_TCL_H -DHAVE_CONFIG_H -I. -I/home/Sharath/Desktop/ns-allinone-2.29/otcl-1.11 -I/home/Sharath/Desktop/ns-allinone-2.29/include -I/home/Sharath/Desktop/ns-allinone-2.29/include -I/home/Sharath/Desktop/ns-allinone-2.29/include -o Tcl.o Tcl.cc
Tcl.cc: In member function ‘void Tcl::eval(char*)’:
Tcl.cc:182: warning: deprecated conversion from string constant to ‘char*’
Tcl.cc: In member function ‘int TclObject::traceVar(const char*, TclObject*)’:
Tcl.cc:421: warning: deprecated conversion from string constant to ‘char*’
Tcl.cc: In static member function ‘static int TclClass::create_shadow(void*, Tcl_Interp*, int, const char**)’:
Tcl.cc:509: warning: deprecated conversion from string constant to ‘char*’
Tcl.cc:511: warning: deprecated conversion from string constant to ‘char*’
Tcl.cc: In static member function ‘static int TclClass::dispatch_instvar(void*, Tcl_Interp*, int, const char**)’:
Tcl.cc:566: error: invalid conversion from ‘const char*’ to ‘char*’
Tcl.cc:571: warning: deprecated conversion from string constant to ‘char*’
Tcl.cc: In member function ‘virtual void TclClass::bind()’:
Tcl.cc:603: warning: deprecated conversion from string constant to ‘char*’
Tcl.cc:605: warning: deprecated conversion from string constant to ‘char*’
make: *** [Tcl.o] Error 1
tclcl-1.17 make failed! Exiting ...

If you face this type of error you have to follow below steps:

# yum install \
gcc-c++ compat-gcc-34-c++ automake autoconf libtool libX11-devel \
libXext-devel libXau-devel libXmu-devel xorg-x11-proto-devel

And for ns-allinone-2.29.3 : gcc, g++ style 3.4.6 / 4.1.x is required.
Either use : 'export CC=gcc34 CXX=g++34' or the Suse compat gcc41*,
whichever makes the best result for a perfect 'validate'.

The Suse gcc41* compat works perfect in Fedora 12 .. 14 :
http://download.opensuse.org/distrib...oss/suse/i586/
rpm -Uvh cpp41-4.1.2_20070115-16.1.i586.rpm
rpm -Uvh libmudflap41-4.1.2_20070115-16.1.i586.rpm
rpm -Uvh gcc41-4.1.2_20070115-16.1.i586.rpm
rpm -Uvh libstdc++41-devel-4.1.2_20070115-16.1.i586.rpm
rpm -Uvh gcc41-c++-4.1.2_20070115-16.1.i586.rpm


cd ns-allinone-2.29/tk8.4.11/
export CC=gcc-4.1 CXX=g++-4.1
patch -p0 < tk-8.4-lastevent.patch
cd .. && ./install

Good luck !


If You Face any Errors during Installation, Feel Free to comment. I will try my Level best to solve it.

Part 3

Adding a New Routing Protocol in Ns with black hole AODV example


"The Network Simulator"


Adding a new routing protocol
Writing routing protocol is fairly easy in NS2, but for beginners it seems very difficult. Therefore, if you are new to NS2 and want to write your own routing protocol, I would strongly recommend to revise AODV source code. Because, I believe AODV source code is straightforward and fairly easy to understand due to the simplicity of the AODV protocol.

Before you begin reading this post, I assume that you have already installed NS2 on Linux. I have used version 2.34, which is current release. If you have not installed yet, DOWNLOAD HERE and INSTALL. Okey, simple requirements to write your own routing protocol
  • NS2 installed
  • You should know how to program in C/C++.
  • Optionally, shell scripting and perl.
Let's start with creating directory of routing protocol.

--> You May Like to Read This

Download and put blackholeaodv folder in $NS_ROOT/ ns-2.34/". 
download them from clicking on the following link blackhole attack source file
(TO Download this file, You have to take a survey. Click on the Ace2three link or any other link and register yourself  there and Activate that link. Mean while don't close that survey window popped. else mail me I will send them to you.)
 I will not explain the code here, and if you don't understand just leave comment I will try to answer.

Now, we are going to modify following files. Therefore it is better you backup these files before you start adding new protocol, so that you can easily go back.


$NS_ROOT/Makefile
$NS_ROOT/queue/priqueue.cc
$NS_ROOT/common/packet.h
$NS_ROOT/trace/cmu-trace.h
$NS_ROOT/trace/cmu-trace.cc
$NS_ROOT/tcl/lib/ns-packet.tcl
$NS_ROOT/tcl/lib/ns-lib.tcl
$NS_ROOT/tcl/lib/ns-agent.tcl
$NS_ROOT/tcl/lib/ns-mobilenode.tcl

Let's start with ~/ns-allinone-2.34/ns-2.34/Makefile just add following lien at 269

blackholeaodv/blackholeaodv.o \

Add following lines to ~/ns-allinone-2.34/ns-2.34/queue/priqueue.cc from line 93.

// blackholeAODV patch
case PT_blackholeAODV:

To define new routing protocol packet type we have to modify ~/ns-allinone-2.34/ns-2.34/common/packet.h file.
We change PT_NTYPE to 63, and for our protocol PT_blackholeAODV = 62.
If you have already installed another routing protocol. Just make sure PT_NTYPE is last, and protocol number is ordered sequentially. From line 85 changes would be :

// blackholeAODV packet
static const packet_t PT_blackholeAODV = 62;
// insert new packet types here
static packet_t PT_NTYPE = 63; // This MUST be the LAST one

We make following code change at line 254 of ~/ns-allinone-2.34/ns-2.34/common/packet.h. 
The code is used that the packet is routing protocol packet and has high priority.

type == PT_AODV ||
type == PT_blackholeAODV)
And at line 390 of the same file

// blackholeAODV patch
name_[PT_blackholeAODV] = "blackholeAODV";

Now we will modify tcl files to create routing agent.
First we define protocol name to use in tcl file.
It would done by modifying ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-packet.tcl @ line 172

# blackholeAODV patch
blackholeAODV

Now we set routing agent by modifying ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-lib.tcl @ line 633

blackholeAODV {
set ragent [$self create-blackholeaodv-agent $node]
}

You May Like to Read This:

From line 860 of the same file following code should be added.

Simulator instproc create-blackholeaodv-agent { node } {
# Create blackholeAODV routing agent
set ragent [new Agent/blackholeAODV [$node node-addr]]
$self at 0.0 "$ragent start"
$node set ragent_ $ragent
return $ragent
}

--> Now we will set port numbers of routing agent. sport is source port, dport is destination port.
Modify ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-agent.tcl line 202

Agent/blackholeAODV instproc init args {
$self next $args
}
Agent/blackholeAODV set sport_ 0
Agent/blackholeAODV set dport_ 0

Frankly speaking I have no idea why I have to add following things. But I believe it should be done according to some tutorial :
 ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-mobilenode.tcl line 201

# Special processing for blackholeAODV
set blackholeaodvonly [string first "blackholeAODV" [$agent info class]]
if {$blackholeaodvonly != -1 } {
$agent if-queue [$self set ifq_(0)] ;# ifq between LL and MAC
}

We are done.
got to ~/ns-allinone-2.34/ns-2.34/ directory and do

make clean
make