使用cmsg公共库解析cmsg信息,对业务层提供获取cmsg句柄的接口

This commit is contained in:
luqiuwen
2019-06-01 17:00:36 +08:00
parent 1b872c246d
commit 77aa3063f7
11 changed files with 133 additions and 283 deletions

View File

@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <errno.h>
#include "tfe_types.h"
#include "tfe_utils.h"
#include "tfe_cmsg.h"
@@ -52,7 +54,7 @@ void tfe_cmsg_destroy(struct tfe_cmsg *cmsg)
FREE(&cmsg);
}
int tfe_cmsg_set(struct tfe_cmsg *cmsg, uint16_t type, const unsigned char *value, uint16_t size)
int tfe_cmsg_set(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, const unsigned char * value, uint16_t size)
{
if(type >= TFE_CMSG_TLV_NR_MAX)
{
@@ -73,17 +75,40 @@ int tfe_cmsg_set(struct tfe_cmsg *cmsg, uint16_t type, const unsigned char *valu
return 0;
}
int tfe_cmsg_get(struct tfe_cmsg *cmsg, uint16_t type, uint16_t *size, unsigned char **pvalue)
int tfe_cmsg_get_value(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, char * out_value,
size_t sz_out_value_buf, uint16_t * out_size)
{
struct tfe_cmsg_tlv *tlv = NULL;
if(type >= TFE_CMSG_TLV_NR_MAX || (tlv = cmsg->tlvs[type]) == NULL)
{
*size = 0;
return TFE_CMSG_INVALID_TYPE;
}
*size = tlv->length - sizeof(struct tfe_cmsg_tlv);
*pvalue = tlv->value_as_string;
return 0;
struct tfe_cmsg_tlv *tlv;
int result = 0;
int value_length = 0;
if (unlikely(type >= TFE_CMSG_TLV_NR_MAX))
{
result = -EINVAL;
goto errout;
}
tlv = cmsg->tlvs[type];
if (unlikely(tlv == NULL))
{
result = -ENOENT;
goto errout;
}
value_length = tlv->length - sizeof(struct tfe_cmsg_tlv);
if (unlikely(sz_out_value_buf < value_length))
{
result = -ENOBUFS;
goto errout;
}
memcpy(out_value, tlv->value_as_string, value_length);
*out_size = value_length;
return 0;
errout:
return result;
}
uint16_t tfe_cmsg_serialize_size_get(struct tfe_cmsg *cmsg)
@@ -160,6 +185,7 @@ int tfe_cmsg_deserialize(const unsigned char *data, uint16_t len, struct tfe_cms
struct tfe_cmsg_serialize_header *header = (struct tfe_cmsg_serialize_header*)data;
struct tfe_cmsg *cmsg = NULL;
int offset = 0, nr_tlvs = -1;
if(len < sizeof(struct tfe_cmsg_serialize_header))
{
goto error_out;
@@ -168,6 +194,7 @@ int tfe_cmsg_deserialize(const unsigned char *data, uint16_t len, struct tfe_cms
{
goto error_out;
}
cmsg = ALLOC(struct tfe_cmsg, 1);
offset = sizeof(struct tfe_cmsg_serialize_header);
nr_tlvs = ntohs(header->nr_tlvs);
@@ -178,13 +205,18 @@ int tfe_cmsg_deserialize(const unsigned char *data, uint16_t len, struct tfe_cms
{
goto error_out;
}
uint16_t type = ntohs(tlv->type);
uint16_t length = ntohs(tlv->length);
if(length < sizeof(struct tfe_cmsg_tlv) || offset + length > len)
{
goto error_out;
}
int ret = tfe_cmsg_set(cmsg, type, data + offset + sizeof(struct tfe_cmsg_tlv), length - sizeof(struct tfe_cmsg_tlv));
int ret = tfe_cmsg_set(cmsg, (enum tfe_cmsg_tlv_type)type,
data + offset + sizeof(struct tfe_cmsg_tlv), length - sizeof(struct tfe_cmsg_tlv));
if(ret < 0)
{
goto error_out;