• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void ReachedLimit(const std::string &iface);
93     void UpdateNetPolicy();
94     void GetDumpMessage(std::string &message);
95 
96 private:
97     class NetsysControllerCallbackImpl : public NetsysControllerCallback {
98     public:
NetsysControllerCallbackImpl(std::shared_ptr<NetPolicyTraffic> traffic)99         NetsysControllerCallbackImpl(std::shared_ptr<NetPolicyTraffic> traffic)
100         {
101             traffic_ = traffic;
102         }
OnInterfaceAddressUpdated(const std::string &,const std::string &,int32_t,int32_t)103         int32_t OnInterfaceAddressUpdated(const std::string &, const std::string &, int32_t, int32_t)
104         {
105             return 0;
106         }
OnInterfaceAddressRemoved(const std::string &,const std::string &,int32_t,int32_t)107         int32_t OnInterfaceAddressRemoved(const std::string &, const std::string &, int32_t, int32_t)
108         {
109             return 0;
110         }
OnInterfaceAdded(const std::string &)111         int32_t OnInterfaceAdded(const std::string &)
112         {
113             return 0;
114         }
OnInterfaceRemoved(const std::string &)115         int32_t OnInterfaceRemoved(const std::string &)
116         {
117             return 0;
118         }
OnInterfaceChanged(const std::string &,bool)119         int32_t OnInterfaceChanged(const std::string &, bool)
120         {
121             return 0;
122         }
OnInterfaceLinkStateChanged(const std::string &,bool)123         int32_t OnInterfaceLinkStateChanged(const std::string &, bool)
124         {
125             return 0;
126         }
OnRouteChanged(bool,const std::string &,const std::string &,const std::string &)127         int32_t OnRouteChanged(bool, const std::string &, const std::string &, const std::string &)
128         {
129             return 0;
130         }
OnDhcpSuccess(NetsysControllerCallback::DhcpResult & dhcpResult)131         int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult)
132         {
133             return 0;
134         }
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)135         int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface)
136         {
137             traffic_->ReachedLimit(iface);
138             return 0;
139         }
140 
141     private:
142         std::shared_ptr<NetPolicyTraffic> traffic_ = nullptr;
143     };
144 
145     class ConnCallBack : public IRemoteStub<INetConnCallback> {
146     public:
ConnCallBack(std::shared_ptr<NetPolicyTraffic> connCallBack)147         ConnCallBack(std::shared_ptr<NetPolicyTraffic> connCallBack)
148         {
149             connCallBack_ = connCallBack;
150         }
NetAvailable(sptr<NetHandle> & netHandle)151         int32_t NetAvailable(sptr<NetHandle> &netHandle)
152         {
153             if (connCallBack_ != nullptr) {
154                 connCallBack_->UpdateNetPolicy();
155                 return 0;
156             }
157             return -1;
158         }
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)159         int32_t NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
160         {
161             return 0;
162         }
163 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)164         int32_t NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
165         {
166             return 0;
167         }
168 
NetLost(sptr<NetHandle> & netHandle)169         int32_t NetLost(sptr<NetHandle> &netHandle)
170         {
171             return 0;
172         }
173 
NetUnavailable()174         int32_t NetUnavailable()
175         {
176             return 0;
177         }
178 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)179         int32_t NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
180         {
181             return 0;
182         }
183 
184     private:
185         std::shared_ptr<NetPolicyTraffic> connCallBack_ = nullptr;
186     };
187 
188 private:
189     int32_t UpdateQuotaPoliciesInner();
190     int64_t GetQuotaRemain(NetQuotaPolicy &quotaPolicy);
191     void UpdateQuotaNotify();
192     void UpdateMeteredIfaces(std::vector<std::string> &newMeteredIfaces);
193     void UpdateNetEnableStatus(const NetQuotaPolicy &quotaPolicy);
194     void FormalizeQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies);
195     const std::vector<std::string> UpdateMeteredIfacesQuota();
196 
197     bool IsValidQuotaPolicy(const NetQuotaPolicy &quotaPolicy);
198     int64_t GetTotalQuota(NetQuotaPolicy &quotaPolicy);
199     void SetNetworkEnableStatus(const NetQuotaPolicy &quotaPolicy, bool enable);
200     void NotifyQuotaWarning(int64_t totalQuota);
201     void NotifyQuotaLimit(int64_t totalQuota);
202     void NotifyQuotaLimitReminded(int64_t totalQuota);
203     void PublishQuotaEvent(const std::string &action, const std::string &describe, int64_t quota);
204     void ReadQuotaPolicies();
205     bool WriteQuotaPolicies();
206     const std::string GetMatchIfaces(const NetQuotaPolicy &quotaPolicy);
207 
208     bool IsValidNetType(int32_t netType);
209     bool IsValidPeriodDuration(const std::string &periodDuration);
210     bool IsQuotaPolicyExist(int32_t netType, const std::string &simId);
211     bool IsValidNetRemindType(uint32_t remindType);
212 
213 private:
214     std::vector<uint32_t> idleAllowedList_;
215     std::vector<NetQuotaPolicy> quotaPolicies_;
216     std::vector<std::string> meteredIfaces_;
217     sptr<NetsysControllerCallback> netsysCallback_ = nullptr;
218     sptr<INetConnCallback> netConnCallback_ = nullptr;
219 };
220 } // namespace NetManagerStandard
221 } // namespace OHOS
222 #endif // NET_POLICY_TRAFFIC_H
223