1 /*
2 * Copyright (c) 2021-2025 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_session_manager.h"
17
18 #include "anonymizer.h"
19 #include "lnn_lane_link.h"
20 #include "permission_entry.h"
21 #include "securec.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_adapter_thread.h"
24 #include "softbus_def.h"
25 #include "softbus_error_code.h"
26 #include "softbus_utils.h"
27 #include "legacy/softbus_hidumper_trans.h"
28 #include "trans_channel_callback.h"
29 #include "trans_client_proxy.h"
30 #include "trans_log.h"
31
32 #define MAX_SESSION_SERVER_NUM 100
33 #define CMD_REGISTED_SESSION_LIST "registed_sessionlist"
34 #define GET_ROUTE_TYPE(type) ((uint32_t)(type) & 0xff)
35 #define GET_CONN_TYPE(type) (((uint32_t)(type) >> 8) & 0xff)
36
37 static SoftBusList *g_sessionServerList = NULL;
38
TransSessionForEachShowInfo(int32_t fd)39 static int32_t TransSessionForEachShowInfo(int32_t fd)
40 {
41 if (g_sessionServerList == NULL) {
42 TRANS_LOGE(TRANS_CTRL, "session server list is empty");
43 return SOFTBUS_NO_INIT;
44 }
45 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
46 TRANS_LOGE(TRANS_CTRL, "lock mutex failed");
47 return SOFTBUS_LOCK_ERR;
48 }
49
50 SessionServer *pos = NULL;
51 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
52 SoftBusTransDumpRegisterSession(fd, pos->pkgName, pos->sessionName, pos->uid, pos->pid);
53 }
54
55 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
56 return SOFTBUS_OK;
57 }
58
TransSessionMgrInit(void)59 int32_t TransSessionMgrInit(void)
60 {
61 if (g_sessionServerList != NULL) {
62 return SOFTBUS_OK;
63 }
64 g_sessionServerList = CreateSoftBusList();
65 if (g_sessionServerList == NULL) {
66 TRANS_LOGE(TRANS_CTRL, "session manager init fail");
67 return SOFTBUS_MALLOC_ERR;
68 }
69
70 return SoftBusRegTransVarDump(CMD_REGISTED_SESSION_LIST, TransSessionForEachShowInfo);
71 }
72
TransSessionMgrDeinit(void)73 void TransSessionMgrDeinit(void)
74 {
75 if (g_sessionServerList != NULL) {
76 DestroySoftBusList(g_sessionServerList);
77 g_sessionServerList = NULL;
78 }
79 }
80
TransSessionServerIsExist(const char * sessionName)81 bool TransSessionServerIsExist(const char *sessionName)
82 {
83 if (sessionName == NULL) {
84 return false;
85 }
86 if (g_sessionServerList == NULL) {
87 TRANS_LOGE(TRANS_CTRL, "sessionServerList not init");
88 return false;
89 }
90
91 SessionServer *pos = NULL;
92 SessionServer *tmp = NULL;
93 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
94 TRANS_LOGE(TRANS_CTRL, "lock mutex failed");
95 return false;
96 }
97
98 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
99 if (strcmp(pos->sessionName, sessionName) == 0) {
100 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
101 return true;
102 }
103 }
104 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
105 char *tmpSessionName = NULL;
106 Anonymize(sessionName, &tmpSessionName);
107 TRANS_LOGE(TRANS_CTRL, "session server not exist, sessionName=%{public}s", AnonymizeWrapper(tmpSessionName));
108 AnonymizeFree(tmpSessionName);
109 return false;
110 }
111
ShowSessionServer(void)112 static void ShowSessionServer(void)
113 {
114 SessionServer *pos = NULL;
115 SessionServer *tmp = NULL;
116 int32_t count = 0;
117 char *tmpName = NULL;
118 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
119 Anonymize(pos->sessionName, &tmpName);
120 TRANS_LOGI(TRANS_CTRL,
121 "session server is exist. count=%{public}d, sessionName=%{public}s", count, AnonymizeWrapper(tmpName));
122 AnonymizeFree(tmpName);
123 count++;
124 }
125 }
126
TransSessionServerAddItem(SessionServer * newNode)127 int32_t TransSessionServerAddItem(SessionServer *newNode)
128 {
129 TRANS_CHECK_AND_RETURN_RET_LOGE(newNode != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "param invalid");
130 TRANS_CHECK_AND_RETURN_RET_LOGE(g_sessionServerList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "not init");
131 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
132 TRANS_LOGE(TRANS_CTRL, "lock mutex failed");
133 return SOFTBUS_LOCK_ERR;
134 }
135 if (g_sessionServerList->cnt >= MAX_SESSION_SERVER_NUM) {
136 (void)ShowSessionServer();
137 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
138 TRANS_LOGE(TRANS_CTRL, "session server num reach max");
139 return SOFTBUS_INVALID_NUM;
140 }
141
142 SessionServer *pos = NULL;
143 SessionServer *tmp = NULL;
144 char *tmpName = NULL;
145 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
146 if (strcmp(pos->sessionName, newNode->sessionName) == 0) {
147 Anonymize(newNode->sessionName, &tmpName);
148 if ((pos->uid == newNode->uid) && (pos->pid == newNode->pid)) {
149 TRANS_LOGI(TRANS_CTRL, "session server is exist, sessionName=%{public}s", AnonymizeWrapper(tmpName));
150 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
151 AnonymizeFree(tmpName);
152 return SOFTBUS_SERVER_NAME_REPEATED;
153 }
154 if (CheckServiceIsRegistered(pos->pkgName, pos->pid) == SOFTBUS_OK) {
155 TRANS_LOGI(TRANS_CTRL,
156 "sessionName=%{public}s has been used by other processes, old Pid=%{public}d, new pid=%{public}d",
157 AnonymizeWrapper(tmpName), pos->pid, newNode->pid);
158 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
159 AnonymizeFree(tmpName);
160 return SOFTBUS_SERVER_NAME_USED;
161 }
162 pos->pid = newNode->pid;
163 TRANS_LOGI(TRANS_CTRL, "sessionName=%{public}s is exist. old pid=%{public}d, new pid=%{public}d",
164 AnonymizeWrapper(tmpName), pos->pid, newNode->pid);
165 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
166 AnonymizeFree(tmpName);
167 SoftBusFree(newNode);
168 return SOFTBUS_OK;
169 }
170 }
171
172 Anonymize(newNode->sessionName, &tmpName);
173 ListAdd(&(g_sessionServerList->list), &(newNode->node));
174 TRANS_LOGI(TRANS_CTRL, "add sessionName=%{public}s", AnonymizeWrapper(tmpName));
175 g_sessionServerList->cnt++;
176 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
177 AnonymizeFree(tmpName);
178 return SOFTBUS_OK;
179 }
180
TransSessionServerDelItem(const char * sessionName)181 int32_t TransSessionServerDelItem(const char *sessionName)
182 {
183 if (sessionName == NULL) {
184 return SOFTBUS_INVALID_PARAM;
185 }
186 if (g_sessionServerList == NULL) {
187 return SOFTBUS_NO_INIT;
188 }
189
190 bool isFind = false;
191 SessionServer *pos = NULL;
192 SessionServer *tmp = NULL;
193 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
194 TRANS_LOGE(TRANS_CTRL, "lock mutex failed");
195 return SOFTBUS_LOCK_ERR;
196 }
197 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
198 if (strcmp(pos->sessionName, sessionName) == 0) {
199 isFind = true;
200 break;
201 }
202 }
203 if (isFind) {
204 ListDelete(&pos->node);
205 if (pos->accessInfo.extraAccessInfo != NULL) {
206 SoftBusFree(pos->accessInfo.extraAccessInfo);
207 pos->accessInfo.extraAccessInfo = NULL;
208 }
209 SoftBusFree(pos);
210 g_sessionServerList->cnt--;
211 char *tmpName = NULL;
212 Anonymize(sessionName, &tmpName);
213 TRANS_LOGI(TRANS_CTRL, "destroy session server sessionName=%{public}s", AnonymizeWrapper(tmpName));
214 AnonymizeFree(tmpName);
215 }
216 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
217 return SOFTBUS_OK;
218 }
219
CheckAndUpdateTimeBySessionName(const char * sessionName,uint64_t timestamp)220 int32_t CheckAndUpdateTimeBySessionName(const char *sessionName, uint64_t timestamp)
221 {
222 if (sessionName == NULL) {
223 TRANS_LOGE(TRANS_CTRL, "Parameter sessionName is empty");
224 return SOFTBUS_INVALID_PARAM;
225 }
226 if (g_sessionServerList == NULL) {
227 TRANS_LOGE(TRANS_CTRL, "sessionServer is empty");
228 return SOFTBUS_INVALID_PARAM;
229 }
230 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
231 TRANS_LOGE(TRANS_CTRL, "Lock failure");
232 return SOFTBUS_LOCK_ERR;
233 }
234
235 SessionServer *pos = NULL;
236 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
237 if (strcmp(pos->sessionName, sessionName) == 0) {
238 if (pos->timestamp < timestamp) {
239 pos->timestamp = timestamp;
240 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
241 return SOFTBUS_OK;
242 }
243 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
244 return SOFTBUS_TRANS_SESSION_TIME_NOT_EQUAL;
245 }
246 }
247 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
248 return SOFTBUS_TRANS_SESSION_NAME_NO_EXIST;
249 }
250
CheckUidAndPid(const char * sessionName,pid_t callingUid,pid_t callingPid)251 bool CheckUidAndPid(const char *sessionName, pid_t callingUid, pid_t callingPid)
252 {
253 if (sessionName == NULL) {
254 TRANS_LOGE(TRANS_CTRL, "Parameter sessionName is empty");
255 return false;
256 }
257 if (g_sessionServerList == NULL) {
258 TRANS_LOGE(TRANS_CTRL, "sessionServer is empty");
259 return false;
260 }
261 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
262 TRANS_LOGE(TRANS_CTRL, "Lock failure");
263 return false;
264 }
265
266 SessionServer *pos = NULL;
267 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
268 if (strcmp(pos->sessionName, sessionName) == 0 &&
269 pos->uid == callingUid && pos->pid == callingPid) {
270 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
271 return true;
272 }
273 }
274
275 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
276 return false;
277 }
278
TransDelItemByPackageName(const char * pkgName,int32_t pid)279 void TransDelItemByPackageName(const char *pkgName, int32_t pid)
280 {
281 if (pkgName == NULL || g_sessionServerList == NULL) {
282 return;
283 }
284
285 SessionServer *pos = NULL;
286 SessionServer *tmp = NULL;
287 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
288 TRANS_LOGE(TRANS_CTRL, "lock mutex failed");
289 return;
290 }
291 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
292 if ((strcmp(pos->pkgName, pkgName) == 0) && (pos->pid == pid)) {
293 if (CheckDBinder(pos->sessionName)) {
294 // Remove RPC Permission when Client Process Exit
295 (void)DeleteDynamicPermission(pos->sessionName);
296 }
297 ListDelete(&pos->node);
298 g_sessionServerList->cnt--;
299 if (pos->accessInfo.extraAccessInfo != NULL) {
300 SoftBusFree(pos->accessInfo.extraAccessInfo);
301 pos->accessInfo.extraAccessInfo = NULL;
302 }
303 SoftBusFree(pos);
304 pos = NULL;
305 }
306 }
307 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
308 TRANS_LOGI(TRANS_CTRL, "del pkgName=%{public}s", pkgName);
309 }
310
TransGetPkgNameBySessionName(const char * sessionName,char * pkgName,uint16_t len)311 int32_t TransGetPkgNameBySessionName(const char *sessionName, char *pkgName, uint16_t len)
312 {
313 if (sessionName == NULL || pkgName == NULL || len == 0) {
314 TRANS_LOGE(TRANS_CTRL, "param error");
315 return SOFTBUS_INVALID_PARAM;
316 }
317 if (g_sessionServerList == NULL) {
318 TRANS_LOGE(TRANS_CTRL, "session server list not init");
319 return SOFTBUS_NO_INIT;
320 }
321
322 SessionServer *pos = NULL;
323 SessionServer *tmp = NULL;
324 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
325 TRANS_LOGE(TRANS_CTRL, "lock mutex failed");
326 return SOFTBUS_LOCK_ERR;
327 }
328 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
329 if (strcmp(pos->sessionName, sessionName) == 0) {
330 int32_t ret = strcpy_s(pkgName, len, pos->pkgName);
331 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
332 if (ret != EOK) {
333 TRANS_LOGE(TRANS_CTRL, "strcpy_s error ret, ret=%{public}d", ret);
334 return SOFTBUS_STRCPY_ERR;
335 }
336 return SOFTBUS_OK;
337 }
338 }
339
340 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
341 char *tmpName = NULL;
342 Anonymize(sessionName, &tmpName);
343 TRANS_LOGE(TRANS_CTRL, "not found sessionName=%{public}s.", AnonymizeWrapper(tmpName));
344 AnonymizeFree(tmpName);
345 return SOFTBUS_TRANS_SESSION_NAME_NO_EXIST;
346 }
347
TransGetUidAndPid(const char * sessionName,int32_t * uid,int32_t * pid)348 int32_t TransGetUidAndPid(const char *sessionName, int32_t *uid, int32_t *pid)
349 {
350 if (sessionName == NULL || uid == NULL || pid == NULL) {
351 return SOFTBUS_INVALID_PARAM;
352 }
353 if (g_sessionServerList == NULL) {
354 TRANS_LOGE(TRANS_CTRL, "not init");
355 return SOFTBUS_NO_INIT;
356 }
357
358 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
359 TRANS_LOGE(TRANS_CTRL, "lock mutex failed");
360 return SOFTBUS_LOCK_ERR;
361 }
362
363 SessionServer *pos = NULL;
364 SessionServer *tmp = NULL;
365 char *tmpName = NULL;
366 Anonymize(sessionName, &tmpName);
367 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
368 if (strcmp(pos->sessionName, sessionName) == 0) {
369 *uid = pos->uid;
370 *pid = pos->pid;
371 TRANS_LOGD(TRANS_CTRL, "sessionName=%{public}s, uid=%{public}d, pid=%{public}d",
372 AnonymizeWrapper(tmpName), pos->uid, pos->pid);
373 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
374 AnonymizeFree(tmpName);
375 return SOFTBUS_OK;
376 }
377 }
378
379 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
380 TRANS_LOGE(TRANS_CTRL, "err: sessionName=%{public}s", AnonymizeWrapper(tmpName));
381 AnonymizeFree(tmpName);
382 return SOFTBUS_TRANS_GET_PID_FAILED;
383 }
384
TransListDelete(ListNode * sessionServerList)385 static void TransListDelete(ListNode *sessionServerList)
386 {
387 SessionServer *pos = NULL;
388 SessionServer *tmp = NULL;
389
390 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, sessionServerList, SessionServer, node) {
391 ListDelete(&pos->node);
392 SoftBusFree(pos);
393 }
394 }
395
TransListCopy(ListNode * sessionServerList)396 static int32_t TransListCopy(ListNode *sessionServerList)
397 {
398 TRANS_LOGD(TRANS_CTRL, "enter.");
399 if (sessionServerList == NULL) {
400 return SOFTBUS_NO_INIT;
401 }
402
403 SessionServer *pos = NULL;
404 SessionServer *tmp = NULL;
405 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
406 TRANS_LOGE(TRANS_CTRL, "lock mutex failed");
407 return SOFTBUS_LOCK_ERR;
408 }
409 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
410 SessionServer *newPos = (SessionServer *)SoftBusMalloc(sizeof(SessionServer));
411 if (newPos == NULL) {
412 TransListDelete(sessionServerList);
413 TRANS_LOGE(TRANS_CTRL, "SoftBusMalloc failed");
414 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
415 return SOFTBUS_MALLOC_ERR;
416 }
417 *newPos = *pos;
418 ListAdd(sessionServerList, &newPos->node);
419 }
420 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
421 return SOFTBUS_OK;
422 }
423
TransOnLinkDown(const char * networkId,const char * uuid,const char * udid,const char * peerIp,int32_t type)424 void TransOnLinkDown(const char *networkId, const char *uuid, const char *udid, const char *peerIp, int32_t type)
425 {
426 #define USER_SWITCH_OFFSET 10
427 if (networkId == NULL || g_sessionServerList == NULL) {
428 return;
429 }
430 int32_t routeType = (int32_t)GET_ROUTE_TYPE(type);
431 int32_t connType = (int32_t)GET_CONN_TYPE(type);
432 bool isUserSwitchEvent = (bool)(((uint32_t)(type) >> USER_SWITCH_OFFSET) & 0xff);
433 char *anonyNetworkId = NULL;
434 Anonymize(networkId, &anonyNetworkId);
435 TRANS_LOGI(TRANS_CTRL,
436 "routeType=%{public}d, networkId=%{public}s connType=%{public}d", routeType,
437 AnonymizeWrapper(anonyNetworkId), connType);
438 AnonymizeFree(anonyNetworkId);
439
440 ListNode sessionServerList = {0};
441 ListInit(&sessionServerList);
442 int32_t ret = TransListCopy(&sessionServerList);
443 if (ret != SOFTBUS_OK) {
444 TRANS_LOGE(TRANS_CTRL, "copy list failed");
445 return;
446 }
447
448 SessionServer *pos = NULL;
449 SessionServer *tmp = NULL;
450 LinkDownInfo info = {
451 .uuid = uuid,
452 .udid = udid,
453 .peerIp = peerIp,
454 .networkId = networkId,
455 .routeType = type
456 };
457
458 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &sessionServerList, SessionServer, node) {
459 if (isUserSwitchEvent && pos->callerType != CALLER_TYPE_FEATURE_ABILITY) {
460 continue;
461 }
462 (void)TransServerOnChannelLinkDown(pos->pkgName, pos->pid, &info);
463 }
464
465 if (routeType == WIFI_P2P) {
466 LaneDeleteP2pAddress(networkId, true);
467 }
468 TransListDelete(&sessionServerList);
469 }
470
TransGetPidAndPkgName(const char * sessionName,int32_t uid,int32_t * pid,char * pkgName,uint32_t len)471 int32_t TransGetPidAndPkgName(const char *sessionName, int32_t uid, int32_t *pid, char *pkgName, uint32_t len)
472 {
473 if (sessionName == NULL || pid == NULL || pkgName == NULL) {
474 return SOFTBUS_INVALID_PARAM;
475 }
476
477 if (len == 0 || len > PKG_NAME_SIZE_MAX) {
478 TRANS_LOGE(TRANS_CTRL, "pkgName len is invalid.");
479 return SOFTBUS_INVALID_PARAM;
480 }
481
482 if (g_sessionServerList == NULL) {
483 TRANS_LOGE(TRANS_CTRL, "g_sessionServerList has not been initialized.");
484 return SOFTBUS_NO_INIT;
485 }
486
487 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
488 TRANS_LOGE(TRANS_CTRL, "lock mutex failed.");
489 return SOFTBUS_LOCK_ERR;
490 }
491
492 SessionServer *pos = NULL;
493 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
494 if (strcmp(pos->sessionName, sessionName) == 0 && pos->uid == uid) {
495 *pid = pos->pid;
496 int32_t ret = strcpy_s(pkgName, len, pos->pkgName);
497 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
498 if (ret != EOK) {
499 TRANS_LOGE(TRANS_CTRL, "strcpy_s error ret, ret=%{public}d.", ret);
500 return SOFTBUS_STRCPY_ERR;
501 }
502 return SOFTBUS_OK;
503 }
504 }
505
506 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
507 char *tmpName = NULL;
508 Anonymize(sessionName, &tmpName);
509 TRANS_LOGE(TRANS_CTRL, "sessionName=%{public}s uid=%{public}d not found.", AnonymizeWrapper(tmpName), uid);
510 AnonymizeFree(tmpName);
511 return SOFTBUS_TRANS_GET_PID_FAILED;
512 }
513
TransGetTokenIdBySessionName(const char * sessionName,uint64_t * tokenId)514 int32_t TransGetTokenIdBySessionName(const char *sessionName, uint64_t *tokenId)
515 {
516 if (sessionName == NULL || tokenId == NULL) {
517 return SOFTBUS_INVALID_PARAM;
518 }
519 if (g_sessionServerList == NULL) {
520 TRANS_LOGE(TRANS_CTRL, "g_sessionServerList has not been initialized.");
521 return SOFTBUS_NO_INIT;
522 }
523
524 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
525 TRANS_LOGE(TRANS_CTRL, "lock mutex failed.");
526 return SOFTBUS_LOCK_ERR;
527 }
528 SessionServer *pos = NULL;
529 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
530 if (strcmp(pos->sessionName, sessionName) == 0) {
531 *tokenId = pos->tokenId;
532 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
533 return SOFTBUS_OK;
534 }
535 }
536 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
537 char *tmpName = NULL;
538 Anonymize(sessionName, &tmpName);
539 TRANS_LOGE(TRANS_CTRL, "sessionName=%{public}s not found.", AnonymizeWrapper(tmpName));
540 AnonymizeFree(tmpName);
541 return SOFTBUS_TRANS_SESSION_NAME_NO_EXIST;
542 }
543
CheckAccessInfoAndCalloc(SessionServer * pos,uint32_t strLen)544 static int32_t CheckAccessInfoAndCalloc(SessionServer *pos, uint32_t strLen)
545 {
546 if (strLen > EXTRA_ACCESS_INFO_LEN_MAX) {
547 TRANS_LOGE(TRANS_CTRL, "extra access info length over limit.");
548 return SOFTBUS_INVALID_PARAM;
549 }
550 if (pos->accessInfo.extraAccessInfo == NULL) {
551 pos->accessInfo.extraAccessInfo = (char *)SoftBusCalloc(strLen);
552 if (pos->accessInfo.extraAccessInfo == NULL) {
553 return SOFTBUS_MALLOC_ERR;
554 }
555 } else {
556 SoftBusFree(pos->accessInfo.extraAccessInfo);
557 pos->accessInfo.extraAccessInfo = (char *)SoftBusCalloc(strLen);
558 if (pos->accessInfo.extraAccessInfo == NULL) {
559 return SOFTBUS_MALLOC_ERR;
560 }
561 }
562 return SOFTBUS_OK;
563 }
564
AddAccessInfoBySessionName(const char * sessionName,const AccessInfo * accessInfo,pid_t callingPid)565 int32_t AddAccessInfoBySessionName(const char *sessionName, const AccessInfo *accessInfo, pid_t callingPid)
566 {
567 if (sessionName == NULL || accessInfo == NULL) {
568 TRANS_LOGE(TRANS_CTRL, "invalid param.");
569 return SOFTBUS_INVALID_PARAM;
570 }
571 if (g_sessionServerList == NULL) {
572 TRANS_LOGE(TRANS_CTRL, "g_sessionServerList has not been initialized.");
573 return SOFTBUS_NO_INIT;
574 }
575
576 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
577 TRANS_LOGE(TRANS_CTRL, "lock mutex failed.");
578 return SOFTBUS_LOCK_ERR;
579 }
580 SessionServer *pos = NULL;
581 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
582 if ((callingPid == 0 || pos->pid == callingPid) && strcmp(pos->sessionName, sessionName) == 0) {
583 uint32_t extraAccessInfoLen = strlen(accessInfo->extraAccessInfo) + 1;
584 if (CheckAccessInfoAndCalloc(pos, extraAccessInfoLen) != SOFTBUS_OK) {
585 TRANS_LOGE(TRANS_CTRL, "accountId or extra access info calloc failed.");
586 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
587 return SOFTBUS_MALLOC_ERR;
588 }
589 pos->accessInfo.userId = accessInfo->userId;
590 pos->accessInfo.localTokenId = accessInfo->localTokenId;
591 if (strcpy_s(pos->accessInfo.extraAccessInfo, extraAccessInfoLen, accessInfo->extraAccessInfo) != EOK) {
592 TRANS_LOGE(TRANS_CTRL, "accountId or extra access info strcpy failed.");
593 SoftBusFree(pos->accessInfo.extraAccessInfo);
594 pos->accessInfo.extraAccessInfo = NULL;
595 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
596 return SOFTBUS_STRCPY_ERR;
597 }
598 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
599 return SOFTBUS_OK;
600 }
601 }
602 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
603 char *tmpName = NULL;
604 Anonymize(sessionName, &tmpName);
605 TRANS_LOGE(TRANS_CTRL, "sessionName=%{public}s not found.", AnonymizeWrapper(tmpName));
606 AnonymizeFree(tmpName);
607 return SOFTBUS_TRANS_SESSION_NAME_NO_EXIST;
608 }
609
GetAccessInfoBySessionName(const char * sessionName,int32_t * userId,uint64_t * tokenId,char * businessAccountId,char * extraAccessInfo)610 int32_t GetAccessInfoBySessionName(
611 const char *sessionName, int32_t *userId, uint64_t *tokenId, char *businessAccountId, char *extraAccessInfo)
612 {
613 if (sessionName == NULL || userId == NULL || tokenId == NULL) {
614 TRANS_LOGE(TRANS_CTRL, "invalid param.");
615 return SOFTBUS_INVALID_PARAM;
616 }
617 if (g_sessionServerList == NULL) {
618 TRANS_LOGE(TRANS_CTRL, "g_sessionServerList has not been initialized.");
619 return SOFTBUS_NO_INIT;
620 }
621
622 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
623 TRANS_LOGE(TRANS_CTRL, "lock mutex failed.");
624 return SOFTBUS_LOCK_ERR;
625 }
626 SessionServer *pos = NULL;
627 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
628 if (strcmp(pos->sessionName, sessionName) == 0) {
629 *userId = pos->accessInfo.userId;
630 *tokenId = pos->accessInfo.localTokenId;
631 if (businessAccountId != NULL && pos->accessInfo.businessAccountId != NULL &&
632 strcpy_s(businessAccountId, ACCOUNT_UID_LEN_MAX, pos->accessInfo.businessAccountId) != EOK) {
633 TRANS_LOGE(TRANS_CTRL, "accountId strcpy failed.");
634 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
635 return SOFTBUS_STRCPY_ERR;
636 }
637 if (extraAccessInfo != NULL && pos->accessInfo.extraAccessInfo != NULL &&
638 strcpy_s(extraAccessInfo, EXTRA_ACCESS_INFO_LEN_MAX, pos->accessInfo.extraAccessInfo) != EOK) {
639 TRANS_LOGE(TRANS_CTRL, "extra access info strcpy failed.");
640 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
641 return SOFTBUS_STRCPY_ERR;
642 }
643 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
644 return SOFTBUS_OK;
645 }
646 }
647 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
648 char *tmpName = NULL;
649 Anonymize(sessionName, &tmpName);
650 TRANS_LOGE(TRANS_CTRL, "sessionName=%{public}s not found.", AnonymizeWrapper(tmpName));
651 AnonymizeFree(tmpName);
652 return SOFTBUS_TRANS_SESSION_NAME_NO_EXIST;
653 }
654
GetTokenTypeBySessionName(const char * sessionName,int32_t * tokenType)655 int32_t GetTokenTypeBySessionName(const char *sessionName, int32_t *tokenType)
656 {
657 if (sessionName == NULL || tokenType == NULL) {
658 TRANS_LOGE(TRANS_CTRL, "invalid param.");
659 return SOFTBUS_INVALID_PARAM;
660 }
661 if (g_sessionServerList == NULL) {
662 TRANS_LOGE(TRANS_CTRL, "g_sessionServerList has not been initialized.");
663 return SOFTBUS_NO_INIT;
664 }
665
666 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
667 TRANS_LOGE(TRANS_CTRL, "lock mutex failed.");
668 return SOFTBUS_LOCK_ERR;
669 }
670 SessionServer *pos = NULL;
671 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
672 if (strcmp(pos->sessionName, sessionName) == 0) {
673 *tokenType = pos->tokenType;
674 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
675 return SOFTBUS_OK;
676 }
677 }
678 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
679 char *tmpName = NULL;
680 Anonymize(sessionName, &tmpName);
681 TRANS_LOGE(TRANS_CTRL, "sessionName=%{public}s not found.", AnonymizeWrapper(tmpName));
682 AnonymizeFree(tmpName);
683 return SOFTBUS_TRANS_SESSION_NAME_NO_EXIST;
684 }
685
TransGetAclInfoBySessionName(const char * sessionName,uint64_t * tokenId,int32_t * pid,int32_t * userId)686 int32_t TransGetAclInfoBySessionName(const char *sessionName, uint64_t *tokenId, int32_t *pid, int32_t *userId)
687 {
688 if (sessionName == NULL || tokenId == NULL) {
689 TRANS_LOGE(TRANS_CTRL, "invalid param.");
690 return SOFTBUS_INVALID_PARAM;
691 }
692
693 if (g_sessionServerList == NULL) {
694 TRANS_LOGE(TRANS_CTRL, "g_sessionServerList has not been initialized.");
695 return SOFTBUS_NO_INIT;
696 }
697
698 if (SoftBusMutexLock(&g_sessionServerList->lock) != SOFTBUS_OK) {
699 TRANS_LOGE(TRANS_CTRL, "lock mutex failed.");
700 return SOFTBUS_LOCK_ERR;
701 }
702 SessionServer *pos = NULL;
703 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
704 if (strcmp(pos->sessionName, sessionName) == 0) {
705 *tokenId = pos->tokenId;
706 if (pid != NULL) {
707 *pid = pos->pid;
708 }
709 if (userId != NULL) {
710 *userId = pos->accessInfo.userId;
711 }
712 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
713 return SOFTBUS_OK;
714 }
715 }
716 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
717 char *tmpName = NULL;
718 Anonymize(sessionName, &tmpName);
719 TRANS_LOGE(TRANS_CTRL, "sessionName=%{public}s not found.", AnonymizeWrapper(tmpName));
720 AnonymizeFree(tmpName);
721 return SOFTBUS_TRANS_SESSION_NAME_NO_EXIST;
722 }
723