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_SERVER_SERVICE_H 17 #define OHOS_DHCP_SERVER_SERVICE_H 18 19 #include <sys/types.h> 20 #include <sys/wait.h> 21 #include <map> 22 #include <list> 23 #include <vector> 24 #include <set> 25 #include <thread> 26 27 #include "i_dhcp_server_service.h" 28 #include "dhcp_define.h" 29 30 31 namespace OHOS { 32 namespace Wifi { 33 class DhcpServerService : public IDhcpServerService { 34 public: 35 /** 36 * @Description : Construct a new dhcp server service object. 37 * 38 */ 39 DhcpServerService(); 40 41 /** 42 * @Description : Destroy the dhcp server service object. 43 * 44 */ 45 ~DhcpServerService() override; 46 47 /** 48 * @Description : Start dhcp server service of specified interface. 49 * 50 * @param ifname - interface name, eg:wlan0 [in] 51 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 52 */ 53 int StartDhcpServer(const std::string& ifname) override; 54 55 /** 56 * @Description : Stop dhcp server service of specified interface. 57 * 58 * @param ifname - interface name, eg:wlan0 [in] 59 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 60 */ 61 int StopDhcpServer(const std::string& ifname) override; 62 63 /** 64 * @Description : Get dhcp server service running status. 65 * 66 * @Return : 0 - not start, 1 - normal started. 67 */ 68 int GetServerStatus(void) override; 69 70 /** 71 * @Description : Add or update dhcp ip address pool. 72 * 73 * @param tagName - ip address pool tag name [in] 74 * @param range - ip address range [in] 75 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 76 */ 77 int PutDhcpRange(const std::string& tagName, const DhcpRange& range) override; 78 79 /** 80 * @Description : Remove dhcp ip address pool. 81 * 82 * @param tagName - ip address pool tag name [in] 83 * @param range - ip address range [in] 84 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 85 */ 86 int RemoveDhcpRange(const std::string& tagName, const DhcpRange& range) override; 87 88 /** 89 * @Description : Remove all dhcp ip address pool. 90 * 91 * @param tagName - ip address pool tag name [in] 92 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 93 */ 94 int RemoveAllDhcpRange(const std::string& tagName) override; 95 96 /** 97 * @Description : Set dhcp ip address pool of specified interface. 98 * 99 * @param ifname - interface name, eg:wlan0 [in] 100 * @param range - ip address range [in] 101 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 102 */ 103 int SetDhcpRange(const std::string& ifname, const DhcpRange& range) override; 104 105 /** 106 * @Description : Set dhcp ip address pool of specified interface. 107 * 108 * @param ifname - interface name, eg:wlan0 [in] 109 * @param tagName - ip address pool tag name [in] 110 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 111 */ 112 int SetDhcpRange(const std::string& ifname, const std::string& tagName) override; 113 114 /** 115 * @Description : Get dhcp server lease info. 116 * 117 * @param ifname - interface name, eg:wlan0 [in] 118 * @param leases - lease info [out] 119 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 120 */ 121 int GetLeases(const std::string& ifname, std::vector<std::string>& leases) override; 122 123 /** 124 * @Description : Obtain the abnormal exit status of dhcp server process. 125 * 126 * @param ifname - interface name, eg:wlan0 [in] 127 * @param pResultNotify - pointer to dhcp result notify [in] 128 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 129 */ 130 int GetDhcpSerProExit(const std::string& ifname, IDhcpResultNotify *pResultNotify) override; 131 132 /** 133 * @Description : Reload dhcp server config. 134 * 135 * @param ifname - interface name, eg:wlan0 [in] 136 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 137 */ 138 int ReloadConfig(const std::string& ifname) override; 139 140 /** 141 * @Description : Check and update dhcp server config of specified interface. 142 * 143 * @param ifname - interface name, eg:wlan0 [in] 144 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 145 */ 146 int CheckAndUpdateConf(const std::string& ifname); 147 148 /** 149 * @Description : Check invalid or already exist in dhcp range. 150 * 151 * @param range - ip address range [in] 152 * @Return : true - yes, false - no. 153 */ 154 bool CheckIpAddrRange(const DhcpRange& range); 155 156 /** 157 * @Description : Add the specified interface. 158 * 159 * @param ifname - interface name, eg:wlan0 [in] 160 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 161 */ 162 int AddSpecifiedInterface(const std::string& ifname); 163 164 /** 165 * @Description : Delete the specified interface. 166 * 167 * @param ifname - interface name, eg:wlan0 [in] 168 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 169 */ 170 int DelSpecifiedInterface(const std::string& ifname); 171 172 /** 173 * @Description : Get dhcp ip address pool of specified interface. 174 * 175 * @param ifname - interface name, eg:wlan0 [in] 176 * @param ipRange - ip address range [out] 177 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 178 */ 179 int GetUsingIpRange(const std::string ifname, std::string& ipRange); 180 181 /** 182 * @Description : Create default config file. 183 * 184 * @param strFile - config file name [in] 185 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 186 */ 187 int CreateDefaultConfigFile(const std::string strFile); 188 189 /** 190 * @Description : Stop dhcp server on exit 191 * 192 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 193 */ 194 int StopDhcpServerOnExit(); 195 196 private: 197 /** 198 * @Description : Fork parent process function. 199 * 200 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 201 */ 202 int ForkParentProcess(); 203 204 /** 205 * @Description : Fork child process function for start dhcp server process. 206 * 207 * @param ifname - interface name, eg:wlan0 [in] 208 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 209 */ 210 int ForkExecProcess(const std::string ifname, const std::string ip, const std::string mask, const std::string pool); 211 212 /** 213 * @Description : Stop dhcp server process. 214 * 215 * @param serverPid - process id [in] 216 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 217 */ 218 int StopServer(const pid_t& serverPid); 219 220 /** 221 * @Description : Exit dhcp process exit abnormal notify thread. 222 * 223 */ 224 void ExitDhcpMgrThreadFunc(); 225 226 /** 227 * @Description : Dhcp server process exit abnormal notify. 228 * 229 */ 230 void RunDhcpSerProExitThreadFunc(); 231 232 /** 233 * @Description : Register the SIGCHID signal. 234 * 235 */ 236 void RegisterSignal() const; 237 238 /** 239 * @Description : Unregister the SIGCHID signal. 240 * 241 */ 242 void UnregisterSignal() const; 243 244 /** 245 * @Description : Receives the SIGCHLD signal of the dhcp server process. 246 * 247 * @param signum - signal num [in] 248 */ 249 static void SigChildHandler(int signum); 250 251 /** 252 * @Description : Get dhcp server process id. 253 * 254 * @param ifname - interface name, eg:wlan0 [in] 255 * @Return : dhcp server process id. 256 */ 257 static pid_t GetDhcpServerPid(const std::string &ifname); 258 259 /** 260 * @Description : Set dhcp server info. 261 * 262 * @param ifname - interface name, eg:wlan0 [in] 263 * @param status - process run status [in] 264 * @param serverPid - process id [in] 265 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 266 */ 267 static int SetDhcpServerInfo(const std::string &ifname, int status, const pid_t& serverPid); 268 269 private: 270 std::set<std::string> m_setInterfaces; /* the started specified interfaces */ 271 std::map<std::string, std::list<DhcpRange>> m_mapTagDhcpRange; /* dhcp server can be used ip range */ 272 std::map<std::string, std::list<DhcpRange>> m_mapInfDhcpRange; /* dhcp server using ip range */ 273 std::map<std::string, IDhcpResultNotify *> m_mapDhcpSerExitNotify; 274 static std::map<std::string, DhcpServerInfo> m_mapDhcpServer; 275 276 bool bDhcpSerProExitThread; 277 std::thread *pDhcpSerProExitThread; 278 }; 279 } // namespace Wifi 280 } // namespace OHOS 281 #endif