Showing posts with label how to clone a protocol in ns2. Show all posts
Showing posts with label how to clone a protocol in ns2. Show all posts

Wednesday, 11 February 2015

how to clone protocol in ns2

How to clone a protocol in ns2.35(AODV)

Step 1: Copy the file of aodv in the different folder name as taodv.

Step 2: Rename all the file names and inside document.

step 3: Replace aodv to taodv and AODV to TAODV.


Step 4: Go to #ns-allinone-2.35\ns-2.35\common\packet.h

typedef unsigned int packet_t;

static const packet_t PT_TCP = 0;
static const packet_t PT_UDP = 1;
static const packet_t PT_CBR = 2;
......
......
......
......
        // insert new packet types here
static const packet_t PT_TAODV = 73;  //newly added packet
static packet_t       PT_NTYPE = 74; // This MUST be the LAST one





class p_info {
public:
    p_info()
    {
        initName();
    }
    const char* name(packet_t p) const {
        if ( p <= p_info::nPkt_ ) return name_[p];
        return 0;
    }
........
........
........

static packetClass classify(packet_t type) {       
        if (type == PT_DSR ||
            type == PT_MESSAGE ||
            type == PT_TORA ||
            type == PT_PUMA ||
            type == PT_AODV ||
            type == PT_TAODV ||
            type == PT_MDART)
            return ROUTING;   
...........
..........
........
........

name_[PT_DCCP]="DCCP";
        name_[PT_DCCP_REQ]="DCCP_Request";
        name_[PT_DCCP_RESP]="DCCP_Response";
        name_[PT_DCCP_ACK]="DCCP_Ack";
        name_[PT_DCCP_DATA]="DCCP_Data";
        name_[PT_DCCP_DATAACK]="DCCP_DataAck";
        name_[PT_DCCP_CLOSE]="DCCP_Close";
        name_[PT_DCCP_CLOSEREQ]="DCCP_CloseReq";
        name_[PT_DCCP_RESET]="DCCP_Reset";
        name_[PT_TAODV]= "TAODV";
        name_[PT_NTYPE]= "undefined";


}

Step 5: #ns-allinone-2.35\ns-2.35\trace\cmu-trace.h

class CMUTrace : public Trace {
public:
    CMUTrace(const char *s, char t);
    void    recv(Packet *p, Handler *h);
    void    recv(Packet *p, const char* why);

    static void addPacketTracer(PacketTracer *pt);

..........
.........
..........
..........

void    format_imep(Packet *p, int offset);
        void    format_aodv(Packet *p, int offset);
        void    format_taodv(Packet *p, int offset);
    void    format_aomdv(Packet *p, int offset);
    void    format_mdart(Packet *p, int offset);
}


Step 6: #ns-allinone-2.35\ns-2.35\trace\cmu-trace.cc
#include <taodv/taodv_packet.h>

//Newly added
void
CMUTrace::format_taodv(Packet *p, int offset)
    {
      struct hdr_taodv *ah = HDR_TAODV(p);
      struct hdr_taodv_request *rq = HDR_TAODV_REQUEST(p);
      struct hdr_taodv_reply *rp = HDR_TAODV_REPLY(p);


      switch(ah->ah_type) {
      case TAODVTYPE_RREQ:

                if (pt_->tagged()) {
                    sprintf(pt_->buffer() + offset,
                            "- taodv:t %x - taodv:h %d - taodv:b %d -taodv:d %d "
                            "- taodv:ds %d - taodv:s %d - taodv:ss %d "
                            "- taodv:c REQUEST ",rq->rq_type,
                             rq->rq_hop_count,
                             rq->rq_bcast_id,
                  rq->rq_dst,
                  rq->rq_dst_seqno,
                  rq->rq_src,
                  rq->rq_src_seqno);
            } else if (newtrace_) {
              sprintf(pt_->buffer() + offset,"-P  taodv -Pt 0x%x -Ph %d -Pb %d -Pd %d -Pds %d -Ps %d -Pss %d -Pc REQUEST ",
              rq->rq_type,
              rq->rq_hop_count,
              rq->rq_bcast_id,
              rq->rq_dst,
              rq->rq_dst_seqno,
              rq->rq_src,
              rq->rq_src_seqno);


                } else {

                    sprintf(pt_->buffer() + offset,
                        "[0x%x %d %d [%d %d] [%d %d]] (RREQ)",
              rq->rq_type,
              rq->rq_hop_count,
              rq->rq_bcast_id,
              rq->rq_dst,
              rq->rq_dst_seqno,
              rq->rq_src,
              rq->rq_src_seqno);
                }
                 break;

        case TAODVTYPE_RREP:
        case TAODVTYPE_HELLO:
        case TAODVTYPE_RERR:

                if (pt_->tagged()) {
                    sprintf(pt_->buffer() + offset,
                            "- taodv:t %x - taodv:h %d - taodv:d %d -tadov:ds %d "
                            "- taodv:l %f - taodv:c %s ",
                            rp->rp_type,
                            rp->rp_hop_count,
                            rp->rp_dst,
                            rp->rp_dst_seqno,
                            rp->rp_lifetime,
                            rp->rp_type == TAODVTYPE_RREP ? "REPLY" :
                            (rp->rp_type == TAODVTYPE_RERR ? "ERROR" :
                             "HELLO"));
                } else if (newtrace_) {

                        sprintf(pt_->buffer() + offset,
                           "-P  taodv -Pt 0x%x -Ph %d -Pd %d -Pds %d -Pl %f -Pc %s ",
                                 rp->rp_type,
                                 rp->rp_hop_count,
                               rp->rp_dst,
                                 rp->rp_dst_seqno,
                  rp->rp_lifetime,
                  rp->rp_type == TAODVTYPE_RREP ?"REPLY" :
                                 (rp->rp_type == TAODVTYPE_RERR ?"ERROR" :
                                 "HELLO"));
                                } else {

                        sprintf(pt_->buffer() + offset,"[0x%x %d [%d %d] %f] (%s)",
                  rp->rp_type,
                  rp->rp_hop_count,
                  rp->rp_dst,
                  rp->rp_dst_seqno,
                  rp->rp_lifetime,
                  rp->rp_type == TAODVTYPE_RREP ? "RREP":
                                (rp->rp_type == TAODVTYPE_RERR ?"ERROR" :
                                  "HELLO"));
                }
                break;

        default:
          #ifdef WIN32
          fprintf(stderr,"CMUTrace::format_ taodv: invalid TAODV packet type\n");
          #else
              fprintf(stderr,"%s: invalid TAODV packet type\n",__FUNCTION__);
        #endif
                abort();
        }
 }
 
...... ......
 //Newly added
 void CMUTrace::format(Packet* p, const char *why)
  {
         ......
        ......
        default:
        ......
        ......
                case PT_AODV:
            format_aodv(p, offset);
                        break;
                case PT_TAODV:                //Newly added
                        format_taodv(p, offset);

                        break;
             break;
        ......
        ......
 }

Step 7: #ns-allinone-2.35\ns-2.35\tcl\lib\ns-packet.tcl



set protolist {
# Common:
    Common
    Flags
    IP     # IP
.......
.......
.......
# Mobility, Ad-Hoc Networks, Sensor Nets:
    AODV     # routing protocol for ad-hoc networks
    TAODV     # routing protocol for ad-hoc networks
.........
........
.........

}

Step 8: #ns-allinone-2.35\ns-2.35\tcl\lib\ns-lib.tcl
Simulator instproc create-wireless-node args {
         ......
        ......
        switch -exact $routingAgent_ {
            ......
            ......
            AODV {
                    set ragent [$self create-aodv-agent $node]
            }
           TAODV {
           set ragent [$self create-taodv-agent $node]            

        }
                
......
                 ......
             }
             ......
             ......
      }
      ......
      ......
      # Newly added
      Simulator instproc create-taodv-agent { node } {
             #  Create TAODV routing agent
             set ragent [new Agent/TAODV [$node node-addr]]
             $self at 0.0 "$ragent start"    ;# start BEACON/HELLO messages
             $node set ragent_ $ragent
             return $ragent
      }


Step 9: #ns-allinone-2.35\ns-2.35\queue\priqueue.cc

//Newly added
      void
      PriQueue::recv(Packet *p, Handler *h)
      {
                     ......
                     ......
                     case PT_AODV:
                     case PT_TAODV:                //Newly added
                     ......
                     ......
      }

Step 10: #ns-allinone-2.35\ns-2.35\Makefile

OBJ_CC = \
             ......
             ......
             aodv/aodv_logs.o aodv/aodv.o \
             aodv/aodv_rtable.o aodv/aodv_rqueue.o \
             taodv/taodv_logs.o taodv/taodv.o \
             taodv/taodv_rtable.o taodv/taodv_rqueue.o \

             ......
             ......
             $(OBJ_STL)
    
    
   *** Make sure you are making the same changes in makefile.vc and Makefile.in otherwise after ./configure
    it will not take into account and object file will not generate.


Step 11: #ns-allinone-2.35\ns-2.35\tcl\lib\ns-agent.tcl
      Agent/AODV instproc init args {
    
              $self next $args
      }
    
      Agent/AODV set sport_   0
      Agent/AODV set dport_   0
    
      # Newly added
      Agent/TAODV instproc init args {
    
              $self next $args
      }
    
      Agent/TAODV set sport_   0
      Agent/TAODV set dport_   0


Step 12: #ns-allinone-2.35\ns-2.35\tcl\lib\ns-mobilenode.tcl
      Node/MobileNode instproc add-target { agent port } {
             ......
             ......
             # 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
             }
             # Newly added
             # Special processing for TAODV
             set taodvonly [string first "TAODV" [$agent info class]]
             if {$taodvonly != -1 } {
        $agent if-queue [$self set ifq_(0)]   ;# ifq between LL and MAC
             }
            
......
             ......
      }

Step 13: #ns-allinone-2.35\ns-2.35\queue\rtqueue.cc
 
      Do not make any changes just go through the Packet Queue used by AODV.

Step 14: #ns-allinone-2.35\ns-2.35\routing\rtable.h

    class Neighbor {
        friend class AODV;
    friend class TAODV;    //Newly added
        friend class rt_entry;
        ......
        ......
        }
       
        class rt_entry {
        friend class rttable;
        friend class AODV;
    friend class TAODV;    //Newly added
    friend class TAODVLocalRepairTimer; //modified
    .....
    .....
    .....
    }
 Step 15: #ns-allinone-2.35\ns-2.35\wpan\p802_15_4nam.cc

    packet_t nam_pktName2Type(const char *name)
    {
        //not all types included

        return (strcmp(packet_info.name(PT_TCP),name) == 0)?PT_TCP:
        ......
        ......
        ......
        (strcmp(packet_info.name(PT_AODV),name) == 0)?PT_AODV:
       (strcmp(packet_info.name(PT_TAODV),name) == 0)?PT_TAODV:
       
      }

Step 16: #ns-allinone-2.35\dei80211mr-1.1.4\src\InitTCL.cc

PacketHeaderManager set tab_(PacketHeader/SR) 1\n\
PacketHeaderManager set tab_(PacketHeader/AODV) 1\n\
PacketHeaderManager set tab_(PacketHeader/TAODV) 1\n\  

Step 17: Rename every aodv file with taodv name inside taodv folder

Step 18: Open each and every file and rename aodv to taodv and AODV to TAODV
Step 19: Rename every timer class in taodv.h and taodv.cc

 E.g. In taodv.h

class TAODVBroadcastTimer : public Handler {
public:
    TAODVBroadcastTimer(TAODV* a) : agent(a) {}
        void    handle(Event*);
private:
        TAODV    *agent;
        Event   intr;
};

.......
......
.....





....
.....
.....
class TAODV: public Tap,public Agent {


....
.....
friend class TAODVBroadcastTimer;
......
......

TAODVBroadcastTimer  btimer;
.....
......




}


 *note rename every timer otherwise it will give an error

E.G.
In taodv.cc


void TAODVHelloTimer::handle(Event* p) {


    agent->sendHello();
    double interval = MinHelloInterval +
    ((MaxHelloInterval - MinHelloInterval) * Random::uniform());
    assert(interval >= 0);
    Scheduler::instance().schedule(this, &intr, interval);


}
*note rename every timer otherwise it will give an error

Step 20: Edit taodv_rtable.h

class taodv_rt_entry {
        friend class taodv_rtable;
        friend class TAODV;
        friend class LocalRepairTimer;
        friend class TAODVLocalRepairTimer; 

 ......
 ......
......


}
 

Step 21: Recompilation:
Step 1: We should recompiled ``packet.cc`` as the ``packet.h'' is modified.
        this can be done by  : ``touch common/packet.cc''
Step 2: ./configure (if this fails go to step 22)
Step 3: make clean
Step 4: make
Step 5: make install

Step 22: If ./configure fails then run ./install

 $cd ns-allinone-2.35
 $./install
 $cd ns-allinone-2.35/ns-2.35
 $sudo make install

You are now done with complete cloning of aodv routing protocol!