📄 rrep.c
字号:
/* * Copyright (C) 2001, University of California, Santa Barbara * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Other copyrights might apply to parts of this software and are so * noted when applicable. *//* * Parts of this program has been derived from PIM sparse-mode pimd. * The pimd program is covered by the license in the accompanying file * named "LICENSE.pimd". * * The pimd program is COPYRIGHT 1998 by University of Southern California. * */#include <sys/types.h>#include "aodvConst.h"#include "aodvMsg.h"#include "aodvPktInfo.h"#include "aodvSocket.h"#include "const.h"#include "debug.h"#include "inet.h"#include "localRepair.h"#include "rerr.h"#include "rrep.h"#include "rreqResend.h"#include "sendDatagram.h"#include "routingTable.h"#include "packet_input.h"void fill_my_rrep(struct RREP *rrep){ rrep->type=AODV_RREP; rrep->R=0; rrep->A=0; //TODO FUTURE rrep->reserved1=0; rrep->reserved2=0; rrep->prefixSize=0; //TODO FUTURE rrep->hopCount=0; rrep->destSeqNum=htonl(mySeqNum); rrep->lifetime=htonl(MY_ROUTE_TIMEOUT);}static void fill_grat_rrep(struct RREP *rrep , u_int32_t dest , u_int32_t src){ rrep->type=AODV_RREP; rrep->R=0; rrep->A=0; //TODO FUTURE rrep->reserved1=0; rrep->reserved2=0; rrep->prefixSize=0; //TODO FUTURE rrep->src=htonl(dest); rrep->dest=htonl(src); rrep->hopCount=getHopCount(src); rrep->destSeqNum=htonl(getDestinationSeqNum(src)); rrep->lifetime=htonl(getLifetime(src)-getTime());}static void fill_other_rrep(struct RREP *rrep , u_int32_t dest, u_int32_t destSeqNum){ rrep->type=AODV_RREP; rrep->R=0; rrep->A=0; //TODO FUTURE rrep->reserved1=0; rrep->reserved2=0; rrep->prefixSize=0; //TODO FUTURE rrep->hopCount=getHopCount(dest) + 1; rrep->destSeqNum=htonl(getDestinationSeqNum(dest)); rrep->lifetime=htonl(getLifetime(dest) - getTime());}void generateRREP(u_int32_t src , u_int32_t dest , u_int32_t destSeqNum , int G){ struct RREP rrep; struct AodvPktInfo info; trace(TRACE_RREP,"generateRREP"); rrep.src=htonl(src); rrep.dest=htonl(dest); //check if I'm the destination if(getip() == htonl(dest)){ //could be done differenty see aodv draft 8.4 if (destSeqNum > mySeqNum){ mySeqNum = destSeqNum; trace(TRACE_RT,"Changing SeqNum to %d because rrep had larger mySeqNum" ,mySeqNum); } fill_my_rrep(&rrep); } else { fill_other_rrep(&rrep,dest,destSeqNum); addPrecursor(src,getNextHop(dest)); } //create pktinfo fill_pktInfo(&info,getNextHop(src)); trace(TRACE_RREP,"generateRREP: sending RREP"); printRREP(&rrep); trace(TRACE_DEMO,"sending RREP to %s for %s" ,inet_fmt_n(info.dest,s1),inet_fmt_n(rrep.src,s2)); //send on datagram trace(TRACE_METRIC,"s RREP"); sendDatagram(&info,&rrep,sizeof(struct RREP)); if(G == 1){ fill_grat_rrep(&rrep,dest,src); fill_pktInfo(&info,getNextHop(dest)); trace(TRACE_RREP,"generateRREP: sending Grat. RREP"); trace(TRACE_DEMO,"sending Grat. RREP to %s" ,inet_fmt_h(dest,s1)); printRREP(&rrep); //send on datagram trace(TRACE_METRIC,"s g RREP"); sendDatagram(&info,&rrep,sizeof(struct RREP)); }}void receiveRREP(struct AodvPktInfo *info , struct RREP *rrep) { struct RoutingTableEntry *entry = NULL; //u_int32_t destNumInRoutingTable; u_int32_t hopCountInRoutingTable; trace(TRACE_RREP,"receiveRREP"); printRREP(rrep); if((info->src == rrep->dest) && (rrep->hopCount == 0)){ //this is a hello message updateNeighbor(ntohl(info->src),ntohl(rrep->destSeqNum)); trace(TRACE_METRIC,"r h RREP"); return; }else{ trace(TRACE_METRIC,"r RREP"); updateNeighbor(ntohl(info->src),0); } entry=getRoute(ntohl(rrep->dest)); if((entry == NULL) || ((entry != NULL) && ((ntohl(rrep->destSeqNum) > entry->destSeqNum) || ((ntohl(rrep->destSeqNum) == entry->destSeqNum) && ((entry->hopCount == DELETE_ROUTE) || (rrep->hopCount <= entry->hopCount)))))){ trace(TRACE_RREP,"received RREP: updating route"); removeFromRreqResend(ntohl(rrep->dest)); if((localRepair == TRUE) && (inLRList(ntohl(rrep->dest)))){ if(hopCountInRoutingTable < rrep->hopCount){ createAndSendRERR(ntohl(rrep->dest),1); } removeFromLRList(ntohl(rrep->dest)); } updateRoute(ntohl(info->src),rrep); //check if I'm the destination if(getip() != rrep->src){ trace(TRACE_RREP,"received RREP: wasn't destination"); (rrep->hopCount)++; addPrecursor(ntohl(rrep->dest),getNextHop(ntohl(rrep->src))); updateLifetime(ntohl(rrep->dest),ntohl(rrep->lifetime)); info->dest=ntohl(getNextHop(ntohl(rrep->src))); info->src=getip(); trace(TRACE_RREP,"received RREP: forwarding rrep->src=%s" ,inet_fmt_n(rrep->src,s1)); trace(TRACE_RREP,"received RREP: forwarding rrep->dest=%s" ,inet_fmt_n(rrep->dest,s1)); (info->ttl)--; if(info->ttl <= 0){ return; } trace(TRACE_DEMO|TRACE_RREP ,"forwarding RREP from %s to %s" ,inet_fmt_n(info->src,s1),inet_fmt_n(rrep->src,s2)); trace(TRACE_METRIC,"s f RREP"); sendDatagram(info,rrep,sizeof(struct RREP)); } else { packet_buff_send(ntohl(rrep->dest)); trace(TRACE_DEMO|TRACE_RREP ,"received RREP from %s: for me" ,inet_fmt_n(info->src,s1),inet_fmt_n(rrep->dest,s2)); } } else { trace(TRACE_RREP,"received RREP: larger num hops throwing away rrep"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -