18e745fdaSopenharmony_ci/* 28e745fdaSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 38e745fdaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48e745fdaSopenharmony_ci * you may not use this file except in compliance with the License. 58e745fdaSopenharmony_ci * You may obtain a copy of the License at 68e745fdaSopenharmony_ci * 78e745fdaSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88e745fdaSopenharmony_ci * 98e745fdaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108e745fdaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118e745fdaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128e745fdaSopenharmony_ci * See the License for the specific language governing permissions and 138e745fdaSopenharmony_ci * limitations under the License. 148e745fdaSopenharmony_ci */ 158e745fdaSopenharmony_ci 168e745fdaSopenharmony_ci#ifndef NET_VPN_IMPL_H 178e745fdaSopenharmony_ci#define NET_VPN_IMPL_H 188e745fdaSopenharmony_ci 198e745fdaSopenharmony_ci#include <cstdint> 208e745fdaSopenharmony_ci#include <memory> 218e745fdaSopenharmony_ci#include <set> 228e745fdaSopenharmony_ci#include <vector> 238e745fdaSopenharmony_ci 248e745fdaSopenharmony_ci#include "bundle_mgr_proxy.h" 258e745fdaSopenharmony_ci#include "i_vpn_conn_state_cb.h" 268e745fdaSopenharmony_ci#include "net_all_capabilities.h" 278e745fdaSopenharmony_ci#include "net_conn_client.h" 288e745fdaSopenharmony_ci#include "net_manager_ext_constants.h" 298e745fdaSopenharmony_ci#include "net_specifier.h" 308e745fdaSopenharmony_ci#include "net_supplier_info.h" 318e745fdaSopenharmony_ci#include "networkvpn_hisysevent.h" 328e745fdaSopenharmony_ci#ifdef SUPPORT_SYSVPN 338e745fdaSopenharmony_ci#include "sysvpn_config.h" 348e745fdaSopenharmony_ci#endif // SUPPORT_SYSVPN 358e745fdaSopenharmony_ci#include "vpn_config.h" 368e745fdaSopenharmony_ci 378e745fdaSopenharmony_cinamespace OHOS { 388e745fdaSopenharmony_cinamespace NetManagerStandard { 398e745fdaSopenharmony_ciconstexpr const char *TUN_CARD_NAME = "vpn-tun"; 408e745fdaSopenharmony_ci 418e745fdaSopenharmony_ciclass NetVpnImpl { 428e745fdaSopenharmony_cipublic: 438e745fdaSopenharmony_ci NetVpnImpl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId, std::vector<int32_t> &activeUserIds); 448e745fdaSopenharmony_ci virtual ~NetVpnImpl() = default; 458e745fdaSopenharmony_ci 468e745fdaSopenharmony_ci virtual bool IsInternalVpn() = 0; 478e745fdaSopenharmony_ci virtual int32_t SetUp() = 0; 488e745fdaSopenharmony_ci virtual int32_t Destroy() = 0; 498e745fdaSopenharmony_ci#ifdef SUPPORT_SYSVPN 508e745fdaSopenharmony_ci virtual int32_t GetConnectedSysVpnConfig(sptr<SysVpnConfig> &sysVpnConfig); 518e745fdaSopenharmony_ci virtual int32_t NotifyConnectStage(const std::string &stage, const int32_t &result); 528e745fdaSopenharmony_ci virtual int32_t GetSysVpnCertUri(const int32_t certType, std::string &certUri); 538e745fdaSopenharmony_ci virtual bool IsSystemVpn(); 548e745fdaSopenharmony_ci#endif // SUPPORT_SYSVPN 558e745fdaSopenharmony_ci int32_t RegisterConnectStateChangedCb(std::shared_ptr<IVpnConnStateCb> callback); 568e745fdaSopenharmony_ci void NotifyConnectState(const VpnConnectState &state); 578e745fdaSopenharmony_ci 588e745fdaSopenharmony_cipublic: 598e745fdaSopenharmony_ci inline sptr<VpnConfig> GetVpnConfig() const 608e745fdaSopenharmony_ci { 618e745fdaSopenharmony_ci return vpnConfig_; 628e745fdaSopenharmony_ci } 638e745fdaSopenharmony_ci inline std::string GetVpnPkg() const 648e745fdaSopenharmony_ci { 658e745fdaSopenharmony_ci return pkgName_; 668e745fdaSopenharmony_ci } 678e745fdaSopenharmony_ci inline int32_t GetUserId() const 688e745fdaSopenharmony_ci { 698e745fdaSopenharmony_ci return userId_; 708e745fdaSopenharmony_ci } 718e745fdaSopenharmony_ci inline bool IsVpnConnecting() const 728e745fdaSopenharmony_ci { 738e745fdaSopenharmony_ci return isVpnConnecting_; 748e745fdaSopenharmony_ci } 758e745fdaSopenharmony_ci inline std::string GetInterfaceName() const 768e745fdaSopenharmony_ci { 778e745fdaSopenharmony_ci return TUN_CARD_NAME; 788e745fdaSopenharmony_ci } 798e745fdaSopenharmony_ci 808e745fdaSopenharmony_ci int32_t ResumeUids(); 818e745fdaSopenharmony_ci 828e745fdaSopenharmony_ciprotected: 838e745fdaSopenharmony_ci bool UpdateNetLinkInfo(); 848e745fdaSopenharmony_ci 858e745fdaSopenharmony_ciprivate: 868e745fdaSopenharmony_ci bool RegisterNetSupplier(NetConnClient &netConnClientIns); 878e745fdaSopenharmony_ci void UnregisterNetSupplier(NetConnClient &netConnClientIns); 888e745fdaSopenharmony_ci bool UpdateNetSupplierInfo(NetConnClient &netConnClientIns, bool isAvailable); 898e745fdaSopenharmony_ci 908e745fdaSopenharmony_ci void DelNetLinkInfo(NetConnClient &netConnClientIns); 918e745fdaSopenharmony_ci void AdjustRouteInfo(Route &route); 928e745fdaSopenharmony_ci void SetIpv4DefaultRoute(Route &ipv4DefaultRoute); 938e745fdaSopenharmony_ci void SetIpv6DefaultRoute(Route &ipv6DefaultRoute); 948e745fdaSopenharmony_ci 958e745fdaSopenharmony_ci void GenerateUidRangesByAcceptedApps(const std::set<int32_t> &uids, std::vector<int32_t> &beginUids, 968e745fdaSopenharmony_ci std::vector<int32_t> &endUids); 978e745fdaSopenharmony_ci void GenerateUidRangesByRefusedApps(int32_t userId, const std::set<int32_t> &uids, std::vector<int32_t> &beginUids, 988e745fdaSopenharmony_ci std::vector<int32_t> &endUids); 998e745fdaSopenharmony_ci std::set<int32_t> GetAppsUids(int32_t userId, const std::vector<std::string> &applications); 1008e745fdaSopenharmony_ci int32_t GenerateUidRanges(int32_t userId, std::vector<int32_t> &beginUids, std::vector<int32_t> &endUids); 1018e745fdaSopenharmony_ci 1028e745fdaSopenharmony_ciprotected: 1038e745fdaSopenharmony_ci sptr<VpnConfig> vpnConfig_ = nullptr; 1048e745fdaSopenharmony_ci 1058e745fdaSopenharmony_ciprivate: 1068e745fdaSopenharmony_ci std::string pkgName_; 1078e745fdaSopenharmony_ci int32_t userId_ = -1; // the calling app's user 1088e745fdaSopenharmony_ci std::vector<int32_t> activeUserIds_; 1098e745fdaSopenharmony_ci bool isVpnConnecting_ = false; 1108e745fdaSopenharmony_ci 1118e745fdaSopenharmony_ci int32_t netId_ = -1; 1128e745fdaSopenharmony_ci uint32_t netSupplierId_ = 0; 1138e745fdaSopenharmony_ci std::vector<int32_t> beginUids_; 1148e745fdaSopenharmony_ci std::vector<int32_t> endUids_; 1158e745fdaSopenharmony_ci std::shared_ptr<IVpnConnStateCb> connChangedCb_; 1168e745fdaSopenharmony_ci sptr<NetSupplierInfo> netSupplierInfo_ = nullptr; 1178e745fdaSopenharmony_ci 1188e745fdaSopenharmony_ci void SetAllUidRanges(); 1198e745fdaSopenharmony_ci}; 1208e745fdaSopenharmony_ci} // namespace NetManagerStandard 1218e745fdaSopenharmony_ci} // namespace OHOS 1228e745fdaSopenharmony_ci#endif // NET_VPN_IMPL_H 123