转载地址:http://narentada.com/code-for-preventing-flood-attack-in-aodv/
step 1:Create two cache table ‘aodv_RREQcount’ and ‘aodv_broodyList’ in aodv_rtable.h
/*
aodv request count cache
*/
//NVT 11Feb12
class aodv_RREQcount{
friend class AODV;
friend class aodv_rt_entry;
public:
aodv_RREQcount(u_int32_t c) {ct_addr=c; RREQentry=0;}
protected:
LIST_ENTRY(aodv_RREQcount) ct_link;
u_int32_t RREQentry;
nsaddr_t ct_addr;
double ct_expire;
};
LIST_HEAD(aodv_countcache, aodv_RREQcount);
/*struct broodyList
{
bool flag;
nsaddr_t addr_;
};*/
class aodv_broodyList{
friend class AODV;
friend class aodv_rt_entry;
public:
//aodv_broodyList(u_int32_t e){bd_addr=e;}
protected:
LIST_ENTRY(aodv_broodyList) bd_link;
nsaddr_t bd_addr;
};
Step 2:
add following code in aodv.cc somewhere
aodv_broodyList*
AODV::bd_lookup(nsaddr_t id)
{
aodv_broodyList *bd = bdhead.lh_first;
for(;bd;bd=bd->bd_link.le_next){
if(bd->bd_addr ==id)
break;
}
return bd;
}
step 3:
add the following funtion in aodv.h inside protected member of AODV class
void ct_add(nsaddr_t id); //NVT 11FEB12
//void ct_lookup(nsaddr_t id);
//aodvplain_RREQcount* ct_lookup(nsaddr_t id);//NVT 11FEB12
void ct_remove(nsaddr_t id);//NVT 12FEB12
void ct_flush(void);//NVT 12FEB12
aodv_broodyList* bd_lookup(nsaddr_t id); //NVT 12FEB12
Step 4:
count number of request coming from neighbor and take the decision in ‘recvRequest()’
/*
* Drop if:
* - I'm the source
* - I recently heard this request.
*/
aodv_broodyList *bd;
bd=bd_lookup(rq->rq_src);
if(!bd)
{
AODV_Neighbor *nb;
//aodvplain_RREQcount *ct;
nb = nb_lookup(rq->rq_src);
if((nb) && (rq->rq_hop_count == 1) )
{
printf("\nin recieve request at current time ::%f, index node %d is neighbor? 'TRUE', of node %d",CURRENT_TIME,index,rq->rq_src);
double now = CURRENT_TIME;
int peak_value =11;
aodv_RREQcount *ct = rt_ctlist.lh_first;
//aodvplain_RREQcount *ct2;
for(; ct; ct = ct->ct_link.le_next)
{
if(ct->ct_addr == rq->rq_src)
{
if((ct->ct_expire <= now) && (ct->RREQentry >peak_value) )
{
printf("\nat receive reqeustTimeout:::Flushhhhhhhhhh\n");
//*printf("\nno of count:: %d\n",count);
//*count++;
//ct_remove(ct->ct_addr);
printf("\nexceed peak value giving permenent penently by droping packet\n");
aodv_broodyList *bd1 = new aodv_broodyList();
bd1->bd_addr=ct->ct_addr;
LIST_INSERT_HEAD(&bdhead, bd1, bd_link);
LIST_REMOVE(ct,ct_link);
delete ct;
break;
}
else if (ct->ct_expire<=now)
{
printf("\nOnly Timeout:::Flushhhhhhhhh\n");
LIST_REMOVE(ct,ct_link);
delete ct;
break;
}
ct->RREQentry= ct->RREQentry+1;
printf(" count table entry:::%d",ct->RREQentry);
break;
}
}
if(!ct)
{
aodv_RREQcount *ct1 = new aodv_RREQcount(rq->rq_src);
//*printf("\nin ct_add while adding entry\n");
//*printf("\nfrom node %d ct->ct_addr==%d\n",id,ct->ct_addr);
ct1->ct_expire = CURRENT_TIME + 1;
ct1->RREQentry= ct1->RREQentry+1;
LIST_INSERT_HEAD(&rt_ctlist, ct1, ct_link);
//printf("\nin node %d count table entry ::%d",index,ct1->RREQentry);
/*ct= ct_lookup(rq->rq_src);
if((!ct))
{
printf("\nooop no entry of node %d",rq->rq_src);
ct_add(rq->rq_src);
}*/
}
}
}
else
{
printf("\nat time %f dropppppppp by %d\n",CURRENT_TIME,index);
drop(p, DROP_RTR_ROUTE_LOOP);
return;
}
Step 5:
for flushing the request count table entries implement flushing mechanism
//NVT 11FEB12
void
AODV::ct_flush() {
aodv_RREQcount *ct = rt_ctlist.lh_first;
aodv_RREQcount *ct1;
//aodvplain_RREQcount *ctc;
double now = CURRENT_TIME;
for(; ct; ct =ct1 ) {
ct1=ct->ct_link.le_next;
if(ct->ct_expire <= now) {
printf("\nTimeout:::Flushhhhhhhhhhhh\n");
LIST_REMOVE(ct,ct_link);
delete ct;
}
}
}
Step 6:
add the timer for flushing entry...
void
CacheTimer::handle(Event*) {
agent->ct_flush();
Scheduler::instance().schedule(this, &intr, CACHE_INTERVAL);
}
Step 7:
add the cache timer and related information in AODV.h
class CacheTimer : public Handler {
public:
CacheTimer(AODV* a): agent(a){}
void handle(Event*);
private:
AODV *agent;
Event intr;
};
class AODV: public Agent{
//......
friend class CacheTimer;//Added by NVT*/
//....
Protected:
//....
void ct_flush(void);//NVT 12FEB12
//...
CacheTimer ctimer; //added byNVT
}
step 8:
add the information to aodv.cc constructor
AODV::AODV(nsaddr_t id) : Agent(PT_aodvplain),
ctimer(this){
//...............
LIST_INIT(&bdhead);
LIST_INIT(&nbhead);
LIST_INIT(&bihead);
LIST_INIT(&rt_ctlist);
LIST_INIT(&trhead);
//............
}