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