1 /*
2 * Copyright (c) 2022-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_tcp_direct_sessionconn.h"
17
18 #include <securec.h>
19
20 #include "auth_interface.h"
21 #include "softbus_adapter_mem.h"
22 #include "softbus_adapter_thread.h"
23 #include "softbus_base_listener.h"
24 #include "softbus_def.h"
25 #include "softbus_errcode.h"
26 #include "trans_channel_manager.h"
27 #include "trans_log.h"
28
29 #define TRANS_SEQ_STEP 2
30
31 static SoftBusList *g_sessionConnList = NULL;
32 static SoftBusList *g_tcpChannelInfoList = NULL;
33
TransTdcGetNewSeqId(void)34 uint64_t TransTdcGetNewSeqId(void)
35 {
36 if (GetSessionConnLock() != SOFTBUS_OK) {
37 TRANS_LOGE(TRANS_CTRL, "GetLock fail");
38 return INVALID_SEQ_ID;
39 }
40
41 static uint64_t seq = 0;
42 seq += TRANS_SEQ_STEP;
43
44 uint64_t retseq = seq;
45
46 ReleaseSessionConnLock();
47
48 return retseq;
49 }
50
CreatSessionConnList(void)51 int32_t CreatSessionConnList(void)
52 {
53 if (g_sessionConnList == NULL) {
54 g_sessionConnList = CreateSoftBusList();
55 if (g_sessionConnList == NULL) {
56 TRANS_LOGE(TRANS_CTRL, "CreateSoftBusList fail");
57 return SOFTBUS_MALLOC_ERR;
58 }
59 }
60 return SOFTBUS_OK;
61 }
62
GetSessionConnList(void)63 SoftBusList *GetSessionConnList(void)
64 {
65 if (g_sessionConnList == NULL) {
66 return NULL;
67 }
68 return g_sessionConnList;
69 }
70
GetTcpChannelInfoList(void)71 SoftBusList *GetTcpChannelInfoList(void)
72 {
73 return g_tcpChannelInfoList;
74 }
75
GetSessionConnLock(void)76 int32_t GetSessionConnLock(void)
77 {
78 if (g_sessionConnList == NULL) {
79 return SOFTBUS_NO_INIT;
80 }
81 if (SoftBusMutexLock(&g_sessionConnList->lock) != SOFTBUS_OK) {
82 return SOFTBUS_LOCK_ERR;
83 }
84 return SOFTBUS_OK;
85 }
86
GetTcpChannelInfoLock(void)87 int32_t GetTcpChannelInfoLock(void)
88 {
89 if (g_tcpChannelInfoList == NULL) {
90 return SOFTBUS_NO_INIT;
91 }
92 return SoftBusMutexLock(&g_tcpChannelInfoList->lock);
93 }
94
ReleaseSessionConnLock(void)95 void ReleaseSessionConnLock(void)
96 {
97 if (g_sessionConnList == NULL) {
98 return;
99 }
100 (void)SoftBusMutexUnlock(&g_sessionConnList->lock);
101 }
102
ReleaseTcpChannelInfoLock(void)103 void ReleaseTcpChannelInfoLock(void)
104 {
105 if (g_tcpChannelInfoList == NULL) {
106 return;
107 }
108 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
109 }
110
GetSessionConnByRequestId(uint32_t requestId)111 SessionConn *GetSessionConnByRequestId(uint32_t requestId)
112 {
113 if (g_sessionConnList == NULL) {
114 return NULL;
115 }
116 SessionConn *item = NULL;
117 SessionConn *nextItem = NULL;
118 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_sessionConnList->list, SessionConn, node) {
119 if (item->requestId == requestId) {
120 return item;
121 }
122 }
123 TRANS_LOGE(TRANS_CTRL, "get session conn by requestId failed: requestId=%{public}u", requestId);
124 return NULL;
125 }
126
GetSessionConnByReq(int64_t req)127 SessionConn *GetSessionConnByReq(int64_t req)
128 {
129 if (g_sessionConnList == NULL) {
130 return NULL;
131 }
132 SessionConn *item = NULL;
133 SessionConn *nextItem = NULL;
134 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_sessionConnList->list, SessionConn, node) {
135 if (item->req == req) {
136 return item;
137 }
138 }
139 TRANS_LOGE(TRANS_CTRL, "get session conn by req failed: req=%{public}" PRIu64, req);
140 return NULL;
141 }
142
CreateNewSessinConn(ListenerModule module,bool isServerSid)143 SessionConn *CreateNewSessinConn(ListenerModule module, bool isServerSid)
144 {
145 SessionConn *conn = (SessionConn *)SoftBusCalloc(sizeof(SessionConn));
146 if (conn == NULL) {
147 return NULL;
148 }
149 conn->serverSide = isServerSid;
150 conn->channelId = GenerateChannelId(true);
151 if (conn->channelId <= INVALID_CHANNEL_ID) {
152 SoftBusFree(conn);
153 TRANS_LOGE(TRANS_CTRL, "generate tdc channel id failed.");
154 return NULL;
155 }
156 conn->status = TCP_DIRECT_CHANNEL_STATUS_INIT;
157 conn->timeout = 0;
158 conn->req = -1;
159 conn->authHandle.authId = AUTH_INVALID_ID;
160 conn->requestId = 0; // invalid num
161 conn->listenMod = module;
162 return conn;
163 }
164
GetSessionConnByFd(int32_t fd,SessionConn * conn)165 int32_t GetSessionConnByFd(int32_t fd, SessionConn *conn)
166 {
167 SessionConn *connInfo = NULL;
168 if (GetSessionConnLock() != SOFTBUS_OK) {
169 return SOFTBUS_LOCK_ERR;
170 }
171 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
172 if (connInfo->appInfo.fd == fd) {
173 if (conn != NULL) {
174 (void)memcpy_s(conn, sizeof(SessionConn), connInfo, sizeof(SessionConn));
175 }
176 ReleaseSessionConnLock();
177 return SOFTBUS_OK;
178 }
179 }
180 ReleaseSessionConnLock();
181
182 return SOFTBUS_TRANS_GET_SESSION_CONN_FAILED;
183 }
184
GetSessionConnById(int32_t channelId,SessionConn * conn)185 int32_t GetSessionConnById(int32_t channelId, SessionConn *conn)
186 {
187 SessionConn *connInfo = NULL;
188 if (GetSessionConnLock() != SOFTBUS_OK) {
189 return SOFTBUS_LOCK_ERR;
190 }
191 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
192 if (connInfo->channelId == channelId) {
193 if (conn != NULL) {
194 (void)memcpy_s(conn, sizeof(SessionConn), connInfo, sizeof(SessionConn));
195 }
196 ReleaseSessionConnLock();
197 return SOFTBUS_OK;
198 }
199 }
200 ReleaseSessionConnLock();
201
202 TRANS_LOGE(TRANS_CTRL, "can not get srv session conn info.");
203 return SOFTBUS_TRANS_GET_SESSION_CONN_FAILED;
204 }
205
SetAppInfoById(int32_t channelId,const AppInfo * appInfo)206 int32_t SetAppInfoById(int32_t channelId, const AppInfo *appInfo)
207 {
208 SessionConn *conn = NULL;
209 if (GetSessionConnLock() != SOFTBUS_OK) {
210 return SOFTBUS_LOCK_ERR;
211 }
212 LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
213 if (conn->channelId == channelId) {
214 (void)memcpy_s(&conn->appInfo, sizeof(AppInfo), appInfo, sizeof(AppInfo));
215 ReleaseSessionConnLock();
216 return SOFTBUS_OK;
217 }
218 }
219 ReleaseSessionConnLock();
220 TRANS_LOGE(TRANS_CTRL, "can not get srv session conn info.");
221 return SOFTBUS_TRANS_SET_APP_INFO_FAILED;
222 }
223
GetAppInfoById(int32_t channelId,AppInfo * appInfo)224 int32_t GetAppInfoById(int32_t channelId, AppInfo *appInfo)
225 {
226 SessionConn *conn = NULL;
227 if (GetSessionConnLock() != SOFTBUS_OK) {
228 return SOFTBUS_LOCK_ERR;
229 }
230 LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
231 if (conn->channelId == channelId) {
232 (void)memcpy_s(appInfo, sizeof(AppInfo), &conn->appInfo, sizeof(AppInfo));
233 ReleaseSessionConnLock();
234 return SOFTBUS_OK;
235 }
236 }
237 ReleaseSessionConnLock();
238 TRANS_LOGE(TRANS_CTRL, "can not get srv session conn info.");
239 return SOFTBUS_TRANS_GET_APP_INFO_FAILED;
240 }
241
SetAuthHandleByChanId(int32_t channelId,AuthHandle * authHandle)242 int32_t SetAuthHandleByChanId(int32_t channelId, AuthHandle *authHandle)
243 {
244 SessionConn *conn = NULL;
245 if (GetSessionConnLock() != SOFTBUS_OK) {
246 return SOFTBUS_LOCK_ERR;
247 }
248 LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
249 if (conn->channelId == channelId) {
250 conn->authHandle = *authHandle;
251 ReleaseSessionConnLock();
252 return SOFTBUS_OK;
253 }
254 }
255 ReleaseSessionConnLock();
256 return SOFTBUS_TRANS_SET_AUTH_HANDLE_FAILED;
257 }
258
GetAuthIdByChanId(int32_t channelId)259 int64_t GetAuthIdByChanId(int32_t channelId)
260 {
261 int64_t authId;
262 SessionConn *conn = NULL;
263 if (GetSessionConnLock() != SOFTBUS_OK) {
264 return AUTH_INVALID_ID;
265 }
266 LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
267 if (conn->channelId == channelId) {
268 authId = conn->authHandle.authId;
269 ReleaseSessionConnLock();
270 return authId;
271 }
272 }
273 ReleaseSessionConnLock();
274 return AUTH_INVALID_ID;
275 }
276
GetAuthHandleByChanId(int32_t channelId,AuthHandle * authHandle)277 int32_t GetAuthHandleByChanId(int32_t channelId, AuthHandle *authHandle)
278 {
279 if (authHandle == NULL) {
280 TRANS_LOGE(TRANS_CTRL, "authHandle is null");
281 return SOFTBUS_INVALID_PARAM;
282 }
283 authHandle->authId = AUTH_INVALID_ID;
284 SessionConn *conn = NULL;
285 if (GetSessionConnLock() != SOFTBUS_OK) {
286 TRANS_LOGE(TRANS_CTRL, "get lock fail");
287 return SOFTBUS_LOCK_ERR;
288 }
289 LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
290 if (conn->channelId == channelId) {
291 *authHandle = conn->authHandle;
292 ReleaseSessionConnLock();
293 return SOFTBUS_OK;
294 }
295 }
296 ReleaseSessionConnLock();
297 return SOFTBUS_TRANS_GET_AUTH_HANDLE_FAILED;
298 }
299
TransDelSessionConnById(int32_t channelId)300 void TransDelSessionConnById(int32_t channelId)
301 {
302 TRANS_LOGW(TRANS_CTRL, "channelId=%{public}d", channelId);
303 SessionConn *item = NULL;
304 SessionConn *next = NULL;
305 if (GetSessionConnLock() != SOFTBUS_OK) {
306 return;
307 }
308 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_sessionConnList->list, SessionConn, node) {
309 if (item->channelId == channelId) {
310 if ((item->listenMod == DIRECT_CHANNEL_SERVER_P2P || (item->listenMod >= DIRECT_CHANNEL_SERVER_HML_START &&
311 item->listenMod <= DIRECT_CHANNEL_SERVER_HML_END)) && item->authHandle.authId != AUTH_INVALID_ID &&
312 !item->serverSide && item->appInfo.routeType != WIFI_P2P_REUSE && item->requestId != REQUEST_INVALID) {
313 AuthCloseConn(item->authHandle);
314 }
315 ListDelete(&item->node);
316 TRANS_LOGI(TRANS_CTRL, "delete channelId=%{public}d", item->channelId);
317 if (item->appInfo.fastTransData != NULL) {
318 SoftBusFree((void*)item->appInfo.fastTransData);
319 }
320 (void)memset_s(item->appInfo.sessionKey, sizeof(item->appInfo.sessionKey), 0,
321 sizeof(item->appInfo.sessionKey));
322 SoftBusFree(item);
323 g_sessionConnList->cnt--;
324 ReleaseSessionConnLock();
325 return;
326 }
327 }
328 ReleaseSessionConnLock();
329 }
330
TransTdcAddSessionConn(SessionConn * conn)331 int32_t TransTdcAddSessionConn(SessionConn *conn)
332 {
333 if (conn == NULL) {
334 return SOFTBUS_INVALID_PARAM;
335 }
336 if (GetSessionConnLock() != SOFTBUS_OK) {
337 return SOFTBUS_LOCK_ERR;
338 }
339 ListInit(&conn->node);
340 ListTailInsert(&g_sessionConnList->list, &conn->node);
341 g_sessionConnList->cnt++;
342 ReleaseSessionConnLock();
343 return SOFTBUS_OK;
344 }
345
CreateTcpChannelInfoList(void)346 int32_t CreateTcpChannelInfoList(void)
347 {
348 if (g_tcpChannelInfoList == NULL) {
349 g_tcpChannelInfoList = CreateSoftBusList();
350 if (g_tcpChannelInfoList == NULL) {
351 TRANS_LOGE(TRANS_CTRL, "CreateSoftBusList fail");
352 return SOFTBUS_MALLOC_ERR;
353 }
354 }
355 return SOFTBUS_OK;
356 }
357
CreateTcpChannelInfo(const ChannelInfo * channel)358 TcpChannelInfo *CreateTcpChannelInfo(const ChannelInfo *channel)
359 {
360 if (channel == NULL) {
361 return NULL;
362 }
363 TcpChannelInfo *tcpChannelInfo = (TcpChannelInfo *)SoftBusCalloc(sizeof(TcpChannelInfo));
364 if (tcpChannelInfo == NULL) {
365 return NULL;
366 }
367 tcpChannelInfo->channelId = channel->channelId;
368 tcpChannelInfo->businessType = channel->businessType;
369 tcpChannelInfo->connectType = channel->connectType;
370 if (strcpy_s(tcpChannelInfo->myIp, IP_LEN, channel->myIp) != EOK) {
371 TRANS_LOGE(TRANS_CTRL, "failed to strcpy myIp, channelId=%{public}d", channel->channelId);
372 SoftBusFree(tcpChannelInfo);
373 return NULL;
374 }
375 tcpChannelInfo->isServer = channel->isServer;
376 tcpChannelInfo->channelType = channel->channelType;
377 if (strcpy_s(tcpChannelInfo->peerSessionName, SESSION_NAME_SIZE_MAX, channel->peerSessionName) != EOK) {
378 TRANS_LOGE(TRANS_CTRL, "failed to strcpy peerSessionName, channelId=%{public}d", channel->channelId);
379 SoftBusFree(tcpChannelInfo);
380 return NULL;
381 }
382 if (strcpy_s(tcpChannelInfo->peerDeviceId, DEVICE_ID_SIZE_MAX, channel->peerDeviceId) != EOK) {
383 TRANS_LOGE(TRANS_CTRL, "failed to strcpy peerDeviceId, channelId=%{public}d", channel->channelId);
384 SoftBusFree(tcpChannelInfo);
385 return NULL;
386 }
387 if (strcpy_s(tcpChannelInfo->peerIp, IP_LEN, channel->peerIp) != EOK) {
388 TRANS_LOGE(TRANS_CTRL, "failed to strcpy peerDeviceId, channelId=%{public}d", channel->channelId);
389 SoftBusFree(tcpChannelInfo);
390 return NULL;
391 }
392 tcpChannelInfo->timeStart = channel->timeStart;
393 tcpChannelInfo->linkType = channel->linkType;
394 return tcpChannelInfo;
395 }
396
TransAddTcpChannelInfo(TcpChannelInfo * info)397 int32_t TransAddTcpChannelInfo(TcpChannelInfo *info)
398 {
399 if (info == NULL) {
400 TRANS_LOGE(TRANS_CTRL, "invalid param, info is null.");
401 return SOFTBUS_INVALID_PARAM;
402 }
403 if (g_tcpChannelInfoList == NULL) {
404 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList not init.");
405 return SOFTBUS_NO_INIT;
406 }
407 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
408 TRANS_LOGE(TRANS_CTRL, "lock error.");
409 return SOFTBUS_LOCK_ERR;
410 }
411 int32_t channelId = info->channelId;
412 ListInit(&info->node);
413 ListAdd(&g_tcpChannelInfoList->list, &(info->node));
414 g_tcpChannelInfoList->cnt++;
415 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
416 TRANS_LOGI(TRANS_CTRL, "TcpChannelInfo add success, channelId=%{public}d.", channelId);
417 return SOFTBUS_OK;
418 }
419
TransTdcGetIpAndConnectTypeById(int32_t channelId,char * localIp,char * remoteIp,uint32_t maxIpLen,int32_t * connectType)420 int32_t TransTdcGetIpAndConnectTypeById(int32_t channelId, char *localIp, char *remoteIp, uint32_t maxIpLen,
421 int32_t *connectType)
422 {
423 if (localIp == NULL || remoteIp == NULL || maxIpLen < IP_LEN || connectType == NULL) {
424 TRANS_LOGE(TRANS_CTRL, "invalid param");
425 return SOFTBUS_INVALID_PARAM;
426 }
427
428 if (g_tcpChannelInfoList == NULL) {
429 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList is null.");
430 return SOFTBUS_NO_INIT;
431 }
432
433 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
434 TRANS_LOGE(TRANS_CTRL, "lock failed.");
435 return SOFTBUS_LOCK_ERR;
436 }
437
438 TcpChannelInfo *item = NULL;
439 TcpChannelInfo *next = NULL;
440 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_tcpChannelInfoList->list, TcpChannelInfo, node) {
441 if (item->channelId == channelId) {
442 if (strcpy_s(localIp, maxIpLen, item->myIp) != EOK) {
443 TRANS_LOGE(TRANS_CTRL, "failed to strcpy localIp. channelId=%{public}d", channelId);
444 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
445 return SOFTBUS_STRCPY_ERR;
446 }
447 if (strcpy_s(remoteIp, maxIpLen, item->peerIp) != EOK) {
448 TRANS_LOGE(TRANS_CTRL, "failed to strcpy remoteIp. channelId=%{public}d", channelId);
449 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
450 return SOFTBUS_STRCPY_ERR;
451 }
452 *connectType = item->connectType;
453 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
454 return SOFTBUS_OK;
455 }
456 }
457 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
458 TRANS_LOGE(TRANS_CTRL, "TcpChannelInfo not found. channelId=%{public}d", channelId);
459 return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
460 }
461
TransDelTcpChannelInfoByChannelId(int32_t channelId)462 int32_t TransDelTcpChannelInfoByChannelId(int32_t channelId)
463 {
464 if (g_tcpChannelInfoList == NULL) {
465 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList is null.");
466 return SOFTBUS_NO_INIT;
467 }
468 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
469 TRANS_LOGE(TRANS_CTRL, "lock failed.");
470 return SOFTBUS_LOCK_ERR;
471 }
472 TcpChannelInfo *item = NULL;
473 TcpChannelInfo *next = NULL;
474 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_tcpChannelInfoList->list, TcpChannelInfo, node) {
475 if (item->channelId == channelId) {
476 ListDelete(&item->node);
477 TRANS_LOGI(TRANS_CTRL, "delete TcpChannelInfo success, channelId=%{public}d", item->channelId);
478 SoftBusFree(item);
479 g_tcpChannelInfoList->cnt--;
480 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
481 return SOFTBUS_OK;
482 }
483 }
484 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
485 TRANS_LOGE(TRANS_CTRL, "TcpChannelInfo not found. channelId=%{public}d", channelId);
486 return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
487 }
488
TransTdcChannelInfoDeathCallback(const char * pkgName,int32_t pid)489 void TransTdcChannelInfoDeathCallback(const char *pkgName, int32_t pid)
490 {
491 char *anonymizePkgName = NULL;
492 Anonymize(pkgName, &anonymizePkgName);
493 TRANS_LOGI(TRANS_CTRL, "pkgName=%{public}s pid=%{public}d died, clean all resource", anonymizePkgName, pid);
494 AnonymizeFree(anonymizePkgName);
495 if (g_tcpChannelInfoList == NULL) {
496 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList is null.");
497 return;
498 }
499 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
500 TRANS_LOGE(TRANS_CTRL, "lock failed.");
501 return;
502 }
503 TcpChannelInfo *item = NULL;
504 TcpChannelInfo *next = NULL;
505 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_tcpChannelInfoList->list, TcpChannelInfo, node) {
506 if ((strcmp(item->pkgName, pkgName) == 0) && (item->pid == pid)) {
507 ListDelete(&item->node);
508 TRANS_LOGI(TRANS_CTRL, "delete TcpChannelInfo success, channelId=%{public}d", item->channelId);
509 SoftBusFree(item);
510 g_tcpChannelInfoList->cnt--;
511 }
512 }
513 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
514 TRANS_LOGD(TRANS_CTRL, "ok");
515 }
516
SetSessionKeyByChanId(int32_t chanId,const char * sessionKey,int32_t keyLen)517 void SetSessionKeyByChanId(int32_t chanId, const char *sessionKey, int32_t keyLen)
518 {
519 if (sessionKey == NULL || keyLen <= 0) {
520 return;
521 }
522 bool isFind = false;
523 SessionConn *conn = NULL;
524 if (GetSessionConnLock() != SOFTBUS_OK) {
525 return;
526 }
527 LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
528 if (conn->channelId == chanId) {
529 isFind = true;
530 break;
531 }
532 }
533 if (isFind && conn != NULL) {
534 if (memcpy_s(conn->appInfo.sessionKey, sizeof(conn->appInfo.sessionKey), sessionKey, keyLen) != EOK) {
535 TRANS_LOGE(TRANS_CTRL, "memcpy fail");
536 ReleaseSessionConnLock();
537 return;
538 }
539 }
540 ReleaseSessionConnLock();
541 }
542
SetSessionConnStatusById(int32_t channelId,uint32_t status)543 int32_t SetSessionConnStatusById(int32_t channelId, uint32_t status)
544 {
545 if (GetSessionConnLock() != SOFTBUS_OK) {
546 return SOFTBUS_LOCK_ERR;
547 }
548 SessionConn *connInfo = NULL;
549 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
550 if (connInfo->channelId == channelId) {
551 connInfo->status = status;
552 ReleaseSessionConnLock();
553 return SOFTBUS_OK;
554 }
555 }
556 ReleaseSessionConnLock();
557 TRANS_LOGE(TRANS_CTRL, "not find: channelId=%{public}d", channelId);
558 return SOFTBUS_NOT_FIND;
559 }
560
IsTdcRecoveryTransLimit(void)561 bool IsTdcRecoveryTransLimit(void)
562 {
563 if (g_tcpChannelInfoList == NULL) {
564 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList is null.");
565 return false;
566 }
567 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
568 TRANS_LOGE(TRANS_CTRL, "lock failed.");
569 return false;
570 }
571 TcpChannelInfo *info = NULL;
572 LIST_FOR_EACH_ENTRY(info, &g_tcpChannelInfoList->list, TcpChannelInfo, node) {
573 if (info->businessType == BUSINESS_TYPE_BYTE) {
574 TRANS_LOGI(TRANS_CTRL, "tcp direct channel exists bytes business, channelId=%{public}d.", info->channelId);
575 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
576 return false;
577 }
578 }
579 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
580 return true;
581 }
582
TcpTranGetAppInfobyChannelId(int32_t channelId,AppInfo * appInfo)583 int32_t TcpTranGetAppInfobyChannelId(int32_t channelId, AppInfo* appInfo)
584 {
585 if (GetSessionConnLock() != SOFTBUS_OK) {
586 return SOFTBUS_LOCK_ERR;
587 }
588 SessionConn *connInfo = NULL;
589 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
590 if (connInfo->channelId == channelId) {
591 memcpy_s(appInfo, sizeof(AppInfo), &connInfo->appInfo, sizeof(AppInfo));
592 ReleaseSessionConnLock();
593 return SOFTBUS_OK;
594 }
595 }
596 ReleaseSessionConnLock();
597 TRANS_LOGE(TRANS_CTRL, "not find: channelId=%{public}d", channelId);
598 return SOFTBUS_NOT_FIND;
599 }
600
GetChannelIdsByAuthIdAndStatus(int32_t * num,const AuthHandle * authHandle,uint32_t status)601 int32_t *GetChannelIdsByAuthIdAndStatus(int32_t *num, const AuthHandle *authHandle, uint32_t status)
602 {
603 if (num == NULL) {
604 TRANS_LOGE(TRANS_CTRL, "Invaild param");
605 return NULL;
606 }
607 TRANS_LOGD(TRANS_CTRL, "AuthId=%{public}" PRId64 ",status=%{public}d", authHandle->authId, status);
608 if (GetSessionConnLock() != SOFTBUS_OK) {
609 TRANS_LOGE(TRANS_CTRL, "GetSessionConnLock failed");
610 return NULL;
611 }
612 SessionConn *connInfo = NULL;
613 int32_t count = 0;
614 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
615 if (connInfo->authHandle.authId == authHandle->authId && connInfo->status == status &&
616 connInfo->authHandle.type == authHandle->type) {
617 count++;
618 }
619 }
620 if (count == 0) {
621 ReleaseSessionConnLock();
622 TRANS_LOGE(TRANS_CTRL, "Not find channle id with authId=%{public}" PRId64 ", status=%{public}d",
623 authHandle->authId, status);
624 return NULL;
625 }
626 *num = count;
627 connInfo = NULL;
628 int32_t tmp = 0;
629 int32_t *result = (int32_t *)SoftBusCalloc(count * sizeof(int32_t));
630 if (result == NULL) {
631 TRANS_LOGE(TRANS_CTRL, "malloc result failed");
632 ReleaseSessionConnLock();
633 return NULL;
634 }
635 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
636 if (connInfo->authHandle.authId == authHandle->authId && connInfo->status == status &&
637 connInfo->authHandle.type == authHandle->type) {
638 result[tmp++] = connInfo->channelId;
639 }
640 }
641 ReleaseSessionConnLock();
642 return result;
643 }
644
TransGetPidByChanId(int32_t channelId,int32_t channelType,int32_t * pid)645 int32_t TransGetPidByChanId(int32_t channelId, int32_t channelType, int32_t *pid)
646 {
647 if (pid == NULL) {
648 TRANS_LOGE(TRANS_CTRL, "pid is null");
649 return SOFTBUS_INVALID_PARAM;
650 }
651
652 if (g_tcpChannelInfoList == NULL) {
653 TRANS_LOGE(TRANS_CTRL, "tcp channel info list hasn't init.");
654 return SOFTBUS_INVALID_PARAM;
655 }
656
657 if (SoftBusMutexLock(&(g_tcpChannelInfoList->lock)) != SOFTBUS_OK) {
658 TRANS_LOGE(TRANS_SVC, "lock failed");
659 return SOFTBUS_LOCK_ERR;
660 }
661
662 TcpChannelInfo *info = NULL;
663 LIST_FOR_EACH_ENTRY(info, &(g_tcpChannelInfoList->list), TcpChannelInfo, node) {
664 if (info->channelId == channelId && info->channelType == channelType) {
665 *pid = info->pid;
666 (void)SoftBusMutexUnlock(&(g_tcpChannelInfoList->lock));
667 return SOFTBUS_OK;
668 }
669 }
670 (void)SoftBusMutexUnlock(&(g_tcpChannelInfoList->lock));
671 TRANS_LOGE(TRANS_SVC, "can not find pid by channelId=%{public}d", channelId);
672 return SOFTBUS_TRANS_INVALID_CHANNEL_ID;
673 }
674