• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "trans_channel_common.h"
17 
18 #include <unistd.h>
19 
20 #include "bus_center_manager.h"
21 #include "g_enhance_trans_func.h"
22 #include "g_enhance_trans_func_pack.h"
23 #include "legacy/softbus_hisysevt_transreporter.h"
24 #include "lnn_distributed_net_ledger.h"
25 #include "lnn_lane_interface.h"
26 #include "lnn_network_manager.h"
27 #include "lnn_ohos_account_adapter.h"
28 #include "softbus_access_token_adapter.h"
29 #include "softbus_adapter_mem.h"
30 #include "softbus_config_type.h"
31 #include "softbus_error_code.h"
32 #include "softbus_feature_config.h"
33 #include "softbus_init_common.h"
34 #include "softbus_proxychannel_manager.h"
35 #include "softbus_wifi_api_adapter.h"
36 #include "trans_auth_manager.h"
37 #include "trans_client_proxy.h"
38 #include "trans_event.h"
39 #include "g_enhance_trans_func_pack.h"
40 #include "trans_ipc_adapter.h"
41 #include "trans_lane_manager.h"
42 #include "trans_lane_pending_ctl.h"
43 #include "trans_log.h"
44 #include "trans_session_account_adapter.h"
45 #include "trans_session_ipc_adapter.h"
46 #include "trans_session_manager.h"
47 #include "trans_tcp_direct_manager.h"
48 #include "trans_tcp_direct_sessionconn.h"
49 #include "trans_udp_channel_manager.h"
50 #include "trans_udp_negotiation.h"
51 #include "trans_uk_manager.h"
52 #include "wifi_direct_manager.h"
53 
54 typedef struct {
55     int32_t channelType;
56     int32_t businessType;
57     ConfigType configType;
58 } ConfigTypeMap;
59 
60 static const ConfigTypeMap g_configTypeMap[] = {
61     { CHANNEL_TYPE_AUTH,       BUSINESS_TYPE_BYTE,    SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH  },
62     { CHANNEL_TYPE_AUTH,       BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH},
63     { CHANNEL_TYPE_PROXY,      BUSINESS_TYPE_BYTE,    SOFTBUS_INT_MAX_BYTES_NEW_LENGTH   },
64     { CHANNEL_TYPE_PROXY,      BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_MAX_MESSAGE_NEW_LENGTH },
65     { CHANNEL_TYPE_TCP_DIRECT, BUSINESS_TYPE_BYTE,    SOFTBUS_INT_MAX_BYTES_NEW_LENGTH   },
66     { CHANNEL_TYPE_TCP_DIRECT, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_MAX_MESSAGE_NEW_LENGTH },
67 };
68 
FindConfigType(int32_t channelType,int32_t businessType)69 static int32_t FindConfigType(int32_t channelType, int32_t businessType)
70 {
71     const int32_t configTypeMapLength = sizeof(g_configTypeMap) / sizeof(ConfigTypeMap);
72     for (uint32_t i = 0; i < configTypeMapLength; i++) {
73         if ((g_configTypeMap[i].channelType == channelType) && (g_configTypeMap[i].businessType == businessType)) {
74             return g_configTypeMap[i].configType;
75         }
76     }
77     return SOFTBUS_CONFIG_TYPE_MAX;
78 }
79 
GetStreamLaneType(int32_t streamType)80 static LaneTransType GetStreamLaneType(int32_t streamType)
81 {
82     switch (streamType) {
83         case RAW_STREAM:
84             return LANE_T_RAW_STREAM;
85         case COMMON_VIDEO_STREAM:
86             return LANE_T_COMMON_VIDEO;
87         case COMMON_AUDIO_STREAM:
88             return LANE_T_COMMON_VOICE;
89         default:
90             break;
91     }
92     return LANE_T_BUTT;
93 }
94 
BuildTransCloseChannelEventExtra(TransEventExtra * extra,int32_t channelId,int32_t channelType,int32_t ret)95 static void BuildTransCloseChannelEventExtra(
96     TransEventExtra *extra, int32_t channelId, int32_t channelType, int32_t ret)
97 {
98     extra->socketName = NULL;
99     extra->peerNetworkId = NULL;
100     extra->calleePkg = NULL;
101     extra->callerPkg = NULL;
102     extra->channelId = channelId;
103     extra->channelType = channelType;
104     extra->errcode = ret;
105     extra->result = (ret == SOFTBUS_OK) ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED;
106 }
107 
TransGetLaneTransTypeBySession(const SessionParam * param)108 LaneTransType TransGetLaneTransTypeBySession(const SessionParam *param)
109 {
110     if (param == NULL || param->attr == NULL) {
111         return LANE_T_BUTT;
112     }
113     int32_t type = param->attr->dataType;
114     int32_t streamType;
115     switch (type) {
116         case TYPE_MESSAGE:
117             return LANE_T_MSG;
118         case TYPE_BYTES:
119             return LANE_T_BYTE;
120         case TYPE_FILE:
121             return LANE_T_FILE;
122         case TYPE_STREAM:
123             streamType = param->attr->attr.streamAttr.streamType;
124             return GetStreamLaneType(streamType);
125         default:
126             break;
127     }
128 
129     TRANS_LOGE(TRANS_SVC, "session type no support. type=%{public}u", type);
130     return LANE_T_BUTT;
131 }
132 
TransCommonGetLocalConfig(int32_t channelType,int32_t businessType,uint32_t * len)133 int32_t TransCommonGetLocalConfig(int32_t channelType, int32_t businessType, uint32_t *len)
134 {
135     if (len == NULL) {
136         TRANS_LOGE(TRANS_CTRL, "len is null");
137         return SOFTBUS_INVALID_PARAM;
138     }
139     ConfigType configType = (ConfigType)FindConfigType(channelType, businessType);
140     if (configType == SOFTBUS_CONFIG_TYPE_MAX) {
141         TRANS_LOGE(TRANS_CTRL, "Invalid channelType=%{public}d businessType=%{public}d", channelType, businessType);
142         return SOFTBUS_INVALID_PARAM;
143     }
144     uint32_t maxLen = 0;
145     if (SoftbusGetConfig(configType, (unsigned char *)&maxLen, sizeof(maxLen)) != SOFTBUS_OK) {
146         TRANS_LOGE(TRANS_CTRL, "get fail configType=%{public}d", configType);
147         return SOFTBUS_GET_CONFIG_VAL_ERR;
148     }
149     *len = maxLen;
150     TRANS_LOGI(TRANS_CTRL, "get appinfo local config len=%{public}d", *len);
151     return SOFTBUS_OK;
152 }
153 
TransGetChannelType(const SessionParam * param,const int32_t type)154 static ChannelType TransGetChannelType(const SessionParam *param, const int32_t type)
155 {
156     LaneTransType transType = TransGetLaneTransTypeBySession(param);
157     if (transType == LANE_T_BUTT) {
158         return CHANNEL_TYPE_BUTT;
159     }
160 
161     if (type == LANE_BR || type == LANE_BLE || type == LANE_BLE_DIRECT || type == LANE_COC ||
162         type == LANE_COC_DIRECT || type == LANE_SLE || type == LANE_SLE_DIRECT) {
163         return CHANNEL_TYPE_PROXY;
164     } else if (transType == LANE_T_FILE || transType == LANE_T_COMMON_VIDEO || transType == LANE_T_COMMON_VOICE ||
165         transType == LANE_T_RAW_STREAM) {
166         return CHANNEL_TYPE_UDP;
167     }
168     return CHANNEL_TYPE_TCP_DIRECT;
169 }
170 
TransGetAccessInfo(AppInfo * appInfo)171 static void TransGetAccessInfo(AppInfo *appInfo)
172 {
173     int32_t ret = GetAccessInfoBySessionName(appInfo->myData.sessionName, &appInfo->myData.userId,
174         &appInfo->myData.tokenId, NULL, appInfo->extraAccessInfo);
175     if (ret != SOFTBUS_OK) {
176         TRANS_LOGE(TRANS_CTRL, "get access info fail, ret=%{public}d", ret);
177         return;
178     }
179     uint32_t size = 0;
180     ret = GetLocalAccountUidByUserId(appInfo->myData.accountId, ACCOUNT_UID_LEN_MAX, &size, appInfo->myData.userId);
181     if (ret != SOFTBUS_OK) {
182         TRANS_LOGW(TRANS_CTRL, "get accountId by userId=%{public}d failed, ret=%{public}d",
183             appInfo->myData.userId, ret);
184     }
185 }
186 
FillAppInfo(AppInfo * appInfo,const SessionParam * param,TransInfo * transInfo,const LaneConnInfo * connInfo)187 void FillAppInfo(AppInfo *appInfo, const SessionParam *param, TransInfo *transInfo, const LaneConnInfo *connInfo)
188 {
189     if (appInfo == NULL || param == NULL || transInfo == NULL || connInfo == NULL) {
190         TRANS_LOGE(TRANS_CTRL, "Invalid param");
191         return;
192     }
193     int32_t ret = SOFTBUS_OK;
194     transInfo->channelType = TransGetChannelType(param, connInfo->type);
195     appInfo->linkType = connInfo->type;
196     appInfo->channelType = transInfo->channelType;
197     appInfo->flowInfo.flowSize = param->flowInfo.flowSize;
198     appInfo->flowInfo.sessionType = param->flowInfo.sessionType;
199     appInfo->flowInfo.flowQosType = param->flowInfo.flowQosType;
200     (void)TransCommonGetLocalConfig(appInfo->channelType, appInfo->businessType, &appInfo->myData.dataConfig);
201     if (connInfo->isLowLatency && CheckHtpPermissionPacked(appInfo->myData.uid)) {
202         appInfo->isFlashLight = true;
203         struct WifiDirectManager *mgr = GetWifiDirectManager();
204         if (mgr != NULL && mgr->getLocalAndRemoteMacByRemoteIp != NULL) {
205             ret = mgr->getLocalAndRemoteMacByRemoteIp(
206                 connInfo->connInfo.p2p.peerIp, appInfo->myData.mac, MAC_LEN, appInfo->peerData.mac, MAC_LEN);
207             if (ret != SOFTBUS_OK) {
208                 TRANS_LOGE(TRANS_CTRL, "get local and remote mac by local ip failed, ret=%{public}d", ret);
209                 return;
210             }
211         }
212     }
213     if (connInfo->type == LANE_P2P || connInfo->type == LANE_HML) {
214         if (strcpy_s(appInfo->myData.addr, IP_LEN, connInfo->connInfo.p2p.localIp) != EOK) {
215             TRANS_LOGE(TRANS_CTRL, "copy local ip failed");
216         }
217     } else if (connInfo->type == LANE_P2P_REUSE) {
218         struct WifiDirectManager *mgr = GetWifiDirectManager();
219         if (mgr != NULL && mgr->getLocalIpByRemoteIp != NULL) {
220             ret = mgr->getLocalIpByRemoteIp(connInfo->connInfo.wlan.addr, appInfo->myData.addr, IP_LEN);
221             if (ret != SOFTBUS_OK) {
222                 TRANS_LOGE(TRANS_CTRL, "get Local Ip fail, ret=%{public}d", ret);
223                 return;
224             }
225         }
226     }
227     TransGetAccessInfo(appInfo);
228     if (GetUkPolicy(appInfo) == NO_NEED_UK) {
229         TRANS_LOGI(TRANS_CTRL, "No need sink generate key.");
230         DisableCapabilityBit(&(appInfo->channelCapability), TRANS_CHANNEL_SINK_GENERATE_KEY_OFFSET);
231     }
232 }
233 
GetOsTypeByNetworkId(const char * networkId,int32_t * osType)234 void GetOsTypeByNetworkId(const char *networkId, int32_t *osType)
235 {
236     int32_t errCode = LnnGetOsTypeByNetworkId(networkId, osType);
237     if (errCode != SOFTBUS_OK) {
238         TRANS_LOGE(TRANS_CTRL, "get remote osType err, errCode=%{public}d", errCode);
239         return;
240     }
241 }
242 
GetRemoteUdidWithNetworkId(const char * networkId,char * info,uint32_t len)243 void GetRemoteUdidWithNetworkId(const char *networkId, char *info, uint32_t len)
244 {
245     int32_t errCode = LnnGetRemoteStrInfo(networkId, STRING_KEY_DEV_UDID, info, len);
246     if (errCode != SOFTBUS_OK) {
247         TRANS_LOGE(TRANS_CTRL, "get remote node udid err, errCode=%{public}d", errCode);
248         return;
249     }
250 }
251 
TransGetRemoteDeviceVersion(const char * id,IdCategory type,char * deviceVersion,uint32_t len)252 void TransGetRemoteDeviceVersion(const char *id, IdCategory type, char *deviceVersion, uint32_t len)
253 {
254     if (id == NULL || deviceVersion == NULL) {
255         TRANS_LOGE(TRANS_CTRL, "invalid param.");
256         return;
257     }
258     NodeInfo nodeInfo;
259     (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
260     if (LnnGetRemoteNodeInfoById(id, type, &nodeInfo) != SOFTBUS_OK) {
261         TRANS_LOGE(TRANS_CTRL, "GetRemoteNodeInfo failed IdCategory type=%{public}d", type);
262         return;
263     }
264     if (strncpy_s(deviceVersion, len, nodeInfo.deviceInfo.deviceVersion,
265         strlen(nodeInfo.deviceInfo.deviceVersion)) != EOK) {
266         TRANS_LOGE(TRANS_CTRL, "STR COPY ERROR!");
267         return;
268     }
269 }
270 
CopyAppInfoFromSessionParam(AppInfo * appInfo,const SessionParam * param)271 static int32_t CopyAppInfoFromSessionParam(AppInfo *appInfo, const SessionParam *param)
272 {
273     if (param == NULL || param->attr == NULL) {
274         TRANS_LOGE(TRANS_CTRL, "parm is null");
275         return SOFTBUS_INVALID_PARAM;
276     }
277     if (param->attr->fastTransData != NULL && param->attr->fastTransDataSize > 0 &&
278         param->attr->fastTransDataSize <= MAX_FAST_DATA_LEN) {
279         if (appInfo->businessType == BUSINESS_TYPE_FILE || appInfo->businessType == BUSINESS_TYPE_STREAM) {
280             TRANS_LOGE(TRANS_CTRL, "not support send fast data");
281             return SOFTBUS_TRANS_BUSINESS_TYPE_NOT_MATCH;
282         }
283         appInfo->fastTransData = (uint8_t*)SoftBusCalloc(param->attr->fastTransDataSize);
284         if (appInfo->fastTransData == NULL) {
285             return SOFTBUS_MALLOC_ERR;
286         }
287         if (memcpy_s((char *)appInfo->fastTransData, param->attr->fastTransDataSize,
288             (const char *)param->attr->fastTransData, param->attr->fastTransDataSize) != EOK) {
289             TRANS_LOGE(TRANS_CTRL, "memcpy_s err");
290             return SOFTBUS_MEM_ERR;
291         }
292     }
293     appInfo->fastTransDataSize = param->attr->fastTransDataSize;
294     int32_t errCode = TransGetUidAndPid(param->sessionName, &appInfo->myData.uid, &appInfo->myData.pid);
295     if (errCode != SOFTBUS_OK) {
296         return errCode;
297     }
298     errCode = strcpy_s(appInfo->groupId, sizeof(appInfo->groupId), param->groupId);
299     TRANS_CHECK_AND_RETURN_RET_LOGE(errCode == EOK, SOFTBUS_MEM_ERR, TRANS_CTRL, "copy groupId failed");
300     errCode = strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), param->sessionName);
301     TRANS_CHECK_AND_RETURN_RET_LOGE(errCode == EOK, SOFTBUS_MEM_ERR, TRANS_CTRL, "copy myData sessionName failed");
302     errCode = strcpy_s(appInfo->peerNetWorkId, sizeof(appInfo->peerNetWorkId), param->peerDeviceId);
303     TRANS_CHECK_AND_RETURN_RET_LOGE(errCode == EOK, SOFTBUS_MEM_ERR, TRANS_CTRL, "copy peerNetWorkId failed");
304 
305     errCode = TransGetPkgNameBySessionName(param->sessionName, appInfo->myData.pkgName, PKG_NAME_SIZE_MAX);
306     if (errCode != SOFTBUS_OK) {
307         return errCode;
308     }
309     errCode = strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), param->peerSessionName);
310     TRANS_CHECK_AND_RETURN_RET_LOGE(errCode == EOK, SOFTBUS_MEM_ERR, TRANS_CTRL, "copy peerData sessionName failed");
311 
312     errCode = LnnGetRemoteStrInfo(param->peerDeviceId, STRING_KEY_UUID,
313                                   appInfo->peerData.deviceId, sizeof(appInfo->peerData.deviceId));
314     if (errCode != SOFTBUS_OK) {
315         TRANS_LOGE(TRANS_CTRL, "get remote node uuid err");
316         return errCode;
317     }
318     GetRemoteUdidWithNetworkId(appInfo->peerNetWorkId, appInfo->peerUdid, sizeof(appInfo->peerUdid));
319     GetOsTypeByNetworkId(appInfo->peerNetWorkId, &appInfo->osType);
320     TransGetRemoteDeviceVersion(appInfo->peerNetWorkId, CATEGORY_NETWORK_ID, appInfo->peerVersion,
321         sizeof(appInfo->peerVersion));
322     return SOFTBUS_OK;
323 }
324 
TransCommonGetAppInfo(const SessionParam * param,AppInfo * appInfo)325 int32_t TransCommonGetAppInfo(const SessionParam *param, AppInfo *appInfo)
326 {
327     TRANS_CHECK_AND_RETURN_RET_LOGE(param != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "Invalid param");
328     TRANS_CHECK_AND_RETURN_RET_LOGE(appInfo != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "Invalid appInfo");
329     char *tmpId = NULL;
330     Anonymize(param->peerDeviceId, &tmpId);
331     TRANS_LOGI(TRANS_CTRL, "GetAppInfo, deviceId=%{public}s", AnonymizeWrapper(tmpId));
332     AnonymizeFree(tmpId);
333     appInfo->appType = APP_TYPE_NORMAL;
334     appInfo->myData.apiVersion = API_V2;
335     appInfo->myData.channelId = INVALID_CHANNEL_ID;
336     appInfo->peerData.channelId = INVALID_CHANNEL_ID;
337     if (param->attr->dataType == TYPE_STREAM) {
338         appInfo->businessType = BUSINESS_TYPE_STREAM;
339         appInfo->streamType = (StreamType)param->attr->attr.streamAttr.streamType;
340     } else if (param->attr->dataType == TYPE_FILE) {
341         appInfo->businessType = BUSINESS_TYPE_FILE;
342     } else if (param->attr->dataType == TYPE_MESSAGE) {
343         appInfo->businessType = BUSINESS_TYPE_MESSAGE;
344     } else if (param->attr->dataType == TYPE_BYTES) {
345         appInfo->businessType = BUSINESS_TYPE_BYTE;
346     }
347     int32_t ret = LnnGetLocalStrInfo(STRING_KEY_UUID, appInfo->myData.deviceId, sizeof(appInfo->myData.deviceId));
348     if (ret != SOFTBUS_OK) {
349         TRANS_LOGE(TRANS_CTRL, "LnnGetLocalStrInfo failed ret=%{public}d", ret);
350         return ret;
351     }
352     ret = CopyAppInfoFromSessionParam(appInfo, param);
353     if (ret != SOFTBUS_OK) {
354         TRANS_LOGE(TRANS_CTRL, "CopyAppInfoFromSessionParam failed ret=%{public}d", ret);
355         return ret;
356     }
357     appInfo->fd = -1;
358     appInfo->peerData.apiVersion = API_V2;
359     appInfo->encrypt = APP_INFO_FILE_FEATURES_SUPPORT;
360     appInfo->algorithm = APP_INFO_ALGORITHM_AES_GCM_256;
361     appInfo->crc = APP_INFO_FILE_FEATURES_SUPPORT;
362     appInfo->autoCloseTime = 0;
363     appInfo->myHandleId = -1;
364     appInfo->peerHandleId = -1;
365     appInfo->timeStart = GetSoftbusRecordTimeMillis();
366     appInfo->callingTokenId = appInfo->myData.pid == getpid() ? 0 : TransAclGetCallingTokenID();
367     appInfo->isClient = true;
368     appInfo->channelCapability = TRANS_CHANNEL_CAPABILITY;
369     appInfo->isLowLatency = param->isLowLatency;
370     TRANS_LOGD(TRANS_CTRL, "GetAppInfo ok");
371     return SOFTBUS_OK;
372 }
373 
TransOpenChannelSetModule(int32_t channelType,ConnectOption * connOpt)374 void TransOpenChannelSetModule(int32_t channelType, ConnectOption *connOpt)
375 {
376     if (connOpt == NULL || connOpt->type != CONNECT_TCP || connOpt->socketOption.protocol != LNN_PROTOCOL_NIP) {
377         TRANS_LOGD(TRANS_CTRL, "param err.");
378         return;
379     }
380 
381     int32_t module = UNUSE_BUTT;
382     if (channelType == CHANNEL_TYPE_PROXY) {
383         module = LnnGetProtocolListenerModule(connOpt->socketOption.protocol, LNN_LISTENER_MODE_PROXY);
384     } else if (channelType == CHANNEL_TYPE_TCP_DIRECT) {
385         module = LnnGetProtocolListenerModule(connOpt->socketOption.protocol, LNN_LISTENER_MODE_DIRECT);
386     }
387     TRANS_LOGI(TRANS_CTRL, "moduleId=%{public}d, channelType=%{public}d", connOpt->socketOption.moduleId, channelType);
388     if (module != UNUSE_BUTT) {
389         connOpt->socketOption.moduleId = module;
390     }
391     TRANS_LOGI(TRANS_CTRL, "set nip moduleId=%{public}d", connOpt->socketOption.moduleId);
392 }
393 
TransOpenChannelProc(ChannelType type,AppInfo * appInfo,const ConnectOption * connOpt,int32_t * channelId)394 int32_t TransOpenChannelProc(ChannelType type, AppInfo *appInfo, const ConnectOption *connOpt, int32_t *channelId)
395 {
396     if (appInfo == NULL || connOpt == NULL) {
397         TRANS_LOGE(TRANS_CTRL, "invalid param.");
398         return SOFTBUS_INVALID_PARAM;
399     }
400     if (type == CHANNEL_TYPE_BUTT) {
401         TRANS_LOGE(TRANS_CTRL, "open invalid channel type.");
402         return SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
403     }
404     int32_t ret = SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
405     if (type == CHANNEL_TYPE_UDP) {
406         ret = TransOpenUdpChannel(appInfo, connOpt, channelId);
407         if (ret != SOFTBUS_OK) {
408             TRANS_LOGE(TRANS_CTRL, "open udp channel err, ret=%{public}d", ret);
409             return ret;
410         }
411     } else if (type == CHANNEL_TYPE_PROXY) {
412         ret = TransProxyOpenProxyChannel(appInfo, connOpt, channelId);
413         if (ret != SOFTBUS_OK) {
414             TRANS_LOGE(TRANS_CTRL, "open proxy channel err");
415             return ret;
416         }
417     } else {
418         ret = TransOpenDirectChannel(appInfo, connOpt, channelId);
419         if (ret != SOFTBUS_OK) {
420             TRANS_LOGE(TRANS_CTRL, "open direct channel err");
421             return ret;
422         }
423     }
424     return SOFTBUS_OK;
425 }
426 
CancelWaitLaneState(const char * sessionName,int32_t sessionId)427 static int32_t CancelWaitLaneState(const char *sessionName, int32_t sessionId)
428 {
429     uint32_t laneHandle = 0;
430     bool isAsync = true;
431     bool isQosLane = false;
432     int32_t ret = TransGetSocketChannelLaneInfoBySession(sessionName, sessionId, &laneHandle, &isQosLane, &isAsync);
433     TRANS_CHECK_AND_RETURN_RET_LOGE(
434         ret == SOFTBUS_OK, ret, TRANS_CTRL, "get socket channel lane info failed, ret=%{public}d", ret);
435     TRANS_LOGI(TRANS_CTRL, "wait lane state, sessionId=%{public}d, laneHandle=%{public}u", sessionId, laneHandle);
436     if (isQosLane && laneHandle != INVALID_LANE_REQ_ID) {
437         TRANS_CHECK_AND_RETURN_RET_LOGE(
438             GetLaneManager() != NULL, SOFTBUS_TRANS_GET_LANE_INFO_ERR, TRANS_CTRL, "GetLaneManager is null");
439         TRANS_CHECK_AND_RETURN_RET_LOGE(GetLaneManager()->lnnCancelLane != NULL, SOFTBUS_TRANS_GET_LANE_INFO_ERR,
440             TRANS_CTRL, "lnnCancelLane is null");
441         ret = GetLaneManager()->lnnCancelLane(laneHandle);
442         if (ret != SOFTBUS_OK) {
443             TRANS_LOGE(
444                 TRANS_CTRL, "Cancel lane failed, free lane. laneHandle=%{public}u, ret=%{public}d", laneHandle, ret);
445             TransFreeLane(laneHandle, isQosLane, isAsync);
446         }
447     }
448     if (isAsync && laneHandle != INVALID_LANE_REQ_ID) {
449         (void)TransDeleteLaneReqItemByLaneHandle(laneHandle, isAsync);
450     }
451     (void)TransDeleteSocketChannelInfoBySession(sessionName, sessionId);
452     return SOFTBUS_OK;
453 }
454 
TransCommonCloseChannel(const char * sessionName,int32_t channelId,int32_t channelType)455 int32_t TransCommonCloseChannel(const char *sessionName, int32_t channelId, int32_t channelType)
456 {
457     TRANS_LOGI(TRANS_CTRL, "close channel: channelId=%{public}d, channelType=%{public}d", channelId, channelType);
458     int32_t ret = SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
459     if (channelType == CHANNEL_TYPE_UNDEFINED) {
460         CoreSessionState state = CORE_SESSION_STATE_INIT;
461         ret = TransGetSocketChannelStateBySession(sessionName, channelId, &state);
462         TRANS_CHECK_AND_RETURN_RET_LOGE(
463             ret == SOFTBUS_OK, ret, TRANS_CTRL, "get socket channel info failed, ret=%{public}d", ret);
464         (void)TransSetSocketChannelStateBySession(sessionName, channelId, CORE_SESSION_STATE_CANCELLING);
465         if (state == CORE_SESSION_STATE_WAIT_LANE) {
466             ret = CancelWaitLaneState(sessionName, channelId);
467             TRANS_CHECK_AND_RETURN_RET_LOGE(
468                 ret == SOFTBUS_OK, ret, TRANS_CTRL, "cancel wait lane failed, ret=%{public}d", ret);
469         }
470     } else {
471         (void)TransSetSocketChannelStateByChannel(channelId, channelType, CORE_SESSION_STATE_CANCELLING);
472         switch (channelType) {
473             case CHANNEL_TYPE_PROXY:
474                 ret = TransProxyCloseProxyChannel(channelId);
475                 (void)TransLaneMgrDelLane(channelId, channelType, false);
476                 break;
477             case CHANNEL_TYPE_TCP_DIRECT:
478                 CloseHtpChannelPacked(channelId);
479                 (void)TransDelTcpChannelInfoByChannelId(channelId);
480                 TransDelSessionConnById(channelId); // socket Fd will be shutdown when recv peer reply
481                 (void)TransLaneMgrDelLane(channelId, channelType, false);
482                 ret = SOFTBUS_OK;
483                 break;
484             case CHANNEL_TYPE_UDP:
485                 (void)NotifyQosChannelClosedPacked(channelId, channelType);
486                 ret = TransCloseUdpChannel(channelId);
487                 (void)TransLaneMgrDelLane(channelId, channelType, false);
488                 break;
489             case CHANNEL_TYPE_AUTH:
490                 ret = TransCloseAuthChannel(channelId);
491                 (void)TransLaneMgrDelLane(channelId, channelType, false);
492                 break;
493             default:
494                 TRANS_LOGE(TRANS_CTRL, "Unknow channel type, type=%{public}d", channelType);
495                 break;
496         }
497         (void)TransDeleteSocketChannelInfoByChannel(channelId, channelType);
498     }
499     TransEventExtra extra;
500     BuildTransCloseChannelEventExtra(&extra, channelId, channelType, ret);
501     TRANS_EVENT(EVENT_SCENE_CLOSE_CHANNEL_ACTIVE, EVENT_STAGE_CLOSE_CHANNEL, extra);
502     return ret;
503 }
504 
TransBuildTransOpenChannelStartEvent(TransEventExtra * extra,AppInfo * appInfo,NodeInfo * nodeInfo,int32_t peerRet)505 void TransBuildTransOpenChannelStartEvent(TransEventExtra *extra, AppInfo *appInfo, NodeInfo *nodeInfo, int32_t peerRet)
506 {
507     if (extra == NULL || appInfo == NULL || nodeInfo == NULL) {
508         TRANS_LOGE(TRANS_CTRL, "invalid param.");
509         return;
510     }
511     extra->calleePkg = NULL;
512     extra->callerPkg = appInfo->myData.pkgName;
513     extra->socketName = appInfo->myData.sessionName;
514     extra->dataType = appInfo->businessType;
515     if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, nodeInfo->masterUdid, UDID_BUF_LEN) == SOFTBUS_OK) {
516         extra->localUdid = nodeInfo->masterUdid;
517     }
518     appInfo->osType = nodeInfo->deviceInfo.osType;
519     extra->osType = appInfo->osType;
520     extra->peerNetworkId = appInfo->peerNetWorkId;
521     extra->peerUdid = peerRet == SOFTBUS_OK ? nodeInfo->deviceInfo.deviceUdid : NULL,
522     extra->peerDevVer = peerRet == SOFTBUS_OK ? nodeInfo->deviceInfo.deviceVersion : NULL,
523     extra->result = EVENT_STAGE_RESULT_OK;
524 }
525 
TransBuildOpenAuthChannelStartEvent(TransEventExtra * extra,const char * sessionName,const ConnectOption * connOpt,char * localUdid,char * callerPkg)526 void TransBuildOpenAuthChannelStartEvent(TransEventExtra *extra, const char *sessionName, const ConnectOption *connOpt,
527     char *localUdid, char *callerPkg)
528 {
529     if (extra == NULL || connOpt == NULL || localUdid == NULL || callerPkg == NULL) {
530         TRANS_LOGE(TRANS_CTRL, "invalid param.");
531         return;
532     }
533     if (!IsValidString(sessionName, SESSION_NAME_SIZE_MAX)) {
534         TRANS_LOGE(TRANS_CTRL, "invalid param.");
535         return;
536     }
537     if (TransGetPkgNameBySessionName(sessionName, callerPkg, PKG_NAME_SIZE_MAX) == SOFTBUS_OK) {
538         extra->callerPkg = callerPkg;
539     }
540     if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN) == SOFTBUS_OK) {
541         extra->localUdid = localUdid;
542     }
543     extra->socketName = sessionName;
544     extra->channelType = CHANNEL_TYPE_AUTH;
545     extra->linkType = connOpt->type;
546     extra->result = EVENT_STAGE_RESULT_OK;
547 }
548 
TransBuildTransOpenChannelEndEvent(TransEventExtra * extra,TransInfo * transInfo,int64_t timeStart,int32_t ret)549 void TransBuildTransOpenChannelEndEvent(TransEventExtra *extra, TransInfo *transInfo, int64_t timeStart, int32_t ret)
550 {
551     if (extra == NULL || transInfo == NULL) {
552         TRANS_LOGE(TRANS_CTRL, "invalid param.");
553         return;
554     }
555     extra->channelId = transInfo->channelId;
556     extra->errcode = ret;
557     extra->costTime = GetSoftbusRecordTimeMillis() - timeStart;
558     extra->result = (ret == SOFTBUS_OK) ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED;
559 }
560 
TransBuildTransOpenChannelCancelEvent(TransEventExtra * extra,TransInfo * transInfo,int64_t timeStart,int32_t ret)561 void TransBuildTransOpenChannelCancelEvent(TransEventExtra *extra, TransInfo *transInfo, int64_t timeStart, int32_t ret)
562 {
563     if (extra == NULL || transInfo == NULL) {
564         TRANS_LOGE(TRANS_CTRL, "invalid param.");
565         return;
566     }
567     extra->channelId = transInfo->channelId;
568     extra->errcode = ret;
569     extra->costTime = GetSoftbusRecordTimeMillis() - timeStart;
570     extra->result = EVENT_STAGE_RESULT_CANCELED;
571 }
572 
TransBuildTransAlarmEvent(TransAlarmExtra * extraAlarm,AppInfo * appInfo,int32_t ret)573 void TransBuildTransAlarmEvent(TransAlarmExtra *extraAlarm, AppInfo *appInfo, int32_t ret)
574 {
575     if (extraAlarm == NULL || appInfo == NULL) {
576         TRANS_LOGE(TRANS_CTRL, "invalid param.");
577         return;
578     }
579     extraAlarm->conflictName = NULL;
580     extraAlarm->conflictedName = NULL;
581     extraAlarm->occupyedName = NULL;
582     extraAlarm->permissionName = NULL;
583     extraAlarm->errcode = ret;
584     extraAlarm->sessionName = appInfo->myData.sessionName;
585 }
586 
TransFreeAppInfo(AppInfo * appInfo)587 void TransFreeAppInfo(AppInfo *appInfo)
588 {
589     if (appInfo == NULL) {
590         TRANS_LOGE(TRANS_CTRL, "invalid param.");
591         return;
592     }
593     if (appInfo->fastTransData != NULL) {
594         SoftBusFree((void *)(appInfo->fastTransData));
595     }
596     SoftBusFree(appInfo);
597 }
598 
TransFreeLane(uint32_t laneHandle,bool isQosLane,bool isAsync)599 void TransFreeLane(uint32_t laneHandle, bool isQosLane, bool isAsync)
600 {
601     TRANS_LOGI(TRANS_CTRL, "Trans free lane laneHandle=%{public}u, isQosLane=%{public}d, isAsync=%{public}d",
602         laneHandle, isQosLane, isAsync);
603     if (laneHandle == INVALID_LANE_REQ_ID) {
604         return;
605     }
606     if (isQosLane) {
607         TransFreeLaneByLaneHandle(laneHandle, isAsync);
608         return;
609     }
610     LnnFreeLane(laneHandle);
611 }
612 
TransReportBadKeyEvent(int32_t errCode,uint32_t connectionId,int64_t seq,int32_t len)613 void TransReportBadKeyEvent(int32_t errCode, uint32_t connectionId, int64_t seq, int32_t len)
614 {
615     TransAuditExtra extra = {
616         .hostPkg = NULL,
617         .localIp = NULL,
618         .localPort = NULL,
619         .localDevId = NULL,
620         .localSessName = NULL,
621         .peerIp = NULL,
622         .peerPort = NULL,
623         .peerDevId = NULL,
624         .peerSessName = NULL,
625         .result = TRANS_AUDIT_DISCONTINUE,
626         .errcode = errCode,
627         .auditType = AUDIT_EVENT_PACKETS_ERROR,
628         .connId = connectionId,
629         .dataSeq = seq,
630         .dataLen = len,
631     };
632     TRANS_AUDIT(AUDIT_SCENE_SEND_MSG, extra);
633 }
634 
IsPeerDeviceLegacyOs(int32_t osType)635 bool IsPeerDeviceLegacyOs(int32_t osType)
636 {
637     // peer device legacyOs when osType is not OH_OS_TYPE
638     return (osType == OH_OS_TYPE) ? false : true;
639 }
640 
TransGetNetCapability(const char * networkId,uint32_t * local,uint32_t * remote)641 static bool TransGetNetCapability(const char *networkId, uint32_t *local, uint32_t *remote)
642 {
643     int32_t ret = LnnGetLocalNumU32Info(NUM_KEY_NET_CAP, local);
644     if (ret != SOFTBUS_OK) {
645         TRANS_LOGE(TRANS_CTRL, "LnnGetLocalNumInfo err, ret=%{public}d, local=%{public}u", ret, *local);
646         return false;
647     }
648     ret = LnnGetRemoteNumU32Info(networkId, NUM_KEY_NET_CAP, remote);
649     if (ret != SOFTBUS_OK) {
650         TRANS_LOGE(TRANS_CTRL, "LnnGetRemoteNumInfo err, ret=%{public}d, remote=%{public}u", ret, *remote);
651         return false;
652     }
653     TRANS_LOGD(TRANS_CTRL, "trans get net capability success, local=%{public}u, remote=%{public}u", *local, *remote);
654     return true;
655 }
656 
TransGetDeviceState(const char * networkId)657 TransDeviceState TransGetDeviceState(const char *networkId)
658 {
659     if (networkId == NULL) {
660         TRANS_LOGE(TRANS_CTRL, "networkId err.");
661         return DEVICE_STATE_INVALID;
662     }
663     SoftBusWifiDetailState wifiState = SoftBusGetWifiState();
664     if (wifiState == SOFTBUS_WIFI_STATE_SEMIACTIVATING || wifiState == SOFTBUS_WIFI_STATE_SEMIACTIVE) {
665         return DEVICE_STATE_LOCAL_WIFI_HALF_OFF;
666     }
667     uint32_t local = 0;
668     uint32_t remote = 0;
669     if (!TransGetNetCapability(networkId, &local, &remote)) {
670         return DEVICE_STATE_INVALID;
671     }
672     if ((local & (1 << BIT_BLE)) && !(local & (1 << BIT_BR))) {
673         return DEVICE_STATE_LOCAL_BT_HALF_OFF;
674     }
675     if ((remote & (1 << BIT_BLE)) && !(remote & (1 << BIT_BR))) {
676         return DEVICE_STATE_REMOTE_BT_HALF_OFF;
677     }
678     if ((remote & (1 << BIT_WIFI_P2P)) && !(remote & (1 << BIT_WIFI)) &&
679         !(remote & (1 << BIT_WIFI_24G)) && !(remote & (1 << BIT_WIFI_5G))) {
680         return DEVICE_STATE_REMOTE_WIFI_HALF_OFF;
681     }
682     return DEVICE_STATE_NOT_CARE;
683 }
684 
GetSinkRelation(const AppInfo * appInfo,CollabInfo * sinkInfo)685 static int32_t GetSinkRelation(const AppInfo *appInfo, CollabInfo *sinkInfo)
686 {
687     int32_t ret = TransGetTokenIdBySessionName(appInfo->myData.sessionName, &sinkInfo->tokenId);
688     if (ret != SOFTBUS_OK) {
689         TRANS_LOGE(TRANS_CTRL, "get tokenId failed.");
690         return ret;
691     }
692     ret = LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, sinkInfo->deviceId, UDID_BUF_LEN);
693     if (ret != SOFTBUS_OK) {
694         TRANS_LOGE(TRANS_CTRL, "LnnGetLocalStrInfo failed.");
695         return ret;
696     }
697     sinkInfo->pid = appInfo->myData.pid;
698     sinkInfo->userId = TransGetUserIdFromSessionName(appInfo->myData.sessionName);
699     if (sinkInfo->userId == INVALID_USER_ID) {
700         TRANS_LOGE(TRANS_CTRL, "get userId failed.");
701         return SOFTBUS_TRANS_GET_LOCAL_UID_FAIL;
702     }
703     uint32_t size = 0;
704     ret = GetOsAccountUidByUserId(sinkInfo->accountId, ACCOUNT_UID_LEN_MAX - 1, &size, sinkInfo->userId);
705     if (ret != SOFTBUS_OK) {
706         TRANS_LOGW(TRANS_CTRL, "get current account failed.");
707     }
708     return SOFTBUS_OK;
709 }
710 
GetSourceRelation(const AppInfo * appInfo,CollabInfo * sourceInfo)711 static void GetSourceRelation(const AppInfo *appInfo, CollabInfo *sourceInfo)
712 {
713     sourceInfo->tokenId = appInfo->callingTokenId;
714     sourceInfo->pid = appInfo->peerData.pid;
715     if (strcpy_s(sourceInfo->accountId, sizeof(sourceInfo->accountId), appInfo->peerData.accountId) != EOK) {
716         TRANS_LOGE(TRANS_CTRL, "get accountId failed.");
717     }
718     sourceInfo->userId = appInfo->peerData.userId;
719     char netWorkId[NETWORK_ID_BUF_LEN] = { 0 };
720     (void)LnnGetNetworkIdByUuid(appInfo->peerData.deviceId, netWorkId, NETWORK_ID_BUF_LEN);
721     (void)LnnGetRemoteStrInfo(netWorkId, STRING_KEY_DEV_UDID, sourceInfo->deviceId, UDID_BUF_LEN);
722 }
723 
CheckSourceCollabRelation(const char * sinkNetworkId,int32_t sourcePid,int32_t sourceUid)724 int32_t CheckSourceCollabRelation(const char *sinkNetworkId, int32_t sourcePid, int32_t sourceUid)
725 {
726     if (sinkNetworkId == NULL) {
727         TRANS_LOGE(TRANS_CTRL, "invalid param, sinkNetworkId is nullptr");
728         return SOFTBUS_INVALID_PARAM;
729     }
730     int32_t dmsPid = 0;
731     int32_t ret = SOFTBUS_OK;
732     CollabInfo sourceInfo;
733     (void)memset_s(&sourceInfo, sizeof(CollabInfo), 0, sizeof(CollabInfo));
734     TransInfo transInfo = {.channelId = -1, .channelType = -1};
735     char dmsPkgName[PKG_NAME_SIZE_MAX] = {0};
736     ret = LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, sourceInfo.deviceId, UDID_BUF_LEN);
737     if (ret != SOFTBUS_OK) {
738         COMM_LOGE(COMM_SVC, "get sourceInfo udid failed. ret=%{public}d", ret);
739         return ret;
740     }
741     sourceInfo.userId = TransGetUserIdFromUid(sourceUid);
742     if (sourceInfo.userId == INVALID_USER_ID) {
743         TRANS_LOGE(TRANS_CTRL, "get userId failed.");
744         return SOFTBUS_TRANS_GET_LOCAL_UID_FAIL;
745     }
746     uint32_t size = 0;
747     ret = GetOsAccountUidByUserId(sourceInfo.accountId, ACCOUNT_UID_LEN_MAX - 1, &size, sourceInfo.userId);
748     if (ret != SOFTBUS_OK) {
749         COMM_LOGE(COMM_SVC, "get current account failed. ret=%{public}d", ret);
750     }
751     ret = TransGetCallingFullTokenId(&sourceInfo.tokenId);
752     if (ret != SOFTBUS_OK) {
753         COMM_LOGE(COMM_SVC, "get callingTokenId failed. ret=%{public}d", ret);
754         return ret;
755     }
756     sourceInfo.pid = sourcePid;
757 
758     TransGetPidAndPkgName(DMS_SESSIONNAME, DMS_UID, &dmsPid, dmsPkgName, PKG_NAME_SIZE_MAX);
759     CollabInfo sinkInfo;
760     (void)memset_s(&sinkInfo, sizeof(CollabInfo), 0, sizeof(CollabInfo));
761     sinkInfo.pid = -1;
762     (void)LnnGetRemoteStrInfo(sinkNetworkId, STRING_KEY_DEV_UDID, sinkInfo.deviceId, UDID_BUF_LEN);
763     ret = ClientIpcCheckCollabRelation(dmsPkgName, dmsPid, &sourceInfo, &sinkInfo, &transInfo);
764     if (ret != SOFTBUS_OK) {
765         COMM_LOGE(COMM_SVC, "channelId=%{public}d check Collaboration relation fail, ret=%{public}d",
766             transInfo.channelId, ret);
767         return ret;
768     }
769     return SOFTBUS_OK;
770 }
771 
CheckCollabRelation(const AppInfo * appInfo,int32_t channelId,int32_t channelType)772 int32_t CheckCollabRelation(const AppInfo *appInfo, int32_t channelId, int32_t channelType)
773 {
774     if (appInfo == NULL) {
775         TRANS_LOGE(TRANS_CTRL, "invalid param.");
776         return SOFTBUS_INVALID_PARAM;
777     }
778 
779     CollabInfo sourceInfo;
780     (void)memset_s(&sourceInfo, sizeof(CollabInfo), 0, sizeof(CollabInfo));
781     GetSourceRelation(appInfo, &sourceInfo);
782     if (sourceInfo.userId == INVALID_USER_ID) {
783         TRANS_LOGW(TRANS_CTRL, "userId is invalid.");
784         return SOFTBUS_TRANS_NOT_NEED_CHECK_RELATION;
785     }
786 
787     CollabInfo sinkInfo;
788     (void)memset_s(&sinkInfo, sizeof(CollabInfo), 0, sizeof(CollabInfo));
789     sinkInfo.pid = -1;
790     int32_t ret = GetSinkRelation(appInfo, &sinkInfo);
791     if (ret != SOFTBUS_OK) {
792         TRANS_LOGE(TRANS_CTRL, "get sink relation failed.");
793         return ret;
794     }
795     if (!SoftBusCheckIsCollabApp(sinkInfo.tokenId, appInfo->myData.sessionName)) {
796         TRANS_LOGE(TRANS_CTRL, "check is not app.");
797         return SOFTBUS_TRANS_NOT_NEED_CHECK_RELATION;
798     }
799 
800     int32_t pid = 0;
801     char pkgName[PKG_NAME_SIZE_MAX] = { 0 };
802     ret = TransGetPidAndPkgName(DMS_SESSIONNAME, DMS_UID, &pid, pkgName, PKG_NAME_SIZE_MAX);
803     if (ret != SOFTBUS_OK) {
804         TRANS_LOGE(TRANS_CTRL, "TransGetPidAndPkgName fail ret=%{public}d.", ret);
805         return ret;
806     }
807 
808     TransInfo transInfo = { .channelId = channelId, .channelType = channelType };
809     ret = ClientIpcCheckCollabRelation(pkgName, pid, &sourceInfo, &sinkInfo, &transInfo);
810     if (ret != SOFTBUS_OK) {
811         TRANS_LOGE(TRANS_CTRL,
812             "channelId=%{public}d check Collaboration relation fail, ret=%{public}d.", transInfo.channelId, ret);
813         return ret;
814     }
815     TRANS_LOGI(TRANS_CTRL, "channelId=%{public}d check Collaboration relationship success.", transInfo.channelId);
816     return SOFTBUS_OK;
817 }
818