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_DHCP_SERVICE_H 17 #define OHOS_DHCP_SERVICE_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 #include "dhcp_define.h" 23 #include "i_dhcp_client_service.h" 24 #include "i_dhcp_result_notify.h" 25 #include "i_dhcp_server_service.h" 26 #include "i_dhcp_service.h" 27 28 namespace OHOS { 29 namespace Wifi { 30 class DhcpService : public IDhcpService { 31 public: 32 /** 33 * @Description : Construct a new dhcp service object. 34 * 35 */ 36 DhcpService(); 37 38 /** 39 * @Description : Destroy the dhcp service object. 40 * 41 */ 42 ~DhcpService() override; 43 44 /** 45 * @Description : Start dhcp client service of specified interface. 46 * 47 * @param ifname - interface name, eg:wlan0 [in] 48 * @param bIpv6 - can or not get ipv6 [in] 49 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 50 */ 51 int StartDhcpClient(const std::string& ifname, bool bIpv6) override; 52 53 /** 54 * @Description : Stop dhcp client service of specified interface. 55 * 56 * @param ifname - interface name, eg:wlan0 [in] 57 * @param bIpv6 - can or not get ipv6 [in] 58 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 59 */ 60 int StopDhcpClient(const std::string& ifname, bool bIpv6) override; 61 62 /** 63 * @Description : Obtain the dhcp result of specified interface asynchronously. 64 * 65 * @param ifname - interface name, eg:wlan0 [in] 66 * @param dhcp - dhcp result notify [in] 67 * @param timeouts - timeout interval [in] 68 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 69 */ 70 int GetDhcpResult(const std::string& ifname, IDhcpResultNotify *pResultNotify, int timeouts) override; 71 72 /** 73 * @Description : remove the dhcp result of specified interface asynchronously. 74 * 75 * @param dhcp - dhcp result notify [in] 76 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 77 */ 78 int RemoveDhcpResult(IDhcpResultNotify *pResultNotify) override; 79 80 /** 81 * @Description : Obtain the dhcp info of specified interface synchronously. 82 * 83 * @param ifname - interface name, eg:wlan0 [in] 84 * @param dhcp - dhcp info [out] 85 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 86 */ 87 int GetDhcpInfo(const std::string& ifname, DhcpServiceInfo& dhcp) override; 88 89 /** 90 * @Description : Renew dhcp client service of specified interface. 91 * 92 * @param ifname - interface name, eg:wlan0 [in] 93 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 94 */ 95 int RenewDhcpClient(const std::string& ifname) override; 96 97 /** 98 * @Description : Release dhcp client service of specified interface. 99 * 100 * @param ifname - interface name, eg:wlan0 [in] 101 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 102 */ 103 int ReleaseDhcpClient(const std::string& ifname) override; 104 105 /** 106 * @Description : Start dhcp server service of specified interface. 107 * 108 * @param ifname - interface name, eg:wlan0 [in] 109 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 110 */ 111 int StartDhcpServer(const std::string& ifname) override; 112 113 /** 114 * @Description : Stop dhcp server service of specified interface. 115 * 116 * @param ifname - interface name, eg:wlan0 [in] 117 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 118 */ 119 int StopDhcpServer(const std::string& ifname) override; 120 121 /** 122 * @Description : Get dhcp server service running status. 123 * 124 * @Return : 0 - not start, 1 - normal started. 125 */ 126 int GetServerStatus(void) override; 127 128 /** 129 * @Description : Add or update dhcp ip address pool. 130 * 131 * @param tagName - ip address pool tag name [in] 132 * @param range - ip address range [in] 133 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 134 */ 135 int PutDhcpRange(const std::string& tagName, const DhcpRange& range) override; 136 137 /** 138 * @Description : Remove dhcp ip address pool. 139 * 140 * @param tagName - ip address pool tag name [in] 141 * @param range - ip address range [in] 142 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 143 */ 144 int RemoveDhcpRange(const std::string& tagName, const DhcpRange& range) override; 145 146 /** 147 * @Description : Remove all dhcp ip address pool. 148 * 149 * @param tagName - ip address pool tag name [in] 150 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 151 */ 152 int RemoveAllDhcpRange(const std::string& tagName) override; 153 154 /** 155 * @Description : Set dhcp ip address pool of specified interface. 156 * 157 * @param ifname - interface name, eg:wlan0 [in] 158 * @param range - ip address range [in] 159 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 160 */ 161 int SetDhcpRange(const std::string& ifname, const DhcpRange& range) override; 162 163 /** 164 * @Description : Set dhcp ip address pool of specified interface. 165 * 166 * @param ifname - interface name, eg:wlan0 [in] 167 * @param tagName - ip address pool tag name [in] 168 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 169 */ 170 int SetDhcpRange(const std::string& ifname, const std::string& tagName) override; 171 172 /** 173 * @Description : Get dhcp server lease info. 174 * 175 * @param ifname - interface name, eg:wlan0 [in] 176 * @param leases - lease info [out] 177 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 178 */ 179 int GetLeases(const std::string& ifname, std::vector<std::string>& leases) override; 180 181 /** 182 * @Description : Obtain the abnormal exit status of dhcp server process. 183 * 184 * @param ifname - interface name, eg:wlan0 [in] 185 * @param pResultNotify - pointer to dhcp result notify [in] 186 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 187 */ 188 int GetDhcpSerProExit(const std::string& ifname, IDhcpResultNotify *pResultNotify) override; 189 190 private: 191 int InitServerService(void); 192 bool CheckIfaceValid(const std::string& ifname); 193 194 private: 195 std::unique_ptr<IDhcpClientService> m_pClientService; 196 std::unique_ptr<IDhcpServerService> m_pServerService; 197 }; 198 } // namespace Wifi 199 } // namespace OHOS 200 #endif 201