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 NETMANAGER_BASE_BANDWIDTH_MANAGER_H 17 #define NETMANAGER_BASE_BANDWIDTH_MANAGER_H 18 19 #include <iostream> 20 #include <map> 21 #include <mutex> 22 #include <vector> 23 24 #include "i_notify_callback.h" 25 #include "iptables_type.h" 26 27 namespace OHOS { 28 namespace nmd { 29 class BandwidthManager { 30 private: 31 enum Operate { 32 OP_SET = 1, 33 OP_UNSET = 2, 34 }; 35 36 public: 37 BandwidthManager(); 38 ~BandwidthManager(); 39 40 /** 41 * Enable data saver 42 * 43 * @param enable Enable or disable 44 * 45 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 46 */ 47 int32_t EnableDataSaver(bool enable); 48 49 /** 50 * Set iface quota 51 * 52 * @param ifName Iface name 53 * @param bytes Quota value 54 * 55 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 56 */ 57 int32_t SetIfaceQuota(const std::string &ifName, int64_t bytes); 58 59 /** 60 * Remove iface quota 61 * 62 * @param ifName Iface name 63 * @param bytes Quota value 64 * 65 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 66 */ 67 int32_t RemoveIfaceQuota(const std::string &ifName); 68 69 /** 70 * Add denied list 71 * @param ifName Iface name 72 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 73 */ 74 int32_t AddDeniedList(uint32_t uid); 75 76 /** 77 * Remove denied list 78 * 79 * @param uid Uid 80 * 81 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 82 */ 83 int32_t RemoveDeniedList(uint32_t uid); 84 85 /** 86 * Add allowed list 87 * 88 * @param uid Uid 89 * 90 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 91 */ 92 int32_t AddAllowedList(uint32_t uid); 93 94 /** 95 * Remove allowed list 96 * 97 * @param uid Uid 98 * 99 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 100 */ 101 int32_t RemoveAllowedList(uint32_t uid); 102 103 private: 104 std::string FetchChainName(NetManagerStandard::ChainType chain); 105 int32_t InitChain(); 106 int32_t DeInitChain(); 107 int32_t InitDefaultBwChainRules(); 108 int32_t InitDefaultListBoxChainRules(); 109 int32_t InitDefaultAlertChainRules(); 110 int32_t InitDefaultRules(); 111 int32_t IptablesNewChain(NetManagerStandard::ChainType chain); 112 int32_t IptablesNewChain(const std::string &chainName); 113 int32_t IptablesDeleteChain(NetManagerStandard::ChainType chain); 114 int32_t IptablesDeleteChain(const std::string &chainName); 115 int32_t SetGlobalAlert(Operate operate, int64_t bytes); 116 int32_t SetCostlyAlert(Operate operate, const std::string &iface, int64_t bytes); 117 inline void CheckChainInitialization(); 118 int32_t SetIfaceQuotaDetail(const std::string &ifName, int64_t bytes); 119 120 private: 121 bool chainInitFlag_ = false; 122 bool dataSaverEnable_ = false; 123 int64_t globalAlertBytes_ = 0; 124 std::mutex bandwidthMutex_; 125 std::map<std::string, int64_t> ifaceAlertBytes_; 126 std::map<std::string, int64_t> ifaceQuotaBytes_; 127 std::vector<uint32_t> deniedListUids_; 128 std::vector<uint32_t> allowedListUids_; 129 }; 130 } // namespace nmd 131 } // namespace OHOS 132 #endif /* NETMANAGER_BASE_BANDWIDTH_MANAGER_H */ 133