1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "trans_channel_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 "softbus_adapter_mem.h"
24 #include "softbus_config_type.h"
25 #include "softbus_errcode.h"
26 #include "softbus_feature_config.h"
27 #include "softbus_hisysevt_transreporter.h"
28 #include "softbus_proxychannel_manager.h"
29 #include "softbus_qos.h"
30 #include "softbus_wifi_api_adapter.h"
31 #include "trans_auth_manager.h"
32 #include "trans_event.h"
33 #include "trans_lane_manager.h"
34 #include "trans_lane_pending_ctl.h"
35 #include "trans_log.h"
36 #include "trans_session_manager.h"
37 #include "trans_tcp_direct_manager.h"
38 #include "trans_tcp_direct_sessionconn.h"
39 #include "trans_udp_channel_manager.h"
40 #include "trans_udp_negotiation.h"
41 #include "wifi_direct_manager.h"
42
43 typedef struct {
44 int32_t channelType;
45 int32_t businessType;
46 ConfigType configType;
47 } ConfigTypeMap;
48
49 static const ConfigTypeMap g_configTypeMap[] = {
50 { CHANNEL_TYPE_AUTH, BUSINESS_TYPE_BYTE, SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH },
51 { CHANNEL_TYPE_AUTH, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH},
52 { CHANNEL_TYPE_PROXY, BUSINESS_TYPE_BYTE, SOFTBUS_INT_MAX_BYTES_NEW_LENGTH },
53 { CHANNEL_TYPE_PROXY, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_MAX_MESSAGE_NEW_LENGTH },
54 { CHANNEL_TYPE_TCP_DIRECT, BUSINESS_TYPE_BYTE, SOFTBUS_INT_MAX_BYTES_NEW_LENGTH },
55 { CHANNEL_TYPE_TCP_DIRECT, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_MAX_MESSAGE_NEW_LENGTH },
56 };
57
FindConfigType(int32_t channelType,int32_t businessType)58 static int32_t FindConfigType(int32_t channelType, int32_t businessType)
59 {
60 const int32_t configTypeMapLength = sizeof(g_configTypeMap) / sizeof(ConfigTypeMap);
61 for (uint32_t i = 0; i < configTypeMapLength; i++) {
62 if ((g_configTypeMap[i].channelType == channelType) && (g_configTypeMap[i].businessType == businessType)) {
63 return g_configTypeMap[i].configType;
64 }
65 }
66 return SOFTBUS_CONFIG_TYPE_MAX;
67 }
68
GetStreamLaneType(int32_t streamType)69 static LaneTransType GetStreamLaneType(int32_t streamType)
70 {
71 switch (streamType) {
72 case RAW_STREAM:
73 return LANE_T_RAW_STREAM;
74 case COMMON_VIDEO_STREAM:
75 return LANE_T_COMMON_VIDEO;
76 case COMMON_AUDIO_STREAM:
77 return LANE_T_COMMON_VOICE;
78 default:
79 break;
80 }
81 return LANE_T_BUTT;
82 }
83
BuildTransCloseChannelEventExtra(TransEventExtra * extra,int32_t channelId,int32_t channelType,int32_t ret)84 static void BuildTransCloseChannelEventExtra(
85 TransEventExtra *extra, int32_t channelId, int32_t channelType, int32_t ret)
86 {
87 extra->socketName = NULL;
88 extra->peerNetworkId = NULL;
89 extra->calleePkg = NULL;
90 extra->callerPkg = NULL;
91 extra->channelId = channelId;
92 extra->channelType = channelType;
93 extra->errcode = ret;
94 extra->result = (ret == SOFTBUS_OK) ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED;
95 }
96
TransGetLaneTransTypeBySession(const SessionParam * param)97 LaneTransType TransGetLaneTransTypeBySession(const SessionParam *param)
98 {
99 if (param == NULL || param->attr == NULL) {
100 return LANE_T_BUTT;
101 }
102 int32_t type = param->attr->dataType;
103 int32_t streamType;
104 switch (type) {
105 case TYPE_MESSAGE:
106 return LANE_T_MSG;
107 case TYPE_BYTES:
108 return LANE_T_BYTE;
109 case TYPE_FILE:
110 return LANE_T_FILE;
111 case TYPE_STREAM:
112 streamType = param->attr->attr.streamAttr.streamType;
113 return GetStreamLaneType(streamType);
114 default:
115 break;
116 }
117
118 TRANS_LOGE(TRANS_SVC, "session type no support. type=%{public}u", type);
119 return LANE_T_BUTT;
120 }
121
TransCommonGetLocalConfig(int32_t channelType,int32_t businessType,uint32_t * len)122 int32_t TransCommonGetLocalConfig(int32_t channelType, int32_t businessType, uint32_t *len)
123 {
124 if (len == NULL) {
125 TRANS_LOGE(TRANS_CTRL, "len is null");
126 return SOFTBUS_INVALID_PARAM;
127 }
128 ConfigType configType = (ConfigType)FindConfigType(channelType, businessType);
129 if (configType == SOFTBUS_CONFIG_TYPE_MAX) {
130 TRANS_LOGE(TRANS_CTRL, "Invalid channelType=%{public}d businessType=%{public}d", channelType, businessType);
131 return SOFTBUS_INVALID_PARAM;
132 }
133 uint32_t maxLen = 0;
134 if (SoftbusGetConfig(configType, (unsigned char *)&maxLen, sizeof(maxLen)) != SOFTBUS_OK) {
135 TRANS_LOGE(TRANS_CTRL, "get fail configType=%{public}d", configType);
136 return SOFTBUS_GET_CONFIG_VAL_ERR;
137 }
138 *len = maxLen;
139 TRANS_LOGI(TRANS_CTRL, "get appinfo local config len=%{public}d", *len);
140 return SOFTBUS_OK;
141 }
142
TransGetChannelType(const SessionParam * param,const int32_t type)143 static ChannelType TransGetChannelType(const SessionParam *param, const int32_t type)
144 {
145 LaneTransType transType = TransGetLaneTransTypeBySession(param);
146 if (transType == LANE_T_BUTT) {
147 return CHANNEL_TYPE_BUTT;
148 }
149
150 if (type == LANE_BR || type == LANE_BLE || type == LANE_BLE_DIRECT || type == LANE_COC || type == LANE_COC_DIRECT) {
151 return CHANNEL_TYPE_PROXY;
152 } else if (transType == LANE_T_FILE || transType == LANE_T_COMMON_VIDEO || transType == LANE_T_COMMON_VOICE ||
153 transType == LANE_T_RAW_STREAM) {
154 return CHANNEL_TYPE_UDP;
155 } else if ((transType == LANE_T_MSG) && (type != LANE_P2P) && (type != LANE_P2P_REUSE) && (type != LANE_HML)) {
156 return CHANNEL_TYPE_PROXY;
157 }
158 return CHANNEL_TYPE_TCP_DIRECT;
159 }
160
FillAppInfo(AppInfo * appInfo,const SessionParam * param,TransInfo * transInfo,const LaneConnInfo * connInfo)161 void FillAppInfo(AppInfo *appInfo, const SessionParam *param, TransInfo *transInfo, const LaneConnInfo *connInfo)
162 {
163 if (appInfo == NULL || param == NULL || transInfo == NULL || connInfo == NULL) {
164 TRANS_LOGE(TRANS_CTRL, "Invalid param");
165 return;
166 }
167 transInfo->channelType = TransGetChannelType(param, connInfo->type);
168 appInfo->linkType = connInfo->type;
169 appInfo->channelType = transInfo->channelType;
170 (void)TransCommonGetLocalConfig(appInfo->channelType, appInfo->businessType, &appInfo->myData.dataConfig);
171 if (connInfo->type == LANE_P2P || connInfo->type == LANE_HML) {
172 if (strcpy_s(appInfo->myData.addr, IP_LEN, connInfo->connInfo.p2p.localIp) != EOK) {
173 TRANS_LOGE(TRANS_CTRL, "copy local ip failed");
174 }
175 } else if (connInfo->type == LANE_P2P_REUSE) {
176 struct WifiDirectManager *mgr = GetWifiDirectManager();
177 if (mgr != NULL && mgr->getLocalIpByRemoteIp != NULL) {
178 int32_t ret = mgr->getLocalIpByRemoteIp(connInfo->connInfo.wlan.addr, appInfo->myData.addr, IP_LEN);
179 if (ret != SOFTBUS_OK) {
180 TRANS_LOGE(TRANS_CTRL, "get Local Ip fail, ret = %{public}d", ret);
181 }
182 }
183 }
184 }
185
GetOsTypeByNetworkId(const char * networkId,int32_t * osType)186 void GetOsTypeByNetworkId(const char *networkId, int32_t *osType)
187 {
188 int32_t errCode = LnnGetOsTypeByNetworkId(networkId, osType);
189 if (errCode != SOFTBUS_OK) {
190 TRANS_LOGE(TRANS_CTRL, "get remote osType err, errCode=%{public}d", errCode);
191 return;
192 }
193 }
194
GetRemoteUdidWithNetworkId(const char * networkId,char * info,uint32_t len)195 void GetRemoteUdidWithNetworkId(const char *networkId, char *info, uint32_t len)
196 {
197 int32_t errCode = LnnGetRemoteStrInfo(networkId, STRING_KEY_DEV_UDID, info, len);
198 if (errCode != SOFTBUS_OK) {
199 TRANS_LOGE(TRANS_CTRL, "get remote node udid err, errCode=%{public}d", errCode);
200 return;
201 }
202 }
203
TransGetRemoteDeviceVersion(const char * id,IdCategory type,char * deviceVersion,uint32_t len)204 void TransGetRemoteDeviceVersion(const char *id, IdCategory type, char *deviceVersion, uint32_t len)
205 {
206 if (id == NULL || deviceVersion == NULL) {
207 TRANS_LOGE(TRANS_CTRL, "invalid param.");
208 return;
209 }
210 NodeInfo nodeInfo;
211 (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
212 if (LnnGetRemoteNodeInfoById(id, type, &nodeInfo) != SOFTBUS_OK) {
213 TRANS_LOGE(TRANS_CTRL, "GetRemoteNodeInfo failed IdCategory type=%{public}d", type);
214 return;
215 }
216 if (strncpy_s(deviceVersion, len, nodeInfo.deviceInfo.deviceVersion,
217 strlen(nodeInfo.deviceInfo.deviceVersion)) != EOK) {
218 TRANS_LOGE(TRANS_CTRL, "STR COPY ERROR!");
219 return;
220 }
221 }
222
CopyAppInfoFromSessionParam(AppInfo * appInfo,const SessionParam * param)223 static int32_t CopyAppInfoFromSessionParam(AppInfo *appInfo, const SessionParam *param)
224 {
225 if (param == NULL || param->attr == NULL) {
226 TRANS_LOGE(TRANS_CTRL, "parm is null");
227 return SOFTBUS_INVALID_PARAM;
228 }
229 if (param->attr->fastTransData != NULL && param->attr->fastTransDataSize > 0 &&
230 param->attr->fastTransDataSize <= MAX_FAST_DATA_LEN) {
231 if (appInfo->businessType == BUSINESS_TYPE_FILE || appInfo->businessType == BUSINESS_TYPE_STREAM) {
232 TRANS_LOGE(TRANS_CTRL, "not support send fast data");
233 return SOFTBUS_TRANS_BUSINESS_TYPE_NOT_MATCH;
234 }
235 appInfo->fastTransData = (uint8_t*)SoftBusCalloc(param->attr->fastTransDataSize);
236 if (appInfo->fastTransData == NULL) {
237 return SOFTBUS_MALLOC_ERR;
238 }
239 if (memcpy_s((char *)appInfo->fastTransData, param->attr->fastTransDataSize,
240 (const char *)param->attr->fastTransData, param->attr->fastTransDataSize) != EOK) {
241 TRANS_LOGE(TRANS_CTRL, "memcpy_s err");
242 return SOFTBUS_MEM_ERR;
243 }
244 }
245 appInfo->fastTransDataSize = param->attr->fastTransDataSize;
246 int32_t errCode = TransGetUidAndPid(param->sessionName, &appInfo->myData.uid, &appInfo->myData.pid);
247 if (errCode != SOFTBUS_OK) {
248 return errCode;
249 }
250 errCode = strcpy_s(appInfo->groupId, sizeof(appInfo->groupId), param->groupId);
251 TRANS_CHECK_AND_RETURN_RET_LOGE(errCode == EOK, SOFTBUS_MEM_ERR, TRANS_CTRL, "copy groupId failed");
252 errCode = strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), param->sessionName);
253 TRANS_CHECK_AND_RETURN_RET_LOGE(errCode == EOK, SOFTBUS_MEM_ERR, TRANS_CTRL, "copy myData sessionName failed");
254 errCode = strcpy_s(appInfo->peerNetWorkId, sizeof(appInfo->peerNetWorkId), param->peerDeviceId);
255 TRANS_CHECK_AND_RETURN_RET_LOGE(errCode == EOK, SOFTBUS_MEM_ERR, TRANS_CTRL, "copy peerNetWorkId failed");
256
257 errCode = TransGetPkgNameBySessionName(param->sessionName, appInfo->myData.pkgName, PKG_NAME_SIZE_MAX);
258 if (errCode != SOFTBUS_OK) {
259 return errCode;
260 }
261 errCode = strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), param->peerSessionName);
262 TRANS_CHECK_AND_RETURN_RET_LOGE(errCode == EOK, SOFTBUS_MEM_ERR, TRANS_CTRL, "copy peerData sessionName failed");
263
264 errCode = LnnGetRemoteStrInfo(param->peerDeviceId, STRING_KEY_UUID,
265 appInfo->peerData.deviceId, sizeof(appInfo->peerData.deviceId));
266 if (errCode != SOFTBUS_OK) {
267 TRANS_LOGE(TRANS_CTRL, "get remote node uuid err");
268 return errCode;
269 }
270 GetRemoteUdidWithNetworkId(appInfo->peerNetWorkId, appInfo->peerUdid, sizeof(appInfo->peerUdid));
271 GetOsTypeByNetworkId(appInfo->peerNetWorkId, &appInfo->osType);
272 TransGetRemoteDeviceVersion(appInfo->peerNetWorkId, CATEGORY_NETWORK_ID, appInfo->peerVersion,
273 sizeof(appInfo->peerVersion));
274 return SOFTBUS_OK;
275 }
276
TransCommonGetAppInfo(const SessionParam * param,AppInfo * appInfo)277 int32_t TransCommonGetAppInfo(const SessionParam *param, AppInfo *appInfo)
278 {
279 TRANS_CHECK_AND_RETURN_RET_LOGE(param != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "Invalid param");
280 TRANS_CHECK_AND_RETURN_RET_LOGE(appInfo != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "Invalid appInfo");
281 char *tmpId = NULL;
282 Anonymize(param->peerDeviceId, &tmpId);
283 TRANS_LOGI(TRANS_CTRL, "GetAppInfo, deviceId=%{public}s", tmpId);
284 AnonymizeFree(tmpId);
285 appInfo->appType = APP_TYPE_NORMAL;
286 appInfo->myData.apiVersion = API_V2;
287 appInfo->myData.channelId = INVALID_CHANNEL_ID;
288 appInfo->peerData.channelId = INVALID_CHANNEL_ID;
289 if (param->attr->dataType == TYPE_STREAM) {
290 appInfo->businessType = BUSINESS_TYPE_STREAM;
291 appInfo->streamType = (StreamType)param->attr->attr.streamAttr.streamType;
292 } else if (param->attr->dataType == TYPE_FILE) {
293 appInfo->businessType = BUSINESS_TYPE_FILE;
294 } else if (param->attr->dataType == TYPE_MESSAGE) {
295 appInfo->businessType = BUSINESS_TYPE_MESSAGE;
296 } else if (param->attr->dataType == TYPE_BYTES) {
297 appInfo->businessType = BUSINESS_TYPE_BYTE;
298 }
299 int32_t ret = LnnGetLocalStrInfo(STRING_KEY_UUID, appInfo->myData.deviceId, sizeof(appInfo->myData.deviceId));
300 if (ret != SOFTBUS_OK) {
301 TRANS_LOGE(TRANS_CTRL, "LnnGetLocalStrInfo failed ret=%{public}d", ret);
302 return ret;
303 }
304 ret = CopyAppInfoFromSessionParam(appInfo, param);
305 if (ret != SOFTBUS_OK) {
306 TRANS_LOGE(TRANS_CTRL, "CopyAppInfoFromSessionParam failed ret=%{public}d", ret);
307 return ret;
308 }
309 appInfo->fd = -1;
310 appInfo->peerData.apiVersion = API_V2;
311 appInfo->encrypt = APP_INFO_FILE_FEATURES_SUPPORT;
312 appInfo->algorithm = APP_INFO_ALGORITHM_AES_GCM_256;
313 appInfo->crc = APP_INFO_FILE_FEATURES_SUPPORT;
314 appInfo->autoCloseTime = 0;
315 appInfo->myHandleId = -1;
316 appInfo->peerHandleId = -1;
317 appInfo->timeStart = GetSoftbusRecordTimeMillis();
318 appInfo->callingTokenId = TransACLGetCallingTokenID();
319 appInfo->isClient = true;
320 TRANS_LOGD(TRANS_CTRL, "GetAppInfo ok");
321 return SOFTBUS_OK;
322 }
323
TransOpenChannelSetModule(int32_t channelType,ConnectOption * connOpt)324 void TransOpenChannelSetModule(int32_t channelType, ConnectOption *connOpt)
325 {
326 if (connOpt->type != CONNECT_TCP || connOpt->socketOption.protocol != LNN_PROTOCOL_NIP) {
327 TRANS_LOGD(TRANS_CTRL, "param err.");
328 return;
329 }
330
331 int32_t module = UNUSE_BUTT;
332 if (channelType == CHANNEL_TYPE_PROXY) {
333 module = LnnGetProtocolListenerModule(connOpt->socketOption.protocol, LNN_LISTENER_MODE_PROXY);
334 } else if (channelType == CHANNEL_TYPE_TCP_DIRECT) {
335 module = LnnGetProtocolListenerModule(connOpt->socketOption.protocol, LNN_LISTENER_MODE_DIRECT);
336 }
337 if (module != UNUSE_BUTT) {
338 connOpt->socketOption.moduleId = module;
339 }
340 TRANS_LOGI(TRANS_CTRL, "set nip moduleId=%{public}d", connOpt->socketOption.moduleId);
341 }
342
TransOpenChannelProc(ChannelType type,AppInfo * appInfo,const ConnectOption * connOpt,int32_t * channelId)343 int32_t TransOpenChannelProc(ChannelType type, AppInfo *appInfo, const ConnectOption *connOpt, int32_t *channelId)
344 {
345 if (type == CHANNEL_TYPE_BUTT) {
346 TRANS_LOGE(TRANS_CTRL, "open invalid channel type.");
347 return SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
348 }
349 int32_t ret = SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
350 if (type == CHANNEL_TYPE_UDP) {
351 ret = TransOpenUdpChannel(appInfo, connOpt, channelId);
352 if (ret != SOFTBUS_OK) {
353 TRANS_LOGE(TRANS_CTRL, "open udp channel err, ret=%{public}d", ret);
354 return ret;
355 }
356 } else if (type == CHANNEL_TYPE_PROXY) {
357 ret = TransProxyOpenProxyChannel(appInfo, connOpt, channelId);
358 if (ret != SOFTBUS_OK) {
359 TRANS_LOGE(TRANS_CTRL, "open proxy channel err");
360 return ret;
361 }
362 } else {
363 ret = TransOpenDirectChannel(appInfo, connOpt, channelId);
364 if (ret != SOFTBUS_OK) {
365 TRANS_LOGE(TRANS_CTRL, "open direct channel err");
366 return ret;
367 }
368 }
369 return SOFTBUS_OK;
370 }
371
CancelWaitLaneState(const char * sessionName,int32_t sessionId)372 static int32_t CancelWaitLaneState(const char *sessionName, int32_t sessionId)
373 {
374 uint32_t laneHandle = 0;
375 bool isAsync = true;
376 bool isQosLane = false;
377 int32_t ret = TransGetSocketChannelLaneInfoBySession(sessionName, sessionId, &laneHandle, &isQosLane, &isAsync);
378 TRANS_CHECK_AND_RETURN_RET_LOGE(
379 ret == SOFTBUS_OK, ret, TRANS_CTRL, "get socket channel lane info failed, ret=%{public}d", ret);
380 TRANS_LOGI(TRANS_CTRL, "wait lane state, sessionId=%{public}d, laneHandle=%{public}u", sessionId, laneHandle);
381 if (isQosLane && laneHandle != INVALID_LANE_REQ_ID) {
382 TRANS_CHECK_AND_RETURN_RET_LOGE(
383 GetLaneManager() != NULL, SOFTBUS_TRANS_GET_LANE_INFO_ERR, TRANS_CTRL, "GetLaneManager is null");
384 TRANS_CHECK_AND_RETURN_RET_LOGE(GetLaneManager()->lnnCancelLane != NULL, SOFTBUS_TRANS_GET_LANE_INFO_ERR,
385 TRANS_CTRL, "lnnCancelLane is null");
386 ret = GetLaneManager()->lnnCancelLane(laneHandle);
387 if (ret != SOFTBUS_OK) {
388 TRANS_LOGE(
389 TRANS_CTRL, "Cancel lane failed, free lane. laneHandle=%{public}u, ret=%{public}d", laneHandle, ret);
390 TransFreeLane(laneHandle, isQosLane, isAsync);
391 }
392 }
393 if (isAsync && laneHandle != INVALID_LANE_REQ_ID) {
394 (void)TransDeleteLaneReqItemByLaneHandle(laneHandle, isAsync);
395 }
396 (void)TransDeleteSocketChannelInfoBySession(sessionName, sessionId);
397 return SOFTBUS_OK;
398 }
399
TransCommonCloseChannel(const char * sessionName,int32_t channelId,int32_t channelType)400 int32_t TransCommonCloseChannel(const char *sessionName, int32_t channelId, int32_t channelType)
401 {
402 TRANS_LOGI(TRANS_CTRL, "close channel: channelId=%{public}d, channelType=%{public}d", channelId, channelType);
403 int32_t ret = SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
404 if (channelType == CHANNEL_TYPE_UNDEFINED) {
405 CoreSessionState state = CORE_SESSION_STATE_INIT;
406 ret = TransGetSocketChannelStateBySession(sessionName, channelId, &state);
407 TRANS_CHECK_AND_RETURN_RET_LOGE(
408 ret == SOFTBUS_OK, ret, TRANS_CTRL, "get socket channel info failed, ret=%{public}d", ret);
409 (void)TransSetSocketChannelStateBySession(sessionName, channelId, CORE_SESSION_STATE_CANCELLING);
410 if (state == CORE_SESSION_STATE_WAIT_LANE) {
411 ret = CancelWaitLaneState(sessionName, channelId);
412 TRANS_CHECK_AND_RETURN_RET_LOGE(
413 ret == SOFTBUS_OK, ret, TRANS_CTRL, "cancel wait lane failed, ret=%{public}d", ret);
414 }
415 } else {
416 (void)TransSetSocketChannelStateByChannel(channelId, channelType, CORE_SESSION_STATE_CANCELLING);
417 switch (channelType) {
418 case CHANNEL_TYPE_PROXY:
419 ret = TransProxyCloseProxyChannel(channelId);
420 (void)TransLaneMgrDelLane(channelId, channelType, false);
421 break;
422 case CHANNEL_TYPE_TCP_DIRECT:
423 (void)TransDelTcpChannelInfoByChannelId(channelId);
424 TransDelSessionConnById(channelId); // socket Fd will be shutdown when recv peer reply
425 (void)TransLaneMgrDelLane(channelId, channelType, false);
426 ret = SOFTBUS_OK;
427 break;
428 case CHANNEL_TYPE_UDP:
429 (void)NotifyQosChannelClosed(channelId, channelType);
430 ret = TransCloseUdpChannel(channelId);
431 (void)TransLaneMgrDelLane(channelId, channelType, false);
432 break;
433 case CHANNEL_TYPE_AUTH:
434 ret = TransCloseAuthChannel(channelId);
435 (void)TransLaneMgrDelLane(channelId, channelType, false);
436 break;
437 default:
438 TRANS_LOGE(TRANS_CTRL, "Unknow channel type, type=%{public}d", channelType);
439 break;
440 }
441 (void)TransDeleteSocketChannelInfoByChannel(channelId, channelType);
442 }
443 TransEventExtra extra;
444 BuildTransCloseChannelEventExtra(&extra, channelId, channelType, ret);
445 TRANS_EVENT(EVENT_SCENE_CLOSE_CHANNEL_ACTIVE, EVENT_STAGE_CLOSE_CHANNEL, extra);
446 return ret;
447 }
448
TransBuildTransOpenChannelStartEvent(TransEventExtra * extra,AppInfo * appInfo,NodeInfo * nodeInfo,int32_t peerRet)449 void TransBuildTransOpenChannelStartEvent(TransEventExtra *extra, AppInfo *appInfo, NodeInfo *nodeInfo, int32_t peerRet)
450 {
451 if (extra == NULL || appInfo == NULL || nodeInfo == NULL) {
452 TRANS_LOGE(TRANS_CTRL, "invalid param.");
453 return;
454 }
455 extra->calleePkg = NULL;
456 extra->callerPkg = appInfo->myData.pkgName;
457 extra->socketName = appInfo->myData.sessionName;
458 extra->dataType = appInfo->businessType;
459 if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, nodeInfo->masterUdid, UDID_BUF_LEN) == SOFTBUS_OK) {
460 extra->localUdid = nodeInfo->masterUdid;
461 }
462 appInfo->osType = nodeInfo->deviceInfo.osType;
463 extra->osType = appInfo->osType;
464 extra->peerNetworkId = appInfo->peerNetWorkId;
465 extra->peerUdid = peerRet == SOFTBUS_OK ? nodeInfo->deviceInfo.deviceUdid : NULL,
466 extra->peerDevVer = peerRet == SOFTBUS_OK ? nodeInfo->deviceInfo.deviceVersion : NULL,
467 extra->result = EVENT_STAGE_RESULT_OK;
468 }
469
TransBuildOpenAuthChannelStartEvent(TransEventExtra * extra,const char * sessionName,const ConnectOption * connOpt,char * localUdid,char * callerPkg)470 void TransBuildOpenAuthChannelStartEvent(TransEventExtra *extra, const char *sessionName, const ConnectOption *connOpt,
471 char *localUdid, char *callerPkg)
472 {
473 if (extra == NULL || connOpt == NULL || localUdid == NULL || callerPkg == NULL) {
474 TRANS_LOGE(TRANS_CTRL, "invalid param.");
475 return;
476 }
477 if (!IsValidString(sessionName, SESSION_NAME_SIZE_MAX)) {
478 TRANS_LOGE(TRANS_CTRL, "invalid param.");
479 return;
480 }
481 if (TransGetPkgNameBySessionName(sessionName, callerPkg, PKG_NAME_SIZE_MAX) == SOFTBUS_OK) {
482 extra->callerPkg = callerPkg;
483 }
484 if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN) == SOFTBUS_OK) {
485 extra->localUdid = localUdid;
486 }
487 extra->socketName = sessionName;
488 extra->channelType = CHANNEL_TYPE_AUTH;
489 extra->linkType = connOpt->type;
490 extra->result = EVENT_STAGE_RESULT_OK;
491 }
492
TransBuildTransOpenChannelEndEvent(TransEventExtra * extra,TransInfo * transInfo,int64_t timeStart,int32_t ret)493 void TransBuildTransOpenChannelEndEvent(TransEventExtra *extra, TransInfo *transInfo, int64_t timeStart, int32_t ret)
494 {
495 if (extra == NULL || transInfo == NULL) {
496 TRANS_LOGE(TRANS_CTRL, "invalid param.");
497 return;
498 }
499 extra->channelId = transInfo->channelId;
500 extra->errcode = ret;
501 extra->costTime = GetSoftbusRecordTimeMillis() - timeStart;
502 extra->result = (ret == SOFTBUS_OK) ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED;
503 }
504
TransBuildTransOpenChannelCancelEvent(TransEventExtra * extra,TransInfo * transInfo,int64_t timeStart,int32_t ret)505 void TransBuildTransOpenChannelCancelEvent(TransEventExtra *extra, TransInfo *transInfo, int64_t timeStart, int32_t ret)
506 {
507 if (extra == NULL || transInfo == NULL) {
508 TRANS_LOGE(TRANS_CTRL, "invalid param.");
509 return;
510 }
511 extra->channelId = transInfo->channelId;
512 extra->errcode = ret;
513 extra->costTime = GetSoftbusRecordTimeMillis() - timeStart;
514 extra->result = EVENT_STAGE_RESULT_CANCELED;
515 }
516
TransBuildTransAlarmEvent(TransAlarmExtra * extraAlarm,AppInfo * appInfo,int32_t ret)517 void TransBuildTransAlarmEvent(TransAlarmExtra *extraAlarm, AppInfo *appInfo, int32_t ret)
518 {
519 if (extraAlarm == NULL || appInfo == NULL) {
520 TRANS_LOGE(TRANS_CTRL, "invalid param.");
521 return;
522 }
523 extraAlarm->conflictName = NULL;
524 extraAlarm->conflictedName = NULL;
525 extraAlarm->occupyedName = NULL;
526 extraAlarm->permissionName = NULL;
527 extraAlarm->errcode = ret;
528 extraAlarm->sessionName = appInfo->myData.sessionName;
529 }
530
TransFreeAppInfo(AppInfo * appInfo)531 void TransFreeAppInfo(AppInfo *appInfo)
532 {
533 if (appInfo == NULL) {
534 TRANS_LOGE(TRANS_CTRL, "invalid param.");
535 return;
536 }
537 if (appInfo->fastTransData != NULL) {
538 SoftBusFree((void *)(appInfo->fastTransData));
539 }
540 SoftBusFree(appInfo);
541 }
542
TransFreeLane(uint32_t laneHandle,bool isQosLane,bool isAsync)543 void TransFreeLane(uint32_t laneHandle, bool isQosLane, bool isAsync)
544 {
545 TRANS_LOGI(TRANS_CTRL, "Trans free lane laneHandle=%{public}u, isQosLane=%{public}d, isAsync=%{public}d",
546 laneHandle, isQosLane, isAsync);
547 if (laneHandle == INVALID_LANE_REQ_ID) {
548 return;
549 }
550 if (isQosLane) {
551 TransFreeLaneByLaneHandle(laneHandle, isAsync);
552 return;
553 }
554 LnnFreeLane(laneHandle);
555 }
556
TransReportBadKeyEvent(int32_t errCode,uint32_t connectionId,int64_t seq,int32_t len)557 void TransReportBadKeyEvent(int32_t errCode, uint32_t connectionId, int64_t seq, int32_t len)
558 {
559 TransAuditExtra extra = {
560 .hostPkg = NULL,
561 .localIp = NULL,
562 .localPort = NULL,
563 .localDevId = NULL,
564 .localSessName = NULL,
565 .peerIp = NULL,
566 .peerPort = NULL,
567 .peerDevId = NULL,
568 .peerSessName = NULL,
569 .result = TRANS_AUDIT_DISCONTINUE,
570 .errcode = errCode,
571 .auditType = AUDIT_EVENT_PACKETS_ERROR,
572 .connId = connectionId,
573 .dataSeq = seq,
574 .dataLen = len,
575 };
576 TRANS_AUDIT(AUDIT_SCENE_SEND_MSG, extra);
577 }
578
IsPeerDeviceLegacyOs(int32_t osType)579 bool IsPeerDeviceLegacyOs(int32_t osType)
580 {
581 // peer device legacyOs when osType is not OH_OS_TYPE
582 return (osType == OH_OS_TYPE) ? false : true;
583 }
584
TransGetNetCapability(const char * networkId,uint32_t * local,uint32_t * remote)585 static bool TransGetNetCapability(const char *networkId, uint32_t *local, uint32_t *remote)
586 {
587 int32_t ret = LnnGetLocalNumU32Info(NUM_KEY_NET_CAP, local);
588 if (ret != SOFTBUS_OK) {
589 TRANS_LOGE(TRANS_CTRL, "LnnGetLocalNumInfo err, ret=%{public}d, local=%{public}u", ret, *local);
590 return false;
591 }
592 ret = LnnGetRemoteNumU32Info(networkId, NUM_KEY_NET_CAP, remote);
593 if (ret != SOFTBUS_OK) {
594 TRANS_LOGE(TRANS_CTRL, "LnnGetRemoteNumInfo err, ret=%{public}d, remote=%{public}u", ret, *remote);
595 return false;
596 }
597 TRANS_LOGD(TRANS_CTRL, "trans get net capability success, local=%{public}u, remote=%{public}u", *local, *remote);
598 return true;
599 }
600
TransGetDeviceState(const char * networkId)601 TransDeviceState TransGetDeviceState(const char *networkId)
602 {
603 if (networkId == NULL) {
604 TRANS_LOGE(TRANS_CTRL, "networkId err.");
605 return DEVICE_STATE_INVALID;
606 }
607 SoftBusWifiDetailState wifiState = SoftBusGetWifiState();
608 if (wifiState == SOFTBUS_WIFI_STATE_SEMIACTIVATING || wifiState == SOFTBUS_WIFI_STATE_SEMIACTIVE) {
609 return DEVICE_STATE_LOCAL_WIFI_HALF_OFF;
610 }
611 uint32_t local = 0;
612 uint32_t remote = 0;
613 if (!TransGetNetCapability(networkId, &local, &remote)) {
614 return DEVICE_STATE_INVALID;
615 }
616 if ((local & (1 << BIT_BLE)) && !(local & (1 << BIT_BR))) {
617 return DEVICE_STATE_LOCAL_BT_HALF_OFF;
618 }
619 if ((remote & (1 << BIT_BLE)) && !(remote & (1 << BIT_BR))) {
620 return DEVICE_STATE_REMOTE_BT_HALF_OFF;
621 }
622 if ((remote & (1 << BIT_WIFI_P2P)) && !(remote & (1 << BIT_WIFI)) &&
623 !(remote & (1 << BIT_WIFI_24G)) && !(remote & (1 << BIT_WIFI_5G))) {
624 return DEVICE_STATE_REMOTE_WIFI_HALF_OFF;
625 }
626 return DEVICE_STATE_NOT_CARE;
627 }
628