• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "bus_center_manager.h"
21 #include "lnn_lane_qos.h"
22 #include "session.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_conn_interface.h"
25 #include "softbus_def.h"
26 #include "softbus_errcode.h"
27 #include "softbus_feature_config.h"
28 #include "softbus_hisysevt_transreporter.h"
29 #include "softbus_log.h"
30 #include "softbus_proxychannel_manager.h"
31 #include "softbus_proxychannel_session.h"
32 #include "softbus_qos.h"
33 #include "softbus_utils.h"
34 #include "trans_auth_manager.h"
35 #include "trans_channel_callback.h"
36 #include "trans_lane_manager.h"
37 #include "trans_lane_pending_ctl.h"
38 #include "trans_link_listener.h"
39 #include "trans_session_manager.h"
40 #include "trans_tcp_direct_manager.h"
41 #include "trans_udp_channel_manager.h"
42 #include "trans_udp_negotiation.h"
43 #include "trans_tcp_direct_sessionconn.h"
44 #include "lnn_network_manager.h"
45 
46 #define MIGRATE_ENABLE 2
47 #define MIGRATE_SUPPORTED 1
48 #define MAX_PROXY_CHANNEL_ID 0x00008000
49 #define MAX_TDC_CHANNEL_ID 0x7FFFFFFF
50 #define MAX_FD_ID 1025
51 #define MAX_PROXY_CHANNEL_ID_COUNT 1024
52 #define ID_NOT_USED 0
53 #define ID_USED 1UL
54 #define BIT_NUM 8
55 
56 static int32_t g_allocTdcChannelId = MAX_PROXY_CHANNEL_ID;
57 static SoftBusMutex g_myIdLock;
58 static unsigned long g_proxyChanIdBits[MAX_PROXY_CHANNEL_ID_COUNT / BIT_NUM / sizeof(long)] = {0};
59 static uint32_t g_proxyIdMark = 0;
60 static uint32_t g_channelIdCount = 0;
61 
62 typedef struct {
63     int32_t channelType;
64     int32_t businessType;
65     ConfigType configType;
66 } ConfigTypeMap;
67 
GenerateTdcChannelId()68 static int32_t GenerateTdcChannelId()
69 {
70     int32_t channelId;
71     if (SoftBusMutexLock(&g_myIdLock) != 0) {
72         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
73         return SOFTBUS_ERR;
74     }
75     channelId = g_allocTdcChannelId++;
76     if (g_allocTdcChannelId >= MAX_TDC_CHANNEL_ID) {
77         g_allocTdcChannelId = MAX_PROXY_CHANNEL_ID;
78     }
79     SoftBusMutexUnlock(&g_myIdLock);
80     return channelId;
81 }
82 
GenerateProxyChannelId()83 static int32_t GenerateProxyChannelId()
84 {
85     if (SoftBusMutexLock(&g_myIdLock) != 0) {
86         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
87         return SOFTBUS_ERR;
88     }
89 
90     if (g_channelIdCount >= MAX_PROXY_CHANNEL_ID_COUNT) {
91         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "No more channel Ids(1024) can be applied");
92         SoftBusMutexUnlock(&g_myIdLock);
93         return INVALID_CHANNEL_ID;
94     }
95 
96     for (uint32_t id = g_proxyIdMark + 1; id != g_proxyIdMark; id++) {
97         id = id % MAX_PROXY_CHANNEL_ID_COUNT;
98         uint32_t index = id / (BIT_NUM * sizeof(long));
99         uint32_t bit = id % (BIT_NUM * sizeof(long));
100         if ((g_proxyChanIdBits[index] & (ID_USED << bit)) == ID_NOT_USED) {
101             g_proxyChanIdBits[index] |= (ID_USED << bit);
102             g_proxyIdMark = id;
103             g_channelIdCount++;
104             SoftBusMutexUnlock(&g_myIdLock);
105             return (int32_t)id + MAX_FD_ID;
106         }
107     }
108     SoftBusMutexUnlock(&g_myIdLock);
109     return INVALID_CHANNEL_ID;
110 }
111 
112 
GenerateChannelId(bool isTdcChannel)113 int32_t GenerateChannelId(bool isTdcChannel)
114 {
115     return isTdcChannel ? GenerateTdcChannelId() : GenerateProxyChannelId();
116 }
117 
TransChannelInit(void)118 int32_t TransChannelInit(void)
119 {
120     IServerChannelCallBack *cb = TransServerGetChannelCb();
121     if (cb == NULL) {
122         return SOFTBUS_ERR;
123     }
124 
125     if (TransLaneMgrInit() != SOFTBUS_OK) {
126         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans lane manager init failed.");
127         return SOFTBUS_ERR;
128     }
129 
130     if (TransAuthInit(cb) != SOFTBUS_OK) {
131         return SOFTBUS_ERR;
132     }
133 
134     if (TransProxyManagerInit(cb) != SOFTBUS_OK) {
135         return SOFTBUS_ERR;
136     }
137 
138     if (TransTcpDirectInit(cb) != SOFTBUS_OK) {
139         return SOFTBUS_ERR;
140     }
141 
142     if (TransUdpChannelInit(cb) != SOFTBUS_OK) {
143         return SOFTBUS_ERR;
144     }
145 
146     if (TransReqLanePendingInit() != SOFTBUS_OK) {
147         return SOFTBUS_ERR;
148     }
149 
150     ReqLinkListener();
151 
152     if (SoftBusMutexInit(&g_myIdLock, NULL) != SOFTBUS_OK) {
153         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "init lock failed");
154         return SOFTBUS_ERR;
155     }
156 
157     return SOFTBUS_OK;
158 }
159 
TransChannelDeinit(void)160 NO_SANITIZE("cfi") void TransChannelDeinit(void)
161 {
162     TransLaneMgrDeinit();
163     TransAuthDeinit();
164     TransProxyManagerDeinit();
165     TransTcpDirectDeinit();
166     TransUdpChannelDeinit();
167     TransReqLanePendingDeinit();
168     SoftBusMutexDestroy(&g_myIdLock);
169 }
170 
CopyAppInfoFromSessionParam(AppInfo * appInfo,const SessionParam * param)171 static int32_t CopyAppInfoFromSessionParam(AppInfo* appInfo, const SessionParam* param)
172 {
173     if (param == NULL || param->attr == NULL) {
174         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "parm is null");
175         return SOFTBUS_ERR;
176     }
177     if (param->attr->fastTransData != NULL && param->attr->fastTransDataSize > 0 &&
178         param->attr->fastTransDataSize <= MAX_FAST_DATA_LEN) {
179         if (appInfo->businessType == BUSINESS_TYPE_FILE || appInfo->businessType == BUSINESS_TYPE_STREAM) {
180             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "not support send fast data");
181             return SOFTBUS_ERR;
182         }
183         appInfo->fastTransData = (uint8_t*)SoftBusCalloc(param->attr->fastTransDataSize);
184         if (appInfo->fastTransData == NULL) {
185             return SOFTBUS_ERR;
186         }
187         if (memcpy_s((char *)appInfo->fastTransData, param->attr->fastTransDataSize,
188             (const char *)param->attr->fastTransData, param->attr->fastTransDataSize) != EOK) {
189             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "memcpy_s err");
190             return SOFTBUS_ERR;
191         }
192     }
193     appInfo->fastTransDataSize = param->attr->fastTransDataSize;
194     if (TransGetUidAndPid(param->sessionName, &appInfo->myData.uid, &appInfo->myData.pid) != SOFTBUS_OK) {
195         return SOFTBUS_ERR;
196     }
197     if (strcpy_s(appInfo->groupId, sizeof(appInfo->groupId), param->groupId) != EOK) {
198         return SOFTBUS_ERR;
199     }
200     if (strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), param->sessionName) != EOK) {
201         return SOFTBUS_ERR;
202     }
203     if (strcpy_s(appInfo->peerNetWorkId, sizeof(appInfo->peerNetWorkId), param->peerDeviceId) != EOK) {
204         return SOFTBUS_ERR;
205     }
206     if (TransGetPkgNameBySessionName(param->sessionName, appInfo->myData.pkgName, PKG_NAME_SIZE_MAX) != SOFTBUS_OK) {
207         return SOFTBUS_ERR;
208     }
209     if (strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), param->peerSessionName) != 0) {
210         return SOFTBUS_ERR;
211     }
212     if (LnnGetRemoteStrInfo(param->peerDeviceId, STRING_KEY_UUID,
213         appInfo->peerData.deviceId, sizeof(appInfo->peerData.deviceId)) != SOFTBUS_OK) {
214         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get remote node uuid err");
215         return SOFTBUS_ERR;
216     }
217     return SOFTBUS_OK;
218 }
219 
GetAppInfo(const SessionParam * param)220 static AppInfo *GetAppInfo(const SessionParam *param)
221 {
222     char *anoyDevId = NULL;
223     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "GetAppInfo, deviceId=%s",
224         ToSecureStrDeviceID(param->peerDeviceId, &anoyDevId));
225     SoftBusFree(anoyDevId);
226     AppInfo *appInfo = (AppInfo *)SoftBusCalloc(sizeof(AppInfo));
227     if (appInfo == NULL) {
228         return NULL;
229     }
230     appInfo->appType = APP_TYPE_NORMAL;
231     appInfo->myData.apiVersion = API_V2;
232     if (param->attr->dataType == TYPE_STREAM) {
233         appInfo->businessType = BUSINESS_TYPE_STREAM;
234         appInfo->streamType = (StreamType)param->attr->attr.streamAttr.streamType;
235     } else if (param->attr->dataType == TYPE_FILE) {
236         appInfo->businessType = BUSINESS_TYPE_FILE;
237     } else if (param->attr->dataType == TYPE_MESSAGE) {
238         appInfo->businessType = BUSINESS_TYPE_MESSAGE;
239     } else if (param->attr->dataType == TYPE_BYTES) {
240         appInfo->businessType = BUSINESS_TYPE_BYTE;
241     }
242     if (LnnGetLocalStrInfo(STRING_KEY_UUID, appInfo->myData.deviceId,
243         sizeof(appInfo->myData.deviceId)) != SOFTBUS_OK) {
244         goto EXIT_ERR;
245     }
246     if (CopyAppInfoFromSessionParam(appInfo, param) != SOFTBUS_OK) {
247         goto EXIT_ERR;
248     }
249 
250     appInfo->peerData.apiVersion = API_V2;
251     appInfo->encrypt = APP_INFO_FILE_FEATURES_SUPPORT;
252     appInfo->algorithm = APP_INFO_ALGORITHM_AES_GCM_256;
253     appInfo->crc = APP_INFO_FILE_FEATURES_SUPPORT;
254     appInfo->autoCloseTime = 0;
255     appInfo->myHandleId = -1;
256     appInfo->peerHandleId = -1;
257 
258     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "GetAppInfo ok");
259     return appInfo;
260 EXIT_ERR:
261     if (appInfo != NULL) {
262         if (appInfo->fastTransData != NULL) {
263             SoftBusFree((void*)appInfo->fastTransData);
264         }
265         SoftBusFree(appInfo);
266     }
267     return NULL;
268 }
269 
TransGetChannelType(const SessionParam * param,const LaneConnInfo * connInfo)270 static ChannelType TransGetChannelType(const SessionParam *param, const LaneConnInfo *connInfo)
271 {
272     LaneTransType transType = TransGetLaneTransTypeBySession(param);
273     if (transType == LANE_T_BUTT) {
274         return CHANNEL_TYPE_BUTT;
275     }
276 
277     if (connInfo->type == LANE_BR || connInfo->type == LANE_BLE || connInfo->type == LANE_BLE_DIRECT ||
278         connInfo->type == LANE_COC || connInfo->type == LANE_COC_DIRECT) {
279         return CHANNEL_TYPE_PROXY;
280     } else if (transType == LANE_T_FILE || transType == LANE_T_COMMON_VIDEO || transType == LANE_T_COMMON_VOICE ||
281         transType == LANE_T_RAW_STREAM) {
282         return CHANNEL_TYPE_UDP;
283     } else if ((transType == LANE_T_MSG) && (connInfo->type != LANE_P2P) && (connInfo->type != LANE_P2P_REUSE)) {
284         return CHANNEL_TYPE_PROXY;
285     }
286     return CHANNEL_TYPE_TCP_DIRECT;
287 }
288 
TransOpenChannelProc(ChannelType type,AppInfo * appInfo,const ConnectOption * connOpt,int32_t * channelId)289 static int32_t TransOpenChannelProc(ChannelType type, AppInfo *appInfo, const ConnectOption *connOpt,
290     int32_t *channelId)
291 {
292     if (type == CHANNEL_TYPE_BUTT) {
293         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "open invalid channel type.");
294         return SOFTBUS_ERR;
295     }
296     if (type == CHANNEL_TYPE_UDP) {
297         if (TransOpenUdpChannel(appInfo, connOpt, channelId) != SOFTBUS_OK) {
298             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "open udp channel err");
299             return SOFTBUS_ERR;
300         }
301     } else if (type == CHANNEL_TYPE_PROXY) {
302         if (TransProxyOpenProxyChannel(appInfo, connOpt, channelId) != SOFTBUS_OK) {
303             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "open proxy channel err");
304             return SOFTBUS_ERR;
305         }
306     } else {
307         if (TransOpenDirectChannel(appInfo, connOpt, channelId) != SOFTBUS_OK) {
308             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "open direct channel err");
309             return SOFTBUS_ERR;
310         }
311     }
312     return SOFTBUS_OK;
313 }
314 
315 static const ConfigTypeMap g_configTypeMap[] = {
316     {CHANNEL_TYPE_AUTH, BUSINESS_TYPE_BYTE, SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH},
317     {CHANNEL_TYPE_AUTH, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH},
318     {CHANNEL_TYPE_PROXY, BUSINESS_TYPE_BYTE, SOFTBUS_INT_MAX_BYTES_NEW_LENGTH},
319     {CHANNEL_TYPE_PROXY, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_MAX_MESSAGE_NEW_LENGTH},
320     {CHANNEL_TYPE_TCP_DIRECT, BUSINESS_TYPE_BYTE, SOFTBUS_INT_MAX_BYTES_NEW_LENGTH},
321     {CHANNEL_TYPE_TCP_DIRECT, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_MAX_MESSAGE_NEW_LENGTH},
322 };
323 
FindConfigType(int32_t channelType,int32_t businessType)324 static int32_t FindConfigType(int32_t channelType, int32_t businessType)
325 {
326     for (uint32_t i = 0; i < sizeof(g_configTypeMap) / sizeof(ConfigTypeMap); i++) {
327         if ((g_configTypeMap[i].channelType == channelType) && (g_configTypeMap[i].businessType == businessType)) {
328             return g_configTypeMap[i].configType;
329         }
330     }
331     return SOFTBUS_CONFIG_TYPE_MAX;
332 }
333 
TransGetLocalConfig(int32_t channelType,int32_t businessType,uint32_t * len)334 static int TransGetLocalConfig(int32_t channelType, int32_t businessType, uint32_t *len)
335 {
336     ConfigType configType = (ConfigType)FindConfigType(channelType, businessType);
337     if (configType == SOFTBUS_CONFIG_TYPE_MAX) {
338         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Invalid channelType[%d] businessType[%d]",
339             channelType, businessType);
340         return SOFTBUS_INVALID_PARAM;
341     }
342     uint32_t maxLen;
343     if (SoftbusGetConfig(configType, (unsigned char *)&maxLen, sizeof(maxLen)) != SOFTBUS_OK) {
344         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get fail configType[%d]", configType);
345         return SOFTBUS_GET_CONFIG_VAL_ERR;
346     }
347     *len = maxLen;
348     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "get appinfo local config[%d]", *len);
349     return SOFTBUS_OK;
350 }
351 
FillAppInfo(AppInfo * appInfo,ConnectOption * connOpt,const SessionParam * param,TransInfo * transInfo,LaneConnInfo * connInfo)352 NO_SANITIZE("cfi") static void FillAppInfo(AppInfo *appInfo, ConnectOption *connOpt, const SessionParam *param,
353     TransInfo *transInfo, LaneConnInfo *connInfo)
354 {
355     transInfo->channelType = TransGetChannelType(param, connInfo);
356     appInfo->linkType = connInfo->type;
357     appInfo->channelType = transInfo->channelType;
358     (void)TransGetLocalConfig(appInfo->channelType, appInfo->businessType, &appInfo->myData.dataConfig);
359 }
360 
TransOpenChannelSetModule(int32_t channelType,ConnectOption * connOpt)361 static void TransOpenChannelSetModule(int32_t channelType, ConnectOption *connOpt)
362 {
363     if (connOpt->type != CONNECT_TCP || connOpt->socketOption.protocol != LNN_PROTOCOL_NIP) {
364         return;
365     }
366 
367     int32_t module = UNUSE_BUTT;
368     if (channelType == CHANNEL_TYPE_PROXY) {
369         module = LnnGetProtocolListenerModule(connOpt->socketOption.protocol, LNN_LISTENER_MODE_PROXY);
370     } else if (channelType == CHANNEL_TYPE_TCP_DIRECT) {
371         module = LnnGetProtocolListenerModule(connOpt->socketOption.protocol, LNN_LISTENER_MODE_DIRECT);
372     }
373     if (module != UNUSE_BUTT) {
374         connOpt->socketOption.moduleId = module;
375     }
376     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "set nip module = %d", connOpt->socketOption.moduleId);
377 }
378 
TransOpenChannel(const SessionParam * param,TransInfo * transInfo)379 NO_SANITIZE("cfi") int32_t TransOpenChannel(const SessionParam *param, TransInfo *transInfo)
380 {
381     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "server TransOpenChannel");
382     int64_t timeStart = GetSoftbusRecordTimeMillis();
383     transInfo->channelId = INVALID_CHANNEL_ID;
384     transInfo->channelType = CHANNEL_TYPE_BUTT;
385     LaneConnInfo connInfo;
386     uint32_t laneId = 0;
387     ConnectOption connOpt;
388     (void)memset_s(&connOpt, sizeof(ConnectOption), 0, sizeof(ConnectOption));
389 
390     AppInfo *appInfo = GetAppInfo(param);
391     TRAN_CHECK_AND_RETURN_RET_LOG(!(appInfo == NULL), INVALID_CHANNEL_ID, "GetAppInfo is null.");
392     if (TransGetLaneInfo(param, &connInfo, &laneId) != SOFTBUS_OK) {
393         SoftbusReportTransErrorEvt(SOFTBUS_TRANS_GET_LANE_INFO_ERR);
394         goto EXIT_ERR;
395     }
396     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO,
397         "sessionName[%s], get laneId[%u], link type[%u].", param->sessionName, laneId, connInfo.type);
398 
399     if (TransGetConnectOptByConnInfo(&connInfo, &connOpt) != SOFTBUS_OK) {
400         SoftbusRecordOpenSessionKpi(appInfo->myData.pkgName, connInfo.type,
401             SOFTBUS_EVT_OPEN_SESSION_FAIL, GetSoftbusRecordTimeMillis() - timeStart);
402         goto EXIT_ERR;
403     }
404     FillAppInfo(appInfo, &connOpt, param, transInfo, &connInfo);
405     TransOpenChannelSetModule(transInfo->channelType, &connOpt);
406     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "lane[%u] get channel type[%u].", laneId, transInfo->channelType);
407     if (TransOpenChannelProc((ChannelType)transInfo->channelType, appInfo, &connOpt,
408         &(transInfo->channelId)) != SOFTBUS_OK) {
409         SoftbusReportTransErrorEvt(SOFTBUS_TRANS_CREATE_CHANNEL_ERR);
410         SoftbusRecordOpenSessionKpi(appInfo->myData.pkgName,
411             appInfo->linkType, SOFTBUS_EVT_OPEN_SESSION_FAIL, GetSoftbusRecordTimeMillis() - timeStart);
412         goto EXIT_ERR;
413     }
414 
415     if (((ChannelType)transInfo->channelType == CHANNEL_TYPE_TCP_DIRECT) && (connOpt.type != CONNECT_P2P)) {
416         LnnFreeLane(laneId);
417     } else if (TransLaneMgrAddLane(transInfo->channelId, transInfo->channelType,
418         &connInfo, laneId, &appInfo->myData) != SOFTBUS_OK) {
419         SoftbusRecordOpenSessionKpi(appInfo->myData.pkgName,
420             appInfo->linkType, SOFTBUS_EVT_OPEN_SESSION_FAIL, GetSoftbusRecordTimeMillis() - timeStart);
421         TransCloseChannel(transInfo->channelId, transInfo->channelType);
422         goto EXIT_ERR;
423     }
424 
425     if (appInfo->fastTransData != NULL) {
426         SoftBusFree((void*)appInfo->fastTransData);
427     }
428     SoftBusFree(appInfo);
429     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "server TransOpenChannel ok: channelId=%d, channelType=%d",
430         transInfo->channelId, transInfo->channelType);
431     return SOFTBUS_OK;
432 EXIT_ERR:
433     if (appInfo->fastTransData != NULL) {
434         SoftBusFree((void*)appInfo->fastTransData);
435     }
436     SoftBusFree(appInfo);
437     if (laneId != 0) {
438         LnnFreeLane(laneId);
439     }
440     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "server TransOpenChannel err");
441     return INVALID_CHANNEL_ID;
442 }
443 
GetAuthAppInfo(const char * mySessionName)444 static AppInfo *GetAuthAppInfo(const char *mySessionName)
445 {
446     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "GetAuthAppInfo");
447     AppInfo *appInfo = (AppInfo *)SoftBusCalloc(sizeof(AppInfo));
448     if (appInfo == NULL) {
449         return NULL;
450     }
451     appInfo->appType = APP_TYPE_AUTH;
452     appInfo->myData.apiVersion = API_V2;
453     appInfo->autoCloseTime = 0;
454     appInfo->businessType = BUSINESS_TYPE_BYTE;
455     appInfo->channelType = CHANNEL_TYPE_AUTH;
456     if (TransGetUidAndPid(mySessionName, &appInfo->myData.uid, &appInfo->myData.pid) != SOFTBUS_OK) {
457         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetAuthAppInfo GetUidAndPid failed");
458         goto EXIT_ERR;
459     }
460     if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, appInfo->myData.deviceId,
461         sizeof(appInfo->myData.deviceId)) != SOFTBUS_OK) {
462         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetAuthAppInfo get deviceId failed");
463         goto EXIT_ERR;
464     }
465     if (strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), mySessionName) != EOK) {
466         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetAuthAppInfo strcpy_s mySessionName failed");
467         goto EXIT_ERR;
468     }
469     if (strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), mySessionName) != EOK) {
470         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetAuthAppInfo strcpy_s peerSessionName failed");
471         goto EXIT_ERR;
472     }
473     if (TransGetPkgNameBySessionName(mySessionName, appInfo->myData.pkgName, PKG_NAME_SIZE_MAX) != SOFTBUS_OK) {
474         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetAuthAppInfo get PkgName failed");
475         goto EXIT_ERR;
476     }
477     if (TransGetLocalConfig(appInfo->channelType, appInfo->businessType, &appInfo->myData.dataConfig) != SOFTBUS_OK) {
478         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetAuthAppInfo get local data config failed");
479         goto EXIT_ERR;
480     }
481 
482     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "GetAuthAppInfo ok");
483     return appInfo;
484 EXIT_ERR:
485     if (appInfo != NULL) {
486         SoftBusFree(appInfo);
487     }
488     return NULL;
489 }
490 
TransOpenAuthChannel(const char * sessionName,const ConnectOption * connOpt,const char * reqId)491 NO_SANITIZE("cfi") int32_t TransOpenAuthChannel(const char *sessionName, const ConnectOption *connOpt,
492     const char *reqId)
493 {
494     int32_t channelId = INVALID_CHANNEL_ID;
495     if (!IsValidString(sessionName, SESSION_NAME_SIZE_MAX) || connOpt == NULL) {
496         return channelId;
497     }
498 
499     if (connOpt->type == CONNECT_TCP) {
500         if (TransOpenAuthMsgChannel(sessionName, connOpt, &channelId, reqId) != SOFTBUS_OK) {
501             return INVALID_CHANNEL_ID;
502         }
503     } else if (connOpt->type == CONNECT_BR || connOpt->type == CONNECT_BLE) {
504         AppInfo *appInfo = GetAuthAppInfo(sessionName);
505         if (appInfo == NULL) {
506             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetAuthAppInfo failed");
507             return INVALID_CHANNEL_ID;
508         }
509         if (strcpy_s(appInfo->reqId, REQ_ID_SIZE_MAX, reqId) != EOK) {
510             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransOpenAuthChannel strcpy_s reqId failed");
511             return INVALID_CHANNEL_ID;
512         }
513         if (TransProxyOpenProxyChannel(appInfo, connOpt, &channelId) != SOFTBUS_OK) {
514             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransOpenAuthChannel proxy channel err");
515             SoftBusFree(appInfo);
516             return INVALID_CHANNEL_ID;
517         }
518         SoftBusFree(appInfo);
519     }
520     return channelId;
521 }
522 
MergeStatsInterval(const uint32_t * data,uint32_t left,uint32_t right)523 static uint32_t MergeStatsInterval(const uint32_t *data, uint32_t left, uint32_t right)
524 {
525     uint32_t result = 0;
526     while (left <= right) {
527         result += data[left];
528         left++;
529     }
530     return result;
531 }
532 
ConvertStreamStats(const StreamSendStats * src,FrameSendStats * dest)533 static void ConvertStreamStats(const StreamSendStats *src, FrameSendStats *dest)
534 {
535     uint32_t *srcCostCnt = (uint32_t *)(src->costTimeStatsCnt);
536     uint32_t *srcBitRate = (uint32_t *)(src->sendBitRateStatsCnt);
537     uint32_t *destCostCnt = dest->costTimeStatsCnt;
538     uint32_t *destBitRate = dest->sendBitRateStatsCnt;
539     destCostCnt[FRAME_COST_TIME_SMALL] = srcCostCnt[FRAME_COST_LT10MS];
540     destCostCnt[FRAME_COST_TIME_MEDIUM] = MergeStatsInterval(srcCostCnt, FRAME_COST_LT30MS, FRAME_COST_LT100MS);
541     destCostCnt[FRAME_COST_TIME_LARGE] = srcCostCnt[FRAME_COST_LT120MS] + srcCostCnt[FRAME_COST_GE120MS];
542     destBitRate[FRAME_BIT_RATE_SMALL] = srcBitRate[FRAME_BIT_RATE_LT3M];
543     destBitRate[FRAME_BIT_RATE_MEDIUM] = MergeStatsInterval(srcBitRate, FRAME_BIT_RATE_LT6M, FRAME_BIT_RATE_LT30M);
544     destBitRate[FRAME_BIT_RATE_LARGE] = srcBitRate[FRAME_BIT_RATE_GE30M];
545 }
546 
TransStreamStats(int32_t channelId,int32_t channelType,const StreamSendStats * data)547 NO_SANITIZE("cfi") int32_t TransStreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
548 {
549     (void)channelType;
550     if (data == NULL) {
551         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "streamStats data is null");
552         return SOFTBUS_INVALID_PARAM;
553     }
554     uint32_t laneId;
555     int32_t ret = TransGetLaneIdByChannelId(channelId, &laneId);
556     if (ret != SOFTBUS_OK) {
557         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get laneId fail, streamStatsInfo cannot be processed");
558         return SOFTBUS_ERR;
559     }
560     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "transStreamStats channelId:%d, laneId:0x%x", channelId, laneId);
561     LaneIdStatsInfo info;
562     (void)memset_s(&info, sizeof(info), 0, sizeof(info));
563     info.laneId = laneId;
564     info.statsType = LANE_T_COMMON_VIDEO;
565     FrameSendStats *stats = &info.statsInfo.stream.frameStats;
566     ConvertStreamStats(data, stats);
567     LnnReportLaneIdStatsInfo(&info, 1); /* only report stream stats */
568     return SOFTBUS_OK;
569 }
570 
TransRequestQos(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)571 NO_SANITIZE("cfi") int32_t TransRequestQos(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
572 {
573     (void)chanType;
574     (void)appType;
575     uint32_t laneId;
576     int32_t ret = TransGetLaneIdByChannelId(channelId, &laneId);
577     if (ret != SOFTBUS_OK) {
578         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get laneId fail, transRequestQos cannot be processed");
579         return SOFTBUS_ERR;
580     }
581     int32_t result = 0;
582     if (quality == QOS_IMPROVE) {
583         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans requestQos");
584         ret = LnnRequestQosOptimization(&laneId, 1, &result, 1);
585     } else if (quality == QOS_RECOVER) {
586         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans cancel Qos");
587         LnnCancelQosOptimization(&laneId, 1);
588         ret = SOFTBUS_OK;
589     } else {
590         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "requestQos quality[%d] invalid", quality);
591         ret = SOFTBUS_ERR;
592     }
593 
594     if (ret != SOFTBUS_OK) {
595         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "request Qos fail,type:%d, err:%d", quality, ret);
596         return SOFTBUS_ERR;
597     }
598     return SOFTBUS_OK;
599 }
600 
TransRippleStats(int32_t channelId,int32_t channelType,const TrafficStats * data)601 NO_SANITIZE("cfi") int32_t TransRippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
602 {
603     (void)channelType;
604     if (data == NULL) {
605         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "rippleStats data is null");
606         return SOFTBUS_INVALID_PARAM;
607     }
608     uint32_t laneId;
609     int32_t ret = TransGetLaneIdByChannelId(channelId, &laneId);
610     if (ret != SOFTBUS_OK) {
611         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get laneId fail, streamStatsInfo cannot be processed");
612         return SOFTBUS_ERR;
613     }
614     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "transRippleStats channelId:%d, laneId:0x%x", channelId, laneId);
615     LnnRippleData rptdata;
616     (void)memset_s(&rptdata, sizeof(rptdata), 0, sizeof(rptdata));
617     if (memcpy_s(&rptdata.stats, sizeof(rptdata.stats), data->stats, sizeof(data->stats)) != EOK) {
618         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "memcpy fail");
619         return SOFTBUS_ERR;
620     }
621     LnnReportRippleData(laneId, &rptdata);
622     return SOFTBUS_OK;
623 }
624 
TransNotifyAuthSuccess(int32_t channelId,int32_t channelType)625 NO_SANITIZE("cfi") int32_t TransNotifyAuthSuccess(int32_t channelId, int32_t channelType)
626 {
627     int32_t ret = SOFTBUS_ERR;
628     ConnectOption connOpt;
629     switch (channelType) {
630         case CHANNEL_TYPE_AUTH:
631             ret = TransAuthGetConnOptionByChanId(channelId, &connOpt);
632             break;
633         case CHANNEL_TYPE_PROXY:
634             ret = TransProxyGetConnOptionByChanId(channelId, &connOpt);
635             break;
636         default:
637             ret = SOFTBUS_ERR;
638             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "channel=%d, type=%d invalid.", channelId, channelType);
639     }
640     if (ret != SOFTBUS_OK) {
641         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
642             "channel=%d, type=%d,notfiy auth success error=%d.", channelId, channelType, ret);
643         return ret;
644     }
645     return TransNotifyAuthDataSuccess(channelId, &connOpt);
646 }
647 
TransCloseChannel(int32_t channelId,int32_t channelType)648 NO_SANITIZE("cfi") int32_t TransCloseChannel(int32_t channelId, int32_t channelType)
649 {
650     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "close channel: id=%d, type=%d", channelId, channelType);
651     int32_t ret = SOFTBUS_ERR;
652     switch (channelType) {
653         case CHANNEL_TYPE_PROXY:
654             (void)TransLaneMgrDelLane(channelId, channelType);
655             ret = TransProxyCloseProxyChannel(channelId);
656             break;
657         case CHANNEL_TYPE_TCP_DIRECT:
658             (void)TransLaneMgrDelLane(channelId, channelType);
659             ret = SOFTBUS_OK;
660             break;
661         case CHANNEL_TYPE_UDP:
662             (void)NotifyQosChannelClosed(channelId, channelType);
663             (void)TransLaneMgrDelLane(channelId, channelType);
664             ret = TransCloseUdpChannel(channelId);
665             break;
666         case CHANNEL_TYPE_AUTH:
667             ret = TransCloseAuthChannel(channelId);
668             break;
669         default:
670             break;
671     }
672     return ret;
673 }
674 
TransSendMsg(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)675 NO_SANITIZE("cfi") int32_t TransSendMsg(int32_t channelId, int32_t channelType, const void *data, uint32_t len,
676     int32_t msgType)
677 {
678     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "send msg: id=%d, type=%d", channelId, channelType);
679     int32_t ret = SOFTBUS_OK;
680     switch (channelType) {
681         case CHANNEL_TYPE_AUTH:
682             ret = TransSendAuthMsg(channelId, (char*)data, (int32_t)len);
683             break;
684         case CHANNEL_TYPE_PROXY:
685             ret = TransProxyPostSessionData(channelId, (unsigned char*)data, len, (SessionPktType)msgType);
686             break;
687         default:
688             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "send msg: id=%d invalid type=%d", channelId, channelType);
689             ret = SOFTBUS_TRANS_CHANNEL_TYPE_INVALID;
690             break;
691     }
692     return ret;
693 }
694 
TransChannelDeathCallback(const char * pkgName,int32_t pid)695 NO_SANITIZE("cfi") void TransChannelDeathCallback(const char *pkgName, int32_t pid)
696 {
697     TransProxyDeathCallback(pkgName, pid);
698     TransTdcDeathCallback(pkgName, pid);
699     TransLaneMgrDeathCallback(pkgName, pid);
700     TransUdpDeathCallback(pkgName, pid);
701 }
702 
TransGetNameByChanId(const TransInfo * info,char * pkgName,char * sessionName,uint16_t pkgLen,uint16_t sessionNameLen)703 NO_SANITIZE("cfi") int32_t TransGetNameByChanId(const TransInfo *info, char *pkgName, char *sessionName,
704     uint16_t pkgLen, uint16_t sessionNameLen)
705 {
706     if (info == NULL || pkgName == NULL || sessionName == NULL) {
707         return SOFTBUS_INVALID_PARAM;
708     }
709     switch ((ChannelType)info->channelType) {
710         case CHANNEL_TYPE_PROXY:
711             return TransProxyGetNameByChanId(info->channelId, pkgName, sessionName, pkgLen, sessionNameLen);
712         case CHANNEL_TYPE_UDP:
713             return TransUdpGetNameByChanId(info->channelId, pkgName, sessionName, pkgLen, sessionNameLen);
714         case CHANNEL_TYPE_AUTH:
715             return TransAuthGetNameByChanId(info->channelId, pkgName, sessionName, pkgLen, sessionNameLen);
716         default:
717             return SOFTBUS_INVALID_PARAM;
718     }
719 }
720 
TransGetAppInfoByChanId(int32_t channelId,int32_t channelType,AppInfo * appInfo)721 NO_SANITIZE("cfi") int32_t TransGetAppInfoByChanId(int32_t channelId, int32_t channelType, AppInfo* appInfo)
722 {
723     if (appInfo == NULL) {
724         return SOFTBUS_INVALID_PARAM;
725     }
726     switch ((ChannelType)channelType) {
727         case CHANNEL_TYPE_TCP_DIRECT:
728             return TcpTranGetAppInfobyChannelId(channelId, appInfo);
729         case CHANNEL_TYPE_PROXY:
730             return TransProxyGetAppInfoByChanId(channelId, appInfo);
731         case CHANNEL_TYPE_UDP:
732             return TransGetUdpAppInfoByChannelId(channelId, appInfo);
733         case CHANNEL_TYPE_AUTH:
734             return TransAuthGetAppInfoByChanId(channelId, appInfo);
735         default:
736             return SOFTBUS_INVALID_PARAM;
737     }
738 }
739 
TransGetConnByChanId(int32_t channelId,int32_t channelType,int32_t * connId)740 NO_SANITIZE("cfi") int32_t TransGetConnByChanId(int32_t channelId, int32_t channelType, int32_t* connId)
741 {
742     if (channelType != CHANNEL_TYPE_PROXY) {
743         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "channelType:%d error", channelType);
744         return SOFTBUS_ERR;
745     }
746     if (TransProxyGetConnIdByChanId(channelId, connId) != SOFTBUS_OK) {
747         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get proxy connId, channelId: %d", channelId);
748         return SOFTBUS_TRANS_PROXY_SEND_CHANNELID_INVALID;
749     }
750     return SOFTBUS_OK;
751 }