1 /* 2 * Copyright (c) 2025 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 #ifndef TRANS_PROXY_PROCESS_DATA_H 17 #define TRANS_PROXY_PROCESS_DATA_H 18 19 #include <stdint.h> 20 21 #include "common_list.h" 22 #include "softbus_app_info.h" 23 #include "softbus_def.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 #ifndef MAGIC_NUMBER 29 #define MAGIC_NUMBER 0xBABEFACE 30 31 #define DC_DATA_HEAD_SIZE 16 32 #define DATA_SEQ_LEN 2 33 #define NONCE_LEN 2 34 #endif 35 36 typedef struct { 37 uint8_t *inData; 38 uint32_t inLen; 39 uint8_t *outData; 40 uint32_t outLen; 41 } ProxyDataInfo; 42 43 typedef struct { 44 int32_t priority; 45 int32_t sliceNum; 46 int32_t sliceSeq; 47 int32_t reserved; 48 } SliceHead; 49 50 typedef struct { 51 int32_t active; 52 int32_t timeout; 53 int32_t sliceNumber; 54 int32_t expectedSeq; 55 int32_t dataLen; 56 int32_t bufLen; 57 char *data; 58 } SliceProcessor; 59 60 typedef struct { 61 int32_t magicNumber; 62 int32_t seq; 63 int32_t flags; 64 int32_t dataLen; 65 } PacketHead; 66 67 typedef struct { 68 int32_t flags; 69 int32_t dataLen; 70 } PacketD2DHead; 71 72 typedef struct { 73 uint16_t nonce; 74 uint16_t dataSeq; 75 } PacketD2DIvSource; 76 77 typedef struct { 78 uint32_t magicNumber; 79 uint8_t tlvCount; 80 int32_t seq; 81 uint32_t dataSeq; 82 uint32_t flags; 83 uint32_t dataLen; 84 bool needAck; 85 } DataHeadTlvPacketHead; 86 87 typedef enum { 88 PROXY_CHANNEL_PRORITY_MESSAGE = 0, 89 PROXY_CHANNEL_PRORITY_BYTES = 1, 90 PROXY_CHANNEL_PRORITY_FILE = 2, 91 PROXY_CHANNEL_PRORITY_BUTT = 3, 92 } ProxyChannelPriority; 93 94 typedef struct { 95 ListNode head; 96 int32_t channelId; 97 SliceProcessor processor[PROXY_CHANNEL_PRORITY_BUTT]; 98 } ChannelSliceProcessor; 99 100 void TransGetProxyDataBufMaxSize(void); 101 int32_t TransProxyPackBytes( 102 int32_t channelId, ProxyDataInfo *dataInfo, const char *sessionKey, SessionPktType flag, int32_t seq); 103 int32_t TransProxyPackTlvBytes( 104 ProxyDataInfo *dataInfo, const char *sessionKey, SessionPktType flag, int32_t seq, DataHeadTlvPacketHead *info); 105 uint8_t *TransProxyPackData( 106 ProxyDataInfo *dataInfo, uint32_t sliceNum, SessionPktType pktType, uint32_t cnt, uint32_t *dataLen); 107 int32_t TransProxyCheckSliceHead(const SliceHead *head); 108 int32_t TransProxyNoSubPacketProc(PacketHead *head, uint32_t len, const char *data, int32_t channelId); 109 int32_t TransProxyProcessSessionData(ProxyDataInfo *dataInfo, const PacketHead *dataHead, const char *data); 110 int32_t TransProxyDecryptPacketData(int32_t seq, ProxyDataInfo *dataInfo, const char *sessionKey); 111 int32_t TransProxyFirstSliceProcess( 112 SliceProcessor *processor, const SliceHead *head, const char *data, uint32_t len, bool supportTlv); 113 int32_t TransProxyNormalSliceProcess(SliceProcessor *processor, const SliceHead *head, const char *data, uint32_t len); 114 int32_t TransProxySliceProcessChkPkgIsValid( 115 const SliceProcessor *processor, const SliceHead *head, const char *data, uint32_t len); 116 void TransProxyClearProcessor(SliceProcessor *processor); 117 void TransUnPackSliceHead(SliceHead *data); 118 int32_t TransGetActualDataLen(const SliceHead *head, uint32_t *actualDataLen); 119 int32_t TransProxySessionDataLenCheck(uint32_t dataLen, SessionPktType type); 120 int32_t TransProxyParseTlv(uint32_t len, const char *data, DataHeadTlvPacketHead *head, uint32_t *headSize); 121 int32_t TransProxyNoSubPacketTlvProc( 122 int32_t channelId, const char *data, uint32_t len, DataHeadTlvPacketHead *pktHead, uint32_t newPktHeadSize); 123 int32_t TransProxyProcData(ProxyDataInfo *dataInfo, const DataHeadTlvPacketHead *pktHead, const char *data); 124 125 uint8_t *TransProxyPackD2DData( 126 ProxyDataInfo *dataInfo, uint32_t sliceNum, SessionPktType pktType, uint32_t cnt, uint32_t *dataLen); 127 int32_t TransProxyProcessD2DData(ProxyDataInfo *dataInfo, const PacketD2DHead *dataHead, 128 const char *data, int32_t businessType); 129 int32_t TransProxyDecryptD2DData( 130 int32_t businessType, ProxyDataInfo *dataInfo, const char *sessionKey, const unsigned char *sessionCommonIv); 131 int32_t TransProxyD2DFirstSliceProcess( 132 SliceProcessor *processor, const SliceHead *head, const char *data, uint32_t len, int32_t busineseeTye); 133 int32_t TransProxyPackD2DBytes(ProxyDataInfo *dataInfo, const char *sessionKey, const char *sessionIv, 134 SessionPktType flag); 135 int32_t TransGenerateToBytesRandIv(unsigned char *sessionIv, const uint32_t *nonce); 136 137 #ifdef __cplusplus 138 } 139 #endif /* __cplusplus */ 140 #endif // TRANS_PROXY_PROCESS_DATA_H 141