• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "iptables_utils.h"
17 
18 #include "edm_log.h"
19 
20 namespace OHOS {
21 namespace EDM {
22 namespace IPTABLES {
WriteAddFilterConfig(const AddFilter & filter,MessageParcel & data)23 void IptablesUtils::WriteAddFilterConfig(const AddFilter &filter, MessageParcel &data)
24 {
25     data.WriteUint32(filter.ruleNo);
26     data.WriteString(filter.srcAddr);
27     data.WriteString(filter.destAddr);
28     data.WriteString(filter.srcPort);
29     data.WriteString(filter.destPort);
30     data.WriteString(filter.uid);
31     data.WriteInt32(static_cast<int32_t>(filter.method));
32     data.WriteInt32(static_cast<int32_t>(filter.direction));
33     data.WriteInt32(static_cast<int32_t>(filter.action));
34     data.WriteInt32(static_cast<int32_t>(filter.protocol));
35 }
36 
ReadAddFilterConfig(AddFilter & filter,MessageParcel & data)37 void IptablesUtils::ReadAddFilterConfig(AddFilter &filter, MessageParcel &data)
38 {
39     filter.ruleNo = data.ReadUint32();
40     filter.srcAddr = data.ReadString();
41     filter.destAddr = data.ReadString();
42     filter.srcPort = data.ReadString();
43     filter.destPort = data.ReadString();
44     filter.uid = data.ReadString();
45     IptablesUtils::ProcessFirewallMethod(data.ReadInt32(), filter.method);
46     IptablesUtils::ProcessFirewallDirection(data.ReadInt32(), filter.direction);
47     IptablesUtils::ProcessFirewallAction(data.ReadInt32(), filter.action);
48     IptablesUtils::ProcessFirewallProtocol(data.ReadInt32(), filter.protocol);
49 }
50 
WriteRemoveFilterConfig(const RemoveFilter & filter,MessageParcel & data)51 void IptablesUtils::WriteRemoveFilterConfig(const RemoveFilter &filter, MessageParcel &data)
52 {
53     data.WriteString(filter.srcAddr);
54     data.WriteString(filter.destAddr);
55     data.WriteString(filter.srcPort);
56     data.WriteString(filter.destPort);
57     data.WriteString(filter.uid);
58     data.WriteInt32(static_cast<int32_t>(filter.direction));
59     data.WriteInt32(static_cast<int32_t>(filter.action));
60     data.WriteInt32(static_cast<int32_t>(filter.protocol));
61 }
62 
ReadRemoveFilterConfig(RemoveFilter & filter,MessageParcel & data)63 void IptablesUtils::ReadRemoveFilterConfig(RemoveFilter &filter, MessageParcel &data)
64 {
65     filter.srcAddr = data.ReadString();
66     filter.destAddr = data.ReadString();
67     filter.srcPort = data.ReadString();
68     filter.destPort = data.ReadString();
69     filter.uid = data.ReadString();
70     IptablesUtils::ProcessFirewallDirection(data.ReadInt32(), filter.direction);
71     IptablesUtils::ProcessFirewallAction(data.ReadInt32(), filter.action);
72     IptablesUtils::ProcessFirewallProtocol(data.ReadInt32(), filter.protocol);
73 }
74 
ProcessFirewallAction(int32_t type,Action & action)75 bool IptablesUtils::ProcessFirewallAction(int32_t type, Action &action)
76 {
77     if (type >= static_cast<int32_t>(Action::ALLOW) && type <= static_cast<int32_t>(Action::DENY)) {
78         action = Action(type);
79         return true;
80     }
81     return false;
82 }
83 
ProcessFirewallMethod(int32_t type,AddMethod & method)84 bool IptablesUtils::ProcessFirewallMethod(int32_t type, AddMethod &method)
85 {
86     if (type >= static_cast<int32_t>(AddMethod::APPEND) && type <= static_cast<int32_t>(AddMethod::INSERT)) {
87         method = AddMethod(type);
88         return true;
89     }
90     return false;
91 }
92 
ProcessFirewallDirection(int32_t type,Direction & direction)93 bool IptablesUtils::ProcessFirewallDirection(int32_t type, Direction &direction)
94 {
95     if (type >= static_cast<int32_t>(Direction::INPUT) && type <= static_cast<int32_t>(Direction::OUTPUT)) {
96         direction = Direction(type);
97         return true;
98     }
99     return false;
100 }
101 
ProcessFirewallProtocol(int32_t type,Protocol & protocol)102 void IptablesUtils::ProcessFirewallProtocol(int32_t type, Protocol &protocol)
103 {
104     if (type >= static_cast<int32_t>(Protocol::ALL) && type <= static_cast<int32_t>(Protocol::ICMP)) {
105         protocol = Protocol(type);
106     }
107 }
108 } // namespace IPTABLES
109 } // namespace EDM
110 } // namespace OHOS