1 /* 2 * Copyright (c) 2021-2022 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 30 namespace OHOS { 31 namespace NetManagerStandard { 32 constexpr uint32_t INVALID_NET_ID = 0; 33 constexpr int32_t MIN_NET_ID = 100; 34 constexpr int32_t MAX_NET_ID = 0xFFFF - 0x400; 35 using NetDetectionHandler = std::function<void(uint32_t supplierId, bool ifValid)>; 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 bool UpdateBasicNetwork(bool isAvailable_); 44 bool UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo); 45 NetLinkInfo GetNetLinkInfo() const; 46 void UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo); 47 void UpdateInterfaces(const NetLinkInfo &newNetLinkInfo); 48 void UpdateRoutes(const NetLinkInfo &newNetLinkInfo); 49 void UpdateDns(const NetLinkInfo &netLinkInfo); 50 void UpdateMtu(const NetLinkInfo &netLinkInfo); 51 void UpdateTcpBufferSize(const NetLinkInfo &netLinkInfo); 52 void RegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 53 int32_t UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 54 void StartNetDetection(bool needReport); 55 void SetDefaultNetWork(); 56 void ClearDefaultNetWorkNetId(); 57 bool IsConnecting() const; 58 bool IsConnected() const; 59 void UpdateNetConnState(NetConnState netConnState); 60 void UpdateGlobalHttpProxy(const HttpProxy &httpProxy); 61 void NetDetectionForDnsHealth(bool dnsHealthSuccess); 62 63 void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override; 64 65 bool ResumeNetworkInfo(); 66 67 private: 68 void StopNetDetection(); 69 bool CreateBasicNetwork(); 70 bool CreateVirtualNetwork(); 71 bool ReleaseBasicNetwork(); 72 bool ReleaseVirtualNetwork(); 73 void InitNetMonitor(); 74 void HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect); 75 void NotifyNetDetectionResult(NetDetectionResultCode detectionResult, const std::string &urlRedirect); 76 NetDetectionResultCode NetDetectionResultConvert(int32_t internalRet); 77 void SendConnectionChangedBroadcast(const NetConnState &netConnState) const; 78 void SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg); 79 void ResetNetlinkInfo(); 80 void NetDetectionForDnsHealthSync(bool dnsHealthSuccess); 81 bool IsDetectionForDnsSuccess(NetDetectionStatus netDetectionState, bool dnsHealthSuccess); 82 bool IsDetectionForDnsFail(NetDetectionStatus netDetectionState, bool dnsHealthSuccess); 83 84 private: 85 int32_t netId_ = 0; 86 uint32_t supplierId_ = 0; 87 NetLinkInfo netLinkInfo_; 88 NetConnState state_ = NET_CONN_STATE_UNKNOWN; 89 NetDetectionStatus detectResult_ = UNKNOWN_STATE; 90 std::atomic_bool isPhyNetCreated_ = false; 91 std::atomic_bool isVirtualCreated_ = false; 92 std::shared_ptr<NetMonitor> netMonitor_ = nullptr; 93 NetDetectionHandler netCallback_; 94 NetBearType netSupplierType_; 95 std::vector<sptr<INetDetectionCallback>> netDetectionRetCallback_; 96 std::shared_ptr<NetConnEventHandler> eventHandler_; 97 std::atomic<bool> isDetectingForDns_ = false; 98 }; 99 } // namespace NetManagerStandard 100 } // namespace OHOS 101 #endif // NETWORK_H 102