TLV格式是什么格式 ?
一种可变格式,TLV的意思就是:Type类型, Lenght长度,Value值;
Type和Length的长度固定,一般那是2、4个字节; Value的长度有Length指定;
解析方法:
1.读取type 转换为ntohl、ntohs转换为主机字节序得到类型;指针偏移+2或4
2.读取lenght,转换为ntohl、ntohs转换为主机字节序得到长度;指针偏移+2或4
3.根据得到的长度读取value,指针偏移+Length;
。。。。
继续处理后面的tlv;
首先介绍下TLV存储信息的链表建立
typedef struct node_t
{
struct node_t * previous;
struct node_t * next;
}tlv_node;
typedef struct node_t
{
tlv_node node;
}tlv_list;
#define head node.next
#define tail node.head
tlv_list LIST;
void init(tlv_list *tList)
{
tList->head = NULL;
tList->tail = NULL;
}
void init(tlv_list *tList, tlv_node *tNode)
{
tlv_node *tNext = NULL;
if(NULL == tList->tail)
{
tNext = tList->head;
tList->head = tNode;
}
else
{
tNext = tList->tail->next;
tList->tail->next = tNode;
}
if(NULL == tNext)
{
tList->tail = tNode;
}
else
{
tNext->previous = tNode;
}
tNode->previous = tList->tail ;
tNode ->next = tNext ;
}
tlv_node *list_first(tlv_list *tList)
{
return tList->head;
}
tlv_node *list_last(tlv_list *tList)
{
return tList->tail;
}
实例:
typedef struct node_t
{
struct node_t *
previous;
struct node_t *
next;
}
gw_node;
typedef struct{
gw_node node;
FUN fun1;
FUN fun2;
}
gw_conf_handle_t;
typedef struct gw_conf_file_s{
gw_list handle_list;
gw_uint32 offset;
gw_int32 vf;
gw_int32 isfs;
gw_uint8 *pval;
gw_uint32 len;
gw_int32 (*open)(struct gw_conf_file_s *pf, gw_uint8 * name, gw_int32 mode);
gw_int32 (*read)(struct gw_conf_file_s *pf, gw_uint32 offset, gw_uint32 len, gw_uint8 *val);
gw_int32 (*write)(struct gw_conf_file_s *pf, gw_uint32 offset, gw_uint32 len, gw_uint8 *val);
gw_int32 (*close)(struct gw_conf_file_s *pf);
}
gw_conf_file_t;
gw_conf_file_t g_conf_file;
1.init(
g_conf_file )
2.
0 4 8 12 16 N M
head_len(8)
|
tlv_len(M-8)
|
T
|
L
|
V
|
last TLV
|