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 NETWORKSHARE_SUB_STATEMACHINE_H
17 #define NETWORKSHARE_SUB_STATEMACHINE_H
18 
19 #include "dhcp_c_api.h"
20 #include "ffrt_inner.h"
21 #include "net_manager_constants.h"
22 #include "net_manager_ext_constants.h"
23 #include "networkshare_configuration.h"
24 #include "networkshare_constants.h"
25 #include "networkshare_hisysevent.h"
26 #include "networkshare_state_common.h"
27 #include "router_advertisement_daemon.h"
28 #include "router_advertisement_params.h"
29 #include <any>
30 #include <cstring>
31 #include <map>
32 #include <mutex>
33 #include <set>
34 #include <sstream>
35 
36 namespace OHOS {
37 namespace NetManagerStandard {
38 class NetworkShareSubStateMachine : public std::enable_shared_from_this<NetworkShareSubStateMachine> {
39     using HandleFunc = int (NetworkShareSubStateMachine::*)(const std::any &messageObj);
40 
41 public:
42     NetworkShareSubStateMachine() = delete;
43     NetworkShareSubStateMachine(const std::string &ifaceName, const SharingIfaceType &interfaceType,
44                                 const std::shared_ptr<NetworkShareConfiguration> &configuration);
45     ~NetworkShareSubStateMachine();
46 
47     /**
48      * get sub state machine share type
49      */
50     SharingIfaceType GetNetShareType() const;
51 
52     /**
53      * get sub state machine interface name
54      */
55     const std::string &GetInterfaceName() const;
56 
57     class SubStateMachineCallback {
58     public:
59         virtual void OnUpdateInterfaceState(const std::shared_ptr<NetworkShareSubStateMachine> &paraSubStateMachine,
60                                             int state, int lastError) = 0;
61     };
62 
63     /**
64      * register callback
65      */
66     void RegisterSubSMCallback(const std::shared_ptr<SubStateMachineCallback> &callback);
67 
68     /**
69      * execute state switch
70      */
71     void SubSmStateSwitch(int newState);
72 
73     /**
74      * execute event
75      */
76     void SubSmEventHandle(int eventId, const std::any &messageObj);
77 
78     /**
79      * get down interface name
80      */
81     void GetDownIfaceName(std::string &downIface);
82 
83     /**
84      * get up interface name
85      */
86     void GetUpIfaceName(std::string &upIface);
87 
88     void HandleConnection();
89 
90 private:
91     void CreateInitStateTable();
92     void CreateSharedStateTable();
93     void InitStateEnter();
94     void SharedStateEnter();
95     void UnavailableStateEnter();
96     void InitStateExit();
97     void SharedStateExit();
98     void UnavailableStateExit();
99     int HandleInitSharingRequest(const std::any &messageObj);
100     int HandleInitInterfaceDown(const std::any &messageObj);
101     int HandleSharedUnrequest(const std::any &messageObj);
102     int HandleSharedInterfaceDown(const std::any &messageObj);
103     int HandleSharedConnectionChange(const std::any &messageObj);
104     int HandleSharedErrors(const std::any &messageObj);
105 
106     bool ConfigureShareDhcp(bool enabled);
107     bool RequestIpv4Address(std::shared_ptr<INetAddr> &netAddr);
108     bool StartDhcp(const std::shared_ptr<INetAddr> &netAddr);
109     bool SetRange(DhcpRange &range, const std::string &ipHead, const std::string &strStartip,
110                   const std::string &strEndip, const std::string &mask);
111     bool StopDhcp();
112     void HandleConnectionChanged(const std::shared_ptr<UpstreamNetworkInfo> &upstreamNetInfo);
113     void RemoveRoutesToLocalNetwork();
114     void AddRoutesToLocalNetwork();
115     void CleanupUpstreamInterface();
116     bool HasChangeUpstreamIfaceSet(const std::string &newUpstreamIface);
117     bool GetWifiHotspotDhcpFlag();
118     bool GetBtDestinationAddr(std::string &addrStr);
119     bool GetWifiApDestinationAddr(std::string &addrStr);
120     bool GetWifiApDstIpv6Addr();
121     bool GetUsbDestinationAddr(std::string &addrStr);
122     bool CheckConfig(std::string &endIp, std::string &mask);
123     bool FindDestinationAddr(std::string &destination);
124     ffrt::recursive_mutex &getUsefulMutex();
125     void StartIpv6();
126     void StopIpv6();
127     std::string MacToEui64Addr(std::string &mac);
128     int32_t GenerateIpv6(const std::string &iface);
129     bool GetShareIpv6Prefix(const std::string &iface);
130     void AddIpv6AddrToLocalNetwork();
131     void AddIpv6InfoToLocalNetwork();
132 
133 private:
134     struct SubSmStateTable {
135         int32_t event_;
136         int32_t curState_;
137         HandleFunc func_;
138         int32_t nextState_;
139     };
140     ffrt::recursive_mutex mutex_;
141     std::string ifaceName_;
142     SharingIfaceType netShareType_;
143     int32_t lastError_ = NETMANAGER_EXT_SUCCESS;
144     std::string upstreamIfaceName_;
145     std::shared_ptr<SubStateMachineCallback> trackerCallback_ = nullptr;
146     std::shared_ptr<NetworkShareConfiguration> configuration_ = nullptr;
147     int32_t curState_ = SUBSTATE_INIT;
148     std::vector<SubSmStateTable> stateTable_;
149     std::shared_ptr<RouterAdvertisementDaemon> raDaemon_ = nullptr;
150     RaParams lastRaParams_;
151 };
152 } // namespace NetManagerStandard
153 } // namespace OHOS
154 #endif // NETWORKSHARE_SUB_STATEMACHINE_H
155