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 NET_DISTRIBUTED_MANAGER_H 17 #define NET_DISTRIBUTED_MANAGER_H 18 19 #include <cstdint> 20 #include <map> 21 #include <set> 22 #include <string> 23 #include <linux/if.h> 24 #include "uid_range.h" 25 26 namespace OHOS { 27 namespace NetManagerStandard { 28 29 class DistributedManager { 30 public: GetInstance()31 static DistributedManager &GetInstance() 32 { 33 static DistributedManager instance; 34 return instance; 35 } 36 37 public: 38 int32_t CreateDistributedNic(const std::string &virNicAddr, const std::string &ifName); 39 int32_t DestroyDistributedNic(const std::string &ifName); 40 int32_t CreateDistributedInterface(const std::string &ifName); 41 int32_t SetDistributedNicMtu(const std::string &ifName, int32_t mtu); 42 int32_t SetDistributedNicAddress(const std::string &ifName, const std::string &tunAddr); 43 void SetServerNicInfo(const std::string &iif, const std::string &devIface); 44 void CloseDistributedTunFd(); 45 std::string GetServerIifNic(); 46 std::string GetServerDevIfaceNic(); 47 int32_t ConfigVirnicAndVeth(const std::string &virNicAddr, const std::string &virnicName, 48 const std::string &virnicVethName); 49 void DisableVirnic(const std::string &virnicName); 50 51 private: 52 DistributedManager() = default; 53 ~DistributedManager() = default; 54 DistributedManager(const DistributedManager &) = delete; 55 DistributedManager &operator=(const DistributedManager &) = delete; 56 57 private: 58 int32_t SetDistributedNicUp(const std::string &ifName); 59 int32_t SetDistributedNicDown(const std::string &ifName); 60 int32_t InitIfreq(ifreq &ifr, const std::string &cardName); 61 int32_t SetDistributedNicResult(std::atomic_int &fd, unsigned long cmd, ifreq &ifr); 62 void CloseDistributedSocket(); 63 64 private: 65 std::atomic_int tunFd_ = 0; 66 std::atomic_int net4Sock_ = 0; 67 std::string serverIif_; 68 std::string serverDevIface_; 69 }; 70 } // namespace NetManagerStandard 71 } // namespace OHOS 72 #endif // NET_DISTRIBUTED_MANAGER_H 73