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

📄 aodv.cc

📁 在NS环境下对aodv路由协议进行仿真
💻 CC
📖 第 1 页 / 共 5 页
字号:
//*****************************************************************************//Ali Hamidian, aodv.cc 0.2.//*****************************************************************************/*  Copyright (c) 1997, 1998 Carnegie Mellon University.  All Rights  Reserved.   Permission to use, copy, modify, and distribute this  software and its documentation is hereby granted (including for  commercial or for-profit use), provided that both the copyright notice  and this permission notice appear in all copies of the software,  derivative works, or modified versions, and any portions thereof, and  that both notices appear in supporting documentation, and that credit  is given to Carnegie Mellon University in all publications reporting  on direct or indirect use of this code or its derivatives.  ALL CODE, SOFTWARE, PROTOCOLS, AND ARCHITECTURES DEVELOPED BY THE CMU  MONARCH PROJECT ARE EXPERIMENTAL AND ARE KNOWN TO HAVE BUGS, SOME OF  WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THIS  SOFTWARE OR OTHER INTELLECTUAL PROPERTY IN ITS ``AS IS'' CONDITION,  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE OR  INTELLECTUAL PROPERTY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH  DAMAGE.  Carnegie Mellon encourages (but does not require) users of this  software or intellectual property to return any improvements or  extensions that they make, and to grant Carnegie Mellon the rights to  redistribute these changes without encumbrance.  The AODV code developed by the CMU/MONARCH group was optimized  and tuned by Samir Das and Mahesh Marina, University of Cincinnati. The  work was partially done in Sun Microsystems. *//*  * ############################################################################ * This code was enhanced by Ali Hamidian, Department of Communication Systems, * Lund Institute of Technology, Lund University. * ns-2.1b9a/ns-2.26/ns-2.27 enhancements for simulations of Internet  * connectivity for mobile ad hoc networks with AODV. * e-mail: alex.hamidian@telecom.lth.se * ############################################################################ *///#include <ip.h>#include <aodv/aodv.h>#include <aodv/aodv_packet.h>#include <random.h>#include <cmu-trace.h>//#include <energy-model.h>//My modification************************************************************//#include <mobilenode.h> //For base_stn() and set_base_stn(int addr)//***************************************************************************//#define max(a,b)        ( (a) > (b) ? (a) : (b) )#define CURRENT_TIME    Scheduler::instance().clock()//#define DEBUG#ifdef DEBUGstatic int route_request = 0;#endif/* ===================================================================   TCL Hooks   ================================================================= */int hdr_aodv::offset_;static class AODVHeaderClass : public PacketHeaderClass {public:  AODVHeaderClass() : PacketHeaderClass("PacketHeader/AODV",					sizeof(hdr_all_aodv)) {    bind_offset(&hdr_aodv::offset_);  } } class_rtProtoAODV_hdr;static class AODVclass : public TclClass {public:  AODVclass() : TclClass("Agent/AODV") {}  TclObject* create(int argc, const char*const* argv) {    assert(argc == 5);    //return (new AODV((nsaddr_t) atoi(argv[4])));    return (new AODV((nsaddr_t) Address::instance().str2addr(argv[4])));  }} class_rtProtoAODV;intAODV::command(int argc, const char*const* argv) {  if(argc == 2) {    Tcl& tcl = Tcl::instance();        if(strncasecmp(argv[1], "id", 2) == 0) {      tcl.resultf("%d", index);      return TCL_OK;    }        if(strncasecmp(argv[1], "start", 2) == 0) {      btimer.handle((Event*) 0);#ifndef AODV_LINK_LAYER_DETECTION      htimer.handle((Event*) 0);      ntimer.handle((Event*) 0);#endif // LINK LAYER DETECTION      //My modification******************************************************//      /*	Implementing proactive and hybrid gateway discovery method...      */      //Call AdvertisementTimer::handle(Event*)      adtimer.handle((Event*) 0);      //*********************************************************************//      rtimer.handle((Event*) 0);      return TCL_OK;    }  }  else if(argc == 3) {    if(strcmp(argv[1], "index") == 0) {      index = atoi(argv[2]);      return TCL_OK;    }    else if(strcmp(argv[1], "log-target") == 0 || 	    strcmp(argv[1], "tracetarget") == 0) {      logtarget = (Trace*) TclObject::lookup(argv[2]);      if(logtarget == 0)	return TCL_ERROR;      return TCL_OK;    }    else if(strcmp(argv[1], "drop-target") == 0) {      int stat = rqueue.command(argc,argv);      if(stat != TCL_OK) return stat;      return Agent::command(argc, argv);    }    else if(strcmp(argv[1], "if-queue") == 0) {      ifqueue = (PriQueue*) TclObject::lookup(argv[2]);            if(ifqueue == 0)	return TCL_ERROR;      return TCL_OK;    }    else if (strcmp(argv[1], "port-dmux") == 0) {      dmux_ = (PortClassifier *)TclObject::lookup(argv[2]);      if (dmux_ == 0) {	fprintf (stderr, "%s: %s lookup of %s failed\n", __FILE__,		 argv[1], argv[2]);	return TCL_ERROR;      }      return TCL_OK;    }  }  return Agent::command(argc, argv);}/* ===================================================================   Constructor   ================================================================= */AODV::AODV(nsaddr_t id) : Agent(PT_AODV),			  btimer(this), htimer(this), ntimer(this), 			  rtimer(this), lrtimer(this), adtimer(this), rqueue(){   index = id;  seqno = 2;  bid = 1;  //My modification**********************************************************//  thisnode = (MobileNode *) (Node::get_node_by_address(id));  /*    Implementing proactive gateway discovery method...  */  ad_bid = 1;  /*    Implementing proactive and hybrid gateway discovery method...    if gw_discovery==0: Proactive gateway discovery on    if gw_discovery==1: Hybrid gateway discovery on  */  bind("gw_discovery", &gw_discovery);  //*************************************************************************//    LIST_INIT(&nbhead);  LIST_INIT(&bihead);  logtarget = 0;  ifqueue = 0;}/* ===================================================================   Timers   ================================================================= */voidBroadcastTimer::handle(Event*) {  agent->id_purge();  Scheduler::instance().schedule(this, &intr, BCAST_ID_SAVE);}//My comment*****************************************************************///*  This function is invoked ONLY if AODV_LINK_LAYER_DETECTION is NOT defined.   The function uses Hello messages instead of link layer (802.11) feedback to   determine when links are up/down.*///***************************************************************************//voidHelloTimer::handle(Event*) {  agent->sendHello();  double interval = MinHelloInterval +     ((MaxHelloInterval - MinHelloInterval) * Random::uniform());  assert(interval >= 0);  Scheduler::instance().schedule(this, &intr, interval);}//My comment*****************************************************************///*  This function is invoked ONLY if AODV_LINK_LAYER_DETECTION is NOT defined.  See aodv.h.*///***************************************************************************//voidNeighborTimer::handle(Event*) {  agent->nb_purge();  Scheduler::instance().schedule(this, &intr, HELLO_INTERVAL);}voidRouteCacheTimer::handle(Event*) {  agent->rt_purge();#define FREQUENCY 0.5 // sec  Scheduler::instance().schedule(this, &intr, FREQUENCY);}voidLocalRepairTimer::handle(Event* p)  {  // SRD: 5/4/99  aodv_rt_entry *rt;  struct hdr_ip *ih = HDR_IP( (Packet *)p);    /* you get here after the timeout in a local repair attempt */  /*	fprintf(stderr, "%s\n", __FUNCTION__); */      rt = agent->rtable.rt_lookup(ih->daddr());    if(rt && rt->rt_flags != RTF_UP) {    // route is yet to be repaired    // I will be conservative and bring down the route    // and send route errors upstream.    /* The following assert fails, not sure why */    /* assert (rt->rt_flags == RTF_IN_REPAIR); */        //rt->rt_seqno++;    agent->rt_down(rt);    // send RERR#ifdef DEBUG    fprintf(stderr,"\n\n*** Node %d: Dst - %d, failed local repair at %f!\n\n",	    index, rt->rt_dst, CURRENT_TIME);#endif        }  Packet::free((Packet *)p);}//My modification************************************************************///*  Implementing proactive and hybrid gateway discovery method...*/voidAdvertisementTimer::handle(Event*) {  static bool printed = false;    if(agent->gw_discovery == 0) {    /*      Proactive gateway discovery - call sendAdvertisement      The packet sent is defined as a new AODV packet: AODVTYPE_ADVERTISEMENT    */    if(!printed) {      fprintf(stderr, "\n*************************************************\n");      fprintf(stderr, "\t Proactive gateway discovery");      fprintf(stderr, "\n*************************************************\n");      printed = true;    }    agent->sendAdvertisement(NETWORK_DIAMETER);        //Randomize the sending of broadcast packets to reduce collisions    double interval =  ADVERTISEMENT_INTERVAL * Random::uniform(0.85, 1.15);    Scheduler::instance().schedule(this, &intr, interval);  }  else if(agent->gw_discovery == 1) {    /*      Hybrid gateway discovery - call sendAdvertisement      The packet sent is defined as an new AODV packet: AODVTYPE_ADVERTISEMENT    */    if(!printed) {      fprintf(stderr, "\n*************************************************\n");      fprintf(stderr, "\t Hybrid gateway discovery");      fprintf(stderr, "\n*************************************************\n");      printed = true;    }    agent->sendAdvertisement(ADVERTISEMENT_ZONE);      //Randomize the sending of broadcast packets to reduce collisions    double interval =  ADVERTISEMENT_INTERVAL * Random::uniform(0.85, 1.15);    Scheduler::instance().schedule(this, &intr, interval);  }  else if(agent->gw_discovery == 2) {    /*      Reactive gateway discovery - do nothing    */    if(!printed) {      fprintf(stderr, "\n*************************************************\n");      fprintf(stderr, "\t Reactive gateway discovery");      fprintf(stderr, "\n*************************************************\n");      printed = true;    }  }  else {    fprintf(stderr, "\n\nNo gateway discovery method chosen! Add the following"	    " line in your Tcl file:"	    "\n\tAgent/AODV set gw_discovery <0 or 1 or 2>\n\n");    exit(1);  }}//***************************************************************************///* =====================================================================   Broadcast ID Management Functions   ===================================================================== */voidAODV::id_insert(nsaddr_t id, u_int32_t bid) {  BroadcastID *b = new BroadcastID(id, bid);    assert(b);  b->expire = CURRENT_TIME + BCAST_ID_SAVE;  LIST_INSERT_HEAD(&bihead, b, link);}/* SRD */boolAODV::id_lookup(nsaddr_t id, u_int32_t bid) {  BroadcastID *b = bihead.lh_first;    // Search the list for a match of source and bid  for( ; b; b = b->link.le_next) {    if((b->src == id) && (b->id == bid))      return true;       }  return false;}voidAODV::id_purge() {  BroadcastID *b = bihead.lh_first;  BroadcastID *bn;  double now = CURRENT_TIME;    for(; b; b = bn) {    bn = b->link.le_next;    if(b->expire <= now) {      LIST_REMOVE(b,link);      delete b;    }  }}/* =====================================================================   Helper Functions   ===================================================================== */doubleAODV::PerHopTime(aodv_rt_entry *rt) {  int num_non_zero = 0, i;  double total_latency = 0.0;    if(!rt)    return ((double) NODE_TRAVERSAL_TIME );    //==> MAX_HISTORY is defined to 3 in rttable.h.  for(i=0; i < MAX_HISTORY; i++) {    if(rt->rt_disc_latency[i] > 0.0) {      num_non_zero++;      total_latency += rt->rt_disc_latency[i];    }  }  if(num_non_zero > 0)    return(total_latency / (double) num_non_zero);  else    return((double) NODE_TRAVERSAL_TIME);  }/* =====================================================================   Link Failure Management Functions   ===================================================================== */static void

⌨️ 快捷键说明

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