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 #ifndef NET_POLICY_FILE_H 17 #define NET_POLICY_FILE_H 18 19 #include <climits> 20 #include <fcntl.h> 21 #include <fstream> 22 #include <iostream> 23 #include <memory> 24 #include <mutex> 25 #include <sstream> 26 #include <sys/sendfile.h> 27 #include <sys/stat.h> 28 #include <sys/types.h> 29 #include <unistd.h> 30 #include <vector> 31 32 #include <json/json.h> 33 34 #include "singleton.h" 35 36 #include "net_policy_constants.h" 37 #include "net_policy_inner_define.h" 38 #include "net_quota_policy.h" 39 40 namespace OHOS { 41 namespace NetManagerStandard { 42 enum NetUidPolicyOpType { 43 NET_POLICY_UID_OP_TYPE_DO_NOTHING = 0, 44 NET_POLICY_UID_OP_TYPE_ADD = 1, 45 NET_POLICY_UID_OP_TYPE_DELETE = 2, 46 NET_POLICY_UID_OP_TYPE_UPDATE = 3, 47 }; 48 49 class NetPolicyFile : public std::enable_shared_from_this<NetPolicyFile> { 50 DECLARE_DELAYED_SINGLETON(NetPolicyFile); 51 52 public: 53 /** 54 * Init by reading policy from file. 55 * @return true Return true means init policy successful. 56 * @return false Return false means init policy failed. 57 */ 58 bool InitPolicy(); 59 60 /** 61 * Judge if this uid is exist. 62 * @param uid The specified UID of application. 63 * @return true Return true means this uid is exist. 64 * @return false Return false means this uid is not exist. 65 */ 66 bool IsUidPolicyExist(uint32_t uid); 67 68 /** 69 * Write struct vector of quotaPolicies to file. 70 * @param quotaPolicies The struct vector of quotaPolicies. 71 * @return true Return true means write quotaPolicies to file successful. 72 * @return false Return false means write quotaPolicies to file failed. 73 */ 74 bool WriteFile(const std::vector<NetQuotaPolicy> "aPolicies); 75 76 /** 77 * Write uid and policy to file. 78 * @param uid The specified UID of application. 79 * @param policy See {@link NetUidPolicy}. 80 */ 81 void WriteFile(uint32_t uid, uint32_t policy); 82 83 /** 84 * Get policy by uid from vector UidPolicy of struct NetPolicy. 85 * @param uid The specified UID of application. 86 * @return NetUidPolicy See {@link NetUidPolicy}. 87 */ 88 NetUidPolicy GetPolicyByUid(uint32_t uid); 89 90 /** 91 * Get uids by policy from vector UidPolicy of struct NetPolicy. 92 * @param policy See {@link NetUidPolicy}. 93 * @param uids The specified UIDS of application. 94 * @return true Return true means get uids that policy equal input policy. 95 * @return false Return false means get none uids that policy equal input policy. 96 */ 97 bool GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids); 98 99 /** 100 * Add quota policies into struct vector quotaPolicies. 101 * @param quotaPolicies The struct vector of quotaPolicies. 102 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 103 */ 104 int32_t ReadQuotaPolicies(std::vector<NetQuotaPolicy> "aPolicies); 105 106 /** 107 * Use netType and iccid to judge if this quota Policy in NetPolicyQuota vector of struct NetPolicy. 108 * @param netType For details, see {@link NetBearType}. 109 * @param iccid The string type of iccid. 110 * @param quotaPolicy The struct vector of quotaPolicies. 111 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 112 */ 113 int32_t GetNetQuotaPolicy(int32_t netType, const std::string &iccid, NetQuotaPolicy "aPolicy); 114 115 /** 116 * Clear the uid and policy in struct NetPolicy's uidPolicy vector, 117 * reset the background policy status to default, 118 * reset the netQuotaPolicy which IccId equal iccid, 119 * write these changes to file. 120 * @param iccid The net quota policy's sim id. 121 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 122 */ 123 int32_t ResetPolicies(const std::string &iccid); 124 125 /** 126 * Write the background policy to file. 127 * @param allowBackground When the allowBackground is true,it means "allow" background policy, 128 * when the allowBackground is false,if means "reject" background policy. 129 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 130 */ 131 int32_t SetBackgroundPolicy(bool allowBackground); 132 133 /** 134 * Get background policy from file. 135 * @return true True means allow background policy. 136 * @return false False means reject background policy. 137 */ 138 bool GetBackgroundPolicy(); 139 140 /** 141 * Get struct vector uid and policy from file. 142 * @return const std::vector<UidPolicy>& Return struct vector netPolicy_.uidPolicies. 143 */ 144 const std::vector<UidPolicy> &GetNetPolicies(); 145 146 private: 147 bool FileExists(const std::string &fileName); 148 bool CreateFile(const std::string &fileName); 149 bool WriteFile(const std::string &fileName); 150 bool WriteFile(uint32_t netUidPolicyOpType, uint32_t uid, uint32_t policy); 151 bool ReadFile(const std::string &fileName, std::string &content); 152 bool Json2Obj(const std::string &content, NetPolicy &netPolicy); 153 void AppendUidPolicy(Json::Value &root); 154 void AppendBackgroundPolicy(Json::Value &root); 155 void AppendQuotaPolicy(Json::Value &root); 156 void ParseUidPolicy(const Json::Value &root, NetPolicy &netPolicy); 157 void ParseBackgroundPolicy(const Json::Value &root, NetPolicy &netPolicy); 158 void ParseQuotaPolicy(const Json::Value &root, NetPolicy &netPolicy); 159 void ParseCellularPolicy(const Json::Value &root, NetPolicy &netPolicy); 160 bool UpdateQuotaPolicyExist(const NetQuotaPolicy "aPolicy); 161 uint32_t ArbitrationWritePolicyToFile(uint32_t uid, uint32_t policy); 162 163 private: 164 NetPolicy netPolicy_; 165 std::mutex mutex_; 166 }; 167 } // namespace NetManagerStandard 168 } // namespace OHOS 169 #endif // NET_POLICY_FILE_H 170