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