1 /*
2  * Copyright (c) 2021-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 NET_POLICY_TRAFFIC_H
17 #define NET_POLICY_TRAFFIC_H
18 
19 #include "singleton.h"
20 #include "system_ability.h"
21 
22 #include "net_policy_base.h"
23 #include "net_policy_callback.h"
24 #include "net_policy_service_stub.h"
25 #include "netsys_controller_callback.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 constexpr int16_t NET_POLICY_LEAP_YEAR_ONE = 1;
30 constexpr int16_t NET_POLICY_LEAP_YEAR_FOUR = 4;
31 constexpr int16_t NET_POLICY_LEAP_YEAR_ONEHUNDRED = 100;
32 constexpr int16_t NET_POLICY_LEAP_YEAR_FOURHUNDRED = 400;
33 constexpr int16_t NET_POLICY_FEBRUARY = 1;
34 constexpr int32_t NET_POLICY_ONEDAYTIME = 86400;
35 constexpr int16_t MONTH_TWENTY_EIGHT = 28;
36 constexpr int16_t MONTH_THIRTY = 30;
37 constexpr int16_t MONTH_THIRTY_ONE = 31;
38 constexpr int32_t NINETY_PERCENTAGE = 90;
39 constexpr int32_t HUNDRED_PERCENTAGE = 100;
40 class NetPolicyTraffic : public NetPolicyBase {
41 public:
42     void Init();
43 
44     /**
45      * Update quota policies.
46      *
47      * @param quotaPolicies The updated quota policies
48      * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}
49      */
50     int32_t UpdateQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies);
51 
52     /**
53      * Get network policies.
54      *
55      * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}.
56      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
57      */
58     int32_t GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies);
59 
60     /**
61      * Update the limit or warning remind time of quota policy.
62      *
63      * @param netType {@link NetBearType}.
64      * @param simId Specify the matched simId of quota policy when netType is cellular.
65      * @param remindType {@link RemindType}.
66      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
67      */
68     int32_t UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType);
69 
70     /**
71      * Handle the event from NetPolicyCore
72      *
73      * @param eventId The event id
74      * @param policyEvent The informations passed from other core
75      */
76     void HandleEvent(int32_t eventId, const std::shared_ptr<PolicyEvent> &policyEvent);
77 
78     /**
79      * Get the metered ifaces.
80      *
81      * @return const std::vector<std::string>& The vector of metered ifaces
82      */
83     const std::vector<std::string> &GetMeteredIfaces();
84 
85     /**
86      * Reset network policies\rules\quota policies\firewall rules.
87      *
88      * @param simId Specify the matched simId of quota policy.
89      */
90     int32_t ResetPolicies(const std::string &simId);
91 
92     /**
93      * Reset all network policies\rules\quota policies\firewall rules.
94      *
95      */
96     int32_t ResetPolicies();
97 
98     void ReachedLimit(const std::string &iface);
99     void UpdateNetPolicy();
100     void GetDumpMessage(std::string &message);
101 
102 private:
103     class NetsysControllerCallbackImpl : public NetsysControllerCallback {
104     public:
NetsysControllerCallbackImpl(std::shared_ptr<NetPolicyTraffic> traffic)105         NetsysControllerCallbackImpl(std::shared_ptr<NetPolicyTraffic> traffic)
106         {
107             traffic_ = traffic;
108         }
OnInterfaceAddressUpdated(const std::string &,const std::string &,int32_t,int32_t)109         int32_t OnInterfaceAddressUpdated(const std::string &, const std::string &, int32_t, int32_t)
110         {
111             return 0;
112         }
OnInterfaceAddressRemoved(const std::string &,const std::string &,int32_t,int32_t)113         int32_t OnInterfaceAddressRemoved(const std::string &, const std::string &, int32_t, int32_t)
114         {
115             return 0;
116         }
OnInterfaceAdded(const std::string &)117         int32_t OnInterfaceAdded(const std::string &)
118         {
119             return 0;
120         }
OnInterfaceRemoved(const std::string &)121         int32_t OnInterfaceRemoved(const std::string &)
122         {
123             return 0;
124         }
OnInterfaceChanged(const std::string &,bool)125         int32_t OnInterfaceChanged(const std::string &, bool)
126         {
127             return 0;
128         }
OnInterfaceLinkStateChanged(const std::string &,bool)129         int32_t OnInterfaceLinkStateChanged(const std::string &, bool)
130         {
131             return 0;
132         }
OnRouteChanged(bool,const std::string &,const std::string &,const std::string &)133         int32_t OnRouteChanged(bool, const std::string &, const std::string &, const std::string &)
134         {
135             return 0;
136         }
OnDhcpSuccess(NetsysControllerCallback::DhcpResult & dhcpResult)137         int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult)
138         {
139             return 0;
140         }
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)141         int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface)
142         {
143             traffic_->ReachedLimit(iface);
144             return 0;
145         }
146 
147     private:
148         std::shared_ptr<NetPolicyTraffic> traffic_ = nullptr;
149     };
150 
151     class ConnCallBack : public IRemoteStub<INetConnCallback> {
152     public:
ConnCallBack(std::shared_ptr<NetPolicyTraffic> connCallBack)153         ConnCallBack(std::shared_ptr<NetPolicyTraffic> connCallBack)
154         {
155             connCallBack_ = connCallBack;
156         }
NetAvailable(sptr<NetHandle> & netHandle)157         int32_t NetAvailable(sptr<NetHandle> &netHandle)
158         {
159             if (connCallBack_ != nullptr) {
160                 connCallBack_->UpdateNetPolicy();
161                 return 0;
162             }
163             return -1;
164         }
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)165         int32_t NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
166         {
167             return 0;
168         }
169 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)170         int32_t NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
171         {
172             return 0;
173         }
174 
NetLost(sptr<NetHandle> & netHandle)175         int32_t NetLost(sptr<NetHandle> &netHandle)
176         {
177             return 0;
178         }
179 
NetUnavailable()180         int32_t NetUnavailable()
181         {
182             return 0;
183         }
184 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)185         int32_t NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
186         {
187             return 0;
188         }
189 
190     private:
191         std::shared_ptr<NetPolicyTraffic> connCallBack_ = nullptr;
192     };
193 
194 private:
195     int32_t UpdateQuotaPoliciesInner();
196     int64_t GetQuotaRemain(NetQuotaPolicy &quotaPolicy);
197     void UpdateQuotaNotify();
198     void UpdateMeteredIfaces(std::vector<std::string> &newMeteredIfaces);
199     void UpdateNetEnableStatus(const NetQuotaPolicy &quotaPolicy);
200     void FormalizeQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies);
201     const std::vector<std::string> UpdateMeteredIfacesQuota();
202 
203     bool IsValidQuotaPolicy(const NetQuotaPolicy &quotaPolicy);
204     int64_t GetTotalQuota(NetQuotaPolicy &quotaPolicy);
205     void SetNetworkEnableStatus(const NetQuotaPolicy &quotaPolicy, bool enable);
206     void NotifyQuotaWarning(int64_t totalQuota);
207     void NotifyQuotaLimit(int64_t totalQuota);
208     void NotifyQuotaLimitReminded(int64_t totalQuota);
209     void PublishQuotaEvent(const std::string &action, const std::string &describe, int64_t quota);
210     void ReadQuotaPolicies();
211     bool WriteQuotaPolicies();
212     const std::string GetMatchIfaces(const NetQuotaPolicy &quotaPolicy);
213 
214     bool IsValidNetType(int32_t netType);
215     bool IsValidPeriodDuration(const std::string &periodDuration);
216     bool IsQuotaPolicyExist(int32_t netType, const std::string &simId);
217     bool IsValidNetRemindType(uint32_t remindType);
218 
219 private:
220     std::vector<uint32_t> idleAllowedList_;
221     std::vector<NetQuotaPolicy> quotaPolicies_;
222     std::vector<std::string> meteredIfaces_;
223     sptr<NetsysControllerCallback> netsysCallback_ = nullptr;
224     sptr<INetConnCallback> netConnCallback_ = nullptr;
225 };
226 } // namespace NetManagerStandard
227 } // namespace OHOS
228 #endif // NET_POLICY_TRAFFIC_H
229