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 #ifndef COMMUNICATIONNETMANAGER_BASE_COMMON_UTILS_H
17 #define COMMUNICATIONNETMANAGER_BASE_COMMON_UTILS_H
18 
19 #include <iosfwd>
20 #include <sstream>
21 #include <vector>
22 
23 namespace OHOS::NetManagerStandard {
24 constexpr uint32_t INVALID_NET_ID = 0;
25 constexpr int32_t MIN_INTERNAL_NET_ID = 1;
26 constexpr int32_t MAX_INTERNAL_NET_ID = 50;
27 constexpr int32_t MIN_NET_ID = 100;
28 constexpr int32_t MAX_NET_ID = 0xFFFF - 0x400;
IsInternalNetId(int32_t netId)29 inline bool IsInternalNetId(int32_t netId)
30 {
31     return netId >= MIN_INTERNAL_NET_ID && netId <= MAX_INTERNAL_NET_ID;
32 }
33 }
34 namespace OHOS::NetManagerStandard::CommonUtils {
Split(const std::string & str,const std::string & sep)35 inline std::vector<std::string> Split(const std::string &str, const std::string &sep)
36 {
37     std::string s = str;
38     std::vector<std::string> res;
39     while (!s.empty()) {
40         size_t pos = s.find(sep);
41         if (pos == std::string::npos) {
42             res.emplace_back(s);
43             break;
44         }
45         res.emplace_back(s.substr(0, pos));
46         s = s.substr(pos + sep.size());
47     }
48     return res;
49 }
50 std::string Strip(const std::string &str, char ch = ' ');
51 std::string ToLower(const std::string &s);
52 bool IsValidIPV4(const std::string &ip);
53 bool IsValidIPV6(const std::string &ip);
54 int8_t GetAddrFamily(const std::string &ip);
55 int GetMaskLength(const std::string &mask);
56 std::string GetMaskByLength(uint32_t length);
57 std::string GetIpv6Prefix(const std::string &ipv6Addr, uint8_t prefixLen);
58 std::string ConvertIpv4Address(uint32_t addressIpv4);
59 uint32_t ConvertIpv4Address(const std::string &address);
60 int32_t Ipv4PrefixLen(const std::string &ip);
61 int32_t Ipv6PrefixLen(const std::string &ip);
62 bool ParseInt(const std::string &str, int32_t *value);
63 int64_t ConvertToInt64(const std::string &str);
64 std::string ToAnonymousIp(const std::string &input);
65 int32_t StrToInt(const std::string &value, int32_t defaultErr = -1);
66 uint32_t StrToUint(const std::string &value, uint32_t defaultErr = 0);
67 bool StrToBool(const std::string &value, bool defaultErr = false);
68 int64_t StrToLong(const std::string &value, int64_t defaultErr = -1);
69 uint64_t StrToUint64(const std::string &value, uint64_t defaultErr = 0);
70 bool CheckIfaceName(const std::string &name);
71 int32_t ForkExec(const std::string &command, std::string *out = nullptr);
72 bool IsValidDomain(const std::string &domain);
73 bool HasInternetPermission();
74 std::string Trim(const std::string &str);
75 bool IsUrlRegexValid(const std::string &regex);
76 std::string InsertCharBefore(const std::string &input, const char from, const char preChar, const char nextChar);
77 std::string ReplaceCharacters(const std::string &input);
78 bool UrlRegexParse(const std::string &str, const std::string &patternStr);
79 uint64_t GenRandomNumber();
80 
GetCurrentSecond()81 inline uint64_t GetCurrentSecond()
82 {
83     return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
84         .count();
85 }
86 
IsSameNaturalDay(uint32_t current,uint32_t another)87 inline bool IsSameNaturalDay(uint32_t current, uint32_t another)
88 {
89     std::time_t tt1 =
90         std::chrono::system_clock::to_time_t(std::chrono::system_clock::time_point(std::chrono::seconds(current)));
91     std::time_t tt2 =
92         std::chrono::system_clock::to_time_t(std::chrono::system_clock::time_point(std::chrono::seconds(another)));
93     std::tm *tm1 = std::localtime(&tt1);
94     std::tm *tm2 = std::localtime(&tt2);
95     if (tm1 == nullptr || tm2 == nullptr) {
96         return false;
97     }
98     return tm1->tm_year == tm2->tm_year && tm1->tm_mon == tm2->tm_mon && tm1->tm_mday == tm2->tm_mday;
99 }
100 
101 bool WriteFile(const std::string &filePath, const std::string &fileContent);
102 } // namespace OHOS::NetManagerStandard::CommonUtils
103 
104 #endif /* COMMUNICATIONNETMANAGER_BASE_COMMON_UTILS_H */
105