📄 aodv.cc
字号:
// The factor 2.0 is just to be safe .. SRD 5/22/99 // Also note that we are making timeouts to be larger if we have // done network wide broadcast before. rt->rt_req_timeout = 2.0 * (double) ih->ttl_ * PerHopTime(rt); if (rt->rt_req_cnt > 0) rt->rt_req_timeout *= rt->rt_req_cnt; rt->rt_req_timeout += CURRENT_TIME; // Don't let the timeout to be too large, however .. SRD 6/8/99 if (rt->rt_req_timeout > CURRENT_TIME + MAX_RREQ_TIMEOUT) rt->rt_req_timeout = CURRENT_TIME + MAX_RREQ_TIMEOUT; rt->rt_expire = 0;#ifdef DEBUG fprintf(stderr, "(%2d) - %2d sending Route Request, dst: %d, tout %f ms\n", ++route_request, index, rt->rt_dst, rt->rt_req_timeout - CURRENT_TIME);#endif // DEBUG // Fill out the RREQ packet // ch->uid() = 0; ch->ptype() = PT_AODV; ch->size() = IP_HDR_LEN + rq->size(); ch->iface() = -2; ch->error() = 0; ch->addr_type() = NS_AF_NONE; ch->prev_hop_ = index; // AODV hack ih->saddr() = index; ih->daddr() = IP_BROADCAST; ih->sport() = RT_PORT; ih->dport() = RT_PORT; // Fill up some more fields. rq->rq_type = AODVTYPE_RREQ; rq->rq_flags = RREQ_NO_FLAG; rq->rq_hop_count = 1; rq->rq_bcast_id = bid++; rq->rq_dst = dst; rq->rq_dst_seqno = (rt ? rt->rt_seqno : 0); rq->rq_src = index; seqno += 2; assert ((seqno%2) == 0); rq->rq_src_seqno = seqno; rq->rq_timestamp = CURRENT_TIME;#ifdef PREDICTION if (rt->rt_flags == RTF_P_LINK){ struct hdr_aodv_request_link *rqlk = HDR_AODV_REQUEST_LINK(p); rqlk->rq_type = AODVTYPE_LINK_RREQ; rqlk->from = index; rqlk->to = rt->rt_nexthop; }#endif#ifdef MULTICAST if (rq->rq_dst >= IP_MULTICAST && rt->rt_req_times == 1){ aodv_glt_entry *glt = gltable.glt_lookup(rq->rq_dst); if (glt && glt->glt_expire > CURRENT_TIME){ ch->addr_type() = NS_AF_INET; ch->next_hop_ = glt->glt_next_hop; ih->daddr() = glt->glt_grp_leader_addr; } } if (ih->daddr() == IP_BROADCAST) controlNextHello();#endif Scheduler::instance().schedule(target_, p, 0.);}/*please note, the sendReply auguments is not the same as original one because of adding multicast functions*/voidAODV::sendReply(nsaddr_t ipdst, u_int8_t flags, u_int32_t hop_count, nsaddr_t rpdst, u_int32_t rpseq, u_int32_t lifetime, double timestamp, u_int8_t hops_grp_leader, nsaddr_t grp_leader_addr) {Packet *p = Packet::alloc();struct hdr_cmn *ch = HDR_CMN(p);struct hdr_ip *ih = HDR_IP(p);struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);aodv_rt_entry *rt = rtable.rt_lookup(ipdst);#ifdef DEBUGfprintf(stderr, "sending Reply from %d at %.9f\n", index, Scheduler::instance().clock());#endif // DEBUG assert(rt); rp->rp_type = AODVTYPE_RREP; /*** added for multicast ***/ rp->rp_flags = flags; /***************************/ //rp->rp_flags = 0x00; rp->rp_hop_count = hop_count; rp->rp_dst = rpdst; rp->rp_dst_seqno = rpseq; rp->rp_src = index; rp->rp_lifetime = lifetime; rp->rp_timestamp = timestamp; // ch->uid() = 0; ch->ptype() = PT_AODV; ch->size() = IP_HDR_LEN + rp->size(); ch->iface() = -2; ch->error() = 0; ch->addr_type() = NS_AF_INET; ch->next_hop_ = rt->rt_nexthop; ch->prev_hop_ = index; // AODV hack ch->direction() = hdr_cmn::DOWN; ih->saddr() = index; ih->daddr() = ipdst; ih->sport() = RT_PORT; ih->dport() = RT_PORT; ih->ttl_ = NETWORK_DIAMETER;#ifdef MULTICAST if (rp->rp_dst >= IP_MULTICAST && flags == RREP_J){ struct hdr_aodv_reply_ext *rpe= (struct hdr_aodv_reply_ext *)(HDR_AODV_REPLY(p) + rp->size()); rpe->type = AODVTYPE_RREP_EXT; rpe->length = AODVTYPE_RREP_EXT_LEN; rpe->hops_grp_leader = hops_grp_leader; rpe->grp_leader_addr = grp_leader_addr; ch->size()+= rpe->size(); }#endif Scheduler::instance().schedule(target_, p, 0.0);}voidAODV::sendError(Packet *p, bool jitter) {struct hdr_cmn *ch = HDR_CMN(p);struct hdr_ip *ih = HDR_IP(p);struct hdr_aodv_error *re = HDR_AODV_ERROR(p); #ifdef ERRORfprintf(stderr, "sending Error from %d at %.2f\n", index, Scheduler::instance().clock());#endif // DEBUG re->re_type = AODVTYPE_RERR; //re->reserved[0] = 0x00; re->reserved[1] = 0x00; // DestCount and list of unreachable destinations are already filled // ch->uid() = 0; ch->ptype() = PT_AODV; ch->size() = IP_HDR_LEN + re->size(); ch->iface() = -2; ch->error() = 0; ch->addr_type() = NS_AF_NONE; ch->next_hop_ = 0; ch->prev_hop_ = index; // AODV hack ch->direction() = hdr_cmn::DOWN; //important: change the packet's direction ih->saddr() = index; ih->daddr() = IP_BROADCAST; ih->sport() = RT_PORT; ih->dport() = RT_PORT; ih->ttl_ = 1;#ifdef MULTICAST controlNextHello();#endif // Do we need any jitter? Yes if (jitter) Scheduler::instance().schedule(target_, p, 0.01*Random::uniform()); else Scheduler::instance().schedule(target_, p, 0.0);}/* Neighbor Management Functions*/voidAODV::sendHello() {#ifdef MULTICASTaodv_mt_entry *mt;bool cont = false;for(mt = mtable.head(); mt; mt = mt->mt_link.le_next){ if (mt->mt_node_status != NOT_ON_TREE){ cont = true; break; }} if (!cont) return;if (hello_timeout > CURRENT_TIME) return;controlNextHello();#endifPacket *p = Packet::alloc();struct hdr_cmn *ch = HDR_CMN(p);struct hdr_ip *ih = HDR_IP(p);struct hdr_aodv_reply *rh = HDR_AODV_REPLY(p);#ifdef DEBUGfprintf(stderr, "sending Hello from %d at %.2f\n", index, Scheduler::instance().clock());#endif // DEBUG rh->rp_type = AODVTYPE_HELLO; //rh->rp_flags = 0x00; rh->rp_hop_count = 1; rh->rp_dst = index; rh->rp_dst_seqno = seqno; rh->rp_lifetime = (1 + ALLOWED_HELLO_LOSS) * HELLO_INTERVAL; // ch->uid() = 0; ch->ptype() = PT_AODV; ch->size() = IP_HDR_LEN + rh->size(); ch->iface() = -2; ch->error() = 0; ch->addr_type() = NS_AF_NONE; ch->prev_hop_ = index; // AODV hack ih->saddr() = index; ih->daddr() = IP_BROADCAST; ih->sport() = RT_PORT; ih->dport() = RT_PORT; ih->ttl_ = 1;#ifdef MULTICAST Scheduler::instance().schedule(target_, p, 0.01*Random::uniform());#else Scheduler::instance().schedule(target_, p, 0.0);#endif}voidAODV::recvHello(Packet *p){#ifdef MULTICAST Packet::free(p);#else//struct hdr_ip *ih = HDR_IP(p);struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);AODV_Neighbor *nb; nb = nb_lookup(rp->rp_dst); if(nb == 0) { nb_insert(rp->rp_dst); } else { nb->nb_expire = CURRENT_TIME + (1.5 * ALLOWED_HELLO_LOSS * HELLO_INTERVAL); } Packet::free(p);#endif}voidAODV::nb_insert(nsaddr_t id) {AODV_Neighbor *nb = new AODV_Neighbor(id); assert(nb);#ifdef MULTICAST nb->nb_expire = CURRENT_TIME + ALLOWED_HELLO_LOSS * MaxHelloInterval;#else nb->nb_expire = CURRENT_TIME + (1.5 * ALLOWED_HELLO_LOSS * HELLO_INTERVAL);#endif LIST_INSERT_HEAD(&nbhead, nb, nb_link); seqno += 2; // set of neighbors changed assert ((seqno%2) == 0);}AODV_Neighbor*AODV::nb_lookup(nsaddr_t id) {AODV_Neighbor *nb = nbhead.lh_first; for(; nb; nb = nb->nb_link.le_next) { if(nb->nb_addr == id) break; } return nb;}/* * Called when we receive *explicit* notification that a Neighbor * is no longer reachable. */voidAODV::nb_delete(nsaddr_t id) {AODV_Neighbor *nb = nbhead.lh_first; log_link_del(id); seqno += 2; // Set of neighbors changed assert ((seqno%2) == 0); for(; nb; nb = nb->nb_link.le_next) { if(nb->nb_addr == id) { LIST_REMOVE(nb,nb_link); delete nb; break; } }#ifndef MULTICAST handle_link_failure(id);#endif}/* * Purges all timed-out Neighbor Entries - runs every * HELLO_INTERVAL * 1.5 seconds. */voidAODV::nb_purge() {AODV_Neighbor *nb = nbhead.lh_first;AODV_Neighbor *nbn;double now = CURRENT_TIME; for(; nb; nb = nbn) { nbn = nb->nb_link.le_next; if(nb->nb_expire <= now) { nb_delete(nb->nb_addr);#ifdef MULTICAST mt_nb_fail(nb->nb_addr);#endif } }}/*** added for prediction for unicast ***/voidAODV::recvLPW(nsaddr_t prev, double breakTime){ aodv_rt_entry *rt = rtable.head(); Packet *p = Packet::alloc(); struct hdr_aodv_rpe *rpe = HDR_AODV_RPE(p); rpe->breakTime = breakTime; rpe->DestCount = 0; for (; rt; rt = rt->rt_link.le_next){ if (rt->rt_nexthop == prev && rt->rt_flags != RTF_DOWN && rt->rt_flags != RTF_P_LINK && rt->rt_expire > breakTime) { rt->rt_flags = RTF_P_LINK; rt->rt_expire = breakTime; rpe->vulnerable_dst[rpe->DestCount] = rt->rt_dst; rpe->vulnerable_dst_seqno[rpe->DestCount] = rt->rt_seqno; rpe->DestCount += 1; printf("recvLPW: %.9f, at node %d, to dest %d, with nexthop %d, route will break at %.9f\n", CURRENT_TIME, index, rt->rt_dst, prev, breakTime); } } if (rpe->DestCount > 0) sendRPE(p); else Packet::free(p);}voidAODV::sendLPW(nsaddr_t prev, double breakTime){ Packet *p = Packet::alloc(); struct hdr_cmn *ch = HDR_CMN(p); struct hdr_ip *ih = HDR_IP(p); struct hdr_aodv_lpw *rp = HDR_AODV_LPW(p); ch->ptype() = PT_AODV; ch->size() = IP_HDR_LEN + rp->size(); ch->iface() = -2; ch->error() = 0; ch->addr_type() = NS_AF_INET; ch->prev_hop_ = index; // AODV hack ch->next_hop_ = prev; ih->saddr() = index; ih->daddr() = prev; ih->sport() = RT_PORT; ih->dport() = RT_PORT; ih->ttl_ = 1; rp->rp_type = AODVTYPE_LPW; rp->rp_dst = prev; rp->breakTime = breakTime; Scheduler::instance().schedule(target_, p, 0.0);}voidAODV::sendRPE(Packet *p){ struct hdr_cmn *ch = HDR_CMN(p); struct hdr_ip *ih = HDR_IP(p); struct hdr_aodv_rpe *rp = HDR_AODV_RPE(p); // ch->uid() = 0; ch->ptype() = PT_AODV; ch->size() = IP_HDR_LEN + rp->size(); ch->iface() = -2; ch->error() = 0; ch->addr_type() = NS_AF_NONE; ch->next_hop_ = MAC_BROADCAST; ch->prev_hop_ = index; // AODV hack ih->saddr() = index; ih->daddr() = IP_BROADCAST; ih->sport() = RT_PORT; ih->dport() = RT_PORT; ih->ttl_ = 1; rp->rp_type = AODVTYPE_RPE;#ifdef MULTICAST controlNextHello();#endif //send with jitter Scheduler::instance().schedule(target_, p, 0.01*Random::uniform());}voidAODV::recvRPE(Packet *p){ struct hdr_ip *ih = HDR_IP(p); struct hdr_aodv_rpe *rpe = HDR_AODV_RPE(p); aodv_rt_entry *rt; u_int8_t i; Packet *np = Packet::alloc(); struct hdr_aodv_rpe *nrpe = HDR_AODV_RPE(np); nrpe->DestCount = 0; nrpe->breakTime = rpe->breakTime; for (i=0; i<rpe->DestCount; i++) { // For each vulnerable route rt = rtable.rt_lookup(rpe->vulnerable_dst[i]); if (rt && (rt->rt_nexthop == ih->saddr()) && (rt->rt_seqno <= rpe->vulnerable_dst_seqno[i]) && (rt->rt_flags == RTF_UP) && (rt->rt_expire > rpe->breakTime)) { rt->rt_seqno = rpe->vulnerable_dst_seqno[i]; rt->rt_expire = rpe->breakTime; rt->rt_flags = RTF_PREDICTION; nrpe->vulnerable_dst[nrpe->DestCount] = rt->rt_dst; nrpe->vulnerable_dst_seqno[nrpe->DestCount] = rt->rt_seqno; nrpe->DestCount += 1; printf("recvRPE: %.9f, at node %d, to dest %d, with nexthop %d, route will break at other link at %.9f\n", CURRENT_TIME, index, rt->rt_dst, ih->saddr(), rpe->breakTime); } } if (nrpe->DestCount > 0) sendRPE(np); else Packet::free(np); Packet::free(p);}voidAODV::recvRequest_P(Packet *p){ struct hdr_ip *ih = HDR_IP(p); struct hdr_aodv_request_link *rqlk = HDR_AODV_REQUEST_LINK(p); if (ih->saddr()== rqlk->from && rqlk->to == index) Packet::free(p); else recvRequest(p);}/**************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -