Search This Blog

Sunday 10 August 2014

Creating your own packet (cont...)

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. The intro post create your own packet - part 1.

      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...




Monday 28 April 2014

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

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.

Thursday 13 February 2014

How can i create my own packets in aodv ns2 ?


Create your own packet :


           >  In aodv, sometimes we need to get/pass our own information  to neighbors.  Like RouteRequest( RREQ ), RouteReply( RREP ) , we can easily get/pass our own information.

           >  e.g  
                       i)   CH may wants to know the trust value of its neighbors.

                       ii)  Passing Voting information.

           >  We need to modify four files

                        i)   aodv-packet.h , where you will define your own packet structure.

                        ii)  aodv.cc , where you will define your own function. We need two receive function for  receiving request and reply. Also, we need two other functions to send request and reply.

                       iii)  In aodv.h , include the function declaration.     

                       iv)  In ns-2.35/trace/cmu-trace.cc , We need to add tracing information otherwise, we will get a "invalid AODV packet type" error.
             

Friday 7 February 2014

Creation of Flooding Attack in NS2 AODV ???

Short Note on Flooding Attack :

         Malicious Node will create a more no of  RREQ  to a node, which is even doesn't exist in the network topology. This is how malicious node,  start to flood the request in the network. The purpose of this attack is to consume the network bandwidth and to exhaust the network resources all the time.

Steps :

     >  In aodv.h,
                           i)    #define FLOOD_INTERVAL  0.09  

                           ii)   Add this, after BroadcastTimer class

                                 class FloodTimer : public Handler 
                                 {
                                  public:
                                           FloodTimer(AODV* a):    agent(a){}
                                             void     handle(Event*);
                                 private:
                                           AODV     *agent;
                                           Event     intr;
                                 };
          
                          iii)   class AODV: public Agent 
                                 {
                                               ...........
                                               ...........
                                               friend class FloodTimer;
                                                ...........
                                    Protected:
                                                ............
                                                /*
                                                 * Packet TX Routines
                                                 */

                                                void FloodRREQ(nsaddr_t dst);
                                                ............

                                                nsaddr_t        index;                  // IP Address of this node
                                                u_int32_t       seqno;                  // Sequence Number
                                                int             bid;                           // Broadcast ID
                                                bool flooder;
                                              
                                                /*
                                                 * Timers
                                                 */

                                                 FloodTimer ftimer;
                                                 ............
                                   };

     >  In aodv.cc,
                            i)    int AODV::command(int argc, const char*const* argv)
                                {
                                          if(argc == 2)
                                          {
            Tcl& tcl = Tcl::instance();

                if(strncasecmp(argv[1], "id", 2) == 0)
                                                   {
                tcl.resultf("%d", index);
                 return TCL_OK;
                                                   }
     
                      if(strcmp(argv[1], "flooder") == 0) 
                                                    {
                flooder = true;
                return TCL_OK;
                 }

                 if(strncasecmp(argv[1], "start", 2) == 0)
                {
                                                            ........
               ftimer.handle((Event*) 0);
                 ........
                                                    }
                                                    ........
                                             }
               ..........
                                      }

                       ii) Add  ftimer(this)  and  flooder = false,

                                AODV::AODV(nsaddr_t id) : Agent(PT_AODV),
                             btimer(this), htimer(this), ntimer(this),
                             rtimer(this), lrtimer(this), ftimer(this), rqueue()
                               {
                                         ........
                                          flooder=false;
                                        ........
                               }

                        iii) In Timers, add FloodTimer()

                                 void FloodTimer::handle(Event*) 
                                  {
                                          if (agent->flooder==true) 
                                        {
                                            agent->FloodRREQ(99);
                                            // index will be a attacker, flood attacker !
                                        }
                                          Scheduler::instance().schedule(this, &intr, FLOOD_INTERVAL);
                                   }  

                                iv) After void AODV::SendRequest(nsaddr_t dst) function add this,

                                       void AODV::FloodRREQ(nsaddr_t dst)
                                       {
                                        Packet *p = Packet::alloc();
                                        struct hdr_cmn *ch = HDR_CMN(p);
                                         struct hdr_ip *ih = HDR_IP(p);
                                        struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p);
                                        aodv_rt_entry *rt = rtable.rt_lookup(dst);

                                        printf("\n***** 'in FloodRREQ' at  node::%d*****\n",index);
                                        // rtable.rt_display(index);
                                         // Fill out the RREQ 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;
 
                                         ih->saddr() = index;
                                         ih->daddr() = IP_BROADCAST;
                                        ih->sport() = RT_PORT;
                                         ih->dport() = RT_PORT;
                                         ih->ttl_ = NETWORK_DIAMETER;
                                         rq->rq_type = AODVTYPE_RREQ;
                                         rq->rq_hop_count = 1;
                                         rq->rq_bcast_id = bid++;
                                         rq->rq_dst = dst;
                                         static int flood=0,num=0;
                                         if(flood==0)
                                        {
                                          num=(rt ? rt->rt_seqno : 0);
                                          flood=1;
                                        }
                                         rq->rq_dst_seqno = num; 
                                         rq->rq_src = index;
                                         seqno += 2;
                                         assert ((seqno%2) == 0);
                                        rq->rq_src_seqno = seqno;
                                         rq->rq_timestamp = CURRENT_TIME;
                                        num=num+2;
                                        Scheduler::instance().schedule(target_, p, 0.);
                                       }

     >  In tcl file
                             i)  At the end, add this code to make a node as flooder
                                            $ns at 0.0 "[$node_(0) set ragent_] flooder"

Now, node 0 will create a RREQ to node 99 ( which doesn't exist in the network ) for every 0.09 seconds.