• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "dhcp_func.h"
16 
17 #include <unistd.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 
24 #include "securec.h"
25 #include "wifi_logger.h"
26 #include "dhcp_event_subscriber.h"
27 
28 DEFINE_WIFILOG_DHCP_LABEL("DhcpFunc");
29 
30 namespace OHOS {
31 namespace Wifi {
Ip4StrConToInt(const std::string & strIp,uint32_t & uIp,bool bHost)32 bool DhcpFunc::Ip4StrConToInt(const std::string& strIp, uint32_t& uIp, bool bHost)
33 {
34     if (strIp.empty()) {
35         WIFI_LOGE("Ip4StrConToInt error, strIp is empty()!");
36         return false;
37     }
38 
39     struct in_addr addr4;
40     int nRet = inet_pton(AF_INET, strIp.c_str(), &addr4);
41     if (nRet != 1) {
42         WIFI_LOGE("Ip4StrConToInt strIp:%{private}s failed, nRet:%{public}d!", strIp.c_str(), nRet);
43         if (nRet == 0) {
44             WIFI_LOGE("Ip4StrConToInt strIp:%{private}s not in presentation format!", strIp.c_str());
45         } else {
46             WIFI_LOGE("Ip4StrConToInt strIp:%{private}s inet_pton not contain a valid address!", strIp.c_str());
47         }
48         return false;
49     }
50 
51     if (bHost) {
52         uIp = ntohl(addr4.s_addr);
53     } else {
54         uIp = addr4.s_addr;
55     }
56 
57     return true;
58 }
59 
Ip4IntConToStr(uint32_t uIp,bool bHost)60 std::string DhcpFunc::Ip4IntConToStr(uint32_t uIp, bool bHost)
61 {
62     char bufIp4[INET_ADDRSTRLEN] = {0};
63     struct in_addr addr4;
64     if (bHost) {
65         addr4.s_addr = htonl(uIp);
66     } else {
67         addr4.s_addr = uIp;
68     }
69 
70     std::string strIp = "";
71     if (inet_ntop(AF_INET, &addr4, bufIp4, INET_ADDRSTRLEN) == NULL) {
72         WIFI_LOGE("Ip4IntConToStr uIp:%{private}u failed, inet_ntop NULL!", uIp);
73     } else {
74         strIp = bufIp4;
75         WIFI_LOGI("Ip4IntConToStr uIp:%{private}u -> strIp:%{private}s.", uIp, strIp.c_str());
76     }
77 
78     return strIp;
79 }
80 
Ip6StrConToChar(const std::string & strIp,uint8_t chIp[],size_t uSize)81 bool DhcpFunc::Ip6StrConToChar(const std::string& strIp, uint8_t chIp[], size_t uSize)
82 {
83     if (strIp.empty()) {
84         WIFI_LOGE("Ip6StrConToChar param error, strIp is empty()!");
85         return false;
86     }
87 
88     struct in6_addr addr6;
89     if (memset_s(&addr6, sizeof(addr6), 0, sizeof(addr6)) != EOK) {
90         return false;
91     }
92     int nRet = inet_pton(AF_INET6, strIp.c_str(), &addr6);
93     if (nRet != 1) {
94         WIFI_LOGE("Ip6StrConToChar inet_pton strIp:%{private}s failed, nRet:%{public}d!", strIp.c_str(), nRet);
95         if (nRet == 0) {
96             WIFI_LOGE("Ip6StrConToChar strIp:%{private}s not in presentation format!", strIp.c_str());
97         } else {
98             WIFI_LOGE("Ip6StrConToChar strIp:%{private}s inet_pton not contain a valid address!", strIp.c_str());
99         }
100         return false;
101     }
102 
103     for (size_t i = 0; i < uSize; i++) {
104         chIp[i] = addr6.s6_addr[i];
105     }
106 
107     return true;
108 }
109 
Ip6CharConToStr(uint8_t chIp[],int size)110 std::string DhcpFunc::Ip6CharConToStr(uint8_t chIp[], int size)
111 {
112     if (size <= 0) {
113         WIFI_LOGE("Ip6CharConToStr param error, size:%{public}d!", size);
114         return "";
115     }
116 
117     std::string strIp = "";
118     char bufIp6[INET6_ADDRSTRLEN] = {0};
119     struct in6_addr addr6;
120     if (memcpy_s(addr6.s6_addr, sizeof(addr6.s6_addr), &chIp, size) != EOK) {
121         return "";
122     }
123     if (inet_ntop(AF_INET6, &addr6, bufIp6, INET6_ADDRSTRLEN) == NULL) {
124         WIFI_LOGE("Ip6CharConToStr chIp failed, inet_ntop NULL!");
125     } else {
126         strIp = bufIp6;
127         WIFI_LOGI("Ip6CharConToStr chIp -> strIp:%{private}s.", strIp.c_str());
128     }
129 
130     return strIp;
131 }
132 
CheckIpStr(const std::string & strIp)133 bool DhcpFunc::CheckIpStr(const std::string& strIp)
134 {
135     if (strIp.empty()) {
136         WIFI_LOGE("CheckIpStr param error, strIp is empty()!");
137         return false;
138     }
139 
140     bool bIp4 = false;
141     bool bIp6 = false;
142     std::string::size_type idx = strIp.find(IP4_SEPARATOR);
143     if (idx != std::string::npos) {
144         bIp4 = true;
145     }
146     idx = strIp.find(IP6_SEPARATOR);
147     if (idx != std::string::npos) {
148         bIp6 = true;
149     }
150     if ((!bIp4 && !bIp6) || (bIp4 && bIp6)) {
151         WIFI_LOGE("CheckIpStr strIp:%{private}s error, bIp4:%{public}d,bIp6:%{public}d!", strIp.c_str(), bIp4, bIp6);
152         return false;
153     }
154 
155     if (bIp4) {
156         uint32_t uIp = 0;
157         if (!Ip4StrConToInt(strIp, uIp)) {
158             WIFI_LOGE("CheckIpStr Ip4StrConToInt failed, strIp:%{private}s.", strIp.c_str());
159             return false;
160         }
161     } else {
162         uint8_t	addr6[sizeof(struct in6_addr)] = {0};
163         if (!Ip6StrConToChar(strIp, addr6, sizeof(struct in6_addr))) {
164             WIFI_LOGE("CheckIpStr Ip6StrConToChar failed, strIp:%{private}s.", strIp.c_str());
165             return false;
166         }
167     }
168 
169     return true;
170 }
171 
GetLocalIp(const std::string strInf,std::string & strIp,std::string & strMask)172 int DhcpFunc::GetLocalIp(const std::string strInf, std::string& strIp, std::string& strMask)
173 {
174     if (strInf.empty()) {
175         WIFI_LOGE("GetLocalIp param error, strInf is empty!");
176         return DHCP_OPT_ERROR;
177     }
178 
179     int fd;
180     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
181         WIFI_LOGE("GetLocalIp strInf:%{public}s failed, socket err:%{public}d!", strInf.c_str(), errno);
182         return DHCP_OPT_FAILED;
183     }
184 
185     struct ifreq iface;
186     if (memset_s(&iface, sizeof(iface), 0, sizeof(iface)) != EOK) {
187         close(fd);
188         return DHCP_OPT_FAILED;
189     }
190     if (strncpy_s(iface.ifr_name, IFNAMSIZ, strInf.c_str(), IFNAMSIZ - 1) != EOK) {
191         close(fd);
192         return DHCP_OPT_FAILED;
193     }
194     iface.ifr_name[IFNAMSIZ - 1] = 0;
195 
196     /* inet addr */
197     if (ioctl(fd, SIOCGIFADDR, &iface) < 0) {
198         WIFI_LOGE("GetLocalIp() %{public}s failed, SIOCGIFADDR err:%{public}d!", strInf.c_str(), errno);
199         close(fd);
200         return DHCP_OPT_FAILED;
201     }
202     struct sockaddr_in *pSockIn = (struct sockaddr_in *)&iface.ifr_addr;
203     char bufIp4[INET_ADDRSTRLEN] = {0};
204     if (inet_ntop(AF_INET, &(pSockIn->sin_addr), bufIp4, INET_ADDRSTRLEN) != nullptr) {
205         strIp = bufIp4;
206     }
207 
208     /* netmask addr */
209     if (ioctl(fd, SIOCGIFNETMASK, &iface) < 0) {
210         WIFI_LOGE("GetLocalIp() %{public}s failed, SIOCGIFNETMASK err:%{public}d!", strInf.c_str(), errno);
211         close(fd);
212         return DHCP_OPT_FAILED;
213     }
214     pSockIn = (struct sockaddr_in *)&iface.ifr_addr;
215     char bufMask[INET_ADDRSTRLEN] = {0};
216     if (inet_ntop(AF_INET, &(pSockIn->sin_addr), bufMask, INET_ADDRSTRLEN) != nullptr) {
217         strMask = bufMask;
218     }
219 
220     close(fd);
221     return DHCP_OPT_SUCCESS;
222 }
223 
GetLocalMac(const std::string ethInf,std::string & ethMac)224 int DhcpFunc::GetLocalMac(const std::string ethInf, std::string& ethMac)
225 {
226     struct ifreq ifr;
227     int sd = 0;
228 
229     bzero(&ifr, sizeof(struct ifreq));
230     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
231         WIFI_LOGE("GetLocalMac socket ethInf:%{public}s,error:%{public}d!", ethInf.c_str(), errno);
232         return -1;
233     }
234 
235     if (strncpy_s(ifr.ifr_name, IFNAMSIZ, ethInf.c_str(), IFNAMSIZ - 1) != EOK) {
236         close(sd);
237         return -1;
238     }
239 
240     if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0) {
241         WIFI_LOGE("GetLocalMac ioctl ethInf:%{public}s,error:%{public}d!", ethInf.c_str(), errno);
242         close(sd);
243         return -1;
244     }
245 
246     char mac[ETH_MAC_ADDR_LEN * ETH_MAC_ADDR_CHAR_NUM] = { 0 };
247     int nRes = snprintf_s(mac,
248         ETH_MAC_ADDR_LEN * ETH_MAC_ADDR_CHAR_NUM,
249         ETH_MAC_ADDR_LEN * ETH_MAC_ADDR_CHAR_NUM - 1,
250         "%02x:%02x:%02x:%02x:%02x:%02x",
251         (unsigned char)ifr.ifr_hwaddr.sa_data[ETH_MAC_ADDR_INDEX_0],
252         (unsigned char)ifr.ifr_hwaddr.sa_data[ETH_MAC_ADDR_INDEX_1],
253         (unsigned char)ifr.ifr_hwaddr.sa_data[ETH_MAC_ADDR_INDEX_2],
254         (unsigned char)ifr.ifr_hwaddr.sa_data[ETH_MAC_ADDR_INDEX_3],
255         (unsigned char)ifr.ifr_hwaddr.sa_data[ETH_MAC_ADDR_INDEX_4],
256         (unsigned char)ifr.ifr_hwaddr.sa_data[ETH_MAC_ADDR_INDEX_5]);
257     if (nRes < 0) {
258         WIFI_LOGE("GetLocalMac snprintf_s ethInf:%{public}s,error:%{public}d!", ethInf.c_str(), errno);
259         close(sd);
260         return -1;
261     }
262     ethMac = mac;
263     close(sd);
264     return 0;
265 }
266 
CheckRangeNetwork(const std::string strInf,const std::string strBegin,const std::string strEnd)267 int DhcpFunc::CheckRangeNetwork(const std::string strInf, const std::string strBegin, const std::string strEnd)
268 {
269     if (strInf.empty() || strBegin.empty() || strEnd.empty()) {
270         WIFI_LOGE("CheckRangeNetwork param error, strInf or strBegin or strEnd is empty!");
271         return DHCP_OPT_ERROR;
272     }
273 
274     std::string strIp, strMask;
275     if (GetLocalIp(strInf, strIp, strMask) != DHCP_OPT_SUCCESS) {
276         WIFI_LOGE("CheckRangeNetwork get %{public}s local ip failed", strInf.c_str());
277         return DHCP_OPT_FAILED;
278     }
279 
280     uint32_t uIp, uMask, uBegin, uEnd;
281     if (!Ip4StrConToInt(strIp, uIp, false) || !Ip4StrConToInt(strMask, uMask, false) ||
282         !Ip4StrConToInt(strBegin, uBegin, false) || !Ip4StrConToInt(strEnd, uEnd, false)) {
283         WIFI_LOGE("CheckRangeNetwork %{public}s Ip4StrConToInt failed", strInf.c_str());
284         return DHCP_OPT_FAILED;
285     }
286 
287     if (!CheckSameNetwork(uIp, uBegin, uMask)) {
288         WIFI_LOGE("Check %{public}s %{public}s %{public}s failed", strInf.c_str(), strIp.c_str(), strBegin.c_str());
289         return DHCP_OPT_FAILED;
290     }
291     if (!CheckSameNetwork(uIp, uEnd, uMask)) {
292         WIFI_LOGE("Check end %{public}s %{public}s %{public}s failed", strInf.c_str(), strIp.c_str(), strEnd.c_str());
293         return DHCP_OPT_FAILED;
294     }
295     return DHCP_OPT_SUCCESS;
296 }
297 
CheckSameNetwork(const uint32_t srcIp,const uint32_t dstIp,const uint32_t maskIp)298 bool DhcpFunc::CheckSameNetwork(const uint32_t srcIp, const uint32_t dstIp, const uint32_t maskIp)
299 {
300     uint32_t srcNet = srcIp & maskIp;
301     uint32_t dstNet = dstIp & maskIp;
302     return (srcNet == dstNet);
303 }
304 
IsExistFile(const std::string & filename)305 bool DhcpFunc::IsExistFile(const std::string& filename)
306 {
307     bool bExist = false;
308     std::fstream ioFile;
309     ioFile.open(filename.c_str(), std::ios::in);
310     if (ioFile) {
311         bExist = true;
312     }
313     ioFile.close();
314 
315     return bExist;
316 }
317 
CreateFile(const std::string & filename,const std::string & filedata)318 bool DhcpFunc::CreateFile(const std::string& filename, const std::string& filedata)
319 {
320     std::ofstream outFile;
321     outFile.open(filename.c_str());
322     outFile.flush();
323     outFile << filedata << std::endl;
324     outFile.close();
325     return true;
326 }
327 
RemoveFile(const std::string & filename)328 bool DhcpFunc::RemoveFile(const std::string& filename)
329 {
330     if (std::remove(filename.c_str()) != 0) {
331         WIFI_LOGE("RemoveFile filename:%{public}s failed!", filename.c_str());
332         return false;
333     }
334     WIFI_LOGI("RemoveFile filename:%{public}s success.", filename.c_str());
335     return true;
336 }
337 
AddFileLineData(const std::string & filename,const std::string & prevdata,const std::string & linedata)338 bool DhcpFunc::AddFileLineData(const std::string& filename, const std::string& prevdata, const std::string& linedata)
339 {
340     bool bAdd = false;
341     std::ifstream inFile;
342     inFile.open(filename.c_str());
343     std::string strFileData = "";
344     std::string strTemp = "";
345     char tmpLineData[1024] = {0};
346     while (inFile.getline(tmpLineData, sizeof(tmpLineData))) {
347         strTemp = tmpLineData;
348         strFileData += strTemp;
349         strFileData += "\n";
350         if (strTemp == prevdata) {
351             strFileData += linedata;
352             bAdd = true;
353         }
354     }
355     inFile.close();
356 
357     if (bAdd) {
358         std::ofstream outFile;
359         outFile.open(filename.c_str());
360         outFile.flush();
361         WIFI_LOGI("AddFileLineData Reflush filename:%{public}s, strFileData:%{public}s.",
362             filename.c_str(), strFileData.c_str());
363         outFile << strFileData;
364         outFile.close();
365     }
366     return true;
367 }
368 
DelFileLineData(const std::string & filename,const std::string & linedata)369 bool DhcpFunc::DelFileLineData(const std::string& filename, const std::string& linedata)
370 {
371     bool bDel = false;
372     std::ifstream inFile;
373     inFile.open(filename.c_str());
374     std::string strFileData = "";
375     std::string strTemp = "";
376     char tmpLineData[1024] = {0};
377     while (inFile.getline(tmpLineData, sizeof(tmpLineData))) {
378         strTemp = tmpLineData;
379         if (strTemp != linedata) {
380             strFileData += strTemp;
381             strFileData += "\n";
382         } else {
383             bDel = true;
384         }
385     }
386     inFile.close();
387 
388     if (bDel) {
389         std::ofstream outFile;
390         outFile.open(filename.c_str());
391         outFile.flush();
392         WIFI_LOGI("DelFileLineData Reflush filename:%{public}s, strFileData:%{public}s.",
393             filename.c_str(), strFileData.c_str());
394         outFile << strFileData;
395         outFile.close();
396     }
397     return true;
398 }
399 
ModifyFileLineData(const std::string & filename,const std::string & srcdata,const std::string & dstdata)400 bool DhcpFunc::ModifyFileLineData(const std::string& filename, const std::string& srcdata, const std::string& dstdata)
401 {
402     bool bModify = false;
403     std::ifstream inFile;
404     inFile.open(filename.c_str());
405     std::string strFileData = "";
406     std::string strTemp = "";
407     char tmpLineData[1024] = {0};
408     while (inFile.getline(tmpLineData, sizeof(tmpLineData))) {
409         strTemp = tmpLineData;
410         if (strTemp != srcdata) {
411             strFileData += strTemp;
412             strFileData += "\n";
413         } else {
414             strFileData += dstdata;
415             strFileData += "\n";
416             bModify = true;
417         }
418     }
419     inFile.close();
420 
421     if (bModify) {
422         std::ofstream outFile;
423         outFile.open(filename.c_str());
424         outFile.flush();
425         WIFI_LOGI("ModifyFileLineData Reflush filename:%{public}s, strFileData:%{public}s.",
426             filename.c_str(), strFileData.c_str());
427         outFile << strFileData;
428         outFile.close();
429     }
430     return true;
431 }
432 
FormatString(struct DhcpPacketResult & result)433 int DhcpFunc::FormatString(struct DhcpPacketResult &result)
434 {
435     if (strncmp(result.strYiaddr, "*", 1) == 0) {
436         if (memset_s(result.strYiaddr, INET_ADDRSTRLEN, 0, INET_ADDRSTRLEN) != EOK) {
437             return -1;
438         }
439     }
440     if (strncmp(result.strOptServerId, "*", 1) == 0) {
441         if (memset_s(result.strOptServerId, INET_ADDRSTRLEN, 0, INET_ADDRSTRLEN) != EOK) {
442             return -1;
443         }
444     }
445     if (strncmp(result.strOptSubnet, "*", 1) == 0) {
446         if (memset_s(result.strOptSubnet, INET_ADDRSTRLEN, 0, INET_ADDRSTRLEN) != EOK) {
447             return -1;
448         }
449     }
450     if (strncmp(result.strOptDns1, "*", 1) == 0) {
451         if (memset_s(result.strOptDns1, INET_ADDRSTRLEN, 0, INET_ADDRSTRLEN) != EOK) {
452             return -1;
453         }
454     }
455     if (strncmp(result.strOptDns2, "*", 1) == 0) {
456         if (memset_s(result.strOptDns2, INET_ADDRSTRLEN, 0, INET_ADDRSTRLEN) != EOK) {
457             return -1;
458         }
459     }
460     if (strncmp(result.strOptRouter1, "*", 1) == 0) {
461         if (memset_s(result.strOptRouter1, INET_ADDRSTRLEN, 0, INET_ADDRSTRLEN) != EOK) {
462             return -1;
463         }
464     }
465     if (strncmp(result.strOptRouter2, "*", 1) == 0) {
466         if (memset_s(result.strOptRouter2, INET_ADDRSTRLEN, 0, INET_ADDRSTRLEN) != EOK) {
467             return -1;
468         }
469     }
470     if (strncmp(result.strOptVendor, "*", 1) == 0) {
471         if (memset_s(result.strOptVendor, DHCP_FILE_MAX_BYTES, 0, DHCP_FILE_MAX_BYTES) != EOK) {
472             return -1;
473         }
474     }
475     return 0;
476 }
477 
InitPidfile(const std::string & piddir,const std::string & pidfile)478 int DhcpFunc::InitPidfile(const std::string& piddir, const std::string& pidfile)
479 {
480     if (piddir.empty() || pidfile.empty()) {
481         WIFI_LOGE("InitPidfile() failed, piddir or pidfile is empty!");
482         return DHCP_OPT_FAILED;
483     }
484     WIFI_LOGI("InitPidfile() piddir:%{public}s, pidfile:%{public}s.", piddir.c_str(), pidfile.c_str());
485     unlink(pidfile.c_str());
486 
487     int fd;
488     if ((fd = open(pidfile.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
489         WIFI_LOGE("InitPidfile() failed, open pidfile:%{public}s err:%{public}d!", pidfile.c_str(), errno);
490         return DHCP_OPT_FAILED;
491     }
492 
493     char buf[PID_MAX_LEN] = {0};
494     if (snprintf_s(buf, PID_MAX_LEN, PID_MAX_LEN - 1, "%d", getpid()) < 0) {
495         WIFI_LOGE("InitPidfile() %{public}s failed, snprintf_s error:%{public}d!", pidfile.c_str(), errno);
496         close(fd);
497         return DHCP_OPT_FAILED;
498     }
499     ssize_t bytes;
500     if ((bytes = write(fd, buf, strlen(buf))) <= 0) {
501         WIFI_LOGE("InitPidfile() failed, write pidfile:%{public}s error:%{public}d, bytes:%{public}zd!",
502             pidfile.c_str(), errno, bytes);
503         close(fd);
504         return DHCP_OPT_FAILED;
505     }
506     WIFI_LOGI("InitPidfile() pid:%{public}s write %{public}s, bytes:%{public}zd!", buf, pidfile.c_str(), bytes);
507     close(fd);
508 
509     if (chdir(piddir.c_str()) != 0) {
510         WIFI_LOGE("InitPidfile() failed, chdir piddir:%{public}s err:%{public}d!", piddir.c_str(), errno);
511         return DHCP_OPT_FAILED;
512     }
513 
514     /* Set default permissions for the specified client process id files and directories. */
515     umask(DEFAULT_UMASK);
516 
517     /* Change attribs to the specified client process id files: 644 (user=rw, group=r, other=r). */
518     chmod(pidfile.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
519 
520     return DHCP_OPT_SUCCESS;
521 }
522 
GetPID(const std::string & pidfile)523 pid_t DhcpFunc::GetPID(const std::string& pidfile)
524 {
525     /* Check pidfile is or not exists. */
526     struct stat sb;
527     if (stat(pidfile.c_str(), &sb) != 0) {
528         WIFI_LOGW("GetPID() pidfile:%{public}s stat:%{public}d!", pidfile.c_str(), errno);
529         return -1;
530     }
531     WIFI_LOGI("GetPID() pidfile:%{public}s stat st_size:%{public}d.", pidfile.c_str(), (int)sb.st_size);
532 
533     int fd;
534     if ((fd = open(pidfile.c_str(), O_RDONLY)) < 0) {
535         WIFI_LOGE("GetPID() failed, open pidfile:%{public}s error!", pidfile.c_str());
536         return -1;
537     }
538 
539     lseek(fd, 0, SEEK_SET);
540 
541     char buf[PID_MAX_LEN] = {0};
542     ssize_t bytes;
543     if ((bytes = read(fd, buf, sb.st_size)) < 0) {
544         WIFI_LOGE("GetPID() failed, read pidfile:%{public}s error, bytes:%{public}zd!", pidfile.c_str(), bytes);
545         close(fd);
546         return -1;
547     }
548     WIFI_LOGI("GetPID() read pidfile:%{public}s, buf:%{public}s, bytes:%{public}zd.", pidfile.c_str(), buf, bytes);
549     close(fd);
550 
551     return atoi(buf);
552 }
553 
CheckProRunning(const pid_t proPid,const std::string & proName)554 int DhcpFunc::CheckProRunning(const pid_t proPid, const std::string& proName)
555 {
556     if ((proPid == 0) || proName.empty()) {
557         WIFI_LOGE("CheckProRunning %{public}ld or %{public}s param error!", (long int)proPid, proName.c_str());
558         return -1;
559     }
560     char buf[DIR_MAX_LEN] = {0};
561     if (snprintf_s(buf, DIR_MAX_LEN, DIR_MAX_LEN - 1, "/proc/%ld", (long int)proPid) < 0) {
562         WIFI_LOGE("CheckProRunning %{public}s failed, snprintf_s errno:%{public}d!", proName.c_str(), errno);
563         return -1;
564     }
565     if (access(buf, F_OK) != 0) {
566         WIFI_LOGI("CheckProRunning %{public}s is not exist, %{public}s no running", buf, proName.c_str());
567         return 0;
568     }
569     if (strcat_s(buf, sizeof(buf), "/exe") != EOK) {
570         WIFI_LOGE("CheckProRunning %{public}s failed, strcat_s errno:%{public}d!", proName.c_str(), errno);
571         return -1;
572     }
573     char proBuf[DIR_MAX_LEN] = {0};
574     if (readlink(buf, proBuf, sizeof(proBuf)) < 0) {
575         WIFI_LOGE("CheckProRunning %{public}s failed, readlink errno:%{public}d!", proName.c_str(), errno);
576         return -1;
577     }
578     if (strstr(proBuf, proName.c_str()) == NULL) {
579         WIFI_LOGI("CheckProRunning %{public}s exe -> %{public}s, %{public}s no running", buf, proBuf, proName.c_str());
580         return 0;
581     }
582     WIFI_LOGI("CheckProRunning %{public}s exe -> %{public}s, %{public}s is running", buf, proBuf, proName.c_str());
583     return 1;
584 }
585 
CreateDirs(const std::string dirs,int mode)586 int DhcpFunc::CreateDirs(const std::string dirs, int mode)
587 {
588     if (dirs.empty() || (dirs.size() >= DIR_MAX_LEN)) {
589         WIFI_LOGE("CreateDirs() dirs:%{public}s error!", dirs.c_str());
590         return DHCP_OPT_FAILED;
591     }
592 
593     int nSrcLen = (int)dirs.size();
594     char strDir[DIR_MAX_LEN] = {0};
595     if (strncpy_s(strDir, sizeof(strDir), dirs.c_str(), dirs.size()) != EOK) {
596         WIFI_LOGE("CreateDirs() strncpy_s dirs:%{public}s failed!", dirs.c_str());
597         return DHCP_OPT_FAILED;
598     }
599     if (strDir[nSrcLen - 1] != '/') {
600         if (nSrcLen == (DIR_MAX_LEN - 1)) {
601             WIFI_LOGE("CreateDirs() dirs:%{public}s len:%{public}d error!", dirs.c_str(), nSrcLen);
602             return DHCP_OPT_FAILED;
603         }
604         if (strcat_s(strDir, sizeof(strDir), "/") != EOK) {
605             WIFI_LOGE("CreateDirs() strcat_s strDir:%{public}s failed!", strDir);
606             return DHCP_OPT_FAILED;
607         }
608         nSrcLen++;
609     }
610 
611     int i = (strDir[0] == '/') ? 1 : 0;
612     for (; i <= nSrcLen - 1; i++) {
613         if (strDir[i] == '/') {
614             strDir[i] = 0;
615             if ((access(strDir, F_OK) != 0) && (mkdir(strDir, mode) != 0)) {
616                 WIFI_LOGE("CreateDirs() mkdir %{public}s %{public}.4o %{public}d!", strDir, mode, errno);
617                 return DHCP_OPT_FAILED;
618             }
619             strDir[i] = '/';
620         }
621     }
622     WIFI_LOGI("CreateDirs() %{public}s %{public}.4o success.", dirs.c_str(), mode);
623     return DHCP_OPT_SUCCESS;
624 }
625 
SplitString(const std::string src,const std::string delim,const int count,std::vector<std::string> & splits)626 bool DhcpFunc::SplitString(
627     const std::string src, const std::string delim, const int count, std::vector<std::string> &splits)
628 {
629     if (src.empty() || delim.empty()) {
630         WIFI_LOGE("SplitString() error, src or delim is empty!");
631         return false;
632     }
633 
634     splits.clear();
635 
636     std::string strData(src);
637     int nDelim = 0;
638     char *pSave = NULL;
639     char *pTok = strtok_r(const_cast<char *>(strData.c_str()), delim.c_str(), &pSave);
640     while (pTok != NULL) {
641         splits.push_back(std::string(pTok));
642         nDelim++;
643         pTok = strtok_r(NULL, delim.c_str(), &pSave);
644     }
645     if (nDelim != count) {
646         WIFI_LOGE("SplitString() %{public}s failed, nDelim:%{public}d,count:%{public}d!", src.c_str(), nDelim, count);
647         return false;
648     }
649     WIFI_LOGI("SplitString() %{private}s success, delim:%{public}s, count:%{public}d, splits.size():%{public}d.",
650         src.c_str(), delim.c_str(), count, (int)splits.size());
651     return true;
652 }
653 
SubscribeDhcpCommonEvent(const std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> & subscriber)654 bool DhcpFunc::SubscribeDhcpCommonEvent(
655     const std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> &subscriber)
656 {
657     return OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
658 }
659 
UnsubscribeDhcpCommonEvent(const std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> & subscriber)660 bool DhcpFunc::UnsubscribeDhcpCommonEvent(
661     const std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> &subscriber)
662 {
663     return OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber);
664 }
665 
PublishDhcpEvent(const std::string action,const int code,const std::string data)666 bool DhcpFunc::PublishDhcpEvent(const std::string action, const int code, const std::string data)
667 {
668     OHOS::EventFwk::Want want;
669     want.SetAction(action);
670     OHOS::EventFwk::CommonEventData commonData;
671     commonData.SetWant(want);
672     commonData.SetCode(code);
673     commonData.SetData(data);
674     if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(commonData)) {
675         WIFI_LOGE("PublishDhcpEvent() PublishCommonEvent failed, action:%{public}s, code:%{public}d, data:%{public}s.",
676             action.c_str(), code, data.c_str());
677         return DHCP_OPT_FAILED;
678     }
679     WIFI_LOGI("PublishDhcpEvent() PublishCommonEvent success, action:%{public}s, code:%{public}d, data:%{private}s.",
680         action.c_str(), code, data.c_str());
681     return DHCP_OPT_SUCCESS;
682 }
683 }  // namespace Wifi
684 }  // namespace OHOS