1 /*
2 * Copyright (c) 2021 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 #include "net_policy_file.h"
16
17 #include <fcntl.h>
18 #include <string>
19
20 #include <json/json.h>
21
22 #include "net_manager_center.h"
23 #include "net_mgr_log_wrapper.h"
24 #include "net_policy_define.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 const std::string MONTH_DEFAULT = "M1";
29
FileExists(const std::string & fileName)30 bool NetPolicyFile::FileExists(const std::string& fileName)
31 {
32 struct stat buffer;
33 return (stat(fileName.c_str(), &buffer) == static_cast<int32_t>(NetPolicyResultCode::ERR_NONE));
34 }
35
CreateFile(const std::string & fileName)36 bool NetPolicyFile::CreateFile(const std::string &fileName)
37 {
38 if (fileName.empty() || FileExists(fileName)) {
39 NETMGR_LOG_E("fileName empty or file not exists.");
40 return false;
41 }
42
43 int32_t fd = open(fileName.c_str(), O_CREAT | O_WRONLY, CHOWN_RWX_USR_GRP);
44 if (fd < 0) {
45 NETMGR_LOG_E("open file error.");
46 return false;
47 }
48 close(fd);
49
50 return true;
51 }
52
ParseUidPolicy(const Json::Value & root,NetPolicy & netPolicy)53 void NetPolicyFile::ParseUidPolicy(const Json::Value &root, NetPolicy &netPolicy)
54 {
55 const Json::Value arrayUidPolicy = root[CONFIG_UID_POLICY];
56 uint32_t size = arrayUidPolicy.size();
57 UidPolicy uidPolicy;
58 for (uint32_t i = 0; i < size; i++) {
59 uidPolicy.uid= arrayUidPolicy[i][CONFIG_UID].asString();
60 uidPolicy.policy = arrayUidPolicy[i][CONFIG_POLICY].asString();
61 netPolicy.uidPolicys.push_back(uidPolicy);
62 }
63 }
64
ParseBackgroundPolicy(const Json::Value & root,NetPolicy & netPolicy)65 void NetPolicyFile::ParseBackgroundPolicy(const Json::Value &root, NetPolicy& netPolicy)
66 {
67 const Json::Value mapBackgroundPolicy = root[CONFIG_BACKGROUND_POLICY];
68 netPolicy.backgroundPolicyStatus_ = mapBackgroundPolicy[CONFIG_BACKGROUND_POLICY_STATUS].asString();
69 }
70
ParseQuotaPolicy(const Json::Value & root,NetPolicy & netPolicy)71 void NetPolicyFile::ParseQuotaPolicy(const Json::Value &root, NetPolicy &netPolicy)
72 {
73 const Json::Value arrayQuotaPolicy = root[CONFIG_QUOTA_POLICY];
74 uint32_t size = arrayQuotaPolicy.size();
75 NetPolicyQuota quotaPolicy;
76 for (uint32_t i = 0; i < size; i++) {
77 quotaPolicy.netType = arrayQuotaPolicy[i][CONFIG_QUOTA_POLICY_NETTYPE].asString();
78 quotaPolicy.simId = arrayQuotaPolicy[i][CONFIG_QUOTA_POLICY_SUBSCRIBERID].asString();
79 quotaPolicy.periodStartTime = arrayQuotaPolicy[i][CONFIG_QUOTA_POLICY_PERIODSTARTTIME].asString();
80 quotaPolicy.periodDuration = arrayQuotaPolicy[i][CONFIG_QUOTA_POLICY_PERIODDURATION].asString();
81 quotaPolicy.warningBytes = arrayQuotaPolicy[i][CONFIG_QUOTA_POLICY_WARNINGBYTES].asString();
82 quotaPolicy.limitBytes = arrayQuotaPolicy[i][CONFIG_QUOTA_POLICY_LIMITBYTES].asString();
83 quotaPolicy.lastLimitSnooze = arrayQuotaPolicy[i][CONFIG_QUOTA_POLICY_LASTLIMITSNOOZE].asString();
84 quotaPolicy.metered = arrayQuotaPolicy[i][CONFIG_QUOTA_POLICY_METERED].asString();
85 quotaPolicy.source = arrayQuotaPolicy[i][CONFIG_QUOTA_POLICY_SOURCE].asString();
86 netPolicy.netQuotaPolicys.push_back(quotaPolicy);
87 }
88 }
89
ParseCellularPolicy(const Json::Value & root,NetPolicy & netPolicy)90 void NetPolicyFile::ParseCellularPolicy(const Json::Value &root, NetPolicy &netPolicy)
91 {
92 const Json::Value arrayCellularPolicy = root[CONFIG_CELLULAR_POLICY];
93 uint32_t size = arrayCellularPolicy.size();
94 NetPolicyCellular cellularPolicy;
95 for (uint32_t i = 0; i < size; i++) {
96 cellularPolicy.simId = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_SUBSCRIBERID].asString();
97 cellularPolicy.periodStartTime = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_PERIODSTARTTIME].asString();
98 cellularPolicy.periodDuration = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_PERIODDURATION].asString();
99 cellularPolicy.title = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_TITLE].asString();
100 cellularPolicy.summary = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_SUMMARY].asString();
101 cellularPolicy.limitBytes = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_LIMITBYTES].asString();
102 cellularPolicy.limitAction = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_LIMITACTION].asString();
103 cellularPolicy.usedBytes = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_USEDBYTES].asString();
104 cellularPolicy.usedTimeDuration = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_USEDTIMEDURATION].asString();
105 cellularPolicy.possessor = arrayCellularPolicy[i][CONFIG_CELLULAR_POLICY_POSSESSOR].asString();
106 netPolicy.netCellularPolicys.push_back(cellularPolicy);
107 }
108 }
109
Json2Obj(const std::string & content,NetPolicy & netPolicy)110 bool NetPolicyFile::Json2Obj(const std::string &content, NetPolicy &netPolicy)
111 {
112 if (content.empty()) {
113 return false;
114 }
115
116 Json::Value root;
117 Json::CharReaderBuilder buidler;
118 std::unique_ptr<Json::CharReader> reader(buidler.newCharReader());
119 JSONCPP_STRING errs;
120
121 bool isSuccess = reader->parse(content.c_str(), content.c_str() + content.length(), &root, &errs);
122 if (isSuccess && errs.size() == 0) {
123 netPolicy.hosVersion = root[CONFIG_HOS_VERSION].asString();
124 if (netPolicy.hosVersion.empty()) {
125 netPolicy.hosVersion = HOS_VERSION;
126 }
127 // parse uid policy from file
128 ParseUidPolicy(root, netPolicy);
129 // parse background policy from file
130 ParseBackgroundPolicy(root, netPolicy);
131 // parse quota policy from file
132 ParseQuotaPolicy(root, netPolicy);
133 // parse cellular policy from file
134 ParseCellularPolicy(root, netPolicy);
135 }
136
137 return true;
138 }
139
ReadFile(const std::string & fileName,std::string & fileContent)140 bool NetPolicyFile::ReadFile(const std::string &fileName, std::string &fileContent)
141 {
142 std::unique_lock<std::mutex> lock(mutex_);
143 if (fileName.empty()) {
144 NETMGR_LOG_E("fileName empty.");
145 return false;
146 }
147
148 if (!FileExists(fileName)) {
149 NETMGR_LOG_E("[%{public}s] not exist.", fileName.c_str());
150 return false;
151 }
152
153 std::fstream file(fileName.c_str(), std::fstream::in);
154 if (file.is_open() == false) {
155 NETMGR_LOG_E("fstream failed.");
156 return false;
157 }
158
159 std::stringstream buffer;
160 buffer << file.rdbuf();
161 fileContent = buffer.str();
162 file.close();
163
164 return true;
165 }
166
AppendQuotaPolicy(Json::Value & root)167 void NetPolicyFile::AppendQuotaPolicy(Json::Value &root)
168 {
169 uint32_t size = netPolicy_.netQuotaPolicys.size();
170 for (uint32_t i = 0; i < size; i++) {
171 Json::Value quotaPolicy;
172 quotaPolicy[CONFIG_QUOTA_POLICY_NETTYPE] = netPolicy_.netQuotaPolicys[i].netType;
173 quotaPolicy[CONFIG_QUOTA_POLICY_SUBSCRIBERID] = netPolicy_.netQuotaPolicys[i].simId;
174 quotaPolicy[CONFIG_QUOTA_POLICY_PERIODSTARTTIME] = netPolicy_.netQuotaPolicys[i].periodStartTime;
175 quotaPolicy[CONFIG_QUOTA_POLICY_PERIODDURATION] = netPolicy_.netQuotaPolicys[i].periodDuration;
176 quotaPolicy[CONFIG_QUOTA_POLICY_WARNINGBYTES] = netPolicy_.netQuotaPolicys[i].warningBytes;
177 quotaPolicy[CONFIG_QUOTA_POLICY_LIMITBYTES] = netPolicy_.netQuotaPolicys[i].limitBytes;
178 quotaPolicy[CONFIG_QUOTA_POLICY_LASTLIMITSNOOZE] = netPolicy_.netQuotaPolicys[i].lastLimitSnooze;
179 quotaPolicy[CONFIG_QUOTA_POLICY_METERED] = netPolicy_.netQuotaPolicys[i].metered;
180 quotaPolicy[CONFIG_QUOTA_POLICY_SOURCE] = netPolicy_.netQuotaPolicys[i].source;
181 root[CONFIG_QUOTA_POLICY].append(quotaPolicy);
182 }
183 }
184
AppendCellularPolicy(Json::Value & root)185 void NetPolicyFile::AppendCellularPolicy(Json::Value &root)
186 {
187 uint32_t size = netPolicy_.netCellularPolicys.size();
188 for (uint32_t i = 0; i < size; i++) {
189 Json::Value cellularPolicy;
190 cellularPolicy[CONFIG_CELLULAR_POLICY_SUBSCRIBERID] = netPolicy_.netCellularPolicys[i].simId;
191 cellularPolicy[CONFIG_CELLULAR_POLICY_PERIODSTARTTIME] =
192 netPolicy_.netCellularPolicys[i].periodStartTime;
193 cellularPolicy[CONFIG_CELLULAR_POLICY_PERIODDURATION] = netPolicy_.netCellularPolicys[i].periodDuration;
194 cellularPolicy[CONFIG_CELLULAR_POLICY_TITLE] = netPolicy_.netCellularPolicys[i].title;
195 cellularPolicy[CONFIG_CELLULAR_POLICY_SUMMARY] = netPolicy_.netCellularPolicys[i].summary;
196 cellularPolicy[CONFIG_CELLULAR_POLICY_LIMITBYTES] = netPolicy_.netCellularPolicys[i].limitBytes;
197 cellularPolicy[CONFIG_CELLULAR_POLICY_LIMITACTION] = netPolicy_.netCellularPolicys[i].limitAction;
198 cellularPolicy[CONFIG_CELLULAR_POLICY_USEDBYTES] = netPolicy_.netCellularPolicys[i].usedBytes;
199 cellularPolicy[CONFIG_CELLULAR_POLICY_USEDTIMEDURATION] =
200 netPolicy_.netCellularPolicys[i].usedTimeDuration;
201 cellularPolicy[CONFIG_CELLULAR_POLICY_POSSESSOR] = netPolicy_.netCellularPolicys[i].possessor;
202 root[CONFIG_CELLULAR_POLICY].append(cellularPolicy);
203 }
204 }
205
AppendUidPolicy(Json::Value & root)206 void NetPolicyFile::AppendUidPolicy(Json::Value &root)
207 {
208 uint32_t size = netPolicy_.uidPolicys.size();
209 for (uint32_t i = 0; i < size; i++) {
210 Json::Value uidPolicy;
211 uidPolicy[CONFIG_UID] = netPolicy_.uidPolicys[i].uid;
212 uidPolicy[CONFIG_POLICY] = netPolicy_.uidPolicys[i].policy;
213 std::string policy = std::to_string(static_cast<uint32_t>(NetUidPolicy::NET_POLICY_TEMPORARY_ALLOW_METERED));
214 /* Temporary permission, no need to write files */
215 if (netPolicy_.uidPolicys[i].policy == policy) {
216 continue;
217 }
218 root[CONFIG_UID_POLICY].append(uidPolicy);
219 }
220 }
221
AppendBackgroundPolicy(Json::Value & root)222 void NetPolicyFile::AppendBackgroundPolicy(Json::Value &root)
223 {
224 Json::Value backgroundPolicy;
225 if (netPolicy_.backgroundPolicyStatus_.empty()) {
226 netPolicy_.backgroundPolicyStatus_ = BACKGROUND_POLICY_ALLOW;
227 }
228 backgroundPolicy[CONFIG_BACKGROUND_POLICY_STATUS] = netPolicy_.backgroundPolicyStatus_;
229 root[CONFIG_BACKGROUND_POLICY] = backgroundPolicy;
230 }
231
WriteFile(const std::string & fileName)232 bool NetPolicyFile::WriteFile(const std::string &fileName)
233 {
234 std::unique_lock<std::mutex> lock(mutex_);
235 if (fileName.empty()) {
236 NETMGR_LOG_D("fileName is empty.");
237 return false;
238 }
239
240 Json::Value root;
241 Json::StreamWriterBuilder builder;
242 std::unique_ptr<Json::StreamWriter> streamWriter(builder.newStreamWriter());
243 std::fstream file(fileName.c_str(), std::fstream::out);
244 if (file.is_open() == false) {
245 NETMGR_LOG_E("fstream failed.");
246 return false;
247 }
248
249 if (netPolicy_.hosVersion.empty()) {
250 netPolicy_.hosVersion = HOS_VERSION;
251 }
252
253 root[CONFIG_HOS_VERSION] = Json::Value(netPolicy_.hosVersion);
254 // uid policy
255 AppendUidPolicy(root);
256 // background policy
257 AppendBackgroundPolicy(root);
258 // quota policy
259 AppendQuotaPolicy(root);
260 // cellular policy
261 AppendCellularPolicy(root);
262 std::ostringstream out;
263 streamWriter->write(root, &out);
264 file << out.str().c_str();
265 file.close();
266
267 return true;
268 }
269
WriteFile(NetUidPolicyOpType netUidPolicyOpType,uint32_t uid,NetUidPolicy policy)270 bool NetPolicyFile::WriteFile(NetUidPolicyOpType netUidPolicyOpType, uint32_t uid, NetUidPolicy policy)
271 {
272 if (netUidPolicyOpType == NetUidPolicyOpType::NET_POLICY_UID_OP_TYPE_UPDATE) {
273 for (auto &uidPolicy : netPolicy_.uidPolicys) {
274 if (uidPolicy.uid == std::to_string(uid)) {
275 uidPolicy.policy = std::to_string(static_cast<uint32_t>(policy));
276 break;
277 }
278 }
279 } else if (netUidPolicyOpType == NetUidPolicyOpType::NET_POLICY_UID_OP_TYPE_DELETE) {
280 for (auto iter = netPolicy_.uidPolicys.begin(); iter != netPolicy_.uidPolicys.end(); ++iter) {
281 if (iter->uid == std::to_string(uid)) {
282 netPolicy_.uidPolicys.erase(iter);
283 break;
284 }
285 }
286 } else {
287 UidPolicy uidPolicy;
288 uidPolicy.uid = std::to_string(uid);
289 uidPolicy.policy = std::to_string(static_cast<uint32_t>(policy));
290 netPolicy_.uidPolicys.push_back(uidPolicy);
291 }
292
293 if (!WriteFile(POLICY_FILE_NAME)) {
294 NETMGR_LOG_E("WriteFile failed");
295 return false;
296 }
297
298 return true;
299 }
300
UpdateQuotaPolicyExist(const NetPolicyQuotaPolicy & quotaPolicy)301 bool NetPolicyFile::UpdateQuotaPolicyExist(const NetPolicyQuotaPolicy "aPolicy)
302 {
303 if (netPolicy_.netQuotaPolicys.empty()) {
304 NETMGR_LOG_E("UpdateQuotaPolicyExist netPolicyQuotaPolicys is empty");
305 return false;
306 }
307
308 for (uint32_t i = 0; i < netPolicy_.netQuotaPolicys.size(); ++i) {
309 if (quotaPolicy.simId_ == netPolicy_.netQuotaPolicys[i].simId &&
310 netPolicy_.netQuotaPolicys[i].netType == std::to_string(quotaPolicy.netType_)) {
311 netPolicy_.netQuotaPolicys[i].lastLimitSnooze = std::to_string(quotaPolicy.lastLimitSnooze_);
312 netPolicy_.netQuotaPolicys[i].limitBytes = std::to_string(quotaPolicy.limitBytes_);
313 netPolicy_.netQuotaPolicys[i].metered = std::to_string(quotaPolicy.metered_);
314 netPolicy_.netQuotaPolicys[i].netType = std::to_string(quotaPolicy.netType_);
315 netPolicy_.netQuotaPolicys[i].periodDuration = quotaPolicy.periodDuration_;
316 netPolicy_.netQuotaPolicys[i].periodStartTime = std::to_string(quotaPolicy.periodStartTime_);
317 netPolicy_.netQuotaPolicys[i].source = std::to_string(quotaPolicy.source_);
318 netPolicy_.netQuotaPolicys[i].simId = quotaPolicy.simId_;
319 netPolicy_.netQuotaPolicys[i].warningBytes = std::to_string(quotaPolicy.warningBytes_);
320 return true;
321 }
322 }
323
324 return false;
325 }
326
WriteFile(const std::vector<NetPolicyQuotaPolicy> & quotaPolicies)327 bool NetPolicyFile::WriteFile(const std::vector<NetPolicyQuotaPolicy> "aPolicies)
328 {
329 uint32_t vSize = static_cast<uint32_t>(quotaPolicies.size());
330 NetPolicyQuota quotaPolicy;
331 for (uint32_t i = 0; i < vSize; i++) {
332 if (UpdateQuotaPolicyExist(quotaPolicies[i])) {
333 continue;
334 }
335 quotaPolicy.lastLimitSnooze = std::to_string(quotaPolicies[i].lastLimitSnooze_);
336 quotaPolicy.limitBytes = std::to_string(quotaPolicies[i].limitBytes_);
337 quotaPolicy.metered = std::to_string(quotaPolicies[i].metered_);
338 quotaPolicy.netType = std::to_string(quotaPolicies[i].netType_);
339 quotaPolicy.periodDuration = quotaPolicies[i].periodDuration_;
340 quotaPolicy.periodStartTime = std::to_string(quotaPolicies[i].periodStartTime_);
341 quotaPolicy.source = std::to_string(quotaPolicies[i].source_);
342 quotaPolicy.simId = quotaPolicies[i].simId_;
343 quotaPolicy.warningBytes = std::to_string(quotaPolicies[i].warningBytes_);
344 netPolicy_.netQuotaPolicys.push_back(quotaPolicy);
345 }
346
347 if (!WriteFile(POLICY_FILE_NAME)) {
348 NETMGR_LOG_E("WriteFile failed");
349 return false;
350 }
351
352 return true;
353 }
354
UpdateCellularPolicyExist(const NetPolicyCellularPolicy & cellularPolicy)355 bool NetPolicyFile::UpdateCellularPolicyExist(const NetPolicyCellularPolicy &cellularPolicy)
356 {
357 if (netPolicy_.netCellularPolicys.empty()) {
358 NETMGR_LOG_E("UpdateCellularPolicyExist netCellularPolicys is empty");
359 return false;
360 }
361
362 for (uint32_t i = 0; i < netPolicy_.netCellularPolicys.size(); ++i) {
363 if (cellularPolicy.simId_ == netPolicy_.netCellularPolicys[i].simId) {
364 netPolicy_.netCellularPolicys[i].simId = cellularPolicy.simId_;
365 netPolicy_.netCellularPolicys[i].periodStartTime = std::to_string(cellularPolicy.periodStartTime_);
366 netPolicy_.netCellularPolicys[i].periodDuration = cellularPolicy.periodDuration_;
367 netPolicy_.netCellularPolicys[i].title = cellularPolicy.title_;
368 netPolicy_.netCellularPolicys[i].summary = cellularPolicy.summary_;
369 netPolicy_.netCellularPolicys[i].limitBytes = std::to_string(cellularPolicy.limitBytes_);
370 netPolicy_.netCellularPolicys[i].limitAction = std::to_string(cellularPolicy.limitAction_);
371 netPolicy_.netCellularPolicys[i].usedBytes = std::to_string(cellularPolicy.usedBytes_);
372 netPolicy_.netCellularPolicys[i].usedTimeDuration = std::to_string(cellularPolicy.usedTimeDuration_);
373 netPolicy_.netCellularPolicys[i].possessor = cellularPolicy.possessor_;
374 return true;
375 }
376 }
377
378 return false;
379 }
380
WriteFile(const std::vector<NetPolicyCellularPolicy> & cellularPolicies)381 bool NetPolicyFile::WriteFile(const std::vector<NetPolicyCellularPolicy> &cellularPolicies)
382 {
383 uint32_t vSize = static_cast<uint32_t>(cellularPolicies.size());
384 NetPolicyCellular cellularPolicy;
385 for (uint32_t i = 0; i < vSize; i++) {
386 if (UpdateCellularPolicyExist(cellularPolicies[i])) {
387 continue;
388 }
389 cellularPolicy.simId = cellularPolicies[i].simId_;
390 cellularPolicy.periodStartTime = std::to_string(cellularPolicies[i].periodStartTime_);
391 cellularPolicy.periodDuration = cellularPolicies[i].periodDuration_;
392 cellularPolicy.title = cellularPolicies[i].title_;
393 cellularPolicy.summary = cellularPolicies[i].summary_;
394 cellularPolicy.limitBytes = std::to_string(cellularPolicies[i].limitBytes_);
395 cellularPolicy.limitAction = std::to_string(cellularPolicies[i].limitAction_);
396 cellularPolicy.usedBytes = std::to_string(cellularPolicies[i].usedBytes_);
397 cellularPolicy.usedTimeDuration = std::to_string(cellularPolicies[i].usedTimeDuration_);
398 cellularPolicy.possessor = cellularPolicies[i].possessor_;
399 netPolicy_.netCellularPolicys.push_back(cellularPolicy);
400 }
401
402 if (!WriteFile(POLICY_FILE_NAME)) {
403 NETMGR_LOG_E("WriteFile failed");
404 return false;
405 }
406
407 return true;
408 }
409
IsUidPolicyExist(uint32_t uid)410 bool NetPolicyFile::IsUidPolicyExist(uint32_t uid)
411 {
412 uint32_t size = netPolicy_.uidPolicys.size();
413 for (uint32_t i = 0; i < size; i++) {
414 if (static_cast<uint32_t>(stol(netPolicy_.uidPolicys[i].uid)) == uid) {
415 return true;
416 }
417 }
418
419 return false;
420 }
421
GetPolicyByUid(uint32_t uid)422 NetUidPolicy NetPolicyFile::GetPolicyByUid(uint32_t uid)
423 {
424 for (auto &uidPolicy : netPolicy_.uidPolicys) {
425 if (uidPolicy.uid == std::to_string(uid)) {
426 return static_cast<NetUidPolicy>(std::stol(uidPolicy.policy));
427 }
428 }
429
430 return NetUidPolicy::NET_POLICY_NONE;
431 }
432
GetUidsByPolicy(NetUidPolicy policy,std::vector<uint32_t> & uids)433 bool NetPolicyFile::GetUidsByPolicy(NetUidPolicy policy, std::vector<uint32_t> &uids)
434 {
435 for (auto &uidPolicy : netPolicy_.uidPolicys) {
436 if (uidPolicy.policy == std::to_string(static_cast<uint32_t>(policy))) {
437 uint32_t uid = static_cast<uint32_t>(std::stol(uidPolicy.uid));
438 uids.push_back(uid);
439 }
440 }
441
442 return true;
443 }
444
GetNetQuotaPolicies(std::vector<NetPolicyQuotaPolicy> & quotaPolicies)445 NetPolicyResultCode NetPolicyFile::GetNetQuotaPolicies(std::vector<NetPolicyQuotaPolicy> "aPolicies)
446 {
447 NetPolicyQuotaPolicy quotaPolicyTmp;
448 for (auto "aPolicy : netPolicy_.netQuotaPolicys) {
449 quotaPolicyTmp.lastLimitSnooze_ = static_cast<int64_t>(std::stoll(quotaPolicy.lastLimitSnooze));
450 quotaPolicyTmp.limitBytes_ = static_cast<int64_t>(std::stoll(quotaPolicy.limitBytes));
451 quotaPolicyTmp.metered_ = static_cast<int8_t>(std::stol(quotaPolicy.metered));
452 quotaPolicyTmp.netType_ = static_cast<int8_t>(std::stol(quotaPolicy.netType));
453 quotaPolicyTmp.periodDuration_ = quotaPolicy.periodDuration;
454 quotaPolicyTmp.periodStartTime_ = static_cast<int64_t>(std::stoll(quotaPolicy.periodStartTime));
455 quotaPolicyTmp.source_ = static_cast<int8_t>(std::stol(quotaPolicy.source));
456 quotaPolicyTmp.simId_ = quotaPolicy.simId;
457 quotaPolicyTmp.warningBytes_ = static_cast<int64_t>(std::stoll(quotaPolicy.warningBytes));
458 quotaPolicies.push_back(quotaPolicyTmp);
459 }
460
461 return NetPolicyResultCode::ERR_NONE;
462 }
463
GetNetQuotaPolicy(int8_t netType,const std::string & simId,NetPolicyQuotaPolicy & quotaPolicy)464 NetPolicyResultCode NetPolicyFile::GetNetQuotaPolicy(int8_t netType, const std::string &simId,
465 NetPolicyQuotaPolicy "aPolicy)
466 {
467 for (auto "aPolicyTemp : netPolicy_.netQuotaPolicys) {
468 if (netType == static_cast<int8_t>(std::stol(quotaPolicyTemp.netType)) &&
469 simId == quotaPolicyTemp.simId) {
470 quotaPolicy.lastLimitSnooze_ = static_cast<int64_t>(std::stoll(quotaPolicyTemp.lastLimitSnooze));
471 quotaPolicy.limitBytes_ = static_cast<int64_t>(std::stoll(quotaPolicyTemp.limitBytes));
472 quotaPolicy.metered_ = static_cast<int8_t>(std::stol(quotaPolicyTemp.metered));
473 quotaPolicy.netType_ = static_cast<int8_t>(std::stol(quotaPolicyTemp.netType));
474 quotaPolicy.periodDuration_ = quotaPolicyTemp.periodDuration;
475 quotaPolicy.periodStartTime_ = static_cast<int64_t>(std::stoll(quotaPolicyTemp.periodStartTime));
476 quotaPolicy.source_ = static_cast<int8_t>(std::stol(quotaPolicyTemp.source));
477 quotaPolicy.simId_ = quotaPolicyTemp.simId;
478 quotaPolicy.warningBytes_ = static_cast<int64_t>(std::stoll(quotaPolicyTemp.warningBytes));
479 return NetPolicyResultCode::ERR_NONE;
480 }
481 }
482
483 return NetPolicyResultCode::ERR_QUOTA_POLICY_NOT_EXIST;
484 }
485
IsInterfaceMetered(const std::string & ifaceName)486 bool NetPolicyFile::IsInterfaceMetered(const std::string &ifaceName)
487 {
488 for (auto "aPolicy : netPolicy_.netQuotaPolicys) {
489 int8_t netType = static_cast<int8_t>(std::stol(quotaPolicy.netType));
490 NetBearType bearerType = static_cast<NetBearType>(netType);
491 std::string policyIfaceName;
492 int32_t ret = NetManagerCenter::GetInstance().GetIfaceNameByType(bearerType,
493 IDENT_PREFIX, policyIfaceName);
494 if (ret != 0 || policyIfaceName.empty()) {
495 continue;
496 }
497 if (ifaceName.compare(policyIfaceName) == 0) {
498 return static_cast<bool>(std::stol(quotaPolicy.metered));
499 }
500 }
501 return false;
502 }
503
GetCellularPolicies(std::vector<NetPolicyCellularPolicy> & cellularPolicies)504 NetPolicyResultCode NetPolicyFile::GetCellularPolicies(std::vector<NetPolicyCellularPolicy> &cellularPolicies)
505 {
506 NetPolicyCellularPolicy cellularPolicyTmp;
507 for (auto &cellularPolicy : netPolicy_.netCellularPolicys) {
508 cellularPolicyTmp.simId_ = cellularPolicy.simId;
509 cellularPolicyTmp.periodStartTime_ = static_cast<int64_t>(std::stoll(cellularPolicy.periodStartTime));
510 cellularPolicyTmp.periodDuration_ = cellularPolicy.periodDuration;
511 cellularPolicyTmp.title_ = cellularPolicy.title;
512 cellularPolicyTmp.summary_ = cellularPolicy.summary;
513 cellularPolicyTmp.limitBytes_ = static_cast<int64_t>(std::stoll(cellularPolicy.limitBytes));
514 cellularPolicyTmp.limitAction_ = static_cast<int32_t>(std::stol(cellularPolicy.limitAction));
515 cellularPolicyTmp.usedBytes_ = static_cast<int64_t>(std::stoll(cellularPolicy.usedBytes));
516 cellularPolicyTmp.usedTimeDuration_ = static_cast<int64_t>(std::stoll(cellularPolicy.usedTimeDuration));
517 cellularPolicyTmp.possessor_ = cellularPolicy.possessor;
518 cellularPolicies.push_back(cellularPolicyTmp);
519 }
520
521 return NetPolicyResultCode::ERR_NONE;
522 }
523
SetFactoryPolicy(const std::string & simId)524 NetPolicyResultCode NetPolicyFile::SetFactoryPolicy(const std::string &simId)
525 {
526 netPolicy_.uidPolicys.clear();
527 netPolicy_.backgroundPolicyStatus_ = BACKGROUND_POLICY_ALLOW;
528
529 if (simId.empty()) {
530 netPolicy_.netQuotaPolicys.clear();
531 netPolicy_.netCellularPolicys.clear();
532 } else {
533 for (auto iter = netPolicy_.netQuotaPolicys.begin(); iter != netPolicy_.netQuotaPolicys.end(); ++iter) {
534 if (simId == iter->simId) {
535 netPolicy_.netQuotaPolicys.erase(iter);
536 break;
537 }
538 }
539
540 for (auto iter = netPolicy_.netCellularPolicys.begin(); iter != netPolicy_.netCellularPolicys.end(); ++iter) {
541 if (simId == iter->simId) {
542 netPolicy_.netCellularPolicys.erase(iter);
543 break;
544 }
545 }
546 }
547
548 if (!WriteFile(POLICY_FILE_NAME)) {
549 NETMGR_LOG_E("WriteFile failed");
550 return NetPolicyResultCode::ERR_INTERNAL_ERROR;
551 }
552
553 return NetPolicyResultCode::ERR_NONE;
554 }
555
SetBackgroundPolicy(bool backgroundPolicy)556 NetPolicyResultCode NetPolicyFile::SetBackgroundPolicy(bool backgroundPolicy)
557 {
558 if (backgroundPolicy) {
559 netPolicy_.backgroundPolicyStatus_ = BACKGROUND_POLICY_ALLOW;
560 } else {
561 netPolicy_.backgroundPolicyStatus_ = BACKGROUND_POLICY_REJECT;
562 }
563
564 if (!WriteFile(POLICY_FILE_NAME)) {
565 NETMGR_LOG_E("WriteFile failed");
566 return NetPolicyResultCode::ERR_INTERNAL_ERROR;
567 }
568
569 return NetPolicyResultCode::ERR_NONE;
570 }
571
GetBackgroundPolicy()572 bool NetPolicyFile::GetBackgroundPolicy()
573 {
574 if (netPolicy_.backgroundPolicyStatus_.compare(BACKGROUND_POLICY_ALLOW) == 0) {
575 return true;
576 }
577 return false;
578 }
579
InitPolicy()580 bool NetPolicyFile::InitPolicy()
581 {
582 NETMGR_LOG_I("InitPolicyFile.");
583 std::string content;
584 if (!ReadFile(POLICY_FILE_NAME, content)) {
585 if (!CreateFile(POLICY_FILE_NAME)) {
586 NETMGR_LOG_D("CreateFile [%{public}s] failed", POLICY_FILE_NAME.c_str());
587 return false;
588 }
589 }
590
591 if (!content.empty() && !Json2Obj(content, netPolicy_)) {
592 NETMGR_LOG_E("Analysis fileconfig failed");
593 return false;
594 }
595 return true;
596 }
597 } // namespace NetManagerStandard
598 } // namespace OHOS
599