1 /* 2 * Copyright (c) 2025 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 MULTI_VPN_MANAGER_H 17 #define MULTI_VPN_MANAGER_H 18 19 #include <cstdint> 20 #include <linux/if.h> 21 #include <string> 22 #include <mutex> 23 24 namespace OHOS { 25 namespace NetManagerStandard { 26 constexpr const char *XFRM_CARD_NAME = "xfrm-vpn"; 27 constexpr const char *PPP_CARD_NAME = "ppp-vpn"; 28 constexpr const char *MULTI_TUN_CARD_NAME = "multitun-vpn"; 29 constexpr const char *PPP_DEVICE_PATH = "/dev/ppp"; 30 constexpr const char *TUN_DEVICE_PATH = "/dev/tun"; 31 32 class MultiVpnManager : public std::enable_shared_from_this<MultiVpnManager> { 33 private: 34 MultiVpnManager(const MultiVpnManager &) = delete; 35 MultiVpnManager &operator=(const MultiVpnManager &) = delete; 36 37 public: 38 MultiVpnManager() = default; 39 ~MultiVpnManager() = default; GetInstance()40 static MultiVpnManager &GetInstance() 41 { 42 static std::shared_ptr<MultiVpnManager> instance = std::make_shared<MultiVpnManager>(); 43 return *instance; 44 } 45 46 public: 47 int32_t CreateVpnInterface(const std::string &ifName); 48 int32_t DestroyVpnInterface(const std::string &ifName); 49 int32_t SetVpnAddress(const std::string &ifName, const std::string &vpnAddr, int32_t prefix); 50 int32_t SetVpnMtu(const std::string &ifName, int32_t mtu); 51 int32_t SetVpnCallMode(const std::string &message); 52 void CreatePppFd(const std::string &ifName); 53 void SetXfrmPhyIfName(const std::string &phyName); 54 void SetVpnRemoteAddress(const std::string &remoteIp); 55 56 private: 57 int32_t SendVpnInterfaceFdToClient(int32_t clientFd, int32_t tunFd); 58 int32_t SetVpnResult(std::atomic_int &fd, unsigned long cmd, ifreq &ifr); 59 int32_t InitIfreq(ifreq &ifr, const std::string &ifName); 60 int32_t SetVpnDown(const std::string &ifName); 61 int32_t SetVpnUp(const std::string &ifName); 62 int32_t AddVpnRemoteAddress(const std::string &ifName, std::atomic_int &net4Sock, ifreq &ifr); 63 int32_t CreatePppInterface(const std::string &ifName); 64 int32_t GetMultiVpnFd(const std::string &ifName, int32_t &multiVpnFd); 65 int32_t DestroyMultiVpnFd(const std::string &ifName); 66 int32_t CreateMultiTunInterface(const std::string &ifName); 67 void StartMultiVpnSocketListen(); 68 void StartMultiVpnInterfaceFdListen(); 69 70 private: 71 std::atomic_bool multiVpnListeningFlag_ = false; 72 std::unordered_map<std::string, std::atomic_int> multiVpnFdMap_; 73 std::string phyName_; 74 std::string multiVpnListeningName_; 75 std::string remoteIpv4Addr_; 76 std::mutex mutex_; 77 }; 78 } // namespace NetManagerStandard 79 } // namespace OHOS 80 #endif // MULTI_VPN_MANAGER_H 81