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