1 /*
2 * Copyright (c) 2022 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_lane_pending_ctl.h"
17
18 #include <securec.h>
19 #include "auth_interface.h"
20 #include "bus_center_info_key.h"
21 #include "bus_center_manager.h"
22 #include "common_list.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_adapter_thread.h"
25 #include "softbus_def.h"
26 #include "softbus_errcode.h"
27 #include "softbus_log.h"
28 #include "softbus_utils.h"
29 #include "trans_session_manager.h"
30 #include "lnn_distributed_net_ledger.h"
31
32 #define TRANS_REQUEST_PENDING_TIMEOUT (5000)
33 #define SESSION_NAME_PHONEPAD "com.huawei.pcassistant.phonepad-connect-channel"
34 #define SESSION_NAME_CASTPLUS "CastPlusSessionName"
35
36 typedef struct {
37 ListNode node;
38 uint32_t laneId;
39 SoftBusCond cond;
40 bool bSucc;
41 bool isFinished;
42 LaneConnInfo connInfo;
43 } TransReqLaneItem;
44
45 static SoftBusList *g_reqLanePendingList = NULL;
46
TransReqLanePendingInit(void)47 NO_SANITIZE("cfi") int32_t TransReqLanePendingInit(void)
48 {
49 g_reqLanePendingList = CreateSoftBusList();
50 if (g_reqLanePendingList == NULL) {
51 return SOFTBUS_ERR;
52 }
53
54 return SOFTBUS_OK;
55 }
56
TransReqLanePendingDeinit(void)57 NO_SANITIZE("cfi") void TransReqLanePendingDeinit(void)
58 {
59 if (g_reqLanePendingList == NULL) {
60 return;
61 }
62
63 if (SoftBusMutexLock(&g_reqLanePendingList->lock) != SOFTBUS_OK) {
64 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed.");
65 return;
66 }
67
68 TransReqLaneItem *item = NULL;
69 TransReqLaneItem *next = NULL;
70 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_reqLanePendingList->list, TransReqLaneItem, node) {
71 (void)SoftBusCondDestroy(&item->cond);
72 ListDelete(&item->node);
73 SoftBusFree(item);
74 }
75 (void)SoftBusMutexUnlock(&g_reqLanePendingList->lock);
76 DestroySoftBusList(g_reqLanePendingList);
77 g_reqLanePendingList = NULL;
78 }
79
TransDelLaneReqFromPendingList(uint32_t laneId)80 static int32_t TransDelLaneReqFromPendingList(uint32_t laneId)
81 {
82 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "del tran request from pending [lane=%u].", laneId);
83 if (g_reqLanePendingList == NULL) {
84 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lane request list hasn't initialized.");
85 return SOFTBUS_ERR;
86 }
87
88 if (SoftBusMutexLock(&(g_reqLanePendingList->lock)) != SOFTBUS_OK) {
89 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed");
90 return SOFTBUS_LOCK_ERR;
91 }
92
93 TransReqLaneItem *laneItem = NULL;
94 TransReqLaneItem *next = NULL;
95 LIST_FOR_EACH_ENTRY_SAFE(laneItem, next, &(g_reqLanePendingList->list), TransReqLaneItem, node) {
96 if (laneItem->laneId == laneId) {
97 (void)SoftBusCondDestroy(&laneItem->cond);
98 ListDelete(&(laneItem->node));
99 g_reqLanePendingList->cnt--;
100 SoftBusFree(laneItem);
101 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
102 return SOFTBUS_OK;
103 }
104 }
105 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
106 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans lane request not found, [laneId=%u].", laneId);
107 return SOFTBUS_ERR;
108 }
109
TransAddLaneReqFromPendingList(uint32_t laneId)110 static int32_t TransAddLaneReqFromPendingList(uint32_t laneId)
111 {
112 if (g_reqLanePendingList == NULL) {
113 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lane pending list no initialized.");
114 return SOFTBUS_ERR;
115 }
116
117 TransReqLaneItem *item = (TransReqLaneItem *)SoftBusCalloc(sizeof(TransReqLaneItem));
118 if (item == NULL) {
119 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "malloc lane request item err.");
120 return SOFTBUS_MALLOC_ERR;
121 }
122 item->laneId = laneId;
123 item->bSucc = false;
124 item->isFinished = false;
125 (void)memset_s(&(item->connInfo), sizeof(LaneConnInfo), 0, sizeof(LaneConnInfo));
126
127 if (SoftBusMutexLock(&g_reqLanePendingList->lock) != SOFTBUS_OK) {
128 SoftBusFree(item);
129 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed.");
130 return SOFTBUS_LOCK_ERR;
131 }
132 if (SoftBusCondInit(&item->cond) != 0) {
133 SoftBusFree(item);
134 (void)SoftBusMutexUnlock(&g_reqLanePendingList->lock);
135 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "cond init failed.");
136 return SOFTBUS_ERR;
137 }
138 ListInit(&(item->node));
139 ListAdd(&(g_reqLanePendingList->list), &(item->node));
140 g_reqLanePendingList->cnt++;
141 (void)SoftBusMutexUnlock(&g_reqLanePendingList->lock);
142
143 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "add tran request to pending [lane=%u].", laneId);
144 return SOFTBUS_OK;
145 }
146
TransGetLaneReqItemByLaneId(uint32_t laneId,bool * bSucc,LaneConnInfo * connInfo)147 static int32_t TransGetLaneReqItemByLaneId(uint32_t laneId, bool *bSucc, LaneConnInfo *connInfo)
148 {
149 if (bSucc == NULL || connInfo == NULL) {
150 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "param err.");
151 return SOFTBUS_ERR;
152 }
153 if (g_reqLanePendingList == NULL) {
154 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lane request list hasn't initialized.");
155 return SOFTBUS_ERR;
156 }
157 if (SoftBusMutexLock(&(g_reqLanePendingList->lock)) != SOFTBUS_OK) {
158 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed.");
159 return SOFTBUS_LOCK_ERR;
160 }
161
162 TransReqLaneItem *item = NULL;
163 LIST_FOR_EACH_ENTRY(item, &(g_reqLanePendingList->list), TransReqLaneItem, node) {
164 if (item->laneId == laneId) {
165 *bSucc = item->bSucc;
166 if (memcpy_s(connInfo, sizeof(LaneConnInfo), &(item->connInfo), sizeof(LaneConnInfo)) != EOK) {
167 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
168 return SOFTBUS_ERR;
169 }
170 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
171 return SOFTBUS_OK;
172 }
173 }
174 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
175 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans lane request not found.[laneId=%u].", laneId);
176 return SOFTBUS_ERR;
177 }
178
TransUpdateLaneConnInfoByLaneId(uint32_t laneId,bool bSucc,const LaneConnInfo * connInfo)179 static int32_t TransUpdateLaneConnInfoByLaneId(uint32_t laneId, bool bSucc, const LaneConnInfo *connInfo)
180 {
181 if (g_reqLanePendingList == NULL) {
182 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lane request list hasn't initialized.");
183 return SOFTBUS_ERR;
184 }
185 if (SoftBusMutexLock(&(g_reqLanePendingList->lock)) != SOFTBUS_OK) {
186 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed.");
187 return SOFTBUS_LOCK_ERR;
188 }
189
190 TransReqLaneItem *item = NULL;
191 LIST_FOR_EACH_ENTRY(item, &(g_reqLanePendingList->list), TransReqLaneItem, node) {
192 if (item->laneId == laneId) {
193 item->bSucc = bSucc;
194 if ((connInfo != NULL) &&
195 (memcpy_s(&(item->connInfo), sizeof(LaneConnInfo), connInfo, sizeof(LaneConnInfo)) != EOK)) {
196 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
197 return SOFTBUS_ERR;
198 }
199 item->isFinished = true;
200 (void)SoftBusCondSignal(&item->cond);
201 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
202 return SOFTBUS_OK;
203 }
204 }
205 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
206 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans lane request not found.[laneId=%u].", laneId);
207 return SOFTBUS_ERR;
208 }
209
TransOnLaneRequestSuccess(uint32_t laneId,const LaneConnInfo * connInfo)210 static void TransOnLaneRequestSuccess(uint32_t laneId, const LaneConnInfo *connInfo)
211 {
212 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "trans on lane[%u] request success.", laneId);
213 if (TransUpdateLaneConnInfoByLaneId(laneId, true, connInfo) != SOFTBUS_OK) {
214 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "update lane connInfo failed, id[%u].", laneId);
215 }
216 return;
217 }
218
TransOnLaneRequestFail(uint32_t laneId,LaneRequestFailReason reason)219 static void TransOnLaneRequestFail(uint32_t laneId, LaneRequestFailReason reason)
220 {
221 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "trans on lane[%u] request failed, reason[%u].", laneId, reason);
222 if (TransUpdateLaneConnInfoByLaneId(laneId, false, NULL) != SOFTBUS_OK) {
223 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "update lane connInfo failed, id[%u].", laneId);
224 }
225 return;
226 }
227
TransOnLaneStateChange(uint32_t laneId,LaneState state)228 static void TransOnLaneStateChange(uint32_t laneId, LaneState state)
229 {
230 /* current no treatment */
231 (void)laneId;
232 (void)state;
233 return;
234 }
235
GetStreamLaneType(int32_t streamType)236 static LaneTransType GetStreamLaneType(int32_t streamType)
237 {
238 switch (streamType) {
239 case RAW_STREAM:
240 return LANE_T_RAW_STREAM;
241 case COMMON_VIDEO_STREAM:
242 return LANE_T_COMMON_VIDEO;
243 case COMMON_AUDIO_STREAM:
244 return LANE_T_COMMON_VOICE;
245 default:
246 break;
247 }
248 return LANE_T_BUTT;
249 }
250
TransGetLaneTransTypeBySession(const SessionParam * param)251 NO_SANITIZE("cfi") LaneTransType TransGetLaneTransTypeBySession(const SessionParam *param)
252 {
253 if (param == NULL) {
254 return LANE_T_BUTT;
255 }
256 int32_t type = param->attr->dataType;
257 int32_t streamType;
258 switch (type) {
259 case TYPE_MESSAGE:
260 return LANE_T_MSG;
261 case TYPE_BYTES:
262 return LANE_T_BYTE;
263 case TYPE_FILE:
264 return LANE_T_FILE;
265 case TYPE_STREAM:
266 streamType = param->attr->attr.streamAttr.streamType;
267 return GetStreamLaneType(streamType);
268 default:
269 break;
270 }
271
272 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "session type:[%u] no support.", type);
273 return LANE_T_BUTT;
274 }
275
276 static const LaneLinkType g_laneMap[LINK_TYPE_MAX + 1] = {
277 LANE_LINK_TYPE_BUTT,
278 LANE_WLAN_5G,
279 LANE_WLAN_2P4G,
280 LANE_P2P,
281 LANE_BR,
282 LANE_BLE,
283 LANE_P2P_REUSE,
284 LANE_BLE_DIRECT,
285 LANE_COC,
286 LANE_COC_DIRECT,
287 };
TransGetLaneLinkTypeBySessionLinkType(LinkType type)288 static LaneLinkType TransGetLaneLinkTypeBySessionLinkType(LinkType type)
289 {
290 return g_laneMap[type];
291 }
292
TransformSessionPreferredToLanePreferred(const SessionParam * param,LanePreferredLinkList * preferred,TransOption * transOption)293 static void TransformSessionPreferredToLanePreferred(const SessionParam *param,
294 LanePreferredLinkList *preferred, TransOption *transOption)
295 {
296 if (param->attr->linkTypeNum <= 0 || param->attr->linkTypeNum > LINK_TYPE_MAX) {
297 preferred->linkTypeNum = 0;
298 return;
299 }
300 preferred->linkTypeNum = 0;
301 for (int32_t i = 0; i < param->attr->linkTypeNum; ++i) {
302 LaneLinkType linkType = TransGetLaneLinkTypeBySessionLinkType(param->attr->linkType[i]);
303 if (linkType == LANE_LINK_TYPE_BUTT) {
304 continue;
305 }
306 if (preferred->linkTypeNum >= LINK_TYPE_MAX) {
307 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
308 "session preferred linknum override lane maxcnt:%d.", LANE_LINK_TYPE_BUTT);
309 break;
310 }
311 preferred->linkType[preferred->linkTypeNum] = linkType;
312 preferred->linkTypeNum += 1;
313 }
314 return;
315 }
316
GetRequestOptionBySessionParam(const SessionParam * param,LaneRequestOption * requestOption)317 static int32_t GetRequestOptionBySessionParam(const SessionParam *param, LaneRequestOption *requestOption)
318 {
319 requestOption->type = LANE_TYPE_TRANS;
320 if (memcpy_s(requestOption->requestInfo.trans.networkId, NETWORK_ID_BUF_LEN,
321 param->peerDeviceId, NETWORK_ID_BUF_LEN) != EOK) {
322 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "memcpy networkId failed.");
323 return SOFTBUS_ERR;
324 }
325
326 LaneTransType transType = TransGetLaneTransTypeBySession(param);
327 if (transType == LANE_T_BUTT) {
328 return SOFTBUS_ERR;
329 }
330 requestOption->requestInfo.trans.networkDelegate = false;
331 if (strcmp(param->sessionName, SESSION_NAME_PHONEPAD) == 0 ||
332 strcmp(param->sessionName, SESSION_NAME_CASTPLUS) == 0) {
333 requestOption->requestInfo.trans.networkDelegate = true;
334 }
335 requestOption->requestInfo.trans.transType = transType;
336 requestOption->requestInfo.trans.expectedBw = 0; /* init expectBW */
337 requestOption->requestInfo.trans.acceptableProtocols = LNN_PROTOCOL_ALL ^ LNN_PROTOCOL_NIP;
338
339 NodeInfo *info = LnnGetNodeInfoById(requestOption->requestInfo.trans.networkId, CATEGORY_NETWORK_ID);
340 if (info != NULL && LnnHasDiscoveryType(info, DISCOVERY_TYPE_LSA)) {
341 requestOption->requestInfo.trans.acceptableProtocols |= LNN_PROTOCOL_NIP;
342 }
343
344 int32_t uid;
345 if (TransGetUidAndPid(param->sessionName, &uid, &(requestOption->requestInfo.trans.pid)) != SOFTBUS_OK) {
346 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "transGetUidAndPid failed.");
347 return SOFTBUS_ERR;
348 }
349
350 TransformSessionPreferredToLanePreferred(param, &(requestOption->requestInfo.trans.expectedLink),
351 &requestOption->requestInfo.trans);
352 return SOFTBUS_OK;
353 }
354
TransSoftBusCondWait(SoftBusCond * cond,SoftBusMutex * mutex,uint32_t timeMillis)355 static int32_t TransSoftBusCondWait(SoftBusCond *cond, SoftBusMutex *mutex, uint32_t timeMillis)
356 {
357 #define CONVERSION_BASE 1000LL
358 if (timeMillis == 0) {
359 return SoftBusCondWait(cond, mutex, NULL);
360 }
361
362 SoftBusSysTime now;
363 if (SoftBusGetTime(&now) != SOFTBUS_OK) {
364 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans softbus get time failed.");
365 return SOFTBUS_ERR;
366 }
367 int64_t usTime = now.sec * CONVERSION_BASE * CONVERSION_BASE + now.usec + timeMillis * CONVERSION_BASE;
368 SoftBusSysTime tv;
369 tv.sec = usTime / CONVERSION_BASE / CONVERSION_BASE;
370 tv.usec = usTime % (CONVERSION_BASE * CONVERSION_BASE);
371 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "start wait cond endSecond:%lld.", tv.sec);
372 return SoftBusCondWait(cond, mutex, &tv);
373 }
374
TransWaitingRequestCallback(uint32_t laneId)375 static int32_t TransWaitingRequestCallback(uint32_t laneId)
376 {
377 if (g_reqLanePendingList == NULL) {
378 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lane request list hasn't initialized.");
379 return SOFTBUS_ERR;
380 }
381 if (SoftBusMutexLock(&(g_reqLanePendingList->lock)) != SOFTBUS_OK) {
382 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed.");
383 return SOFTBUS_LOCK_ERR;
384 }
385 bool isFound = false;
386 TransReqLaneItem *item = NULL;
387 LIST_FOR_EACH_ENTRY(item, &(g_reqLanePendingList->list), TransReqLaneItem, node) {
388 if (item->laneId == laneId) {
389 isFound = true;
390 break;
391 }
392 }
393 if (!isFound) {
394 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
395 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "not found lane[%u] in pending.", laneId);
396 return SOFTBUS_ERR;
397 }
398 if (item->isFinished == false) {
399 int32_t rc = TransSoftBusCondWait(&item->cond, &g_reqLanePendingList->lock, 0);
400 if (rc != SOFTBUS_OK) {
401 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
402 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "wait cond failed laneId[%u].", laneId);
403 return rc;
404 }
405 }
406 (void)SoftBusMutexUnlock(&(g_reqLanePendingList->lock));
407 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "receive lane cond laneId[%u].", laneId);
408 return SOFTBUS_OK;
409 }
410
TransAddLaneReqToPendingAndWaiting(uint32_t laneId,const LaneRequestOption * requestOption)411 static int32_t TransAddLaneReqToPendingAndWaiting(uint32_t laneId, const LaneRequestOption *requestOption)
412 {
413 if (requestOption == NULL) {
414 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "param error.");
415 return SOFTBUS_ERR;
416 }
417
418 int32_t ret = TransAddLaneReqFromPendingList(laneId);
419 if (ret != SOFTBUS_OK) {
420 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add lane[%u] to pending failed.", laneId);
421 return SOFTBUS_ERR;
422 }
423
424 ILaneListener listener;
425 listener.OnLaneRequestSuccess = TransOnLaneRequestSuccess;
426 listener.OnLaneRequestFail = TransOnLaneRequestFail;
427 listener.OnLaneStateChange = TransOnLaneStateChange;
428 if (LnnRequestLane(laneId, requestOption, &listener) != SOFTBUS_OK) {
429 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans request lane failed.");
430 (void)TransDelLaneReqFromPendingList(laneId);
431 return SOFTBUS_ERR;
432 }
433 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "add lane[%u] to pending and start waiting.", laneId);
434 if (TransWaitingRequestCallback(laneId) != SOFTBUS_OK) {
435 (void)TransDelLaneReqFromPendingList(laneId);
436 return SOFTBUS_ERR;
437 }
438
439 return SOFTBUS_OK;
440 }
441
TransGetLaneInfoByOption(const LaneRequestOption * requestOption,LaneConnInfo * connInfo,uint32_t * laneId)442 NO_SANITIZE("cfi") int32_t TransGetLaneInfoByOption(const LaneRequestOption *requestOption, LaneConnInfo *connInfo,
443 uint32_t *laneId)
444 {
445 if ((requestOption == NULL) || (connInfo == NULL) || (laneId == NULL)) {
446 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get lane info by option param error.");
447 return SOFTBUS_ERR;
448 }
449
450 *laneId = ApplyLaneId(LANE_TYPE_TRANS);
451 if (*laneId <= 0) {
452 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans apply lane failed.");
453 return SOFTBUS_ERR;
454 }
455 if (TransAddLaneReqToPendingAndWaiting(*laneId, requestOption) != SOFTBUS_OK) {
456 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans add lane to pending list failed.");
457 return SOFTBUS_ERR;
458 }
459 bool bSuccess = false;
460 if (TransGetLaneReqItemByLaneId(*laneId, &bSuccess, connInfo) != SOFTBUS_OK) {
461 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get lane req item failed. id[%u].", *laneId);
462 (void)TransDelLaneReqFromPendingList(*laneId);
463 return SOFTBUS_ERR;
464 }
465
466 int32_t ret = SOFTBUS_OK;
467 if (!bSuccess) {
468 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "request lane conninfo failed. id[%u].", *laneId);
469 ret = SOFTBUS_ERR;
470 } else {
471 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "request lane conninfo success. id[%u].", *laneId);
472 }
473 (void)TransDelLaneReqFromPendingList(*laneId);
474 return ret;
475 }
476
TransGetLaneInfo(const SessionParam * param,LaneConnInfo * connInfo,uint32_t * laneId)477 NO_SANITIZE("cfi") int32_t TransGetLaneInfo(const SessionParam *param, LaneConnInfo *connInfo, uint32_t *laneId)
478 {
479 if ((param == NULL) || (connInfo == NULL) || (laneId == NULL)) {
480 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get lane info param error.");
481 return SOFTBUS_ERR;
482 }
483
484 LaneRequestOption requestOption;
485 (void)memset_s(&requestOption, sizeof(LaneRequestOption), 0, sizeof(LaneRequestOption));
486 if (GetRequestOptionBySessionParam(param, &requestOption) != SOFTBUS_OK) {
487 return SOFTBUS_ERR;
488 }
489
490 int32_t ret = TransGetLaneInfoByOption(&requestOption, connInfo, laneId);
491 if (ret != SOFTBUS_OK) {
492 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get lane info by option failed.");
493 return ret;
494 }
495 return SOFTBUS_OK;
496 }
497
SetP2pConnInfo(const P2pConnInfo * p2pInfo,ConnectOption * connOpt)498 static int32_t SetP2pConnInfo(const P2pConnInfo *p2pInfo, ConnectOption *connOpt)
499 {
500 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "set p2p conn info.");
501 connOpt->type = CONNECT_P2P;
502 if (strcpy_s(connOpt->socketOption.addr, sizeof(connOpt->socketOption.addr), p2pInfo->peerIp) != EOK) {
503 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "set p2p localIp err");
504 return SOFTBUS_MEM_ERR;
505 }
506 connOpt->socketOption.protocol = LNN_PROTOCOL_IP;
507 connOpt->socketOption.port = -1;
508 return SOFTBUS_OK;
509 }
SetP2pReusesConnInfo(const WlanConnInfo * connInfo,ConnectOption * connOpt)510 static int32_t SetP2pReusesConnInfo(const WlanConnInfo *connInfo, ConnectOption *connOpt)
511 {
512 connOpt->type = CONNECT_P2P_REUSE;
513 connOpt->socketOption.port = (int32_t)connInfo->port;
514 connOpt->socketOption.protocol = connInfo->protocol;
515 if (strcpy_s(connOpt->socketOption.addr, sizeof(connOpt->socketOption.addr), connInfo->addr) != EOK) {
516 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "set wlan localIp err");
517 return SOFTBUS_ERR;
518 }
519 return SOFTBUS_OK;
520 }
521
SetWlanConnInfo(const WlanConnInfo * connInfo,ConnectOption * connOpt)522 static int32_t SetWlanConnInfo(const WlanConnInfo *connInfo, ConnectOption *connOpt)
523 {
524 connOpt->type = CONNECT_TCP;
525 connOpt->socketOption.port = (int32_t)connInfo->port;
526 connOpt->socketOption.protocol = connInfo->protocol;
527 if (strcpy_s(connOpt->socketOption.addr, sizeof(connOpt->socketOption.addr), connInfo->addr) != EOK) {
528 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "set wlan localIp err");
529 return SOFTBUS_ERR;
530 }
531 return SOFTBUS_OK;
532 }
533
SetBrConnInfo(const BrConnInfo * brInfo,ConnectOption * connOpt)534 static int32_t SetBrConnInfo(const BrConnInfo *brInfo, ConnectOption *connOpt)
535 {
536 connOpt->type = CONNECT_BR;
537 if (strcpy_s(connOpt->brOption.brMac, sizeof(connOpt->brOption.brMac),
538 brInfo->brMac) != EOK) {
539 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "set br mac err");
540 return SOFTBUS_ERR;
541 }
542
543 return SOFTBUS_OK;
544 }
545
SetBleConnInfo(const BleConnInfo * bleInfo,ConnectOption * connOpt)546 static int32_t SetBleConnInfo(const BleConnInfo *bleInfo, ConnectOption *connOpt)
547 {
548 connOpt->type = CONNECT_BLE;
549 if (strcpy_s(connOpt->bleOption.bleMac, sizeof(connOpt->bleOption.bleMac),
550 bleInfo->bleMac) != EOK) {
551 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "set ble mac err");
552 return SOFTBUS_ERR;
553 }
554 if (memcpy_s(connOpt->bleOption.deviceIdHash, sizeof(connOpt->bleOption.deviceIdHash),
555 bleInfo->deviceIdHash, sizeof(bleInfo->deviceIdHash)) != EOK) {
556 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "set deviceId hash err");
557 return SOFTBUS_ERR;
558 }
559 connOpt->bleOption.protocol = bleInfo->protoType;
560 connOpt->bleOption.psm = bleInfo->psm;
561 connOpt->bleOption.fastestConnectEnable = true;
562 return SOFTBUS_OK;
563 }
564
SetBleDirectConnInfo(const BleDirectConnInfo * bleDirect,ConnectOption * connOpt)565 static int32_t SetBleDirectConnInfo(const BleDirectConnInfo* bleDirect, ConnectOption *connOpt)
566 {
567 if (memcpy_s(connOpt->bleDirectOption.nodeIdHash, NODEID_SHORT_HASH_LEN,
568 bleDirect->nodeIdHash, NODEID_SHORT_HASH_LEN) != EOK) {
569 return SOFTBUS_ERR;
570 }
571 if (memcpy_s(connOpt->bleDirectOption.localUdidHash, UDID_SHORT_HASH_LEN,
572 bleDirect->localUdidHash, UDID_SHORT_HASH_LEN) != EOK) {
573 return SOFTBUS_ERR;
574 }
575 if (memcpy_s(connOpt->bleDirectOption.peerUdidHash, SHA_256_HASH_LEN,
576 bleDirect->peerUdidHash, SHA_256_HASH_LEN) != EOK) {
577 return SOFTBUS_ERR;
578 }
579
580 connOpt->type = CONNECT_BLE_DIRECT;
581 connOpt->bleDirectOption.protoType = bleDirect->protoType;
582 return SOFTBUS_OK;
583 }
584
TransGetConnectOptByConnInfo(const LaneConnInfo * info,ConnectOption * connOpt)585 NO_SANITIZE("cfi") int32_t TransGetConnectOptByConnInfo(const LaneConnInfo *info, ConnectOption *connOpt)
586 {
587 if (info == NULL || connOpt == NULL) {
588 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "[%s] invalid param.", __func__);
589 return SOFTBUS_ERR;
590 }
591 if (info->type == LANE_P2P) {
592 return SetP2pConnInfo(&(info->connInfo.p2p), connOpt);
593 } else if (info->type == LANE_WLAN_2P4G || info->type == LANE_WLAN_5G || info->type == LANE_ETH) {
594 return SetWlanConnInfo(&(info->connInfo.wlan), connOpt);
595 } else if (info->type == LANE_BR) {
596 return SetBrConnInfo(&(info->connInfo.br), connOpt);
597 } else if ((info->type == LANE_BLE) || (info->type == LANE_COC)) {
598 return SetBleConnInfo(&(info->connInfo.ble), connOpt);
599 } else if (info->type == LANE_P2P_REUSE) {
600 return SetP2pReusesConnInfo(&(info->connInfo.wlan), connOpt);
601 } else if (info->type == LANE_BLE_DIRECT || info->type == LANE_COC_DIRECT) {
602 return SetBleDirectConnInfo(&(info->connInfo.bleDirect), connOpt);
603 }
604
605 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get conn opt err: type=%d", info->type);
606 return SOFTBUS_ERR;
607 }
608
TransGetAuthTypeByNetWorkId(const char * peerNetWorkId)609 NO_SANITIZE("cfi") bool TransGetAuthTypeByNetWorkId(const char *peerNetWorkId)
610 {
611 int32_t value = 0;
612 int32_t ret = LnnGetRemoteNumInfo(peerNetWorkId, NUM_KEY_META_NODE, &value);
613 if (ret != SOFTBUS_OK) {
614 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetAuthType fail, ret=%d", ret);
615 return false;
616 }
617 return ((uint32_t)value == (1 << ONLINE_METANODE)) ? true : false;
618 }