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