• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "trans_tcp_direct_sessionconn.h"
17 
18 #include <securec.h>
19 
20 #include "auth_interface.h"
21 #include "softbus_adapter_mem.h"
22 #include "softbus_adapter_thread.h"
23 #include "softbus_base_listener.h"
24 #include "softbus_def.h"
25 #include "softbus_errcode.h"
26 #include "softbus_log.h"
27 #include "trans_channel_manager.h"
28 
29 #define TRANS_SEQ_STEP 2
30 
31 static SoftBusList *g_sessionConnList = NULL;
32 
TransTdcGetNewSeqId(void)33 NO_SANITIZE("cfi") uint64_t TransTdcGetNewSeqId(void)
34 {
35     if (GetSessionConnLock() != SOFTBUS_OK) {
36         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransTdcGetNewSeqId GetLock fail");
37         return INVALID_SEQ_ID;
38     }
39 
40     static uint64_t seq = 0;
41     seq += TRANS_SEQ_STEP;
42 
43     uint64_t retseq = seq;
44 
45     ReleaseSessonConnLock();
46 
47     return retseq;
48 }
49 
CreatSessionConnList(void)50 NO_SANITIZE("cfi") int32_t CreatSessionConnList(void)
51 {
52     if (g_sessionConnList == NULL) {
53         g_sessionConnList = CreateSoftBusList();
54         if (g_sessionConnList == NULL) {
55             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CreateSoftBusList fail");
56             return SOFTBUS_MALLOC_ERR;
57         }
58     }
59     return SOFTBUS_OK;
60 }
61 
GetSessionConnList(void)62 NO_SANITIZE("cfi") SoftBusList *GetSessionConnList(void)
63 {
64     if (g_sessionConnList == NULL) {
65         return NULL;
66     }
67     return g_sessionConnList;
68 }
69 
GetSessionConnLock(void)70 NO_SANITIZE("cfi") int32_t GetSessionConnLock(void)
71 {
72     if (g_sessionConnList == NULL) {
73         return SOFTBUS_NO_INIT;
74     }
75     if (SoftBusMutexLock(&g_sessionConnList->lock) != 0) {
76         return SOFTBUS_LOCK_ERR;
77     }
78     return SOFTBUS_OK;
79 }
80 
ReleaseSessonConnLock(void)81 NO_SANITIZE("cfi") void ReleaseSessonConnLock(void)
82 {
83     if (g_sessionConnList == NULL) {
84         return;
85     }
86     (void)SoftBusMutexUnlock(&g_sessionConnList->lock);
87 }
88 
GetSessionConnByRequestId(uint32_t requestId)89 NO_SANITIZE("cfi") SessionConn *GetSessionConnByRequestId(uint32_t requestId)
90 {
91     if (g_sessionConnList == NULL) {
92         return NULL;
93     }
94     SessionConn *item = NULL;
95     SessionConn *nextItem = NULL;
96     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_sessionConnList->list, SessionConn, node) {
97         if (item->requestId == requestId) {
98             return item;
99         }
100     }
101     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetSessionConnByReqId fail: reqId=%u", requestId);
102     return NULL;
103 }
104 
GetSessionConnByReq(int64_t req)105 NO_SANITIZE("cfi") SessionConn *GetSessionConnByReq(int64_t req)
106 {
107     if (g_sessionConnList == NULL) {
108         return NULL;
109     }
110     SessionConn *item = NULL;
111     SessionConn *nextItem = NULL;
112     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_sessionConnList->list, SessionConn, node) {
113         if (item->req == req) {
114             return item;
115         }
116     }
117     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetSessionConnByReqId fail: reqId=%" PRIu64, req);
118     return NULL;
119 }
120 
CreateNewSessinConn(ListenerModule module,bool isServerSid)121 NO_SANITIZE("cfi") SessionConn *CreateNewSessinConn(ListenerModule module, bool isServerSid)
122 {
123     SessionConn *conn = (SessionConn *)SoftBusCalloc(sizeof(SessionConn));
124     if (conn == NULL) {
125         return NULL;
126     }
127     conn->serverSide = isServerSid;
128     conn->channelId = GenerateChannelId(true);
129     if (conn->channelId == INVALID_CHANNEL_ID) {
130         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "generate tdc channel id failed.");
131         return NULL;
132     }
133     conn->status = TCP_DIRECT_CHANNEL_STATUS_INIT;
134     conn->timeout = 0;
135     conn->req = -1;
136     conn->authId = AUTH_INVALID_ID;
137     conn->requestId = 0; // invalid num
138     conn->listenMod = module;
139     return conn;
140 }
141 
GetSessionConnByFd(int32_t fd,SessionConn * conn)142 NO_SANITIZE("cfi") SessionConn *GetSessionConnByFd(int32_t fd, SessionConn *conn)
143 {
144     SessionConn *connInfo = NULL;
145     if (GetSessionConnLock() != SOFTBUS_OK) {
146         return NULL;
147     }
148     LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
149         if (connInfo->appInfo.fd == fd) {
150             if (conn != NULL) {
151                 (void)memcpy_s(conn, sizeof(SessionConn), connInfo, sizeof(SessionConn));
152             }
153             ReleaseSessonConnLock();
154             return connInfo;
155         }
156     }
157     ReleaseSessonConnLock();
158 
159     return NULL;
160 }
161 
GetSessionConnById(int32_t channelId,SessionConn * conn)162 NO_SANITIZE("cfi") SessionConn *GetSessionConnById(int32_t channelId, SessionConn *conn)
163 {
164     SessionConn *connInfo = NULL;
165     if (GetSessionConnLock() != SOFTBUS_OK) {
166         return NULL;
167     }
168     LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
169         if (connInfo->channelId == channelId) {
170             if (conn != NULL) {
171                 (void)memcpy_s(conn, sizeof(SessionConn), connInfo, sizeof(SessionConn));
172             }
173             ReleaseSessonConnLock();
174             return connInfo;
175         }
176     }
177     ReleaseSessonConnLock();
178 
179     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "can not get srv session conn info.");
180     return NULL;
181 }
182 
SetAppInfoById(int32_t channelId,const AppInfo * appInfo)183 NO_SANITIZE("cfi") int32_t SetAppInfoById(int32_t channelId, const AppInfo *appInfo)
184 {
185     SessionConn *conn = NULL;
186     if (GetSessionConnLock() != SOFTBUS_OK) {
187         return SOFTBUS_LOCK_ERR;
188     }
189     LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
190         if (conn->channelId == channelId) {
191             (void)memcpy_s(&conn->appInfo, sizeof(AppInfo), appInfo, sizeof(AppInfo));
192             ReleaseSessonConnLock();
193             return SOFTBUS_OK;
194         }
195     }
196     ReleaseSessonConnLock();
197     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "can not get srv session conn info.");
198     return SOFTBUS_ERR;
199 }
200 
GetAppInfoById(int32_t channelId,AppInfo * appInfo)201 NO_SANITIZE("cfi") int32_t GetAppInfoById(int32_t channelId, AppInfo *appInfo)
202 {
203     SessionConn *conn = NULL;
204     if (GetSessionConnLock() != SOFTBUS_OK) {
205         return SOFTBUS_LOCK_ERR;
206     }
207     LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
208         if (conn->channelId == channelId) {
209             (void)memcpy_s(appInfo, sizeof(AppInfo), &conn->appInfo, sizeof(AppInfo));
210             ReleaseSessonConnLock();
211             return SOFTBUS_OK;
212         }
213     }
214     ReleaseSessonConnLock();
215     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "can not get srv session conn info.");
216     return SOFTBUS_ERR;
217 }
218 
SetAuthIdByChanId(int32_t channelId,int64_t authId)219 NO_SANITIZE("cfi") int32_t SetAuthIdByChanId(int32_t channelId, int64_t authId)
220 {
221     SessionConn *conn = NULL;
222     if (GetSessionConnLock() != SOFTBUS_OK) {
223         return SOFTBUS_LOCK_ERR;
224     }
225     LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
226         if (conn->channelId == channelId) {
227             conn->authId = authId;
228             ReleaseSessonConnLock();
229             return SOFTBUS_OK;
230         }
231     }
232     ReleaseSessonConnLock();
233     return SOFTBUS_ERR;
234 }
235 
GetAuthIdByChanId(int32_t channelId)236 NO_SANITIZE("cfi") int64_t GetAuthIdByChanId(int32_t channelId)
237 {
238     int64_t authId;
239     SessionConn *conn = NULL;
240     if (GetSessionConnLock() != SOFTBUS_OK) {
241         return AUTH_INVALID_ID;
242     }
243     LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
244         if (conn->channelId == channelId) {
245             authId = conn->authId;
246             ReleaseSessonConnLock();
247             return authId;
248         }
249     }
250     ReleaseSessonConnLock();
251     return AUTH_INVALID_ID;
252 }
253 
TransDelSessionConnById(int32_t channelId)254 NO_SANITIZE("cfi") void TransDelSessionConnById(int32_t channelId)
255 {
256     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransDelSessionConnById: channelId=%d", channelId);
257     SessionConn *item = NULL;
258     SessionConn *next = NULL;
259     if (GetSessionConnLock() != SOFTBUS_OK) {
260         return;
261     }
262     LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_sessionConnList->list, SessionConn, node) {
263         if (item->channelId == channelId) {
264             if (item->listenMod == DIRECT_CHANNEL_SERVER_P2P && item->authId != AUTH_INVALID_ID &&
265                 !item->serverSide && item->appInfo.routeType != WIFI_P2P_REUSE && item->requestId != REQUEST_INVALID) {
266                 AuthCloseConn(item->authId);
267             }
268             ListDelete(&item->node);
269             SoftBusFree(item);
270             g_sessionConnList->cnt--;
271             ReleaseSessonConnLock();
272             return;
273         }
274     }
275     ReleaseSessonConnLock();
276 }
277 
TransTdcAddSessionConn(SessionConn * conn)278 NO_SANITIZE("cfi") int32_t TransTdcAddSessionConn(SessionConn *conn)
279 {
280     if (conn == NULL) {
281         return SOFTBUS_INVALID_PARAM;
282     }
283     if (GetSessionConnLock() != SOFTBUS_OK) {
284         return SOFTBUS_LOCK_ERR;
285     }
286     ListInit(&conn->node);
287     ListTailInsert(&g_sessionConnList->list, &conn->node);
288     g_sessionConnList->cnt++;
289     ReleaseSessonConnLock();
290     return SOFTBUS_OK;
291 }
292 
SetSessionKeyByChanId(int32_t chanId,const char * sessionKey,int32_t keyLen)293 NO_SANITIZE("cfi") void SetSessionKeyByChanId(int32_t chanId, const char *sessionKey, int32_t keyLen)
294 {
295     if (sessionKey == NULL || keyLen <= 0) {
296         return;
297     }
298     bool isFind = false;
299     SessionConn *conn = NULL;
300     if (GetSessionConnLock() != SOFTBUS_OK) {
301         return;
302     }
303     LIST_FOR_EACH_ENTRY(conn, &g_sessionConnList->list, SessionConn, node) {
304         if (conn->channelId == chanId) {
305             isFind = true;
306             break;
307         }
308     }
309     if (isFind && conn != NULL) {
310         if (memcpy_s(conn->appInfo.sessionKey, sizeof(conn->appInfo.sessionKey), sessionKey, keyLen) != EOK) {
311             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SetSessionKeyByChanId memcpy fail");
312             ReleaseSessonConnLock();
313             return;
314         }
315     }
316     ReleaseSessonConnLock();
317 }
318 
SetSessionConnStatusById(int32_t channelId,uint32_t status)319 NO_SANITIZE("cfi") int32_t SetSessionConnStatusById(int32_t channelId, uint32_t status)
320 {
321     if (GetSessionConnLock() != SOFTBUS_OK) {
322         return SOFTBUS_LOCK_ERR;
323     }
324     SessionConn *connInfo = NULL;
325     LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
326         if (connInfo->channelId == channelId) {
327             connInfo->status = status;
328             ReleaseSessonConnLock();
329             return SOFTBUS_OK;
330         }
331     }
332     ReleaseSessonConnLock();
333     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SetSessionConnStatusById not find: channelId=%d", channelId);
334     return SOFTBUS_NOT_FIND;
335 }
336 
TcpTranGetAppInfobyChannelId(int32_t channelId,AppInfo * appInfo)337 NO_SANITIZE("cfi") int32_t TcpTranGetAppInfobyChannelId(int32_t channelId, AppInfo* appInfo)
338 {
339     if (GetSessionConnLock() != SOFTBUS_OK) {
340         return SOFTBUS_LOCK_ERR;
341     }
342     SessionConn *connInfo = NULL;
343     LIST_FOR_EACH_ENTRY(connInfo, &g_sessionConnList->list, SessionConn, node) {
344         if (connInfo->channelId == channelId) {
345             memcpy_s(appInfo, sizeof(AppInfo), &connInfo->appInfo, sizeof(AppInfo));
346             ReleaseSessonConnLock();
347             return SOFTBUS_OK;
348         }
349     }
350     ReleaseSessonConnLock();
351     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TcpTranGetAppInfobyChannelId not find: channelId=%d", channelId);
352     return SOFTBUS_NOT_FIND;
353 }