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 "rule_utils.h"
17 
18 namespace OHOS {
19 namespace EDM {
20 namespace IPTABLES {
21 
EnumToString(Action action)22 std::string RuleUtils::EnumToString(Action action)
23 {
24     switch (action) {
25         case Action::INVALID:
26             return {};
27         case Action::ALLOW:
28             return ACCEPT_TARGET;
29         case Action::DENY:
30             return DROP_TARGET;
31     }
32     return {};
33 }
34 
StringToAction(std::string action)35 Action RuleUtils::StringToAction(std::string action)
36 {
37     if (action == ACCEPT_TARGET) {
38         return Action::ALLOW;
39     } else if (action == DROP_TARGET) {
40         return Action::DENY;
41     } else {
42         return Action::INVALID;
43     }
44 }
45 
EnumToString(Protocol protocol)46 std::string RuleUtils::EnumToString(Protocol protocol)
47 {
48     switch (protocol) {
49         case Protocol::INVALID:
50             return {};
51         case Protocol::ALL:
52             return PROTOCOL_ALL;
53         case Protocol::TCP:
54             return PROTOCOL_TCP;
55         case Protocol::UDP:
56             return PROTOCOL_UDP;
57         case Protocol::ICMP:
58             return PROTOCOL_ICMP;
59     }
60     return {};
61 }
StringProtocl(std::string protocol)62 Protocol RuleUtils::StringProtocl(std::string protocol)
63 {
64     if (protocol == PROTOCOL_ALL) {
65         return Protocol::ALL;
66     } else if (protocol == PROTOCOL_TCP) {
67         return Protocol::TCP;
68     } else if (protocol == PROTOCOL_UDP) {
69         return Protocol::UDP;
70     } else if (protocol == PROTOCOL_ICMP) {
71         return Protocol::ICMP;
72     } else {
73         return Protocol::INVALID;
74     }
75 }
76 } // namespace IPTABLES
77 } // namespace EDM
78 } // namespace OHOS