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 #ifndef NET_FIREWALL_PARAM_CHECK_H
16 #define NET_FIREWALL_PARAM_CHECK_H
17 
18 #include <napi/native_api.h>
19 
20 #include "netfirewall_common.h"
21 
22 namespace OHOS {
23 namespace NetManagerStandard {
24 class NetFirewallParamCheck {
25 public:
26     // Firewall state input verification
27     static int32_t CheckFirewallRulePolicy(napi_env env, napi_value object);
28 
29     // Add firewall rule entry verification
30     static int32_t CheckAddFirewallRule(napi_env env, napi_value object);
31 
32     // Update firewall rule entry verification
33     static int32_t CheckUpdateFirewallRule(napi_env env, napi_value object);
34 
35 private:
36     // Verify firewall rules
37     static int32_t CheckFirewallRule(napi_env env, napi_value object);
38 
39     // Verify DNS parameters in firewall rules
40     static int32_t CheckFirewallDns(napi_env env, napi_value object);
41 
42     // Verify actions in firewall rules
43     static int32_t CheckFirewallAction(napi_env env, napi_value object);
44 
45     // Verify the direction in firewall rules
46     static int32_t CheckFirewallDirection(napi_env env, napi_value object);
47 
48     // Verify IP list
49     static int32_t CheckIpList(napi_env env, napi_value object);
50 
51     static int32_t CheckPortList(napi_env env, napi_value object);
52 
53     static int32_t CheckDomainList(napi_env env, napi_value object);
54 
55     /**
56      * Verify IP address
57      *
58      * @param ipStr string of ip
59      * @return Returns 0 success. Otherwise fail
60      */
61     static int32_t CheckIpAddress(const std::string &ipStr);
62 
63     /**
64      * Verify IP address
65      *
66      * @param ipStr string of ip
67      * @param family IPV4 or IPV6
68      * @return Returns 0 success. Otherwise fail
69      */
70     static int32_t CheckIpAddress(const std::string &ipStr, const int32_t family);
71 
72     /**
73      * Verify IP segment address
74      *
75      * @param startIp Starting IP
76      * @param endIp Terminate IP
77      * @param family IPV4 or IPV6
78      * @return Success returns true, otherwise returns false
79      */
80     static bool CheckIpAddress(const std::string &startIp, const std::string &endIp, const int32_t family);
81 
82     /**
83      * Verify if the given IP is IPv4
84      *
85      * @param ipV4 string of ip
86      * @return Returns 0 success. Otherwise fail
87      */
88     static int32_t CheckIpV4(const std::string &ipV4);
89 
90     /**
91      * Verify if the given IP is IPv4
92      *
93      * @param ipV4 string of ip
94      * @return Returns 0 success. Otherwise fail
95      */
96     static int32_t CheckIpV6(const std::string &ipV6);
97 
98     // Single IP rule input verification
99     static int32_t CheckSingeIp(napi_env env, napi_value valAttr, int32_t family);
100 
101     static int32_t CheckMultipleIp(napi_env env, napi_value valAttr, int32_t family);
102 
103     // Verify the value of the object attribute for the rule type
104     static int32_t CheckRuleObjectParamValue(napi_env env, napi_value object, const std::string &propertyName);
105 
106     // Verify attributes of rule type object
107     static int32_t CheckRuleObjectParams(napi_env env, napi_value object, const NetFirewallRuleType &type);
108 
109     // Verify firewall rule name
110     static int32_t CheckRuleName(napi_env env, napi_value object);
111 
112     // Verify properties with rule type number
113     static int32_t CheckRuleNumberParam(napi_env env, napi_value object, const std::string &propertyName);
114 };
115 } // namespace NetManagerStandard
116 } // namespace OHOS
117 #endif // NET_FIREWALL_PARAM_CHECK_H
118