• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-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 
16 #include "netmanager_base_common_utils.h"
17 
18 #include <algorithm>
19 #include <arpa/inet.h>
20 #include <cstddef>
21 #include <cstdlib>
22 #include <netinet/in.h>
23 #include <regex>
24 #include <set>
25 #include <string>
26 #include <sys/socket.h>
27 #include <sys/wait.h>
28 #include <type_traits>
29 #include <unistd.h>
30 #include <vector>
31 #include <numeric>
32 
33 #include "net_manager_constants.h"
34 #include "net_mgr_log_wrapper.h"
35 #include "securec.h"
36 
37 namespace OHOS::NetManagerStandard::CommonUtils {
38 constexpr int32_t INET_OPTION_SUC = 1;
39 constexpr int32_t DECIMAL_SYSTEM = 10;
40 constexpr uint32_t CONST_MASK = 0x80000000;
41 constexpr size_t MAX_DISPLAY_NUM = 2;
42 constexpr uint32_t IPV4_DOT_NUM = 3;
43 constexpr int32_t MIN_BYTE = 0;
44 constexpr int32_t MAX_BYTE = 255;
45 constexpr int32_t BYTE_16 = 16;
46 constexpr uint32_t BIT_NUM_BYTE = 8;
47 constexpr int32_t BITS_24 = 24;
48 constexpr int32_t BITS_16 = 16;
49 constexpr int32_t BITS_8 = 8;
50 constexpr uint32_t INTERFACE_NAME_MAX_SIZE = 16;
51 constexpr int32_t CHAR_ARRAY_SIZE_MAX = 1024;
52 constexpr int32_t PIPE_FD_NUM = 2;
53 constexpr int32_t PIPE_OUT = 0;
54 constexpr int32_t PIPE_IN = 1;
55 constexpr int32_t DOMAIN_VALID_MIN_PART_SIZE = 2;
56 constexpr int32_t DOMAIN_VALID_MAX_PART_SIZE = 5;
57 constexpr int32_t NET_MASK_MAX_LENGTH = 32;
58 constexpr int32_t NET_MASK_GROUP_COUNT = 4;
59 constexpr int32_t MAX_IPV6_PREFIX_LENGTH = 128;
60 const std::string IPADDR_DELIMITER = ".";
61 constexpr const char *CMD_SEP = " ";
62 constexpr const char *DOMAIN_DELIMITER = ".";
63 constexpr const char *TLDS_SPLIT_SYMBOL = "|";
64 constexpr const char *HOST_DOMAIN_PATTERN_HEADER = "^(https?://)?[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.(";
65 constexpr const char *HOST_DOMAIN_PATTERN_TAIL = ")$";
66 constexpr const char *DEFAULT_IPV6_ANY_INIT_ADDR = "::";
67 const std::regex IP_PATTERN{
68     "((2([0-4]\\d|5[0-5])|1\\d\\d|[1-9]\\d|\\d)\\.){3}(2([0-4]\\d|5[0-5])|1\\d\\d|[1-9]\\d|\\d)"};
69 
70 const std::regex IP_MASK_PATTERN{
71     "((2([0-4]\\d|5[0-5])|1\\d\\d|[1-9]\\d|\\d)\\.){3}(2([0-4]\\d|5[0-5])|1\\d\\d|[1-9]\\d|\\d)/"
72     "(3[0-2]|[1-2]\\d|\\d)"};
73 
74 const std::regex IPV6_PATTERN{"([\\da-fA-F]{0,4}:){2,7}([\\da-fA-F]{0,4})"};
75 
76 const std::regex IPV6_MASK_PATTERN{"([\\da-fA-F]{0,4}:){2,7}([\\da-fA-F]{0,4})/(1[0-2][0-8]|[1-9]\\d|[1-9])"};
77 
78 std::vector<std::string> HOST_DOMAIN_TLDS{"com",  "net",     "org",    "edu",  "gov", "mil",  "cn",   "hk",  "tw",
79                                           "jp",   "de",      "uk",     "fr",   "au",  "ca",   "br",   "ru",  "it",
80                                           "es",   "in",      "online", "shop", "vip", "club", "xyz",  "top", "icu",
81                                           "work", "website", "tech",   "asia", "xin", "co",   "mobi", "info"};
82 std::mutex g_commonUtilsMutex;
83 std::mutex g_forkExecMutex;
84 
Strip(const std::string & str,char ch)85 std::string Strip(const std::string &str, char ch)
86 {
87     auto size = static_cast<int64_t>(str.size());
88     int64_t i = 0;
89     while (i < size && str[i] == ch) {
90         ++i;
91     }
92     int64_t j = size - 1;
93     while (j > 0 && str[j] == ch) {
94         --j;
95     }
96     if (i >= 0 && i < size && j >= 0 && j < size && j - i + 1 > 0) {
97         return str.substr(i, j - i + 1);
98     }
99     return "";
100 }
101 
ToLower(const std::string & s)102 std::string ToLower(const std::string &s)
103 {
104     std::string res = s;
105     std::transform(res.begin(), res.end(), res.begin(), tolower);
106     return res;
107 }
108 
IsValidIPV4(const std::string & ip)109 bool IsValidIPV4(const std::string &ip)
110 {
111     if (ip.empty()) {
112         return false;
113     }
114     struct in_addr s;
115     return inet_pton(AF_INET, ip.c_str(), reinterpret_cast<void *>(&s)) == INET_OPTION_SUC;
116 }
117 
IsValidIPV6(const std::string & ip)118 bool IsValidIPV6(const std::string &ip)
119 {
120     if (ip.empty()) {
121         return false;
122     }
123     struct in6_addr s;
124     return inet_pton(AF_INET6, ip.c_str(), reinterpret_cast<void *>(&s)) == INET_OPTION_SUC;
125 }
126 
GetAddrFamily(const std::string & ip)127 int8_t GetAddrFamily(const std::string &ip)
128 {
129     if (IsValidIPV4(ip)) {
130         return AF_INET;
131     }
132     if (IsValidIPV6(ip)) {
133         return AF_INET6;
134     }
135     return 0;
136 }
137 
GetMaskLength(const std::string & mask)138 int GetMaskLength(const std::string &mask)
139 {
140     int netMask = 0;
141     unsigned int maskTmp = ntohl(static_cast<int>(inet_addr(mask.c_str())));
142     while (maskTmp & CONST_MASK) {
143         ++netMask;
144         maskTmp = (maskTmp << 1);
145     }
146     return netMask;
147 }
148 
GetMaskByLength(uint32_t length)149 std::string GetMaskByLength(uint32_t length)
150 {
151     const auto mask = length == 0 ? 0 : -1 << (NET_MASK_MAX_LENGTH - length);
152     auto maskGroup = new int[NET_MASK_GROUP_COUNT];
153     for (int i = 0; i < NET_MASK_GROUP_COUNT; i++) {
154         int pos = NET_MASK_GROUP_COUNT - 1 - i;
155         maskGroup[pos] = (static_cast<uint32_t>(mask) >> (i * BIT_NUM_BYTE)) & 0x000000ff;
156     }
157     std::string sMask = "" + std::to_string(maskGroup[0]);
158     for (int i = 1; i < NET_MASK_GROUP_COUNT; i++) {
159         sMask = sMask + "." + std::to_string(maskGroup[i]);
160     }
161     delete[] maskGroup;
162     return sMask;
163 }
164 
GetIpv6Prefix(const std::string & ipv6Addr,uint8_t prefixLen)165 std::string GetIpv6Prefix(const std::string &ipv6Addr, uint8_t prefixLen)
166 {
167     if (prefixLen >= MAX_IPV6_PREFIX_LENGTH) {
168         return ipv6Addr;
169     }
170 
171     in6_addr ipv6AddrBuf = IN6ADDR_ANY_INIT;
172     inet_pton(AF_INET6, ipv6Addr.c_str(), &ipv6AddrBuf);
173 
174     char buf[INET6_ADDRSTRLEN] = {0};
175     if (inet_ntop(AF_INET6, &ipv6AddrBuf, buf, INET6_ADDRSTRLEN) == nullptr) {
176         return ipv6Addr;
177     }
178 
179     in6_addr ipv6Prefix = IN6ADDR_ANY_INIT;
180     uint32_t byteIndex = prefixLen / BIT_NUM_BYTE;
181     if (memset_s(ipv6Prefix.s6_addr, sizeof(ipv6Prefix.s6_addr), 0, sizeof(ipv6Prefix.s6_addr)) != EOK ||
182         memcpy_s(ipv6Prefix.s6_addr, sizeof(ipv6Prefix.s6_addr), &ipv6AddrBuf, byteIndex) != EOK) {
183         return DEFAULT_IPV6_ANY_INIT_ADDR;
184     }
185     uint32_t bitOffset = prefixLen & 0x7;
186     if ((bitOffset != 0) && (byteIndex < INET_ADDRSTRLEN)) {
187         ipv6Prefix.s6_addr[byteIndex] = ipv6AddrBuf.s6_addr[byteIndex] & (0xff00 >> bitOffset);
188     }
189     char ipv6PrefixBuf[INET6_ADDRSTRLEN] = {0};
190     inet_ntop(AF_INET6, &ipv6Prefix, ipv6PrefixBuf, INET6_ADDRSTRLEN);
191     return ipv6PrefixBuf;
192 }
193 
ConvertIpv4Address(uint32_t addressIpv4)194 std::string ConvertIpv4Address(uint32_t addressIpv4)
195 {
196     if (addressIpv4 == 0) {
197         return "";
198     }
199 
200     std::ostringstream stream;
201     stream << ((addressIpv4 >> BITS_24) & 0xFF) << IPADDR_DELIMITER << ((addressIpv4 >> BITS_16) & 0xFF)
202            << IPADDR_DELIMITER << ((addressIpv4 >> BITS_8) & 0xFF) << IPADDR_DELIMITER << (addressIpv4 & 0xFF);
203     return stream.str();
204 }
205 
ConvertIpv4Address(const std::string & address)206 uint32_t ConvertIpv4Address(const std::string &address)
207 {
208     std::string tmpAddress = address;
209     uint32_t addrInt = 0;
210     uint32_t i = 0;
211     for (i = 0; i < IPV4_DOT_NUM; i++) {
212         std::string::size_type npos = tmpAddress.find(IPADDR_DELIMITER);
213         if (npos == std::string::npos) {
214             break;
215         }
216         const auto &value = tmpAddress.substr(0, npos);
217         int32_t itmp = std::atoi(value.c_str());
218         if ((itmp < MIN_BYTE) || (itmp > MAX_BYTE)) {
219             break;
220         }
221         uint32_t utmp = static_cast<uint32_t>(itmp);
222         addrInt += utmp << ((IPV4_DOT_NUM - i) * BIT_NUM_BYTE);
223         tmpAddress = tmpAddress.substr(npos + 1);
224     }
225 
226     if (i != IPV4_DOT_NUM) {
227         return 0;
228     }
229     int32_t itmp = std::atoi(tmpAddress.c_str());
230     if ((itmp < MIN_BYTE) || (itmp > MAX_BYTE)) {
231         return 0;
232     }
233     uint32_t utmp = static_cast<uint32_t>(itmp);
234     addrInt += utmp;
235 
236     return addrInt;
237 }
238 
Ipv4PrefixLen(const std::string & ip)239 int32_t Ipv4PrefixLen(const std::string &ip)
240 {
241     constexpr int32_t BIT32 = 32;
242     constexpr int32_t BIT24 = 24;
243     constexpr int32_t BIT16 = 16;
244     constexpr int32_t BIT8 = 8;
245     if (ip.empty()) {
246         return 0;
247     }
248     int32_t ret = 0;
249     uint32_t ipNum = 0;
250     uint8_t c1 = 0;
251     uint8_t c2 = 0;
252     uint8_t c3 = 0;
253     uint8_t c4 = 0;
254     int32_t cnt = 0;
255     ret = sscanf_s(ip.c_str(), "%hhu.%hhu.%hhu.%hhu", &c1, &c2, &c3, &c4);
256     if (ret != sizeof(int32_t)) {
257         return 0;
258     }
259     ipNum = (c1 << static_cast<uint32_t>(BIT24)) | (c2 << static_cast<uint32_t>(BIT16)) |
260             (c3 << static_cast<uint32_t>(BIT8)) | c4;
261     if (ipNum == 0xFFFFFFFF) {
262         return BIT32;
263     }
264     if (ipNum == 0xFFFFFF00) {
265         return BIT24;
266     }
267     if (ipNum == 0xFFFF0000) {
268         return BIT16;
269     }
270     if (ipNum == 0xFF000000) {
271         return BIT8;
272     }
273     for (int32_t i = 0; i < BIT32; i++) {
274         if ((ipNum << i) & 0x80000000) {
275             cnt++;
276         } else {
277             break;
278         }
279     }
280     return cnt;
281 }
282 
Ipv6PrefixLen(const std::string & ip)283 int32_t Ipv6PrefixLen(const std::string &ip)
284 {
285     constexpr int32_t LENGTH_8 = 8;
286     constexpr int32_t LENGTH_7 = 7;
287     constexpr int32_t LENGTH_6 = 6;
288     constexpr int32_t LENGTH_5 = 5;
289     constexpr int32_t LENGTH_4 = 4;
290     constexpr int32_t LENGTH_3 = 3;
291     constexpr int32_t LENGTH_2 = 2;
292     constexpr int32_t LENGTH_1 = 1;
293     if (ip.empty()) {
294         return 0;
295     }
296     in6_addr addr{};
297     inet_pton(AF_INET6, ip.c_str(), &addr);
298     int32_t prefixLen = 0;
299     for (int32_t i = 0; i < BYTE_16; ++i) {
300         if (addr.s6_addr[i] == 0xFF) {
301             prefixLen += LENGTH_8;
302         } else if (addr.s6_addr[i] == 0xFE) {
303             prefixLen += LENGTH_7;
304             break;
305         } else if (addr.s6_addr[i] == 0xFC) {
306             prefixLen += LENGTH_6;
307             break;
308         } else if (addr.s6_addr[i] == 0xF8) {
309             prefixLen += LENGTH_5;
310             break;
311         } else if (addr.s6_addr[i] == 0xF0) {
312             prefixLen += LENGTH_4;
313             break;
314         } else if (addr.s6_addr[i] == 0xE0) {
315             prefixLen += LENGTH_3;
316             break;
317         } else if (addr.s6_addr[i] == 0xC0) {
318             prefixLen += LENGTH_2;
319             break;
320         } else if (addr.s6_addr[i] == 0x80) {
321             prefixLen += LENGTH_1;
322             break;
323         } else {
324             break;
325         }
326     }
327     return prefixLen;
328 }
329 
ParseInt(const std::string & str,int32_t * value)330 bool ParseInt(const std::string &str, int32_t *value)
331 {
332     char *end;
333     long long v = strtoll(str.c_str(), &end, 10);
334     if (std::string(end) == str || *end != '\0' || v < INT_MIN || v > INT_MAX) {
335         return false;
336     }
337     *value = v;
338     return true;
339 }
340 
ConvertToInt64(const std::string & str)341 int64_t ConvertToInt64(const std::string &str)
342 {
343     return strtoll(str.c_str(), nullptr, DECIMAL_SYSTEM);
344 }
345 
MaskIpv4(std::string & maskedResult)346 std::string MaskIpv4(std::string &maskedResult)
347 {
348     int maxDisplayNum = MAX_DISPLAY_NUM;
349     for (char &i : maskedResult) {
350         if (i == '/') {
351             break;
352         }
353         if (maxDisplayNum > 0) {
354             if (i == '.') {
355                 maxDisplayNum--;
356             }
357         } else {
358             if (i != '.') {
359                 i = '*';
360             }
361         }
362     }
363     return maskedResult;
364 }
365 
MaskIpv6(std::string & maskedResult)366 std::string MaskIpv6(std::string &maskedResult)
367 {
368     size_t colonCount = 0;
369     for (char &i : maskedResult) {
370         if (i == '/') {
371             break;
372         }
373         if (i == ':') {
374             colonCount++;
375         }
376 
377         if (colonCount >= MAX_DISPLAY_NUM) { // An legal ipv6 address has at least 2 ':'.
378             if (i != ':' && i != '/') {
379                 i = '*';
380             }
381         }
382     }
383     return maskedResult;
384 }
385 
ToAnonymousIp(const std::string & input)386 std::string ToAnonymousIp(const std::string &input)
387 {
388     std::lock_guard<std::mutex> lock(g_commonUtilsMutex);
389     std::string maskedResult{input};
390     // Mask ipv4 address.
391     if (std::regex_match(maskedResult, IP_PATTERN) || std::regex_match(maskedResult, IP_MASK_PATTERN)) {
392         return MaskIpv4(maskedResult);
393     }
394     // Mask ipv6 address.
395     if (std::regex_match(maskedResult, IPV6_PATTERN) || std::regex_match(maskedResult, IPV6_MASK_PATTERN)) {
396         return MaskIpv6(maskedResult);
397     }
398     return input;
399 }
400 
StrToInt(const std::string & value,int32_t defaultErr)401 int32_t StrToInt(const std::string &value, int32_t defaultErr)
402 {
403     errno = 0;
404     char *pEnd = nullptr;
405     int64_t result = std::strtol(value.c_str(), &pEnd, 0);
406     if (pEnd == value.c_str() || (result < INT_MIN || result > LONG_MAX) || errno == ERANGE) {
407         return defaultErr;
408     }
409     return result;
410 }
411 
StrToUint(const std::string & value,uint32_t defaultErr)412 uint32_t StrToUint(const std::string &value, uint32_t defaultErr)
413 {
414     errno = 0;
415     char *pEnd = nullptr;
416     uint64_t result = std::strtoul(value.c_str(), &pEnd, 0);
417     if (pEnd == value.c_str() || result > UINT32_MAX || errno == ERANGE) {
418         return defaultErr;
419     }
420     return result;
421 }
422 
StrToBool(const std::string & value,bool defaultErr)423 bool StrToBool(const std::string &value, bool defaultErr)
424 {
425     errno = 0;
426     char *pEnd = nullptr;
427     uint64_t result = std::strtoul(value.c_str(), &pEnd, 0);
428     if (pEnd == value.c_str() || result > UINT32_MAX || errno == ERANGE) {
429         return defaultErr;
430     }
431     return static_cast<bool>(result);
432 }
433 
StrToLong(const std::string & value,int64_t defaultErr)434 int64_t StrToLong(const std::string &value, int64_t defaultErr)
435 {
436     errno = 0;
437     char *pEnd = nullptr;
438     int64_t result = std::strtoll(value.c_str(), &pEnd, 0);
439     if (pEnd == value.c_str() || errno == ERANGE) {
440         return defaultErr;
441     }
442     return result;
443 }
444 
StrToUint64(const std::string & value,uint64_t defaultErr)445 uint64_t StrToUint64(const std::string &value, uint64_t defaultErr)
446 {
447     errno = 0;
448     char *pEnd = nullptr;
449     uint64_t result = std::strtoull(value.c_str(), &pEnd, 0);
450     if (pEnd == value.c_str() || errno == ERANGE) {
451         return defaultErr;
452     }
453     return result;
454 }
455 
CheckIfaceName(const std::string & name)456 bool CheckIfaceName(const std::string &name)
457 {
458     uint32_t index = 0;
459     if (name.empty()) {
460         return false;
461     }
462     size_t len = name.size();
463     if (len > INTERFACE_NAME_MAX_SIZE) {
464         return false;
465     }
466     while (index < len) {
467         if ((index == 0) && !isalnum(name[index])) {
468             return false;
469         }
470         if (!isalnum(name[index]) && (name[index] != '-') && (name[index] != '_') && (name[index] != '.') &&
471             (name[index] != ':')) {
472             return false;
473         }
474         index++;
475     }
476     return true;
477 }
478 
FormatCmd(const std::vector<std::string> & cmd)479 std::vector<const char *> FormatCmd(const std::vector<std::string> &cmd)
480 {
481     std::vector<const char *> res;
482     res.reserve(cmd.size() + 1);
483 
484     // string is converted to char * and the result is saved in res
485     std::transform(cmd.begin(), cmd.end(), std::back_inserter(res), [](const std::string &str) { return str.c_str(); });
486     res.emplace_back(nullptr);
487     return res;
488 }
489 
ForkExecChildProcess(const int32_t * pipeFd,int32_t count,const std::vector<const char * > & args)490 int32_t ForkExecChildProcess(const int32_t *pipeFd, int32_t count, const std::vector<const char *> &args)
491 {
492     if (count != PIPE_FD_NUM) {
493         NETMGR_LOG_E("fork exec parent process failed");
494         _exit(-1);
495     }
496     if (close(pipeFd[PIPE_OUT]) != 0) {
497         NETMGR_LOG_E("close failed, errorno:%{public}d, errormsg:%{public}s", errno, strerror(errno));
498         _exit(-1);
499     }
500     if (dup2(pipeFd[PIPE_IN], STDOUT_FILENO) == -1) {
501         NETMGR_LOG_E("dup2 failed, errorno:%{public}d, errormsg:%{public}s", errno, strerror(errno));
502         _exit(-1);
503     }
504     if (execv(args[0], const_cast<char *const *>(&args[0])) == -1) {
505         NETMGR_LOG_E("execv command failed, errorno:%{public}d, errormsg:%{public}s", errno, strerror(errno));
506     }
507     if (close(pipeFd[PIPE_IN]) != 0) {
508         NETMGR_LOG_E("close failed, errorno:%{public}d, errormsg:%{public}s", errno, strerror(errno));
509         _exit(-1);
510     }
511     _exit(-1);
512 }
513 
ForkExecParentProcess(const int32_t * pipeFd,int32_t count,pid_t childPid,std::string * out)514 int32_t ForkExecParentProcess(const int32_t *pipeFd, int32_t count, pid_t childPid, std::string *out)
515 {
516     if (count != PIPE_FD_NUM) {
517         NETMGR_LOG_E("fork exec parent process failed");
518         return NETMANAGER_ERROR;
519     }
520     if (out != nullptr) {
521         char buf[CHAR_ARRAY_SIZE_MAX] = {0};
522         out->clear();
523         if (close(pipeFd[PIPE_IN]) != 0) {
524             NETMGR_LOG_E("close failed, errorno:%{public}d, errormsg:%{public}s", errno, strerror(errno));
525         }
526         while (read(pipeFd[PIPE_OUT], buf, CHAR_ARRAY_SIZE_MAX - 1) > 0) {
527             out->append(buf);
528             if (memset_s(buf, sizeof(buf), 0, sizeof(buf)) != 0) {
529                 NETMGR_LOG_E("memset is false");
530                 close(pipeFd[PIPE_OUT]);
531                 return NETMANAGER_ERROR;
532             }
533         }
534         if (close(pipeFd[PIPE_OUT]) != 0) {
535             NETMGR_LOG_E("close failed, errorno:%{public}d, errormsg:%{public}s", errno, strerror(errno));
536             _exit(-1);
537         }
538         return NETMANAGER_SUCCESS;
539     } else {
540         NETMGR_LOG_D("there is no need to return execution results");
541         close(pipeFd[PIPE_IN]);
542         close(pipeFd[PIPE_OUT]);
543     }
544     pid_t pidRet = waitpid(childPid, nullptr, 0);
545     if (pidRet != childPid) {
546         NETMGR_LOG_E("waitpid[%{public}d] failed, pidRet:%{public}d", childPid, pidRet);
547         return NETMANAGER_ERROR;
548     }
549     return NETMANAGER_SUCCESS;
550 }
551 
ForkExec(const std::string & command,std::string * out)552 int32_t ForkExec(const std::string &command, std::string *out)
553 {
554     std::unique_lock<std::mutex> lock(g_forkExecMutex);
555     const std::vector<std::string> cmd = Split(command, CMD_SEP);
556     std::vector<const char *> args = FormatCmd(cmd);
557     int32_t pipeFd[PIPE_FD_NUM] = {0};
558     if (pipe(pipeFd) < 0) {
559         NETMGR_LOG_E("creat pipe failed, errorno:%{public}d, errormsg:%{public}s", errno, strerror(errno));
560         return NETMANAGER_ERROR;
561     }
562     pid_t pid = fork();
563     if (pid < 0) {
564         NETMGR_LOG_E("fork failed, errorno:%{public}d, errormsg:%{public}s", errno, strerror(errno));
565         return NETMANAGER_ERROR;
566     }
567     if (pid == 0) {
568         ForkExecChildProcess(pipeFd, PIPE_FD_NUM, args);
569         return NETMANAGER_SUCCESS;
570     } else {
571         return ForkExecParentProcess(pipeFd, PIPE_FD_NUM, pid, out);
572     }
573 }
574 
IsValidDomain(const std::string & domain)575 bool IsValidDomain(const std::string &domain)
576 {
577     if (domain.empty()) {
578         return false;
579     }
580 
581     std::string pattern = HOST_DOMAIN_PATTERN_HEADER;
582     pattern = std::accumulate(HOST_DOMAIN_TLDS.begin(), HOST_DOMAIN_TLDS.end(), pattern,
583                               [](const std::string& pattern, const std::string& tlds) { return pattern + tlds + TLDS_SPLIT_SYMBOL; });
584     pattern = pattern.replace(pattern.size() - 1, 1, "") + HOST_DOMAIN_PATTERN_TAIL;
585     std::regex reg(pattern);
586     if (!std::regex_match(domain, reg)) {
587         NETMGR_LOG_E("Domain:%{public}s regex match failed.", domain.c_str());
588         return false;
589     }
590 
591     std::vector<std::string> parts = Split(domain, DOMAIN_DELIMITER);
592     if (parts.size() < DOMAIN_VALID_MIN_PART_SIZE || parts.size() > DOMAIN_VALID_MAX_PART_SIZE) {
593         NETMGR_LOG_E("The domain:[%{public}s] parts size:[%{public}d] is invalid", domain.c_str(),
594                      static_cast<int>(parts.size()));
595         return false;
596     }
597 
598     std::set<std::string> tldsList;
599     for (const auto &item : parts) {
600         if (std::find(HOST_DOMAIN_TLDS.begin(), HOST_DOMAIN_TLDS.end(), item) == HOST_DOMAIN_TLDS.end()) {
601             continue;
602         }
603         if (tldsList.find(item) != tldsList.end()) {
604             NETMGR_LOG_E("Domain:%{public}s has duplicate tlds:%{public}s", domain.c_str(), item.c_str());
605             return false;
606         }
607         tldsList.insert(item);
608     }
609     return true;
610 }
611 } // namespace OHOS::NetManagerStandard::CommonUtils
612