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 INCLUDE_SHARING_MANAGER_H
17 #define INCLUDE_SHARING_MANAGER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include "iptables_wrapper.h"
26 #include "network_sharing.h"
27 
28 namespace OHOS {
29 namespace nmd {
30 class SharingManager {
31 public:
32     SharingManager();
33     ~SharingManager() = default;
34 
35     /*
36      * @brief Enable IP forwarding
37      *
38      * @param requestor
39      * @return NETMANAGER_ERROR code
40      */
41     int32_t IpEnableForwarding(const std::string &requestor);
42 
43     /*
44      * @brief Disable IP forwarding
45      *
46      * @param requestor
47      * @return NETMANAGER_ERROR code
48      */
49     int32_t IpDisableForwarding(const std::string &requestor);
50 
51     /*
52      * @brief Enable network address forwarding
53      *
54      * @param downstreamIface
55      * @param upstreamIface
56      * @return NETMANAGER_ERROR code
57      */
58     int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface);
59 
60     /*
61      * @brief Disable network address forwarding
62      *
63      * @param downstreamIface
64      * @param upstreamIface
65      * @return NETMANAGER_ERROR code
66      */
67     int32_t DisableNat(const std::string &downstramIface, const std::string &upstreamIface);
68 
69     /*
70      * @brief According to the network cark configuration rules of iptables
71      *
72      * @param fromIface
73      * @param toIface
74      * @return NETMANAGER_ERROR code
75      */
76     int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface);
77 
78     /*
79      * @brief According to the network cark configuration rules of iptables
80      *
81      * @param fromIface
82      * @param toIface
83      * @return NETMANAGER_ERROR code
84      */
85     int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface);
86 
87     /*
88      * @brief Get trafic of network sharing
89      *
90      * @param downIface
91      * @param upIface
92      * @param traffic
93      * @return NETMANAGER_ERROR code
94      */
95     int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
96                                      NetworkSharingTraffic &traffic);
97 
98     int32_t SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on);
99     int32_t SetEnableIpv6(const std::string &interfaceName, const uint32_t on);
100 private:
101     std::set<std::string> forwardingRequests_;
102     std::set<std::string> interfaceForwards_;
103     std::shared_ptr<IptablesWrapper> iptablesWrapper_ = nullptr;
104     bool inited_ = false;
105     std::mutex initedMutex_;
106 
107     void IpfwdExecSaveBak();
108     void InitChildChains();
109     void CheckInited();
110     int32_t SetIpFwdEnable();
111     int32_t SetForwardRules(bool set, const std::string &cmds);
112 };
113 } // namespace nmd
114 } // namespace OHOS
115 #endif // INCLUDE_SHARING_MANAGER_H
116