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 32 namespace OHOS { 33 namespace NetManagerStandard { 34 using NetDetectionHandler = std::function<void(uint32_t supplierId, NetDetectionStatus netState)>; 35 class Network : public virtual RefBase, public INetMonitorCallback, public std::enable_shared_from_this<Network> { 36 public: 37 Network(int32_t netId, uint32_t supplierId, const NetDetectionHandler &handler, NetBearType bearerType, 38 const std::shared_ptr<NetConnEventHandler> &eventHandler); 39 ~Network() = default; 40 bool operator==(const Network &network) const; 41 int32_t GetNetId() const; 42 uint32_t GetSupplierId() const; 43 bool UpdateBasicNetwork(bool isAvailable_); 44 bool UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo); 45 NetLinkInfo GetNetLinkInfo() const; 46 std::string GetIfaceName() const; 47 std::string GetIdent() const; 48 HttpProxy GetHttpProxy() const; 49 void UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo); 50 void HandleUpdateIpAddrs(const NetLinkInfo &newNetLinkInfo); 51 void UpdateInterfaces(const NetLinkInfo &newNetLinkInfo); 52 void UpdateRoutes(const NetLinkInfo &newNetLinkInfo); 53 void UpdateDns(const NetLinkInfo &netLinkInfo); 54 void UpdateMtu(const NetLinkInfo &netLinkInfo); 55 void UpdateTcpBufferSize(const NetLinkInfo &netLinkInfo); 56 void UpdateStatsCached(const NetLinkInfo &netLinkInfo); 57 void RegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 58 int32_t UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 59 void StartNetDetection(bool needReport); 60 void SetNetCaps(const std::set<NetCap> &netCaps); 61 void SetDefaultNetWork(); 62 void ClearDefaultNetWorkNetId(); 63 bool IsConnecting() const; 64 bool IsConnected() const; 65 void UpdateNetConnState(NetConnState netConnState); 66 void UpdateGlobalHttpProxy(const HttpProxy &httpProxy); 67 void NetDetectionForDnsHealth(bool dnsHealthSuccess); 68 69 void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override; 70 71 bool ResumeNetworkInfo(); 72 73 private: 74 void StopNetDetection(); 75 bool CreateBasicNetwork(); 76 bool CreateVirtualNetwork(); 77 bool ReleaseBasicNetwork(); 78 bool ReleaseVirtualNetwork(); 79 void InitNetMonitor(); 80 void HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect); 81 void NotifyNetDetectionResult(NetDetectionResultCode detectionResult, const std::string &urlRedirect); 82 NetDetectionResultCode NetDetectionResultConvert(int32_t internalRet); 83 void SendConnectionChangedBroadcast(const NetConnState &netConnState) const; 84 void SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg); 85 void ResetNetlinkInfo(); 86 bool IsDetectionForDnsSuccess(NetDetectionStatus netDetectionState, bool dnsHealthSuccess); 87 bool IsDetectionForDnsFail(NetDetectionStatus netDetectionState, bool dnsHealthSuccess); 88 bool IsAddrInOtherNetwork(const INetAddr &netAddr); 89 bool IsIfaceNameInUse(); 90 bool IsNat464Prefered(); 91 std::string GetNetCapabilitiesAsString(const uint32_t supplierId) const; 92 93 private: 94 int32_t netId_ = 0; 95 uint32_t supplierId_ = 0; 96 NetLinkInfo netLinkInfo_; 97 NetConnState state_ = NET_CONN_STATE_UNKNOWN; 98 NetDetectionStatus detectResult_ = UNKNOWN_STATE; 99 std::atomic_bool isPhyNetCreated_ = false; 100 std::atomic_bool isVirtualCreated_ = false; 101 std::shared_ptr<NetMonitor> netMonitor_ = nullptr; 102 NetDetectionHandler netCallback_; 103 NetBearType netSupplierType_; 104 std::vector<sptr<INetDetectionCallback>> netDetectionRetCallback_; 105 std::shared_ptr<NetConnEventHandler> eventHandler_; 106 std::atomic<bool> isDetectingForDns_ = false; 107 std::set<NetCap> netCaps_; 108 std::unique_ptr<Nat464Service> nat464Service_; 109 }; 110 } // namespace NetManagerStandard 111 } // namespace OHOS 112 #endif // NETWORK_H 113