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 "uid_range.h"
20 #include <cstdint>
21 #include <linux/if.h>
22 #include <map>
23 #include <set>
24 #include <string>
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 
48 private:
49     DistributedManager() = default;
50     ~DistributedManager() = default;
51     DistributedManager(const DistributedManager &) = delete;
52     DistributedManager &operator=(const DistributedManager &) = delete;
53 
54 private:
55     int32_t SetDistributedNicUp(const std::string &ifName);
56     int32_t SetDistributedNicDown(const std::string &ifName);
57     int32_t InitIfreq(ifreq &ifr, const std::string &cardName);
58     int32_t SetDistributedNicResult(std::atomic_int &fd, unsigned long cmd, ifreq &ifr);
59     void CloseDistributedSocket();
60 
61 private:
62     std::atomic_int tunFd_ = 0;
63     std::atomic_int net4Sock_ = 0;
64     std::string serverIif_;
65     std::string serverDevIface_;
66 };
67 } // namespace NetManagerStandard
68 } // namespace OHOS
69 #endif // NET_DISTRIBUTED_MANAGER_H
70