1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "softbus_proxychannel_listener.h"
16
17 #include <securec.h>
18
19 #include "bus_center_manager.h"
20 #include "lnn_distributed_net_ledger.h"
21 #include "lnn_lane_interface.h"
22 #include "softbus_conn_interface.h"
23 #include "softbus_def.h"
24 #include "softbus_errcode.h"
25 #include "softbus_hisysevt_transreporter.h"
26 #include "softbus_proxychannel_callback.h"
27 #include "softbus_proxychannel_manager.h"
28 #include "softbus_proxychannel_network.h"
29 #include "softbus_proxychannel_session.h"
30 #include "softbus_proxychannel_control.h"
31 #include "softbus_utils.h"
32 #include "softbus_adapter_mem.h"
33 #include "trans_channel_common.h"
34 #include "trans_lane_manager.h"
35 #include "trans_lane_pending_ctl.h"
36 #include "trans_log.h"
37 #include "trans_event.h"
38
NotifyNormalChannelClosed(const char * pkgName,int32_t pid,int32_t channelId)39 static int32_t NotifyNormalChannelClosed(const char *pkgName, int32_t pid, int32_t channelId)
40 {
41 int32_t ret = TransProxyOnChannelClosed(pkgName, pid, channelId);
42 TRANS_LOGI(TRANS_CTRL, "proxy channel close, channelId=%{public}d, ret=%{public}d", channelId, ret);
43 return ret;
44 }
45
NotifyNormalChannelOpenFailed(const char * pkgName,int32_t pid,int32_t channelId,int32_t errCode)46 static int32_t NotifyNormalChannelOpenFailed(const char *pkgName, int32_t pid, int32_t channelId, int32_t errCode)
47 {
48 int32_t ret = TransProxyOnChannelOpenFailed(pkgName, pid, channelId, errCode);
49 TRANS_LOGW(TRANS_CTRL, "proxy channel open fail, channelId=%{public}d, ret=%{public}d", channelId, ret);
50 return ret;
51 }
52
GetProxyChannelInfo(int32_t channelId,const AppInfo * appInfo,bool isServer,ChannelInfo * info)53 static void GetProxyChannelInfo(int32_t channelId, const AppInfo *appInfo, bool isServer, ChannelInfo *info)
54 {
55 info->channelId = channelId;
56 info->channelType = CHANNEL_TYPE_PROXY;
57 info->isServer = isServer;
58 info->isEnabled = true;
59 info->isEncrypt = appInfo->appType != APP_TYPE_AUTH;
60 info->groupId = (char *)appInfo->groupId;
61 info->peerSessionName = (char *)appInfo->peerData.sessionName;
62 info->peerPid = appInfo->peerData.pid;
63 info->peerUid = appInfo->peerData.uid;
64 info->sessionKey = (char *)appInfo->sessionKey;
65 info->keyLen = SESSION_KEY_LENGTH;
66 info->fileEncrypt = appInfo->encrypt;
67 info->algorithm = appInfo->algorithm;
68 info->crc = appInfo->crc;
69 info->routeType = appInfo->routeType;
70 info->businessType = (int32_t)(appInfo->appType == APP_TYPE_AUTH ? BUSINESS_TYPE_NOT_CARE : appInfo->businessType);
71 info->autoCloseTime = appInfo->autoCloseTime;
72 info->myHandleId = appInfo->myHandleId;
73 info->peerHandleId = appInfo->peerHandleId;
74 info->linkType = appInfo->linkType;
75 info->dataConfig = appInfo->myData.dataConfig;
76 if (appInfo->appType == APP_TYPE_AUTH) {
77 info->reqId = (char *)appInfo->reqId;
78 }
79 info->timeStart = appInfo->timeStart;
80 info->linkType = appInfo->linkType;
81 info->connectType = appInfo->connectType;
82 info->osType = appInfo->osType;
83 TransGetLaneIdByChannelId(channelId, &info->laneId);
84 }
85
NotifyNormalChannelOpened(int32_t channelId,const AppInfo * appInfo,bool isServer)86 static int32_t NotifyNormalChannelOpened(int32_t channelId, const AppInfo *appInfo, bool isServer)
87 {
88 ChannelInfo info = {0};
89 int32_t ret = SOFTBUS_ERR;
90 char buf[NETWORK_ID_BUF_LEN] = {0};
91
92 GetProxyChannelInfo(channelId, appInfo, isServer, &info);
93 if (appInfo->appType != APP_TYPE_AUTH) {
94 ret = LnnGetNetworkIdByUuid(appInfo->peerData.deviceId, buf, NETWORK_ID_BUF_LEN);
95 if (ret != SOFTBUS_OK) {
96 char *anonyUuid = NULL;
97 Anonymize(appInfo->peerData.deviceId, &anonyUuid);
98 TRANS_LOGE(TRANS_CTRL, "get info networkId fail, uuid=%{public}s", anonyUuid);
99 AnonymizeFree(anonyUuid);
100 return ret;
101 }
102 info.peerDeviceId = buf;
103 if (LnnGetOsTypeByNetworkId(buf, &(info.osType)) != SOFTBUS_OK) {
104 TRANS_LOGE(TRANS_CTRL, "get remote osType fail, channelId=%{public}d", channelId);
105 }
106 } else {
107 info.peerDeviceId = (char *)appInfo->peerData.deviceId;
108 }
109
110 ret = TransProxyOnChannelOpened(appInfo->myData.pkgName, appInfo->myData.pid, appInfo->myData.sessionName, &info);
111 TRANS_LOGI(TRANS_CTRL, "proxy channel open, channelId=%{public}d, ret=%{public}d", channelId, ret);
112 return ret;
113 }
114
FillExtraByProxyChannelErrorEnd(TransEventExtra * extra,const AppInfo * appInfo,char * localUdid,uint32_t len)115 static void FillExtraByProxyChannelErrorEnd(TransEventExtra *extra, const AppInfo *appInfo, char *localUdid,
116 uint32_t len)
117 {
118 if (extra == NULL || appInfo == NULL || localUdid == NULL) {
119 TRANS_LOGE(TRANS_CTRL, "invalid param.");
120 return;
121 }
122 extra->socketName = appInfo->myData.sessionName;
123 extra->callerPkg = appInfo->myData.pkgName;
124 extra->linkType = appInfo->connectType;
125 if (appInfo->appType == APP_TYPE_AUTH && strlen(appInfo->peerVersion) == 0) {
126 TransGetRemoteDeviceVersion(extra->peerUdid, CATEGORY_UDID, (char *)(appInfo->peerVersion),
127 sizeof(appInfo->peerVersion));
128 }
129 extra->peerDevVer = appInfo->peerVersion;
130 (void)LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, len);
131 extra->localUdid = localUdid;
132 }
133
OnProxyChannelOpened(int32_t channelId,const AppInfo * appInfo,unsigned char isServer)134 int32_t OnProxyChannelOpened(int32_t channelId, const AppInfo *appInfo, unsigned char isServer)
135 {
136 int32_t ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
137 if (appInfo == NULL) {
138 TRANS_LOGE(TRANS_CTRL, "proxy channel opened app info invalid.");
139 return SOFTBUS_INVALID_PARAM;
140 }
141 TRANS_LOGI(TRANS_CTRL, "proxy channel opened: channelId=%{public}d, appType=%{public}d, isServer=%{public}d",
142 channelId, appInfo->appType, isServer);
143
144 switch (appInfo->appType) {
145 case APP_TYPE_NORMAL:
146 case APP_TYPE_AUTH:
147 ret = NotifyNormalChannelOpened(channelId, appInfo, isServer);
148 break;
149 case APP_TYPE_INNER:
150 ret = NotifyNetworkingChannelOpened(appInfo->myData.sessionName, channelId, appInfo, isServer);
151 break;
152 default:
153 ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
154 break;
155 }
156 TransEventExtra extra = {
157 .socketName = NULL,
158 .peerNetworkId = NULL,
159 .calleePkg = NULL,
160 .callerPkg = NULL,
161 .channelId = channelId,
162 .costTime = GetSoftbusRecordTimeMillis() - appInfo->connectedStart,
163 .errcode = ret,
164 .result = (ret == SOFTBUS_OK) ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED
165 };
166 extra.osType = (appInfo->osType < 0) ? UNKNOW_OS_TYPE : appInfo->osType;
167 extra.peerUdid = appInfo->peerUdid;
168 extra.deviceState = TransGetDeviceState(appInfo->peerNetWorkId);
169 if (!isServer) {
170 extra.peerUdid = appInfo->appType == APP_TYPE_AUTH ? appInfo->peerData.deviceId : extra.peerUdid;
171 if (extra.result == EVENT_STAGE_RESULT_FAILED) {
172 char localUdid[UDID_BUF_LEN] = { 0 };
173 FillExtraByProxyChannelErrorEnd(&extra, appInfo, localUdid, sizeof(localUdid));
174 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_END, extra);
175 } else {
176 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_REPLY, extra);
177 }
178 } else if (ret != SOFTBUS_OK) {
179 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_OPEN_CHANNEL_END, extra);
180 }
181 TRANS_LOGI(TRANS_CTRL, "on open, channelId=%{public}d, ret=%{public}d", channelId, ret);
182 return ret;
183 }
184
TransProxyGetChannelIsServer(int32_t channelId,int8_t * isServer)185 static int32_t TransProxyGetChannelIsServer(int32_t channelId, int8_t *isServer)
186 {
187 ProxyChannelInfo *chan = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
188 if (chan == NULL) {
189 TRANS_LOGE(TRANS_MSG, "malloc in trans proxy send message. channelId=%{public}d", channelId);
190 return SOFTBUS_MALLOC_ERR;
191 }
192 if (TransProxyGetChanByChanId(channelId, chan) != SOFTBUS_OK) {
193 SoftBusFree(chan);
194 return SOFTBUS_TRANS_PROXY_CHANNEL_NOT_FOUND;
195 }
196 *isServer = chan->isServer;
197 SoftBusFree(chan);
198 return SOFTBUS_OK;
199 }
200
TransProxyNotifyOpenFailedByType(const AppInfo * appInfo,AppType appType,int32_t channelId,int32_t errCode)201 static int32_t TransProxyNotifyOpenFailedByType(
202 const AppInfo *appInfo, AppType appType, int32_t channelId, int32_t errCode)
203 {
204 switch (appType) {
205 case APP_TYPE_NORMAL:
206 case APP_TYPE_AUTH:
207 return NotifyNormalChannelOpenFailed(appInfo->myData.pkgName, appInfo->myData.pid, channelId, errCode);
208 case APP_TYPE_INNER:
209 NotifyNetworkingChannelOpenFailed(appInfo->myData.sessionName, channelId, appInfo->peerData.deviceId);
210 return SOFTBUS_TRANS_NOTIFY_NETWORK_OPEN_ERR;
211 default:
212 TRANS_LOGE(TRANS_CTRL, "SOFTBUS_INVALID_APPTYPE appType=%{public}d", appType);
213 return SOFTBUS_INVALID_APPTYPE;
214 }
215 }
216
OnProxyChannelBind(int32_t channelId,const AppInfo * appInfo)217 int32_t OnProxyChannelBind(int32_t channelId, const AppInfo *appInfo)
218 {
219 int32_t ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
220 if (appInfo == NULL) {
221 TRANS_LOGE(TRANS_CTRL, "proxy channel bind app info invalid channelId=%{public}d", channelId);
222 return SOFTBUS_INVALID_PARAM;
223 }
224
225 switch (appInfo->appType) {
226 case APP_TYPE_NORMAL:
227 case APP_TYPE_AUTH:
228 ret = TransProxyOnChannelBind(appInfo->myData.pkgName, appInfo->myData.pid, channelId);
229 break;
230 case APP_TYPE_INNER:
231 ret = SOFTBUS_OK;
232 break;
233 default:
234 ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
235 break;
236 }
237 TRANS_LOGI(
238 TRANS_CTRL, "channelId=%{public}d, ret=%{public}d, appType=%{public}d", channelId, ret, appInfo->appType);
239 return ret;
240 }
241
TransNotifyAlarm(const AppInfo * appInfo,int32_t errCode)242 static void TransNotifyAlarm(const AppInfo *appInfo, int32_t errCode)
243 {
244 TransAlarmExtra extraAlarm = {
245 .conflictName = NULL,
246 .conflictedName = NULL,
247 .occupyedName = NULL,
248 .permissionName = NULL,
249 .linkType = appInfo->linkType,
250 .errcode = errCode,
251 .sessionName = appInfo->myData.sessionName,
252 };
253 TRANS_ALARM(OPEN_SESSION_FAIL_ALARM, CONTROL_ALARM_TYPE, extraAlarm);
254 return;
255 }
256
TransProxyReportOpenChannelFailEvent(int32_t channelId,const AppInfo * appInfo,int32_t errCode)257 static void TransProxyReportOpenChannelFailEvent(int32_t channelId, const AppInfo *appInfo, int32_t errCode)
258 {
259 if (appInfo->appType == APP_TYPE_INNER) {
260 TRANS_LOGI(TRANS_CTRL, "channelId=%{public}d is inner channel, no need report fail event", channelId);
261 return;
262 }
263
264 int64_t timeStart = appInfo->timeStart;
265 int64_t timediff = GetSoftbusRecordTimeMillis() - timeStart;
266 int8_t isServer;
267
268 if ((TransProxyGetChannelIsServer(channelId, &isServer) == SOFTBUS_OK && !isServer) || appInfo->isClient) {
269 char localUdid[UDID_BUF_LEN] = { 0 };
270 (void)LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, sizeof(localUdid));
271 TransEventExtra extra = {
272 .calleePkg = NULL,
273 .peerNetworkId = (appInfo->appType == APP_TYPE_AUTH) ? appInfo->peerData.deviceId : appInfo->peerNetWorkId,
274 .linkType = appInfo->connectType,
275 .channelId = channelId,
276 .costTime = timediff,
277 .errcode = errCode,
278 .callerPkg = appInfo->myData.pkgName,
279 .socketName = appInfo->myData.sessionName,
280 .localUdid = localUdid,
281 .peerUdid = appInfo->appType == APP_TYPE_AUTH ? appInfo->peerData.deviceId : appInfo->peerUdid,
282 .osType = (appInfo->osType < 0) ? UNKNOW_OS_TYPE : appInfo->osType,
283 .result = EVENT_STAGE_RESULT_FAILED
284 };
285 extra.deviceState = TransGetDeviceState(appInfo->peerNetWorkId);
286 if (appInfo->appType == APP_TYPE_AUTH && strlen(appInfo->peerVersion) == 0) {
287 TransGetRemoteDeviceVersion(extra.peerUdid, CATEGORY_UDID, (char *)(appInfo->peerVersion),
288 sizeof(appInfo->peerVersion));
289 }
290 extra.peerDevVer = appInfo->peerVersion;
291 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_END, extra);
292 TransNotifyAlarm(appInfo, errCode);
293 }
294 SoftbusRecordOpenSessionKpi(appInfo->myData.pkgName, appInfo->linkType, SOFTBUS_EVT_OPEN_SESSION_FAIL, timediff);
295 }
296
OnProxyChannelOpenFailed(int32_t channelId,const AppInfo * appInfo,int32_t errCode)297 int32_t OnProxyChannelOpenFailed(int32_t channelId, const AppInfo *appInfo, int32_t errCode)
298 {
299 if (appInfo == NULL) {
300 return SOFTBUS_INVALID_PARAM;
301 }
302
303 TransProxyReportOpenChannelFailEvent(channelId, appInfo, errCode);
304
305 char *tmpName = NULL;
306 Anonymize(appInfo->myData.sessionName, &tmpName);
307 TRANS_LOGI(TRANS_CTRL,
308 "proxy channel openfailed:sessionName=%{public}s, channelId=%{public}d, appType=%{public}d, errCode=%{public}d",
309 tmpName, channelId, appInfo->appType, errCode);
310 AnonymizeFree(tmpName);
311 return TransProxyNotifyOpenFailedByType(appInfo, appInfo->appType, channelId, errCode);
312 }
313
OnProxyChannelClosed(int32_t channelId,const AppInfo * appInfo)314 int32_t OnProxyChannelClosed(int32_t channelId, const AppInfo *appInfo)
315 {
316 if (appInfo == NULL) {
317 return SOFTBUS_INVALID_PARAM;
318 }
319 TRANS_LOGI(TRANS_CTRL,
320 "proxy channel closed: channelId=%{public}d, appType=%{public}d", channelId, appInfo->appType);
321
322 int32_t ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
323 switch (appInfo->appType) {
324 case APP_TYPE_NORMAL:
325 case APP_TYPE_AUTH:
326 ret = NotifyNormalChannelClosed(appInfo->myData.pkgName, appInfo->myData.pid, channelId);
327 break;
328 case APP_TYPE_INNER:
329 NotifyNetworkingChannelClosed(appInfo->myData.sessionName, channelId);
330 break;
331 default:
332 ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
333 break;
334 }
335 return ret;
336 }
337
OnProxyChannelMsgReceived(int32_t channelId,const AppInfo * appInfo,const char * data,uint32_t len)338 int32_t OnProxyChannelMsgReceived(int32_t channelId, const AppInfo *appInfo, const char *data,
339 uint32_t len)
340 {
341 int32_t ret = SOFTBUS_OK;
342 if (appInfo == NULL || data == NULL || len == 0) {
343 return SOFTBUS_INVALID_PARAM;
344 }
345
346 switch (appInfo->appType) {
347 case APP_TYPE_NORMAL:
348 case APP_TYPE_AUTH:
349 TransOnNormalMsgReceived(appInfo->myData.pkgName, appInfo->myData.pid, channelId, data, len);
350 break;
351 case APP_TYPE_INNER:
352 NotifyNetworkingMsgReceived(appInfo->myData.sessionName, channelId, data, len);
353 break;
354 default:
355 ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
356 break;
357 }
358 return ret;
359 }
360
TransProxyGetAppInfo(const char * sessionName,const char * peerNetworkId,AppInfo * appInfo)361 static int32_t TransProxyGetAppInfo(const char *sessionName, const char *peerNetworkId, AppInfo *appInfo)
362 {
363 int ret = SOFTBUS_TRANS_GET_APP_INFO_FAILED;
364 int32_t osType = 0;
365 GetOsTypeByNetworkId(peerNetworkId, &osType);
366 appInfo->osType = osType;
367 appInfo->appType = APP_TYPE_INNER;
368 appInfo->myData.apiVersion = API_V2;
369 appInfo->autoCloseTime = 0;
370 ret = LnnGetLocalStrInfo(STRING_KEY_UUID, appInfo->myData.deviceId, sizeof(appInfo->myData.deviceId));
371 if (ret != SOFTBUS_OK) {
372 TRANS_LOGE(TRANS_CTRL, "get local uuid fail. ret=%{public}d", ret);
373 return ret;
374 }
375 if (strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), sessionName) != EOK) {
376 TRANS_LOGE(TRANS_CTRL, "strcpy_s my sessionName failed");
377 return SOFTBUS_STRCPY_ERR;
378 }
379 appInfo->peerData.apiVersion = API_V2;
380 if (strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), sessionName) != EOK) {
381 TRANS_LOGE(TRANS_CTRL, "strcpy_s peer sessionName failed");
382 return SOFTBUS_STRCPY_ERR;
383 }
384 if (strcpy_s(appInfo->peerNetWorkId, sizeof(appInfo->peerNetWorkId), peerNetworkId) != EOK) {
385 TRANS_LOGE(TRANS_CTRL, "strcpy_s peerNetworkId failed");
386 return SOFTBUS_STRCPY_ERR;
387 }
388
389 ret = LnnGetRemoteStrInfo(peerNetworkId, STRING_KEY_UUID,
390 appInfo->peerData.deviceId, sizeof(appInfo->peerData.deviceId));
391 if (ret != SOFTBUS_OK) {
392 TRANS_LOGE(TRANS_CTRL, "get remote node uuid err. ret=%{public}d", ret);
393 return SOFTBUS_GET_REMOTE_UUID_ERR;
394 }
395
396 GetRemoteUdidWithNetworkId(peerNetworkId, appInfo->peerUdid, sizeof(appInfo->peerUdid));
397 TransGetRemoteDeviceVersion(peerNetworkId, CATEGORY_NETWORK_ID, appInfo->peerVersion, sizeof(appInfo->peerVersion));
398
399 return SOFTBUS_OK;
400 }
401
TransGetConnectOption(const char * peerNetworkId,ConnectOption * connOpt,const LanePreferredLinkList * preferred)402 static int32_t TransGetConnectOption(
403 const char *peerNetworkId, ConnectOption *connOpt, const LanePreferredLinkList *preferred)
404 {
405 uint32_t laneHandle = INVALID_LANE_REQ_ID;
406 LaneConnInfo connInfo;
407 LaneRequestOption option;
408 (void)memset_s(&option, sizeof(LaneRequestOption), 0, sizeof(LaneRequestOption));
409 #define DEFAULT_PID 0
410 option.type = LANE_TYPE_TRANS;
411 option.requestInfo.trans.pid = DEFAULT_PID;
412 option.requestInfo.trans.transType = LANE_T_MSG;
413 option.requestInfo.trans.expectedBw = 0;
414 option.requestInfo.trans.acceptableProtocols = LNN_PROTOCOL_ALL ^ LNN_PROTOCOL_NIP;
415 if (memcpy_s(option.requestInfo.trans.networkId, NETWORK_ID_BUF_LEN,
416 peerNetworkId, NETWORK_ID_BUF_LEN) != EOK) {
417 TRANS_LOGE(TRANS_CTRL, "memcpy networkId failed.");
418 return SOFTBUS_MEM_ERR;
419 }
420 if (preferred != NULL) {
421 for (uint32_t i = 0; i < preferred->linkTypeNum; i++) {
422 option.requestInfo.trans.expectedLink.linkType[i] = preferred->linkType[i];
423 }
424 option.requestInfo.trans.expectedLink.linkTypeNum = preferred->linkTypeNum;
425 }
426 if (TransGetLaneInfoByOption(&option, &connInfo, &laneHandle) != SOFTBUS_OK) {
427 goto EXIT_ERR;
428 }
429 TRANS_LOGI(TRANS_CTRL, "net channel lane info. laneHandle=%{public}u, type=%{public}d", laneHandle, connInfo.type);
430 if (TransGetConnectOptByConnInfo(&connInfo, connOpt) != SOFTBUS_OK) {
431 goto EXIT_ERR;
432 }
433 LnnFreeLane(laneHandle);
434 return SOFTBUS_OK;
435 EXIT_ERR:
436 if (laneHandle != 0) {
437 LnnFreeLane(laneHandle);
438 }
439 return SOFTBUS_TRANS_GET_LANE_INFO_ERR;
440 }
441
442
TransOpenNetWorkingChannel(const char * sessionName,const char * peerNetworkId,const LanePreferredLinkList * preferred)443 int32_t TransOpenNetWorkingChannel(
444 const char *sessionName, const char *peerNetworkId, const LanePreferredLinkList *preferred)
445 {
446 AppInfo appInfo;
447 ConnectOption connOpt;
448 int32_t channelId = INVALID_CHANNEL_ID;
449
450 if (!IsValidString(sessionName, SESSION_NAME_SIZE_MAX) ||
451 !IsValidString(peerNetworkId, DEVICE_ID_SIZE_MAX)) {
452 return channelId;
453 }
454 if (TransGetConnectOption(peerNetworkId, &connOpt, preferred) != SOFTBUS_OK) {
455 TRANS_LOGE(TRANS_CTRL, "networking get connect option fail");
456 return channelId;
457 }
458 (void)memset_s(&appInfo, sizeof(AppInfo), 0, sizeof(AppInfo));
459 if (TransProxyGetAppInfo(sessionName, peerNetworkId, &appInfo) != SOFTBUS_OK) {
460 TRANS_LOGE(TRANS_CTRL, "networking get app info fail");
461 return channelId;
462 }
463
464 if (TransProxyOpenProxyChannel(&appInfo, &connOpt, &channelId) != SOFTBUS_OK) {
465 TRANS_LOGE(TRANS_CTRL, "networking open channel fail");
466 channelId = INVALID_CHANNEL_ID;
467 }
468 return channelId;
469 }
470
TransSendNetworkingMessage(int32_t channelId,const char * data,uint32_t dataLen,int32_t priority)471 int32_t TransSendNetworkingMessage(int32_t channelId, const char *data, uint32_t dataLen,
472 int32_t priority)
473 {
474 ProxyChannelInfo *info = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
475 if (info == NULL) {
476 TRANS_LOGE(TRANS_MSG, "malloc in trans proxy send message. channelId=%{public}d", channelId);
477 return SOFTBUS_MALLOC_ERR;
478 }
479
480 int32_t ret = TransProxyGetSendMsgChanInfo(channelId, info);
481 (void)memset_s(info->appInfo.sessionKey, sizeof(info->appInfo.sessionKey), 0, sizeof(info->appInfo.sessionKey));
482 if (ret != SOFTBUS_OK) {
483 TRANS_LOGE(TRANS_MSG, "get proxy channelId failed. channelId=%{public}d", channelId);
484 SoftBusFree(info);
485 return SOFTBUS_TRANS_PROXY_INVALID_CHANNEL_ID;
486 }
487
488 if (info->status != PROXY_CHANNEL_STATUS_COMPLETED && info->status != PROXY_CHANNEL_STATUS_KEEPLIVEING) {
489 TRANS_LOGE(TRANS_MSG, "proxy channel status is err. status=%{public}d", info->status);
490 SoftBusFree(info);
491 return SOFTBUS_TRANS_PROXY_CHANNLE_STATUS_INVALID;
492 }
493
494 if (info->appInfo.appType != APP_TYPE_INNER) {
495 TRANS_LOGE(TRANS_MSG, "wrong appType=%{public}d", info->appInfo.appType);
496 SoftBusFree(info);
497 return SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
498 }
499
500 ret = TransProxySendInnerMessage(info, (char *)data, dataLen, priority);
501 SoftBusFree(info);
502 return ret;
503 }
504
TransCloseNetWorkingChannel(int32_t channelId)505 int32_t TransCloseNetWorkingChannel(int32_t channelId)
506 {
507 return TransProxyCloseProxyChannel(channelId);
508 }
509