📄 aodv.cc
字号:
if(n_val == 1) {#ifdef DEBUG cout << "adov: processRERR: received RERR with N bit set" << endl;#endif rtable_entry *rtEntry; /* dest cnt will always be 1 in this case */ if(rerrOb.getDestCnt() == 1) { rtEntry = rtable.findActiveRoute(rerrOb.getUnrchDest()); if((rtEntry != NULL) && (rtEntry->getNextHop() == data->src_ip)) { /* rerr arrived along the next hop for this dest */ /* check for precursor list, * and forward the RERR if any precursor exist */ rtEntry->setDestSeqNum(rerrOb.getUnrchDestSeqNum()); if(rtEntry->getPrecCnt() > 0) generateRERR(rerrOb.getUnrchDest(),1,false); else { /* rerr has reached the originator node */ cout << "aodv:processRERR: rerr has reached the originator node " << endl; } } } return; } /* generate a new RERR if needed and send it out */ #ifdef DEBUG cout << "adov: processRERR: generate a new RERR" << endl;#endif result = rerrNewOb.createNewRERR(&rerrOb, data->src_ip, precAddr); if( result ) {#ifdef DEBUG cout << "adov: processRERR: transmit the new RERR" << endl;#endif char *newBuf; int len; len = 4*(1+(2*rerrNewOb.getDestCnt())); newBuf = (char *)malloc(sizeof(len)); memset(newBuf,0,len); packBuf(newBuf,&rerrNewOb); if(precAddr != NULL) {#ifdef DEBUG cout << "aodv: processRERR: unicast new RERR" << endl;#endif /* unicast RERR to the addr */ genSndData(&snd,*precAddr,1); copyToSendBuf((void *)newBuf, len); sendPacket(&snd,(void *)sendBuffer,len); } else { /* broadcast RERR */#ifdef DEBUG cout << "aodv: processRERR: broadcast new RERR" << endl;#endif genSndData(&snd,BROADCAST_ADDR,1); copyToSendBuf((void *)newBuf, len); sendPacket(&snd,(void *)sendBuffer,len); } free(precAddr); free(newBuf); } else cout << "aodv: processRERR: do not transmit the new RERR " << endl; } return;}/* process received RREP-ACK message */void aodv::processRREP_ACK(aodvData *data){ /* remove the rrep_ack timer if set*/#ifdef DEBUG cout << "aodv::processRREP_ACK: entered " << endl;#endif timer_Q.removeRREP_ACK_Timer(data->src_ip); return;}u_int32_t aodv::get_next_hop(u_int32_t dst){ /* Here also update the lifetime field */ rtable_entry *rtEntry; rtEntry = rtable.findActiveRoute(dst); if(rtEntry != NULL) { rtEntry->setLifeTime(MAX(rtEntry->getLifeTime(),(getcurrtime()+ACTIVE_ROUTE_TIMEOUT))); return rtEntry->getNextHop(); } else return 0;}/* handler function for data received on asl socket, is called whenever a packet arrives on * asl socket*/void recv_asl_data_handler(void){#ifdef DEBUG cout << "recv_asl_data_handler: got data" << endl;#endif aodvOb.recv_asl_data();}void aodv::recv_asl_data(){ int result; struct route_info *rInfo; rInfo = (struct route_info *)malloc(sizeof(struct route_info)); #ifdef DEBUG cout << "recv_asl_data Entered" << endl;#endif result = read_route_request(asl_sock, rInfo); if(result <=0 ) { cout << "read_route_request(): returned error code " << result << endl; } else if (result == 0) cout << "Duplicate route_request_read: Since route discovery already in progress, do nothing " << endl; else if ( result > 0) {#ifdef DEBUG cout << " recv_asl_data: got data packet from asl" << endl;#endif process_asl(rInfo); } free(rInfo); return;}/* this function is called when data is received from asl */void aodv::process_asl(struct route_info *rInfo){ u_int8_t rflags, protocol; u_int32_t src_ip, dst_ip; RERR rerrOb; dst_ip = (rInfo->dest_ip); src_ip = (rInfo->src_ip); protocol = (rInfo->protocol);#ifdef DEBUG cout << "aodv: process_asl: entered, dst is " << getDotIP(dst_ip) << endl; cout << "aodv: process_asl: entered, src is " << getDotIP(src_ip) << endl;#endif /* start route discovery for received dest IP */ if(duringReboot) { /* generate a rerr for this destination */ /* reset the reboot timer */ #ifdef DEBUG cout << "process_asl: during reboot" << endl;#endif if(src_ip == g_my_ip) { /* locally generated data packets can not be forwarded during reboot */ route_discovery_done(dst_ip, ASL_NO_ROUTE); return; } if(src_ip != g_my_ip) {#ifdef DEBUG cout << "process_asl: during reboot, got non local packet " << endl;#endif timer_Q.resetRebootTimer(); route_discovery_done(dst_ip, ASL_NO_ROUTE); generateRERR(dst_ip, 0, true); } return; }#ifdef DEBUG cout << "aodv: process_asl: not during reboot " << endl;#endif /* check if a valid route table entry exist for this destination. * it might have happened that this entry is not yet added to the * kernel routing table because of ioctl latency */ if(rtable.findActiveRoute(dst_ip)) {#ifdef DEBUG cout << "aodv: process_asl: route already exist " << endl;#endif route_discovery_done(dst_ip, ASL_ROUTE_FOUND); return; } if(src_ip == g_my_ip) {#ifdef DEBUG cout << "aodv: process_asl: locally generated data packet" << endl; cout << "aodv: process_asl: dest ip is " << getDotIP(dst_ip) << endl;#endif /* data packet is generated locally */ if(protocol == IPPROTO_TCP) { /* set the G flag */#ifdef DEBUG cout << "aodv: process_asl:set G flag " << endl;#endif rflags = 1; } /* send out a rreq for locally generated data packets */ sendRREQ(dst_ip, rflags); } else { /* in case of intermediate node , check if local repair can be done */ /* the way ASL is designed, if a data packet is received for a dest * and if that dest happens to be in local repair list, then no * earlier attempt for local repair has been made, so the broken route * check for local repair is not really needed, but I anyway do it * just in case */ if(local_repair.findInList(dst_ip) != NULL) {#ifdef DEBUG cout << "aodv: process_asl:dest in local repair list " << endl;#endif /* do local repair here */ if(protocol == IPPROTO_TCP) rflags = 1; else rflags = 0; rtable_entry *rtEntry; rtEntry = rtable.findRoute(dst_ip); if((rtEntry != NULL) && (rtEntry->getRFlags()== BROKEN_ROUTE)) { cout << "aodv:process_asl: do loca repair " << endl; doLocalRepair(dst_ip, src_ip, rflags); } else { cout << "aodv: process_asl: local repair in progress.. " << endl; cout << "do not return any result to ASL now " << endl; return; } } else { /* return "route can not be discovered to asl * and generate a RERR */ route_discovery_done(dst_ip, ASL_NO_ROUTE); rtable_entry *rtEntry; cout << "aodv:process_asl: no route exist, return NO_ROUTE " << endl; rtEntry = rtable.findRoute(dst_ip); if(rtEntry != NULL) generateRERR(dst_ip,0,false); else generateRERR(dst_ip,0,true); } } return;}void aodv::packBuf(char *buf, RERR *rerrOb){ cout << "aodv: packBuf: entered" << endl; rerrOb->copyIntoBuf(buf); /* after this buf will have packed rerr in htonl() format */}/* this function is called to carry local repair */int aodv::doLocalRepair(u_int32_t dst, u_int32_t src, u_int8_t rflags){ RREQ rreqOb; u_int32_t orgHopCnt=0; int MIN_REPAIR_TTL, ttl; sendData snd; rtable_entry *rtEntry, *rtDestEntry;#ifdef DEBUG cout << " aodv: doLocalRepair: entered" << endl;#endif rreqOb.createRREQ(dst,rflags); rtEntry = rtable.findRoute(src); if(rtEntry != NULL ) { cout << "doLocalRepair: getting org hop cnt " << endl; if(rtEntry->getHopCnt() == INFINITY) orgHopCnt = rtEntry->getLastHopCnt(); else orgHopCnt = rtEntry->getHopCnt(); } rtDestEntry = rtable.findRoute(dst); if(rtDestEntry != NULL) { //rtDestEntry->setRFlags(LOCAL_REPAIR_ROUTE); MIN_REPAIR_TTL = rtDestEntry->getLastHopCnt(); } else MIN_REPAIR_TTL = 0; //ttl = (int)MAX(MIN_REPAIR_TTL, (0.5*orgHopCnt)) + LOCAL_ADD_TTL; ttl = (MIN_REPAIR_TTL) + LOCAL_ADD_TTL;#ifdef DEBUG cout << " aodv: doLocalRepair: orgHopCnt is " << orgHopCnt << "ttl is " << ttl << endl;#endif setLRTimer(dst, ttl); genSndData(&snd,BROADCAST_ADDR, ttl); copyToSendBuf((void *)&rreqOb, sizeof(RREQ)); sendPacket(&snd,(void*)sendBuffer,sizeof(RREQ)); return 0;}int aodv::sendRREQ(u_int32_t dst_ip, u_int8_t flags){ int retries=0; sendData snd;#ifdef DEBUG cout << "sendRREQ: entered" << endl;#endif /* this should never be true, checking to be on the safer side */ if(dst_ip == g_my_ip) { /* do not send a rreq for itself */ return 0; } RREQ rreqOb; rreqOb.createRREQ(dst_ip,flags); int ttl; /* set the lifetime of route entry */ /* a route table entry waiting for a RREP * should not be expunged before currtime+PATH+TRAVERSAL_TIME(sec 6.4) */ rtable_entry *rtEntry; rtEntry = rtable.findRoute(dst_ip); if(rtEntry != NULL) rtEntry->setLifeTime(getcurrtime()+PATH_TRAVERSAL_TIME); /* do expanding ring search */ if(expandingRingSearch) {#ifdef DEBUG cout << "aodv: sendRREQ: expanding ring search " << endl;#endif ttl=(int)rtable.getTTL(dst_ip); if(ttl == 0) ttl=TTL_START; else { /* an entry for the destination already exists */ /* update the lifetime of this entry */ ttl=ttl+TTL_INCREMENT; } }#ifdef DEBUG cout << "aodv: sendRREQ: dest ip is " << getDotIP(rreqOb.getDestIP()) << endl;#endif rreqL.addToList(rreqOb, retries, ttl ); setRREQTimer(dst_ip, ttl); genSndData(&snd,BROADCAST_ADDR, ttl); copyToSendBuf((void *)&rreqOb, sizeof(RREQ)); sendPacket(&snd,(void*)sendBuffer,sizeof(RREQ)); return 0;}/* this function sets the RREQ timer */int aodv::setRREQTimer(u_int32_t dst, int ttl){ struct timerData tData; tData.type = RREQ_TIMER; tData.data = dst; if(expandingRingSearch) { timer_Q.set_timer(2*ttl*NODE_TRAVERSAL_TIME, resendRREQ_handler, (void *)&tData); } else timer_Q.set_timer(NET_TRAVERSAL_TIME, resendRREQ_handler, (void *)&tData); return 0;}/* this function sets the local repiar timer */int aodv::setLRTimer(u_int32_t dst, int ttl){ struct timerData tData; tData.type = LR_TIMER; tData.data = dst;#ifdef DEBUG cout << "aodv: setLRTimer: entered" << endl;#endif timer_Q.set_timer(2*ttl*NODE_TRAVERSAL_TIME, LR_timer_handler, (void *)&tData); return 0;}void LR_timer_handler(void *data){#ifdef DEBUG cout << "aodv: LR_timer_handler: entered" << endl;#endif aodvOb.LR_handler(data); return;}void aodv::LR_handler(void *data){ u_int32_t dst; RERR rerrOb; rtable_entry *rtEntry;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -