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_error_code.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 SessionConn conn = { 0 };
395 if (GetSessionConnById(channel->channelId, &conn) != SOFTBUS_OK) {
396 TRANS_LOGE(TRANS_CTRL, "failed to get callingTokenId, channelId=%{public}d", channel->channelId);
397 SoftBusFree(tcpChannelInfo);
398 return NULL;
399 }
400 tcpChannelInfo->callingTokenId = conn.appInfo.callingTokenId;
401 return tcpChannelInfo;
402 }
403
TransAddTcpChannelInfo(TcpChannelInfo * info)404 int32_t TransAddTcpChannelInfo(TcpChannelInfo *info)
405 {
406 if (info == NULL) {
407 TRANS_LOGE(TRANS_CTRL, "invalid param, info is null.");
408 return SOFTBUS_INVALID_PARAM;
409 }
410 if (g_tcpChannelInfoList == NULL) {
411 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList not init.");
412 return SOFTBUS_NO_INIT;
413 }
414 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
415 TRANS_LOGE(TRANS_CTRL, "lock error.");
416 return SOFTBUS_LOCK_ERR;
417 }
418 int32_t channelId = info->channelId;
419 ListInit(&info->node);
420 ListAdd(&g_tcpChannelInfoList->list, &(info->node));
421 g_tcpChannelInfoList->cnt++;
422 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
423 TRANS_LOGI(TRANS_CTRL, "TcpChannelInfo add success, channelId=%{public}d.", channelId);
424 return SOFTBUS_OK;
425 }
426
TransTdcGetIpAndConnectTypeById(int32_t channelId,char * localIp,char * remoteIp,uint32_t maxIpLen,int32_t * connectType)427 int32_t TransTdcGetIpAndConnectTypeById(int32_t channelId, char *localIp, char *remoteIp, uint32_t maxIpLen,
428 int32_t *connectType)
429 {
430 if (localIp == NULL || remoteIp == NULL || maxIpLen < IP_LEN || connectType == NULL) {
431 TRANS_LOGE(TRANS_CTRL, "invalid param");
432 return SOFTBUS_INVALID_PARAM;
433 }
434
435 if (g_tcpChannelInfoList == NULL) {
436 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList is null.");
437 return SOFTBUS_NO_INIT;
438 }
439
440 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
441 TRANS_LOGE(TRANS_CTRL, "lock failed.");
442 return SOFTBUS_LOCK_ERR;
443 }
444
445 TcpChannelInfo *item = NULL;
446 TcpChannelInfo *next = NULL;
447 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_tcpChannelInfoList->list, TcpChannelInfo, node) {
448 if (item->channelId == channelId) {
449 if (strcpy_s(localIp, maxIpLen, item->myIp) != EOK) {
450 TRANS_LOGE(TRANS_CTRL, "failed to strcpy localIp. channelId=%{public}d", channelId);
451 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
452 return SOFTBUS_STRCPY_ERR;
453 }
454 if (strcpy_s(remoteIp, maxIpLen, item->peerIp) != EOK) {
455 TRANS_LOGE(TRANS_CTRL, "failed to strcpy remoteIp. channelId=%{public}d", channelId);
456 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
457 return SOFTBUS_STRCPY_ERR;
458 }
459 *connectType = item->connectType;
460 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
461 return SOFTBUS_OK;
462 }
463 }
464 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
465 TRANS_LOGE(TRANS_CTRL, "TcpChannelInfo not found, channelId=%{public}d", channelId);
466 return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
467 }
468
TransDelTcpChannelInfoByChannelId(int32_t channelId)469 int32_t TransDelTcpChannelInfoByChannelId(int32_t channelId)
470 {
471 if (g_tcpChannelInfoList == NULL) {
472 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList is null.");
473 return SOFTBUS_NO_INIT;
474 }
475 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
476 TRANS_LOGE(TRANS_CTRL, "lock failed.");
477 return SOFTBUS_LOCK_ERR;
478 }
479 TcpChannelInfo *item = NULL;
480 TcpChannelInfo *next = NULL;
481 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_tcpChannelInfoList->list, TcpChannelInfo, node) {
482 if (item->channelId == channelId) {
483 ListDelete(&item->node);
484 TRANS_LOGI(TRANS_CTRL, "delete TcpChannelInfo success, channelId=%{public}d", item->channelId);
485 SoftBusFree(item);
486 g_tcpChannelInfoList->cnt--;
487 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
488 return SOFTBUS_OK;
489 }
490 }
491 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
492 TRANS_LOGE(TRANS_CTRL, "TcpChannelInfo not found. channelId=%{public}d", channelId);
493 return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
494 }
495
TransTdcChannelInfoDeathCallback(const char * pkgName,int32_t pid)496 void TransTdcChannelInfoDeathCallback(const char *pkgName, int32_t pid)
497 {
498 char *anonymizePkgName = NULL;
499 Anonymize(pkgName, &anonymizePkgName);
500 TRANS_LOGI(TRANS_CTRL, "pkgName=%{public}s pid=%{public}d died, clean all resource",
501 AnonymizeWrapper(anonymizePkgName), pid);
502 AnonymizeFree(anonymizePkgName);
503 if (g_tcpChannelInfoList == NULL) {
504 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList is null.");
505 return;
506 }
507 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
508 TRANS_LOGE(TRANS_CTRL, "lock failed.");
509 return;
510 }
511 TcpChannelInfo *item = NULL;
512 TcpChannelInfo *next = NULL;
513 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_tcpChannelInfoList->list, TcpChannelInfo, node) {
514 if ((strcmp(item->pkgName, pkgName) == 0) && (item->pid == pid)) {
515 ListDelete(&item->node);
516 TRANS_LOGI(TRANS_CTRL, "delete TcpChannelInfo success, channelId=%{public}d", item->channelId);
517 SoftBusFree(item);
518 g_tcpChannelInfoList->cnt--;
519 }
520 }
521 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
522 TRANS_LOGD(TRANS_CTRL, "ok");
523 }
524
SetSessionKeyByChanId(int32_t chanId,const char * sessionKey,int32_t keyLen)525 void SetSessionKeyByChanId(int32_t chanId, const char *sessionKey, int32_t keyLen)
526 {
527 if (sessionKey == NULL || keyLen <= 0) {
528 return;
529 }
530 bool isFind = false;
531 SessionConn *conn = NULL;
532 if (GetSessionConnLock() != SOFTBUS_OK) {
533 return;
534 }
535 LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
536 if (conn->channelId == chanId) {
537 isFind = true;
538 break;
539 }
540 }
541 if (isFind && conn != NULL) {
542 if (memcpy_s(conn->appInfo.sessionKey, sizeof(conn->appInfo.sessionKey), sessionKey, keyLen) != EOK) {
543 TRANS_LOGE(TRANS_CTRL, "memcpy fail");
544 ReleaseSessionConnLock();
545 return;
546 }
547 }
548 ReleaseSessionConnLock();
549 }
550
SetSessionConnStatusById(int32_t channelId,uint32_t status)551 int32_t SetSessionConnStatusById(int32_t channelId, uint32_t status)
552 {
553 if (GetSessionConnLock() != SOFTBUS_OK) {
554 return SOFTBUS_LOCK_ERR;
555 }
556 SessionConn *connInfo = NULL;
557 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
558 if (connInfo->channelId == channelId) {
559 connInfo->status = status;
560 ReleaseSessionConnLock();
561 return SOFTBUS_OK;
562 }
563 }
564 ReleaseSessionConnLock();
565 TRANS_LOGE(TRANS_CTRL, "not find: channelId=%{public}d", channelId);
566 return SOFTBUS_NOT_FIND;
567 }
568
IsTdcRecoveryTransLimit(void)569 bool IsTdcRecoveryTransLimit(void)
570 {
571 if (g_tcpChannelInfoList == NULL) {
572 TRANS_LOGE(TRANS_CTRL, "g_tcpChannelInfoList is null.");
573 return false;
574 }
575 if (SoftBusMutexLock(&g_tcpChannelInfoList->lock) != SOFTBUS_OK) {
576 TRANS_LOGE(TRANS_CTRL, "lock failed.");
577 return false;
578 }
579 TcpChannelInfo *info = NULL;
580 LIST_FOR_EACH_ENTRY(info, &g_tcpChannelInfoList->list, TcpChannelInfo, node) {
581 if (info->businessType == BUSINESS_TYPE_BYTE) {
582 TRANS_LOGI(TRANS_CTRL, "tcp direct channel exists bytes business, channelId=%{public}d.", info->channelId);
583 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
584 return false;
585 }
586 }
587 (void)SoftBusMutexUnlock(&g_tcpChannelInfoList->lock);
588 return true;
589 }
590
TcpTranGetAppInfobyChannelId(int32_t channelId,AppInfo * appInfo)591 int32_t TcpTranGetAppInfobyChannelId(int32_t channelId, AppInfo* appInfo)
592 {
593 if (GetSessionConnLock() != SOFTBUS_OK) {
594 return SOFTBUS_LOCK_ERR;
595 }
596 SessionConn *connInfo = NULL;
597 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
598 if (connInfo->channelId == channelId) {
599 memcpy_s(appInfo, sizeof(AppInfo), &connInfo->appInfo, sizeof(AppInfo));
600 ReleaseSessionConnLock();
601 return SOFTBUS_OK;
602 }
603 }
604 ReleaseSessionConnLock();
605 TRANS_LOGE(TRANS_CTRL, "not find: channelId=%{public}d", channelId);
606 return SOFTBUS_NOT_FIND;
607 }
608
GetChannelIdsByAuthIdAndStatus(int32_t * num,const AuthHandle * authHandle,uint32_t status)609 int32_t *GetChannelIdsByAuthIdAndStatus(int32_t *num, const AuthHandle *authHandle, uint32_t status)
610 {
611 if (num == NULL || authHandle == NULL) {
612 TRANS_LOGE(TRANS_CTRL, "Invaild param");
613 return NULL;
614 }
615 TRANS_LOGD(TRANS_CTRL, "AuthId=%{public}" PRId64 ",status=%{public}d", authHandle->authId, status);
616 if (GetSessionConnLock() != SOFTBUS_OK) {
617 TRANS_LOGE(TRANS_CTRL, "GetSessionConnLock failed");
618 return NULL;
619 }
620 SessionConn *connInfo = NULL;
621 int32_t count = 0;
622 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
623 if (connInfo->authHandle.authId == authHandle->authId && connInfo->status == status &&
624 connInfo->authHandle.type == authHandle->type) {
625 count++;
626 }
627 }
628 if (count == 0) {
629 ReleaseSessionConnLock();
630 TRANS_LOGE(TRANS_CTRL, "Not find channle id with authId=%{public}" PRId64 ", status=%{public}d",
631 authHandle->authId, status);
632 return NULL;
633 }
634 *num = count;
635 connInfo = NULL;
636 int32_t tmp = 0;
637 int32_t *result = (int32_t *)SoftBusCalloc(count * sizeof(int32_t));
638 if (result == NULL) {
639 TRANS_LOGE(TRANS_CTRL, "malloc result failed");
640 ReleaseSessionConnLock();
641 return NULL;
642 }
643 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
644 if (connInfo->authHandle.authId == authHandle->authId && connInfo->status == status &&
645 connInfo->authHandle.type == authHandle->type) {
646 result[tmp++] = connInfo->channelId;
647 }
648 }
649 ReleaseSessionConnLock();
650 return result;
651 }
652
TransGetPidByChanId(int32_t channelId,int32_t channelType,int32_t * pid)653 int32_t TransGetPidByChanId(int32_t channelId, int32_t channelType, int32_t *pid)
654 {
655 if (pid == NULL) {
656 TRANS_LOGE(TRANS_CTRL, "pid is null");
657 return SOFTBUS_INVALID_PARAM;
658 }
659
660 if (g_tcpChannelInfoList == NULL) {
661 TRANS_LOGE(TRANS_CTRL, "tcp channel info list hasn't init.");
662 return SOFTBUS_INVALID_PARAM;
663 }
664
665 if (SoftBusMutexLock(&(g_tcpChannelInfoList->lock)) != SOFTBUS_OK) {
666 TRANS_LOGE(TRANS_SVC, "lock failed");
667 return SOFTBUS_LOCK_ERR;
668 }
669
670 TcpChannelInfo *info = NULL;
671 LIST_FOR_EACH_ENTRY(info, &(g_tcpChannelInfoList->list), TcpChannelInfo, node) {
672 if (info->channelId == channelId && info->channelType == channelType) {
673 *pid = info->pid;
674 (void)SoftBusMutexUnlock(&(g_tcpChannelInfoList->lock));
675 return SOFTBUS_OK;
676 }
677 }
678 (void)SoftBusMutexUnlock(&(g_tcpChannelInfoList->lock));
679 TRANS_LOGE(TRANS_SVC, "can not find pid by channelId=%{public}d", channelId);
680 return SOFTBUS_TRANS_INVALID_CHANNEL_ID;
681 }
682
TransTdcUpdateReplyCnt(int32_t channelId)683 int32_t TransTdcUpdateReplyCnt(int32_t channelId)
684 {
685 if (GetSessionConnLock() != SOFTBUS_OK) {
686 TRANS_LOGE(TRANS_CTRL, " g_sessionConnList lock fail!");
687 return SOFTBUS_LOCK_ERR;
688 }
689 SessionConn *connInfo = NULL;
690 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
691 if (connInfo->channelId == channelId) {
692 connInfo->appInfo.waitOpenReplyCnt = CHANNEL_OPEN_SUCCESS;
693 ReleaseSessionConnLock();
694 return SOFTBUS_OK;
695 }
696 }
697 ReleaseSessionConnLock();
698 TRANS_LOGE(TRANS_CTRL, "not find: channelId=%{public}d", channelId);
699 return SOFTBUS_NOT_FIND;
700 }
701
TransTdcResetReplyCnt(int32_t channelId)702 int32_t TransTdcResetReplyCnt(int32_t channelId)
703 {
704 if (GetSessionConnLock() != SOFTBUS_OK) {
705 return SOFTBUS_LOCK_ERR;
706 }
707 SessionConn *connInfo = NULL;
708 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
709 if (connInfo->channelId == channelId) {
710 connInfo->appInfo.waitOpenReplyCnt = 0;
711 ReleaseSessionConnLock();
712 return SOFTBUS_OK;
713 }
714 }
715 ReleaseSessionConnLock();
716 TRANS_LOGE(TRANS_SVC, "can not find by channelId=%{public}d", channelId);
717 return SOFTBUS_NOT_FIND;
718 }
719
TransCheckTdcChannelOpenStatus(int32_t channelId,int32_t * curCount)720 int32_t TransCheckTdcChannelOpenStatus(int32_t channelId, int32_t *curCount)
721 {
722 if (GetSessionConnLock() != SOFTBUS_OK) {
723 TRANS_LOGE(TRANS_CTRL, " g_sessionConnList lock fail!");
724 return SOFTBUS_LOCK_ERR;
725 }
726 SessionConn *connInfo = NULL;
727 LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
728 if (connInfo->channelId == channelId) {
729 if (connInfo->appInfo.waitOpenReplyCnt != CHANNEL_OPEN_SUCCESS) {
730 connInfo->appInfo.waitOpenReplyCnt++;
731 }
732 *curCount = connInfo->appInfo.waitOpenReplyCnt;
733 ReleaseSessionConnLock();
734 return SOFTBUS_OK;
735 }
736 }
737 ReleaseSessionConnLock();
738 TRANS_LOGE(TRANS_CTRL, "session conn item not found by channelId=%{public}d", channelId);
739 return SOFTBUS_NOT_FIND;
740 }
741
TransTcpGetPrivilegeCloseList(ListNode * privilegeCloseList,uint64_t tokenId,int32_t pid)742 int32_t TransTcpGetPrivilegeCloseList(ListNode *privilegeCloseList, uint64_t tokenId, int32_t pid)
743 {
744 if (privilegeCloseList == NULL) {
745 TRANS_LOGE(TRANS_CTRL, "privilegeCloseList is null");
746 return SOFTBUS_INVALID_PARAM;
747 }
748 if (g_tcpChannelInfoList == NULL) {
749 TRANS_LOGE(TRANS_CTRL, "tcp channel info list hasn't init.");
750 return SOFTBUS_INVALID_PARAM;
751 }
752 if (SoftBusMutexLock(&(g_tcpChannelInfoList->lock)) != SOFTBUS_OK) {
753 TRANS_LOGE(TRANS_SVC, "lock failed");
754 return SOFTBUS_LOCK_ERR;
755 }
756 TcpChannelInfo *item = NULL;
757 LIST_FOR_EACH_ENTRY(item, &g_tcpChannelInfoList->list, TcpChannelInfo, node) {
758 if (item->callingTokenId == tokenId && item->pid == pid) {
759 (void)PrivilegeCloseListAddItem(privilegeCloseList, item->pid, item->pkgName);
760 }
761 }
762 (void)SoftBusMutexUnlock(&(g_tcpChannelInfoList->lock));
763 return SOFTBUS_OK;
764 }