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