1 /*
2 * Copyright (c) 2021-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 #ifndef CLIENT_TRANS_SESSION_MANAGER_H
17 #define CLIENT_TRANS_SESSION_MANAGER_H
18
19 #include "session.h"
20 #include "socket.h"
21 #include "softbus_def.h"
22 #include "softbus_trans_def.h"
23 #include "client_trans_session_adapter.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #define IS_SERVER 0
30 #define IS_CLIENT 1
31 #define ISHARE_AUTH_SESSION "IShareAuthSession"
32 #define ISHARE_AUTH_SESSION_MAX_IDLE_TIME 5000 // 5s
33
34 typedef struct {
35 char peerSessionName[SESSION_NAME_SIZE_MAX];
36 char peerDeviceId[DEVICE_ID_SIZE_MAX];
37 char groupId[GROUP_ID_SIZE_MAX];
38 int flag; // TYPE_MESSAGE & TYPE_BYTES & TYPE_FILE
39 int streamType;
40 } SessionTag;
41
42 typedef enum {
43 SESSION_ROLE_INIT,
44 SESSION_ROLE_CLIENT,
45 SESSION_ROLE_SERVER,
46 SESSION_ROLE_BUTT,
47 } SessionRole;
48
49 typedef enum {
50 SESSION_STATE_INIT,
51 SESSION_STATE_OPENING,
52 SESSION_STATE_OPENED,
53 SESSION_STATE_CALLBACK_FINISHED,
54 SESSION_STATE_CANCELLING,
55 SESSION_STATE_BUTT,
56 } SessionState;
57
58 typedef struct {
59 SessionState sessionState;
60 SoftBusCond callbackCond;
61 bool condIsWaiting;
62 int32_t bindErrCode;
63 uint32_t maxWaitTime; // 0 means no check time out, for Bind end
64 uint32_t waitTime;
65 } SocketLifecycleData;
66
67 typedef enum {
68 ENABLE_STATUS_INIT,
69 ENABLE_STATUS_SUCCESS,
70 ENABLE_STATUS_FAILED,
71 ENABLE_STATUS_BUTT,
72 } SessionEnableStatus;
73
74 typedef struct {
75 ListNode node;
76 int32_t sessionId;
77 int32_t channelId;
78 ChannelType channelType;
79 SessionTag info;
80 bool isServer;
81 bool isEncyptedRawStream;
82 bool isAsync;
83 bool isClosing;
84 SessionRole role;
85 uint32_t maxIdleTime;
86 uint32_t timeout;
87 SessionEnableStatus enableStatus;
88 int32_t peerUid;
89 int32_t peerPid;
90 bool isEncrypt;
91 int32_t routeType;
92 int32_t businessType;
93 int32_t fileEncrypt;
94 int32_t algorithm;
95 int32_t crc;
96 LinkType linkType[LINK_TYPE_MAX];
97 uint32_t dataConfig;
98 SocketLifecycleData lifecycle;
99 uint32_t actionId;
100 int32_t osType;
101 } SessionInfo;
102
103 typedef struct {
104 bool isSocketListener;
105 ISessionListener session;
106 ISocketListener socketClient;
107 ISocketListener socketServer;
108 } SessionListenerAdapter;
109
110 typedef struct {
111 ListNode node;
112 SoftBusSecType type;
113 char sessionName[SESSION_NAME_SIZE_MAX];
114 char pkgName[PKG_NAME_SIZE_MAX];
115 SessionListenerAdapter listener;
116 ListNode sessionList;
117 bool permissionState;
118 bool isSrvEncryptedRawStream;
119 int32_t sessionAddingCnt;
120 } ClientSessionServer;
121
122 typedef enum {
123 KEY_SESSION_NAME = 1,
124 KEY_PEER_SESSION_NAME,
125 KEY_PEER_DEVICE_ID,
126 KEY_IS_SERVER,
127 KEY_PEER_PID,
128 KEY_PEER_UID,
129 KEY_PKG_NAME,
130 KEY_ACTION_ID,
131 } SessionKey;
132
133 typedef enum {
134 TIMER_ACTION_START,
135 TIMER_ACTION_STOP,
136 TIMER_ACTION_BUTT
137 } TimerAction;
138
139 typedef struct {
140 ListNode node;
141 char pkgName[PKG_NAME_SIZE_MAX];
142 char sessionName[SESSION_NAME_SIZE_MAX];
143 } SessionServerInfo;
144
145 typedef struct {
146 ListNode node;
147 int32_t sessionId;
148 int32_t channelId;
149 ChannelType channelType;
150 bool isAsync;
151 void (*OnSessionClosed)(int sessionId);
152 void (*OnShutdown)(int32_t socket, ShutdownReason reason);
153 char sessionName[SESSION_NAME_SIZE_MAX];
154 char pkgName[PKG_NAME_SIZE_MAX];
155 SocketLifecycleData lifecycle;
156 } DestroySessionInfo;
157
158 int32_t ClientAddNewSession(const char *sessionName, SessionInfo *session);
159
160 /**
161 * @brief Add session.
162 * @return if session already added, return SOFTBUS_TRANS_SESSION_REPEATED, else return SOFTBUS_OK or SOFTBUS_ERR.
163 */
164 int32_t ClientAddSession(const SessionParam *param, int32_t *sessionId, SessionEnableStatus *isEnabled);
165
166 int32_t ClientAddAuthSession(const char *sessionName, int32_t *sessionId);
167
168 int32_t ClientDeleteSessionServer(SoftBusSecType type, const char *sessionName);
169
170 int32_t ClientDeleteSession(int32_t sessionId);
171
172 int32_t ClientGetSessionDataById(int32_t sessionId, char *data, uint16_t len, SessionKey key);
173
174 int32_t ClientGetSessionIntegerDataById(int32_t sessionId, int *data, SessionKey key);
175
176 int32_t ClientGetChannelBySessionId(
177 int32_t sessionId, int32_t *channelId, int32_t *type, SessionEnableStatus *enableStatus);
178
179 int32_t ClientSetChannelBySessionId(int32_t sessionId, TransInfo *transInfo);
180
181 int32_t ClientGetChannelBusinessTypeBySessionId(int32_t sessionId, int32_t *businessType);
182
183 int32_t GetEncryptByChannelId(int32_t channelId, int32_t channelType, int32_t *data);
184
185 int32_t ClientGetSessionStateByChannelId(int32_t channelId, int32_t channelType, SessionState *sessionState);
186
187 int32_t ClientGetSessionIdByChannelId(int32_t channelId, int32_t channelType, int32_t *sessionId, bool isClosing);
188
189 int32_t ClientGetSessionIsAsyncBySessionId(int32_t sessionId, bool *isAsync);
190
191 int32_t ClientGetRouteTypeByChannelId(int32_t channelId, int32_t channelType, int32_t *routeType);
192
193 int32_t ClientGetDataConfigByChannelId(int32_t channelId, int32_t channelType, uint32_t *dataConfig);
194
195 int32_t ClientEnableSessionByChannelId(const ChannelInfo *channel, int32_t *sessionId);
196
197 int32_t ClientGetSessionCallbackById(int32_t sessionId, ISessionListener *callback);
198
199 int32_t ClientGetSessionCallbackByName(const char *sessionName, ISessionListener *callback);
200
201 int32_t ClientAddSessionServer(SoftBusSecType type, const char *pkgName, const char *sessionName,
202 const ISessionListener *listener);
203
204 int32_t ClientGetSessionSide(int32_t sessionId);
205
206 int32_t ClientGetFileConfigInfoById(int32_t sessionId, int32_t *fileEncrypt, int32_t *algorithm, int32_t *crc);
207
208 int TransClientInit(void);
209 void TransClientDeinit(void);
210
211 void ClientTransRegLnnOffline(void);
212
213 void ClientTransOnLinkDown(const char *networkId, int32_t routeType);
214
215 void ClientCleanAllSessionWhenServerDeath(ListNode *sessionServerInfoList);
216
217 int32_t CheckPermissionState(int32_t sessionId);
218
219 void PermissionStateChange(const char *pkgName, int32_t state);
220
221 int32_t ClientAddSocketServer(SoftBusSecType type, const char *pkgName, const char *sessionName);
222
223 int32_t ClientAddSocketSession(
224 const SessionParam *param, bool isEncyptedRawStream, int32_t *sessionId, SessionEnableStatus *isEnabled);
225
226 int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *listener, bool isServer);
227
228 int32_t ClientIpcOpenSession(
229 int32_t sessionId, const QosTV *qos, uint32_t qosCount, TransInfo *transInfo, bool isAsync);
230
231 int32_t ClientSetActionIdBySessionId(int32_t sessionId, uint32_t actionId);
232
233 int32_t ClientSetSocketState(int32_t socket, uint32_t maxIdleTimeout, SessionRole role);
234
235 int32_t ClientGetSessionCallbackAdapterByName(const char *sessionName, SessionListenerAdapter *callbackAdapter);
236
237 int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAdapter *callbackAdapter, bool *isServer);
238
239 int32_t ClientGetPeerSocketInfoById(int32_t sessionId, PeerSocketInfo *peerSocketInfo);
240
241 bool IsSessionExceedLimit(void);
242
243 int32_t ClientResetIdleTimeoutById(int32_t sessionId);
244
245 int32_t ClientGetSessionNameByChannelId(int32_t channelId, int32_t channelType, char *sessionName, int32_t len);
246
247 int32_t ClientRawStreamEncryptDefOptGet(const char *sessionName, bool *isEncrypt);
248
249 int32_t ClientRawStreamEncryptOptGet(int32_t channelId, int32_t channelType, bool *isEncrypt);
250
251 int32_t SetSessionIsAsyncById(int32_t sessionId, bool isAsync);
252
253 int32_t ClientTransSetChannelInfo(const char *sessionName, int32_t sessionId, int32_t channelId, int32_t channelType);
254
255 void DelSessionStateClosing(void);
256
257 int32_t ClientHandleBindWaitTimer(int32_t socket, uint32_t maxWaitTime, TimerAction action);
258
IsValidQosInfo(const QosTV qos[], uint32_t qosCount)259 inline bool IsValidQosInfo(const QosTV qos[], uint32_t qosCount)
260 {
261 return (qos == NULL) ? (qosCount == 0) : (qosCount <= QOS_TYPE_BUTT);
262 }
263
264 void AddSessionStateClosing(void);
265
266 int32_t SetSessionInitInfoById(int32_t sessionId);
267
268 int32_t ClientSetEnableStatusBySocket(int32_t socket, SessionEnableStatus enableStatus);
269
270 int32_t TryDeleteEmptySessionServer(const char *pkgName, const char *sessionName);
271
272 int32_t DeleteSocketSession(int32_t sessionId, char *pkgName, char *sessionName);
273
274 int32_t SetSessionStateBySessionId(int32_t sessionId, SessionState sessionState, int32_t optional);
275
276 int32_t GetSocketLifecycleAndSessionNameBySessionId(
277 int32_t sessionId, char *sessionName, SocketLifecycleData *lifecycle);
278
279 int32_t ClientWaitSyncBind(int32_t socket);
280
281 int32_t ClientSignalSyncBind(int32_t socket, int32_t errCode);
282
283 int32_t ClientDfsIpcOpenSession(int32_t sessionId, TransInfo *transInfo);
284
285 void SocketServerStateUpdate(const char *sessionName);
286
287 int32_t ClientCancelAuthSessionTimer(int32_t sessionId);
288
289 int32_t ClientSetStatusClosingBySocket(int32_t socket, bool isClosing);
290
291 int32_t ClientGetChannelOsTypeBySessionId(int32_t sessionId, int32_t *osType);
292 #ifdef __cplusplus
293 }
294 #endif
295 #endif // CLIENT_TRANS_SESSION_MANAGER_H
296