⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rreq.c

📁 ucsb大学开发的aodv路由协议代码。
💻 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 <arpa/inet.h>#include <errno.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/types.h>#include <unistd.h>#include "aodvConst.h"#include "aodvMsg.h"#include "aodvSocket.h"#include "const.h"#include "debug.h"#include "inet.h"#include "routingTable.h"#include "routingTableEntry.h"#include "rrep.h"#include "rreq.h"#include "rreqResend.h"#include "rreqTable.h"#include "sendDatagram.h"int expandingRingSearch = FALSE;//TODO FUTURE HOW DO YOU KNOW WHAT TO PUT IN G?struct RREQ *newRREQ(u_int32_t dest, unsigned int G) {  struct RREQ *rreq;  trace(TRACE_RREQ,"newRREQ");  rreq = (struct RREQ *)malloc(sizeof(struct RREQ));  if(rreq == NULL){    log(LOG_ERR,TRUE,"unable to allocate memory, RREQ");  }    rreq->type = AODV_RREQ;  rreq->J = 0;  //TODO FUTURE what should go here?   rreq->R = 0;  //TODO FUTURE what should go here?   rreq->G = G;   rreq->reserved1 = 0;  rreq->reserved2 = 0;  rreq->hopCount = 0;  rreq->broadcastId = htonl(broadcastId++);  rreq->dest = htonl(dest);  rreq->destSeqNum = htonl(getDestinationSeqNum(dest));  rreq->src = getip();  mySeqNum++;  rreq->srcSeqNum = htonl(mySeqNum);  return rreq;}void receiveRREQ(struct AodvPktInfo *info , struct RREQ *rreq) {  struct RoutingTableEntry *rt_dst = NULL;  //u_int32_t destSeqNumInRoutingTable;  if(rreq->src == getip()){    //disregard rreq from self    trace(TRACE_RREQ,"received RREQ - disregarding because from self");    printRREQ(rreq);    return;  }  trace(TRACE_RREQ,"received RREQ");  printRREQ(rreq);  updateNeighbor(ntohl(info->src),0);  if(!receivedAlready(ntohl(rreq->src),ntohl(rreq->broadcastId))){    trace(TRACE_DEMO,"received RREQ from host %s"	  ,inet_fmt_n(info->src,s1));    trace(TRACE_DEMO,"received RREQ src:%s dst:%s"	  ,inet_fmt_n(rreq->src,s1)	  ,inet_fmt_n(rreq->dest,s2));    //always    updateReverseRoute(ntohl(info->src),rreq);    rt_dst = getRoute(ntohl(rreq->dest));    if(rreq->dest==getip()){      //I'm the destintation      trace(TRACE_RREQ,"received RREQ: this is the destination");      generateRREP(ntohl(rreq->src)		   ,ntohl(rreq->dest)		   ,ntohl(rreq->destSeqNum)		   ,rreq->G);    }else if((rt_dst != NULL) 	     && (rt_dst->hopCount != DELETE_ROUTE) 	     && (rt_dst->destSeqNum >= ntohl(rreq->destSeqNum))){      trace(TRACE_RREQ,"received RREQ: have active route");      generateRREP(ntohl(rreq->src)		   ,ntohl(rreq->dest)		   ,ntohl(rreq->destSeqNum)		   ,rreq->G);    }else {      //if((rt_dst == NULL) || (rt_dst->hopCount == DELETE_ROUTE)){      trace(TRACE_RREQ,"received RREQ: no active route");      rt_dst = getRoute(ntohl(rreq->dest));      rreq->hopCount++;      //fill out pktinfo      info->src=getip();      info->dest=BROADCAST;      (info->ttl)--;      //check TTL      if (info->ttl > 0){	//set destSeqNum	if((rt_dst != NULL) && (ntohl(rreq->destSeqNum) < rt_dst->destSeqNum)){	  rreq->destSeqNum = htonl(rt_dst->destSeqNum);	}	trace(TRACE_RREQ,"forwarding RREQ %s:%u"	      ,inet_fmt_n(rreq->src,s1),ntohl(rreq->srcSeqNum));	trace(TRACE_DEMO,"forwarding RREQ %s"	      ,inet_fmt_n(rreq->src,s1));	printRREQ(rreq);	trace(TRACE_METRIC,"s f RREQ");	sendDatagram(info,rreq,sizeof(struct RREQ));      }    }  }}void generateRREQ(u_int32_t dest,unsigned int G){  struct RREQ *rreq = NULL;  struct AodvPktInfo *info = NULL;  int lastHopCount;  //check if RREQ already pending  if(rreqPending(dest)){    trace(TRACE_RREQ,"rreq already pending");     return;  }  trace(TRACE_RREQ,"generateRREQ");   rreq = newRREQ(dest,G);  info = new_pktInfo(BROADCAST);  if(expandingRingSearch == TRUE){    lastHopCount = getTTL(dest);    if(lastHopCount == 0) {      info->ttl = TTL_START;    } else {      info->ttl = lastHopCount + TTL_INCREMENT;    }   }  //insertRREQintoResendList handles timers for resend  insertRREQIntoResendList(info,rreq);  trace(TRACE_RREQ|TRACE_DEMO,"generate RREQ for %s"	,inet_fmt_n(rreq->dest,s1));  printRREQ(rreq);  trace(TRACE_METRIC,"s RREQ");  sendDatagram(info,rreq,sizeof(struct RREQ));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -