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