• 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 #ifndef INTERFACES_INNER_API_NETWORK_MANAGER_INCLUDE_IPTABLES_UTILS_H
17 #define INTERFACES_INNER_API_NETWORK_MANAGER_INCLUDE_IPTABLES_UTILS_H
18 
19 #include <string>
20 
21 #include "message_parcel.h"
22 
23 namespace OHOS {
24 namespace EDM {
25 namespace IPTABLES {
26 enum class AddMethod { INVALID = -1, APPEND, INSERT };
27 
28 enum class Direction { INVALID = -1, INPUT, OUTPUT };
29 
30 enum class Action { INVALID = -1, ALLOW, DENY };
31 
32 enum class Protocol { INVALID = -1, ALL, TCP, UDP, ICMP };
33 
34 struct Firewall {
35     std::string srcAddr;
36     std::string destAddr;
37     std::string srcPort;
38     std::string destPort;
39     std::string uid;
40     Direction direction;
41     Action action;
42     Protocol protocol;
43 };
44 
45 struct AddFilter : Firewall {
46     uint32_t ruleNo = 0;
47     AddMethod method;
AddFilterAddFilter48     AddFilter()
49     {
50         protocol = Protocol::INVALID;
51         action = Action::INVALID;
52         direction = Direction::INVALID;
53         method = AddMethod::INVALID;
54     }
55 };
56 
57 struct RemoveFilter : Firewall {
RemoveFilterRemoveFilter58     RemoveFilter()
59     {
60         protocol = Protocol::INVALID;
61         action = Action::INVALID;
62         direction = Direction::INVALID;
63     }
64 };
65 
66 class IptablesUtils {
67 public:
68     static void WriteAddFilterConfig(const AddFilter &fiter, MessageParcel &data);
69     static void WriteRemoveFilterConfig(const RemoveFilter &fiter, MessageParcel &data);
70     static void ReadAddFilterConfig(AddFilter &fiter, MessageParcel &data);
71     static void ReadRemoveFilterConfig(RemoveFilter &fiter, MessageParcel &data);
72     static bool ProcessFirewallAction(int32_t type, Action &action);
73     static bool ProcessFirewallMethod(int32_t type, AddMethod &method);
74     static bool ProcessFirewallDirection(int32_t type, Direction &direction);
75     static void ProcessFirewallProtocol(int32_t type, Protocol &protocol);
76 };
77 } // namespace IPTABLES
78 } // namespace EDM
79 } // namespace OHOS
80 
81 #endif // INTERFACES_INNER_API_NETWORK_MANAGER_INCLUDE_IPTABLES_UTILS_H
82