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 LNN_NET_BUILDER_H
17 #define LNN_NET_BUILDER_H
18 
19 #include <stdint.h>
20 
21 #include "auth_interface.h"
22 #include "lnn_event.h"
23 #include "lnn_sync_info_manager.h"
24 #include "softbus_bus_center.h"
25 #include "message_handler.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 typedef enum {
32     NODE_TYPE_C,
33     NODE_TYPE_L
34 } NodeType;
35 
36 #define JSON_KEY_NODE_CODE "NODE_CODE"
37 #define JSON_KEY_NODE_ADDR "NODE_ADDR"
38 #define JSON_KEY_NODE_PROXY_PORT "PROXY_PORT"
39 #define JSON_KEY_NODE_SESSION_PORT "SESSION_PORT"
40 
41 typedef enum {
42     MSG_TYPE_JOIN_LNN = 0,
43     MSG_TYPE_DISCOVERY_DEVICE,
44     MSG_TYPE_CLEAN_CONN_FSM,
45     MSG_TYPE_VERIFY_RESULT,
46     MSG_TYPE_DEVICE_VERIFY_PASS,
47     MSG_TYPE_DEVICE_DISCONNECT = 5,
48     MSG_TYPE_DEVICE_NOT_TRUSTED,
49     MSG_TYPE_LEAVE_LNN,
50     MSG_TYPE_SYNC_OFFLINE_FINISH,
51     MSG_TYPE_NODE_STATE_CHANGED,
52     MSG_TYPE_MASTER_ELECT = 10,
53     MSG_TYPE_LEAVE_INVALID_CONN,
54     MSG_TYPE_LEAVE_BY_ADDR_TYPE,
55     MSG_TYPE_LEAVE_SPECIFIC,
56     MSG_TYPE_LEAVE_BY_AUTH_ID,
57     MSG_TYPE_BUILD_MAX,
58 } NetBuilderMessageType;
59 
60 typedef struct {
61     int32_t code;
62     char nodeAddr[SHORT_ADDRESS_MAX_LEN];
63     int32_t proxyPort;
64     int32_t sessionPort;
65     int32_t authPort;
66 } LnnNodeAddr;
67 
68 typedef struct {
69     ListNode node;
70     ConnectionAddr addr;
71     char networkId[NETWORK_ID_BUF_LEN];
72     char pkgName[PKG_NAME_SIZE_MAX];
73     int64_t authId;
74     int32_t callingPid;
75     uint32_t requestId;
76     uint32_t flag;
77     bool needReportFailure;
78 } MetaJoinRequestNode;
79 
80 typedef struct {
81     ListNode node;
82     ConnectionAddr addr;
83     bool needReportFailure;
84 } PendingJoinRequestNode;
85 
86 typedef struct {
87     NodeType nodeType;
88 
89     /* connection fsm list */
90     ListNode fsmList;
91     ListNode pendingList;
92     /* connection count */
93     int32_t connCount;
94 
95     SoftBusLooper *looper;
96     SoftBusHandler handler;
97 
98     int32_t maxConnCount;
99     int32_t maxConcurrentCount;
100     bool isInit;
101 } NetBuilder;
102 
103 typedef struct {
104     uint32_t requestId;
105     int32_t retCode;
106     AuthHandle authHandle;
107     NodeInfo *nodeInfo;
108 } VerifyResultMsgPara;
109 
110 typedef struct {
111     ConnectionAddr addr;
112     AuthHandle authHandle;
113     NodeInfo *nodeInfo;
114 } DeviceVerifyPassMsgPara;
115 
116 typedef struct {
117     char networkId[NETWORK_ID_BUF_LEN];
118     char masterUdid[UDID_BUF_LEN];
119     int32_t masterWeight;
120 } ElectMsgPara;
121 
122 typedef struct {
123     char oldNetworkId[NETWORK_ID_BUF_LEN];
124     ConnectionAddrType addrType;
125     char newNetworkId[NETWORK_ID_BUF_LEN];
126 } LeaveInvalidConnMsgPara;
127 
128 typedef struct {
129     char networkId[NETWORK_ID_BUF_LEN];
130     ConnectionAddrType addrType;
131 } SpecificLeaveMsgPara;
132 
133 typedef struct {
134     char pkgName[PKG_NAME_SIZE_MAX];
135     bool isNeedConnect;
136     ConnectionAddr addr;
137     NodeInfo *dupInfo;
138     LnnDfxDeviceInfoReport infoReport;
139 } JoinLnnMsgPara;
140 
141 typedef struct {
142     char pkgName[PKG_NAME_SIZE_MAX];
143     char networkId[NETWORK_ID_BUF_LEN];
144 } LeaveLnnMsgPara;
145 
146 int32_t LnnInitNetBuilder(void);
147 int32_t LnnInitNetBuilderDelay(void);
148 void LnnDeinitNetBuilder(void);
149 
150 int32_t LnnNotifyDiscoveryDevice(
151     const ConnectionAddr *addr, const LnnDfxDeviceInfoReport *infoReport, bool isNeedConnect);
152 void LnnSyncOfflineComplete(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t len);
153 int32_t LnnRequestLeaveByAddrType(const bool *type, uint32_t typeLen);
154 int32_t LnnRequestLeaveSpecific(const char *networkId, ConnectionAddrType addrType);
155 void LnnRequestLeaveAllOnlineNodes(void);
156 int32_t LnnRequestLeaveInvalidConn(const char *oldNetworkId, ConnectionAddrType addrType, const char *newNetworkId);
157 int32_t LnnRequestCleanConnFsm(uint16_t connFsmId);
158 int32_t LnnNotifyNodeStateChanged(const ConnectionAddr *addr);
159 int32_t LnnNotifyMasterElect(const char *networkId, const char *masterUdid, int32_t masterWeight);
160 int32_t LnnNotifyAuthHandleLeaveLNN(AuthHandle authHandle);
161 int32_t LnnNotifyEmptySessionKey(int64_t authId);
162 int32_t LnnNotifyLeaveLnnByAuthHandle(AuthHandle *authHandle);
163 int32_t LnnUpdateNodeAddr(const char *addr);
164 NodeInfo *FindNodeInfoByRquestId(uint32_t requestId);
165 AuthVerifyCallback *LnnGetVerifyCallback(void);
166 AuthVerifyCallback *LnnGetReAuthVerifyCallback(void);
167 void SetWatchdogFlag(bool flag);
168 bool GetWatchdogFlag(void);
169 NetBuilder* LnnGetNetBuilder(void);
170 void AddNodeToLnnBleReportExtraMap(const char *udidHash, const LnnBleReportExtra *bleExtra);
171 int32_t GetNodeFromLnnBleReportExtraMap(const char *udidHash, LnnBleReportExtra *bleExtra);
172 bool IsExistLnnDfxNodeByUdidHash(const char *udidHash, LnnBleReportExtra *bleExtra);
173 void DeleteNodeFromLnnBleReportExtraMap(const char *udidHash);
174 void ClearLnnBleReportExtraMap(void);
175 void DfxRecordLnnServerjoinStart(const ConnectionAddr *addr, const char *packageName, bool needReportFailure);
176 bool TryPendingJoinRequest(const JoinLnnMsgPara *para, bool needReportFailure);
177 bool IsNeedWifiReauth(const char *networkId, const char *newAccountHash, int32_t len);
178 void DfxRecordLnnAuthStart(const AuthConnInfo *connInfo, const JoinLnnMsgPara *para, uint32_t requestId);
179 void TryRemovePendingJoinRequest(void);
180 void UpdateLocalMasterNode(bool isCurrentNode, const char *masterUdid, int32_t weight);
181 void SendElectMessageToAll(const char *skipNetworkId);
182 bool IsNodeOnline(const char *networkId);
183 void RemovePendingRequestByAddrType(const bool *addrType, uint32_t typeLen);
184 void UpdateLocalNetCapability(void);
185 void OnReceiveMasterElectMsg(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t len);
186 void OnReceiveNodeAddrChangedMsg(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t size);
187 int32_t ConifgLocalLedger(void);
188 int32_t SyncElectMessage(const char *networkId);
189 ConnectionAddrType GetCurrentConnectType(void);
190 NodeInfo *DupNodeInfo(const NodeInfo *nodeInfo);
191 bool NeedPendingJoinRequest(void);
192 void PostVerifyResult(uint32_t requestId, int32_t retCode, AuthHandle authHandle, const NodeInfo *info);
193 int32_t TrySendJoinLNNRequest(const JoinLnnMsgPara *para, bool needReportFailure, bool isShort);
194 int32_t PostBuildMessageToHandler(int32_t msgType, void *para);
195 bool DeletePcNodeInfo(const char *peerUdid);
196 const char *SelectUseUdid(const char *peerUdid, const char *lowerUdid);
197 void LnnDeleteLinkFinderInfo(const char *peerUdid);
198 void LnnProcessCompleteNotTrustedMsg(LnnSyncInfoType syncType, const char *networkId,
199     const uint8_t *msg, uint32_t len);
200 void OnLnnProcessNotTrustedMsgDelay(void *para);
201 void LnnBlePcRestrictMapInit(void);
202 void AddNodeToPcRestrictMap(const char *udidHash);
203 void ClearPcRestrictMap(void);
204 void DeleteNodeFromPcRestrictMap(const char *udidHash);
205 int32_t GetNodeFromPcRestrictMap(const char *udidHash, uint32_t *count);
206 int32_t UpdateNodeFromPcRestrictMap(const char *udidHash);
207 int32_t JoinLnnWithNodeInfo(ConnectionAddr *addr, NodeInfo *info);
208 int32_t AuthFailNotifyProofInfo(int32_t errCode, const char *errorReturn, uint32_t errorReturnLen);
209 #ifdef __cplusplus
210 }
211 #endif
212 
213 #endif