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