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)22std::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 case Action::REJECT: 32 return REJECT_TARGET; 33 } 34 return {}; 35 } 36 StringToAction(const std::string & action)37Action RuleUtils::StringToAction(const std::string &action) 38 { 39 if (action == ACCEPT_TARGET) { 40 return Action::ALLOW; 41 } else if (action == DROP_TARGET) { 42 return Action::DENY; 43 } else if (action == REJECT_TARGET) { 44 return Action::REJECT; 45 } else { 46 return Action::INVALID; 47 } 48 } 49 EnumToString(Protocol protocol)50std::string RuleUtils::EnumToString(Protocol protocol) 51 { 52 switch (protocol) { 53 case Protocol::INVALID: 54 return {}; 55 case Protocol::ALL: 56 return PROTOCOL_ALL; 57 case Protocol::TCP: 58 return PROTOCOL_TCP; 59 case Protocol::UDP: 60 return PROTOCOL_UDP; 61 case Protocol::ICMP: 62 return PROTOCOL_ICMP; 63 } 64 return {}; 65 } StringProtocol(const std::string & protocol)66Protocol RuleUtils::StringProtocol(const std::string &protocol) 67 { 68 if (protocol == PROTOCOL_ALL) { 69 return Protocol::ALL; 70 } else if (protocol == PROTOCOL_TCP) { 71 return Protocol::TCP; 72 } else if (protocol == PROTOCOL_UDP) { 73 return Protocol::UDP; 74 } else if (protocol == PROTOCOL_ICMP) { 75 return Protocol::ICMP; 76 } else { 77 return Protocol::INVALID; 78 } 79 } 80 } // namespace IPTABLES 81 } // namespace EDM 82 } // namespace OHOS