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