1 /*
2  * Copyright (C) 2021-2022 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 OHOS_WIFI_P2P_SERVICE_IMPL_H
17 #define OHOS_WIFI_P2P_SERVICE_IMPL_H
18 
19 #ifndef OHOS_ARCH_LITE
20 #include "system_ability.h"
21 #include "dhcp_server_stub.h"
22 #include "iremote_object.h"
23 #else
24 #include "dhcp_server_stub_lite.h"
25 #endif
26 #include <mutex>
27 #include <list>
28 #include <set>
29 #include <string>
30 
31 namespace OHOS {
32 namespace DHCP {
33 constexpr int MAXRETRYTIMEOUT = 10;
34 
35 enum ServerServiceRunningState { STATE_NOT_START, STATE_RUNNING };
36 void DeviceConnectCallBack(const char* ifname);
37 
38 class DhcpServerServiceImpl :
39 #ifndef OHOS_ARCH_LITE
40 public SystemAbility,
41 #endif
42 public DhcpServerStub {
43 #ifndef OHOS_ARCH_LITE
44     DECLARE_SYSTEM_ABILITY(DhcpServerServiceImpl);
45 #endif
46 
47 public:
48     DhcpServerServiceImpl();
49     virtual ~DhcpServerServiceImpl();
50 #ifdef OHOS_ARCH_LITE
51     static std::shared_ptr<DhcpServerServiceImpl> GetInstance();
52     void OnStart();
53     void OnStop();
54 #else
55     static sptr<DhcpServerServiceImpl> GetInstance();
56     void OnStart() override;
57     void OnStop() override;
58 #endif
59 #ifdef OHOS_ARCH_LITE
60     ErrCode RegisterDhcpServerCallBack(const std::string& ifname,
61         const std::shared_ptr<IDhcpServerCallBack> &serverCallback) override;
62 #else
63     ErrCode RegisterDhcpServerCallBack(const std::string& ifname,
64         const sptr<IDhcpServerCallBack> &serverCallback) override;
65 #endif
66     ErrCode StartDhcpServer(const std::string& ifname) override;
67     ErrCode StopDhcpServer(const std::string& ifname) override;
68     ErrCode PutDhcpRange(const std::string& tagName, const DhcpRange& range) override;
69     ErrCode RemoveDhcpRange(const std::string& tagName, const DhcpRange& range) override;
70     ErrCode RemoveAllDhcpRange(const std::string& tagName) override;
71     ErrCode SetDhcpRange(const std::string& ifname, const DhcpRange& range) override;
72     ErrCode SetDhcpName(const std::string& ifname, const std::string& tagName) override;
73     ErrCode GetDhcpClientInfos(const std::string& ifname, std::vector<std::string>& leases) override;
74     ErrCode UpdateLeasesTime(const std::string& leaseTime) override;
75     bool IsRemoteDied(void) override;
76     ErrCode DeleteLeaseFile(const std::string& ifname);
77 
78 #ifndef OHOS_ARCH_LITE
79     void StartServiceAbility(int sleepS);
80 #endif
81 
82     /**
83      * @Description : Fork child process function for start dhcp server process.
84      *
85      * @param ifname - interface name, eg:wlan0 [in]
86      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
87      */
88     int ForkExecProcess(const std::string ifname, const std::string ip, const std::string mask, const std::string pool);
89     int DelSpecifiedInterface(const std::string& ifname);
90     void DeviceInfoCallBack(const std::string& ifname);
91 private:
92     bool Init();
93     int CheckAndUpdateConf(const std::string& ifname);
94     bool CheckIpAddrRange(const DhcpRange& range);
95     int AddSpecifiedInterface(const std::string& ifname);
96     int GetUsingIpRange(const std::string ifname, std::string& ipRange);
97     int CreateDefaultConfigFile(const std::string strFile);
98     int StopServer(const pid_t& serverPid);
99     void UnregisterSignal() const;
100     bool IsNativeProcess();
101     void ConvertLeasesToStationInfos(std::vector<std::string> &leases, std::vector<DhcpStationInfo>& stationInfos);
102 
103     bool mPublishFlag;
104     static std::mutex g_instanceLock;
105     ServerServiceRunningState mState;
106     std::map<std::string, std::list<DhcpRange>> m_mapInfDhcpRange;  /* dhcp server using ip range */
107     std::map<std::string, std::list<DhcpRange>> m_mapTagDhcpRange;  /* dhcp server can be used ip range */
108     std::set<std::string> m_setInterfaces;                          /* the started specified interfaces */
109 
110     std::mutex m_serverCallBackMutex;
111 #ifdef OHOS_ARCH_LITE
112     std::map<std::string, std::shared_ptr<IDhcpServerCallBack>> m_mapServerCallBack;
113 #else
114     std::map<std::string, sptr<IDhcpServerCallBack>> m_mapServerCallBack;
115 #endif
116 
117 private:
118 #ifdef OHOS_ARCH_LITE
119     static std::shared_ptr<DhcpServerServiceImpl> g_instance;
120 #else
121     static sptr<DhcpServerServiceImpl> g_instance;
122 #endif
123     static std::map<std::string, DhcpServerInfo> m_mapDhcpServer;
124 
125 };
126 }  // namespace DHCP
127 }  // namespace OHOS
128 #endif