-
Notifications
You must be signed in to change notification settings - Fork 2
/
onps_utils.c
1285 lines (1093 loc) · 31 KB
/
onps_utils.c
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022-2024 The Onps Project Author All Rights Reserved.
*
* Author:Neo-T
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* http://www.onps.org.cn/apache2.0.txt
*
*/
#include "port/datatype.h"
#include "port/sys_config.h"
#include "onps_errors.h"
#include "port/os_datatype.h"
#include "port/os_adapter.h"
#include "mmu/buf_list.h"
#include "ip/ip_frame.h"
#define SYMBOL_GLOBALS
#include "onps_utils.h"
#undef SYMBOL_GLOBALS
#include "netif/netif.h"
CHAR *mem_char(CHAR *pszMem, CHAR ch, UINT unMemSize)
{
CHAR *pszNext = pszMem;
UINT i;
for (i = 0; i < unMemSize; i++)
{
if (*(pszNext + i) == ch)
return (pszNext + i);
}
return NULL;
}
CHAR *mem_str(CHAR *pszMem, CHAR *pszStr, UINT unStrSize, UINT unMemSize)
{
CHAR *pszNext = pszMem;
pszNext = mem_char(pszNext, pszStr[0], unMemSize);
while (pszNext != NULL)
{
if (strncmp((char *)pszNext, (char *)pszStr, unStrSize) == 0)
return pszNext;
pszNext += 1;
pszNext = mem_char(pszNext, (CHAR)pszStr[0], (pszMem + unMemSize) - pszNext);
}
return NULL;
}
USHORT tcpip_checksum(USHORT *pusData, INT nDataBytes)
{
ULONG ulChecksum = 0;
while (nDataBytes > 1)
{
ulChecksum += *pusData++;
nDataBytes -= sizeof(USHORT);
}
if (nDataBytes)
{
ulChecksum += *(UCHAR*)pusData;
}
ulChecksum = (ulChecksum >> 16) + (ulChecksum & 0xffff);
ulChecksum += (ulChecksum >> 16);
return (USHORT)(~ulChecksum);
}
USHORT tcpip_checksum_ext(SHORT sBufListHead)
{
ULONG ulChecksum = 0;
SHORT sNextNode = sBufListHead;
UCHAR *pubData;
USHORT usDataLen;
USHORT usData = 0;
CHAR bState = 0;
__lblGetNextNode:
pubData = (UCHAR *)buf_list_get_next_node(&sNextNode, &usDataLen);
if (NULL == pubData)
{
ulChecksum += usData;
ulChecksum = (ulChecksum >> 16) + (ulChecksum & 0xffff);
ulChecksum += (ulChecksum >> 16);
return (USHORT)(~ulChecksum);
}
USHORT i;
for (i = 0; i < usDataLen; i++)
{
switch (bState)
{
case 0:
((UCHAR *)&usData)[0] = pubData[i];
bState = 1;
break;
case 1:
((UCHAR *)&usData)[1] = pubData[i];
ulChecksum += usData;
usData = 0;
bState = 0;
break;
}
}
goto __lblGetNextNode;
}
USHORT tcpip_checksum_ipv4(in_addr_t unSrcAddr, in_addr_t unDstAddr, USHORT usPayloadLen, UCHAR ubProto, SHORT sBufListHead, EN_ONPSERR *penErr)
{
//* 填充用于校验和计算的ip伪报头
ST_IP_PSEUDOHDR stPseudoHdr;
stPseudoHdr.unSrcAddr = unSrcAddr;
stPseudoHdr.unDstAddr = unDstAddr;
stPseudoHdr.ubMustBeZero = 0;
stPseudoHdr.ubProto = ubProto;
stPseudoHdr.usPacketLen = htons(usPayloadLen);
SHORT sPseudoHdrNode = buf_list_get_ext((UCHAR *)&stPseudoHdr, (UINT)sizeof(ST_IP_PSEUDOHDR), penErr);
if (sPseudoHdrNode < 0)
return 0;
buf_list_put_head(&sBufListHead, sPseudoHdrNode);
//* 计算校验和
USHORT usChecksum = tcpip_checksum_ext(sBufListHead);
//* 释放伪报头
buf_list_free_head(&sBufListHead, sPseudoHdrNode);
return usChecksum;
}
USHORT tcpip_checksum_ipv4_ext(in_addr_t unSrcAddr, in_addr_t unDstAddr, UCHAR ubProto, UCHAR *pubPayload, USHORT usPayloadLen, EN_ONPSERR *penErr)
{
//* 把完整的udp报文放到buf list链表以便计算udp校验和确保收到的udp报文正确
SHORT sBufListHead = -1;
SHORT sPayloadNode = buf_list_get_ext(pubPayload, (UINT)usPayloadLen, penErr);
if (sPayloadNode < 0)
return 0;
buf_list_put_head(&sBufListHead, sPayloadNode);
USHORT usChecksum = tcpip_checksum_ipv4(unSrcAddr, unDstAddr, usPayloadLen, ubProto, sBufListHead, penErr);
buf_list_free(sPayloadNode);
return usChecksum;
}
#if SUPPORT_IPV6
USHORT tcpip_checksum_ipv6(UCHAR ubaSrcAddr[16], UCHAR ubaDstAddr[16], UINT unPayloadLen, UCHAR ubProto, SHORT sBufListHead, EN_ONPSERR *penErr)
{
//* 填充用于校验和计算的ipv6伪报头
ST_IPv6_PSEUDOHDR stPseudoHdr;
memcpy(stPseudoHdr.ubaSrcIpv6, ubaSrcAddr, 16);
memcpy(stPseudoHdr.ubaDstIpv6, ubaDstAddr, 16);
stPseudoHdr.unIpv6PayloadLen = htonl(unPayloadLen);
stPseudoHdr.ubaMustBeZero[0] = stPseudoHdr.ubaMustBeZero[1] = stPseudoHdr.ubaMustBeZero[2] = 0;
stPseudoHdr.ubProto = ubProto;
SHORT sPseudoHdrNode = buf_list_get_ext((UCHAR *)&stPseudoHdr, (UINT)sizeof(ST_IPv6_PSEUDOHDR), penErr);
if (sPseudoHdrNode < 0)
return 0;
buf_list_put_head(&sBufListHead, sPseudoHdrNode);
//* 计算校验和
USHORT usChecksum = tcpip_checksum_ext(sBufListHead);
//* 释放伪报头
buf_list_free_head(&sBufListHead, sPseudoHdrNode);
return usChecksum;
}
USHORT tcpip_checksum_ipv6_ext(UCHAR ubaSrcAddr[16], UCHAR ubaDstAddr[16], UCHAR ubProto, UCHAR *pubPayload, UINT unPayloadLen, EN_ONPSERR *penErr)
{
//* 把完整的udp报文放到buf list链表以便计算udp校验和确保收到的udp报文正确
SHORT sBufListHead = -1;
SHORT sPayloadNode = buf_list_get_ext(pubPayload, unPayloadLen, penErr);
if (sPayloadNode < 0)
return 0;
buf_list_put_head(&sBufListHead, sPayloadNode);
USHORT usChecksum = tcpip_checksum_ipv6(ubaSrcAddr, ubaDstAddr, (USHORT)unPayloadLen, ubProto, sBufListHead, penErr);
buf_list_free(sPayloadNode);
return usChecksum;
}
#endif
void snprintf_hex(const UCHAR *pubHexData, USHORT usHexDataLen, CHAR *pszDstBuf, UINT unDstBufSize, BOOL blIsSeparateWithSpace)
{
UINT i, unFormatBytes = 0;
if (!usHexDataLen || !unDstBufSize || unDstBufSize < 4)
return;
pszDstBuf[0] = 0;
sprintf(pszDstBuf, "%02X", pubHexData[0]);
unFormatBytes = 2;
for (i = 1; i < usHexDataLen; i++)
{
CHAR szHex[sizeof(UCHAR) * 2 + 2];
if (blIsSeparateWithSpace)
{
sprintf(szHex, " %02X", pubHexData[i]);
unFormatBytes += 3;
}
else
{
sprintf(szHex, "%02X", pubHexData[i]);
unFormatBytes += 2;
}
if (unFormatBytes < unDstBufSize)
strcat(pszDstBuf, szHex);
else
break;
}
pszDstBuf[unFormatBytes] = 0;
}
#if SUPPORT_PRINTF
void printf_hex(const UCHAR *pubHex, USHORT usHexDataLen, UCHAR ubBytesPerLine)
{
if (!usHexDataLen)
return;
printf("%02X", pubHex[0]);
INT i;
for (i = 1; i < usHexDataLen; i++)
{
if (i % (INT)ubBytesPerLine)
printf(" ");
else
printf("\r\n");
printf("%02X", pubHex[i]);
}
printf("\r\n");
}
void printf_hex_ext(SHORT sBufListHead, UCHAR ubBytesPerLine)
{
SHORT sNextNode = sBufListHead;
UCHAR *pubData;
USHORT usDataLen;
INT i = 0;
while (NULL != (pubData = (UCHAR *)buf_list_get_next_node(&sNextNode, &usDataLen)))
{
INT k;
for (k = 0; k < (INT)usDataLen; i++, k++)
{
if (i >= ubBytesPerLine)
{
i = 0;
printf("\r\n");
}
if (i)
printf(" %02X", pubData[k]);
else
printf("%02X", pubData[k]);
}
}
printf("\r\n");
}
#endif
PST_SLINKEDLIST_NODE sllist_get_node(PST_SLINKEDLIST *ppstSLList)
{
if (NULL == *ppstSLList)
return NULL;
PST_SLINKEDLIST_NODE pstNode = *ppstSLList;
*ppstSLList = (*ppstSLList)->pstNext;
pstNode->pstNext = NULL;
return pstNode;
}
PST_SLINKEDLIST_NODE sllist_get_tail_node(PST_SLINKEDLIST *ppstSLList)
{
if (NULL == *ppstSLList)
return NULL;
PST_SLINKEDLIST_NODE pstNextNode = *ppstSLList;
PST_SLINKEDLIST_NODE pstPrevNode = NULL;
while (pstNextNode)
{
if(NULL == pstNextNode->pstNext) //* 到了尾部节点了
{
if (pstPrevNode)
pstPrevNode->pstNext = NULL;
else
*ppstSLList = NULL;
pstNextNode->pstNext = NULL;
return pstNextNode;
}
pstPrevNode = pstNextNode;
pstNextNode = pstNextNode->pstNext;
}
return NULL;
}
void sllist_del_node(PST_SLINKEDLIST *ppstSLList, PST_SLINKEDLIST_NODE pstNode)
{
PST_SLINKEDLIST_NODE pstNextNode = *ppstSLList;
PST_SLINKEDLIST_NODE pstPrevNode = NULL;
while (pstNextNode)
{
//* 节点地址相等则意味着找到了要删除的节点在链表中的链接位置
if (pstNextNode == pstNode)
{
if (pstPrevNode)
pstPrevNode->pstNext = pstNextNode->pstNext;
else
*ppstSLList = pstNextNode->pstNext;
break;
}
pstPrevNode = pstNextNode;
pstNextNode = pstNextNode->pstNext;
}
}
void sllist_del_node_ext(PST_SLINKEDLIST *ppstSLList, void *pvData)
{
PST_SLINKEDLIST_NODE pstNextNode = *ppstSLList;
PST_SLINKEDLIST_NODE pstPrevNode = NULL;
while (pstNextNode)
{
//* 节点携带的数据地址相等则意味着找到了要删除的节点在链表中的链接位置
if (pstNextNode->uniData.ptr == pvData)
{
if (pstPrevNode)
pstPrevNode->pstNext = pstNextNode->pstNext;
else
*ppstSLList = pstNextNode->pstNext;
break;
}
pstPrevNode = pstNextNode;
pstNextNode = pstNextNode->pstNext;
}
}
void sllist_put_node(PST_SLINKEDLIST *ppstSLList, PST_SLINKEDLIST_NODE pstNode)
{
pstNode->pstNext = *ppstSLList;
*ppstSLList = pstNode;
}
void sllist_put_tail_node(PST_SLINKEDLIST *ppstSLList, PST_SLINKEDLIST_NODE pstNode)
{
if (NULL == *ppstSLList)
{
*ppstSLList = pstNode;
pstNode->pstNext = NULL;
return;
}
PST_SLINKEDLIST_NODE pstNextNode = *ppstSLList;
PST_SLINKEDLIST_NODE pstPrevNode = NULL;
while (pstNextNode)
{
pstPrevNode = pstNextNode;
pstNextNode = pstNextNode->pstNext;
}
//* 一旦执行到这里尾部节点一定不为NULL
pstPrevNode->pstNext = pstNode;
pstNode->pstNext = NULL;
}
CHAR array_linked_list_get_index(void *pvUnit, void *pvArray, UCHAR ubUnitSize, CHAR bUnitNum)
{
CHAR i;
for (i = 0; i < bUnitNum; i++)
{
if ((UCHAR *)pvUnit == ((UCHAR *)pvArray + i * ubUnitSize))
return i;
}
return -1;
}
void *array_linked_list_get(CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bOffsetNextUnit, CHAR *pbUnitIdx)
{
void *pvUnit;
if (*pbListHead < 0)
return NULL;
if (pbUnitIdx)
*pbUnitIdx = *pbListHead;
pvUnit = (UCHAR *)pvArray + (*pbListHead) * ubUnitSize;
*pbListHead = *((CHAR *)pvArray + (*pbListHead) * ubUnitSize + bOffsetNextUnit);
return pvUnit;
}
void *array_linked_list_get_safe(CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bOffsetNextUnit, CHAR *pbUnitIdx)
{
void *pvUnit;
os_critical_init();
os_enter_critical();
{
if (*pbListHead < 0)
{
os_exit_critical();
return NULL;
}
if (pbUnitIdx)
*pbUnitIdx = *pbListHead;
pvUnit = (UCHAR *)pvArray + (*pbListHead) * ubUnitSize;
*pbListHead = *((CHAR *)pvArray + (*pbListHead) * ubUnitSize + bOffsetNextUnit);
}
os_exit_critical();
return pvUnit;
}
void array_linked_list_put(void *pvUnit, CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bUnitNum, CHAR bOffsetNextUnit)
{
CHAR bNodeIdx = array_linked_list_get_index(pvUnit, pvArray, ubUnitSize, bUnitNum);
if (bNodeIdx >= 0)
{
*((CHAR *)pvUnit + bOffsetNextUnit) = *pbListHead;
*pbListHead = bNodeIdx;
}
}
void array_linked_list_put_safe(void *pvUnit, CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bUnitNum, CHAR bOffsetNextUnit)
{
os_critical_init();
CHAR bNodeIdx = array_linked_list_get_index(pvUnit, pvArray, ubUnitSize, bUnitNum);
if (bNodeIdx >= 0)
{
os_enter_critical();
{
*((CHAR *)pvUnit + bOffsetNextUnit) = *pbListHead;
*pbListHead = bNodeIdx;
}
os_exit_critical();
}
}
void array_linked_list_put_tail(void *pvUnit, CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bUnitNum, CHAR bOffsetNextUnit)
{
CHAR bNodeIdx = array_linked_list_get_index(pvUnit, pvArray, ubUnitSize, bUnitNum);
if (bNodeIdx >= 0)
{
CHAR bNode = *pbListHead;
if (bNode >= 0)
{
CHAR bNextNode;
do {
bNextNode = *((CHAR *)pvArray + bNode * ubUnitSize + bOffsetNextUnit);
if (bNextNode < 0) //* 到了尾部
{
*((CHAR *)pvArray + bNode * ubUnitSize + bOffsetNextUnit) = bNodeIdx;
break;
}
bNode = bNextNode;
} while (TRUE);
}
else
*pbListHead = bNodeIdx;
*((CHAR *)pvUnit + bOffsetNextUnit) = -1;
}
}
void array_linked_list_put_tail_safe(void *pvUnit, CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bUnitNum, CHAR bOffsetNextUnit)
{
os_critical_init();
CHAR bNodeIdx = array_linked_list_get_index(pvUnit, pvArray, ubUnitSize, bUnitNum);
if (bNodeIdx >= 0)
{
os_enter_critical();
{
CHAR bNode = *pbListHead;
if (bNode >= 0)
{
CHAR bNextNode;
do {
bNextNode = *((CHAR *)pvArray + bNode * ubUnitSize + bOffsetNextUnit);
if (bNextNode < 0) //* 到了尾部
{
*((CHAR *)pvArray + bNode * ubUnitSize + bOffsetNextUnit) = bNodeIdx;
break;
}
bNode = bNextNode;
} while (TRUE);
}
else
*pbListHead = bNodeIdx;
*((CHAR *)pvUnit + bOffsetNextUnit) = -1;
}
os_exit_critical();
}
}
void array_linked_list_del(void *pvUnit, CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bOffsetNextUnit)
{
CHAR bNextNode = *pbListHead;
CHAR bPrevNode = -1;
while (bNextNode >= 0)
{
//* 找到要摘除的节点并摘除之
if ((UCHAR *)pvUnit == (UCHAR *)pvArray + bNextNode * ubUnitSize)
{
if (bPrevNode >= 0)
*((CHAR *)pvArray + bPrevNode * ubUnitSize + bOffsetNextUnit) = *((CHAR *)pvUnit + bOffsetNextUnit);
else
*pbListHead = *((CHAR *)pvUnit + bOffsetNextUnit);
break;
}
bPrevNode = bNextNode;
bNextNode = *((CHAR *)pvArray + bNextNode * ubUnitSize + bOffsetNextUnit);
}
}
void array_linked_list_del_safe(void *pvUnit, CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bOffsetNextUnit)
{
os_critical_init();
os_enter_critical();
{
CHAR bNextNode = *pbListHead;
CHAR bPrevNode = -1;
while (bNextNode >= 0)
{
//* 找到要摘除的节点并摘除之
if ((UCHAR *)pvUnit == (UCHAR *)pvArray + bNextNode * ubUnitSize)
{
if (bPrevNode >= 0)
*((CHAR *)pvArray + bPrevNode * ubUnitSize + bOffsetNextUnit) = *((CHAR *)pvUnit + bOffsetNextUnit);
else
*pbListHead = *((CHAR *)pvUnit + bOffsetNextUnit);
break;
}
bPrevNode = bNextNode;
bNextNode = *((CHAR *)pvArray + bNextNode * ubUnitSize + bOffsetNextUnit);
}
}
os_exit_critical();
}
void *array_linked_list_next(CHAR *pbNextUnit, CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bOffsetNextUnit, CHAR *pbUnitIdx)
{
if (*pbListHead >= 0)
{
CHAR bUnit;
if (*pbNextUnit >= 0)
bUnit = *pbNextUnit;
else
bUnit = *pbListHead;
if (pbUnitIdx)
*pbUnitIdx = bUnit;
*pbNextUnit = *((CHAR *)pvArray + bUnit * ubUnitSize + bOffsetNextUnit);
return (CHAR *)pvArray + bUnit * ubUnitSize;
}
else
return NULL;
}
void *array_linked_list_next_ext(void *pvUnit, CHAR *pbListHead, void *pvArray, UCHAR ubUnitSize, CHAR bOffsetNextUnit)
{
if (pvUnit)
{
CHAR bNextUnit = *pbListHead;
void *pvNextUnit;
while (bNextUnit >= 0)
{
pvNextUnit = (UCHAR *)pvArray + bNextUnit * ubUnitSize;
if (pvUnit == (void *)pvNextUnit) //* 找到当前节点
{
//* 取下一个节点
bNextUnit = *((CHAR *)pvArray + bNextUnit * ubUnitSize + bOffsetNextUnit);
if (bNextUnit >= 0)
return (CHAR *)pvArray + bNextUnit * ubUnitSize;
break;
}
bNextUnit = *((CHAR *)pvArray + bNextUnit * ubUnitSize + bOffsetNextUnit);
}
}
else
{
if(*pbListHead >= 0)
return (CHAR *)pvArray + (*pbListHead) * ubUnitSize;
}
return NULL;
}
CHAR *strtok_safe(CHAR **ppszStart, const CHAR *pszSplitStr)
{
if (NULL == *ppszStart)
return NULL;
CHAR *pszItem = NULL;
CHAR *pszStart = *ppszStart;
INT i = 0;
CHAR ch;
while ((ch = pszStart[i]) != 0x00)
{
CHAR bSplit;
BOOL blIsNotFound = TRUE;
INT k = 0;
while ((bSplit = pszSplitStr[k++]) != 0x00)
{
if (bSplit == ch)
{
pszStart[i] = 0;
blIsNotFound = FALSE;
break;
}
}
if (blIsNotFound)
{
if (NULL == pszItem)
pszItem = pszStart + i;
}
else
{
if (pszItem)
{
*ppszStart = pszStart + i + 1;
return pszItem;
}
}
i++;
}
*ppszStart = NULL;
return pszItem;
}
UINT rand_big(void)
{
UINT unRandomVal;
((UCHAR *)&unRandomVal)[0] = (UCHAR)(rand() % 256);
((UCHAR *)&unRandomVal)[1] = (UCHAR)(rand() % 256);
((UCHAR *)&unRandomVal)[2] = (UCHAR)(rand() % 256);
((UCHAR *)&unRandomVal)[3] = (UCHAR)(rand() % 256);
return unRandomVal;
}
UCHAR *rand_any_bytes(UCHAR *pubRandSeq, UINT unSeqLen)
{
UINT i;
for (i = 0; i < unSeqLen; i++)
pubRandSeq[i] = (UCHAR)(rand() % 256);
return pubRandSeq;
}
const CHAR *hex_to_str_8(UCHAR ubVal, CHAR szDst[3], BOOL blIsUppercase)
{
szDst[0] = ubVal >> 4;
szDst[1] = ubVal & 0x0F;
hex_to_char(szDst[0], blIsUppercase);
hex_to_char(szDst[1], blIsUppercase);
szDst[2] = 0;
return szDst;
}
const CHAR *hex_to_str_no_lz_8(UCHAR ubVal, CHAR szDst[3], BOOL blIsUppercase, CHAR *pbBytes)
{
if (!ubVal)
{
szDst[0] = '0';
szDst[1] = 0;
if (pbBytes)
*pbBytes = 1;
return szDst;
}
szDst[0] = ubVal >> 4;
szDst[1] = ubVal & 0x0F;
CHAR bBytes = 2;
if (szDst[0])
{
hex_to_char(szDst[0], blIsUppercase);
hex_to_char(szDst[1], blIsUppercase);
szDst[bBytes] = 0;
}
else
{
hex_to_char_no_lz(szDst[0], szDst[1], blIsUppercase);
bBytes = 1;
szDst[bBytes] = 0;
}
if (pbBytes)
*pbBytes = bBytes;
return szDst;
}
const CHAR *hex_to_str_16(USHORT usVal, CHAR szDst[5], BOOL blIsUppercase, BOOL blIsBigEndian)
{
if (blIsBigEndian)
{
szDst[0] = (((UCHAR*)&usVal)[0]) >> 4;
szDst[1] = (((UCHAR*)&usVal)[0]) & 0x0F;
szDst[2] = (((UCHAR*)&usVal)[1]) >> 4;
szDst[3] = (((UCHAR*)&usVal)[1]) & 0x0F;
}
else
{
szDst[0] = (((UCHAR*)&usVal)[1]) >> 4;
szDst[1] = (((UCHAR*)&usVal)[1]) & 0x0F;
szDst[2] = (((UCHAR*)&usVal)[0]) >> 4;
szDst[3] = (((UCHAR*)&usVal)[0]) & 0x0F;
}
hex_to_char(szDst[0], blIsUppercase);
hex_to_char(szDst[1], blIsUppercase);
hex_to_char(szDst[2], blIsUppercase);
hex_to_char(szDst[3], blIsUppercase);
szDst[4] = 0;
return szDst;
}
const CHAR *hex_to_str_no_lz_16(USHORT usVal, CHAR szDst[5], BOOL blIsUppercase, BOOL blIsBigEndian, CHAR *pbBytes)
{
if (!usVal)
{
szDst[0] = '0';
szDst[1] = 0;
if (pbBytes)
*pbBytes = 1;
return szDst;
}
if (blIsBigEndian)
{
szDst[0] = (((UCHAR*)&usVal)[0]) >> 4;
szDst[1] = (((UCHAR*)&usVal)[0]) & 0x0F;
szDst[2] = (((UCHAR*)&usVal)[1]) >> 4;
szDst[3] = (((UCHAR*)&usVal)[1]) & 0x0F;
}
else
{
szDst[0] = (((UCHAR*)&usVal)[1]) >> 4;
szDst[1] = (((UCHAR*)&usVal)[1]) & 0x0F;
szDst[2] = (((UCHAR*)&usVal)[0]) >> 4;
szDst[3] = (((UCHAR*)&usVal)[0]) & 0x0F;
}
//* 计算前导零个数
CHAR i = 0, k = 0;
for (; i < 3; i++)
{
if (szDst[i])
break;
k++;
}
if (pbBytes)
*pbBytes = 4 - k;
hex_to_char(szDst[0], blIsUppercase);
hex_to_char(szDst[1], blIsUppercase);
hex_to_char(szDst[2], blIsUppercase);
hex_to_char(szDst[3], blIsUppercase);
//* k为0,没有前导0
if (!k)
{
szDst[4] = 0;
}
else
{
//* 去掉前导0
CHAR bMovNum = 4 - k;
for (i = 0; i < bMovNum; i++)
szDst[i] = szDst[k++];
szDst[bMovNum] = 0;
}
return szDst;
}
CHAR ascii_to_hex_4(CHAR ch)
{
if (ch >= '0' && ch <= '9')
return (ch - '0');
if (ch >= 'A' && ch <= 'F')
return (ch - 'A') + 10;
if (ch >= 'a' && ch <= 'f')
return (ch - 'a') + 10;
return 0;
}
USHORT ascii_to_hex_16(const CHAR *pszAscii)
{
USHORT usValue = 0;
INT i;
for (i = 0; i < strlen(pszAscii); i++)
usValue = (usValue << 4) + ascii_to_hex_4(pszAscii[i]);
return usValue;
}
BOOL is_valid_ip(const CHAR *pszIP)
{
INT nSum = 0;
INT i = 0, j = 0;
while ('\0' != pszIP[i])
{
if (j > 3)
return FALSE;
if (pszIP[i] > 0x2F && pszIP[i] < 0x3A)
nSum = nSum * 10 + pszIP[i] - '0';
else if (pszIP[i] == '.')
{
j++;
if (nSum < 0 || nSum > 255)
return FALSE;
else
nSum = 0;
}
else
return FALSE;
i++;
}
if (j != 3 || (nSum < 0 || nSum > 255))
return FALSE;
return TRUE;
}
in_addr_t inet_addr_small(const char *pszIP)
{
in_addr_t unAddr;
CHAR *pszStart = (CHAR *)pszIP, *pszDot;
UINT unLen = (UINT)strlen(pszIP);
pszDot = mem_char(pszStart, '.', unLen);
((UCHAR *)&unAddr)[3] = (UCHAR)atoi(pszStart);
pszStart = pszDot + 1;
pszDot = mem_char(pszStart, '.', unLen - (pszStart - pszIP));
((UCHAR *)&unAddr)[2] = (UCHAR)atoi(pszStart);
pszStart = pszDot + 1;
pszDot = mem_char(pszStart, '.', unLen - (pszStart - pszIP));
((UCHAR *)&unAddr)[1] = (UCHAR)atoi(pszStart);
pszStart = pszDot + 1;
((UCHAR *)&unAddr)[0] = (UCHAR)atoi(pszStart);
return unAddr;
}
in_addr_t inet_addr(const char *pszIP)
{
in_addr_t unAddr;
CHAR *pszStart = (CHAR *)pszIP, *pszDot;
UINT unLen = (UINT)strlen(pszIP);
pszDot = mem_char(pszStart, '.', unLen);
((UCHAR *)&unAddr)[0] = (UCHAR)atoi(pszStart);
pszStart = pszDot + 1;
pszDot = mem_char(pszStart, '.', unLen - (pszStart - pszIP));
((UCHAR *)&unAddr)[1] = (UCHAR)atoi(pszStart);
pszStart = pszDot + 1;
pszDot = mem_char(pszStart, '.', unLen - (pszStart - pszIP));
((UCHAR *)&unAddr)[2] = (UCHAR)atoi(pszStart);
pszStart = pszDot + 1;
((UCHAR *)&unAddr)[3] = (UCHAR)atoi(pszStart);
return unAddr;
}
char *inet_ntoa(struct in_addr stInAddr)
{
static char szAddr[20];
UCHAR *pubAddr = (UCHAR *)&stInAddr.s_addr;
sprintf(szAddr, "%d.%d.%d.%d", pubAddr[0], pubAddr[1], pubAddr[2], pubAddr[3]);
return szAddr;
}
char *inet_ntoa_ext(in_addr_t unAddr)
{
static char szAddr[20];
UCHAR *pubAddr = (UCHAR *)&unAddr;
sprintf(szAddr, "%d.%d.%d.%d", pubAddr[0], pubAddr[1], pubAddr[2], pubAddr[3]);
return szAddr;
}
char *inet_ntoa_safe(struct in_addr stInAddr, char *pszAddr)
{
UCHAR *pubAddr = (UCHAR *)&stInAddr.s_addr;
sprintf(pszAddr, "%d.%d.%d.%d", pubAddr[0], pubAddr[1], pubAddr[2], pubAddr[3]);
return pszAddr;
}
char *inet_ntoa_safe_ext(in_addr_t unAddr, char *pszAddr)
{
UCHAR *pubAddr = (UCHAR *)&unAddr;
sprintf(pszAddr, "%d.%d.%d.%d", pubAddr[0], pubAddr[1], pubAddr[2], pubAddr[3]);
return pszAddr;
}
const CHAR *get_ip_proto_name(UCHAR ubProto)
{
switch ((EN_IPPROTO)ubProto)
{
case IPPROTO_ICMP:
return "icmp";
case IPPROTO_IGMP:
return "igmp";
case IPPROTO_TCP:
return "tcp";
case IPPROTO_UDP:
return "udp";
case IPPROTO_ICMPv6:
return "icmpv6";
case IPPROTO_RAW:
return "raw ip";
default:
return "unsupported";
}
}
//* 几级域名,也就是域名分了几段,为封装dns查询报文提供数据支持
INT get_level_of_domain_name(const CHAR *pszDomainName, INT *pnBytesOf1stSeg)
{
INT i, nCount = 1, nBytesOf1stSeg = 0;
INT nDomainNameLen = strlen(pszDomainName);
for (i = 0; i < nDomainNameLen; i++)
{
if (pszDomainName[i] == '.')
{
nCount++;
if (0 == nBytesOf1stSeg)
nBytesOf1stSeg = i;
}
}
if (0 == nBytesOf1stSeg)
nBytesOf1stSeg = i;
*pnBytesOf1stSeg = nBytesOf1stSeg;
return nCount;
}
#if SUPPORT_ETHERNET
//* 判断mac地址是否匹配
BOOL ethernet_mac_matched(const UCHAR *pubaMacAddr1, const UCHAR *pubaMacAddr2)