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_service.h"
17 #include <map>
18 #include <string>
19 #include <net/if.h>
20 #include "dhcp_client_service_impl.h"
21 #include "dhcp_server_service.h"
22 #include "wifi_logger.h"
23
24 namespace OHOS {
25 namespace Wifi {
26 DEFINE_WIFILOG_DHCP_LABEL("DhcpService");
27
DhcpService()28 DhcpService::DhcpService() : m_pClientService(nullptr), m_pServerService(nullptr)
29 {
30 WIFI_LOGI("DhcpService constructor...");
31 DhcpClientServiceImpl::m_mapDhcpResult.clear();
32 DhcpClientServiceImpl::m_mapDhcpInfo.clear();
33 }
34
~DhcpService()35 DhcpService::~DhcpService()
36 {
37 WIFI_LOGI("DhcpService destructor...");
38 DhcpClientServiceImpl::m_mapDhcpResult.clear();
39 DhcpClientServiceImpl::m_mapDhcpInfo.clear();
40 }
41
StartDhcpClient(const std::string & ifname,bool bIpv6)42 int DhcpService::StartDhcpClient(const std::string& ifname, bool bIpv6)
43 {
44 if (!CheckIfaceValid(ifname)) {
45 return DHCP_OPT_FAILED;
46 }
47 if (m_pClientService == nullptr) {
48 m_pClientService = std::make_unique<DhcpClientServiceImpl>();
49 if (m_pClientService == nullptr) {
50 WIFI_LOGE("DhcpService::StartDhcpClient() std::make_unique<DhcpClientServiceImpl>() failed!");
51 return DHCP_OPT_FAILED;
52 }
53 }
54
55 return m_pClientService->StartDhcpClient(ifname, bIpv6);
56 }
57
StopDhcpClient(const std::string & ifname,bool bIpv6)58 int DhcpService::StopDhcpClient(const std::string& ifname, bool bIpv6)
59 {
60 if (m_pClientService == nullptr) {
61 m_pClientService = std::make_unique<DhcpClientServiceImpl>();
62 if (m_pClientService == nullptr) {
63 WIFI_LOGE("DhcpService::StopDhcpClient() std::make_unique<DhcpClientServiceImpl>() failed!");
64 return DHCP_OPT_FAILED;
65 }
66 }
67
68 return m_pClientService->StopDhcpClient(ifname, bIpv6);
69 }
70
GetDhcpResult(const std::string & ifname,IDhcpResultNotify * pResultNotify,int timeouts)71 int DhcpService::GetDhcpResult(const std::string& ifname, IDhcpResultNotify *pResultNotify, int timeouts)
72 {
73 if (m_pClientService == nullptr) {
74 WIFI_LOGE("DhcpService::GetDhcpResult() error, m_pClientService = nullptr!");
75 return DHCP_OPT_FAILED;
76 }
77
78 return m_pClientService->GetDhcpResult(ifname, pResultNotify, timeouts);
79 }
80
RemoveDhcpResult(IDhcpResultNotify * pResultNotify)81 int DhcpService::RemoveDhcpResult(IDhcpResultNotify *pResultNotify)
82 {
83 if (m_pClientService == nullptr) {
84 WIFI_LOGE("DhcpService::RemoveDhcpResult() error, m_pClientService = nullptr!");
85 return DHCP_OPT_FAILED;
86 }
87
88 return m_pClientService->RemoveDhcpResult(pResultNotify);
89 }
90
GetDhcpInfo(const std::string & ifname,DhcpServiceInfo & dhcp)91 int DhcpService::GetDhcpInfo(const std::string& ifname, DhcpServiceInfo& dhcp)
92 {
93 if (m_pClientService == nullptr) {
94 WIFI_LOGE("DhcpService::GetDhcpInfo() error, m_pClientService = nullptr!");
95 return DHCP_OPT_FAILED;
96 }
97
98 return m_pClientService->GetDhcpInfo(ifname, dhcp);
99 }
100
RenewDhcpClient(const std::string & ifname)101 int DhcpService::RenewDhcpClient(const std::string& ifname)
102 {
103 if (m_pClientService == nullptr) {
104 WIFI_LOGE("DhcpService::RenewDhcpClient() error, m_pClientService = nullptr!");
105 return DHCP_OPT_FAILED;
106 }
107
108 return m_pClientService->RenewDhcpClient(ifname);
109 }
110
ReleaseDhcpClient(const std::string & ifname)111 int DhcpService::ReleaseDhcpClient(const std::string& ifname)
112 {
113 if (m_pClientService == nullptr) {
114 WIFI_LOGE("DhcpService::ReleaseDhcpClient() error, m_pClientService = nullptr!");
115 return DHCP_OPT_FAILED;
116 }
117
118 return m_pClientService->ReleaseDhcpClient(ifname);
119 }
120
StartDhcpServer(const std::string & ifname)121 int DhcpService::StartDhcpServer(const std::string& ifname)
122 {
123 if (!CheckIfaceValid(ifname)) {
124 return DHCP_OPT_FAILED;
125 }
126 if (InitServerService() != DHCP_OPT_SUCCESS) {
127 WIFI_LOGE("DhcpService::StartDhcpServer() InitServerService failed!");
128 return DHCP_OPT_FAILED;
129 }
130
131 return m_pServerService->StartDhcpServer(ifname);
132 }
133
StopDhcpServer(const std::string & ifname)134 int DhcpService::StopDhcpServer(const std::string& ifname)
135 {
136 if (InitServerService() != DHCP_OPT_SUCCESS) {
137 WIFI_LOGE("DhcpService::StopDhcpServer() InitServerService failed!");
138 return DHCP_OPT_FAILED;
139 }
140
141 return m_pServerService->StopDhcpServer(ifname);
142 }
143
GetServerStatus()144 int DhcpService::GetServerStatus()
145 {
146 if (InitServerService() != DHCP_OPT_SUCCESS) {
147 WIFI_LOGE("DhcpService::GetServerStatus() InitServerService failed!");
148 return DHCP_OPT_FAILED;
149 }
150
151 return m_pServerService->GetServerStatus();
152 }
153
PutDhcpRange(const std::string & tagName,const DhcpRange & range)154 int DhcpService::PutDhcpRange(const std::string& tagName, const DhcpRange& range)
155 {
156 if (InitServerService() != DHCP_OPT_SUCCESS) {
157 WIFI_LOGE("DhcpService::PutDhcpRange() InitServerService failed!");
158 return DHCP_OPT_FAILED;
159 }
160
161 return m_pServerService->PutDhcpRange(tagName, range);
162 }
163
RemoveDhcpRange(const std::string & tagName,const DhcpRange & range)164 int DhcpService::RemoveDhcpRange(const std::string& tagName, const DhcpRange& range)
165 {
166 if (InitServerService() != DHCP_OPT_SUCCESS) {
167 WIFI_LOGE("DhcpService::RemoveDhcpRange() InitServerService failed!");
168 return DHCP_OPT_FAILED;
169 }
170
171 return m_pServerService->RemoveDhcpRange(tagName, range);
172 }
173
RemoveAllDhcpRange(const std::string & tagName)174 int DhcpService::RemoveAllDhcpRange(const std::string& tagName)
175 {
176 if (InitServerService() != DHCP_OPT_SUCCESS) {
177 WIFI_LOGE("DhcpService::RemoveAllDhcpRange() InitServerService failed!");
178 return DHCP_OPT_FAILED;
179 }
180
181 return m_pServerService->RemoveAllDhcpRange(tagName);
182 }
183
SetDhcpRange(const std::string & ifname,const DhcpRange & range)184 int DhcpService::SetDhcpRange(const std::string& ifname, const DhcpRange& range)
185 {
186 if (InitServerService() != DHCP_OPT_SUCCESS) {
187 WIFI_LOGE("DhcpService::SetDhcpRange() InitServerService failed!");
188 return DHCP_OPT_FAILED;
189 }
190
191 return m_pServerService->SetDhcpRange(ifname, range);
192 }
193
SetDhcpRange(const std::string & ifname,const std::string & tagName)194 int DhcpService::SetDhcpRange(const std::string& ifname, const std::string& tagName)
195 {
196 if (InitServerService() != DHCP_OPT_SUCCESS) {
197 WIFI_LOGE("DhcpService::SetDhcpRange() tag InitServerService failed!");
198 return DHCP_OPT_FAILED;
199 }
200
201 return m_pServerService->SetDhcpRange(ifname, tagName);
202 }
203
GetLeases(const std::string & ifname,std::vector<std::string> & leases)204 int DhcpService::GetLeases(const std::string& ifname, std::vector<std::string>& leases)
205 {
206 if (InitServerService() != DHCP_OPT_SUCCESS) {
207 WIFI_LOGE("DhcpService::GetLeases() InitServerService failed!");
208 return DHCP_OPT_FAILED;
209 }
210
211 return m_pServerService->GetLeases(ifname, leases);
212 }
213
GetDhcpSerProExit(const std::string & ifname,IDhcpResultNotify * pResultNotify)214 int DhcpService::GetDhcpSerProExit(const std::string& ifname, IDhcpResultNotify *pResultNotify)
215 {
216 if (InitServerService() != DHCP_OPT_SUCCESS) {
217 WIFI_LOGE("DhcpService::GetDhcpSerProExit() InitServerService failed!");
218 return DHCP_OPT_FAILED;
219 }
220
221 return m_pServerService->GetDhcpSerProExit(ifname, pResultNotify);
222 }
223
InitServerService()224 int DhcpService::InitServerService()
225 {
226 if (m_pServerService == nullptr) {
227 m_pServerService = std::make_unique<DhcpServerService>();
228 if (m_pServerService == nullptr) {
229 WIFI_LOGE("DhcpService::InitServerService() std::make_unique<DhcpServerService>() failed!");
230 return DHCP_OPT_FAILED;
231 }
232 }
233 return DHCP_OPT_SUCCESS;
234 }
235
CheckIfaceValid(const std::string & ifname)236 bool DhcpService::CheckIfaceValid(const std::string& ifname)
237 {
238 struct if_nameindex *ifidxs, *ifni;
239
240 ifidxs = if_nameindex();
241 if (ifidxs == nullptr) {
242 WIFI_LOGE("can not get interfaces");
243 return false;
244 }
245 for (ifni = ifidxs; !(ifni->if_index == 0 && ifni->if_name == nullptr); ifni++) {
246 if (ifni->if_name == nullptr) {
247 WIFI_LOGW("ifni->if_name is nullptr");
248 continue;
249 }
250 if (strcmp(ifni->if_name, ifname.c_str()) == 0) {
251 if_freenameindex(ifidxs);
252 return true;
253 }
254 }
255 if_freenameindex(ifidxs);
256 WIFI_LOGE("invalid interface: %{public}s", ifname.c_str());
257 return false;
258 }
259 } // namespace Wifi
260 } // namespace OHOS
261