1 /*
2 * Copyright (c) 2021-2024 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 "softbus_proxychannel_session.h"
17
18 #include <securec.h>
19
20 #include "softbus_adapter_crypto.h"
21 #include "softbus_adapter_mem.h"
22 #include "softbus_adapter_socket.h"
23 #include "softbus_adapter_thread.h"
24 #include "softbus_conn_interface.h"
25 #include "softbus_def.h"
26 #include "softbus_errcode.h"
27 #include "softbus_property.h"
28 #include "softbus_proxychannel_callback.h"
29 #include "softbus_proxychannel_manager.h"
30 #include "softbus_proxychannel_message.h"
31 #include "softbus_proxychannel_transceiver.h"
32 #include "softbus_socket.h"
33 #include "softbus_transmission_interface.h"
34 #include "softbus_utils.h"
35 #include "softbus_datahead_transform.h"
36 #include "trans_log.h"
37 #include "trans_pending_pkt.h"
38
39 #define TIME_OUT 10
40 #define USECTONSEC 1000
41 #define MSG_HEAD_LENGTH (28 + 16 + 16)
42
43 int32_t TransProxyTransDataSendMsg(ProxyChannelInfo *chanInfo, const unsigned char *payLoad,
44 int32_t payLoadLen, ProxyPacketType flag);
45
NotifyClientMsgReceived(const char * pkgName,int32_t pid,int32_t channelId,TransReceiveData * receiveData)46 int32_t NotifyClientMsgReceived(const char *pkgName, int32_t pid, int32_t channelId, TransReceiveData *receiveData)
47 {
48 if (pkgName == NULL) {
49 TRANS_LOGE(TRANS_MSG, "param invalid");
50 return SOFTBUS_INVALID_PARAM;
51 }
52 int32_t ret = TransProxyOnMsgReceived(pkgName, pid, channelId, receiveData);
53 if (ret != SOFTBUS_OK) {
54 TRANS_LOGE(TRANS_MSG, "notify ret=%{public}d", ret);
55 }
56 return ret;
57 }
58
SessionTypeToPacketType(SessionPktType sessionType)59 ProxyPacketType SessionTypeToPacketType(SessionPktType sessionType)
60 {
61 switch (sessionType) {
62 case TRANS_SESSION_BYTES:
63 return PROXY_FLAG_BYTES;
64 case TRANS_SESSION_MESSAGE:
65 return PROXY_FLAG_MESSAGE;
66 case TRANS_SESSION_FILE_FIRST_FRAME:
67 return PROXY_FILE_FIRST_FRAME;
68 case TRANS_SESSION_FILE_ONGOINE_FRAME:
69 return PROXY_FILE_ONGOINE_FRAME;
70 case TRANS_SESSION_FILE_LAST_FRAME:
71 return PROXY_FILE_LAST_FRAME;
72 case TRANS_SESSION_FILE_ONLYONE_FRAME:
73 return PROXY_FILE_ONLYONE_FRAME;
74 case TRANS_SESSION_FILE_ALLFILE_SENT:
75 return PROXY_FILE_ALLFILE_SENT;
76 case TRANS_SESSION_FILE_CRC_CHECK_FRAME:
77 return PROXY_FILE_CRC_CHECK_FRAME;
78 case TRANS_SESSION_FILE_RESULT_FRAME:
79 return PROXY_FILE_RESULT_FRAME;
80 case TRANS_SESSION_FILE_ACK_REQUEST_SENT:
81 return PROXY_FILE_ACK_REQUEST_SENT;
82 case TRANS_SESSION_FILE_ACK_RESPONSE_SENT:
83 return PROXY_FILE_ACK_RESPONSE_SENT;
84 default:
85 return PROXY_FLAG_BYTES;
86 }
87 }
88
ProxyTypeToConnPri(ProxyPacketType proxyType)89 SendPriority ProxyTypeToConnPri(ProxyPacketType proxyType)
90 {
91 switch (proxyType) {
92 case PROXY_FLAG_BYTES:
93 return CONN_MIDDLE;
94 case PROXY_FLAG_MESSAGE:
95 case PROXY_FLAG_ASYNC_MESSAGE:
96 case PROXY_FLAG_ACK:
97 return CONN_HIGH;
98 default:
99 return CONN_DEFAULT;
100 }
101 }
102
TransProxyPostPacketData(int32_t channelId,const unsigned char * data,uint32_t len,ProxyPacketType flags)103 static int32_t TransProxyPostPacketData(int32_t channelId, const unsigned char *data,
104 uint32_t len, ProxyPacketType flags)
105 {
106 if (data == NULL || len == 0) {
107 TRANS_LOGE(TRANS_MSG, "invalid param");
108 return SOFTBUS_INVALID_PARAM;
109 }
110 ProxyChannelInfo *chanInfo = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
111 if (chanInfo == NULL) {
112 TRANS_LOGE(TRANS_MSG, "malloc in channelId=%{public}d", channelId);
113 return SOFTBUS_MALLOC_ERR;
114 }
115 if (TransProxyGetSendMsgChanInfo(channelId, chanInfo) != SOFTBUS_OK) {
116 SoftBusFree(chanInfo);
117 TRANS_LOGE(TRANS_MSG, "can not find proxy channel channelId=%{public}d", channelId);
118 return SOFTBUS_TRANS_PROXY_CHANNEL_NOT_FOUND;
119 }
120 (void)memset_s(chanInfo->appInfo.sessionKey, sizeof(chanInfo->appInfo.sessionKey), 0,
121 sizeof(chanInfo->appInfo.sessionKey));
122 int32_t ret = TransProxyTransDataSendMsg(chanInfo, data, len, flags);
123 if (ret != SOFTBUS_OK) {
124 TRANS_LOGE(TRANS_MSG, "send msg fail, len=%{public}u, flags=%{public}d, ret=%{public}d", len, flags, ret);
125 }
126
127 SoftBusFree(chanInfo);
128 return ret;
129 }
130
TransProxyPostSessionData(int32_t channelId,const unsigned char * data,uint32_t len,SessionPktType flags)131 int32_t TransProxyPostSessionData(int32_t channelId, const unsigned char *data, uint32_t len, SessionPktType flags)
132 {
133 ProxyPacketType type = SessionTypeToPacketType(flags);
134 return TransProxyPostPacketData(channelId, data, len, type);
135 }
136
TransProxyPackAppNormalMsg(const ProxyMessageHead * msg,const char * payLoad,int32_t datalen,int32_t * outlen)137 static char *TransProxyPackAppNormalMsg(const ProxyMessageHead *msg, const char *payLoad,
138 int32_t datalen, int32_t *outlen)
139 {
140 ProxyMessageHead proxyMessageHead;
141 uint32_t connHeadLen = ConnGetHeadSize();
142 uint32_t bufLen = PROXY_CHANNEL_HEAD_LEN + connHeadLen + (uint32_t)datalen;
143
144 char *buf = (char *)SoftBusCalloc(bufLen);
145 if (buf == NULL) {
146 TRANS_LOGE(TRANS_MSG, "buf calloc failed");
147 return NULL;
148 }
149 if (memcpy_s(&proxyMessageHead, sizeof(ProxyMessageHead), msg, sizeof(ProxyMessageHead)) != EOK) {
150 TRANS_LOGE(TRANS_MSG, "memcpy_s message failed.");
151 SoftBusFree(buf);
152 return NULL;
153 }
154 PackProxyMessageHead(&proxyMessageHead);
155 if (memcpy_s(buf + connHeadLen, bufLen - connHeadLen, &proxyMessageHead, sizeof(ProxyMessageHead)) != EOK) {
156 TRANS_LOGE(TRANS_MSG, "memcpy_s buf failed.");
157 SoftBusFree(buf);
158 return NULL;
159 }
160 uint32_t dstLen = bufLen - connHeadLen - sizeof(ProxyMessageHead);
161 if (memcpy_s(buf + connHeadLen + sizeof(ProxyMessageHead), dstLen, payLoad, datalen) != EOK) {
162 TRANS_LOGE(TRANS_MSG, "memcpy_s buf failed.");
163 SoftBusFree(buf);
164 return NULL;
165 }
166 *outlen = (int32_t)bufLen;
167
168 return buf;
169 }
170
TransProxyTransNormalMsg(const ProxyChannelInfo * info,const char * payLoad,int32_t payLoadLen,ProxyPacketType flag)171 static int32_t TransProxyTransNormalMsg(const ProxyChannelInfo *info, const char *payLoad, int32_t payLoadLen,
172 ProxyPacketType flag)
173 {
174 ProxyMessageHead msgHead = { 0 };
175 msgHead.type = (PROXYCHANNEL_MSG_TYPE_NORMAL & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
176 msgHead.myId = info->myId;
177 msgHead.peerId = info->peerId;
178 int32_t bufLen = 0;
179 char *buf = TransProxyPackAppNormalMsg(&msgHead, payLoad, payLoadLen, &bufLen);
180 if (buf == NULL) {
181 TRANS_LOGE(TRANS_MSG, "proxy pack msg error");
182 return SOFTBUS_TRANS_PROXY_PACKMSG_ERR;
183 }
184 int32_t ret = TransProxyTransSendMsg(info->connId, (uint8_t *)buf, (uint32_t)bufLen,
185 ProxyTypeToConnPri(flag), info->appInfo.myData.pid);
186 if (ret == SOFTBUS_CONNECTION_ERR_SENDQUEUE_FULL) {
187 TRANS_LOGE(TRANS_MSG, "proxy send queue full.");
188 return SOFTBUS_CONNECTION_ERR_SENDQUEUE_FULL;
189 }
190 if (ret != SOFTBUS_OK) {
191 TRANS_LOGE(TRANS_MSG, "proxy send msg error");
192 return SOFTBUS_TRANS_PROXY_SENDMSG_ERR;
193 }
194 return SOFTBUS_OK;
195 }
196
TransProxyTransDataSendMsg(ProxyChannelInfo * info,const unsigned char * payLoad,int32_t payLoadLen,ProxyPacketType flag)197 int32_t TransProxyTransDataSendMsg(ProxyChannelInfo *info, const unsigned char *payLoad,
198 int32_t payLoadLen, ProxyPacketType flag)
199 {
200 if (info == NULL || payLoad == NULL) {
201 TRANS_LOGE(TRANS_MSG, "param invalid");
202 return SOFTBUS_INVALID_PARAM;
203 }
204 if ((info->status != PROXY_CHANNEL_STATUS_COMPLETED && info->status != PROXY_CHANNEL_STATUS_KEEPLIVEING)) {
205 TRANS_LOGE(TRANS_MSG, "status is err status=%{public}d", info->status);
206 return SOFTBUS_TRANS_PROXY_CHANNLE_STATUS_INVALID;
207 }
208 if (info->appInfo.appType == APP_TYPE_INNER) {
209 TRANS_LOGE(TRANS_MSG, "err app type Inner");
210 return SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
211 }
212
213 return TransProxyTransNormalMsg(info, (const char *)payLoad, payLoadLen, flag);
214 }
215
TransOnNormalMsgReceived(const char * pkgName,int32_t pid,int32_t channelId,const char * data,uint32_t len)216 int32_t TransOnNormalMsgReceived(const char *pkgName, int32_t pid, int32_t channelId, const char *data, uint32_t len)
217 {
218 if (data == NULL || pkgName == NULL) {
219 TRANS_LOGE(TRANS_MSG, "data or pkgname is null.");
220 return SOFTBUS_INVALID_PARAM;
221 }
222 TransReceiveData receiveData;
223 receiveData.data = (void *)data;
224 receiveData.dataLen = len;
225
226 int32_t ret = NotifyClientMsgReceived(pkgName, pid, channelId, &receiveData);
227 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret,
228 TRANS_MSG, "msg receive err, channelId=%{public}d, len=%{public}u, pid=%{public}d", channelId, len, pid);
229
230 return SOFTBUS_OK;
231 }
232