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_CLIENT_SERVICE_IMPL_H 17 #define OHOS_DHCP_CLIENT_SERVICE_IMPL_H 18 19 #include <map> 20 #include <list> 21 #include <thread> 22 #include <mutex> 23 24 #include "i_dhcp_client_service.h" 25 #include "dhcp_ipv6_client.h" 26 27 namespace OHOS { 28 namespace Wifi { 29 struct DhcpResultReq { 30 int timeouts; 31 int getTimestamp; 32 int status; // 1-ipv4 getted,2-ipv6 getted,3-all getted 33 IDhcpResultNotify *pResultNotify; 34 DhcpResultReqDhcpResultReq35 DhcpResultReq() 36 { 37 timeouts = RECEIVER_TIMEOUT; 38 getTimestamp = 0; 39 status = 0; 40 pResultNotify = nullptr; 41 } 42 }; 43 class DhcpEventSubscriber; 44 class DhcpClientServiceImpl : public IDhcpClientService { 45 public: 46 /** 47 * @Description : Construct a new dhcp client service object. 48 * 49 */ 50 DhcpClientServiceImpl(); 51 52 /** 53 * @Description : Destroy the dhcp client service object. 54 * 55 */ 56 ~DhcpClientServiceImpl() override; 57 58 /** 59 * @Description : Start dhcp client service of specified interface. 60 * 61 * @param ifname - interface name, eg:wlan0 [in] 62 * @param bIpv6 - can or not get ipv6 [in] 63 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 64 */ 65 int StartDhcpClient(const std::string& ifname, bool bIpv6) override; 66 67 /** 68 * @Description : Stop dhcp client service of specified interface. 69 * 70 * @param ifname - interface name, eg:wlan0 [in] 71 * @param bIpv6 - can or not get ipv6 [in] 72 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 73 */ 74 int StopDhcpClient(const std::string& ifname, bool bIpv6) override; 75 76 /** 77 * @Description : Get dhcp client service running status of specified interface. 78 * 79 * @param ifname - interface name, eg:wlan0 [in] 80 * @Return : 0 - not start, 1 - normal started, -1 - not normal. 81 */ 82 int GetDhcpStatus(const std::string& ifname) override; 83 84 /** 85 * @Description : Obtain the dhcp result of specified interface asynchronously. 86 * 87 * @param ifname - interface name, eg:wlan0 [in] 88 * @param pResultNotify - dhcp result notify [in] 89 * @param timeouts - timeout interval [in] 90 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 91 */ 92 int GetDhcpResult(const std::string& ifname, IDhcpResultNotify *pResultNotify, int timeouts) override; 93 94 /** 95 * @Description : remove the dhcp result of specified interface asynchronously. 96 * 97 * @param dhcp - dhcp result notify [in] 98 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 99 */ 100 int RemoveDhcpResult(IDhcpResultNotify *pResultNotify) override; 101 102 /** 103 * @Description : Obtain the dhcp info of specified interface synchronously. 104 * 105 * @param ifname - interface name, eg:wlan0 [in] 106 * @param dhcp - dhcp info [out] 107 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 108 */ 109 int GetDhcpInfo(const std::string& ifname, DhcpServiceInfo& dhcp) override; 110 111 /** 112 * @Description : Renew dhcp client service of specified interface. 113 * 114 * @param ifname - interface name, eg:wlan0 [in] 115 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 116 */ 117 int RenewDhcpClient(const std::string& ifname) override; 118 119 /** 120 * @Description : Release dhcp client service of specified interface. 121 * 122 * @param ifname - interface name, eg:wlan0 [in] 123 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 124 */ 125 int ReleaseDhcpClient(const std::string &ifname) override; 126 127 /** 128 * @Description : Handle dhcp result. 129 * 130 * @param second - sleep second number [out] 131 */ 132 void DhcpResultHandle(uint32_t &second); 133 134 /** 135 * @Description : Get the dhcp client process pid of specified interface. 136 * 137 * @param ifname - interface name, eg:wlan0 [in] 138 * @Return : The dhcp client process pid. 139 */ 140 pid_t GetDhcpClientProPid(const std::string &ifname); 141 142 /** 143 * @Description : Check the dhcp client process of specified interface is or not running. 144 * 145 * @param ifname - interface name, eg:wlan0 [in] 146 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 147 */ 148 int CheckDhcpClientRunning(const std::string &ifname); 149 150 /** 151 * @Description : Get dhcp event success ipv4 result. 152 * 153 * @param splits - dhcp event result vector [in] 154 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 155 */ 156 static int GetSuccessIpv4Result(const std::vector<std::string> &splits); 157 158 /** 159 * @Description : Get dhcp event ipv4 result. 160 * 161 * @param code - dhcp event result code [in] 162 * @param splits - dhcp event result vector [in] 163 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 164 */ 165 static int GetDhcpEventIpv4Result(const int code, const std::vector<std::string> &splits); 166 167 /** 168 * @Description : Handle dhcp event result string. 169 * 170 * @param code - dhcp event result code [in] 171 * @param data - dhcp event result string [in] 172 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 173 */ 174 static int DhcpEventResultHandle(const int code, const std::string &data); 175 176 /** 177 * @Description : check result already exist 178 * 179 * @param ifname - interface name 180 * @param result - dhcp result 181 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 182 */ 183 static bool CheckDhcpResultExist(const std::string &ifname, DhcpResult &result); 184 185 /** 186 * @Description : Handle dhcp event result string. 187 * 188 * @param ifname - interface name 189 * @param result - dhcp result 190 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 191 */ 192 static void PushDhcpResult(const std::string &ifname, DhcpResult &result); 193 194 public: 195 static pthread_mutex_t m_DhcpResultInfoMutex; 196 static std::map<std::string, std::vector<DhcpResult>> m_mapDhcpResult; 197 static std::map<std::string, DhcpServiceInfo> m_mapDhcpInfo; 198 199 private: 200 /** 201 * @Description : Start dhcp result handle threads. 202 * 203 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 204 */ 205 int InitDhcpMgrThread(); 206 /** 207 * @Description : Exit dhcp result handle threads and recv msg threads. 208 * 209 */ 210 void ExitDhcpMgrThread(); 211 /** 212 * @Description : Check dhcp result req is or not timeout. 213 * 214 */ 215 void CheckTimeout(); 216 /** 217 * @Description : Dhcp result handle threads execution function. 218 * 219 */ 220 void RunDhcpResultHandleThreadFunc(); 221 222 /** 223 * @Description : get ipv6 info tread 224 * 225 */ 226 void RunIpv6ThreadFunc(); 227 228 #ifdef OHOS_ARCH_LITE 229 /** 230 * @Description : Dhcp recv msg threads execution function. 231 * 232 * @param ifname - interface name, eg:wlan0 [in] 233 */ 234 void RunDhcpRecvMsgThreadFunc(const std::string& ifname); 235 236 /** 237 * @Description : Handle dhcp packet info. 238 * 239 * @param ifname - interface name, eg:wlan0 [in] 240 * @param packetResult - dhcp packet result [in] 241 * @param success - get success is true, get failed is false [in] 242 */ 243 void DhcpPacketInfoHandle(const std::string& ifname, struct DhcpPacketResult &packetResult, bool success = true); 244 #endif 245 /** 246 * @Description : Fork child process function for start or stop dhcp process. 247 * 248 * @param ifname - interface name, eg:wlan0 [in] 249 * @param bIpv6 - can or not get ipv6 [in] 250 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 251 */ 252 int ForkExecChildProcess(const std::string& ifname, bool bIpv6, bool bStart = false); 253 /** 254 * @Description : Fork parent process function for handle dhcp function. 255 * 256 * @param ifname - interface name, eg:wlan0 [in] 257 * @param bIpv6 - can or not get ipv6 [in] 258 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 259 */ 260 int ForkExecParentProcess(const std::string& ifname, bool bIpv6, bool bStart = false, pid_t pid = 0); 261 262 /** 263 * @Description : Fork parent process start function for handle dhcp function. 264 * 265 * @param ifname - interface name, eg:wlan0 [in] 266 * @param bIpv6 - can or not get ipv6 [in] 267 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 268 */ 269 int DealParentProcessStart(const std::string& ifname, bool bIpv6, pid_t pid = 0); 270 271 /** 272 * @Description : Fork parent process stop function for handle dhcp function. 273 * 274 * @param ifname - interface name, eg:wlan0 [in] 275 * @param bIpv6 - can or not get ipv6 [in] 276 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 277 */ 278 int DealParentProcessStop(const std::string& ifname, bool bIpv6, pid_t pid = 0); 279 280 /** 281 * @Description : Subscribe dhcp event. 282 * 283 * @param strAction - event action [in] 284 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 285 */ 286 int SubscribeDhcpEvent(const std::string &strAction); 287 /** 288 * @Description : Unsubscribe dhcp event. 289 * 290 * @param strAction - event action [in] 291 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 292 */ 293 int UnsubscribeDhcpEvent(const std::string &strAction); 294 295 /** 296 * @Description : release result notify memory. 297 * 298 */ 299 void ReleaseResultNotifyMemory(); 300 301 /** 302 * @Description : Unsubscribe all dhcp event. 303 * 304 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 305 */ 306 int UnsubscribeAllDhcpEvent(); 307 308 /** 309 * @Description : handle ipv6 info changed. 310 * 311 * @Return : none. 312 */ 313 void OnAddressChangedCallback(const std::string ifname, DhcpIpv6Info &info); 314 315 private: 316 std::mutex mResultNotifyMutex; 317 bool isExitDhcpResultHandleThread; 318 std::thread *pDhcpResultHandleThread; 319 #ifdef OHOS_ARCH_LITE 320 std::mutex mRecvMsgThreadMutex; 321 std::map<std::string, std::thread *> m_mapDhcpRecvMsgThread; 322 #endif 323 std::map<std::string, std::list<DhcpResultReq*>> m_mapDhcpResultNotify; 324 325 std::mutex m_subscriberMutex; 326 std::map<std::string, std::shared_ptr<OHOS::Wifi::DhcpEventSubscriber>> m_mapEventSubscriber; 327 328 std::string currIfName; 329 DhcpIpv6Client ipv6Client; 330 std::thread *pDhcpIpv6ClientThread; 331 }; 332 } // namespace Wifi 333 } // namespace OHOS 334 #endif