1 /* 2 * Copyright (c) 2022 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 NETMANAGER_BASE_IPTABLES_WRAPPER_H 17 #define NETMANAGER_BASE_IPTABLES_WRAPPER_H 18 19 #include <condition_variable> 20 #include <cstring> 21 #include <iostream> 22 #include <mutex> 23 #include <queue> 24 #include <thread> 25 26 #include "event_runner.h" 27 #include "event_handler.h" 28 #include "singleton.h" 29 #include "ffrt.h" 30 31 namespace OHOS { 32 namespace nmd { 33 using EventRunner = OHOS::AppExecFwk::EventRunner; 34 using EventHandler = OHOS::AppExecFwk::EventHandler; 35 enum IpType { 36 IPTYPE_IPV4 = 1, 37 IPTYPE_IPV6 = 2, 38 IPTYPE_IPV4V6 = 3, 39 }; 40 class IptablesWrapper : public std::enable_shared_from_this<IptablesWrapper> { 41 public: 42 IptablesWrapper(); 43 ~IptablesWrapper(); GetInstance()44 static std::shared_ptr<IptablesWrapper> &GetInstance() 45 { 46 static std::shared_ptr<IptablesWrapper> instance = std::make_shared<IptablesWrapper>(); 47 return instance; 48 } 49 50 /** 51 * @param ipType ipv4 or ipv6 52 * @param command iptables command 53 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 54 */ 55 int32_t RunCommand(const IpType &ipType, const std::string &command); 56 57 /** 58 * @param ipType ipv4 or ipv6 59 * @param command iptables command 60 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 61 */ 62 std::string RunCommandForTraffic(const IpType &ipType, const std::string &command); 63 64 /** 65 * @brief run iptables exec for result. 66 * 67 * @param ipType ipv4 or ipv6. 68 * @param command iptables command. 69 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 70 */ 71 std::string RunCommandForRes(const IpType &ipType, const std::string &command); 72 73 /** 74 * @brief run mutiple iptables commands. 75 * 76 * @param ipType ipv4 or ipv6. 77 * @param commands iptables commands. 78 * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed 79 */ 80 int32_t RunMutipleCommands(const IpType &ipType, const std::vector<std::string> &commands); 81 82 private: 83 void ExecuteCommand(const std::string &command); 84 void ExecuteCommandForRes(const std::string &command); 85 void ExecuteCommandForTraffic(const std::string &command); 86 private: 87 std::mutex iptablesMutex_; 88 std::condition_variable conditionVarLock_; 89 bool isRunningFlag_ = false; 90 bool isIptablesSystemAccess_ = false; 91 bool isIp6tablesSystemAccess_ = false; 92 std::string result_; 93 std::string resultTraffic_; 94 std::thread iptablesWrapperThread_; 95 std::queue<std::string> commandsQueue_; 96 std::shared_ptr<ffrt::queue> iptablesWrapperFfrtQueue_ = nullptr; 97 }; 98 } // namespace nmd 99 } // namespace OHOS 100 #endif /* NETMANAGER_BASE_IPTABLES_WRAPPER_H */ 101