1 /* 2 * Copyright (c) 2024 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 NETSYS_WEARABLE_DISTRIBUTED_NET_MANAGER_H 17 #define NETSYS_WEARABLE_DISTRIBUTED_NET_MANAGER_H 18 19 #include <cstdint> 20 #include <fstream> 21 #include <string> 22 #include <vector> 23 #include "cJSON.h" 24 25 #define TCP_ADD16 "-w -o lo -t nat -A DISTRIBUTED_NET_TCP -p tcp -j REDIRECT "\ 26 "--to-ports %d" 27 #define UDP_ADD16 "-w -i lo -t mangle -A DISTRIBUTED_NET_UDP -p udp -j TPROXY "\ 28 "--tproxy-mark 0x1/0x1 --on-port %d" 29 #define INPUT_ADD "-w -A INPUT -p tcp -s 127.0.0.1 --destination-port %d -j REJECT" 30 #define INPUT_DEL "-w -D INPUT -p tcp -s 127.0.0.1 --destination-port %d -j REJECT" 31 32 namespace OHOS { 33 namespace nmd { 34 class WearableDistributedNet { 35 public: 36 enum RULES_TYPE { 37 TCP_ADD_RULE, 38 UDP_ADD_RULE, 39 INPUT_ADD_RULE, 40 INPUT_DEL_RULE, 41 DEFAULT_RULE 42 }; 43 44 /** 45 * @brief Enables the wearable distributed network forwarding by configuring TCP and UDP ports 46 * 47 * @param tcpPortId The TCP port ID 48 * @param udpPortId The UDP port ID 49 * @return NETMANAGER_SUCCESS if successful, NETMANAGER_ERROR if any of the operations fail 50 */ 51 int32_t EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId); 52 53 /** 54 * @brief Disables the wearable distributed network forwarding by removing configured rules 55 * 56 * @return NETMANAGER_SUCCESS if successful, NETMANAGER_ERROR if any of the operations fail 57 */ 58 int32_t DisableWearableDistributedNetForward(); 59 60 /** 61 * @brief Reads the system's iptables configuration from a JSON file and processes the relevant iptables settings 62 * 63 * This function reads a JSON configuration file located at IPTABLES_CONFIG_PATH, parses it, and then extracts 64 * the iptables configuration. It specifically looks for the iptables component flag to decide whether to 65 * proceed with reading and applying iptables interfaces or not 66 * 67 * @return true if the configuration was successfully read and processed, false otherwise 68 */ 69 bool ReadSystemIptablesConfiguration(); 70 71 private: 72 int32_t EstablishTcpIpRules(); 73 int32_t EstablishUdpIpRules(const int32_t udpPortId); 74 int32_t ExecuteIptablesCommands(const std::vector<std::string> &commands); 75 std::string GenerateRule(const std::string &inputRules, const int32_t portId); 76 int32_t ApplyRule(const RULES_TYPE type, const int32_t portId); 77 void SetTcpPort(const int32_t tcpPortId); 78 int32_t GetTcpPort(); 79 80 bool ReadIptablesInterfaces(const cJSON &json); 81 std::string ReadJsonFile(); 82 std::vector<std::string> GetTcpIptables(); 83 std::string GetOutputAddTcp(); 84 std::vector<std::string> GetUdpIptables(); 85 std::string GetUdpoutput(); 86 std::vector<std::string> GetIptablesDeleteCmds(); 87 88 bool ParseTcpIptables(const cJSON &json); 89 bool ParseTcpOutputRule(const cJSON &json); 90 bool ParseUdpIptables(const cJSON &json); 91 bool ParseUdpOutputRule(const cJSON &json); 92 bool ParseIptablesDeleteCmds(const cJSON &json); 93 94 private: 95 int32_t tcpPort_; 96 std::vector<std::string> tcpIptables_; 97 std::string tcpOutput_; 98 std::vector<std::string> udpIptables_; 99 std::string udpOutput_; 100 std::vector<std::string> iptablesDeleteCmds_; 101 std::string configPath_ = IPTABLES_CONFIG_PATH; 102 }; 103 } // namespace nmd 104 } // namespace OHOS// namespace OHOS::nmd 105 #endif // NETSYS_WEARABLE_DISTRIBUTED_NET_MANAGER_H