1 /*
2 * Copyright (C) 2021-2022 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 "wifi_common_util.h"
17 #include <sstream>
18 #include <iterator>
19 #include <regex>
20 #include "bundle_mgr_interface.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "wifi_logger.h"
26
27 namespace OHOS {
28 namespace Wifi {
29 DEFINE_WIFILOG_LABEL("WifiCommonUtil");
DataAnonymize(const std::string str,const char delim,const char hiddenCh,const int startIdx=0)30 static std::string DataAnonymize(const std::string str, const char delim,
31 const char hiddenCh, const int startIdx = 0)
32 {
33 std::string s = str;
34 constexpr auto minDelimSize = 2;
35 constexpr auto minKeepSize = 6;
36 if (std::count(s.begin(), s.end(), delim) < minDelimSize) {
37 if (s.size() <= minKeepSize) {
38 return std::string(s.size(), hiddenCh);
39 }
40 auto idx1 = 2;
41 const auto idx2 = s.size() - 4;
42 while (idx1++ < idx2) {
43 s[idx1] = hiddenCh;
44 }
45 return s;
46 }
47
48 std::string::size_type begin = s.find_first_of(delim);
49 std::string::size_type end = s.find_last_of(delim);
50 int idx = 0;
51 while (idx++ < startIdx && begin < end) {
52 begin = s.find_first_of(delim, begin + 1);
53 }
54 while (begin++ != end) {
55 if (s[begin] != delim) {
56 s[begin] = hiddenCh;
57 }
58 }
59 return s;
60 }
61
MacAnonymize(const std::string str)62 std::string MacAnonymize(const std::string str)
63 {
64 return DataAnonymize(str, ':', '*', 1);
65 }
66
IpAnonymize(const std::string str)67 std::string IpAnonymize(const std::string str)
68 {
69 return DataAnonymize(str, '.', '*');
70 }
71
ConvertStrChar(char ch)72 static unsigned char ConvertStrChar(char ch)
73 {
74 constexpr int numDiffForHexAlphabet = 10;
75 if (ch >= '0' && ch <= '9') {
76 return (ch - '0');
77 }
78 if (ch >= 'A' && ch <= 'F') {
79 return (ch - 'A' + numDiffForHexAlphabet);
80 }
81 if (ch >= 'a' && ch <= 'f') {
82 return (ch - 'a' + numDiffForHexAlphabet);
83 }
84 return 0;
85 }
86
MacStrToArray(const std::string & strMac,unsigned char mac[WIFI_MAC_LEN])87 errno_t MacStrToArray(const std::string& strMac, unsigned char mac[WIFI_MAC_LEN])
88 {
89 constexpr int strMacLen = 18;
90 char tempArray[strMacLen] = { 0 };
91 errno_t ret = memcpy_s(tempArray, strMacLen, strMac.c_str(), strMac.size() + 1);
92 if (ret != EOK) {
93 return ret;
94 }
95
96 int idx = 0;
97 constexpr int bitWidth = 4;
98 char *ptr = nullptr;
99 char *p = strtok_s(tempArray, ":", &ptr);
100 while (p != nullptr) {
101 mac[idx++] = (ConvertStrChar(*p) << bitWidth) | ConvertStrChar(*(p + 1));
102 p = strtok_s(nullptr, ":", &ptr);
103 }
104 return EOK;
105 }
106
ConvertArrayChar(unsigned char ch)107 static char ConvertArrayChar(unsigned char ch)
108 {
109 constexpr int maxDecNum = 9;
110 constexpr int numDiffForHexAlphabet = 10;
111 if (ch >= 0 && ch <= maxDecNum) {
112 return '0' + ch;
113 }
114 if (ch >= 0xa && ch <= 0xf) {
115 return ch + 'a' - numDiffForHexAlphabet;
116 }
117 return '0';
118 }
119
MacArrayToStr(const unsigned char mac[WIFI_MAC_LEN])120 std::string MacArrayToStr(const unsigned char mac[WIFI_MAC_LEN])
121 {
122 constexpr int bitWidth = 4;
123 constexpr int noColonBit = 5;
124 std::stringstream ss;
125 for (int i = 0; i != WIFI_MAC_LEN; ++i) {
126 ss << ConvertArrayChar(mac[i] >> bitWidth) << ConvertArrayChar(mac[i] & 0xf);
127 if (i != noColonBit) {
128 ss << ":";
129 }
130 }
131 return ss.str();
132 }
133
IsMacArrayEmpty(const unsigned char mac[WIFI_MAC_LEN])134 bool IsMacArrayEmpty(const unsigned char mac[WIFI_MAC_LEN])
135 {
136 for (int i = 0; i != WIFI_MAC_LEN; ++i) {
137 if (mac[i] != 0) {
138 return false;
139 }
140 }
141 return true;
142 }
143
Ip2Number(const std::string & strIp)144 int Ip2Number(const std::string& strIp)
145 {
146 std::string::size_type front = 0;
147 std::string::size_type back = 0;
148 int number = 0;
149 int size = 32;
150 constexpr int sectionSize = 8;
151
152 std::string ip(strIp + '.');
153 while ((back = ip.find_first_of('.', back)) != (std::string::size_type)std::string::npos) {
154 number |= atoi(ip.substr(front, back - front).c_str()) << (size -= sectionSize);
155 front = ++back;
156 }
157 return number;
158 }
159
Number2Ip(int intIp)160 std::string Number2Ip(int intIp)
161 {
162 constexpr int fourthPartMoveLen = 24;
163 constexpr int thirdPartMoveLen = 16;
164 constexpr int secondPartMoveLen = 8;
165
166 std::string ip;
167 ip.append(std::to_string(((unsigned int)intIp & 0xff000000) >> fourthPartMoveLen));
168 ip.push_back('.');
169 ip.append(std::to_string((intIp & 0x00ff0000) >> thirdPartMoveLen));
170 ip.push_back('.');
171 ip.append(std::to_string((intIp & 0x0000ff00) >> secondPartMoveLen));
172 ip.push_back('.');
173 ip.append(std::to_string(intIp & 0x000000ff));
174 return ip;
175 }
176
StrSplit(const std::string & str,const std::string & delim)177 std::vector<std::string> StrSplit(const std::string& str, const std::string& delim) {
178 std::regex re(delim);
179 std::sregex_token_iterator
180 first{ str.begin(), str.end(), re, -1 },
181 last;
182 return { first, last };
183 }
184
GetBundleManager()185 sptr<AppExecFwk::IBundleMgr> GetBundleManager()
186 {
187 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
188 if (systemManager == nullptr) {
189 WIFI_LOGE("Get system ability manager failed!");
190 return nullptr;
191 }
192 return iface_cast<AppExecFwk::IBundleMgr>(systemManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID));
193 }
194
GetBundleName()195 std::string GetBundleName()
196 {
197 sptr<AppExecFwk::IBundleMgr> bundleInstance = GetBundleManager();
198 if (bundleInstance == nullptr) {
199 return "";
200 }
201
202 std::string bundleName;
203 int uid = IPCSkeleton::GetCallingUid();
204 if (!bundleInstance->GetBundleNameForUid(uid, bundleName)) {
205 WIFI_LOGE("Get bundle name failed!");
206 }
207 WIFI_LOGI("Get bundle name uid[%{public}d]: %{public}s", uid, bundleName.c_str());
208 return bundleName;
209 }
210
IsSystemApp()211 bool IsSystemApp()
212 {
213 sptr<AppExecFwk::IBundleMgr> bundleInstance = GetBundleManager();
214 if (bundleInstance == nullptr) {
215 return false;
216 }
217
218 std::string bundleName;
219 int uid = IPCSkeleton::GetCallingUid();
220 bool isSysApp = bundleInstance->CheckIsSystemAppByUid(uid);
221 WIFI_LOGI("Is system App uid[%{public}d]: %{public}d", uid, isSysApp);
222 return isSysApp;
223 }
224 } // namespace Wifi
225 } // namespace OHOS