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 (!parcel.WriteUint32(size)) {
129         return false;
130     }
131     for (auto value : data) {
132         if (!value.Marshalling(parcel)) {
133             NETMGR_EXT_LOG_E("FirewallRulePage write Marshalling to parcel failed");
134             return false;
135         }
136     }
137     return true;
138 }
139 
Unmarshalling(Parcel & parcel)140 sptr<FirewallRulePage> FirewallRulePage::Unmarshalling(Parcel &parcel)
141 {
142     sptr<FirewallRulePage> ptr = new (std::nothrow) FirewallRulePage();
143     if (ptr == nullptr) {
144         return nullptr;
145     }
146     if (!parcel.ReadInt32(ptr->page)) {
147         return nullptr;
148     }
149     if (!parcel.ReadInt32(ptr->pageSize)) {
150         return nullptr;
151     }
152     if (!parcel.ReadInt32(ptr->totalPage)) {
153         return nullptr;
154     }
155     uint32_t size = 0;
156     if (!parcel.ReadUint32(size)) {
157         return nullptr;
158     }
159     for (uint32_t i = 0; i < size; i++) {
160         auto value = NetFirewallRule::Unmarshalling(parcel);
161         if (value == nullptr) {
162             NETMGR_EXT_LOG_E("FirewallRulePage read Unmarshalling to parcel failed");
163             return nullptr;
164         }
165         ptr->data.push_back(*value);
166     }
167     return ptr;
168 }
169 
170 // Intercept record pagination content
Marshalling(Parcel & parcel) const171 bool InterceptRecordPage::Marshalling(Parcel &parcel) const
172 {
173     if (!parcel.WriteInt32(page)) {
174         return false;
175     }
176     if (!parcel.WriteInt32(pageSize)) {
177         return false;
178     }
179     if (!parcel.WriteInt32(totalPage)) {
180         return false;
181     }
182     uint32_t size = data.size();
183     if (!parcel.WriteUint32(size)) {
184         return false;
185     }
186     for (auto value : data) {
187         if (!value.Marshalling(parcel)) {
188             NETMGR_EXT_LOG_E("InterceptRecordPage write Marshalling to parcel failed");
189             return false;
190         }
191     }
192     return true;
193 }
194 
Unmarshalling(Parcel & parcel)195 sptr<InterceptRecordPage> InterceptRecordPage::Unmarshalling(Parcel &parcel)
196 {
197     sptr<InterceptRecordPage> ptr = new (std::nothrow) InterceptRecordPage();
198     if (ptr == nullptr) {
199         return nullptr;
200     }
201     if (!parcel.ReadInt32(ptr->page)) {
202         return nullptr;
203     }
204     if (!parcel.ReadInt32(ptr->pageSize)) {
205         return nullptr;
206     }
207     if (!parcel.ReadInt32(ptr->totalPage)) {
208         return nullptr;
209     }
210     uint32_t size = 0;
211     if (!parcel.ReadUint32(size)) {
212         return nullptr;
213     }
214     for (uint32_t i = 0; i < size; i++) {
215         auto value = InterceptRecord::Unmarshalling(parcel);
216         if (value == nullptr) {
217             NETMGR_EXT_LOG_E("InterceptRecordPage read Unmarshalling to parcel failed");
218             return nullptr;
219         }
220         ptr->data.push_back(*value);
221     }
222     return ptr;
223 }
224 } // namespace NetManagerStandard
225 } // namespace OHOS