1 /*
2 * Copyright (c) 2021 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 "securec.h"
19 #include "softbus_adapter_mem.h"
20 #include "softbus_adapter_thread.h"
21 #include "softbus_errcode.h"
22 #include "softbus_log.h"
23 #include "softbus_utils.h"
24 #include "trans_channel_callback.h"
25
26 #define MAX_SESSION_SERVER_NUM 32
27
28 static SoftBusList *g_sessionServerList = NULL;
29
TransSessionMgrInit(void)30 int32_t TransSessionMgrInit(void)
31 {
32 if (g_sessionServerList != NULL) {
33 return SOFTBUS_OK;
34 }
35 g_sessionServerList = CreateSoftBusList();
36 if (g_sessionServerList == NULL) {
37 return SOFTBUS_ERR;
38 }
39 return SOFTBUS_OK;
40 }
41
TransSessionMgrDeinit(void)42 void TransSessionMgrDeinit(void)
43 {
44 if (g_sessionServerList != NULL) {
45 DestroySoftBusList(g_sessionServerList);
46 g_sessionServerList = NULL;
47 }
48 }
49
TransSessionServerIsExist(const char * sessionName)50 bool TransSessionServerIsExist(const char *sessionName)
51 {
52 if (sessionName == NULL) {
53 return false;
54 }
55 if (g_sessionServerList == NULL) {
56 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "not init");
57 return false;
58 }
59
60 SessionServer *pos = NULL;
61 SessionServer *tmp = NULL;
62 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
63 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
64 return false;
65 }
66 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
67 if (strcmp(pos->sessionName, sessionName) == 0) {
68 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "session server [%s] is exist", sessionName);
69 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
70 return true;
71 }
72 }
73
74 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
75 return false;
76 }
77
TransSessionServerAddItem(SessionServer * newNode)78 int32_t TransSessionServerAddItem(SessionServer *newNode)
79 {
80 if (newNode == NULL) {
81 return SOFTBUS_INVALID_PARAM;
82 }
83 if (g_sessionServerList == NULL) {
84 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "not init");
85 return SOFTBUS_NO_INIT;
86 }
87
88 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
89 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
90 return SOFTBUS_ERR;
91 }
92 if (g_sessionServerList->cnt >= MAX_SESSION_SERVER_NUM) {
93 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
94 return SOFTBUS_INVALID_NUM;
95 }
96 SessionServer *pos = NULL;
97 SessionServer *tmp = NULL;
98 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
99 if (strcmp(pos->sessionName, newNode->sessionName) == 0) {
100 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "session server [%s] is exist", newNode->sessionName);
101 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
102 return SOFTBUS_SERVER_NAME_REPEATED;
103 }
104 }
105
106 ListAdd(&(g_sessionServerList->list), &(newNode->node));
107 g_sessionServerList->cnt++;
108 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
109
110 return SOFTBUS_OK;
111 }
112
TransSessionServerDelItem(const char * sessionName)113 int32_t TransSessionServerDelItem(const char *sessionName)
114 {
115 if (sessionName == NULL) {
116 return SOFTBUS_INVALID_PARAM;
117 }
118 if (g_sessionServerList == NULL) {
119 return SOFTBUS_ERR;
120 }
121
122 bool isFind = false;
123 SessionServer *pos = NULL;
124 SessionServer *tmp = NULL;
125 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
126 return SOFTBUS_ERR;
127 }
128 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
129 if (strcmp(pos->sessionName, sessionName) == 0) {
130 isFind = true;
131 break;
132 }
133 }
134 if (isFind) {
135 ListDelete(&pos->node);
136 g_sessionServerList->cnt--;
137 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "destroy session server [%s]", sessionName);
138 SoftBusFree(pos);
139 }
140 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
141 return SOFTBUS_OK;
142 }
143
TransDelItemByPackageName(const char * pkgName)144 void TransDelItemByPackageName(const char *pkgName)
145 {
146 if (pkgName == NULL || g_sessionServerList == NULL) {
147 return;
148 }
149
150 SessionServer *pos = NULL;
151 SessionServer *tmp = NULL;
152 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
153 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
154 return;
155 }
156 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
157 if (strcmp(pos->pkgName, pkgName) == 0) {
158 ListDelete(&pos->node);
159 g_sessionServerList->cnt--;
160 SoftBusFree(pos);
161 pos = NULL;
162 break;
163 }
164 }
165 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
166 }
167
TransGetPkgNameBySessionName(const char * sessionName,char * pkgName,uint16_t len)168 int32_t TransGetPkgNameBySessionName(const char *sessionName, char *pkgName, uint16_t len)
169 {
170 if ((sessionName == NULL) || (pkgName == NULL) || (len == 0)) {
171 return SOFTBUS_ERR;
172 }
173 if (g_sessionServerList == NULL) {
174 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "not init");
175 return SOFTBUS_ERR;
176 }
177
178 SessionServer *pos = NULL;
179 SessionServer *tmp = NULL;
180 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
181 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
182 return SOFTBUS_ERR;
183 }
184 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
185 if (strcmp(pos->sessionName, sessionName) == 0) {
186 int32_t ret = strcpy_s(pkgName, len, pos->pkgName);
187 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
188 if (ret != 0) {
189 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "strcpy_s error ret, [%d]", ret);
190 return SOFTBUS_ERR;
191 }
192 return SOFTBUS_OK;
193 }
194 }
195
196 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
197 return SOFTBUS_ERR;
198 }
199
TransGetUidAndPid(const char * sessionName,int32_t * uid,int32_t * pid)200 int32_t TransGetUidAndPid(const char *sessionName, int32_t *uid, int32_t *pid)
201 {
202 if (sessionName == NULL || uid == NULL || pid == NULL) {
203 return SOFTBUS_INVALID_PARAM;
204 }
205 if (g_sessionServerList == NULL) {
206 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "not init");
207 return SOFTBUS_ERR;
208 }
209
210 SessionServer *pos = NULL;
211 SessionServer *tmp = NULL;
212 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
213 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
214 return SOFTBUS_LOCK_ERR;
215 }
216 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
217 if (strcmp(pos->sessionName, sessionName) == 0) {
218 *uid = pos->uid;
219 *pid = pos->pid;
220 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransGetUidAndPid: sessionName=%s, uid=%d, pid=%d",
221 sessionName, pos->uid, pos->pid);
222 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
223 return SOFTBUS_OK;
224 }
225 }
226
227 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
228 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransGetUidAndPid err: sessionName=%s", sessionName);
229 return SOFTBUS_ERR;
230 }
231
TransOnLinkDown(const char * networkId,int32_t routeType)232 void TransOnLinkDown(const char *networkId, int32_t routeType)
233 {
234 if (networkId == NULL || g_sessionServerList == NULL) {
235 return;
236 }
237 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransOnLinkDown: routeType=%d", routeType);
238
239 SessionServer *pos = NULL;
240 SessionServer *tmp = NULL;
241
242 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
243 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
244 return;
245 }
246 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
247 (void)TransServerOnChannelLinkDown(pos->pkgName, networkId, routeType);
248 }
249 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
250
251 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransOnLinkDown end");
252 return;
253 }