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_IMPL_H
17 #define NET_VPN_IMPL_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <set>
22 #include <vector>
23 
24 #include "bundle_mgr_proxy.h"
25 #include "i_vpn_conn_state_cb.h"
26 #include "net_all_capabilities.h"
27 #include "net_conn_client.h"
28 #include "net_manager_ext_constants.h"
29 #include "net_specifier.h"
30 #include "net_supplier_info.h"
31 #include "networkvpn_hisysevent.h"
32 #include "vpn_config.h"
33 
34 namespace OHOS {
35 namespace NetManagerStandard {
36 constexpr const char *TUN_CARD_NAME = "vpn-tun";
37 
38 class NetVpnImpl {
39 public:
40     NetVpnImpl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId, std::vector<int32_t> &activeUserIds);
41     virtual ~NetVpnImpl() = default;
42 
43     virtual bool IsInternalVpn() = 0;
44     virtual int32_t SetUp() = 0;
45     virtual int32_t Destroy() = 0;
46 
47     int32_t RegisterConnectStateChangedCb(std::shared_ptr<IVpnConnStateCb> callback);
48     void NotifyConnectState(const VpnConnectState &state);
49 
50 public:
GetVpnConfig()51     inline sptr<VpnConfig> GetVpnConfig() const
52     {
53         return vpnConfig_;
54     }
GetVpnPkg()55     inline std::string GetVpnPkg() const
56     {
57         return pkgName_;
58     }
GetUserId()59     inline int32_t GetUserId() const
60     {
61         return userId_;
62     }
IsVpnConnecting()63     inline bool IsVpnConnecting() const
64     {
65         return isVpnConnecting_;
66     }
GetInterfaceName()67     inline std::string GetInterfaceName() const
68     {
69         return TUN_CARD_NAME;
70     }
71 
72     int32_t ResumeUids();
73 
74 private:
75     bool RegisterNetSupplier(NetConnClient &netConnClientIns);
76     void UnregisterNetSupplier(NetConnClient &netConnClientIns);
77     bool UpdateNetSupplierInfo(NetConnClient &netConnClientIns, bool isAvailable);
78     bool UpdateNetLinkInfo(NetConnClient &netConnClientIns);
79     void DelNetLinkInfo(NetConnClient &netConnClientIns);
80     void AdjustRouteInfo(Route &route);
81     void SetIpv4DefaultRoute(Route &ipv4DefaultRoute);
82     void SetIpv6DefaultRoute(Route &ipv6DefaultRoute);
83 
84     void GenerateUidRangesByAcceptedApps(const std::set<int32_t> &uids, std::vector<int32_t> &beginUids,
85                                          std::vector<int32_t> &endUids);
86     void GenerateUidRangesByRefusedApps(int32_t userId, const std::set<int32_t> &uids, std::vector<int32_t> &beginUids,
87                                         std::vector<int32_t> &endUids);
88     std::set<int32_t> GetAppsUids(int32_t userId, const std::vector<std::string> &applications);
89     int32_t GenerateUidRanges(int32_t userId, std::vector<int32_t> &beginUids, std::vector<int32_t> &endUids);
90 
91 protected:
92     sptr<VpnConfig> vpnConfig_ = nullptr;
93 
94 private:
95     std::string pkgName_;
96     int32_t userId_ = -1; // the calling app's user
97     std::vector<int32_t> activeUserIds_;
98     bool isVpnConnecting_ = false;
99 
100     int32_t netId_ = -1;
101     uint32_t netSupplierId_ = 0;
102     std::vector<int32_t> beginUids_;
103     std::vector<int32_t> endUids_;
104     std::shared_ptr<IVpnConnStateCb> connChangedCb_;
105     sptr<NetSupplierInfo> netSupplierInfo_ = nullptr;
106 };
107 } // namespace NetManagerStandard
108 } // namespace OHOS
109 #endif // NET_VPN_IMPL_H
110