ns2中aodv协议中路由链表的操作

来源:互联网 发布:linux文件绝对路径 编辑:程序博客网 时间:2024/05/16 08:09

aodv协议中对路由链表的操作部分大多在lib/bsd-list.h文件中

双向链表

/*
 * List definitions.
 */

//创建指向链表第一个元素的指针
#define LIST_HEAD(name, type) \    
struct name { \
type *lh_first;/* first element */\
}

//创建匿名结构体 只包含该节点的前驱和后继,注意:后继为一级指针;前驱为二级指针

//后继指向的是下一个节点,而前驱指向的是前一个节点的后继,即它的前驱是一个指向指针的指针,画图会更清晰一些

#define LIST_ENTRY(type)\
struct { \
type *le_next;/* next element */\       
type **le_prev;/* address of previous next element */\
}


/*
 * List functions.
 */

//初始化路由链表
#define LIST_INIT(head) {\
(head)->lh_first = NULL;\
}

//在链表中某个节点的后面插入一个新的节点

//其中listelm为链表中的某个节点,elm为新加节点,field为结构体,包含该节点的前驱和后继(弄明白这一点后面就都不难理解了)
#define LIST_INSERT_AFTER(listelm, elm, field) { \
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)\
(listelm)->field.le_next->field.le_prev =\
   &(elm)->field.le_next;\
(listelm)->field.le_next = (elm);\
(elm)->field.le_prev = &(listelm)->field.le_next;\
}

//在链表中某个节点的前面插入一个新的节点

//其中listelm为链表中的某个节点,elm为新加节点,field为结构体,包含该节点的前驱和后继
#define LIST_INSERT_BEFORE(listelm, elm, field) { \
(elm)->field.le_prev = (listelm)->field.le_prev;\
(elm)->field.le_next = (listelm);\
*(listelm)->field.le_prev = (elm);\
(listelm)->field.le_prev = &(elm)->field.le_next;\
}

//头节点处插入
#define LIST_INSERT_HEAD(head, elm, field) { \
if (((elm)->field.le_next = (head)->lh_first) != NULL)\
(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
(head)->lh_first = (elm);\
(elm)->field.le_prev = &(head)->lh_first;\
}

//删除路由链表中某个节点,删除失效路由
#define LIST_REMOVE(elm, field) { \
if ((elm)->field.le_next != NULL)\
(elm)->field.le_next->field.le_prev = \
   (elm)->field.le_prev;\
*(elm)->field.le_prev = (elm)->field.le_next;\
}


#endif /* !_NS_BSD_LIST_H_ */


aodv.rtable.h中对邻接表类的定义

class AODV_Neighbor {
        friend class AODV;
        friend class aodv_rt_entry;
 public:
        AODV_Neighbor(u_int32_t a) { nb_addr = a; }


 protected:
        LIST_ENTRY(AODV_Neighbor) nb_link;
        nsaddr_t        nb_addr;
        double          nb_expire;      // ALLOWED_HELLO_LOSS * HELLO_INTERVAL
};

0 0