📄 aodv.txt
字号:
01050 // Not a broadcast packet, no delay, send immediately
01051 Scheduler::instance().schedule(target_, p, 0.); //是广播包,而且不用delay
01052 }
01053 }
01054
01055 }
01056
01057
01058 void
01059 AODV::sendRequest(nsaddr_t dst) { //发rreq
01060 // Allocate a RREQ packet
01061 Packet *p = Packet::alloc(); //先给新的rreq包分配一个内存空间
01062 struct hdr_cmn *ch = HDR_CMN(p);
01063 struct hdr_ip *ih = HDR_IP(p);
01064 struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p);
01065 aodv_rt_entry *rt = rtable.rt_lookup(dst); //找是否有到目的节点的路由
01066
01067 assert(rt); //有
01068
01069 /*
01070 * Rate limit sending of Route Requests. We are very conservative
01071 * about sending out route requests.
01072 */
01073
01074 if (rt->rt_flags == RTF_UP) { //路由有效
01075 assert(rt->rt_hops != INFINITY2); //hop数不是无穷大
01076 Packet::free((Packet *)p);
01077 return;
01078 }
01079
01080 if (rt->rt_req_timeout > CURRENT_TIME) {
01081 Packet::free((Packet *)p);
01082 return;
01083 }
01084
01085 // rt_req_cnt is the no. of times we did network-wide broadcast
01086 // RREQ_RETRIES is the maximum number we will allow before
01087 // going to a long timeout.
01088
01089 if (rt->rt_req_cnt > RREQ_RETRIES) { //如果已经发了很多次了,超过上限rreq_retries
01090 rt->rt_req_timeout = CURRENT_TIME + MAX_RREQ_TIMEOUT; //重新设定timeout时间
01091 rt->rt_req_cnt = 0; //次数归0
01092 Packet *buf_pkt;
01093 while ((buf_pkt = rqueue.deque(rt->rt_dst))) { //如果buffer里还有要去此目的节点的分组
01094 drop(buf_pkt, DROP_RTR_NO_ROUTE); //丢掉,因为过了很长时间了,我们还是没有找到路由
01095 }
01096 Packet::free((Packet *)p);
01097 return;
01098 }
01099
01100 #ifdef DEBUG
01101 fprintf(stderr, "(%2d) - %2d sending Route Request, dst: %d\n",
01102 ++route_request, index, rt->rt_dst);
01103 #endif // DEBUG
01104
01105 // Determine the TTL to be used this time. //这一段也看不懂
01106 // Dynamic TTL evaluation - SRD
01107
01108 rt->rt_req_last_ttl = max(rt->rt_req_last_ttl,rt->rt_last_hop_count);
01109
01110 if (0 == rt->rt_req_last_ttl) {
01111 // first time query broadcast
01112 ih->ttl_ = TTL_START;
01113 }
01114 else {
01115 // Expanding ring search.
01116 if (rt->rt_req_last_ttl < TTL_THRESHOLD)
01117 ih->ttl_ = rt->rt_req_last_ttl + TTL_INCREMENT;
01118 else {
01119 // network-wide broadcast
01120 ih->ttl_ = NETWORK_DIAMETER;
01121 rt->rt_req_cnt += 1;
01122 }
01123 }
01124
01125 // remember the TTL used for the next time
01126 rt->rt_req_last_ttl = ih->ttl_;
01127
01128 // PerHopTime is the roundtrip time per hop for route requests.
01129 // The factor 2.0 is just to be safe .. SRD 5/22/99
01130 // Also note that we are making timeouts to be larger if we have
01131 // done network wide broadcast before.
01132
01133 rt->rt_req_timeout = 2.0 * (double) ih->ttl_ * PerHopTime(rt);
01134 if (rt->rt_req_cnt > 0)
01135 rt->rt_req_timeout *= rt->rt_req_cnt;
01136 rt->rt_req_timeout += CURRENT_TIME;
01137
01138 // Don't let the timeout to be too large, however .. SRD 6/8/99
01139 if (rt->rt_req_timeout > CURRENT_TIME + MAX_RREQ_TIMEOUT)
01140 rt->rt_req_timeout = CURRENT_TIME + MAX_RREQ_TIMEOUT;
01141 rt->rt_expire = 0;
01142
01143 #ifdef DEBUG
01144 fprintf(stderr, "(%2d) - %2d sending Route Request, dst: %d, tout %f ms\n",
01145 ++route_request,
01146 index, rt->rt_dst,
01147 rt->rt_req_timeout - CURRENT_TIME);
01148 #endif // DEBUG
01149
01150
01151 // Fill out the RREQ packet //这里是建构rreq分组,填入相关信息到分组中,然后发送
01152 // ch->uid() = 0;
01153 ch->ptype() = PT_AODV;
01154 ch->size() = IP_HDR_LEN + rq->size();
01155 ch->iface() = -2;
01156 ch->error() = 0;
01157 ch->addr_type() = NS_AF_NONE;
01158 ch->prev_hop_ = index; // AODV hack
01159
01160 ih->saddr() = index;
01161 ih->daddr() = IP_BROADCAST;
01162 ih->sport() = RT_PORT;
01163 ih->dport() = RT_PORT;
01164
01165 // Fill up some more fields.
01166 rq->rq_type = AODVTYPE_RREQ;
01167 rq->rq_hop_count = 1;
01168 rq->rq_bcast_id = bid++;
01169 rq->rq_dst = dst;
01170 rq->rq_dst_seqno = (rt ? rt->rt_seqno : 0);
01171 rq->rq_src = index;
01172 seqno += 2;
01173 assert ((seqno%2) == 0);
01174 rq->rq_src_seqno = seqno;
01175 rq->rq_timestamp = CURRENT_TIME;
01176
01177 Scheduler::instance().schedule(target_, p, 0.);
01178
01179 }
01180
01181 void
01182 AODV::sendReply(nsaddr_t ipdst, u_int32_t hop_count, nsaddr_t rpdst, //send reply
01183 u_int32_t rpseq, u_int32_t lifetime, double timestamp) {
01184 Packet *p = Packet::alloc();
01185 struct hdr_cmn *ch = HDR_CMN(p);
01186 struct hdr_ip *ih = HDR_IP(p);
01187 struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);
01188 aodv_rt_entry *rt = rtable.rt_lookup(ipdst);
01189
01190 #ifdef DEBUG
01191 fprintf(stderr, "sending Reply from %d at %.2f\n", index, Scheduler::instance().clock());
01192 #endif // DEBUG
01193 assert(rt); //确定在路由表里找到了相关信息,有关将要发送的reply
01194
01195 rp->rp_type = AODVTYPE_RREP; //把type设成是aodv_rrep
01196 //rp->rp_flags = 0x00;
01197 rp->rp_hop_count = hop_count; //把routing table里显示的所需的hop count放到packet里
01198 rp->rp_dst = rpdst; //目标地址
01199 rp->rp_dst_seqno = rpseq; //seqno
01200 rp->rp_src = index; //自己的地址
01201 rp->rp_lifetime = lifetime;
01202 rp->rp_timestamp = timestamp;
01203
01204 // ch->uid() = 0; //在cmn头里
01205 ch->ptype() = PT_AODV; //定义type是aodv的包,具体是aodv的什么包等到check rp头的时候会知道
01206 ch->size() = IP_HDR_LEN + rp->size(); //加ip头和aodv_rp头
01207 ch->iface() = -2;
01208 ch->error() = 0;
01209 ch->addr_type() = NS_AF_INET;
01210 ch->next_hop_ = rt->rt_nexthop;
01211 ch->prev_hop_ = index; // AODV hack
01212 ch->direction() = hdr_cmn::DOWN; //发送
01213
01214 ih->saddr() = index; //定义ip头的相关信息
01215 ih->daddr() = ipdst;
01216 ih->sport() = RT_PORT;
01217 ih->dport() = RT_PORT;
01218 ih->ttl_ = NETWORK_DIAMETER;
01219
01220 Scheduler::instance().schedule(target_, p, 0.); //发!不等待
01221
01222 }
01223
01224 void
01225 AODV::sendError(Packet *p, bool jitter) {
01226 struct hdr_cmn *ch = HDR_CMN(p);
01227 struct hdr_ip *ih = HDR_IP(p);
01228 struct hdr_aodv_error *re = HDR_AODV_ERROR(p);
01229
01230 #ifdef ERROR
01231 fprintf(stderr, "sending Error from %d at %.2f\n", index, Scheduler::instance().clock());
01232 #endif // DEBUG
01233
01234 re->re_type = AODVTYPE_RERR;
01235 //re->reserved[0] = 0x00; re->reserved[1] = 0x00;
01236 // DestCount and list of unreachable destinations are already filled
01237
01238 // ch->uid() = 0;
01239 ch->ptype() = PT_AODV;
01240 ch->size() = IP_HDR_LEN + re->size();
01241 ch->iface() = -2;
01242 ch->error() = 0;
01243 ch->addr_type() = NS_AF_NONE;
01244 ch->next_hop_ = 0; //为什么这里是0?
01245 ch->prev_hop_ = index; // AODV hack
01246 ch->direction() = hdr_cmn::DOWN; //important: change the packet's direction
01247
01248 ih->saddr() = index;
01249 ih->daddr() = IP_BROADCAST;
01250 ih->sport() = RT_PORT;
01251 ih->dport() = RT_PORT;
01252 ih->ttl_ = 1;
01253
01254 // Do we need any jitter? Yes
01255 if (jitter) //random 的发
01256 Scheduler::instance().schedule(target_, p, 0.01*Random::uniform());
01257 else
01258 Scheduler::instance().schedule(target_, p, 0.0);
01259
01260 }
01261
01262
01263 /*
01264 Neighbor Management Functions
01265 */
01266
01267 void
01268 AODV::sendHello() { //周其发送hello分组,以监测邻节点的连通性
01269 Packet *p = Packet::alloc();
01270 struct hdr_cmn *ch = HDR_CMN(p);
01271 struct hdr_ip *ih = HDR_IP(p);
01272 struct hdr_aodv_reply *rh = HDR_AODV_REPLY(p);
01273
01274 #ifdef DEBUG
01275 fprintf(stderr, "sending Hello from %d at %.2f\n", index, Scheduler::instance().clock());
01276 #endif // DEBUG
01277
01278 rh->rp_type = AODVTYPE_HELLO;
01279 //rh->rp_flags = 0x00;
01280 rh->rp_hop_count = 1;
01281 rh->rp_dst = index;
01282 rh->rp_dst_seqno = seqno;
01283 rh->rp_lifetime = (1 + ALLOWED_HELLO_LOSS) * HELLO_INTERVAL;
01284
01285 // ch->uid() = 0;
01286 ch->ptype() = PT_AODV;
01287 ch->size() = IP_HDR_LEN + rh->size();
01288 ch->iface() = -2;
01289 ch->error() = 0;
01290 ch->addr_type() = NS_AF_NONE;
01291 ch->prev_hop_ = index; // AODV hack
01292
01293 ih->saddr() = index;
01294 ih->daddr() = IP_BROADCAST; //这里表示广播
01295 ih->sport() = RT_PORT;
01296 ih->dport() = RT_PORT;
01297 ih->ttl_ = 1; //单跳广播,只给neighbor知道就ok
01298
01299 Scheduler::instance().schedule(target_, p, 0.0); //发,不等
01300 }
01301
01302
01303 void
01304 AODV::recvHello(Packet *p) { //收到hello message
01305 //struct hdr_ip *ih = HDR_IP(p);
01306 struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);
01307 AODV_Neighbor *nb; //指向neighbor的指针
01308
01309 nb = nb_lookup(rp->rp_dst); //找neighbor,其实rp_dst就是发送这个hello message的节点本身是不是?
01310 if(nb == 0) { //没有neighbor
01311 nb_insert(rp->rp_dst); //添加
01312 }
01313 else {
01314 nb->nb_expire = CURRENT_TIME +
01315 (1.5 * ALLOWED_HELLO_LOSS * HELLO_INTERVAL); //有neighbor
01316 }
01317
01318 Packet::free(p);
01319 }
01320
01321 void
01322 AODV::nb_insert(nsaddr_t id) {
01323 AODV_Neighbor *nb = new AODV_Neighbor(id);
01324
01325 assert(nb);
01326 nb->nb_expire = CURRENT_TIME +
01327 (1.5 * ALLOWED_HELLO_LOSS * HELLO_INTERVAL);
01328 LIST_INSERT_HEAD(&nbhead, nb, nb_link);
01329 seqno += 2; // set of neighbors changed
01330 assert ((seqno%2) == 0);
01331 }
01332
01333
01334 AODV_Neighbor*
01335 AODV::nb_lookup(nsaddr_t id) {
01336 AODV_Neighbor *nb = nbhead.lh_first;
01337
01338 for(; nb; nb = nb->nb_link.le_next) {
01339 if(nb->nb_addr == id) break;
01340 }
01341 return nb;
01342 }
01343
01344
01345 /*
01346 * Called when we receive *explicit* notification that a Neighbor
01347 * is no longer reachable.
01348 */
01349 void
01350 AODV::nb_delete(nsaddr_t id) {
01351 AODV_Neighbor *nb = nbhead.lh_first;
01352
01353 log_link_del(id);
01354 seqno += 2; // Set of neighbors changed
01355 assert ((seqno%2) == 0);
01356
01357 for(; nb; nb = nb->nb_link.le_next) {
01358 if(nb->nb_addr == id) {
01359 LIST_REMOVE(nb,nb_link);
01360 delete nb;
01361 break;
01362 }
01363 }
01364
01365 handle_link_failure(id);
01366
01367 }
01368
01369
01370 /*
01371 * Purges all timed-out Neighbor Entries - runs every
01372 * HELLO_INTERVAL * 1.5 seconds.
01373 */
01374 void
01375 AODV::nb_purge() {
01376 AODV_Neighbor *nb = nbhead.lh_first;
01377 AODV_Neighbor *nbn;
01378 double now = CURRENT_TIME;
01379
01380 for(; nb; nb = nbn) {
01381 nbn = nb->nb_link.le_next;
01382 if(nb->nb_expire <= now) {
01383 nb_delete(nb->nb_addr);
01384 }
01385 }
01386
01387 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -