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_DEFAULT_RULE_PARSER_H
17 #define NET_FIREWALL_DEFAULT_RULE_PARSER_H
18 
19 #include "netfirewall_common.h"
20 #include "cJSON.h"
21 
22 namespace OHOS {
23 namespace NetManagerStandard {
24 class NetFirewallDefaultRuleParser {
25 public:
26     NetFirewallDefaultRuleParser() = default;
27     ~NetFirewallDefaultRuleParser() = default;
28 
29     /**
30      * Get default netfirewall rules
31      *
32      * @param ruleList Netfirewall rule list
33      * @return If get object item DEFAULT_RULES is not null, and item is object, and json file is array format, there is
34      *     a return to true, otherwise it will be false
35      */
36     static bool GetDefaultRules(std::vector<sptr<NetFirewallRule>> &ruleList);
37 private:
38     /**
39      * Parsing netfirewall rules in JSON.
40      *
41      * @param rule Netfirewall rule
42      * @param mem cJson Object
43      */
44     static void ConvertFirewallRuleToConfig(sptr<NetFirewallRule> &rule, const cJSON * const mem);
45 
46     /**
47      * Parsing ip parameters in JSON
48      *
49      * @param rule Netfirewall ip parameter rule
50      * @param mem cJson Object
51      */
52     static void ConvertIpParamToConfig(NetFirewallIpParam &rule, const cJSON * const mem);
53 
54     /**
55      * Parsing port parameters in JSON
56      *
57      * @param rule Netfirewall port parameter rule
58      * @param mem cJson Object
59      */
60     static void ConvertPortParamToConfig(NetFirewallPortParam &rule, const cJSON * const mem);
61 
62     /**
63      * Parsing domain name parameters in JSON
64      *
65      * @param rule Netfirewall domain name parameter rule
66      * @param mem cJson Object
67      */
68     static void ConvertDomainParamToConfig(NetFirewallDomainParam &rule, const cJSON * const mem);
69 
70     /**
71      * Parsing dns parameters in JSON
72      *
73      * @param rule Netfirewall dns parameter rule
74      * @param mem cJson Object
75      */
76     static void ConvertDnsParamToConfig(NetFirewallDnsParam &rule, const cJSON * const mem);
77 
78     /**
79      * Read JSON file
80      *
81      * @param filePath Json file path
82      * @return json file content
83      */
84     static std::string ReadJsonFile(const std::string &filePath);
85 
86     /**
87      * Parse ip list
88      *
89      * @param ipParamlist Ip paramter list
90      * @param mem cJson Object
91      * @param jsonKey json key
92      */
93     static void ParseIpList(std::vector<NetFirewallIpParam> &ipParamlist, const cJSON * const mem,
94         const std::string jsonKey);
95 
96     /**
97      * Parse port list
98      *
99      * @param portParamlist Port paramter list
100      * @param mem cJson Object
101      * @param jsonKey Json key
102      */
103     static void ParsePortList(std::vector<NetFirewallPortParam> &portParamlist, const cJSON * const mem,
104         const std::string jsonKey);
105 
106     /**
107      * Parse domain list
108      *
109      * @param domain name paramter list
110      * @param mem cJson Object
111      * @param jsonKey Json key
112      */
113     static void ParseDomainList(std::vector<NetFirewallDomainParam> &domainParamlist, const cJSON * const mem,
114         const std::string jsonKey);
115 
116     /**
117      * Parse dns object
118      *
119      * @param dnsParam Dns paramter list
120      * @param mem cJson Object
121      * @param jsonKey Json key
122      */
123     static void ParseDnsObject(NetFirewallDnsParam &dnsParam, const cJSON * const mem, const std::string jsonKey);
124 
125     /**
126      * Parse list object
127      *
128      * @param rule Netfirwall rule
129      * @param mem cJson Object
130      */
131     static void ParseListObject(sptr<NetFirewallRule> &rule, const cJSON * const mem);
132 };
133 } // namespace NetManagerStandard
134 } // namespace OHOS
135 #endif // NET_FIREWALL_DEFAULT_RULE_PARSER_H
136