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