1 /* 2 * Copyright (c) 2021 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 "i_net_detection_callback.h" 20 #include "inet_addr.h" 21 #include "net_conn_types.h" 22 #include "net_link_info.h" 23 #include "net_monitor.h" 24 #include "route.h" 25 26 namespace OHOS { 27 namespace NetManagerStandard { 28 constexpr uint32_t INVALID_NET_ID = 0; 29 constexpr int32_t MIN_NET_ID = 100; 30 constexpr int32_t MAX_NET_ID = 0xFFFF - 0x400; 31 using NetDetectionHandler = std::function<void(uint32_t supplierId, bool ifValid)>; 32 class Network : public virtual RefBase { 33 public: 34 Network(int32_t netId, uint32_t supplierId, NetDetectionHandler handler); 35 ~Network(); 36 bool operator==(const Network &network) const; 37 int32_t GetNetId() const; 38 bool UpdateBasicNetwork(bool isAvailable_); 39 bool UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo); 40 NetLinkInfo GetNetLinkInfo() const; 41 void UpdateIpAddrs(const NetLinkInfo &netLinkInfo); 42 void UpdateInterfaces(const NetLinkInfo &netLinkInfo); 43 void UpdateRoutes(const NetLinkInfo &netLinkInfo); 44 void UpdateDnses(const NetLinkInfo &netLinkInfo); 45 void UpdateMtu(const NetLinkInfo &netLinkInfo); 46 void RegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 47 int32_t UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback); 48 void StartNetDetection(); 49 uint64_t GetNetWorkMonitorResult(); 50 void SetDefaultNetWork(); 51 void ClearDefaultNetWorkNetId(); 52 void SetExternDetection(); 53 54 private: 55 void StopNetDetection(); 56 bool CreateBasicNetwork(); 57 bool ReleaseBasicNetwork(); 58 void StartDetectionThread(); 59 void HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect); 60 void NotifyNetDetectionResult(NetDetectionResultCode detectionResult, const std::string &urlRedirect); 61 int32_t Ipv4PrefixLen(const std::string &ip); 62 NetDetectionResultCode NetDetectionResultConvert(int32_t internalRet); 63 64 private: 65 int32_t netId_ = 0; 66 uint32_t supplierId_ = 0; 67 NetLinkInfo netLinkInfo_; 68 bool isPhyNetCreated_ = false; 69 std::unique_ptr<NetMonitor> netMonitor_ = nullptr; 70 NetDetectionHandler netCallback_; 71 NetDetectionStatus netDetectionState_; 72 std::string urlRedirect_; 73 std::vector<sptr<INetDetectionCallback>> netDetectionRetCallback_; 74 bool isExternDetection_ = false; 75 }; 76 } // namespace NetManagerStandard 77 } // namespace OHOS 78 #endif // NETWORK_H