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 IsNeedDisplayTrafficAncoList();
93 bool IsSimAnco(const std::string &bundleName);
94 bool IsSim2Anco(const std::string &bundleName);
95 std::string GetGatewayAddr(const std::string& ipAddr, const std::string& subnetMask);
96 bool IpToInt(const std::string& ipAddr, uint32_t &ipIntAddr);
97 bool IpToString(uint32_t ipAddr, std::string &ipStrAddr);
98 int32_t ReadFromChildProcess(const int32_t *pipeFd, pid_t childPid, std::string *out);
99
GetCurrentSecond()100 inline uint64_t GetCurrentSecond()
101 {
102 return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
103 .count();
104 }
105
GetCurrentMilliSecond()106 inline uint64_t GetCurrentMilliSecond()
107 {
108 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
109 .count();
110 }
111
112 bool IsSameNaturalDay(uint32_t current, uint32_t another);
113 bool WriteFile(const std::string &filePath, const std::string &fileContent);
114 std::string GetHostnameFromURL(const std::string &url);
115 int32_t GetTodayMidnightTimestamp(int hour, int min, int sec);
116 void DeleteFile(const std::string &filePath);
117 } // namespace OHOS::NetManagerStandard::CommonUtils
118
119 #endif /* COMMUNICATIONNETMANAGER_BASE_COMMON_UTILS_H */
120