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