1 /*
2  * Copyright (c) 2022-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 SOFTBUS_TCP_DIRECT_SESSIONCONN_H
17 #define SOFTBUS_TCP_DIRECT_SESSIONCONN_H
18 
19 #include <stdint.h>
20 #include "auth_interface.h"
21 #include "common_list.h"
22 #include "softbus_app_info.h"
23 #include "softbus_base_listener.h"
24 #include "softbus_def.h"
25 
26 #ifdef __cplusplus
27 #if __cplusplus
28 extern "C" {
29 #endif /* __cplusplus */
30 #endif /* __cplusplus */
31 
32 #define REQUEST_INVALID 0
33 
34 typedef enum {
35     TCP_DIRECT_CHANNEL_STATUS_INIT,
36     TCP_DIRECT_CHANNEL_STATUS_AUTH_CHANNEL,
37     TCP_DIRECT_CHANNEL_STATUS_VERIFY_P2P,
38     TCP_DIRECT_CHANNEL_STATUS_HANDSHAKING,
39     TCP_DIRECT_CHANNEL_STATUS_CONNECTING,
40     TCP_DIRECT_CHANNEL_STATUS_CONNECTED,
41     TCP_DIRECT_CHANNEL_STATUS_TIMEOUT,
42 } TcpDirectChannelStatus;
43 
44 typedef struct {
45     ListNode node;
46     bool serverSide;
47     int32_t channelId;
48     AppInfo appInfo;
49     uint32_t status;
50     uint32_t timeout;
51     int64_t req;
52     uint32_t requestId;
53     AuthHandle authHandle;
54     bool isMeta;
55     ListenerModule listenMod;
56 } SessionConn;
57 
58 typedef struct {
59     ListNode node;
60     int32_t channelId;
61     int32_t businessType;
62     int32_t connectType;
63     char myIp[IP_LEN];
64     char pkgName[PKG_NAME_SIZE_MAX];
65     int32_t pid;
66     bool isServer;
67     int32_t channelType;
68     char peerSessionName[SESSION_NAME_SIZE_MAX];
69     char peerDeviceId[DEVICE_ID_SIZE_MAX];
70     char peerIp[IP_LEN];
71     int64_t timeStart;
72     int32_t linkType;
73 } TcpChannelInfo;
74 
75 uint64_t TransTdcGetNewSeqId(void);
76 
77 int32_t CreatSessionConnList(void);
78 
79 SoftBusList *GetSessionConnList(void);
80 
81 SoftBusList *GetTcpChannelInfoList(void);
82 
83 int32_t GetSessionConnLock(void);
84 
85 int32_t GetTcpChannelInfoLock(void);
86 
87 void ReleaseSessionConnLock(void);
88 
89 void ReleaseTcpChannelInfoLock(void);
90 
91 SessionConn *GetSessionConnByRequestId(uint32_t requestId);
92 
93 SessionConn *GetSessionConnByReq(int64_t req);
94 
95 SessionConn *CreateNewSessinConn(ListenerModule module, bool isServerSid);
96 
97 int32_t GetSessionConnByFd(int32_t fd, SessionConn *conn);
98 
99 int32_t GetSessionConnById(int32_t channelId, SessionConn *conn);
100 
101 int32_t SetAppInfoById(int32_t channelId, const AppInfo *appInfo);
102 int32_t GetAppInfoById(int32_t channelId, AppInfo *appInfo);
103 
104 int32_t SetAuthHandleByChanId(int32_t channelId, AuthHandle *authHandle);
105 int64_t GetAuthIdByChanId(int32_t channelId);
106 int32_t GetAuthHandleByChanId(int32_t channelId, AuthHandle *authHandle);
107 
108 void TransDelSessionConnById(int32_t channelId);
109 
110 int32_t TransTdcAddSessionConn(SessionConn *conn);
111 
112 void SetSessionKeyByChanId(int32_t chanId, const char *sessionKey, int32_t keyLen);
113 
114 int32_t SetSessionConnStatusById(int32_t channelId, uint32_t status);
115 
116 int32_t TcpTranGetAppInfobyChannelId(int32_t channelId, AppInfo* appInfo);
117 
118 int32_t *GetChannelIdsByAuthIdAndStatus(int32_t *num, const AuthHandle *authHandle, uint32_t status);
119 
120 bool IsTdcRecoveryTransLimit(void);
121 
122 int32_t CreateTcpChannelInfoList(void);
123 
124 TcpChannelInfo *CreateTcpChannelInfo(const ChannelInfo *channel);
125 
126 int32_t TransAddTcpChannelInfo(TcpChannelInfo *info);
127 
128 int32_t TransDelTcpChannelInfoByChannelId(int32_t channelId);
129 
130 void TransTdcChannelInfoDeathCallback(const char *pkgName, int32_t pid);
131 
132 int32_t TransTdcGetIpAndConnectTypeById(int32_t channelId, char *localIp, char *remoteIp, uint32_t maxIpLen,
133     int32_t *connectType);
134 
135 int32_t TransGetPidByChanId(int32_t channelId, int32_t channelType, int32_t *pid);
136 
137 #ifdef __cplusplus
138 #if __cplusplus
139 }
140 #endif /* __cplusplus */
141 #endif /* __cplusplus */
142 #endif
143