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

📄 flood.cc

📁 在NS-2环境中实现了移动Ad Hoc的两种广播算法:Flood和Bcast。里面本来有个test目录
💻 CC
字号:
/******************************************************************* Copyright (C) 2004 Thomas Kunz, CRC Canada, BCAST for IPv4. DISTRIBUTED WITH NO WARRANTY, EXPRESS OR IMPLIED. See the GNU Library General Public License (file COPYING in the MANET_multicast directory) for conditions of use and redistribution.*********************************************************************//* Simple FLOODING routing protocol, derived from AODV code. */#include <flood/flood.h>#include <MANET_multicast/mttable.h>#include <random.h>#include <cmu-trace.h>#define CURRENT_TIME    Scheduler::instance().clock()//#define DEBUG//#define ERROR/*  TCL Hooks*/static class FLOODclass : public TclClass {public:        FLOODclass() : TclClass("Agent/FLOOD") {}        TclObject* create(int argc, const char*const* argv) {          assert(argc == 5);          return (new FLOOD((nsaddr_t) atoi(argv[4])));        }} class_rtProtoFLOOD;intFLOOD::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) {      uid_handler.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], "flood-join-group")==0) {      nsaddr_t mcast_addr = atoi(argv[2]);      if (mcast_addr < IP_MULTICAST_RANGE) return TCL_ERROR;				      mt_entry *mt = mtable.mt_lookup(mcast_addr);      if (!mt) mt = mtable.mt_add(mcast_addr);      return TCL_OK;    }    else if(strcmp(argv[1], "flood-leave-group")==0) {      nsaddr_t mcast_addr = atoi(argv[2]);      if (mcast_addr < IP_MULTICAST_RANGE) return TCL_ERROR;				      mt_entry *mt = mtable.mt_lookup(mcast_addr);      if (!mt) mtable.mt_delete(mcast_addr);      return TCL_OK;    }    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*/FLOOD::FLOOD(nsaddr_t id) : Agent(PT_FLOOD), uid_handler(), mtable() {  index = id;  ifqueue = 0;}/*  Packet Reception Routines*/voidFLOOD::recv(Packet *p, Handler*) {struct hdr_cmn *ch = HDR_CMN(p);struct hdr_ip *ih = HDR_IP(p);nsaddr_t src = ih->saddr();unsigned int unique_id = (unsigned int)ch->uid();mt_entry *mt; assert(initialized()); /*  *  Must be a packet I'm originating...  */ if((ih->saddr() == index) && (ch->num_forwards() == 0)) { /*  * Add the IP Header  */   ch->size() += IP_HDR_LEN;   ih->ttl_ = NETWORK_DIAMETER; } else {   ih->ttl_ -= 1;  if(ih->ttl_ == 0) {#ifdef DEBUG   fprintf(stderr, "%s: calling drop()\n", __PRETTY_FUNCTION__);#endif // DEBUG   drop(p, DROP_RTR_TTL);   return;  } } if (uid_handler.id_lookup(src, unique_id)) {#ifdef DEBUG   fprintf(stderr, "%s: discarding request\n", __FUNCTION__);#endif // DEBUG   Packet::free(p);   return; } /*  * Check whether this packet belongs to a multicast group this node subscribed to?  */ mt = mtable.mt_lookup(ih->daddr()); if (mt) {   // Need to make a copy of the packet   Packet *p_new = p->copy();   struct hdr_cmn *ch_new = HDR_CMN(p_new);   ch_new->addr_type() = NS_AF_INET;   ch_new->direction() = hdr_cmn::UP;   struct hdr_ip *ih_new = HDR_IP(p_new);   // "strip" the IP header...   ch_new->size() -= IP_HDR_LEN;   // send to "myself" at port 0 (assume that receiving NULL agent sits there...)   ih_new->daddr() = index;   ih_new->dport() = 0;   // pass packet to Link Layer, from where it will be passed up the stack to   // agent at port 0 (routing agent cannot directly pass packets up the stack   // unless we emulate the way DSR agents are linked in the protocol stack)   Scheduler::instance().schedule(target_, p_new, 0.); }  /*  * Cache the broadcast ID  */ uid_handler.id_insert(src, unique_id); ch->addr_type() = NS_AF_ILINK; // allows us to force MAC broadcast  ch->direction() = hdr_cmn::DOWN; //important: change the packet's direction /* force MAC broadcast, bypassing ARP etc. Better than IP broadcast, which    would override IP destination address */ ch->next_hop_ = MAC_BROADCAST; /*  *  Jitter the sending by 10ms  */ Scheduler::instance().schedule(target_, p,      				   0.01 * Random::uniform()); return;}

⌨️ 快捷键说明

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