1b1b8bc3fSopenharmony_ci/* 2b1b8bc3fSopenharmony_ci * Copyright (c) 2021-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 NETWORK_H 17b1b8bc3fSopenharmony_ci#define NETWORK_H 18b1b8bc3fSopenharmony_ci 19b1b8bc3fSopenharmony_ci#include "event_report.h" 20b1b8bc3fSopenharmony_ci#include "i_net_detection_callback.h" 21b1b8bc3fSopenharmony_ci#include "i_net_monitor_callback.h" 22b1b8bc3fSopenharmony_ci#include "inet_addr.h" 23b1b8bc3fSopenharmony_ci#include "net_conn_event_handler.h" 24b1b8bc3fSopenharmony_ci#include "net_conn_types.h" 25b1b8bc3fSopenharmony_ci#include "net_link_info.h" 26b1b8bc3fSopenharmony_ci#include "net_monitor.h" 27b1b8bc3fSopenharmony_ci#include "net_supplier_info.h" 28b1b8bc3fSopenharmony_ci#include "route.h" 29b1b8bc3fSopenharmony_ci#include "nat464_service.h" 30b1b8bc3fSopenharmony_ci#include "netmanager_base_common_utils.h" 31b1b8bc3fSopenharmony_ci 32b1b8bc3fSopenharmony_cinamespace OHOS { 33b1b8bc3fSopenharmony_cinamespace NetManagerStandard { 34b1b8bc3fSopenharmony_ciusing NetDetectionHandler = std::function<void(uint32_t supplierId, NetDetectionStatus netState)>; 35b1b8bc3fSopenharmony_ciclass Network : public virtual RefBase, public INetMonitorCallback, public std::enable_shared_from_this<Network> { 36b1b8bc3fSopenharmony_cipublic: 37b1b8bc3fSopenharmony_ci Network(int32_t netId, uint32_t supplierId, const NetDetectionHandler &handler, NetBearType bearerType, 38b1b8bc3fSopenharmony_ci const std::shared_ptr<NetConnEventHandler> &eventHandler); 39b1b8bc3fSopenharmony_ci ~Network() = default; 40b1b8bc3fSopenharmony_ci bool operator==(const Network &network) const; 41b1b8bc3fSopenharmony_ci int32_t GetNetId() const; 42b1b8bc3fSopenharmony_ci uint32_t GetSupplierId() const; 43b1b8bc3fSopenharmony_ci bool UpdateBasicNetwork(bool isAvailable_); 44b1b8bc3fSopenharmony_ci bool UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo); 45b1b8bc3fSopenharmony_ci NetLinkInfo GetNetLinkInfo() const; 46b1b8bc3fSopenharmony_ci std::string GetIfaceName() const; 47b1b8bc3fSopenharmony_ci std::string GetIdent() const; 48b1b8bc3fSopenharmony_ci void UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo); 49b1b8bc3fSopenharmony_ci void HandleUpdateIpAddrs(const NetLinkInfo &newNetLinkInfo); 50b1b8bc3fSopenharmony_ci void UpdateInterfaces(const NetLinkInfo &newNetLinkInfo); 51b1b8bc3fSopenharmony_ci void UpdateRoutes(const NetLinkInfo &newNetLinkInfo); 52b1b8bc3fSopenharmony_ci void UpdateDns(const NetLinkInfo &netLinkInfo); 53b1b8bc3fSopenharmony_ci void UpdateMtu(const NetLinkInfo &netLinkInfo); 54b1b8bc3fSopenharmony_ci void UpdateTcpBufferSize(const NetLinkInfo &netLinkInfo); 55b1b8bc3fSopenharmony_ci void UpdateStatsCached(const NetLinkInfo &netLinkInfo); 56b1b8bc3fSopenharmony_ci void RegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 57b1b8bc3fSopenharmony_ci int32_t UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 58b1b8bc3fSopenharmony_ci void StartNetDetection(bool needReport); 59b1b8bc3fSopenharmony_ci void SetNetCaps(const std::set<NetCap> &netCaps); 60b1b8bc3fSopenharmony_ci void SetDefaultNetWork(); 61b1b8bc3fSopenharmony_ci void ClearDefaultNetWorkNetId(); 62b1b8bc3fSopenharmony_ci bool IsConnecting() const; 63b1b8bc3fSopenharmony_ci bool IsConnected() const; 64b1b8bc3fSopenharmony_ci void UpdateNetConnState(NetConnState netConnState); 65b1b8bc3fSopenharmony_ci void UpdateGlobalHttpProxy(const HttpProxy &httpProxy); 66b1b8bc3fSopenharmony_ci void NetDetectionForDnsHealth(bool dnsHealthSuccess); 67b1b8bc3fSopenharmony_ci 68b1b8bc3fSopenharmony_ci void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override; 69b1b8bc3fSopenharmony_ci 70b1b8bc3fSopenharmony_ci bool ResumeNetworkInfo(); 71b1b8bc3fSopenharmony_ci void CloseSocketsUid(uint32_t uid); 72b1b8bc3fSopenharmony_ci void StopNetDetection(); 73b1b8bc3fSopenharmony_ci#ifdef FEATURE_SUPPORT_POWERMANAGER 74b1b8bc3fSopenharmony_ci void UpdateForbidDetectionFlag(bool forbidDetectionFlag); 75b1b8bc3fSopenharmony_ci#endif 76b1b8bc3fSopenharmony_ci 77b1b8bc3fSopenharmony_ciprivate: 78b1b8bc3fSopenharmony_ci bool CreateBasicNetwork(); 79b1b8bc3fSopenharmony_ci bool CreateVirtualNetwork(); 80b1b8bc3fSopenharmony_ci bool ReleaseBasicNetwork(); 81b1b8bc3fSopenharmony_ci bool ReleaseVirtualNetwork(); 82b1b8bc3fSopenharmony_ci void InitNetMonitor(); 83b1b8bc3fSopenharmony_ci void HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect); 84b1b8bc3fSopenharmony_ci void NotifyNetDetectionResult(NetDetectionResultCode detectionResult, const std::string &urlRedirect); 85b1b8bc3fSopenharmony_ci NetDetectionResultCode NetDetectionResultConvert(int32_t internalRet); 86b1b8bc3fSopenharmony_ci void SendConnectionChangedBroadcast(const NetConnState &netConnState) const; 87b1b8bc3fSopenharmony_ci void SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg); 88b1b8bc3fSopenharmony_ci void ResetNetlinkInfo(); 89b1b8bc3fSopenharmony_ci bool IsDetectionForDnsSuccess(NetDetectionStatus netDetectionState, bool dnsHealthSuccess); 90b1b8bc3fSopenharmony_ci bool IsDetectionForDnsFail(NetDetectionStatus netDetectionState, bool dnsHealthSuccess); 91b1b8bc3fSopenharmony_ci bool IsIfaceNameInUse(); 92b1b8bc3fSopenharmony_ci bool IsNat464Prefered(); 93b1b8bc3fSopenharmony_ci std::string GetNetCapabilitiesAsString(const uint32_t supplierId) const; 94b1b8bc3fSopenharmony_ci 95b1b8bc3fSopenharmony_ciprivate: 96b1b8bc3fSopenharmony_ci int32_t netId_ = 0; 97b1b8bc3fSopenharmony_ci uint32_t supplierId_ = 0; 98b1b8bc3fSopenharmony_ci NetLinkInfo netLinkInfo_; 99b1b8bc3fSopenharmony_ci NetConnState state_ = NET_CONN_STATE_UNKNOWN; 100b1b8bc3fSopenharmony_ci NetDetectionStatus detectResult_ = UNKNOWN_STATE; 101b1b8bc3fSopenharmony_ci std::atomic_bool isPhyNetCreated_ = false; 102b1b8bc3fSopenharmony_ci std::atomic_bool isVirtualCreated_ = false; 103b1b8bc3fSopenharmony_ci std::shared_ptr<NetMonitor> netMonitor_ = nullptr; 104b1b8bc3fSopenharmony_ci NetDetectionHandler netCallback_; 105b1b8bc3fSopenharmony_ci NetBearType netSupplierType_; 106b1b8bc3fSopenharmony_ci std::vector<sptr<INetDetectionCallback>> netDetectionRetCallback_; 107b1b8bc3fSopenharmony_ci std::shared_ptr<NetConnEventHandler> eventHandler_; 108b1b8bc3fSopenharmony_ci std::atomic<bool> isDetectingForDns_ = false; 109b1b8bc3fSopenharmony_ci std::set<NetCap> netCaps_; 110b1b8bc3fSopenharmony_ci std::unique_ptr<Nat464Service> nat464Service_; 111b1b8bc3fSopenharmony_ci#ifdef FEATURE_SUPPORT_POWERMANAGER 112b1b8bc3fSopenharmony_ci bool forbidDetectionFlag_ = false; 113b1b8bc3fSopenharmony_ci#endif 114b1b8bc3fSopenharmony_ci bool isNeedResume_ = false; 115b1b8bc3fSopenharmony_ci}; 116b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard 117b1b8bc3fSopenharmony_ci} // namespace OHOS 118b1b8bc3fSopenharmony_ci#endif // NETWORK_H 119