1 /*
2 * Copyright (C) 2021-2023 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 #include "kits/c/dhcp_c_api.h"
16 #include "inner_api/dhcp_client.h"
17 #include "inner_api/dhcp_server.h"
18 #include "dhcp_sdk_define.h"
19 #include "dhcp_c_utils.h"
20 #include "dhcp_event.h"
21 #include "dhcp_logger.h"
22 #ifndef OHOS_ARCH_LITE
23 #include <string_ex.h>
24 #endif
25 DEFINE_DHCPLOG_DHCP_LABEL("DhcpCService");
26 namespace {
27 std::shared_ptr<OHOS::DHCP::DhcpClient> dhcpClientPtr = nullptr;
28 std::shared_ptr<OHOS::DHCP::DhcpServer> dhcpServerPtr = nullptr;
29 }
30
31 #ifdef OHOS_ARCH_LITE
32 static std::shared_ptr<DhcpClientCallBack> dhcpClientCallBack = nullptr;
33 static std::shared_ptr<DhcpServerCallBack> dhcpServerCallBack = nullptr;
34 #else
35 static OHOS::sptr<DhcpClientCallBack> dhcpClientCallBack = nullptr;
36 static OHOS::sptr<DhcpServerCallBack> dhcpServerCallBack = nullptr;
37 #endif
38
RegisterDhcpClientCallBack(const char * ifname,const ClientCallBack * event)39 NO_SANITIZE("cfi") DhcpErrorCode RegisterDhcpClientCallBack(const char *ifname, const ClientCallBack *event)
40 {
41 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
42 CHECK_PTR_RETURN(event, DHCP_INVALID_PARAM);
43 if (dhcpClientPtr == nullptr) {
44 dhcpClientPtr = OHOS::DHCP::DhcpClient::GetInstance(DHCP_CLIENT_ABILITY_ID);
45 }
46 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
47 #ifdef OHOS_ARCH_LITE
48 if (dhcpClientCallBack == nullptr) {
49 dhcpClientCallBack = std::shared_ptr<DhcpClientCallBack>(new (std::nothrow)DhcpClientCallBack());
50 }
51 #else
52 if (dhcpClientCallBack == nullptr) {
53 dhcpClientCallBack = OHOS::sptr<DhcpClientCallBack>(new (std::nothrow)DhcpClientCallBack());
54 }
55 #endif
56 CHECK_PTR_RETURN(dhcpClientCallBack, DHCP_INVALID_PARAM);
57 dhcpClientCallBack->RegisterCallBack(ifname, event);
58 return GetCErrorCode(dhcpClientPtr->RegisterDhcpClientCallBack(ifname, dhcpClientCallBack));
59 }
60
RegisterDhcpClientReportCallBack(const char * ifname,const DhcpClientReport * event)61 DhcpErrorCode RegisterDhcpClientReportCallBack(const char *ifname, const DhcpClientReport *event)
62 {
63 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
64 CHECK_PTR_RETURN(event, DHCP_INVALID_PARAM);
65 #ifdef OHOS_ARCH_LITE
66 if (dhcpClientCallBack == nullptr) {
67 dhcpClientCallBack = std::make_shared<DhcpClientCallBack>();
68 }
69 #else
70 if (dhcpClientCallBack == nullptr) {
71 dhcpClientCallBack = OHOS::sptr<DhcpClientCallBack>(new (std::nothrow)DhcpClientCallBack());
72 }
73 #endif
74 CHECK_PTR_RETURN(dhcpClientCallBack, DHCP_INVALID_PARAM);
75 dhcpClientCallBack->RegisterDhcpClientReportCallBack(ifname, event);
76 return DHCP_SUCCESS;
77 }
78
StartDhcpClient(const RouterConfig & config)79 NO_SANITIZE("cfi") DhcpErrorCode StartDhcpClient(const RouterConfig &config)
80 {
81 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
82 OHOS::DHCP::RouterConfig routerConfig;
83 routerConfig.ifname = config.ifname;
84 routerConfig.bssid = config.bssid;
85 routerConfig.prohibitUseCacheIp = config.prohibitUseCacheIp;
86 routerConfig.bIpv6 = config.bIpv6;
87 routerConfig.bSpecificNetwork = config.bSpecificNetwork;
88 routerConfig.isStaticIpv4 = config.isStaticIpv4;
89 routerConfig.bIpv4 = config.bIpv4;
90 return GetCErrorCode(dhcpClientPtr->StartDhcpClient(routerConfig));
91 }
92
DealWifiDhcpCache(int32_t cmd,const IpCacheInfo & ipCacheInfo)93 DhcpErrorCode DealWifiDhcpCache(int32_t cmd, const IpCacheInfo &ipCacheInfo)
94 {
95 CHECK_PTR_RETURN(ipCacheInfo.ssid, DHCP_INVALID_PARAM);
96 CHECK_PTR_RETURN(ipCacheInfo.bssid, DHCP_INVALID_PARAM);
97 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
98 OHOS::DHCP::IpCacheInfo cacheInfo;
99 cacheInfo.ssid = ipCacheInfo.ssid;
100 cacheInfo.bssid = ipCacheInfo.bssid;
101 return GetCErrorCode(dhcpClientPtr->DealWifiDhcpCache(cmd, cacheInfo));
102 }
103
StopDhcpClient(const char * ifname,bool bIpv6,bool bIpv4)104 NO_SANITIZE("cfi") DhcpErrorCode StopDhcpClient(const char *ifname, bool bIpv6, bool bIpv4)
105 {
106 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
107 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
108 CHECK_PTR_RETURN(dhcpClientCallBack, DHCP_INVALID_PARAM);
109 if (bIpv4) {
110 dhcpClientCallBack->UnRegisterCallBack(ifname);
111 }
112 return GetCErrorCode(dhcpClientPtr->StopDhcpClient(ifname, bIpv6, bIpv4));
113 }
114
RegisterDhcpServerCallBack(const char * ifname,const ServerCallBack * event)115 NO_SANITIZE("cfi") DhcpErrorCode RegisterDhcpServerCallBack(const char *ifname, const ServerCallBack *event)
116 {
117 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
118 CHECK_PTR_RETURN(event, DHCP_INVALID_PARAM);
119 if (dhcpServerPtr == nullptr) {
120 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
121 }
122 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
123 #ifdef OHOS_ARCH_LITE
124 if (dhcpServerCallBack == nullptr) {
125 dhcpServerCallBack = std::shared_ptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
126 }
127 #else
128 if (dhcpServerCallBack == nullptr) {
129 dhcpServerCallBack = OHOS::sptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
130 }
131 #endif
132 CHECK_PTR_RETURN(dhcpServerCallBack, DHCP_INVALID_PARAM);
133 dhcpServerCallBack->RegisterCallBack(ifname, event);
134 return GetCErrorCode(dhcpServerPtr->RegisterDhcpServerCallBack(ifname, dhcpServerCallBack));
135 }
136
StartDhcpServer(const char * ifname)137 NO_SANITIZE("cfi") DhcpErrorCode StartDhcpServer(const char *ifname)
138 {
139 if (dhcpServerPtr == nullptr) {
140 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
141 }
142 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
143 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
144 return GetCErrorCode(dhcpServerPtr->StartDhcpServer(ifname));
145 }
146
StopDhcpServer(const char * ifname)147 NO_SANITIZE("cfi") DhcpErrorCode StopDhcpServer(const char *ifname)
148 {
149 if (dhcpServerPtr == nullptr) {
150 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
151 }
152 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
153 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
154 #ifdef OHOS_ARCH_LITE
155 if (dhcpServerCallBack == nullptr) {
156 dhcpServerCallBack = std::make_shared<DhcpServerCallBack>();
157 }
158 #else
159 if (dhcpServerCallBack == nullptr) {
160 dhcpServerCallBack = OHOS::sptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
161 }
162 #endif
163 CHECK_PTR_RETURN(dhcpServerCallBack, DHCP_INVALID_PARAM);
164 dhcpServerCallBack->UnRegisterCallBack(ifname);
165 return GetCErrorCode(dhcpServerPtr->StopDhcpServer(ifname));
166 }
167
SetDhcpRange(const char * ifname,const DhcpRange * range)168 NO_SANITIZE("cfi") DhcpErrorCode SetDhcpRange(const char *ifname, const DhcpRange *range)
169 {
170 if (dhcpServerPtr == nullptr) {
171 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
172 }
173 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
174 CHECK_PTR_RETURN(range, DHCP_INVALID_PARAM);
175 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
176 OHOS::DHCP::DhcpRange rangeNew;
177 rangeNew.iptype = range->iptype;
178 rangeNew.strStartip = range->strStartip;
179 rangeNew.strEndip = range->strEndip;
180 rangeNew.strSubnet = range->strSubnet;
181 rangeNew.strTagName = range->strTagName;
182 return GetCErrorCode(dhcpServerPtr->SetDhcpRange(ifname, rangeNew));
183 }
184
SetDhcpName(const char * ifname,const char * tagName)185 NO_SANITIZE("cfi") DhcpErrorCode SetDhcpName(const char *ifname, const char *tagName)
186 {
187 if (dhcpServerPtr == nullptr) {
188 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
189 }
190 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
191 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
192 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
193 return GetCErrorCode(dhcpServerPtr->SetDhcpName(ifname, tagName));
194 }
195
PutDhcpRange(const char * tagName,const DhcpRange * range)196 NO_SANITIZE("cfi") DhcpErrorCode PutDhcpRange(const char *tagName, const DhcpRange *range)
197 {
198 if (dhcpServerPtr == nullptr) {
199 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
200 }
201 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
202 CHECK_PTR_RETURN(range, DHCP_INVALID_PARAM);
203 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
204 OHOS::DHCP::DhcpRange rangeNew;
205 rangeNew.iptype = range->iptype;
206 rangeNew.strStartip = range->strStartip;
207 rangeNew.strEndip = range->strEndip;
208 rangeNew.strSubnet = range->strSubnet;
209 rangeNew.strTagName = range->strTagName;
210 return GetCErrorCode(dhcpServerPtr->PutDhcpRange(tagName, rangeNew));
211 }
212
RemoveAllDhcpRange(const char * tagName)213 NO_SANITIZE("cfi") DhcpErrorCode RemoveAllDhcpRange(const char *tagName)
214 {
215 if (dhcpServerPtr == nullptr) {
216 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
217 }
218 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
219 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
220 return GetCErrorCode(dhcpServerPtr->RemoveAllDhcpRange(tagName));
221 }
222
RemoveDhcpRange(const char * tagName,const void * range)223 DhcpErrorCode RemoveDhcpRange(const char *tagName, const void *range)
224 {
225 if (dhcpServerPtr == nullptr) {
226 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
227 }
228 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
229 CHECK_PTR_RETURN(range, DHCP_INVALID_PARAM);
230 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
231 return GetCErrorCode(dhcpServerPtr->RemoveDhcpRange(tagName, *(OHOS::DHCP::DhcpRange *)range));
232 }
233
ParseClientInfos(int staNumber,DhcpStationInfo * staInfo,int * staSize,std::vector<std::string> & vecInfo)234 DhcpErrorCode ParseClientInfos(int staNumber, DhcpStationInfo *staInfo, int *staSize, std::vector<std::string> &vecInfo)
235 {
236 int size = (int)vecInfo.size();
237 DHCP_LOGI("ParseClientInfos staNumber:%{public}d size:%{public}d", staNumber, size);
238 int i = 0;
239 for (i = 0; i < size; i++) {
240 std::vector<std::string> tmp;
241 std::string str = vecInfo[i];
242 OHOS::SplitStr(str, " ", tmp);
243 if (tmp.empty()) {
244 continue;
245 }
246 if (tmp[DHCP_LEASE_MAC_ADDR_POS] == "duid") {
247 break;
248 }
249 if (tmp.size() < DHCP_LEASE_FORMAT_SIZE) {
250 continue;
251 }
252 std::string mac = tmp[DHCP_LEASE_MAC_ADDR_POS];
253 std::string ipAddr = tmp[DHCP_LEASE_IP_ADDR_POS];
254 std::string deviceName = "";
255 if (tmp.size() >= DHCP_LEASE_FORMAT_MAX_SIZE) {
256 deviceName = tmp[DHCP_LEASE_HOSTNAME_POS];
257 }
258 if (i >= staNumber) {
259 break;
260 }
261 if (strcpy_s(staInfo[i].macAddr, MAC_ADDR_MAX_LEN, mac.c_str()) != EOK) {
262 DHCP_LOGE("get dhcp client info, cpy mac failed! srclen=%{public}zu", strlen(mac.c_str()));
263 return DHCP_FAILED;
264 }
265 if (strcpy_s(staInfo[i].ipAddr, INET_ADDRSTRLEN, ipAddr.c_str()) != EOK) {
266 DHCP_LOGE("get dhcp client info, cpy ip failed!");
267 return DHCP_FAILED;
268 }
269 if (strcpy_s(staInfo[i].deviceName, DHCP_LEASE_DATA_MAX_LEN, deviceName.c_str()) != EOK) {
270 DHCP_LOGE("get dhcp client info, cpy name failed!");
271 return DHCP_FAILED;
272 }
273 }
274 *staSize = i;
275 DHCP_LOGI("GetDhcpClientInfos staNumber:%{public}d staSize:%{public}d", staNumber, *staSize);
276 return DHCP_SUCCESS;
277 }
278
GetDhcpClientInfos(const char * ifname,int staNumber,DhcpStationInfo * staInfo,int * staSize)279 DhcpErrorCode GetDhcpClientInfos(const char *ifname, int staNumber, DhcpStationInfo *staInfo, int *staSize)
280 {
281 if (dhcpServerPtr == nullptr) {
282 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
283 }
284 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
285 CHECK_PTR_RETURN(staInfo, DHCP_INVALID_PARAM);
286 CHECK_PTR_RETURN(staSize, DHCP_INVALID_PARAM);
287 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
288 std::vector<std::string> vecInfo;
289 DhcpErrorCode ret = GetCErrorCode(dhcpServerPtr->GetDhcpClientInfos(ifname, vecInfo));
290 if (ret != DHCP_SUCCESS) {
291 DHCP_LOGE("GetDhcpClientInfos failed!");
292 return DHCP_FAILED;
293 }
294 return ParseClientInfos(staNumber, staInfo, staSize, vecInfo);
295 }
296
UpdateLeasesTime(const char * leaseTime)297 NO_SANITIZE("cfi") DhcpErrorCode UpdateLeasesTime(const char *leaseTime)
298 {
299 if (dhcpServerPtr == nullptr) {
300 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
301 }
302 CHECK_PTR_RETURN(leaseTime, DHCP_INVALID_PARAM);
303 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
304 return GetCErrorCode(dhcpServerPtr->UpdateLeasesTime(leaseTime));
305 }
306
StopDhcpdClientSa(void)307 DhcpErrorCode StopDhcpdClientSa(void)
308 {
309 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
310 return GetCErrorCode(dhcpClientPtr->StopClientSa());
311 }
312
StopDhcpdServerSa(void)313 DhcpErrorCode StopDhcpdServerSa(void)
314 {
315 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
316 return GetCErrorCode(dhcpServerPtr->StopServerSa());
317 }