📄 aodv-uu.cc
字号:
*/ scheduleNextEvent();}/* Entry-level packet reception */void NS_CLASS recv(Packet *p, Handler *){ struct hdr_cmn *ch = HDR_CMN(p); struct hdr_ip *ih = HDR_IP(p); struct in_addr saddr; /* Routing agent must be started before processing packets */ assert(initialized); saddr.s_addr = ih->saddr(); /* Detect routing loops */ if ((saddr.s_addr == DEV_NR(NS_DEV_NR).ipaddr.s_addr) && ch->num_forwards() > 0) { drop(p, DROP_RTR_ROUTE_LOOP); goto end; } /* Handle packet depending on type */ switch(ch->ptype()) { case PT_AODVUU: recvAODVUUPacket(p); // AODV-UU messages (control packets) break;#ifdef CONFIG_GATEWAY case PT_ENCAPSULATED: // Decapsulate... if (internet_gw_mode) { rt_table_t *rev_rt, *next_hop_rt; rev_rt = rt_table_find(saddr); /* Update reverse route */ if (rev_rt && rev_rt->state == VALID) { rt_table_update_timeout(rev_rt, ACTIVE_ROUTE_TIMEOUT); next_hop_rt = rt_table_find(rev_rt->next_hop); if (next_hop_rt && next_hop_rt->state == VALID && rev_rt && next_hop_rt->dest_addr.s_addr != rev_rt->dest_addr.s_addr) rt_table_update_timeout(next_hop_rt, ACTIVE_ROUTE_TIMEOUT); } /* Decapsulate the packet */ p = pkt_decapsulate(p); /* Send to wired target (address classifier) */ target_->recv(p, (Handler *)0); break; } #endif /* CONFIG_GATEWAY */ default: processPacket(p); // Data path } end: /* Check if any other events are pending and reschedule the timeout */ scheduleNextEvent();}/* Sends a packet using the specified next hop and delay */void NS_CLASS sendPacket(Packet *p, 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, DROP_RTR_TTL); return; } /* Set packet fields depending on packet type */ if (dest_addr.s_addr == AODV_BROADCAST) { /* Broadcast packet */ ch->next_hop_ = 0; ch->prev_hop_ = DEV_NR(NS_DEV_NR).ipaddr.s_addr; ch->addr_type() = NS_AF_NONE; ch->direction() = hdr_cmn::DOWN; jitter = 0.02 * Random::uniform(); Scheduler::instance().schedule(ll, p, jitter); } else { /* Unicast packet */ ch->next_hop_ = next_hop.s_addr; ch->prev_hop_ = DEV_NR(NS_DEV_NR).ipaddr.s_addr; 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; } Scheduler::instance().schedule(ll, p, delay); }}/* Interpreter for commands from Tcl */int NS_CLASS command(int argc, const char *const *argv){ TclObject *obj; 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) { if (strcasecmp (argv[1], "addr") == 0) { DEV_NR(NS_DEV_NR).ipaddr.s_addr = Address::instance().str2addr(argv[2]); return TCL_OK; } if((obj = TclObject::lookup(argv[2])) == 0) { fprintf(stderr, "AODVUU: %s lookup of %s failed\n", argv[1], argv[2]); return TCL_ERROR; } if (strcasecmp (argv[1], "node") == 0) { node_ = (MobileNode*) obj; return TCL_OK; } if (strcasecmp(argv[1], "if-queue") == 0) { ifqueue = (PriQueue *)obj; return TCL_OK; } if (strcasecmp(argv[1], "add-ll") == 0) { ll = (NsObject*) obj; return TCL_OK; } } /* Unknown commands are passed to the Agent base class */ return Agent::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 with IP = %s : %d started.", ip_to_str(DEV_NR(NS_DEV_NR).ipaddr), DEV_NR(NS_DEV_NR).ipaddr); 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(); 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;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -