1b1b8bc3fSopenharmony_ci/* 2b1b8bc3fSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License. 5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at 6b1b8bc3fSopenharmony_ci * 7b1b8bc3fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8b1b8bc3fSopenharmony_ci * 9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and 13b1b8bc3fSopenharmony_ci * limitations under the License. 14b1b8bc3fSopenharmony_ci */ 15b1b8bc3fSopenharmony_ci 16b1b8bc3fSopenharmony_ci#include "common_event_support.h" 17b1b8bc3fSopenharmony_ci 18b1b8bc3fSopenharmony_ci#include "broadcast_manager.h" 19b1b8bc3fSopenharmony_ci#include "event_report.h" 20b1b8bc3fSopenharmony_ci#include "net_conn_service_iface.h" 21b1b8bc3fSopenharmony_ci#include "net_manager_constants.h" 22b1b8bc3fSopenharmony_ci#include "net_mgr_log_wrapper.h" 23b1b8bc3fSopenharmony_ci#include "net_stats_client.h" 24b1b8bc3fSopenharmony_ci#include "netmanager_base_common_utils.h" 25b1b8bc3fSopenharmony_ci#include "netsys_controller.h" 26b1b8bc3fSopenharmony_ci#include "network.h" 27b1b8bc3fSopenharmony_ci#include "route_utils.h" 28b1b8bc3fSopenharmony_ci#include "securec.h" 29b1b8bc3fSopenharmony_ci 30b1b8bc3fSopenharmony_ciusing namespace OHOS::NetManagerStandard::CommonUtils; 31b1b8bc3fSopenharmony_ci 32b1b8bc3fSopenharmony_cinamespace OHOS { 33b1b8bc3fSopenharmony_cinamespace NetManagerStandard { 34b1b8bc3fSopenharmony_cinamespace { 35b1b8bc3fSopenharmony_ci// hisysevent error messgae 36b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_CREATE_PHYSICAL_NETWORK_FAILED = "Create physical network failed, net id:"; 37b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_CREATE_VIRTUAL_NETWORK_FAILED = "Create virtual network failed, net id:"; 38b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_ADD_NET_INTERFACE_FAILED = "Add network interface failed"; 39b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_REMOVE_NET_INTERFACE_FAILED = "Remove network interface failed"; 40b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_DELETE_NET_IP_ADDR_FAILED = "Delete network ip address failed"; 41b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_ADD_NET_IP_ADDR_FAILED = "Add network ip address failed"; 42b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_REMOVE_NET_ROUTES_FAILED = "Remove network routes failed"; 43b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_ADD_NET_ROUTES_FAILED = "Add network routes failed"; 44b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_UPDATE_NET_ROUTES_FAILED = "Update netlink routes failed,routes list is empty"; 45b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_SET_NET_RESOLVER_FAILED = "Set network resolver config failed"; 46b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_UPDATE_NET_DNSES_FAILED = "Update netlink dns failed,dns list is empty"; 47b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_SET_NET_MTU_FAILED = "Set netlink interface mtu failed"; 48b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_SET_NET_TCP_BUFFER_SIZE_FAILED = "Set netlink tcp buffer size failed"; 49b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_UPDATE_STATS_CACHED = "force update kernel map stats cached failed"; 50b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_SET_DEFAULT_NETWORK_FAILED = "Set default network failed"; 51b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_CLEAR_DEFAULT_NETWORK_FAILED = "Clear default network failed"; 52b1b8bc3fSopenharmony_ciconstexpr const char *LOCAL_ROUTE_NEXT_HOP = "0.0.0.0"; 53b1b8bc3fSopenharmony_ciconstexpr const char *LOCAL_ROUTE_IPV6_DESTINATION = "::"; 54b1b8bc3fSopenharmony_ciconstexpr int32_t ERRNO_EADDRNOTAVAIL = -99; 55b1b8bc3fSopenharmony_ci} // namespace 56b1b8bc3fSopenharmony_ci 57b1b8bc3fSopenharmony_ciNetwork::Network(int32_t netId, uint32_t supplierId, const NetDetectionHandler &handler, NetBearType bearerType, 58b1b8bc3fSopenharmony_ci const std::shared_ptr<NetConnEventHandler> &eventHandler) 59b1b8bc3fSopenharmony_ci : netId_(netId), 60b1b8bc3fSopenharmony_ci supplierId_(supplierId), 61b1b8bc3fSopenharmony_ci netCallback_(handler), 62b1b8bc3fSopenharmony_ci netSupplierType_(bearerType), 63b1b8bc3fSopenharmony_ci eventHandler_(eventHandler) 64b1b8bc3fSopenharmony_ci{ 65b1b8bc3fSopenharmony_ci} 66b1b8bc3fSopenharmony_ci 67b1b8bc3fSopenharmony_ciint32_t Network::GetNetId() const 68b1b8bc3fSopenharmony_ci{ 69b1b8bc3fSopenharmony_ci return netId_; 70b1b8bc3fSopenharmony_ci} 71b1b8bc3fSopenharmony_ci 72b1b8bc3fSopenharmony_ciuint32_t Network::GetSupplierId() const 73b1b8bc3fSopenharmony_ci{ 74b1b8bc3fSopenharmony_ci return supplierId_; 75b1b8bc3fSopenharmony_ci} 76b1b8bc3fSopenharmony_ci 77b1b8bc3fSopenharmony_cibool Network::operator==(const Network &network) const 78b1b8bc3fSopenharmony_ci{ 79b1b8bc3fSopenharmony_ci return netId_ == network.netId_; 80b1b8bc3fSopenharmony_ci} 81b1b8bc3fSopenharmony_ci 82b1b8bc3fSopenharmony_cibool Network::UpdateBasicNetwork(bool isAvailable_) 83b1b8bc3fSopenharmony_ci{ 84b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Enter UpdateBasicNetwork"); 85b1b8bc3fSopenharmony_ci if (isAvailable_) { 86b1b8bc3fSopenharmony_ci if (netSupplierType_ == BEARER_VPN) { 87b1b8bc3fSopenharmony_ci return CreateVirtualNetwork(); 88b1b8bc3fSopenharmony_ci } 89b1b8bc3fSopenharmony_ci return CreateBasicNetwork(); 90b1b8bc3fSopenharmony_ci } else { 91b1b8bc3fSopenharmony_ci if (netSupplierType_ == BEARER_VPN) { 92b1b8bc3fSopenharmony_ci return ReleaseVirtualNetwork(); 93b1b8bc3fSopenharmony_ci } 94b1b8bc3fSopenharmony_ci if (nat464Service_ != nullptr) { 95b1b8bc3fSopenharmony_ci nat464Service_->UpdateService(NAT464_SERVICE_STOP); 96b1b8bc3fSopenharmony_ci } 97b1b8bc3fSopenharmony_ci return ReleaseBasicNetwork(); 98b1b8bc3fSopenharmony_ci } 99b1b8bc3fSopenharmony_ci} 100b1b8bc3fSopenharmony_ci 101b1b8bc3fSopenharmony_cibool Network::CreateBasicNetwork() 102b1b8bc3fSopenharmony_ci{ 103b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Enter CreateBasicNetwork"); 104b1b8bc3fSopenharmony_ci if (!isPhyNetCreated_) { 105b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Create physical network"); 106b1b8bc3fSopenharmony_ci // Create a physical network 107b1b8bc3fSopenharmony_ci if (NetsysController::GetInstance().NetworkCreatePhysical(netId_, 0) != NETMANAGER_SUCCESS) { 108b1b8bc3fSopenharmony_ci std::string errMsg = std::string(ERROR_MSG_CREATE_PHYSICAL_NETWORK_FAILED).append(std::to_string(netId_)); 109b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_CREATE_PHYSICAL_NETWORK_FAILED, errMsg); 110b1b8bc3fSopenharmony_ci } 111b1b8bc3fSopenharmony_ci NetsysController::GetInstance().CreateNetworkCache(netId_); 112b1b8bc3fSopenharmony_ci isPhyNetCreated_ = true; 113b1b8bc3fSopenharmony_ci } 114b1b8bc3fSopenharmony_ci return true; 115b1b8bc3fSopenharmony_ci} 116b1b8bc3fSopenharmony_ci 117b1b8bc3fSopenharmony_cibool Network::CreateVirtualNetwork() 118b1b8bc3fSopenharmony_ci{ 119b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Enter create virtual network"); 120b1b8bc3fSopenharmony_ci if (!isVirtualCreated_) { 121b1b8bc3fSopenharmony_ci // Create a virtual network here 122b1b8bc3fSopenharmony_ci bool hasDns = netLinkInfo_.dnsList_.size() ? true : false; 123b1b8bc3fSopenharmony_ci if (NetsysController::GetInstance().NetworkCreateVirtual(netId_, hasDns) != NETMANAGER_SUCCESS) { 124b1b8bc3fSopenharmony_ci std::string errMsg = std::string(ERROR_MSG_CREATE_VIRTUAL_NETWORK_FAILED).append(std::to_string(netId_)); 125b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_CREATE_VIRTUAL_NETWORK_FAILED, errMsg); 126b1b8bc3fSopenharmony_ci } 127b1b8bc3fSopenharmony_ci NetsysController::GetInstance().CreateNetworkCache(netId_); 128b1b8bc3fSopenharmony_ci isVirtualCreated_ = true; 129b1b8bc3fSopenharmony_ci } 130b1b8bc3fSopenharmony_ci return true; 131b1b8bc3fSopenharmony_ci} 132b1b8bc3fSopenharmony_ci 133b1b8bc3fSopenharmony_cibool Network::IsIfaceNameInUse() 134b1b8bc3fSopenharmony_ci{ 135b1b8bc3fSopenharmony_ci return NetConnServiceIface().IsIfaceNameInUse(netLinkInfo_.ifaceName_, netId_); 136b1b8bc3fSopenharmony_ci} 137b1b8bc3fSopenharmony_ci 138b1b8bc3fSopenharmony_cistd::string Network::GetNetCapabilitiesAsString(const uint32_t supplierId) const 139b1b8bc3fSopenharmony_ci{ 140b1b8bc3fSopenharmony_ci return NetConnServiceIface().GetNetCapabilitiesAsString(supplierId); 141b1b8bc3fSopenharmony_ci} 142b1b8bc3fSopenharmony_ci 143b1b8bc3fSopenharmony_cibool Network::ReleaseBasicNetwork() 144b1b8bc3fSopenharmony_ci{ 145b1b8bc3fSopenharmony_ci if (!isPhyNetCreated_) { 146b1b8bc3fSopenharmony_ci NETMGR_LOG_E("physical network has not created"); 147b1b8bc3fSopenharmony_ci return true; 148b1b8bc3fSopenharmony_ci } 149b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Destroy physical network"); 150b1b8bc3fSopenharmony_ci StopNetDetection(); 151b1b8bc3fSopenharmony_ci std::string netCapabilities = GetNetCapabilitiesAsString(supplierId_); 152b1b8bc3fSopenharmony_ci NETMGR_LOG_D("ReleaseBasicNetwork supplierId %{public}u, netId %{public}d, netCapabilities %{public}s", 153b1b8bc3fSopenharmony_ci supplierId_, netId_, netCapabilities.c_str()); 154b1b8bc3fSopenharmony_ci if (!IsIfaceNameInUse() || isNeedResume_) { 155b1b8bc3fSopenharmony_ci for (const auto &inetAddr : netLinkInfo_.netAddrList_) { 156b1b8bc3fSopenharmony_ci int32_t prefixLen = inetAddr.prefixlen_ == 0 ? Ipv4PrefixLen(inetAddr.netMask_) : inetAddr.prefixlen_; 157b1b8bc3fSopenharmony_ci NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, 158b1b8bc3fSopenharmony_ci prefixLen); 159b1b8bc3fSopenharmony_ci } 160b1b8bc3fSopenharmony_ci for (const auto &route : netLinkInfo_.routeList_) { 161b1b8bc3fSopenharmony_ci if (route.destination_.address_ != LOCAL_ROUTE_NEXT_HOP && 162b1b8bc3fSopenharmony_ci route.destination_.address_ != LOCAL_ROUTE_IPV6_DESTINATION) { 163b1b8bc3fSopenharmony_ci auto family = GetAddrFamily(route.destination_.address_); 164b1b8bc3fSopenharmony_ci std::string nextHop = (family == AF_INET6) ? "" : LOCAL_ROUTE_NEXT_HOP; 165b1b8bc3fSopenharmony_ci auto destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_); 166b1b8bc3fSopenharmony_ci NetsysController::GetInstance().NetworkRemoveRoute(LOCAL_NET_ID, route.iface_, destAddress, nextHop); 167b1b8bc3fSopenharmony_ci } 168b1b8bc3fSopenharmony_ci } 169b1b8bc3fSopenharmony_ci isNeedResume_ = false; 170b1b8bc3fSopenharmony_ci } else { 171b1b8bc3fSopenharmony_ci for (const auto &inetAddr : netLinkInfo_.netAddrList_) { 172b1b8bc3fSopenharmony_ci int32_t prefixLen = inetAddr.prefixlen_ == 0 ? Ipv4PrefixLen(inetAddr.netMask_) : inetAddr.prefixlen_; 173b1b8bc3fSopenharmony_ci NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, 174b1b8bc3fSopenharmony_ci prefixLen, netCapabilities); 175b1b8bc3fSopenharmony_ci } 176b1b8bc3fSopenharmony_ci } 177b1b8bc3fSopenharmony_ci for (const auto &route : netLinkInfo_.routeList_) { 178b1b8bc3fSopenharmony_ci auto destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_); 179b1b8bc3fSopenharmony_ci NetsysController::GetInstance().NetworkRemoveRoute(netId_, route.iface_, destAddress, 180b1b8bc3fSopenharmony_ci route.gateway_.address_); 181b1b8bc3fSopenharmony_ci } 182b1b8bc3fSopenharmony_ci NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_); 183b1b8bc3fSopenharmony_ci NetsysController::GetInstance().NetworkDestroy(netId_); 184b1b8bc3fSopenharmony_ci NetsysController::GetInstance().DestroyNetworkCache(netId_); 185b1b8bc3fSopenharmony_ci netLinkInfo_.Initialize(); 186b1b8bc3fSopenharmony_ci isPhyNetCreated_ = false; 187b1b8bc3fSopenharmony_ci return true; 188b1b8bc3fSopenharmony_ci} 189b1b8bc3fSopenharmony_ci 190b1b8bc3fSopenharmony_cibool Network::ReleaseVirtualNetwork() 191b1b8bc3fSopenharmony_ci{ 192b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Enter release virtual network"); 193b1b8bc3fSopenharmony_ci if (isVirtualCreated_) { 194b1b8bc3fSopenharmony_ci for (const auto &inetAddr : netLinkInfo_.netAddrList_) { 195b1b8bc3fSopenharmony_ci int32_t prefixLen = inetAddr.prefixlen_; 196b1b8bc3fSopenharmony_ci if (prefixLen == 0) { 197b1b8bc3fSopenharmony_ci prefixLen = Ipv4PrefixLen(inetAddr.netMask_); 198b1b8bc3fSopenharmony_ci } 199b1b8bc3fSopenharmony_ci NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, prefixLen); 200b1b8bc3fSopenharmony_ci } 201b1b8bc3fSopenharmony_ci NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_); 202b1b8bc3fSopenharmony_ci NetsysController::GetInstance().NetworkDestroy(netId_); 203b1b8bc3fSopenharmony_ci NetsysController::GetInstance().DestroyNetworkCache(netId_); 204b1b8bc3fSopenharmony_ci netLinkInfo_.Initialize(); 205b1b8bc3fSopenharmony_ci isVirtualCreated_ = false; 206b1b8bc3fSopenharmony_ci } 207b1b8bc3fSopenharmony_ci return true; 208b1b8bc3fSopenharmony_ci} 209b1b8bc3fSopenharmony_ci 210b1b8bc3fSopenharmony_cibool Network::UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo) 211b1b8bc3fSopenharmony_ci{ 212b1b8bc3fSopenharmony_ci NETMGR_LOG_D("update net link information process"); 213b1b8bc3fSopenharmony_ci UpdateStatsCached(netLinkInfo); 214b1b8bc3fSopenharmony_ci UpdateInterfaces(netLinkInfo); 215b1b8bc3fSopenharmony_ci bool isIfaceNameInUse = NetConnServiceIface().IsIfaceNameInUse(netLinkInfo.ifaceName_, netId_); 216b1b8bc3fSopenharmony_ci if (!isIfaceNameInUse || netCaps_.find(NetCap::NET_CAPABILITY_INTERNET) != netCaps_.end()) { 217b1b8bc3fSopenharmony_ci UpdateIpAddrs(netLinkInfo); 218b1b8bc3fSopenharmony_ci } 219b1b8bc3fSopenharmony_ci UpdateRoutes(netLinkInfo); 220b1b8bc3fSopenharmony_ci UpdateDns(netLinkInfo); 221b1b8bc3fSopenharmony_ci UpdateMtu(netLinkInfo); 222b1b8bc3fSopenharmony_ci UpdateTcpBufferSize(netLinkInfo); 223b1b8bc3fSopenharmony_ci 224b1b8bc3fSopenharmony_ci netLinkInfo_ = netLinkInfo; 225b1b8bc3fSopenharmony_ci if (IsNat464Prefered()) { 226b1b8bc3fSopenharmony_ci if (nat464Service_ == nullptr) { 227b1b8bc3fSopenharmony_ci nat464Service_ = std::make_unique<Nat464Service>(netId_, netLinkInfo_.ifaceName_); 228b1b8bc3fSopenharmony_ci } 229b1b8bc3fSopenharmony_ci nat464Service_->MaybeUpdateV6Iface(netLinkInfo_.ifaceName_); 230b1b8bc3fSopenharmony_ci nat464Service_->UpdateService(NAT464_SERVICE_CONTINUE); 231b1b8bc3fSopenharmony_ci } else if (nat464Service_ != nullptr) { 232b1b8bc3fSopenharmony_ci nat464Service_->UpdateService(NAT464_SERVICE_STOP); 233b1b8bc3fSopenharmony_ci } 234b1b8bc3fSopenharmony_ci 235b1b8bc3fSopenharmony_ci if (netSupplierType_ != BEARER_VPN && netCaps_.find(NetCap::NET_CAPABILITY_INTERNET) != netCaps_.end()) { 236b1b8bc3fSopenharmony_ci StartNetDetection(false); 237b1b8bc3fSopenharmony_ci } 238b1b8bc3fSopenharmony_ci return true; 239b1b8bc3fSopenharmony_ci} 240b1b8bc3fSopenharmony_ci 241b1b8bc3fSopenharmony_ciNetLinkInfo Network::GetNetLinkInfo() const 242b1b8bc3fSopenharmony_ci{ 243b1b8bc3fSopenharmony_ci NetLinkInfo linkInfo = netLinkInfo_; 244b1b8bc3fSopenharmony_ci for (auto iter = linkInfo.routeList_.begin(); iter != linkInfo.routeList_.end();) { 245b1b8bc3fSopenharmony_ci if (iter->destination_.address_ == LOCAL_ROUTE_NEXT_HOP || 246b1b8bc3fSopenharmony_ci iter->destination_.address_ == LOCAL_ROUTE_IPV6_DESTINATION) { 247b1b8bc3fSopenharmony_ci ++iter; 248b1b8bc3fSopenharmony_ci continue; 249b1b8bc3fSopenharmony_ci } 250b1b8bc3fSopenharmony_ci iter = linkInfo.routeList_.erase(iter); 251b1b8bc3fSopenharmony_ci } 252b1b8bc3fSopenharmony_ci return linkInfo; 253b1b8bc3fSopenharmony_ci} 254b1b8bc3fSopenharmony_ci 255b1b8bc3fSopenharmony_cistd::string Network::GetIfaceName() const 256b1b8bc3fSopenharmony_ci{ 257b1b8bc3fSopenharmony_ci return netLinkInfo_.ifaceName_; 258b1b8bc3fSopenharmony_ci} 259b1b8bc3fSopenharmony_ci 260b1b8bc3fSopenharmony_cistd::string Network::GetIdent() const 261b1b8bc3fSopenharmony_ci{ 262b1b8bc3fSopenharmony_ci return netLinkInfo_.ident_; 263b1b8bc3fSopenharmony_ci} 264b1b8bc3fSopenharmony_ci 265b1b8bc3fSopenharmony_civoid Network::UpdateInterfaces(const NetLinkInfo &newNetLinkInfo) 266b1b8bc3fSopenharmony_ci{ 267b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateInterfaces in."); 268b1b8bc3fSopenharmony_ci if (newNetLinkInfo.ifaceName_ == netLinkInfo_.ifaceName_) { 269b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateInterfaces out. same with before."); 270b1b8bc3fSopenharmony_ci return; 271b1b8bc3fSopenharmony_ci } 272b1b8bc3fSopenharmony_ci 273b1b8bc3fSopenharmony_ci int32_t ret = NETMANAGER_SUCCESS; 274b1b8bc3fSopenharmony_ci // Call netsys to add and remove interface 275b1b8bc3fSopenharmony_ci if (!newNetLinkInfo.ifaceName_.empty()) { 276b1b8bc3fSopenharmony_ci ret = NetsysController::GetInstance().NetworkAddInterface(netId_, newNetLinkInfo.ifaceName_, netSupplierType_); 277b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS) { 278b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_ADD_NET_INTERFACE_FAILED); 279b1b8bc3fSopenharmony_ci } 280b1b8bc3fSopenharmony_ci } 281b1b8bc3fSopenharmony_ci if (!netLinkInfo_.ifaceName_.empty()) { 282b1b8bc3fSopenharmony_ci ret = NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_); 283b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS) { 284b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_REMOVE_NET_INTERFACE_FAILED); 285b1b8bc3fSopenharmony_ci } 286b1b8bc3fSopenharmony_ci } 287b1b8bc3fSopenharmony_ci netLinkInfo_.ifaceName_ = newNetLinkInfo.ifaceName_; 288b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateInterfaces out."); 289b1b8bc3fSopenharmony_ci} 290b1b8bc3fSopenharmony_ci 291b1b8bc3fSopenharmony_civoid Network::UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) 292b1b8bc3fSopenharmony_ci{ 293b1b8bc3fSopenharmony_ci // netLinkInfo_ represents the old, netLinkInfo represents the new 294b1b8bc3fSopenharmony_ci // Update: remove old Ips first, then add the new Ips 295b1b8bc3fSopenharmony_ci NETMGR_LOG_I("UpdateIpAddrs, old ip addrs size: [%{public}zu]", netLinkInfo_.netAddrList_.size()); 296b1b8bc3fSopenharmony_ci for (const auto &inetAddr : netLinkInfo_.netAddrList_) { 297b1b8bc3fSopenharmony_ci if (newNetLinkInfo.HasNetAddr(inetAddr)) { 298b1b8bc3fSopenharmony_ci NETMGR_LOG_W("Same ip address:[%{public}s], there is not need to be deleted", 299b1b8bc3fSopenharmony_ci CommonUtils::ToAnonymousIp(inetAddr.address_).c_str()); 300b1b8bc3fSopenharmony_ci continue; 301b1b8bc3fSopenharmony_ci } 302b1b8bc3fSopenharmony_ci auto family = GetAddrFamily(inetAddr.address_); 303b1b8bc3fSopenharmony_ci auto prefixLen = inetAddr.prefixlen_ ? static_cast<int32_t>(inetAddr.prefixlen_) 304b1b8bc3fSopenharmony_ci : ((family == AF_INET6) ? Ipv6PrefixLen(inetAddr.netMask_) 305b1b8bc3fSopenharmony_ci : Ipv4PrefixLen(inetAddr.netMask_)); 306b1b8bc3fSopenharmony_ci int32_t ret = 307b1b8bc3fSopenharmony_ci NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, prefixLen); 308b1b8bc3fSopenharmony_ci if (NETMANAGER_SUCCESS != ret) { 309b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_DELETE_NET_IP_ADDR_FAILED); 310b1b8bc3fSopenharmony_ci } 311b1b8bc3fSopenharmony_ci 312b1b8bc3fSopenharmony_ci if ((ret == ERRNO_EADDRNOTAVAIL) || (ret == 0)) { 313b1b8bc3fSopenharmony_ci NETMGR_LOG_W("remove route info of ip address:[%{public}s]", 314b1b8bc3fSopenharmony_ci CommonUtils::ToAnonymousIp(inetAddr.address_).c_str()); 315b1b8bc3fSopenharmony_ci netLinkInfo_.routeList_.remove_if([family](const Route &route) { 316b1b8bc3fSopenharmony_ci INetAddr::IpType addrFamily = INetAddr::IpType::UNKNOWN; 317b1b8bc3fSopenharmony_ci if (family == AF_INET) { 318b1b8bc3fSopenharmony_ci addrFamily = INetAddr::IpType::IPV4; 319b1b8bc3fSopenharmony_ci } else if (family == AF_INET6) { 320b1b8bc3fSopenharmony_ci addrFamily = INetAddr::IpType::IPV6; 321b1b8bc3fSopenharmony_ci } 322b1b8bc3fSopenharmony_ci return route.destination_.type_ == addrFamily; 323b1b8bc3fSopenharmony_ci }); 324b1b8bc3fSopenharmony_ci } 325b1b8bc3fSopenharmony_ci } 326b1b8bc3fSopenharmony_ci 327b1b8bc3fSopenharmony_ci HandleUpdateIpAddrs(newNetLinkInfo); 328b1b8bc3fSopenharmony_ci} 329b1b8bc3fSopenharmony_ci 330b1b8bc3fSopenharmony_civoid Network::HandleUpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) 331b1b8bc3fSopenharmony_ci{ 332b1b8bc3fSopenharmony_ci NETMGR_LOG_I("HandleUpdateIpAddrs, new ip addrs size: [%{public}zu]", newNetLinkInfo.netAddrList_.size()); 333b1b8bc3fSopenharmony_ci for (const auto &inetAddr : newNetLinkInfo.netAddrList_) { 334b1b8bc3fSopenharmony_ci if (netLinkInfo_.HasNetAddr(inetAddr)) { 335b1b8bc3fSopenharmony_ci NETMGR_LOG_W("Same ip address:[%{public}s], there is no need to add it again", 336b1b8bc3fSopenharmony_ci CommonUtils::ToAnonymousIp(inetAddr.address_).c_str()); 337b1b8bc3fSopenharmony_ci continue; 338b1b8bc3fSopenharmony_ci } 339b1b8bc3fSopenharmony_ci auto family = GetAddrFamily(inetAddr.address_); 340b1b8bc3fSopenharmony_ci auto prefixLen = inetAddr.prefixlen_ ? static_cast<int32_t>(inetAddr.prefixlen_) 341b1b8bc3fSopenharmony_ci : ((family == AF_INET6) ? Ipv6PrefixLen(inetAddr.netMask_) 342b1b8bc3fSopenharmony_ci : Ipv4PrefixLen(inetAddr.netMask_)); 343b1b8bc3fSopenharmony_ci if (NETMANAGER_SUCCESS != NetsysController::GetInstance().AddInterfaceAddress(newNetLinkInfo.ifaceName_, 344b1b8bc3fSopenharmony_ci inetAddr.address_, prefixLen)) { 345b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_ADD_NET_IP_ADDR_FAILED); 346b1b8bc3fSopenharmony_ci } 347b1b8bc3fSopenharmony_ci } 348b1b8bc3fSopenharmony_ci} 349b1b8bc3fSopenharmony_ci 350b1b8bc3fSopenharmony_civoid Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) 351b1b8bc3fSopenharmony_ci{ 352b1b8bc3fSopenharmony_ci // netLinkInfo_ contains the old routes info, netLinkInfo contains the new routes info 353b1b8bc3fSopenharmony_ci // Update: remove old routes first, then add the new routes 354b1b8bc3fSopenharmony_ci NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str()); 355b1b8bc3fSopenharmony_ci for (const auto &route : netLinkInfo_.routeList_) { 356b1b8bc3fSopenharmony_ci if (newNetLinkInfo.HasRoute(route)) { 357b1b8bc3fSopenharmony_ci NETMGR_LOG_W("Same route:[%{public}s] ifo, there is not need to be deleted", 358b1b8bc3fSopenharmony_ci CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); 359b1b8bc3fSopenharmony_ci continue; 360b1b8bc3fSopenharmony_ci } 361b1b8bc3fSopenharmony_ci std::string destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_); 362b1b8bc3fSopenharmony_ci auto ret = NetsysController::GetInstance().NetworkRemoveRoute(netId_, route.iface_, destAddress, 363b1b8bc3fSopenharmony_ci route.gateway_.address_); 364b1b8bc3fSopenharmony_ci int32_t res = NETMANAGER_SUCCESS; 365b1b8bc3fSopenharmony_ci if (netSupplierType_ != BEARER_VPN && route.destination_.address_ != LOCAL_ROUTE_NEXT_HOP && 366b1b8bc3fSopenharmony_ci route.destination_.address_ != LOCAL_ROUTE_IPV6_DESTINATION) { 367b1b8bc3fSopenharmony_ci auto family = GetAddrFamily(route.destination_.address_); 368b1b8bc3fSopenharmony_ci std::string nextHop = (family == AF_INET6) ? "" : LOCAL_ROUTE_NEXT_HOP; 369b1b8bc3fSopenharmony_ci res = NetsysController::GetInstance().NetworkRemoveRoute(LOCAL_NET_ID, route.iface_, destAddress, nextHop); 370b1b8bc3fSopenharmony_ci } 371b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS || res != NETMANAGER_SUCCESS) { 372b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_REMOVE_NET_ROUTES_FAILED); 373b1b8bc3fSopenharmony_ci } 374b1b8bc3fSopenharmony_ci } 375b1b8bc3fSopenharmony_ci 376b1b8bc3fSopenharmony_ci NETMGR_LOG_D("UpdateRoutes, new routes: [%{public}s]", newNetLinkInfo.ToStringRoute("").c_str()); 377b1b8bc3fSopenharmony_ci for (const auto &route : newNetLinkInfo.routeList_) { 378b1b8bc3fSopenharmony_ci if (netLinkInfo_.HasRoute(route)) { 379b1b8bc3fSopenharmony_ci NETMGR_LOG_W("Same route:[%{public}s] ifo, there is no need to add it again", 380b1b8bc3fSopenharmony_ci CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); 381b1b8bc3fSopenharmony_ci continue; 382b1b8bc3fSopenharmony_ci } 383b1b8bc3fSopenharmony_ci 384b1b8bc3fSopenharmony_ci std::string destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_); 385b1b8bc3fSopenharmony_ci auto ret = 386b1b8bc3fSopenharmony_ci NetsysController::GetInstance().NetworkAddRoute(netId_, route.iface_, destAddress, route.gateway_.address_); 387b1b8bc3fSopenharmony_ci int32_t res = NETMANAGER_SUCCESS; 388b1b8bc3fSopenharmony_ci if (netSupplierType_ != BEARER_VPN && route.destination_.address_ != LOCAL_ROUTE_NEXT_HOP && 389b1b8bc3fSopenharmony_ci route.destination_.address_ != LOCAL_ROUTE_IPV6_DESTINATION) { 390b1b8bc3fSopenharmony_ci auto family = GetAddrFamily(route.destination_.address_); 391b1b8bc3fSopenharmony_ci std::string nextHop = (family == AF_INET6) ? "" : LOCAL_ROUTE_NEXT_HOP; 392b1b8bc3fSopenharmony_ci res = NetsysController::GetInstance().NetworkAddRoute(LOCAL_NET_ID, route.iface_, destAddress, nextHop); 393b1b8bc3fSopenharmony_ci } 394b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS || res != NETMANAGER_SUCCESS) { 395b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_ADD_NET_ROUTES_FAILED); 396b1b8bc3fSopenharmony_ci } 397b1b8bc3fSopenharmony_ci } 398b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateRoutes out."); 399b1b8bc3fSopenharmony_ci if (newNetLinkInfo.routeList_.empty()) { 400b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_UPDATE_NET_ROUTES_FAILED); 401b1b8bc3fSopenharmony_ci } 402b1b8bc3fSopenharmony_ci} 403b1b8bc3fSopenharmony_ci 404b1b8bc3fSopenharmony_civoid Network::UpdateDns(const NetLinkInfo &netLinkInfo) 405b1b8bc3fSopenharmony_ci{ 406b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateDns in."); 407b1b8bc3fSopenharmony_ci std::vector<std::string> servers; 408b1b8bc3fSopenharmony_ci std::vector<std::string> domains; 409b1b8bc3fSopenharmony_ci std::stringstream ss; 410b1b8bc3fSopenharmony_ci for (const auto &dns : netLinkInfo.dnsList_) { 411b1b8bc3fSopenharmony_ci servers.emplace_back(dns.address_); 412b1b8bc3fSopenharmony_ci domains.emplace_back(dns.hostName_); 413b1b8bc3fSopenharmony_ci ss << '[' << CommonUtils::ToAnonymousIp(dns.address_).c_str() << ']'; 414b1b8bc3fSopenharmony_ci } 415b1b8bc3fSopenharmony_ci NETMGR_LOG_I("update dns server: %{public}s", ss.str().c_str()); 416b1b8bc3fSopenharmony_ci // Call netsys to set dns, use default timeout and retry 417b1b8bc3fSopenharmony_ci int32_t ret = NetsysController::GetInstance().SetResolverConfig(netId_, 0, 0, servers, domains); 418b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS) { 419b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_SET_NET_RESOLVER_FAILED); 420b1b8bc3fSopenharmony_ci } 421b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateDns out."); 422b1b8bc3fSopenharmony_ci if (netLinkInfo.dnsList_.empty()) { 423b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_UPDATE_NET_DNSES_FAILED); 424b1b8bc3fSopenharmony_ci } 425b1b8bc3fSopenharmony_ci} 426b1b8bc3fSopenharmony_ci 427b1b8bc3fSopenharmony_civoid Network::UpdateMtu(const NetLinkInfo &netLinkInfo) 428b1b8bc3fSopenharmony_ci{ 429b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateMtu in."); 430b1b8bc3fSopenharmony_ci if (netLinkInfo.mtu_ == netLinkInfo_.mtu_) { 431b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateMtu out. same with before."); 432b1b8bc3fSopenharmony_ci return; 433b1b8bc3fSopenharmony_ci } 434b1b8bc3fSopenharmony_ci 435b1b8bc3fSopenharmony_ci int32_t ret = NetsysController::GetInstance().SetInterfaceMtu(netLinkInfo.ifaceName_, netLinkInfo.mtu_); 436b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS) { 437b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_SET_NET_MTU_FAILED); 438b1b8bc3fSopenharmony_ci } 439b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateMtu out."); 440b1b8bc3fSopenharmony_ci} 441b1b8bc3fSopenharmony_ci 442b1b8bc3fSopenharmony_civoid Network::UpdateTcpBufferSize(const NetLinkInfo &netLinkInfo) 443b1b8bc3fSopenharmony_ci{ 444b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateTcpBufferSize in."); 445b1b8bc3fSopenharmony_ci if (netLinkInfo.tcpBufferSizes_ == netLinkInfo_.tcpBufferSizes_) { 446b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateTcpBufferSize out. same with before."); 447b1b8bc3fSopenharmony_ci return; 448b1b8bc3fSopenharmony_ci } 449b1b8bc3fSopenharmony_ci int32_t ret = NetsysController::GetInstance().SetTcpBufferSizes(netLinkInfo.tcpBufferSizes_); 450b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS) { 451b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_SET_NET_TCP_BUFFER_SIZE_FAILED); 452b1b8bc3fSopenharmony_ci } 453b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateTcpBufferSize out."); 454b1b8bc3fSopenharmony_ci} 455b1b8bc3fSopenharmony_ci 456b1b8bc3fSopenharmony_civoid Network::UpdateStatsCached(const NetLinkInfo &netLinkInfo) 457b1b8bc3fSopenharmony_ci{ 458b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateStatsCached in."); 459b1b8bc3fSopenharmony_ci if (netLinkInfo.ifaceName_ == netLinkInfo_.ifaceName_ && netLinkInfo.ident_ == netLinkInfo_.ident_) { 460b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateStatsCached out. same with before"); 461b1b8bc3fSopenharmony_ci return; 462b1b8bc3fSopenharmony_ci } 463b1b8bc3fSopenharmony_ci int32_t ret = NetStatsClient::GetInstance().UpdateStatsData(); 464b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS) { 465b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_UPDATE_STATS_CACHED); 466b1b8bc3fSopenharmony_ci } 467b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Network UpdateStatsCached out."); 468b1b8bc3fSopenharmony_ci} 469b1b8bc3fSopenharmony_ci 470b1b8bc3fSopenharmony_civoid Network::RegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback) 471b1b8bc3fSopenharmony_ci{ 472b1b8bc3fSopenharmony_ci NETMGR_LOG_I("Enter RNDCB"); 473b1b8bc3fSopenharmony_ci if (callback == nullptr) { 474b1b8bc3fSopenharmony_ci NETMGR_LOG_E("The parameter callback is null"); 475b1b8bc3fSopenharmony_ci return; 476b1b8bc3fSopenharmony_ci } 477b1b8bc3fSopenharmony_ci 478b1b8bc3fSopenharmony_ci for (const auto &iter : netDetectionRetCallback_) { 479b1b8bc3fSopenharmony_ci if (callback->AsObject().GetRefPtr() == iter->AsObject().GetRefPtr()) { 480b1b8bc3fSopenharmony_ci NETMGR_LOG_D("netDetectionRetCallback_ had this callback"); 481b1b8bc3fSopenharmony_ci return; 482b1b8bc3fSopenharmony_ci } 483b1b8bc3fSopenharmony_ci } 484b1b8bc3fSopenharmony_ci 485b1b8bc3fSopenharmony_ci netDetectionRetCallback_.emplace_back(callback); 486b1b8bc3fSopenharmony_ci} 487b1b8bc3fSopenharmony_ci 488b1b8bc3fSopenharmony_ciint32_t Network::UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback) 489b1b8bc3fSopenharmony_ci{ 490b1b8bc3fSopenharmony_ci NETMGR_LOG_I("Enter URNDCB"); 491b1b8bc3fSopenharmony_ci if (callback == nullptr) { 492b1b8bc3fSopenharmony_ci NETMGR_LOG_E("The parameter of callback is null"); 493b1b8bc3fSopenharmony_ci return NETMANAGER_ERR_LOCAL_PTR_NULL; 494b1b8bc3fSopenharmony_ci } 495b1b8bc3fSopenharmony_ci 496b1b8bc3fSopenharmony_ci for (auto iter = netDetectionRetCallback_.begin(); iter != netDetectionRetCallback_.end(); ++iter) { 497b1b8bc3fSopenharmony_ci if (callback->AsObject().GetRefPtr() == (*iter)->AsObject().GetRefPtr()) { 498b1b8bc3fSopenharmony_ci netDetectionRetCallback_.erase(iter); 499b1b8bc3fSopenharmony_ci return NETMANAGER_SUCCESS; 500b1b8bc3fSopenharmony_ci } 501b1b8bc3fSopenharmony_ci } 502b1b8bc3fSopenharmony_ci 503b1b8bc3fSopenharmony_ci return NETMANAGER_SUCCESS; 504b1b8bc3fSopenharmony_ci} 505b1b8bc3fSopenharmony_ci 506b1b8bc3fSopenharmony_civoid Network::StartNetDetection(bool needReport) 507b1b8bc3fSopenharmony_ci{ 508b1b8bc3fSopenharmony_ci NETMGR_LOG_I("Enter StartNetDetection"); 509b1b8bc3fSopenharmony_ci#ifdef FEATURE_SUPPORT_POWERMANAGER 510b1b8bc3fSopenharmony_ci if (forbidDetectionFlag_) { 511b1b8bc3fSopenharmony_ci NETMGR_LOG_W("Sleep status, forbid detection"); 512b1b8bc3fSopenharmony_ci return; 513b1b8bc3fSopenharmony_ci } 514b1b8bc3fSopenharmony_ci#endif 515b1b8bc3fSopenharmony_ci if (needReport || netMonitor_) { 516b1b8bc3fSopenharmony_ci StopNetDetection(); 517b1b8bc3fSopenharmony_ci InitNetMonitor(); 518b1b8bc3fSopenharmony_ci return; 519b1b8bc3fSopenharmony_ci } 520b1b8bc3fSopenharmony_ci if (!netMonitor_) { 521b1b8bc3fSopenharmony_ci NETMGR_LOG_I("netMonitor_ is null."); 522b1b8bc3fSopenharmony_ci InitNetMonitor(); 523b1b8bc3fSopenharmony_ci return; 524b1b8bc3fSopenharmony_ci } 525b1b8bc3fSopenharmony_ci} 526b1b8bc3fSopenharmony_ci 527b1b8bc3fSopenharmony_ci#ifdef FEATURE_SUPPORT_POWERMANAGER 528b1b8bc3fSopenharmony_civoid Network::UpdateForbidDetectionFlag(bool forbidDetectionFlag) 529b1b8bc3fSopenharmony_ci{ 530b1b8bc3fSopenharmony_ci forbidDetectionFlag_ = forbidDetectionFlag; 531b1b8bc3fSopenharmony_ci} 532b1b8bc3fSopenharmony_ci#endif 533b1b8bc3fSopenharmony_ci 534b1b8bc3fSopenharmony_civoid Network::SetNetCaps(const std::set<NetCap> &netCaps) 535b1b8bc3fSopenharmony_ci{ 536b1b8bc3fSopenharmony_ci netCaps_ = netCaps; 537b1b8bc3fSopenharmony_ci} 538b1b8bc3fSopenharmony_ci 539b1b8bc3fSopenharmony_civoid Network::NetDetectionForDnsHealth(bool dnsHealthSuccess) 540b1b8bc3fSopenharmony_ci{ 541b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Enter NetDetectionForDnsHealthSync"); 542b1b8bc3fSopenharmony_ci if (netMonitor_ == nullptr) { 543b1b8bc3fSopenharmony_ci NETMGR_LOG_E("netMonitor_ is nullptr"); 544b1b8bc3fSopenharmony_ci return; 545b1b8bc3fSopenharmony_ci } 546b1b8bc3fSopenharmony_ci NetDetectionStatus lastDetectResult = detectResult_; 547b1b8bc3fSopenharmony_ci { 548b1b8bc3fSopenharmony_ci static NetDetectionStatus preStatus = UNKNOWN_STATE; 549b1b8bc3fSopenharmony_ci if (preStatus != lastDetectResult) { 550b1b8bc3fSopenharmony_ci NETMGR_LOG_I("Last netDetectionState: [%{public}d->%{public}d]", preStatus, lastDetectResult); 551b1b8bc3fSopenharmony_ci preStatus = lastDetectResult; 552b1b8bc3fSopenharmony_ci } 553b1b8bc3fSopenharmony_ci } 554b1b8bc3fSopenharmony_ci if (IsDetectionForDnsSuccess(lastDetectResult, dnsHealthSuccess)) { 555b1b8bc3fSopenharmony_ci NETMGR_LOG_I("Dns report success, so restart detection."); 556b1b8bc3fSopenharmony_ci isDetectingForDns_ = true; 557b1b8bc3fSopenharmony_ci StopNetDetection(); 558b1b8bc3fSopenharmony_ci InitNetMonitor(); 559b1b8bc3fSopenharmony_ci } else if (IsDetectionForDnsFail(lastDetectResult, dnsHealthSuccess)) { 560b1b8bc3fSopenharmony_ci NETMGR_LOG_I("Dns report fail, start net detection"); 561b1b8bc3fSopenharmony_ci netMonitor_->Start(); 562b1b8bc3fSopenharmony_ci } else { 563b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Not match, no need to restart."); 564b1b8bc3fSopenharmony_ci } 565b1b8bc3fSopenharmony_ci} 566b1b8bc3fSopenharmony_ci 567b1b8bc3fSopenharmony_civoid Network::StopNetDetection() 568b1b8bc3fSopenharmony_ci{ 569b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Enter StopNetDetection"); 570b1b8bc3fSopenharmony_ci if (netMonitor_ != nullptr) { 571b1b8bc3fSopenharmony_ci netMonitor_->Stop(); 572b1b8bc3fSopenharmony_ci netMonitor_ = nullptr; 573b1b8bc3fSopenharmony_ci } 574b1b8bc3fSopenharmony_ci} 575b1b8bc3fSopenharmony_ci 576b1b8bc3fSopenharmony_civoid Network::InitNetMonitor() 577b1b8bc3fSopenharmony_ci{ 578b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Enter InitNetMonitor"); 579b1b8bc3fSopenharmony_ci std::weak_ptr<INetMonitorCallback> monitorCallback = shared_from_this(); 580b1b8bc3fSopenharmony_ci netMonitor_ = std::make_shared<NetMonitor>(netId_, netSupplierType_, netLinkInfo_, monitorCallback); 581b1b8bc3fSopenharmony_ci if (netMonitor_ == nullptr) { 582b1b8bc3fSopenharmony_ci NETMGR_LOG_E("new NetMonitor failed,netMonitor_ is null!"); 583b1b8bc3fSopenharmony_ci return; 584b1b8bc3fSopenharmony_ci } 585b1b8bc3fSopenharmony_ci netMonitor_->Start(); 586b1b8bc3fSopenharmony_ci} 587b1b8bc3fSopenharmony_ci 588b1b8bc3fSopenharmony_civoid Network::HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) 589b1b8bc3fSopenharmony_ci{ 590b1b8bc3fSopenharmony_ci NETMGR_LOG_I("HNMR, [%{public}d]", netDetectionState); 591b1b8bc3fSopenharmony_ci isDetectingForDns_ = false; 592b1b8bc3fSopenharmony_ci NotifyNetDetectionResult(NetDetectionResultConvert(static_cast<int32_t>(netDetectionState)), urlRedirect); 593b1b8bc3fSopenharmony_ci if (netCallback_ && (detectResult_ != netDetectionState)) { 594b1b8bc3fSopenharmony_ci detectResult_ = netDetectionState; 595b1b8bc3fSopenharmony_ci netCallback_(supplierId_, netDetectionState); 596b1b8bc3fSopenharmony_ci } 597b1b8bc3fSopenharmony_ci} 598b1b8bc3fSopenharmony_ci 599b1b8bc3fSopenharmony_civoid Network::NotifyNetDetectionResult(NetDetectionResultCode detectionResult, const std::string &urlRedirect) 600b1b8bc3fSopenharmony_ci{ 601b1b8bc3fSopenharmony_ci for (const auto &callback : netDetectionRetCallback_) { 602b1b8bc3fSopenharmony_ci NETMGR_LOG_D("start callback!"); 603b1b8bc3fSopenharmony_ci if (callback) { 604b1b8bc3fSopenharmony_ci callback->OnNetDetectionResultChanged(detectionResult, urlRedirect); 605b1b8bc3fSopenharmony_ci } 606b1b8bc3fSopenharmony_ci } 607b1b8bc3fSopenharmony_ci} 608b1b8bc3fSopenharmony_ci 609b1b8bc3fSopenharmony_ciNetDetectionResultCode Network::NetDetectionResultConvert(int32_t internalRet) 610b1b8bc3fSopenharmony_ci{ 611b1b8bc3fSopenharmony_ci switch (internalRet) { 612b1b8bc3fSopenharmony_ci case static_cast<int32_t>(INVALID_DETECTION_STATE): 613b1b8bc3fSopenharmony_ci return NET_DETECTION_FAIL; 614b1b8bc3fSopenharmony_ci case static_cast<int32_t>(VERIFICATION_STATE): 615b1b8bc3fSopenharmony_ci return NET_DETECTION_SUCCESS; 616b1b8bc3fSopenharmony_ci case static_cast<int32_t>(CAPTIVE_PORTAL_STATE): 617b1b8bc3fSopenharmony_ci return NET_DETECTION_CAPTIVE_PORTAL; 618b1b8bc3fSopenharmony_ci default: 619b1b8bc3fSopenharmony_ci break; 620b1b8bc3fSopenharmony_ci } 621b1b8bc3fSopenharmony_ci return NET_DETECTION_FAIL; 622b1b8bc3fSopenharmony_ci} 623b1b8bc3fSopenharmony_ci 624b1b8bc3fSopenharmony_civoid Network::SetDefaultNetWork() 625b1b8bc3fSopenharmony_ci{ 626b1b8bc3fSopenharmony_ci int32_t ret = NetsysController::GetInstance().SetDefaultNetWork(netId_); 627b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS) { 628b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_SET_DEFAULT_NETWORK_FAILED, ERROR_MSG_SET_DEFAULT_NETWORK_FAILED); 629b1b8bc3fSopenharmony_ci } 630b1b8bc3fSopenharmony_ci} 631b1b8bc3fSopenharmony_ci 632b1b8bc3fSopenharmony_civoid Network::ClearDefaultNetWorkNetId() 633b1b8bc3fSopenharmony_ci{ 634b1b8bc3fSopenharmony_ci int32_t ret = NetsysController::GetInstance().ClearDefaultNetWorkNetId(); 635b1b8bc3fSopenharmony_ci if (ret != NETMANAGER_SUCCESS) { 636b1b8bc3fSopenharmony_ci SendSupplierFaultHiSysEvent(FAULT_CLEAR_DEFAULT_NETWORK_FAILED, ERROR_MSG_CLEAR_DEFAULT_NETWORK_FAILED); 637b1b8bc3fSopenharmony_ci } 638b1b8bc3fSopenharmony_ci} 639b1b8bc3fSopenharmony_ci 640b1b8bc3fSopenharmony_cibool Network::IsConnecting() const 641b1b8bc3fSopenharmony_ci{ 642b1b8bc3fSopenharmony_ci return state_ == NET_CONN_STATE_CONNECTING; 643b1b8bc3fSopenharmony_ci} 644b1b8bc3fSopenharmony_ci 645b1b8bc3fSopenharmony_cibool Network::IsConnected() const 646b1b8bc3fSopenharmony_ci{ 647b1b8bc3fSopenharmony_ci return state_ == NET_CONN_STATE_CONNECTED; 648b1b8bc3fSopenharmony_ci} 649b1b8bc3fSopenharmony_ci 650b1b8bc3fSopenharmony_civoid Network::UpdateNetConnState(NetConnState netConnState) 651b1b8bc3fSopenharmony_ci{ 652b1b8bc3fSopenharmony_ci if (state_ == netConnState) { 653b1b8bc3fSopenharmony_ci NETMGR_LOG_D("Ignore same network state changed."); 654b1b8bc3fSopenharmony_ci return; 655b1b8bc3fSopenharmony_ci } 656b1b8bc3fSopenharmony_ci NetConnState oldState = state_; 657b1b8bc3fSopenharmony_ci switch (netConnState) { 658b1b8bc3fSopenharmony_ci case NET_CONN_STATE_IDLE: 659b1b8bc3fSopenharmony_ci case NET_CONN_STATE_CONNECTING: 660b1b8bc3fSopenharmony_ci case NET_CONN_STATE_CONNECTED: 661b1b8bc3fSopenharmony_ci case NET_CONN_STATE_DISCONNECTING: 662b1b8bc3fSopenharmony_ci state_ = netConnState; 663b1b8bc3fSopenharmony_ci break; 664b1b8bc3fSopenharmony_ci case NET_CONN_STATE_DISCONNECTED: 665b1b8bc3fSopenharmony_ci state_ = netConnState; 666b1b8bc3fSopenharmony_ci ResetNetlinkInfo(); 667b1b8bc3fSopenharmony_ci break; 668b1b8bc3fSopenharmony_ci default: 669b1b8bc3fSopenharmony_ci state_ = NET_CONN_STATE_UNKNOWN; 670b1b8bc3fSopenharmony_ci break; 671b1b8bc3fSopenharmony_ci } 672b1b8bc3fSopenharmony_ci 673b1b8bc3fSopenharmony_ci SendConnectionChangedBroadcast(netConnState); 674b1b8bc3fSopenharmony_ci if (IsNat464Prefered()) { 675b1b8bc3fSopenharmony_ci if (nat464Service_ == nullptr) { 676b1b8bc3fSopenharmony_ci nat464Service_ = std::make_unique<Nat464Service>(netId_, netLinkInfo_.ifaceName_); 677b1b8bc3fSopenharmony_ci } 678b1b8bc3fSopenharmony_ci nat464Service_->MaybeUpdateV6Iface(netLinkInfo_.ifaceName_); 679b1b8bc3fSopenharmony_ci nat464Service_->UpdateService(NAT464_SERVICE_CONTINUE); 680b1b8bc3fSopenharmony_ci } else if (nat464Service_ != nullptr) { 681b1b8bc3fSopenharmony_ci nat464Service_->UpdateService(NAT464_SERVICE_STOP); 682b1b8bc3fSopenharmony_ci } 683b1b8bc3fSopenharmony_ci NETMGR_LOG_I("Network[%{public}d] state changed, from [%{public}d] to [%{public}d]", netId_, oldState, state_); 684b1b8bc3fSopenharmony_ci} 685b1b8bc3fSopenharmony_ci 686b1b8bc3fSopenharmony_civoid Network::SendConnectionChangedBroadcast(const NetConnState &netConnState) const 687b1b8bc3fSopenharmony_ci{ 688b1b8bc3fSopenharmony_ci BroadcastInfo info; 689b1b8bc3fSopenharmony_ci info.action = EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE; 690b1b8bc3fSopenharmony_ci info.data = "Net Manager Connection State Changed"; 691b1b8bc3fSopenharmony_ci info.code = static_cast<int32_t>(netConnState); 692b1b8bc3fSopenharmony_ci info.ordered = false; 693b1b8bc3fSopenharmony_ci std::map<std::string, int32_t> param = {{"NetType", static_cast<int32_t>(netSupplierType_)}}; 694b1b8bc3fSopenharmony_ci BroadcastManager::GetInstance().SendBroadcast(info, param); 695b1b8bc3fSopenharmony_ci} 696b1b8bc3fSopenharmony_ci 697b1b8bc3fSopenharmony_civoid Network::SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg) 698b1b8bc3fSopenharmony_ci{ 699b1b8bc3fSopenharmony_ci struct EventInfo eventInfo = {.netlinkInfo = netLinkInfo_.ToString(" "), 700b1b8bc3fSopenharmony_ci .supplierId = static_cast<int32_t>(supplierId_), 701b1b8bc3fSopenharmony_ci .errorType = static_cast<int32_t>(errorType), 702b1b8bc3fSopenharmony_ci .errorMsg = errMsg}; 703b1b8bc3fSopenharmony_ci EventReport::SendSupplierFaultEvent(eventInfo); 704b1b8bc3fSopenharmony_ci} 705b1b8bc3fSopenharmony_ci 706b1b8bc3fSopenharmony_civoid Network::ResetNetlinkInfo() 707b1b8bc3fSopenharmony_ci{ 708b1b8bc3fSopenharmony_ci netLinkInfo_.Initialize(); 709b1b8bc3fSopenharmony_ci detectResult_ = UNKNOWN_STATE; 710b1b8bc3fSopenharmony_ci} 711b1b8bc3fSopenharmony_ci 712b1b8bc3fSopenharmony_civoid Network::UpdateGlobalHttpProxy(const HttpProxy &httpProxy) 713b1b8bc3fSopenharmony_ci{ 714b1b8bc3fSopenharmony_ci if (netMonitor_ == nullptr) { 715b1b8bc3fSopenharmony_ci NETMGR_LOG_E("netMonitor_ is nullptr"); 716b1b8bc3fSopenharmony_ci return; 717b1b8bc3fSopenharmony_ci } 718b1b8bc3fSopenharmony_ci netMonitor_->UpdateGlobalHttpProxy(httpProxy); 719b1b8bc3fSopenharmony_ci StartNetDetection(false); 720b1b8bc3fSopenharmony_ci} 721b1b8bc3fSopenharmony_ci 722b1b8bc3fSopenharmony_civoid Network::OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) 723b1b8bc3fSopenharmony_ci{ 724b1b8bc3fSopenharmony_ci if (eventHandler_) { 725b1b8bc3fSopenharmony_ci auto network = shared_from_this(); 726b1b8bc3fSopenharmony_ci eventHandler_->PostAsyncTask([netDetectionState, urlRedirect, 727b1b8bc3fSopenharmony_ci network]() { network->HandleNetMonitorResult(netDetectionState, urlRedirect); }, 728b1b8bc3fSopenharmony_ci 0); 729b1b8bc3fSopenharmony_ci } 730b1b8bc3fSopenharmony_ci} 731b1b8bc3fSopenharmony_ci 732b1b8bc3fSopenharmony_cibool Network::ResumeNetworkInfo() 733b1b8bc3fSopenharmony_ci{ 734b1b8bc3fSopenharmony_ci NetLinkInfo nli = netLinkInfo_; 735b1b8bc3fSopenharmony_ci if (netCaps_.find(NetCap::NET_CAPABILITY_INTERNET) != netCaps_.end()) { 736b1b8bc3fSopenharmony_ci isNeedResume_ = true; 737b1b8bc3fSopenharmony_ci } 738b1b8bc3fSopenharmony_ci NETMGR_LOG_D("ResumeNetworkInfo UpdateBasicNetwork false"); 739b1b8bc3fSopenharmony_ci if (!UpdateBasicNetwork(false)) { 740b1b8bc3fSopenharmony_ci NETMGR_LOG_E("%s release existed basic network failed", __FUNCTION__); 741b1b8bc3fSopenharmony_ci return false; 742b1b8bc3fSopenharmony_ci } 743b1b8bc3fSopenharmony_ci 744b1b8bc3fSopenharmony_ci NETMGR_LOG_D("ResumeNetworkInfo UpdateBasicNetwork true"); 745b1b8bc3fSopenharmony_ci if (!UpdateBasicNetwork(true)) { 746b1b8bc3fSopenharmony_ci NETMGR_LOG_E("%s create basic network failed", __FUNCTION__); 747b1b8bc3fSopenharmony_ci return false; 748b1b8bc3fSopenharmony_ci } 749b1b8bc3fSopenharmony_ci 750b1b8bc3fSopenharmony_ci NETMGR_LOG_D("ResumeNetworkInfo UpdateNetLinkInfo"); 751b1b8bc3fSopenharmony_ci return UpdateNetLinkInfo(nli); 752b1b8bc3fSopenharmony_ci} 753b1b8bc3fSopenharmony_ci 754b1b8bc3fSopenharmony_cibool Network::IsDetectionForDnsSuccess(NetDetectionStatus netDetectionState, bool dnsHealthSuccess) 755b1b8bc3fSopenharmony_ci{ 756b1b8bc3fSopenharmony_ci return ((netDetectionState == INVALID_DETECTION_STATE) && dnsHealthSuccess && !isDetectingForDns_); 757b1b8bc3fSopenharmony_ci} 758b1b8bc3fSopenharmony_ci 759b1b8bc3fSopenharmony_cibool Network::IsDetectionForDnsFail(NetDetectionStatus netDetectionState, bool dnsHealthSuccess) 760b1b8bc3fSopenharmony_ci{ 761b1b8bc3fSopenharmony_ci return ((netDetectionState == VERIFICATION_STATE) && !dnsHealthSuccess && !(netMonitor_->IsDetecting())); 762b1b8bc3fSopenharmony_ci} 763b1b8bc3fSopenharmony_ci 764b1b8bc3fSopenharmony_cibool Network::IsNat464Prefered() 765b1b8bc3fSopenharmony_ci{ 766b1b8bc3fSopenharmony_ci if (netSupplierType_ != BEARER_CELLULAR && netSupplierType_ != BEARER_WIFI && netSupplierType_ != BEARER_ETHERNET) { 767b1b8bc3fSopenharmony_ci return false; 768b1b8bc3fSopenharmony_ci } 769b1b8bc3fSopenharmony_ci if (std::any_of(netLinkInfo_.netAddrList_.begin(), netLinkInfo_.netAddrList_.end(), 770b1b8bc3fSopenharmony_ci [](const INetAddr &i) { return i.type_ != INetAddr::IPV6; })) { 771b1b8bc3fSopenharmony_ci return false; 772b1b8bc3fSopenharmony_ci } 773b1b8bc3fSopenharmony_ci if (netLinkInfo_.ifaceName_.empty() || !IsConnected()) { 774b1b8bc3fSopenharmony_ci return false; 775b1b8bc3fSopenharmony_ci } 776b1b8bc3fSopenharmony_ci return true; 777b1b8bc3fSopenharmony_ci} 778b1b8bc3fSopenharmony_ci 779b1b8bc3fSopenharmony_ci 780b1b8bc3fSopenharmony_civoid Network::CloseSocketsUid(uint32_t uid) 781b1b8bc3fSopenharmony_ci{ 782b1b8bc3fSopenharmony_ci for (const auto &inetAddr : netLinkInfo_.netAddrList_) { 783b1b8bc3fSopenharmony_ci NetsysController::GetInstance().CloseSocketsUid(inetAddr.address_, uid); 784b1b8bc3fSopenharmony_ci } 785b1b8bc3fSopenharmony_ci} 786b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard 787b1b8bc3fSopenharmony_ci} // namespace OHOS 788