邻节点请求与回复控制包函数.docx

上传人:b****6 文档编号:5547849 上传时间:2022-12-19 格式:DOCX 页数:14 大小:18.77KB
下载 相关 举报
邻节点请求与回复控制包函数.docx_第1页
第1页 / 共14页
邻节点请求与回复控制包函数.docx_第2页
第2页 / 共14页
邻节点请求与回复控制包函数.docx_第3页
第3页 / 共14页
邻节点请求与回复控制包函数.docx_第4页
第4页 / 共14页
邻节点请求与回复控制包函数.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

邻节点请求与回复控制包函数.docx

《邻节点请求与回复控制包函数.docx》由会员分享,可在线阅读,更多相关《邻节点请求与回复控制包函数.docx(14页珍藏版)》请在冰豆网上搜索。

邻节点请求与回复控制包函数.docx

邻节点请求与回复控制包函数

NEIGHBOR_REQUEST//邻节点请求

NEIGHBOR_REPLY//邻节点回复

Neighbor_request//邻节点请求

Neighbor_reply//邻节点回复

neighbor_request//邻节点请求

neighbor_reply//邻节点回复

nbrq//邻节点请求

nbrp//邻节点回复

 

1.在aodv_packet.h中:

(1)首先在aodv_packet..h控制包头文件中对所添加的控制包进行定义:

 

#defineAODVTYPE_HELLO0x01

#defineAODVTYPE_RREQ0x02

#defineAODVTYPE_RREP0x04

#defineAODVTYPE_RERR0x08

#defineAODVTYPE_RREP_ACK0x10

#defineAODVTYPE_NEIGHBOR_REQUEST0x20

#defineAODVTYPE_NEIGHBOR_REPLY0x40

(2)接着添加数据包申请内存空间的宏

/*

*AODVRoutingProtocolHeaderMacros

*/

#defineHDR_AODV(p)((structhdr_aodv*)hdr_aodv:

:

access(p))

#defineHDR_AODV_REQUEST(p)((structhdr_aodv_request*)hdr_aodv:

:

access(p))

#defineHDR_AODV_REPLY(p)((structhdr_aodv_reply*)hdr_aodv:

:

access(p))

#defineHDR_AODV_ERROR(p)((structhdr_aodv_error*)hdr_aodv:

:

access(p))

#defineHDR_AODV_RREP_ACK(p)((structhdr_aodv_rrep_ack*)hdr_aodv:

:

access(p))

#defineHDR_AODV_NEIGHBOR_REQUEST(p)((structhdr_aodv_neighbor_request*)hdr_aodv:

:

access(p))

#defineHDR_AODV_NEIGHBOR_REPLY(p)((structhdr_aodv_neighbor_reply*)hdr_aodv:

:

access(p))

(3)接着要具体定义新的数据包格式了。

我这里定义的AODV的格式如下所示:

邻节点请求数据包格式:

structhdr_aodv_neighbor_request{

u_int8_tnbrq_type;//类型编号

u_int8_treserved[3];//保留字,没用到

u_int8_tmal_count;//Maliciousnodenumberu_int32_tnbrq_bcast_id;//BroadcastID

nsaddr_tmalicious_node_addr[AODV_MAX_NEIGHBOR_REQUEST];//malicious

doubleobs[AODV_MAX_NEIGHBOR_REQUEST];

nsaddr_tsrc_addr;//源节点

inlineintsize()//计算数据包的大小

{

intsz=0;

/*

sz=sizeof(u_int8_t)//type

+sizeof(nsaddr_t)//malicious_node_addr

+sizeof(nsaddr_t)//src_addr

+sizeof(u_int32_t)//nbrq_bcast_id

*/

sz=(2*mal_count+3)*sizeof(u_int32_t);

assert(sz);

returnsz;

}

};

邻节点回复数据包格式:

structhdr_aodv_neighbor_reply{

u_int8_tnbrp_type;//类型编号

u_int8_treserved[3];//保留字,没用到

u_int8_tmal_count;//Maliciousnodenumberu_int32_tnbrp_bcast_id;//BroadcastID

nsaddr_tmalicious_node_addr[AODV_MAX_NEIGHBOR_REPLY];//malicious

doubleobs[AODV_MAX_NEIGHBOR_REPLY];

nsaddr_tsrc_addr;//源节点

inlineintsize()//计算数据包的大小

{

intsz=0;

/*

sz=sizeof(u_int8_t)//type

+sizeof(nsaddr_t)//malicious_node_addr

+sizeof(nsaddr_t)//src_addr

+sizeof(u_int32_t)//nbrp_bcast_id

*/

sz=(2*mal_count+3)*sizeof(u_int32_t);

assert(sz);

returnsz;

}

};

(4)然后是在hdr_all_aodv添加hdr_aodv_neighbor_request以及hdr_aodv_neighbor_reply,如下所示:

//forsizecalculationofheader-spacereservation

unionhdr_all_aodv{

hdr_aodvah;

hdr_aodv_requestrreq;

hdr_aodv_replyrrep;

hdr_aodv_errorrerr;

hdr_aodv_rrep_ackrrep_ack;

hdr_aodv_neighbor_requestnbrq_err;

hdr_aodv_neighbor_replynbrp_err;

};

 

2.在aodv.h文件中:

在数据包头文件中添加完毕之后,还需要在aodv.h头文件中添加四个函数:

voidsendNeighbor_request(Packet*p);//发送请求

voidsendNeighbor_reply(Packet*p);//发送回复

voidrecvNeighbor_request(Packet*p);//接收请求

voidrecvNeighbor_reply(Packet*p);//接收回复

3.在aodv.cc文件中:

(1)实现发送临节点请求的功能:

void

AODV:

:

sendNeighbor_requst(Packet*p)

{

//Packet*p=Packet:

:

alloc();

structhdr_cmn*ch=HDR_CMN(p);

structhdr_ip*ih=HDR_IP(p);

structhdr_aodv_Neighbor_requst*nbrq=HDR_AODV_Neighbor_requst(p);

nbrq->nbrq_type=AODVTYPE_Neighbor_requst;

//nbrq->malicious_node_addr=malicious_addr;

nbrq->src_addr=index;

nbrq->nbrq_bcast_id=bid++;//广播ID

//nbrq->reserved[0,1,2]=....

ch->ptype()=PT_AODV;

ch->size()=IP_HDR_LEN+nbrq->size();

ch->iface()=-2;

ch->error()=0;

ch->addr_type()=NS_AF_NONE;

ch->next_hop_=0;

ch->prev_hop_=index;

ch->direction()=hdr_cmn:

:

DOWN;//important:

changethepacket'sdirection

ih->saddr()=index;//源地址

ih->daddr()=IP_BROADCAST;//广播分组

ih->sport()=RT_PORT;//源端口号

ih->dport()=RT_PORT;//目的端口号

Scheduler:

:

instance().schedule(target_,p,0.0);//printf("endsendNeighbor_requst,andnbrq_type=%x/n",nbrq->nbrq_type);

}

(2)实现发送邻节点回复的功能:

void

AODV:

:

sendNeighbor_reply(Packet*p)

{

//Packet*p=Packet:

:

alloc();

structhdr_cmn*ch=HDR_CMN(p);

structhdr_ip*ih=HDR_IP(p);

structhdr_aodv_Neighbor_reply*nbrp=HDR_AODV_Neighbor_reply(p);

nbrp->nbrp_type=AODVTYPE_Neighbor_reply;

//nbrp->malicious_node_addr=malicious_addr;

nbrp->src_addr=index;

nbrp->nbrp_bcast_id=bid++;//广播ID

//nbrp->reserved[0,1,2]=....

ch->ptype()=PT_AODV;

ch->size()=IP_HDR_LEN+nbrp->size();

ch->iface()=-2;

ch->error()=0;

ch->addr_type()=NS_AF_NONE;

ch->next_hop_=0;

ch->prev_hop_=index;

ch->direction()=hdr_cmn:

:

DOWN;//important:

changethepacket'sdirection

ih->saddr()=index;//源地址

ih->daddr()=IP_BROADCAST;//广播分组

ih->sport()=RT_PORT;//源端口号

ih->dport()=RT_PORT;//目的端口号

Scheduler:

:

instance().schedule(target_,p,0.0);//printf("endsendNeighbor_reply,andnbrp_type=%x/n",nbrp->nbrp_type);

}

 

(3)实现接收邻节点请求的功能:

void

AODV:

:

recvNeighbor_request(Packet*p){

structhdr_ip*ih=HDR_IP(p);

structhdr_aodv_neighbor_request*nbrq=HDR_AODV_NEIGHBOR_REQUEST(p);

aodv_rt_entry*rt;

/*

*Dropif:

*-I'mthesource

*-Irecentlyheardthisneighbor_request.

*/

if(nbrq->nbrq_src==index){

#ifdefDEBUG

fprintf(stderr,"%s:

gotmyownNEIGHBOR_REQUEST\n",__FUNCTION__);

#endif//DEBUG

Packet:

:

free(p);

return;

}

if(id_lookup(nbrq->nbrq_src,nbrq->nbrq_bcast_id)){

#ifdefDEBUG

fprintf(stderr,"%s:

discardingneighbor_request\n",__FUNCTION__);

#endif//DEBUG

Packet:

:

free(p);

return;

}

/*

*CachethebroadcastID

*/

id_insert(nbrq->nbrq_src,nbrq->nbrq_bcast_id);

 

/*

*WeareeithergoingtoforwardtheNEIGHBOR_REQUESTorgeneratea

*NEIGHBOR_REPLY.Beforewedoanything,wemakesurethattheREVERSE

*routeisintheroutetable.

*/

aodv_rt_entry*rt0;//rt0isthereverseroute

rt0=rtable.rt_lookup(nbrq->nbrq_src);

if(rt0==0){/*ifnotintheroutetable*/

//createanentryforthereverseroute.

rt0=rtable.rt_add(nbrq->nbrq_src);

}

rt0->rt_expire=max(rt0->rt_expire,(CURRENT_TIME+REV_ROUTE_LIFE));

if((nbrq->nbrq_src_seqno>rt0->rt_seqno)||

((nbrq->nbrq_src_seqno==rt0->rt_seqno)&&

(nbrq->nbrq_hop_countrt_hops))){

//Ifwehaveafresherseqno.orlesser#hopsforthe

//sameseqno.,updatethertentry.Elsedon'tbother.

rt_update(rt0,nbrq->nbrq_src_seqno,nbrq->nbrq_hop_count,ih->saddr(),

max(rt0->rt_expire,(CURRENT_TIME+REV_ROUTE_LIFE)));

if(rt0->rt_req_timeout>0.0){

//Resetthesoftstateand

//SetexpirytimetoCURRENT_TIME+ACTIVE_ROUTE_TIMEOUT

//Thisisbecauserouteisusedintheforwarddirection,

//butonlysourcesgetbenefitedbythischange

rt0->rt_req_cnt=0;

rt0->rt_req_timeout=0.0;

rt0->rt_req_last_ttl=nbrq->nbrq_hop_count;

rt0->rt_expire=CURRENT_TIME+ACTIVE_ROUTE_TIMEOUT;

}

/*Findoutwhetheranybufferedpacketcanbenefitfromthe

*reverseroute.

*Mayneedsomechangeinthefollowingcode-Mahesh09/11/99

*/

assert(rt0->rt_flags==RTF_UP);

Packet*buffered_pkt;

while((buffered_pkt=rqueue.deque(rt0->rt_dst))){

if(rt0&&(rt0->rt_flags==RTF_UP)){

assert(rt0->rt_hops!

=INFINITY2);

forward(rt0,buffered_pkt,NO_DELAY);

}

}

}

//Endforputtingreverserouteinrttable

 

/*

*Wehavetakencareofthereverseroutestuff.

*Nowseewhetherwecansendaroutereply.

*/

rt=rtable.rt_lookup(nbrq->nbrq_dst);

//FirstcheckifIamthedestination..

if(nbrq->nbrq_dst==index){

#ifdefDEBUG

fprintf(stderr,"%d-%s:

destinationsendingreply\n",

index,__FUNCTION__);

#endif//DEBUG

//Justtobesafe,Iusethemax.Somebodymayhave

//incrementedthedstseqno.

seqno=max(seqno,nbrq->nbrq_dst_seqno)+1;

if(seqno%2)seqno++;

sendReply(nbrq->nbrq_src,//IPDestination

1,//HopCount

index,//DestIPAddress

seqno,//DestSequenceNum

MY_ROUTE_TIMEOUT,//Lifetime

nbrq->nbrq_timestamp);//timestamp

Packet:

:

free(p);

}

//Iamnotthedestination,butImayhaveafreshenoughroute.

elseif(rt&&(rt->rt_hops!

=INFINITY2)&&

(rt->rt_seqno>=nbrq->nbrq_dst_seqno)){

//assert(rt->rt_flags==RTF_UP);

assert(nbrq->nbrq_dst==rt->rt_dst);

//assert((rt->rt_seqno%2)==0);//istheseqnoeven?

sendReply(nbrq->nbrq_src,

rt->rt_hops+1,

nbrq->nbrq_dst,

rt->rt_seqno,

(u_int32_t)(rt->rt_expire-CURRENT_TIME),

//rt->rt_expire-CURRENT_TIME,

nbrq->nbrq_timestamp);

//InsertnexthopstoNBREQsourceandNBREQdestinationinthe

//precursorlistsofdestinationandsourcerespectively

rt->pc_insert(rt0->rt_nexthop);//nexthoptoNBREQsource

rt0->pc_insert(rt->rt_nexthop);//nexthoptoNBREQdestination

#ifdefNBREQ_GRAT_NBREP

sendReply(nbrq->nbrq_dst,

nbrq->nbrq_hop_count,

nbrq->nbrq_src,

nbrq->nbrq_src_seqno,

(u_int32_t)(rt->rt_expire-CURRENT_TIME),

//rt->rt_expire-CURRENT_TIME,

nbrq->nbrq_timestamp);

#endif

//TODO:

sendgratRREPtodstifGflagsetinNBREQusingnbrq->nbrq_src_seqno,nbrq->nbrq_hop_counT

//DONE:

IncludedgratuitousrepliestobesentasperIETFaodvdraftspecification.Asofnow,Gflaghasnotbeendynamicallyusedandisalwayssetorresetinaodv-packet.h---AnantUtgikar,09/16/02.

Packet:

:

free(p);

}

/*

*Can'treply.SoforwardtheRouteneighbor_request

*/

else{

ih->saddr()=index;

ih->daddr()=IP_BROADCAST;

nbrq->nbrq_hop_count+=1;

//Maximumsequencenumberseenenroute

if(rt)nbrq->nbrq_dst_seqno=max(rt->rt_seqno,nbrq->nbrq_dst_seqno);

forward((aodv_rt_entry*)0,p,DELAY);

}

}

(4)实现接收邻节点回复的功能:

void

AODV:

:

recvNeighbor_reply(Packet*p){

//structhdr_cmn*ch=HDR_CMN(p);

structhdr_ip*ih=HDR_IP(p);

structhdr_aodv_neighbor_reply*nbrp=HDR_AODV_REPLY

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1