• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (callback == nullptr) {
79         NETNATIVE_LOGE("callback is nullptr");
80         return 0;
81     }
82     std::unique_lock<std::shared_mutex> locker(callbackMutex_);
83     for (const auto &cb : callback_) {
84         if (cb->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
85             NETNATIVE_LOGI("callback is already registered");
86             return 0;
87         }
88     }
89     callback_.push_back(callback);
90     return 0;
91 }
92 
UnregisterNotifyCallback(sptr<OHOS::NetsysNative::INotifyCallback> & callback)93 int32_t DhcpController::UnregisterNotifyCallback(sptr<OHOS::NetsysNative::INotifyCallback> &callback)
94 {
95     NETNATIVE_LOGI("DhcpController UnregisterNotifyCallback");
96     if (callback == nullptr) {
97         NETNATIVE_LOGE("callback is nullptr");
98         return 0;
99     }
100     std::unique_lock<std::shared_mutex> locker(callbackMutex_);
101     for (auto it = callback_.begin(); it != callback_.end(); ++it) {
102         if ((*it)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
103             callback_.erase(it);
104             NETNATIVE_LOGI("callback is unregistered successfully");
105             return 0;
106         }
107     }
108     NETNATIVE_LOGI("callback is not registered");
109     return 0;
110 }
111 
StartClient(const std::string & iface,bool bIpv6)112 void DhcpController::StartClient(const std::string &iface, bool bIpv6)
113 {
114     clientEvent.OnIpSuccessChanged = DhcpControllerResultNotify::OnSuccess;
115     clientEvent.OnIpFailChanged = DhcpControllerResultNotify::OnFailed;
116     dhcpResultNotify_->SetDhcpController(this);
117     if (RegisterDhcpClientCallBack(iface.c_str(), &clientEvent) != DHCP_SUCCESS) {
118         NETNATIVE_LOGE("RegisterDhcpClientCallBack failed.");
119         return;
120     }
121 
122     NETNATIVE_LOGI("DhcpController StartDhcpClient iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
123     RouterConfig config;
124     config.bIpv6 = bIpv6;
125     if (strncpy_s(config.ifname, sizeof(config.ifname), iface.c_str(), iface.length()) != DHCP_SUCCESS) {
126         NETNATIVE_LOGE("strncpy_s config.ifname failed.");
127         return;
128     }
129     if (StartDhcpClient(config) != DHCP_SUCCESS) {
130         NETNATIVE_LOGE("Start dhcp client failed");
131     }
132 }
133 
StopClient(const std::string & iface,bool bIpv6)134 void DhcpController::StopClient(const std::string &iface, bool bIpv6)
135 {
136     NETNATIVE_LOGI("DhcpController StopDhcpClient iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
137     if (StopDhcpClient(iface.c_str(), bIpv6) != DHCP_SUCCESS) {
138         NETNATIVE_LOGE("Stop dhcp client failed");
139     }
140 }
141 
Process(const std::string & iface,DhcpResult * result)142 void DhcpController::Process(const std::string &iface, DhcpResult *result)
143 {
144     NETNATIVE_LOGI("DhcpController Process");
145     sptr<OHOS::NetsysNative::DhcpResultParcel> ptr = new (std::nothrow) OHOS::NetsysNative::DhcpResultParcel();
146     if (ptr == nullptr) {
147         return;
148     }
149     ptr->iface_ = iface;
150     ptr->ipAddr_ = result->strOptClientId;
151     ptr->gateWay_ = result->strOptServerId;
152     ptr->subNet_ = result->strOptSubnet;
153     ptr->route1_ = result->strOptRouter1;
154     ptr->route2_ = result->strOptRouter2;
155     ptr->dns1_ = result->strOptDns1;
156     ptr->dns2_ = result->strOptDns2;
157     NETNATIVE_LOGI("DhcpController Process iface[%{public}s]", iface.c_str());
158     std::shared_lock<std::shared_mutex> locker(callbackMutex_);
159     for (auto cb : callback_) {
160         cb->OnDhcpSuccess(ptr);
161     }
162 }
163 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)164 bool DhcpController::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
165 {
166     constexpr int32_t IP_V4 = 0;
167     std::string ipAddr = ipv4addr;
168     std::string::size_type pos = ipAddr.rfind(".");
169     if (pos == std::string::npos) {
170         return false;
171     }
172 
173     std::string ipHead = ipAddr.substr(0, pos);
174     std::string strStartip = ipHead + DEFAULT_STR_STARTIP;
175     std::string strEndip = ipHead + DEFAULT_STR_ENDIP;
176     std::string strSubnet = DEFAULT_STR_SUBNET;
177 
178     DhcpRange range;
179     range.iptype = IP_V4;
180     if (strcpy_s(range.strTagName, DHCP_MAX_FILE_BYTES, iface.c_str()) != 0) {
181         NETNATIVE_LOGE("strcpy_s strTagName failed!");
182         return false;
183     }
184 
185     if (strcpy_s(range.strStartip, INET_ADDRSTRLEN, strStartip.c_str()) != 0) {
186         NETNATIVE_LOGE("strcpy_s strStartip failed!");
187         return false;
188     }
189 
190     if (strcpy_s(range.strEndip, INET_ADDRSTRLEN, strEndip.c_str()) != 0) {
191         NETNATIVE_LOGE("strcpy_s strEndip failed!");
192         return false;
193     }
194 
195     if (strcpy_s(range.strSubnet, INET_ADDRSTRLEN, strSubnet.c_str()) != 0) {
196         NETNATIVE_LOGE("strcpy_s strSubnet failed!");
197         return false;
198     }
199 
200     if (SetDhcpRange(iface.c_str(), &range) != DHCP_SUCCESS) {
201         NETNATIVE_LOGE("SetDhcpRange failed!");
202         return false;
203     }
204     NETNATIVE_LOGI(
205         "Set dhcp range : ifaceName[%{public}s] TagName[%{public}s] start ip[%{public}s] end ip[%{public}s]",
206         iface.c_str(), range.strTagName, range.strStartip, range.strEndip);
207     if (StartDhcpServer(iface.c_str()) != DHCP_SUCCESS) {
208         NETNATIVE_LOGE("Start dhcp server failed!, iface:[%{public}s]", iface.c_str());
209         return false;
210     }
211     return true;
212 }
213 
StopDhcpService(const std::string & iface)214 bool DhcpController::StopDhcpService(const std::string &iface)
215 {
216     if (RemoveAllDhcpRange(iface.c_str()) != DHCP_SUCCESS) {
217         NETNATIVE_LOGE("failed to remove [%{public}s] dhcp range.", iface.c_str());
218     }
219 
220     if (StopDhcpServer(iface.c_str()) != DHCP_SUCCESS) {
221         NETNATIVE_LOGE("Stop dhcp server failed!");
222         return false;
223     }
224     NETNATIVE_LOGI("StopDhcpService ifaceName[%{public}s]", iface.c_str());
225     return true;
226 }
227 } // namespace nmd
228 } // namespace OHOS
229