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