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     SessionRole role;
82     uint32_t maxIdleTime;
83     uint32_t timeout;
84     SessionEnableStatus enableStatus;
85     int32_t peerUid;
86     int32_t peerPid;
87     bool isEncrypt;
88     int32_t routeType;
89     int32_t businessType;
90     int32_t fileEncrypt;
91     int32_t algorithm;
92     int32_t crc;
93     LinkType linkType[LINK_TYPE_MAX];
94     uint32_t dataConfig;
95     bool isEncyptedRawStream;
96     bool isAsync;
97     SocketLifecycleData lifecycle;
98     uint32_t actionId;
99     int32_t osType;
100 } SessionInfo;
101 
102 typedef struct {
103     bool isSocketListener;
104     ISessionListener session;
105     ISocketListener socketClient;
106     ISocketListener socketServer;
107 } SessionListenerAdapter;
108 
109 typedef struct {
110     ListNode node;
111     SoftBusSecType type;
112     char sessionName[SESSION_NAME_SIZE_MAX];
113     char pkgName[PKG_NAME_SIZE_MAX];
114     SessionListenerAdapter listener;
115     ListNode sessionList;
116     bool permissionState;
117     bool isSrvEncryptedRawStream;
118     int32_t sessionAddingCnt;
119 } ClientSessionServer;
120 
121 typedef enum {
122     KEY_SESSION_NAME = 1,
123     KEY_PEER_SESSION_NAME,
124     KEY_PEER_DEVICE_ID,
125     KEY_IS_SERVER,
126     KEY_PEER_PID,
127     KEY_PEER_UID,
128     KEY_PKG_NAME,
129     KEY_ACTION_ID,
130 } SessionKey;
131 
132 typedef struct {
133     ListNode node;
134     char pkgName[PKG_NAME_SIZE_MAX];
135     char sessionName[SESSION_NAME_SIZE_MAX];
136 } SessionServerInfo;
137 
138 typedef enum {
139     TIMER_ACTION_START,
140     TIMER_ACTION_STOP,
141     TIMER_ACTION_BUTT
142 } TimerAction;
143 
144 typedef struct {
145     ListNode node;
146     int32_t sessionId;
147     int32_t channelId;
148     ChannelType channelType;
149     bool isAsync;
150     void (*OnSessionClosed)(int sessionId);
151     void (*OnShutdown)(int32_t socket, ShutdownReason reason);
152     char sessionName[SESSION_NAME_SIZE_MAX];
153     char pkgName[PKG_NAME_SIZE_MAX];
154     SocketLifecycleData lifecycle;
155 } DestroySessionInfo;
156 
157 int32_t ClientAddNewSession(const char *sessionName, SessionInfo *session);
158 
159 /**
160  * @brief Add session.
161  * @return  if session already added, return SOFTBUS_TRANS_SESSION_REPEATED, else return SOFTBUS_OK or SOFTBUS_ERR.
162  */
163 int32_t ClientAddSession(const SessionParam *param, int32_t *sessionId, SessionEnableStatus *isEnabled);
164 
165 int32_t ClientAddAuthSession(const char *sessionName, int32_t *sessionId);
166 
167 int32_t ClientDeleteSessionServer(SoftBusSecType type, const char *sessionName);
168 
169 int32_t ClientDeleteSession(int32_t sessionId);
170 
171 int32_t ClientGetSessionDataById(int32_t sessionId, char *data, uint16_t len, SessionKey key);
172 
173 int32_t ClientGetSessionIntegerDataById(int32_t sessionId, int *data, SessionKey key);
174 
175 int32_t ClientGetChannelBySessionId(
176     int32_t sessionId, int32_t *channelId, int32_t *type, SessionEnableStatus *enableStatus);
177 
178 int32_t ClientSetChannelBySessionId(int32_t sessionId, TransInfo *transInfo);
179 
180 int32_t ClientGetChannelBusinessTypeBySessionId(int32_t sessionId, int32_t *businessType);
181 
182 int32_t ClientGetSessionStateByChannelId(int32_t channelId, int32_t channelType, SessionState *sessionState);
183 
184 int32_t GetEncryptByChannelId(int32_t channelId, int32_t channelType, int32_t *data);
185 
186 int32_t ClientGetSessionStateByChannelId(int32_t channelId, int32_t channelType, SessionState *sessionState);
187 
188 int32_t ClientGetSessionIdByChannelId(int32_t channelId, int32_t channelType, int32_t *sessionId);
189 
190 int32_t ClientGetSessionIsAsyncBySessionId(int32_t sessionId, bool *isAsync);
191 
192 int32_t ClientGetRouteTypeByChannelId(int32_t channelId, int32_t channelType, int32_t *routeType);
193 
194 int32_t ClientGetDataConfigByChannelId(int32_t channelId, int32_t channelType, uint32_t *dataConfig);
195 
196 int32_t ClientEnableSessionByChannelId(const ChannelInfo *channel, int32_t *sessionId);
197 
198 int32_t ClientGetSessionCallbackById(int32_t sessionId, ISessionListener *callback);
199 
200 int32_t ClientGetSessionCallbackByName(const char *sessionName, ISessionListener *callback);
201 
202 int32_t ClientAddSessionServer(SoftBusSecType type, const char *pkgName, const char *sessionName,
203     const ISessionListener *listener);
204 
205 int32_t ClientGetSessionSide(int32_t sessionId);
206 
207 int32_t ClientGetFileConfigInfoById(int32_t sessionId, int32_t *fileEncrypt, int32_t *algorithm, int32_t *crc);
208 
209 int TransClientInit(void);
210 void TransClientDeinit(void);
211 
212 void ClientTransRegLnnOffline(void);
213 
214 void ClientTransOnLinkDown(const char *networkId, int32_t routeType);
215 
216 void ClientCleanAllSessionWhenServerDeath(ListNode *sessionServerInfoList);
217 
218 int32_t CheckPermissionState(int32_t sessionId);
219 
220 void PermissionStateChange(const char *pkgName, int32_t state);
221 
222 int32_t ClientAddSocketServer(SoftBusSecType type, const char *pkgName, const char *sessionName);
223 
224 int32_t ClientAddSocketSession(
225     const SessionParam *param, bool isEncyptedRawStream, int32_t *sessionId, SessionEnableStatus *isEnabled);
226 
227 int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *listener, bool isServer);
228 
229 int32_t ClientIpcOpenSession(
230     int32_t sessionId, const QosTV *qos, uint32_t qosCount, TransInfo *transInfo, bool isAsync);
231 
232 int32_t ClientSetActionIdBySessionId(int32_t sessionId, uint32_t actionId);
233 
234 int32_t ClientSetSocketState(int32_t socket, uint32_t maxIdleTimeout, SessionRole role);
235 
236 int32_t ClientGetSessionCallbackAdapterByName(const char *sessionName, SessionListenerAdapter *callbackAdapter);
237 
238 int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAdapter *callbackAdapter, bool *isServer);
239 
240 int32_t ClientGetPeerSocketInfoById(int32_t sessionId, PeerSocketInfo *peerSocketInfo);
241 
242 bool IsSessionExceedLimit(void);
243 
244 int32_t ClientResetIdleTimeoutById(int32_t sessionId);
245 
246 int32_t ClientGetSessionNameByChannelId(int32_t channelId, int32_t channelType, char *sessionName, int32_t len);
247 
248 int32_t ClientRawStreamEncryptDefOptGet(const char *sessionName, bool *isEncrypt);
249 
250 int32_t ClientRawStreamEncryptOptGet(int32_t channelId, int32_t channelType, bool *isEncrypt);
251 
252 int32_t SetSessionIsAsyncById(int32_t sessionId, bool isAsync);
253 
254 int32_t ClientTransSetChannelInfo(const char *sessionName, int32_t sessionId, int32_t channelId, int32_t channelType);
255 
256 void DelSessionStateClosing(void);
257 
258 void AddSessionStateClosing(void);
259 
260 int32_t ClientHandleBindWaitTimer(int32_t socket, uint32_t maxWaitTime, TimerAction action);
261 
IsValidQosInfo(const QosTV qos[],uint32_t qosCount)262 inline bool IsValidQosInfo(const QosTV qos[], uint32_t qosCount)
263 {
264     return (qos == NULL) ? (qosCount == 0) : (qosCount <= QOS_TYPE_BUTT);
265 }
266 
267 int32_t SetSessionInitInfoById(int32_t sessionId);
268 
269 int32_t ClientSetEnableStatusBySocket(int32_t socket, SessionEnableStatus enableStatus);
270 
271 int32_t TryDeleteEmptySessionServer(const char *pkgName, const char *sessionName);
272 
273 int32_t DeleteSocketSession(int32_t sessionId, char *pkgName, char *sessionName);
274 
275 int32_t SetSessionStateBySessionId(int32_t sessionId, SessionState sessionState, int32_t optional);
276 
277 int32_t GetSocketLifecycleAndSessionNameBySessionId(
278     int32_t sessionId, char *sessionName, SocketLifecycleData *lifecycle);
279 
280 int32_t ClientWaitSyncBind(int32_t socket);
281 
282 int32_t ClientSignalSyncBind(int32_t socket, int32_t errCode);
283 
284 int32_t ClientDfsIpcOpenSession(int32_t sessionId, TransInfo *transInfo);
285 
286 void SocketServerStateUpdate(const char *sessionName);
287 
288 int32_t ClientCancelAuthSessionTimer(int32_t sessionId);
289 
290 int32_t ClientGetChannelOsTypeBySessionId(int32_t sessionId, int32_t *osType);
291 #ifdef __cplusplus
292 }
293 #endif
294 #endif // CLIENT_TRANS_SESSION_MANAGER_H
295