Sunday 2 November 2014

How to place a Node in ns2 AODV (or) 

Random Distribution of nodes in ns2 (or) 

Creating a 100 nodes in ns2 AODV

Random Placement of Nodes in MANET:

            Placement of nodes in the topography can be static or uniform or random. In static method, we use a list of predefined values for nodes placement. But, in Random Distribution, nodes are placed randomly within the topography. Using rand() function, we can implement the random distribution of nodes placement in ns2. We can create any number of nodes using this method.

Code:

         > In tcl file,
                         for { set j 0 } { $j < $val(nn) } { incr j } {
                        set node_($j) [$ns node]
                        $node_($j) set X_ [expr {rand()*1000}]
                        $node_($j) set Y_ [expr {rand()*500}]
                        $node_($j) set Z_ [expr {0.0}]
                         }

         > The above code works for 1000 x 500 Topography.

Note :
           rand() * 1000 gives values between 0 to 1000.

Packet drop in aodv ns2 ?


     >  Packet drop can be done in two ways,

                i)  drop(p) or drop(p , DROP_RTR_NO_ROUTE)

                         >  In cmu-trace.h ( ns-2.35/trace ), you can find different kind of packet drop.

                ii)  Packet::free(p)

                         >  In packet.h ( ns-2.35/common ), you can find the free() function.

                         >  Note, free() is not the same as drop(). If you use free() function to drop a packet then you cannot see a packet drop in nam.

In AODV ns2 where can i find node's 

Position, Energy, Speed and Transmission range  ? 

>  In mobilenode.h ( ns-2.35/common ), you can find node's position ( X & Y ) and speed.

   >  In node.h ( ns-2.35/common ), you can find the energy of a node ( energy_model() -> energy() ).

   >  In god.h ( ns-2.35/mobile ), you can find Macro variable RANGE (Transmission Range).

I am back. Finally, I could make little time to write this post. I believe such information is available in elsewhere. But, I also believe explaining simple method of obtaining node position and current remaining energy would help a lot of people. I strongly suggest scanning the code of WFRP before reading the post further.

Alright, lets get on the work. First you have to include mobilenode.h (located in NSROOT/ns-2.xx/common/ ) into wfrp.h file. Add following line after #include <cmu-trace.h>
1#include <mobilenode.h>
We have to make little more modifications to wfrp.h, thus change the lines from 119 to 122 (wfrp.h) as below
1// Node Location
2double      posx;       // position x;
3double      posy;       // position y;
4 
5// Remaining Energy
6double      iEnergy;
At line 153 (wfrp.h) add following code
1// Energy Management
2void        update_energy();
3 
4// This node;
5MobileNode  *iNode;
Now we are going to update actual node position and energy. Add following code to line 147 of wfrp.cc :
1//initialize energy
2iEnergy = 0;
3 
4// Get pointer to the node
5iNode = (MobileNode *) (Node::get_node_by_address(index));

In above code get_node_by_address(nsaddr_t id) is located in $NSROOT/ns-2.xx/common/node.h, which obtains pointer to the node according to given address. The class Node contains various information about node, including energy model, location, speed, neighbors, etc.

Update the wfrp.cc code from the line 530 as below
1void
2WFRP::update_position() {
3 
4iNode->update_position();
5 
6posx = iNode->X();
7posy = iNode->Y();
8 
9#ifdef DEBUG
10printf("U (%.6f): UPDATE POSITION, for Node %d, X: %.4f  and Y : %.4f n", CURRENT_TIME, index, posx, posy);
11#endif
12 
13}
14 
15void
16WFRP:: update_energy() {
17iEnergy = iNode->energy_model()->energy();
18 
19#ifdef DEBUG
20printf("U (%.6f): UPDATE ENERGY, for Node %d, Energy %.4f n", CURRENT_TIME, index, iEnergy);
21#endif
22 
23}      

First routine (update_position) updates the position at request time and puts X and Y position to posx, posy respectively. Note, iNode->update_position() routine is in $NSROOT/ns-2.xx/common/mobilenode.cc. You may also obtain node’s current speed, radio range, and Z position.
Second routine (update_energy) gets nodes current remaining energy and puts it to iEnergy. For information refer to $NSROOT/ns-2.xx/mobile/energy-model.cc.
After completing modification just re-make. For location based routing protocols this might very useful. Leave comments here if you have any problems with the code.
Note : energyModel must be set in TCL file, in order to obtain node’s energy or energy model related information.

To enable Hello Packets in AODV ?

     > By default Hello Packet is not enabled. You have to enable it.

 Steps :
           i) In aodv.cc, comment the line as shown

                     //#ifndef AODV_LINK_LAYER_DETECTION
                       htimer.handle((Event*) 0);
                       ntimer.handle((Event*) 0);
                     //#endif // LINK LAYER DETECTION


 How can i change the Hello interval ?

     >  By default hello interval set to 1 second ( 1000 milli sec )

      >  In aodv.h, Macro variable HELLO_INTERVAL can be found.
 
Expected Questions
how to disable data packet ack from all intermediate nodes in aodv
Do not use TCP protocol, use UDP.

how to mointor whether intermediate node send data packets to next intermediate node in aodv ,with out using ack 
thats where tap function plays the role
 
 

For using promiscuous mode in AODV NS2

Intro:-

          In Promiscuous mode each node in the network listens the packets transmitted by its own neighbor nodes. 
          In NS-2, some time we have to calculate the packet drops or analyze the process. For the same we need to set the network to operate in promiscuous mode. Here, I am going to explain how we can set our wireless network in promiscuous mode with AODV as Routing protocol using NS2 simulator.

1) We need to modify in total 3 files, so it's good to take a backup of it.
    Files are:
    • ns-allinone-2.34/ns-2.34/aodv/aodv.cc
    • ns-allinone-2.34/ns-2.34/aodv/aodv.h
    • ns-allinone-2.34/ns-2.34/tcl/lib/ns-mobilenode.tcl
2) Open the file ns-allinone-2.34/ns-2.34/aodv/aodv.h in your favorite editor
    and make the changes as shown in blue color.
  #include <mac.h>
  class AODV: public Tap, public Agent {
  public:
  void tap(const Packet *p);
  ......
  protected:
  Mac *mac_;
  ......
 }
3) Open the file ns-allinone-2.34/ns-2.34/aodv/aodv.cc and make the changes as  
    shown in blue color.
int
AODV::command(int argc, const char* const * argv) {
......
else if(argc == 3) {
......
else if (strcmp(argv[1], "install-tap") == 0) {
mac_ = (Mac*)TclObject::lookup(argv[2]);
if (mac_ == 0) return TCL_ERROR;
mac_->installTap(this);
return TCL_OK;
}
}
return Agent::command(argc, argv);

}
void
AODV::tap(const Packet *p) {
// put your code here
   
4) Open the file ns-allinone-2.34/ns-2.34/tcl/lib/ns-mobilenode.tcl and make the  
    changes as shown in blue color.

Node/MobileNode instproc add-target { agent port } {
$self instvar dmux_ imep_ toraDebug_ mac_
......
# Special processing for AODV
set aodvonly [string first "AODV" [$agent info class]]
if {$aodvonly != -1 } {
$agent if-queue [$self set ifq_(0)] ;
# ifq between LL and MAC

$agent install-tap $mac_(0)
......

}


Explanation:-

         The above procedure will put all the nodes in the wireless network in promiscuous mode. If a node A wants to listen the packets from a node B in promiscuous mode to check whether to check B is forwarding the packets which are sent by A to B or not we can write the code in function tap()

5) To simulate a node as a malicious node in AODV see below (not yours but blogs)

Many people have asked me how to implement malicious drop in AODV. I have decided to write simple code for adding malicious node in AODV ( or in any routing protocol).
First you need to modify aodv.cc and aodv.h files. In aodv.h after
1/*  The Routing Agent */
2class AODV: public Agent {
3...
4/*
5* History management
6*/
7double      PerHopTime(aodv_rt_entry *rt);
8...
add following line
1bool     malicious;
With this variable we are trying to define if the node is malicious or not. In aodv.cc after
1/*
2  Constructor
3*/
4AODV::AODV(nsaddr_t id) : Agent(PT_AODV),btimer(this), htimer(this), ntimer(this), rtimer(this), lrtimer(this), rqueue() {
5index = id;
6seqno = 2;
7bid = 1;
8...
add following line
1malicious = false;
The above code is needed to initialize, and all nodes are initially not malicious. Then we will write a code to catch which node is set as malicious. In aodv.cc after
1if(argc == 2) {
2  Tcl& tcl = Tcl::instance();
3 
4    if(strncasecmp(argv[1], "id", 2) == 0) {
5      tcl.resultf("%d", index);
6      return TCL_OK;
7    }
add following line
1if(strcmp(argv[1], "hacker") == 0) {
2    malicious = true;
3   return TCL_OK;
4}
Now we will do some work in TCL to set a malicious node. Using script in my post , we add following line to set node 5 as malicious node.
1$ns at 0.0 "[$mnode_(5) set ragent_] hacker"
You may add this line after
1for {set i 0} {$i &lt; $val(nn)} { incr i } {
2$ns initial_node_pos $mnode_($i) 10
3}
4...
Alright, we have set malicious node but we did not tell malicious node what to do. As it is known, rt_resolve(Packet *p) function is used to select next hop node when routing data packets. So, we tell malicious node just drop any packet when it receives. To do that after
1/*
2Route Handling Functions
3*/
4void
5AODV::rt_resolve(Packet *p) {
6struct hdr_cmn *ch = HDR_CMN(p);
7struct hdr_ip *ih = HDR_IP(p);
8aodv_rt_entry *rt;
9...

We add a few lines
1// if I am malicious node
2 if (malicious == true ) {
3    drop(p, DROP_RTR_ROUTE_LOOP);
4    // DROP_RTR_ROUTE_LOOP is added for no reason.
5 }
And implementing malicious node is done. I hope the post will be helpful to design your secure routing protocol.
P.S. Guys please don’t ask me c/c++ questions,

 

How can i monitor my neighbor nodes in aodv 

(Promiscuous mode in aodv) <<In the previous post?


            >  Using tap function we can  monitor the neighbor nodes transmission.

            >  If a node forwards a packet then tap function will be called for all of its neighbors. Using this functionality we can easily find out packet drop.

            >  tap function can be found in mac.h, dsragent.h, dsragent.cc.

         Steps :

            >   We need to modify 3 files
                        i)  aodv.h
                        ii) aodv.cc
                        iii) ns-mobilenode.tcl ( can be found under ns-2.35/tcl/lib )

            >   In  aodv.h

                       i) Include the header file
                               #include <mac.h>
                       ii)
                           /*
                           The Routing Agent
                          */
                         class AODV: public Tap, public Agent
                        {
                          .......
                          public:
                                ........
                                void tap(const Packet *p); 
                                ........
                          protected:
                              Mac *mac_;
                                .........
                         }


            >   In aodv.cc,

                   int AODV::command(int argc, const char* const * argv )
                   {
                         .......
                        elseif( argc==3 ) {
                        ........
                       else if (strcmp(argv[1], "install-tap") == 0)
                       { 
                          mac_ = (Mac*)TclObject::lookup(argv[2]);
                          if (mac_ == 0)
                         return TCL_ERROR;
                          mac_->installTap(this);
                          return TCL_OK;
                        }
                       }
                       return Agent::command(argc, argv);
                    }
    
                     void AODV::tap(const Packet *p)
                    {
                           // Write your own code here
                    }


         >  In ns-mobilenode.tcl,

                   Node/MobileNode instproc add-target { agent port }
                   {
                        $self instvar dmux_ imep_ toraDebug_ mac_ 
                        .........
                        # Special processing for AODV
                       set aodvonly [string first "AODV" [$agent info class]]
                       if {$aodvonly != -1 }
                        {
                       $agent if-queue [$self set ifq_(0)]   ;# ifq between LL and MAC
                        $agent install-tap $mac_(0)
                         }
                         .........
                    }

Possible Questions
Sir,what code have to write in void AODV::tap(const Packet *p) to monitor two nodes by one node?

Solution
It depends.first of all, if you want to monitor another node then it should be in your transmission range. if you only want a node to monitor every other node, then you should write code like that using node id(i.e index)

// let's say you are counting the no of packets dropped by node 2, which lies transmission range of node 10. For that, you need to modify two function i) forward fn ii) tap fn. In forward fn, node 10 should increase the count on node 2 by 1. In tap fn, node 10 should decrease the count on node 2 by one. 


If tap fn called then no drop, else the drop count will be one. like this, you need to develop your code to monitor your neighbor nodes.

Possible Question
how to find malicious node in a network 
there are several ways to find a malicious node in a network. The popular one is based on trust value. Using trust also, we have several way. For more info study ieee, acm.

Possible Question
After all node set as promiscuous mode, node 1 wants to monitor its neighbor nodes {2,3,4} whether they are forwarding its (node1)packets or not. For this, how to write code in tap( ) function? 
Solution there are many ways , we can do that. Here is a way, in forward function, before forwarding, say node 2 forwards, node 1 should increase the count of no of packets fwded by node 2 and in tab fun node 1 should decrease the count of no of packets fwded by node 2. If the count greater than zero or some threshold, you may want to include something for congestion then pkt drop happened. hope it helps.  

Questions
Want to know neighbor list of every node.

In AODV, by sending hello messages every node updates its neighbour list. Start with sending hello messages.


Question
how can a neighbor node can monitor a node that either it is sending the data packet to its next intermediate node in the route or to the destination.for this how the "tap" function should be implemented or what sort of changes should be done in this function to monitor the neighboring nodes.remember that this work is going on in AODV.kindly help us in this matter.thanks.

Solution it follows the route. In general, if a node has dest node as neighbour node then it directly forwards the packet, otherwise it uses the intermediate node. Implementation of tab function is based on what you want by monitoring. In general, for packet drop calc you can inc the count in forward fun and decrease it in tab. Hope this helps. If you need more about tab fun do some digging in the code.  
 

Ns2 installation To set path in ns2 (or) Why i can't see the modification done in aodv.cc ?


Installation Steps:

                         i)   Open a terminal, type  sudo apt-get install build-essential autoconf automake libxmu-dev

                        ii)   Download the ns-allinone-2.35.tar.gz file here.

                        iii)  Extract the file and goto ns-allinone-2.35 folder

                       iv)  In terminal, type ./install

                       v)   At the end of ./install, you will be asked to set path. Copy the Path information in local file.

   
After ns2 installation you have to set path. For that

 
                           i) Goto the ns2 folder  (or) The folder where you have ns-allinone-2.35
                         
                           ii) gedit  .bashrc

                                      export PATH=$PATH:<path1>:<path2>:<path3>

                                      export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path1>:<path2>

                                      export TCL_LIBRARY=$TCL_LIBRARY:<path>

                          For  e.g) .bashrc file ( http://ubuntuone.com/4TzcPf6PLfihJbogRmyxsO )

                          iii) source .bashrc

                          iv) now type ns, you will get % ( successful installation )

For basic start with this guide 

Very Important How to compile after editing aodv files ?


In cd ns-2.35 folder

     i) ./configure  ( optional )

     ii) make clean

     iii) make

    iv) make install or  sudo make install



Possible Error
when i do step ii ..when it completes this error come :
g++ -Wl,-export-dynamic -o common/ptypes2tcl common/ptypes2tcl.o
./common/ptypes2tcl > gen/ptypes.cc
/bin/sh: 1: cannot create gen/ptypes.cc: Permission denied
make: *** [gen/ptypes.cc] Error 2

 I am assuming, you have solved the error as  mentioned. (FYI - login as root then do all the compilation if you have denied of permission)

Steps to create my own packets in aodv ns2 ?

Create your own packet :

           >  In aodv, sometimes we need to get/pass our own information  to neighbors.  Like RouteRequest( RREQ ), RouteReply( RREP ) , we can easily get/pass our own information.
           >  e.g  
                       i)   CH may wants to know the trust value of its neighbors.
                       ii)  Passing Voting information.
           >  We need to modify four files
                        i)   aodv-packet.h , where you will define your own packet structure.
                        ii)  aodv.cc , where you will define your own function. We need two receive function for  receiving request and reply. Also, we need two other functions to send request and reply.
                       iii)  In aodv.h , include the function declaration.     
                       iv)  In ns-2.35/trace/cmu-trace.cc , We need to add tracing information otherwise, we will get a "invalid AODV packet type" error.

how to add tracing information of new packet into cmu-trace.cc. Can u give an example.
Give it a try, this is the place where you will add new packets tracing info...
void
CMUTrace::format_aodv(Packet *p, int offset)
{
}
hope, it helps. If you really need an example, leav a comment