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 #include "dhcp_client_service_impl.h"
17 #include <unistd.h>
18 #include "dhcp_common_utils.h"
19 #ifndef OHOS_ARCH_LITE
20 #include "dhcp_client_death_recipient.h"
21 #endif
22 #include "dhcp_function.h"
23 #include "dhcp_define.h"
24 #include "dhcp_errcode.h"
25 #include "dhcp_logger.h"
26 #include "dhcp_result_store_manager.h"
27 #include "dhcp_permission_utils.h"
28 #ifndef OHOS_ARCH_LITE
29 #include "ipc_skeleton.h"
30 #include "netsys_controller.h"
31 #ifndef OHOS_EUPDATER
32 #include "dhcp_sa_manager.h"
33 #endif
34 #endif
35
36 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientServiceImpl");
37
38 namespace OHOS {
39 namespace DHCP {
40 namespace {
41 constexpr uint32_t MAX_REGISTER_CLIENT_NUM = 1000;
42 }
43 std::mutex DhcpClientServiceImpl::g_instanceLock;
44
45 #ifdef OHOS_ARCH_LITE
46 std::shared_ptr<DhcpClientServiceImpl> DhcpClientServiceImpl::g_instance = nullptr;
GetInstance()47 std::shared_ptr<DhcpClientServiceImpl> DhcpClientServiceImpl::GetInstance()
48 {
49 if (g_instance == nullptr) {
50 std::lock_guard<std::mutex> autoLock(g_instanceLock);
51 if (g_instance == nullptr) {
52 std::shared_ptr<DhcpClientServiceImpl> service = std::make_shared<DhcpClientServiceImpl>();
53 g_instance = service;
54 }
55 }
56 return g_instance;
57 }
58 #else
59 sptr<DhcpClientServiceImpl> DhcpClientServiceImpl::g_instance = nullptr;
60 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(DhcpClientServiceImpl::GetInstance().GetRefPtr());
GetInstance()61 sptr<DhcpClientServiceImpl> DhcpClientServiceImpl::GetInstance()
62 {
63 if (g_instance == nullptr) {
64 std::lock_guard<std::mutex> autoLock(g_instanceLock);
65 if (g_instance == nullptr) {
66 DHCP_LOGI("new DhcpClientServiceImpl GetInstance()");
67 sptr<DhcpClientServiceImpl> service = sptr<DhcpClientServiceImpl>::MakeSptr();
68 g_instance = service;
69 }
70 }
71 return g_instance;
72 }
73 #endif
74
DhcpClientServiceImpl()75 DhcpClientServiceImpl::DhcpClientServiceImpl()
76 #ifndef OHOS_ARCH_LITE
77 : SystemAbility(DHCP_CLIENT_ABILITY_ID, true), mPublishFlag(false),
78 mState(ClientServiceRunningState::STATE_NOT_START)
79 #endif
80 {
81 DHCP_LOGI("enter DhcpClientServiceImpl()");
82 {
83 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
84 m_mapClientService.clear();
85 }
86
87 {
88 std::lock_guard<std::mutex> autoLock(m_dhcpResultMutex);
89 m_mapDhcpResult.clear();
90 }
91
92 {
93 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
94 m_mapClientCallBack.clear();
95 }
96 CreateDirs(DHCP_WORK_DIR.c_str(), DIR_DEFAULT_MODE);
97 }
98
~DhcpClientServiceImpl()99 DhcpClientServiceImpl::~DhcpClientServiceImpl()
100 {
101 DHCP_LOGI("enter ~DhcpClientServiceImpl()");
102 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
103 auto iter = m_mapClientService.begin();
104 while(iter != m_mapClientService.end()) {
105 if ((iter->second).pipv6Client != nullptr) {
106 delete (iter->second).pipv6Client;
107 (iter->second).pipv6Client = nullptr;
108 }
109 if ((iter->second).pStaStateMachine != nullptr) {
110 delete (iter->second).pStaStateMachine;
111 (iter->second).pStaStateMachine = nullptr;
112 }
113 iter++;
114 }
115 }
116
OnStart()117 void DhcpClientServiceImpl::OnStart()
118 {
119 DHCP_LOGI("enter Client OnStart");
120 if (mState == ClientServiceRunningState::STATE_RUNNING) {
121 DHCP_LOGW("Service has already started.");
122 return;
123 }
124 if (!Init()) {
125 DHCP_LOGE("Failed to init dhcp client service");
126 OnStop();
127 return;
128 }
129 mState = ClientServiceRunningState::STATE_RUNNING;
130 DHCP_LOGI("Client Service has started.");
131 }
132
OnStop()133 void DhcpClientServiceImpl::OnStop()
134 {
135 mPublishFlag = false;
136 DHCP_LOGI("OnStop dhcp client service!");
137 }
138
Init()139 bool DhcpClientServiceImpl::Init()
140 {
141 DHCP_LOGI("enter client Init");
142 if (!mPublishFlag) {
143 #ifdef OHOS_ARCH_LITE
144 bool ret = true;
145 #else
146 bool ret = Publish(DhcpClientServiceImpl::GetInstance());
147 #endif
148 if (!ret) {
149 DHCP_LOGE("Failed to publish dhcp client service!");
150 return false;
151 }
152 mPublishFlag = true;
153 }
154 return true;
155 }
156
157 #ifdef OHOS_ARCH_LITE
RegisterDhcpClientCallBack(const std::string & ifname,const std::shared_ptr<IDhcpClientCallBack> & clientCallback)158 ErrCode DhcpClientServiceImpl::RegisterDhcpClientCallBack(const std::string& ifname,
159 const std::shared_ptr<IDhcpClientCallBack> &clientCallback)
160 #else
161 ErrCode DhcpClientServiceImpl::RegisterDhcpClientCallBack(const std::string& ifname,
162 const sptr<IDhcpClientCallBack> &clientCallback)
163 #endif
164 {
165 if (!DhcpPermissionUtils::VerifyIsNativeProcess()) {
166 DHCP_LOGE("RegisterDhcpClientCallBack:NOT NATIVE PROCESS, PERMISSION_DENIED!");
167 return DHCP_E_PERMISSION_DENIED;
168 }
169 if (!DhcpPermissionUtils::VerifyDhcpNetworkPermission("ohos.permission.NETWORK_DHCP")) {
170 DHCP_LOGE("RegisterDhcpClientCallBack:VerifyDhcpNetworkPermission PERMISSION_DENIED!");
171 return DHCP_E_PERMISSION_DENIED;
172 }
173 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
174 auto iter = m_mapClientCallBack.find(ifname);
175 if (iter != m_mapClientCallBack.end()) {
176 (iter->second) = clientCallback;
177 DHCP_LOGI("RegisterDhcpClientCallBack find ifname update clientCallback, ifname:%{public}s", ifname.c_str());
178 } else {
179 uint32_t registerNum = m_mapClientCallBack.size();
180 if (registerNum > MAX_REGISTER_CLIENT_NUM) {
181 DHCP_LOGI("RegisterDhcpClientCallBack, ifname:%{public}s register failed, num over limit", ifname.c_str());
182 return DHCP_E_FAILED;
183 }
184 #ifdef OHOS_ARCH_LITE
185 std::shared_ptr<IDhcpClientCallBack> mclientCallback = clientCallback;
186 #else
187 sptr<IDhcpClientCallBack> mclientCallback = clientCallback;
188 #endif
189 m_mapClientCallBack.emplace(std::make_pair(ifname, mclientCallback));
190 DHCP_LOGI("RegisterDhcpClientCallBack add ifname and mclientCallback, ifname:%{public}s", ifname.c_str());
191 }
192 return DHCP_E_SUCCESS;
193 }
194
StartDhcpClient(const RouterConfig & config)195 ErrCode DhcpClientServiceImpl::StartDhcpClient(const RouterConfig &config)
196 {
197 DHCP_LOGI("StartDhcpClient ifName:%{public}s bIpv6:%{public}d, isStaticIpv4:%{public}d", config.ifname.c_str(),
198 config.bIpv6, config.isStaticIpv4);
199 if (!DhcpPermissionUtils::VerifyIsNativeProcess()) {
200 DHCP_LOGE("StartDhcpClient:NOT NATIVE PROCESS, PERMISSION_DENIED!");
201 return DHCP_E_PERMISSION_DENIED;
202 }
203 if (!DhcpPermissionUtils::VerifyDhcpNetworkPermission("ohos.permission.NETWORK_DHCP")) {
204 DHCP_LOGE("StartDhcpClient:VerifyDhcpNetworkPermission PERMISSION_DENIED!");
205 return DHCP_E_PERMISSION_DENIED;
206 }
207 if (config.ifname.empty()) {
208 DHCP_LOGE("StartDhcpClient ifname is empty!");
209 return DHCP_E_FAILED;
210 }
211 RouterConfig innerCfg;
212 innerCfg.ifname = config.ifname;
213 innerCfg.bssid = config.bssid;
214 innerCfg.prohibitUseCacheIp = config.prohibitUseCacheIp;
215 innerCfg.bIpv6 = config.bIpv6;
216 innerCfg.bSpecificNetwork = config.bSpecificNetwork;
217 innerCfg.isStaticIpv4 = config.isStaticIpv4;
218 innerCfg.bIpv4 = config.bIpv4;
219 {
220 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
221 auto iter = m_mapClientService.find(innerCfg.ifname);
222 if (iter != m_mapClientService.end()) {
223 return StartOldClient(innerCfg, iter->second);
224 }
225 }
226 return StartNewClient(innerCfg);
227 }
228
DealWifiDhcpCache(int32_t cmd,const IpCacheInfo & ipCacheInfo)229 ErrCode DhcpClientServiceImpl::DealWifiDhcpCache(int32_t cmd, const IpCacheInfo &ipCacheInfo)
230 {
231 DHCP_LOGI("DealWifiDhcpCache enter");
232 if (!DhcpPermissionUtils::VerifyIsNativeProcess()) {
233 DHCP_LOGE("DealWifiDhcpCache:NOT NATIVE PROCESS, PERMISSION_DENIED!");
234 return DHCP_E_PERMISSION_DENIED;
235 }
236 if (!DhcpPermissionUtils::VerifyDhcpNetworkPermission("ohos.permission.SET_WIFI_CONFIG")) {
237 DHCP_LOGE("DealWifiDhcpCache:VerifyDhcpNetworkPermission PERMISSION_DENIED!");
238 return DHCP_E_PERMISSION_DENIED;
239 }
240 IpInfoCached cacheInfo;
241 cacheInfo.ssid = ipCacheInfo.ssid;
242 cacheInfo.bssid = ipCacheInfo.bssid;
243 #ifndef OHOS_ARCH_LITE
244 std::function<void()> func;
245 if (cmd == WIFI_DHCP_CACHE_ADD) {
246 func = [cacheInfo]() { DhcpResultStoreManager::GetInstance().AddCachedIp(cacheInfo); };
247 } else if (cmd == WIFI_DHCP_CACHE_REMOVE) {
248 func = [cacheInfo]() { DhcpResultStoreManager::GetInstance().RemoveCachedIp(cacheInfo); };
249 } else {
250 return DHCP_E_FAILED;
251 }
252 uint32_t taskId = 0;
253 DhcpTimer::GetInstance()->Register(func, taskId, 0);
254 return (taskId > 0) ? DHCP_E_SUCCESS : DHCP_E_FAILED;
255 #else
256 if (cmd == WIFI_DHCP_CACHE_ADD) {
257 DhcpResultStoreManager::GetInstance().AddCachedIp(cacheInfo);
258 } else if (cmd == WIFI_DHCP_CACHE_REMOVE) {
259 DhcpResultStoreManager::GetInstance().RemoveCachedIp(cacheInfo);
260 }
261 return DHCP_E_SUCCESS;
262 #endif
263 }
264
StartOldClient(const RouterConfig & config,DhcpClient & dhcpClient)265 ErrCode DhcpClientServiceImpl::StartOldClient(const RouterConfig &config, DhcpClient &dhcpClient)
266 {
267 DHCP_LOGI("StartOldClient ifname:%{public}s bIpv6:%{public}d", config.ifname.c_str(), config.bIpv6);
268 ErrCode ret = DHCP_E_SUCCESS;
269 const std::string ifname = config.ifname;
270 if (config.bIpv6) {
271 if (dhcpClient.pipv6Client == nullptr) {
272 ret = StartNewIpv6Client(config, dhcpClient);
273 } else {
274 #ifndef OHOS_ARCH_LITE
275 NetManagerStandard::NetsysController::GetInstance().SetIpv6PrivacyExtensions(ifname, DHCP_IPV6_ENABLE);
276 NetManagerStandard::NetsysController::GetInstance().SetEnableIpv6(ifname, DHCP_IPV6_ENABLE);
277 #endif
278 dhcpClient.pipv6Client->Reset();
279 dhcpClient.pipv6Client->SetCallback(
280 [this](const std::string ifname, DhcpIpv6Info &info) { this->DhcpIpv6ResulCallback(ifname, info); });
281 dhcpClient.pipv6Client->StartIpv6Thread(ifname, config.bIpv6);
282 }
283 }
284 if (ret != DHCP_E_SUCCESS) {
285 return ret;
286 }
287 if (config.bIpv4) {
288 if (dhcpClient.pStaStateMachine == nullptr) {
289 ret = StartNewIpv4Client(config, dhcpClient);
290 } else {
291 dhcpClient.pStaStateMachine->SetConfiguration(config);
292 if (!config.isStaticIpv4) {
293 dhcpClient.pStaStateMachine->StartIpv4Type(ifname, config.bIpv6, ACTION_START_OLD);
294 }
295 }
296 }
297 if (ret != DHCP_E_SUCCESS) {
298 return ret;
299 }
300 return DHCP_E_SUCCESS;
301 }
302
StartNewIpv4Client(const RouterConfig & config,DhcpClient & client)303 ErrCode DhcpClientServiceImpl::StartNewIpv4Client(const RouterConfig &config, DhcpClient &client)
304 {
305 DHCP_LOGI("StartNewIpv4Client ifname:%{public}s", config.ifname.c_str());
306 const std::string ifname = config.ifname;
307 DhcpClientStateMachine *pStaState = new (std::nothrow)DhcpClientStateMachine(ifname);
308 if (pStaState == nullptr) {
309 DHCP_LOGI("StartNewIpv4Client ifname:%{public}s", config.ifname.c_str());
310 DHCP_LOGE("StartNewIpv4Client new DhcpClientStateMachine failed!, ifname:%{public}s", ifname.c_str());
311 return DHCP_E_FAILED;
312 }
313 client.pStaStateMachine = pStaState;
314 pStaState->SetConfiguration(config);
315 if (!config.isStaticIpv4) {
316 pStaState->StartIpv4Type(ifname, config.bIpv6, ACTION_START_NEW);
317 }
318 return DHCP_E_SUCCESS;
319 }
320
StartNewIpv6Client(const RouterConfig & config,DhcpClient & client)321 ErrCode DhcpClientServiceImpl::StartNewIpv6Client(const RouterConfig &config, DhcpClient &client)
322 {
323 DHCP_LOGI("StartNewIpv6Client ifname:%{public}s", config.ifname.c_str());
324 const std::string ifname = config.ifname;
325 DhcpIpv6Client *pipv6Client = new (std::nothrow)DhcpIpv6Client(ifname);
326 if (pipv6Client == nullptr) {
327 DHCP_LOGE("StartNewIpv6Client new DhcpIpv6Client failed!, ifname:%{public}s", ifname.c_str());
328 return DHCP_E_FAILED;
329 }
330 client.pipv6Client = pipv6Client;
331 DHCP_LOGI("StartNewClient new DhcpIpv6Client, ifname:%{public}s, bIpv6:%{public}d", ifname.c_str(),
332 config.bIpv6);
333 #ifndef OHOS_ARCH_LITE
334 NetManagerStandard::NetsysController::GetInstance().SetIpv6PrivacyExtensions(ifname, DHCP_IPV6_ENABLE);
335 NetManagerStandard::NetsysController::GetInstance().SetEnableIpv6(ifname, DHCP_IPV6_ENABLE);
336 #endif
337 pipv6Client->Reset();
338 pipv6Client->SetCallback(
339 [this](const std::string ifname, DhcpIpv6Info &info) { this->DhcpIpv6ResulCallback(ifname, info); });
340 pipv6Client->StartIpv6Thread(ifname, config.bIpv6);
341 return DHCP_E_SUCCESS;
342 }
343
344
StartNewClient(const RouterConfig & config)345 ErrCode DhcpClientServiceImpl::StartNewClient(const RouterConfig &config)
346 {
347 DHCP_LOGI("StartNewClient ifname:%{public}s, bIpv6:%{public}d", config.ifname.c_str(), config.bIpv6);
348 DhcpClient client;
349 ErrCode ret = DHCP_E_SUCCESS;
350 const std::string ifname = config.ifname;
351 if (config.bIpv6) {
352 client.isIpv6 = config.bIpv6;
353 ret = StartNewIpv6Client(config, client);
354 }
355 if (ret != DHCP_E_SUCCESS) {
356 return ret;
357 }
358 if (config.bIpv4) {
359 ret = StartNewIpv4Client(config, client);
360 }
361 if (ret != DHCP_E_SUCCESS) {
362 return ret;
363 }
364 client.ifName = ifname;
365 {
366 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
367 m_mapClientService.emplace(std::make_pair(ifname, client));
368 }
369 return DHCP_E_SUCCESS;
370 }
371
372
StopDhcpIpv4Client(const std::string & ifname)373 ErrCode DhcpClientServiceImpl::StopDhcpIpv4Client(const std::string& ifname)
374 {
375 {
376 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
377 auto iter2 = m_mapClientService.find(ifname);
378 if (iter2 != m_mapClientService.end()) {
379 if ((iter2->second).pStaStateMachine != nullptr) {
380 DHCP_LOGI("StopDhcpClient pStaStateMachine StopIpv4, ifname:%{public}s", ifname.c_str());
381 (iter2->second).pStaStateMachine->StopIpv4();
382 (iter2->second).pStaStateMachine->CloseAllRenewTimer();
383 }
384 }
385 }
386 {
387 std::lock_guard<std::mutex> autoLock(m_dhcpResultMutex);
388 auto iter = m_mapDhcpResult.find(ifname);
389 if (iter != m_mapDhcpResult.end()) {
390 DHCP_LOGI("m_mapDhcpResult erase ifName:%{public}s", ifname.c_str());
391 for (auto result = iter->second.begin(); result != iter->second.end();) {
392 if (result->iptype == 0) {
393 result = iter->second.erase(result);
394 } else {
395 result ++;
396 }
397 }
398 if (iter->second.empty()) {
399 m_mapDhcpResult.erase(iter);
400 }
401 }
402 }
403 return DHCP_E_SUCCESS;
404 }
405
StopDhcpIpv6Client(const std::string & ifname)406 ErrCode DhcpClientServiceImpl::StopDhcpIpv6Client(const std::string& ifname)
407 {
408 {
409 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
410 auto iter = m_mapClientCallBack.find(ifname);
411 if (iter != m_mapClientCallBack.end()) {
412 m_mapClientCallBack.erase(iter);
413 DHCP_LOGI("StopDhcpClient erase ClientCallBack ifName:%{public}s", ifname.c_str());
414 }
415 }
416 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
417 auto iter2 = m_mapClientService.find(ifname);
418 if (iter2 != m_mapClientService.end()) {
419 if ((iter2->second).pipv6Client != nullptr) {
420 DHCP_LOGI("StopDhcpClient pipv6Client DhcpIPV6Stop, ifname:%{public}s", ifname.c_str());
421 (iter2->second).pipv6Client->DhcpIPV6Stop();
422 #ifndef OHOS_ARCH_LITE
423 NetManagerStandard::NetsysController::GetInstance().SetEnableIpv6(ifname, DHCP_IPV6_DISENABLE);
424 #endif
425 }
426 }
427 {
428 std::lock_guard<std::mutex> autoLock(m_dhcpResultMutex);
429 auto iter = m_mapDhcpResult.find(ifname);
430 if (iter != m_mapDhcpResult.end()) {
431 DHCP_LOGI("m_mapDhcpResult erase ifName:%{public}s", ifname.c_str());
432 for (auto result = iter->second.begin(); result != iter->second.end();) {
433 if (result->iptype == 1) {
434 result = iter->second.erase(result);
435 } else {
436 result++;
437 }
438 }
439 if (iter->second.empty()) {
440 m_mapDhcpResult.erase(iter);
441 }
442 }
443 }
444 return DHCP_E_SUCCESS;
445 }
446
StopDhcpClient(const std::string & ifname,bool bIpv6,bool bIpv4)447 ErrCode DhcpClientServiceImpl::StopDhcpClient(const std::string& ifname, bool bIpv6, bool bIpv4)
448 {
449 DHCP_LOGI("StopDhcpClient ifName:%{public}s, bIpv6:%{public}d, bIpv4:%{public}d", ifname.c_str(), bIpv6, bIpv4);
450 if (!DhcpPermissionUtils::VerifyIsNativeProcess()) {
451 DHCP_LOGE("StopDhcpClient:NOT NATIVE PROCESS, PERMISSION_DENIED!");
452 return DHCP_E_PERMISSION_DENIED;
453 }
454 if (!DhcpPermissionUtils::VerifyDhcpNetworkPermission("ohos.permission.NETWORK_DHCP")) {
455 DHCP_LOGE("StopDhcpClient:VerifyDhcpNetworkPermission PERMISSION_DENIED!");
456 return DHCP_E_PERMISSION_DENIED;
457 }
458 if (ifname.empty()) {
459 DHCP_LOGE("StopDhcpClient ifname is empty!");
460 return DHCP_E_FAILED;
461 }
462 if (bIpv6) {
463 StopDhcpIpv6Client(ifname);
464 }
465 if (bIpv4) {
466 StopDhcpIpv4Client(ifname);
467 }
468
469 return DHCP_E_SUCCESS;
470 }
471
StopClientSa(void)472 ErrCode DhcpClientServiceImpl::StopClientSa(void)
473 {
474 if (!DhcpPermissionUtils::VerifyIsNativeProcess()) {
475 DHCP_LOGE("StopDhcpClient:NOT NATIVE PROCESS, PERMISSION_DENIED!");
476 return DHCP_E_PERMISSION_DENIED;
477 }
478 if (!DhcpPermissionUtils::VerifyDhcpNetworkPermission("ohos.permission.NETWORK_DHCP")) {
479 DHCP_LOGE("StopClientSa:VerifyDhcpNetworkPermission PERMISSION_DENIED!");
480 return DHCP_E_PERMISSION_DENIED;
481 }
482 #ifdef OHOS_ARCH_LITE
483 return DHCP_E_SUCCESS;
484 #else
485 #ifndef OHOS_EUPDATER
486 return DhcpSaLoadManager::GetInstance().UnloadWifiSa(DHCP_CLIENT_ABILITY_ID);
487 #else
488 return DHCP_E_SUCCESS;
489 #endif
490 #endif
491 }
492
DhcpIpv4ResultSuccess(struct DhcpIpResult & ipResult)493 int DhcpClientServiceImpl::DhcpIpv4ResultSuccess(struct DhcpIpResult &ipResult)
494 {
495 std::string ifname = ipResult.ifname;
496 OHOS::DHCP::DhcpResult result;
497 result.iptype = 0;
498 result.isOptSuc = true;
499 result.uGetTime = (uint32_t)time(NULL);
500 result.uAddTime = ipResult.uAddTime;
501 result.uLeaseTime = ipResult.uOptLeasetime;
502 result.strYourCli = ipResult.strYiaddr;
503 result.strServer = ipResult.strOptServerId;
504 result.strSubnet = ipResult.strOptSubnet;
505 result.strDns1 = ipResult.strOptDns1;
506 result.strDns2 = ipResult.strOptDns2;
507 result.strRouter1 = ipResult.strOptRouter1;
508 result.strRouter2 = ipResult.strOptRouter2;
509 result.strVendor = ipResult.strOptVendor;
510 for (std::vector<std::string>::iterator it = ipResult.dnsAddr.begin(); it != ipResult.dnsAddr.end(); it++) {
511 result.vectorDnsAddr.push_back(*it);
512 }
513 DHCP_LOGI("DhcpIpv4ResultSuccess %{public}s, %{public}d, opt:%{public}d, cli:%{private}s, server:%{private}s, "
514 "Subnet:%{private}s, Dns1:%{private}s, Dns2:%{private}s, Router1:%{private}s, Router2:%{private}s, "
515 "strVendor:%{public}s, uLeaseTime:%{public}u, uAddTime:%{public}u, uGetTime:%{public}u.",
516 ifname.c_str(), result.iptype, result.isOptSuc, result.strYourCli.c_str(), result.strServer.c_str(),
517 result.strSubnet.c_str(), result.strDns1.c_str(), result.strDns2.c_str(), result.strRouter1.c_str(),
518 result.strRouter2.c_str(), result.strVendor.c_str(), result.uLeaseTime, result.uAddTime, result.uGetTime);
519
520 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
521 auto iter = m_mapClientCallBack.find(ifname);
522 if (iter == m_mapClientCallBack.end()) {
523 DHCP_LOGE("DhcpIpv4ResultSuccess m_mapClientCallBack not find callback!");
524 return DHCP_OPT_FAILED;
525 }
526 if ((iter->second) == nullptr) {
527 DHCP_LOGE("DhcpIpv4ResultSuccess mclientCallback is nullptr!");
528 return DHCP_OPT_FAILED;
529 }
530 if (CheckDhcpResultExist(ifname, result)) {
531 DHCP_LOGI("DhcpIpv4ResultSuccess DhcpResult %{public}s equal new addtime %{public}u, no need update.",
532 ifname.c_str(), result.uAddTime);
533 return DHCP_OPT_SUCCESS;
534 }
535 PushDhcpResult(ifname, result);
536 (iter->second)->OnIpSuccessChanged(DHCP_OPT_SUCCESS, ifname, result);
537 return DHCP_OPT_SUCCESS;
538 }
539 #ifndef OHOS_ARCH_LITE
DhcpOfferResultSuccess(struct DhcpIpResult & ipResult)540 int DhcpClientServiceImpl::DhcpOfferResultSuccess(struct DhcpIpResult &ipResult)
541 {
542 std::string ifname = ipResult.ifname;
543 OHOS::DHCP::DhcpResult result;
544 result.iptype = 0;
545 result.isOptSuc = true;
546 result.uGetTime = static_cast<uint32_t>(time(NULL));
547 result.uAddTime = ipResult.uAddTime;
548 result.uLeaseTime = ipResult.uOptLeasetime;
549 result.strYourCli = ipResult.strYiaddr;
550 result.strServer = ipResult.strOptServerId;
551 result.strSubnet = ipResult.strOptSubnet;
552 result.strDns1 = ipResult.strOptDns1;
553 result.strDns2 = ipResult.strOptDns2;
554 result.strRouter1 = ipResult.strOptRouter1;
555 result.strRouter2 = ipResult.strOptRouter2;
556 result.strVendor = ipResult.strOptVendor;
557 for (std::vector<std::string>::iterator it = ipResult.dnsAddr.begin(); it != ipResult.dnsAddr.end(); it++) {
558 result.vectorDnsAddr.push_back(*it);
559 }
560
561 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
562 auto iter = m_mapClientCallBack.find(ifname);
563 if (iter == m_mapClientCallBack.end()) {
564 DHCP_LOGE("OnDhcpOfferReport m_mapClientCallBack not find callback!");
565 return DHCP_OPT_FAILED;
566 }
567 if ((iter->second) == nullptr) {
568 DHCP_LOGE("OnDhcpOfferReport mclientCallback is nullptr!");
569 return DHCP_OPT_FAILED;
570 }
571 (iter->second)->OnDhcpOfferReport(0, ifname, result);
572 return DHCP_OPT_SUCCESS;
573 }
574 #endif
DhcpIpv4ResultFail(struct DhcpIpResult & ipResult)575 int DhcpClientServiceImpl::DhcpIpv4ResultFail(struct DhcpIpResult &ipResult)
576 {
577 std::string ifname = ipResult.ifname;
578 OHOS::DHCP::DhcpResult result;
579 result.iptype = 0;
580 result.isOptSuc = false;
581 result.uGetTime = (uint32_t)time(NULL);
582 result.uAddTime = ipResult.uAddTime;
583
584 PushDhcpResult(ifname, result);
585 DHCP_LOGI("DhcpIpv4ResultFail ifname:%{public}s result.isOptSuc:false!", ifname.c_str());
586 ActionMode action = ACTION_INVALID;
587 {
588 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
589 auto iterlient = m_mapClientService.find(ifname);
590 if (iterlient != m_mapClientService.end() && ((iterlient->second).pStaStateMachine != nullptr)) {
591 action = (iterlient->second).pStaStateMachine->GetAction();
592 }
593 }
594 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
595 auto iter = m_mapClientCallBack.find(ifname);
596 if (iter == m_mapClientCallBack.end()) {
597 DHCP_LOGE("DhcpIpv4ResultFail m_mapClientCallBack not find callback!");
598 return DHCP_OPT_FAILED;
599 }
600 if ((iter->second) == nullptr) {
601 DHCP_LOGE("DhcpIpv4ResultFail mclientCallback == nullptr!");
602 return DHCP_OPT_FAILED;
603 }
604 if ((action == ACTION_RENEW_T1) || (action == ACTION_RENEW_T2) || (action == ACTION_RENEW_T3)) {
605 (iter->second)->OnIpFailChanged(DHCP_OPT_RENEW_FAILED, ifname.c_str(), "get dhcp renew result failed!");
606 } else {
607 (iter->second)->OnIpFailChanged(DHCP_OPT_FAILED, ifname.c_str(), "get dhcp ip result failed!");
608 }
609 DHCP_LOGI("DhcpIpv4ResultFail OnIpFailChanged!, action:%{public}d", action);
610 return DHCP_OPT_SUCCESS;
611 }
612
DhcpIpv4ResultTimeOut(const std::string & ifname)613 int DhcpClientServiceImpl::DhcpIpv4ResultTimeOut(const std::string &ifname)
614 {
615 DHCP_LOGI("DhcpIpv4ResultTimeOut ifname:%{public}s", ifname.c_str());
616 ActionMode action = ACTION_INVALID;
617 {
618 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
619 auto iterlient = m_mapClientService.find(ifname);
620 if (iterlient != m_mapClientService.end() && ((iterlient->second).pStaStateMachine != nullptr)) {
621 action = (iterlient->second).pStaStateMachine->GetAction();
622 }
623 }
624 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
625 auto iter = m_mapClientCallBack.find(ifname);
626 if (iter == m_mapClientCallBack.end()) {
627 DHCP_LOGE("DhcpIpv4ResultTimeOut m_mapClientCallBack not find callback!");
628 return DHCP_OPT_FAILED;
629 }
630 if ((iter->second) == nullptr) {
631 DHCP_LOGE("DhcpIpv4ResultTimeOut mclientCallback == nullptr!");
632 return DHCP_OPT_FAILED;
633 }
634 if ((action == ACTION_RENEW_T1) || (action == ACTION_RENEW_T2) || (action == ACTION_RENEW_T2)) {
635 (iter->second)->OnIpFailChanged(DHCP_OPT_RENEW_TIMEOUT, ifname.c_str(), "get dhcp renew result timeout!");
636 } else {
637 (iter->second)->OnIpFailChanged(DHCP_OPT_TIMEOUT, ifname.c_str(), "get dhcp result timeout!");
638 }
639 DHCP_LOGI("DhcpIpv4ResultTimeOut OnIpFailChanged Timeout!, action:%{public}d", action);
640 return DHCP_OPT_SUCCESS;
641 }
642
DhcpIpv4ResultExpired(const std::string & ifname)643 int DhcpClientServiceImpl::DhcpIpv4ResultExpired(const std::string &ifname)
644 {
645 DHCP_LOGI("DhcpIpv4ResultExpired ifname:%{public}s", ifname.c_str());
646 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
647 auto iter = m_mapClientCallBack.find(ifname);
648 if (iter == m_mapClientCallBack.end()) {
649 DHCP_LOGE("DhcpIpv4ResultExpired not find ifname callback!");
650 return DHCP_OPT_FAILED;
651 }
652 if ((iter->second) == nullptr) {
653 DHCP_LOGE("DhcpIpv4ResultExpired callback is nullptr!");
654 return DHCP_OPT_FAILED;
655 }
656 (iter->second)->OnIpFailChanged(DHCP_OPT_LEASE_EXPIRED, ifname.c_str(), "ifname ip lease expired!");
657 DHCP_LOGI("DhcpIpv4ResultExpired OnIpFailChanged Lease Expired!");
658 return DHCP_OPT_SUCCESS;
659 }
660
DhcpIpv6ResulCallback(const std::string ifname,DhcpIpv6Info & info)661 void DhcpClientServiceImpl::DhcpIpv6ResulCallback(const std::string ifname, DhcpIpv6Info &info)
662 {
663 OHOS::DHCP::DhcpResult result;
664 result.uAddTime = (uint32_t)time(NULL);
665 result.iptype = 1;
666 result.isOptSuc = true;
667 result.uGetTime = (uint32_t)time(NULL);
668 result.strYourCli = info.globalIpv6Addr;
669 result.strSubnet = info.ipv6SubnetAddr;
670 result.strRouter1 = info.routeAddr;
671 result.strDns1 = info.dnsAddr;
672 result.strDns2 = info.dnsAddr2;
673 result.strRouter2 = "*";
674 result.strLinkIpv6Addr = info.linkIpv6Addr;
675 result.strRandIpv6Addr = info.randIpv6Addr;
676 result.strLocalAddr1 = info.uniqueLocalAddr1;
677 result.strLocalAddr2 = info.uniqueLocalAddr2;
678 for (auto dnsAddr : info.vectorDnsAddr) {
679 result.vectorDnsAddr.push_back(dnsAddr);
680 }
681
682 PushDhcpResult(ifname, result);
683 DHCP_LOGI("DhcpIpv6ResulCallback %{public}s, %{public}d, opt:%{public}d, cli:%{public}s, server:%{public}s, "
684 "Subnet:%{public}s, Dns1:%{public}s, Dns2:%{public}s, Router1:%{public}s, Router2:%{public}s, "
685 "strVendor:%{public}s, strLinkIpv6Addr:%{public}s, strRandIpv6Addr:%{public}s, uLeaseTime:%{public}u, "
686 "uAddTime:%{public}u, uGetTime:%{public}u.",
687 ifname.c_str(), result.iptype, result.isOptSuc, Ipv6Anonymize(result.strYourCli).c_str(),
688 Ipv6Anonymize(result.strServer).c_str(), Ipv6Anonymize(result.strSubnet).c_str(),
689 Ipv6Anonymize(result.strDns1).c_str(), Ipv6Anonymize(result.strDns2).c_str(),
690 Ipv6Anonymize(result.strRouter1).c_str(), Ipv6Anonymize(result.strRouter2).c_str(),
691 Ipv6Anonymize(result.strVendor).c_str(), Ipv6Anonymize(result.strLinkIpv6Addr).c_str(),
692 Ipv6Anonymize(result.strRandIpv6Addr).c_str(), result.uLeaseTime, result.uAddTime, result.uGetTime);
693 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
694 auto iter = m_mapClientCallBack.find(ifname);
695 if (iter == m_mapClientCallBack.end()) {
696 DHCP_LOGE("DhcpIpv6ResulCallback m_mapClientCallBack not find callback!");
697 return;
698 }
699 if ((iter->second) == nullptr) {
700 DHCP_LOGE("DhcpIpv6ResulCallback mclientCallback == nullptr!");
701 return;
702 }
703 (iter->second)->OnIpSuccessChanged(PUBLISH_CODE_SUCCESS, ifname, result);
704 DHCP_LOGI("DhcpIpv6ResulCallback OnIpSuccessChanged");
705 }
706
DhcpIpv6ResultTimeOut(const std::string & ifname)707 int DhcpClientServiceImpl::DhcpIpv6ResultTimeOut(const std::string &ifname)
708 {
709 DHCP_LOGI("DhcpIpv6ResultTimeOut ifname:%{public}s", ifname.c_str());
710 DhcpFreeIpv6(ifname);
711 return DHCP_OPT_SUCCESS;
712 }
713
DhcpFreeIpv6(const std::string ifname)714 int DhcpClientServiceImpl::DhcpFreeIpv6(const std::string ifname)
715 {
716 DHCP_LOGI("DhcpFreeIpv6 ifname:%{public}s", ifname.c_str());
717 std::lock_guard<std::mutex> autoLockServer(m_clientServiceMutex);
718 auto iter = m_mapClientService.find(ifname);
719 if (iter != m_mapClientService.end()) {
720 if ((iter->second).pipv6Client != nullptr) {
721 (iter->second).pipv6Client->DhcpIPV6Stop();
722 }
723 }
724 return DHCP_OPT_SUCCESS;
725 }
726
PushDhcpResult(const std::string & ifname,OHOS::DHCP::DhcpResult & result)727 void DhcpClientServiceImpl::PushDhcpResult(const std::string &ifname, OHOS::DHCP::DhcpResult &result)
728 {
729 std::lock_guard<std::mutex> autoLock(m_dhcpResultMutex);
730 auto iterResult = m_mapDhcpResult.find(ifname);
731 if (iterResult != m_mapDhcpResult.end()) {
732 for (size_t i = 0; i < iterResult->second.size(); i++) {
733 if (iterResult->second[i].iptype != result.iptype) {
734 continue;
735 }
736 if (iterResult->second[i].iptype == 0) { // 0-ipv4
737 if (iterResult->second[i].uAddTime != result.uAddTime) {
738 iterResult->second[i] = result;
739 DHCP_LOGI("PushDhcpResult update ipv4 result, ifname:%{public}s", ifname.c_str());
740 }
741 } else { // 1-ipv6
742 DHCP_LOGI("PushDhcpResult update ipv6 result, ifname:%{public}s", ifname.c_str());
743 iterResult->second[i] = result;
744 }
745 return;
746 }
747 DHCP_LOGI("PushDhcpResult ifname add new result, ifname:%{public}s", ifname.c_str());
748 iterResult->second.push_back(result);
749 } else {
750 std::vector<OHOS::DHCP::DhcpResult> results;
751 results.push_back(result);
752 m_mapDhcpResult.emplace(std::make_pair(ifname, results));
753 DHCP_LOGI("PushDhcpResult add new ifname result, ifname:%{public}s", ifname.c_str());
754 }
755 }
756
CheckDhcpResultExist(const std::string & ifname,OHOS::DHCP::DhcpResult & result)757 bool DhcpClientServiceImpl::CheckDhcpResultExist(const std::string &ifname, OHOS::DHCP::DhcpResult &result)
758 {
759 bool exist = false;
760 std::lock_guard<std::mutex> autoLock(m_dhcpResultMutex);
761 auto iterResult = m_mapDhcpResult.find(ifname);
762 if (iterResult != m_mapDhcpResult.end()) {
763 for (size_t i = 0; i < iterResult->second.size(); i++) {
764 if (iterResult->second[i].iptype != result.iptype) {
765 continue;
766 }
767 if (iterResult->second[i].uAddTime == result.uAddTime) {
768 exist = true;
769 break;
770 }
771 }
772 }
773 return exist;
774 }
775
IsRemoteDied(void)776 bool DhcpClientServiceImpl::IsRemoteDied(void)
777 {
778 DHCP_LOGD("IsRemoteDied");
779 return true;
780 }
781
IsGlobalIPv6Address(std::string ipAddress)782 bool DhcpClientServiceImpl::IsGlobalIPv6Address(std::string ipAddress)
783 {
784 const char* ipAddr = ipAddress.c_str();
785 int first = ipAddr[0]-'0';
786 DHCP_LOGI("first = %{public}d", first);
787 if (first == NUMBER_TWO || first == NUMBER_THREE) {
788 return true;
789 }
790 return false;
791 }
792 }
793 }