• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "iptables_wrapper.h"
17 
18 #include <unistd.h>
19 
20 #include "net_manager_constants.h"
21 #include "netmanager_base_common_utils.h"
22 #include "netnative_log_wrapper.h"
23 
24 namespace OHOS {
25 namespace nmd {
26 using namespace NetManagerStandard;
27 namespace {
28 constexpr int32_t IPTABLES_WAIT_FOR_TIME_MS = 1000;
29 constexpr const char *IPATBLES_CMD_PATH = "/system/bin/iptables";
30 } // namespace
31 
IptablesWrapper()32 IptablesWrapper::IptablesWrapper()
33 {
34     isRunningFlag_ = true;
35     if (access(IPATBLES_CMD_PATH, F_OK) == 0) {
36         isIptablesSystemAccess_ = true;
37     } else {
38         isIptablesSystemAccess_ = false;
39     }
40 
41     auto eventLoop = AppExecFwk::EventRunner::Create("IptablesWrapper");
42     if (eventLoop == nullptr) {
43         return;
44     }
45     handler_ = std::make_shared<AppExecFwk::EventHandler>(eventLoop);
46 }
47 
~IptablesWrapper()48 IptablesWrapper::~IptablesWrapper()
49 {
50     isRunningFlag_ = false;
51 }
52 
ExecuteCommand(const std::string & command)53 void IptablesWrapper::ExecuteCommand(const std::string &command)
54 {
55     if (CommonUtils::ForkExec(command) == NETMANAGER_ERROR) {
56         NETNATIVE_LOGE("run exec faild, command=%{public}s", command.c_str());
57     }
58 }
59 
ExecuteCommandForRes(const std::string & command)60 void IptablesWrapper::ExecuteCommandForRes(const std::string &command)
61 {
62     std::unique_lock<std::mutex> lock(iptablesMutex_);
63     if (CommonUtils::ForkExec(command, &result_) == NETMANAGER_ERROR) {
64         NETNATIVE_LOGE("run exec faild, command=%{public}s", command.c_str());
65     }
66     conditionVarLock_.notify_one();
67 }
68 
RunCommand(const IpType & ipType,const std::string & command)69 int32_t IptablesWrapper::RunCommand(const IpType &ipType, const std::string &command)
70 {
71     NETNATIVE_LOG_D("IptablesWrapper::RunCommand, ipType:%{public}d, command:%{public}s", ipType, command.c_str());
72     if (handler_ == nullptr) {
73         NETNATIVE_LOGE("RunCommand failed! handler is nullptr");
74         return NETMANAGER_ERROR;
75     }
76     std::string cmd = std::string(IPATBLES_CMD_PATH) + " " + command;
77     std::function<void()> executeCommand = std::bind(&IptablesWrapper::ExecuteCommand, this, cmd);
78     handler_->PostTask(executeCommand);
79     return NetManagerStandard::NETMANAGER_SUCCESS;
80 }
81 
RunCommandForRes(const IpType & ipType,const std::string & command)82 std::string IptablesWrapper::RunCommandForRes(const IpType &ipType, const std::string &command)
83 {
84     NETNATIVE_LOG_D("IptablesWrapper::RunCommandForRes, ipType:%{public}d, command:%{public}s", ipType,
85                     command.c_str());
86     if (handler_ == nullptr) {
87         NETNATIVE_LOGE("RunCommandForRes failed! handler is nullptr");
88         return result_;
89     }
90     std::string cmd = std::string(IPATBLES_CMD_PATH) + " " + command;
91     std::unique_lock<std::mutex> lock(iptablesMutex_);
92     std::function<void()> executeCommandForRes = std::bind(&IptablesWrapper::ExecuteCommandForRes, this, cmd);
93     handler_->PostTask(executeCommandForRes);
94     auto status = conditionVarLock_.wait_for(lock, std::chrono::milliseconds(IPTABLES_WAIT_FOR_TIME_MS));
95     if (status == std::cv_status::timeout) {
96         NETNATIVE_LOGI("ExecuteCommandForRes timeout!");
97     }
98     return result_;
99 }
100 } // namespace nmd
101 } // namespace OHOS
102