• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "trans_channel_manager.h"
17 
18 #include <securec.h>
19 
20 #include "access_control.h"
21 #include "anonymizer.h"
22 #include "bus_center_manager.h"
23 #include "legacy/softbus_hisysevt_transreporter.h"
24 #include "lnn_distributed_net_ledger.h"
25 #include "lnn_lane_qos.h"
26 #include "message_handler.h"
27 #include "session.h"
28 #include "softbus_adapter_mem.h"
29 #include "softbus_conn_interface.h"
30 #include "softbus_def.h"
31 #include "softbus_error_code.h"
32 #include "softbus_proxychannel_manager.h"
33 #include "softbus_proxychannel_session.h"
34 #include "softbus_proxychannel_transceiver.h"
35 #include "softbus_qos.h"
36 #include "softbus_utils.h"
37 #include "trans_auth_lane_pending_ctl.h"
38 #include "trans_auth_manager.h"
39 #include "trans_auth_negotiation.h"
40 #include "trans_bind_request_manager.h"
41 #include "trans_channel_callback.h"
42 #include "trans_channel_common.h"
43 #include "trans_event.h"
44 #include "trans_lane_manager.h"
45 #include "trans_lane_pending_ctl.h"
46 #include "trans_link_listener.h"
47 #include "trans_log.h"
48 #include "trans_network_statistics.h"
49 #include "trans_session_manager.h"
50 #include "trans_tcp_direct_manager.h"
51 #include "trans_tcp_direct_message.h"
52 #include "trans_tcp_direct_sessionconn.h"
53 #include "trans_udp_channel_manager.h"
54 #include "trans_udp_negotiation.h"
55 
56 #define MAX_PROXY_CHANNEL_ID 0x00000800
57 #define MAX_TDC_CHANNEL_ID 0x7FFFFFFF
58 #define MIN_FD_ID 1025
59 #define MAX_FD_ID 2048
60 #define MAX_PROXY_CHANNEL_ID_COUNT 1024
61 #define ID_NOT_USED 0
62 #define ID_USED 1UL
63 #define BIT_NUM 8
64 #define REPORT_UDP_INFO_SIZE 4
65 
66 static int32_t g_allocTdcChannelId = MAX_PROXY_CHANNEL_ID;
67 static SoftBusMutex g_myIdLock;
68 static unsigned long g_proxyChanIdBits[MAX_PROXY_CHANNEL_ID_COUNT / BIT_NUM / sizeof(long)] = {0};
69 static uint32_t g_proxyIdMark = 0;
70 static uint32_t g_channelIdCount = 0;
71 
72 const char *g_channelResultLoopName = "transChannelResultLoopName";
73 SoftBusHandler g_channelResultHandler = { 0 };
74 
75 typedef enum {
76     LOOP_CHANNEL_OPEN_MSG,
77     LOOP_LIMIT_CHANGE_MSG,
78 } ChannelResultLoopMsg;
79 
80 typedef struct {
81     ListNode node;
82     int32_t pid;
83     char pkgName[PKG_NAME_SIZE_MAX];
84 } PrivilegeCloseChannelInfo;
85 
GenerateTdcChannelId()86 static int32_t GenerateTdcChannelId()
87 {
88     int32_t channelId;
89     if (SoftBusMutexLock(&g_myIdLock) != SOFTBUS_OK) {
90         TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
91         return SOFTBUS_LOCK_ERR;
92     }
93     channelId = g_allocTdcChannelId++;
94     if (g_allocTdcChannelId >= MAX_TDC_CHANNEL_ID) {
95         g_allocTdcChannelId = MAX_PROXY_CHANNEL_ID;
96     }
97     SoftBusMutexUnlock(&g_myIdLock);
98     return channelId;
99 }
100 
GenerateProxyChannelId()101 static int32_t GenerateProxyChannelId()
102 {
103     if (SoftBusMutexLock(&g_myIdLock) != SOFTBUS_OK) {
104         TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
105         return SOFTBUS_LOCK_ERR;
106     }
107 
108     if (g_channelIdCount >= MAX_PROXY_CHANNEL_ID_COUNT) {
109         TRANS_LOGE(TRANS_CTRL, "No more channel Ids(1024) can be applied");
110         SoftBusMutexUnlock(&g_myIdLock);
111         return INVALID_CHANNEL_ID;
112     }
113 
114     for (uint32_t id = g_proxyIdMark + 1; id != g_proxyIdMark; id++) {
115         id = id % MAX_PROXY_CHANNEL_ID_COUNT;
116         uint32_t index = id / (BIT_NUM * sizeof(long));
117         uint32_t bit = id % (BIT_NUM * sizeof(long));
118         if ((g_proxyChanIdBits[index] & (ID_USED << bit)) == ID_NOT_USED) {
119             g_proxyChanIdBits[index] |= (ID_USED << bit);
120             g_proxyIdMark = id;
121             g_channelIdCount++;
122             SoftBusMutexUnlock(&g_myIdLock);
123             return (int32_t)id + MIN_FD_ID;
124         }
125     }
126     SoftBusMutexUnlock(&g_myIdLock);
127     return INVALID_CHANNEL_ID;
128 }
129 
ReleaseProxyChannelId(int32_t channelId)130 void ReleaseProxyChannelId(int32_t channelId)
131 {
132     if (channelId < MIN_FD_ID || channelId > MAX_FD_ID) {
133         return;
134     }
135     if (SoftBusMutexLock(&g_myIdLock) != SOFTBUS_OK) {
136         TRANS_LOGE(TRANS_CTRL, "lock mutex fail");
137         return;
138     }
139     if (g_channelIdCount >= ID_USED) {
140         g_channelIdCount--;
141     } else {
142         TRANS_LOGE(TRANS_CTRL, "g_channelIdCount error");
143     }
144     uint32_t id = (uint32_t)channelId - MIN_FD_ID;
145     uint32_t dex = id / (8 * sizeof(long));
146     uint32_t bit = id % (8 * sizeof(long));
147     g_proxyChanIdBits[dex] &= (~(ID_USED << bit));
148     SoftBusMutexUnlock(&g_myIdLock);
149 }
150 
GenerateChannelId(bool isTdcChannel)151 int32_t GenerateChannelId(bool isTdcChannel)
152 {
153     return isTdcChannel ? GenerateTdcChannelId() : GenerateProxyChannelId();
154 }
155 
TransAsyncChannelOpenTaskManager(int32_t channelId,int32_t channelType)156 void TransAsyncChannelOpenTaskManager(int32_t channelId, int32_t channelType)
157 {
158     switch (channelType) {
159         case CHANNEL_TYPE_PROXY:
160             TransAsyncProxyChannelTask(channelId);
161             break;
162         case CHANNEL_TYPE_TCP_DIRECT:
163             TransAsyncTcpDirectChannelTask(channelId);
164             break;
165         case CHANNEL_TYPE_UDP:
166             TransAsyncUdpChannelTask(channelId);
167             break;
168         case CHANNEL_TYPE_AUTH:
169             TransAsyncAuthChannelTask(channelId);
170             break;
171         default:
172             TRANS_LOGE(TRANS_CTRL, "channelType=%{public}d is error!", channelType);
173     }
174 }
175 
TransChannelResultLoopMsgHandler(SoftBusMessage * msg)176 static void TransChannelResultLoopMsgHandler(SoftBusMessage *msg)
177 {
178     TRANS_CHECK_AND_RETURN_LOGE(msg != NULL, TRANS_MSG, "param msg is invalid");
179     int32_t channelId;
180     int32_t channelType;
181     if (msg->what == LOOP_CHANNEL_OPEN_MSG) {
182         channelId = (int32_t)msg->arg1;
183         channelType = (int32_t)msg->arg2;
184         TransAsyncChannelOpenTaskManager(channelId, channelType);
185     }
186 }
187 
TransChannelResultLoopInit(void)188 int32_t TransChannelResultLoopInit(void)
189 {
190     g_channelResultHandler.name = (char *)g_channelResultLoopName;
191     g_channelResultHandler.looper = GetLooper(LOOP_TYPE_DEFAULT);
192     if (g_channelResultHandler.looper == NULL) {
193         return SOFTBUS_TRANS_INIT_FAILED;
194     }
195     g_channelResultHandler.HandleMessage = TransChannelResultLoopMsgHandler;
196     return SOFTBUS_OK;
197 }
198 
TransChannelResultFreeLoopMsg(SoftBusMessage * msg)199 static void TransChannelResultFreeLoopMsg(SoftBusMessage *msg)
200 {
201     if (msg != NULL) {
202         if (msg->obj != NULL) {
203             SoftBusFree(msg->obj);
204         }
205         SoftBusFree((void *)msg);
206     }
207 }
208 
TransChannelResultCreateLoopMsg(int32_t what,uint64_t arg1,uint64_t arg2,char * data)209 static SoftBusMessage *TransChannelResultCreateLoopMsg(int32_t what, uint64_t arg1, uint64_t arg2, char *data)
210 {
211     SoftBusMessage *msg = (SoftBusMessage *)SoftBusCalloc(sizeof(SoftBusMessage));
212     if (msg == NULL) {
213         TRANS_LOGE(TRANS_MSG, "msg calloc failed");
214         return NULL;
215     }
216     msg->what = what;
217     msg->arg1 = arg1;
218     msg->arg2 = arg2;
219     msg->handler = &g_channelResultHandler;
220     msg->FreeMessage = TransChannelResultFreeLoopMsg;
221     msg->obj = (void *)data;
222     return msg;
223 }
224 
TransCheckChannelOpenToLooperDelay(int32_t channelId,int32_t channelType,uint32_t delayTime)225 void TransCheckChannelOpenToLooperDelay(int32_t channelId, int32_t channelType, uint32_t delayTime)
226 {
227     SoftBusMessage *msg  = TransChannelResultCreateLoopMsg(LOOP_CHANNEL_OPEN_MSG, channelId, channelType, NULL);
228     TRANS_CHECK_AND_RETURN_LOGE(msg != NULL, TRANS_MSG, "msg create failed");
229 
230     g_channelResultHandler.looper->PostMessageDelay(g_channelResultHandler.looper, msg, delayTime);
231 }
232 
RemoveMessageDestroy(const SoftBusMessage * msg,void * data)233 static int32_t RemoveMessageDestroy(const SoftBusMessage *msg, void *data)
234 {
235     int32_t channelId = *(int32_t *)data;
236     if (msg->what == LOOP_CHANNEL_OPEN_MSG && channelId == (int32_t)msg->arg1) {
237         TRANS_LOGE(TRANS_CTRL, "remove delay check channel opened message succ, channelId=%{public}d.", channelId);
238         return SOFTBUS_OK;
239     }
240     return SOFTBUS_INVALID_PARAM;
241 }
242 
TransCheckChannelOpenRemoveFromLooper(int32_t channelId)243 void TransCheckChannelOpenRemoveFromLooper(int32_t channelId)
244 {
245     g_channelResultHandler.looper->RemoveMessageCustom(
246         g_channelResultHandler.looper, &g_channelResultHandler, RemoveMessageDestroy, &channelId);
247 }
248 
TransChannelInit(void)249 int32_t TransChannelInit(void)
250 {
251     IServerChannelCallBack *cb = TransServerGetChannelCb();
252     TRANS_CHECK_AND_RETURN_RET_LOGE(cb != NULL, SOFTBUS_NO_INIT, TRANS_INIT, "cd is null.");
253 
254     int32_t ret = TransLaneMgrInit();
255     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans lane manager init failed.");
256 
257     ret = TransSocketLaneMgrInit();
258     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans socket lane manager init failed.");
259 
260     ret = TransAuthInit(cb);
261     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans auth init failed.");
262 
263     ret = TransProxyManagerInit(cb);
264     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans proxy manager init failed.");
265 
266     ret = TransTcpDirectInit(cb);
267     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans tcp direct init failed.");
268 
269     ret = TransUdpChannelInit(cb);
270     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans udp channel init failed.");
271 
272     ret = TransBindRequestManagerInit();
273     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans bind request manager init failed.");
274 
275     ret = TransReqLanePendingInit();
276     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans req lane pending init failed.");
277 
278     ret = TransNetworkStatisticsInit();
279     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans network statistics init failed.");
280 
281     ret = TransAsyncReqLanePendingInit();
282     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans async req lane pending init failed.");
283 
284     ret = TransReqAuthPendingInit();
285     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans auth request pending init failed.");
286     ret = TransAuthWithParaReqLanePendingInit();
287     TRANS_CHECK_AND_RETURN_RET_LOGE(
288         ret == SOFTBUS_OK, ret, TRANS_INIT, "trans auth with para req lane pending init failed.");
289 
290     ret = TransFreeLanePendingInit();
291     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans free lane pending init failed.");
292 
293     ret = TransChannelResultLoopInit();
294     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans channel result loop init failed.");
295 
296     ReqLinkListener();
297     ret = SoftBusMutexInit(&g_myIdLock, NULL);
298     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "init lock failed.");
299     return SOFTBUS_OK;
300 }
301 
TransChannelDeinit(void)302 void TransChannelDeinit(void)
303 {
304     TransLaneMgrDeinit();
305     TransSocketLaneMgrDeinit();
306     TransAuthDeinit();
307     TransProxyManagerDeinit();
308     TransTcpDirectDeinit();
309     TransUdpChannelDeinit();
310     TransReqLanePendingDeinit();
311     TransAsyncReqLanePendingDeinit();
312     TransNetworkStatisticsDeinit();
313     TransReqAuthPendingDeinit();
314     TransAuthWithParaReqLanePendingDeinit();
315     TransFreeLanePendingDeinit();
316     TransBindRequestManagerDeinit();
317     SoftBusMutexDestroy(&g_myIdLock);
318 }
319 
TransSetFirstTokenInfo(AppInfo * appInfo,TransEventExtra * event)320 static void TransSetFirstTokenInfo(AppInfo *appInfo, TransEventExtra *event)
321 {
322     event->firstTokenId = TransACLGetFirstTokenID();
323     if (event->firstTokenId == TOKENID_NOT_SET) {
324         event->firstTokenId = appInfo->callingTokenId;
325     }
326     TransGetTokenInfo(event->firstTokenId, appInfo->tokenName, sizeof(appInfo->tokenName), &event->firstTokenType);
327     event->firstTokenName = appInfo->tokenName;
328 }
329 
TransSetQosInfo(const QosTV * qos,uint32_t qosCount,TransEventExtra * extra)330 static void TransSetQosInfo(const QosTV *qos, uint32_t qosCount, TransEventExtra *extra)
331 {
332     if (qosCount > QOS_TYPE_BUTT) {
333         TRANS_LOGE(TRANS_CTRL, "qos count error, qosCount=%{public}" PRIu32, qosCount);
334         return;
335     }
336 
337     for (uint32_t i = 0; i < qosCount; i++) {
338         switch (qos[i].qos) {
339             case QOS_TYPE_MIN_BW:
340                 extra->minBW = qos[i].value;
341                 break;
342             case QOS_TYPE_MAX_LATENCY:
343                 extra->maxLatency = qos[i].value;
344                 break;
345             case QOS_TYPE_MIN_LATENCY:
346                 extra->minLatency = qos[i].value;
347                 break;
348             default:
349                 break;
350         }
351     }
352 }
353 
IsLaneModuleError(int32_t errcode)354 static bool IsLaneModuleError(int32_t errcode)
355 {
356     if (errcode >= SOFTBUS_LANE_ERR_BASE && errcode < SOFTBUS_CONN_ERR_BASE) {
357         return true;
358     }
359     return false;
360 }
361 
TransFreeLaneInner(uint32_t laneHandle,bool isQosLane,bool isAsync)362 static void TransFreeLaneInner(uint32_t laneHandle, bool isQosLane, bool isAsync)
363 {
364     if (isQosLane) {
365         TransFreeLaneByLaneHandle(laneHandle, isAsync);
366     } else {
367         LnnFreeLane(laneHandle);
368     }
369 }
370 
TransOpenChannel(const SessionParam * param,TransInfo * transInfo)371 int32_t TransOpenChannel(const SessionParam *param, TransInfo *transInfo)
372 {
373     if (param == NULL || transInfo == NULL) {
374         TRANS_LOGE(TRANS_CTRL, "param invalid");
375         return SOFTBUS_INVALID_PARAM;
376     }
377     if (GetDeniedFlagByPeer(param->sessionName, param->peerSessionName, param->peerDeviceId)) {
378         TRANS_LOGE(TRANS_CTRL, "request denied: sessionId=%{public}d", param->sessionId);
379         return SOFTBUS_TRANS_BIND_REQUEST_DENIED;
380     }
381     char *tmpName = NULL;
382     Anonymize(param->sessionName, &tmpName);
383     TRANS_LOGI(TRANS_CTRL, "server TransOpenChannel, sessionName=%{public}s, socket=%{public}d, actionId=%{public}d, "
384                            "isQosLane=%{public}d, isAsync=%{public}d",
385         AnonymizeWrapper(tmpName), param->sessionId, param->actionId, param->isQosLane, param->isAsync);
386     AnonymizeFree(tmpName);
387     int32_t ret = INVALID_CHANNEL_ID;
388     uint32_t laneHandle = INVALID_LANE_REQ_ID;
389     CoreSessionState state = CORE_SESSION_STATE_INIT;
390     AppInfo *appInfo = (AppInfo *)SoftBusCalloc(sizeof(AppInfo));
391     TRANS_CHECK_AND_RETURN_RET_LOGE(appInfo != NULL, SOFTBUS_MALLOC_ERR, TRANS_CTRL, "calloc appInfo failed.");
392     ret = TransCommonGetAppInfo(param, appInfo);
393     if (ret != SOFTBUS_OK) {
394         TRANS_LOGE(TRANS_CTRL, "get appinfo failed");
395         TransFreeAppInfo(appInfo);
396         return ret;
397     }
398     ret = TransAddSocketChannelInfo(
399         param->sessionName, param->sessionId, INVALID_CHANNEL_ID, CHANNEL_TYPE_UNDEFINED, CORE_SESSION_STATE_INIT);
400     if (ret != SOFTBUS_OK) {
401         TRANS_LOGE(TRANS_CTRL, "Add socket channel record failed.");
402         TransFreeAppInfo(appInfo);
403         return ret;
404     }
405     NodeInfo nodeInfo;
406     (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
407     int32_t peerRet = LnnGetRemoteNodeInfoById(appInfo->peerNetWorkId, CATEGORY_NETWORK_ID, &nodeInfo);
408     appInfo->osType = nodeInfo.deviceInfo.osType;
409     TransEventExtra extra;
410     (void)memset_s(&extra, sizeof(TransEventExtra), 0, sizeof(TransEventExtra));
411     TransBuildTransOpenChannelStartEvent(&extra, appInfo, &nodeInfo, peerRet);
412     TransSetFirstTokenInfo(appInfo, &extra);
413     TransSetQosInfo(param->qos, param->qosCount, &extra);
414     extra.sessionId = param->sessionId;
415     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_START, extra);
416     if (param->isQosLane) {
417         uint32_t callingTokenId = TransACLGetCallingTokenID();
418         ret = TransAsyncGetLaneInfo(param, &laneHandle, callingTokenId, appInfo->timeStart);
419         if (ret != SOFTBUS_OK) {
420             Anonymize(param->sessionName, &tmpName);
421             TRANS_LOGE(TRANS_CTRL, "Async get Lane failed, sessionName=%{public}s, sessionId=%{public}d",
422                 AnonymizeWrapper(tmpName), param->sessionId);
423             AnonymizeFree(tmpName);
424             if (ret != SOFTBUS_TRANS_STOP_BIND_BY_CANCEL) {
425                 TransFreeLaneInner(laneHandle, param->isQosLane, param->isAsync);
426             }
427             (void)TransDeleteSocketChannelInfoBySession(param->sessionName, param->sessionId);
428         }
429         TransFreeAppInfo(appInfo);
430         return ret;
431     }
432     transInfo->channelId = INVALID_CHANNEL_ID;
433     transInfo->channelType = CHANNEL_TYPE_BUTT;
434     LaneConnInfo connInfo;
435     ConnectOption connOpt;
436     (void)memset_s(&connOpt, sizeof(ConnectOption), 0, sizeof(ConnectOption));
437     ret = TransGetLaneInfo(param, &connInfo, &laneHandle);
438     if (ret != SOFTBUS_OK) {
439         SoftbusReportTransErrorEvt(SOFTBUS_TRANS_GET_LANE_INFO_ERR);
440         goto EXIT_ERR;
441     }
442     Anonymize(param->sessionName, &tmpName);
443     TRANS_LOGI(TRANS_CTRL,
444         "sessionName=%{public}s, socket=%{public}d, laneHandle=%{public}u, linkType=%{public}u.",
445         AnonymizeWrapper(tmpName), param->sessionId, laneHandle, connInfo.type);
446     AnonymizeFree(tmpName);
447     ret = TransGetConnectOptByConnInfo(&connInfo, &connOpt);
448     if (ret != SOFTBUS_OK) {
449         SoftbusRecordOpenSessionKpi(appInfo->myData.pkgName, connInfo.type,
450             SOFTBUS_EVT_OPEN_SESSION_FAIL, GetSoftbusRecordTimeMillis() - appInfo->timeStart);
451         goto EXIT_ERR;
452     }
453     appInfo->connectType = connOpt.type;
454     extra.linkType = connOpt.type;
455     extra.deviceState = TransGetDeviceState(param->peerDeviceId);
456     FillAppInfo(appInfo, param, transInfo, &connInfo);
457     TransOpenChannelSetModule(transInfo->channelType, &connOpt);
458     TRANS_LOGI(TRANS_CTRL, "laneHandle=%{public}u, channelType=%{public}u", laneHandle, transInfo->channelType);
459     TransGetSocketChannelStateBySession(param->sessionName, param->sessionId, &state);
460     if (state == CORE_SESSION_STATE_CANCELLING) {
461         goto EXIT_CANCEL;
462     }
463     TransSetSocketChannelStateBySession(param->sessionName, param->sessionId, CORE_SESSION_STATE_LAN_COMPLETE);
464     ret = TransOpenChannelProc((ChannelType)transInfo->channelType, appInfo, &connOpt,
465         &(transInfo->channelId));
466     (void)memset_s(appInfo->sessionKey, sizeof(appInfo->sessionKey), 0, sizeof(appInfo->sessionKey));
467     if (ret != SOFTBUS_OK) {
468         SoftbusReportTransErrorEvt(SOFTBUS_TRANS_CREATE_CHANNEL_ERR);
469         SoftbusRecordOpenSessionKpi(appInfo->myData.pkgName,
470             appInfo->linkType, SOFTBUS_EVT_OPEN_SESSION_FAIL, GetSoftbusRecordTimeMillis() - appInfo->timeStart);
471         goto EXIT_ERR;
472     }
473     if (TransUpdateSocketChannelInfoBySession(
474         param->sessionName, param->sessionId, transInfo->channelId, transInfo->channelType) != SOFTBUS_OK) {
475         SoftbusRecordOpenSessionKpi(appInfo->myData.pkgName, appInfo->linkType, SOFTBUS_EVT_OPEN_SESSION_FAIL,
476             GetSoftbusRecordTimeMillis() - appInfo->timeStart);
477         TransCloseChannel(NULL, transInfo->channelId, transInfo->channelType);
478         goto EXIT_ERR;
479     }
480     TransSetSocketChannelStateByChannel(
481         transInfo->channelId, transInfo->channelType, CORE_SESSION_STATE_CHANNEL_OPENED);
482     if (TransLaneMgrAddLane(transInfo, &connInfo, laneHandle, param->isQosLane, &appInfo->myData) != SOFTBUS_OK) {
483         SoftbusRecordOpenSessionKpi(appInfo->myData.pkgName, appInfo->linkType, SOFTBUS_EVT_OPEN_SESSION_FAIL,
484             GetSoftbusRecordTimeMillis() - appInfo->timeStart);
485         TransCloseChannel(NULL, transInfo->channelId, transInfo->channelType);
486         goto EXIT_ERR;
487     }
488     AddChannelStatisticsInfo(transInfo->channelId, transInfo->channelType);
489     TransFreeAppInfo(appInfo);
490     TRANS_LOGI(TRANS_CTRL,
491         "server TransOpenChannel ok: socket=%{public}d, channelId=%{public}d, channelType=%{public}d, "
492         "laneHandle=%{public}u",
493         param->sessionId, transInfo->channelId, transInfo->channelType, laneHandle);
494     return SOFTBUS_OK;
495 EXIT_ERR:
496     extra.linkType = IsLaneModuleError(ret) ? extra.linkType : CONNECT_HML;
497     TransBuildTransOpenChannelEndEvent(&extra, transInfo, appInfo->timeStart, ret);
498     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_END, extra);
499     TransAlarmExtra extraAlarm;
500     TransBuildTransAlarmEvent(&extraAlarm, appInfo, ret);
501     TRANS_ALARM(OPEN_SESSION_FAIL_ALARM, CONTROL_ALARM_TYPE, extraAlarm);
502     TransFreeAppInfo(appInfo);
503     if (ret != SOFTBUS_TRANS_STOP_BIND_BY_CANCEL || laneHandle != INVALID_LANE_REQ_ID) {
504         TransFreeLaneInner(laneHandle, param->isQosLane, param->isAsync);
505     }
506     (void)TransDeleteSocketChannelInfoBySession(param->sessionName, param->sessionId);
507     TRANS_LOGE(TRANS_SVC, "server TransOpenChannel err, socket=%{public}d, ret=%{public}d", param->sessionId, ret);
508     return ret;
509 EXIT_CANCEL:
510     TransBuildTransOpenChannelCancelEvent(&extra, transInfo, appInfo->timeStart, SOFTBUS_TRANS_STOP_BIND_BY_CANCEL);
511     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_END, extra);
512     TransFreeAppInfo(appInfo);
513     TransFreeLaneInner(laneHandle, param->isQosLane, param->isAsync);
514     (void)TransDeleteSocketChannelInfoBySession(param->sessionName, param->sessionId);
515     TRANS_LOGE(TRANS_SVC, "server open channel cancel, socket=%{public}d", param->sessionId);
516     return SOFTBUS_TRANS_STOP_BIND_BY_CANCEL;
517 }
518 
GetAuthAppInfo(const char * mySessionName)519 static AppInfo *GetAuthAppInfo(const char *mySessionName)
520 {
521     TRANS_LOGD(TRANS_CTRL, "enter.");
522     AppInfo *appInfo = (AppInfo *)SoftBusCalloc(sizeof(AppInfo));
523     if (appInfo == NULL) {
524         return NULL;
525     }
526     appInfo->appType = APP_TYPE_AUTH;
527     appInfo->myData.apiVersion = API_V2;
528     appInfo->autoCloseTime = 0;
529     appInfo->businessType = BUSINESS_TYPE_BYTE;
530     appInfo->channelType = CHANNEL_TYPE_AUTH;
531     appInfo->timeStart = GetSoftbusRecordTimeMillis();
532     appInfo->isClient = true;
533     if (TransGetUidAndPid(mySessionName, &appInfo->myData.uid, &appInfo->myData.pid) != SOFTBUS_OK) {
534         TRANS_LOGE(TRANS_CTRL, "GetAuthAppInfo GetUidAndPid failed");
535         goto EXIT_ERR;
536     }
537     if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, appInfo->myData.deviceId,
538         sizeof(appInfo->myData.deviceId)) != SOFTBUS_OK) {
539         TRANS_LOGE(TRANS_CTRL, "GetAuthAppInfo get deviceId failed");
540         goto EXIT_ERR;
541     }
542     if (strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), mySessionName) != EOK) {
543         TRANS_LOGE(TRANS_CTRL, "GetAuthAppInfo strcpy_s mySessionName failed");
544         goto EXIT_ERR;
545     }
546     if (strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), mySessionName) != EOK) {
547         TRANS_LOGE(TRANS_CTRL, "GetAuthAppInfo strcpy_s peerSessionName failed");
548         goto EXIT_ERR;
549     }
550     if (TransGetPkgNameBySessionName(mySessionName, appInfo->myData.pkgName, PKG_NAME_SIZE_MAX) != SOFTBUS_OK) {
551         TRANS_LOGE(TRANS_CTRL, "GetAuthAppInfo get PkgName failed");
552         goto EXIT_ERR;
553     }
554     if (TransCommonGetLocalConfig(appInfo->channelType, appInfo->businessType, &appInfo->myData.dataConfig) !=
555         SOFTBUS_OK) {
556         TRANS_LOGE(TRANS_CTRL, "GetAuthAppInfo get local data config failed");
557         goto EXIT_ERR;
558     }
559 
560     TRANS_LOGD(TRANS_CTRL, "ok");
561     return appInfo;
562 EXIT_ERR:
563     SoftBusFree(appInfo);
564     return NULL;
565 }
566 
TransOpenAuthChannel(const char * sessionName,const ConnectOption * connOpt,const char * reqId,const ConnectParam * param)567 int32_t TransOpenAuthChannel(const char *sessionName, const ConnectOption *connOpt,
568     const char *reqId, const ConnectParam *param)
569 {
570     int32_t channelId = INVALID_CHANNEL_ID;
571     if (!IsValidString(sessionName, SESSION_NAME_SIZE_MAX) || connOpt == NULL || param == NULL) {
572         return channelId;
573     }
574     char callerPkg[PKG_NAME_SIZE_MAX] = {0};
575     char localUdid[UDID_BUF_LEN] = {0};
576     TransEventExtra extra;
577     (void)memset_s(&extra, sizeof(TransEventExtra), 0, sizeof(TransEventExtra));
578     TransBuildOpenAuthChannelStartEvent(&extra, sessionName, connOpt, localUdid, callerPkg);
579     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_START, extra);
580     if (connOpt->type == CONNECT_TCP) {
581         if (TransOpenAuthMsgChannel(sessionName, connOpt, &channelId, reqId) != SOFTBUS_OK) {
582             goto EXIT_ERR;
583         }
584     } else if (connOpt->type == CONNECT_BR || connOpt->type == CONNECT_BLE) {
585         AppInfo *appInfo = GetAuthAppInfo(sessionName);
586         if (appInfo == NULL) {
587             TRANS_LOGE(TRANS_CTRL, "GetAuthAppInfo failed");
588             goto EXIT_ERR;
589         }
590         appInfo->blePriority = param->blePriority;
591         appInfo->connectType = connOpt->type;
592         if (strcpy_s(appInfo->reqId, REQ_ID_SIZE_MAX, reqId) != EOK) {
593             TRANS_LOGE(TRANS_CTRL, "strcpy_s reqId failed");
594             SoftBusFree(appInfo);
595             goto EXIT_ERR;
596         }
597         if (TransProxyOpenProxyChannel(appInfo, connOpt, &channelId) != SOFTBUS_OK) {
598             TRANS_LOGE(TRANS_CTRL, "proxy channel err");
599             SoftBusFree(appInfo);
600             goto EXIT_ERR;
601         }
602         SoftBusFree(appInfo);
603     } else {
604         goto EXIT_ERR;
605     }
606     return channelId;
607 EXIT_ERR:
608     extra.result = EVENT_STAGE_RESULT_FAILED;
609     extra.errcode = SOFTBUS_TRANS_AUTH_CHANNEL_NOT_FOUND;
610     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_END, extra);
611     return INVALID_CHANNEL_ID;
612 }
613 
MergeStatsInterval(const uint32_t * data,uint32_t left,uint32_t right)614 static uint32_t MergeStatsInterval(const uint32_t *data, uint32_t left, uint32_t right)
615 {
616     uint32_t result = 0;
617     while (left <= right) {
618         result += data[left];
619         left++;
620     }
621     return result;
622 }
623 
ConvertStreamStats(const StreamSendStats * src,FrameSendStats * dest)624 static void ConvertStreamStats(const StreamSendStats *src, FrameSendStats *dest)
625 {
626     uint32_t *srcCostCnt = (uint32_t *)(src->costTimeStatsCnt);
627     uint32_t *srcBitRate = (uint32_t *)(src->sendBitRateStatsCnt);
628     uint32_t *destCostCnt = dest->costTimeStatsCnt;
629     uint32_t *destBitRate = dest->sendBitRateStatsCnt;
630     destCostCnt[FRAME_COST_TIME_SMALL] = srcCostCnt[FRAME_COST_LT10MS];
631     destCostCnt[FRAME_COST_TIME_MEDIUM] = MergeStatsInterval(srcCostCnt, FRAME_COST_LT30MS, FRAME_COST_LT100MS);
632     destCostCnt[FRAME_COST_TIME_LARGE] = srcCostCnt[FRAME_COST_LT120MS] + srcCostCnt[FRAME_COST_GE120MS];
633     destBitRate[FRAME_BIT_RATE_SMALL] = srcBitRate[FRAME_BIT_RATE_LT3M];
634     destBitRate[FRAME_BIT_RATE_MEDIUM] = MergeStatsInterval(srcBitRate, FRAME_BIT_RATE_LT6M, FRAME_BIT_RATE_LT30M);
635     destBitRate[FRAME_BIT_RATE_LARGE] = srcBitRate[FRAME_BIT_RATE_GE30M];
636 }
637 
TransStreamStats(int32_t channelId,int32_t channelType,const StreamSendStats * data)638 int32_t TransStreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
639 {
640     (void)channelType;
641     if (data == NULL) {
642         TRANS_LOGE(TRANS_STREAM, "streamStats data is null");
643         return SOFTBUS_INVALID_PARAM;
644     }
645     uint32_t laneHandle;
646     int32_t ret = TransGetLaneHandleByChannelId(channelId, &laneHandle);
647     if (ret != SOFTBUS_OK) {
648         TRANS_LOGE(TRANS_STREAM, "get laneHandle fail, streamStatsInfo cannot be processed");
649         return ret;
650     }
651     TRANS_LOGI(TRANS_STREAM, "transStreamStats channelId=%{public}d, laneHandle=0x%{public}x", channelId, laneHandle);
652     // modify with laneId
653     uint64_t laneId = INVALID_LANE_ID;
654     LaneIdStatsInfo info;
655     (void)memset_s(&info, sizeof(info), 0, sizeof(info));
656     info.laneId = laneId;
657     info.statsType = LANE_T_COMMON_VIDEO;
658     FrameSendStats *stats = &info.statsInfo.stream.frameStats;
659     ConvertStreamStats(data, stats);
660     LnnReportLaneIdStatsInfo(&info, 1); /* only report stream stats */
661     return SOFTBUS_OK;
662 }
663 
TransRequestQos(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)664 int32_t TransRequestQos(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
665 {
666     (void)chanType;
667     (void)appType;
668     uint32_t laneHandle;
669     int32_t ret = TransGetLaneHandleByChannelId(channelId, &laneHandle);
670     if (ret != SOFTBUS_OK) {
671         TRANS_LOGE(TRANS_QOS, "get laneHandle fail, transRequestQos cannot be processed");
672         return ret;
673     }
674     // modify with laneId
675     uint64_t laneId = INVALID_LANE_ID;
676     int32_t result = 0;
677     if (quality == QOS_IMPROVE) {
678         TRANS_LOGI(TRANS_QOS, "trans requestQos");
679         ret = LnnRequestQosOptimization(&laneId, 1, &result, 1);
680     } else if (quality == QOS_RECOVER) {
681         TRANS_LOGI(TRANS_QOS, "trans cancel Qos");
682         LnnCancelQosOptimization(&laneId, 1);
683         ret = SOFTBUS_OK;
684     } else {
685         TRANS_LOGE(TRANS_QOS, "requestQos invalid. quality=%{public}d", quality);
686         ret = SOFTBUS_TRANS_REQUEST_QOS_INVALID;
687     }
688 
689     if (ret != SOFTBUS_OK) {
690         TRANS_LOGE(TRANS_QOS, "request Qos fail, quality=%{public}d, ret=%{public}d", quality, ret);
691         return SOFTBUS_TRANS_REQUEST_QOS_FAILED;
692     }
693     return SOFTBUS_OK;
694 }
695 
TransRippleStats(int32_t channelId,int32_t channelType,const TrafficStats * data)696 int32_t TransRippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
697 {
698     (void)channelType;
699     if (data == NULL) {
700         TRANS_LOGE(TRANS_CTRL, "rippleStats data is null");
701         return SOFTBUS_INVALID_PARAM;
702     }
703     uint32_t laneHandle;
704     int32_t ret = TransGetLaneHandleByChannelId(channelId, &laneHandle);
705     if (ret != SOFTBUS_OK) {
706         TRANS_LOGE(TRANS_CTRL, "get laneHandle fail, streamStatsInfo cannot be processed, ret=%{public}d", ret);
707         return ret;
708     }
709     TRANS_LOGI(TRANS_CTRL, "transRippleStats channelId=%{public}d, laneHandle=0x%{public}x", channelId, laneHandle);
710     LnnRippleData rptdata;
711     (void)memset_s(&rptdata, sizeof(rptdata), 0, sizeof(rptdata));
712     if (memcpy_s(&rptdata.stats, sizeof(rptdata.stats), data->stats, sizeof(data->stats)) != EOK) {
713         TRANS_LOGE(TRANS_CTRL, "memcpy fail");
714         return SOFTBUS_MEM_ERR;
715     }
716     // modify with laneId
717     uint64_t laneId = INVALID_LANE_ID;
718     LnnReportRippleData(laneId, &rptdata);
719     return SOFTBUS_OK;
720 }
721 
TransNotifyAuthSuccess(int32_t channelId,int32_t channelType)722 int32_t TransNotifyAuthSuccess(int32_t channelId, int32_t channelType)
723 {
724     int32_t ret = SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
725     ConnectOption connOpt;
726     switch (channelType) {
727         case CHANNEL_TYPE_AUTH:
728             ret = TransAuthGetConnOptionByChanId(channelId, &connOpt);
729             break;
730         case CHANNEL_TYPE_PROXY:
731             ret = TransProxyGetConnOptionByChanId(channelId, &connOpt);
732             break;
733         default:
734             ret = SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
735             TRANS_LOGE(TRANS_CTRL, "invalid. channelId=%{public}d, channelType=%{public}d.", channelId, channelType);
736     }
737     if (ret != SOFTBUS_OK) {
738         TRANS_LOGE(TRANS_CTRL,
739             "notfiy auth success. channelId=%{public}d, channelType=%{public}d, ret=%{public}d",
740             channelId, channelType, ret);
741         return ret;
742     }
743     return TransNotifyAuthDataSuccess(channelId, &connOpt);
744 }
745 
TransReleaseUdpResources(int32_t channelId)746 int32_t TransReleaseUdpResources(int32_t channelId)
747 {
748     TRANS_LOGI(TRANS_CTRL, "release Udp channel resources: channelId=%{public}d", channelId);
749     NotifyQosChannelClosed(channelId, CHANNEL_TYPE_UDP);
750     (void)TransLaneMgrDelLane(channelId, CHANNEL_TYPE_UDP, false);
751     (void)TransDelUdpChannel(channelId);
752     return SOFTBUS_OK;
753 }
754 
TransCloseChannel(const char * sessionName,int32_t channelId,int32_t channelType)755 int32_t TransCloseChannel(const char *sessionName, int32_t channelId, int32_t channelType)
756 {
757     int32_t ret = TransCommonCloseChannel(sessionName, channelId, channelType);
758     if (IsTdcRecoveryTransLimit() && IsUdpRecoveryTransLimit()) {
759         UdpChannelFileTransRecoveryLimit(FILE_PRIORITY_BE);
760     }
761     return ret;
762 }
763 
TransCloseChannelWithStatistics(int32_t channelId,int32_t channelType,uint64_t laneId,const void * dataInfo,uint32_t len)764 int32_t TransCloseChannelWithStatistics(int32_t channelId, int32_t channelType, uint64_t laneId,
765     const void *dataInfo, uint32_t len)
766 {
767     UpdateNetworkResourceByLaneId(channelId, channelType, laneId, dataInfo, len);
768     return SOFTBUS_OK;
769 }
770 
TransSendMsg(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)771 int32_t TransSendMsg(int32_t channelId, int32_t channelType, const void *data, uint32_t len,
772     int32_t msgType)
773 {
774     int32_t ret = SOFTBUS_OK;
775     switch (channelType) {
776         case CHANNEL_TYPE_AUTH:
777             TRANS_LOGI(TRANS_MSG,
778                 "send msg auth channelType. channelId=%{public}d, channelType=%{public}d", channelId, channelType);
779             ret = TransSendAuthMsg(channelId, (char*)data, (int32_t)len);
780             break;
781         case CHANNEL_TYPE_PROXY:
782             TRANS_LOGD(TRANS_MSG,
783                 "send msg proxy channelType. channelId=%{public}d, channelType=%{public}d", channelId, channelType);
784             ret = TransProxyPostSessionData(channelId, (unsigned char*)data, len, (SessionPktType)msgType);
785             break;
786         default:
787             TRANS_LOGE(TRANS_MSG,
788                 "send msg invalid channelType. channelId=%{public}d, channelType=%{public}d", channelId, channelType);
789             ret = SOFTBUS_TRANS_CHANNEL_TYPE_INVALID;
790             break;
791     }
792     return ret;
793 }
794 
TransChannelDeathCallback(const char * pkgName,int32_t pid)795 void TransChannelDeathCallback(const char *pkgName, int32_t pid)
796 {
797     TransProxyDeathCallback(pkgName, pid);
798     TransTdcDeathCallback(pkgName, pid);
799     TransTdcChannelInfoDeathCallback(pkgName, pid);
800     TransLaneMgrDeathCallback(pkgName, pid);
801     TransUdpDeathCallback(pkgName, pid);
802     TransAuthDeathCallback(pkgName, pid);
803 }
804 
TransGetNameByChanId(const TransInfo * info,char * pkgName,char * sessionName,uint16_t pkgLen,uint16_t sessionNameLen)805 int32_t TransGetNameByChanId(const TransInfo *info, char *pkgName, char *sessionName,
806     uint16_t pkgLen, uint16_t sessionNameLen)
807 {
808     if (info == NULL || pkgName == NULL || sessionName == NULL) {
809         TRANS_LOGE(TRANS_CTRL, "invalid param");
810         return SOFTBUS_INVALID_PARAM;
811     }
812     switch ((ChannelType)info->channelType) {
813         case CHANNEL_TYPE_PROXY:
814             return TransProxyGetNameByChanId(info->channelId, pkgName, sessionName, pkgLen, sessionNameLen);
815         case CHANNEL_TYPE_UDP:
816             return TransUdpGetNameByChanId(info->channelId, pkgName, sessionName, pkgLen, sessionNameLen);
817         case CHANNEL_TYPE_AUTH:
818             return TransAuthGetNameByChanId(info->channelId, pkgName, sessionName, pkgLen, sessionNameLen);
819         default:
820             return SOFTBUS_INVALID_PARAM;
821     }
822 }
823 
TransGetAndComparePid(pid_t pid,int32_t channelId,int32_t channelType)824 int32_t TransGetAndComparePid(pid_t pid, int32_t channelId, int32_t channelType)
825 {
826     int32_t curChannelPid;
827     int32_t ret = SOFTBUS_OK;
828     if ((ChannelType)channelType == CHANNEL_TYPE_TCP_DIRECT) {
829         ret = TransGetPidByChanId(channelId, channelType, &curChannelPid);
830         if (ret != SOFTBUS_OK) {
831             TRANS_LOGE(TRANS_CTRL, "get pid by channelId failed, channelId=%{public}d, ret=%{public}d", channelId, ret);
832             return ret;
833         }
834     } else {
835         AppInfo appInfo;
836         ret = TransGetAppInfoByChanId(channelId, channelType, &appInfo);
837         (void)memset_s(appInfo.sessionKey, sizeof(appInfo.sessionKey), 0, sizeof(appInfo.sessionKey));
838         if (ret != SOFTBUS_OK) {
839             TRANS_LOGE(TRANS_CTRL, "get appInfo by channelId failed, channelId=%{public}d, ret=%{public}d",
840                 channelId, ret);
841             return ret;
842         }
843         curChannelPid = appInfo.myData.pid;
844     }
845     if (pid != (pid_t)curChannelPid) {
846         TRANS_LOGE(TRANS_CTRL, "callingPid=%{public}d not equal curChannelPid=%{public}d", pid, curChannelPid);
847         return SOFTBUS_TRANS_CHECK_PID_ERROR;
848     }
849     return SOFTBUS_OK;
850 }
851 
TransGetAndComparePidBySession(pid_t pid,const char * sessionName,int32_t sessionlId)852 int32_t TransGetAndComparePidBySession(pid_t pid, const char *sessionName, int32_t sessionlId)
853 {
854     pid_t curSessionPid;
855     int32_t ret = TransGetPidFromSocketChannelInfoBySession(sessionName, sessionlId, &curSessionPid);
856     if (ret != SOFTBUS_OK) {
857         TRANS_LOGE(TRANS_CTRL, "get pid by session failed, ret=%{public}d", ret);
858         return ret;
859     }
860     if (pid != curSessionPid) {
861         TRANS_LOGE(TRANS_CTRL, "callingPid=%{public}d not equal curSessionPid=%{public}d", pid, curSessionPid);
862         return SOFTBUS_TRANS_CHECK_PID_ERROR;
863     }
864     return SOFTBUS_OK;
865 }
866 
TransGetAppInfoByChanId(int32_t channelId,int32_t channelType,AppInfo * appInfo)867 int32_t TransGetAppInfoByChanId(int32_t channelId, int32_t channelType, AppInfo* appInfo)
868 {
869     if (appInfo == NULL) {
870         return SOFTBUS_INVALID_PARAM;
871     }
872     switch ((ChannelType)channelType) {
873         case CHANNEL_TYPE_TCP_DIRECT:
874             return TcpTranGetAppInfobyChannelId(channelId, appInfo);
875         case CHANNEL_TYPE_PROXY:
876             return TransProxyGetAppInfoByChanId(channelId, appInfo);
877         case CHANNEL_TYPE_UDP:
878             return TransGetUdpAppInfoByChannelId(channelId, appInfo);
879         case CHANNEL_TYPE_AUTH:
880             return TransAuthGetAppInfoByChanId(channelId, appInfo);
881         default:
882             return SOFTBUS_INVALID_PARAM;
883     }
884 }
885 
TransGetConnByChanId(int32_t channelId,int32_t channelType,int32_t * connId)886 int32_t TransGetConnByChanId(int32_t channelId, int32_t channelType, int32_t* connId)
887 {
888     int32_t ret;
889 
890     switch (channelType) {
891         case CHANNEL_TYPE_PROXY:
892             ret = TransProxyGetConnIdByChanId(channelId, connId);
893             break;
894         case CHANNEL_TYPE_AUTH:
895             ret = TransAuthGetConnIdByChanId(channelId, connId);
896             break;
897         default:
898             TRANS_LOGE(TRANS_CTRL, "channelType=%{public}d error", channelType);
899             ret = SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
900     }
901     if (ret != SOFTBUS_OK) {
902         TRANS_LOGE(TRANS_MSG, "get connId failed, channelId=%{public}d, channelType=%{public}d",
903             channelId, channelType);
904     }
905 
906     return ret;
907 }
908 
CheckAuthChannelIsExit(ConnectOption * connInfo)909 int32_t CheckAuthChannelIsExit(ConnectOption *connInfo)
910 {
911     if (connInfo == NULL) {
912         TRANS_LOGE(TRANS_CTRL, "invalid param");
913         return SOFTBUS_INVALID_PARAM;
914     }
915 
916     int32_t ret = SOFTBUS_TRANS_NOT_MATCH;
917     if (connInfo->type == CONNECT_TCP) {
918         ret = CheckIsWifiAuthChannel(connInfo);
919     } else if (connInfo->type == CONNECT_BR || connInfo->type == CONNECT_BLE) {
920         ret = CheckIsProxyAuthChannel(connInfo);
921     }
922     TRANS_LOGW(TRANS_CTRL, "connInfo type=%{public}d, ret=%{public}d", connInfo->type, ret);
923     return ret;
924 }
925 
GetChannelInfoFromBuf(uint8_t * buf,int32_t * channelId,int32_t * channelType,int32_t * openResult,uint32_t len)926 static int32_t GetChannelInfoFromBuf(
927     uint8_t *buf, int32_t *channelId, int32_t *channelType, int32_t *openResult, uint32_t len)
928 {
929     int32_t offSet = 0;
930     int32_t ret = SOFTBUS_OK;
931     ret = ReadInt32FromBuf(buf, len, &offSet, channelId);
932     if (ret != SOFTBUS_OK) {
933         TRANS_LOGE(TRANS_CTRL, "get channelId from buf failed!");
934         return ret;
935     }
936     ret = ReadInt32FromBuf(buf, len, &offSet, channelType);
937     if (ret != SOFTBUS_OK) {
938         TRANS_LOGE(TRANS_CTRL, "get channelId from buf failed!");
939         return ret;
940     }
941     ret = ReadInt32FromBuf(buf, len, &offSet, openResult);
942     if (ret != SOFTBUS_OK) {
943         TRANS_LOGE(TRANS_CTRL, "get openResult from buf failed!");
944         return ret;
945     }
946     return ret;
947 }
948 
GetUdpChannelInfoFromBuf(uint8_t * buf,int32_t * channelId,int32_t * channelType,int32_t * openResult,int32_t * udpPort,uint32_t len)949 static int32_t GetUdpChannelInfoFromBuf(
950     uint8_t *buf, int32_t *channelId, int32_t *channelType, int32_t *openResult, int32_t *udpPort, uint32_t len)
951 {
952     int32_t offSet = 0;
953     int32_t ret = SOFTBUS_OK;
954     ret = ReadInt32FromBuf(buf, len, &offSet, channelId);
955     if (ret != SOFTBUS_OK) {
956         TRANS_LOGE(TRANS_CTRL, "get channelId from buf failed!");
957         return ret;
958     }
959     ret = ReadInt32FromBuf(buf, len, &offSet, channelType);
960     if (ret != SOFTBUS_OK) {
961         TRANS_LOGE(TRANS_CTRL, "get channelId from buf failed!");
962         return ret;
963     }
964     ret = ReadInt32FromBuf(buf, len, &offSet, openResult);
965     if (ret != SOFTBUS_OK) {
966         TRANS_LOGE(TRANS_CTRL, "get openResult from buf failed!");
967         return ret;
968     }
969     ret = ReadInt32FromBuf(buf, len, &offSet, udpPort);
970     if (ret != SOFTBUS_OK) {
971         TRANS_LOGE(TRANS_CTRL, "get udpPort from buf failed!");
972         return ret;
973     }
974     return ret;
975 }
976 
GetLimitChangeInfoFromBuf(uint8_t * buf,int32_t * channelId,uint8_t * tos,int32_t * limitChangeResult,uint32_t len)977 static int32_t GetLimitChangeInfoFromBuf(
978     uint8_t *buf, int32_t *channelId, uint8_t *tos, int32_t *limitChangeResult, uint32_t len)
979 {
980     int32_t offSet = 0;
981     int32_t ret = SOFTBUS_OK;
982     ret = ReadInt32FromBuf(buf, len, &offSet, channelId);
983     if (ret != SOFTBUS_OK) {
984         TRANS_LOGE(TRANS_CTRL, "get channelId from buf failed!");
985         return ret;
986     }
987     ret = ReadUint8FromBuf(buf, len, &offSet, tos);
988     if (ret != SOFTBUS_OK) {
989         TRANS_LOGE(TRANS_CTRL, "get tos from buf failed!");
990         return ret;
991     }
992     ret = ReadInt32FromBuf(buf, len, &offSet, limitChangeResult);
993     if (ret != SOFTBUS_OK) {
994         TRANS_LOGE(TRANS_CTRL, "get limitChangeResult from buf failed!");
995         return ret;
996     }
997     return ret;
998 }
999 
TransReportChannelOpenedInfo(uint8_t * buf,uint32_t len)1000 static int32_t TransReportChannelOpenedInfo(uint8_t *buf, uint32_t len)
1001 {
1002     int32_t channelId = 0;
1003     int32_t channelType = 0;
1004     int32_t openResult = 0;
1005     int32_t udpPort = 0;
1006     int32_t ret = SOFTBUS_OK;
1007     if (len == sizeof(int32_t) * REPORT_UDP_INFO_SIZE) {
1008         ret = GetUdpChannelInfoFromBuf(buf, &channelId, &channelType, &openResult, &udpPort, len);
1009     } else {
1010         ret = GetChannelInfoFromBuf(buf, &channelId, &channelType, &openResult, len);
1011     }
1012     if (ret != SOFTBUS_OK) {
1013         return ret;
1014     }
1015     switch (channelType) {
1016         case CHANNEL_TYPE_PROXY:
1017             ret = TransDealProxyChannelOpenResult(channelId, openResult);
1018             break;
1019         case CHANNEL_TYPE_TCP_DIRECT:
1020             ret = TransDealTdcChannelOpenResult(channelId, openResult);
1021             break;
1022         case CHANNEL_TYPE_UDP:
1023             ret = TransDealUdpChannelOpenResult(channelId, openResult, udpPort);
1024             break;
1025         case CHANNEL_TYPE_AUTH:
1026             ret = TransDealAuthChannelOpenResult(channelId, openResult);
1027             break;
1028         default:
1029             TRANS_LOGE(TRANS_CTRL, "channelType=%{public}d is error", channelType);
1030             ret = SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
1031     }
1032     if (ret != SOFTBUS_OK) {
1033         TRANS_LOGE(TRANS_CTRL, "report Event channel opened info failed");
1034     }
1035     return ret;
1036 }
1037 
TransReportLimitChangeInfo(uint8_t * buf,uint32_t len)1038 static void TransReportLimitChangeInfo(uint8_t *buf, uint32_t len)
1039 {
1040     int32_t channelId = 0;
1041     uint8_t tos = 0;
1042     int32_t limitChangeResult = 0;
1043     int32_t ret = SOFTBUS_OK;
1044     ret = GetLimitChangeInfoFromBuf(buf, &channelId, &tos, &limitChangeResult, len);
1045     if (ret != SOFTBUS_OK) {
1046         TRANS_LOGE(TRANS_CTRL, "GetLimitChangeInfoFromBuf failed, ret=%{public}d", ret);
1047     }
1048     if (limitChangeResult != SOFTBUS_OK) {
1049         TRANS_LOGE(TRANS_CTRL, "limitChangeResult is failed, limitChangeResult=%{public}d", limitChangeResult);
1050     }
1051     ret = TransSetTos(channelId, tos);
1052     if (ret != SOFTBUS_OK) {
1053         TRANS_LOGE(TRANS_CTRL, "Set limit change event failed, ret=%{public}d", ret);
1054     }
1055 }
1056 
GetCollabCheckResultFromBuf(uint8_t * buf,int32_t * channelId,int32_t * channelType,int32_t * checkResult,uint32_t len)1057 static int32_t GetCollabCheckResultFromBuf(uint8_t *buf,
1058     int32_t *channelId, int32_t *channelType, int32_t *checkResult, uint32_t len)
1059 {
1060     int32_t offset = 0;
1061     int32_t ret = ReadInt32FromBuf(buf, len, &offset, channelId);
1062     if (ret != SOFTBUS_OK) {
1063         TRANS_LOGE(TRANS_CTRL, "get channelId from buf failed.");
1064         return ret;
1065     }
1066     ret = ReadInt32FromBuf(buf, len, &offset, channelType);
1067     if (ret != SOFTBUS_OK) {
1068         TRANS_LOGE(TRANS_CTRL, "get channelType from buf failed.");
1069         return ret;
1070     }
1071     ret = ReadInt32FromBuf(buf, len, &offset, checkResult);
1072     if (ret != SOFTBUS_OK) {
1073         TRANS_LOGE(TRANS_CTRL, "get checkResult from buf failed.");
1074         return ret;
1075     }
1076     return ret;
1077 }
1078 
TransReportCheckCollabInfo(uint8_t * buf,uint32_t len)1079 static int32_t TransReportCheckCollabInfo(uint8_t *buf, uint32_t len)
1080 {
1081     int32_t channelId = 0;
1082     int32_t channelType = 0;
1083     int32_t checkResult = 0;
1084     int32_t ret = GetCollabCheckResultFromBuf(buf, &channelId, &channelType, &checkResult, len);
1085     if (ret != SOFTBUS_OK) {
1086         return ret;
1087     }
1088     switch (channelType) {
1089         case CHANNEL_TYPE_PROXY:
1090             ret = TransDealProxyCheckCollabResult(channelId, checkResult);
1091             break;
1092         case CHANNEL_TYPE_TCP_DIRECT:
1093             ret = TransDealTdcCheckCollabResult(channelId, checkResult);
1094             break;
1095         case CHANNEL_TYPE_UDP:
1096             ret = TransDealUdpCheckCollabResult(channelId, checkResult);
1097             break;
1098         default:
1099             TRANS_LOGE(TRANS_CTRL, "channelType=%{public}d is error.", channelType);
1100             ret = SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
1101     }
1102     return ret;
1103 }
1104 
TransProcessInnerEvent(int32_t eventType,uint8_t * buf,uint32_t len)1105 int32_t TransProcessInnerEvent(int32_t eventType, uint8_t *buf, uint32_t len)
1106 {
1107     if (buf == NULL) {
1108         TRANS_LOGE(TRANS_CTRL, "process inner event buf is null");
1109         return SOFTBUS_INVALID_PARAM;
1110     }
1111     int32_t ret = SOFTBUS_OK;
1112     switch (eventType) {
1113         case EVENT_TYPE_CHANNEL_OPENED:
1114             ret = TransReportChannelOpenedInfo(buf, len);
1115             break;
1116         case EVENT_TYPE_TRANS_LIMIT_CHANGE:
1117             TransReportLimitChangeInfo(buf, len);
1118             break;
1119         case EVENT_TYPE_COLLAB_CHECK:
1120             ret = TransReportCheckCollabInfo(buf, len);
1121             break;
1122         default:
1123             TRANS_LOGE(TRANS_CTRL, "eventType=%{public}d error", eventType);
1124             ret = SOFTBUS_TRANS_MSG_INVALID_EVENT_TYPE;
1125     }
1126     if (ret != SOFTBUS_OK) {
1127         TRANS_LOGE(TRANS_CTRL, "report event failed, eventType=%{public}d", eventType);
1128     }
1129     return ret;
1130 }
1131 
TransPrivilegeCloseChannel(uint64_t tokenId,int32_t pid,const char * peerNetworkId)1132 int32_t TransPrivilegeCloseChannel(uint64_t tokenId, int32_t pid, const char *peerNetworkId)
1133 {
1134 #define PRIVILEGE_CLOSE_OFFSET 11
1135     if (peerNetworkId == NULL) {
1136         TRANS_LOGE(TRANS_CTRL, "invalid param");
1137         return SOFTBUS_INVALID_PARAM;
1138     }
1139     ListNode privilegeCloseList;
1140     ListInit(&privilegeCloseList);
1141     (void)TransTcpGetPrivilegeCloseList(&privilegeCloseList, tokenId, pid);
1142     (void)TransProxyGetPrivilegeCloseList(&privilegeCloseList, tokenId, pid);
1143     (void)TransUdpGetPrivilegeCloseList(&privilegeCloseList, tokenId, pid);
1144     LinkDownInfo info = {
1145         .uuid = "",
1146         .udid = "",
1147         .peerIp = "",
1148         .networkId = peerNetworkId,
1149         .routeType = ROUTE_TYPE_ALL | 1 << PRIVILEGE_CLOSE_OFFSET,
1150     };
1151     PrivilegeCloseChannelInfo *pos = NULL;
1152     PrivilegeCloseChannelInfo *tmp = NULL;
1153     LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &privilegeCloseList, PrivilegeCloseChannelInfo, node) {
1154         (void)TransServerOnChannelLinkDown(pos->pkgName, pos->pid, &info);
1155         ListDelete(&(pos->node));
1156         SoftBusFree(pos);
1157     }
1158     return SOFTBUS_OK;
1159 }
1160 
PrivilegeCloseListAddItem(ListNode * privilegeCloseList,int32_t pid,const char * pkgName)1161 int32_t PrivilegeCloseListAddItem(ListNode *privilegeCloseList, int32_t pid, const char *pkgName)
1162 {
1163     if (privilegeCloseList == NULL || pkgName == NULL) {
1164         TRANS_LOGE(TRANS_CTRL, "invalid param");
1165         return SOFTBUS_INVALID_PARAM;
1166     }
1167     PrivilegeCloseChannelInfo *exitItem = NULL;
1168     LIST_FOR_EACH_ENTRY(exitItem, privilegeCloseList, PrivilegeCloseChannelInfo, node) {
1169         if (strcmp(exitItem->pkgName, pkgName) == 0 && exitItem->pid == pid) {
1170             return SOFTBUS_OK;
1171         }
1172     }
1173     PrivilegeCloseChannelInfo *item = (PrivilegeCloseChannelInfo *)SoftBusCalloc(sizeof(PrivilegeCloseChannelInfo));
1174     TRANS_CHECK_AND_RETURN_RET_LOGE(item != NULL, SOFTBUS_MALLOC_ERR, TRANS_CTRL, "calloc failed");
1175     if (strcpy_s(item->pkgName, PKG_NAME_SIZE_MAX, pkgName) != EOK) {
1176         SoftBusFree(item);
1177         return SOFTBUS_STRCPY_ERR;
1178     }
1179     item->pid = pid;
1180     ListInit(&(item->node));
1181     ListAdd(privilegeCloseList, &(item->node));
1182     TRANS_LOGI(TRANS_CTRL, "add success, pkgName=%{public}s, pid=%{public}d", pkgName, pid);
1183     return SOFTBUS_OK;
1184 }