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