• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include "common_event_support.h"
17 
18 #include "broadcast_manager.h"
19 #include "event_report.h"
20 #include "net_manager_constants.h"
21 #include "net_mgr_log_wrapper.h"
22 #include "netmanager_base_common_utils.h"
23 #include "netsys_controller.h"
24 #include "network.h"
25 #include "route_utils.h"
26 #include "securec.h"
27 
28 using namespace OHOS::NetManagerStandard::CommonUtils;
29 
30 namespace OHOS {
31 namespace NetManagerStandard {
32 namespace {
33 // hisysevent error messgae
34 constexpr const char *ERROR_MSG_CREATE_PHYSICAL_NETWORK_FAILED = "Create physical network failed, net id:";
35 constexpr const char *ERROR_MSG_CREATE_VIRTUAL_NETWORK_FAILED = "Create virtual network failed, net id:";
36 constexpr const char *ERROR_MSG_ADD_NET_INTERFACE_FAILED = "Add network interface failed";
37 constexpr const char *ERROR_MSG_REMOVE_NET_INTERFACE_FAILED = "Remove network interface failed";
38 constexpr const char *ERROR_MSG_DELETE_NET_IP_ADDR_FAILED = "Delete network ip address failed";
39 constexpr const char *ERROR_MSG_ADD_NET_IP_ADDR_FAILED = "Add network ip address failed";
40 constexpr const char *ERROR_MSG_REMOVE_NET_ROUTES_FAILED = "Remove network routes failed";
41 constexpr const char *ERROR_MSG_ADD_NET_ROUTES_FAILED = "Add network routes failed";
42 constexpr const char *ERROR_MSG_UPDATE_NET_ROUTES_FAILED = "Update netlink routes failed,routes list is empty";
43 constexpr const char *ERROR_MSG_SET_NET_RESOLVER_FAILED = "Set network resolver config failed";
44 constexpr const char *ERROR_MSG_UPDATE_NET_DNSES_FAILED = "Update netlink dns failed,dns list is empty";
45 constexpr const char *ERROR_MSG_SET_NET_MTU_FAILED = "Set netlink interface mtu failed";
46 constexpr const char *ERROR_MSG_SET_DEFAULT_NETWORK_FAILED = "Set default network failed";
47 constexpr const char *ERROR_MSG_CLEAR_DEFAULT_NETWORK_FAILED = "Clear default network failed";
48 constexpr const char *LOCAL_ROUTE_NEXT_HOP = "0.0.0.0";
49 constexpr const char *LOCAL_ROUTE_IPV6_DESTINATION = "::";
50 } // namespace
51 
Network(int32_t netId,uint32_t supplierId,const NetDetectionHandler & handler,NetBearType bearerType,const std::shared_ptr<NetConnEventHandler> & eventHandler)52 Network::Network(int32_t netId, uint32_t supplierId, const NetDetectionHandler &handler, NetBearType bearerType,
53                  const std::shared_ptr<NetConnEventHandler> &eventHandler)
54     : netId_(netId),
55       supplierId_(supplierId),
56       netCallback_(handler),
57       netSupplierType_(bearerType),
58       eventHandler_(eventHandler)
59 {
60 }
61 
GetNetId() const62 int32_t Network::GetNetId() const
63 {
64     return netId_;
65 }
66 
operator ==(const Network & network) const67 bool Network::operator==(const Network &network) const
68 {
69     return netId_ == network.netId_;
70 }
71 
UpdateBasicNetwork(bool isAvailable_)72 bool Network::UpdateBasicNetwork(bool isAvailable_)
73 {
74     NETMGR_LOG_D("Enter UpdateBasicNetwork");
75     if (isAvailable_) {
76         if (netSupplierType_ == BEARER_VPN) {
77             return CreateVirtualNetwork();
78         }
79         return CreateBasicNetwork();
80     } else {
81         if (netSupplierType_ == BEARER_VPN) {
82             return ReleaseVirtualNetwork();
83         }
84         return ReleaseBasicNetwork();
85     }
86 }
87 
CreateBasicNetwork()88 bool Network::CreateBasicNetwork()
89 {
90     NETMGR_LOG_D("Enter CreateBasicNetwork");
91     if (!isPhyNetCreated_) {
92         NETMGR_LOG_D("Create physical network");
93         // Create a physical network
94         if (NetsysController::GetInstance().NetworkCreatePhysical(netId_, 0) != NETMANAGER_SUCCESS) {
95             std::string errMsg = std::string(ERROR_MSG_CREATE_PHYSICAL_NETWORK_FAILED).append(std::to_string(netId_));
96             SendSupplierFaultHiSysEvent(FAULT_CREATE_PHYSICAL_NETWORK_FAILED, errMsg);
97         }
98         NetsysController::GetInstance().CreateNetworkCache(netId_);
99         isPhyNetCreated_ = true;
100     }
101     return true;
102 }
103 
CreateVirtualNetwork()104 bool Network::CreateVirtualNetwork()
105 {
106     NETMGR_LOG_D("Enter create virtual network");
107     if (!isVirtualCreated_) {
108         // Create a virtual network here
109         bool hasDns = netLinkInfo_.dnsList_.size() ? true : false;
110         if (NetsysController::GetInstance().NetworkCreateVirtual(netId_, hasDns) != NETMANAGER_SUCCESS) {
111             std::string errMsg = std::string(ERROR_MSG_CREATE_VIRTUAL_NETWORK_FAILED).append(std::to_string(netId_));
112             SendSupplierFaultHiSysEvent(FAULT_CREATE_VIRTUAL_NETWORK_FAILED, errMsg);
113         }
114         NetsysController::GetInstance().CreateNetworkCache(netId_);
115         isVirtualCreated_ = true;
116     }
117     return true;
118 }
119 
ReleaseBasicNetwork()120 bool Network::ReleaseBasicNetwork()
121 {
122     NETMGR_LOG_D("Enter ReleaseBasicNetwork");
123     if (isPhyNetCreated_) {
124         NETMGR_LOG_D("Destroy physical network");
125         StopNetDetection();
126         for (const auto &inetAddr : netLinkInfo_.netAddrList_) {
127             int32_t prefixLen = inetAddr.prefixlen_;
128             if (prefixLen == 0) {
129                 prefixLen = Ipv4PrefixLen(inetAddr.netMask_);
130             }
131             NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, prefixLen);
132         }
133         for (const auto &route : netLinkInfo_.routeList_) {
134             std::string destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_);
135             NetsysController::GetInstance().NetworkRemoveRoute(netId_, route.iface_, destAddress,
136                                                                route.gateway_.address_);
137             if (route.destination_.address_ != LOCAL_ROUTE_NEXT_HOP &&
138                 route.destination_.address_ != LOCAL_ROUTE_IPV6_DESTINATION) {
139                 auto family = GetAddrFamily(route.destination_.address_);
140                 std::string nextHop = (family == AF_INET6) ? "" : LOCAL_ROUTE_NEXT_HOP;
141                 NetsysController::GetInstance().NetworkRemoveRoute(LOCAL_NET_ID, route.iface_, destAddress, nextHop);
142             }
143         }
144         NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_);
145         NetsysController::GetInstance().NetworkDestroy(netId_);
146         NetsysController::GetInstance().DestroyNetworkCache(netId_);
147         netLinkInfo_.Initialize();
148         isPhyNetCreated_ = false;
149     }
150     return true;
151 }
152 
ReleaseVirtualNetwork()153 bool Network::ReleaseVirtualNetwork()
154 {
155     NETMGR_LOG_D("Enter release virtual network");
156     if (isVirtualCreated_) {
157         for (const auto &inetAddr : netLinkInfo_.netAddrList_) {
158             int32_t prefixLen = inetAddr.prefixlen_;
159             if (prefixLen == 0) {
160                 prefixLen = Ipv4PrefixLen(inetAddr.netMask_);
161             }
162             NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, prefixLen);
163         }
164         NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_);
165         NetsysController::GetInstance().NetworkDestroy(netId_);
166         NetsysController::GetInstance().DestroyNetworkCache(netId_);
167         netLinkInfo_.Initialize();
168         isVirtualCreated_ = false;
169     }
170     return true;
171 }
172 
UpdateNetLinkInfo(const NetLinkInfo & netLinkInfo)173 bool Network::UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo)
174 {
175     NETMGR_LOG_D("update net link information process");
176     UpdateInterfaces(netLinkInfo);
177     UpdateIpAddrs(netLinkInfo);
178     UpdateRoutes(netLinkInfo);
179     UpdateDns(netLinkInfo);
180     UpdateMtu(netLinkInfo);
181     netLinkInfo_ = netLinkInfo;
182     if (netSupplierType_ != BEARER_VPN) {
183         StartNetDetection(false);
184     }
185     return true;
186 }
187 
GetNetLinkInfo() const188 NetLinkInfo Network::GetNetLinkInfo() const
189 {
190     return netLinkInfo_;
191 }
192 
UpdateInterfaces(const NetLinkInfo & netLinkInfo)193 void Network::UpdateInterfaces(const NetLinkInfo &netLinkInfo)
194 {
195     NETMGR_LOG_D("Network UpdateInterfaces in.");
196     if (netLinkInfo.ifaceName_ == netLinkInfo_.ifaceName_) {
197         NETMGR_LOG_D("Network UpdateInterfaces out. same with before.");
198         return;
199     }
200 
201     int32_t ret = NETMANAGER_SUCCESS;
202     // Call netsys to add and remove interface
203     if (!netLinkInfo.ifaceName_.empty()) {
204         ret = NetsysController::GetInstance().NetworkAddInterface(netId_, netLinkInfo.ifaceName_);
205         if (ret != NETMANAGER_SUCCESS) {
206             SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_ADD_NET_INTERFACE_FAILED);
207         }
208     }
209     if (!netLinkInfo_.ifaceName_.empty()) {
210         ret = NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_);
211         if (ret != NETMANAGER_SUCCESS) {
212             SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_REMOVE_NET_INTERFACE_FAILED);
213         }
214     }
215     netLinkInfo_.ifaceName_ = netLinkInfo.ifaceName_;
216     NETMGR_LOG_D("Network UpdateInterfaces out.");
217 }
218 
UpdateIpAddrs(const NetLinkInfo & netLinkInfo)219 void Network::UpdateIpAddrs(const NetLinkInfo &netLinkInfo)
220 {
221     // netLinkInfo_ represents the old, netLinkInfo represents the new
222     // Update: remove old Ips first, then add the new Ips
223     NETMGR_LOG_D("UpdateIpAddrs, old ip addrs: ...");
224     for (const auto &inetAddr : netLinkInfo_.netAddrList_) {
225         auto family = GetAddrFamily(inetAddr.address_);
226         auto prefixLen = inetAddr.prefixlen_ ? static_cast<int32_t>(inetAddr.prefixlen_)
227                                              : ((family == AF_INET6) ? Ipv6PrefixLen(inetAddr.netMask_)
228                                                                      : Ipv4PrefixLen(inetAddr.netMask_));
229         if (NETMANAGER_SUCCESS != NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_,
230                                                                                       inetAddr.address_, prefixLen)) {
231             SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_DELETE_NET_IP_ADDR_FAILED);
232         }
233     }
234 
235     NETMGR_LOG_D("UpdateIpAddrs, new ip addrs: ...");
236     for (const auto &inetAddr : netLinkInfo.netAddrList_) {
237         auto family = GetAddrFamily(inetAddr.address_);
238         auto prefixLen = inetAddr.prefixlen_ ? static_cast<int32_t>(inetAddr.prefixlen_)
239                                              : ((family == AF_INET6) ? Ipv6PrefixLen(inetAddr.netMask_)
240                                                                      : Ipv4PrefixLen(inetAddr.netMask_));
241         if (NETMANAGER_SUCCESS !=
242             NetsysController::GetInstance().AddInterfaceAddress(netLinkInfo.ifaceName_, inetAddr.address_, prefixLen)) {
243             SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_ADD_NET_IP_ADDR_FAILED);
244         }
245     }
246 }
247 
UpdateRoutes(const NetLinkInfo & netLinkInfo)248 void Network::UpdateRoutes(const NetLinkInfo &netLinkInfo)
249 {
250     // netLinkInfo_ contains the old routes info, netLinkInfo contains the new routes info
251     // Update: remove old routes first, then add the new routes
252     NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str());
253     for (const auto &route : netLinkInfo_.routeList_) {
254         std::string destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_);
255         auto ret = NetsysController::GetInstance().NetworkRemoveRoute(netId_, route.iface_, destAddress,
256                                                                       route.gateway_.address_);
257         int32_t res = NETMANAGER_SUCCESS;
258         if (route.destination_.address_ != LOCAL_ROUTE_NEXT_HOP &&
259             route.destination_.address_ != LOCAL_ROUTE_IPV6_DESTINATION) {
260             auto family = GetAddrFamily(route.destination_.address_);
261             std::string nextHop = (family == AF_INET6) ? "" : LOCAL_ROUTE_NEXT_HOP;
262             res = NetsysController::GetInstance().NetworkRemoveRoute(LOCAL_NET_ID, route.iface_, destAddress, nextHop);
263         }
264         if (ret != NETMANAGER_SUCCESS || res != NETMANAGER_SUCCESS) {
265             SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_REMOVE_NET_ROUTES_FAILED);
266         }
267     }
268 
269     NETMGR_LOG_D("UpdateRoutes, new routes: [%{public}s]", netLinkInfo.ToStringRoute("").c_str());
270     for (const auto &route : netLinkInfo.routeList_) {
271         std::string destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_);
272         auto ret =
273             NetsysController::GetInstance().NetworkAddRoute(netId_, route.iface_, destAddress, route.gateway_.address_);
274         int32_t res = NETMANAGER_SUCCESS;
275         if (route.destination_.address_ != LOCAL_ROUTE_NEXT_HOP &&
276             route.destination_.address_ != LOCAL_ROUTE_IPV6_DESTINATION) {
277             auto family = GetAddrFamily(route.destination_.address_);
278             std::string nextHop = (family == AF_INET6) ? "" : LOCAL_ROUTE_NEXT_HOP;
279             res = NetsysController::GetInstance().NetworkAddRoute(LOCAL_NET_ID, route.iface_, destAddress, nextHop);
280         }
281         if (ret != NETMANAGER_SUCCESS || res != NETMANAGER_SUCCESS) {
282             SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_ADD_NET_ROUTES_FAILED);
283         }
284     }
285     NETMGR_LOG_D("Network UpdateRoutes out.");
286     if (netLinkInfo.routeList_.empty()) {
287         SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_UPDATE_NET_ROUTES_FAILED);
288     }
289 }
290 
UpdateDns(const NetLinkInfo & netLinkInfo)291 void Network::UpdateDns(const NetLinkInfo &netLinkInfo)
292 {
293     NETMGR_LOG_D("Network UpdateDns in.");
294     std::vector<std::string> servers;
295     std::vector<std::string> domains;
296     for (const auto &dns : netLinkInfo.dnsList_) {
297         servers.emplace_back(dns.address_);
298         domains.emplace_back(dns.hostName_);
299     }
300     // Call netsys to set dns, use default timeout and retry
301     int32_t ret = NetsysController::GetInstance().SetResolverConfig(netId_, 0, 0, servers, domains);
302     if (ret != NETMANAGER_SUCCESS) {
303         SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_SET_NET_RESOLVER_FAILED);
304     }
305     NETMGR_LOG_D("Network UpdateDns out.");
306     if (netLinkInfo.dnsList_.empty()) {
307         SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_UPDATE_NET_DNSES_FAILED);
308     }
309 }
310 
UpdateMtu(const NetLinkInfo & netLinkInfo)311 void Network::UpdateMtu(const NetLinkInfo &netLinkInfo)
312 {
313     NETMGR_LOG_D("Network UpdateMtu in.");
314     if (netLinkInfo.mtu_ == netLinkInfo_.mtu_) {
315         NETMGR_LOG_D("Network UpdateMtu out. same with before.");
316         return;
317     }
318 
319     int32_t ret = NetsysController::GetInstance().SetInterfaceMtu(netLinkInfo.ifaceName_, netLinkInfo.mtu_);
320     if (ret != NETMANAGER_SUCCESS) {
321         SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_SET_NET_MTU_FAILED);
322     }
323     NETMGR_LOG_D("Network UpdateMtu out.");
324 }
325 
RegisterNetDetectionCallback(const sptr<INetDetectionCallback> & callback)326 void Network::RegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback)
327 {
328     NETMGR_LOG_D("Enter RegisterNetDetectionCallback");
329     if (callback == nullptr) {
330         NETMGR_LOG_E("The parameter callback is null");
331         return;
332     }
333 
334     for (const auto &iter : netDetectionRetCallback_) {
335         if (callback->AsObject().GetRefPtr() == iter->AsObject().GetRefPtr()) {
336             NETMGR_LOG_D("netDetectionRetCallback_ had this callback");
337             return;
338         }
339     }
340 
341     netDetectionRetCallback_.emplace_back(callback);
342 }
343 
UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> & callback)344 int32_t Network::UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback)
345 {
346     NETMGR_LOG_D("Enter UnRegisterNetDetectionCallback");
347     if (callback == nullptr) {
348         NETMGR_LOG_E("The parameter of callback is null");
349         return NETMANAGER_ERR_LOCAL_PTR_NULL;
350     }
351 
352     for (auto iter = netDetectionRetCallback_.begin(); iter != netDetectionRetCallback_.end(); ++iter) {
353         if (callback->AsObject().GetRefPtr() == (*iter)->AsObject().GetRefPtr()) {
354             netDetectionRetCallback_.erase(iter);
355             return NETMANAGER_SUCCESS;
356         }
357     }
358 
359     return NETMANAGER_SUCCESS;
360 }
361 
StartNetDetection(bool needReport)362 void Network::StartNetDetection(bool needReport)
363 {
364     NETMGR_LOG_D("Enter Network::StartNetDetection");
365     if (needReport) {
366         StopNetDetection();
367     }
368     InitNetMonitor();
369 }
370 
StopNetDetection()371 void Network::StopNetDetection()
372 {
373     NETMGR_LOG_D("Enter Network::StopNetDetection");
374     if (netMonitor_ != nullptr) {
375         netMonitor_->Stop();
376         netMonitor_ = nullptr;
377     }
378 }
379 
InitNetMonitor()380 void Network::InitNetMonitor()
381 {
382     if (netMonitor_ == nullptr) {
383         std::weak_ptr<INetMonitorCallback> monitorCallback = shared_from_this();
384         netMonitor_ = std::make_shared<NetMonitor>(netId_, monitorCallback);
385         if (netMonitor_ == nullptr) {
386             NETMGR_LOG_E("new NetMonitor failed,netMonitor_ is null!");
387             return;
388         }
389     }
390     netMonitor_->Start();
391 }
392 
HandleNetMonitorResult(NetDetectionStatus netDetectionState,const std::string & urlRedirect)393 void Network::HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect)
394 {
395     NETMGR_LOG_D("HandleNetMonitorResult, netDetectionState[%{public}d]", netDetectionState);
396     NotifyNetDetectionResult(NetDetectionResultConvert(static_cast<int32_t>(netDetectionState)), urlRedirect);
397     if (netCallback_ && (detectResult_ != netDetectionState)) {
398         detectResult_ = netDetectionState;
399         netCallback_(supplierId_, netDetectionState == VERIFICATION_STATE);
400     }
401 }
402 
NotifyNetDetectionResult(NetDetectionResultCode detectionResult,const std::string & urlRedirect)403 void Network::NotifyNetDetectionResult(NetDetectionResultCode detectionResult, const std::string &urlRedirect)
404 {
405     for (const auto &callback : netDetectionRetCallback_) {
406         NETMGR_LOG_D("start callback!");
407         if (callback) {
408             callback->OnNetDetectionResultChanged(detectionResult, urlRedirect);
409         }
410     }
411 }
412 
NetDetectionResultConvert(int32_t internalRet)413 NetDetectionResultCode Network::NetDetectionResultConvert(int32_t internalRet)
414 {
415     switch (internalRet) {
416         case static_cast<int32_t>(INVALID_DETECTION_STATE):
417             return NET_DETECTION_FAIL;
418         case static_cast<int32_t>(VERIFICATION_STATE):
419             return NET_DETECTION_SUCCESS;
420         case static_cast<int32_t>(CAPTIVE_PORTAL_STATE):
421             return NET_DETECTION_CAPTIVE_PORTAL;
422         default:
423             break;
424     }
425     return NET_DETECTION_FAIL;
426 }
427 
SetDefaultNetWork()428 void Network::SetDefaultNetWork()
429 {
430     int32_t ret = NetsysController::GetInstance().SetDefaultNetWork(netId_);
431     if (ret != NETMANAGER_SUCCESS) {
432         SendSupplierFaultHiSysEvent(FAULT_SET_DEFAULT_NETWORK_FAILED, ERROR_MSG_SET_DEFAULT_NETWORK_FAILED);
433     }
434 }
435 
ClearDefaultNetWorkNetId()436 void Network::ClearDefaultNetWorkNetId()
437 {
438     int32_t ret = NetsysController::GetInstance().ClearDefaultNetWorkNetId();
439     if (ret != NETMANAGER_SUCCESS) {
440         SendSupplierFaultHiSysEvent(FAULT_CLEAR_DEFAULT_NETWORK_FAILED, ERROR_MSG_CLEAR_DEFAULT_NETWORK_FAILED);
441     }
442 }
443 
IsConnecting() const444 bool Network::IsConnecting() const
445 {
446     return state_ == NET_CONN_STATE_CONNECTING;
447 }
448 
IsConnected() const449 bool Network::IsConnected() const
450 {
451     return state_ == NET_CONN_STATE_CONNECTED;
452 }
453 
UpdateNetConnState(NetConnState netConnState)454 void Network::UpdateNetConnState(NetConnState netConnState)
455 {
456     if (state_ == netConnState) {
457         NETMGR_LOG_E("Ignore same network state changed.");
458         return;
459     }
460     NetConnState oldState = state_;
461     switch (netConnState) {
462         case NET_CONN_STATE_IDLE:
463         case NET_CONN_STATE_CONNECTING:
464         case NET_CONN_STATE_CONNECTED:
465         case NET_CONN_STATE_DISCONNECTING:
466             state_ = netConnState;
467             break;
468         case NET_CONN_STATE_DISCONNECTED:
469             state_ = netConnState;
470             ResetNetlinkInfo();
471             break;
472         default:
473             state_ = NET_CONN_STATE_UNKNOWN;
474             break;
475     }
476 
477     SendConnectionChangedBroadcast(netConnState);
478     NETMGR_LOG_I("Network[%{public}d] state changed, from [%{public}d] to [%{public}d]", netId_, oldState, state_);
479 }
480 
SendConnectionChangedBroadcast(const NetConnState & netConnState) const481 void Network::SendConnectionChangedBroadcast(const NetConnState &netConnState) const
482 {
483     BroadcastInfo info;
484     info.action = EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE;
485     info.data = "Net Manager Connection State Changed";
486     info.code = static_cast<int32_t>(netConnState);
487     info.ordered = true;
488     std::map<std::string, int32_t> param = {{"NetType", static_cast<int32_t>(netSupplierType_)}};
489     BroadcastManager::GetInstance().SendBroadcast(info, param);
490 }
491 
SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType,const std::string & errMsg)492 void Network::SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg)
493 {
494     struct EventInfo eventInfo = {.netlinkInfo = netLinkInfo_.ToString(" "),
495                                   .supplierId = static_cast<int32_t>(supplierId_),
496                                   .errorType = static_cast<int32_t>(errorType),
497                                   .errorMsg = errMsg};
498     EventReport::SendSupplierFaultEvent(eventInfo);
499 }
500 
ResetNetlinkInfo()501 void Network::ResetNetlinkInfo()
502 {
503     netLinkInfo_.Initialize();
504 }
505 
OnHandleNetMonitorResult(NetDetectionStatus netDetectionState,const std::string & urlRedirect)506 void Network::OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect)
507 {
508     if (eventHandler_) {
509         eventHandler_->PostAsyncTask(
510             [netDetectionState, &urlRedirect, this]() { this->HandleNetMonitorResult(netDetectionState, urlRedirect); },
511             0);
512     }
513 }
514 } // namespace NetManagerStandard
515 } // namespace OHOS
516