Search This Blog

Saturday 21 February 2015

How to avoid flooding node in my route and choose alternate path in ns2 ?

Avoid packets through Flooding/Malicious/Blackhole node and choose alternate path:

Flooding node/malicious node/blackhole node, the answer is same. Delete the destination node from your route table and restrict adding these node to your route table. Thats it. After the hello period, aodv automatically finds a new route to your destination excluding the malicious/flooder/malicious node.

i)  Delete the route in your rt_table.
In forward function,  
   if(dest is blockhole)    
     rtable.rt_delete(ih->daddr());


ii) Drop ROUTE_REP from blockhole node and restrict them by not adding to your rt_table
In recvReply function  
if(rt == 0)   {    
  if( ! blockhole_node)    
      rt = rtable.rt_add(rp->rp_dst);  
}

It will work. This is just one way. If you have a better way in mind, feel free to comment.

P.S: Guys the above logic is for avoiding packets through the flooding/malicious/blackhole node and finding an alternative path (not meant for flooding prevention).

Sunday 8 February 2015

ns2 tips

The initial mistake i did, 
      * Do not forget to set the path after installing ns2. No matter what, how many times you try it won't work. You gotta set the path. The best way to ensure that you installed ns2 correctly is, try enabling the hello packets.  click here
      * Compiling process, after modifying aodv files. click here
                * Not reading this basic aodv tutorial click here.

            Do share your mistakes in the comments...

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