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 GetOsTypeByNetworkId(info.peerDeviceId, &info.osType);
111 ret = TransProxyOnChannelOpened(appInfo->myData.pkgName, appInfo->myData.pid, appInfo->myData.sessionName, &info);
112 TRANS_LOGI(TRANS_CTRL, "proxy channel open, channelId=%{public}d, ret=%{public}d", channelId, ret);
113 return ret;
114 }
115
FillExtraByProxyChannelErrorEnd(TransEventExtra * extra,const AppInfo * appInfo,char * localUdid,uint32_t len)116 static void FillExtraByProxyChannelErrorEnd(TransEventExtra *extra, const AppInfo *appInfo, char *localUdid,
117 uint32_t len)
118 {
119 if (extra == NULL || appInfo == NULL || localUdid == NULL) {
120 TRANS_LOGE(TRANS_CTRL, "invalid param.");
121 return;
122 }
123 extra->socketName = appInfo->myData.sessionName;
124 extra->callerPkg = appInfo->myData.pkgName;
125 extra->linkType = appInfo->connectType;
126 if (appInfo->appType == APP_TYPE_AUTH && strlen(appInfo->peerVersion) == 0) {
127 TransGetRemoteDeviceVersion(extra->peerUdid, CATEGORY_UDID, (char *)(appInfo->peerVersion),
128 sizeof(appInfo->peerVersion));
129 }
130 extra->peerDevVer = appInfo->peerVersion;
131 (void)LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, len);
132 extra->localUdid = localUdid;
133 }
134
OnProxyChannelOpened(int32_t channelId,const AppInfo * appInfo,unsigned char isServer)135 int32_t OnProxyChannelOpened(int32_t channelId, const AppInfo *appInfo, unsigned char isServer)
136 {
137 int32_t ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
138 if (appInfo == NULL) {
139 TRANS_LOGE(TRANS_CTRL, "proxy channel opened app info invalid.");
140 return SOFTBUS_INVALID_PARAM;
141 }
142 TRANS_LOGI(TRANS_CTRL, "proxy channel opened: channelId=%{public}d, appType=%{public}d, isServer=%{public}d",
143 channelId, appInfo->appType, isServer);
144
145 switch (appInfo->appType) {
146 case APP_TYPE_NORMAL:
147 case APP_TYPE_AUTH:
148 ret = NotifyNormalChannelOpened(channelId, appInfo, isServer);
149 break;
150 case APP_TYPE_INNER:
151 ret = NotifyNetworkingChannelOpened(appInfo->myData.sessionName, channelId, appInfo, isServer);
152 break;
153 default:
154 ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
155 break;
156 }
157 TransEventExtra extra = {
158 .socketName = NULL,
159 .peerNetworkId = NULL,
160 .calleePkg = NULL,
161 .callerPkg = NULL,
162 .channelId = channelId,
163 .costTime = GetSoftbusRecordTimeMillis() - appInfo->connectedStart,
164 .errcode = ret,
165 .result = (ret == SOFTBUS_OK) ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED
166 };
167 extra.osType = (appInfo->osType < 0) ? UNKNOW_OS_TYPE : appInfo->osType;
168 extra.peerUdid = appInfo->peerUdid;
169 extra.deviceState = TransGetDeviceState(appInfo->peerNetWorkId);
170 if (!isServer) {
171 extra.peerUdid = appInfo->appType == APP_TYPE_AUTH ? appInfo->peerData.deviceId : extra.peerUdid;
172 if (extra.result == EVENT_STAGE_RESULT_FAILED) {
173 char localUdid[UDID_BUF_LEN] = { 0 };
174 FillExtraByProxyChannelErrorEnd(&extra, appInfo, localUdid, sizeof(localUdid));
175 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_END, extra);
176 } else {
177 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_REPLY, extra);
178 }
179 } else if (ret != SOFTBUS_OK) {
180 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_OPEN_CHANNEL_END, extra);
181 }
182 TRANS_LOGI(TRANS_CTRL, "on open, channelId=%{public}d, ret=%{public}d", channelId, ret);
183 return ret;
184 }
185
TransProxyGetChannelIsServer(int32_t channelId,int8_t * isServer)186 static int32_t TransProxyGetChannelIsServer(int32_t channelId, int8_t *isServer)
187 {
188 ProxyChannelInfo *chan = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
189 if (chan == NULL) {
190 TRANS_LOGE(TRANS_MSG, "malloc in trans proxy send message. channelId=%{public}d", channelId);
191 return SOFTBUS_MALLOC_ERR;
192 }
193 if (TransProxyGetChanByChanId(channelId, chan) != SOFTBUS_OK) {
194 SoftBusFree(chan);
195 return SOFTBUS_TRANS_PROXY_CHANNEL_NOT_FOUND;
196 }
197 *isServer = chan->isServer;
198 SoftBusFree(chan);
199 return SOFTBUS_OK;
200 }
201
TransProxyNotifyOpenFailedByType(const AppInfo * appInfo,AppType appType,int32_t channelId,int32_t errCode)202 static int32_t TransProxyNotifyOpenFailedByType(
203 const AppInfo *appInfo, AppType appType, int32_t channelId, int32_t errCode)
204 {
205 switch (appType) {
206 case APP_TYPE_NORMAL:
207 case APP_TYPE_AUTH:
208 return NotifyNormalChannelOpenFailed(appInfo->myData.pkgName, appInfo->myData.pid, channelId, errCode);
209 case APP_TYPE_INNER:
210 NotifyNetworkingChannelOpenFailed(appInfo->myData.sessionName, channelId, appInfo->peerData.deviceId);
211 return SOFTBUS_TRANS_NOTIFY_NETWORK_OPEN_ERR;
212 default:
213 TRANS_LOGE(TRANS_CTRL, "SOFTBUS_INVALID_APPTYPE appType=%{public}d", appType);
214 return SOFTBUS_INVALID_APPTYPE;
215 }
216 }
217
OnProxyChannelBind(int32_t channelId,const AppInfo * appInfo)218 int32_t OnProxyChannelBind(int32_t channelId, const AppInfo *appInfo)
219 {
220 int32_t ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
221 if (appInfo == NULL) {
222 TRANS_LOGE(TRANS_CTRL, "proxy channel bind app info invalid channelId=%{public}d", channelId);
223 return SOFTBUS_INVALID_PARAM;
224 }
225
226 switch (appInfo->appType) {
227 case APP_TYPE_NORMAL:
228 case APP_TYPE_AUTH:
229 ret = TransProxyOnChannelBind(appInfo->myData.pkgName, appInfo->myData.pid, channelId);
230 break;
231 case APP_TYPE_INNER:
232 ret = SOFTBUS_OK;
233 break;
234 default:
235 ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
236 break;
237 }
238 TRANS_LOGI(
239 TRANS_CTRL, "channelId=%{public}d, ret=%{public}d, appType=%{public}d", channelId, ret, appInfo->appType);
240 return ret;
241 }
242
TransNotifyAlarm(const AppInfo * appInfo,int32_t errCode)243 static void TransNotifyAlarm(const AppInfo *appInfo, int32_t errCode)
244 {
245 TransAlarmExtra extraAlarm = {
246 .conflictName = NULL,
247 .conflictedName = NULL,
248 .occupyedName = NULL,
249 .permissionName = NULL,
250 .linkType = appInfo->linkType,
251 .errcode = errCode,
252 .sessionName = appInfo->myData.sessionName,
253 };
254 TRANS_ALARM(OPEN_SESSION_FAIL_ALARM, CONTROL_ALARM_TYPE, extraAlarm);
255 return;
256 }
257
TransProxyReportOpenChannelFailEvent(int32_t channelId,const AppInfo * appInfo,int32_t errCode)258 static void TransProxyReportOpenChannelFailEvent(int32_t channelId, const AppInfo *appInfo, int32_t errCode)
259 {
260 if (appInfo->appType == APP_TYPE_INNER) {
261 TRANS_LOGI(TRANS_CTRL, "channelId=%{public}d is inner channel, no need report fail event", channelId);
262 return;
263 }
264
265 int64_t timeStart = appInfo->timeStart;
266 int64_t timediff = GetSoftbusRecordTimeMillis() - timeStart;
267 int8_t isServer;
268
269 if ((TransProxyGetChannelIsServer(channelId, &isServer) == SOFTBUS_OK && !isServer) || appInfo->isClient) {
270 char localUdid[UDID_BUF_LEN] = { 0 };
271 (void)LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, sizeof(localUdid));
272 TransEventExtra extra = {
273 .calleePkg = NULL,
274 .peerNetworkId = (appInfo->appType == APP_TYPE_AUTH) ? appInfo->peerData.deviceId : appInfo->peerNetWorkId,
275 .linkType = appInfo->connectType,
276 .channelId = channelId,
277 .costTime = timediff,
278 .errcode = errCode,
279 .callerPkg = appInfo->myData.pkgName,
280 .socketName = appInfo->myData.sessionName,
281 .localUdid = localUdid,
282 .peerUdid = appInfo->appType == APP_TYPE_AUTH ? appInfo->peerData.deviceId : appInfo->peerUdid,
283 .osType = (appInfo->osType < 0) ? UNKNOW_OS_TYPE : appInfo->osType,
284 .result = EVENT_STAGE_RESULT_FAILED
285 };
286 extra.deviceState = TransGetDeviceState(appInfo->peerNetWorkId);
287 if (appInfo->appType == APP_TYPE_AUTH && strlen(appInfo->peerVersion) == 0) {
288 TransGetRemoteDeviceVersion(extra.peerUdid, CATEGORY_UDID, (char *)(appInfo->peerVersion),
289 sizeof(appInfo->peerVersion));
290 }
291 extra.peerDevVer = appInfo->peerVersion;
292 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_END, extra);
293 TransNotifyAlarm(appInfo, errCode);
294 }
295 SoftbusRecordOpenSessionKpi(appInfo->myData.pkgName, appInfo->linkType, SOFTBUS_EVT_OPEN_SESSION_FAIL, timediff);
296 }
297
OnProxyChannelOpenFailed(int32_t channelId,const AppInfo * appInfo,int32_t errCode)298 int32_t OnProxyChannelOpenFailed(int32_t channelId, const AppInfo *appInfo, int32_t errCode)
299 {
300 if (appInfo == NULL) {
301 return SOFTBUS_INVALID_PARAM;
302 }
303
304 TransProxyReportOpenChannelFailEvent(channelId, appInfo, errCode);
305
306 char *tmpName = NULL;
307 Anonymize(appInfo->myData.sessionName, &tmpName);
308 TRANS_LOGI(TRANS_CTRL,
309 "proxy channel openfailed:sessionName=%{public}s, channelId=%{public}d, appType=%{public}d, errCode=%{public}d",
310 tmpName, channelId, appInfo->appType, errCode);
311 AnonymizeFree(tmpName);
312 return TransProxyNotifyOpenFailedByType(appInfo, appInfo->appType, channelId, errCode);
313 }
314
OnProxyChannelClosed(int32_t channelId,const AppInfo * appInfo)315 int32_t OnProxyChannelClosed(int32_t channelId, const AppInfo *appInfo)
316 {
317 if (appInfo == NULL) {
318 return SOFTBUS_INVALID_PARAM;
319 }
320 TRANS_LOGI(TRANS_CTRL,
321 "proxy channel closed: channelId=%{public}d, appType=%{public}d", channelId, appInfo->appType);
322
323 int32_t ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
324 switch (appInfo->appType) {
325 case APP_TYPE_NORMAL:
326 case APP_TYPE_AUTH:
327 ret = NotifyNormalChannelClosed(appInfo->myData.pkgName, appInfo->myData.pid, channelId);
328 break;
329 case APP_TYPE_INNER:
330 NotifyNetworkingChannelClosed(appInfo->myData.sessionName, channelId);
331 break;
332 default:
333 ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
334 break;
335 }
336 return ret;
337 }
338
OnProxyChannelMsgReceived(int32_t channelId,const AppInfo * appInfo,const char * data,uint32_t len)339 int32_t OnProxyChannelMsgReceived(int32_t channelId, const AppInfo *appInfo, const char *data,
340 uint32_t len)
341 {
342 int32_t ret = SOFTBUS_OK;
343 if (appInfo == NULL || data == NULL || len == 0) {
344 return SOFTBUS_INVALID_PARAM;
345 }
346
347 switch (appInfo->appType) {
348 case APP_TYPE_NORMAL:
349 case APP_TYPE_AUTH:
350 TransOnNormalMsgReceived(appInfo->myData.pkgName, appInfo->myData.pid, channelId, data, len);
351 break;
352 case APP_TYPE_INNER:
353 NotifyNetworkingMsgReceived(appInfo->myData.sessionName, channelId, data, len);
354 break;
355 default:
356 ret = SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
357 break;
358 }
359 return ret;
360 }
361
TransProxyGetAppInfo(const char * sessionName,const char * peerNetworkId,AppInfo * appInfo)362 static int32_t TransProxyGetAppInfo(const char *sessionName, const char *peerNetworkId, AppInfo *appInfo)
363 {
364 int ret = SOFTBUS_TRANS_GET_APP_INFO_FAILED;
365 int32_t osType = 0;
366 GetOsTypeByNetworkId(peerNetworkId, &osType);
367 appInfo->osType = osType;
368 appInfo->appType = APP_TYPE_INNER;
369 appInfo->myData.apiVersion = API_V2;
370 appInfo->autoCloseTime = 0;
371 ret = LnnGetLocalStrInfo(STRING_KEY_UUID, appInfo->myData.deviceId, sizeof(appInfo->myData.deviceId));
372 if (ret != SOFTBUS_OK) {
373 TRANS_LOGE(TRANS_CTRL, "get local uuid fail. ret=%{public}d", ret);
374 return ret;
375 }
376 if (strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), sessionName) != EOK) {
377 TRANS_LOGE(TRANS_CTRL, "strcpy_s my sessionName failed");
378 return SOFTBUS_STRCPY_ERR;
379 }
380 appInfo->peerData.apiVersion = API_V2;
381 if (strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), sessionName) != EOK) {
382 TRANS_LOGE(TRANS_CTRL, "strcpy_s peer sessionName failed");
383 return SOFTBUS_STRCPY_ERR;
384 }
385 if (strcpy_s(appInfo->peerNetWorkId, sizeof(appInfo->peerNetWorkId), peerNetworkId) != EOK) {
386 TRANS_LOGE(TRANS_CTRL, "strcpy_s peerNetworkId failed");
387 return SOFTBUS_STRCPY_ERR;
388 }
389
390 ret = LnnGetRemoteStrInfo(peerNetworkId, STRING_KEY_UUID,
391 appInfo->peerData.deviceId, sizeof(appInfo->peerData.deviceId));
392 if (ret != SOFTBUS_OK) {
393 TRANS_LOGE(TRANS_CTRL, "get remote node uuid err. ret=%{public}d", ret);
394 return SOFTBUS_GET_REMOTE_UUID_ERR;
395 }
396
397 GetRemoteUdidWithNetworkId(peerNetworkId, appInfo->peerUdid, sizeof(appInfo->peerUdid));
398 TransGetRemoteDeviceVersion(peerNetworkId, CATEGORY_NETWORK_ID, appInfo->peerVersion, sizeof(appInfo->peerVersion));
399
400 return SOFTBUS_OK;
401 }
402
TransGetConnectOption(const char * peerNetworkId,ConnectOption * connOpt,const LanePreferredLinkList * preferred)403 static int32_t TransGetConnectOption(
404 const char *peerNetworkId, ConnectOption *connOpt, const LanePreferredLinkList *preferred)
405 {
406 uint32_t laneHandle = INVALID_LANE_REQ_ID;
407 LaneConnInfo connInfo;
408 LaneRequestOption option;
409 (void)memset_s(&option, sizeof(LaneRequestOption), 0, sizeof(LaneRequestOption));
410 #define DEFAULT_PID 0
411 option.type = LANE_TYPE_TRANS;
412 option.requestInfo.trans.pid = DEFAULT_PID;
413 option.requestInfo.trans.transType = LANE_T_MSG;
414 option.requestInfo.trans.expectedBw = 0;
415 option.requestInfo.trans.acceptableProtocols = LNN_PROTOCOL_ALL ^ LNN_PROTOCOL_NIP;
416 if (memcpy_s(option.requestInfo.trans.networkId, NETWORK_ID_BUF_LEN,
417 peerNetworkId, NETWORK_ID_BUF_LEN) != EOK) {
418 TRANS_LOGE(TRANS_CTRL, "memcpy networkId failed.");
419 return SOFTBUS_MEM_ERR;
420 }
421 if (preferred != NULL) {
422 for (uint32_t i = 0; i < preferred->linkTypeNum; i++) {
423 option.requestInfo.trans.expectedLink.linkType[i] = preferred->linkType[i];
424 }
425 option.requestInfo.trans.expectedLink.linkTypeNum = preferred->linkTypeNum;
426 }
427 if (TransGetLaneInfoByOption(&option, &connInfo, &laneHandle) != SOFTBUS_OK) {
428 goto EXIT_ERR;
429 }
430 TRANS_LOGI(TRANS_CTRL, "net channel lane info. laneHandle=%{public}u, type=%{public}d", laneHandle, connInfo.type);
431 if (TransGetConnectOptByConnInfo(&connInfo, connOpt) != SOFTBUS_OK) {
432 goto EXIT_ERR;
433 }
434 LnnFreeLane(laneHandle);
435 return SOFTBUS_OK;
436 EXIT_ERR:
437 if (laneHandle != 0) {
438 LnnFreeLane(laneHandle);
439 }
440 return SOFTBUS_TRANS_GET_LANE_INFO_ERR;
441 }
442
443
TransOpenNetWorkingChannel(const char * sessionName,const char * peerNetworkId,const LanePreferredLinkList * preferred)444 int32_t TransOpenNetWorkingChannel(
445 const char *sessionName, const char *peerNetworkId, const LanePreferredLinkList *preferred)
446 {
447 AppInfo appInfo;
448 ConnectOption connOpt;
449 int32_t channelId = INVALID_CHANNEL_ID;
450
451 if (!IsValidString(sessionName, SESSION_NAME_SIZE_MAX) ||
452 !IsValidString(peerNetworkId, DEVICE_ID_SIZE_MAX)) {
453 return channelId;
454 }
455 if (TransGetConnectOption(peerNetworkId, &connOpt, preferred) != SOFTBUS_OK) {
456 TRANS_LOGE(TRANS_CTRL, "networking get connect option fail");
457 return channelId;
458 }
459 (void)memset_s(&appInfo, sizeof(AppInfo), 0, sizeof(AppInfo));
460 if (TransProxyGetAppInfo(sessionName, peerNetworkId, &appInfo) != SOFTBUS_OK) {
461 TRANS_LOGE(TRANS_CTRL, "networking get app info fail");
462 return channelId;
463 }
464
465 if (TransProxyOpenProxyChannel(&appInfo, &connOpt, &channelId) != SOFTBUS_OK) {
466 TRANS_LOGE(TRANS_CTRL, "networking open channel fail");
467 channelId = INVALID_CHANNEL_ID;
468 }
469 return channelId;
470 }
471
TransSendNetworkingMessage(int32_t channelId,const char * data,uint32_t dataLen,int32_t priority)472 int32_t TransSendNetworkingMessage(int32_t channelId, const char *data, uint32_t dataLen,
473 int32_t priority)
474 {
475 ProxyChannelInfo *info = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
476 if (info == NULL) {
477 TRANS_LOGE(TRANS_MSG, "malloc in trans proxy send message. channelId=%{public}d", channelId);
478 return SOFTBUS_MALLOC_ERR;
479 }
480
481 int32_t ret = TransProxyGetSendMsgChanInfo(channelId, info);
482 (void)memset_s(info->appInfo.sessionKey, sizeof(info->appInfo.sessionKey), 0, sizeof(info->appInfo.sessionKey));
483 if (ret != SOFTBUS_OK) {
484 TRANS_LOGE(TRANS_MSG, "get proxy channelId failed. channelId=%{public}d", channelId);
485 SoftBusFree(info);
486 return SOFTBUS_TRANS_PROXY_INVALID_CHANNEL_ID;
487 }
488
489 if (info->status != PROXY_CHANNEL_STATUS_COMPLETED && info->status != PROXY_CHANNEL_STATUS_KEEPLIVEING) {
490 TRANS_LOGE(TRANS_MSG, "proxy channel status is err. status=%{public}d", info->status);
491 SoftBusFree(info);
492 return SOFTBUS_TRANS_PROXY_CHANNLE_STATUS_INVALID;
493 }
494
495 if (info->appInfo.appType != APP_TYPE_INNER) {
496 TRANS_LOGE(TRANS_MSG, "wrong appType=%{public}d", info->appInfo.appType);
497 SoftBusFree(info);
498 return SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
499 }
500
501 ret = TransProxySendInnerMessage(info, (char *)data, dataLen, priority);
502 SoftBusFree(info);
503 return ret;
504 }
505
TransCloseNetWorkingChannel(int32_t channelId)506 int32_t TransCloseNetWorkingChannel(int32_t channelId)
507 {
508 return TransProxyCloseProxyChannel(channelId);
509 }
510