1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#ifndef NET_VNIC_MANAGER_H
17b1b8bc3fSopenharmony_ci#define NET_VNIC_MANAGER_H
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_ci#include "uid_range.h"
20b1b8bc3fSopenharmony_ci#include <cstdint>
21b1b8bc3fSopenharmony_ci#include <linux/if.h>
22b1b8bc3fSopenharmony_ci#include <map>
23b1b8bc3fSopenharmony_ci#include <set>
24b1b8bc3fSopenharmony_ci#include <string>
25b1b8bc3fSopenharmony_ci
26b1b8bc3fSopenharmony_cinamespace OHOS {
27b1b8bc3fSopenharmony_cinamespace NetManagerStandard {
28b1b8bc3fSopenharmony_ci
29b1b8bc3fSopenharmony_ciclass VnicManager {
30b1b8bc3fSopenharmony_ciprivate:
31b1b8bc3fSopenharmony_ci    VnicManager() = default;
32b1b8bc3fSopenharmony_ci    ~VnicManager() = default;
33b1b8bc3fSopenharmony_ci    VnicManager(const VnicManager &) = delete;
34b1b8bc3fSopenharmony_ci    VnicManager &operator=(const VnicManager &) = delete;
35b1b8bc3fSopenharmony_ci
36b1b8bc3fSopenharmony_cipublic:
37b1b8bc3fSopenharmony_ci    static VnicManager &GetInstance()
38b1b8bc3fSopenharmony_ci    {
39b1b8bc3fSopenharmony_ci        static VnicManager instance;
40b1b8bc3fSopenharmony_ci        return instance;
41b1b8bc3fSopenharmony_ci    }
42b1b8bc3fSopenharmony_ci
43b1b8bc3fSopenharmony_cipublic:
44b1b8bc3fSopenharmony_ci    int32_t CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix, const std::set<int32_t> &uids);
45b1b8bc3fSopenharmony_ci    int32_t DestroyVnic();
46b1b8bc3fSopenharmony_ci    int32_t CreateVnicInterface();
47b1b8bc3fSopenharmony_ci    void DestroyVnicInterface();
48b1b8bc3fSopenharmony_ci    int32_t SetVnicMtu(const std::string &ifName, int32_t mtu);
49b1b8bc3fSopenharmony_ci    int32_t SetVnicAddress(const std::string &ifName, const std::string &tunAddr, int32_t prefix);
50b1b8bc3fSopenharmony_ci
51b1b8bc3fSopenharmony_ciprivate:
52b1b8bc3fSopenharmony_ci    std::atomic_int& GetNetSock(bool ipv4);
53b1b8bc3fSopenharmony_ci    int32_t SetVnicUp();
54b1b8bc3fSopenharmony_ci    int32_t SetVnicDown();
55b1b8bc3fSopenharmony_ci    int32_t AddDefaultRoute();
56b1b8bc3fSopenharmony_ci    int32_t DelDefaultRoute();
57b1b8bc3fSopenharmony_ci    int32_t InitIfreq(ifreq &ifr, const std::string &cardName);
58b1b8bc3fSopenharmony_ci    int32_t SetVnicResult(std::atomic_int &fd, unsigned long cmd, ifreq &ifr);
59b1b8bc3fSopenharmony_ci
60b1b8bc3fSopenharmony_ciprivate:
61b1b8bc3fSopenharmony_ci    std::atomic_int tunFd_ = 0;
62b1b8bc3fSopenharmony_ci    std::atomic_int net4Sock_ = 0;
63b1b8bc3fSopenharmony_ci    std::atomic_int net6Sock_ = 0;
64b1b8bc3fSopenharmony_ci    std::vector<NetManagerStandard::UidRange> uidRanges;
65b1b8bc3fSopenharmony_ci};
66b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard
67b1b8bc3fSopenharmony_ci} // namespace OHOS
68b1b8bc3fSopenharmony_ci#endif // NET_VNIC_MANAGER_H
69