• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "softbus_proxychannel_control.h"
17 
18 #include <securec.h>
19 #include <string.h>
20 
21 #include "auth_interface.h"
22 #include "auth_apply_key_process.h"
23 #include "cJSON.h"
24 #include "legacy/softbus_hisysevt_transreporter.h"
25 #include "softbus_adapter_crypto.h"
26 #include "softbus_adapter_mem.h"
27 #include "softbus_def.h"
28 #include "softbus_error_code.h"
29 #include "softbus_proxychannel_manager.h"
30 #include "softbus_proxychannel_message.h"
31 #include "softbus_proxychannel_transceiver.h"
32 #include "softbus_utils.h"
33 #include "trans_log.h"
34 #include "trans_event.h"
35 
TransProxySendEncryptInnerMessage(ProxyChannelInfo * info,const char * inData,uint32_t inDataLen,ProxyMessageHead * msgHead,ProxyDataInfo * dataInfo)36 static int32_t TransProxySendEncryptInnerMessage(ProxyChannelInfo *info,
37     const char *inData, uint32_t inDataLen, ProxyMessageHead *msgHead, ProxyDataInfo *dataInfo)
38 {
39     uint32_t outPayLoadLen = inDataLen + OVERHEAD_LEN;
40     char *outPayLoad = (char *)SoftBusCalloc(outPayLoadLen);
41     if (outPayLoad == NULL) {
42         TRANS_LOGE(TRANS_CTRL, "malloc len failed");
43         return SOFTBUS_MALLOC_ERR;
44     }
45 
46     AesGcmCipherKey cipherKey = { 0 };
47     cipherKey.keyLen = SESSION_KEY_LENGTH;
48     if (memcpy_s(cipherKey.key, SESSION_KEY_LENGTH, info->appInfo.sessionKey, SESSION_KEY_LENGTH) != EOK) {
49         TRANS_LOGE(TRANS_CTRL, "memcpy key error.");
50         SoftBusFree(outPayLoad);
51         return SOFTBUS_MEM_ERR;
52     }
53 
54     int32_t ret =
55         SoftBusEncryptData(&cipherKey, (unsigned char *)inData, inDataLen, (unsigned char *)outPayLoad, &outPayLoadLen);
56     (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
57     if (ret != SOFTBUS_OK) {
58         TRANS_LOGE(TRANS_CTRL, "SoftBusEncryptData failed, ret=%{public}d", ret);
59         SoftBusFree(outPayLoad);
60         return SOFTBUS_ENCRYPT_ERR;
61     }
62 
63     dataInfo->inData = (uint8_t *)outPayLoad;
64     dataInfo->inLen = outPayLoadLen;
65     if (TransProxyPackMessage(msgHead, info->authHandle, dataInfo) != SOFTBUS_OK) {
66         TRANS_LOGE(TRANS_CTRL, "pack msg error");
67         SoftBusFree(outPayLoad);
68         return SOFTBUS_TRANS_PROXY_PACKMSG_ERR;
69     }
70     ret = TransProxyTransSendMsg(
71         info->connId, dataInfo->outData, dataInfo->outLen, CONN_HIGH, info->appInfo.myData.pid);
72     if (ret != SOFTBUS_OK) {
73         TRANS_LOGE(TRANS_CTRL, "send encrypt msg failed");
74     }
75     SoftBusFree(outPayLoad);
76     return ret;
77 }
78 
TransProxySendInnerMessage(ProxyChannelInfo * info,const char * payLoad,uint32_t payLoadLen,int32_t priority)79 int32_t TransProxySendInnerMessage(ProxyChannelInfo *info, const char *payLoad, uint32_t payLoadLen, int32_t priority)
80 {
81     if (info == NULL || payLoad == NULL) {
82         TRANS_LOGW(TRANS_CTRL, "invalid param.");
83         return SOFTBUS_INVALID_PARAM;
84     }
85 
86     ProxyDataInfo dataInfo = { 0 };
87     ProxyMessageHead msgHead = { 0 };
88     msgHead.type = (PROXYCHANNEL_MSG_TYPE_NORMAL & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
89     msgHead.cipher = (msgHead.cipher | ENCRYPTED);
90     msgHead.myId = info->myId;
91     msgHead.peerId = info->peerId;
92 
93     if ((info->appInfo.channelCapability & TRANS_CHANNEL_INNER_ENCRYPT) != 0) {
94         int32_t ret = TransProxySendEncryptInnerMessage(info, payLoad, payLoadLen, &msgHead, &dataInfo);
95         if (ret != SOFTBUS_OK) {
96             TRANS_LOGE(TRANS_CTRL, "send encrypt msg failed");
97             return ret;
98         }
99     } else {
100         dataInfo.inData = (uint8_t *)payLoad;
101         dataInfo.inLen = payLoadLen;
102         if (TransProxyPackMessage(&msgHead, info->authHandle, &dataInfo) != SOFTBUS_OK) {
103             TRANS_LOGE(TRANS_CTRL, "pack msg error");
104             return SOFTBUS_TRANS_PROXY_PACKMSG_ERR;
105         }
106         return TransProxyTransSendMsg(info->connId, dataInfo.outData, dataInfo.outLen,
107             priority, info->appInfo.myData.pid);
108     }
109     return SOFTBUS_OK;
110 }
111 
ConvertConnectType2AuthLinkType(ConnectType type)112 static inline AuthLinkType ConvertConnectType2AuthLinkType(ConnectType type)
113 {
114     if (type == CONNECT_TCP) {
115         return AUTH_LINK_TYPE_WIFI;
116     } else if ((type == CONNECT_BLE) || (type == CONNECT_BLE_DIRECT)) {
117         return AUTH_LINK_TYPE_BLE;
118     } else if (type == CONNECT_BR) {
119         return AUTH_LINK_TYPE_BR;
120     } else if ((type == CONNECT_SLE) || (type == CONNECT_SLE_DIRECT)) {
121         return AUTH_LINK_TYPE_SLE;
122     }
123     return AUTH_LINK_TYPE_P2P;
124 }
125 
SetCipherOfHandshakeMsg(ProxyChannelInfo * info,uint8_t * cipher)126 static int32_t SetCipherOfHandshakeMsg(ProxyChannelInfo *info, uint8_t *cipher)
127 {
128     AuthGetLatestIdByUuid(info->appInfo.peerData.deviceId, ConvertConnectType2AuthLinkType(info->type),
129                           false, &info->authHandle);
130     if (info->authHandle.authId == AUTH_INVALID_ID) {
131         TRANS_LOGE(TRANS_CTRL, "get authId for cipher err");
132         return SOFTBUS_TRANS_PROXY_GET_AUTH_ID_FAILED;
133     }
134 
135     int32_t ret = TransProxySetAuthHandleByChanId((int32_t)info->channelId, info->authHandle);
136     if (ret != SOFTBUS_OK) {
137         TRANS_LOGE(TRANS_CTRL, "set authHandle fail, ret=%{public}d", ret);
138         return ret;
139     }
140     AuthConnInfo connInfo;
141     (void)memset_s(&connInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo));
142     ret = AuthGetConnInfo(info->authHandle, &connInfo);
143     if (ret != SOFTBUS_OK) {
144         TRANS_LOGE(TRANS_CTRL, "get auth connInfo fail");
145         return ret;
146     }
147     bool isAuthServer = false;
148     ret = AuthGetServerSide(info->authHandle.authId, &isAuthServer);
149     if (ret != SOFTBUS_OK) {
150         TRANS_LOGE(TRANS_CTRL, "get auth server side fail");
151         return ret;
152     }
153 
154     *cipher |= ENCRYPTED;
155     if (isAuthServer) {
156         *cipher |= AUTH_SERVER_SIDE;
157     }
158     if (connInfo.type == AUTH_LINK_TYPE_BLE) {
159         *cipher |= USE_BLE_CIPHER;
160     }
161     return SOFTBUS_OK;
162 }
163 
TransPagingHandshake(int32_t channelId,uint8_t * authKey,uint32_t keyLen)164 int32_t TransPagingHandshake(int32_t channelId, uint8_t *authKey, uint32_t keyLen)
165 {
166     TRANS_CHECK_AND_RETURN_RET_LOGE(keyLen != 0 && keyLen <= SESSION_KEY_LENGTH && authKey != NULL,
167         SOFTBUS_INVALID_PARAM, TRANS_CTRL, "invalid param");
168     ProxyChannelInfo info = { 0 };
169     int32_t ret = TransProxyGetSendMsgChanInfo(channelId, &info);
170     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "get send msg channel info fail");
171     char *payLoad = NULL;
172     ProxyDataInfo dataInfo = { 0 };
173     PagingProxyMessage msg = { 0 };
174     msg.msgHead.type = (PROXYCHANNEL_MSG_TYPE_PAGING_HANDSHAKE & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
175     msg.msgHead.channelId = info.myId;
176     if (memcpy_s(&msg.authKey, SESSION_KEY_LENGTH, authKey, keyLen) != EOK) {
177         TRANS_LOGE(TRANS_CTRL, "memcpy authKey fail");
178         return SOFTBUS_MEM_ERR;
179     }
180     payLoad = TransPagingPackHandshakeMsg(&info);
181     if (payLoad == NULL) {
182         TRANS_LOGE(TRANS_CTRL, "pack handshake fail");
183         return SOFTBUS_TRANS_PROXY_PACK_HANDSHAKE_ERR;
184     }
185     dataInfo.inData = (uint8_t *)payLoad;
186     dataInfo.inLen = strlen(payLoad) + 1;
187     ret = TransPagingPackMessage(&msg, &dataInfo, &info, true);
188     if (ret != SOFTBUS_OK) {
189         TRANS_LOGE(TRANS_CTRL, "pack handshake head fail");
190         cJSON_free(payLoad);
191         return ret;
192     }
193     cJSON_free(payLoad);
194     dataInfo.inData = NULL;
195     TRANS_LOGI(TRANS_CTRL, "send paging handshake, myChannelId=%{public}d", channelId);
196     ret = TransProxyTransSendMsg(info.connId, dataInfo.outData, dataInfo.outLen,
197         CONN_HIGH, info.appInfo.myData.pid);
198     if (ret != SOFTBUS_OK) {
199         TRANS_LOGE(TRANS_CTRL, "send handshake buf fail");
200         return ret;
201     }
202     info.appInfo.timeStart = GetSoftbusRecordTimeMillis();
203     TransEventExtra extra = {
204         .channelId = channelId,
205         .connectionId = (int32_t)info.connId,
206         .result = EVENT_STAGE_RESULT_OK,
207         .peerUdid = info.appInfo.peerUdid,
208         .callerAccountId = info.appInfo.myData.callerAccountId,
209         .calleeAccountId = info.appInfo.myData.calleeAccountId,
210         .businessFlag = info.appInfo.myData.businessFlag
211     };
212     TRANS_EVENT(EVENT_SCENE_PAGING_CONNECT, EVENT_STAGE_PAGING_SOURCE_HANDSHAKE_START, extra);
213     return SOFTBUS_OK;
214 }
215 
TransProxyHandshake(ProxyChannelInfo * info)216 int32_t TransProxyHandshake(ProxyChannelInfo *info)
217 {
218     TRANS_CHECK_AND_RETURN_RET_LOGE(info != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "invalid param.");
219     char *payLoad = NULL;
220     ProxyDataInfo dataInfo = {0};
221     ProxyMessageHead msgHead = {0};
222     msgHead.type = (PROXYCHANNEL_MSG_TYPE_HANDSHAKE & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
223     msgHead.cipher = CS_MODE;
224     if (info->appInfo.appType != APP_TYPE_AUTH) {
225         if (SetCipherOfHandshakeMsg(info, &msgHead.cipher) != SOFTBUS_OK) {
226             TRANS_LOGE(TRANS_CTRL, "set cipher fail");
227             return SOFTBUS_TRANS_PROXY_SET_CIPHER_FAILED;
228         }
229     }
230     msgHead.myId = info->myId;
231     msgHead.peerId = INVALID_CHANNEL_ID;
232     TRANS_LOGI(TRANS_CTRL, "handshake myChannelId=%{public}d, cipher=0x%{public}02x", msgHead.myId, msgHead.cipher);
233     payLoad = TransProxyPackHandshakeMsg(info);
234     if (payLoad == NULL) {
235         TRANS_LOGE(TRANS_CTRL, "pack handshake fail");
236         return SOFTBUS_TRANS_PROXY_PACK_HANDSHAKE_ERR;
237     }
238     dataInfo.inData = (uint8_t *)payLoad;
239     dataInfo.inLen = strlen(payLoad) + 1;
240     if (TransProxyPackMessage(&msgHead, info->authHandle, &dataInfo) != SOFTBUS_OK) {
241         TRANS_LOGE(TRANS_CTRL, "pack handshake head fail");
242         cJSON_free(payLoad);
243         return SOFTBUS_TRANS_PROXY_PACK_HANDSHAKE_HEAD_ERR;
244     }
245     cJSON_free(payLoad);
246     dataInfo.inData = NULL;
247     int32_t ret = TransProxyTransSendMsg(info->connId, dataInfo.outData, dataInfo.outLen,
248         CONN_HIGH, info->appInfo.myData.pid);
249     if (ret != SOFTBUS_OK) {
250         TRANS_LOGE(TRANS_CTRL, "send handshake buf fail");
251         return ret;
252     }
253     TransEventExtra extra = {
254         .channelId = info->myId,
255         .connectionId = (int32_t)info->connId,
256         .result = EVENT_STAGE_RESULT_OK
257     };
258     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_START, extra);
259     return SOFTBUS_OK;
260 }
261 
TransPagingGetAuthKey(ProxyChannelInfo * chan,PagingProxyMessage * msg)262 static int32_t TransPagingGetAuthKey(ProxyChannelInfo *chan, PagingProxyMessage *msg)
263 {
264     RequestBusinessInfo businessInfo;
265     (void)memset_s(&businessInfo, sizeof(RequestBusinessInfo), 0, sizeof(RequestBusinessInfo));
266     businessInfo.type = BUSINESS_TYPE_D2D;
267     if (ConvertBytesToHexString(businessInfo.udidHash, D2D_UDID_HASH_STR_LEN,
268         (const unsigned char *)chan->appInfo.peerData.shortUdidHash, D2D_SHORT_UDID_HASH_LEN) != SOFTBUS_OK ||
269         ConvertBytesToHexString(businessInfo.accountHash, D2D_ACCOUNT_HASH_STR_LEN,
270         (const unsigned char *)chan->appInfo.peerData.shortAccountHash, D2D_SHORT_ACCOUNT_HASH_LEN) != SOFTBUS_OK) {
271         TRANS_LOGE(TRANS_CTRL, "convert udidHash or account hex string fail");
272         return SOFTBUS_NETWORK_BYTES_TO_HEX_STR_ERR;
273     }
274     uint8_t applyKey[SESSION_KEY_LENGTH] = { 0 };
275     char accountHash[SHA_256_HEX_HASH_LEN] = { 0 };
276     int32_t ret = AuthFindApplyKey(&businessInfo, applyKey, accountHash, SHA_256_HEX_HASH_LEN);
277     if (ret != SOFTBUS_OK) {
278         TRANS_LOGE(TRANS_CTRL, "get auth key fail");
279         return ret;
280     }
281     if (memcpy_s(msg->authKey, SESSION_KEY_LENGTH, applyKey, SESSION_KEY_LENGTH)) {
282         TRANS_LOGE(TRANS_CTRL, "memcpy auth key fail");
283         return SOFTBUS_MEM_ERR;
284     }
285     return SOFTBUS_OK;
286 }
287 
ReportPagingAckHandshakeExtra(const ProxyChannelInfo * chan,int32_t retCode)288 static void ReportPagingAckHandshakeExtra(const ProxyChannelInfo *chan, int32_t retCode)
289 {
290     TransEventExtra extra = {
291         .channelId = chan->channelId,
292         .peerChannelId = chan->peerId,
293         .connectionId = (int32_t)chan->connId,
294         .callerAccountId = chan->appInfo.myData.callerAccountId,
295         .calleeAccountId = chan->appInfo.myData.calleeAccountId,
296         .socketName = chan->appInfo.myData.sessionName,
297         .costTime = GetSoftbusRecordTimeMillis() - chan->appInfo.timeStart,
298         .errcode = retCode
299     };
300     extra.result = (retCode == SOFTBUS_OK) ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED;
301     TRANS_EVENT(EVENT_SCENE_PAGING_CONNECT, EVENT_STAGE_PAGING_SINK_HANDSHAKE_END, extra);
302 }
303 
TransPagingAckHandshake(ProxyChannelInfo * chan,int32_t retCode)304 int32_t TransPagingAckHandshake(ProxyChannelInfo *chan, int32_t retCode)
305 {
306     TRANS_CHECK_AND_RETURN_RET_LOGE(chan != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "invalid param.");
307     char *payLoad = NULL;
308     ProxyDataInfo dataInfo = { 0 };
309     PagingProxyMessage msg = { 0 };
310     msg.msgHead.type = (PROXYCHANNEL_MSG_TYPE_PAGING_HANDSHAKE_ACK & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
311     msg.msgHead.channelId = chan->peerId;
312     if (retCode != SOFTBUS_OK) {
313         TRANS_LOGI(TRANS_CTRL,
314             "send paging handshake error msg errCode=%{public}d, myChannelId=%{public}d, peerChannelId=%{public}d",
315             retCode, chan->myId, chan->peerId);
316         payLoad = TransPagingPackHandshakeErrMsg(retCode, chan->myId);
317     } else {
318         TRANS_LOGI(TRANS_CTRL, "send paging handshake ack msg myChannelId=%{public}d, peerChannelId=%{public}d",
319             chan->myId, chan->peerId);
320         payLoad = TransPagingPackHandshakeAckMsg(chan);
321     }
322     if (payLoad == NULL) {
323         TRANS_LOGE(TRANS_CTRL, "pack handshake ack fail");
324         ReportPagingAckHandshakeExtra(chan, SOFTBUS_TRANS_PROXY_PACKMSG_ERR);
325         return SOFTBUS_TRANS_PROXY_PACKMSG_ERR;
326     }
327     dataInfo.inData = (uint8_t *)payLoad;
328     dataInfo.inLen = strlen(payLoad) + 1;
329     int32_t ret = TransPagingGetAuthKey(chan, &msg);
330     if (ret != SOFTBUS_OK) {
331         TRANS_LOGE(TRANS_CTRL, "get auth key fail");
332         cJSON_free(payLoad);
333         ReportPagingAckHandshakeExtra(chan, ret);
334         return ret;
335     }
336     if (TransPagingPackMessage(&msg, &dataInfo, chan, false) != SOFTBUS_OK) {
337         TRANS_LOGE(TRANS_CTRL, "pack handshake ack head fail");
338         cJSON_free(payLoad);
339         ReportPagingAckHandshakeExtra(chan, SOFTBUS_TRANS_PROXY_PACKMSG_ERR);
340         return SOFTBUS_TRANS_PROXY_PACKMSG_ERR;
341     }
342     cJSON_free(payLoad);
343     ret = TransProxyTransSendMsg(chan->connId, dataInfo.outData, dataInfo.outLen,
344         CONN_HIGH, chan->appInfo.myData.pid);
345     if (ret != SOFTBUS_OK) {
346         TRANS_LOGE(TRANS_CTRL, "send handshake ack buf fail");
347         ReportPagingAckHandshakeExtra(chan, ret);
348         return ret;
349     }
350     ReportPagingAckHandshakeExtra(chan, ret);
351     return SOFTBUS_OK;
352 }
353 
TransProxyAckHandshake(uint32_t connId,ProxyChannelInfo * chan,int32_t retCode)354 int32_t TransProxyAckHandshake(uint32_t connId, ProxyChannelInfo *chan, int32_t retCode)
355 {
356     TRANS_CHECK_AND_RETURN_RET_LOGE(chan != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "invalid param.");
357     char *payLoad = NULL;
358     ProxyDataInfo dataInfo = {0};
359     ProxyMessageHead msgHead = {0};
360     msgHead.type = (PROXYCHANNEL_MSG_TYPE_HANDSHAKE_ACK & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
361     if (chan->appInfo.appType != APP_TYPE_AUTH) {
362         msgHead.cipher = (msgHead.cipher | ENCRYPTED);
363     }
364     msgHead.myId = chan->myId;
365     msgHead.peerId = chan->peerId;
366 
367     if (retCode != SOFTBUS_OK) {
368         TRANS_LOGI(TRANS_CTRL,
369             "send handshake error msg errCode=%{public}d, myChannelId=%{public}d, peerChannelId=%{public}d",
370             retCode, chan->myId, chan->peerId);
371         payLoad = TransProxyPackHandshakeErrMsg(retCode);
372     } else {
373         TRANS_LOGI(TRANS_CTRL, "send handshake ack msg myChannelId=%{public}d, peerChannelId=%{public}d",
374             chan->myId, chan->peerId);
375         payLoad = TransProxyPackHandshakeAckMsg(chan);
376     }
377     if (payLoad == NULL) {
378         TRANS_LOGE(TRANS_CTRL, "pack handshake ack fail");
379         return SOFTBUS_TRANS_PROXY_PACKMSG_ERR;
380     }
381     dataInfo.inData = (uint8_t *)payLoad;
382     dataInfo.inLen = strlen(payLoad) + 1;
383     if (TransProxyPackMessage(&msgHead, chan->authHandle, &dataInfo) != SOFTBUS_OK) {
384         TRANS_LOGE(TRANS_CTRL, "pack handshake ack head fail");
385         cJSON_free(payLoad);
386         return SOFTBUS_TRANS_PROXY_PACKMSG_ERR;
387     }
388     cJSON_free(payLoad);
389     int32_t ret = TransProxyTransSendMsg(connId, dataInfo.outData, dataInfo.outLen,
390         CONN_HIGH, chan->appInfo.myData.pid);
391     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "send handshakeack buf fail");
392     return SOFTBUS_OK;
393 }
394 
TransProxyKeepalive(uint32_t connId,const ProxyChannelInfo * info)395 void TransProxyKeepalive(uint32_t connId, const ProxyChannelInfo *info)
396 {
397     if (info == NULL) {
398         TRANS_LOGW(TRANS_CTRL, "invalid param.");
399         return;
400     }
401 
402     char *payLoad = NULL;
403     ProxyDataInfo dataInfo = {0};
404     ProxyMessageHead msgHead = {0};
405     msgHead.type = (PROXYCHANNEL_MSG_TYPE_KEEPALIVE & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
406     msgHead.myId = info->myId;
407     msgHead.peerId = info->peerId;
408     if (info->appInfo.appType != APP_TYPE_AUTH) {
409         msgHead.cipher = (msgHead.cipher | ENCRYPTED);
410     }
411 
412     payLoad = TransProxyPackIdentity(info->identity);
413     if (payLoad == NULL) {
414         TRANS_LOGE(TRANS_CTRL, "pack keepalive fail");
415         return;
416     }
417     dataInfo.inData = (uint8_t *)payLoad;
418     dataInfo.inLen = strlen(payLoad) + 1;
419     if (TransProxyPackMessage(&msgHead, info->authHandle, &dataInfo) != SOFTBUS_OK) {
420         TRANS_LOGE(TRANS_CTRL, "pack keepalive head fail");
421         cJSON_free(payLoad);
422         return;
423     }
424     cJSON_free(payLoad);
425     if (TransProxyTransSendMsg(connId, dataInfo.outData, dataInfo.outLen,
426         CONN_HIGH, info->appInfo.myData.pid) != SOFTBUS_OK) {
427         TRANS_LOGE(TRANS_CTRL, "send keepalive buf fail");
428         return;
429     }
430 }
431 
TransProxyAckKeepalive(ProxyChannelInfo * info)432 int32_t TransProxyAckKeepalive(ProxyChannelInfo *info)
433 {
434     if (info == NULL) {
435         TRANS_LOGW(TRANS_CTRL, "invalid param.");
436         return SOFTBUS_INVALID_PARAM;
437     }
438     char *payLoad = NULL;
439     ProxyDataInfo dataInfo = {0};
440     ProxyMessageHead msgHead = {0};
441     msgHead.type = (PROXYCHANNEL_MSG_TYPE_KEEPALIVE_ACK & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
442     msgHead.myId = info->myId;
443     msgHead.peerId = info->peerId;
444     if (info->appInfo.appType != APP_TYPE_AUTH) {
445         msgHead.cipher = (msgHead.cipher | ENCRYPTED);
446     }
447 
448     payLoad = TransProxyPackIdentity(info->identity);
449     if (payLoad == NULL) {
450         TRANS_LOGE(TRANS_CTRL, "pack keepalive ack fail");
451         return SOFTBUS_TRANS_PACK_LEEPALIVE_ACK_FAILED;
452     }
453     dataInfo.inData = (uint8_t *)payLoad;
454     dataInfo.inLen = strlen(payLoad) + 1;
455     int32_t ret = TransProxyPackMessage(&msgHead, info->authHandle, &dataInfo);
456     if (ret != SOFTBUS_OK) {
457         TRANS_LOGE(TRANS_CTRL, "pack keepalive ack head fail");
458         cJSON_free(payLoad);
459         return ret;
460     }
461     cJSON_free(payLoad);
462     ret = TransProxyTransSendMsg(info->connId, dataInfo.outData, dataInfo.outLen, CONN_HIGH, info->appInfo.myData.pid);
463     if (ret != SOFTBUS_OK) {
464         TRANS_LOGE(TRANS_CTRL, "send keepalive ack buf fail");
465         return ret;
466     }
467     return SOFTBUS_OK;
468 }
469 
TransPagingReset(ProxyChannelInfo * info)470 int32_t TransPagingReset(ProxyChannelInfo *info)
471 {
472     char *payLoad = NULL;
473     ProxyDataInfo dataInfo = { 0 };
474     PagingProxyMessage msg = { 0 };
475     msg.msgHead.type = (PROXYCHANNEL_MSG_TYPE_PAGING_RESET & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
476     msg.msgHead.channelId = info->peerId;
477     payLoad = TransProxyPagingPackChannelId(info->myId);
478     if (payLoad == NULL) {
479         TRANS_LOGE(TRANS_CTRL, "pack paging reset fail.");
480         return SOFTBUS_TRANS_PACK_LEEPALIVE_ACK_FAILED;
481     }
482     dataInfo.inData = (uint8_t *)payLoad;
483     dataInfo.inLen = strlen(payLoad) + 1;
484     int32_t ret = TransPagingGetAuthKey(info, &msg);
485     if (ret != SOFTBUS_OK) {
486         TRANS_LOGE(TRANS_CTRL, "get auth key fail.");
487         cJSON_free(payLoad);
488         return ret;
489     }
490     ret = TransPagingPackMessage(&msg, &dataInfo, info, false);
491     if (ret != SOFTBUS_OK) {
492         TRANS_LOGE(TRANS_CTRL, "pack reset head fail.");
493         cJSON_free(payLoad);
494         return ret;
495     }
496     cJSON_free(payLoad);
497     ret = TransProxyTransSendMsg(info->connId, dataInfo.outData, dataInfo.outLen, CONN_LOW, info->appInfo.myData.pid);
498     if (ret != SOFTBUS_OK) {
499         TRANS_LOGE(TRANS_CTRL, "send paging reset buf fail.");
500         return ret;
501     }
502     return SOFTBUS_OK;
503 }
504 
TransProxyResetPeer(ProxyChannelInfo * info)505 int32_t TransProxyResetPeer(ProxyChannelInfo *info)
506 {
507     if (info == NULL) {
508         TRANS_LOGW(TRANS_CTRL, "invalid param.");
509         return SOFTBUS_INVALID_PARAM;
510     }
511     TRANS_LOGI(TRANS_CTRL, "send reset msg myChannelId=%{public}d, peerChannelId=%{public}d", info->myId, info->peerId);
512     if (info->isD2D) {
513         return TransPagingReset(info);
514     }
515     char *payLoad = NULL;
516     ProxyDataInfo dataInfo = {0};
517     ProxyMessageHead msgHead = {0};
518     msgHead.type = (PROXYCHANNEL_MSG_TYPE_RESET & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
519     msgHead.myId = info->myId;
520     msgHead.peerId = info->peerId;
521     if (info->appInfo.appType != APP_TYPE_AUTH) {
522         msgHead.cipher = (msgHead.cipher | ENCRYPTED);
523     }
524 
525     payLoad = TransProxyPackIdentity(info->identity);
526     if (payLoad == NULL) {
527         TRANS_LOGE(TRANS_CTRL, "pack reset fail");
528         return SOFTBUS_TRANS_PACK_LEEPALIVE_ACK_FAILED;
529     }
530     dataInfo.inData = (uint8_t *)payLoad;
531     dataInfo.inLen = strlen(payLoad) + 1;
532     int32_t ret = TransProxyPackMessage(&msgHead, info->authHandle, &dataInfo);
533     if (ret != SOFTBUS_OK) {
534         TRANS_LOGE(TRANS_CTRL, "pack reset head fail");
535         cJSON_free(payLoad);
536         return ret;
537     }
538     cJSON_free(payLoad);
539     ret = TransProxyTransSendMsg(info->connId, dataInfo.outData, dataInfo.outLen, CONN_LOW, info->appInfo.myData.pid);
540     TransEventExtra extra = {
541         .socketName = info->appInfo.myData.sessionName,
542         .channelId = info->channelId,
543         .errcode = ret,
544         .result = EVENT_STAGE_RESULT_OK
545     };
546     if (ret != SOFTBUS_OK) {
547         TRANS_LOGE(TRANS_CTRL, "send reset buf fail");
548         extra.result = EVENT_STAGE_RESULT_FAILED;
549         TRANS_EVENT(EVENT_SCENE_TRANS_PROXY_RESET_PEER, EVENT_STAGE_TRANS_COMMON_ONE, extra);
550         return ret;
551     }
552     TRANS_EVENT(EVENT_SCENE_TRANS_PROXY_RESET_PEER, EVENT_STAGE_TRANS_COMMON_ONE, extra);
553     return SOFTBUS_OK;
554 }