1 /*
2 * Copyright (C) 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 "ethernet_dhcp_controller.h"
17 #include <string>
18 #include "ethernet_dhcp_callback.h"
19 #include "netmgr_ext_log_wrapper.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
23
24 EthernetDhcpController *EthernetDhcpController::EthernetDhcpControllerResultNotify::ethDhcpController_ = nullptr;
OnSuccess(int status,const char * ifname,DhcpResult * result)25 void EthernetDhcpController::EthernetDhcpControllerResultNotify::OnSuccess(int status, const char *ifname,
26 DhcpResult *result)
27 {
28 if (ifname == nullptr || result == nullptr) {
29 NETMGR_EXT_LOG_E("ifname or result is nullptr!");
30 return;
31 }
32
33 if (ethDhcpController_ != nullptr) {
34 NETMGR_EXT_LOG_I("EthernetDhcpControllerResultNotify OnSuccess.");
35 ethDhcpController_->OnDhcpSuccess(ifname, result);
36 }
37 }
38
OnFailed(int status,const char * ifname,const char * reason)39 void EthernetDhcpController::EthernetDhcpControllerResultNotify::OnFailed(int status, const char *ifname,
40 const char *reason)
41 {
42 NETMGR_EXT_LOG_I("EthernetDhcpControllerResultNotify OnFailed.");
43 return;
44 }
45
SetEthernetDhcpController(EthernetDhcpController * ethDhcpController)46 void EthernetDhcpController::EthernetDhcpControllerResultNotify::SetEthernetDhcpController(
47 EthernetDhcpController *ethDhcpController)
48 {
49 ethDhcpController_ = ethDhcpController;
50 }
51
RegisterDhcpCallback(sptr<EthernetDhcpCallback> callback)52 void EthernetDhcpController::RegisterDhcpCallback(sptr<EthernetDhcpCallback> callback)
53 {
54 cbObject_ = callback;
55 }
56
StartClient(const std::string & iface,bool bIpv6)57 void EthernetDhcpController::StartClient(const std::string &iface, bool bIpv6)
58 {
59 clientEvent.OnIpSuccessChanged = EthernetDhcpControllerResultNotify::OnSuccess;
60 clientEvent.OnIpFailChanged = EthernetDhcpControllerResultNotify::OnFailed;
61 dhcpResultNotify_->SetEthernetDhcpController(this);
62 if (RegisterDhcpClientCallBack(iface.c_str(), &clientEvent) != DHCP_SUCCESS) {
63 NETMGR_EXT_LOG_E("RegisterDhcpClientCallBack failed.");
64 return;
65 }
66 NETMGR_EXT_LOG_I("Start dhcp client iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
67 if (StartDhcpClient(iface.c_str(), bIpv6) != DHCP_SUCCESS) {
68 NETMGR_EXT_LOG_E("StartDhcpClient failed.");
69 }
70 }
71
StopClient(const std::string & iface,bool bIpv6)72 void EthernetDhcpController::StopClient(const std::string &iface, bool bIpv6)
73 {
74 NETMGR_EXT_LOG_D("StopClient iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
75 if (StopDhcpClient(iface.c_str(), bIpv6) != DHCP_SUCCESS) {
76 NETMGR_EXT_LOG_E("StopDhcpClient failed.");
77 }
78 }
79
OnDhcpSuccess(const std::string & iface,DhcpResult * result)80 void EthernetDhcpController::OnDhcpSuccess(const std::string &iface, DhcpResult *result)
81 {
82 if (cbObject_ == nullptr || result == nullptr) {
83 NETMGR_EXT_LOG_E("cbObject_ or result is nullptr!");
84 return;
85 }
86 NETMGR_EXT_LOG_I("OnDhcpSuccess, iface[%{public}s]", iface.c_str());
87 EthernetDhcpCallback::DhcpResult dhcpResult;
88 dhcpResult.iface = iface;
89 dhcpResult.ipAddr = result->strOptClientId;
90 dhcpResult.gateWay = result->strOptRouter1;
91 dhcpResult.subNet = result->strOptSubnet;
92 dhcpResult.route1 = result->strOptRouter1;
93 dhcpResult.route2 = result->strOptRouter2;
94 dhcpResult.dns1 = result->strOptDns1;
95 dhcpResult.dns2 = result->strOptDns2;
96 cbObject_->OnDhcpSuccess(dhcpResult);
97 }
98
OnDhcpFailed(int status,const std::string & ifname,const char * reason)99 void EthernetDhcpController::OnDhcpFailed(int status, const std::string &ifname, const char *reason) {}
100 } // namespace NetManagerStandard
101 } // namespace OHOS
102