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 "softbus_def.h"
20 #include "softbus_trans_def.h"
21 #include "client_trans_session_adapter.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 #define IS_SERVER 0
28 #define IS_CLIENT 1
29 #define ISHARE_AUTH_SESSION "IShareAuthSession"
30 #define DM_AUTH_SESSION "ohos.distributedhardware.devicemanager.resident"
31 #define ISHARE_AUTH_SESSION_MAX_IDLE_TIME 5000 // 5s
32 #define DM_AUTH_SESSION_MAX_IDLE_TIME 275000 // 275s
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 QoSEvent event;
76 QosTV qos[QOS_TYPE_BUTT];
77 uint32_t count;
78 } CachedQosEvent;
79
80 typedef struct {
81 ListNode node;
82 int32_t sessionId;
83 int32_t channelId;
84 ChannelType channelType;
85 SessionTag info;
86 bool isServer;
87 bool isEncyptedRawStream;
88 bool isAsync;
89 bool isClosing;
90 SessionRole role;
91 uint32_t maxIdleTime;
92 uint32_t timeout;
93 SessionEnableStatus enableStatus;
94 int32_t peerUid;
95 int32_t peerPid;
96 bool isEncrypt;
97 int32_t routeType;
98 int32_t businessType;
99 int32_t fileEncrypt;
100 int32_t algorithm;
101 int32_t crc;
102 LinkType linkType[LINK_TYPE_MAX];
103 uint32_t dataConfig;
104 SocketLifecycleData lifecycle;
105 uint32_t actionId;
106 int32_t osType;
107 CachedQosEvent cachedQosEvent;
108 bool isSupportTlv;
109 bool needAck;
110 } SessionInfo;
111
112 typedef struct {
113 bool isSocketListener;
114 ISessionListener session;
115 ISocketListener socketClient;
116 ISocketListener socketServer;
117 } SessionListenerAdapter;
118
119 typedef struct {
120 ListNode node;
121 SoftBusSecType type;
122 char sessionName[SESSION_NAME_SIZE_MAX];
123 char pkgName[PKG_NAME_SIZE_MAX];
124 SessionListenerAdapter listener;
125 ListNode sessionList;
126 bool permissionState;
127 bool isSrvEncryptedRawStream;
128 int32_t sessionAddingCnt;
129 } ClientSessionServer;
130
131 typedef enum {
132 KEY_SESSION_NAME = 1,
133 KEY_PEER_SESSION_NAME,
134 KEY_PEER_DEVICE_ID,
135 KEY_IS_SERVER,
136 KEY_PEER_PID,
137 KEY_PEER_UID,
138 KEY_PKG_NAME,
139 KEY_ACTION_ID,
140 } TransSessionKey;
141
142 typedef struct {
143 ListNode node;
144 char pkgName[PKG_NAME_SIZE_MAX];
145 char sessionName[SESSION_NAME_SIZE_MAX];
146 } SessionServerInfo;
147
148 typedef enum {
149 TIMER_ACTION_START,
150 TIMER_ACTION_STOP,
151 TIMER_ACTION_BUTT
152 } TimerAction;
153
154 typedef struct {
155 ListNode node;
156 int32_t sessionId;
157 int32_t channelId;
158 ChannelType channelType;
159 bool isAsync;
160 void (*OnSessionClosed)(int sessionId);
161 void (*OnShutdown)(int32_t socket, ShutdownReason reason);
162 char sessionName[SESSION_NAME_SIZE_MAX];
163 char pkgName[PKG_NAME_SIZE_MAX];
164 SocketLifecycleData lifecycle;
165 } DestroySessionInfo;
166
167 int32_t ClientAddNewSession(const char *sessionName, SessionInfo *session);
168
169 /**
170 * @brief Add session.
171 * @return if the operation is successful, return SOFTBUS_OK.
172 * @return if session already added, return SOFTBUS_TRANS_SESSION_REPEATED.
173 * @return return other error codes.
174 */
175 int32_t ClientAddSession(const SessionParam *param, int32_t *sessionId, SessionEnableStatus *isEnabled);
176
177 int32_t ClientAddAuthSession(const char *sessionName, int32_t *sessionId);
178
179 int32_t ClientDeleteSessionServer(SoftBusSecType type, const char *sessionName);
180
181 int32_t ClientDeleteSession(int32_t sessionId);
182
183 int32_t ClientGetSessionDataById(int32_t sessionId, char *data, uint16_t len, TransSessionKey key);
184
185 int32_t ClientGetSessionIntegerDataById(int32_t sessionId, int *data, TransSessionKey key);
186
187 int32_t ClientGetChannelBySessionId(
188 int32_t sessionId, int32_t *channelId, int32_t *type, SessionEnableStatus *enableStatus);
189
190 int32_t ClientSetChannelBySessionId(int32_t sessionId, TransInfo *transInfo);
191
192 int32_t ClientGetChannelBusinessTypeBySessionId(int32_t sessionId, int32_t *businessType);
193
194 int32_t GetEncryptByChannelId(int32_t channelId, int32_t channelType, int32_t *data);
195
196 int32_t GetSupportTlvAndNeedAckById(int32_t channelId, int32_t channelType, bool *supportTlv, bool *needAck);
197
198 int32_t ClientGetSessionStateByChannelId(int32_t channelId, int32_t channelType, SessionState *sessionState);
199
200 int32_t ClientGetSessionIdByChannelId(int32_t channelId, int32_t channelType, int32_t *sessionId, bool isClosing);
201
202 int32_t ClientGetSessionIsAsyncBySessionId(int32_t sessionId, bool *isAsync);
203
204 int32_t ClientGetRouteTypeByChannelId(int32_t channelId, int32_t channelType, int32_t *routeType);
205
206 int32_t ClientGetDataConfigByChannelId(int32_t channelId, int32_t channelType, uint32_t *dataConfig);
207
208 int32_t ClientEnableSessionByChannelId(const ChannelInfo *channel, int32_t *sessionId);
209
210 int32_t ClientGetSessionCallbackById(int32_t sessionId, ISessionListener *callback);
211
212 int32_t ClientGetSessionCallbackByName(const char *sessionName, ISessionListener *callback);
213
214 int32_t ClientAddSessionServer(SoftBusSecType type, const char *pkgName, const char *sessionName,
215 const ISessionListener *listener);
216
217 int32_t ClientGetSessionSide(int32_t sessionId);
218
219 int32_t ClientGetFileConfigInfoById(int32_t sessionId, int32_t *fileEncrypt, int32_t *algorithm, int32_t *crc);
220
221 int TransClientInit(void);
222 void TransClientDeinit(void);
223
224 void ClientTransRegLnnOffline(void);
225
226 void ClientTransOnUserSwitch(void);
227
228 void ClientTransOnLinkDown(const char *networkId, int32_t routeType);
229
230 void ClientCleanAllSessionWhenServerDeath(ListNode *sessionServerInfoList);
231
232 int32_t CheckPermissionState(int32_t sessionId);
233
234 void PermissionStateChange(const char *pkgName, int32_t state);
235
236 int32_t ClientAddSocketServer(SoftBusSecType type, const char *pkgName, const char *sessionName);
237
238 int32_t ClientAddSocketSession(
239 const SessionParam *param, bool isEncyptedRawStream, int32_t *sessionId, SessionEnableStatus *isEnabled);
240
241 int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *listener, bool isServer);
242
243 int32_t ClientIpcOpenSession(
244 int32_t sessionId, const QosTV *qos, uint32_t qosCount, TransInfo *transInfo, bool isAsync);
245
246 int32_t ClientSetActionIdBySessionId(int32_t sessionId, uint32_t actionId);
247
248 int32_t ClientSetSocketState(int32_t socket, uint32_t maxIdleTimeout, SessionRole role);
249
250 int32_t ClientGetSessionCallbackAdapterByName(const char *sessionName, SessionListenerAdapter *callbackAdapter);
251
252 int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAdapter *callbackAdapter, bool *isServer);
253
254 int32_t ClientGetPeerSocketInfoById(int32_t sessionId, PeerSocketInfo *peerSocketInfo);
255
256 bool IsSessionExceedLimit(void);
257
258 int32_t ClientResetIdleTimeoutById(int32_t sessionId);
259
260 int32_t ClientGetSessionNameByChannelId(int32_t channelId, int32_t channelType, char *sessionName, int32_t len);
261
262 int32_t ClientRawStreamEncryptDefOptGet(const char *sessionName, bool *isEncrypt);
263
264 int32_t ClientRawStreamEncryptOptGet(int32_t channelId, int32_t channelType, bool *isEncrypt);
265
266 int32_t SetSessionIsAsyncById(int32_t sessionId, bool isAsync);
267
268 int32_t ClientTransSetChannelInfo(const char *sessionName, int32_t sessionId, int32_t channelId, int32_t channelType);
269
270 void DelSessionStateClosing(void);
271
272 void AddSessionStateClosing(void);
273
274 int32_t ClientHandleBindWaitTimer(int32_t socket, uint32_t maxWaitTime, TimerAction action);
275
IsValidQosInfo(const QosTV qos[],uint32_t qosCount)276 inline bool IsValidQosInfo(const QosTV qos[], uint32_t qosCount)
277 {
278 return (qos == NULL) ? (qosCount == 0) : (qosCount <= QOS_TYPE_BUTT);
279 }
280
281 int32_t SetSessionInitInfoById(int32_t sessionId);
282
283 int32_t ClientSetEnableStatusBySocket(int32_t socket, SessionEnableStatus enableStatus);
284
285 int32_t TryDeleteEmptySessionServer(const char *pkgName, const char *sessionName);
286
287 int32_t DeleteSocketSession(int32_t sessionId, char *pkgName, char *sessionName);
288
289 int32_t SetSessionStateBySessionId(int32_t sessionId, SessionState sessionState, int32_t optional);
290
291 int32_t GetSocketLifecycleAndSessionNameBySessionId(
292 int32_t sessionId, char *sessionName, SocketLifecycleData *lifecycle);
293
294 int32_t ClientWaitSyncBind(int32_t socket);
295
296 int32_t ClientSignalSyncBind(int32_t socket, int32_t errCode);
297
298 int32_t ClientDfsIpcOpenSession(int32_t sessionId, TransInfo *transInfo);
299
300 void SocketServerStateUpdate(const char *sessionName);
301
302 int32_t ClientCancelAuthSessionTimer(int32_t sessionId);
303
304 int32_t ClientSetStatusClosingBySocket(int32_t socket, bool isClosing);
305
306 int32_t ClientGetChannelOsTypeBySessionId(int32_t sessionId, int32_t *osType);
307
308 int32_t ClientCacheQosEvent(int32_t socket, QoSEvent event, const QosTV *qos, uint32_t count);
309
310 int32_t ClientGetCachedQosEventBySocket(int32_t socket, CachedQosEvent *cachedQosEvent);
311
312 int32_t GetMaxIdleTimeBySocket(int32_t socket, uint32_t *maxIdleTime);
313
314 int32_t SetMaxIdleTimeBySocket(int32_t socket, uint32_t maxIdleTime);
315
316 void ClientTransOnPrivilegeClose(const char *peerNetworkId);
317
318 int32_t TransGetSupportTlvBySocket(int32_t socket, bool *supportTlv, int32_t *optValueSize);
319
320 int32_t TransSetNeedAckBySocket(int32_t socket, bool needAck);
321
322 bool IsRawAuthSession(const char *sessionName);
323 #ifdef __cplusplus
324 }
325 #endif
326 #endif // CLIENT_TRANS_SESSION_MANAGER_H
327