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_VNIC_MANAGER_H
17#define NET_VNIC_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
26namespace OHOS {
27namespace NetManagerStandard {
28
29class VnicManager {
30private:
31    VnicManager() = default;
32    ~VnicManager() = default;
33    VnicManager(const VnicManager &) = delete;
34    VnicManager &operator=(const VnicManager &) = delete;
35
36public:
37    static VnicManager &GetInstance()
38    {
39        static VnicManager instance;
40        return instance;
41    }
42
43public:
44    int32_t CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix, const std::set<int32_t> &uids);
45    int32_t DestroyVnic();
46    int32_t CreateVnicInterface();
47    void DestroyVnicInterface();
48    int32_t SetVnicMtu(const std::string &ifName, int32_t mtu);
49    int32_t SetVnicAddress(const std::string &ifName, const std::string &tunAddr, int32_t prefix);
50
51private:
52    std::atomic_int& GetNetSock(bool ipv4);
53    int32_t SetVnicUp();
54    int32_t SetVnicDown();
55    int32_t AddDefaultRoute();
56    int32_t DelDefaultRoute();
57    int32_t InitIfreq(ifreq &ifr, const std::string &cardName);
58    int32_t SetVnicResult(std::atomic_int &fd, unsigned long cmd, ifreq &ifr);
59
60private:
61    std::atomic_int tunFd_ = 0;
62    std::atomic_int net4Sock_ = 0;
63    std::atomic_int net6Sock_ = 0;
64    std::vector<NetManagerStandard::UidRange> uidRanges;
65};
66} // namespace NetManagerStandard
67} // namespace OHOS
68#endif // NET_VNIC_MANAGER_H
69