1 /*
2  * Copyright (c) 2024 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_FIREWALL_RULE_NATIVE_HELPER_H
17 #define NET_FIREWALL_RULE_NATIVE_HELPER_H
18 
19 #include <string>
20 #include <mutex>
21 
22 #include "netfirewall_common.h"
23 
24 namespace OHOS {
25 namespace NetManagerStandard {
26 class NetFirewallRuleNativeHelper {
27 public:
28     static NetFirewallRuleNativeHelper &GetInstance();
29     NetFirewallRuleNativeHelper();
30     ~NetFirewallRuleNativeHelper();
31 
32     /**
33      * Set firewall rules to bpf maps
34      *
35      * @param ruleList list of NetFirewallIpRule
36      * @return 0 if success or -1 if an error occurred
37      */
38     int32_t SetFirewallIpRules(const std::vector<sptr<NetFirewallIpRule>> &ruleList);
39 
40     /**
41      * Set firewall default action
42      *
43      * @param inDefault  Default action of NetFirewallRuleDirection:RULE_IN
44      * @param outDefault Default action of NetFirewallRuleDirection:RULE_OUT
45      * @return 0 if success or -1 if an error occurred
46      */
47     int32_t SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault);
48 
49     /* *
50      * Clear firewall rules by type
51      *
52      * @param type ip, dns, domain, all
53      * @return 0 if success or -1 if an error occurred
54      */
55     int32_t ClearFirewallRules(NetFirewallRuleType type);
56 
57     /**
58      * Set the Firewall DNS rules
59      *
60      * @param ruleList firewall rules
61      * @return 0 if success or-1 if an error occurred
62      */
63     int32_t SetFirewallDnsRules(const std::vector<sptr<NetFirewallDnsRule>> &ruleList);
64 
65     /**
66      * Set the Firewall domain rules
67      *
68      * @param  ruleList firewall rules
69      * @return 0 if success or-1 if an error occurred
70      */
71     int32_t SetFirewallDomainRules(const std::vector<sptr<NetFirewallDomainRule>> &ruleList);
72 
73     /**
74      * Set the Firewall current user id
75      *
76      * @param  userId firewall user id
77      * @return 0 if success or-1 if an error occurred
78      */
79     int32_t SetCurrentUserId(int32_t userId);
80 
81 private:
82     int32_t SetFirewallRulesInner(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList,
83                                   uint32_t pageSize);
84     std::mutex callNetSysController_;
85 };
86 } // namespace NetManagerStandard
87 } // namespace OHOS
88 #endif /* NET_FIREWALL_RULE_NATIVE_HELPER_H */
89