📄 aodv源代码.txt
字号:
00710 rt_update(rt0, rq->rq_src_seqno, rq->rq_hop_count, ih->saddr(),
00711 max(rt0->rt_expire, (CURRENT_TIME + REV_ROUTE_LIFE)) ); //更新路由表相关内容,
00712 if (rt0->rt_req_timeout > 0.0) {
00713 // Reset the soft state and
00714 // Set expiry time to CURRENT_TIME + ACTIVE_ROUTE_TIMEOUT
00715 // This is because route is used in the forward direction,
00716 // but only sources get benefited by this change
00717 rt0->rt_req_cnt = 0;
00718 rt0->rt_req_timeout = 0.0;
00719 rt0->rt_req_last_ttl = rq->rq_hop_count;
00720 rt0->rt_expire = CURRENT_TIME + ACTIVE_ROUTE_TIMEOUT;
00721 }
00722
00723 /* Find out whether any buffered packet can benefit from the //看原先buffer的分组能不能有用
00724 * reverse route.
00725 * May need some change in the following code - Mahesh 09/11/99
00726 */
00727 assert (rt0->rt_flags == RTF_UP); //先确保是有效路由
00728 Packet *buffered_pkt; //定义一个指向buffer的分组的指针
00729 while ((buffered_pkt = rqueue.deque(rt0->rt_dst))) { //如果有到目的地地址的buffer分组
00730 if (rt0 && (rt0->rt_flags == RTF_UP)) { //看是不是有返回指针,并且反向路由有效
00731 assert(rt0->rt_hops != INFINITY2); //确保反向路由的hop数不是无穷大的前提下
00732 forward(rt0, buffered_pkt, NO_DELAY); //向反向路由转发,不等待
00733 }
00734 }
00735 }
00736 // End for putting reverse route in rt table
00737
00738
00739 /*
00740 * We have taken care of the reverse route stuff.
00741 * Now see whether we can send a route reply. //现在我们来看怎么恢复这个rreq,就是发送rrep
00742 */
00743
00744 rt = rtable.rt_lookup(rq->rq_dst); //查找是否有到目的地的路由
00745
00746 // First check if I am the destination ..
00747
00748 if(rq->rq_dst == index) { //如果当前节点就是目标地址
00749
00750 #ifdef DEBUG
00751 fprintf(stderr, "%d - %s: destination sending reply\n", //输出信息说要发rrep
00752 index, __FUNCTION__);
00753 #endif // DEBUG
00754
00755
00756 // Just to be safe, I use the max. Somebody may have
00757 // incremented the dst seqno.
00758 seqno = max(seqno, rq->rq_dst_seqno)+1; //先取路由表和rreq里的大的那个seqno
00759 if (seqno%2) seqno++; //如果是奇数,+1,变成有效
00760
00761 sendReply(rq->rq_src, // IP Destination //发送rrep,这是要调用sendReply这个函数,
00762 1, // Hop Count //现在是1
00763 index, // Dest IP Address //我自己
00764 seqno, // Dest Sequence Num //刚取得新值
00765 MY_ROUTE_TIMEOUT, // Lifetime
00766 rq->rq_timestamp); // timestamp
00767
00768 Packet::free(p); //丢弃刚才的rreq分组
00769 }
00770
00771 // I am not the destination, but I may have a fresh enough route. //如果我不是目标节点,但是我有到目标节点的最新的路由信息
00772
00773 else if (rt && (rt->rt_hops != INFINITY2) && //我有到目标节点的路由,并且hop数不是无穷大
00774 (rt->rt_seqno >= rq->rq_dst_seqno) ) { //而且我的路由表中的seqno够新鲜
00775
00776 //assert (rt->rt_flags == RTF_UP);
00777 assert(rq->rq_dst == rt->rt_dst); //在确保我的信息的确是针对目标节点的路由的前提下
00778 //assert ((rt->rt_seqno%2) == 0); // is the seqno even?
00779 sendReply(rq->rq_src, //发送rrep
00780 rt->rt_hops + 1,
00781 rq->rq_dst,
00782 rt->rt_seqno,
00783 (u_int32_t) (rt->rt_expire - CURRENT_TIME),
00784 // rt->rt_expire - CURRENT_TIME,
00785 rq->rq_timestamp);
00786 // Insert nexthops to RREQ source and RREQ destination in the
00787 // precursor lists of destination and source respectively
00788 rt->pc_insert(rt0->rt_nexthop); // nexthop to RREQ source
00789 rt0->pc_insert(rt->rt_nexthop); // nexthop to RREQ destination
00790
00791 #ifdef RREQ_GRAT_RREP //grap_rrep表示这个节点既要向源节点发reply也要向目标节点发reply
00792
00793 sendReply(rq->rq_dst,
00794 rq->rq_hop_count,
00795 rq->rq_src, //rreq的源节点
00796 rq->rq_src_seqno,
00797 (u_int32_t) (rt->rt_expire - CURRENT_TIME),
00798 // rt->rt_expire - CURRENT_TIME,
00799 rq->rq_timestamp);
00800 #endif
00801
00802 // TODO: send grat RREP to dst if G flag set in RREQ using rq->rq_src_seqno, rq->rq_hop_counT
00803
00804 // DONE: Included gratuitous replies to be sent as per IETF aodv draft specification. As of now, G flag has not been dynamically used and is always set or reset in aodv-packet.h --- Anant Utgikar, 09/16/02.
00805
00806 Packet::free(p);
00807 }
00808 /*
00809 * Can't reply. So forward the Route Request //如果啥都没有,只能再广播rreq
00810 */
00811 else {
00812 ih->saddr() = index; //我是原地址
00813 ih->daddr() = IP_BROADCAST; //广播分组
00814 rq->rq_hop_count += 1; //路由表里的hop count+1
00815 // Maximum sequence number seen en route
00816 if (rt) rq->rq_dst_seqno = max(rt->rt_seqno, rq->rq_dst_seqno); //取seqno大的值
00817 forward((aodv_rt_entry*) 0, p, DELAY); //转发,不等待
00818 }
00819
00820 }
00821
00822
00823 void
00824 AODV::recvReply(Packet *p) { //收到rrep的处理
00825 //struct hdr_cmn *ch = HDR_CMN(p);
00826 struct hdr_ip *ih = HDR_IP(p);
00827 struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p); //看aodv_reply头
00828 aodv_rt_entry *rt;
00829 char suppress_reply = 0; //什么意思?不懂
00830 double delay = 0.0;
00831
00832 #ifdef DEBUG
00833 fprintf(stderr, "%d - %s: received a REPLY\n", index, __FUNCTION__);
00834 #endif // DEBUG
00835
00836
00837 /*
00838 * Got a reply. So reset the "soft state" maintained for
00839 * route requests in the request table. We don't really have
00840 * have a separate request table. It is just a part of the
00841 * routing table itself.
00842 */
00843 // Note that rp_dst is the dest of the data packets, not the
00844 // the dest of the reply, which is the src of the data packets. //也就是说rp_dst和rq_dst的地址是一样的,都是数据包的地址
00845
00846 rt = rtable.rt_lookup(rp->rp_dst); //找到目标地址的路由
00847
00848 /*
00849 * If I don't have a rt entry to this host... adding
00850 */
00851 if(rt == 0) { //没有
00852 rt = rtable.rt_add(rp->rp_dst); //添加路由表项
00853 }
00854
00855 /*
00856 * Add a forward route table entry... here I am following
00857 * Perkins-Royer AODV paper almost literally - SRD 5/99
00858 */
00859
00860 if ( (rt->rt_seqno < rp->rp_dst_seqno) || // newer route //如果我的路由表里的seqno小于收到的rrep里的seqno
00861 ((rt->rt_seqno == rp->rp_dst_seqno) && //或者两者相等
00862 (rt->rt_hops > rp->rp_hop_count)) ) { // shorter or better route //但是路由表里的hop数要多一些
00863
00864 // Update the rt entry
00865 rt_update(rt, rp->rp_dst_seqno, rp->rp_hop_count, //更新路由表的内容
00866 rp->rp_src, CURRENT_TIME + rp->rp_lifetime);
00867
00868 // reset the soft state
00869 rt->rt_req_cnt = 0;
00870 rt->rt_req_timeout = 0.0;
00871 rt->rt_req_last_ttl = rp->rp_hop_count;
00872
00873 if (ih->daddr() == index) { // If I am the original source //如果我是rrep的源节点
00874 // Update the route discovery latency statistics
00875 // rp->rp_timestamp is the time of request origination
00876
00877 rt->rt_disc_latency[rt->hist_indx] = (CURRENT_TIME - rp->rp_timestamp)
00878 / (double) rp->rp_hop_count;
00879 // increment indx for next time
00880 rt->hist_indx = (rt->hist_indx + 1) % MAX_HISTORY;
00881 }
00882
00883 /*
00884 * Send all packets queued in the sendbuffer destined for
00885 * this destination.
00886 * XXX - observe the "second" use of p.
00887 */
00888 Packet *buf_pkt;
00889 while((buf_pkt = rqueue.deque(rt->rt_dst))) { //如果我的buffer里的分组都是朝向这个目标节点的
00890 if(rt->rt_hops != INFINITY2) { //并且hops数不是无穷大
00891 assert (rt->rt_flags == RTF_UP); //有效路由
00892 // Delay them a little to help ARP. Otherwise ARP
00893 // may drop packets. -SRD 5/23/99
00894 forward(rt, buf_pkt, delay); //等ARP的时间,然后转发
00895 delay += ARP_DELAY;
00896 }
00897 }
00898 }
00899 else {
00900 suppress_reply = 1;
00901 }
00902
00903 /*
00904 * If reply is for me, discard it. //如果收到的REPLY就是给我的
00905 */
00906
00907 if(ih->daddr() == index || suppress_reply) { //IP头的目标地址是我自己,或者suppress_reply=1
00908 Packet::free(p); //丢弃包
00909 }
00910 /*
00911 * Otherwise, forward the Route Reply. //不然forward
00912 */
00913 else {
00914 // Find the rt entry
00915 aodv_rt_entry *rt0 = rtable.rt_lookup(ih->daddr()); //查找有没有到目标地址的路由
00916 // If the rt is up, forward
00917 if(rt0 && (rt0->rt_hops != INFINITY2)) { //有,且跳数不是无穷大
00918 assert (rt0->rt_flags == RTF_UP); //路由有效
00919 rp->rp_hop_count += 1; //hop+1
00920 rp->rp_src = index; //src是我自己(一转发就把源节点的地址更新了么?)
00921 forward(rt0, p, NO_DELAY); //转发
00922 // Insert the nexthop towards the RREQ source to
00923 // the precursor list of the RREQ destination
00924 rt->pc_insert(rt0->rt_nexthop); // nexthop to RREQ source
00925
00926 }
00927 else {
00928 // I don't know how to forward .. drop the reply.
00929 #ifdef DEBUG
00930 fprintf(stderr, "%s: dropping Route Reply\n", __FUNCTION__);
00931 #endif // DEBUG
00932 drop(p, DROP_RTR_NO_ROUTE);
00933 }
00934 }
00935 }
00936
00937
00938 void
00939 AODV::recvError(Packet *p) { //收到提示链路中断的分组
00940 struct hdr_ip *ih = HDR_IP(p);
00941 struct hdr_aodv_error *re = HDR_AODV_ERROR(p);
00942 aodv_rt_entry *rt;
00943 u_int8_t i;
00944 Packet *rerr = Packet::alloc();
00945 struct hdr_aodv_error *nre = HDR_AODV_ERROR(rerr);
00946
00947 nre->DestCount = 0; //初始化不能到达的目标地址的个数为0
00948
00949 for (i=0; i<re->DestCount; i++) { //循环
00950 // For each unreachable destination
00951 rt = rtable.rt_lookup(re->unreachable_dst); //查找路由表里有没有到不能到达的路由的表项
00952 if ( rt && (rt->rt_hops != INFINITY2) && //有,且跳数不是无穷大
00953 (rt->rt_nexthop == ih->saddr()) && //且next hop 是源地址???还是当前点?
00954 (rt->rt_seqno <= re->unreachable_dst_seqno) ) { //且seqno小于error message里的值
00955 assert(rt->rt_flags == RTF_UP); //确保路由有效
00956 assert((rt->rt_seqno%2) == 0); // is the seqno even?
00957 #ifdef DEBUG
00958 fprintf(stderr, "%s(%f): %d\t(%d\t%u\t%d)\t(%d\t%u\t%d)\n", __FUNCTION__,CURRENT_TIME,
00959 index, rt->rt_dst, rt->rt_seqno, rt->rt_nexthop,
00960 re->unreachable_dst,re->unreachable_dst_seqno,
00961 ih->saddr());
00962 #endif // DEBUG
00963 rt->rt_seqno = re->unreachable_dst_seqno; //更新路由表的seqno
00964 rt_down(rt); //调用rt_down函数, rt_down的功能就是更新路由表的相关值以显示路由无效
00965
00966 // Not sure whether this is the right thing to do
00967 Packet *pkt;
00968 while((pkt = ifqueue->filter(ih->saddr()))) {
00969 drop(pkt, DROP_RTR_MAC_CALLBACK);
00970 }
00971
00972 // if precursor list non-empty add to RERR and delete the precursor list
00973 if (!rt->pc_empty()) {
00974 nre->unreachable_dst[nre->DestCount] = rt->rt_dst; //将目标地址添加到list里面
00975 nre->unreachable_dst_seqno[nre->DestCount] = rt->rt_seqno; //更新seqno
00976 nre->DestCount += 1; //数量加1
00977 rt->pc_delete();
00978 }
00979 }
00980 }
00981
00982 if (nre->DestCount > 0) {
00983 #ifdef DEBUG
00984 fprintf(stderr, "%s(%f): %d\t sending RERR...\n", __FUNCTION__, CURRENT_TIME, index);
00985 #endif // DEBUG
00986 sendError(rerr);
00987 }
00988 else {
00989 Packet::free(rerr);
00990 }
00991
00992 Packet::free(p);
00993 }
00994
00995
00996 /*
00997 Packet Transmission Routines
00998 */
00999
01000 void
01001 AODV::forward(aodv_rt_entry *rt, Packet *p, double delay) { //转发分组
01002 struct hdr_cmn *ch = HDR_CMN(p);
01003 struct hdr_ip *ih = HDR_IP(p);
01004
01005 if(ih->ttl_ == 0) { //如果time to leave=0, 超时
01006
01007 #ifdef DEBUG
01008 fprintf(stderr, "%s: calling drop()\n", __PRETTY_FUNCTION__);
01009 #endif // DEBUG
01010
01011 drop(p, DROP_RTR_TTL); //drop
01012 return;
01013 }
01014
01015 if (ch->ptype() != PT_AODV && ch->direction() == hdr_cmn::UP &&
01016 ((u_int32_t)ih->daddr() == IP_BROADCAST)
01017 || ((u_int32_t)ih->daddr() == here_.addr_)) { //看是不是非aodv包?并且是不是收到包,?并且是不是广播包,或者是发给当前节点的包?
01018 dmux_->recv(p,0); //收包
01019 return;
01020 }
01021
01022 if (rt) { //路由表
01023 assert(rt->rt_flags == RTF_UP); //确保有效路由
01024 rt->rt_expire = CURRENT_TIME + ACTIVE_ROUTE_TIMEOUT;
01025 ch->next_hop_ = rt->rt_nexthop; //下一跳
01026 ch->addr_type() = NS_AF_INET;
01027 ch->direction() = hdr_cmn::DOWN; //important: change the packet's direction //down 的都是发送(表示从上层到下层),up是接受
01028 }
01029 else { // if it is a broadcast packet
01030 // assert(ch->ptype() == PT_AODV); // maybe a diff pkt type like gaf
01031 assert(ih->daddr() == (nsaddr_t) IP_BROADCAST); //确保是broadcast
01032 ch->addr_type() = NS_AF_NONE;
01033 ch->direction() = hdr_cmn::DOWN; //important: change the packet's direction
01034 }
01035
01036 if (ih->daddr() == (nsaddr_t) IP_BROADCAST) {
01037 // If it is a broadcast packet
01038 assert(rt == 0); //broadcast保没有路由表项?
01039 /*
01040 * Jitter the sending of broadcast packets by 10ms
01041 */
01042 Scheduler::instance().schedule(target_, p,
01043 0.01 * Random::uniform()); //random等待以后广播
01044 }
01045 else { // Not a broadcast packet //如果不是broadcast包
01046 if(delay > 0.0) { //而且delay不等于0
01047 Scheduler::instance().schedule(target_, p, delay); //等待delay以后转发
01048 }
01049 else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -