19 March 2014

Printing Routing Table in AODV: NS2

There are no automatic provisioning to print a routing table for a given node in ns2. However, using the tracefile generated using ns2, routing table can be printed, but one should be good enough in processing a file using awk, python or any other scripts.

The following code will make you print the routing table when you are using AODV Protocol.

Files to be modified:
~ns-2.35/aodv/aodv.h
~ns-2.35/aodv/aodv.cc

The RED colored code indicates the addition of these lines in to the existing codes.

The following line can be added to the aodv.h file inside the class AODV in the protected scope.  probably add the line after the rt_down() function.

void rt_print(nsaddr_t nodeid);

In the aodv.cc file, add the following entry anywhere, but probably after the end of rt_down() function. 
void  AODV::rt_print(nsaddr_t nodeid) {
FILE *fp;
fp = fopen("route_table.txt", "a");
aodv_rt_entry *rt;
for (rt=rtable.head();rt; rt = rt->rt_link.le_next) {
fprintf(fp, "NODE: %i %f %i %i %i %i %i %f %d \n", nodeid, CURRENT_TIME, rt->rt_dst, rt->rt_nexthop, rt->rt_hops, rt->rt_seqno, rt->rt_expire, rt->rt_flags);
}
fclose(fp);
}

The above function records the routing table entry in to a file called route_table.txt. 
The above code 
  • creates a file pointer inturn a file called route_table.txt under append mode.
  • it creates an pointer to routing table entry. 
  • processes from the head of the queue to the last and print these information to the file with pointer fp and 
  • finally close the file pointer. 
Finally the rt_print() function have to be called. This can be called from anywhere. Here is the function call in aodv.cc file. 
It is inside the void AODV::recvReply(Packet *p) {

if (ih->daddr() == index) { // If I am the original source
  // Update the route discovery latency statistics
  // rp->rp_timestamp is the time of request origination
    rt_print(index); // print this nodes whole 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;
  } 

Once the above steps are done.  Open the terminal and go to the ~ns-2.35. folder and type the following commands one by one
prompt$] make
Once the compilation is successful, test any of the wireless tcl files with AODV protocol is the routing and run it using ns filename.tcl
If you need a testing file, download here
Here is the sample screenshot of the routing table
routing table
Print Routing Table in AODV

This post is taken and modified from www.elmurod.net
T S Pradeep Kumar