• 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 "datetime_ex.h"
21 #include "net_manager_constants.h"
22 #include "netmanager_base_common_utils.h"
23 #include "netnative_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace nmd {
27 using namespace NetManagerStandard;
28 namespace {
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     handlerRunner_ = AppExecFwk::EventRunner::Create("IptablesWrapper");
42     if (handlerRunner_ == nullptr) {
43         return;
44     }
45     handler_ = std::make_shared<AppExecFwk::EventHandler>(handlerRunner_);
46 }
47 
~IptablesWrapper()48 IptablesWrapper::~IptablesWrapper()
49 {
50     isRunningFlag_ = false;
51     if (handlerRunner_) {
52         handlerRunner_->Stop();
53         handlerRunner_.reset();
54     }
55     if (handler_) {
56         handler_.reset();
57         handler_ = nullptr;
58     }
59 }
60 
ExecuteCommand(const std::string & command)61 void IptablesWrapper::ExecuteCommand(const std::string &command)
62 {
63     if (CommonUtils::ForkExec(command) == NETMANAGER_ERROR) {
64         NETNATIVE_LOGE("run exec faild, command=%{public}s", command.c_str());
65     }
66 }
67 
ExecuteCommandForRes(const std::string & command)68 void IptablesWrapper::ExecuteCommandForRes(const std::string &command)
69 {
70     if (CommonUtils::ForkExec(command, &result_) == NETMANAGER_ERROR) {
71         NETNATIVE_LOGE("run exec faild, command=%{public}s", command.c_str());
72     }
73 }
74 
RunCommand(const IpType & ipType,const std::string & command)75 int32_t IptablesWrapper::RunCommand(const IpType &ipType, const std::string &command)
76 {
77     NETNATIVE_LOG_D("IptablesWrapper::RunCommand, ipType:%{public}d, command:%{public}s", ipType, command.c_str());
78     if (handler_ == nullptr) {
79         NETNATIVE_LOGE("RunCommand failed! handler is nullptr");
80         return NETMANAGER_ERROR;
81     }
82     std::string cmd = std::string(IPATBLES_CMD_PATH) + " " + command;
83     std::function<void()> executeCommand = std::bind(&IptablesWrapper::ExecuteCommand, shared_from_this(), cmd);
84     handler_->PostTask(executeCommand);
85     return NetManagerStandard::NETMANAGER_SUCCESS;
86 }
87 
RunCommandForRes(const IpType & ipType,const std::string & command)88 std::string IptablesWrapper::RunCommandForRes(const IpType &ipType, const std::string &command)
89 {
90     NETNATIVE_LOG_D("IptablesWrapper::RunCommandForRes, ipType:%{public}d, command:%{public}s", ipType,
91                     command.c_str());
92     if (handler_ == nullptr) {
93         NETNATIVE_LOGE("RunCommandForRes failed! handler is nullptr");
94         return result_;
95     }
96     std::string cmd = std::string(IPATBLES_CMD_PATH) + " " + command;
97     std::function<void()> executeCommandForRes =
98         std::bind(&IptablesWrapper::ExecuteCommandForRes, shared_from_this(), cmd);
99 
100     int64_t start = GetTickCount();
101     handler_->PostSyncTask(executeCommandForRes);
102     NETNATIVE_LOGI("PostSyncTask cost:%{public}lld ms", static_cast<long long>(GetTickCount() - start));
103 
104     return result_;
105 }
106 } // namespace nmd
107 } // namespace OHOS
108