• 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 
22 namespace OHOS {
23 namespace nmd {
24 constexpr int32_t DHCP_TIMEOUT = 300;
25 static constexpr const char *DEFAULT_STR_SUBNET = "255.255.255.0";
26 static constexpr const char *DEFAULT_STR_STARTIP = ".3";
27 static constexpr const char *DEFAULT_STR_ENDIP = ".254";
DhcpControllerResultNotify(DhcpController & dhcpController)28 DhcpController::DhcpControllerResultNotify::DhcpControllerResultNotify(DhcpController &dhcpController)
29     : dhcpController_(dhcpController)
30 {
31 }
32 
~DhcpControllerResultNotify()33 DhcpController::DhcpControllerResultNotify::~DhcpControllerResultNotify() {}
34 
OnSuccess(int status,const std::string & ifname,OHOS::Wifi::DhcpResult & result)35 void DhcpController::DhcpControllerResultNotify::OnSuccess(int status, const std::string &ifname,
36                                                            OHOS::Wifi::DhcpResult &result)
37 {
38     NETNATIVE_LOG_D(
39         "Enter DhcpController::DhcpControllerResultNotify::OnSuccess "
40         "ifname=[%{public}s], iptype=[%{public}d], strYourCli=[%{public}s], "
41         "strServer=[%{public}s], strSubnet=[%{public}s], strDns1=[%{public}s], "
42         "strDns2=[%{public}s] strRouter1=[%{public}s] strRouter2=[%{public}s]",
43         ifname.c_str(), result.iptype, result.strYourCli.c_str(),
44         NetManagerStandard::CommonUtils::ToAnonymousIp(result.strServer).c_str(),
45         NetManagerStandard::CommonUtils::ToAnonymousIp(result.strSubnet).c_str(),
46         NetManagerStandard::CommonUtils::ToAnonymousIp(result.strDns1).c_str(),
47         NetManagerStandard::CommonUtils::ToAnonymousIp(result.strDns2).c_str(),
48         NetManagerStandard::CommonUtils::ToAnonymousIp(result.strRouter1).c_str(),
49         NetManagerStandard::CommonUtils::ToAnonymousIp(result.strRouter2).c_str());
50     dhcpController_.Process(ifname, result);
51 }
52 
OnFailed(int status,const std::string & ifname,const std::string & reason)53 void DhcpController::DhcpControllerResultNotify::OnFailed(int status, const std::string &ifname,
54                                                           const std::string &reason)
55 {
56     NETNATIVE_LOGE("Enter DhcpController::DhcpControllerResultNotify::OnFailed");
57 }
58 
OnSerExitNotify(const std::string & ifname)59 void DhcpController::DhcpControllerResultNotify::OnSerExitNotify(const std::string &ifname)
60 {
61     NETNATIVE_LOGE("DhcpController::DhcpControllerResultNotify::OnSerExitNotify");
62 }
63 
DhcpController()64 DhcpController::DhcpController()
65 {
66     dhcpResultNotify_ = std::make_unique<DhcpControllerResultNotify>(*this);
67 }
68 
~DhcpController()69 DhcpController::~DhcpController() {}
70 
RegisterNotifyCallback(sptr<OHOS::NetsysNative::INotifyCallback> & callback)71 int32_t DhcpController::RegisterNotifyCallback(sptr<OHOS::NetsysNative::INotifyCallback> &callback)
72 {
73     NETNATIVE_LOGI("DhcpController RegisterNotifyCallback");
74     callback_ = callback;
75     return 0;
76 }
77 
StartDhcpClient(const std::string & iface,bool bIpv6)78 void DhcpController::StartDhcpClient(const std::string &iface, bool bIpv6)
79 {
80     NETNATIVE_LOGI("DhcpController StartDhcpClient iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
81     if (OHOS::Wifi::DhcpServiceApi::GetInstance() == nullptr) {
82         NETNATIVE_LOGE("OHOS::Wifi::DhcpServiceApi is nullptr");
83         return;
84     }
85     OHOS::Wifi::DhcpServiceApi::GetInstance()->StartDhcpClient(iface, bIpv6);
86     if (OHOS::Wifi::DhcpServiceApi::GetInstance()->GetDhcpResult(iface, dhcpResultNotify_.get(), DHCP_TIMEOUT) != 0) {
87         NETNATIVE_LOGE(" Dhcp connection failed");
88     }
89 }
90 
StopDhcpClient(const std::string & iface,bool bIpv6)91 void DhcpController::StopDhcpClient(const std::string &iface, bool bIpv6)
92 {
93     NETNATIVE_LOGI("DhcpController StopDhcpClient iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
94     if (OHOS::Wifi::DhcpServiceApi::GetInstance() == nullptr) {
95         NETNATIVE_LOGE("OHOS::Wifi::DhcpServiceApi is nullptr");
96         return;
97     }
98     OHOS::Wifi::DhcpServiceApi::GetInstance()->StopDhcpClient(iface, bIpv6);
99 }
100 
Process(const std::string & iface,OHOS::Wifi::DhcpResult & result)101 void DhcpController::Process(const std::string &iface, OHOS::Wifi::DhcpResult &result)
102 {
103     NETNATIVE_LOGI("DhcpController Process");
104     sptr<OHOS::NetsysNative::DhcpResultParcel> ptr = new (std::nothrow) OHOS::NetsysNative::DhcpResultParcel();
105     if (ptr == nullptr) {
106         return;
107     }
108 
109     ptr->iface_ = iface;
110     ptr->ipAddr_ = result.strYourCli;
111     ptr->gateWay_ = result.strServer;
112     ptr->subNet_ = result.strSubnet;
113     ptr->route1_ = result.strRouter1;
114     ptr->route2_ = result.strRouter2;
115     ptr->dns1_ = result.strDns1;
116     ptr->dns2_ = result.strDns2;
117     callback_->OnDhcpSuccess(ptr);
118 }
119 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)120 bool DhcpController::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
121 {
122     constexpr int32_t IP_V4 = 0;
123     if (OHOS::Wifi::DhcpServiceApi::GetInstance() == nullptr) {
124         NETNATIVE_LOGE("OHOS::Wifi::DhcpServiceApi is nullptr");
125         return false;
126     }
127     std::string ipAddr = ipv4addr;
128     std::string::size_type pos = ipAddr.rfind(".");
129     if (pos == std::string::npos) {
130         return false;
131     }
132     std::string ipHead = ipAddr.substr(0, pos);
133     OHOS::Wifi::DhcpRange range;
134     range.iptype = IP_V4;
135     range.strStartip = ipHead + DEFAULT_STR_STARTIP;
136     range.strEndip = ipHead + DEFAULT_STR_ENDIP;
137     range.strSubnet = DEFAULT_STR_SUBNET;
138     range.strTagName = iface;
139     if (OHOS::Wifi::DhcpServiceApi::GetInstance()->SetDhcpRange(iface, range) != 0) {
140         return false;
141     }
142     NETNATIVE_LOGI(
143         "Set dhcp range : ifaceName[%{public}s] TagName[%{public}s] start ip[%{public}s] end ip[%{public}s]",
144         iface.c_str(), range.strTagName.c_str(),
145         NetManagerStandard::CommonUtils::ToAnonymousIp(range.strStartip).c_str(),
146         NetManagerStandard::CommonUtils::ToAnonymousIp(range.strEndip).c_str());
147     if (OHOS::Wifi::DhcpServiceApi::GetInstance()->StartDhcpServer(iface) != 0) {
148         return false;
149     }
150     if (OHOS::Wifi::DhcpServiceApi::GetInstance()->GetDhcpSerProExit(iface, dhcpResultNotify_.get())) {
151         return false;
152     }
153     return true;
154 }
StopDhcpService(const std::string & iface)155 bool DhcpController::StopDhcpService(const std::string &iface)
156 {
157     if (OHOS::Wifi::DhcpServiceApi::GetInstance() == nullptr) {
158         NETNATIVE_LOGE("OHOS::Wifi::DhcpServiceApi is nullptr");
159         return false;
160     }
161     if (OHOS::Wifi::DhcpServiceApi::GetInstance()->RemoveAllDhcpRange(iface) != 0) {
162         NETNATIVE_LOGI("failed to remove [%{public}s] dhcp range.", iface.c_str());
163     }
164     if (OHOS::Wifi::DhcpServiceApi::GetInstance()->StopDhcpServer(iface) != 0) {
165         NETNATIVE_LOGI("Stop dhcp server failed!");
166         return false;
167     }
168     return true;
169 }
170 } // namespace nmd
171 } // namespace OHOS
172