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 &netLinkInfo); 47 void UpdateInterfaces(const NetLinkInfo &netLinkInfo); 48 void UpdateRoutes(const NetLinkInfo &netLinkInfo); 49 void UpdateDns(const NetLinkInfo &netLinkInfo); 50 void UpdateMtu(const NetLinkInfo &netLinkInfo); 51 void RegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 52 int32_t UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 53 void StartNetDetection(bool needReport); 54 void SetDefaultNetWork(); 55 void ClearDefaultNetWorkNetId(); 56 bool IsConnecting() const; 57 bool IsConnected() const; 58 void UpdateNetConnState(NetConnState netConnState); 59 60 void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override; 61 62 private: 63 void StopNetDetection(); 64 bool CreateBasicNetwork(); 65 bool CreateVirtualNetwork(); 66 bool ReleaseBasicNetwork(); 67 bool ReleaseVirtualNetwork(); 68 void InitNetMonitor(); 69 void HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect); 70 void NotifyNetDetectionResult(NetDetectionResultCode detectionResult, const std::string &urlRedirect); 71 NetDetectionResultCode NetDetectionResultConvert(int32_t internalRet); 72 void SendConnectionChangedBroadcast(const NetConnState &netConnState) const; 73 void SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg); 74 void ResetNetlinkInfo(); 75 76 private: 77 int32_t netId_ = 0; 78 uint32_t supplierId_ = 0; 79 NetLinkInfo netLinkInfo_; 80 NetConnState state_ = NET_CONN_STATE_UNKNOWN; 81 NetDetectionStatus detectResult_ = UNKNOWN_STATE; 82 std::atomic_bool isPhyNetCreated_ = false; 83 std::atomic_bool isVirtualCreated_ = false; 84 std::shared_ptr<NetMonitor> netMonitor_ = nullptr; 85 NetDetectionHandler netCallback_; 86 NetBearType netSupplierType_; 87 std::vector<sptr<INetDetectionCallback>> netDetectionRetCallback_; 88 std::shared_ptr<NetConnEventHandler> eventHandler_; 89 }; 90 } // namespace NetManagerStandard 91 } // namespace OHOS 92 #endif // NETWORK_H 93