Thursday, 25 July 2013

Printing Routing Table in NS-2


Step 1 : aodv.h
       Please search for aodv.h header file  under AODV folder in NS-2 directory.
Open it and search for rt_down( )  function
 
void rt_down(aodv_rt_entry *rt);  

void  routeTablePrint(nsaddr_t routeNode_id);        // Method added after rt_down();



    Step 2: aodv.cc

   Open the aodv.cc file and search for rt_down function to add  routeTablePrint() function below to it.

     void AODV::rt_down(aodv_rt_entry *rt)   

     void  AODV:: routeTablePrint(nsaddr_t routeNode_id)
 {
    FILE * routeFile;
    char routeFileName[50] = "AodvRoutingTable.txt"; //You can give your desired name

    routeFile= fopen(routeFileName, 'a');

    aodv_rt_entry *rt;

    fprintf(routeFile, "**************************************************");

    for (rt=rtable.head();rt; rt = rt->rt_link.le_next) {

        fprintf(routeFile, "NODE: %it %.4lft %it %it %it %it %it %.4lft %d n", node_id, CURRENT_TIME, rt->rt_dst, rt->rt_nexthop, rt->rt_hops, rt->rt_seqno, rt->rt_expire, rt->rt_flags)

    }

    fclose(routeFile);
}  //end of routeTablePrint

Step 3:  Usage

The routeTablePrint() function  in generic and can be used where ever you want to print the routing information  in AODV. For an instance, It can be used while route request generated node receives route reply message (RREP).

if (ih->daddr() == index) {  

    routeTablePrint(index); // print  routing table

    rt->rt_disc_latency[(unsigned char)rt->hist_indx] = (CURRENT_TIME - rp->rp_timestamp)
                                         / (double) rp->rp_hop_count;
    // increment indx for next time
    rt->hist_indx = (rt->hist_indx + 1) % MAX_HISTORY;
}



The same procedure will be followed for any routing protocol like AODV,DSR,PUMA etc.. .

Printing Routing Table in NS2


Step 1 : aodv.h
       Please search for aodv.h header file  under AODV folder in NS-2 directory.
Open it and search for rt_down( )  function
 
void rt_down(aodv_rt_entry *rt);  

void  routeTablePrint(nsaddr_t routeNode_id);        // Method added after rt_down();



    Step 2: aodv.cc

   Open the aodv.cc file and search for rt_down function to add  routeTablePrint() function below to it.

     void AODV::rt_down(aodv_rt_entry *rt)   

     void  AODV:: routeTablePrint(nsaddr_t routeNode_id)
 {
    FILE * routeFile;
    char routeFileName[50] = "AodvRoutingTable.txt"; //You can give your desired name

    routeFile= fopen(routeFileName, 'a');

    aodv_rt_entry *rt;

    fprintf(routeFile, "**************************************************");

    for (rt=rtable.head();rt; rt = rt->rt_link.le_next) {

        fprintf(routeFile, "NODE: %it %.4lft %it %it %it %it %it %.4lft %d n", node_id, CURRENT_TIME, rt->rt_dst, rt->rt_nexthop, rt->rt_hops, rt->rt_seqno, rt->rt_expire, rt->rt_flags)

    }

    fclose(routeFile);
}  //end of routeTablePrint

Step 3:  Usage

The routeTablePrint() function  in generic and can be used where ever you want to print the routing information  in AODV. For an instance, It can be used while route request generated node receives route reply message (RREP).

if (ih->daddr() == index) {  

    routeTablePrint(index); // print  routing table

    rt->rt_disc_latency[(unsigned char)rt->hist_indx] = (CURRENT_TIME - rp->rp_timestamp)
                                         / (double) rp->rp_hop_count;
    // increment indx for next time
    rt->hist_indx = (rt->hist_indx + 1) % MAX_HISTORY;
}



The same procedure will be followed for any routing protocol like AODV,DSR,PUMA etc.. .

Friday, 31 May 2013

AWK script not running in NS-2

HI ,
       These days i just identified that most of the NS-2 Users are facing problem with running AWK scripts and getting final simulation results.

Here are some common mistakes :

   1) Running with out generating the Trace file.
   2) Running with out testing the trace file format(old/new)
   3) Division by zero awk script error.
   4) Using New trace file with old awk script combination or Old trace file with New awk script-->This is the major mistake.

Solution:

    Before you proceed with applying awk scripts, Make sure which stage you are in currently.
In my view the stages are

Stage 1: creation of scenario(environmental setup) and creating TCL file.Important step check for new trace file or old trace file.....
  • If you want new trace file (contains energy and node position details) : put $ns use-newtrace in your TCL file. 
  • If you want old trace file format  : just comment the above command in your TCL file with #.

Stage 2:  Execution of TCL file and getting TRACE file (.tr file) . Generated with run the command  $ns example.tcl
Stage 3:  Download or modify the AWK scripts according to your scenario
Stage 4:  Use xgraph to generate graph associated to your results.

Checking Awk script for Old/New trace file format:

   AWK scripts are very useful to apply and execute trace file. important aspect is checking the column values in the awk script.

         AWK script for New trace file:   

  •   It contains more than 35 columns. which gives energy in joules, (x,y,z)axis location of node, send,receive, etc.

    •  So if you found any value like $16 or $34 or $23  means that the script is for new trace file . Use  when you need energy calculation apart from that pdf, jitter,throughput,delay else go with Old awk script.

           AWK script for Old trace file: 

  •   column values will be less than 10.   you can see like $5 ,$9  in the script.

Now please check the trace file whether it is old or new  and then apply associated AWK script..Search appropriate AWK script and use.

NOTE: 
        Please open trace file and now check how many columns are there. old trace file for example

 s     2.556838879    _0_     RTR      ---   0 AODV    48    [0 0 0 0] -------    [0:255 -1:255 30 0] 


 Columns in AWK script
-------------------------------
$1-->s
$2-->  2.556838879
$3-->_0_
$4--> RTR     


Hope this Post helps you folks

      

AWK scripts not running in NS2

HI ,
       These days i just identified that most of the NS2 Users are facing problem with running AWK scripts and getting final simulation results.

Here are some common mistakes :

   1) Running with out generating the Trace file.
   2) Running with out testing the trace file format(old/new)
   3) Division by zero awk script error.
   4) Using New trace file with old awk script combination or Old trace file with New awk script-->This is the major mistake.

Solution:

    Before you proceed with applying awk scripts, Make sure which stage you are in currently.
In my view the stages are

Stage 1: creation of scenario(environmental setup) and creating TCL file.Important step check for new trace file or old trace file.....
  • If you want new trace file (contains energy and node position details) : put $ns use-newtrace in your TCL file. 
  • If you want old trace file format  : just comment the above command in your TCL file with #.

Stage 2:  Execution of TCL file and getting TRACE file (.tr file) . Generated with run the command  $ns example.tcl
Stage 3:  Download or modify the AWK scripts according to your scenario
Stage 4:  Use xgraph to generate graph associated to your results.

Checking Awk script for Old/New trace file format:

   AWK scripts are very useful to apply and execute trace file. important aspect is checking the column values in the awk script.

         AWK script for New trace file:   

  •   It contains more than 35 columns. which gives energy in joules, (x,y,z)axis location of node, send,receive, etc.

    •  So if you found any value like $16 or $34 or $23  means that the script is for new trace file . Use  when you need energy calculation apart from that pdf, jitter,throughput,delay else go with Old awk script.

           AWK script for Old trace file: 

  •   column values will be less than 10.   you can see like $5 ,$9  in the script.

Now please check the trace file whether it is old or new  and then apply associated AWK script..Search appropriate AWK script and use.

NOTE: 
        Please open trace file and now check how many columns are there. old trace file for example

 s     2.556838879    _0_     RTR      ---   0 AODV    48    [0 0 0 0] -------    [0:255 -1:255 30 0] 


 Columns in AWK script
-------------------------------
$1-->s
$2-->  2.556838879
$3-->_0_
$4--> RTR     


Hope this Post helps you folks

      

Tuesday, 23 April 2013

Black Hole Attack Implementation in NS-2

Black Hole Attack

Black Hole Attack in Networking basically occurs when a node participates in data transmission act as forwarder .It receives data from the sender and replies it sent the data without sending it to receiver. it does not know any path , simply its goal to drop the packet data. In receive request function of .cc file there it directly reply the packet with highest sequence number to the source and the when data received it will drop these data at recv function of .cc file in the AODV.

Procedure for Creating malicious node to present Blackhole attack in AODV.


step 1:  Is for the tcl code, add this below line in your .tcl file
(tclfile)
#$ns_ at 0.0 “[$n2 set ragent_] malicious”

Below 2 would be at aodv.h in anywhere AODV class. add boolean variable BLACKHOLE in aodv.h

step 2 : (aodv.h)
   
bool     BLACKHOLE;

in 3 add the BLACKHOLE as false in aodv constructor

step 3 : (aodv.cc)

aodv::aodv(nsaddr_t id) : Agent(PT_aodvplain)
{
bid = 1;
  LIST_INIT(&nbhead);
  LIST_INIT(&bihead);
BLACKHOLE=false;
  logtarget = 0;
}

In 4, check malicious variable is set or not in tcl file.

step 4: (aodv.cc(AODV::command))
 
 if(strncasecmp(argv[1], "id", 2) == 0) {
      tcl.resultf("%d", index);
      return TCL_OK;
    }
    if(strcmp(argv[1], "malicious") == 0) {
        BLACKHOLE = true;
       return TCL_OK;
    }

                             

                            Download & Install




step 5 : Is for adding blackhole behaviour in node which drop the data packet.
(aodv.cc(recv))
 if(ch->ptype() == PT_aodvplain) {
   ih->ttl_ -= 1;
   recvaodvplain(p);
   return;
 }

if(BLACKHOLE)
{
//If destination address is itself
if ( (u_int32_t)ih->saddr() == index)
   forward((aodv_rt_entry*) 0, p, NO_DELAY);
else
    drop(p, DROP_RTR_ROUTE_LOOP);
}
else
{
 /*
  *  Must be a packet that originating...
  */
if((ih->saddr() == index) && (ch->num_forwards() == 0)) {
 /*
  * Add the IP Header
  */
   ch->size() += IP_HDR_LEN;

}


step 6:  Is for replying a request packet, in request packet use the maximum 32 bit number as sequence number.
(aodv.cc(recvreqeust))
 
   seqno = max(seqno, rq->rq_dst_seqno)+1;
   if (seqno%2) seqno++;
   if(BLACKHOLE)  seqno= rq->rq_dst=rq->rq_src=4294967295;

step 7: Please paste the else if  code  as shown below   

 else if(BLACKHOLE)
{
 sendReply(rq->rq_src,       // IP Destination
             1,         // Hop Count
             rq->rq_dst,     // Dest IP Address
             4294967295,        // Highest Dest Sequence Num that is largest 32-bit integers from -2147483647 to +2147483647
           MY_ROUTE_TIMEOUT,    // Lifetime
             rq->rq_timestamp); // timestamp

Packet::free(p);
}
/* Can't reply. So forward the  Route Request */
else
{
  ih->saddr() = index;
   ih->daddr() = IP_BROADCAST;
   rq->rq_hop_count += 1;
   // Maximum sequence number seen en route
   if (rt) rq->rq_dst_seqno = max(rt->rt_seqno, rq->rq_dst_seqno);
   forward((aodvplain_rt_entry*) 0, p, DELAY);
 }

credits to “http://elmurod.net/index.php/2009/10/24/adding-malicious-node-in-aodv/” and "http://narentada.com"

                          Download & Install

Black Hole Attack Implementation in NS2

Black Hole Attack in NS2

Black Hole Attack in Networking basically occurs when a node participates in data transmission act as forwarder .It receives data from the sender and replies it sent the data without sending it to receiver. it does not know any path , simply its goal to drop the packet data. In receive request function of .cc file there it directly reply the packet with highest sequence number to the source and the when data received it will drop these data at recv function of .cc file in the AODV.

Procedure for Creating malicious node to present Blackhole attack in AODV.


step 1:  Is for the tcl code, add this below line in your .tcl file
(tclfile)
#$ns_ at 0.0 “[$n2 set ragent_] malicious”

Below 2 would be at aodv.h in anywhere AODV class. add boolean variable BLACKHOLE in aodv.h

step 2 : (aodv.h)
   
bool     BLACKHOLE;

in 3 add the BLACKHOLE as false in aodv constructor

step 3 : (aodv.cc)

aodv::aodv(nsaddr_t id) : Agent(PT_aodvplain)
{
bid = 1;
  LIST_INIT(&nbhead);
  LIST_INIT(&bihead);
BLACKHOLE=false;
  logtarget = 0;
}

In 4, check malicious variable is set or not in tcl file.

step 4: (aodv.cc(AODV::command))
 
 if(strncasecmp(argv[1], "id", 2) == 0) {
      tcl.resultf("%d", index);
      return TCL_OK;
    }
    if(strcmp(argv[1], "malicious") == 0) {
        BLACKHOLE = true;
       return TCL_OK;
    }

                             

                            Download & Install




step 5 : Is for adding blackhole behaviour in node which drop the data packet.
(aodv.cc(recv))
 if(ch->ptype() == PT_aodvplain) {
   ih->ttl_ -= 1;
   recvaodvplain(p);
   return;
 }

if(BLACKHOLE)
{
//If destination address is itself
if ( (u_int32_t)ih->saddr() == index)
   forward((aodv_rt_entry*) 0, p, NO_DELAY);
else
    drop(p, DROP_RTR_ROUTE_LOOP);
}
else
{
 /*
  *  Must be a packet that originating...
  */
if((ih->saddr() == index) && (ch->num_forwards() == 0)) {
 /*
  * Add the IP Header
  */
   ch->size() += IP_HDR_LEN;

}


step 6:  Is for replying a request packet, in request packet use the maximum 32 bit number as sequence number.
(aodv.cc(recvreqeust))
 
   seqno = max(seqno, rq->rq_dst_seqno)+1;
   if (seqno%2) seqno++;
   if(BLACKHOLE)  seqno= rq->rq_dst=rq->rq_src=4294967295;

step 7: Please paste the else if  code  as shown below   

 else if(BLACKHOLE)
{
 sendReply(rq->rq_src,       // IP Destination
             1,         // Hop Count
             rq->rq_dst,     // Dest IP Address
             4294967295,        // Highest Dest Sequence Num that is largest 32-bit integers from -2147483647 to +2147483647
           MY_ROUTE_TIMEOUT,    // Lifetime
             rq->rq_timestamp); // timestamp

Packet::free(p);
}
/* Can't reply. So forward the  Route Request */
else
{
  ih->saddr() = index;
   ih->daddr() = IP_BROADCAST;
   rq->rq_hop_count += 1;
   // Maximum sequence number seen en route
   if (rt) rq->rq_dst_seqno = max(rt->rt_seqno, rq->rq_dst_seqno);
   forward((aodvplain_rt_entry*) 0, p, DELAY);
 }

credits to “http://elmurod.net/index.php/2009/10/24/adding-malicious-node-in-aodv/” and "http://narentada.com"


                          Download & Install

Thursday, 18 April 2013

How to avoid Hidden Terminal Problem in Ns-2

Hidden Terminal Problem


Consider a scenario of 3 nodes A,B,C . A and C are in the same transmission medium and they are hidden to each other due to the presence of B in between them.

A want to send some data to B. B is free and it accepts the data from A mean while C wants to send data to B. C does not  that A is already sending some data to B. Whenever C starts sending data to B, the data packets from both A and C get collide at B which results in data loss.

                                                      A ---> B <---C

One  Solution for above scenario

It can be solved using CTS(clear to send) /RTS(request to send)  but the load will be high as number of packets increase in the network.

                 A send RTS request packet  to B for  data transmission. once B receives it , it will send CTS to all  nodes in its transmission range. C came to know that B is receiving some data from some one and it  sleep for some time and try to send RTS to B for transmission of data.


Other Possible Solutions To avoid Hidden Terminal Problem in NS-2

1) Increase the Transmission power in ut tcl file.    set Rx_ "value" increase this power value.
2) Move the nodes in ur environment setup. moving nodes can sense the carriers easily  there by reduce the problem
3) Use Omni Directional Antenna instead of Directional in ur tcl fine under antenna set up.

How to avoid Hidden Terminal Problem in NS2

Hidden Terminal Problem


Consider a scenario of 3 nodes A,B,C . A and C are in the same transmission medium and they are hidden to each other due to the presence of B in between them.

A want to send some data to B. B is free and it accepts the data from A mean while C wants to send data to B. C does not  that A is already sending some data to B. Whenever C starts sending data to B, the data packets from both A and C get collide at B which results in data loss.

                                                      A ---> B <---C

One  Solution for above scenario

It can be solved using CTS(clear to send) /RTS(request to send)  but the load will be high as number of packets increase in the network.

                 A send RTS request packet  to B for  data transmission. once B receives it , it will send CTS to all  nodes in its transmission range. C came to know that B is receiving some data from some one and it  sleep for some time and try to send RTS to B for transmission of data.


Other Possible Solutions To avoid Hidden Terminal Problem in NS2

1) Increase the Transmission power in ut tcl file.    set Rx_ "value" increase this power value.
2) Move the nodes in ur environment setup. moving nodes can sense the carriers easily  there by reduce the problem
3) Use Omni Directional Antenna instead of Directional in ur tcl fine under antenna set up.

Saturday, 13 April 2013

AODV TCL file Downloads

Hi all,
             Here you can download all the files for AODV protocol with various group size.please find the below link to download all the files.
                               
                             

                              All Files Download & Installations

                           
                                                   AODV TCL script downloads : Click Here
     
       

AODV TCL file Downloads

Hi all,
             Here you can download all the files for AODV protocol with various group size.please find the below link to download all the files.
                               
                             

                              All Files Download & Installations

                           
                                                   AODV TCL script downloads : Click Here
   
     

Friday, 29 March 2013

Energy Model in Ns-2

The energy model is used through the node-config API. An example is shown as follows

      $ns_ node-config -adhocRouting DumbAgent \
-llType $opt(ll) \
-macType Mac/SMAC \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo_ \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-energyModel $opt(energymodel) \
-idlePower 1.0 \
-rxPower 1.0 \
-txPower 2.0 \
-sleepPower 0.001 \
-transitionPower 0.2 \
-transitionTime 0.005 \
-initialEnergy $opt(initialenergy)

The following parameters are newly added:
-sleepPower: power consumption (Watt) in sleep state
-transitionPower: power consumption (Watt) in state transition from sleep to idle (active)
-transitionTime: time (second) used in state transition from sleep to idle (active)



Analysis through Trace files


In addition to the total energy, now users will be able to see the energy consumption in different states at a given time. Following is an example from a trace file on energy.

 [energy 979.917000 ei 20.074 es 0.000 et 0.003 er 0.006]
The meaning of each item is as follows:
energy: total remaining energy
ei: energy consumption in IDLE state
es: energy consumption in SLEEP state
et: energy consumed in transmitting packets
er: energy consumed in receiving packets

Sample TCL script : Click Here

Energy Model in NS2

The energy model is used through the node-config API. An example is shown as follows

      $ns_ node-config -adhocRouting DumbAgent \
-llType $opt(ll) \
-macType Mac/SMAC \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo_ \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-energyModel $opt(energymodel) \
-idlePower 1.0 \
-rxPower 1.0 \
-txPower 2.0 \
-sleepPower 0.001 \
-transitionPower 0.2 \
-transitionTime 0.005 \
-initialEnergy $opt(initialenergy)

The following parameters are newly added:
-sleepPower: power consumption (Watt) in sleep state
-transitionPower: power consumption (Watt) in state transition from sleep to idle (active)
-transitionTime: time (second) used in state transition from sleep to idle (active)



Analysis through Trace files


In addition to the total energy, now users will be able to see the energy consumption in different states at a given time. Following is an example from a trace file on energy.

 [energy 979.917000 ei 20.074 es 0.000 et 0.003 er 0.006]
The meaning of each item is as follows:
energy: total remaining energy
ei: energy consumption in IDLE state
es: energy consumption in SLEEP state
et: energy consumed in transmitting packets
er: energy consumed in receiving packets

Sample TCL script : Click Here

Rate Control Protocol patch in Ns-2

RCP's patch for ns-2 is available on the above link for ns-2.28 and ns-2.30 versions.

Follow the steps given below to apply RCP's patch to ns-2.35:

1. Download ns-allinone-2.35.tar.gz

2. Download rcp-ns2-35.patch

3. Unzip ns-allinone-2.35.tar.gz. You will get a folder named ns-allinone-2.35

4. Paste the downloaded RCP patch in the above mentioned folder.

5. Give the following command:

patch -p1 < rcp-ns2-35.patch

6. Go in ns-allinone-2.35 via terminal and give the following command (It is always recommended to be in root mode while giving the below command):

./install

If you already have an installed copy of ns-allinone-2.35, then follow the steps given below to apply the RCP patch:

1. Paste the downloaded RCP patch in ns-allinone-2.35 directory.

2. Give the following command:

patch -p1 < rcp-ns2-35.patch

3. Go in ns-allinone-2.35/ns-2.35 directory and give the following commands:

./configure
make clean
make
make install

You are done with it!

Sample TCL Scripts:
Use the sample TCL Scripts provided on the official website of RCP. Following is the link:

http://yuba.stanford.edu/rcp/#implementation

rcp-ns-2.30.tar.gz must be downloaded from the above link. It contains a folder named "example-tcl-files" which contains the sample TCL Scripts.

A Note on the installation of ns-allinone-2.35:
You may or may not get installation errors depending on which version of gcc is used in your OS. With Ubuntu 10.04 (gcc 4.4.3), the installation procedure did not give any error. However, with later versions of Ubuntu there are chances that you may get a few errors.

Rate Control Protocol patch in NS2

RCP's patch for NS2 is available on the above link for NS-2.28 and NS-2.30 versions.

Follow the steps given below to apply RCP's patch to NS-2.35:

1. Download ns-allinone-2.35.tar.gz

2. Download rcp-ns2-35.patch

3. Unzip ns-allinone-2.35.tar.gz. You will get a folder named ns-allinone-2.35

4. Paste the downloaded RCP patch in the above mentioned folder.

5. Give the following command:

patch -p1 < rcp-ns2-35.patch

6. Go in ns-allinone-2.35 via terminal and give the following command (It is always recommended to be in root mode while giving the below command):

./install

If you already have an installed copy of ns-allinone-2.35, then follow the steps given below to apply the RCP patch:

1. Paste the downloaded RCP patch in ns-allinone-2.35 directory.

2. Give the following command:

patch -p1 < rcp-ns2-35.patch

3. Go in ns-allinone-2.35/ns-2.35 directory and give the following commands:

./configure
make clean
make
make install

You are done with it!

Sample TCL Scripts:
Use the sample TCL Scripts provided on the official website of RCP. Following is the link:

http://yuba.stanford.edu/rcp/#implementation

rcp-ns-2.30.tar.gz must be downloaded from the above link. It contains a folder named "example-tcl-files" which contains the sample TCL Scripts.

A Note on the installation of ns-allinone-2.35:
You may or may not get installation errors depending on which version of gcc is used in your OS. With Ubuntu 10.04 (gcc 4.4.3), the installation procedure did not give any error. However, with later versions of Ubuntu there are chances that you may get a few errors.

Ant Colony Optimization Patching to Ns-2

 

Ant Colony Installation in NS2