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 NETFIREWALL_COMMON_H
17 #define NETFIREWALL_COMMON_H
18 
19 #include <string>
20 #include <vector>
21 #include "parcel.h"
22 #include "netmgr_ext_log_wrapper.h"
23 #include "netfirewall_parcel.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
28 // Network firewall related specifications
29 // Maximum number of rules per user
30 constexpr int32_t FIREWALL_USER_MAX_RULE = 1000;
31 // Maximum number of pages per page during pagination queries
32 constexpr int32_t MAX_PAGE_SIZE = 50;
33 // Maximum number of rules for all users
34 constexpr int32_t FIREWALL_ALL_USER_MAX_RULE = 2000;
35 // Maximum number of domain for per users
36 constexpr int32_t FIREWALL_SINGLE_USER_MAX_DOMAIN = 1000;
37 // Maximum number of domain for all users
38 constexpr int32_t FIREWALL_ALL_USER_MAX_DOMAIN = 2000;
39 // Maximum number of fuzzy domain for all users
40 constexpr int32_t FIREWALL_ALL_USER_MAX_FUZZY_DOMAIN = 100;
41 // Maximum length of rule name
42 constexpr int32_t MAX_RULE_NAME_LEN = 128;
43 // Maximum length of rule description
44 constexpr int32_t MAX_RULE_DESCRIPTION_LEN = 256;
45 // Maximum number of IPs per rule
46 constexpr int32_t MAX_RULE_IP_COUNT = 10;
47 // Maximum number of ports per rule
48 constexpr int32_t MAX_RULE_PORT_COUNT = 10;
49 // Maximum number of domain per rule
50 constexpr int32_t MAX_RULE_DOMAIN_COUNT = 100;
51 // Maximum exact domain name length
52 constexpr int32_t MAX_EXACT_DOMAIN_NAME_LEN = 253;
53 // Maximum fuzzy domain name length
54 constexpr int32_t MAX_FUZZY_DOMAIN_NAME_LEN = 63;
55 // Intercept log aging: maximum save time
56 constexpr int32_t RECORD_MAX_SAVE_TIME = 8 * 24 * 60 * 60;
57 // Intercept log aging: Save maximum number of entries
58 constexpr int32_t RECORD_MAX_DATA_NUM = 1000;
59 
60 constexpr uint8_t IPV4_MASK_MAX = 32;
61 constexpr uint8_t IPV6_MASK_MAX = 128;
62 
63 const std::string NET_FIREWALL_PAGE = "page";
64 const std::string NET_FIREWALL_PAGE_SIZE = "pageSize";
65 const std::string NET_FIREWALL_ORDER_FIELD = "orderField";
66 const std::string NET_FIREWALL_ORDER_TYPE = "orderType";
67 const std::string NET_FIREWALL_TOTAL_PAGE = "totalPage";
68 const std::string NET_FIREWALL_PAGE_DATA = "data";
69 }
70 
71 // Sort by rule name or interception time
72 enum class NetFirewallOrderField {
73     ORDER_BY_RULE_NAME = 1,     // Sort by rule name
74     ORDER_BY_RECORD_TIME = 100, // Sort by interception record time
75 };
76 
77 // Paging query sorting enumeration
78 enum class NetFirewallOrderType {
79     ORDER_ASC = 1,    // Ascending order
80     ORDER_DESC = 100, // Descending order
81 };
82 
83 // Firewall policy
84 struct NetFirewallPolicy : public Parcelable {
85     bool isOpen;                  // Whether to open, required
86     FirewallRuleAction inAction;  // Inbound default allowed or blocked, mandatory
87     FirewallRuleAction outAction; // Outbound default allowed or blocked, mandatory
88 
89     virtual bool Marshalling(Parcel &parcel) const override;
90     static sptr<NetFirewallPolicy> Unmarshalling(Parcel &parcel);
91 };
92 
93 // Pagination query input
94 struct RequestParam : public Parcelable {
95     int32_t page;                     // Current page
96     int32_t pageSize;                 // Page size
97     NetFirewallOrderField orderField; // Sort Filed
98     NetFirewallOrderType orderType;   // sort order
99     std::string ToString() const;
100     virtual bool Marshalling(Parcel &parcel) const override;
101     static sptr<RequestParam> Unmarshalling(Parcel &parcel);
102 };
103 
104 // Paging query results
105 struct FirewallRulePage : public Parcelable {
106     int32_t page;                      // Current page
107     int32_t pageSize;                  // Page size
108     int32_t totalPage;                 // General page
109     std::vector<NetFirewallRule> data; // Page data
110     virtual bool Marshalling(Parcel &parcel) const override;
111     static sptr<FirewallRulePage> Unmarshalling(Parcel &parcel);
112 };
113 
114 // Intercept record pagination content
115 struct InterceptRecordPage : public Parcelable {
116     int32_t page;                      // Current page
117     int32_t pageSize;                  // Page size
118     int32_t totalPage;                 // General page
119     std::vector<InterceptRecord> data; // Page data
120     virtual bool Marshalling(Parcel &parcel) const override;
121     static sptr<InterceptRecordPage> Unmarshalling(Parcel &parcel);
122 };
123 
GetCurrentMilliseconds()124 inline uint64_t GetCurrentMilliseconds()
125 {
126     return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
127         .count();
128 }
129 } // namespace NetManagerStandard
130 } // namespace OHOS
131 
132 #endif // NETFIREWALL_COMMON_H