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 = new (std::nothrow) DhcpClientServiceImpl;
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", config.ifname.c_str(), config.bIpv6);
198 if (!DhcpPermissionUtils::VerifyIsNativeProcess()) {
199 DHCP_LOGE("StartDhcpClient:NOT NATIVE PROCESS, PERMISSION_DENIED!");
200 return DHCP_E_PERMISSION_DENIED;
201 }
202 if (!DhcpPermissionUtils::VerifyDhcpNetworkPermission("ohos.permission.NETWORK_DHCP")) {
203 DHCP_LOGE("StartDhcpClient:VerifyDhcpNetworkPermission PERMISSION_DENIED!");
204 return DHCP_E_PERMISSION_DENIED;
205 }
206 if (config.ifname.empty()) {
207 DHCP_LOGE("StartDhcpClient ifname is empty!");
208 return DHCP_E_FAILED;
209 }
210 RouterCfg innerCfg;
211 innerCfg.ifname = config.ifname;
212 innerCfg.bssid = config.bssid;
213 innerCfg.prohibitUseCacheIp = config.prohibitUseCacheIp;
214 innerCfg.bIpv6 = config.bIpv6;
215 innerCfg.bSpecificNetwork = config.bSpecificNetwork;
216 {
217 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
218 auto iter = m_mapClientService.find(innerCfg.ifname);
219 if (iter != m_mapClientService.end()) {
220 return StartOldClient(innerCfg, iter->second);
221 }
222 }
223 return StartNewClient(innerCfg);
224 }
225
DealWifiDhcpCache(int32_t cmd,const IpCacheInfo & ipCacheInfo)226 ErrCode DhcpClientServiceImpl::DealWifiDhcpCache(int32_t cmd, const IpCacheInfo &ipCacheInfo)
227 {
228 DHCP_LOGI("DealWifiDhcpCache enter");
229 if (!DhcpPermissionUtils::VerifyIsNativeProcess()) {
230 DHCP_LOGE("DealWifiDhcpCache:NOT NATIVE PROCESS, PERMISSION_DENIED!");
231 return DHCP_E_PERMISSION_DENIED;
232 }
233 if (!DhcpPermissionUtils::VerifyDhcpNetworkPermission("ohos.permission.SET_WIFI_CONFIG")) {
234 DHCP_LOGE("DealWifiDhcpCache:VerifyDhcpNetworkPermission PERMISSION_DENIED!");
235 return DHCP_E_PERMISSION_DENIED;
236 }
237 IpInfoCached cacheInfo;
238 cacheInfo.ssid = ipCacheInfo.ssid;
239 cacheInfo.bssid = ipCacheInfo.bssid;
240 #ifndef OHOS_ARCH_LITE
241 std::function<void()> func;
242 if (cmd == WIFI_DHCP_CACHE_ADD) {
243 func = [cacheInfo]() { DhcpResultStoreManager::GetInstance().AddCachedIp(cacheInfo); };
244 } else if (cmd == WIFI_DHCP_CACHE_REMOVE) {
245 func = [cacheInfo]() { DhcpResultStoreManager::GetInstance().RemoveCachedIp(cacheInfo); };
246 } else {
247 return DHCP_E_FAILED;
248 }
249 uint32_t taskId = 0;
250 DhcpTimer::GetInstance()->Register(func, taskId, 0);
251 return (taskId > 0) ? DHCP_E_SUCCESS : DHCP_E_FAILED;
252 #else
253 if (cmd == WIFI_DHCP_CACHE_ADD) {
254 DhcpResultStoreManager::GetInstance().AddCachedIp(cacheInfo);
255 } else if (cmd == WIFI_DHCP_CACHE_REMOVE) {
256 DhcpResultStoreManager::GetInstance().RemoveCachedIp(cacheInfo);
257 }
258 return DHCP_E_SUCCESS;
259 #endif
260 }
261
StartOldClient(const RouterCfg & config,DhcpClient & dhcpClient)262 ErrCode DhcpClientServiceImpl::StartOldClient(const RouterCfg &config, DhcpClient &dhcpClient)
263 {
264 DHCP_LOGI("StartOldClient ifname:%{public}s bIpv6:%{public}d", config.ifname.c_str(), config.bIpv6);
265 if (dhcpClient.pStaStateMachine == nullptr) {
266 DHCP_LOGE("StartOldClient pStaStateMachine is null!");
267 return DHCP_E_FAILED;
268 }
269 const std::string ifname = config.ifname;
270 dhcpClient.pStaStateMachine->SetConfiguration(config);
271 dhcpClient.pStaStateMachine->StartIpv4Type(ifname, config.bIpv6, ACTION_START_OLD);
272 if (config.bIpv6) {
273 if (dhcpClient.pipv6Client == nullptr) {
274 DHCP_LOGE("StartOldClient pipv6Client is null!");
275 DhcpIpv6Client *pipv6Client = new (std::nothrow)DhcpIpv6Client(ifname);
276 if (pipv6Client == nullptr) {
277 DHCP_LOGE("StartOldClient new DhcpIpv6Client failed!, ifname:%{public}s", ifname.c_str());
278 return DHCP_E_FAILED;
279 }
280 dhcpClient.pipv6Client = pipv6Client;
281 DHCP_LOGI("StartOldClient new DhcpIpv6Client, ifname:%{public}s, bIpv6:%{public}d",
282 ifname.c_str(), config.bIpv6);
283 }
284 #ifndef OHOS_ARCH_LITE
285 NetManagerStandard::NetsysController::GetInstance().SetIpv6PrivacyExtensions(ifname, DHCP_IPV6_ENABLE);
286 NetManagerStandard::NetsysController::GetInstance().SetEnableIpv6(ifname, DHCP_IPV6_ENABLE);
287 #endif
288 dhcpClient.pipv6Client->Reset();
289 dhcpClient.pipv6Client->SetCallback(
290 [this](const std::string ifname, DhcpIpv6Info &info) { this->DhcpIpv6ResulCallback(ifname, info); });
291 dhcpClient.pipv6Client->StartIpv6Thread(ifname, config.bIpv6);
292 }
293 return DHCP_E_SUCCESS;
294 }
295
StartNewClient(const RouterCfg & config)296 ErrCode DhcpClientServiceImpl::StartNewClient(const RouterCfg &config)
297 {
298 DHCP_LOGI("StartNewClient ifname:%{public}s, bIpv6:%{public}d", config.ifname.c_str(), config.bIpv6);
299 DhcpClient client;
300 const std::string ifname = config.ifname;
301 if (config.bIpv6) {
302 DhcpIpv6Client *pipv6Client = new (std::nothrow)DhcpIpv6Client(ifname);
303 if (pipv6Client == nullptr) {
304 DHCP_LOGE("StartNewClient new DhcpIpv6Client failed!, ifname:%{public}s", ifname.c_str());
305 return DHCP_E_FAILED;
306 }
307 client.pipv6Client = pipv6Client;
308 DHCP_LOGI("StartNewClient new DhcpIpv6Client, ifname:%{public}s, bIpv6:%{public}d", ifname.c_str(),
309 config.bIpv6);
310 #ifndef OHOS_ARCH_LITE
311 NetManagerStandard::NetsysController::GetInstance().SetIpv6PrivacyExtensions(ifname, DHCP_IPV6_ENABLE);
312 NetManagerStandard::NetsysController::GetInstance().SetEnableIpv6(ifname, DHCP_IPV6_ENABLE);
313 #endif
314 pipv6Client->Reset();
315 pipv6Client->SetCallback(
316 [this](const std::string ifname, DhcpIpv6Info &info) { this->DhcpIpv6ResulCallback(ifname, info); });
317 pipv6Client->StartIpv6Thread(ifname, config.bIpv6);
318 }
319 DhcpClientStateMachine *pStaState = new (std::nothrow)DhcpClientStateMachine(ifname);
320 if (pStaState == nullptr) {
321 DHCP_LOGE("StartNewClient new DhcpClientStateMachine failed!, ifname:%{public}s", ifname.c_str());
322 return DHCP_E_FAILED;
323 }
324 client.ifName = ifname;
325 client.isIpv6 = config.bIpv6;
326 client.pStaStateMachine = pStaState;
327 {
328 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
329 m_mapClientService.emplace(std::make_pair(ifname, client));
330 }
331 DHCP_LOGI("StartNewClient new DhcpClientStateMachine, ifname:%{public}s, bIpv6:%{public}d",
332 ifname.c_str(), config.bIpv6);
333 pStaState->SetConfiguration(config);
334 pStaState->StartIpv4Type(ifname, config.bIpv6, ACTION_START_NEW);
335 return DHCP_E_SUCCESS;
336 }
337
StopDhcpClient(const std::string & ifname,bool bIpv6)338 ErrCode DhcpClientServiceImpl::StopDhcpClient(const std::string& ifname, bool bIpv6)
339 {
340 DHCP_LOGI("StopDhcpClient ifName:%{public}s, bIpv6:%{public}d", ifname.c_str(), bIpv6);
341 if (!DhcpPermissionUtils::VerifyIsNativeProcess()) {
342 DHCP_LOGE("StopDhcpClient:NOT NATIVE PROCESS, PERMISSION_DENIED!");
343 return DHCP_E_PERMISSION_DENIED;
344 }
345 if (!DhcpPermissionUtils::VerifyDhcpNetworkPermission("ohos.permission.NETWORK_DHCP")) {
346 DHCP_LOGE("StopDhcpClient:VerifyDhcpNetworkPermission PERMISSION_DENIED!");
347 return DHCP_E_PERMISSION_DENIED;
348 }
349 if (ifname.empty()) {
350 DHCP_LOGE("StopDhcpClient ifname is empty!");
351 return DHCP_E_FAILED;
352 }
353 {
354 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
355 auto iter = m_mapClientCallBack.find(ifname);
356 if (iter != m_mapClientCallBack.end()) {
357 m_mapClientCallBack.erase(iter);
358 DHCP_LOGI("StopDhcpClient erase ClientCallBack ifName:%{public}s", ifname.c_str());
359 }
360 }
361 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
362 auto iter2 = m_mapClientService.find(ifname);
363 if (iter2 != m_mapClientService.end()) {
364 if ((iter2->second).pStaStateMachine != nullptr) {
365 DHCP_LOGI("StopDhcpClient pStaStateMachine StopIpv4, ifname:%{public}s, bIpv6:%{public}d", ifname.c_str(),
366 bIpv6);
367 (iter2->second).pStaStateMachine->StopIpv4();
368 (iter2->second).pStaStateMachine->CloseAllRenewTimer();
369 }
370 if ((iter2->second).pipv6Client != nullptr) {
371 DHCP_LOGI("StopDhcpClient pipv6Client DhcpIPV6Stop, ifname:%{public}s, bIpv6:%{public}d", ifname.c_str(),
372 bIpv6);
373 (iter2->second).pipv6Client->DhcpIPV6Stop();
374 #ifndef OHOS_ARCH_LITE
375 NetManagerStandard::NetsysController::GetInstance().SetEnableIpv6(ifname, DHCP_IPV6_DISENABLE);
376 #endif
377 }
378 }
379 return DHCP_E_SUCCESS;
380 }
381
StopClientSa(void)382 ErrCode DhcpClientServiceImpl::StopClientSa(void)
383 {
384 if (!DhcpPermissionUtils::VerifyIsNativeProcess()) {
385 DHCP_LOGE("StopDhcpClient:NOT NATIVE PROCESS, PERMISSION_DENIED!");
386 return DHCP_E_PERMISSION_DENIED;
387 }
388 if (!DhcpPermissionUtils::VerifyDhcpNetworkPermission("ohos.permission.NETWORK_DHCP")) {
389 DHCP_LOGE("StopClientSa:VerifyDhcpNetworkPermission PERMISSION_DENIED!");
390 return DHCP_E_PERMISSION_DENIED;
391 }
392 #ifdef OHOS_ARCH_LITE
393 return DHCP_E_SUCCESS;
394 #else
395 #ifndef OHOS_EUPDATER
396 return DhcpSaLoadManager::GetInstance().UnloadWifiSa(DHCP_CLIENT_ABILITY_ID);
397 #else
398 return DHCP_E_SUCCESS;
399 #endif
400 #endif
401 }
402
DhcpIpv4ResultSuccess(struct DhcpIpResult & ipResult)403 int DhcpClientServiceImpl::DhcpIpv4ResultSuccess(struct DhcpIpResult &ipResult)
404 {
405 std::string ifname = ipResult.ifname;
406 OHOS::DHCP::DhcpResult result;
407 result.iptype = 0;
408 result.isOptSuc = true;
409 result.uGetTime = (uint32_t)time(NULL);
410 result.uAddTime = ipResult.uAddTime;
411 result.uLeaseTime = ipResult.uOptLeasetime;
412 result.strYourCli = ipResult.strYiaddr;
413 result.strServer = ipResult.strOptServerId;
414 result.strSubnet = ipResult.strOptSubnet;
415 result.strDns1 = ipResult.strOptDns1;
416 result.strDns2 = ipResult.strOptDns2;
417 result.strRouter1 = ipResult.strOptRouter1;
418 result.strRouter2 = ipResult.strOptRouter2;
419 result.strVendor = ipResult.strOptVendor;
420 for (std::vector<std::string>::iterator it = ipResult.dnsAddr.begin(); it != ipResult.dnsAddr.end(); it++) {
421 result.vectorDnsAddr.push_back(*it);
422 }
423 DHCP_LOGI("DhcpIpv4ResultSuccess %{public}s, %{public}d, opt:%{public}d, cli:%{private}s, server:%{private}s, "
424 "Subnet:%{private}s, Dns1:%{private}s, Dns2:%{private}s, Router1:%{private}s, Router2:%{private}s, "
425 "strVendor:%{public}s, uLeaseTime:%{public}u, uAddTime:%{public}u, uGetTime:%{public}u.",
426 ifname.c_str(), result.iptype, result.isOptSuc, result.strYourCli.c_str(), result.strServer.c_str(),
427 result.strSubnet.c_str(), result.strDns1.c_str(), result.strDns2.c_str(), result.strRouter1.c_str(),
428 result.strRouter2.c_str(), result.strVendor.c_str(), result.uLeaseTime, result.uAddTime, result.uGetTime);
429
430 if (CheckDhcpResultExist(ifname, result)) {
431 DHCP_LOGI("DhcpIpv4ResultSuccess DhcpResult %{public}s equal new addtime %{public}u, no need update.",
432 ifname.c_str(), result.uAddTime);
433 return DHCP_OPT_SUCCESS;
434 }
435 PushDhcpResult(ifname, result);
436 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
437 auto iter = m_mapClientCallBack.find(ifname);
438 if (iter == m_mapClientCallBack.end()) {
439 DHCP_LOGE("DhcpIpv4ResultSuccess m_mapClientCallBack not find callback!");
440 return DHCP_OPT_FAILED;
441 }
442 if ((iter->second) == nullptr) {
443 DHCP_LOGE("DhcpIpv4ResultSuccess mclientCallback is nullptr!");
444 return DHCP_OPT_FAILED;
445 }
446 (iter->second)->OnIpSuccessChanged(DHCP_OPT_SUCCESS, ifname, result);
447 return DHCP_OPT_SUCCESS;
448 }
449 #ifndef OHOS_ARCH_LITE
DhcpOfferResultSuccess(struct DhcpIpResult & ipResult)450 int DhcpClientServiceImpl::DhcpOfferResultSuccess(struct DhcpIpResult &ipResult)
451 {
452 std::string ifname = ipResult.ifname;
453 OHOS::DHCP::DhcpResult result;
454 result.iptype = 0;
455 result.isOptSuc = true;
456 result.uGetTime = static_cast<uint32_t>(time(NULL));
457 result.uAddTime = ipResult.uAddTime;
458 result.uLeaseTime = ipResult.uOptLeasetime;
459 result.strYourCli = ipResult.strYiaddr;
460 result.strServer = ipResult.strOptServerId;
461 result.strSubnet = ipResult.strOptSubnet;
462 result.strDns1 = ipResult.strOptDns1;
463 result.strDns2 = ipResult.strOptDns2;
464 result.strRouter1 = ipResult.strOptRouter1;
465 result.strRouter2 = ipResult.strOptRouter2;
466 result.strVendor = ipResult.strOptVendor;
467 for (std::vector<std::string>::iterator it = ipResult.dnsAddr.begin(); it != ipResult.dnsAddr.end(); it++) {
468 result.vectorDnsAddr.push_back(*it);
469 }
470
471 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
472 auto iter = m_mapClientCallBack.find(ifname);
473 if (iter == m_mapClientCallBack.end()) {
474 DHCP_LOGE("OnDhcpOfferReport m_mapClientCallBack not find callback!");
475 return DHCP_OPT_FAILED;
476 }
477 if ((iter->second) == nullptr) {
478 DHCP_LOGE("OnDhcpOfferReport mclientCallback is nullptr!");
479 return DHCP_OPT_FAILED;
480 }
481 (iter->second)->OnDhcpOfferReport(0, ifname, result);
482 return DHCP_OPT_SUCCESS;
483 }
484 #endif
DhcpIpv4ResultFail(struct DhcpIpResult & ipResult)485 int DhcpClientServiceImpl::DhcpIpv4ResultFail(struct DhcpIpResult &ipResult)
486 {
487 std::string ifname = ipResult.ifname;
488 OHOS::DHCP::DhcpResult result;
489 result.iptype = 0;
490 result.isOptSuc = false;
491 result.uGetTime = (uint32_t)time(NULL);
492 result.uAddTime = ipResult.uAddTime;
493 PushDhcpResult(ifname, result);
494 DHCP_LOGI("DhcpIpv4ResultFail ifname:%{public}s result.isOptSuc:false!", ifname.c_str());
495 ActionMode action = ACTION_INVALID;
496 {
497 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
498 auto iterlient = m_mapClientService.find(ifname);
499 if (iterlient != m_mapClientService.end() && ((iterlient->second).pStaStateMachine != nullptr)) {
500 action = (iterlient->second).pStaStateMachine->GetAction();
501 }
502 }
503 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
504 auto iter = m_mapClientCallBack.find(ifname);
505 if (iter == m_mapClientCallBack.end()) {
506 DHCP_LOGE("DhcpIpv4ResultFail m_mapClientCallBack not find callback!");
507 return DHCP_OPT_FAILED;
508 }
509 if ((iter->second) == nullptr) {
510 DHCP_LOGE("DhcpIpv4ResultFail mclientCallback == nullptr!");
511 return DHCP_OPT_FAILED;
512 }
513 if ((action == ACTION_RENEW_T1) || (action == ACTION_RENEW_T2) || (action == ACTION_RENEW_T3)) {
514 (iter->second)->OnIpFailChanged(DHCP_OPT_RENEW_FAILED, ifname.c_str(), "get dhcp renew result failed!");
515 } else {
516 (iter->second)->OnIpFailChanged(DHCP_OPT_FAILED, ifname.c_str(), "get dhcp ip result failed!");
517 }
518 DHCP_LOGI("DhcpIpv4ResultFail OnIpFailChanged!, action:%{public}d", action);
519 return DHCP_OPT_SUCCESS;
520 }
521
DhcpIpv4ResultTimeOut(const std::string & ifname)522 int DhcpClientServiceImpl::DhcpIpv4ResultTimeOut(const std::string &ifname)
523 {
524 DHCP_LOGI("DhcpIpv4ResultTimeOut ifname:%{public}s", ifname.c_str());
525 ActionMode action = ACTION_INVALID;
526 {
527 std::lock_guard<std::mutex> autoLock(m_clientServiceMutex);
528 auto iterlient = m_mapClientService.find(ifname);
529 if (iterlient != m_mapClientService.end() && ((iterlient->second).pStaStateMachine != nullptr)) {
530 action = (iterlient->second).pStaStateMachine->GetAction();
531 }
532 }
533 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
534 auto iter = m_mapClientCallBack.find(ifname);
535 if (iter == m_mapClientCallBack.end()) {
536 DHCP_LOGE("DhcpIpv4ResultTimeOut m_mapClientCallBack not find callback!");
537 return DHCP_OPT_FAILED;
538 }
539 if ((iter->second) == nullptr) {
540 DHCP_LOGE("DhcpIpv4ResultTimeOut mclientCallback == nullptr!");
541 return DHCP_OPT_FAILED;
542 }
543 if ((action == ACTION_RENEW_T1) || (action == ACTION_RENEW_T2) || (action == ACTION_RENEW_T2)) {
544 (iter->second)->OnIpFailChanged(DHCP_OPT_RENEW_TIMEOUT, ifname.c_str(), "get dhcp renew result timeout!");
545 } else {
546 (iter->second)->OnIpFailChanged(DHCP_OPT_TIMEOUT, ifname.c_str(), "get dhcp result timeout!");
547 }
548 DHCP_LOGI("DhcpIpv4ResultTimeOut OnIpFailChanged Timeout!, action:%{public}d", action);
549 return DHCP_OPT_SUCCESS;
550 }
551
DhcpIpv4ResultExpired(const std::string & ifname)552 int DhcpClientServiceImpl::DhcpIpv4ResultExpired(const std::string &ifname)
553 {
554 DHCP_LOGI("DhcpIpv4ResultExpired ifname:%{public}s", ifname.c_str());
555 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
556 auto iter = m_mapClientCallBack.find(ifname);
557 if (iter == m_mapClientCallBack.end()) {
558 DHCP_LOGE("DhcpIpv4ResultExpired not find ifname callback!");
559 return DHCP_OPT_FAILED;
560 }
561 if ((iter->second) == nullptr) {
562 DHCP_LOGE("DhcpIpv4ResultExpired callback is nullptr!");
563 return DHCP_OPT_FAILED;
564 }
565 (iter->second)->OnIpFailChanged(DHCP_OPT_LEASE_EXPIRED, ifname.c_str(), "ifname ip lease expired!");
566 DHCP_LOGI("DhcpIpv4ResultExpired OnIpFailChanged Lease Expired!");
567 return DHCP_OPT_SUCCESS;
568 }
569
DhcpIpv6ResulCallback(const std::string ifname,DhcpIpv6Info & info)570 void DhcpClientServiceImpl::DhcpIpv6ResulCallback(const std::string ifname, DhcpIpv6Info &info)
571 {
572 if (strlen(info.dnsAddr) == 0 || strlen(info.linkIpv6Addr) == 0) {
573 DHCP_LOGE("DhcpIpv6ResulCallback invalid, ifname:%{public}s, status:%{public}d", ifname.c_str(), info.status);
574 return;
575 }
576 OHOS::DHCP::DhcpResult result;
577 result.uAddTime = (uint32_t)time(NULL);
578 result.iptype = 1;
579 result.isOptSuc = true;
580 result.uGetTime = (uint32_t)time(NULL);
581 result.strYourCli = info.globalIpv6Addr;
582 result.strSubnet = info.ipv6SubnetAddr;
583 result.strRouter1 = info.routeAddr;
584 result.strDns1 = info.dnsAddr;
585 result.strDns2 = info.dnsAddr2;
586 result.strRouter2 = "*";
587 result.strLinkIpv6Addr = info.linkIpv6Addr;
588 result.strRandIpv6Addr = info.randIpv6Addr;
589 result.strLocalAddr1 = info.uniqueLocalAddr1;
590 result.strLocalAddr2 = info.uniqueLocalAddr2;
591 for (auto dnsAddr : info.vectorDnsAddr) {
592 result.vectorDnsAddr.push_back(dnsAddr);
593 }
594
595 PushDhcpResult(ifname, result);
596 DHCP_LOGI("DhcpIpv6ResulCallback %{public}s, %{public}d, opt:%{public}d, cli:%{public}s, server:%{public}s, "
597 "Subnet:%{public}s, Dns1:%{public}s, Dns2:%{public}s, Router1:%{public}s, Router2:%{public}s, "
598 "strVendor:%{public}s, strLinkIpv6Addr:%{public}s, strRandIpv6Addr:%{public}s, uLeaseTime:%{public}u, "
599 "uAddTime:%{public}u, uGetTime:%{public}u.",
600 ifname.c_str(), result.iptype, result.isOptSuc, Ipv6Anonymize(result.strYourCli).c_str(),
601 Ipv6Anonymize(result.strServer).c_str(), Ipv6Anonymize(result.strSubnet).c_str(),
602 Ipv6Anonymize(result.strDns1).c_str(), Ipv6Anonymize(result.strDns2).c_str(),
603 Ipv6Anonymize(result.strRouter1).c_str(), Ipv6Anonymize(result.strRouter2).c_str(),
604 Ipv6Anonymize(result.strVendor).c_str(), Ipv6Anonymize(result.strLinkIpv6Addr).c_str(),
605 Ipv6Anonymize(result.strRandIpv6Addr).c_str(), result.uLeaseTime, result.uAddTime, result.uGetTime);
606 std::lock_guard<std::mutex> autoLock(m_clientCallBackMutex);
607 auto iter = m_mapClientCallBack.find(ifname);
608 if (iter == m_mapClientCallBack.end()) {
609 DHCP_LOGE("DhcpIpv6ResulCallback m_mapClientCallBack not find callback!");
610 return;
611 }
612 if ((iter->second) == nullptr) {
613 DHCP_LOGE("DhcpIpv6ResulCallback mclientCallback == nullptr!");
614 return;
615 }
616 (iter->second)->OnIpSuccessChanged(PUBLISH_CODE_SUCCESS, ifname, result);
617 DHCP_LOGI("DhcpIpv6ResulCallback OnIpSuccessChanged");
618 }
619
DhcpIpv6ResultTimeOut(const std::string & ifname)620 int DhcpClientServiceImpl::DhcpIpv6ResultTimeOut(const std::string &ifname)
621 {
622 DHCP_LOGI("DhcpIpv6ResultTimeOut ifname:%{public}s", ifname.c_str());
623 DhcpFreeIpv6(ifname);
624 return DHCP_OPT_SUCCESS;
625 }
626
DhcpFreeIpv6(const std::string ifname)627 int DhcpClientServiceImpl::DhcpFreeIpv6(const std::string ifname)
628 {
629 DHCP_LOGI("DhcpFreeIpv6 ifname:%{public}s", ifname.c_str());
630 std::lock_guard<std::mutex> autoLockServer(m_clientServiceMutex);
631 auto iter = m_mapClientService.find(ifname);
632 if (iter != m_mapClientService.end()) {
633 if ((iter->second).pipv6Client != nullptr) {
634 (iter->second).pipv6Client->DhcpIPV6Stop();
635 }
636 }
637 return DHCP_OPT_SUCCESS;
638 }
639
PushDhcpResult(const std::string & ifname,OHOS::DHCP::DhcpResult & result)640 void DhcpClientServiceImpl::PushDhcpResult(const std::string &ifname, OHOS::DHCP::DhcpResult &result)
641 {
642 std::lock_guard<std::mutex> autoLock(m_dhcpResultMutex);
643 auto iterResult = m_mapDhcpResult.find(ifname);
644 if (iterResult != m_mapDhcpResult.end()) {
645 for (size_t i = 0; i < iterResult->second.size(); i++) {
646 if (iterResult->second[i].iptype != result.iptype) {
647 continue;
648 }
649 if (iterResult->second[i].iptype == 0) { // 0-ipv4
650 if (iterResult->second[i].uAddTime != result.uAddTime) {
651 iterResult->second[i] = result;
652 DHCP_LOGI("PushDhcpResult update ipv4 result, ifname:%{public}s", ifname.c_str());
653 }
654 } else { // 1-ipv6
655 DHCP_LOGI("PushDhcpResult update ipv6 result, ifname:%{public}s", ifname.c_str());
656 iterResult->second[i] = result;
657 }
658 return;
659 }
660 DHCP_LOGI("PushDhcpResult ifname add new result, ifname:%{public}s", ifname.c_str());
661 iterResult->second.push_back(result);
662 } else {
663 std::vector<OHOS::DHCP::DhcpResult> results;
664 results.push_back(result);
665 m_mapDhcpResult.emplace(std::make_pair(ifname, results));
666 DHCP_LOGI("PushDhcpResult add new ifname result, ifname:%{public}s", ifname.c_str());
667 }
668 }
669
CheckDhcpResultExist(const std::string & ifname,OHOS::DHCP::DhcpResult & result)670 bool DhcpClientServiceImpl::CheckDhcpResultExist(const std::string &ifname, OHOS::DHCP::DhcpResult &result)
671 {
672 bool exist = false;
673 std::lock_guard<std::mutex> autoLock(m_dhcpResultMutex);
674 auto iterResult = m_mapDhcpResult.find(ifname);
675 if (iterResult != m_mapDhcpResult.end()) {
676 for (size_t i = 0; i < iterResult->second.size(); i++) {
677 if (iterResult->second[i].iptype != result.iptype) {
678 continue;
679 }
680 if (iterResult->second[i].uAddTime == result.uAddTime) {
681 exist = true;
682 break;
683 }
684 }
685 }
686 return exist;
687 }
688
IsRemoteDied(void)689 bool DhcpClientServiceImpl::IsRemoteDied(void)
690 {
691 DHCP_LOGD("IsRemoteDied");
692 return true;
693 }
694
IsGlobalIPv6Address(std::string ipAddress)695 bool DhcpClientServiceImpl::IsGlobalIPv6Address(std::string ipAddress)
696 {
697 const char* ipAddr = ipAddress.c_str();
698 int first = ipAddr[0]-'0';
699 DHCP_LOGI("first = %{public}d", first);
700 if (first == NUMBER_TWO || first == NUMBER_THREE) {
701 return true;
702 }
703 return false;
704 }
705 }
706 }