• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
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 private:
73     bool RegisterNetSupplier(NetConnClient &netConnClientIns);
74     void UnregisterNetSupplier(NetConnClient &netConnClientIns);
75     bool UpdateNetSupplierInfo(NetConnClient &netConnClientIns, bool isAvailable);
76     bool UpdateNetLinkInfo(NetConnClient &netConnClientIns);
77     void DelNetLinkInfo(NetConnClient &netConnClientIns);
78     void AdjustRouteInfo(Route &route);
79 
80     void GenerateUidRangesByAcceptedApps(const std::set<int32_t> &uids, std::vector<int32_t> &beginUids,
81                                          std::vector<int32_t> &endUids);
82     void GenerateUidRangesByRefusedApps(const std::set<int32_t> &uids, std::vector<int32_t> &beginUids,
83                                         std::vector<int32_t> &endUids);
84     std::set<int32_t> GetAppsUids(const std::vector<std::string> &applications);
85     int32_t GenerateUidRanges(std::vector<int32_t> &beginUids, std::vector<int32_t> &endUids);
86 
87 protected:
88     sptr<VpnConfig> vpnConfig_ = nullptr;
89 
90 private:
91     std::string pkgName_;
92     int32_t userId_ = -1; // the calling app's user
93     bool isVpnConnecting_ = false;
94 
95     int32_t netId_ = -1;
96     uint32_t netSupplierId_ = 0;
97     std::vector<int32_t> beginUids_;
98     std::vector<int32_t> endUids_;
99     std::shared_ptr<IVpnConnStateCb> connChangedCb_;
100     sptr<NetSupplierInfo> netSupplierInfo_ = nullptr;
101 };
102 } // namespace NetManagerStandard
103 } // namespace OHOS
104 #endif // NET_VPN_IMPL_H
105