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