1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "packet.h"
17 #include "buffer.h"
18
19 #include <memory.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "platform/include/platform_def.h"
24 #include "securec.h"
25 #include "log.h"
26
27 typedef struct Payload {
28 struct Payload *next;
29 struct Payload *prev;
30 Buffer *buf;
31 } Payload;
32
33 typedef struct Packet {
34 Payload *head;
35 Payload *tail;
36 Payload *payload;
37 } PacketInternal;
38
PayloadNew(uint32_t size)39 static inline Payload *PayloadNew(uint32_t size)
40 {
41 Payload *payload = (Payload *)calloc(1, sizeof(Payload));
42 if (payload == NULL) {
43 return NULL;
44 }
45 payload->buf = BufferMalloc(size);
46 return payload;
47 }
48
PayloadNewRef(const Buffer * buf)49 static inline Payload *PayloadNewRef(const Buffer *buf)
50 {
51 Payload *payload = (Payload *)calloc(1, sizeof(Payload));
52 if (payload == NULL) {
53 return NULL;
54 }
55 payload->buf = BufferRefMalloc(buf);
56 return payload;
57 }
58
PayloadFree(Payload * payload)59 static inline void PayloadFree(Payload *payload)
60 {
61 if (payload == NULL) {
62 return;
63 }
64 BufferFree(payload->buf);
65 free(payload);
66 }
67
PacketMalloc(uint16_t headSize,uint16_t tailSize,uint32_t payloadSize)68 Packet *PacketMalloc(uint16_t headSize, uint16_t tailSize, uint32_t payloadSize)
69 {
70 Packet *packet = (Packet *)calloc(1, sizeof(Packet));
71 if (packet == NULL) {
72 return NULL;
73 }
74 packet->head = PayloadNew(headSize);
75 packet->tail = PayloadNew(tailSize);
76 packet->payload = PayloadNew(payloadSize);
77
78 if ((packet->head != NULL) && (packet->tail != NULL) && (packet->payload != NULL)) {
79 packet->head->next = packet->payload;
80 packet->payload->prev = packet->head;
81
82 packet->payload->next = packet->tail;
83 packet->tail->prev = packet->payload;
84 }
85 return packet;
86 }
87
PacketRefMalloc(const Packet * pkt)88 Packet *PacketRefMalloc(const Packet *pkt)
89 {
90 Packet *refPacket = (Packet *)calloc(1, sizeof(Packet));
91 if (refPacket == NULL) {
92 return NULL;
93 }
94 refPacket->head = PayloadNewRef(pkt->head->buf);
95 refPacket->tail = PayloadNewRef(pkt->tail->buf);
96
97 if ((refPacket->head != NULL) && (refPacket->tail != NULL)) {
98 Payload *srcPayload = pkt->payload;
99 Payload *refPayload = refPacket->head;
100
101 while (srcPayload != pkt->tail) {
102 Payload *new = PayloadNewRef(srcPayload->buf);
103 if (new != NULL) {
104 refPayload->next = new;
105 new->prev = refPayload;
106 refPayload = new;
107 srcPayload = srcPayload->next;
108 }
109 }
110
111 refPayload->next = refPacket->tail;
112 refPacket->tail->prev = refPayload;
113
114 refPacket->payload = refPacket->head->next;
115 refPacket->payload->prev = refPacket->head;
116 }
117 return refPacket;
118 }
119
PacketInheritMalloc(const Packet * pkt,uint16_t headSize,uint16_t tailSize)120 Packet *PacketInheritMalloc(const Packet *pkt, uint16_t headSize, uint16_t tailSize)
121 {
122 Packet *inheritPacket = PacketRefMalloc(pkt);
123 if (inheritPacket != NULL) {
124 Payload *tempTail = inheritPacket->tail;
125
126 inheritPacket->payload = inheritPacket->head;
127 inheritPacket->head = PayloadNew(headSize);
128 if (inheritPacket->head != NULL) {
129 inheritPacket->head->next = inheritPacket->payload;
130 inheritPacket->payload->prev = inheritPacket->head;
131 }
132 inheritPacket->tail = PayloadNew(tailSize);
133 if (inheritPacket->tail != NULL) {
134 tempTail->next = inheritPacket->tail;
135 inheritPacket->tail->prev = tempTail;
136 }
137 }
138 return inheritPacket;
139 }
140
PacketPayloadAddLast(const Packet * pkt,const Buffer * buf)141 void PacketPayloadAddLast(const Packet *pkt, const Buffer *buf)
142 {
143 Payload *last = pkt->payload;
144 while (last != NULL && last->next != pkt->tail) {
145 last = last->next;
146 }
147
148 if (last != NULL) {
149 if (last->buf == NULL) {
150 last->buf = BufferRefMalloc(buf);
151 } else {
152 Payload *newLast = PayloadNewRef(buf);
153 if (newLast != NULL) {
154 newLast->next = pkt->tail;
155 pkt->tail->prev = newLast;
156 last->next = newLast;
157 newLast->prev = last;
158 }
159 }
160 }
161 }
162
PacketFree(Packet * pkt)163 void PacketFree(Packet *pkt)
164 {
165 if (pkt == NULL) {
166 return;
167 }
168
169 Payload *node = pkt->head;
170 while (node != NULL) {
171 Payload *tempNode = node;
172 node = node->next;
173 PayloadFree(tempNode);
174 }
175 free(pkt);
176 }
177
PacketHead(const Packet * pkt)178 Buffer *PacketHead(const Packet *pkt)
179 {
180 ASSERT(pkt);
181 return pkt->head->buf;
182 }
183
PacketTail(const Packet * pkt)184 Buffer *PacketTail(const Packet *pkt)
185 {
186 ASSERT(pkt);
187 return pkt->tail->buf;
188 }
189
PacketContinuousPayload(Packet * pkt)190 Buffer *PacketContinuousPayload(Packet *pkt)
191 {
192 ASSERT(pkt);
193 if (pkt->payload == NULL) {
194 return NULL;
195 }
196 if (pkt->payload->next == pkt->tail) {
197 return pkt->payload->buf;
198 }
199
200 uint32_t payloadSize = PacketPayloadSize(pkt);
201 if (payloadSize == 0) {
202 return NULL;
203 }
204
205 Payload *newPayload = PayloadNew(payloadSize);
206 if (newPayload == NULL) {
207 return NULL;
208 }
209 Payload *payloadIter = pkt->payload;
210 uint8_t *bufPtr = BufferPtr(newPayload->buf);
211
212 while (payloadIter != pkt->tail) {
213 uint32_t bufSize = BufferGetSize(payloadIter->buf);
214 if (bufSize > 0) {
215 (void)memcpy_s(bufPtr, bufSize, BufferPtr(payloadIter->buf), bufSize);
216 bufPtr += bufSize;
217 }
218 Payload *payloadTemp = payloadIter;
219 payloadIter = payloadIter->next;
220 PayloadFree(payloadTemp);
221 }
222
223 newPayload->next = pkt->tail;
224 pkt->tail->prev = newPayload;
225
226 pkt->head->next = newPayload;
227 newPayload->prev = pkt->head;
228
229 pkt->payload = newPayload;
230
231 return newPayload->buf;
232 }
233
PacketPayloadSize(const Packet * pkt)234 uint32_t PacketPayloadSize(const Packet *pkt)
235 {
236 ASSERT(pkt);
237 uint32_t size = 0;
238 Payload *node = pkt->payload;
239 while (node != pkt->tail) {
240 size += BufferGetSize(node->buf);
241 node = node->next;
242 }
243 return size;
244 }
245
PacketMoveToOffset(const Packet * pkt,Payload * start,const Payload * end,uint32_t * offset)246 static Payload *PacketMoveToOffset(const Packet *pkt, Payload *start, const Payload *end, uint32_t *offset)
247 {
248 uint32_t pktSize = PacketSize(pkt);
249 if (*offset > pktSize) {
250 return NULL;
251 }
252 uint32_t currOffset = 0;
253 while (BufferGetSize(start->buf) + currOffset <= *offset) {
254 if (start == end) {
255 return 0;
256 } else {
257 currOffset += BufferGetSize(start->buf);
258 start = start->next;
259 }
260 }
261
262 *offset = *offset - currOffset;
263 return start;
264 }
265
PacketCopyToBuffer(Payload * start,const Payload * end,uint8_t * buffer,uint32_t offset,uint32_t size)266 static uint32_t PacketCopyToBuffer(Payload *start, const Payload *end, uint8_t *buffer, uint32_t offset, uint32_t size)
267 {
268 uint32_t retSize;
269 uint32_t remain = BufferGetSize(start->buf) - offset;
270 if (remain >= size) {
271 if (memcpy_s(buffer, size, (uint8_t *)BufferPtr(start->buf) + offset, size) != EOK) {
272 LOG_ERROR("PacketCopyToBuffer, memcpy_s fail");
273 return 0;
274 }
275 return size;
276 } else {
277 retSize = remain;
278 (void)memcpy_s(buffer, remain, (uint8_t *)BufferPtr(start->buf) + offset, remain);
279
280 buffer += remain;
281 remain = size - remain;
282 start = start->next;
283
284 for (; start != end; start = start->next) {
285 if (remain == 0) {
286 break;
287 }
288 uint32_t buffSize = BufferGetSize(start->buf);
289 uint32_t copySize = ((remain <= buffSize) ? remain : buffSize);
290 (void)memcpy_s(buffer, copySize, (uint8_t *)BufferPtr(start->buf), copySize);
291 retSize += copySize;
292 buffer += copySize;
293 remain -= copySize;
294 }
295 }
296 return retSize;
297 }
298
PacketCopyFromBuffer(Payload * start,const Payload * end,const uint8_t * buffer,uint32_t offset,uint32_t size)299 static uint32_t PacketCopyFromBuffer(
300 Payload *start, const Payload *end, const uint8_t *buffer, uint32_t offset, uint32_t size)
301 {
302 uint32_t retSize;
303 uint32_t remain = BufferGetSize(start->buf) - offset;
304 if (remain >= size) {
305 if (memcpy_s((uint8_t *)BufferPtr(start->buf) + offset, size, buffer, size) != EOK) {
306 LOG_ERROR("PacketCopyFromBuffer, memcpy_s fail");
307 return 0;
308 }
309 return size;
310 } else {
311 retSize = remain;
312 (void)memcpy_s((uint8_t *)BufferPtr(start->buf) + offset, size, buffer, remain);
313
314 buffer += remain;
315 remain = size - remain;
316 start = start->next;
317
318 for (; start != end; start = start->next) {
319 if (remain == 0) {
320 break;
321 }
322 uint32_t buffSize = BufferGetSize(start->buf);
323 uint32_t copySize = ((remain <= buffSize) ? remain : buffSize);
324 (void)memcpy_s((uint8_t *)BufferPtr(start->buf), copySize, buffer, copySize);
325 retSize += copySize;
326 buffer += copySize;
327 remain -= copySize;
328 }
329 }
330 return retSize;
331 }
332
PacketPayloadRead(const Packet * pkt,uint8_t * dst,uint32_t offset,uint32_t size)333 uint32_t PacketPayloadRead(const Packet *pkt, uint8_t *dst, uint32_t offset, uint32_t size)
334 {
335 ASSERT(pkt);
336 ASSERT(dst);
337 if (size == 0) {
338 return 0;
339 }
340 Payload *start = pkt->payload;
341 Payload *end = pkt->tail;
342 start = PacketMoveToOffset(pkt, start, end, &offset);
343 if (start == NULL) {
344 return 0;
345 }
346
347 return PacketCopyToBuffer(start, end, dst, offset, size);
348 }
349
PacketPayloadWrite(const Packet * pkt,const uint8_t * src,uint32_t offset,uint32_t size)350 uint32_t PacketPayloadWrite(const Packet *pkt, const uint8_t *src, uint32_t offset, uint32_t size)
351 {
352 ASSERT(pkt);
353 ASSERT(src);
354 if (size == 0) {
355 return 0;
356 }
357 Payload *start = pkt->payload;
358 Payload *end = pkt->tail;
359 start = PacketMoveToOffset(pkt, start, end, &offset);
360 if (start == NULL) {
361 return 0;
362 }
363
364 return PacketCopyFromBuffer(start, end, src, offset, size);
365 }
366
PacketSize(const Packet * pkt)367 uint32_t PacketSize(const Packet *pkt)
368 {
369 ASSERT(pkt);
370 uint32_t size = 0;
371 Payload *node = pkt->head;
372 while (node != NULL) {
373 size += BufferGetSize(node->buf);
374 node = node->next;
375 }
376 return size;
377 }
378
PacketRead(const Packet * pkt,uint8_t * dst,uint32_t offset,uint32_t size)379 uint32_t PacketRead(const Packet *pkt, uint8_t *dst, uint32_t offset, uint32_t size)
380 {
381 ASSERT(pkt);
382 ASSERT(dst);
383 if (size == 0) {
384 return 0;
385 }
386 Payload *start = pkt->head;
387 Payload *end = NULL;
388 start = PacketMoveToOffset(pkt, start, end, &offset);
389 if (start == NULL) {
390 return 0;
391 }
392
393 return PacketCopyToBuffer(start, end, dst, offset, size);
394 }
395
PacketExtractHead(Packet * pkt,uint8_t * data,uint32_t size)396 void PacketExtractHead(Packet *pkt, uint8_t *data, uint32_t size)
397 {
398 ASSERT(pkt);
399 ASSERT(data);
400 if (size > PacketPayloadSize(pkt)) {
401 return;
402 }
403
404 Payload *first = pkt->payload;
405 while (BufferGetSize(first->buf) < size) {
406 uint32_t buffSize = BufferGetSize(first->buf);
407 (void)memcpy_s(data, buffSize, (uint8_t *)BufferPtr(first->buf), buffSize);
408 size -= buffSize;
409 data += buffSize;
410 Payload *tempFirst = first;
411 first = first->next;
412 PayloadFree(tempFirst);
413 }
414 (void)memcpy_s(data, size, (uint8_t *)BufferPtr(first->buf), size);
415 first->buf = BufferResize(first->buf, size, BufferGetSize(first->buf) - size);
416 pkt->payload = first;
417 pkt->head->next = pkt->payload;
418 pkt->payload->prev = pkt->head;
419 }
420
PacketExtractTail(const Packet * pkt,uint8_t * data,uint32_t size)421 void PacketExtractTail(const Packet *pkt, uint8_t *data, uint32_t size)
422 {
423 ASSERT(pkt);
424 ASSERT(data);
425 if (size > PacketPayloadSize(pkt)) {
426 return;
427 }
428
429 Payload *last = pkt->tail->prev;
430 while (BufferGetSize(last->buf) < size) {
431 size_t buffSize = BufferGetSize(last->buf);
432 size -= buffSize;
433 (void)memcpy_s(data + size, buffSize, (uint8_t *)BufferPtr(last->buf), buffSize);
434
435 struct Payload *tempLast = last;
436 last = last->prev;
437 PayloadFree(tempLast);
438 }
439 uint32_t buffSize = BufferGetSize(last->buf);
440 (void)memcpy_s(data, size, (uint8_t *)BufferPtr(last->buf) + (buffSize - size), size);
441 last->buf = BufferResize(last->buf, 0, buffSize - size);
442 last->next = pkt->tail;
443 pkt->tail->prev = last;
444 }
445
PacketFragment(Packet * uplayer,const Packet * downlayer,uint32_t fragLength)446 uint32_t PacketFragment(Packet *uplayer, const Packet *downlayer, uint32_t fragLength)
447 {
448 ASSERT(uplayer);
449 ASSERT(downlayer);
450 if (fragLength == 0) {
451 return 0;
452 }
453 Payload *dFirst = downlayer->payload;
454 Payload *first = uplayer->head;
455 dFirst->next = NULL;
456 while (first != NULL && fragLength > 0) {
457 uint32_t buffSize = BufferGetSize(first->buf);
458 if (fragLength >= buffSize) {
459 dFirst->next = PayloadNewRef(first->buf);
460 if (dFirst->next != NULL) {
461 dFirst->next->prev = dFirst;
462 fragLength -= buffSize;
463 Payload *temp = first;
464 first = first->next;
465 dFirst = dFirst->next;
466 uplayer->head = first;
467 PayloadFree(temp);
468 }
469 } else {
470 dFirst->next = (Payload *)calloc(1, sizeof(Payload));
471 if (dFirst->next != NULL) {
472 dFirst->next->prev = dFirst;
473 dFirst->next->buf = BufferSliceMalloc(first->buf, 0, fragLength);
474 dFirst = dFirst->next;
475 first->buf = BufferResize(first->buf, fragLength, buffSize - fragLength);
476 fragLength = 0;
477 uplayer->head = first;
478 }
479 }
480 }
481 if (first == NULL) {
482 uplayer->head = NULL;
483 uplayer->payload = NULL;
484 uplayer->tail = NULL;
485 dFirst->next = downlayer->tail;
486 downlayer->tail->prev = dFirst;
487 return 0;
488 }
489 dFirst->next = downlayer->tail;
490 downlayer->tail->prev = dFirst;
491 return PacketSize(uplayer);
492 }
493
PacketAssemble(const Packet * uplayer,const Packet * downlayer)494 void PacketAssemble(const Packet *uplayer, const Packet *downlayer)
495 {
496 Packet *refPacket = PacketRefMalloc(downlayer);
497 if (refPacket == NULL) {
498 LOG_ERROR("Failed to execute func PacketRefMalloc in func PacketAssemble");
499 return;
500 }
501 Payload *upLast = uplayer->payload;
502 while ((upLast != NULL) && (upLast->next != uplayer->tail)) {
503 upLast = upLast->next;
504 }
505
506 if (upLast != NULL && refPacket->payload != NULL) {
507 upLast->next = refPacket->payload;
508 refPacket->payload->prev = upLast;
509 }
510
511 Payload *refLast = refPacket->payload;
512 while ((refLast != NULL) && (refLast->next != refPacket->tail)) {
513 refLast = refLast->next;
514 }
515
516 PayloadFree(refPacket->head);
517 refPacket->head = NULL;
518 PayloadFree(refPacket->tail);
519 refPacket->tail = NULL;
520
521 if (refLast != NULL) {
522 refLast->next = uplayer->tail;
523 uplayer->tail->prev = refLast;
524 }
525
526 free(refPacket);
527 }
528
PacketCalCrc16(const Packet * pkt,CalCrc16 calCrc16)529 uint16_t PacketCalCrc16(const Packet *pkt, CalCrc16 calCrc16)
530 {
531 uint16_t retVal = 0;
532 Payload *iter = pkt->head;
533 while (iter != pkt->tail) {
534 Buffer *buf = iter->buf;
535 uint8_t *ptr = BufferPtr(buf);
536 uint32_t bufSize = BufferGetSize(buf);
537
538 while (bufSize) {
539 retVal = calCrc16(*ptr, retVal);
540 ptr++;
541 bufSize--;
542 }
543
544 iter = iter->next;
545 }
546
547 return retVal;
548 }
549
PacketVerCrc16(const Packet * pkt,CalCrc16 calCrc16,uint16_t crcVal)550 int32_t PacketVerCrc16(const Packet *pkt, CalCrc16 calCrc16, uint16_t crcVal)
551 {
552 uint16_t calVal = 0;
553 Payload *iter = pkt->head;
554 while (iter != pkt->tail) {
555 Buffer *buf = iter->buf;
556 uint8_t *ptr = BufferPtr(buf);
557 uint32_t bufSize = BufferGetSize(buf);
558
559 while (bufSize) {
560 calVal = calCrc16(*ptr, calVal);
561 ptr++;
562 bufSize--;
563 }
564
565 iter = iter->next;
566 }
567
568 if (calVal != crcVal) {
569 return -1;
570 }
571
572 return 0;
573 }