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

📄 aodv-uu.cc

📁 AODV version of Uppsala University
💻 CC
📖 第 1 页 / 共 2 页
字号:
/* Sends a packet using the specified next hop and delay */void NS_CLASS sendPacket(Packet *p, unsigned int ifindex, struct in_addr next_hop, double delay){    struct hdr_cmn *ch = HDR_CMN(p);    struct hdr_ip *ih = HDR_IP(p);    double jitter = 0.0;    struct in_addr dest_addr;	    dest_addr.s_addr = ih->daddr();    /* If TTL = 0, drop packet */    if (ih->ttl() == 0) {	DEBUG(LOG_WARNING, 0, "Dropping packet with TTL = 0.");	drop(p, 0, DROP_RTR_TTL);	return;    }    /* Get the ipaddress of this interface (ifindex) */    nsaddr_t ifaddr = (nsaddr_t) DEV_IFINDEX(ifindex).ipaddr.s_addr;    /* Set packet fields depending on packet type */    if (dest_addr.s_addr == AODV_BROADCAST) {        /* Broadcast packet */        ch->next_hop_ = AODV_BROADCAST;        ch->prev_hop_ = ifaddr;        ch->addr_type() = NS_AF_NONE;        ch->direction() = hdr_cmn::DOWN;        jitter = 0.02 * Random::uniform();        /* Find correct ip-interface module and senddown */        sendDown(ifindex, p, jitter);    } else {        /* Unicast packet */        ch->next_hop_ = next_hop.s_addr;        ch->prev_hop_ = ifaddr;        ch->addr_type() = NS_AF_INET;        ch->direction() = hdr_cmn::DOWN;        if (llfeedback) {            ch->xmit_failure_ = link_layer_callback;            ch->xmit_failure_data_ = (void *) this;        }        /* Find correct ip-interface module and senddown */        sendDown(ifindex, p, delay);    }}/* Interpreter for commands from Tcl */int NS_CLASS command(int argc, const char *const *argv){    TclObject *obj;    Tcl& tcl = Tcl::instance();        if (strcasecmp(argv[1], "start") == 0) {	return startAODVUUAgent();    }        /* tracetarget: target for trace info specific for the routing agent */    if (strcasecmp(argv[1], "tracetarget") == 0) {	// Note: Currently no such trace info is generated.	return TCL_OK;    }        if (argc == 3) {		obj = TclObject::lookup(argv[2]);#ifdef CONFIG_GATEWAY        if(strcasecmp(argv[1], "set-default-gw") == 0) {            gw_default = str2addr(argv[2]);            return TCL_OK;        }#endif        if (strcasecmp(argv[1], "if-queue") == 0) {            if(obj == 0)                return TCL_ERROR;            else {                ifqueues.push_back((PriQueue *)obj);                return TCL_OK;            }        }        if (strcasecmp(argv[1], "add-if") == 0) {            IPModule *ipif = dynamic_cast<IPModule *>(obj);            if (ipif)            {                tcl.evalf("%s addr", ipif->name());                nsaddr_t ipaddr = (in_addr_t) atoi(tcl.result());                tcl.evalf("%s subnet", ipif->name());                nsmask_t subnet = (in_addr_t) atoi(tcl.result());                initInterface(ipaddr, subnet, ipif->getId());                return TCL_OK;            }            return TCL_ERROR;        }#ifdef CONFIG_GATEWAY        if (strcasecmp(argv[1], "add-gwif") == 0) {            IPModule *ipif = dynamic_cast<IPModule *>(obj);            if (ipif)            {                tcl.evalf("%s addr", ipif->name());                gw_interface.ipaddr = (nsaddr_t) atoi(tcl.result());                tcl.evalf("%s subnet", ipif->name());                gw_interface.subnet = (nsmask_t) atoi(tcl.result());                gw_interface.moduleid = ipif->getId();                return TCL_OK;            }            return TCL_ERROR;        }#endif    }    /* Unknown commands are passed to the Module base class */    return Module::command(argc, argv);}/* Starts the AODV-UU routing agent */int NS_CLASS startAODVUUAgent(){    if (initialized == 0) {	log_init();	/* Set up the wait-on-reboot timer */	if (wait_on_reboot) {	    timer_init(&worb_timer, &NS_CLASS wait_on_reboot_timeout, &wait_on_reboot);	    timer_set_timeout(&worb_timer, DELETE_PERIOD);	    DEBUG(LOG_NOTICE, 0, "In wait on reboot for %d milliseconds.",		  DELETE_PERIOD);	}	/* Schedule the first HELLO */	if (!llfeedback && !optimized_hellos)	    hello_start();	/* Initialize routing table logging */	if (rt_log_interval)	    log_rt_table_init();	/* Initialization complete */	initialized = 1;	DEBUG(LOG_DEBUG, 0, "Routing agent started.");	DEBUG(LOG_DEBUG, 0, "Settings:");	DEBUG(LOG_DEBUG, 0, "unidir_hack %s", unidir_hack ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "rreq_gratuitous %s", rreq_gratuitous ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "expanding_ring_search %s", expanding_ring_search ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "local_repair %s", local_repair ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "receive_n_hellos %s", receive_n_hellos ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "hello_jittering %s", hello_jittering ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "wait_on_reboot %s", wait_on_reboot ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "optimized_hellos %s", optimized_hellos ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "ratelimit %s", ratelimit ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "llfeedback %s", llfeedback ? "ON" : "OFF");	DEBUG(LOG_DEBUG, 0, "internet_gw_mode %s", internet_gw_mode ? "ON" : "OFF");	if (llfeedback) {	    active_route_timeout = ACTIVE_ROUTE_TIMEOUT_LLF;	    ttl_start = TTL_START_LLF;	    delete_period =  DELETE_PERIOD_LLF;	} else {	    active_route_timeout = ACTIVE_ROUTE_TIMEOUT_HELLO;	    ttl_start = TTL_START_HELLO;	    delete_period = DELETE_PERIOD_HELLO;	}	DEBUG(LOG_DEBUG, 0, "ACTIVE_ROUTE_TIMEOUT=%d", ACTIVE_ROUTE_TIMEOUT);	DEBUG(LOG_DEBUG, 0, "TTL_START=%d", TTL_START);	DEBUG(LOG_DEBUG, 0, "DELETE_PERIOD=%d", DELETE_PERIOD);		/* Schedule the first timeout */	scheduleNextEvent();	return TCL_OK;    } else {	/* AODV-UU routing agent already started */	return TCL_ERROR;    }}Packet *NS_CLASS pkt_encapsulate(Packet *p, struct in_addr gw_addr) {    struct in_addr dest;    Packet *ep = allocpkt(); //sizeof(Packet*));    if (!ep) {        DEBUG(LOG_DEBUG, 0, "Enc ep==NULL!");        return NULL;    }    hdr_encap *eh = hdr_encap::access(ep);    eh->encap(p);    hdr_cmn* ch_e = hdr_cmn::access(ep);    hdr_cmn* ch_p = hdr_cmn::access(p);#define MIN_ENC_OVERHEAD 8    ch_e->ptype() = PT_ENCAPSULATED;    ch_e->size() = ch_p->size() + IP_HDR_LEN + MIN_ENC_OVERHEAD;    ch_e->timestamp() = ch_p->timestamp();    struct hdr_ip *ih_e = HDR_IP(ep);    struct hdr_ip *ih_p = HDR_IP(p);    dest.s_addr = ih_p->daddr();    ih_e->daddr() = gw_addr.s_addr;    ih_e->saddr() = ih_p->saddr();	ih_e->ttl() = ih_p->ttl();    return ep;}Packet *NS_CLASS pkt_decapsulate(Packet *p) {    hdr_cmn* ch = hdr_cmn::access(p);    Packet *p_dec;        if (ch->ptype() == PT_ENCAPSULATED) {	hdr_encap *eh = hdr_encap::access(p);	/* Now decapsulate the packet */	p_dec = eh->decap();		/* Free the decapsulator packet */	Packet::free(p);	return p_dec;    }    return NULL;}/*  Reschedules the timer queue timer to go off at the time of the  earliest event (so that the timer queue will be investigated then).  Should be called whenever something might have changed the timer queue.*/void NS_CLASS scheduleNextEvent(){    struct timeval *timeout;    timeout = timer_age_queue();    if (timeout)	tqtimer.resched((double) timeout->tv_sec +			(double) timeout->tv_usec / (double) 1000000);}/*  Replacement for gettimeofday(), used for timers.  The timeval should only be interpreted as number of seconds and  fractions of seconds since the start of the simulation.*/int NS_CLASS gettimeofday(struct timeval *tv, struct timezone *tz){    double current_time, tmp;    /* Timeval is required, timezone is ignored */    if (!tv)	return -1;    current_time = Scheduler::instance().clock();    tv->tv_sec = (long)current_time; /* Remove decimal part */    tmp = (current_time - tv->tv_sec) * 1000000;    tv->tv_usec = (long)tmp;    return 0;}/*  Replacement for if_indextoname(), used in routing table logging.*/char *NS_CLASS if_indextoname(int ifindex, char *ifname){    assert(ifindex >= 0);    strncpy(ifname, DEV_IFINDEX(ifindex).ifname, IFNAMSIZ - 1);    DEV_IFINDEX(ifindex).ifname[IFNAMSIZ - 1] = '\0';    return ifname;}void NS_CLASS initInterface(in_addr_t ipaddr, in_addr_t subnet, int ifindex){        int dev_nr = this_host.nif;        /* Set network interface parameters */        char ifname[] = "nsifx";        DEV_NR(dev_nr).enabled = 1;        DEV_NR(dev_nr).sock = -1;        DEV_NR(dev_nr).ifindex = ifindex;        dev_indices[dev_nr] = ifindex;        ifname[4] = '0' + dev_nr;        strncpy(DEV_NR(dev_nr).ifname, ifname, IFNAMSIZ - 1);        DEV_NR(dev_nr).ifname[IFNAMSIZ - 1] = '\0';        //Netmask not used in simulations        DEV_NR(dev_nr).netmask.s_addr = subnet;        DEV_NR(dev_nr).broadcast.s_addr = AODV_BROADCAST;        /* Set ip addr */        DEV_NR(dev_nr).ipaddr.s_addr = ipaddr;        this_host.nif += 1;}#ifdef CONFIG_GATEWAYvoid NS_CLASS sendGwPacket(Packet *p){    hdr_cmn* ch = HDR_CMN(p);    hdr_ip* ih = HDR_IP(p);    /* If destination is on local subnet, next_hop_ is destination, otherwise default gw is used */    if((ih->daddr() & gw_interface.subnet) == (gw_interface.ipaddr & gw_interface.subnet))        ch->next_hop_ = ih->daddr();    else        ch->next_hop_ = gw_default;    /* Send down to gw interace */    sendDown(gw_interface.moduleid, p, 0);}#endif /* CONFIG_GATEWAY */Packet* NS_CLASS allocpkt() const{	Packet* p = Packet::alloc();	hdr_cmn* ch = HDR_CMN(p);	ch->uid() = uidcnt_++;	ch->size() = 0;	ch->timestamp() = Scheduler::instance().clock();	ch->iface() = UNKN_IFACE.value(); // from packet.h (agent is local)	ch->direction() = hdr_cmn::NONE;	ch->error() = 0;	/* pkt not corrupt to start with */	hdr_flags* hf = hdr_flags::access(p);	hf->ecn_capable_ = 0;	hf->ecn_ = 0;	hf->eln_ = 0;	hf->ecn_to_echo_ = 0;	hf->fs_ = 0;	hf->no_ts_ = 0;	hf->pri_ = 0;	hf->cong_action_ = 0;	hf->qs_ = 0;		return p;}nsaddr_t NS_CLASS str2addr(const char *str){	int level[4] = {0,0,0,0};	char tmp[20];	strncpy(tmp,str,19);	tmp[19] = '\0';	char *p = strtok(tmp, ".");	for(int i = 0; p && i < 4; p = strtok(NULL, "."), i++)	{		level[i] = atoi(p);		if(level[i] > 255)			level[i] = 255;		else if(level[i] < 0)			level[i] = 0;	}	nsaddr_t addr = 0;	for(int i = 0; i < 4; i++)	{		addr += (level[i] << 8 * (3 - i));	}	return addr;}

⌨️ 快捷键说明

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