Showing posts with label ns. Show all posts
Showing posts with label ns. Show all posts

Sunday 2 November 2014

MannaSim installation procedure for generating Script 

Script generator framework

MannaSim framework is the Script generator used mainly for sensor network research.

Note: After installing ns2, this procedure must be undergone.

Step 1: Download Manasim.tar.gz file
 
Step 2: untar the file by typing
            tar zxvf manasim.tar.gz
 
Step 3: A new folder titles manasim with appear. copy that file and paste it in /home/usernname/ns2.35-allinone/ns.2.35/
 
Step 4: Open the mansim folder. Then, go to NS-modified-files folder. There you can find a few files.


Copy the files from the ns-modified-files/ to these locations
ns-allinone-2.35/ns-2.35/apps/udp.cc
ns-allinone-2.35/ns-2.35/common/ns-process.h
ns-allinone-2.35/ns-2.35/common/packet.cc
ns-allinone-2.35/ns-2.35/common/packet.h
ns-allinone-2.35/ns-2.35/Makefile.in
ns-allinone-2.35/ns-2.35/tcl/lib/ns-default.tcl
ns-allinone-2.35/ns-2.35/tcl/lib/ns-lib.tcl
 
Step 5:Then, go to /home/username/ns2.35-allinone/ns2.35
Then, type each command at terminal
           ./configure
           make

Step 6: Java installation procedure

            sudo apt-get install java
            sudo apt-get install openjdk-7-jdk

Tutorial on TCL for Begineers

Firstly, we should get ns or tcl shell installed to work with tcl script. 
 
If you didn't install ns2, for the installation procedure. Else, follow the tutorial. 
After installing ns-2, type ns in terminal. Then, there appears % symbol, which symbolizes the invokement of tcl shell script.
 
Note(optional): TCL scripting individually can be practiced with in Linux system with tcl shell scripting.


Introduction: 
TCL is an abbreviation for Tool Command Language. Tcl is a string-based command (script) language, with simple syntax. Tcl is designed to be a glue that assembles software building blocks into applications.The latest version of TCL is 8.8.2. 

The goal of this tutorial is to introduce the learner to TCL scripting and make comfortable to do scripting required for ns2 network simulations. It covers working with variables, lists, arrays, control structures and other core features, in TCL. The tutorial was created on Ubuntu Linux, but the users can practice TCL scripting individually in WINDOWS by downloading ActiveTCL.


Working with TCL:

set – command to assign a value to a variable.

set variable_name value
set u 10

set variable_name2 $variable_name
set c $u

# - Symbol used for line comment. Tcl interpreter will not execute that line. There isn’t any paragraph commenter in tcl/otcl scripting.

expr – command to invoke any mathematical operation.
set variable_name [expr $var1+$var2]
set d [expr $b + $c]

puts – Command to print output to the console.
puts 10
puts d
puts $d
puts d is different from puts $d

In Tcl, there are no variable types. So, a variable can be a string or integer depending on value you assign to it. Also, the typecasting rules also apply here.
puts [expr 1/2]
puts [expr 1.0/2]
puts [expr 1/2.0]
puts [expr 1.0/2.0]

/  operator is used for division. \ operator is not defined in tcl. NOTE: spaces are not important
expr 5*3
expr 5    *     3

Both these statements, result the same answer. But, spaces are not preferable for good programming.

NOTE:
Dividing by zero
expr 1/0
Interpreter shows "DIVIDE BY ZERO" error.
This can't be done by a programming language

Working with strings, in TCL:

set e “udhay prakash”;
set f {udhayprakash.blogspot.in};
puts $e
puts “udhay prakash”
puts $f
puts “udhayprakash.blogspot.in”

Working with files, in TCL:
·         To create and write in a new file, with file1 as the filename: set file1 [open filename w]; 
·         To read an already existing file, with file2 as the filename:   set file2 [open filename r];
·         To print data into a file, with file2 as filename:                                   puts $file2 “text”;

Conditional Statements:
Syntax:
if {expression}
{          <execute some commands>
} else {
            <execute some commands>
}

Example:
set a 10;
set b 20;
if($a!=$b){
puts “$a!=$b”;
}else{
puts “$a=$b”;
}

Loop Statements:
for loop syntax:
for{initialization}{condition}{increment/decrement}
{ <execute some commands>
}

for loop example:
set i 0
for{set i 0}{$i<5}{incr i}{
puts “$i/t”
puts [expr $i/t]
}

while loop syntax:
while {condition}{
<execute some commands>
<increment/decrement looping variable>
}

while loop example:
set m 10
set n 2
while{$m!=0}{
puts {$m\n}
incr m -1
}

do-while loop syntax:
do{
<execute some commands>
<increment/decrement looping variable>
}while{condition};
do-while loop example:
set m 10
set n 2
do{
puts {$m\n}
incr m -1
}while{m};
return – command to return a value from procedure
continue – command to bypass a loop in a procedure
break – command to come out of a procedure
proc – command to create a procedure(function). The procedure receives some parameters that can be objects, files or variables.

Procedure syntax:
 proc procedure{in_par1, in_par2, …..}{
            global variable_list, file_list
            <execute some commands>
             return $something
            }
 
Procedure Example1:
proc factorial {j} {
for {set result 1} {$j > 1} {set j [expr $j ­1]} {
set result [expr $result * $j]
}
 
Procedure Example2:
#create a procedure
proc test{}{
set a 4
set b 2
set c [expr $a+$b]
set d [expr [expr $a-$b]*$c]
puts “c=$c, d=$d”
for {set k 0}{$k<10}{incr k}
if {$k<5}{
puts “k<5,pow=[expr pow($d,$k)]”
} else{
puts “k>=5,mod=[expr $d%$k]”
}
}}
Calling a Procedure:
Syntax: procedure_name
Example: test

NS 2 - Tutorial for beginners 1

Explanation for a Simple Script

To start we have to set a few variables like simulator object, trace file and object, nam file and object.

set ns [new Simulator]
This would set the simulator with the simulator object which is to be accessed in the script.

set nf [open out.nam w]
$ns namtrace-all $nf
This would set the nam file (network animation file) with the object and connect to ns.
set tr [open out.tr w]
$ns trace-all $tr
This would set the trace file and would connect to the simulator. The trace file is required to analyze the various packets which are send, received type of application used etc.


set n0 [$ns node]
Now the nodes could be set as many as you want, for loop could be used if many nodes are to be made.
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
The connection for the various nodes with each other with the band width and rate.
$ns duplex-link-op $n0 $n1 orient right-up
The nodes could be given with various orientations with this option. right, right-up and right down could be used depending on the node.
For the application like tcp or udp to run, we need to set two agents and the application which should run in between.
When using tcp, we have ftp as the application and tcpsink as the end agent. connection must be made between tcp and tcpsink, same in udp with cbr and null respectively.
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
This would make a tcp agent and connect it with the node.
set ftp [new Application/FTP]
$ftp attach-agent $tcp

Now the ftp is connected with the tcp

set agent [new Agent/TCPSink]
$ns attach-agent $n3 $sink
Now the tcpsink is set to a node where the tcp packets are received.
The tcp and sink (agents) needs to be connected, such that the network flows.
$ns connect $tcp $sink
Similarly, for udp,
set udp [new Agent/UDP]
$ns attach-agent $n2 $udp

set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $n3 $null

$ns connect $udp $null
We can use the routing protocols in the simulator using rtmodel (to break the link), rtproto (to use the protocol)
$ns rtmodel-at 1.0 down $n1 $n2
$ns rtmodel-at 2.0 up $n1 $n3
For distance vector we could use
$ns rtproto DV
For linkstate we could use
$ns rtproto LS
When all this is done the TCP could be started at some point and could call the finish procedure to end. 
The out.tr file is used to trace the packets. A normal awk command could be used to analyse the packets.
$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"


$ns at 5.0 "finish"
We could also stop the TCP or UDP in between using stop instead of start, hence nam out.nam need to be used if finish is not used.
run is used to run the whole simulation.

$ns run
The file should be saved in .tcl format and should use ns filename.tcl to run

NS2: Printing Routing Table in AODV

Actually you can find all this information in trace file which NS2 made, however using following code simplifies getting required informaiton during running time. The code stores

Add following code to aodv.h after void rt_down(aodv_rt_entry *rt);
1void  rt_print(nsaddr_t node_id);
Add following code to aodv.cc after혻void혻 AODV::rt_down(aodv_rt_entry *rt)
1void  AODV::rt_print(nsaddr_t node_id) {
2    FILE * dumpFile;
3    char dumpFileName[50] = "rtable.txt";
4 
5    dumpFile = fopen(dumpFileName, 'a');
6 
7    aodv_rt_entry *rt;
8 
9    fprintf(dumpFile, "=======================================================");
10 
11    for (rt=rtable.head();rt; rt = rt->rt_link.le_next) {
12 
13        fprintf(dumpFile, "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)
14 
15    }
16 
17    fclose(dumpFile);
18}
The function (rt_print) can be used anywhere in AODV. For example, I am using the function, when route request generated혻node receives route reply message (RREP).

1if (ih->daddr() == index) { // If I am the original source
2  // Update the route discovery latency statistics
3  // rp->rp_timestamp is the time of request origination
4 
5    rt_print(index); // print this nodes whole routing table
6 
7    rt->rt_disc_latency[(unsigned char)rt->hist_indx] = (CURRENT_TIME - rp->rp_timestamp)
8                                         / (double) rp->rp_hop_count;
9    // increment indx for next time
10    rt->hist_indx = (rt->hist_indx + 1) % MAX_HISTORY;
11}

How to Install multiple instances of cygwin in Windows XP?

When I was doing my research, I wanted to install two instances of cygwin to ease the job. I found some tips in a website and modified them to suite my needs. Though I do not remember the original idea's link, I would like to share the mine.

Step 1. Install the first instance of cygwin in a partition.

Step 2. The installation makes the following registry entries.


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions]

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin]

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2]
"cygdrive prefix"="/cygdrive"
"cygdrive flags"=dword:00000022

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/]
"native"="C:\\hide-cygwin"
"flags"=dword:0000000a

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/usr/bin]
"native"="C:\\hide-cygwin/bin"
"flags"=dword:0000000a

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/usr/lib]
"native"="C:\\hide-cygwin/lib"
"flags"=dword:0000000a

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\Program Options]

3. Export the registry tree of HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions. If help is needed with exporting registry, refer to this link.

4. Then copy cygwin installation folder to another location (partition), and make a copy of the exported registry file. Open the copied registry file and change the contents to reflect the partition change.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions]

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin]

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2]
"cygdrive prefix"="/cygdrive"
"cygdrive flags"=dword:00000022

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/]
"native"="D:\\cyg2win"
"flags"=dword:0000000a

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/usr/bin]
"native"="D:\\cyg2win/bin"
"flags"=dword:0000000a

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/usr/lib]
"native"="D:\\cyg2win/lib"
"flags"=dword:0000000a

[HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\Program Options]

5. Now I have 2 exported registry files. And I am going to create 2 shortcuts to bash binary file like this, C:\hide-cygwin\bin\bash.exe --login -i. And another shortcut pointing to D:\cyg2win\bin\bash.exe --login -i.

6. Finally, the cygwin instances can be run successfully. When one of them is about to run, the related registry file needs to be applied. Otherwise, bash would have problems.


Cons in NS-2 Wireless Trace Format Takes More Space

I tried NS-2 wireless trace format as I needed to test python scripts which does not work on old wireless format. The same tcl file is used to generate new trace. 

When then old format is used, trace file size is 6,113 KB. It becomes 14,801 KB in the case of new trace. It is more than 2 times. It is probably the best to use the old format if the resource is limited. I did not do a proper research as I did was create a tcl file, use both trace files, compare sizes.

When a wireless simulation is run, the trace format of it is set to old wireless format by default. If there is a need to use the new wireless format, it should be specified in the .tcl file. The following code is to use the new trace format in wireless scenarios.

Trick for Installing ns 2.34 in Ubuntu 10.04

Ubuntu 10.04 or Lucid Lynx has newer gcc version which gcc 4.4.3. It is not compatible with otcl. It needs gcc 4.3 to be compiled successfully. So, modifying makefile.in in otcl-1.13 folder solved my problem.
Change the code in Makefile.in
From this
CC = @CC@
to this
CC = gcc-4.3

Hope it helps for otcl problems in installing ns 2.34 on Ubuntu 10.04.

How to Plott NS2 Trace

I saw a nice tutorial on plotting ns2 trace files using a program named gnuplot.

http://alkautsarpens.wordpress.com/2008/05/15/visualize-trace-file-data-with-gnuplot/

WiMAX module for the ns-2 simulator

Another interest.
I have to go back to ns-2.28 to run WiMax simulation. Though, the first try is a mess. Installing ns-allinone-2.28 is a piece of cake. 
Just needed to apply a patch to nam-1.11. nam is working fine before updating the package with WiMax modules. 
After the update, it freezes even with the simple tcl programs. Gotta try more. May be it is because of Windows. It may work in Fedora. 

nam-1.11 patch

Just this line.

Manual Edit:
At line 73 of agent.h under nam-1.11 folder, change NULL to 0.

from this, double &corner_x, double &corner_y) const = NULL;
to this, double &corner_x, double &corner_y) const = 0;

Patch file:
http://www.isi.edu/nsnam/dist/nam-1.11.patch

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.  
 

Node placement in ns2 AODV (or) Random Distribution of nodes in ns2 (or) Create a 100 nodes in ns2 AODV

How to place 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.

In Ns2 How to create your own packet

My own packet:

       Since i am getting a lot of messages related to creating your own packet in aodv,  i am going to write another post explaining about packet creation and how to add tracing information in cmu_trace.cc. 

      i) Create your own packet in aodv-packet.h

/* =====================================================================
   Packet Formats...
 ===================================================================== */
#define AODVTYPE_HELLO  0x01
#define AODVTYPE_RREQ   0x02
#define AODVTYPE_RREP   0x04
#define AODVTYPE_RERR   0x08
#define AODVTYPE_RREP_ACK  0x10
#define AODVTYPE_MYREQ   0x20
#define AODVTYPE_MYREP   0x40

...
...
/*
 * AODV Routing Protocol Header Macros
 */
#define HDR_AODV(p) ((struct hdr_aodv*)hdr_aodv::access(p))
#define HDR_AODV_REQUEST(p)  ((struct hdr_aodv_request*)hdr_aodv::access(p))
#define HDR_AODV_REPLY(p) ((struct hdr_aodv_reply*)hdr_aodv::access(p))
///
#define HDR_AODV_MYREQUEST(p)  ((struct hdr_aodv_myrequest*)hdr_aodv::access(p))
#define HDR_AODV_MYREPLY(p) ((struct hdr_aodv_myreply*)hdr_aodv::access(p))

....
....

struct hdr_aodv_myrequest {
        u_int8_t        rq_type; // Packet Type
        u_int8_t        reserved[2];
        nsaddr_t        rq_dst;         // Destination IP Address
        nsaddr_t        rq_src;         // Source IP Address
       nsaddr_t trust_on;       // Node on which, we want to know the trust

        double          rq_timestamp;   // when REQUEST sent;
       // used to compute route discovery latency
       //#define RREQ_GRAT_RREP 0x80

  inline int size() { 
  int sz = 0;
  /*
  sz = sizeof(u_int8_t) // rq_type
    + 2*sizeof(u_int8_t) // reserved
    + sizeof(u_int8_t) // rq_hop_count
    + sizeof(double) // rq_timestamp
    + sizeof(u_int32_t) // rq_bcast_id
    + sizeof(nsaddr_t) // rq_dst
    + sizeof(u_int32_t) // rq_dst_seqno
    + sizeof(nsaddr_t) // rq_src
    + sizeof(u_int32_t); // rq_src_seqno
  */
  sz = 7*sizeof(u_int32_t);
  assert (sz >= 0);
return sz;
  }
};
struct hdr_aodv_myreply {
        u_int8_t        rp_type;        // Packet Type
        u_int8_t        reserved[2];
        nsaddr_t        rp_dst;                 // Destination IP Address
        nsaddr_t        rp_src;                 // Source IP Address
        nsaddr_t  trust_on; // Node on which, we want to know the trust
        double         rp_lifetime;            // Lifetime
        double trust_val;
        double          rp_timestamp;           // when corresponding REQ sent;
       // used to compute route discovery latency
        inline int size() { 
        int sz = 0;
  /*
  sz = sizeof(u_int8_t) // rp_type
    + 2*sizeof(u_int8_t) // rp_flags + reserved
    + sizeof(u_int8_t) // rp_hop_count
    + sizeof(double) // rp_timestamp
    + sizeof(nsaddr_t) // rp_dst
    + sizeof(u_int32_t) // rp_dst_seqno
    + sizeof(nsaddr_t) // rp_src
    + sizeof(u_int32_t); // rp_lifetime
  */
  sz = 6*sizeof(u_int32_t);
  assert (sz >= 0);
return sz;
  }

};

....
    ii)  In aodv.cc
....
....
void AODV::recvAODV(Packet *p) 
{
struct hdr_aodv *ah = HDR_AODV(p);
assert(HDR_IP (p)->sport() == RT_PORT);
assert(HDR_IP (p)->dport() == RT_PORT);
/*
* Incoming Packets.
*/
///
switch(ah->ah_type) 
{
case AODVTYPE_RREQ:
recvRequest(p);
break;
case AODVTYPE_RREP:
recvReply(p);
break;

case AODVTYPE_MYREQ:
recvMyRequest(p);
break;

case AODVTYPE_MYREP:
recvMyReply(p);
break;

....
....

void AODV::recvMyRequest(Packet *p) 
{
struct hdr_aodv_myrequest *rq = HDR_AODV_MYREQUEST(p);

// Get the trust value from rq->rq_dst i.e index
MobileNode *iNode;
iNode=(MobileNode *) (Node::get_node_by_address(index));
double tval = iNode->neigh_trust[rq->trust_on];
// Re-initialize the neighbor trust
iNode->neigh_trust[rq->trust_on] = 0;

sendMyReply(rq->rq_src, // IP Destination
            index, // Dest IP Address
rq->trust_on,   // Node on which, we want to know the trust
MY_ROUTE_TIMEOUT, // Lifetime
            rq->rq_timestamp,  // Timestamp
              tval); // Trust value
Packet::free(p);
// drop the myreq packet and create a reply packet
}

void AODV::recvMyReply(Packet *p) 
{
struct hdr_ip *ih = HDR_IP(p);
struct hdr_aodv_myreply *rp = HDR_AODV_MYREPLY(p);
temp_trust[rp->rp_src][rp->trust_on] = rp->trust_val;
fprintf(fp,"\n%d on %d is %lf by %d at %lf",rp->rp_src,rp->trust_on,rp->trust_val,index,CURRENT_TIME);
Packet::free(p);
}
....
....
void AODV::sendMyRequest(nsaddr_t dst, nsaddr_t trust_n) 
{
// printf("\nsend trust req to %d %d by %d at %lf\n",dst,trust_n,index,CURRENT_TIME);
// Allocate a RREQ packet 
Packet *p = Packet::alloc();
struct hdr_cmn *ch = HDR_CMN(p);
struct hdr_ip *ih = HDR_IP(p);
struct hdr_aodv_myrequest *rq = HDR_AODV_MYREQUEST(p);
// Fill out the MYREQ packet 
// ch->uid() = 0;
ch->ptype() = PT_AODV;
ch->size() = IP_HDR_LEN + rq->size();
ch->iface() = -2;
ch->error() = 0;
ch->addr_type() = NS_AF_NONE;
ch->prev_hop_ = index;          // AODV hack
ch->next_hop_ = dst;

ih->saddr() = index;
ih->daddr() = dst;
ih->sport() = RT_PORT;
ih->dport() = RT_PORT;

// Fill up some more fields. 
rq->rq_type = AODVTYPE_MYREQ;
rq->rq_dst = dst;
rq->rq_src = index;
rq->trust_on = trust_n;
rq->rq_timestamp = CURRENT_TIME;

Scheduler::instance().schedule(target_, p, 0.);
}
....
....
void AODV::sendMyReply(nsaddr_t ipdst, nsaddr_t rpdst, nsaddr_t trust_n, u_int32_t lifetime, double timestamp, double trust_v) 
{
Packet *p = Packet::alloc();
struct hdr_cmn *ch = HDR_CMN(p);
struct hdr_ip *ih = HDR_IP(p);
struct hdr_aodv_myreply *rp = HDR_AODV_MYREPLY(p);

rp->rp_type = AODVTYPE_MYREP;
rp->rp_dst = ipdst;
rp->rp_src = index;
rp->trust_on = trust_n;
rp->trust_val = trust_v;
rp->rp_lifetime = lifetime;
rp->rp_timestamp = timestamp;
   
// ch->uid() = 0;
ch->ptype() = PT_AODV;
ch->size() = IP_HDR_LEN + rp->size();
ch->iface() = -2;
ch->error() = 0;
ch->addr_type() = NS_AF_INET;
ch->next_hop_ = ipdst;
ch->prev_hop_ = index;          // AODV hack
ch->direction() = hdr_cmn::DOWN;

ih->saddr() = index;
ih->daddr() = ipdst;
ih->sport() = RT_PORT;
ih->dport() = RT_PORT;
ih->ttl_ = NETWORK_DIAMETER;

Scheduler::instance().schedule(target_, p, 0.);
}
....
....
// calling sendMyRequest() function
// inside 
// in my use case
sendMyRequest(i,j); 
   iii) Include all the function declaration in aodv.h
   iv) Adding tracing info in cmu-trace.cc file
...
...
void
CMUTrace::format_aodv(Packet *p, int offset)
{
        struct hdr_aodv *ah = HDR_AODV(p);
        struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p);
        struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);
        struct hdr_aodv_myrequest *trq = HDR_AODV_MYREQUEST(p);
        struct hdr_aodv_myreply *trp = HDR_AODV_MYREPLY(p);
        switch(ah->ah_type) {
        case AODVTYPE_RREQ:
                ....
                ....
                
        case AODVTYPE_MYREQ:

if (pt_->tagged()) {
    sprintf(pt_->buffer() + offset,
    "-aodv:t %x -aodv:d %d -aodv:s %d -aodv:o %d"
    "-aodv:c REQUEST ",
    trq->rq_type,
                            trq->rq_dst,
                            trq->rq_src,
                            trq->trust_on);
  } else if (newtrace_) {

    sprintf(pt_->buffer() + offset,
"-P aodv -Pt 0x%x -Pd %d -Ps %d -Po %d -Pc REQUEST ",
trq->rq_type,
                        trq->rq_dst,
                        trq->rq_src,
                        trq->trust_on);

} else {

    sprintf(pt_->buffer() + offset,
"[0x%x [%d] [%d]] (REQUEST)",
trq->rq_type,
                        trq->rq_dst,
                        trq->rq_src,
                        trq->trust_on);
}
                break;

        case AODVTYPE_RREP:
        case AODVTYPE_HELLO:
        case AODVTYPE_RERR:
               ....
               ....
             break;
               
case AODVTYPE_MYREP:
if (pt_->tagged()) {
    sprintf(pt_->buffer() + offset,
    "-aodv:t %x -aodv:d %d -aodv:o %d"
    "-aodv:l %f -aodv:v %f -aodv:c %s ",
    trp->rp_type,
    trp->rp_dst,
    trp->trust_on,
    trp->rp_lifetime,
    trp->trust_val,
    "MYREPLY");
} else if (newtrace_) {
sprintf(pt_->buffer() + offset,
    "-P aodv -Pt 0x%x -Pd %d -Po %d -Pl %f -Pv %f -Pc %s ",
trp->rp_type,
trp->rp_dst,
trp->trust_on,
trp->rp_lifetime,
trp->trust_val,
"REPLY");
        } else {
sprintf(pt_->buffer() + offset,
"[0x%x [%d] %d %f %f] (%s)",
trp->rp_type,
trp->rp_dst,
trp->trust_on,
trp->rp_lifetime,
trp->trust_val,
"MYREPLY");
}
                break;
        default:
#ifdef WIN32
                fprintf(stderr,
        "CMUTrace::format_aodv: invalid AODV packet type\n");
#else
fprintf(stderr,
        "%s: invalid AODV packet type\n", __FUNCTION__);
#endif
                abort();
        }
}
     v)  In ns-2.35/trace/cmu-trace.cc , We need to add tracing information otherwise, we will get a "invalid AODV packet type" error.
     vi)  Whenever you do changes in aodv files, run all these four commands (Do Not Forget),
                    ./configure
                     make clean
                     make
                     make install   
Note:  I didn't do everything, but i did give you enough information, to proceed. Try to understand the functionality, then it is much simple. Hope it helps..