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 #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 32 typedef struct { 33 char peerSessionName[SESSION_NAME_SIZE_MAX]; 34 char peerDeviceId[DEVICE_ID_SIZE_MAX]; 35 char groupId[GROUP_ID_SIZE_MAX]; 36 int flag; // TYPE_MESSAGE & TYPE_BYTES & TYPE_FILE 37 int streamType; 38 } SessionTag; 39 40 typedef enum { 41 SESSION_ROLE_INIT, 42 SESSION_ROLE_CLIENT, 43 SESSION_ROLE_SERVER, 44 SESSION_ROLE_BUTT, 45 } SessionRole; 46 47 typedef struct { 48 ListNode node; 49 int32_t sessionId; 50 int32_t channelId; 51 ChannelType channelType; 52 SessionTag info; 53 bool isServer; 54 SessionRole role; 55 uint32_t maxIdleTime; 56 uint32_t timeout; 57 bool isEnable; 58 int32_t peerUid; 59 int32_t peerPid; 60 bool isEncrypt; 61 int32_t routeType; 62 int32_t businessType; 63 int32_t fileEncrypt; 64 int32_t algorithm; 65 int32_t crc; 66 LinkType linkType[LINK_TYPE_MAX]; 67 uint32_t dataConfig; 68 } SessionInfo; 69 70 typedef struct { 71 bool isSocketListener; 72 ISessionListener session; 73 ISocketListener socketClient; 74 ISocketListener socketServer; 75 } SessionListenerAdapter; 76 77 typedef struct { 78 ListNode node; 79 SoftBusSecType type; 80 char sessionName[SESSION_NAME_SIZE_MAX]; 81 char pkgName[PKG_NAME_SIZE_MAX]; 82 SessionListenerAdapter listener; 83 ListNode sessionList; 84 bool permissionState; 85 } ClientSessionServer; 86 87 typedef enum { 88 KEY_SESSION_NAME = 1, 89 KEY_PEER_SESSION_NAME, 90 KEY_PEER_DEVICE_ID, 91 KEY_IS_SERVER, 92 KEY_PEER_PID, 93 KEY_PEER_UID, 94 KEY_PKG_NAME, 95 } SessionKey; 96 97 int32_t ClientAddNewSession(const char* sessionName, SessionInfo* session); 98 99 /** 100 * @brief Add session. 101 * @return if session already added, return SOFTBUS_TRANS_SESSION_REPEATED, else return SOFTBUS_OK or SOFTBUS_ERR. 102 */ 103 int32_t ClientAddSession(const SessionParam *param, int32_t *sessionId, bool *isEnabled); 104 105 int32_t ClientAddAuthSession(const char *sessionName, int32_t *sessionId); 106 107 int32_t ClientDeleteSessionServer(SoftBusSecType type, const char *sessionName); 108 109 int32_t ClientDeleteSession(int32_t sessionId); 110 111 int32_t ClientGetSessionDataById(int32_t sessionId, char *data, uint16_t len, SessionKey key); 112 113 int32_t ClientGetSessionIntegerDataById(int32_t sessionId, int *data, SessionKey key); 114 115 int32_t ClientGetChannelBySessionId(int32_t sessionId, int32_t *channelId, int32_t *type, bool *isEnable); 116 117 int32_t ClientSetChannelBySessionId(int32_t sessionId, TransInfo *transInfo); 118 119 int32_t ClientGetChannelBusinessTypeBySessionId(int32_t sessionId, int32_t *businessType); 120 121 int32_t GetEncryptByChannelId(int32_t channelId, int32_t channelType, int32_t *data); 122 123 int32_t ClientGetSessionIdByChannelId(int32_t channelId, int32_t channelType, int32_t *sessionId); 124 125 int32_t ClientGetRouteTypeByChannelId(int32_t channelId, int32_t channelType, int32_t *routeType); 126 127 int32_t ClientGetDataConfigByChannelId(int32_t channelId, int32_t channelType, uint32_t *dataConfig); 128 129 int32_t ClientEnableSessionByChannelId(const ChannelInfo *channel, int32_t *sessionId); 130 131 int32_t ClientGetSessionCallbackById(int32_t sessionId, ISessionListener *callback); 132 133 int32_t ClientGetSessionCallbackByName(const char *sessionName, ISessionListener *callback); 134 135 int32_t ClientAddSessionServer(SoftBusSecType type, const char *pkgName, const char *sessionName, 136 const ISessionListener *listener); 137 138 int32_t ClientGetSessionSide(int32_t sessionId); 139 140 int32_t ClientGrantPermission(int uid, int pid, const char *busName); 141 142 int32_t ClientRemovePermission(const char *busName); 143 144 int32_t ClientGetFileConfigInfoById(int32_t sessionId, int32_t *fileEncrypt, int32_t *algorithm, int32_t *crc); 145 146 int TransClientInit(void); 147 void TransClientDeinit(void); 148 149 int32_t ReCreateSessionServerToServer(void); 150 void ClientTransRegLnnOffline(void); 151 152 void ClientTransOnLinkDown(const char *networkId, int32_t routeType); 153 154 void ClientCleanAllSessionWhenServerDeath(void); 155 156 int32_t CheckPermissionState(int32_t sessionId); 157 158 void PermissionStateChange(const char *pkgName, int32_t state); 159 160 int32_t ClientAddSocketServer(SoftBusSecType type, const char *pkgName, const char *sessionName); 161 162 int32_t ClientDeleteSocketSession(int32_t sessionId); 163 164 int32_t ClientAddSocketSession(const SessionParam *param, int32_t *sessionId, bool *isEnabled); 165 166 int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *listener, bool isServer); 167 168 int32_t ClientIpcOpenSession(int32_t sessionId, const QosTV *qos, uint32_t qosCount, TransInfo *transInfo); 169 170 int32_t ClientSetSocketState(int32_t socket, uint32_t maxIdleTimeout, SessionRole role); 171 172 int32_t ClientGetSessionCallbackAdapterByName(const char *sessionName, SessionListenerAdapter *callbackAdapter); 173 174 int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAdapter *callbackAdapter, bool *isServer); 175 176 int32_t ClientGetPeerSocketInfoById(int32_t sessionId, PeerSocketInfo *peerSocketInfo); 177 178 bool IsSessionExceedLimit(); 179 180 int32_t ClientResetIdleTimeoutById(int32_t sessionId); 181 182 int32_t ClientGetSessionNameByChannelId(int32_t channelId, int32_t channelType, char *sessionName, int32_t len); 183 #ifdef __cplusplus 184 } 185 #endif 186 #endif // CLIENT_TRANS_SESSION_MANAGER_H 187