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