summaryrefslogtreecommitdiff
path: root/common/src/tfe_cmsg.cpp
blob: 6f9d2b61b250ac001adb4d1b5a6a8a2d71ef437a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <errno.h>
#include <pthread.h>

#include "tfe_types.h"
#include "tfe_utils.h"
#include "tfe_cmsg.h"

struct tfe_cmsg_tlv
{
	uint16_t type;
	uint16_t length;
	union
	{
		uint8_t value_as_uint8[0];
		uint16_t value_as_uint16[0];
		uint32_t value_as_uint32[0];
		unsigned char value_as_string[0];
	};
} __attribute__((packed));

struct tfe_cmsg
{
	uint8_t flag;
	uint8_t ref;
	pthread_rwlock_t rwlock;
	uint16_t nr_tlvs;
	struct tfe_cmsg_tlv* tlvs[TFE_CMSG_TLV_NR_MAX];
	uint16_t size;
} __attribute__((packed));

struct tfe_cmsg_serialize_header
{
	uint8_t __magic__[2]; /* Must be 0x4d, 0x5a */
	uint16_t nr_tlvs;
	struct tfe_cmsg_tlv tlvs[0];
} __attribute__((packed));

struct tfe_cmsg* tfe_cmsg_init()
{
	struct tfe_cmsg *cmsg = ALLOC(struct tfe_cmsg, 1);
	cmsg->size = sizeof(struct tfe_cmsg_serialize_header);

	pthread_rwlock_init(&(cmsg->rwlock), NULL);

	ATOMIC_ZERO(&cmsg->flag);
	ATOMIC_ZERO(&cmsg->ref);
	ATOMIC_INC(&cmsg->ref);
	return cmsg;
}

void tfe_cmsg_destroy(struct tfe_cmsg *cmsg)
{
	if(cmsg != NULL)
	{
		if ((__sync_sub_and_fetch(&cmsg->ref, 1) != 0))
			return;
		pthread_rwlock_wrlock(&cmsg->rwlock);
		for(int i = 0; i < TFE_CMSG_TLV_NR_MAX; i++)
		{
			FREE(&(cmsg->tlvs[i]));
		}
		pthread_rwlock_unlock(&cmsg->rwlock);
		pthread_rwlock_destroy(&cmsg->rwlock);
		FREE(&cmsg);
	}
}

void tfe_cmsg_dup(struct tfe_cmsg *cmsg)
{
	if (cmsg == NULL)
		return;
	ATOMIC_INC(&cmsg->ref);
}

void tfe_cmsg_set_flag(struct tfe_cmsg *cmsg, uint8_t flag)
{
	if (cmsg == NULL)
		return;
	ATOMIC_SET(&cmsg->flag, flag);
}

uint8_t tfe_cmsg_get_flag(struct tfe_cmsg *cmsg)
{
	if (cmsg == NULL)
		return 0;
    uint8_t flag = 0;
    flag = ATOMIC_READ(&cmsg->flag);
    return flag;
}

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)
	{
		return TFE_CMSG_INVALID_TYPE;
	}
	pthread_rwlock_wrlock(&cmsg->rwlock);
	struct tfe_cmsg_tlv *tlv = cmsg->tlvs[type];
	uint16_t length = sizeof(struct tfe_cmsg_tlv) + size;

	// If the current tlv has been set, the previous value will be overwritten
	if (NULL != tlv)
	{
		cmsg->nr_tlvs--;
		cmsg->size -= tlv->length;
		cmsg->tlvs[type] = NULL;
		free(tlv);
		tlv = NULL;
	}
	
	if(tlv == NULL)
	{
		tlv = (struct tfe_cmsg_tlv*)ALLOC(char, length);
		cmsg->nr_tlvs++;
		cmsg->size += length;
	}
	tlv->type = type;
	tlv->length = length;
	memcpy(tlv->value_as_string, value, size);
	cmsg->tlvs[type] = tlv;
	pthread_rwlock_unlock(&cmsg->rwlock);
	return 0;
}

int tfe_cmsg_get_value(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, unsigned char * out_value,
    size_t sz_out_value_buf, uint16_t * out_size)
{
	struct tfe_cmsg_tlv *tlv;
	int result = 0;
	size_t value_length = 0;

	if (unlikely(type >= TFE_CMSG_TLV_NR_MAX))
    {
        result = -EINVAL;
        goto errout;
    }

	pthread_rwlock_rdlock(&cmsg->rwlock);
    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;
	pthread_rwlock_unlock(&cmsg->rwlock);
    return 0;

errout:
	pthread_rwlock_unlock(&cmsg->rwlock);
	return result;
}

uint16_t tfe_cmsg_serialize_size_get(struct tfe_cmsg *cmsg)
{
	pthread_rwlock_rdlock(&cmsg->rwlock);
	uint16_t size = cmsg->size;
	pthread_rwlock_unlock(&cmsg->rwlock);
	return size;
}

int tfe_cmsg_serialize(struct tfe_cmsg *cmsg, unsigned char *buff, uint16_t bufflen, uint16_t *serialize_len)
{
	//size是serialize之后的实际长度
	pthread_rwlock_rdlock(&cmsg->rwlock);
	uint16_t size = cmsg->size;
	//传入buff是否够长
	if(bufflen < size)
	{
		pthread_rwlock_unlock(&cmsg->rwlock);
		return TFE_CMSG_BUFF_NOT_ENOUGH;
	}
	//size是否正确
	if(size < sizeof(struct tfe_cmsg_serialize_header))
	{
		pthread_rwlock_unlock(&cmsg->rwlock);
		return TFE_CMSG_BUFF_NOT_ENOUGH;
	}
	struct tfe_cmsg_serialize_header *header = (struct tfe_cmsg_serialize_header*)buff;
	header->__magic__[0] = 0x4d;
	header->__magic__[1] = 0x5a;
	header->nr_tlvs = htons(cmsg->nr_tlvs);
	uint16_t offset = sizeof(struct tfe_cmsg_serialize_header);
	//检查nr_tlvs是否合法
	int count = 0;
	for(int i = 0; i < TFE_CMSG_TLV_NR_MAX; i++){
		if(cmsg->tlvs[i] != NULL)
		{
			count++;
		}
	}
	if(count != cmsg->nr_tlvs)
	{
		pthread_rwlock_unlock(&cmsg->rwlock);
		return TFE_CMSG_INVALID_FORMAT;
	}
	//序列化
	for(int i = 0; i < TFE_CMSG_TLV_NR_MAX; i++)
	{
		struct tfe_cmsg_tlv *tlv = cmsg->tlvs[i];
		if(tlv == NULL)
		{
			continue;
		}
		if(i != tlv->type)
		{
			pthread_rwlock_unlock(&cmsg->rwlock);
			return TFE_CMSG_INVALID_FORMAT;
		}
		uint16_t length = tlv->length;
		if(length < sizeof(struct tfe_cmsg_tlv) || offset + length > size)
		{
			pthread_rwlock_unlock(&cmsg->rwlock);
			return TFE_CMSG_INVALID_FORMAT;
		}
		memcpy((char*)header + offset, (void*)tlv, length);
		struct tfe_cmsg_tlv *tlv1 = (struct tfe_cmsg_tlv*)((char*)header + offset);
		tlv1->type = htons(tlv->type);
		tlv1->length = htons(tlv->length);
		offset += length;
	}
	//检查size是否正确
	if(offset != size)
	{
		pthread_rwlock_unlock(&cmsg->rwlock);
		return TFE_CMSG_INVALID_FORMAT;
	}
	*serialize_len = size;
	pthread_rwlock_unlock(&cmsg->rwlock);
	return 0;
}

//反序列化
int tfe_cmsg_deserialize(const unsigned char *data, uint16_t len, struct tfe_cmsg** pcmsg)
{
	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;
	}
	if(header->__magic__[0] != 0x4d || header->__magic__[1] != 0x5a)
	{
		goto error_out;
	}

	cmsg = ALLOC(struct tfe_cmsg, 1);
	pthread_rwlock_init(&(cmsg->rwlock), NULL);
	offset = sizeof(struct tfe_cmsg_serialize_header);
	nr_tlvs = ntohs(header->nr_tlvs);
	for(int i = 0; i < nr_tlvs; i++)
	{
		struct tfe_cmsg_tlv *tlv = (struct tfe_cmsg_tlv*)(data + offset);
		if(offset + sizeof(struct tfe_cmsg_tlv) > len)
		{
			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, (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;
		}
		offset += length;
	}
	cmsg->size = offset;
	*pcmsg = cmsg;
	return 0;

error_out:
	tfe_cmsg_destroy(cmsg);
	return TFE_CMSG_INVALID_FORMAT;
}