1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#ifndef NETMANAGER_BASE_IPTABLES_WRAPPER_H
17b1b8bc3fSopenharmony_ci#define NETMANAGER_BASE_IPTABLES_WRAPPER_H
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_ci#include <condition_variable>
20b1b8bc3fSopenharmony_ci#include <cstring>
21b1b8bc3fSopenharmony_ci#include <iostream>
22b1b8bc3fSopenharmony_ci#include <mutex>
23b1b8bc3fSopenharmony_ci#include <queue>
24b1b8bc3fSopenharmony_ci#include <thread>
25b1b8bc3fSopenharmony_ci
26b1b8bc3fSopenharmony_ci#include "event_runner.h"
27b1b8bc3fSopenharmony_ci#include "event_handler.h"
28b1b8bc3fSopenharmony_ci#include "singleton.h"
29b1b8bc3fSopenharmony_ci#include "ffrt.h"
30b1b8bc3fSopenharmony_ci
31b1b8bc3fSopenharmony_cinamespace OHOS {
32b1b8bc3fSopenharmony_cinamespace nmd {
33b1b8bc3fSopenharmony_ciusing EventRunner = OHOS::AppExecFwk::EventRunner;
34b1b8bc3fSopenharmony_ciusing EventHandler = OHOS::AppExecFwk::EventHandler;
35b1b8bc3fSopenharmony_cienum IpType {
36b1b8bc3fSopenharmony_ci    IPTYPE_IPV4 = 1,
37b1b8bc3fSopenharmony_ci    IPTYPE_IPV6 = 2,
38b1b8bc3fSopenharmony_ci    IPTYPE_IPV4V6 = 3,
39b1b8bc3fSopenharmony_ci};
40b1b8bc3fSopenharmony_ciclass IptablesWrapper : public std::enable_shared_from_this<IptablesWrapper> {
41b1b8bc3fSopenharmony_cipublic:
42b1b8bc3fSopenharmony_ci    IptablesWrapper();
43b1b8bc3fSopenharmony_ci    ~IptablesWrapper();
44b1b8bc3fSopenharmony_ci    static std::shared_ptr<IptablesWrapper> &GetInstance()
45b1b8bc3fSopenharmony_ci    {
46b1b8bc3fSopenharmony_ci        static std::shared_ptr<IptablesWrapper> instance = std::make_shared<IptablesWrapper>();
47b1b8bc3fSopenharmony_ci        return instance;
48b1b8bc3fSopenharmony_ci    }
49b1b8bc3fSopenharmony_ci
50b1b8bc3fSopenharmony_ci    /**
51b1b8bc3fSopenharmony_ci     * @param ipType ipv4 or ipv6
52b1b8bc3fSopenharmony_ci     * @param command iptables command
53b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
54b1b8bc3fSopenharmony_ci     */
55b1b8bc3fSopenharmony_ci    int32_t RunCommand(const IpType &ipType, const std::string &command);
56b1b8bc3fSopenharmony_ci
57b1b8bc3fSopenharmony_ci    /**
58b1b8bc3fSopenharmony_ci     * @brief run iptables exec for result.
59b1b8bc3fSopenharmony_ci     *
60b1b8bc3fSopenharmony_ci     * @param ipType ipv4 or ipv6.
61b1b8bc3fSopenharmony_ci     * @param command iptables command.
62b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
63b1b8bc3fSopenharmony_ci     */
64b1b8bc3fSopenharmony_ci    std::string RunCommandForRes(const IpType &ipType, const std::string &command);
65b1b8bc3fSopenharmony_ci
66b1b8bc3fSopenharmony_ci    /**
67b1b8bc3fSopenharmony_ci     * @brief run mutiple iptables commands.
68b1b8bc3fSopenharmony_ci     *
69b1b8bc3fSopenharmony_ci     * @param ipType ipv4 or ipv6.
70b1b8bc3fSopenharmony_ci     * @param commands iptables commands.
71b1b8bc3fSopenharmony_ci     * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
72b1b8bc3fSopenharmony_ci     */
73b1b8bc3fSopenharmony_ci    int32_t RunMutipleCommands(const IpType &ipType, const std::vector<std::string> &commands);
74b1b8bc3fSopenharmony_ci
75b1b8bc3fSopenharmony_ciprivate:
76b1b8bc3fSopenharmony_ci    void ExecuteCommand(const std::string &command);
77b1b8bc3fSopenharmony_ci    void ExecuteCommandForRes(const std::string &command);
78b1b8bc3fSopenharmony_ci    static std::string AnonymizeIptablesCommand(const std::string &command)
79b1b8bc3fSopenharmony_ci    {
80b1b8bc3fSopenharmony_ci        std::string temp{command};
81b1b8bc3fSopenharmony_ci        std::transform(temp.cbegin(), temp.cend(), temp.begin(), [](char c) {
82b1b8bc3fSopenharmony_ci            return std::isdigit(c) ? 'x' : c;
83b1b8bc3fSopenharmony_ci        });
84b1b8bc3fSopenharmony_ci        return temp;
85b1b8bc3fSopenharmony_ci    }
86b1b8bc3fSopenharmony_ci
87b1b8bc3fSopenharmony_ciprivate:
88b1b8bc3fSopenharmony_ci    std::mutex iptablesMutex_;
89b1b8bc3fSopenharmony_ci    std::condition_variable conditionVarLock_;
90b1b8bc3fSopenharmony_ci    bool isRunningFlag_ = false;
91b1b8bc3fSopenharmony_ci    bool isIptablesSystemAccess_ = false;
92b1b8bc3fSopenharmony_ci    bool isIp6tablesSystemAccess_ = false;
93b1b8bc3fSopenharmony_ci    std::string result_;
94b1b8bc3fSopenharmony_ci    std::thread iptablesWrapperThread_;
95b1b8bc3fSopenharmony_ci    std::queue<std::string> commandsQueue_;
96b1b8bc3fSopenharmony_ci    std::shared_ptr<ffrt::queue> iptablesWrapperFfrtQueue_ = nullptr;
97b1b8bc3fSopenharmony_ci};
98b1b8bc3fSopenharmony_ci} // namespace nmd
99b1b8bc3fSopenharmony_ci} // namespace OHOS
100b1b8bc3fSopenharmony_ci#endif /* NETMANAGER_BASE_IPTABLES_WRAPPER_H */
101