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