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