• 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 #include "softbus_proxychannel_manager.h"
16 
17 #include <securec.h>
18 #include <string.h>
19 
20 #include "access_control.h"
21 #include "auth_interface.h"
22 #include "auth_manager.h"
23 #include "auth_session_fsm.h"
24 #include "bus_center_event.h"
25 #include "bus_center_info_key.h"
26 #include "bus_center_manager.h"
27 #include "common_list.h"
28 #include "data_bus_native.h"
29 #include "lnn_distributed_net_ledger.h"
30 #include "softbus_adapter_crypto.h"
31 #include "softbus_adapter_timer.h"
32 #include "legacy/softbus_adapter_hitrace.h"
33 #include "softbus_adapter_mem.h"
34 #include "softbus_adapter_thread.h"
35 #include "softbus_conn_interface.h"
36 #include "softbus_def.h"
37 #include "softbus_feature_config.h"
38 #include "softbus_proxychannel_callback.h"
39 #include "softbus_proxychannel_control.h"
40 #include "softbus_proxychannel_listener.h"
41 #include "softbus_proxychannel_message.h"
42 #include "softbus_proxychannel_session.h"
43 #include "softbus_proxychannel_transceiver.h"
44 #include "softbus_utils.h"
45 #include "trans_auth_negotiation.h"
46 #include "trans_bind_request_manager.h"
47 #include "trans_channel_common.h"
48 #include "trans_channel_limit.h"
49 #include "trans_channel_manager.h"
50 #include "trans_event.h"
51 #include "trans_log.h"
52 #include "trans_session_manager.h"
53 
54 #define ID_OFFSET (1)
55 
56 #define PROXY_CHANNEL_CONTROL_TIMEOUT  19    // 19s
57 #define PROXY_CHANNEL_BT_IDLE_TIMEOUT  240   // 4min
58 #define PROXY_CHANNEL_IDLE_TIMEOUT     15    // 10800 = 3 hour
59 #define PROXY_CHANNEL_TCP_IDLE_TIMEOUT 43200 // tcp 24 hour
60 #define PROXY_CHANNEL_CLIENT           0
61 #define PROXY_CHANNEL_SERVER           1
62 static SoftBusList *g_proxyChannelList = NULL;
63 
64 typedef struct {
65     int32_t channelType;
66     int32_t businessType;
67     ConfigType configType;
68 } ConfigTypeMap;
69 
GetProxyChannelMgrHead(void)70 SoftBusList *GetProxyChannelMgrHead(void)
71 {
72     return g_proxyChannelList;
73 }
74 
GetProxyChannelLock(void)75 int32_t GetProxyChannelLock(void)
76 {
77     if (g_proxyChannelList == NULL) {
78         return SOFTBUS_NO_INIT;
79     }
80     if (SoftBusMutexLock(&g_proxyChannelList->lock) != SOFTBUS_OK) {
81         TRANS_LOGE(TRANS_CTRL, "lock failed");
82         return SOFTBUS_LOCK_ERR;
83     }
84     return SOFTBUS_OK;
85 }
86 
ReleaseProxyChannelLock(void)87 void ReleaseProxyChannelLock(void)
88 {
89     if (g_proxyChannelList == NULL) {
90         return;
91     }
92     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
93 }
94 
ChanIsEqual(ProxyChannelInfo * a,ProxyChannelInfo * b)95 static bool ChanIsEqual(ProxyChannelInfo *a, ProxyChannelInfo *b)
96 {
97     if ((a->myId == b->myId) &&
98         (a->peerId == b->peerId) &&
99         (strncmp(a->identity, b->identity, sizeof(a->identity)) == 0)) {
100         return true;
101     }
102     return false;
103 }
104 
ResetChanIsEqual(int8_t status,ProxyChannelInfo * a,ProxyChannelInfo * b)105 static bool ResetChanIsEqual(int8_t status, ProxyChannelInfo *a, ProxyChannelInfo *b)
106 {
107     if (status == PROXY_CHANNEL_STATUS_HANDSHAKEING) {
108         if ((a->myId == b->myId) &&
109             (strncmp(a->identity, b->identity, sizeof(a->identity)) == 0)) {
110             return true;
111         }
112     }
113 
114     if ((a->myId == b->myId) &&
115         (a->peerId == b->peerId) &&
116         (strncmp(a->identity, b->identity, sizeof(a->identity)) == 0)) {
117         return true;
118     }
119     return false;
120 }
121 
TransProxyGetAppInfoType(int16_t myId,const char * identity,AppType * appType)122 int32_t TransProxyGetAppInfoType(int16_t myId, const char *identity, AppType *appType)
123 {
124     TRANS_CHECK_AND_RETURN_RET_LOGE(
125         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "fail to lock mutex!");
126     ProxyChannelInfo *item = NULL;
127     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
128         if ((item->myId == myId) && (strcmp(item->identity, identity) == 0)) {
129             *appType = item->appInfo.appType;
130             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
131             return SOFTBUS_OK;
132         }
133     }
134     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
135     return SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
136 }
137 
TransProxyUpdateAckInfo(ProxyChannelInfo * info)138 static int32_t TransProxyUpdateAckInfo(ProxyChannelInfo *info)
139 {
140     TRANS_CHECK_AND_RETURN_RET_LOGE((g_proxyChannelList != NULL && info != NULL), SOFTBUS_INVALID_PARAM, TRANS_CTRL,
141         "g_proxyChannelList or item is null");
142     TRANS_CHECK_AND_RETURN_RET_LOGE(
143         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
144     ProxyChannelInfo *item = NULL;
145     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
146         if ((item->myId == info->myId) && (strncmp(item->identity, info->identity, sizeof(item->identity)) == 0)) {
147             item->peerId = info->peerId;
148             item->status = PROXY_CHANNEL_STATUS_COMPLETED;
149             item->timeout = 0;
150             item->appInfo.encrypt = info->appInfo.encrypt;
151             item->appInfo.algorithm = info->appInfo.algorithm;
152             item->appInfo.crc = info->appInfo.crc;
153             item->appInfo.myData.dataConfig = info->appInfo.myData.dataConfig;
154             item->appInfo.peerHandleId = info->appInfo.peerHandleId;
155             item->appInfo.channelCapability = info->appInfo.channelCapability;
156             if (memcpy_s(&(item->appInfo.peerData), sizeof(item->appInfo.peerData),
157                 &(info->appInfo.peerData), sizeof(info->appInfo.peerData)) != EOK ||
158                 memcpy_s(info, sizeof(ProxyChannelInfo), item, sizeof(ProxyChannelInfo)) != EOK) {
159                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
160                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
161                 return SOFTBUS_MEM_ERR;
162             }
163             SoftbusHitraceStart(SOFTBUS_HITRACE_ID_VALID, (uint64_t)(item->channelId + ID_OFFSET));
164             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
165             return SOFTBUS_OK;
166         }
167     }
168     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
169     return SOFTBUS_TRANS_NODE_NOT_FOUND;
170 }
171 
TransRefreshProxyTimesNative(int32_t channelId)172 int32_t TransRefreshProxyTimesNative(int32_t channelId)
173 {
174     TRANS_CHECK_AND_RETURN_RET_LOGE(
175         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
176     TRANS_CHECK_AND_RETURN_RET_LOGE(
177         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
178 
179     ProxyChannelInfo *item = NULL;
180     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
181         if (item->myId == channelId) {
182             item->timeout = 0;
183             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
184             return SOFTBUS_OK;
185         }
186     }
187     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
188     return SOFTBUS_TRANS_NODE_NOT_FOUND;
189 }
190 
TransProxyAddChanItem(ProxyChannelInfo * chan)191 static int32_t TransProxyAddChanItem(ProxyChannelInfo *chan)
192 {
193     TRANS_CHECK_AND_RETURN_RET_LOGE((g_proxyChannelList != NULL && chan != NULL), SOFTBUS_INVALID_PARAM, TRANS_CTRL,
194         "trans proxy add channel param nullptr!");
195     TRANS_CHECK_AND_RETURN_RET_LOGE(
196         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
197 
198     ListAdd(&(g_proxyChannelList->list), &(chan->node));
199     g_proxyChannelList->cnt++;
200     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
201     return SOFTBUS_OK;
202 }
203 
TransProxySpecialUpdateChanInfo(ProxyChannelInfo * channelInfo)204 int32_t TransProxySpecialUpdateChanInfo(ProxyChannelInfo *channelInfo)
205 {
206     TRANS_CHECK_AND_RETURN_RET_LOGE((g_proxyChannelList != NULL && channelInfo != NULL), SOFTBUS_INVALID_PARAM,
207         TRANS_CTRL, "g_proxyChannelList or channelInfo is NULL!");
208     TRANS_CHECK_AND_RETURN_RET_LOGE(
209         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
210 
211     ProxyChannelInfo *item = NULL;
212     ProxyChannelInfo *nextNode = NULL;
213     LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
214         if (item->channelId == channelInfo->channelId) {
215             if (channelInfo->reqId != -1) {
216                 item->reqId = channelInfo->reqId;
217             }
218             if (channelInfo->isServer != -1) {
219                 item->isServer = channelInfo->isServer;
220             }
221             if (channelInfo->type != CONNECT_TYPE_MAX) {
222                 item->type = channelInfo->type;
223             }
224             if (channelInfo->status != -1) {
225                 item->status = channelInfo->status;
226             }
227             if (channelInfo->status == PROXY_CHANNEL_STATUS_HANDSHAKEING) {
228                 item->connId = channelInfo->connId;
229             }
230             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
231             return SOFTBUS_OK;
232         }
233     }
234     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
235     return SOFTBUS_TRANS_NODE_NOT_FOUND;
236 }
237 
TransProxyGetChanByChanId(int32_t chanId,ProxyChannelInfo * chan)238 int32_t TransProxyGetChanByChanId(int32_t chanId, ProxyChannelInfo *chan)
239 {
240     TRANS_CHECK_AND_RETURN_RET_LOGE((g_proxyChannelList != NULL && chan != NULL), SOFTBUS_INVALID_PARAM, TRANS_CTRL,
241         "trans proxy get channel param nullptr!");
242     TRANS_CHECK_AND_RETURN_RET_LOGE(
243         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
244 
245     ProxyChannelInfo *item = NULL;
246     ProxyChannelInfo *nextNode = NULL;
247 
248     LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
249         if (item->channelId == chanId) {
250             if (memcpy_s(chan, sizeof(ProxyChannelInfo), item, sizeof(ProxyChannelInfo)) != EOK) {
251                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
252                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
253                 return SOFTBUS_MEM_ERR;
254             }
255             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
256             return SOFTBUS_OK;
257         }
258     }
259     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
260     TRANS_LOGE(TRANS_CTRL, "proxy channel not found by chanId. chanId=%{public}d", chanId);
261     return SOFTBUS_TRANS_NODE_NOT_FOUND;
262 }
263 
TransProxyGetChanByReqId(int32_t reqId,ProxyChannelInfo * chan)264 int32_t TransProxyGetChanByReqId(int32_t reqId, ProxyChannelInfo *chan)
265 {
266     ProxyChannelInfo *item = NULL;
267     TRANS_CHECK_AND_RETURN_RET_LOGE(
268         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
269     TRANS_CHECK_AND_RETURN_RET_LOGE(
270         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
271 
272     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
273         if (item->reqId == reqId && item->status == PROXY_CHANNEL_STATUS_PYH_CONNECTING) {
274             *chan = *item;
275             break;
276         }
277     }
278     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
279     return SOFTBUS_OK;
280 }
281 
TransProxyDelChanByReqId(int32_t reqId,int32_t errCode)282 void TransProxyDelChanByReqId(int32_t reqId, int32_t errCode)
283 {
284     ProxyChannelInfo *item = NULL;
285     ProxyChannelInfo *nextNode = NULL;
286 
287     TRANS_CHECK_AND_RETURN_LOGE(
288         g_proxyChannelList != NULL, TRANS_CTRL, "g_proxyChannelList is null");
289     TRANS_CHECK_AND_RETURN_LOGE(
290         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, TRANS_CTRL, "lock mutex fail!");
291 
292     LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
293         if ((item->reqId == reqId) &&
294             (item->status == PROXY_CHANNEL_STATUS_PYH_CONNECTING)) {
295             ReleaseProxyChannelId(item->channelId);
296             ListDelete(&(item->node));
297             g_proxyChannelList->cnt--;
298             TRANS_LOGI(TRANS_CTRL, "del channelId by reqId. channelId=%{public}d", item->channelId);
299             SoftBusFree((void *)item->appInfo.fastTransData);
300             item->appInfo.fastTransData = NULL;
301             TransProxyPostOpenFailMsgToLoop(item, errCode);
302         }
303     }
304     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
305     return;
306 }
307 
TransProxyDelChanByChanId(int32_t chanlId)308 void TransProxyDelChanByChanId(int32_t chanlId)
309 {
310     ProxyChannelInfo *item = NULL;
311     ProxyChannelInfo *nextNode = NULL;
312 
313     TRANS_CHECK_AND_RETURN_LOGE(
314         g_proxyChannelList != NULL, TRANS_CTRL, "g_proxyChannelList is null");
315     TRANS_CHECK_AND_RETURN_LOGE(
316         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, TRANS_CTRL, "lock mutex fail!");
317 
318     LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
319         if (item->channelId == chanlId) {
320             ReleaseProxyChannelId(item->channelId);
321             ListDelete(&(item->node));
322             if (item->appInfo.fastTransData != NULL) {
323                 SoftBusFree((void *)item->appInfo.fastTransData);
324             }
325             (void)memset_s(item->appInfo.sessionKey, sizeof(item->appInfo.sessionKey), 0,
326                 sizeof(item->appInfo.sessionKey));
327             SoftBusFree(item);
328             g_proxyChannelList->cnt--;
329             break;
330         }
331     }
332     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
333     TRANS_LOGE(TRANS_CTRL, "del channelId by chanId! channelId=%{public}d", chanlId);
334     return;
335 }
336 
TransProxyChanProcessByReqId(int32_t reqId,uint32_t connId,int32_t errCode)337 void TransProxyChanProcessByReqId(int32_t reqId, uint32_t connId, int32_t errCode)
338 {
339     ProxyChannelInfo *item = NULL;
340     TRANS_CHECK_AND_RETURN_LOGE(
341         g_proxyChannelList != NULL, TRANS_CTRL, "g_proxyChannelList is null");
342     TRANS_CHECK_AND_RETURN_LOGE(
343         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, TRANS_CTRL, "lock mutex fail!");
344 
345     bool isUsing = false;
346     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
347         if (item->reqId == reqId && item->status == PROXY_CHANNEL_STATUS_PYH_CONNECTING) {
348             item->status = PROXY_CHANNEL_STATUS_HANDSHAKEING;
349             item->connId = connId;
350             isUsing = true;
351             TransAddConnRefByConnId(connId, (bool)item->isServer);
352             TransProxyPostHandshakeMsgToLoop(item->channelId);
353         }
354     }
355 
356     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
357     if (!isUsing && errCode != SOFTBUS_TRANS_SESSION_INFO_NOT_FOUND) {
358         TRANS_LOGW(TRANS_CTRL, "logical channel is already closed, connId=%{public}u", connId);
359         TransProxyCloseConnChannel(connId, false);
360     }
361 }
362 
TransProxyCloseProxyOtherRes(int32_t channelId,const ProxyChannelInfo * info)363 static void TransProxyCloseProxyOtherRes(int32_t channelId, const ProxyChannelInfo *info)
364 {
365     uint32_t connId = info->connId;
366     bool isServer = (bool)info->isServer;
367     ProxyChannelInfo *disChanInfo = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
368     if (disChanInfo != NULL) {
369         if (memcpy_s(disChanInfo, sizeof(ProxyChannelInfo), info, sizeof(ProxyChannelInfo)) != EOK) {
370             SoftBusFree(disChanInfo);
371             SoftBusFree((void *)info);
372             TRANS_LOGE(TRANS_SVC, "memcpy info to disChanInfo failed");
373             return;
374         }
375     }
376     TransProxyPostResetPeerMsgToLoop(info);
377     TransProxyPostDisConnectMsgToLoop(connId, isServer, disChanInfo);
378 }
379 
TransProxyReleaseChannelList(ListNode * proxyChannelList,int32_t errCode)380 static void TransProxyReleaseChannelList(ListNode *proxyChannelList, int32_t errCode)
381 {
382     TRANS_CHECK_AND_RETURN_LOGE(!IsListEmpty(proxyChannelList), TRANS_CTRL, "proxyChannelList is empty");
383 
384     ProxyChannelInfo *removeNode = NULL;
385     ProxyChannelInfo *nextNode = NULL;
386     LIST_FOR_EACH_ENTRY_SAFE(removeNode, nextNode, proxyChannelList, ProxyChannelInfo, node) {
387         ListDelete(&(removeNode->node));
388         if (removeNode->status == PROXY_CHANNEL_STATUS_HANDSHAKEING ||
389             removeNode->status == PROXY_CHANNEL_STATUS_PYH_CONNECTING) {
390             TransProxyOpenProxyChannelFail(removeNode->channelId, &(removeNode->appInfo), errCode);
391         } else {
392             OnProxyChannelClosed(removeNode->channelId, &(removeNode->appInfo));
393         }
394         if (removeNode->appInfo.fastTransData != NULL) {
395             SoftBusFree((void *)removeNode->appInfo.fastTransData);
396         }
397         SoftBusFree(removeNode);
398     }
399 }
400 
TransProxyDelByConnId(uint32_t connId)401 void TransProxyDelByConnId(uint32_t connId)
402 {
403     ProxyChannelInfo *removeNode = NULL;
404     ProxyChannelInfo *nextNode = NULL;
405     ListNode proxyChannelList;
406 
407     TRANS_CHECK_AND_RETURN_LOGE(g_proxyChannelList != NULL, TRANS_CTRL, "g_proxyChannelList is null");
408     TRANS_CHECK_AND_RETURN_LOGE(
409         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, TRANS_CTRL, "lock mutex fail!");
410 
411     ListInit(&proxyChannelList);
412     LIST_FOR_EACH_ENTRY_SAFE(removeNode, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
413         if (removeNode->connId == connId) {
414             ReleaseProxyChannelId(removeNode->channelId);
415             ListDelete(&(removeNode->node));
416             g_proxyChannelList->cnt--;
417             ListAdd(&proxyChannelList, &removeNode->node);
418             TRANS_LOGI(TRANS_CTRL, "trans proxy del channel by connId=%{public}d", connId);
419         }
420     }
421     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
422     TransProxyReleaseChannelList(&proxyChannelList, SOFTBUS_TRANS_PROXY_DISCONNECTED);
423 }
424 
TransProxyDelByChannelId(int32_t channelId,ProxyChannelInfo * channelInfo)425 static int32_t TransProxyDelByChannelId(int32_t channelId, ProxyChannelInfo *channelInfo)
426 {
427     ProxyChannelInfo *removeNode = NULL;
428     ProxyChannelInfo *nextNode = NULL;
429 
430     TRANS_CHECK_AND_RETURN_RET_LOGE(
431         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
432     TRANS_CHECK_AND_RETURN_RET_LOGE(
433         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
434 
435     LIST_FOR_EACH_ENTRY_SAFE(removeNode, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
436         if (removeNode->channelId == channelId) {
437             if (channelInfo != NULL) {
438                 (void)memcpy_s(channelInfo, sizeof(ProxyChannelInfo), removeNode, sizeof(ProxyChannelInfo));
439             }
440             ReleaseProxyChannelId(removeNode->channelId);
441             if (removeNode->appInfo.fastTransData != NULL) {
442                 SoftBusFree((void *)removeNode->appInfo.fastTransData);
443             }
444             ListDelete(&(removeNode->node));
445             SoftBusFree(removeNode);
446             g_proxyChannelList->cnt--;
447             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
448             TRANS_LOGI(TRANS_CTRL, "trans proxy del channel by channelId=%{public}d", channelId);
449             return SOFTBUS_OK;
450         }
451     }
452     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
453     return SOFTBUS_TRANS_NODE_NOT_FOUND;
454 }
455 
TransProxyResetChan(ProxyChannelInfo * chanInfo)456 static int32_t TransProxyResetChan(ProxyChannelInfo *chanInfo)
457 {
458     ProxyChannelInfo *removeNode = NULL;
459     ProxyChannelInfo *nextNode = NULL;
460 
461     TRANS_CHECK_AND_RETURN_RET_LOGE(
462         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
463     TRANS_CHECK_AND_RETURN_RET_LOGE(
464         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
465 
466     LIST_FOR_EACH_ENTRY_SAFE(removeNode, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
467         if (ResetChanIsEqual(removeNode->status, removeNode, chanInfo)) {
468             if (memcpy_s(chanInfo, sizeof(ProxyChannelInfo), removeNode, sizeof(ProxyChannelInfo)) != EOK) {
469                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
470                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
471                 return SOFTBUS_MEM_ERR;
472             }
473             ReleaseProxyChannelId(removeNode->channelId);
474             if (removeNode->appInfo.fastTransData != NULL) {
475                 SoftBusFree((void *)removeNode->appInfo.fastTransData);
476             }
477             ListDelete(&(removeNode->node));
478             SoftBusFree(removeNode);
479             g_proxyChannelList->cnt--;
480             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
481             TRANS_LOGI(TRANS_CTRL, "trans proxy reset channelId=%{public}d", chanInfo->channelId);
482             return SOFTBUS_OK;
483         }
484     }
485     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
486 
487     return SOFTBUS_TRANS_NODE_NOT_FOUND;
488 }
489 
TransProxyGetRecvMsgChanInfo(int16_t myId,int16_t peerId,ProxyChannelInfo * chanInfo)490 static int32_t TransProxyGetRecvMsgChanInfo(int16_t myId, int16_t peerId, ProxyChannelInfo *chanInfo)
491 {
492     ProxyChannelInfo *item = NULL;
493 
494     TRANS_CHECK_AND_RETURN_RET_LOGE(
495         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
496     TRANS_CHECK_AND_RETURN_RET_LOGE(
497         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
498 
499     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
500         if ((item->myId == myId) && (item->peerId == peerId)) {
501             if (item->status == PROXY_CHANNEL_STATUS_COMPLETED) {
502                 item->timeout = 0;
503             }
504             if (memcpy_s(chanInfo, sizeof(ProxyChannelInfo), item, sizeof(ProxyChannelInfo)) != EOK) {
505                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
506                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
507                 return SOFTBUS_MEM_ERR;
508             }
509             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
510             return SOFTBUS_OK;
511         }
512     }
513     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
514     return SOFTBUS_TRANS_NODE_NOT_FOUND;
515 }
516 
TransProxyKeepAliveChan(ProxyChannelInfo * chanInfo)517 static int32_t TransProxyKeepAliveChan(ProxyChannelInfo *chanInfo)
518 {
519     ProxyChannelInfo *item = NULL;
520 
521     TRANS_CHECK_AND_RETURN_RET_LOGE(
522         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
523     TRANS_CHECK_AND_RETURN_RET_LOGE(
524         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
525 
526     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
527         if (ChanIsEqual(item, chanInfo)) {
528             if (item->status == PROXY_CHANNEL_STATUS_KEEPLIVEING || item->status == PROXY_CHANNEL_STATUS_COMPLETED) {
529                 item->timeout = 0;
530                 item->status = PROXY_CHANNEL_STATUS_COMPLETED;
531             }
532             if (memcpy_s(chanInfo, sizeof(ProxyChannelInfo), item, sizeof(ProxyChannelInfo)) != EOK) {
533                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
534                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
535                 return SOFTBUS_MEM_ERR;
536             }
537             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
538             return SOFTBUS_OK;
539         }
540     }
541     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
542     return SOFTBUS_TRANS_NODE_NOT_FOUND;
543 }
544 
TransProxyGetSendMsgChanInfo(int32_t channelId,ProxyChannelInfo * chanInfo)545 int32_t TransProxyGetSendMsgChanInfo(int32_t channelId, ProxyChannelInfo *chanInfo)
546 {
547     ProxyChannelInfo *item = NULL;
548 
549     TRANS_CHECK_AND_RETURN_RET_LOGE(
550         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
551     TRANS_CHECK_AND_RETURN_RET_LOGE(
552         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
553 
554     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
555         if (item->channelId == channelId) {
556             if (item->status == PROXY_CHANNEL_STATUS_COMPLETED) {
557                 item->timeout = 0;
558             }
559             if (memcpy_s(chanInfo, sizeof(ProxyChannelInfo), item, sizeof(ProxyChannelInfo)) != EOK) {
560                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
561                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
562                 return SOFTBUS_MEM_ERR;
563             }
564             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
565             return SOFTBUS_OK;
566         }
567     }
568     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
569     return SOFTBUS_TRANS_NODE_NOT_FOUND;
570 }
571 
TransProxyGetNewChanSeq(int32_t channelId)572 int32_t TransProxyGetNewChanSeq(int32_t channelId)
573 {
574     ProxyChannelInfo *item = NULL;
575     int32_t seq = 0;
576 
577     TRANS_CHECK_AND_RETURN_RET_LOGE(
578         g_proxyChannelList != NULL, seq, TRANS_CTRL, "g_proxyChannelList is null");
579     TRANS_CHECK_AND_RETURN_RET_LOGE(
580         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, seq, TRANS_CTRL, "lock mutex fail!");
581 
582     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
583         if (item->channelId == channelId) {
584             seq = item->seq;
585             item->seq++;
586             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
587             return seq;
588         }
589     }
590     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
591     return seq;
592 }
593 
TransProxyGetAuthId(int32_t channelId,AuthHandle * authHandle)594 int32_t TransProxyGetAuthId(int32_t channelId, AuthHandle *authHandle)
595 {
596     TRANS_CHECK_AND_RETURN_RET_LOGE(
597         authHandle != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "authHandle is null");
598     ProxyChannelInfo *item = NULL;
599     TRANS_CHECK_AND_RETURN_RET_LOGE(
600         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
601     TRANS_CHECK_AND_RETURN_RET_LOGE(
602         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
603     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
604         if (item->channelId == channelId) {
605             *authHandle = item->authHandle;
606             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
607             return SOFTBUS_OK;
608         }
609     }
610     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
611     return SOFTBUS_TRANS_NODE_NOT_FOUND;
612 }
613 
TransProxyGetSessionKeyByChanId(int32_t channelId,char * sessionKey,uint32_t sessionKeySize)614 int32_t TransProxyGetSessionKeyByChanId(int32_t channelId, char *sessionKey, uint32_t sessionKeySize)
615 {
616     TRANS_CHECK_AND_RETURN_RET_LOGE(
617         sessionKey != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "sessionKey is null");
618     ProxyChannelInfo *item = NULL;
619     TRANS_CHECK_AND_RETURN_RET_LOGE(
620         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
621     TRANS_CHECK_AND_RETURN_RET_LOGE(
622         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
623     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
624         if (item->channelId == channelId) {
625             if (item->status == PROXY_CHANNEL_STATUS_COMPLETED) {
626                 item->timeout = 0;
627             }
628             if (memcpy_s(sessionKey, sessionKeySize, item->appInfo.sessionKey,
629                 sizeof(item->appInfo.sessionKey)) != EOK) {
630                 TRANS_LOGE(TRANS_CTRL, "memcpy_s fail!");
631                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
632                 return SOFTBUS_MEM_ERR;
633             }
634             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
635             return SOFTBUS_OK;
636         }
637     }
638     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
639     TRANS_LOGE(TRANS_CTRL, "not found ChannelInfo by channelId=%{public}d", channelId);
640     return SOFTBUS_TRANS_SESSION_INFO_NOT_FOUND;
641 }
642 
TransProxyProcessErrMsg(ProxyChannelInfo * info,int32_t errCode)643 static inline void TransProxyProcessErrMsg(ProxyChannelInfo *info, int32_t errCode)
644 {
645     TRANS_LOGW(TRANS_CTRL, "TransProxyProcessErrMsg errCode=%{public}d", errCode);
646     TRANS_CHECK_AND_RETURN_LOGE(
647         TransProxyGetChanByChanId(info->myId, info) == SOFTBUS_OK, TRANS_CTRL, "TransProxyGetChanByChanId fail");
648     if ((info->appInfo.appType == APP_TYPE_NORMAL) || (info->appInfo.appType == APP_TYPE_AUTH)) {
649         TransProxyCloseProxyChannel(info->channelId);
650         (void)TransProxyOpenProxyChannelFail(info->channelId, &(info->appInfo), errCode);
651     }
652 }
653 
TransProxyGetAppInfo(int16_t myId,AppInfo * appInfo)654 static int32_t TransProxyGetAppInfo(int16_t myId, AppInfo *appInfo)
655 {
656     ProxyChannelInfo *item = NULL;
657 
658     TRANS_CHECK_AND_RETURN_RET_LOGE(
659         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
660     TRANS_CHECK_AND_RETURN_RET_LOGE(
661         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
662 
663     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
664         if (item->myId == myId) {
665             if (memcpy_s(appInfo, sizeof(AppInfo), &(item->appInfo), sizeof(item->appInfo)) != EOK) {
666                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
667                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
668                 return SOFTBUS_MEM_ERR;
669             }
670             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
671             return SOFTBUS_OK;
672         }
673     }
674     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
675     return SOFTBUS_TRANS_NODE_NOT_FOUND;
676 }
677 
TransProxyGetReqIdAndStatus(int32_t myId,int32_t * reqId,int8_t * status)678 static int32_t TransProxyGetReqIdAndStatus(int32_t myId, int32_t *reqId, int8_t *status)
679 {
680     TRANS_CHECK_AND_RETURN_RET_LOGE(
681         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "proxy channel list not init");
682 
683     int32_t ret = SoftBusMutexLock(&g_proxyChannelList->lock);
684     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
685     ProxyChannelInfo *item = NULL;
686     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
687         if (item->myId == myId) {
688             *reqId = item->reqId;
689             *status = item->status;
690             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
691             return SOFTBUS_OK;
692         }
693     }
694     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
695     TRANS_LOGE(TRANS_CTRL, "not found proxyChannelInfo by channelId=%{public}d", myId);
696     return SOFTBUS_TRANS_PROXY_CHANNEL_NOT_FOUND;
697 }
698 
699 static const ConfigTypeMap g_configTypeMap[] = {
700     { CHANNEL_TYPE_AUTH,  BUSINESS_TYPE_BYTE,    SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH   },
701     { CHANNEL_TYPE_AUTH,  BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH },
702     { CHANNEL_TYPE_PROXY, BUSINESS_TYPE_BYTE,    SOFTBUS_INT_MAX_BYTES_NEW_LENGTH    },
703     { CHANNEL_TYPE_PROXY, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_MAX_MESSAGE_NEW_LENGTH  },
704 };
705 
FindConfigType(int32_t channelType,int32_t businessType)706 static int32_t FindConfigType(int32_t channelType, int32_t businessType)
707 {
708     uint32_t size = (uint32_t)sizeof(g_configTypeMap) / sizeof(ConfigTypeMap);
709     for (uint32_t i = 0; i < size; i++) {
710         if ((g_configTypeMap[i].channelType == channelType) && (g_configTypeMap[i].businessType == businessType)) {
711             return g_configTypeMap[i].configType;
712         }
713     }
714     return SOFTBUS_CONFIG_TYPE_MAX;
715 }
716 
TransGetLocalConfig(int32_t channelType,int32_t businessType,uint32_t * len)717 static int32_t TransGetLocalConfig(int32_t channelType, int32_t businessType, uint32_t *len)
718 {
719     ConfigType configType = (ConfigType)FindConfigType(channelType, businessType);
720     if (configType == SOFTBUS_CONFIG_TYPE_MAX) {
721         TRANS_LOGE(TRANS_CTRL, "Invalid channelType=%{public}d, businessType=%{public}d", channelType, businessType);
722         return SOFTBUS_INVALID_PARAM;
723     }
724     uint32_t maxLen;
725     if (SoftbusGetConfig(configType, (unsigned char *)&maxLen, sizeof(maxLen)) != SOFTBUS_OK) {
726         TRANS_LOGE(TRANS_CTRL, "get fail configType=%{public}d", configType);
727         return SOFTBUS_GET_CONFIG_VAL_ERR;
728     }
729     *len = maxLen;
730     TRANS_LOGI(TRANS_CTRL, "get local config len=%{public}u", *len);
731     return SOFTBUS_OK;
732 }
733 
TransProxyProcessDataConfig(AppInfo * appInfo)734 static int32_t TransProxyProcessDataConfig(AppInfo *appInfo)
735 {
736     if (appInfo == NULL) {
737         TRANS_LOGE(TRANS_CTRL, "appInfo is null");
738         return SOFTBUS_INVALID_PARAM;
739     }
740     if (appInfo->businessType != BUSINESS_TYPE_MESSAGE && appInfo->businessType != BUSINESS_TYPE_BYTE) {
741         TRANS_LOGE(TRANS_CTRL, "invalid businessType=%{public}d", appInfo->businessType);
742         return SOFTBUS_OK;
743     }
744     if (appInfo->peerData.dataConfig != 0) {
745         appInfo->myData.dataConfig = MIN(appInfo->myData.dataConfig, appInfo->peerData.dataConfig);
746         TRANS_LOGI(TRANS_CTRL, "process dataConfig succ. dataConfig=%{public}u", appInfo->myData.dataConfig);
747         return SOFTBUS_OK;
748     }
749     ConfigType configType = appInfo->businessType == BUSINESS_TYPE_BYTE ? SOFTBUS_INT_PROXY_MAX_BYTES_LENGTH :
750                                                                           SOFTBUS_INT_PROXY_MAX_MESSAGE_LENGTH;
751     if (SoftbusGetConfig(configType, (unsigned char *)&appInfo->myData.dataConfig,
752         sizeof(appInfo->myData.dataConfig)) != SOFTBUS_OK) {
753         TRANS_LOGE(TRANS_CTRL, "get config failed, configType=%{public}d", configType);
754         return SOFTBUS_GET_CONFIG_VAL_ERR;
755     }
756     TRANS_LOGI(TRANS_CTRL, "process dataConfig=%{public}d", appInfo->myData.dataConfig);
757     return SOFTBUS_OK;
758 }
759 
TransProxyReportAuditEvent(ProxyChannelInfo * info,SoftbusAuditType auditType,int32_t errCode)760 static void TransProxyReportAuditEvent(ProxyChannelInfo *info, SoftbusAuditType auditType, int32_t errCode)
761 {
762     TransAuditExtra extra = {
763         .hostPkg = NULL,
764         .localIp = NULL,
765         .localPort = NULL,
766         .localDevId = NULL,
767         .localSessName = NULL,
768         .peerIp = NULL,
769         .peerPort = NULL,
770         .peerDevId = NULL,
771         .peerSessName = NULL,
772         .result = TRANS_AUDIT_DISCONTINUE,
773         .errcode = errCode,
774         .auditType = auditType,
775     };
776     if (info != NULL) {
777         extra.localChannelId = info->myId;
778         extra.peerChannelId = info->peerId;
779     }
780     TRANS_AUDIT(AUDIT_SCENE_OPEN_SESSION, extra);
781 }
782 
TransProxyHandshakeUnpackErrMsg(ProxyChannelInfo * info,const ProxyMessage * msg,int32_t * errCode)783 static int32_t TransProxyHandshakeUnpackErrMsg(ProxyChannelInfo *info, const ProxyMessage *msg, int32_t *errCode)
784 {
785     if (errCode == NULL) {
786         TRANS_LOGE(TRANS_CTRL, "errCode is invalid.");
787         return SOFTBUS_INVALID_PARAM;
788     }
789     int32_t ret = TransProxyUnPackHandshakeErrMsg(msg->data, errCode, msg->dateLen);
790     if (ret == SOFTBUS_OK) {
791         TransEventExtra extra = {
792             .socketName = NULL,
793             .peerNetworkId = NULL,
794             .calleePkg = NULL,
795             .callerPkg = NULL,
796             .channelId = info->myId,
797             .peerChannelId = info->peerId,
798             .errcode = *errCode,
799             .result = EVENT_STAGE_RESULT_FAILED
800         };
801         TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_REPLY, extra);
802         TransProxyReportAuditEvent(info, AUDIT_EVENT_MSG_ERROR, *errCode);
803         return SOFTBUS_OK;
804     }
805     return ret;
806 }
807 
TransProxyHandshakeUnpackRightMsg(ProxyChannelInfo * info,const ProxyMessage * msg,int32_t errCode,uint16_t * fastDataSize)808 static int32_t TransProxyHandshakeUnpackRightMsg(
809     ProxyChannelInfo *info, const ProxyMessage *msg, int32_t errCode, uint16_t *fastDataSize)
810 {
811     if (fastDataSize == NULL) {
812         TRANS_LOGE(TRANS_CTRL, "fastDataSize is invalid.");
813         return SOFTBUS_INVALID_PARAM;
814     }
815     int32_t ret = TransProxyUnpackHandshakeAckMsg(msg->data, info, msg->dateLen, fastDataSize);
816     if (ret != SOFTBUS_OK) {
817         TransProxyReportAuditEvent(info, AUDIT_EVENT_PACKETS_ERROR, errCode);
818         TRANS_LOGE(TRANS_CTRL, "UnpackHandshakeAckMsg failed");
819         return ret;
820     }
821     TRANS_LOGI(TRANS_CTRL,
822         "recv Handshake ack myChannelid=%{public}d, peerChannelId=%{public}d, identity=%{public}s, crc=%{public}d",
823         info->myId, info->peerId, info->identity, info->appInfo.crc);
824     return SOFTBUS_OK;
825 }
826 
TransProxyProcessHandshakeAckMsg(const ProxyMessage * msg)827 void TransProxyProcessHandshakeAckMsg(const ProxyMessage *msg)
828 {
829     uint16_t fastDataSize = 0;
830     ProxyChannelInfo info = {
831         .myId = msg->msgHead.myId,
832         .peerId = msg->msgHead.peerId
833     };
834 
835     if (TransProxyGetAppInfo(info.myId, &(info.appInfo)) != SOFTBUS_OK) {
836         TRANS_LOGE(TRANS_CTRL, "failed to get peer data info");
837         return;
838     }
839     int32_t errCode = SOFTBUS_OK;
840     if (TransProxyHandshakeUnpackErrMsg(&info, msg, &errCode) == SOFTBUS_OK) {
841         TransProxyProcessErrMsg(&info, errCode);
842         goto EXIT;
843     }
844     if (TransProxyHandshakeUnpackRightMsg(&info, msg, errCode, &fastDataSize) != SOFTBUS_OK) {
845         goto EXIT;
846     }
847 
848     if (TransProxyProcessDataConfig(&(info.appInfo)) != SOFTBUS_OK) {
849         TRANS_LOGE(TRANS_CTRL, "ProcessDataConfig failed");
850         goto EXIT;
851     }
852 
853     if (TransProxyUpdateAckInfo(&info) != SOFTBUS_OK) {
854         TRANS_LOGE(TRANS_CTRL, "UpdateAckInfo failed");
855         goto EXIT;
856     }
857 
858     info.appInfo.peerData.channelId = msg->msgHead.peerId;
859     if (info.appInfo.fastTransDataSize <= 0 || (fastDataSize > 0 && fastDataSize == info.appInfo.fastTransDataSize)) {
860         (void)OnProxyChannelOpened(info.channelId, &(info.appInfo), PROXY_CHANNEL_CLIENT);
861     } else {
862         uint32_t outLen;
863         char *buf = TransProxyPackFastData(&(info.appInfo), &outLen);
864         if (buf == NULL) {
865             TRANS_LOGE(TRANS_CTRL, "failed to pack bytes.");
866             goto EXIT;
867         }
868         (void)TransSendMsg(info.channelId, CHANNEL_TYPE_PROXY, buf, outLen, info.appInfo.businessType);
869         SoftBusFree(buf);
870         (void)OnProxyChannelOpened(info.channelId, &(info.appInfo), PROXY_CHANNEL_CLIENT);
871     }
872 EXIT:
873     (void)memset_s(info.appInfo.sessionKey, sizeof(info.appInfo.sessionKey), 0, sizeof(info.appInfo.sessionKey));
874     return;
875 }
876 
TransProxyGetLocalInfo(ProxyChannelInfo * chan)877 static int32_t TransProxyGetLocalInfo(ProxyChannelInfo *chan)
878 {
879     bool noNeedGetPkg = (chan->appInfo.appType == APP_TYPE_INNER);
880     if (!noNeedGetPkg) {
881         if (TransProxyGetPkgName(chan->appInfo.myData.sessionName,
882             chan->appInfo.myData.pkgName, sizeof(chan->appInfo.myData.pkgName)) != SOFTBUS_OK) {
883             TRANS_LOGE(TRANS_CTRL, "channelId=%{public}d proc handshake get pkg name fail", chan->channelId);
884             return SOFTBUS_TRANS_PEER_SESSION_NOT_CREATED;
885         }
886 
887         if (TransProxyGetUidAndPidBySessionName(chan->appInfo.myData.sessionName,
888             &chan->appInfo.myData.uid, &chan->appInfo.myData.pid) != SOFTBUS_OK) {
889             TRANS_LOGE(TRANS_CTRL, "channelId=%{public}d proc handshake get uid pid fail", chan->channelId);
890             return SOFTBUS_TRANS_PEER_SESSION_NOT_CREATED;
891         }
892     }
893 
894     InfoKey key = STRING_KEY_UUID;
895     if (chan->appInfo.appType == APP_TYPE_AUTH) {
896         key = STRING_KEY_DEV_UDID;
897     }
898     int32_t ret = LnnGetLocalStrInfo(key, chan->appInfo.myData.deviceId, sizeof(chan->appInfo.myData.deviceId));
899     TRANS_CHECK_AND_RETURN_RET_LOGE(
900         ret == SOFTBUS_OK, ret, TRANS_CTRL, "channelId=%{public}d Handshake get local info fail", chan->channelId);
901     return SOFTBUS_OK;
902 }
903 
CheckAppTypeAndMsgHead(const ProxyMessageHead * msgHead,const AppInfo * appInfo)904 static inline int32_t CheckAppTypeAndMsgHead(const ProxyMessageHead *msgHead, const AppInfo *appInfo)
905 {
906     if (((msgHead->cipher & ENCRYPTED) == 0) && (appInfo->appType != APP_TYPE_AUTH)) {
907         return SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
908     }
909     return SOFTBUS_OK;
910 }
911 
SelectRouteType(ConnectType type,RouteType * routeType)912 static void SelectRouteType(ConnectType type, RouteType *routeType)
913 {
914     if (type == CONNECT_TCP) {
915         *routeType = WIFI_STA;
916     } else if (type == CONNECT_BR) {
917         *routeType = BT_BR;
918     } else if (type == CONNECT_BLE) {
919         *routeType = BT_BLE;
920     } else if (type == CONNECT_BLE_DIRECT) {
921         *routeType = BT_BLE;
922     }
923 }
924 
ConstructProxyChannelInfo(ProxyChannelInfo * chan,const ProxyMessage * msg,int16_t newChanId,const ConnectionInfo * info)925 static void ConstructProxyChannelInfo(
926     ProxyChannelInfo *chan, const ProxyMessage *msg, int16_t newChanId, const ConnectionInfo *info)
927 {
928     // always be client when communicating with WinPC
929     chan->isServer = (msg->msgHead.cipher & CS_MODE) == 0 ? 0 : 1;
930     if (chan->isServer == 0) {
931         chan->deviceTypeIsWinpc = true;
932     }
933     chan->status = PROXY_CHANNEL_STATUS_COMPLETED;
934     chan->connId = msg->connId;
935     chan->myId = newChanId;
936     chan->channelId = newChanId;
937     chan->peerId = msg->msgHead.peerId;
938     chan->authHandle = msg->authHandle;
939     chan->type = info->type;
940     if (chan->type == CONNECT_BLE || chan->type == CONNECT_BLE_DIRECT) {
941         chan->bleProtocolType = info->bleInfo.protocol;
942     }
943 
944     SelectRouteType(info->type, &chan->appInfo.routeType);
945 }
946 
TransProxyFillDataConfig(AppInfo * appInfo)947 static int32_t TransProxyFillDataConfig(AppInfo *appInfo)
948 {
949     if (appInfo == NULL) {
950         TRANS_LOGE(TRANS_CTRL, "appInfo is null");
951         return SOFTBUS_INVALID_PARAM;
952     }
953     if (appInfo->appType == APP_TYPE_AUTH) {
954         appInfo->businessType = BUSINESS_TYPE_BYTE;
955     }
956     if (appInfo->businessType != BUSINESS_TYPE_MESSAGE && appInfo->businessType != BUSINESS_TYPE_BYTE) {
957         TRANS_LOGI(TRANS_CTRL, "invalid businessType=%{public}d", appInfo->businessType);
958         return SOFTBUS_OK;
959     }
960     if (appInfo->peerData.dataConfig != 0) {
961         uint32_t localDataConfig = 0;
962         if (TransGetLocalConfig(CHANNEL_TYPE_PROXY, appInfo->businessType, &localDataConfig) != SOFTBUS_OK) {
963             TRANS_LOGE(TRANS_CTRL, "get local config failed, businessType=%{public}d", appInfo->businessType);
964             return SOFTBUS_GET_CONFIG_VAL_ERR;
965         }
966         appInfo->myData.dataConfig = MIN(localDataConfig, appInfo->peerData.dataConfig);
967         TRANS_LOGI(TRANS_CTRL, "fill dataConfig success. dataConfig=%{public}u", appInfo->myData.dataConfig);
968         return SOFTBUS_OK;
969     }
970     ConfigType configType = appInfo->businessType == BUSINESS_TYPE_BYTE ? SOFTBUS_INT_PROXY_MAX_BYTES_LENGTH :
971                                                                           SOFTBUS_INT_PROXY_MAX_MESSAGE_LENGTH;
972     if (SoftbusGetConfig(configType, (unsigned char *)&appInfo->myData.dataConfig,
973         sizeof(appInfo->myData.dataConfig)) != SOFTBUS_OK) {
974         TRANS_LOGE(TRANS_CTRL, "get config failed, configType=%{public}d", configType);
975         return SOFTBUS_GET_CONFIG_VAL_ERR;
976     }
977     TRANS_LOGD(TRANS_CTRL, "fill dataConfig=%{public}d", appInfo->myData.dataConfig);
978     return SOFTBUS_OK;
979 }
980 
TransProxyFillChannelInfo(const ProxyMessage * msg,ProxyChannelInfo * chan)981 static int32_t TransProxyFillChannelInfo(const ProxyMessage *msg, ProxyChannelInfo *chan)
982 {
983     int32_t ret = TransProxyUnpackHandshakeMsg(msg->data, chan, msg->dateLen);
984     if (ret != SOFTBUS_OK) {
985         TransProxyReportAuditEvent(chan, AUDIT_EVENT_PACKETS_ERROR, ret);
986         TRANS_LOGE(TRANS_CTRL, "UnpackHandshakeMsg fail.");
987         return ret;
988     }
989     if ((chan->appInfo.appType == APP_TYPE_AUTH) &&
990         (!CheckSessionNameValidOnAuthChannel(chan->appInfo.myData.sessionName))) {
991         TRANS_LOGE(TRANS_CTRL, "proxy auth check sessionname valid.");
992         return SOFTBUS_TRANS_AUTH_NOTALLOW_OPENED;
993     }
994 
995     if (CheckAppTypeAndMsgHead(&msg->msgHead, &chan->appInfo) != SOFTBUS_OK) {
996         TRANS_LOGE(TRANS_CTRL, "only auth channel surpport plain text data");
997         return SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
998     }
999 
1000     ConnectionInfo info;
1001     (void)memset_s(&info, sizeof(ConnectionInfo), 0, sizeof(ConnectionInfo));
1002     ret = ConnGetConnectionInfo(msg->connId, &info);
1003     if (ret != SOFTBUS_OK) {
1004         TRANS_LOGE(TRANS_CTRL, "GetConnectionInfo fail. connId=%{public}u", msg->connId);
1005         return ret;
1006     }
1007     ConnectType type;
1008     if (ConnGetTypeByConnectionId(msg->connId, &type) != SOFTBUS_OK) {
1009         return SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT;
1010     }
1011     SelectRouteType(type, &chan->appInfo.routeType);
1012 
1013     int16_t newChanId = (int16_t)(GenerateChannelId(false));
1014     ConstructProxyChannelInfo(chan, msg, newChanId, &info);
1015 
1016     if (chan->appInfo.appType == APP_TYPE_NORMAL && chan->appInfo.callingTokenId != TOKENID_NOT_SET &&
1017         TransCheckServerAccessControl(&chan->appInfo) != SOFTBUS_OK) {
1018         return SOFTBUS_TRANS_CHECK_ACL_FAILED;
1019     }
1020 
1021     if (CheckSecLevelPublic(chan->appInfo.myData.sessionName, chan->appInfo.peerData.sessionName) != SOFTBUS_OK) {
1022         return SOFTBUS_PERMISSION_SERVER_DENIED;
1023     }
1024     ret = TransProxyGetLocalInfo(chan);
1025     if (ret != SOFTBUS_OK) {
1026         return ret;
1027     }
1028 
1029     ret = TransProxyFillDataConfig(&chan->appInfo);
1030     if (ret != SOFTBUS_OK) {
1031         TRANS_LOGE(TRANS_CTRL, "fill dataConfig fail.");
1032         return ret;
1033     }
1034     return SOFTBUS_OK;
1035 }
1036 
TransProxyProcessHandshakeAuthMsg(const ProxyMessage * msg)1037 void TransProxyProcessHandshakeAuthMsg(const ProxyMessage *msg)
1038 {
1039     AppInfo appInfo;
1040     int32_t ret = TransProxyGetAppInfoByChanId(msg->msgHead.myId, &appInfo);
1041     (void)memset_s(appInfo.sessionKey, sizeof(appInfo.sessionKey), 0, sizeof(appInfo.sessionKey));
1042     if (ret != SOFTBUS_OK) {
1043         return;
1044     }
1045     if (((uint32_t)appInfo.transFlag & TRANS_FLAG_HAS_CHANNEL_AUTH) == 0) {
1046         return;
1047     }
1048     int64_t authSeq = appInfo.authSeq;
1049     AuthSessionProcessAuthData(authSeq, (uint8_t *)msg->data, msg->dateLen);
1050 }
1051 
TransProxyFastDataRecv(ProxyChannelInfo * chan)1052 static void TransProxyFastDataRecv(ProxyChannelInfo *chan)
1053 {
1054     TRANS_LOGD(TRANS_CTRL, "begin, fastTransDataSize=%{public}d", chan->appInfo.fastTransDataSize);
1055     TransReceiveData receiveData;
1056     receiveData.data = (void *)chan->appInfo.fastTransData;
1057     if (chan->appInfo.businessType == BUSINESS_TYPE_MESSAGE && chan->appInfo.routeType == WIFI_STA) {
1058         receiveData.dataLen = chan->appInfo.fastTransDataSize + FAST_EXT_MSG_SIZE;
1059     } else {
1060         receiveData.dataLen = chan->appInfo.fastTransDataSize + FAST_EXT_BYTE_SIZE;
1061     }
1062     if (chan->appInfo.businessType == BUSINESS_TYPE_MESSAGE) {
1063         receiveData.dataType = TRANS_SESSION_MESSAGE;
1064     } else {
1065         receiveData.dataType = TRANS_SESSION_BYTES;
1066     }
1067     if (NotifyClientMsgReceived(chan->appInfo.myData.pkgName, chan->appInfo.myData.pid,
1068         chan->channelId, &receiveData) != SOFTBUS_OK) {
1069         TRANS_LOGE(TRANS_CTRL, "TransProxyFastDataRecv err");
1070         chan->appInfo.fastTransDataSize = 0;
1071     }
1072     TRANS_LOGD(TRANS_CTRL, "ok");
1073     return;
1074 }
1075 
ReleaseChannelInfo(ProxyChannelInfo * chan)1076 static void ReleaseChannelInfo(ProxyChannelInfo *chan)
1077 {
1078     if (chan == NULL) {
1079         return;
1080     }
1081     if (chan->appInfo.fastTransData != NULL) {
1082         SoftBusFree((void*)chan->appInfo.fastTransData);
1083     }
1084     SoftBusFree(chan);
1085 }
1086 
FillProxyHandshakeExtra(TransEventExtra * extra,ProxyChannelInfo * chan,char * socketName,NodeInfo * nodeInfo)1087 static void FillProxyHandshakeExtra(
1088     TransEventExtra *extra, ProxyChannelInfo *chan, char *socketName, NodeInfo *nodeInfo)
1089 {
1090     if (strcpy_s(socketName, SESSION_NAME_SIZE_MAX, chan->appInfo.myData.sessionName) != EOK) {
1091         TRANS_LOGW(TRANS_CTRL, "strcpy_s socketName failed");
1092     }
1093     extra->calleePkg = NULL;
1094     extra->callerPkg = NULL;
1095     extra->channelId = chan->myId;
1096     extra->peerChannelId = chan->peerId;
1097     extra->socketName = socketName;
1098     extra->authId = chan->authHandle.authId;
1099     extra->connectionId = (int32_t)chan->connId;
1100     extra->channelType = chan->appInfo.appType == APP_TYPE_AUTH ? CHANNEL_TYPE_AUTH : CHANNEL_TYPE_PROXY;
1101     extra->linkType = chan->type;
1102 
1103     if (chan->appInfo.appType == APP_TYPE_AUTH &&
1104         strcpy_s(nodeInfo->deviceInfo.deviceUdid, UDID_BUF_LEN, chan->appInfo.peerData.deviceId) != EOK) {
1105         extra->peerUdid = nodeInfo->deviceInfo.deviceUdid;
1106     } else if (chan->appInfo.appType != APP_TYPE_AUTH &&
1107         LnnGetRemoteNodeInfoById(chan->appInfo.peerData.deviceId, CATEGORY_UUID, nodeInfo) == SOFTBUS_OK) {
1108         extra->peerUdid = nodeInfo->deviceInfo.deviceUdid;
1109         extra->peerDevVer = nodeInfo->deviceInfo.deviceVersion;
1110     }
1111     if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, nodeInfo->masterUdid, UDID_BUF_LEN) == SOFTBUS_OK) {
1112         extra->localUdid = nodeInfo->masterUdid;
1113     }
1114 }
1115 
TransProxySendHandShakeMsgWhenInner(uint32_t connId,ProxyChannelInfo * chan,int32_t retCode)1116 static int32_t TransProxySendHandShakeMsgWhenInner(uint32_t connId, ProxyChannelInfo *chan, int32_t retCode)
1117 {
1118     if (chan->appInfo.appType != APP_TYPE_INNER) {
1119         return SOFTBUS_OK;
1120     }
1121     if (chan->appInfo.fastTransData != NULL && chan->appInfo.fastTransDataSize > 0) {
1122         TransProxyFastDataRecv(chan);
1123     }
1124     chan->appInfo.myHandleId = 0;
1125     int32_t ret = TransProxyAckHandshake(connId, chan, SOFTBUS_OK);
1126     if (ret != SOFTBUS_OK) {
1127         TRANS_LOGE(
1128             TRANS_CTRL, "AckHandshake fail channelId=%{public}d, connId=%{public}u", chan->channelId, connId);
1129         (void)OnProxyChannelClosed(chan->channelId, &(chan->appInfo));
1130         TransProxyDelChanByChanId(chan->channelId);
1131         return ret;
1132     }
1133     return SOFTBUS_OK;
1134 }
1135 
TransServerProxyChannelOpened(ProxyChannelInfo * chan,TransEventExtra * extra,int32_t proxyChannelId)1136 static int32_t TransServerProxyChannelOpened(ProxyChannelInfo *chan, TransEventExtra *extra, int32_t proxyChannelId)
1137 {
1138     extra->result = EVENT_STAGE_RESULT_OK;
1139     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_HANDSHAKE_START, *extra);
1140     int32_t ret = OnProxyChannelOpened(proxyChannelId, &(chan->appInfo), PROXY_CHANNEL_SERVER);
1141     if (ret != SOFTBUS_OK) {
1142         TRANS_LOGE(TRANS_CTRL, "Trans send on channel opened request fail. ret=%{public}d.", ret);
1143         (void)TransProxyAckHandshake(chan->connId, chan, ret);
1144         TransProxyDelChanByChanId(proxyChannelId);
1145         return ret;
1146     }
1147     ret = TransProxySendHandShakeMsgWhenInner(chan->connId, chan, SOFTBUS_OK);
1148     if (ret != SOFTBUS_OK) {
1149         return ret;
1150     }
1151     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_HANDSHAKE_REPLY, *extra);
1152     return SOFTBUS_OK;
1153 }
1154 
TransProxyProcessHandshakeMsg(const ProxyMessage * msg)1155 void TransProxyProcessHandshakeMsg(const ProxyMessage *msg)
1156 {
1157     TRANS_CHECK_AND_RETURN_LOGE(msg != NULL, TRANS_CTRL, "invalid param");
1158     TRANS_LOGI(TRANS_CTRL, "recv Handshake myChannelId=%{public}d, peerChannelId=%{public}d", msg->msgHead.myId,
1159         msg->msgHead.peerId);
1160     ProxyChannelInfo *chan = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1161     TRANS_CHECK_AND_RETURN_LOGW(!(chan == NULL), TRANS_CTRL, "proxy handshake calloc failed.");
1162     int32_t ret = TransProxyFillChannelInfo(msg, chan);
1163     if ((ret == SOFTBUS_TRANS_PEER_SESSION_NOT_CREATED || ret == SOFTBUS_TRANS_CHECK_ACL_FAILED ||
1164          ret == SOFTBUS_PERMISSION_SERVER_DENIED) && (TransProxyAckHandshake(msg->connId, chan, ret) != SOFTBUS_OK)) {
1165         TRANS_LOGE(TRANS_CTRL, "ErrHandshake fail, connId=%{public}u.", msg->connId);
1166     }
1167     char tmpSocketName[SESSION_NAME_SIZE_MAX] = { 0 };
1168     NodeInfo nodeInfo;
1169     (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
1170     TransEventExtra extra = { 0 };
1171     FillProxyHandshakeExtra(&extra, chan, tmpSocketName, &nodeInfo);
1172     chan->connId = msg->connId;
1173     int32_t proxyChannelId = chan->channelId;
1174     if (ret != SOFTBUS_OK) {
1175         ReleaseProxyChannelId(proxyChannelId);
1176         ReleaseChannelInfo(chan);
1177         goto EXIT_ERR;
1178     }
1179     TransCreateConnByConnId(chan->connId, (bool)chan->isServer);
1180     if ((ret = TransProxyAddChanItem(chan)) != SOFTBUS_OK) {
1181         TRANS_LOGE(TRANS_CTRL, "AddChanItem fail");
1182         ReleaseProxyChannelId(proxyChannelId);
1183         ReleaseChannelInfo(chan);
1184         goto EXIT_ERR;
1185     }
1186     if (chan->appInfo.appType == APP_TYPE_NORMAL) {
1187         ret = CheckCollabRelation(&(chan->appInfo), chan->channelId, CHANNEL_TYPE_PROXY);
1188         if (ret == SOFTBUS_OK) {
1189             TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_HANDSHAKE_REPLY, extra);
1190             return;
1191         } else if (ret != SOFTBUS_TRANS_NOT_NEED_CHECK_RELATION) {
1192             (void)TransProxyAckHandshake(chan->connId, chan, ret);
1193             TransProxyDelChanByChanId(proxyChannelId);
1194             goto EXIT_ERR;
1195         }
1196     }
1197     ret = TransServerProxyChannelOpened(chan, &extra, proxyChannelId);
1198     if (ret != SOFTBUS_OK) {
1199         goto EXIT_ERR;
1200     }
1201     return;
1202 EXIT_ERR:
1203     extra.result = EVENT_STAGE_RESULT_FAILED;
1204     extra.errcode = ret;
1205     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_HANDSHAKE_REPLY, extra);
1206 }
1207 
TransProxyUpdateReplyCnt(int32_t channelId)1208 static int32_t TransProxyUpdateReplyCnt(int32_t channelId)
1209 {
1210     TRANS_CHECK_AND_RETURN_RET_LOGE(
1211         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
1212     TRANS_CHECK_AND_RETURN_RET_LOGE(
1213         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
1214     ProxyChannelInfo *item = NULL;
1215     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
1216         if (item->channelId == channelId) {
1217             item->appInfo.waitOpenReplyCnt = CHANNEL_OPEN_SUCCESS;
1218             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
1219             return SOFTBUS_OK;
1220         }
1221     }
1222     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
1223     TRANS_LOGE(TRANS_CTRL, "proxy channel not found by channelId. channelId=%{public}d", channelId);
1224     return SOFTBUS_TRANS_NODE_NOT_FOUND;
1225 }
1226 
TransDealProxyChannelOpenResult(int32_t channelId,int32_t openResult)1227 int32_t TransDealProxyChannelOpenResult(int32_t channelId, int32_t openResult)
1228 {
1229     ProxyChannelInfo chan;
1230     (void)memset_s(&chan, sizeof(ProxyChannelInfo), 0, sizeof(ProxyChannelInfo));
1231     int32_t ret = TransProxyGetChanByChanId(channelId, &chan);
1232     if (ret != SOFTBUS_OK) {
1233         TRANS_LOGE(TRANS_CTRL, "get proxy channelInfo failed, channelId=%{public}d, ret=%{public}d", channelId, ret);
1234         return ret;
1235     }
1236     ret = TransProxyUpdateReplyCnt(channelId);
1237     if (ret != SOFTBUS_OK) {
1238         TRANS_LOGE(TRANS_CTRL, "update waitOpenReplyCnt failed, channelId=%{public}d, ret=%{public}d", channelId, ret);
1239         return ret;
1240     }
1241     if (openResult != SOFTBUS_OK) {
1242         TRANS_LOGE(TRANS_CTRL, "open proxy channel failed, ret=%{public}d", openResult);
1243         (void)TransProxyAckHandshake(chan.connId, &chan, openResult);
1244         TransProxyDelChanByChanId(channelId);
1245         return SOFTBUS_OK;
1246     }
1247 
1248     if (chan.appInfo.fastTransData != NULL && chan.appInfo.fastTransDataSize > 0) {
1249         TransProxyFastDataRecv(&chan);
1250     }
1251     chan.appInfo.myHandleId = 0;
1252     if ((ret = TransProxyAckHandshake(chan.connId, &chan, SOFTBUS_OK)) != SOFTBUS_OK) {
1253         TRANS_LOGE(
1254             TRANS_CTRL, "AckHandshake fail channelId=%{public}d, connId=%{public}u", channelId, chan.connId);
1255         (void)OnProxyChannelClosed(channelId, &(chan.appInfo));
1256         TransProxyDelChanByChanId(channelId);
1257         return ret;
1258     }
1259     if ((ret = OnProxyChannelBind(channelId, &(chan.appInfo))) != SOFTBUS_OK) {
1260         TRANS_LOGE(TRANS_CTRL, "OnProxyChannelBind fail channelId=%{public}d, connId=%{public}u", channelId,
1261             chan.connId);
1262         TransProxyDelChanByChanId(channelId);
1263         return ret;
1264     }
1265     return SOFTBUS_OK;
1266 }
1267 
TransCheckProxyChannelOpenStatus(int32_t channelId,int32_t * curCount)1268 static int32_t TransCheckProxyChannelOpenStatus(int32_t channelId, int32_t *curCount)
1269 {
1270     TRANS_CHECK_AND_RETURN_RET_LOGE(
1271         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
1272     TRANS_CHECK_AND_RETURN_RET_LOGE(
1273         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
1274 
1275     ProxyChannelInfo *item = NULL;
1276     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
1277         if (item->channelId == channelId) {
1278             if (item->appInfo.waitOpenReplyCnt != CHANNEL_OPEN_SUCCESS) {
1279                 item->appInfo.waitOpenReplyCnt++;
1280             }
1281             *curCount = item->appInfo.waitOpenReplyCnt;
1282             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
1283             return SOFTBUS_OK;
1284         }
1285     }
1286     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
1287     TRANS_LOGE(TRANS_CTRL, "proxy channel not found by channelId. channelId=%{public}d", channelId);
1288     return SOFTBUS_TRANS_NODE_NOT_FOUND;
1289 }
1290 
TransAsyncProxyChannelTask(int32_t channelId)1291 void TransAsyncProxyChannelTask(int32_t channelId)
1292 {
1293     int32_t curCount = 0;
1294     int32_t ret = TransCheckProxyChannelOpenStatus(channelId, &curCount);
1295     if (ret != SOFTBUS_OK) {
1296         TRANS_LOGE(TRANS_CTRL,
1297             "check proxy channel open statue failed, channelId=%{public}d, ret=%{public}d", channelId, ret);
1298         return;
1299     }
1300     if (curCount == CHANNEL_OPEN_SUCCESS) {
1301         TRANS_LOGI(TRANS_CTRL, "Open proxy channel success, channelId=%{public}d", channelId);
1302         return;
1303     }
1304     ProxyChannelInfo chan;
1305     (void)memset_s(&chan, sizeof(ProxyChannelInfo), 0, sizeof(ProxyChannelInfo));
1306     ret = TransProxyGetChanByChanId(channelId, &chan);
1307     if (ret != SOFTBUS_OK) {
1308         TRANS_LOGE(TRANS_CTRL, "get proxy channel info by channelId=%{public}d failed, ret=%{public}d", channelId, ret);
1309         return;
1310     }
1311     if (curCount >= LOOPER_REPLY_CNT_MAX) {
1312         TRANS_LOGE(TRANS_CTRL, "Open proxy channel timeout, channelId=%{public}d", channelId);
1313         (void)TransProxyAckHandshake(chan.connId, &chan, SOFTBUS_TRANS_OPEN_CHANNEL_NEGTIATE_TIMEOUT);
1314         (void)OnProxyChannelClosed(channelId, &(chan.appInfo));
1315         TransProxyDelChanByChanId(channelId);
1316         return;
1317     }
1318     TRANS_LOGI(TRANS_CTRL, "Open proxy channelId=%{public}d not finished, generate new task and waiting", channelId);
1319     uint32_t delayTime = (curCount <= LOOPER_SEPARATE_CNT) ? FAST_INTERVAL_MILLISECOND : SLOW_INTERVAL_MILLISECOND;
1320     TransCheckChannelOpenToLooperDelay(channelId, CHANNEL_TYPE_PROXY, delayTime);
1321 }
1322 
TransGetRemoteDeviceIdByReqId(int32_t requestId,char * peerNetworkId)1323 static int32_t TransGetRemoteDeviceIdByReqId(int32_t requestId, char *peerNetworkId)
1324 {
1325     TRANS_CHECK_AND_RETURN_RET_LOGE(
1326         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "proxy channel list not init");
1327 
1328     int32_t ret = SoftBusMutexLock(&g_proxyChannelList->lock);
1329     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
1330 
1331     ProxyChannelInfo *item = NULL;
1332     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
1333         if (item->reqId == requestId) {
1334             if (memcpy_s(peerNetworkId, DEVICE_ID_SIZE_MAX, item->appInfo.peerNetWorkId, DEVICE_ID_SIZE_MAX) != EOK) {
1335                 TRANS_LOGE(TRANS_CTRL, "memcpy_s peerNetworkId failed");
1336                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
1337                 return SOFTBUS_MEM_ERR;
1338             }
1339             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
1340             return SOFTBUS_OK;
1341         }
1342     }
1343     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
1344     TRANS_LOGE(TRANS_CTRL, "proxy channel not found by requestId=%{public}d", requestId);
1345     return SOFTBUS_TRANS_PROXY_CHANNEL_NOT_FOUND;
1346 }
1347 
TransProxyProcessReNegotiateMsg(const ProxyMessage * msg,const ProxyChannelInfo * info)1348 static int32_t TransProxyProcessReNegotiateMsg(const ProxyMessage *msg, const ProxyChannelInfo *info)
1349 {
1350     TRANS_LOGW(TRANS_CTRL, "receive reNegotiate msg, retry one time");
1351     AuthConnInfo authConnInfo;
1352     (void)memset_s(&authConnInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo));
1353     int32_t ret = GetAuthConnInfoByConnId(msg->connId, &authConnInfo);
1354     if (ret != SOFTBUS_OK) {
1355         TRANS_LOGE(TRANS_CTRL, "get authConnInfo by connId=%{public}u fail, ret=%{public}d", msg->connId, ret);
1356         return ret;
1357     }
1358 
1359     ret = TransReNegotiateSessionKey(&authConnInfo, info->myId);
1360     if (ret != SOFTBUS_OK) {
1361         TransProxyNegoSessionKeyFail(info->myId, ret);
1362         TRANS_LOGE(TRANS_CTRL, "generate session key failed ret=%{public}d", ret);
1363         return ret;
1364     }
1365     TRANS_LOGI(TRANS_CTRL, "call regenerate sessionKey succ");
1366     return SOFTBUS_OK;
1367 }
1368 
TransProxyProcessResetMsgHelper(const ProxyChannelInfo * info,const ProxyMessage * msg)1369 static void TransProxyProcessResetMsgHelper(const ProxyChannelInfo *info, const ProxyMessage *msg)
1370 {
1371     if (info->status == PROXY_CHANNEL_STATUS_HANDSHAKEING) {
1372         int32_t errCode = ((msg->msgHead.cipher & BAD_CIPHER) == BAD_CIPHER) ?
1373             SOFTBUS_TRANS_BAD_KEY : SOFTBUS_TRANS_HANDSHAKE_ERROR;
1374         TransProxyUnPackRestErrMsg(msg->data, &errCode, msg->dateLen);
1375         TRANS_LOGE(TRANS_CTRL, "TransProxyProcessResetMsg errCode=%{public}d", errCode);
1376         TransProxyOpenProxyChannelFail(info->channelId, &(info->appInfo), errCode);
1377     } else if (info->status == PROXY_CHANNEL_STATUS_COMPLETED) {
1378         TransEventExtra extra = {
1379             .socketName = NULL,
1380             .peerNetworkId = NULL,
1381             .calleePkg = NULL,
1382             .callerPkg = NULL,
1383             .channelId = msg->msgHead.myId,
1384             .peerChannelId = msg->msgHead.peerId,
1385             .result = EVENT_STAGE_RESULT_OK
1386         };
1387         TRANS_EVENT(EVENT_SCENE_CLOSE_CHANNEL_PASSIVE, EVENT_STAGE_CLOSE_CHANNEL, extra);
1388         OnProxyChannelClosed(info->channelId, &(info->appInfo));
1389     }
1390     (void)TransProxyCloseConnChannelReset(msg->connId, (info->isServer == 0), info->isServer, info->deviceTypeIsWinpc);
1391     if ((msg->msgHead.cipher & BAD_CIPHER) == BAD_CIPHER) {
1392         TRANS_LOGE(TRANS_CTRL, "clear bad key cipher=%{public}d, authId=%{public}" PRId64 ", keyIndex=%{public}d",
1393             msg->msgHead.cipher, msg->authHandle.authId, msg->keyIndex);
1394         RemoveAuthSessionKeyByIndex(msg->authHandle.authId, msg->keyIndex, (AuthLinkType)msg->authHandle.type);
1395     }
1396 }
1397 
TransProxyProcessResetMsg(const ProxyMessage * msg)1398 void TransProxyProcessResetMsg(const ProxyMessage *msg)
1399 {
1400     ProxyChannelInfo *info = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1401     if (info == NULL) {
1402         TRANS_LOGE(TRANS_CTRL, "ProxyProcessResetMsg calloc failed.");
1403         return;
1404     }
1405 
1406     TRANS_LOGI(TRANS_CTRL, "recv reset myChannelId=%{public}d, peerChanelId=%{public}d, cipher=%{public}d",
1407         msg->msgHead.myId, msg->msgHead.peerId, msg->msgHead.cipher);
1408     if (TransProxyUnpackIdentity(msg->data, info->identity, sizeof(info->identity), msg->dateLen) != SOFTBUS_OK) {
1409         TRANS_LOGE(TRANS_CTRL, "reset identity fail");
1410         SoftBusFree(info);
1411         return;
1412     }
1413 
1414     info->peerId = msg->msgHead.peerId;
1415     info->myId = msg->msgHead.myId;
1416 
1417     if (TransProxyGetAppInfo(info->myId, &(info->appInfo)) != SOFTBUS_OK) {
1418         TRANS_LOGE(TRANS_CTRL, "fail to get peer data info");
1419         SoftBusFree(info);
1420         return;
1421     }
1422 
1423     if (TransProxyGetReqIdAndStatus(info->myId, &info->reqId, &info->status) != SOFTBUS_OK) {
1424         TRANS_LOGE(TRANS_CTRL, "fail to get conn reqId");
1425         goto EXIT;
1426     }
1427 
1428     if (CheckAppTypeAndMsgHead(&msg->msgHead, &info->appInfo) != SOFTBUS_OK) {
1429         TRANS_LOGE(TRANS_CTRL, "only auth channel surpport plain text data");
1430         goto EXIT;
1431     }
1432 
1433     if (info->status == PROXY_CHANNEL_STATUS_HANDSHAKEING &&
1434         (msg->msgHead.cipher & AUTH_SINGLE_CIPHER) == AUTH_SINGLE_CIPHER &&
1435         TransProxyProcessReNegotiateMsg(msg, info) == SOFTBUS_OK) {
1436         goto EXIT;
1437     }
1438 
1439     if (TransProxyResetChan(info) != SOFTBUS_OK) {
1440         TRANS_LOGE(TRANS_CTRL, "reset chan fail mychannelId=%{public}d, peerChannelId=%{public}d", msg->msgHead.myId,
1441             msg->msgHead.peerId);
1442         goto EXIT;
1443     }
1444 
1445     TransProxyProcessResetMsgHelper(info, msg);
1446 EXIT:
1447     (void)memset_s(info->appInfo.sessionKey, sizeof(info->appInfo.sessionKey), 0, sizeof(info->appInfo.sessionKey));
1448     SoftBusFree(info);
1449     return;
1450 }
1451 
TransProxyProcessKeepAlive(const ProxyMessage * msg)1452 void TransProxyProcessKeepAlive(const ProxyMessage *msg)
1453 {
1454     ProxyChannelInfo *info = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1455     if (info == NULL) {
1456         TRANS_LOGE(TRANS_CTRL, "ProxyProcessKeepAlive calloc failed.");
1457         return;
1458     }
1459 
1460     TRANS_LOGI(TRANS_CTRL, "recv keepalive myChannelId=%{public}d, peerChannelId=%{public}d", msg->msgHead.myId,
1461         msg->msgHead.peerId);
1462     if (TransProxyUnpackIdentity(msg->data, info->identity, sizeof(info->identity), msg->dateLen) != SOFTBUS_OK) {
1463         TRANS_LOGE(TRANS_CTRL, "keep alive unpack identity fail");
1464         SoftBusFree(info);
1465         return;
1466     }
1467     info->peerId = msg->msgHead.peerId;
1468     info->myId = msg->msgHead.myId;
1469 
1470     if (TransProxyKeepAliveChan(info) != SOFTBUS_OK) {
1471         TRANS_LOGE(TRANS_CTRL, "reset keep alive proc fail myChannelId=%{public}d, peerChannelId=%{public}d",
1472             msg->msgHead.myId, msg->msgHead.peerId);
1473         SoftBusFree(info);
1474         return;
1475     }
1476 
1477     TransProxyAckKeepalive(info);
1478     (void)memset_s(info->appInfo.sessionKey, sizeof(info->appInfo.sessionKey), 0, sizeof(info->appInfo.sessionKey));
1479     SoftBusFree(info);
1480 }
1481 
TransProxyProcessKeepAliveAck(const ProxyMessage * msg)1482 void TransProxyProcessKeepAliveAck(const ProxyMessage *msg)
1483 {
1484     ProxyChannelInfo *info = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1485     if (info == NULL) {
1486         TRANS_LOGE(TRANS_CTRL, "ProxyProcessKeepAliveAck calloc failed.");
1487         return;
1488     }
1489 
1490     TRANS_LOGI(TRANS_CTRL, "recv keepalive ack myChannelId=%{public}d, peerChannelId=%{public}d", msg->msgHead.myId,
1491         msg->msgHead.peerId);
1492     if (TransProxyUnpackIdentity(msg->data, info->identity, sizeof(info->identity), msg->dateLen) != SOFTBUS_OK) {
1493         SoftBusFree(info);
1494         return;
1495     }
1496     info->peerId = msg->msgHead.peerId;
1497     info->myId = msg->msgHead.myId;
1498 
1499     if (TransProxyKeepAliveChan(info) != SOFTBUS_OK) {
1500         TRANS_LOGE(TRANS_CTRL, "reset keep alive ack proc fail myChannelId=%{public}d, peerChannelId=%{public}d",
1501             msg->msgHead.myId, msg->msgHead.peerId);
1502         SoftBusFree(info);
1503         return;
1504     }
1505     (void)memset_s(info->appInfo.sessionKey, sizeof(info->appInfo.sessionKey), 0, sizeof(info->appInfo.sessionKey));
1506     SoftBusFree(info);
1507 }
1508 
TransProxyProcessDataRecv(const ProxyMessage * msg)1509 void TransProxyProcessDataRecv(const ProxyMessage *msg)
1510 {
1511     ProxyChannelInfo *info = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1512     if (info == NULL) {
1513         TRANS_LOGE(TRANS_CTRL, "ProxyProcessDataRecv calloc failed.");
1514         return;
1515     }
1516 
1517     if (TransProxyGetRecvMsgChanInfo(msg->msgHead.myId, msg->msgHead.peerId, info) != SOFTBUS_OK) {
1518         TRANS_LOGE(TRANS_CTRL, "data recv get info fail myChannelId=%{public}d, peerChannelId=%{public}d",
1519             msg->msgHead.myId, msg->msgHead.peerId);
1520         SoftBusFree(info);
1521         return;
1522     }
1523 
1524     OnProxyChannelMsgReceived(info->channelId, &(info->appInfo), msg->data, msg->dateLen);
1525     (void)memset_s(info->appInfo.sessionKey, sizeof(info->appInfo.sessionKey), 0, sizeof(info->appInfo.sessionKey));
1526     SoftBusFree(info);
1527 }
1528 
TransProxyOnMessageReceived(const ProxyMessage * msg)1529 void TransProxyOnMessageReceived(const ProxyMessage *msg)
1530 {
1531     if (msg == NULL) {
1532         return;
1533     }
1534     switch (msg->msgHead.type) {
1535         case PROXYCHANNEL_MSG_TYPE_HANDSHAKE: {
1536             TransProxyProcessHandshakeMsg(msg);
1537             break;
1538         }
1539         case PROXYCHANNEL_MSG_TYPE_HANDSHAKE_ACK: {
1540             TransProxyProcessHandshakeAckMsg(msg);
1541             break;
1542         }
1543         case PROXYCHANNEL_MSG_TYPE_RESET: {
1544             TransProxyProcessResetMsg(msg);
1545             break;
1546         }
1547         case PROXYCHANNEL_MSG_TYPE_KEEPALIVE: {
1548             TransProxyProcessKeepAlive(msg);
1549             break;
1550         }
1551         case PROXYCHANNEL_MSG_TYPE_KEEPALIVE_ACK: {
1552             TransProxyProcessKeepAliveAck(msg);
1553             break;
1554         }
1555         case PROXYCHANNEL_MSG_TYPE_NORMAL: {
1556             TransProxyProcessDataRecv(msg);
1557             break;
1558         }
1559         case PROXYCHANNEL_MSG_TYPE_HANDSHAKE_AUTH: {
1560             TransProxyProcessHandshakeAuthMsg(msg);
1561             break;
1562         }
1563         default: {
1564             break;
1565         }
1566     }
1567 }
1568 
CopyAppInfoFastTransData(ProxyChannelInfo * chan,const AppInfo * appInfo)1569 static int32_t CopyAppInfoFastTransData(ProxyChannelInfo *chan, const AppInfo *appInfo)
1570 {
1571     if (appInfo->fastTransData != NULL && appInfo->fastTransDataSize > 0) {
1572         uint8_t *fastTransData = (uint8_t *)SoftBusCalloc(appInfo->fastTransDataSize);
1573         if (fastTransData == NULL) {
1574             return SOFTBUS_MALLOC_ERR;
1575         }
1576         if (memcpy_s((char *)fastTransData, appInfo->fastTransDataSize, (const char *)appInfo->fastTransData,
1577             appInfo->fastTransDataSize) != EOK) {
1578             TRANS_LOGE(TRANS_CTRL, "memcpy fastTransData fail");
1579             SoftBusFree(fastTransData);
1580             return SOFTBUS_MEM_ERR;
1581         }
1582         chan->appInfo.fastTransData = fastTransData;
1583     }
1584     return SOFTBUS_OK;
1585 }
1586 
TransProxyCreateChanInfo(ProxyChannelInfo * chan,int32_t channelId,const AppInfo * appInfo)1587 int32_t TransProxyCreateChanInfo(ProxyChannelInfo *chan, int32_t channelId, const AppInfo *appInfo)
1588 {
1589     chan->myId = (int16_t)channelId;
1590     chan->channelId = channelId;
1591 
1592     int32_t ret = GenerateRandomStr(chan->identity, sizeof(chan->identity));
1593     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "GenerateRandomStr err");
1594 
1595     if (appInfo->appType != APP_TYPE_AUTH) {
1596         ret = SoftBusGenerateRandomArray((unsigned char *)appInfo->sessionKey, sizeof(appInfo->sessionKey));
1597         TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "GenerateRandomArray err");
1598     }
1599 
1600     if (memcpy_s(&(chan->appInfo), sizeof(chan->appInfo), appInfo, sizeof(AppInfo)) != EOK) {
1601         TRANS_LOGE(TRANS_CTRL, "appInfo memcpy failed.");
1602         return SOFTBUS_MEM_ERR;
1603     }
1604 
1605     ret = CopyAppInfoFastTransData(chan, appInfo);
1606     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "copy appinfo fast trans data fail");
1607 
1608     ret = TransProxyAddChanItem(chan);
1609     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret,
1610         TRANS_CTRL, "trans proxy add channelId fail. channelId=%{public}d", channelId);
1611     return SOFTBUS_OK;
1612 }
1613 
TransProxyUpdateBlePriority(int32_t channelId,uint32_t connId,BlePriority priority)1614 static void TransProxyUpdateBlePriority(int32_t channelId, uint32_t connId, BlePriority priority)
1615 {
1616     if (priority <= BLE_PRIORITY_DEFAULT || priority >= BLE_PRIORITY_MAX) {
1617         TRANS_LOGD(TRANS_CTRL, "not need updated ble priority");
1618         return;
1619     }
1620     ConnectBlePriority blePriority = CONN_BLE_PRIORITY_BALANCED;
1621     switch (priority) {
1622         case BLE_PRIORITY_BALANCED:
1623             blePriority = CONN_BLE_PRIORITY_BALANCED;
1624             break;
1625         case BLE_PRIORITY_HIGH:
1626             blePriority = CONN_BLE_PRIORITY_HIGH;
1627             break;
1628         case BLE_PRIORITY_LOW_POWER:
1629             blePriority = CONN_BLE_PRIORITY_LOW_POWER;
1630             break;
1631         default:
1632             return;
1633     }
1634     UpdateOption option = {
1635         .type = CONNECT_BLE,
1636         .bleOption = {
1637             .priority = blePriority,
1638         }
1639     };
1640     int32_t ret = ConnUpdateConnection(connId, &option);
1641     TRANS_LOGI(TRANS_CTRL, "update ble priority. channelId=%{public}d, connId=%{public}u, "
1642         "blePriority=%{public}d, ret=%{public}d", channelId, connId, priority, ret);
1643 }
1644 
TransProxyOpenProxyChannelSuccess(int32_t channelId)1645 void TransProxyOpenProxyChannelSuccess(int32_t channelId)
1646 {
1647     TRANS_LOGI(TRANS_CTRL, "send handshake msg. channelId=%{public}d", channelId);
1648     ProxyChannelInfo *channelInfo = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1649     TRANS_CHECK_AND_RETURN_LOGE(channelInfo != NULL, TRANS_CTRL, "malloc proxyChannelInfo failed");
1650 
1651     if (TransProxyGetChanByChanId(channelId, channelInfo) != SOFTBUS_OK) {
1652         SoftBusFree(channelInfo);
1653         TRANS_LOGE(TRANS_CTRL, "disconnect device channelId=%{public}d", channelId);
1654         return;
1655     }
1656     if (channelInfo->type == CONNECT_BLE) {
1657         TransProxyUpdateBlePriority(channelId, channelInfo->connId, channelInfo->appInfo.blePriority);
1658     }
1659     (void)memset_s(channelInfo->appInfo.sessionKey, sizeof(channelInfo->appInfo.sessionKey), 0,
1660         sizeof(channelInfo->appInfo.sessionKey));
1661     AuthConnInfo authConnInfo;
1662     (void)memset_s(&authConnInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo));
1663     int32_t ret = GetAuthConnInfoByConnId(channelInfo->connId, &authConnInfo);
1664     if (ret != SOFTBUS_OK) {
1665         TRANS_LOGE(TRANS_CTRL, "get authConnInfo by connId=%{public}u fail, ret=%{public}d", channelInfo->connId, ret);
1666         SoftBusFree(channelInfo);
1667         return;
1668     }
1669 
1670     char peerNetworkId[DEVICE_ID_SIZE_MAX] = { 0 };
1671     ret = TransGetRemoteDeviceIdByReqId(channelInfo->reqId, peerNetworkId);
1672     SoftBusFree(channelInfo);
1673     if (ret != SOFTBUS_OK) {
1674         TRANS_LOGE(TRANS_CTRL, "get networkId failed, ret=%{public}d", ret);
1675         return;
1676     }
1677 
1678     ret = TransNegotiateSessionKey(&authConnInfo, channelId, peerNetworkId);
1679     if (ret != SOFTBUS_OK) {
1680         TransProxyNegoSessionKeyFail(channelId, ret);
1681         TRANS_LOGE(TRANS_CTRL, "generate session key failed ret=%{public}d", ret);
1682         return;
1683     }
1684 }
1685 
TransProxyOpenProxyChannelFail(int32_t channelId,const AppInfo * appInfo,int32_t errCode)1686 void TransProxyOpenProxyChannelFail(int32_t channelId, const AppInfo *appInfo, int32_t errCode)
1687 {
1688     if (errCode == SOFTBUS_TRANS_PEER_SESSION_NOT_CREATED) {
1689         (void)TransAddTimestampToList(
1690             appInfo->myData.sessionName, appInfo->peerData.sessionName, appInfo->peerNetWorkId, SoftBusGetSysTimeMs());
1691     }
1692     (void)OnProxyChannelOpenFailed(channelId, appInfo, errCode);
1693 }
1694 
TransProxyOpenProxyChannel(AppInfo * appInfo,const ConnectOption * connInfo,int32_t * channelId)1695 int32_t TransProxyOpenProxyChannel(AppInfo *appInfo, const ConnectOption *connInfo,
1696     int32_t *channelId)
1697 {
1698     if (appInfo == NULL || connInfo == NULL || channelId == NULL) {
1699         TRANS_LOGE(TRANS_CTRL, "open normal channel: invalid para");
1700         return SOFTBUS_INVALID_PARAM;
1701     }
1702 
1703     SelectRouteType(connInfo->type, &appInfo->routeType);
1704     return TransProxyOpenConnChannel(appInfo, connInfo, channelId);
1705 }
1706 
TransProxyCloseProxyChannel(int32_t channelId)1707 int32_t TransProxyCloseProxyChannel(int32_t channelId)
1708 {
1709     ProxyChannelInfo *info = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1710     if (info == NULL) {
1711         return SOFTBUS_MALLOC_ERR;
1712     }
1713 
1714     if (TransProxyDelByChannelId(channelId, info) != SOFTBUS_OK) {
1715         TRANS_LOGE(TRANS_CTRL, "proxy del failed. channelId=%{public}d", channelId);
1716         SoftBusFree(info);
1717         return SOFTBUS_TRANS_PROXY_INVALID_CHANNEL_ID;
1718     }
1719     if (info->type == CONNECT_BLE && info->appInfo.blePriority > BLE_PRIORITY_BALANCED &&
1720         info->appInfo.blePriority < BLE_PRIORITY_MAX) {
1721         TransProxyUpdateBlePriority(channelId, info->connId, BLE_PRIORITY_BALANCED);
1722     }
1723     (void)memset_s(info->appInfo.sessionKey, sizeof(info->appInfo.sessionKey), 0, sizeof(info->appInfo.sessionKey));
1724     TransProxyCloseProxyOtherRes(channelId, info);
1725     return SOFTBUS_OK;
1726 }
1727 
TransProxyTimerItemProc(const ListNode * proxyProcList)1728 static void TransProxyTimerItemProc(const ListNode *proxyProcList)
1729 {
1730     if (IsListEmpty(proxyProcList)) {
1731         return;
1732     }
1733     TRANS_LOGI(TRANS_CTRL, "enter.");
1734     ProxyChannelInfo *removeNode = NULL;
1735     ProxyChannelInfo *nextNode = NULL;
1736     ProxyChannelInfo *disChanInfo = NULL;
1737     uint32_t connId;
1738     int8_t status;
1739     bool isServer;
1740 
1741     LIST_FOR_EACH_ENTRY_SAFE(removeNode, nextNode, proxyProcList, ProxyChannelInfo, node) {
1742         ListDelete(&(removeNode->node));
1743         status = removeNode->status;
1744         SoftBusFree((void *)removeNode->appInfo.fastTransData);
1745         removeNode->appInfo.fastTransData = NULL;
1746         (void)memset_s(removeNode->appInfo.sessionKey, sizeof(removeNode->appInfo.sessionKey), 0,
1747             sizeof(removeNode->appInfo.sessionKey));
1748         if (status == PROXY_CHANNEL_STATUS_HANDSHAKE_TIMEOUT) {
1749             connId = removeNode->connId;
1750             isServer = removeNode->isServer;
1751             disChanInfo = (ProxyChannelInfo *)SoftBusMalloc(sizeof(ProxyChannelInfo));
1752             if (disChanInfo == NULL) {
1753                 SoftBusFree(removeNode);
1754                 TRANS_LOGE(TRANS_SVC, "SoftBusMalloc failed");
1755                 return;
1756             }
1757             if (memcpy_s(disChanInfo, sizeof(ProxyChannelInfo), removeNode, sizeof(ProxyChannelInfo)) != EOK) {
1758                 SoftBusFree(removeNode);
1759                 SoftBusFree(disChanInfo);
1760                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
1761                 return;
1762             }
1763             TransProxyPostOpenFailMsgToLoop(removeNode, SOFTBUS_TRANS_HANDSHAKE_TIMEOUT);
1764             TransProxyPostDisConnectMsgToLoop(connId, isServer, disChanInfo);
1765         } else if (status == PROXY_CHANNEL_STATUS_CONNECTING_TIMEOUT) {
1766             (void)TransDelConnByReqId(removeNode->reqId);
1767             TransProxyPostOpenFailMsgToLoop(removeNode, SOFTBUS_TRANS_HANDSHAKE_TIMEOUT);
1768         } else if (status == PROXY_CHANNEL_STATUS_TIMEOUT) {
1769             TRANS_LOGI(TRANS_CTRL, "send keepalive channelId=%{public}d", removeNode->myId);
1770             TransProxyPostKeepAliveMsgToLoop(removeNode);
1771         } else {
1772             SoftBusFree(removeNode);
1773         }
1774     }
1775 }
1776 
TransProxyTimerProc(void)1777 void TransProxyTimerProc(void)
1778 {
1779     ProxyChannelInfo *removeNode = NULL;
1780     ProxyChannelInfo *nextNode = NULL;
1781     ListNode proxyProcList;
1782 
1783     TRANS_CHECK_AND_RETURN_LOGE(
1784         g_proxyChannelList != NULL, TRANS_CTRL, "g_proxyChannelList is null");
1785     TRANS_CHECK_AND_RETURN_LOGE(
1786         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, TRANS_CTRL, "lock mutex fail!");
1787     if (g_proxyChannelList->cnt <= 0) {
1788         (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
1789         return;
1790     }
1791 
1792     ListInit(&proxyProcList);
1793     LIST_FOR_EACH_ENTRY_SAFE(removeNode, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
1794         removeNode->timeout++;
1795         if (removeNode->status == PROXY_CHANNEL_STATUS_HANDSHAKEING ||
1796             removeNode->status == PROXY_CHANNEL_STATUS_PYH_CONNECTING) {
1797             if (removeNode->timeout >= PROXY_CHANNEL_CONTROL_TIMEOUT) {
1798                 removeNode->status = (removeNode->status == PROXY_CHANNEL_STATUS_HANDSHAKEING) ?
1799                     PROXY_CHANNEL_STATUS_HANDSHAKE_TIMEOUT : PROXY_CHANNEL_STATUS_CONNECTING_TIMEOUT;
1800                 TRANS_LOGE(TRANS_CTRL, "handshake is timeout. channelId=%{public}d", removeNode->myId);
1801                 ReleaseProxyChannelId(removeNode->channelId);
1802                 ListDelete(&(removeNode->node));
1803                 ListAdd(&proxyProcList, &(removeNode->node));
1804                 g_proxyChannelList->cnt--;
1805             }
1806         }
1807         if (removeNode->status == PROXY_CHANNEL_STATUS_KEEPLIVEING) {
1808             if (removeNode->timeout >= PROXY_CHANNEL_CONTROL_TIMEOUT) {
1809                 removeNode->status = PROXY_CHANNEL_STATUS_TIMEOUT;
1810                 TRANS_LOGE(TRANS_CTRL, "keepalvie is timeout. channelId=%{public}d", removeNode->myId);
1811                 ReleaseProxyChannelId(removeNode->channelId);
1812                 ListDelete(&(removeNode->node));
1813                 ListAdd(&proxyProcList, &(removeNode->node));
1814                 g_proxyChannelList->cnt--;
1815             }
1816         }
1817     }
1818     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
1819     TransProxyTimerItemProc(&proxyProcList);
1820 }
1821 
TransWifiOnLineProc(const char * peerNetworkId)1822 static void TransWifiOnLineProc(const char *peerNetworkId)
1823 {
1824     TRANS_LOGI(TRANS_CTRL, "wifi is online");
1825     if (peerNetworkId == NULL) {
1826         TRANS_LOGE(TRANS_CTRL, "invalid networkId");
1827         return;
1828     }
1829     int32_t ret = NotifyNearByOnMigrateEvents(peerNetworkId, WIFI_STA, true);
1830     if (ret == SOFTBUS_OK) {
1831         TRANS_LOGI(TRANS_CTRL, "notify upgrade migrate success");
1832         return;
1833     }
1834     TRANS_LOGE(TRANS_CTRL, "notify upgrade migrate fail");
1835 }
1836 
TransWifiOffLineProc(const char * peerNetworkId)1837 static void TransWifiOffLineProc(const char *peerNetworkId)
1838 {
1839     TRANS_LOGI(TRANS_CTRL, "wifi is offline");
1840     if (peerNetworkId == NULL) {
1841         TRANS_LOGE(TRANS_CTRL, "invalid networkId");
1842         return;
1843     }
1844     int32_t ret = NotifyNearByOnMigrateEvents(peerNetworkId, WIFI_STA, false);
1845     if (ret == SOFTBUS_OK) {
1846         TRANS_LOGI(TRANS_CTRL, "notify degrade migrate success");
1847         return;
1848     }
1849     TRANS_LOGE(TRANS_CTRL, "notify degrade migrate fail");
1850 }
1851 
TransWifiStateChange(const LnnEventBasicInfo * info)1852 void TransWifiStateChange(const LnnEventBasicInfo *info)
1853 {
1854     TRANS_LOGI(TRANS_CTRL, "Start");
1855     if ((info == NULL) || (info->event != LNN_EVENT_NODE_MIGRATE)) {
1856         return;
1857     }
1858 
1859     LnnOnlineStateEventInfo *onlineStateInfo = (LnnOnlineStateEventInfo *)info;
1860     if (onlineStateInfo->isOnline == true) {
1861         TransWifiOnLineProc(onlineStateInfo->networkId);
1862     } else {
1863         TransWifiOffLineProc(onlineStateInfo->networkId);
1864     }
1865 }
1866 
TransNotifySingleNetworkOffLine(const LnnEventBasicInfo * info)1867 static void TransNotifySingleNetworkOffLine(const LnnEventBasicInfo *info)
1868 {
1869     if ((info == NULL) || (info->event != LNN_EVENT_SINGLE_NETWORK_OFFLINE)) {
1870         return;
1871     }
1872     LnnSingleNetworkOffLineEvent *offlineInfo = (LnnSingleNetworkOffLineEvent *)info;
1873     ConnectionAddrType type = offlineInfo->type;
1874     if (type == CONNECTION_ADDR_WLAN) {
1875         TransOnLinkDown(offlineInfo->networkId, offlineInfo->uuid, offlineInfo->udid, "", WIFI_STA);
1876     } else if (type == CONNECTION_ADDR_BLE) {
1877         TransOnLinkDown(offlineInfo->networkId, offlineInfo->uuid, offlineInfo->udid, "", BT_BLE);
1878     } else if (type == CONNECTION_ADDR_BR) {
1879         TransOnLinkDown(offlineInfo->networkId, offlineInfo->uuid, offlineInfo->udid, "", BT_BR);
1880     }
1881 }
1882 
TransNotifyOffLine(const LnnEventBasicInfo * info)1883 static void TransNotifyOffLine(const LnnEventBasicInfo *info)
1884 {
1885     TRANS_LOGI(TRANS_CTRL, "Trans Notify OffLine Start");
1886     if ((info == NULL) || (info->event != LNN_EVENT_NODE_ONLINE_STATE_CHANGED)) {
1887         return;
1888     }
1889     LnnOnlineStateEventInfo *onlineStateInfo = (LnnOnlineStateEventInfo *)info;
1890     if (onlineStateInfo->isOnline) {
1891         return;
1892     }
1893 
1894     TransOnLinkDown(onlineStateInfo->networkId, onlineStateInfo->uuid, onlineStateInfo->udid, "", WIFI_P2P);
1895     TransOnLinkDown(onlineStateInfo->networkId, onlineStateInfo->uuid, onlineStateInfo->udid, "", WIFI_STA);
1896     TransOnLinkDown(onlineStateInfo->networkId, onlineStateInfo->uuid, onlineStateInfo->udid, "", BT_BR);
1897     TransOnLinkDown(onlineStateInfo->networkId, onlineStateInfo->uuid, onlineStateInfo->udid, "", BT_BLE);
1898 }
1899 
TransNotifyUserSwitch(const LnnEventBasicInfo * info)1900 static void TransNotifyUserSwitch(const LnnEventBasicInfo *info)
1901 {
1902 #define USER_SWITCH_OFFSET 10
1903     TRANS_CHECK_AND_RETURN_LOGE(info != NULL, TRANS_CTRL, "invalid Lnn info");
1904     const LnnMonitorHbStateChangedEvent *event = (const LnnMonitorHbStateChangedEvent *)info;
1905     SoftBusUserSwitchState userSwitchState = (SoftBusUserSwitchState)event->status;
1906     switch (userSwitchState) {
1907         case SOFTBUS_USER_SWITCHED: {
1908             TransOnLinkDown("", "", "", "", ROUTE_TYPE_ALL | 1 << USER_SWITCH_OFFSET);
1909             break;
1910         }
1911         case SOFTBUS_USER_SWITCH_UNKNOWN:
1912         default: {
1913             TRANS_LOGE(TRANS_CTRL, "recv unknow user switch event, state=%{public}u", event->status);
1914             break;
1915         }
1916     }
1917 }
1918 
TransProxyManagerInitInner(const IServerChannelCallBack * cb)1919 static int32_t TransProxyManagerInitInner(const IServerChannelCallBack *cb)
1920 {
1921     int32_t ret = TransProxySetCallBack(cb);
1922     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "TransProxySetCallBack fail");
1923 
1924     g_proxyChannelList = CreateSoftBusList();
1925     if (g_proxyChannelList == NULL) {
1926         TRANS_LOGE(TRANS_INIT, "proxy manager init inner failed");
1927         return SOFTBUS_MALLOC_ERR;
1928     }
1929     return SOFTBUS_OK;
1930 }
1931 
TransProxyManagerInit(const IServerChannelCallBack * cb)1932 int32_t TransProxyManagerInit(const IServerChannelCallBack *cb)
1933 {
1934     int32_t ret = TransProxyManagerInitInner(cb);
1935     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "init proxy manager failed");
1936 
1937     ret = TransProxyTransInit();
1938     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "TransProxyTransInit fail");
1939 
1940     ret = RegisterTimeoutCallback(SOFTBUS_PROXYCHANNEL_TIMER_FUN, TransProxyTimerProc);
1941     if (ret != SOFTBUS_OK) {
1942         DestroySoftBusList(g_proxyChannelList);
1943         TRANS_LOGE(TRANS_INIT, "trans proxy register timeout callback failed.");
1944         return ret;
1945     }
1946 
1947     ret = LnnRegisterEventHandler(LNN_EVENT_SINGLE_NETWORK_OFFLINE, TransNotifySingleNetworkOffLine);
1948     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret,
1949         TRANS_INIT, "register TransNotifySingleNetworkOffLine failed.");
1950 
1951     ret = LnnRegisterEventHandler(LNN_EVENT_NODE_ONLINE_STATE_CHANGED, TransNotifyOffLine);
1952     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "register TransNotifyOffLine failed.");
1953 
1954     ret = LnnRegisterEventHandler(LNN_EVENT_NODE_MIGRATE, TransWifiStateChange);
1955     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "TransWifiStateChange register failed.");
1956 
1957     ret = LnnRegisterEventHandler(LNN_EVENT_USER_SWITCHED, TransNotifyUserSwitch);
1958     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "register user switch event failed.");
1959 
1960     TRANS_LOGI(TRANS_INIT, "proxy channel init ok");
1961     return SOFTBUS_OK;
1962 }
1963 
TransProxyGetNameByChanId(int32_t chanId,char * pkgName,char * sessionName,uint16_t pkgLen,uint16_t sessionLen)1964 int32_t TransProxyGetNameByChanId(int32_t chanId, char *pkgName, char *sessionName,
1965     uint16_t pkgLen, uint16_t sessionLen)
1966 {
1967     TRANS_CHECK_AND_RETURN_RET_LOGE((pkgName != NULL && sessionName != NULL), SOFTBUS_INVALID_PARAM, TRANS_CTRL,
1968         "invalid param");
1969     ProxyChannelInfo *chan = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1970     TRANS_CHECK_AND_RETURN_RET_LOGE(chan != NULL, SOFTBUS_MALLOC_ERR, TRANS_CTRL, "malloc err");
1971     int32_t ret = TransProxyGetChanByChanId(chanId, chan);
1972     (void)memset_s(chan->appInfo.sessionKey, sizeof(chan->appInfo.sessionKey), 0, sizeof(chan->appInfo.sessionKey));
1973     if (ret != SOFTBUS_OK) {
1974         TRANS_LOGE(TRANS_CTRL, "get channel info by chanId failed. chanId=%{public}d", chanId);
1975         SoftBusFree(chan);
1976         return ret;
1977     }
1978     ret = TransProxyGetPkgName(chan->appInfo.myData.sessionName, pkgName, pkgLen);
1979     if (ret != SOFTBUS_OK) {
1980         TRANS_LOGE(TRANS_CTRL, "get pkgName failed");
1981         SoftBusFree(chan);
1982         return ret;
1983     }
1984     if (strcpy_s(sessionName, sessionLen, chan->appInfo.myData.sessionName) != EOK) {
1985         TRANS_LOGE(TRANS_CTRL, "strcpy_s failed");
1986         SoftBusFree(chan);
1987         return SOFTBUS_STRCPY_ERR;
1988     }
1989     SoftBusFree(chan);
1990     return SOFTBUS_OK;
1991 }
1992 
TransProxyManagerDeinitInner(void)1993 static void TransProxyManagerDeinitInner(void)
1994 {
1995     TRANS_CHECK_AND_RETURN_LOGE(
1996         g_proxyChannelList != NULL, TRANS_INIT, "g_proxyChannelList is null");
1997     TRANS_CHECK_AND_RETURN_LOGE(
1998         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, TRANS_INIT, "lock mutex fail!");
1999     ProxyChannelInfo *item = NULL;
2000     ProxyChannelInfo *nextNode = NULL;
2001     LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
2002         ReleaseProxyChannelId(item->channelId);
2003         ListDelete(&(item->node));
2004         if (item->appInfo.fastTransData != NULL) {
2005             SoftBusFree((void *)item->appInfo.fastTransData);
2006         }
2007         SoftBusFree(item);
2008     }
2009     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2010 
2011     DestroySoftBusList(g_proxyChannelList);
2012     g_proxyChannelList = NULL;
2013 }
2014 
TransProxyManagerDeinit(void)2015 void TransProxyManagerDeinit(void)
2016 {
2017     (void)RegisterTimeoutCallback(SOFTBUS_PROXYCHANNEL_TIMER_FUN, NULL);
2018     TransProxyManagerDeinitInner();
2019 }
2020 
TransProxyDestroyChannelList(const ListNode * destroyList)2021 static void TransProxyDestroyChannelList(const ListNode *destroyList)
2022 {
2023     TRANS_LOGD(TRANS_CTRL, "enter.");
2024     TRANS_CHECK_AND_RETURN_LOGE(
2025         (destroyList != NULL && !IsListEmpty(destroyList)), TRANS_INIT, "destroyList is null");
2026     ProxyChannelInfo *destroyNode = NULL;
2027     ProxyChannelInfo *nextDestroyNode = NULL;
2028     LIST_FOR_EACH_ENTRY_SAFE(destroyNode, nextDestroyNode, destroyList, ProxyChannelInfo, node) {
2029         ListDelete(&(destroyNode->node));
2030         TransProxyResetPeer(destroyNode);
2031         TransProxyCloseConnChannel(destroyNode->connId, destroyNode->isServer);
2032         if (destroyNode->appInfo.fastTransData != NULL) {
2033             SoftBusFree((void *)destroyNode->appInfo.fastTransData);
2034         }
2035         (void)memset_s(destroyNode->appInfo.sessionKey, sizeof(destroyNode->appInfo.sessionKey), 0,
2036             sizeof(destroyNode->appInfo.sessionKey));
2037         SoftBusFree(destroyNode);
2038     }
2039     return;
2040 }
2041 
TransProxyDeathCallback(const char * pkgName,int32_t pid)2042 void TransProxyDeathCallback(const char *pkgName, int32_t pid)
2043 {
2044     TRANS_CHECK_AND_RETURN_LOGE(
2045         (pkgName != NULL && g_proxyChannelList != NULL), TRANS_CTRL, "pkgName or proxy channel list is null.");
2046     char *anonymizePkgName = NULL;
2047     Anonymize(pkgName, &anonymizePkgName);
2048     TRANS_LOGW(TRANS_CTRL, "pkgName=%{public}s, pid=%{public}d", AnonymizeWrapper(anonymizePkgName), pid);
2049     AnonymizeFree(anonymizePkgName);
2050     ListNode destroyList;
2051     ListInit(&destroyList);
2052     ProxyChannelInfo *item = NULL;
2053     ProxyChannelInfo *nextNode = NULL;
2054     if (SoftBusMutexLock(&g_proxyChannelList->lock) != SOFTBUS_OK) {
2055         TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
2056         return;
2057     }
2058     LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
2059         if ((strcmp(item->appInfo.myData.pkgName, pkgName) == 0) && (item->appInfo.myData.pid == pid)) {
2060             ReleaseProxyChannelId(item->channelId);
2061             ListDelete(&(item->node));
2062             g_proxyChannelList->cnt--;
2063             ListAdd(&destroyList, &(item->node));
2064             TRANS_LOGI(TRANS_CTRL, "add channelId=%{public}d", item->channelId);
2065         }
2066     }
2067     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2068     TransProxyDestroyChannelList(&destroyList);
2069     TRANS_LOGD(TRANS_CTRL, "ok");
2070 }
2071 
TransProxyGetAppInfoByChanId(int32_t chanId,AppInfo * appInfo)2072 int32_t TransProxyGetAppInfoByChanId(int32_t chanId, AppInfo *appInfo)
2073 {
2074     TRANS_CHECK_AND_RETURN_RET_LOGE(appInfo != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL,
2075         "invalid param");
2076 
2077     ProxyChannelInfo *item = NULL;
2078     ProxyChannelInfo *nextNode = NULL;
2079 
2080     TRANS_CHECK_AND_RETURN_RET_LOGE(
2081         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
2082     TRANS_CHECK_AND_RETURN_RET_LOGE(
2083         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
2084 
2085     LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
2086         if (item->channelId == chanId) {
2087             if (memcpy_s(appInfo, sizeof(AppInfo), &item->appInfo, sizeof(AppInfo)) != EOK) {
2088                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2089                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
2090                 return SOFTBUS_MEM_ERR;
2091             }
2092             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2093             return SOFTBUS_OK;
2094         }
2095     }
2096     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2097     TRANS_LOGE(TRANS_CTRL, "Proxy channel not find: channelId=%{public}d", chanId);
2098     return SOFTBUS_TRANS_NODE_NOT_FOUND;
2099 }
2100 
TransProxyGetConnIdByChanId(int32_t channelId,int32_t * connId)2101 int32_t TransProxyGetConnIdByChanId(int32_t channelId, int32_t *connId)
2102 {
2103     TRANS_CHECK_AND_RETURN_RET_LOGE(connId != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "invalid param");
2104 
2105     ProxyChannelInfo *item = NULL;
2106 
2107     TRANS_CHECK_AND_RETURN_RET_LOGE(
2108         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
2109     TRANS_CHECK_AND_RETURN_RET_LOGE(
2110         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
2111 
2112     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
2113         if (item->channelId == channelId) {
2114             if (item->status == PROXY_CHANNEL_STATUS_COMPLETED ||
2115                 item->status == PROXY_CHANNEL_STATUS_KEEPLIVEING) {
2116                 *connId = (int32_t)item->connId;
2117                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2118                 return SOFTBUS_OK;
2119             } else {
2120                 TRANS_LOGE(TRANS_CTRL, "g_proxyChannel status error");
2121                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2122                 return SOFTBUS_TRANS_PROXY_CHANNLE_STATUS_INVALID;
2123             }
2124         }
2125     }
2126     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2127     return SOFTBUS_TRANS_NODE_NOT_FOUND;
2128 }
2129 
TransProxyGetProxyChannelInfoByChannelId(int32_t channelId,ProxyChannelInfo * chan)2130 int32_t TransProxyGetProxyChannelInfoByChannelId(int32_t channelId, ProxyChannelInfo *chan)
2131 {
2132     if (g_proxyChannelList == NULL) {
2133         TRANS_LOGE(TRANS_CTRL, "g_proxyChannelList is null");
2134         return SOFTBUS_INVALID_PARAM;
2135     }
2136     TRANS_CHECK_AND_RETURN_RET_LOGE(chan != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "invalid param");
2137 
2138     if (SoftBusMutexLock(&g_proxyChannelList->lock) != SOFTBUS_OK) {
2139         TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
2140         return SOFTBUS_LOCK_ERR;
2141     }
2142     ProxyChannelInfo *item = NULL;
2143     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
2144         if (item->channelId == channelId) {
2145             if (item->status != PROXY_CHANNEL_STATUS_COMPLETED && item->status != PROXY_CHANNEL_STATUS_KEEPLIVEING) {
2146                 TRANS_LOGE(TRANS_CTRL, "invalid status=%{public}d, channelId=%{public}d", item->status, channelId);
2147                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2148                 return SOFTBUS_TRANS_PROXY_CHANNLE_STATUS_INVALID;
2149             }
2150             if (memcpy_s(chan, sizeof(ProxyChannelInfo), item, sizeof(ProxyChannelInfo)) != EOK) {
2151                 (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2152                 TRANS_LOGE(TRANS_CTRL, "memcpy_s failed");
2153                 return SOFTBUS_MEM_ERR;
2154             }
2155             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2156             return SOFTBUS_OK;
2157         }
2158     }
2159     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2160     TRANS_LOGE(TRANS_CTRL, "not found proxy channel info by channelId=%{public}d", channelId);
2161     return SOFTBUS_TRANS_NODE_NOT_FOUND;
2162 }
2163 
TransProxySetAuthHandleByChanId(int32_t channelId,AuthHandle authHandle)2164 int32_t TransProxySetAuthHandleByChanId(int32_t channelId, AuthHandle authHandle)
2165 {
2166     ProxyChannelInfo *item = NULL;
2167     ProxyChannelInfo *nextNode = NULL;
2168 
2169     TRANS_CHECK_AND_RETURN_RET_LOGE(
2170         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
2171     TRANS_CHECK_AND_RETURN_RET_LOGE(
2172         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
2173 
2174     LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &g_proxyChannelList->list, ProxyChannelInfo, node) {
2175         if (item->channelId == channelId) {
2176             item->authHandle.authId = authHandle.authId;
2177             item->authHandle.type = authHandle.type;
2178             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2179             return SOFTBUS_OK;
2180         }
2181     }
2182     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2183     TRANS_LOGE(TRANS_CTRL, "proxy channel not found by chanId, chanId=%{public}d", channelId);
2184     return SOFTBUS_TRANS_PROXY_CHANNEL_NOT_FOUND;
2185 }
2186 
TransProxyGetPrivilegeCloseList(ListNode * privilegeCloseList,uint64_t tokenId,int32_t pid)2187 int32_t TransProxyGetPrivilegeCloseList(ListNode *privilegeCloseList, uint64_t tokenId, int32_t pid)
2188 {
2189     if (privilegeCloseList == NULL) {
2190         TRANS_LOGE(TRANS_CTRL, "privilegeCloseList is null");
2191         return SOFTBUS_INVALID_PARAM;
2192     }
2193     TRANS_CHECK_AND_RETURN_RET_LOGE(
2194         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null");
2195     TRANS_CHECK_AND_RETURN_RET_LOGE(
2196         SoftBusMutexLock(&g_proxyChannelList->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
2197     ProxyChannelInfo *item = NULL;
2198     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
2199         if (item->appInfo.callingTokenId == tokenId && item->appInfo.myData.pid == pid) {
2200             (void)PrivilegeCloseListAddItem(privilegeCloseList, item->appInfo.myData.pid, item->appInfo.myData.pkgName);
2201         }
2202     }
2203     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2204     return SOFTBUS_OK;
2205 }
2206 
TransProxyResetReplyCnt(int32_t channelId)2207 static int32_t TransProxyResetReplyCnt(int32_t channelId)
2208 {
2209     TRANS_CHECK_AND_RETURN_RET_LOGE(
2210         g_proxyChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_proxyChannelList is null.");
2211     int32_t ret = SoftBusMutexLock(&g_proxyChannelList->lock);
2212     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
2213     ProxyChannelInfo *item = NULL;
2214     LIST_FOR_EACH_ENTRY(item, &g_proxyChannelList->list, ProxyChannelInfo, node) {
2215         if (item->channelId == channelId) {
2216             item->appInfo.waitOpenReplyCnt = 0;
2217             (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2218             return SOFTBUS_OK;
2219         }
2220     }
2221     (void)SoftBusMutexUnlock(&g_proxyChannelList->lock);
2222     TRANS_LOGE(TRANS_CTRL, "proxy channel not found by channelId=%{public}d", channelId);
2223     return SOFTBUS_TRANS_PROXY_CHANNEL_NOT_FOUND;
2224 }
2225 
TransDealProxyCheckCollabResult(int32_t channelId,int32_t checkResult)2226 int32_t TransDealProxyCheckCollabResult(int32_t channelId, int32_t checkResult)
2227 {
2228     ProxyChannelInfo chan = { 0 };
2229     int32_t ret = TransProxyGetChanByChanId(channelId, &chan);
2230     if (ret != SOFTBUS_OK) {
2231         TRANS_LOGE(TRANS_CTRL, "get channelInfo failed, channelId=%{public}d.", channelId);
2232         return ret;
2233     }
2234 
2235     ret = TransProxyUpdateReplyCnt(channelId);
2236     if (ret != SOFTBUS_OK) {
2237         TRANS_LOGE(TRANS_CTRL, "update waitOpenReplyCnt failed, channelId=%{public}d.", channelId);
2238         goto ERR_EXIT;
2239     }
2240     // Remove old check tasks.
2241     TransCheckChannelOpenRemoveFromLooper(channelId);
2242     if (checkResult != SOFTBUS_OK) {
2243         TRANS_LOGE(TRANS_CTRL, "check synertistic relation failed, channelId=%{public}d.", channelId);
2244         ret = checkResult;
2245         goto ERR_EXIT;
2246     }
2247     // Reset the check count to 0.
2248     ret = TransProxyResetReplyCnt(channelId);
2249     if (ret != SOFTBUS_OK) {
2250         goto ERR_EXIT;
2251     }
2252 
2253     ret = OnProxyChannelOpened(channelId, &(chan.appInfo), PROXY_CHANNEL_SERVER);
2254     if (ret != SOFTBUS_OK) {
2255         goto ERR_EXIT;
2256     }
2257     return SOFTBUS_OK;
2258 
2259 ERR_EXIT:
2260     (void)TransProxyAckHandshake(chan.connId, &chan, ret);
2261     TransProxyDelChanByChanId(channelId);
2262     return ret;
2263 }
2264