📄 aodv源代码.txt
字号:
00370 #endif // DEBUG
00371 re->DestCount += 1;
00372 rt_down(rt);
00373 }
00374 // remove the lost neighbor from all the precursor lists
00375 rt->pc_delete(id);
00376 }
00377
00378 if (re->DestCount > 0) {
00379 #ifdef DEBUG
00380 fprintf(stderr, "%s(%f): %d\tsending RERR...\n", __FUNCTION__, CURRENT_TIME, index);
00381 #endif // DEBUG
00382 sendError(rerr, false); //如果已经被告之路由无效,发rrer
00383 }
00384 else {
00385 Packet::free(rerr);
00386 }
00387 }
00388
00389 void
00390 AODV::local_rt_repair(aodv_rt_entry *rt, Packet *p) {
00391 #ifdef DEBUG
00392 fprintf(stderr,"%s: Dst - %d\n", __FUNCTION__, rt->rt_dst);
00393 #endif
00394 // Buffer the packet
00395 rqueue.enque(p); //修路的时候,先buffer分组
00396
00397 // mark the route as under repair
00398 rt->rt_flags = RTF_IN_REPAIR; //把路由状态设为repair
00399
00400 sendRequest(rt->rt_dst); //向目标节点发送request
00401
00402 // set up a timer interrupt
00403 Scheduler::instance().schedule(&lrtimer, p->copy(), rt->rt_req_timeout);
00404 }
00405
00406 void
00407 AODV::rt_update(aodv_rt_entry *rt, u_int32_t seqnum, u_int16_t metric, //更新路由表的信息
00408 nsaddr_t nexthop, double expire_time) {
00409
00410 rt->rt_seqno = seqnum;
00411 rt->rt_hops = metric;
00412 rt->rt_flags = RTF_UP;
00413 rt->rt_nexthop = nexthop;
00414 rt->rt_expire = expire_time;
00415 }
00416
00417 void
00418 AODV::rt_down(aodv_rt_entry *rt) { //如果路由被告知broken,更新路由表信息
00419 /*
00420 * Make sure that you don't "down" a route more than once.
00421 */
00422
00423 if(rt->rt_flags == RTF_DOWN) {
00424 return;
00425 }
00426
00427 // assert (rt->rt_seqno%2); // is the seqno odd?
00428 rt->rt_last_hop_count = rt->rt_hops;
00429 rt->rt_hops = INFINITY2;
00430 rt->rt_flags = RTF_DOWN;
00431 rt->rt_nexthop = 0;
00432 rt->rt_expire = 0;
00433
00434 } /* rt_down function */
00435
00436 /*
00437 Route Handling Functions
00438 */
00439
00440 void
00441 AODV::rt_resolve(Packet *p) { //解析收到的分组,是aodv该怎么办,不是又该怎么办
00442 struct hdr_cmn *ch = HDR_CMN(p); //看cmn的头
00443 struct hdr_ip *ih = HDR_IP(p); //看ip头
00444 aodv_rt_entry *rt; //定义routing table entry的指针rt
00445
00446 /*
00447 * Set the transmit failure callback. That
00448 * won't change.
00449 */
00450 ch->xmit_failure_ = aodv_rt_failed_callback;
00451 ch->xmit_failure_data_ = (void*) this;
00452 rt = rtable.rt_lookup(ih->daddr()); //在路由表里查找是不是有到这个目的地址的路由
00453 if(rt == 0) { //没有
00454 rt = rtable.rt_add(ih->daddr()); //在路由表里增加一个终点位这个目的地址的entry
00455 }
00456
00457 /*
00458 * If the route is up, forward the packet
00459 */
00460
00461 if(rt->rt_flags == RTF_UP) { //如果有有效路由
00462 assert(rt->rt_hops != INFINITY2); //确保hop数不是无穷大(无效路由的hop数是无穷大)
00463 forward(rt, p, NO_DELAY); //转发这个分组,并且没有延迟
00464 }
00465 /*
00466 * if I am the source of the packet, then do a Route Request.
00467 */
00468 else if(ih->saddr() == index) { //如果ip头显示自己是源点
00469 rqueue.enque(p); //则先buffer一下这个分组
00470 sendRequest(rt->rt_dst); //并且发路由请求(RREQ)
00471 }
00472 /*
00473 * A local repair is in progress. Buffer the packet.
00474 */
00475 else if (rt->rt_flags == RTF_IN_REPAIR) { //如果这条路由在修
00476 rqueue.enque(p); //则暂时buffer一下这个分组
00477 }
00478
00479 /*
00480 * I am trying to forward a packet for someone else to which
00481 * I don't have a route. //做后的情况是要转发的这个分组的目的地地址,我的路由表里没有有效路由
00482 */
00483 else {
00484 Packet *rerr = Packet::alloc(); //给新的RRER分组分配一内存空间
00485 struct hdr_aodv_error *re = HDR_AODV_ERROR(rerr); //access这个分组的头
00486 /*
00487 * For now, drop the packet and send error upstream.
00488 * Now the route errors are broadcast to upstream
00489 * neighbors - Mahesh 09/11/99
00490 */
00491
00492 assert (rt->rt_flags == RTF_DOWN); //确保这条路由无效
00493 re->DestCount = 0; //先把不能到达的目的地的数量设为0
00494 re->unreachable_dst[re->DestCount] = rt->rt_dst; //把这个目的地的ip地址放到这个不能到达的list里面
00495 re->unreachable_dst_seqno[re->DestCount] = rt->rt_seqno; //记录这个目的地地址的当前sequence number
00496 re->DestCount += 1; //记录好以后,将数量+1
00497 #ifdef DEBUG
00498 fprintf(stderr, "%s: sending RERR...\n", __FUNCTION__); //输出错误信息提示
00499 #endif
00500 sendError(rerr, false); //发送刚才set的RRER分组
00501
00502 drop(p, DROP_RTR_NO_ROUTE); //丢弃要传的分组,原因是没有有效路由
00503 }
00504
00505 }
00506
00507 void
00508 AODV::rt_purge() {
00509 aodv_rt_entry *rt, *rtn;
00510 double now = CURRENT_TIME;
00511 double delay = 0.0;
00512 Packet *p;
00513
00514 for(rt = rtable.head(); rt; rt = rtn) { // for each rt entry
00515 rtn = rt->rt_link.le_next;
00516 if ((rt->rt_flags == RTF_UP) && (rt->rt_expire < now)) { //有效路由,但过期了
00517 // if a valid route has expired, purge all packets from
00518 // send buffer and invalidate the route.
00519 assert(rt->rt_hops != INFINITY2);
00520 while((p = rqueue.deque(rt->rt_dst))) { //把指针指向所有跟这个目标节点相关的被buffer的分组
00521 #ifdef DEBUG
00522 fprintf(stderr, "%s: calling drop()\n",
00523 __FUNCTION__);
00524 #endif // DEBUG
00525 drop(p, DROP_RTR_NO_ROUTE); //丢包
00526 }
00527 rt->rt_seqno++; //seqno+1
00528 assert (rt->rt_seqno%2); //确保是奇数
00529 rt_down(rt); //call rt_down(rt)
00530 }
00531 else if (rt->rt_flags == RTF_UP) { //有效路由,且不过期
00532 // If the route is not expired,
00533 // and there are packets in the sendbuffer waiting,
00534 // forward them. This should not be needed, but this extra
00535 // check does no harm.
00536 assert(rt->rt_hops != INFINITY2);
00537 while((p = rqueue.deque(rt->rt_dst))) {
00538 forward (rt, p, delay); //转发,等待delay
00539 delay += ARP_DELAY;
00540 }
00541 }
00542 else if (rqueue.find(rt->rt_dst))
00543 // If the route is down and
00544 // if there is a packet for this destination waiting in
00545 // the sendbuffer, then send out route request. sendRequest
00546 // will check whether it is time to really send out request
00547 // or not.
00548 // This may not be crucial to do it here, as each generated
00549 // packet will do a sendRequest anyway.
00550
00551 sendRequest(rt->rt_dst);
00552 }
00553
00554 }
00555
00556 /*
00557 Packet Reception Routines
00558 */
00559
00560 void
00561 AODV::recv(Packet *p, Handler*) {
00562 struct hdr_cmn *ch = HDR_CMN(p); //看cmn头
00563 struct hdr_ip *ih = HDR_IP(p); //看ip头
00564
00565 assert(initialized()); //确保初始化了
00566 //assert(p->incoming == 0);
00567 // XXXXX NOTE: use of incoming flag has been depracated; In order to track direction of pkt flow, direction_ in hdr_cmn is used instead. see packet.h for details.
00568
00569 if(ch->ptype() == PT_AODV) { //从cmn头的信息里看分组的类型,是不是aodv包?
00570 ih->ttl_ -= 1; //是的话,time to leave-1
00571 recvAODV(p); //并且调用recvAODV(p)函数,是专门处理aodv分组的函数
00572 return;
00573 }
00574
00575
00576 /*
00577 * Must be a packet I'm originating...
00578 */
00579 if((ih->saddr() == index) && (ch->num_forwards() == 0)) { //如果从ip头看出来,我就是分组的源节点,并且还没有loop
00580 /*
00581 * Add the IP Header
00582 */
00583 ch->size() += IP_HDR_LEN; //把ip加头上
00584 // Added by Parag Dadhania && John Novatnack to handle broadcasting
00585 if ( (u_int32_t)ih->daddr() != IP_BROADCAST) //如果这个不是broadcast分组
00586 ih->ttl_ = NETWORK_DIAMETER; //设time to leave = 30,因为前面定义network_diameter=30
00587 }
00588 /*
00589 * I received a packet that I sent. Probably
00590 * a routing loop.
00591 */
00592 else if(ih->saddr() == index) { //如果我收到自己发送的分组(相当于num_forwards >= 0)
00593 drop(p, DROP_RTR_ROUTE_LOOP); //丢弃分组,原因是由loop
00594 return;
00595 }
00596 /*
00597 * Packet I'm forwarding...
00598 */
00599 else {
00600 /*
00601 * Check the TTL. If it is zero, then discard.
00602 */
00603 if(--ih->ttl_ == 0) { //如果time to leave == 0
00604 drop(p, DROP_RTR_TTL); //丢弃分组,原因是超时
00605 return;
00606 }
00607 }
00608 // Added by Parag Dadhania && John Novatnack to handle broadcasting
00609 if ( (u_int32_t)ih->daddr() != IP_BROADCAST) //如果这个不是broadcast分组
00610 rt_resolve(p); //解析这个分组
00611 else
00612 forward((aodv_rt_entry*) 0, p, NO_DELAY); //如果是broadcast分组,转发分组,不等待
00613 }
00614
00615
00616 void
00617 AODV::recvAODV(Packet *p) { //如果我收到的是aodv分组
00618 struct hdr_aodv *ah = HDR_AODV(p); //看aodv的头
00619 struct hdr_ip *ih = HDR_IP(p); //看ip头
00620
00621 assert(ih->sport() == RT_PORT); //确保送到正确的agent端口
00622 assert(ih->dport() == RT_PORT);
00623
00624 /*
00625 * Incoming Packets.
00626 */
00627 switch(ah->ah_type) { //如果是aodv包
00628
00629 case AODVTYPE_RREQ: //看是不是RREQ分组
00630 recvRequest(p); //是,调用recvRequest(p)函数
00631 break;
00632
00633 case AODVTYPE_RREP: //看是不是RREP分组
00634 recvReply(p); //是,调用recvReply(p)函数
00635 break;
00636
00637 case AODVTYPE_RERR: //看是不是RERR分组
00638 recvError(p); //是,调用recvError(p)函数
00639 break;
00640
00641 case AODVTYPE_HELLO: //看是不是hello message
00642 recvHello(p); //是,调用recvHello(p)函数
00643 break;
00644
00645 default:
00646 fprintf(stderr, "Invalid AODV type (%x)\n", ah->ah_type);
00647 exit(1); //不然的话,输出错误信息
00648 }
00649
00650 }
00651
00652
00653 void
00654 AODV::recvRequest(Packet *p) { //如果是收到路由请求
00655 struct hdr_ip *ih = HDR_IP(p); //看ip头
00656 struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p); //看aodv_request头
00657 aodv_rt_entry *rt; //定义routing table entry 指针
00658
00659 /*
00660 * Drop if:
00661 * - I'm the source
00662 * - I recently heard this request.
00663 */
00664
00665 if(rq->rq_src == index) { //aodv_request头显示我是源节点
00666 #ifdef DEBUG
00667 fprintf(stderr, "%s: got my own REQUEST\n", __FUNCTION__); //表示我收到自己的请求
00668 #endif // DEBUG
00669 Packet::free(p); //释放指针
00670 return;
00671 }
00672
00673 if (id_lookup(rq->rq_src, rq->rq_bcast_id)) { //查找当前节点是不是已经收到过该源节点发出的相同的rreq
00674
00675 #ifdef DEBUG
00676 fprintf(stderr, "%s: discarding request\n", __FUNCTION__); //是,输出错误信息
00677 #endif // DEBUG
00678
00679 Packet::free(p); //释放指针,相当于扔掉分组
00680 return;
00681 }
00682
00683 /*
00684 * Cache the broadcast ID
00685 */
00686 id_insert(rq->rq_src, rq->rq_bcast_id); //这个用来判断是否已经收到该rreq
00687
00688
00689
00690 /*
00691 * We are either going to forward the REQUEST or generate a //在我们还不确定到底是转发这个请求还是回复这个请求以前,
00692 * REPLY. Before we do anything, we make sure that the REVERSE //先确保路由表中已经有反向路由
00693 * route is in the route table.
00694 */
00695 aodv_rt_entry *rt0; // rt0 is the reverse route //rt0是反向路由的指针
00696
00697 rt0 = rtable.rt_lookup(rq->rq_src); //现在路由表中找返回源节点的路由
00698 if(rt0 == 0) { /* if not in the route table */ //如果不存在
00699 // create an entry for the reverse route.
00700 rt0 = rtable.rt_add(rq->rq_src); //先增加一个路由表项,用来存储回向源节点的反向路由
00701 }
00702
00703 rt0->rt_expire = max(rt0->rt_expire, (CURRENT_TIME + REV_ROUTE_LIFE)); //限超时,aodv.h里定义#define REV_ROUTE_LIFE 6 // 5 seconds
00704
00705 if ( (rq->rq_src_seqno > rt0->rt_seqno ) || //如果routing table里面的反向原点的seqno比现在收到的rreq里source seqno小
00706 ((rq->rq_src_seqno == rt0->rt_seqno) && //或者相等
00707 (rq->rq_hop_count < rt0->rt_hops)) ) { //但是routing table里显示的hop数比rreq里的大
00708 // If we have a fresher seq no. or lesser #hops for the
00709 // same seq no., update the rt entry. Else don't bother.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -