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 return GetCErrorCode(dhcpClientPtr->StartDhcpClient(routerConfig));
89 }
90
DealWifiDhcpCache(int32_t cmd,const IpCacheInfo & ipCacheInfo)91 DhcpErrorCode DealWifiDhcpCache(int32_t cmd, const IpCacheInfo &ipCacheInfo)
92 {
93 CHECK_PTR_RETURN(ipCacheInfo.ssid, DHCP_INVALID_PARAM);
94 CHECK_PTR_RETURN(ipCacheInfo.bssid, DHCP_INVALID_PARAM);
95 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
96 OHOS::DHCP::IpCacheInfo cacheInfo;
97 cacheInfo.ssid = ipCacheInfo.ssid;
98 cacheInfo.bssid = ipCacheInfo.bssid;
99 return GetCErrorCode(dhcpClientPtr->DealWifiDhcpCache(cmd, cacheInfo));
100 }
101
StopDhcpClient(const char * ifname,bool bIpv6)102 NO_SANITIZE("cfi") DhcpErrorCode StopDhcpClient(const char *ifname, bool bIpv6)
103 {
104 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
105 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
106 CHECK_PTR_RETURN(dhcpClientCallBack, DHCP_INVALID_PARAM);
107 dhcpClientCallBack->UnRegisterCallBack(ifname);
108 return GetCErrorCode(dhcpClientPtr->StopDhcpClient(ifname, bIpv6));
109 }
110
RegisterDhcpServerCallBack(const char * ifname,const ServerCallBack * event)111 NO_SANITIZE("cfi") DhcpErrorCode RegisterDhcpServerCallBack(const char *ifname, const ServerCallBack *event)
112 {
113 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
114 CHECK_PTR_RETURN(event, DHCP_INVALID_PARAM);
115 if (dhcpServerPtr == nullptr) {
116 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
117 }
118 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
119 #ifdef OHOS_ARCH_LITE
120 if (dhcpServerCallBack == nullptr) {
121 dhcpServerCallBack = std::shared_ptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
122 }
123 #else
124 if (dhcpServerCallBack == nullptr) {
125 dhcpServerCallBack = OHOS::sptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
126 }
127 #endif
128 CHECK_PTR_RETURN(dhcpServerCallBack, DHCP_INVALID_PARAM);
129 dhcpServerCallBack->RegisterCallBack(ifname, event);
130 return GetCErrorCode(dhcpServerPtr->RegisterDhcpServerCallBack(ifname, dhcpServerCallBack));
131 }
132
StartDhcpServer(const char * ifname)133 NO_SANITIZE("cfi") DhcpErrorCode StartDhcpServer(const char *ifname)
134 {
135 if (dhcpServerPtr == nullptr) {
136 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
137 }
138 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
139 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
140 return GetCErrorCode(dhcpServerPtr->StartDhcpServer(ifname));
141 }
142
StopDhcpServer(const char * ifname)143 NO_SANITIZE("cfi") DhcpErrorCode StopDhcpServer(const char *ifname)
144 {
145 if (dhcpServerPtr == nullptr) {
146 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
147 }
148 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
149 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
150 #ifdef OHOS_ARCH_LITE
151 if (dhcpServerCallBack == nullptr) {
152 dhcpServerCallBack = std::make_shared<DhcpServerCallBack>();
153 }
154 #else
155 if (dhcpServerCallBack == nullptr) {
156 dhcpServerCallBack = OHOS::sptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
157 }
158 #endif
159 CHECK_PTR_RETURN(dhcpServerCallBack, DHCP_INVALID_PARAM);
160 dhcpServerCallBack->UnRegisterCallBack(ifname);
161 return GetCErrorCode(dhcpServerPtr->StopDhcpServer(ifname));
162 }
163
SetDhcpRange(const char * ifname,const DhcpRange * range)164 NO_SANITIZE("cfi") DhcpErrorCode SetDhcpRange(const char *ifname, const DhcpRange *range)
165 {
166 if (dhcpServerPtr == nullptr) {
167 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
168 }
169 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
170 CHECK_PTR_RETURN(range, DHCP_INVALID_PARAM);
171 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
172 OHOS::DHCP::DhcpRange rangeNew;
173 rangeNew.iptype = range->iptype;
174 rangeNew.strStartip = range->strStartip;
175 rangeNew.strEndip = range->strEndip;
176 rangeNew.strSubnet = range->strSubnet;
177 rangeNew.strTagName = range->strTagName;
178 return GetCErrorCode(dhcpServerPtr->SetDhcpRange(ifname, rangeNew));
179 }
180
SetDhcpName(const char * ifname,const char * tagName)181 NO_SANITIZE("cfi") DhcpErrorCode SetDhcpName(const char *ifname, const char *tagName)
182 {
183 if (dhcpServerPtr == nullptr) {
184 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
185 }
186 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
187 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
188 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
189 return GetCErrorCode(dhcpServerPtr->SetDhcpName(ifname, tagName));
190 }
191
PutDhcpRange(const char * tagName,const DhcpRange * range)192 NO_SANITIZE("cfi") DhcpErrorCode PutDhcpRange(const char *tagName, const DhcpRange *range)
193 {
194 if (dhcpServerPtr == nullptr) {
195 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
196 }
197 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
198 CHECK_PTR_RETURN(range, DHCP_INVALID_PARAM);
199 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
200 OHOS::DHCP::DhcpRange rangeNew;
201 rangeNew.iptype = range->iptype;
202 rangeNew.strStartip = range->strStartip;
203 rangeNew.strEndip = range->strEndip;
204 rangeNew.strSubnet = range->strSubnet;
205 rangeNew.strTagName = range->strTagName;
206 return GetCErrorCode(dhcpServerPtr->PutDhcpRange(tagName, rangeNew));
207 }
208
RemoveAllDhcpRange(const char * tagName)209 NO_SANITIZE("cfi") DhcpErrorCode RemoveAllDhcpRange(const char *tagName)
210 {
211 if (dhcpServerPtr == nullptr) {
212 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
213 }
214 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
215 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
216 return GetCErrorCode(dhcpServerPtr->RemoveAllDhcpRange(tagName));
217 }
218
RemoveDhcpRange(const char * tagName,const void * range)219 DhcpErrorCode RemoveDhcpRange(const char *tagName, const void *range)
220 {
221 if (dhcpServerPtr == nullptr) {
222 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
223 }
224 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
225 CHECK_PTR_RETURN(range, DHCP_INVALID_PARAM);
226 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
227 return GetCErrorCode(dhcpServerPtr->RemoveDhcpRange(tagName, *(OHOS::DHCP::DhcpRange *)range));
228 }
229
ParseClientInfos(int staNumber,DhcpStationInfo * staInfo,int * staSize,std::vector<std::string> & vecInfo)230 DhcpErrorCode ParseClientInfos(int staNumber, DhcpStationInfo *staInfo, int *staSize, std::vector<std::string> &vecInfo)
231 {
232 int size = (int)vecInfo.size();
233 DHCP_LOGI("ParseClientInfos staNumber:%{public}d size:%{public}d", staNumber, size);
234 int i = 0;
235 for (i = 0; i < size; i++) {
236 std::vector<std::string> tmp;
237 std::string str = vecInfo[i];
238 OHOS::SplitStr(str, " ", tmp);
239 if (tmp.empty()) {
240 continue;
241 }
242 if (tmp[DHCP_LEASE_MAC_ADDR_POS] == "duid") {
243 break;
244 }
245 if (tmp.size() < DHCP_LEASE_FORMAT_SIZE) {
246 continue;
247 }
248 std::string mac = tmp[DHCP_LEASE_MAC_ADDR_POS];
249 std::string ipAddr = tmp[DHCP_LEASE_IP_ADDR_POS];
250 std::string deviceName = "";
251 if (tmp.size() >= DHCP_LEASE_FORMAT_MAX_SIZE) {
252 deviceName = tmp[DHCP_LEASE_HOSTNAME_POS];
253 }
254 if (i >= staNumber) {
255 break;
256 }
257 if (strcpy_s(staInfo[i].macAddr, MAC_ADDR_MAX_LEN, mac.c_str()) != EOK) {
258 DHCP_LOGE("get dhcp client info, cpy mac failed! srclen=%{public}zu", strlen(mac.c_str()));
259 return DHCP_FAILED;
260 }
261 if (strcpy_s(staInfo[i].ipAddr, INET_ADDRSTRLEN, ipAddr.c_str()) != EOK) {
262 DHCP_LOGE("get dhcp client info, cpy ip failed!");
263 return DHCP_FAILED;
264 }
265 if (strcpy_s(staInfo[i].deviceName, DHCP_LEASE_DATA_MAX_LEN, deviceName.c_str()) != EOK) {
266 DHCP_LOGE("get dhcp client info, cpy name failed!");
267 return DHCP_FAILED;
268 }
269 }
270 *staSize = i;
271 DHCP_LOGI("GetDhcpClientInfos staNumber:%{public}d staSize:%{public}d", staNumber, *staSize);
272 return DHCP_SUCCESS;
273 }
274
GetDhcpClientInfos(const char * ifname,int staNumber,DhcpStationInfo * staInfo,int * staSize)275 DhcpErrorCode GetDhcpClientInfos(const char *ifname, int staNumber, DhcpStationInfo *staInfo, int *staSize)
276 {
277 if (dhcpServerPtr == nullptr) {
278 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
279 }
280 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
281 CHECK_PTR_RETURN(staInfo, DHCP_INVALID_PARAM);
282 CHECK_PTR_RETURN(staSize, DHCP_INVALID_PARAM);
283 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
284 std::vector<std::string> vecInfo;
285 DhcpErrorCode ret = GetCErrorCode(dhcpServerPtr->GetDhcpClientInfos(ifname, vecInfo));
286 if (ret != DHCP_SUCCESS) {
287 DHCP_LOGE("GetDhcpClientInfos failed!");
288 return DHCP_FAILED;
289 }
290 return ParseClientInfos(staNumber, staInfo, staSize, vecInfo);
291 }
292
UpdateLeasesTime(const char * leaseTime)293 NO_SANITIZE("cfi") DhcpErrorCode UpdateLeasesTime(const char *leaseTime)
294 {
295 if (dhcpServerPtr == nullptr) {
296 dhcpServerPtr = OHOS::DHCP::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
297 }
298 CHECK_PTR_RETURN(leaseTime, DHCP_INVALID_PARAM);
299 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
300 return GetCErrorCode(dhcpServerPtr->UpdateLeasesTime(leaseTime));
301 }
302
StopDhcpdClientSa(void)303 DhcpErrorCode StopDhcpdClientSa(void)
304 {
305 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
306 return GetCErrorCode(dhcpClientPtr->StopClientSa());
307 }
308
StopDhcpdServerSa(void)309 DhcpErrorCode StopDhcpdServerSa(void)
310 {
311 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
312 return GetCErrorCode(dhcpServerPtr->StopServerSa());
313 }