1 /*
2  * Copyright (c) 2023 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 "firewall_rule.h"
17 
18 namespace OHOS {
19 namespace EDM {
20 namespace IPTABLES {
FirewallRuleParcel(FirewallRule rule)21 FirewallRuleParcel::FirewallRuleParcel(FirewallRule rule) : rule_(std::move(rule)) {}
22 
Marshalling(MessageParcel & parcel) const23 bool FirewallRuleParcel::Marshalling(MessageParcel& parcel) const
24 {
25     parcel.WriteUint32(static_cast<int32_t>(std::get<FIREWALL_DICECTION_IND>(rule_)));
26     parcel.WriteUint32(static_cast<int32_t>(std::get<FIREWALL_ACTION_IND>(rule_)));
27     parcel.WriteUint32(static_cast<int32_t>(std::get<FIREWALL_PROT_IND>(rule_)));
28     parcel.WriteString(std::get<FIREWALL_SRCADDR_IND>(rule_));
29     parcel.WriteString(std::get<FIREWALL_DESTADDR_IND>(rule_));
30     parcel.WriteString(std::get<FIREWALL_SRCPORT_IND>(rule_));
31     parcel.WriteString(std::get<FIREWALL_DESTPORT_IND>(rule_));
32     parcel.WriteString(std::get<FIREWALL_APPUID_IND>(rule_));
33     return true;
34 }
35 
Unmarshalling(MessageParcel & parcel,FirewallRuleParcel & firewallRuleParcel)36 bool FirewallRuleParcel::Unmarshalling(MessageParcel& parcel, FirewallRuleParcel& firewallRuleParcel)
37 {
38     IPTABLES::Direction direction = IPTABLES::Direction::INVALID;
39     IptablesUtils::ProcessFirewallDirection(parcel.ReadInt32(), direction);
40     IPTABLES::Action action = IPTABLES::Action::INVALID;
41     IptablesUtils::ProcessFirewallAction(parcel.ReadInt32(), action);
42     IPTABLES::Protocol protocol = IPTABLES::Protocol::INVALID;
43     IptablesUtils::ProcessFirewallProtocol(parcel.ReadInt32(), protocol);
44     std::string srcAddr = parcel.ReadString();
45     std::string destAddr = parcel.ReadString();
46     std::string srcPort = parcel.ReadString();
47     std::string destPort = parcel.ReadString();
48     std::string appUid = parcel.ReadString();
49     firewallRuleParcel.rule_ = {direction, action, protocol, srcAddr, destAddr, srcPort, destPort, appUid};
50     return true;
51 }
52 
GetRule() const53 FirewallRule FirewallRuleParcel::GetRule() const
54 {
55     return rule_;
56 }
57 } // namespace IPTABLES
58 } // namespace EDM
59 } // namespace OHOS