1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (C) 2022-2023 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#ifndef NETMANAGER_BASE_BANDWIDTH_MANAGER_H
17b1b8bc3fSopenharmony_ci#define NETMANAGER_BASE_BANDWIDTH_MANAGER_H
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_ci#include <iostream>
20b1b8bc3fSopenharmony_ci#include <map>
21b1b8bc3fSopenharmony_ci#include <mutex>
22b1b8bc3fSopenharmony_ci#include <vector>
23b1b8bc3fSopenharmony_ci#include <unordered_set>
24b1b8bc3fSopenharmony_ci
25b1b8bc3fSopenharmony_ci#include "i_notify_callback.h"
26b1b8bc3fSopenharmony_ci#include "iptables_type.h"
27b1b8bc3fSopenharmony_ci
28b1b8bc3fSopenharmony_cinamespace OHOS {
29b1b8bc3fSopenharmony_cinamespace nmd {
30b1b8bc3fSopenharmony_ciclass BandwidthManager {
31b1b8bc3fSopenharmony_ciprivate:
32b1b8bc3fSopenharmony_ci    enum Operate {
33b1b8bc3fSopenharmony_ci        OP_SET = 1,
34b1b8bc3fSopenharmony_ci        OP_UNSET = 2,
35b1b8bc3fSopenharmony_ci    };
36b1b8bc3fSopenharmony_ci
37b1b8bc3fSopenharmony_cipublic:
38b1b8bc3fSopenharmony_ci    BandwidthManager();
39b1b8bc3fSopenharmony_ci    ~BandwidthManager();
40b1b8bc3fSopenharmony_ci
41b1b8bc3fSopenharmony_ci    /**
42b1b8bc3fSopenharmony_ci     * Enable data saver
43b1b8bc3fSopenharmony_ci     *
44b1b8bc3fSopenharmony_ci     * @param enable Enable or disable
45b1b8bc3fSopenharmony_ci     *
46b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
47b1b8bc3fSopenharmony_ci     */
48b1b8bc3fSopenharmony_ci    int32_t EnableDataSaver(bool enable);
49b1b8bc3fSopenharmony_ci
50b1b8bc3fSopenharmony_ci    /**
51b1b8bc3fSopenharmony_ci     * Set iface quota
52b1b8bc3fSopenharmony_ci     *
53b1b8bc3fSopenharmony_ci     * @param ifName Iface name
54b1b8bc3fSopenharmony_ci     * @param bytes Quota value
55b1b8bc3fSopenharmony_ci     *
56b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
57b1b8bc3fSopenharmony_ci     */
58b1b8bc3fSopenharmony_ci    int32_t SetIfaceQuota(const std::string &ifName, int64_t bytes);
59b1b8bc3fSopenharmony_ci
60b1b8bc3fSopenharmony_ci    /**
61b1b8bc3fSopenharmony_ci     * Remove iface quota
62b1b8bc3fSopenharmony_ci     *
63b1b8bc3fSopenharmony_ci     * @param ifName Iface name
64b1b8bc3fSopenharmony_ci     * @param bytes Quota value
65b1b8bc3fSopenharmony_ci     *
66b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
67b1b8bc3fSopenharmony_ci     */
68b1b8bc3fSopenharmony_ci    int32_t RemoveIfaceQuota(const std::string &ifName);
69b1b8bc3fSopenharmony_ci
70b1b8bc3fSopenharmony_ci    /**
71b1b8bc3fSopenharmony_ci     * Add denied list
72b1b8bc3fSopenharmony_ci     * @param ifName Iface name
73b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
74b1b8bc3fSopenharmony_ci     */
75b1b8bc3fSopenharmony_ci    int32_t AddDeniedList(uint32_t uid);
76b1b8bc3fSopenharmony_ci
77b1b8bc3fSopenharmony_ci    /**
78b1b8bc3fSopenharmony_ci     * Remove denied list
79b1b8bc3fSopenharmony_ci     *
80b1b8bc3fSopenharmony_ci     * @param uid Uid
81b1b8bc3fSopenharmony_ci     *
82b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
83b1b8bc3fSopenharmony_ci     */
84b1b8bc3fSopenharmony_ci    int32_t RemoveDeniedList(uint32_t uid);
85b1b8bc3fSopenharmony_ci
86b1b8bc3fSopenharmony_ci    /**
87b1b8bc3fSopenharmony_ci     * Add allowed list
88b1b8bc3fSopenharmony_ci     *
89b1b8bc3fSopenharmony_ci     * @param uid Uid
90b1b8bc3fSopenharmony_ci     *
91b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
92b1b8bc3fSopenharmony_ci     */
93b1b8bc3fSopenharmony_ci    int32_t AddAllowedList(uint32_t uid);
94b1b8bc3fSopenharmony_ci
95b1b8bc3fSopenharmony_ci    /**
96b1b8bc3fSopenharmony_ci     * Remove allowed list
97b1b8bc3fSopenharmony_ci     *
98b1b8bc3fSopenharmony_ci     * @param uid Uid
99b1b8bc3fSopenharmony_ci     *
100b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
101b1b8bc3fSopenharmony_ci     */
102b1b8bc3fSopenharmony_ci    int32_t RemoveAllowedList(uint32_t uid);
103b1b8bc3fSopenharmony_ci
104b1b8bc3fSopenharmony_ciprivate:
105b1b8bc3fSopenharmony_ci    std::string FetchChainName(NetManagerStandard::ChainType chain);
106b1b8bc3fSopenharmony_ci    int32_t InitChain();
107b1b8bc3fSopenharmony_ci    int32_t DeInitChain();
108b1b8bc3fSopenharmony_ci    int32_t InitDefaultBwChainRules();
109b1b8bc3fSopenharmony_ci    int32_t InitDefaultListBoxChainRules();
110b1b8bc3fSopenharmony_ci    int32_t InitDefaultAlertChainRules();
111b1b8bc3fSopenharmony_ci    int32_t InitDefaultRules();
112b1b8bc3fSopenharmony_ci    int32_t IptablesNewChain(NetManagerStandard::ChainType chain);
113b1b8bc3fSopenharmony_ci    int32_t IptablesNewChain(const std::string &chainName);
114b1b8bc3fSopenharmony_ci    int32_t IptablesDeleteChain(NetManagerStandard::ChainType chain);
115b1b8bc3fSopenharmony_ci    int32_t IptablesDeleteChain(const std::string &chainName);
116b1b8bc3fSopenharmony_ci    int32_t SetGlobalAlert(Operate operate, int64_t bytes);
117b1b8bc3fSopenharmony_ci    int32_t SetCostlyAlert(Operate operate, const std::string &iface, int64_t bytes);
118b1b8bc3fSopenharmony_ci    inline void CheckChainInitialization();
119b1b8bc3fSopenharmony_ci    int32_t SetIfaceQuotaDetail(const std::string &ifName, int64_t bytes);
120b1b8bc3fSopenharmony_ci
121b1b8bc3fSopenharmony_ciprivate:
122b1b8bc3fSopenharmony_ci    bool chainInitFlag_ = false;
123b1b8bc3fSopenharmony_ci    bool dataSaverEnable_ = false;
124b1b8bc3fSopenharmony_ci    int64_t globalAlertBytes_ = 0;
125b1b8bc3fSopenharmony_ci    std::mutex bandwidthMutex_;
126b1b8bc3fSopenharmony_ci    std::map<std::string, int64_t> ifaceAlertBytes_;
127b1b8bc3fSopenharmony_ci    std::map<std::string, int64_t> ifaceQuotaBytes_;
128b1b8bc3fSopenharmony_ci    std::unordered_set<uint32_t> deniedListUids_;
129b1b8bc3fSopenharmony_ci    std::unordered_set<uint32_t> allowedListUids_;
130b1b8bc3fSopenharmony_ci};
131b1b8bc3fSopenharmony_ci} // namespace nmd
132b1b8bc3fSopenharmony_ci} // namespace OHOS
133b1b8bc3fSopenharmony_ci#endif /* NETMANAGER_BASE_BANDWIDTH_MANAGER_H */
134