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_controller.h"
17
18 #include "dhcp_result_parcel.h"
19 #include "netnative_log_wrapper.h"
20 #include "netmanager_base_common_utils.h"
21 #include <securec.h>
22 #include <cstring>
23
24 namespace OHOS {
25 namespace nmd {
26 static constexpr const char *DEFAULT_STR_SUBNET = "255.255.255.0";
27 static constexpr const char *DEFAULT_STR_STARTIP = ".3";
28 static constexpr const char *DEFAULT_STR_ENDIP = ".254";
29 DhcpController *DhcpController::DhcpControllerResultNotify::dhcpController_ = nullptr;
30
DhcpControllerResultNotify()31 DhcpController::DhcpControllerResultNotify::DhcpControllerResultNotify() {}
32
~DhcpControllerResultNotify()33 DhcpController::DhcpControllerResultNotify::~DhcpControllerResultNotify() {}
34
SetDhcpController(DhcpController * dhcpController)35 void DhcpController::DhcpControllerResultNotify::SetDhcpController(DhcpController *dhcpController)
36 {
37 dhcpController_ = dhcpController;
38 }
39
OnSuccess(int status,const char * ifname,DhcpResult * result)40 void DhcpController::DhcpControllerResultNotify::OnSuccess(int status, const char *ifname,
41 DhcpResult *result)
42 {
43 if (ifname == nullptr || result == nullptr) {
44 NETNATIVE_LOGE("ifname or result is nullptr!");
45 return;
46 }
47 NETNATIVE_LOGI(
48 "Enter DhcpController::DhcpControllerResultNotify::OnSuccess "
49 "ifname=[%{public}s], iptype=[%{public}d], strYourCli=[%{public}s], "
50 "strServer=[%{public}s], strSubnet=[%{public}s], strDns1=[%{public}s], "
51 "strDns2=[%{public}s] strRouter1=[%{public}s] strRouter2=[%{public}s]",
52 ifname, result->iptype, result->strOptClientId,
53 NetManagerStandard::CommonUtils::ToAnonymousIp(result->strOptServerId).c_str(),
54 NetManagerStandard::CommonUtils::ToAnonymousIp(result->strOptSubnet).c_str(),
55 NetManagerStandard::CommonUtils::ToAnonymousIp(result->strOptDns1).c_str(),
56 NetManagerStandard::CommonUtils::ToAnonymousIp(result->strOptDns2).c_str(),
57 NetManagerStandard::CommonUtils::ToAnonymousIp(result->strOptRouter1).c_str(),
58 NetManagerStandard::CommonUtils::ToAnonymousIp(result->strOptRouter2).c_str());
59 dhcpController_->Process(ifname, result);
60 }
61
OnFailed(int status,const char * ifname,const char * reason)62 void DhcpController::DhcpControllerResultNotify::OnFailed(int status, const char *ifname,
63 const char *reason)
64 {
65 NETNATIVE_LOGE("Enter DhcpController::DhcpControllerResultNotify::OnFailed");
66 }
67
DhcpController()68 DhcpController::DhcpController()
69 {
70 dhcpResultNotify_ = std::make_unique<DhcpControllerResultNotify>();
71 }
72
~DhcpController()73 DhcpController::~DhcpController() {}
74
RegisterNotifyCallback(sptr<OHOS::NetsysNative::INotifyCallback> & callback)75 int32_t DhcpController::RegisterNotifyCallback(sptr<OHOS::NetsysNative::INotifyCallback> &callback)
76 {
77 NETNATIVE_LOGI("DhcpController RegisterNotifyCallback");
78 callback_ = callback;
79 return 0;
80 }
81
StartClient(const std::string & iface,bool bIpv6)82 void DhcpController::StartClient(const std::string &iface, bool bIpv6)
83 {
84 clientEvent.OnIpSuccessChanged = DhcpControllerResultNotify::OnSuccess;
85 clientEvent.OnIpFailChanged = DhcpControllerResultNotify::OnFailed;
86 dhcpResultNotify_->SetDhcpController(this);
87 if (RegisterDhcpClientCallBack(iface.c_str(), &clientEvent) != DHCP_SUCCESS) {
88 NETNATIVE_LOGE("RegisterDhcpClientCallBack failed.");
89 return;
90 }
91
92 NETNATIVE_LOGI("DhcpController StartDhcpClient iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
93 RouterConfig config;
94 config.bIpv6 = bIpv6;
95 if (strncpy_s(config.ifname, sizeof(config.ifname), iface.c_str(), iface.length()) != DHCP_SUCCESS) {
96 NETNATIVE_LOGE("strncpy_s config.ifname failed.");
97 return;
98 }
99 if (StartDhcpClient(config) != DHCP_SUCCESS) {
100 NETNATIVE_LOGE("Start dhcp client failed");
101 }
102 }
103
StopClient(const std::string & iface,bool bIpv6)104 void DhcpController::StopClient(const std::string &iface, bool bIpv6)
105 {
106 NETNATIVE_LOGI("DhcpController StopDhcpClient iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
107 if (StopDhcpClient(iface.c_str(), bIpv6) != DHCP_SUCCESS) {
108 NETNATIVE_LOGE("Stop dhcp client failed");
109 }
110 }
111
Process(const std::string & iface,DhcpResult * result)112 void DhcpController::Process(const std::string &iface, DhcpResult *result)
113 {
114 NETNATIVE_LOGI("DhcpController Process");
115 sptr<OHOS::NetsysNative::DhcpResultParcel> ptr = new (std::nothrow) OHOS::NetsysNative::DhcpResultParcel();
116 if (ptr == nullptr) {
117 return;
118 }
119 ptr->iface_ = iface;
120 ptr->ipAddr_ = result->strOptClientId;
121 ptr->gateWay_ = result->strOptServerId;
122 ptr->subNet_ = result->strOptSubnet;
123 ptr->route1_ = result->strOptRouter1;
124 ptr->route2_ = result->strOptRouter2;
125 ptr->dns1_ = result->strOptDns1;
126 ptr->dns2_ = result->strOptDns2;
127 NETNATIVE_LOGI("DhcpController Process iface[%{public}s]", iface.c_str());
128 callback_->OnDhcpSuccess(ptr);
129 }
130
StartDhcpService(const std::string & iface,const std::string & ipv4addr)131 bool DhcpController::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
132 {
133 constexpr int32_t IP_V4 = 0;
134 std::string ipAddr = ipv4addr;
135 std::string::size_type pos = ipAddr.rfind(".");
136 if (pos == std::string::npos) {
137 return false;
138 }
139
140 std::string ipHead = ipAddr.substr(0, pos);
141 std::string strStartip = ipHead + DEFAULT_STR_STARTIP;
142 std::string strEndip = ipHead + DEFAULT_STR_ENDIP;
143 std::string strSubnet = DEFAULT_STR_SUBNET;
144
145 DhcpRange range;
146 range.iptype = IP_V4;
147 if (strcpy_s(range.strTagName, DHCP_MAX_FILE_BYTES, iface.c_str()) != 0) {
148 NETNATIVE_LOGE("strcpy_s strTagName failed!");
149 return false;
150 }
151
152 if (strcpy_s(range.strStartip, INET_ADDRSTRLEN, strStartip.c_str()) != 0) {
153 NETNATIVE_LOGE("strcpy_s strStartip failed!");
154 return false;
155 }
156
157 if (strcpy_s(range.strEndip, INET_ADDRSTRLEN, strEndip.c_str()) != 0) {
158 NETNATIVE_LOGE("strcpy_s strEndip failed!");
159 return false;
160 }
161
162 if (strcpy_s(range.strSubnet, INET_ADDRSTRLEN, strSubnet.c_str()) != 0) {
163 NETNATIVE_LOGE("strcpy_s strSubnet failed!");
164 return false;
165 }
166
167 if (SetDhcpRange(iface.c_str(), &range) != DHCP_SUCCESS) {
168 NETNATIVE_LOGE("SetDhcpRange failed!");
169 return false;
170 }
171 NETNATIVE_LOGI(
172 "Set dhcp range : ifaceName[%{public}s] TagName[%{public}s] start ip[%{public}s] end ip[%{public}s]",
173 iface.c_str(), range.strTagName, range.strStartip, range.strEndip);
174 if (StartDhcpServer(iface.c_str()) != DHCP_SUCCESS) {
175 NETNATIVE_LOGE("Start dhcp server failed!, iface:[%{public}s]", iface.c_str());
176 return false;
177 }
178 return true;
179 }
180
StopDhcpService(const std::string & iface)181 bool DhcpController::StopDhcpService(const std::string &iface)
182 {
183 if (RemoveAllDhcpRange(iface.c_str()) != DHCP_SUCCESS) {
184 NETNATIVE_LOGE("failed to remove [%{public}s] dhcp range.", iface.c_str());
185 }
186
187 if (StopDhcpServer(iface.c_str()) != DHCP_SUCCESS) {
188 NETNATIVE_LOGE("Stop dhcp server failed!");
189 return false;
190 }
191 NETNATIVE_LOGI("StopDhcpService ifaceName[%{public}s]", iface.c_str());
192 return true;
193 }
194 } // namespace nmd
195 } // namespace OHOS
196