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 #include <sstream>
17 
18 #include "netmgr_ext_log_wrapper.h"
19 #include "refbase.h"
20 #include "netfirewall_common.h"
21 
22 namespace OHOS {
23 namespace NetManagerStandard {
24 // Firewall policy
Marshalling(Parcel &parcel) const25 bool NetFirewallPolicy::Marshalling(Parcel &parcel) const
26 {
27     if (!parcel.WriteBool(isOpen)) {
28         return false;
29     }
30     if (!parcel.WriteInt32(static_cast<int32_t>(inAction))) {
31         return false;
32     }
33     if (!parcel.WriteInt32(static_cast<int32_t>(outAction))) {
34         return false;
35     }
36     return true;
37 }
38 
Unmarshalling(Parcel &parcel)39 sptr<NetFirewallPolicy> NetFirewallPolicy::Unmarshalling(Parcel &parcel)
40 {
41     sptr<NetFirewallPolicy> ptr = new (std::nothrow) NetFirewallPolicy();
42     if (ptr == nullptr) {
43         NETMGR_EXT_LOG_E("NetFirewallPolicy ptr is null");
44         return nullptr;
45     }
46     if (!parcel.ReadBool(ptr->isOpen)) {
47         return nullptr;
48     }
49     int32_t inAction = 0;
50     if (!parcel.ReadInt32(inAction)) {
51         return nullptr;
52     }
53     int32_t outAction = 0;
54     if (!parcel.ReadInt32(outAction)) {
55         return nullptr;
56     }
57     ptr->inAction = static_cast<FirewallRuleAction>(inAction);
58     ptr->outAction = static_cast<FirewallRuleAction>(outAction);
59     return ptr;
60 }
61 
62 // Pagination query input
ToString() const63 std::string RequestParam::ToString() const
64 {
65     std::stringstream ss;
66     ss << "RequestParam:{" << NET_FIREWALL_PAGE << EQUAL << this->page << COMMA << NET_FIREWALL_PAGE_SIZE << EQUAL <<
67         this->pageSize << COMMA << NET_FIREWALL_ORDER_FIELD << EQUAL << static_cast<int32_t>(this->orderField) <<
68         COMMA << NET_FIREWALL_ORDER_TYPE << EQUAL << static_cast<int32_t>(this->orderType) << "}";
69     return ss.str();
70 }
71 
Marshalling(Parcel &parcel) const72 bool RequestParam::Marshalling(Parcel &parcel) const
73 {
74     if (!parcel.WriteInt32(page)) {
75         return false;
76     }
77     if (!parcel.WriteInt32(pageSize)) {
78         return false;
79     }
80     if (!parcel.WriteInt32(static_cast<int32_t>(orderField))) {
81         return false;
82     }
83     if (!parcel.WriteInt32(static_cast<int32_t>(orderType))) {
84         return false;
85     }
86     return true;
87 }
88 
Unmarshalling(Parcel &parcel)89 sptr<RequestParam> RequestParam::Unmarshalling(Parcel &parcel)
90 {
91     sptr<RequestParam> ptr = new (std::nothrow) RequestParam();
92     if (ptr == nullptr) {
93         NETMGR_EXT_LOG_E("RequestParam ptr is null");
94         return nullptr;
95     }
96     if (!parcel.ReadInt32(ptr->page)) {
97         return nullptr;
98     }
99     if (!parcel.ReadInt32(ptr->pageSize)) {
100         return nullptr;
101     }
102     int32_t orderField = 0;
103     if (!parcel.ReadInt32(orderField)) {
104         return nullptr;
105     }
106     ptr->orderField = static_cast<NetFirewallOrderField>(orderField);
107     int32_t orderType = 0;
108     if (!parcel.ReadInt32(orderType)) {
109         return nullptr;
110     }
111     ptr->orderType = static_cast<NetFirewallOrderType>(orderType);
112     return ptr;
113 }
114 
115 // The content of the rules page
Marshalling(Parcel &parcel) const116 bool FirewallRulePage::Marshalling(Parcel &parcel) const
117 {
118     if (!parcel.WriteInt32(page)) {
119         return false;
120     }
121     if (!parcel.WriteInt32(pageSize)) {
122         return false;
123     }
124     if (!parcel.WriteInt32(totalPage)) {
125         return false;
126     }
127     uint32_t size = data.size();
128     if (size > MAX_PAGE_SIZE) {
129         return false;
130     }
131     if (!parcel.WriteUint32(size)) {
132         return false;
133     }
134     for (auto value : data) {
135         if (!value.Marshalling(parcel)) {
136             NETMGR_EXT_LOG_E("FirewallRulePage write Marshalling to parcel failed");
137             return false;
138         }
139     }
140     return true;
141 }
142 
Unmarshalling(Parcel &parcel)143 sptr<FirewallRulePage> FirewallRulePage::Unmarshalling(Parcel &parcel)
144 {
145     sptr<FirewallRulePage> ptr = new (std::nothrow) FirewallRulePage();
146     if (ptr == nullptr) {
147         return nullptr;
148     }
149     if (!parcel.ReadInt32(ptr->page)) {
150         return nullptr;
151     }
152     if (!parcel.ReadInt32(ptr->pageSize)) {
153         return nullptr;
154     }
155     if (!parcel.ReadInt32(ptr->totalPage)) {
156         return nullptr;
157     }
158     uint32_t size = 0;
159     if (!parcel.ReadUint32(size)) {
160         return nullptr;
161     }
162     if (size > MAX_PAGE_SIZE) {
163         NETMGR_EXT_LOG_E("InterceptRecordPage read list size is too large");
164         return nullptr;
165     }
166     for (uint32_t i = 0; i < size; i++) {
167         auto value = NetFirewallRule::Unmarshalling(parcel);
168         if (value == nullptr) {
169             NETMGR_EXT_LOG_E("FirewallRulePage read Unmarshalling to parcel failed");
170             return nullptr;
171         }
172         ptr->data.push_back(*value);
173     }
174     return ptr;
175 }
176 
177 // Intercept record pagination content
Marshalling(Parcel &parcel) const178 bool InterceptRecordPage::Marshalling(Parcel &parcel) const
179 {
180     if (!parcel.WriteInt32(page)) {
181         return false;
182     }
183     if (!parcel.WriteInt32(pageSize)) {
184         return false;
185     }
186     if (!parcel.WriteInt32(totalPage)) {
187         return false;
188     }
189     uint32_t size = data.size();
190     if (size > MAX_PAGE_SIZE) {
191         return false;
192     }
193     if (!parcel.WriteUint32(size)) {
194         return false;
195     }
196     for (auto value : data) {
197         if (!value.Marshalling(parcel)) {
198             NETMGR_EXT_LOG_E("InterceptRecordPage write Marshalling to parcel failed");
199             return false;
200         }
201     }
202     return true;
203 }
204 
Unmarshalling(Parcel &parcel)205 sptr<InterceptRecordPage> InterceptRecordPage::Unmarshalling(Parcel &parcel)
206 {
207     sptr<InterceptRecordPage> ptr = new (std::nothrow) InterceptRecordPage();
208     if (ptr == nullptr) {
209         return nullptr;
210     }
211     if (!parcel.ReadInt32(ptr->page)) {
212         return nullptr;
213     }
214     if (!parcel.ReadInt32(ptr->pageSize)) {
215         return nullptr;
216     }
217     if (!parcel.ReadInt32(ptr->totalPage)) {
218         return nullptr;
219     }
220     uint32_t size = 0;
221     if (!parcel.ReadUint32(size)) {
222         return nullptr;
223     }
224     if (size > MAX_PAGE_SIZE) {
225         NETMGR_EXT_LOG_E("InterceptRecordPage read list size is too large");
226         return nullptr;
227     }
228     for (uint32_t i = 0; i < size; i++) {
229         auto value = InterceptRecord::Unmarshalling(parcel);
230         if (value == nullptr) {
231             NETMGR_EXT_LOG_E("InterceptRecordPage read Unmarshalling to parcel failed");
232             return nullptr;
233         }
234         ptr->data.push_back(*value);
235     }
236     return ptr;
237 }
238 } // namespace NetManagerStandard
239 } // namespace OHOS