1 /* 2 * Copyright (c) 2023 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 NET_VPN_MANAGER_H 17 #define NET_VPN_MANAGER_H 18 19 #include <cstdint> 20 #include <linux/if.h> 21 #include <string> 22 23 namespace OHOS { 24 namespace NetManagerStandard { 25 class VpnManager { 26 private: 27 VpnManager() = default; 28 ~VpnManager() = default; 29 VpnManager(const VpnManager &) = delete; 30 VpnManager &operator=(const VpnManager &) = delete; 31 32 public: GetInstance()33 static VpnManager &GetInstance() 34 { 35 static VpnManager instance; 36 return instance; 37 } 38 39 public: 40 int32_t CreateVpnInterface(); 41 void DestroyVpnInterface(); 42 int32_t SetVpnMtu(const std::string &ifName, int32_t mtu); 43 int32_t SetVpnAddress(const std::string &ifName, const std::string &tunAddr, int32_t prefix); 44 45 private: 46 int32_t SetVpnUp(); 47 int32_t SetVpnDown(); 48 int32_t InitIfreq(ifreq &ifr, const std::string &cardName); 49 int32_t SetVpnResult(std::atomic_int &fd, unsigned long cmd, ifreq &ifr); 50 51 int32_t SendVpnInterfaceFdToClient(int32_t clientFd, int32_t tunFd); 52 void StartUnixSocketListen(); 53 void StartVpnInterfaceFdListen(); 54 55 private: 56 std::atomic_int tunFd_ = 0; 57 std::atomic_int net4Sock_ = 0; 58 std::atomic_int net6Sock_ = 0; 59 std::atomic_bool listeningFlag_ = false; 60 }; 61 } // namespace NetManagerStandard 62 } // namespace OHOS 63 #endif // NET_VPN_MANAGER_H 64