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#include <atomic>
16b1b8bc3fSopenharmony_ci#include <fstream>
17b1b8bc3fSopenharmony_ci#include <functional>
18b1b8bc3fSopenharmony_ci#include <memory>
19b1b8bc3fSopenharmony_ci#include <sys/time.h>
20b1b8bc3fSopenharmony_ci#include <utility>
21b1b8bc3fSopenharmony_ci#include <regex>
22b1b8bc3fSopenharmony_ci#include <condition_variable>
23b1b8bc3fSopenharmony_ci
24b1b8bc3fSopenharmony_ci#include "common_event_data.h"
25b1b8bc3fSopenharmony_ci#include "common_event_manager.h"
26b1b8bc3fSopenharmony_ci#include "common_event_subscriber.h"
27b1b8bc3fSopenharmony_ci#include "common_event_support.h"
28b1b8bc3fSopenharmony_ci#include "netmanager_base_common_utils.h"
29b1b8bc3fSopenharmony_ci#include "network.h"
30b1b8bc3fSopenharmony_ci#include "system_ability_definition.h"
31b1b8bc3fSopenharmony_ci#include "want.h"
32b1b8bc3fSopenharmony_ci
33b1b8bc3fSopenharmony_ci#include "broadcast_manager.h"
34b1b8bc3fSopenharmony_ci#include "event_report.h"
35b1b8bc3fSopenharmony_ci#include "net_activate.h"
36b1b8bc3fSopenharmony_ci#include "net_conn_service.h"
37b1b8bc3fSopenharmony_ci#include "net_conn_types.h"
38b1b8bc3fSopenharmony_ci#include "net_policy_client.h"
39b1b8bc3fSopenharmony_ci#include "net_datashare_utils.h"
40b1b8bc3fSopenharmony_ci#include "net_http_proxy_tracker.h"
41b1b8bc3fSopenharmony_ci#include "net_manager_center.h"
42b1b8bc3fSopenharmony_ci#include "net_manager_constants.h"
43b1b8bc3fSopenharmony_ci#include "net_mgr_log_wrapper.h"
44b1b8bc3fSopenharmony_ci#include "net_supplier.h"
45b1b8bc3fSopenharmony_ci#include "netmanager_base_permission.h"
46b1b8bc3fSopenharmony_ci#include "netsys_controller.h"
47b1b8bc3fSopenharmony_ci#include "ipc_skeleton.h"
48b1b8bc3fSopenharmony_ci#include "parameter.h"
49b1b8bc3fSopenharmony_ci
50b1b8bc3fSopenharmony_cinamespace OHOS {
51b1b8bc3fSopenharmony_cinamespace NetManagerStandard {
52b1b8bc3fSopenharmony_cinamespace {
53b1b8bc3fSopenharmony_ciconstexpr uint32_t MAX_ALLOW_UID_NUM = 2000;
54b1b8bc3fSopenharmony_ciconstexpr uint32_t INVALID_SUPPLIER_ID = 0;
55b1b8bc3fSopenharmony_ci// hisysevent error messgae
56b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_NULL_SUPPLIER_INFO = "Net supplier info is nullptr";
57b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_NULL_NET_LINK_INFO = "Net link info is nullptr";
58b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_NULL_NET_SPECIFIER = "The parameter of netSpecifier or callback is null";
59b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_CAN_NOT_FIND_SUPPLIER = "Can not find supplier by id:";
60b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_UPDATE_NETLINK_INFO_FAILED = "Update net link info failed";
61b1b8bc3fSopenharmony_ciconstexpr const char *ERROR_MSG_UPDATE_ERROR_UID = "Update net link info by error uid";
62b1b8bc3fSopenharmony_ciconstexpr const char *NET_CONN_MANAGER_WORK_THREAD = "NET_CONN_MANAGER_WORK_THREAD";
63b1b8bc3fSopenharmony_ciconstexpr const char *URL_CFG_FILE = "/system/etc/netdetectionurl.conf";
64b1b8bc3fSopenharmony_ciconstexpr const char *HTTP_URL_HEADER = "HttpProbeUrl:";
65b1b8bc3fSopenharmony_ciconstexpr const char NEW_LINE_STR = '\n';
66b1b8bc3fSopenharmony_ciconst uint32_t SYS_PARAMETER_SIZE = 256;
67b1b8bc3fSopenharmony_ciconstexpr const char *CFG_NETWORK_PRE_AIRPLANE_MODE_WAIT_TIMES = "persist.network.pre_airplane_mode_wait_times";
68b1b8bc3fSopenharmony_ciconstexpr const char *NO_DELAY_TIME_CONFIG = "100";
69b1b8bc3fSopenharmony_ciconstexpr const char *SETTINGS_DATASHARE_URI =
70b1b8bc3fSopenharmony_ci        "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
71b1b8bc3fSopenharmony_ciconstexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
72b1b8bc3fSopenharmony_ciconstexpr uint32_t INPUT_VALUE_LENGTH = 10;
73b1b8bc3fSopenharmony_ciconstexpr uint32_t MAX_DELAY_TIME = 200;
74b1b8bc3fSopenharmony_ciconstexpr uint16_t DEFAULT_MTU = 1500;
75b1b8bc3fSopenharmony_ci} // namespace
76b1b8bc3fSopenharmony_ci
77b1b8bc3fSopenharmony_ciconst bool REGISTER_LOCAL_RESULT =
78b1b8bc3fSopenharmony_ci    SystemAbility::MakeAndRegisterAbility(NetConnService::GetInstance().get());
79b1b8bc3fSopenharmony_ci
80b1b8bc3fSopenharmony_ciNetConnService::NetConnService()
81b1b8bc3fSopenharmony_ci    : SystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID, true), registerToService_(false), state_(STATE_STOPPED)
82b1b8bc3fSopenharmony_ci{
83b1b8bc3fSopenharmony_ci}
84b1b8bc3fSopenharmony_ci
85b1b8bc3fSopenharmony_ciNetConnService::~NetConnService()
86b1b8bc3fSopenharmony_ci{
87b1b8bc3fSopenharmony_ci    RemoveALLClientDeathRecipient();
88b1b8bc3fSopenharmony_ci}
89b1b8bc3fSopenharmony_ci
90b1b8bc3fSopenharmony_civoid NetConnService::OnStart()
91b1b8bc3fSopenharmony_ci{
92b1b8bc3fSopenharmony_ci    struct timeval tv;
93b1b8bc3fSopenharmony_ci    gettimeofday(&tv, nullptr);
94b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("OnStart begin");
95b1b8bc3fSopenharmony_ci    if (state_ == STATE_RUNNING) {
96b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("the state is already running");
97b1b8bc3fSopenharmony_ci        return;
98b1b8bc3fSopenharmony_ci    }
99b1b8bc3fSopenharmony_ci    if (!Init()) {
100b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("init failed");
101b1b8bc3fSopenharmony_ci        return;
102b1b8bc3fSopenharmony_ci    }
103b1b8bc3fSopenharmony_ci    state_ = STATE_RUNNING;
104b1b8bc3fSopenharmony_ci    gettimeofday(&tv, nullptr);
105b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("OnStart end");
106b1b8bc3fSopenharmony_ci}
107b1b8bc3fSopenharmony_ci
108b1b8bc3fSopenharmony_civoid NetConnService::CreateDefaultRequest()
109b1b8bc3fSopenharmony_ci{
110b1b8bc3fSopenharmony_ci    if (!defaultNetActivate_) {
111b1b8bc3fSopenharmony_ci        defaultNetSpecifier_ = (std::make_unique<NetSpecifier>()).release();
112b1b8bc3fSopenharmony_ci        defaultNetSpecifier_->SetCapabilities({NET_CAPABILITY_INTERNET, NET_CAPABILITY_NOT_VPN});
113b1b8bc3fSopenharmony_ci        std::weak_ptr<INetActivateCallback> timeoutCb;
114b1b8bc3fSopenharmony_ci        defaultNetActivate_ = std::make_shared<NetActivate>(defaultNetSpecifier_, nullptr, timeoutCb, 0,
115b1b8bc3fSopenharmony_ci                                                            netConnEventHandler_, 0, REQUEST);
116b1b8bc3fSopenharmony_ci        defaultNetActivate_->StartTimeOutNetAvailable();
117b1b8bc3fSopenharmony_ci        defaultNetActivate_->SetRequestId(DEFAULT_REQUEST_ID);
118b1b8bc3fSopenharmony_ci        netActivates_[DEFAULT_REQUEST_ID] = defaultNetActivate_;
119b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("defaultnetcap size = [%{public}zu]", defaultNetSpecifier_->netCapabilities_.netCaps_.size());
120b1b8bc3fSopenharmony_ci    }
121b1b8bc3fSopenharmony_ci}
122b1b8bc3fSopenharmony_ci
123b1b8bc3fSopenharmony_civoid NetConnService::OnStop()
124b1b8bc3fSopenharmony_ci{
125b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("OnStop begin");
126b1b8bc3fSopenharmony_ci    if (netConnEventRunner_) {
127b1b8bc3fSopenharmony_ci        netConnEventRunner_->Stop();
128b1b8bc3fSopenharmony_ci        netConnEventRunner_.reset();
129b1b8bc3fSopenharmony_ci    }
130b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
131b1b8bc3fSopenharmony_ci        netConnEventHandler_.reset();
132b1b8bc3fSopenharmony_ci    }
133b1b8bc3fSopenharmony_ci    state_ = STATE_STOPPED;
134b1b8bc3fSopenharmony_ci    registerToService_ = false;
135b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("OnStop end");
136b1b8bc3fSopenharmony_ci}
137b1b8bc3fSopenharmony_ci
138b1b8bc3fSopenharmony_cibool NetConnService::Init()
139b1b8bc3fSopenharmony_ci{
140b1b8bc3fSopenharmony_ci    if (!REGISTER_LOCAL_RESULT) {
141b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Register to local sa manager failed");
142b1b8bc3fSopenharmony_ci        registerToService_ = false;
143b1b8bc3fSopenharmony_ci        return false;
144b1b8bc3fSopenharmony_ci    }
145b1b8bc3fSopenharmony_ci
146b1b8bc3fSopenharmony_ci    AddSystemAbilityListener(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
147b1b8bc3fSopenharmony_ci
148b1b8bc3fSopenharmony_ci    SubscribeCommonEvent("usual.event.DATA_SHARE_READY",
149b1b8bc3fSopenharmony_ci                         [this](auto && PH1) { OnReceiveEvent(std::forward<decltype(PH1)>(PH1)); });
150b1b8bc3fSopenharmony_ci#ifdef FEATURE_SUPPORT_POWERMANAGER
151b1b8bc3fSopenharmony_ci    SubscribeCommonEvent("usual.event.POWER_MANAGER_STATE_CHANGED",
152b1b8bc3fSopenharmony_ci                         [this](auto && PH1) { OnReceiveEvent(std::forward<decltype(PH1)>(PH1)); });
153b1b8bc3fSopenharmony_ci#endif
154b1b8bc3fSopenharmony_ci
155b1b8bc3fSopenharmony_ci    netConnEventRunner_ = AppExecFwk::EventRunner::Create(NET_CONN_MANAGER_WORK_THREAD);
156b1b8bc3fSopenharmony_ci    if (netConnEventRunner_ == nullptr) {
157b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Create event runner failed.");
158b1b8bc3fSopenharmony_ci        return false;
159b1b8bc3fSopenharmony_ci    }
160b1b8bc3fSopenharmony_ci    netConnEventHandler_ = std::make_shared<NetConnEventHandler>(netConnEventRunner_);
161b1b8bc3fSopenharmony_ci    CreateDefaultRequest();
162b1b8bc3fSopenharmony_ci    serviceIface_ = std::make_unique<NetConnServiceIface>().release();
163b1b8bc3fSopenharmony_ci    NetManagerCenter::GetInstance().RegisterConnService(serviceIface_);
164b1b8bc3fSopenharmony_ci
165b1b8bc3fSopenharmony_ci    interfaceStateCallback_ = new (std::nothrow) NetInterfaceStateCallback();
166b1b8bc3fSopenharmony_ci    if (interfaceStateCallback_) {
167b1b8bc3fSopenharmony_ci        NetsysController::GetInstance().RegisterCallback(interfaceStateCallback_);
168b1b8bc3fSopenharmony_ci    }
169b1b8bc3fSopenharmony_ci    dnsResultCallback_ = std::make_unique<NetDnsResultCallback>().release();
170b1b8bc3fSopenharmony_ci    int32_t regDnsResult = NetsysController::GetInstance().RegisterDnsResultCallback(dnsResultCallback_, 0);
171b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Register Dns Result callback result: [%{public}d]", regDnsResult);
172b1b8bc3fSopenharmony_ci
173b1b8bc3fSopenharmony_ci    netFactoryResetCallback_ = std::make_unique<NetFactoryResetCallback>().release();
174b1b8bc3fSopenharmony_ci    if (netFactoryResetCallback_ == nullptr) {
175b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netFactoryResetCallback_ is nullptr");
176b1b8bc3fSopenharmony_ci    }
177b1b8bc3fSopenharmony_ci    AddSystemAbilityListener(ACCESS_TOKEN_MANAGER_SERVICE_ID);
178b1b8bc3fSopenharmony_ci    AddSystemAbilityListener(COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID);
179b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Init end");
180b1b8bc3fSopenharmony_ci    return true;
181b1b8bc3fSopenharmony_ci}
182b1b8bc3fSopenharmony_ci
183b1b8bc3fSopenharmony_cibool NetConnService::CheckIfSettingsDataReady()
184b1b8bc3fSopenharmony_ci{
185b1b8bc3fSopenharmony_ci    if (isDataShareReady_.load()) {
186b1b8bc3fSopenharmony_ci        return true;
187b1b8bc3fSopenharmony_ci    }
188b1b8bc3fSopenharmony_ci    sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
189b1b8bc3fSopenharmony_ci    if (saManager == nullptr) {
190b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("GetSystemAbilityManager failed.");
191b1b8bc3fSopenharmony_ci        return false;
192b1b8bc3fSopenharmony_ci    }
193b1b8bc3fSopenharmony_ci    sptr<IRemoteObject> dataShareSa = saManager->GetSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
194b1b8bc3fSopenharmony_ci    if (dataShareSa == nullptr) {
195b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Get dataShare SA Failed.");
196b1b8bc3fSopenharmony_ci        return false;
197b1b8bc3fSopenharmony_ci    }
198b1b8bc3fSopenharmony_ci    sptr<IRemoteObject> remoteObj = saManager->GetSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
199b1b8bc3fSopenharmony_ci    if (remoteObj == nullptr) {
200b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("NetDataShareHelperUtils GetSystemAbility Service Failed.");
201b1b8bc3fSopenharmony_ci        return false;
202b1b8bc3fSopenharmony_ci    }
203b1b8bc3fSopenharmony_ci    std::pair<int, std::shared_ptr<DataShare::DataShareHelper>> ret =
204b1b8bc3fSopenharmony_ci            DataShare::DataShareHelper::Create(remoteObj, SETTINGS_DATASHARE_URI, SETTINGS_DATA_EXT_URI);
205b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("create data_share helper, ret=%{public}d", ret.first);
206b1b8bc3fSopenharmony_ci    if (ret.first == DataShare::E_OK) {
207b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("create data_share helper success");
208b1b8bc3fSopenharmony_ci        auto helper = ret.second;
209b1b8bc3fSopenharmony_ci        if (helper != nullptr) {
210b1b8bc3fSopenharmony_ci            bool releaseRet = helper->Release();
211b1b8bc3fSopenharmony_ci            NETMGR_LOG_I("release data_share helper, releaseRet=%{public}d", releaseRet);
212b1b8bc3fSopenharmony_ci        }
213b1b8bc3fSopenharmony_ci        isDataShareReady_ = true;
214b1b8bc3fSopenharmony_ci        return true;
215b1b8bc3fSopenharmony_ci    } else if (ret.first == DataShare::E_DATA_SHARE_NOT_READY) {
216b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("create data_share helper failed");
217b1b8bc3fSopenharmony_ci        isDataShareReady_ = false;
218b1b8bc3fSopenharmony_ci        return false;
219b1b8bc3fSopenharmony_ci    }
220b1b8bc3fSopenharmony_ci    NETMGR_LOG_E("data_share unknown.");
221b1b8bc3fSopenharmony_ci    return true;
222b1b8bc3fSopenharmony_ci}
223b1b8bc3fSopenharmony_ci
224b1b8bc3fSopenharmony_ciint32_t NetConnService::SystemReady()
225b1b8bc3fSopenharmony_ci{
226b1b8bc3fSopenharmony_ci    if (state_ == STATE_RUNNING) {
227b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("System ready.");
228b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
229b1b8bc3fSopenharmony_ci    } else {
230b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
231b1b8bc3fSopenharmony_ci    }
232b1b8bc3fSopenharmony_ci}
233b1b8bc3fSopenharmony_ci
234b1b8bc3fSopenharmony_ci// Do not post into event handler, because this interface should have good performance
235b1b8bc3fSopenharmony_ciint32_t NetConnService::SetInternetPermission(uint32_t uid, uint8_t allow)
236b1b8bc3fSopenharmony_ci{
237b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().SetInternetPermission(uid, allow);
238b1b8bc3fSopenharmony_ci}
239b1b8bc3fSopenharmony_ci
240b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
241b1b8bc3fSopenharmony_ci                                            const std::set<NetCap> &netCaps, uint32_t &supplierId)
242b1b8bc3fSopenharmony_ci{
243b1b8bc3fSopenharmony_ci    std::set<NetCap> tmp = netCaps;
244b1b8bc3fSopenharmony_ci    int32_t callingUid = IPCSkeleton::GetCallingUid();
245b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("RegisterNetSupplier in netcaps size = %{public}zu.", tmp.size());
246b1b8bc3fSopenharmony_ci    if (bearerType != BEARER_VPN) {
247b1b8bc3fSopenharmony_ci        tmp.insert(NET_CAPABILITY_NOT_VPN);
248b1b8bc3fSopenharmony_ci    }
249b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("RegisterNetSupplier out netcaps size = %{public}zu.", tmp.size());
250b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
251b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
252b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, bearerType, &ident, tmp, &supplierId, callingUid, &result]() {
253b1b8bc3fSopenharmony_ci            result = this->RegisterNetSupplierAsync(bearerType, ident, tmp, supplierId, callingUid);
254b1b8bc3fSopenharmony_ci        });
255b1b8bc3fSopenharmony_ci    }
256b1b8bc3fSopenharmony_ci    return result;
257b1b8bc3fSopenharmony_ci}
258b1b8bc3fSopenharmony_ci
259b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetSupplierCallback(uint32_t supplierId, const sptr<INetSupplierCallback> &callback)
260b1b8bc3fSopenharmony_ci{
261b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
262b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
263b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, supplierId, &callback, &result]() {
264b1b8bc3fSopenharmony_ci            result = this->RegisterNetSupplierCallbackAsync(supplierId, callback);
265b1b8bc3fSopenharmony_ci        });
266b1b8bc3fSopenharmony_ci    }
267b1b8bc3fSopenharmony_ci    return result;
268b1b8bc3fSopenharmony_ci}
269b1b8bc3fSopenharmony_ci
270b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetConnCallback(const sptr<INetConnCallback> callback)
271b1b8bc3fSopenharmony_ci{
272b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("RegisterNetConnCallback service in.");
273b1b8bc3fSopenharmony_ci    return RegisterNetConnCallback(defaultNetSpecifier_, callback, 0);
274b1b8bc3fSopenharmony_ci}
275b1b8bc3fSopenharmony_ci
276b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier,
277b1b8bc3fSopenharmony_ci                                                const sptr<INetConnCallback> callback, const uint32_t &timeoutMS)
278b1b8bc3fSopenharmony_ci{
279b1b8bc3fSopenharmony_ci    uint32_t callingUid = static_cast<uint32_t>(IPCSkeleton::GetCallingUid());
280b1b8bc3fSopenharmony_ci
281b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
282b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
283b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, &netSpecifier, &callback, timeoutMS, callingUid, &result]() {
284b1b8bc3fSopenharmony_ci            result = this->RegisterNetConnCallbackAsync(netSpecifier, callback, timeoutMS, callingUid);
285b1b8bc3fSopenharmony_ci        });
286b1b8bc3fSopenharmony_ci    }
287b1b8bc3fSopenharmony_ci    return result;
288b1b8bc3fSopenharmony_ci}
289b1b8bc3fSopenharmony_ci
290b1b8bc3fSopenharmony_ciint32_t NetConnService::RequestNetConnection(const sptr<NetSpecifier> netSpecifier,
291b1b8bc3fSopenharmony_ci                                             const sptr<INetConnCallback> callback, const uint32_t timeoutMS)
292b1b8bc3fSopenharmony_ci{
293b1b8bc3fSopenharmony_ci    uint32_t callingUid = static_cast<uint32_t>(IPCSkeleton::GetCallingUid());
294b1b8bc3fSopenharmony_ci
295b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
296b1b8bc3fSopenharmony_ci    if (netSpecifier == nullptr) {
297b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
298b1b8bc3fSopenharmony_ci    }
299b1b8bc3fSopenharmony_ci    std::set<NetCap> &netCaps = netSpecifier->netCapabilities_.netCaps_;
300b1b8bc3fSopenharmony_ci    if (netCaps.find(NetCap::NET_CAPABILITY_INTERNAL_DEFAULT) != netCaps.end()) {
301b1b8bc3fSopenharmony_ci        if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
302b1b8bc3fSopenharmony_ci                NETMGR_LOG_I("Permission deny: Request with INTERNAL_DEFAULT But not has CONNECTIVITY_INTERNAL");
303b1b8bc3fSopenharmony_ci                return NETMANAGER_ERR_PERMISSION_DENIED;
304b1b8bc3fSopenharmony_ci        }
305b1b8bc3fSopenharmony_ci    } else {
306b1b8bc3fSopenharmony_ci        if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
307b1b8bc3fSopenharmony_ci                NETMGR_LOG_I("Permission deny: request need GET_NETWORK_INFO permission");
308b1b8bc3fSopenharmony_ci                return NETMANAGER_ERR_PERMISSION_DENIED;
309b1b8bc3fSopenharmony_ci        }
310b1b8bc3fSopenharmony_ci    }
311b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
312b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, netSpecifier, callback, timeoutMS, &callingUid, &result]() {
313b1b8bc3fSopenharmony_ci            result = this->RequestNetConnectionAsync(netSpecifier, callback, timeoutMS, callingUid);
314b1b8bc3fSopenharmony_ci        });
315b1b8bc3fSopenharmony_ci    }
316b1b8bc3fSopenharmony_ci    return result;
317b1b8bc3fSopenharmony_ci}
318b1b8bc3fSopenharmony_ci
319b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
320b1b8bc3fSopenharmony_ci{
321b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter RegisterNetDetectionCallback");
322b1b8bc3fSopenharmony_ci    return RegUnRegNetDetectionCallback(netId, callback, true);
323b1b8bc3fSopenharmony_ci}
324b1b8bc3fSopenharmony_ci
325b1b8bc3fSopenharmony_ciint32_t NetConnService::UnregisterNetSupplier(uint32_t supplierId)
326b1b8bc3fSopenharmony_ci{
327b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
328b1b8bc3fSopenharmony_ci    int32_t callingUid = IPCSkeleton::GetCallingUid();
329b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
330b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, supplierId, &callingUid, &result]() {
331b1b8bc3fSopenharmony_ci            result = this->UnregisterNetSupplierAsync(supplierId, false, callingUid);
332b1b8bc3fSopenharmony_ci        });
333b1b8bc3fSopenharmony_ci    }
334b1b8bc3fSopenharmony_ci    return result;
335b1b8bc3fSopenharmony_ci}
336b1b8bc3fSopenharmony_ci
337b1b8bc3fSopenharmony_ciint32_t NetConnService::UnregisterNetConnCallback(const sptr<INetConnCallback> &callback)
338b1b8bc3fSopenharmony_ci{
339b1b8bc3fSopenharmony_ci    uint32_t callingUid = static_cast<uint32_t>(IPCSkeleton::GetCallingUid());
340b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
341b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
342b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, &callback, callingUid, &result]() {
343b1b8bc3fSopenharmony_ci            result = this->UnregisterNetConnCallbackAsync(callback, callingUid);
344b1b8bc3fSopenharmony_ci        });
345b1b8bc3fSopenharmony_ci    }
346b1b8bc3fSopenharmony_ci    return result;
347b1b8bc3fSopenharmony_ci}
348b1b8bc3fSopenharmony_ci
349b1b8bc3fSopenharmony_ciint32_t NetConnService::UnRegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
350b1b8bc3fSopenharmony_ci{
351b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter UnRegisterNetDetectionCallback");
352b1b8bc3fSopenharmony_ci    return RegUnRegNetDetectionCallback(netId, callback, false);
353b1b8bc3fSopenharmony_ci}
354b1b8bc3fSopenharmony_ci
355b1b8bc3fSopenharmony_ciint32_t NetConnService::RegUnRegNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback,
356b1b8bc3fSopenharmony_ci                                                     bool isReg)
357b1b8bc3fSopenharmony_ci{
358b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
359b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
360b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, netId, &callback, isReg, &result]() {
361b1b8bc3fSopenharmony_ci            result = this->RegUnRegNetDetectionCallbackAsync(netId, callback, isReg);
362b1b8bc3fSopenharmony_ci        });
363b1b8bc3fSopenharmony_ci    }
364b1b8bc3fSopenharmony_ci    return result;
365b1b8bc3fSopenharmony_ci}
366b1b8bc3fSopenharmony_ci
367b1b8bc3fSopenharmony_ciint32_t NetConnService::UpdateNetStateForTest(const sptr<NetSpecifier> &netSpecifier, int32_t netState)
368b1b8bc3fSopenharmony_ci{
369b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
370b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
371b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, &netSpecifier, netState, &result]() {
372b1b8bc3fSopenharmony_ci            result = this->UpdateNetStateForTestAsync(netSpecifier, netState);
373b1b8bc3fSopenharmony_ci        });
374b1b8bc3fSopenharmony_ci    }
375b1b8bc3fSopenharmony_ci    return result;
376b1b8bc3fSopenharmony_ci}
377b1b8bc3fSopenharmony_ci
378b1b8bc3fSopenharmony_ciint32_t NetConnService::UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo)
379b1b8bc3fSopenharmony_ci{
380b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
381b1b8bc3fSopenharmony_ci    int32_t callingUid = IPCSkeleton::GetCallingUid();
382b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
383b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, supplierId, &netSupplierInfo, callingUid, &result]() {
384b1b8bc3fSopenharmony_ci            result = this->UpdateNetSupplierInfoAsync(supplierId, netSupplierInfo, callingUid);
385b1b8bc3fSopenharmony_ci        });
386b1b8bc3fSopenharmony_ci    }
387b1b8bc3fSopenharmony_ci    return result;
388b1b8bc3fSopenharmony_ci}
389b1b8bc3fSopenharmony_ci
390b1b8bc3fSopenharmony_ciint32_t NetConnService::UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo)
391b1b8bc3fSopenharmony_ci{
392b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
393b1b8bc3fSopenharmony_ci    int32_t callingUid = IPCSkeleton::GetCallingUid();
394b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
395b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, supplierId, &netLinkInfo, callingUid, &result]() {
396b1b8bc3fSopenharmony_ci            result = this->UpdateNetLinkInfoAsync(supplierId, netLinkInfo, callingUid);
397b1b8bc3fSopenharmony_ci        });
398b1b8bc3fSopenharmony_ci    }
399b1b8bc3fSopenharmony_ci    return result;
400b1b8bc3fSopenharmony_ci}
401b1b8bc3fSopenharmony_ci
402b1b8bc3fSopenharmony_ciint32_t NetConnService::NetDetection(int32_t netId)
403b1b8bc3fSopenharmony_ci{
404b1b8bc3fSopenharmony_ci    int32_t callingUid = IPCSkeleton::GetCallingUid();
405b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("NetDetection, call uid [%{public}d]", callingUid);
406b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
407b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
408b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, netId, &result]() { result = this->NetDetectionAsync(netId); });
409b1b8bc3fSopenharmony_ci    }
410b1b8bc3fSopenharmony_ci    return result;
411b1b8bc3fSopenharmony_ci}
412b1b8bc3fSopenharmony_ci
413b1b8bc3fSopenharmony_ciint32_t NetConnService::RestrictBackgroundChanged(bool restrictBackground)
414b1b8bc3fSopenharmony_ci{
415b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
416b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
417b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, restrictBackground, &result]() {
418b1b8bc3fSopenharmony_ci            result = this->RestrictBackgroundChangedAsync(restrictBackground);
419b1b8bc3fSopenharmony_ci        });
420b1b8bc3fSopenharmony_ci    }
421b1b8bc3fSopenharmony_ci    return result;
422b1b8bc3fSopenharmony_ci}
423b1b8bc3fSopenharmony_ci
424b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetSupplierAsync(NetBearType bearerType, const std::string &ident,
425b1b8bc3fSopenharmony_ci                                                 const std::set<NetCap> &netCaps, uint32_t &supplierId,
426b1b8bc3fSopenharmony_ci                                                 int32_t callingUid)
427b1b8bc3fSopenharmony_ci{
428b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("RegisterNetSupplier service in, bearerType[%{public}u], ident[%{public}s]",
429b1b8bc3fSopenharmony_ci                 static_cast<uint32_t>(bearerType), ident.c_str());
430b1b8bc3fSopenharmony_ci    // If there is no supplier in the list, create a supplier
431b1b8bc3fSopenharmony_ci    if (bearerType < BEARER_CELLULAR || bearerType >= BEARER_DEFAULT) {
432b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netType parameter invalid");
433b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NET_TYPE_NOT_FOUND;
434b1b8bc3fSopenharmony_ci    }
435b1b8bc3fSopenharmony_ci    sptr<NetSupplier> supplier = GetNetSupplierFromList(bearerType, ident, netCaps);
436b1b8bc3fSopenharmony_ci    if (supplier != nullptr) {
437b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Supplier[%{public}d %{public}s] already exists.", supplier->GetSupplierId(), ident.c_str());
438b1b8bc3fSopenharmony_ci        supplierId = supplier->GetSupplierId();
439b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
440b1b8bc3fSopenharmony_ci    }
441b1b8bc3fSopenharmony_ci    // If there is no supplier in the list, create a supplier
442b1b8bc3fSopenharmony_ci    supplier = (std::make_unique<NetSupplier>(bearerType, ident, netCaps)).release();
443b1b8bc3fSopenharmony_ci    if (supplier == nullptr) {
444b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("supplier is nullptr");
445b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NO_SUPPLIER;
446b1b8bc3fSopenharmony_ci    }
447b1b8bc3fSopenharmony_ci    supplierId = supplier->GetSupplierId();
448b1b8bc3fSopenharmony_ci    // create network
449b1b8bc3fSopenharmony_ci    bool isContainInternal = netCaps.find(NetCap::NET_CAPABILITY_INTERNAL_DEFAULT) != netCaps.end();
450b1b8bc3fSopenharmony_ci    int32_t netId = isContainInternal ? GenerateInternalNetId() : GenerateNetId();
451b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("GenerateNetId is: [%{public}d], bearerType: %{public}d, supplierId: %{public}d",
452b1b8bc3fSopenharmony_ci        netId, bearerType, supplierId);
453b1b8bc3fSopenharmony_ci    if (netId == INVALID_NET_ID) {
454b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("GenerateNetId fail");
455b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_INVALID_NETWORK;
456b1b8bc3fSopenharmony_ci    }
457b1b8bc3fSopenharmony_ci    std::shared_ptr<Network> network = std::make_shared<Network>(
458b1b8bc3fSopenharmony_ci        netId, supplierId,
459b1b8bc3fSopenharmony_ci        std::bind(&NetConnService::HandleDetectionResult, shared_from_this(),
460b1b8bc3fSopenharmony_ci            std::placeholders::_1, std::placeholders::_2),
461b1b8bc3fSopenharmony_ci        bearerType, netConnEventHandler_);
462b1b8bc3fSopenharmony_ci    network->SetNetCaps(netCaps);
463b1b8bc3fSopenharmony_ci    supplier->SetNetwork(network);
464b1b8bc3fSopenharmony_ci    supplier->SetUid(callingUid);
465b1b8bc3fSopenharmony_ci    // save supplier
466b1b8bc3fSopenharmony_ci    std::unique_lock<std::recursive_mutex> locker(netManagerMutex_);
467b1b8bc3fSopenharmony_ci    netSuppliers_[supplierId] = supplier;
468b1b8bc3fSopenharmony_ci    networks_[netId] = network;
469b1b8bc3fSopenharmony_ci    locker.unlock();
470b1b8bc3fSopenharmony_ci    struct EventInfo eventInfo = {.netId = netId, .bearerType = bearerType, .ident = ident, .supplierId = supplierId};
471b1b8bc3fSopenharmony_ci    EventReport::SendSupplierBehaviorEvent(eventInfo);
472b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("RegisterNetSupplier service out, supplier[%{public}d %{public}s] netId[%{public}d]", supplierId,
473b1b8bc3fSopenharmony_ci                 ident.c_str(), netId);
474b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
475b1b8bc3fSopenharmony_ci}
476b1b8bc3fSopenharmony_ci
477b1b8bc3fSopenharmony_civoid NetConnService::OnNetSupplierRemoteDied(const wptr<IRemoteObject> &remoteObject)
478b1b8bc3fSopenharmony_ci{
479b1b8bc3fSopenharmony_ci    sptr<IRemoteObject> diedRemoted = remoteObject.promote();
480b1b8bc3fSopenharmony_ci    if (diedRemoted == nullptr || netConnEventHandler_ == nullptr) {
481b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("diedRemoted or netConnEventHandler_ is null");
482b1b8bc3fSopenharmony_ci        return;
483b1b8bc3fSopenharmony_ci    }
484b1b8bc3fSopenharmony_ci    uint32_t callingUid = static_cast<uint32_t>(IPCSkeleton::GetCallingUid());
485b1b8bc3fSopenharmony_ci    uint32_t tmpSupplierId = INVALID_SUPPLIER_ID;
486b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("OnNetSupplierRemoteDied, callingUid=%{public}u", callingUid);
487b1b8bc3fSopenharmony_ci    sptr<INetSupplierCallback> callback = iface_cast<INetSupplierCallback>(diedRemoted);
488b1b8bc3fSopenharmony_ci
489b1b8bc3fSopenharmony_ci    netConnEventHandler_->PostSyncTask([this, &tmpSupplierId, callingUid, &callback]() {
490b1b8bc3fSopenharmony_ci        for (const auto &supplier : netSuppliers_) {
491b1b8bc3fSopenharmony_ci            if (supplier.second == nullptr || supplier.second->GetSupplierCallback() == nullptr) {
492b1b8bc3fSopenharmony_ci                continue;
493b1b8bc3fSopenharmony_ci            }
494b1b8bc3fSopenharmony_ci            if (supplier.second->GetSupplierCallback()->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
495b1b8bc3fSopenharmony_ci                tmpSupplierId = supplier.second->GetSupplierId();
496b1b8bc3fSopenharmony_ci                break;
497b1b8bc3fSopenharmony_ci            }
498b1b8bc3fSopenharmony_ci        }
499b1b8bc3fSopenharmony_ci        if (tmpSupplierId != INVALID_SUPPLIER_ID) {
500b1b8bc3fSopenharmony_ci            NETMGR_LOG_I("OnNetSupplierRemoteDied UnregisterNetSupplier SupplierId %{public}u", tmpSupplierId);
501b1b8bc3fSopenharmony_ci            UnregisterNetSupplierAsync(tmpSupplierId, true, callingUid);
502b1b8bc3fSopenharmony_ci        }
503b1b8bc3fSopenharmony_ci    });
504b1b8bc3fSopenharmony_ci}
505b1b8bc3fSopenharmony_ci
506b1b8bc3fSopenharmony_civoid NetConnService::RemoveNetSupplierDeathRecipient(const sptr<INetSupplierCallback> &callback)
507b1b8bc3fSopenharmony_ci{
508b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
509b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("RemoveNetSupplierDeathRecipient is null");
510b1b8bc3fSopenharmony_ci        return;
511b1b8bc3fSopenharmony_ci    }
512b1b8bc3fSopenharmony_ci    callback->AsObject()->RemoveDeathRecipient(netSuplierDeathRecipient_);
513b1b8bc3fSopenharmony_ci}
514b1b8bc3fSopenharmony_ci
515b1b8bc3fSopenharmony_civoid NetConnService::AddNetSupplierDeathRecipient(const sptr<INetSupplierCallback> &callback)
516b1b8bc3fSopenharmony_ci{
517b1b8bc3fSopenharmony_ci    if (netSuplierDeathRecipient_ == nullptr) {
518b1b8bc3fSopenharmony_ci        netSuplierDeathRecipient_ = new (std::nothrow) NetSupplierCallbackDeathRecipient(*this);
519b1b8bc3fSopenharmony_ci    }
520b1b8bc3fSopenharmony_ci    if (netSuplierDeathRecipient_ == nullptr) {
521b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netSuplierDeathRecipient_ is null");
522b1b8bc3fSopenharmony_ci        return;
523b1b8bc3fSopenharmony_ci    }
524b1b8bc3fSopenharmony_ci    if (!callback->AsObject()->AddDeathRecipient(netSuplierDeathRecipient_)) {
525b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("AddNetSupplierDeathRecipient failed");
526b1b8bc3fSopenharmony_ci        return;
527b1b8bc3fSopenharmony_ci    }
528b1b8bc3fSopenharmony_ci}
529b1b8bc3fSopenharmony_ci
530b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetSupplierCallbackAsync(uint32_t supplierId,
531b1b8bc3fSopenharmony_ci                                                         const sptr<INetSupplierCallback> &callback)
532b1b8bc3fSopenharmony_ci{
533b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("RegisterNetSupplierCallback service in, supplierId[%{public}d]", supplierId);
534b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
535b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("The parameter callback is null");
536b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
537b1b8bc3fSopenharmony_ci    }
538b1b8bc3fSopenharmony_ci    auto supplier = FindNetSupplier(supplierId);
539b1b8bc3fSopenharmony_ci    if (supplier == nullptr) {
540b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("supplier doesn't exist.");
541b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NO_SUPPLIER;
542b1b8bc3fSopenharmony_ci    }
543b1b8bc3fSopenharmony_ci    supplier->RegisterSupplierCallback(callback);
544b1b8bc3fSopenharmony_ci    SendAllRequestToNetwork(supplier);
545b1b8bc3fSopenharmony_ci    AddNetSupplierDeathRecipient(callback);
546b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("RegisterNetSupplierCallback service out");
547b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
548b1b8bc3fSopenharmony_ci}
549b1b8bc3fSopenharmony_ci
550b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetConnCallbackAsync(const sptr<NetSpecifier> &netSpecifier,
551b1b8bc3fSopenharmony_ci                                                     const sptr<INetConnCallback> &callback, const uint32_t &timeoutMS,
552b1b8bc3fSopenharmony_ci                                                     const uint32_t callingUid)
553b1b8bc3fSopenharmony_ci{
554b1b8bc3fSopenharmony_ci    if (netSpecifier == nullptr || callback == nullptr) {
555b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("The parameter of netSpecifier or callback is null");
556b1b8bc3fSopenharmony_ci        struct EventInfo eventInfo = {.errorType = static_cast<int32_t>(FAULT_INVALID_PARAMETER),
557b1b8bc3fSopenharmony_ci                                      .errorMsg = ERROR_MSG_NULL_NET_SPECIFIER};
558b1b8bc3fSopenharmony_ci        EventReport::SendRequestFaultEvent(eventInfo);
559b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
560b1b8bc3fSopenharmony_ci    }
561b1b8bc3fSopenharmony_ci    uint32_t reqId = 0;
562b1b8bc3fSopenharmony_ci    if (FindSameCallback(callback, reqId)) {
563b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("FindSameCallback callUid:%{public}u reqId:%{public}u", callingUid,
564b1b8bc3fSopenharmony_ci                     reqId);
565b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_SAME_CALLBACK;
566b1b8bc3fSopenharmony_ci    }
567b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Register net connect callback async, callUid[%{public}u], reqId[%{public}u]", callingUid, reqId);
568b1b8bc3fSopenharmony_ci    int32_t ret = IncreaseNetConnCallbackCntForUid(callingUid);
569b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
570b1b8bc3fSopenharmony_ci        return ret;
571b1b8bc3fSopenharmony_ci    }
572b1b8bc3fSopenharmony_ci    AddClientDeathRecipient(callback);
573b1b8bc3fSopenharmony_ci    return ActivateNetwork(netSpecifier, callback, timeoutMS, REGISTER, callingUid);
574b1b8bc3fSopenharmony_ci}
575b1b8bc3fSopenharmony_ci
576b1b8bc3fSopenharmony_ciint32_t NetConnService::RequestNetConnectionAsync(const sptr<NetSpecifier> &netSpecifier,
577b1b8bc3fSopenharmony_ci                                                  const sptr<INetConnCallback> &callback, const uint32_t &timeoutMS,
578b1b8bc3fSopenharmony_ci                                                  const uint32_t callingUid)
579b1b8bc3fSopenharmony_ci{
580b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Request net connect callback async, call uid [%{public}u]", callingUid);
581b1b8bc3fSopenharmony_ci    if (netSpecifier == nullptr || callback == nullptr) {
582b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("The parameter of netSpecifier or callback is null");
583b1b8bc3fSopenharmony_ci        struct EventInfo eventInfo = {.errorType = static_cast<int32_t>(FAULT_INVALID_PARAMETER),
584b1b8bc3fSopenharmony_ci                                      .errorMsg = ERROR_MSG_NULL_NET_SPECIFIER};
585b1b8bc3fSopenharmony_ci        EventReport::SendRequestFaultEvent(eventInfo);
586b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
587b1b8bc3fSopenharmony_ci    }
588b1b8bc3fSopenharmony_ci    uint32_t reqId = 0;
589b1b8bc3fSopenharmony_ci    if (FindSameCallback(callback, reqId)) {
590b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("RequestNetConnection found same callback");
591b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_SAME_CALLBACK;
592b1b8bc3fSopenharmony_ci    }
593b1b8bc3fSopenharmony_ci    int32_t ret = IncreaseNetConnCallbackCntForUid(callingUid, REQUEST);
594b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
595b1b8bc3fSopenharmony_ci        return ret;
596b1b8bc3fSopenharmony_ci    }
597b1b8bc3fSopenharmony_ci    AddClientDeathRecipient(callback);
598b1b8bc3fSopenharmony_ci    return ActivateNetwork(netSpecifier, callback, timeoutMS, REQUEST, callingUid);
599b1b8bc3fSopenharmony_ci}
600b1b8bc3fSopenharmony_ci
601b1b8bc3fSopenharmony_ciint32_t NetConnService::UnregisterNetSupplierAsync(uint32_t supplierId, bool ignoreUid, int32_t callingUid)
602b1b8bc3fSopenharmony_ci{
603b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("UnregisterNetSupplier service in, supplierId[%{public}d]", supplierId);
604b1b8bc3fSopenharmony_ci    // Remove supplier from the list based on supplierId
605b1b8bc3fSopenharmony_ci    auto supplier = FindNetSupplier(supplierId);
606b1b8bc3fSopenharmony_ci    if (supplier == nullptr) {
607b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("supplier doesn't exist.");
608b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NO_SUPPLIER;
609b1b8bc3fSopenharmony_ci    }
610b1b8bc3fSopenharmony_ci    if (!ignoreUid && CheckAndCompareUid(supplier, callingUid) != NETMANAGER_SUCCESS) {
611b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("UnregisterNetSupplierAsync uid[%{public}d] is not equal to callingUid[%{public}d].",
612b1b8bc3fSopenharmony_ci                     supplier->GetUid(), callingUid);
613b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
614b1b8bc3fSopenharmony_ci    }
615b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Unregister supplier[%{public}d, %{public}d, %{public}s], defaultNetSupplier[%{public}d], %{public}s]",
616b1b8bc3fSopenharmony_ci                 supplier->GetSupplierId(), supplier->GetUid(), supplier->GetNetSupplierIdent().c_str(),
617b1b8bc3fSopenharmony_ci                 defaultNetSupplier_ ? defaultNetSupplier_->GetSupplierId() : 0,
618b1b8bc3fSopenharmony_ci                 defaultNetSupplier_ ? defaultNetSupplier_->GetNetSupplierIdent().c_str() : "null");
619b1b8bc3fSopenharmony_ci
620b1b8bc3fSopenharmony_ci    struct EventInfo eventInfo = {.bearerType = supplier->GetNetSupplierType(),
621b1b8bc3fSopenharmony_ci                                  .ident = supplier->GetNetSupplierIdent(),
622b1b8bc3fSopenharmony_ci                                  .supplierId = supplier->GetSupplierId()};
623b1b8bc3fSopenharmony_ci    EventReport::SendSupplierBehaviorEvent(eventInfo);
624b1b8bc3fSopenharmony_ci
625b1b8bc3fSopenharmony_ci    int32_t netId = supplier->GetNetId();
626b1b8bc3fSopenharmony_ci    NET_NETWORK_MAP::iterator iterNetwork = networks_.find(netId);
627b1b8bc3fSopenharmony_ci    if (iterNetwork != networks_.end()) {
628b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("the iterNetwork already exists.");
629b1b8bc3fSopenharmony_ci        std::unique_lock<std::recursive_mutex> locker(netManagerMutex_);
630b1b8bc3fSopenharmony_ci        networks_.erase(iterNetwork);
631b1b8bc3fSopenharmony_ci        locker.unlock();
632b1b8bc3fSopenharmony_ci    }
633b1b8bc3fSopenharmony_ci    if (defaultNetSupplier_ == supplier) {
634b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("Set default net supplier to nullptr.");
635b1b8bc3fSopenharmony_ci        sptr<NetSupplier> newSupplier = nullptr;
636b1b8bc3fSopenharmony_ci        MakeDefaultNetWork(defaultNetSupplier_, newSupplier);
637b1b8bc3fSopenharmony_ci    }
638b1b8bc3fSopenharmony_ci    NetSupplierInfo info;
639b1b8bc3fSopenharmony_ci    supplier->UpdateNetSupplierInfo(info);
640b1b8bc3fSopenharmony_ci    RemoveNetSupplierDeathRecipient(supplier->GetSupplierCallback());
641b1b8bc3fSopenharmony_ci    std::unique_lock<std::recursive_mutex> locker(netManagerMutex_);
642b1b8bc3fSopenharmony_ci    netSuppliers_.erase(supplierId);
643b1b8bc3fSopenharmony_ci    locker.unlock();
644b1b8bc3fSopenharmony_ci    FindBestNetworkForAllRequest();
645b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("UnregisterNetSupplier supplierId[%{public}d] out", supplierId);
646b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
647b1b8bc3fSopenharmony_ci}
648b1b8bc3fSopenharmony_ci
649b1b8bc3fSopenharmony_ciint32_t NetConnService::CheckAndCompareUid(sptr<NetSupplier> &supplier, int32_t callingUid)
650b1b8bc3fSopenharmony_ci{
651b1b8bc3fSopenharmony_ci    int32_t uid = supplier->GetUid();
652b1b8bc3fSopenharmony_ci    if (uid != callingUid) {
653b1b8bc3fSopenharmony_ci        struct EventInfo eventInfo = {
654b1b8bc3fSopenharmony_ci            .errorType = static_cast<int32_t>(NETMANAGER_ERR_INVALID_PARAMETER),
655b1b8bc3fSopenharmony_ci            .errorMsg = std::string(ERROR_MSG_UPDATE_ERROR_UID) +
656b1b8bc3fSopenharmony_ci                        std::to_string(callingUid)
657b1b8bc3fSopenharmony_ci        };
658b1b8bc3fSopenharmony_ci        EventReport::SendSupplierFaultEvent(eventInfo);
659b1b8bc3fSopenharmony_ci    }
660b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
661b1b8bc3fSopenharmony_ci}
662b1b8bc3fSopenharmony_ci
663b1b8bc3fSopenharmony_ci#ifdef FEATURE_SUPPORT_POWERMANAGER
664b1b8bc3fSopenharmony_civoid NetConnService::StopAllNetDetection()
665b1b8bc3fSopenharmony_ci{
666b1b8bc3fSopenharmony_ci    for (const auto& pNetSupplier : netSuppliers_) {
667b1b8bc3fSopenharmony_ci        if (pNetSupplier.second == nullptr) {
668b1b8bc3fSopenharmony_ci            continue;
669b1b8bc3fSopenharmony_ci        }
670b1b8bc3fSopenharmony_ci        std::shared_ptr<Network> pNetwork = pNetSupplier.second->GetNetwork();
671b1b8bc3fSopenharmony_ci        if (pNetwork == nullptr) {
672b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("pNetwork is null, id:%{public}d", pNetSupplier.first);
673b1b8bc3fSopenharmony_ci            continue;
674b1b8bc3fSopenharmony_ci        }
675b1b8bc3fSopenharmony_ci        pNetwork->StopNetDetection();
676b1b8bc3fSopenharmony_ci        pNetwork->UpdateForbidDetectionFlag(true);
677b1b8bc3fSopenharmony_ci    }
678b1b8bc3fSopenharmony_ci}
679b1b8bc3fSopenharmony_ci
680b1b8bc3fSopenharmony_civoid NetConnService::StartAllNetDetection()
681b1b8bc3fSopenharmony_ci{
682b1b8bc3fSopenharmony_ci    for (const auto& pNetSupplier : netSuppliers_) {
683b1b8bc3fSopenharmony_ci        if (pNetSupplier.second == nullptr) {
684b1b8bc3fSopenharmony_ci            continue;
685b1b8bc3fSopenharmony_ci        }
686b1b8bc3fSopenharmony_ci        std::shared_ptr<Network> pNetwork = pNetSupplier.second->GetNetwork();
687b1b8bc3fSopenharmony_ci        if (pNetwork == nullptr) {
688b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("pNetwork is null, id:%{public}d", pNetSupplier.first);
689b1b8bc3fSopenharmony_ci            continue;
690b1b8bc3fSopenharmony_ci        }
691b1b8bc3fSopenharmony_ci        pNetwork->UpdateForbidDetectionFlag(false);
692b1b8bc3fSopenharmony_ci    }
693b1b8bc3fSopenharmony_ci    if ((defaultNetSupplier_ == nullptr)) {
694b1b8bc3fSopenharmony_ci        NETMGR_LOG_W("defaultNetSupplier_ is  null");
695b1b8bc3fSopenharmony_ci        return;
696b1b8bc3fSopenharmony_ci    }
697b1b8bc3fSopenharmony_ci    std::shared_ptr<Network> pDefaultNetwork = defaultNetSupplier_->GetNetwork();
698b1b8bc3fSopenharmony_ci    if (pDefaultNetwork == nullptr) {
699b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("pDefaultNetwork is null");
700b1b8bc3fSopenharmony_ci        return;
701b1b8bc3fSopenharmony_ci    }
702b1b8bc3fSopenharmony_ci    pDefaultNetwork->StartNetDetection(false);
703b1b8bc3fSopenharmony_ci}
704b1b8bc3fSopenharmony_ci
705b1b8bc3fSopenharmony_civoid NetConnService::HandlePowerMgrEvent(int code)
706b1b8bc3fSopenharmony_ci{
707b1b8bc3fSopenharmony_ci    if (code == STATE_ENTER_FORCESLEEP || code == STATE_ENTER_SLEEP_NOT_FORCE) {
708b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("on receive enter sleep, code %{public}d.", code);
709b1b8bc3fSopenharmony_ci        if (netConnEventHandler_) {
710b1b8bc3fSopenharmony_ci            netConnEventHandler_->PostSyncTask([this]() {
711b1b8bc3fSopenharmony_ci                this->StopAllNetDetection();
712b1b8bc3fSopenharmony_ci            });
713b1b8bc3fSopenharmony_ci        }
714b1b8bc3fSopenharmony_ci    } else if (code == STATE_EXIT_FORCESLEEP || code == STATE_EXIT_SLEEP_NOT_FORCE) {
715b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("on receive exit sleep, code %{public}d.", code);
716b1b8bc3fSopenharmony_ci        if (netConnEventHandler_) {
717b1b8bc3fSopenharmony_ci            netConnEventHandler_->PostSyncTask([this]() {
718b1b8bc3fSopenharmony_ci                this->StartAllNetDetection();
719b1b8bc3fSopenharmony_ci            });
720b1b8bc3fSopenharmony_ci        }
721b1b8bc3fSopenharmony_ci    }
722b1b8bc3fSopenharmony_ci}
723b1b8bc3fSopenharmony_ci#endif
724b1b8bc3fSopenharmony_ci
725b1b8bc3fSopenharmony_ciint32_t NetConnService::UnregisterNetConnCallbackAsync(const sptr<INetConnCallback> &callback,
726b1b8bc3fSopenharmony_ci                                                       const uint32_t callingUid)
727b1b8bc3fSopenharmony_ci{
728b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
729b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("callback is null, callUid[%{public}u]", callingUid);
730b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
731b1b8bc3fSopenharmony_ci    }
732b1b8bc3fSopenharmony_ci    RegisterType registerType = INVALIDTYPE;
733b1b8bc3fSopenharmony_ci    uint32_t reqId = 0;
734b1b8bc3fSopenharmony_ci    if (!FindSameCallback(callback, reqId, registerType) || registerType == INVALIDTYPE) {
735b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("NotFindSameCallback callUid:%{public}u reqId:%{public}u",
736b1b8bc3fSopenharmony_ci                     callingUid, reqId);
737b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_CALLBACK_NOT_FOUND;
738b1b8bc3fSopenharmony_ci    }
739b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("start, callUid:%{public}u, reqId:%{public}u", callingUid, reqId);
740b1b8bc3fSopenharmony_ci    DecreaseNetConnCallbackCntForUid(callingUid, registerType);
741b1b8bc3fSopenharmony_ci    DecreaseNetActivatesForUid(callingUid, callback);
742b1b8bc3fSopenharmony_ci    DecreaseNetActivates(callingUid, callback, reqId);
743b1b8bc3fSopenharmony_ci
744b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
745b1b8bc3fSopenharmony_ci}
746b1b8bc3fSopenharmony_ci
747b1b8bc3fSopenharmony_ciint32_t NetConnService::IncreaseNetConnCallbackCntForUid(const uint32_t callingUid, const RegisterType registerType)
748b1b8bc3fSopenharmony_ci{
749b1b8bc3fSopenharmony_ci    auto &netUidRequest = registerType == REGISTER ?
750b1b8bc3fSopenharmony_ci        netUidRequest_ : internalDefaultUidRequest_;
751b1b8bc3fSopenharmony_ci    auto requestNetwork = netUidRequest.find(callingUid);
752b1b8bc3fSopenharmony_ci    if (requestNetwork == netUidRequest.end()) {
753b1b8bc3fSopenharmony_ci        netUidRequest.insert(std::make_pair(callingUid, 1));
754b1b8bc3fSopenharmony_ci    } else {
755b1b8bc3fSopenharmony_ci        if (requestNetwork->second >= MAX_ALLOW_UID_NUM) {
756b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("return falied for UID [%{public}d] has registered over [%{public}d] callback",
757b1b8bc3fSopenharmony_ci                         callingUid, MAX_ALLOW_UID_NUM);
758b1b8bc3fSopenharmony_ci            return NET_CONN_ERR_NET_OVER_MAX_REQUEST_NUM;
759b1b8bc3fSopenharmony_ci        } else {
760b1b8bc3fSopenharmony_ci            requestNetwork->second++;
761b1b8bc3fSopenharmony_ci        }
762b1b8bc3fSopenharmony_ci    }
763b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
764b1b8bc3fSopenharmony_ci}
765b1b8bc3fSopenharmony_ci
766b1b8bc3fSopenharmony_civoid NetConnService::DecreaseNetConnCallbackCntForUid(const uint32_t callingUid, const RegisterType registerType)
767b1b8bc3fSopenharmony_ci{
768b1b8bc3fSopenharmony_ci    auto &netUidRequest = registerType == REGISTER ?
769b1b8bc3fSopenharmony_ci        netUidRequest_ : internalDefaultUidRequest_;
770b1b8bc3fSopenharmony_ci    auto requestNetwork = netUidRequest.find(callingUid);
771b1b8bc3fSopenharmony_ci    if (requestNetwork == netUidRequest.end()) {
772b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Could not find the request calling uid");
773b1b8bc3fSopenharmony_ci    } else {
774b1b8bc3fSopenharmony_ci        if (requestNetwork->second >= 1) {
775b1b8bc3fSopenharmony_ci            requestNetwork->second--;
776b1b8bc3fSopenharmony_ci        }
777b1b8bc3fSopenharmony_ci        if (requestNetwork->second == 0) {
778b1b8bc3fSopenharmony_ci            netUidRequest.erase(requestNetwork);
779b1b8bc3fSopenharmony_ci        }
780b1b8bc3fSopenharmony_ci    }
781b1b8bc3fSopenharmony_ci}
782b1b8bc3fSopenharmony_ci
783b1b8bc3fSopenharmony_civoid NetConnService::DecreaseNetActivatesForUid(const uint32_t callingUid, const sptr<INetConnCallback> &callback)
784b1b8bc3fSopenharmony_ci{
785b1b8bc3fSopenharmony_ci    auto it = netUidActivates_.find(callingUid);
786b1b8bc3fSopenharmony_ci    if (it != netUidActivates_.end()) {
787b1b8bc3fSopenharmony_ci        std::vector<std::shared_ptr<NetActivate>> &activates = it->second;
788b1b8bc3fSopenharmony_ci        for (auto iter = activates.begin(); iter != activates.end();) {
789b1b8bc3fSopenharmony_ci            if ((*iter)->GetNetCallback() == callback) {
790b1b8bc3fSopenharmony_ci                iter = activates.erase(iter);
791b1b8bc3fSopenharmony_ci                break;
792b1b8bc3fSopenharmony_ci            } else {
793b1b8bc3fSopenharmony_ci                ++iter;
794b1b8bc3fSopenharmony_ci            }
795b1b8bc3fSopenharmony_ci        }
796b1b8bc3fSopenharmony_ci        if (activates.empty()) {
797b1b8bc3fSopenharmony_ci            netUidActivates_.erase(it);
798b1b8bc3fSopenharmony_ci        }
799b1b8bc3fSopenharmony_ci    }
800b1b8bc3fSopenharmony_ci}
801b1b8bc3fSopenharmony_ci
802b1b8bc3fSopenharmony_civoid NetConnService::DecreaseNetActivates(const uint32_t callingUid, const sptr<INetConnCallback> &callback,
803b1b8bc3fSopenharmony_ci                                          uint32_t reqId)
804b1b8bc3fSopenharmony_ci{
805b1b8bc3fSopenharmony_ci    NET_ACTIVATE_MAP::iterator iterActive;
806b1b8bc3fSopenharmony_ci    for (iterActive = netActivates_.begin(); iterActive != netActivates_.end();) {
807b1b8bc3fSopenharmony_ci        if (!iterActive->second) {
808b1b8bc3fSopenharmony_ci            ++iterActive;
809b1b8bc3fSopenharmony_ci            continue;
810b1b8bc3fSopenharmony_ci        }
811b1b8bc3fSopenharmony_ci        sptr<INetConnCallback> saveCallback = iterActive->second->GetNetCallback();
812b1b8bc3fSopenharmony_ci        if (saveCallback == nullptr) {
813b1b8bc3fSopenharmony_ci            ++iterActive;
814b1b8bc3fSopenharmony_ci            continue;
815b1b8bc3fSopenharmony_ci        }
816b1b8bc3fSopenharmony_ci        if (callback->AsObject().GetRefPtr() != saveCallback->AsObject().GetRefPtr()) {
817b1b8bc3fSopenharmony_ci            ++iterActive;
818b1b8bc3fSopenharmony_ci            continue;
819b1b8bc3fSopenharmony_ci        }
820b1b8bc3fSopenharmony_ci        reqId = iterActive->first;
821b1b8bc3fSopenharmony_ci        auto netActivate = iterActive->second;
822b1b8bc3fSopenharmony_ci        NetRequest netRequest(netActivate->GetUid(), reqId);
823b1b8bc3fSopenharmony_ci        if (netActivate) {
824b1b8bc3fSopenharmony_ci            sptr<NetSupplier> supplier = netActivate->GetServiceSupply();
825b1b8bc3fSopenharmony_ci            if (supplier) {
826b1b8bc3fSopenharmony_ci                supplier->CancelRequest(netRequest);
827b1b8bc3fSopenharmony_ci            }
828b1b8bc3fSopenharmony_ci        }
829b1b8bc3fSopenharmony_ci        NET_SUPPLIER_MAP::iterator iterSupplier;
830b1b8bc3fSopenharmony_ci        for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) {
831b1b8bc3fSopenharmony_ci            if (iterSupplier->second != nullptr) {
832b1b8bc3fSopenharmony_ci                iterSupplier->second->CancelRequest(netRequest);
833b1b8bc3fSopenharmony_ci            }
834b1b8bc3fSopenharmony_ci        }
835b1b8bc3fSopenharmony_ci        iterActive = netActivates_.erase(iterActive);
836b1b8bc3fSopenharmony_ci        RemoveClientDeathRecipient(callback);
837b1b8bc3fSopenharmony_ci    }
838b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("end, callUid:%{public}u, reqId:%{public}u", callingUid, reqId);
839b1b8bc3fSopenharmony_ci}
840b1b8bc3fSopenharmony_ci
841b1b8bc3fSopenharmony_ciint32_t NetConnService::RegUnRegNetDetectionCallbackAsync(int32_t netId, const sptr<INetDetectionCallback> &callback,
842b1b8bc3fSopenharmony_ci                                                          bool isReg)
843b1b8bc3fSopenharmony_ci{
844b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter Async");
845b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
846b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("The parameter of callback is null");
847b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
848b1b8bc3fSopenharmony_ci    }
849b1b8bc3fSopenharmony_ci
850b1b8bc3fSopenharmony_ci    auto iterNetwork = networks_.find(netId);
851b1b8bc3fSopenharmony_ci    if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) {
852b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Could not find the corresponding network.");
853b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NETID_NOT_FOUND;
854b1b8bc3fSopenharmony_ci    }
855b1b8bc3fSopenharmony_ci    if (isReg) {
856b1b8bc3fSopenharmony_ci        iterNetwork->second->RegisterNetDetectionCallback(callback);
857b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
858b1b8bc3fSopenharmony_ci    }
859b1b8bc3fSopenharmony_ci    return iterNetwork->second->UnRegisterNetDetectionCallback(callback);
860b1b8bc3fSopenharmony_ci}
861b1b8bc3fSopenharmony_ci
862b1b8bc3fSopenharmony_ciint32_t NetConnService::UpdateNetStateForTestAsync(const sptr<NetSpecifier> &netSpecifier, int32_t netState)
863b1b8bc3fSopenharmony_ci{
864b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Test NetConnService::UpdateNetStateForTest(), begin");
865b1b8bc3fSopenharmony_ci    if (netSpecifier == nullptr) {
866b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("The parameter of netSpecifier or callback is null");
867b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
868b1b8bc3fSopenharmony_ci    }
869b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
870b1b8bc3fSopenharmony_ci}
871b1b8bc3fSopenharmony_ci
872b1b8bc3fSopenharmony_ciint32_t NetConnService::UpdateNetSupplierInfoAsync(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo,
873b1b8bc3fSopenharmony_ci                                                   int32_t callingUid)
874b1b8bc3fSopenharmony_ci{
875b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("UpdateNetSupplierInfo service in. supplierId[%{public}d]", supplierId);
876b1b8bc3fSopenharmony_ci    struct EventInfo eventInfo = {.updateSupplierId = supplierId};
877b1b8bc3fSopenharmony_ci    if (netSupplierInfo == nullptr) {
878b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netSupplierInfo is nullptr");
879b1b8bc3fSopenharmony_ci        eventInfo.errorType = static_cast<int32_t>(FAULT_UPDATE_SUPPLIERINFO_INV_PARAM);
880b1b8bc3fSopenharmony_ci        eventInfo.errorMsg = ERROR_MSG_NULL_SUPPLIER_INFO;
881b1b8bc3fSopenharmony_ci        EventReport::SendSupplierFaultEvent(eventInfo);
882b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_PARAMETER_ERROR;
883b1b8bc3fSopenharmony_ci    }
884b1b8bc3fSopenharmony_ci    eventInfo.supplierInfo = netSupplierInfo->ToString("\"");
885b1b8bc3fSopenharmony_ci
886b1b8bc3fSopenharmony_ci    auto supplier = FindNetSupplier(supplierId);
887b1b8bc3fSopenharmony_ci    if (supplier == nullptr) {
888b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Can not find supplier for supplierId[%{public}d]", supplierId);
889b1b8bc3fSopenharmony_ci        eventInfo.errorType = static_cast<int32_t>(FAULT_UPDATE_SUPPLIERINFO_INV_PARAM);
890b1b8bc3fSopenharmony_ci        eventInfo.errorMsg = std::string(ERROR_MSG_CAN_NOT_FIND_SUPPLIER).append(std::to_string(supplierId));
891b1b8bc3fSopenharmony_ci        EventReport::SendSupplierFaultEvent(eventInfo);
892b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NO_SUPPLIER;
893b1b8bc3fSopenharmony_ci    }
894b1b8bc3fSopenharmony_ci
895b1b8bc3fSopenharmony_ci    if (CheckAndCompareUid(supplier, callingUid) != NETMANAGER_SUCCESS) {
896b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("UpdateNetSupplierInfoAsync uid[%{public}d] is not equal to callingUid[%{public}d].",
897b1b8bc3fSopenharmony_ci                     supplier->GetUid(), callingUid);
898b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
899b1b8bc3fSopenharmony_ci    }
900b1b8bc3fSopenharmony_ci    eventInfo.bearerType = supplier->GetNetSupplierType();
901b1b8bc3fSopenharmony_ci    eventInfo.netId = supplier->GetNetId();
902b1b8bc3fSopenharmony_ci    EventReport::SendSupplierBehaviorEvent(eventInfo);
903b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Update supplier[%{public}d, %{public}d, %{public}s], supplierInfo:[ %{public}s ]", supplierId,
904b1b8bc3fSopenharmony_ci                 supplier->GetUid(), supplier->GetNetSupplierIdent().c_str(), netSupplierInfo->ToString(" ").c_str());
905b1b8bc3fSopenharmony_ci
906b1b8bc3fSopenharmony_ci    supplier->UpdateNetSupplierInfo(*netSupplierInfo);
907b1b8bc3fSopenharmony_ci    if (!netSupplierInfo->isAvailable_) {
908b1b8bc3fSopenharmony_ci        CallbackForSupplier(supplier, CALL_TYPE_LOST);
909b1b8bc3fSopenharmony_ci        supplier->ResetNetSupplier();
910b1b8bc3fSopenharmony_ci    } else {
911b1b8bc3fSopenharmony_ci        CallbackForSupplier(supplier, CALL_TYPE_UPDATE_CAP);
912b1b8bc3fSopenharmony_ci    }
913b1b8bc3fSopenharmony_ci    // Init score again here in case of net supplier type changed.
914b1b8bc3fSopenharmony_ci    if (netSupplierInfo->score_ == 0) {
915b1b8bc3fSopenharmony_ci        supplier->InitNetScore();
916b1b8bc3fSopenharmony_ci    }
917b1b8bc3fSopenharmony_ci    FindBestNetworkForAllRequest();
918b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("UpdateNetSupplierInfo service out.");
919b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
920b1b8bc3fSopenharmony_ci}
921b1b8bc3fSopenharmony_ci
922b1b8bc3fSopenharmony_ciint32_t NetConnService::UpdateNetLinkInfoAsync(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo,
923b1b8bc3fSopenharmony_ci                                               int32_t callingUid)
924b1b8bc3fSopenharmony_ci{
925b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("UpdateNetLinkInfo service in. supplierId[%{public}d]", supplierId);
926b1b8bc3fSopenharmony_ci    struct EventInfo eventInfo = {.updateNetlinkId = supplierId};
927b1b8bc3fSopenharmony_ci
928b1b8bc3fSopenharmony_ci    if (netLinkInfo == nullptr) {
929b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netLinkInfo is nullptr");
930b1b8bc3fSopenharmony_ci        eventInfo.errorType = static_cast<int32_t>(FAULT_UPDATE_NETLINK_INFO_INV_PARAM);
931b1b8bc3fSopenharmony_ci        eventInfo.errorMsg = ERROR_MSG_NULL_NET_LINK_INFO;
932b1b8bc3fSopenharmony_ci        EventReport::SendSupplierFaultEvent(eventInfo);
933b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_PARAMETER_ERROR;
934b1b8bc3fSopenharmony_ci    }
935b1b8bc3fSopenharmony_ci    eventInfo.netlinkInfo = netLinkInfo->ToString(" ");
936b1b8bc3fSopenharmony_ci
937b1b8bc3fSopenharmony_ci    auto supplier = FindNetSupplier(supplierId);
938b1b8bc3fSopenharmony_ci    if (supplier == nullptr) {
939b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("supplier is nullptr");
940b1b8bc3fSopenharmony_ci        eventInfo.errorType = static_cast<int32_t>(FAULT_UPDATE_NETLINK_INFO_INV_PARAM);
941b1b8bc3fSopenharmony_ci        eventInfo.errorMsg = std::string(ERROR_MSG_CAN_NOT_FIND_SUPPLIER).append(std::to_string(supplierId));
942b1b8bc3fSopenharmony_ci        EventReport::SendSupplierFaultEvent(eventInfo);
943b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NO_SUPPLIER;
944b1b8bc3fSopenharmony_ci    }
945b1b8bc3fSopenharmony_ci
946b1b8bc3fSopenharmony_ci    if (CheckAndCompareUid(supplier, callingUid) != NETMANAGER_SUCCESS) {
947b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("UpdateNetLinkInfoAsync uid[%{public}d] is not equal to callingUid[%{public}d].",
948b1b8bc3fSopenharmony_ci                     supplier->GetUid(), callingUid);
949b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
950b1b8bc3fSopenharmony_ci    }
951b1b8bc3fSopenharmony_ci    eventInfo.bearerType = supplier->GetNetSupplierType();
952b1b8bc3fSopenharmony_ci    eventInfo.netId = supplier->GetNetId();
953b1b8bc3fSopenharmony_ci    EventReport::SendSupplierBehaviorEvent(eventInfo);
954b1b8bc3fSopenharmony_ci    HttpProxy oldHttpProxy;
955b1b8bc3fSopenharmony_ci    supplier->GetHttpProxy(oldHttpProxy);
956b1b8bc3fSopenharmony_ci    // According to supplier id, get network from the list
957b1b8bc3fSopenharmony_ci    std::unique_lock<std::recursive_mutex> locker(netManagerMutex_);
958b1b8bc3fSopenharmony_ci    if (supplier->UpdateNetLinkInfo(*netLinkInfo) != NETMANAGER_SUCCESS) {
959b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("UpdateNetLinkInfo fail");
960b1b8bc3fSopenharmony_ci        eventInfo.errorType = static_cast<int32_t>(FAULT_UPDATE_NETLINK_INFO_FAILED);
961b1b8bc3fSopenharmony_ci        eventInfo.errorMsg = ERROR_MSG_UPDATE_NETLINK_INFO_FAILED;
962b1b8bc3fSopenharmony_ci        EventReport::SendSupplierFaultEvent(eventInfo);
963b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
964b1b8bc3fSopenharmony_ci    }
965b1b8bc3fSopenharmony_ci    locker.unlock();
966b1b8bc3fSopenharmony_ci    CallbackForSupplier(supplier, CALL_TYPE_UPDATE_LINK);
967b1b8bc3fSopenharmony_ci    FindBestNetworkForAllRequest();
968b1b8bc3fSopenharmony_ci
969b1b8bc3fSopenharmony_ci    if (oldHttpProxy != netLinkInfo->httpProxy_) {
970b1b8bc3fSopenharmony_ci        SendHttpProxyChangeBroadcast(netLinkInfo->httpProxy_);
971b1b8bc3fSopenharmony_ci    }
972b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("UpdateNetLinkInfo service out.");
973b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
974b1b8bc3fSopenharmony_ci}
975b1b8bc3fSopenharmony_ci
976b1b8bc3fSopenharmony_ciint32_t NetConnService::NetDetectionAsync(int32_t netId)
977b1b8bc3fSopenharmony_ci{
978b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter NetDetection, netId=[%{public}d]", netId);
979b1b8bc3fSopenharmony_ci    auto iterNetwork = networks_.find(netId);
980b1b8bc3fSopenharmony_ci    if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) {
981b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Could not find the corresponding network.");
982b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NETID_NOT_FOUND;
983b1b8bc3fSopenharmony_ci    }
984b1b8bc3fSopenharmony_ci    iterNetwork->second->StartNetDetection(true);
985b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("End NetDetection");
986b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
987b1b8bc3fSopenharmony_ci}
988b1b8bc3fSopenharmony_ci
989b1b8bc3fSopenharmony_ciint32_t NetConnService::NetDetectionForDnsHealthSync(int32_t netId, bool dnsHealthSuccess)
990b1b8bc3fSopenharmony_ci{
991b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter NetDetectionForDnsHealthSync");
992b1b8bc3fSopenharmony_ci    auto iterNetwork = networks_.find(netId);
993b1b8bc3fSopenharmony_ci    if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) {
994b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Could not find the corresponding network");
995b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NETID_NOT_FOUND;
996b1b8bc3fSopenharmony_ci    }
997b1b8bc3fSopenharmony_ci    iterNetwork->second->NetDetectionForDnsHealth(dnsHealthSuccess);
998b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
999b1b8bc3fSopenharmony_ci}
1000b1b8bc3fSopenharmony_ci
1001b1b8bc3fSopenharmony_ciint32_t NetConnService::RestrictBackgroundChangedAsync(bool restrictBackground)
1002b1b8bc3fSopenharmony_ci{
1003b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Restrict background changed, background = %{public}d", restrictBackground);
1004b1b8bc3fSopenharmony_ci    for (auto it = netSuppliers_.begin(); it != netSuppliers_.end(); ++it) {
1005b1b8bc3fSopenharmony_ci        if (it->second == nullptr) {
1006b1b8bc3fSopenharmony_ci            continue;
1007b1b8bc3fSopenharmony_ci        }
1008b1b8bc3fSopenharmony_ci
1009b1b8bc3fSopenharmony_ci        if (it->second->GetRestrictBackground() == restrictBackground) {
1010b1b8bc3fSopenharmony_ci            NETMGR_LOG_D("it->second->GetRestrictBackground() == restrictBackground");
1011b1b8bc3fSopenharmony_ci            return NET_CONN_ERR_NET_NO_RESTRICT_BACKGROUND;
1012b1b8bc3fSopenharmony_ci        }
1013b1b8bc3fSopenharmony_ci
1014b1b8bc3fSopenharmony_ci        if (it->second->GetNetSupplierType() == BEARER_VPN) {
1015b1b8bc3fSopenharmony_ci            CallbackForSupplier(it->second, CALL_TYPE_BLOCK_STATUS);
1016b1b8bc3fSopenharmony_ci        }
1017b1b8bc3fSopenharmony_ci        it->second->SetRestrictBackground(restrictBackground);
1018b1b8bc3fSopenharmony_ci    }
1019b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("End RestrictBackgroundChangedAsync");
1020b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1021b1b8bc3fSopenharmony_ci}
1022b1b8bc3fSopenharmony_ci
1023b1b8bc3fSopenharmony_civoid NetConnService::SendHttpProxyChangeBroadcast(const HttpProxy &httpProxy)
1024b1b8bc3fSopenharmony_ci{
1025b1b8bc3fSopenharmony_ci    BroadcastInfo info;
1026b1b8bc3fSopenharmony_ci    info.action = EventFwk::CommonEventSupport::COMMON_EVENT_HTTP_PROXY_CHANGE;
1027b1b8bc3fSopenharmony_ci    info.data = "Global HttpProxy Changed";
1028b1b8bc3fSopenharmony_ci    info.ordered = false;
1029b1b8bc3fSopenharmony_ci    std::map<std::string, std::string> param = {{"HttpProxy", httpProxy.ToString()}};
1030b1b8bc3fSopenharmony_ci    int32_t userId;
1031b1b8bc3fSopenharmony_ci    int32_t ret = GetCallingUserId(userId);
1032b1b8bc3fSopenharmony_ci    if (ret == NETMANAGER_SUCCESS) {
1033b1b8bc3fSopenharmony_ci        param.emplace("UserId", std::to_string(userId));
1034b1b8bc3fSopenharmony_ci    } else {
1035b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("SendHttpProxyChangeBroadcast get calling userId fail.");
1036b1b8bc3fSopenharmony_ci    }
1037b1b8bc3fSopenharmony_ci    BroadcastManager::GetInstance().SendBroadcast(info, param);
1038b1b8bc3fSopenharmony_ci}
1039b1b8bc3fSopenharmony_ci
1040b1b8bc3fSopenharmony_ciint32_t NetConnService::ActivateNetwork(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> &callback,
1041b1b8bc3fSopenharmony_ci                                        const uint32_t &timeoutMS, const int32_t registerType,
1042b1b8bc3fSopenharmony_ci                                        const uint32_t callingUid)
1043b1b8bc3fSopenharmony_ci{
1044b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("ActivateNetwork Enter");
1045b1b8bc3fSopenharmony_ci    if (netSpecifier == nullptr || callback == nullptr) {
1046b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("The parameter of netSpecifier or callback is null");
1047b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_PARAMETER_ERROR;
1048b1b8bc3fSopenharmony_ci    }
1049b1b8bc3fSopenharmony_ci    std::weak_ptr<INetActivateCallback> timeoutCb = shared_from_this();
1050b1b8bc3fSopenharmony_ci    std::shared_ptr<NetActivate> request = std::make_shared<NetActivate>(
1051b1b8bc3fSopenharmony_ci        netSpecifier, callback, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType);
1052b1b8bc3fSopenharmony_ci    request->StartTimeOutNetAvailable();
1053b1b8bc3fSopenharmony_ci    uint32_t reqId = request->GetRequestId();
1054b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("New request [id:%{public}u]", reqId);
1055b1b8bc3fSopenharmony_ci    NetRequest netrequest(request->GetUid(), reqId);
1056b1b8bc3fSopenharmony_ci    netActivates_[reqId] = request;
1057b1b8bc3fSopenharmony_ci    netUidActivates_[callingUid].push_back(request);
1058b1b8bc3fSopenharmony_ci    sptr<NetSupplier> bestNet = nullptr;
1059b1b8bc3fSopenharmony_ci    int bestScore = static_cast<int>(FindBestNetworkForRequest(bestNet, request));
1060b1b8bc3fSopenharmony_ci    if (bestScore != 0 && bestNet != nullptr) {
1061b1b8bc3fSopenharmony_ci        NETMGR_LOG_I(
1062b1b8bc3fSopenharmony_ci            "Match to optimal supplier:[%{public}d %{public}s] netId[%{public}d] score[%{public}d] "
1063b1b8bc3fSopenharmony_ci            "reqId[%{public}u]",
1064b1b8bc3fSopenharmony_ci            bestNet->GetSupplierId(), bestNet->GetNetSupplierIdent().c_str(), bestNet->GetNetId(), bestScore, reqId);
1065b1b8bc3fSopenharmony_ci        bestNet->SelectAsBestNetwork(netrequest);
1066b1b8bc3fSopenharmony_ci        request->SetServiceSupply(bestNet);
1067b1b8bc3fSopenharmony_ci        CallbackForAvailable(bestNet, callback);
1068b1b8bc3fSopenharmony_ci        if ((bestNet->GetNetSupplierType() == BEARER_CELLULAR) || (bestNet->GetNetSupplierType() == BEARER_WIFI)) {
1069b1b8bc3fSopenharmony_ci            struct EventInfo eventInfo = {.capabilities = bestNet->GetNetCapabilities().ToString(" "),
1070b1b8bc3fSopenharmony_ci                                          .supplierIdent = bestNet->GetNetSupplierIdent()};
1071b1b8bc3fSopenharmony_ci            EventReport::SendRequestBehaviorEvent(eventInfo);
1072b1b8bc3fSopenharmony_ci        }
1073b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
1074b1b8bc3fSopenharmony_ci    }
1075b1b8bc3fSopenharmony_ci    if (timeoutMS == 0) {
1076b1b8bc3fSopenharmony_ci        callback->NetUnavailable();
1077b1b8bc3fSopenharmony_ci    }
1078b1b8bc3fSopenharmony_ci
1079b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Not matched to the optimal network, send request to all networks.");
1080b1b8bc3fSopenharmony_ci    SendRequestToAllNetwork(request);
1081b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1082b1b8bc3fSopenharmony_ci}
1083b1b8bc3fSopenharmony_ci
1084b1b8bc3fSopenharmony_civoid NetConnService::OnNetActivateTimeOut(uint32_t reqId)
1085b1b8bc3fSopenharmony_ci{
1086b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
1087b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([reqId, this]() {
1088b1b8bc3fSopenharmony_ci            NETMGR_LOG_I("DeactivateNetwork Enter, reqId is [%{public}d]", reqId);
1089b1b8bc3fSopenharmony_ci            auto iterActivate = netActivates_.find(reqId);
1090b1b8bc3fSopenharmony_ci            if (iterActivate == netActivates_.end()) {
1091b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("not found the reqId: [%{public}d]", reqId);
1092b1b8bc3fSopenharmony_ci                return;
1093b1b8bc3fSopenharmony_ci            }
1094b1b8bc3fSopenharmony_ci            NetRequest netrequest;
1095b1b8bc3fSopenharmony_ci            netrequest.requestId = reqId;
1096b1b8bc3fSopenharmony_ci            if (iterActivate->second != nullptr) {
1097b1b8bc3fSopenharmony_ci                sptr<NetSupplier> pNetService = iterActivate->second->GetServiceSupply();
1098b1b8bc3fSopenharmony_ci                netrequest.uid = iterActivate->second->GetUid();
1099b1b8bc3fSopenharmony_ci                if (pNetService) {
1100b1b8bc3fSopenharmony_ci                    pNetService->CancelRequest(netrequest);
1101b1b8bc3fSopenharmony_ci                }
1102b1b8bc3fSopenharmony_ci            }
1103b1b8bc3fSopenharmony_ci
1104b1b8bc3fSopenharmony_ci            NET_SUPPLIER_MAP::iterator iterSupplier;
1105b1b8bc3fSopenharmony_ci            for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) {
1106b1b8bc3fSopenharmony_ci                if (iterSupplier->second == nullptr) {
1107b1b8bc3fSopenharmony_ci                    continue;
1108b1b8bc3fSopenharmony_ci                }
1109b1b8bc3fSopenharmony_ci                iterSupplier->second->CancelRequest(netrequest);
1110b1b8bc3fSopenharmony_ci            }
1111b1b8bc3fSopenharmony_ci        });
1112b1b8bc3fSopenharmony_ci    }
1113b1b8bc3fSopenharmony_ci}
1114b1b8bc3fSopenharmony_ci
1115b1b8bc3fSopenharmony_cisptr<NetSupplier> NetConnService::FindNetSupplier(uint32_t supplierId)
1116b1b8bc3fSopenharmony_ci{
1117b1b8bc3fSopenharmony_ci    auto iterSupplier = netSuppliers_.find(supplierId);
1118b1b8bc3fSopenharmony_ci    if (iterSupplier != netSuppliers_.end()) {
1119b1b8bc3fSopenharmony_ci        return iterSupplier->second;
1120b1b8bc3fSopenharmony_ci    }
1121b1b8bc3fSopenharmony_ci    return nullptr;
1122b1b8bc3fSopenharmony_ci}
1123b1b8bc3fSopenharmony_ci
1124b1b8bc3fSopenharmony_cibool NetConnService::FindSameCallback(const sptr<INetConnCallback> &callback,
1125b1b8bc3fSopenharmony_ci                                      uint32_t &reqId, RegisterType &registerType)
1126b1b8bc3fSopenharmony_ci{
1127b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
1128b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("callback is null");
1129b1b8bc3fSopenharmony_ci        return false;
1130b1b8bc3fSopenharmony_ci    }
1131b1b8bc3fSopenharmony_ci    NET_ACTIVATE_MAP::iterator iterActive;
1132b1b8bc3fSopenharmony_ci    for (iterActive = netActivates_.begin(); iterActive != netActivates_.end(); ++iterActive) {
1133b1b8bc3fSopenharmony_ci        if (!iterActive->second) {
1134b1b8bc3fSopenharmony_ci            continue;
1135b1b8bc3fSopenharmony_ci        }
1136b1b8bc3fSopenharmony_ci        sptr<INetConnCallback> saveCallback = iterActive->second->GetNetCallback();
1137b1b8bc3fSopenharmony_ci        if (saveCallback == nullptr) {
1138b1b8bc3fSopenharmony_ci            continue;
1139b1b8bc3fSopenharmony_ci        }
1140b1b8bc3fSopenharmony_ci        if (callback->AsObject().GetRefPtr() == saveCallback->AsObject().GetRefPtr()) {
1141b1b8bc3fSopenharmony_ci            reqId = iterActive->first;
1142b1b8bc3fSopenharmony_ci            if (iterActive->second) {
1143b1b8bc3fSopenharmony_ci                auto specifier = iterActive->second->GetNetSpecifier();
1144b1b8bc3fSopenharmony_ci                registerType = (specifier != nullptr &&
1145b1b8bc3fSopenharmony_ci                    specifier->netCapabilities_.netCaps_.count(
1146b1b8bc3fSopenharmony_ci                        NetManagerStandard::NET_CAPABILITY_INTERNAL_DEFAULT) > 0) ?
1147b1b8bc3fSopenharmony_ci                        REQUEST : REGISTER;
1148b1b8bc3fSopenharmony_ci            }
1149b1b8bc3fSopenharmony_ci            return true;
1150b1b8bc3fSopenharmony_ci        }
1151b1b8bc3fSopenharmony_ci    }
1152b1b8bc3fSopenharmony_ci    return false;
1153b1b8bc3fSopenharmony_ci}
1154b1b8bc3fSopenharmony_ci
1155b1b8bc3fSopenharmony_cibool NetConnService::FindSameCallback(const sptr<INetConnCallback> &callback, uint32_t &reqId)
1156b1b8bc3fSopenharmony_ci{
1157b1b8bc3fSopenharmony_ci    RegisterType registerType = INVALIDTYPE;
1158b1b8bc3fSopenharmony_ci    return FindSameCallback(callback, reqId, registerType);
1159b1b8bc3fSopenharmony_ci}
1160b1b8bc3fSopenharmony_ci
1161b1b8bc3fSopenharmony_civoid NetConnService::FindBestNetworkForAllRequest()
1162b1b8bc3fSopenharmony_ci{
1163b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("FindBestNetworkForAllRequest Enter. netActivates_ size: [%{public}zu]", netActivates_.size());
1164b1b8bc3fSopenharmony_ci    NET_ACTIVATE_MAP::iterator iterActive;
1165b1b8bc3fSopenharmony_ci    sptr<NetSupplier> bestSupplier = nullptr;
1166b1b8bc3fSopenharmony_ci    for (iterActive = netActivates_.begin(); iterActive != netActivates_.end(); ++iterActive) {
1167b1b8bc3fSopenharmony_ci        if (!iterActive->second) {
1168b1b8bc3fSopenharmony_ci            continue;
1169b1b8bc3fSopenharmony_ci        }
1170b1b8bc3fSopenharmony_ci        int score = static_cast<int>(FindBestNetworkForRequest(bestSupplier, iterActive->second));
1171b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("Find best supplier[%{public}d, %{public}s]for request[%{public}d]",
1172b1b8bc3fSopenharmony_ci                     bestSupplier ? bestSupplier->GetSupplierId() : 0,
1173b1b8bc3fSopenharmony_ci                     bestSupplier ? bestSupplier->GetNetSupplierIdent().c_str() : "null",
1174b1b8bc3fSopenharmony_ci                     iterActive->second->GetRequestId());
1175b1b8bc3fSopenharmony_ci        if (iterActive->second == defaultNetActivate_) {
1176b1b8bc3fSopenharmony_ci            MakeDefaultNetWork(defaultNetSupplier_, bestSupplier);
1177b1b8bc3fSopenharmony_ci        }
1178b1b8bc3fSopenharmony_ci        sptr<NetSupplier> oldSupplier = iterActive->second->GetServiceSupply();
1179b1b8bc3fSopenharmony_ci        sptr<INetConnCallback> callback = iterActive->second->GetNetCallback();
1180b1b8bc3fSopenharmony_ci        if (!bestSupplier) {
1181b1b8bc3fSopenharmony_ci            // not found the bestNetwork
1182b1b8bc3fSopenharmony_ci            NotFindBestSupplier(iterActive->first, iterActive->second, oldSupplier, callback);
1183b1b8bc3fSopenharmony_ci            continue;
1184b1b8bc3fSopenharmony_ci        }
1185b1b8bc3fSopenharmony_ci        SendBestScoreAllNetwork(iterActive->first, score, bestSupplier->GetSupplierId(), iterActive->second->GetUid());
1186b1b8bc3fSopenharmony_ci        if (bestSupplier == oldSupplier) {
1187b1b8bc3fSopenharmony_ci            NETMGR_LOG_D("bestSupplier is equal with oldSupplier.");
1188b1b8bc3fSopenharmony_ci            continue;
1189b1b8bc3fSopenharmony_ci        }
1190b1b8bc3fSopenharmony_ci        if (oldSupplier) {
1191b1b8bc3fSopenharmony_ci            oldSupplier->RemoveBestRequest(iterActive->first);
1192b1b8bc3fSopenharmony_ci        }
1193b1b8bc3fSopenharmony_ci        iterActive->second->SetServiceSupply(bestSupplier);
1194b1b8bc3fSopenharmony_ci        CallbackForAvailable(bestSupplier, callback);
1195b1b8bc3fSopenharmony_ci        NetRequest netRequest(iterActive->second->GetUid(), iterActive->first);
1196b1b8bc3fSopenharmony_ci        bestSupplier->SelectAsBestNetwork(netRequest);
1197b1b8bc3fSopenharmony_ci    }
1198b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("FindBestNetworkForAllRequest end");
1199b1b8bc3fSopenharmony_ci}
1200b1b8bc3fSopenharmony_ci
1201b1b8bc3fSopenharmony_ciuint32_t NetConnService::FindBestNetworkForRequest(sptr<NetSupplier> &supplier,
1202b1b8bc3fSopenharmony_ci                                                   std::shared_ptr<NetActivate> &netActivateNetwork)
1203b1b8bc3fSopenharmony_ci{
1204b1b8bc3fSopenharmony_ci    int bestScore = 0;
1205b1b8bc3fSopenharmony_ci    supplier = nullptr;
1206b1b8bc3fSopenharmony_ci    if (netActivateNetwork == nullptr) {
1207b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netActivateNetwork is null");
1208b1b8bc3fSopenharmony_ci        return bestScore;
1209b1b8bc3fSopenharmony_ci    }
1210b1b8bc3fSopenharmony_ci
1211b1b8bc3fSopenharmony_ci    NET_SUPPLIER_MAP::iterator iter;
1212b1b8bc3fSopenharmony_ci    for (iter = netSuppliers_.begin(); iter != netSuppliers_.end(); ++iter) {
1213b1b8bc3fSopenharmony_ci        if (iter->second == nullptr) {
1214b1b8bc3fSopenharmony_ci            continue;
1215b1b8bc3fSopenharmony_ci        }
1216b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("supplier info, supplier[%{public}d, %{public}s], realScore[%{public}d], isConnected[%{public}d]",
1217b1b8bc3fSopenharmony_ci                     iter->second->GetSupplierId(), iter->second->GetNetSupplierIdent().c_str(),
1218b1b8bc3fSopenharmony_ci                     iter->second->GetRealScore(), iter->second->IsConnected());
1219b1b8bc3fSopenharmony_ci        if ((!iter->second->IsConnected()) || (!netActivateNetwork->MatchRequestAndNetwork(iter->second))) {
1220b1b8bc3fSopenharmony_ci            NETMGR_LOG_D("Supplier[%{public}d] is not connected or not match request.", iter->second->GetSupplierId());
1221b1b8bc3fSopenharmony_ci            continue;
1222b1b8bc3fSopenharmony_ci        }
1223b1b8bc3fSopenharmony_ci        int score = iter->second->GetRealScore();
1224b1b8bc3fSopenharmony_ci        if (score > bestScore) {
1225b1b8bc3fSopenharmony_ci            bestScore = score;
1226b1b8bc3fSopenharmony_ci            supplier = iter->second;
1227b1b8bc3fSopenharmony_ci        }
1228b1b8bc3fSopenharmony_ci    }
1229b1b8bc3fSopenharmony_ci    NETMGR_LOG_D(
1230b1b8bc3fSopenharmony_ci        "bestScore[%{public}d], bestSupplier[%{public}d, %{public}s], "
1231b1b8bc3fSopenharmony_ci        "request[%{public}d] is [%{public}s],",
1232b1b8bc3fSopenharmony_ci        bestScore, supplier ? supplier->GetSupplierId() : 0,
1233b1b8bc3fSopenharmony_ci        supplier ? supplier->GetNetSupplierIdent().c_str() : "null", netActivateNetwork->GetRequestId(),
1234b1b8bc3fSopenharmony_ci        netActivateNetwork->GetNetSpecifier() ? netActivateNetwork->GetNetSpecifier()->ToString(" ").c_str() : "null");
1235b1b8bc3fSopenharmony_ci    return bestScore;
1236b1b8bc3fSopenharmony_ci}
1237b1b8bc3fSopenharmony_ci
1238b1b8bc3fSopenharmony_civoid NetConnService::RequestAllNetworkExceptDefault()
1239b1b8bc3fSopenharmony_ci{
1240b1b8bc3fSopenharmony_ci    if ((defaultNetSupplier_ == nullptr) || (defaultNetSupplier_->IsNetValidated())) {
1241b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("defaultNetSupplier_ is  null or IsNetValidated");
1242b1b8bc3fSopenharmony_ci        return;
1243b1b8bc3fSopenharmony_ci    }
1244b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Default supplier[%{public}d, %{public}s] is not valid,request to activate another network",
1245b1b8bc3fSopenharmony_ci                 defaultNetSupplier_->GetSupplierId(), defaultNetSupplier_->GetNetSupplierIdent().c_str());
1246b1b8bc3fSopenharmony_ci    if (defaultNetActivate_ == nullptr) {
1247b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Default net request is null");
1248b1b8bc3fSopenharmony_ci        return;
1249b1b8bc3fSopenharmony_ci    }
1250b1b8bc3fSopenharmony_ci    // Request activation of all networks except the default network
1251b1b8bc3fSopenharmony_ci    NetRequest netrequest(
1252b1b8bc3fSopenharmony_ci        defaultNetActivate_->GetUid(), defaultNetActivate_->GetRequestId(), defaultNetActivate_->GetRegisterType());
1253b1b8bc3fSopenharmony_ci    for (const auto &netSupplier : netSuppliers_) {
1254b1b8bc3fSopenharmony_ci        if (netSupplier.second == nullptr || netSupplier.second == defaultNetSupplier_) {
1255b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("netSupplier is null or is defaultNetSupplier_");
1256b1b8bc3fSopenharmony_ci            continue;
1257b1b8bc3fSopenharmony_ci        }
1258b1b8bc3fSopenharmony_ci        if (netSupplier.second->GetNetScore() >= defaultNetSupplier_->GetNetScore()) {
1259b1b8bc3fSopenharmony_ci            continue;
1260b1b8bc3fSopenharmony_ci        }
1261b1b8bc3fSopenharmony_ci        if (netSupplier.second->HasNetCap(NetCap::NET_CAPABILITY_INTERNAL_DEFAULT)) {
1262b1b8bc3fSopenharmony_ci            NETMGR_LOG_I("Supplier[%{public}d] is internal, skip.", netSupplier.second->GetSupplierId());
1263b1b8bc3fSopenharmony_ci            continue;
1264b1b8bc3fSopenharmony_ci        }
1265b1b8bc3fSopenharmony_ci        if (!defaultNetActivate_->MatchRequestAndNetwork(netSupplier.second, true)) {
1266b1b8bc3fSopenharmony_ci            continue;
1267b1b8bc3fSopenharmony_ci        }
1268b1b8bc3fSopenharmony_ci        if (!netSupplier.second->RequestToConnect(netrequest)) {
1269b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("Request network for supplier[%{public}d, %{public}s] failed",
1270b1b8bc3fSopenharmony_ci                         netSupplier.second->GetSupplierId(), netSupplier.second->GetNetSupplierIdent().c_str());
1271b1b8bc3fSopenharmony_ci        }
1272b1b8bc3fSopenharmony_ci    }
1273b1b8bc3fSopenharmony_ci}
1274b1b8bc3fSopenharmony_ci
1275b1b8bc3fSopenharmony_ciint32_t NetConnService::GenerateNetId()
1276b1b8bc3fSopenharmony_ci{
1277b1b8bc3fSopenharmony_ci    for (int32_t i = MIN_NET_ID; i <= MAX_NET_ID; ++i) {
1278b1b8bc3fSopenharmony_ci        netIdLastValue_++;
1279b1b8bc3fSopenharmony_ci        if (netIdLastValue_ > MAX_NET_ID) {
1280b1b8bc3fSopenharmony_ci            netIdLastValue_ = MIN_NET_ID;
1281b1b8bc3fSopenharmony_ci        }
1282b1b8bc3fSopenharmony_ci        if (networks_.find(netIdLastValue_) == networks_.end()) {
1283b1b8bc3fSopenharmony_ci            return netIdLastValue_;
1284b1b8bc3fSopenharmony_ci        }
1285b1b8bc3fSopenharmony_ci    }
1286b1b8bc3fSopenharmony_ci    return INVALID_NET_ID;
1287b1b8bc3fSopenharmony_ci}
1288b1b8bc3fSopenharmony_ci
1289b1b8bc3fSopenharmony_ciint32_t NetConnService::GenerateInternalNetId()
1290b1b8bc3fSopenharmony_ci{
1291b1b8bc3fSopenharmony_ci    for (int32_t i = MIN_INTERNAL_NET_ID; i <= MAX_INTERNAL_NET_ID; ++i) {
1292b1b8bc3fSopenharmony_ci        int32_t value = internalNetIdLastValue_++;
1293b1b8bc3fSopenharmony_ci        if (value > MAX_INTERNAL_NET_ID) {
1294b1b8bc3fSopenharmony_ci            internalNetIdLastValue_ = MIN_INTERNAL_NET_ID;
1295b1b8bc3fSopenharmony_ci            value = MIN_INTERNAL_NET_ID;
1296b1b8bc3fSopenharmony_ci        }
1297b1b8bc3fSopenharmony_ci        if (networks_.find(value) == networks_.end()) {
1298b1b8bc3fSopenharmony_ci            return value;
1299b1b8bc3fSopenharmony_ci        }
1300b1b8bc3fSopenharmony_ci    }
1301b1b8bc3fSopenharmony_ci    return INVALID_NET_ID;
1302b1b8bc3fSopenharmony_ci}
1303b1b8bc3fSopenharmony_ci
1304b1b8bc3fSopenharmony_civoid NetConnService::NotFindBestSupplier(uint32_t reqId, const std::shared_ptr<NetActivate> &active,
1305b1b8bc3fSopenharmony_ci                                         const sptr<NetSupplier> &supplier, const sptr<INetConnCallback> &callback)
1306b1b8bc3fSopenharmony_ci{
1307b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Could not find best supplier for request:[%{public}d]", reqId);
1308b1b8bc3fSopenharmony_ci    if (supplier != nullptr) {
1309b1b8bc3fSopenharmony_ci        supplier->RemoveBestRequest(reqId);
1310b1b8bc3fSopenharmony_ci        if (callback != nullptr) {
1311b1b8bc3fSopenharmony_ci            sptr<NetHandle> netHandle = supplier->GetNetHandle();
1312b1b8bc3fSopenharmony_ci            callback->NetLost(netHandle);
1313b1b8bc3fSopenharmony_ci        }
1314b1b8bc3fSopenharmony_ci    }
1315b1b8bc3fSopenharmony_ci    if (active != nullptr) {
1316b1b8bc3fSopenharmony_ci        active->SetServiceSupply(nullptr);
1317b1b8bc3fSopenharmony_ci        SendRequestToAllNetwork(active);
1318b1b8bc3fSopenharmony_ci    }
1319b1b8bc3fSopenharmony_ci}
1320b1b8bc3fSopenharmony_ci
1321b1b8bc3fSopenharmony_civoid NetConnService::SendAllRequestToNetwork(sptr<NetSupplier> supplier)
1322b1b8bc3fSopenharmony_ci{
1323b1b8bc3fSopenharmony_ci    if (supplier == nullptr) {
1324b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("supplier is null");
1325b1b8bc3fSopenharmony_ci        return;
1326b1b8bc3fSopenharmony_ci    }
1327b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Send all request to supplier[%{public}d, %{public}s]", supplier->GetSupplierId(),
1328b1b8bc3fSopenharmony_ci                 supplier->GetNetSupplierIdent().c_str());
1329b1b8bc3fSopenharmony_ci    NET_ACTIVATE_MAP::iterator iter;
1330b1b8bc3fSopenharmony_ci    for (iter = netActivates_.begin(); iter != netActivates_.end(); ++iter) {
1331b1b8bc3fSopenharmony_ci        if (iter->second == nullptr) {
1332b1b8bc3fSopenharmony_ci            continue;
1333b1b8bc3fSopenharmony_ci        }
1334b1b8bc3fSopenharmony_ci        if (!iter->second->MatchRequestAndNetwork(supplier, true)) {
1335b1b8bc3fSopenharmony_ci            continue;
1336b1b8bc3fSopenharmony_ci        }
1337b1b8bc3fSopenharmony_ci        NetRequest netrequest(iter->second->GetUid(), iter->first, iter->second->GetRegisterType());
1338b1b8bc3fSopenharmony_ci        netrequest.bearTypes = iter->second->GetBearType();
1339b1b8bc3fSopenharmony_ci        bool result = supplier->RequestToConnect(netrequest);
1340b1b8bc3fSopenharmony_ci        if (!result) {
1341b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("Request network for supplier[%{public}d, %{public}s] failed", supplier->GetSupplierId(),
1342b1b8bc3fSopenharmony_ci                         supplier->GetNetSupplierIdent().c_str());
1343b1b8bc3fSopenharmony_ci        }
1344b1b8bc3fSopenharmony_ci    }
1345b1b8bc3fSopenharmony_ci}
1346b1b8bc3fSopenharmony_ci
1347b1b8bc3fSopenharmony_civoid NetConnService::SendRequestToAllNetwork(std::shared_ptr<NetActivate> request)
1348b1b8bc3fSopenharmony_ci{
1349b1b8bc3fSopenharmony_ci    if (request == nullptr) {
1350b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("request is null");
1351b1b8bc3fSopenharmony_ci        return;
1352b1b8bc3fSopenharmony_ci    }
1353b1b8bc3fSopenharmony_ci
1354b1b8bc3fSopenharmony_ci    NetRequest netrequest(request->GetUid(),
1355b1b8bc3fSopenharmony_ci            request->GetRequestId(),
1356b1b8bc3fSopenharmony_ci            request->GetRegisterType(),
1357b1b8bc3fSopenharmony_ci            request->GetNetSpecifier()->ident_,
1358b1b8bc3fSopenharmony_ci            request->GetBearType());
1359b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Send request[%{public}d] to all supplier", netrequest.requestId);
1360b1b8bc3fSopenharmony_ci    NET_SUPPLIER_MAP::iterator iter;
1361b1b8bc3fSopenharmony_ci    for (iter = netSuppliers_.begin(); iter != netSuppliers_.end(); ++iter) {
1362b1b8bc3fSopenharmony_ci        if (iter->second == nullptr) {
1363b1b8bc3fSopenharmony_ci            continue;
1364b1b8bc3fSopenharmony_ci        }
1365b1b8bc3fSopenharmony_ci        if (!request->MatchRequestAndNetwork(iter->second, true)) {
1366b1b8bc3fSopenharmony_ci            continue;
1367b1b8bc3fSopenharmony_ci        }
1368b1b8bc3fSopenharmony_ci
1369b1b8bc3fSopenharmony_ci        bool result = iter->second->RequestToConnect(netrequest);
1370b1b8bc3fSopenharmony_ci        if (!result) {
1371b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("Request network for supplier[%{public}d, %{public}s] failed", iter->second->GetSupplierId(),
1372b1b8bc3fSopenharmony_ci                         iter->second->GetNetSupplierIdent().c_str());
1373b1b8bc3fSopenharmony_ci        }
1374b1b8bc3fSopenharmony_ci    }
1375b1b8bc3fSopenharmony_ci}
1376b1b8bc3fSopenharmony_ci
1377b1b8bc3fSopenharmony_civoid NetConnService::SendBestScoreAllNetwork(uint32_t reqId, int32_t bestScore, uint32_t supplierId, uint32_t uid)
1378b1b8bc3fSopenharmony_ci{
1379b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Send best supplier[%{public}d]-score[%{public}d] to all supplier", supplierId, bestScore);
1380b1b8bc3fSopenharmony_ci    NET_SUPPLIER_MAP::iterator iter;
1381b1b8bc3fSopenharmony_ci    for (iter = netSuppliers_.begin(); iter != netSuppliers_.end(); ++iter) {
1382b1b8bc3fSopenharmony_ci        if (iter->second == nullptr) {
1383b1b8bc3fSopenharmony_ci            continue;
1384b1b8bc3fSopenharmony_ci        }
1385b1b8bc3fSopenharmony_ci        if (iter->second->HasNetCap(NetCap::NET_CAPABILITY_INTERNAL_DEFAULT)) {
1386b1b8bc3fSopenharmony_ci            continue;
1387b1b8bc3fSopenharmony_ci        }
1388b1b8bc3fSopenharmony_ci        NetRequest netrequest;
1389b1b8bc3fSopenharmony_ci        netrequest.uid = uid;
1390b1b8bc3fSopenharmony_ci        netrequest.requestId = reqId;
1391b1b8bc3fSopenharmony_ci        iter->second->ReceiveBestScore(bestScore, supplierId, netrequest);
1392b1b8bc3fSopenharmony_ci    }
1393b1b8bc3fSopenharmony_ci}
1394b1b8bc3fSopenharmony_ci
1395b1b8bc3fSopenharmony_civoid NetConnService::CallbackForSupplier(sptr<NetSupplier> &supplier, CallbackType type)
1396b1b8bc3fSopenharmony_ci{
1397b1b8bc3fSopenharmony_ci    if (supplier == nullptr) {
1398b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("supplier is nullptr");
1399b1b8bc3fSopenharmony_ci        return;
1400b1b8bc3fSopenharmony_ci    }
1401b1b8bc3fSopenharmony_ci    std::set<uint32_t> &bestReqList = supplier->GetBestRequestList();
1402b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Callback type: %{public}d for supplier[%{public}d, %{public}s], best request size: %{public}zd",
1403b1b8bc3fSopenharmony_ci                 static_cast<int32_t>(type), supplier->GetSupplierId(), supplier->GetNetSupplierIdent().c_str(),
1404b1b8bc3fSopenharmony_ci                 bestReqList.size());
1405b1b8bc3fSopenharmony_ci    for (auto it : bestReqList) {
1406b1b8bc3fSopenharmony_ci        auto reqIt = netActivates_.find(it);
1407b1b8bc3fSopenharmony_ci        if ((reqIt == netActivates_.end()) || (reqIt->second == nullptr)) {
1408b1b8bc3fSopenharmony_ci            continue;
1409b1b8bc3fSopenharmony_ci        }
1410b1b8bc3fSopenharmony_ci        sptr<INetConnCallback> callback = reqIt->second->GetNetCallback();
1411b1b8bc3fSopenharmony_ci        if (!callback) {
1412b1b8bc3fSopenharmony_ci            continue;
1413b1b8bc3fSopenharmony_ci        }
1414b1b8bc3fSopenharmony_ci        sptr<NetHandle> netHandle = supplier->GetNetHandle();
1415b1b8bc3fSopenharmony_ci        switch (type) {
1416b1b8bc3fSopenharmony_ci            case CALL_TYPE_LOST: {
1417b1b8bc3fSopenharmony_ci                callback->NetLost(netHandle);
1418b1b8bc3fSopenharmony_ci                break;
1419b1b8bc3fSopenharmony_ci            }
1420b1b8bc3fSopenharmony_ci            case CALL_TYPE_UPDATE_CAP: {
1421b1b8bc3fSopenharmony_ci                sptr<NetAllCapabilities> pNetAllCap = std::make_unique<NetAllCapabilities>().release();
1422b1b8bc3fSopenharmony_ci                *pNetAllCap = supplier->GetNetCapabilities();
1423b1b8bc3fSopenharmony_ci                callback->NetCapabilitiesChange(netHandle, pNetAllCap);
1424b1b8bc3fSopenharmony_ci                break;
1425b1b8bc3fSopenharmony_ci            }
1426b1b8bc3fSopenharmony_ci            case CALL_TYPE_UPDATE_LINK: {
1427b1b8bc3fSopenharmony_ci                sptr<NetLinkInfo> pInfo = std::make_unique<NetLinkInfo>().release();
1428b1b8bc3fSopenharmony_ci                auto network = supplier->GetNetwork();
1429b1b8bc3fSopenharmony_ci                if (network != nullptr && pInfo != nullptr) {
1430b1b8bc3fSopenharmony_ci                    *pInfo = network->GetNetLinkInfo();
1431b1b8bc3fSopenharmony_ci                }
1432b1b8bc3fSopenharmony_ci                callback->NetConnectionPropertiesChange(netHandle, pInfo);
1433b1b8bc3fSopenharmony_ci                break;
1434b1b8bc3fSopenharmony_ci            }
1435b1b8bc3fSopenharmony_ci            case CALL_TYPE_BLOCK_STATUS: {
1436b1b8bc3fSopenharmony_ci                bool Metered = supplier->HasNetCap(NET_CAPABILITY_NOT_METERED);
1437b1b8bc3fSopenharmony_ci                bool newBlocked = NetManagerCenter::GetInstance().IsUidNetAccess(supplier->GetSupplierUid(), Metered);
1438b1b8bc3fSopenharmony_ci                callback->NetBlockStatusChange(netHandle, newBlocked);
1439b1b8bc3fSopenharmony_ci                break;
1440b1b8bc3fSopenharmony_ci            }
1441b1b8bc3fSopenharmony_ci            default:
1442b1b8bc3fSopenharmony_ci                break;
1443b1b8bc3fSopenharmony_ci        }
1444b1b8bc3fSopenharmony_ci    }
1445b1b8bc3fSopenharmony_ci}
1446b1b8bc3fSopenharmony_ci
1447b1b8bc3fSopenharmony_civoid NetConnService::CallbackForAvailable(sptr<NetSupplier> &supplier, const sptr<INetConnCallback> &callback)
1448b1b8bc3fSopenharmony_ci{
1449b1b8bc3fSopenharmony_ci    if (supplier == nullptr || callback == nullptr) {
1450b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Input parameter is null.");
1451b1b8bc3fSopenharmony_ci        return;
1452b1b8bc3fSopenharmony_ci    }
1453b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("CallbackForAvailable supplier[%{public}d, %{public}s]", supplier->GetSupplierId(),
1454b1b8bc3fSopenharmony_ci                 supplier->GetNetSupplierIdent().c_str());
1455b1b8bc3fSopenharmony_ci    sptr<NetHandle> netHandle = supplier->GetNetHandle();
1456b1b8bc3fSopenharmony_ci    callback->NetAvailable(netHandle);
1457b1b8bc3fSopenharmony_ci    sptr<NetAllCapabilities> pNetAllCap = std::make_unique<NetAllCapabilities>().release();
1458b1b8bc3fSopenharmony_ci    *pNetAllCap = supplier->GetNetCapabilities();
1459b1b8bc3fSopenharmony_ci    callback->NetCapabilitiesChange(netHandle, pNetAllCap);
1460b1b8bc3fSopenharmony_ci    sptr<NetLinkInfo> pInfo = std::make_unique<NetLinkInfo>().release();
1461b1b8bc3fSopenharmony_ci    auto network = supplier->GetNetwork();
1462b1b8bc3fSopenharmony_ci    if (network != nullptr && pInfo != nullptr) {
1463b1b8bc3fSopenharmony_ci        *pInfo = network->GetNetLinkInfo();
1464b1b8bc3fSopenharmony_ci    }
1465b1b8bc3fSopenharmony_ci    callback->NetConnectionPropertiesChange(netHandle, pInfo);
1466b1b8bc3fSopenharmony_ci    NetsysController::GetInstance().NotifyNetBearerTypeChange(pNetAllCap->bearerTypes_);
1467b1b8bc3fSopenharmony_ci}
1468b1b8bc3fSopenharmony_ci
1469b1b8bc3fSopenharmony_civoid NetConnService::MakeDefaultNetWork(sptr<NetSupplier> &oldSupplier, sptr<NetSupplier> &newSupplier)
1470b1b8bc3fSopenharmony_ci{
1471b1b8bc3fSopenharmony_ci    NETMGR_LOG_I(
1472b1b8bc3fSopenharmony_ci        "oldSupplier[%{public}d, %{public}s], newSupplier[%{public}d, %{public}s], old equals "
1473b1b8bc3fSopenharmony_ci        "new is [%{public}d]", oldSupplier ? oldSupplier->GetSupplierId() : 0,
1474b1b8bc3fSopenharmony_ci        oldSupplier ? oldSupplier->GetNetSupplierIdent().c_str() : "null",
1475b1b8bc3fSopenharmony_ci        newSupplier ? newSupplier->GetSupplierId() : 0,
1476b1b8bc3fSopenharmony_ci        newSupplier ? newSupplier->GetNetSupplierIdent().c_str() : "null", oldSupplier == newSupplier);
1477b1b8bc3fSopenharmony_ci    if (oldSupplier == newSupplier) {
1478b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("old supplier equal to new supplier.");
1479b1b8bc3fSopenharmony_ci        return;
1480b1b8bc3fSopenharmony_ci    }
1481b1b8bc3fSopenharmony_ci    if (oldSupplier != nullptr) {
1482b1b8bc3fSopenharmony_ci        oldSupplier->ClearDefault();
1483b1b8bc3fSopenharmony_ci    }
1484b1b8bc3fSopenharmony_ci    if (newSupplier != nullptr) {
1485b1b8bc3fSopenharmony_ci        newSupplier->SetDefault();
1486b1b8bc3fSopenharmony_ci    }
1487b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1488b1b8bc3fSopenharmony_ci    oldSupplier = newSupplier;
1489b1b8bc3fSopenharmony_ci}
1490b1b8bc3fSopenharmony_ci
1491b1b8bc3fSopenharmony_civoid NetConnService::HandleDetectionResult(uint32_t supplierId, NetDetectionStatus netState)
1492b1b8bc3fSopenharmony_ci{
1493b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter HandleDetectionResult, ifValid[%{public}d]", netState);
1494b1b8bc3fSopenharmony_ci    auto supplier = FindNetSupplier(supplierId);
1495b1b8bc3fSopenharmony_ci    if (supplier == nullptr) {
1496b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("supplier doesn't exist.");
1497b1b8bc3fSopenharmony_ci        return;
1498b1b8bc3fSopenharmony_ci    }
1499b1b8bc3fSopenharmony_ci    supplier->SetNetValid(netState);
1500b1b8bc3fSopenharmony_ci    supplier->SetDetectionDone();
1501b1b8bc3fSopenharmony_ci    CallbackForSupplier(supplier, CALL_TYPE_UPDATE_CAP);
1502b1b8bc3fSopenharmony_ci    FindBestNetworkForAllRequest();
1503b1b8bc3fSopenharmony_ci    bool ifValid = netState == VERIFICATION_STATE;
1504b1b8bc3fSopenharmony_ci    if (!ifValid && defaultNetSupplier_ && defaultNetSupplier_->GetSupplierId() == supplierId) {
1505b1b8bc3fSopenharmony_ci        RequestAllNetworkExceptDefault();
1506b1b8bc3fSopenharmony_ci    }
1507b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter HandleDetectionResult end");
1508b1b8bc3fSopenharmony_ci}
1509b1b8bc3fSopenharmony_ci
1510b1b8bc3fSopenharmony_cistd::list<sptr<NetSupplier>> NetConnService::GetNetSupplierFromList(NetBearType bearerType, const std::string &ident)
1511b1b8bc3fSopenharmony_ci{
1512b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1513b1b8bc3fSopenharmony_ci    std::list<sptr<NetSupplier>> ret;
1514b1b8bc3fSopenharmony_ci    for (const auto &netSupplier : netSuppliers_) {
1515b1b8bc3fSopenharmony_ci        if (netSupplier.second == nullptr) {
1516b1b8bc3fSopenharmony_ci            continue;
1517b1b8bc3fSopenharmony_ci        }
1518b1b8bc3fSopenharmony_ci        if ((bearerType != netSupplier.second->GetNetSupplierType())) {
1519b1b8bc3fSopenharmony_ci            continue;
1520b1b8bc3fSopenharmony_ci        }
1521b1b8bc3fSopenharmony_ci        if (!ident.empty() && netSupplier.second->GetNetSupplierIdent() != ident) {
1522b1b8bc3fSopenharmony_ci            continue;
1523b1b8bc3fSopenharmony_ci        }
1524b1b8bc3fSopenharmony_ci        ret.push_back(netSupplier.second);
1525b1b8bc3fSopenharmony_ci    }
1526b1b8bc3fSopenharmony_ci    return ret;
1527b1b8bc3fSopenharmony_ci}
1528b1b8bc3fSopenharmony_ci
1529b1b8bc3fSopenharmony_cisptr<NetSupplier> NetConnService::GetNetSupplierFromList(NetBearType bearerType, const std::string &ident,
1530b1b8bc3fSopenharmony_ci                                                         const std::set<NetCap> &netCaps)
1531b1b8bc3fSopenharmony_ci{
1532b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1533b1b8bc3fSopenharmony_ci    for (const auto &netSupplier : netSuppliers_) {
1534b1b8bc3fSopenharmony_ci        if (netSupplier.second == nullptr) {
1535b1b8bc3fSopenharmony_ci            continue;
1536b1b8bc3fSopenharmony_ci        }
1537b1b8bc3fSopenharmony_ci        if ((bearerType == netSupplier.second->GetNetSupplierType()) &&
1538b1b8bc3fSopenharmony_ci            (ident == netSupplier.second->GetNetSupplierIdent()) && netSupplier.second->CompareNetCaps(netCaps)) {
1539b1b8bc3fSopenharmony_ci            return netSupplier.second;
1540b1b8bc3fSopenharmony_ci        }
1541b1b8bc3fSopenharmony_ci    }
1542b1b8bc3fSopenharmony_ci    return nullptr;
1543b1b8bc3fSopenharmony_ci}
1544b1b8bc3fSopenharmony_ci
1545b1b8bc3fSopenharmony_ciint32_t NetConnService::GetDefaultNet(int32_t &netId)
1546b1b8bc3fSopenharmony_ci{
1547b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1548b1b8bc3fSopenharmony_ci    if (!defaultNetSupplier_) {
1549b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("not found the netId");
1550b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
1551b1b8bc3fSopenharmony_ci    }
1552b1b8bc3fSopenharmony_ci
1553b1b8bc3fSopenharmony_ci    netId = defaultNetSupplier_->GetNetId();
1554b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("GetDefaultNet found the netId: [%{public}d]", netId);
1555b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1556b1b8bc3fSopenharmony_ci}
1557b1b8bc3fSopenharmony_ci
1558b1b8bc3fSopenharmony_ciint32_t NetConnService::GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList)
1559b1b8bc3fSopenharmony_ci{
1560b1b8bc3fSopenharmony_ci    return NetManagerCenter::GetInstance().GetAddressesByName(host, static_cast<uint16_t>(netId), addrList);
1561b1b8bc3fSopenharmony_ci}
1562b1b8bc3fSopenharmony_ci
1563b1b8bc3fSopenharmony_ciint32_t NetConnService::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr)
1564b1b8bc3fSopenharmony_ci{
1565b1b8bc3fSopenharmony_ci    std::vector<INetAddr> addrList;
1566b1b8bc3fSopenharmony_ci    int ret = GetAddressesByName(host, netId, addrList);
1567b1b8bc3fSopenharmony_ci    if (ret == NETMANAGER_SUCCESS) {
1568b1b8bc3fSopenharmony_ci        if (!addrList.empty()) {
1569b1b8bc3fSopenharmony_ci            addr = addrList[0];
1570b1b8bc3fSopenharmony_ci            return ret;
1571b1b8bc3fSopenharmony_ci        }
1572b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NO_ADDRESS;
1573b1b8bc3fSopenharmony_ci    }
1574b1b8bc3fSopenharmony_ci    return ret;
1575b1b8bc3fSopenharmony_ci}
1576b1b8bc3fSopenharmony_ci
1577b1b8bc3fSopenharmony_ciint32_t NetConnService::GetSpecificNet(NetBearType bearerType, std::list<int32_t> &netIdList)
1578b1b8bc3fSopenharmony_ci{
1579b1b8bc3fSopenharmony_ci    if (bearerType < BEARER_CELLULAR || bearerType >= BEARER_DEFAULT) {
1580b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netType parameter invalid");
1581b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NET_TYPE_NOT_FOUND;
1582b1b8bc3fSopenharmony_ci    }
1583b1b8bc3fSopenharmony_ci
1584b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1585b1b8bc3fSopenharmony_ci    NET_SUPPLIER_MAP::iterator iterSupplier;
1586b1b8bc3fSopenharmony_ci    for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) {
1587b1b8bc3fSopenharmony_ci        if (iterSupplier->second == nullptr) {
1588b1b8bc3fSopenharmony_ci            continue;
1589b1b8bc3fSopenharmony_ci        }
1590b1b8bc3fSopenharmony_ci        auto supplierType = iterSupplier->second->GetNetSupplierType();
1591b1b8bc3fSopenharmony_ci        if (bearerType == supplierType) {
1592b1b8bc3fSopenharmony_ci            netIdList.push_back(iterSupplier->second->GetNetId());
1593b1b8bc3fSopenharmony_ci        }
1594b1b8bc3fSopenharmony_ci    }
1595b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("netSuppliers_ size[%{public}zd] networks_ size[%{public}zd]", netSuppliers_.size(), networks_.size());
1596b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1597b1b8bc3fSopenharmony_ci}
1598b1b8bc3fSopenharmony_ci
1599b1b8bc3fSopenharmony_ciint32_t NetConnService::GetAllNets(std::list<int32_t> &netIdList)
1600b1b8bc3fSopenharmony_ci{
1601b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1602b1b8bc3fSopenharmony_ci    auto currentUid = IPCSkeleton::GetCallingUid();
1603b1b8bc3fSopenharmony_ci    for (const auto &network : networks_) {
1604b1b8bc3fSopenharmony_ci        if (network.second != nullptr && network.second->IsConnected()) {
1605b1b8bc3fSopenharmony_ci            auto netId = network.second->GetNetId();
1606b1b8bc3fSopenharmony_ci            sptr<NetSupplier> curSupplier = FindNetSupplier(network.second->GetSupplierId());
1607b1b8bc3fSopenharmony_ci            // inner virtual interface and uid is not trusted, skip
1608b1b8bc3fSopenharmony_ci            if (curSupplier != nullptr &&
1609b1b8bc3fSopenharmony_ci                curSupplier->HasNetCap(NetCap::NET_CAPABILITY_INTERNAL_DEFAULT) &&
1610b1b8bc3fSopenharmony_ci                !IsInRequestNetUids(currentUid)) {
1611b1b8bc3fSopenharmony_ci                NETMGR_LOG_D("Network [%{public}d] is internal, uid [%{public}d] skips.", netId, currentUid);
1612b1b8bc3fSopenharmony_ci                continue;
1613b1b8bc3fSopenharmony_ci            }
1614b1b8bc3fSopenharmony_ci            netIdList.push_back(netId);
1615b1b8bc3fSopenharmony_ci        }
1616b1b8bc3fSopenharmony_ci    }
1617b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("netSuppliers_ size[%{public}zd] netIdList size[%{public}zd]", netSuppliers_.size(), netIdList.size());
1618b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1619b1b8bc3fSopenharmony_ci}
1620b1b8bc3fSopenharmony_ci
1621b1b8bc3fSopenharmony_cibool NetConnService::IsInRequestNetUids(int32_t uid)
1622b1b8bc3fSopenharmony_ci{
1623b1b8bc3fSopenharmony_ci    return internalDefaultUidRequest_.count(uid) > 0;
1624b1b8bc3fSopenharmony_ci}
1625b1b8bc3fSopenharmony_ci
1626b1b8bc3fSopenharmony_ciint32_t NetConnService::GetSpecificUidNet(int32_t uid, int32_t &netId)
1627b1b8bc3fSopenharmony_ci{
1628b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter GetSpecificUidNet, uid is [%{public}d].", uid);
1629b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1630b1b8bc3fSopenharmony_ci    netId = INVALID_NET_ID;
1631b1b8bc3fSopenharmony_ci    NET_SUPPLIER_MAP::iterator iterSupplier;
1632b1b8bc3fSopenharmony_ci    for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) {
1633b1b8bc3fSopenharmony_ci        if ((iterSupplier->second != nullptr) && (uid == iterSupplier->second->GetSupplierUid()) &&
1634b1b8bc3fSopenharmony_ci            (iterSupplier->second->GetNetSupplierType() == BEARER_VPN)) {
1635b1b8bc3fSopenharmony_ci            netId = iterSupplier->second->GetNetId();
1636b1b8bc3fSopenharmony_ci            return NETMANAGER_SUCCESS;
1637b1b8bc3fSopenharmony_ci        }
1638b1b8bc3fSopenharmony_ci    }
1639b1b8bc3fSopenharmony_ci    if (defaultNetSupplier_ != nullptr) {
1640b1b8bc3fSopenharmony_ci        netId = defaultNetSupplier_->GetNetId();
1641b1b8bc3fSopenharmony_ci    }
1642b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("GetDefaultNet found the netId: [%{public}d]", netId);
1643b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1644b1b8bc3fSopenharmony_ci}
1645b1b8bc3fSopenharmony_ci
1646b1b8bc3fSopenharmony_ciint32_t NetConnService::GetConnectionProperties(int32_t netId, NetLinkInfo &info)
1647b1b8bc3fSopenharmony_ci{
1648b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1649b1b8bc3fSopenharmony_ci    auto iterNetwork = networks_.find(netId);
1650b1b8bc3fSopenharmony_ci    if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) {
1651b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_INVALID_NETWORK;
1652b1b8bc3fSopenharmony_ci    }
1653b1b8bc3fSopenharmony_ci
1654b1b8bc3fSopenharmony_ci    info = iterNetwork->second->GetNetLinkInfo();
1655b1b8bc3fSopenharmony_ci    if (info.mtu_ == 0) {
1656b1b8bc3fSopenharmony_ci        info.mtu_ = DEFAULT_MTU;
1657b1b8bc3fSopenharmony_ci    }
1658b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1659b1b8bc3fSopenharmony_ci}
1660b1b8bc3fSopenharmony_ci
1661b1b8bc3fSopenharmony_ciint32_t NetConnService::GetNetCapabilities(int32_t netId, NetAllCapabilities &netAllCap)
1662b1b8bc3fSopenharmony_ci{
1663b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1664b1b8bc3fSopenharmony_ci    NET_SUPPLIER_MAP::iterator iterSupplier;
1665b1b8bc3fSopenharmony_ci    for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) {
1666b1b8bc3fSopenharmony_ci        if ((iterSupplier->second != nullptr) && (netId == iterSupplier->second->GetNetId())) {
1667b1b8bc3fSopenharmony_ci            netAllCap = iterSupplier->second->GetNetCapabilities();
1668b1b8bc3fSopenharmony_ci            return NETMANAGER_SUCCESS;
1669b1b8bc3fSopenharmony_ci        }
1670b1b8bc3fSopenharmony_ci    }
1671b1b8bc3fSopenharmony_ci    return NET_CONN_ERR_INVALID_NETWORK;
1672b1b8bc3fSopenharmony_ci}
1673b1b8bc3fSopenharmony_ci
1674b1b8bc3fSopenharmony_ciint32_t NetConnService::GetIfaceNames(NetBearType bearerType, std::list<std::string> &ifaceNames)
1675b1b8bc3fSopenharmony_ci{
1676b1b8bc3fSopenharmony_ci    if (bearerType < BEARER_CELLULAR || bearerType >= BEARER_DEFAULT) {
1677b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NET_TYPE_NOT_FOUND;
1678b1b8bc3fSopenharmony_ci    }
1679b1b8bc3fSopenharmony_ci
1680b1b8bc3fSopenharmony_ci    if (netConnEventHandler_ == nullptr) {
1681b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netConnEventHandler_ is nullptr.");
1682b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
1683b1b8bc3fSopenharmony_ci    }
1684b1b8bc3fSopenharmony_ci    netConnEventHandler_->PostSyncTask([bearerType, &ifaceNames, this]() {
1685b1b8bc3fSopenharmony_ci        auto suppliers = GetNetSupplierFromList(bearerType);
1686b1b8bc3fSopenharmony_ci        for (auto supplier : suppliers) {
1687b1b8bc3fSopenharmony_ci            if (supplier == nullptr) {
1688b1b8bc3fSopenharmony_ci                continue;
1689b1b8bc3fSopenharmony_ci            }
1690b1b8bc3fSopenharmony_ci            std::shared_ptr<Network> network = supplier->GetNetwork();
1691b1b8bc3fSopenharmony_ci            if (network == nullptr) {
1692b1b8bc3fSopenharmony_ci                continue;
1693b1b8bc3fSopenharmony_ci            }
1694b1b8bc3fSopenharmony_ci            std::string ifaceName = network->GetIfaceName();
1695b1b8bc3fSopenharmony_ci            if (!ifaceName.empty()) {
1696b1b8bc3fSopenharmony_ci                ifaceNames.push_back(ifaceName);
1697b1b8bc3fSopenharmony_ci            }
1698b1b8bc3fSopenharmony_ci        }
1699b1b8bc3fSopenharmony_ci    });
1700b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1701b1b8bc3fSopenharmony_ci}
1702b1b8bc3fSopenharmony_ci
1703b1b8bc3fSopenharmony_ciint32_t NetConnService::GetIfaceNameByType(NetBearType bearerType, const std::string &ident, std::string &ifaceName)
1704b1b8bc3fSopenharmony_ci{
1705b1b8bc3fSopenharmony_ci    if (bearerType < BEARER_CELLULAR || bearerType >= BEARER_DEFAULT) {
1706b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netType parameter invalid");
1707b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NET_TYPE_NOT_FOUND;
1708b1b8bc3fSopenharmony_ci    }
1709b1b8bc3fSopenharmony_ci    if (netConnEventHandler_ == nullptr) {
1710b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netConnEventHandler_ is nullptr.");
1711b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
1712b1b8bc3fSopenharmony_ci    }
1713b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_SUCCESS;
1714b1b8bc3fSopenharmony_ci    netConnEventHandler_->PostSyncTask([bearerType, &ifaceName, &ident, &result, this]() {
1715b1b8bc3fSopenharmony_ci        auto suppliers = GetNetSupplierFromList(bearerType, ident);
1716b1b8bc3fSopenharmony_ci        if (suppliers.empty()) {
1717b1b8bc3fSopenharmony_ci            NETMGR_LOG_D("supplier is nullptr.");
1718b1b8bc3fSopenharmony_ci            result = NET_CONN_ERR_NO_SUPPLIER;
1719b1b8bc3fSopenharmony_ci            return;
1720b1b8bc3fSopenharmony_ci        }
1721b1b8bc3fSopenharmony_ci        auto supplier = suppliers.front();
1722b1b8bc3fSopenharmony_ci        if (supplier == nullptr) {
1723b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("supplier is nullptr");
1724b1b8bc3fSopenharmony_ci            result = NETMANAGER_ERR_LOCAL_PTR_NULL;
1725b1b8bc3fSopenharmony_ci            return;
1726b1b8bc3fSopenharmony_ci        }
1727b1b8bc3fSopenharmony_ci        std::shared_ptr<Network> network = supplier->GetNetwork();
1728b1b8bc3fSopenharmony_ci        if (network == nullptr) {
1729b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("network is nullptr");
1730b1b8bc3fSopenharmony_ci            result = NET_CONN_ERR_INVALID_NETWORK;
1731b1b8bc3fSopenharmony_ci            return;
1732b1b8bc3fSopenharmony_ci        }
1733b1b8bc3fSopenharmony_ci        ifaceName = network->GetIfaceName();
1734b1b8bc3fSopenharmony_ci    });
1735b1b8bc3fSopenharmony_ci    return result;
1736b1b8bc3fSopenharmony_ci}
1737b1b8bc3fSopenharmony_ci
1738b1b8bc3fSopenharmony_ciint32_t NetConnService::GetIfaceNameIdentMaps(NetBearType bearerType,
1739b1b8bc3fSopenharmony_ci                                              SafeMap<std::string, std::string> &ifaceNameIdentMaps)
1740b1b8bc3fSopenharmony_ci{
1741b1b8bc3fSopenharmony_ci    if (bearerType < BEARER_CELLULAR || bearerType >= BEARER_DEFAULT) {
1742b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NET_TYPE_NOT_FOUND;
1743b1b8bc3fSopenharmony_ci    }
1744b1b8bc3fSopenharmony_ci
1745b1b8bc3fSopenharmony_ci    if (netConnEventHandler_ == nullptr) {
1746b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netConnEventHandler_ is nullptr.");
1747b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
1748b1b8bc3fSopenharmony_ci    }
1749b1b8bc3fSopenharmony_ci    netConnEventHandler_->PostSyncTask([bearerType, &ifaceNameIdentMaps, this]() {
1750b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("Enter GetIfaceNameIdentMaps, netBearType=%{public}d", bearerType);
1751b1b8bc3fSopenharmony_ci        ifaceNameIdentMaps.Clear();
1752b1b8bc3fSopenharmony_ci        auto suppliers = GetNetSupplierFromList(bearerType);
1753b1b8bc3fSopenharmony_ci        for (auto supplier: suppliers) {
1754b1b8bc3fSopenharmony_ci            if (supplier == nullptr || !supplier->HasNetCap(NET_CAPABILITY_INTERNET)) {
1755b1b8bc3fSopenharmony_ci                continue;
1756b1b8bc3fSopenharmony_ci            }
1757b1b8bc3fSopenharmony_ci            std::shared_ptr <Network> network = supplier->GetNetwork();
1758b1b8bc3fSopenharmony_ci            if (network == nullptr || !network->IsConnected()) {
1759b1b8bc3fSopenharmony_ci                continue;
1760b1b8bc3fSopenharmony_ci            }
1761b1b8bc3fSopenharmony_ci            std::string ifaceName = network->GetIfaceName();
1762b1b8bc3fSopenharmony_ci            if (ifaceName.empty()) {
1763b1b8bc3fSopenharmony_ci                continue;
1764b1b8bc3fSopenharmony_ci            }
1765b1b8bc3fSopenharmony_ci            std::string ident = network->GetIdent();
1766b1b8bc3fSopenharmony_ci            ifaceNameIdentMaps.EnsureInsert(std::move(ifaceName), std::move(ident));
1767b1b8bc3fSopenharmony_ci        }
1768b1b8bc3fSopenharmony_ci    });
1769b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1770b1b8bc3fSopenharmony_ci}
1771b1b8bc3fSopenharmony_ci
1772b1b8bc3fSopenharmony_ciint32_t NetConnService::GetGlobalHttpProxy(HttpProxy &httpProxy)
1773b1b8bc3fSopenharmony_ci{
1774b1b8bc3fSopenharmony_ci    LoadGlobalHttpProxy(httpProxy);
1775b1b8bc3fSopenharmony_ci    if (httpProxy.GetHost().empty()) {
1776b1b8bc3fSopenharmony_ci        httpProxy.SetPort(0);
1777b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("The http proxy host is empty");
1778b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
1779b1b8bc3fSopenharmony_ci    }
1780b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1781b1b8bc3fSopenharmony_ci}
1782b1b8bc3fSopenharmony_ci
1783b1b8bc3fSopenharmony_ciint32_t NetConnService::GetDefaultHttpProxy(int32_t bindNetId, HttpProxy &httpProxy)
1784b1b8bc3fSopenharmony_ci{
1785b1b8bc3fSopenharmony_ci    auto startTime = std::chrono::steady_clock::now();
1786b1b8bc3fSopenharmony_ci    LoadGlobalHttpProxy(httpProxy);
1787b1b8bc3fSopenharmony_ci    if (!httpProxy.GetHost().empty()) {
1788b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("Return global http proxy as default.");
1789b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
1790b1b8bc3fSopenharmony_ci    }
1791b1b8bc3fSopenharmony_ci
1792b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1793b1b8bc3fSopenharmony_ci    auto iter = networks_.find(bindNetId);
1794b1b8bc3fSopenharmony_ci    if ((iter != networks_.end()) && (iter->second != nullptr)) {
1795b1b8bc3fSopenharmony_ci        httpProxy = iter->second->GetNetLinkInfo().httpProxy_;
1796b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("Return bound network's http proxy as default.");
1797b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
1798b1b8bc3fSopenharmony_ci    }
1799b1b8bc3fSopenharmony_ci
1800b1b8bc3fSopenharmony_ci    if (defaultNetSupplier_ != nullptr) {
1801b1b8bc3fSopenharmony_ci        defaultNetSupplier_->GetHttpProxy(httpProxy);
1802b1b8bc3fSopenharmony_ci        auto endTime = std::chrono::steady_clock::now();
1803b1b8bc3fSopenharmony_ci        auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime);
1804b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("Use default http proxy, cost=%{public}lld",  durationNs.count());
1805b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
1806b1b8bc3fSopenharmony_ci    }
1807b1b8bc3fSopenharmony_ci    auto endTime = std::chrono::steady_clock::now();
1808b1b8bc3fSopenharmony_ci    auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime);
1809b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("No default http proxy, durationNs=%{public}lld", durationNs.count());
1810b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1811b1b8bc3fSopenharmony_ci}
1812b1b8bc3fSopenharmony_ci
1813b1b8bc3fSopenharmony_ciint32_t NetConnService::GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList)
1814b1b8bc3fSopenharmony_ci{
1815b1b8bc3fSopenharmony_ci    if (ident.empty()) {
1816b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("The identifier in service is null");
1817b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
1818b1b8bc3fSopenharmony_ci    }
1819b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1820b1b8bc3fSopenharmony_ci    for (auto iterSupplier : netSuppliers_) {
1821b1b8bc3fSopenharmony_ci        if (iterSupplier.second == nullptr) {
1822b1b8bc3fSopenharmony_ci            continue;
1823b1b8bc3fSopenharmony_ci        }
1824b1b8bc3fSopenharmony_ci        if (iterSupplier.second->GetNetSupplierIdent() == ident) {
1825b1b8bc3fSopenharmony_ci            int32_t netId = iterSupplier.second->GetNetId();
1826b1b8bc3fSopenharmony_ci            netIdList.push_back(netId);
1827b1b8bc3fSopenharmony_ci        }
1828b1b8bc3fSopenharmony_ci    }
1829b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1830b1b8bc3fSopenharmony_ci}
1831b1b8bc3fSopenharmony_ci
1832b1b8bc3fSopenharmony_civoid NetConnService::GetDumpMessage(std::string &message)
1833b1b8bc3fSopenharmony_ci{
1834b1b8bc3fSopenharmony_ci    message.append("Net connect Info:\n");
1835b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1836b1b8bc3fSopenharmony_ci    if (defaultNetSupplier_) {
1837b1b8bc3fSopenharmony_ci        message.append("\tSupplierId: " + std::to_string(defaultNetSupplier_->GetSupplierId()) + "\n");
1838b1b8bc3fSopenharmony_ci        std::shared_ptr<Network> network = defaultNetSupplier_->GetNetwork();
1839b1b8bc3fSopenharmony_ci        if (network) {
1840b1b8bc3fSopenharmony_ci            message.append("\tNetId: " + std::to_string(network->GetNetId()) + "\n");
1841b1b8bc3fSopenharmony_ci        } else {
1842b1b8bc3fSopenharmony_ci            message.append("\tNetId: " + std::to_string(INVALID_NET_ID) + "\n");
1843b1b8bc3fSopenharmony_ci        }
1844b1b8bc3fSopenharmony_ci        message.append("\tConnStat: " + std::to_string(defaultNetSupplier_->IsConnected()) + "\n");
1845b1b8bc3fSopenharmony_ci        message.append("\tIsAvailable: " + std::to_string(defaultNetSupplier_->IsNetValidated()) + "\n");
1846b1b8bc3fSopenharmony_ci        message.append("\tIsRoaming: " + std::to_string(defaultNetSupplier_->GetRoaming()) + "\n");
1847b1b8bc3fSopenharmony_ci        message.append("\tStrength: " + std::to_string(defaultNetSupplier_->GetStrength()) + "\n");
1848b1b8bc3fSopenharmony_ci        message.append("\tFrequency: " + std::to_string(defaultNetSupplier_->GetFrequency()) + "\n");
1849b1b8bc3fSopenharmony_ci        message.append("\tLinkUpBandwidthKbps: " +
1850b1b8bc3fSopenharmony_ci                       std::to_string(defaultNetSupplier_->GetNetCapabilities().linkUpBandwidthKbps_) + "\n");
1851b1b8bc3fSopenharmony_ci        message.append("\tLinkDownBandwidthKbps: " +
1852b1b8bc3fSopenharmony_ci                       std::to_string(defaultNetSupplier_->GetNetCapabilities().linkDownBandwidthKbps_) + "\n");
1853b1b8bc3fSopenharmony_ci        message.append("\tUid: " + std::to_string(defaultNetSupplier_->GetSupplierUid()) + "\n");
1854b1b8bc3fSopenharmony_ci    } else {
1855b1b8bc3fSopenharmony_ci        message.append("\tdefaultNetSupplier_ is nullptr\n");
1856b1b8bc3fSopenharmony_ci        message.append("\tSupplierId: \n");
1857b1b8bc3fSopenharmony_ci        message.append("\tNetId: 0\n");
1858b1b8bc3fSopenharmony_ci        message.append("\tConnStat: 0\n");
1859b1b8bc3fSopenharmony_ci        message.append("\tIsAvailable: \n");
1860b1b8bc3fSopenharmony_ci        message.append("\tIsRoaming: 0\n");
1861b1b8bc3fSopenharmony_ci        message.append("\tStrength: 0\n");
1862b1b8bc3fSopenharmony_ci        message.append("\tFrequency: 0\n");
1863b1b8bc3fSopenharmony_ci        message.append("\tLinkUpBandwidthKbps: 0\n");
1864b1b8bc3fSopenharmony_ci        message.append("\tLinkDownBandwidthKbps: 0\n");
1865b1b8bc3fSopenharmony_ci        message.append("\tUid: 0\n");
1866b1b8bc3fSopenharmony_ci    }
1867b1b8bc3fSopenharmony_ci    if (dnsResultCallback_ != nullptr) {
1868b1b8bc3fSopenharmony_ci        dnsResultCallback_->GetDumpMessageForDnsResult(message);
1869b1b8bc3fSopenharmony_ci    }
1870b1b8bc3fSopenharmony_ci}
1871b1b8bc3fSopenharmony_ci
1872b1b8bc3fSopenharmony_ciint32_t NetConnService::HasDefaultNet(bool &flag)
1873b1b8bc3fSopenharmony_ci{
1874b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1875b1b8bc3fSopenharmony_ci    if (!defaultNetSupplier_) {
1876b1b8bc3fSopenharmony_ci        flag = false;
1877b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
1878b1b8bc3fSopenharmony_ci    }
1879b1b8bc3fSopenharmony_ci    flag = true;
1880b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1881b1b8bc3fSopenharmony_ci}
1882b1b8bc3fSopenharmony_ci
1883b1b8bc3fSopenharmony_ciint32_t NetConnService::IsDefaultNetMetered(bool &isMetered)
1884b1b8bc3fSopenharmony_ci{
1885b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
1886b1b8bc3fSopenharmony_ci    if (defaultNetSupplier_) {
1887b1b8bc3fSopenharmony_ci        isMetered = !defaultNetSupplier_->HasNetCap(NET_CAPABILITY_NOT_METERED);
1888b1b8bc3fSopenharmony_ci    } else {
1889b1b8bc3fSopenharmony_ci        isMetered = true;
1890b1b8bc3fSopenharmony_ci    }
1891b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1892b1b8bc3fSopenharmony_ci}
1893b1b8bc3fSopenharmony_ci
1894b1b8bc3fSopenharmony_ciint32_t NetConnService::BindSocket(int32_t socketFd, int32_t netId)
1895b1b8bc3fSopenharmony_ci{
1896b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Enter BindSocket.");
1897b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().BindSocket(socketFd, netId);
1898b1b8bc3fSopenharmony_ci}
1899b1b8bc3fSopenharmony_ci
1900b1b8bc3fSopenharmony_ciint32_t NetConnService::Dump(int32_t fd, const std::vector<std::u16string> &args)
1901b1b8bc3fSopenharmony_ci{
1902b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Start Dump, fd: %{public}d", fd);
1903b1b8bc3fSopenharmony_ci    std::string result;
1904b1b8bc3fSopenharmony_ci    GetDumpMessage(result);
1905b1b8bc3fSopenharmony_ci    int32_t ret = dprintf(fd, "%s\n", result.c_str());
1906b1b8bc3fSopenharmony_ci    return (ret < 0) ? static_cast<int32_t>(NET_CONN_ERR_CREATE_DUMP_FAILED) : static_cast<int32_t>(NETMANAGER_SUCCESS);
1907b1b8bc3fSopenharmony_ci}
1908b1b8bc3fSopenharmony_ci
1909b1b8bc3fSopenharmony_cibool NetConnService::IsValidDecValue(const std::string &inputValue)
1910b1b8bc3fSopenharmony_ci{
1911b1b8bc3fSopenharmony_ci    if (inputValue.length() > INPUT_VALUE_LENGTH) {
1912b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("The value entered is out of range, value:%{public}s", inputValue.c_str());
1913b1b8bc3fSopenharmony_ci        return false;
1914b1b8bc3fSopenharmony_ci    }
1915b1b8bc3fSopenharmony_ci    bool isValueNumber = regex_match(inputValue, std::regex("(-[\\d+]+)|(\\d+)"));
1916b1b8bc3fSopenharmony_ci    if (isValueNumber) {
1917b1b8bc3fSopenharmony_ci        int64_t numberValue = std::stoll(inputValue);
1918b1b8bc3fSopenharmony_ci        if ((numberValue >= INT32_MIN) && (numberValue <= INT32_MAX)) {
1919b1b8bc3fSopenharmony_ci            return true;
1920b1b8bc3fSopenharmony_ci        }
1921b1b8bc3fSopenharmony_ci    }
1922b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("InputValue is not a decimal number");
1923b1b8bc3fSopenharmony_ci    return false;
1924b1b8bc3fSopenharmony_ci}
1925b1b8bc3fSopenharmony_ci
1926b1b8bc3fSopenharmony_ciint32_t NetConnService::GetDelayNotifyTime()
1927b1b8bc3fSopenharmony_ci{
1928b1b8bc3fSopenharmony_ci    char param[SYS_PARAMETER_SIZE] = { 0 };
1929b1b8bc3fSopenharmony_ci    int32_t delayTime = 0;
1930b1b8bc3fSopenharmony_ci    int32_t code = GetParameter(CFG_NETWORK_PRE_AIRPLANE_MODE_WAIT_TIMES, NO_DELAY_TIME_CONFIG,
1931b1b8bc3fSopenharmony_ci                                param, SYS_PARAMETER_SIZE);
1932b1b8bc3fSopenharmony_ci    std::string time = param;
1933b1b8bc3fSopenharmony_ci    if (code <= 0 || !IsValidDecValue(time)) {
1934b1b8bc3fSopenharmony_ci        try {
1935b1b8bc3fSopenharmony_ci            delayTime = std::stoi(NO_DELAY_TIME_CONFIG);
1936b1b8bc3fSopenharmony_ci        } catch (const std::invalid_argument& e) {
1937b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("invalid_argument");
1938b1b8bc3fSopenharmony_ci            return delayTime;
1939b1b8bc3fSopenharmony_ci        } catch (const std::out_of_range& e) {
1940b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("out_of_range");
1941b1b8bc3fSopenharmony_ci            return delayTime;
1942b1b8bc3fSopenharmony_ci        }
1943b1b8bc3fSopenharmony_ci    } else {
1944b1b8bc3fSopenharmony_ci        try {
1945b1b8bc3fSopenharmony_ci            auto tmp = std::stoi(time);
1946b1b8bc3fSopenharmony_ci            delayTime = tmp > static_cast<int32_t>(MAX_DELAY_TIME) ? std::stoi(NO_DELAY_TIME_CONFIG) : tmp;
1947b1b8bc3fSopenharmony_ci        } catch (const std::invalid_argument& e) {
1948b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("invalid_argument");
1949b1b8bc3fSopenharmony_ci            return delayTime;
1950b1b8bc3fSopenharmony_ci        } catch (const std::out_of_range& e) {
1951b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("out_of_range");
1952b1b8bc3fSopenharmony_ci            return delayTime;
1953b1b8bc3fSopenharmony_ci        }
1954b1b8bc3fSopenharmony_ci    }
1955b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("delay time is %{public}d", delayTime);
1956b1b8bc3fSopenharmony_ci    return delayTime;
1957b1b8bc3fSopenharmony_ci}
1958b1b8bc3fSopenharmony_ci
1959b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)
1960b1b8bc3fSopenharmony_ci{
1961b1b8bc3fSopenharmony_ci    int32_t callingUid = static_cast<int32_t>(IPCSkeleton::GetCallingUid());
1962b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("RegisterPreAirplaneCallback, calllinguid [%{public}d]", callingUid);
1963b1b8bc3fSopenharmony_ci    std::lock_guard guard(preAirplaneCbsMutex_);
1964b1b8bc3fSopenharmony_ci    preAirplaneCallbacks_[callingUid] = callback;
1965b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1966b1b8bc3fSopenharmony_ci}
1967b1b8bc3fSopenharmony_ci
1968b1b8bc3fSopenharmony_ciint32_t NetConnService::UnregisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)
1969b1b8bc3fSopenharmony_ci{
1970b1b8bc3fSopenharmony_ci    int32_t callingUid = static_cast<int32_t>(IPCSkeleton::GetCallingUid());
1971b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("UnregisterPreAirplaneCallback, calllinguid [%{public}d]", callingUid);
1972b1b8bc3fSopenharmony_ci    std::lock_guard guard(preAirplaneCbsMutex_);
1973b1b8bc3fSopenharmony_ci    preAirplaneCallbacks_.erase(callingUid);
1974b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
1975b1b8bc3fSopenharmony_ci}
1976b1b8bc3fSopenharmony_ci
1977b1b8bc3fSopenharmony_ciint32_t NetConnService::SetAirplaneMode(bool state)
1978b1b8bc3fSopenharmony_ci{
1979b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter SetAirplaneMode, AirplaneMode is %{public}d", state);
1980b1b8bc3fSopenharmony_ci    if (state) {
1981b1b8bc3fSopenharmony_ci        std::lock_guard guard(preAirplaneCbsMutex_);
1982b1b8bc3fSopenharmony_ci        for (const auto& mem : preAirplaneCallbacks_) {
1983b1b8bc3fSopenharmony_ci            if (mem.second != nullptr) {
1984b1b8bc3fSopenharmony_ci                int32_t ret = mem.second->PreAirplaneStart();
1985b1b8bc3fSopenharmony_ci                NETMGR_LOG_D("PreAirplaneStart result %{public}d", ret);
1986b1b8bc3fSopenharmony_ci            }
1987b1b8bc3fSopenharmony_ci        }
1988b1b8bc3fSopenharmony_ci    }
1989b1b8bc3fSopenharmony_ci    if (netConnEventHandler_ == nullptr) {
1990b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netConnEventHandler_ is nullptr.");
1991b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
1992b1b8bc3fSopenharmony_ci    }
1993b1b8bc3fSopenharmony_ci    netConnEventHandler_->RemoveAsyncTask("delay airplane mode");
1994b1b8bc3fSopenharmony_ci    auto delayTime = GetDelayNotifyTime();
1995b1b8bc3fSopenharmony_ci
1996b1b8bc3fSopenharmony_ci    bool ret = netConnEventHandler_->PostAsyncTask(
1997b1b8bc3fSopenharmony_ci        [state]() {
1998b1b8bc3fSopenharmony_ci            NETMGR_LOG_I("Enter delay");
1999b1b8bc3fSopenharmony_ci            auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
2000b1b8bc3fSopenharmony_ci            std::string airplaneMode = std::to_string(state);
2001b1b8bc3fSopenharmony_ci            Uri uri(AIRPLANE_MODE_URI);
2002b1b8bc3fSopenharmony_ci            int32_t ret = dataShareHelperUtils->Update(uri, KEY_AIRPLANE_MODE, airplaneMode);
2003b1b8bc3fSopenharmony_ci            if (ret != NETMANAGER_SUCCESS) {
2004b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("Update airplane mode:%{public}d to datashare failed.", state);
2005b1b8bc3fSopenharmony_ci                return;
2006b1b8bc3fSopenharmony_ci            }
2007b1b8bc3fSopenharmony_ci            BroadcastInfo info;
2008b1b8bc3fSopenharmony_ci            info.action = EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED;
2009b1b8bc3fSopenharmony_ci            info.data = "Net Manager Airplane Mode Changed";
2010b1b8bc3fSopenharmony_ci            info.code = static_cast<int32_t>(state);
2011b1b8bc3fSopenharmony_ci            info.ordered = false;
2012b1b8bc3fSopenharmony_ci            std::map<std::string, int32_t> param;
2013b1b8bc3fSopenharmony_ci            BroadcastManager::GetInstance().SendBroadcast(info, param);
2014b1b8bc3fSopenharmony_ci        },
2015b1b8bc3fSopenharmony_ci        "delay airplane mode", delayTime);
2016b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("SetAirplaneMode out [%{public}d]", ret);
2017b1b8bc3fSopenharmony_ci
2018b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2019b1b8bc3fSopenharmony_ci}
2020b1b8bc3fSopenharmony_ci
2021b1b8bc3fSopenharmony_civoid NetConnService::ActiveHttpProxy()
2022b1b8bc3fSopenharmony_ci{
2023b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("ActiveHttpProxy thread start");
2024b1b8bc3fSopenharmony_ci    while (httpProxyThreadNeedRun_.load()) {
2025b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("Keep global http-proxy active every 2 minutes");
2026b1b8bc3fSopenharmony_ci        CURL *curl = nullptr;
2027b1b8bc3fSopenharmony_ci        HttpProxy tempProxy;
2028b1b8bc3fSopenharmony_ci        {
2029b1b8bc3fSopenharmony_ci            auto userInfoHelp = NetProxyUserinfo::GetInstance();
2030b1b8bc3fSopenharmony_ci            LoadGlobalHttpProxy(tempProxy);
2031b1b8bc3fSopenharmony_ci            userInfoHelp.GetHttpProxyHostPass(tempProxy);
2032b1b8bc3fSopenharmony_ci        }
2033b1b8bc3fSopenharmony_ci        auto proxyType = (tempProxy.host_.find("https://") != std::string::npos) ? CURLPROXY_HTTPS : CURLPROXY_HTTP;
2034b1b8bc3fSopenharmony_ci        if (!tempProxy.host_.empty() && !tempProxy.username_.empty()) {
2035b1b8bc3fSopenharmony_ci            std::string httpUrl;
2036b1b8bc3fSopenharmony_ci            GetHttpUrlFromConfig(httpUrl);
2037b1b8bc3fSopenharmony_ci            if (httpUrl.empty()) {
2038b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("ActiveHttpProxy thread get url failed!");
2039b1b8bc3fSopenharmony_ci                continue;
2040b1b8bc3fSopenharmony_ci            }
2041b1b8bc3fSopenharmony_ci            curl = curl_easy_init();
2042b1b8bc3fSopenharmony_ci            curl_easy_setopt(curl, CURLOPT_URL, httpUrl.c_str());
2043b1b8bc3fSopenharmony_ci            curl_easy_setopt(curl, CURLOPT_PROXY, tempProxy.host_.c_str());
2044b1b8bc3fSopenharmony_ci            curl_easy_setopt(curl, CURLOPT_PROXYPORT, tempProxy.port_);
2045b1b8bc3fSopenharmony_ci            curl_easy_setopt(curl, CURLOPT_PROXYTYPE, proxyType);
2046b1b8bc3fSopenharmony_ci            curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, tempProxy.username_.c_str());
2047b1b8bc3fSopenharmony_ci            curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
2048b1b8bc3fSopenharmony_ci            if (!tempProxy.password_.empty()) {
2049b1b8bc3fSopenharmony_ci                curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, tempProxy.password_.c_str());
2050b1b8bc3fSopenharmony_ci            }
2051b1b8bc3fSopenharmony_ci        }
2052b1b8bc3fSopenharmony_ci        if (curl) {
2053b1b8bc3fSopenharmony_ci            auto ret = curl_easy_perform(curl);
2054b1b8bc3fSopenharmony_ci            NETMGR_LOG_I("SetGlobalHttpProxy ActiveHttpProxy %{public}d", static_cast<int>(ret));
2055b1b8bc3fSopenharmony_ci            curl_easy_cleanup(curl);
2056b1b8bc3fSopenharmony_ci        }
2057b1b8bc3fSopenharmony_ci        if (httpProxyThreadNeedRun_.load()) {
2058b1b8bc3fSopenharmony_ci            std::unique_lock lock(httpProxyThreadMutex_);
2059b1b8bc3fSopenharmony_ci            httpProxyThreadCv_.wait_for(lock, std::chrono::seconds(HTTP_PROXY_ACTIVE_PERIOD_S));
2060b1b8bc3fSopenharmony_ci        } else {
2061b1b8bc3fSopenharmony_ci            NETMGR_LOG_W("ActiveHttpProxy has been clear.");
2062b1b8bc3fSopenharmony_ci        }
2063b1b8bc3fSopenharmony_ci    }
2064b1b8bc3fSopenharmony_ci}
2065b1b8bc3fSopenharmony_ci
2066b1b8bc3fSopenharmony_civoid NetConnService::GetHttpUrlFromConfig(std::string &httpUrl)
2067b1b8bc3fSopenharmony_ci{
2068b1b8bc3fSopenharmony_ci    if (!std::filesystem::exists(URL_CFG_FILE)) {
2069b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("File not exist (%{public}s)", URL_CFG_FILE);
2070b1b8bc3fSopenharmony_ci        return;
2071b1b8bc3fSopenharmony_ci    }
2072b1b8bc3fSopenharmony_ci
2073b1b8bc3fSopenharmony_ci    std::ifstream file(URL_CFG_FILE);
2074b1b8bc3fSopenharmony_ci    if (!file.is_open()) {
2075b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Open file failed (%{public}s)", strerror(errno));
2076b1b8bc3fSopenharmony_ci        return;
2077b1b8bc3fSopenharmony_ci    }
2078b1b8bc3fSopenharmony_ci
2079b1b8bc3fSopenharmony_ci    std::ostringstream oss;
2080b1b8bc3fSopenharmony_ci    oss << file.rdbuf();
2081b1b8bc3fSopenharmony_ci    std::string content = oss.str();
2082b1b8bc3fSopenharmony_ci    auto pos = content.find(HTTP_URL_HEADER);
2083b1b8bc3fSopenharmony_ci    if (pos != std::string::npos) {
2084b1b8bc3fSopenharmony_ci        pos += strlen(HTTP_URL_HEADER);
2085b1b8bc3fSopenharmony_ci        httpUrl = content.substr(pos, content.find(NEW_LINE_STR, pos) - pos);
2086b1b8bc3fSopenharmony_ci    }
2087b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("Get net detection http url:[%{public}s]", httpUrl.c_str());
2088b1b8bc3fSopenharmony_ci}
2089b1b8bc3fSopenharmony_ci
2090b1b8bc3fSopenharmony_ciint32_t NetConnService::SetGlobalHttpProxy(const HttpProxy &httpProxy)
2091b1b8bc3fSopenharmony_ci{
2092b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter SetGlobalHttpProxy. httpproxy = %{public}zu", httpProxy.GetHost().length());
2093b1b8bc3fSopenharmony_ci    HttpProxy oldHttpProxy;
2094b1b8bc3fSopenharmony_ci    LoadGlobalHttpProxy(oldHttpProxy);
2095b1b8bc3fSopenharmony_ci    if (oldHttpProxy != httpProxy) {
2096b1b8bc3fSopenharmony_ci        HttpProxy newHttpProxy = httpProxy;
2097b1b8bc3fSopenharmony_ci        httpProxyThreadCv_.notify_all();
2098b1b8bc3fSopenharmony_ci        int32_t userId;
2099b1b8bc3fSopenharmony_ci        int32_t ret = GetCallingUserId(userId);
2100b1b8bc3fSopenharmony_ci        if (ret != NETMANAGER_SUCCESS) {
2101b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("GlobalHttpProxy get calling userId fail.");
2102b1b8bc3fSopenharmony_ci            return ret;
2103b1b8bc3fSopenharmony_ci        }
2104b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("GlobalHttpProxy userId is %{public}d", userId);
2105b1b8bc3fSopenharmony_ci        NetHttpProxyTracker httpProxyTracker;
2106b1b8bc3fSopenharmony_ci        if (IsPrimaryUserId(userId)) {
2107b1b8bc3fSopenharmony_ci            if (!httpProxyTracker.WriteToSettingsData(newHttpProxy)) {
2108b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("GlobalHttpProxy write settingDate fail.");
2109b1b8bc3fSopenharmony_ci                return NETMANAGER_ERR_INTERNAL;
2110b1b8bc3fSopenharmony_ci            }
2111b1b8bc3fSopenharmony_ci        }
2112b1b8bc3fSopenharmony_ci        if (!httpProxyTracker.WriteToSettingsDataUser(newHttpProxy, userId)) {
2113b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("GlobalHttpProxy write settingDateUser fail. userId=%{public}d", userId);
2114b1b8bc3fSopenharmony_ci            return NETMANAGER_ERR_INTERNAL;
2115b1b8bc3fSopenharmony_ci        }
2116b1b8bc3fSopenharmony_ci        globalHttpProxyCache_.EnsureInsert(userId, newHttpProxy);
2117b1b8bc3fSopenharmony_ci        SendHttpProxyChangeBroadcast(newHttpProxy);
2118b1b8bc3fSopenharmony_ci        UpdateGlobalHttpProxy(newHttpProxy);
2119b1b8bc3fSopenharmony_ci    }
2120b1b8bc3fSopenharmony_ci    if (!httpProxyThreadNeedRun_ && !httpProxy.GetUsername().empty()) {
2121b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("ActiveHttpProxy  user.len[%{public}zu], pwd.len[%{public}zu]", httpProxy.username_.length(),
2122b1b8bc3fSopenharmony_ci                     httpProxy.password_.length());
2123b1b8bc3fSopenharmony_ci        httpProxyThreadNeedRun_ = true;
2124b1b8bc3fSopenharmony_ci        std::thread t([this]() { ActiveHttpProxy(); });
2125b1b8bc3fSopenharmony_ci        std::string threadName = "ActiveHttpProxy";
2126b1b8bc3fSopenharmony_ci        pthread_setname_np(t.native_handle(), threadName.c_str());
2127b1b8bc3fSopenharmony_ci        t.detach();
2128b1b8bc3fSopenharmony_ci    } else if (httpProxyThreadNeedRun_ && httpProxy.GetHost().empty()) {
2129b1b8bc3fSopenharmony_ci        httpProxyThreadNeedRun_ = false;
2130b1b8bc3fSopenharmony_ci    }
2131b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("End SetGlobalHttpProxy.");
2132b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2133b1b8bc3fSopenharmony_ci}
2134b1b8bc3fSopenharmony_ci
2135b1b8bc3fSopenharmony_ciint32_t NetConnService::GetCallingUserId(int32_t &userId)
2136b1b8bc3fSopenharmony_ci{
2137b1b8bc3fSopenharmony_ci    std::vector<int> activeIds;
2138b1b8bc3fSopenharmony_ci    int ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds);
2139b1b8bc3fSopenharmony_ci    if (ret != 0) {
2140b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("QueryActiveOsAccountIds failed. ret is %{public}d", ret);
2141b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INTERNAL;
2142b1b8bc3fSopenharmony_ci    }
2143b1b8bc3fSopenharmony_ci    if (activeIds.empty()) {
2144b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("QueryActiveOsAccountIds is empty");
2145b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INTERNAL;
2146b1b8bc3fSopenharmony_ci    }
2147b1b8bc3fSopenharmony_ci    userId = activeIds[0];
2148b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2149b1b8bc3fSopenharmony_ci}
2150b1b8bc3fSopenharmony_ci
2151b1b8bc3fSopenharmony_ciint32_t NetConnService::SetAppNet(int32_t netId)
2152b1b8bc3fSopenharmony_ci{
2153b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2154b1b8bc3fSopenharmony_ci}
2155b1b8bc3fSopenharmony_ci
2156b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback)
2157b1b8bc3fSopenharmony_ci{
2158b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
2159b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("callback is nullptr");
2160b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
2161b1b8bc3fSopenharmony_ci    }
2162b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter RegisterNetInterfaceCallback.");
2163b1b8bc3fSopenharmony_ci    if (interfaceStateCallback_ == nullptr) {
2164b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("interfaceStateCallback_ is nullptr");
2165b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
2166b1b8bc3fSopenharmony_ci    }
2167b1b8bc3fSopenharmony_ci    return interfaceStateCallback_->RegisterInterfaceCallback(callback);
2168b1b8bc3fSopenharmony_ci}
2169b1b8bc3fSopenharmony_ci
2170b1b8bc3fSopenharmony_ciint32_t NetConnService::GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config)
2171b1b8bc3fSopenharmony_ci{
2172b1b8bc3fSopenharmony_ci    using namespace OHOS::nmd;
2173b1b8bc3fSopenharmony_ci    InterfaceConfigurationParcel configParcel;
2174b1b8bc3fSopenharmony_ci    configParcel.ifName = iface;
2175b1b8bc3fSopenharmony_ci    if (NetsysController::GetInstance().GetInterfaceConfig(configParcel) != NETMANAGER_SUCCESS) {
2176b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INTERNAL;
2177b1b8bc3fSopenharmony_ci    }
2178b1b8bc3fSopenharmony_ci    config.ifName_ = configParcel.ifName;
2179b1b8bc3fSopenharmony_ci    config.hwAddr_ = configParcel.hwAddr;
2180b1b8bc3fSopenharmony_ci    config.ipv4Addr_ = configParcel.ipv4Addr;
2181b1b8bc3fSopenharmony_ci    config.prefixLength_ = configParcel.prefixLength;
2182b1b8bc3fSopenharmony_ci    config.flags_.assign(configParcel.flags.begin(), configParcel.flags.end());
2183b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2184b1b8bc3fSopenharmony_ci}
2185b1b8bc3fSopenharmony_ci
2186b1b8bc3fSopenharmony_ciint32_t NetConnService::NetDetectionForDnsHealth(int32_t netId, bool dnsHealthSuccess)
2187b1b8bc3fSopenharmony_ci{
2188b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
2189b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2190b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([netId, dnsHealthSuccess, &result, this]() {
2191b1b8bc3fSopenharmony_ci            result = this->NetDetectionForDnsHealthSync(netId, dnsHealthSuccess);
2192b1b8bc3fSopenharmony_ci        });
2193b1b8bc3fSopenharmony_ci    }
2194b1b8bc3fSopenharmony_ci    return result;
2195b1b8bc3fSopenharmony_ci}
2196b1b8bc3fSopenharmony_ci
2197b1b8bc3fSopenharmony_civoid NetConnService::LoadGlobalHttpProxy(HttpProxy &httpProxy)
2198b1b8bc3fSopenharmony_ci{
2199b1b8bc3fSopenharmony_ci    int32_t userId;
2200b1b8bc3fSopenharmony_ci    int32_t ret = GetCallingUserId(userId);
2201b1b8bc3fSopenharmony_ci    if (ret != NETMANAGER_SUCCESS) {
2202b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("LoadGlobalHttpProxy get calling userId fail.");
2203b1b8bc3fSopenharmony_ci        return;
2204b1b8bc3fSopenharmony_ci    }
2205b1b8bc3fSopenharmony_ci    if (globalHttpProxyCache_.Find(userId, httpProxy)) {
2206b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("Global http proxy has been loaded from the SettingsData database. userId=%{public}d", userId);
2207b1b8bc3fSopenharmony_ci        return;
2208b1b8bc3fSopenharmony_ci    }
2209b1b8bc3fSopenharmony_ci    if (!isDataShareReady_.load() && !CheckIfSettingsDataReady()) {
2210b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("data share is not ready.");
2211b1b8bc3fSopenharmony_ci        return;
2212b1b8bc3fSopenharmony_ci    }
2213b1b8bc3fSopenharmony_ci    NetHttpProxyTracker httpProxyTracker;
2214b1b8bc3fSopenharmony_ci    HttpProxy tmpHttpProxy;
2215b1b8bc3fSopenharmony_ci    if (IsPrimaryUserId(userId)) {
2216b1b8bc3fSopenharmony_ci        httpProxyTracker.ReadFromSettingsData(tmpHttpProxy);
2217b1b8bc3fSopenharmony_ci    } else {
2218b1b8bc3fSopenharmony_ci        httpProxyTracker.ReadFromSettingsDataUser(tmpHttpProxy, userId);
2219b1b8bc3fSopenharmony_ci    }
2220b1b8bc3fSopenharmony_ci    {
2221b1b8bc3fSopenharmony_ci        std::lock_guard guard(globalHttpProxyMutex_);
2222b1b8bc3fSopenharmony_ci        httpProxy = tmpHttpProxy;
2223b1b8bc3fSopenharmony_ci    }
2224b1b8bc3fSopenharmony_ci    globalHttpProxyCache_.EnsureInsert(userId, tmpHttpProxy);
2225b1b8bc3fSopenharmony_ci}
2226b1b8bc3fSopenharmony_ci
2227b1b8bc3fSopenharmony_civoid NetConnService::UpdateGlobalHttpProxy(const HttpProxy &httpProxy)
2228b1b8bc3fSopenharmony_ci{
2229b1b8bc3fSopenharmony_ci    if (netConnEventHandler_ == nullptr) {
2230b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netConnEventHandler_ is nullptr.");
2231b1b8bc3fSopenharmony_ci        return;
2232b1b8bc3fSopenharmony_ci    }
2233b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("UpdateGlobalHttpProxy start");
2234b1b8bc3fSopenharmony_ci    netConnEventHandler_->PostAsyncTask([this, httpProxy]() {
2235b1b8bc3fSopenharmony_ci        for (const auto &supplier : netSuppliers_) {
2236b1b8bc3fSopenharmony_ci            if (supplier.second == nullptr) {
2237b1b8bc3fSopenharmony_ci                continue;
2238b1b8bc3fSopenharmony_ci            }
2239b1b8bc3fSopenharmony_ci            supplier.second->UpdateGlobalHttpProxy(httpProxy);
2240b1b8bc3fSopenharmony_ci        }
2241b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("UpdateGlobalHttpProxy end");
2242b1b8bc3fSopenharmony_ci    });
2243b1b8bc3fSopenharmony_ci}
2244b1b8bc3fSopenharmony_ci
2245b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::OnInterfaceAddressUpdated(const std::string &addr,
2246b1b8bc3fSopenharmony_ci                                                                             const std::string &ifName, int flags,
2247b1b8bc3fSopenharmony_ci                                                                             int scope)
2248b1b8bc3fSopenharmony_ci{
2249b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> locker(mutex_);
2250b1b8bc3fSopenharmony_ci    for (const auto &callback : ifaceStateCallbacks_) {
2251b1b8bc3fSopenharmony_ci        if (callback == nullptr) {
2252b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("callback is null");
2253b1b8bc3fSopenharmony_ci            continue;
2254b1b8bc3fSopenharmony_ci        }
2255b1b8bc3fSopenharmony_ci        callback->OnInterfaceAddressUpdated(addr, ifName, flags, scope);
2256b1b8bc3fSopenharmony_ci    }
2257b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2258b1b8bc3fSopenharmony_ci}
2259b1b8bc3fSopenharmony_ci
2260b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::OnInterfaceAddressRemoved(const std::string &addr,
2261b1b8bc3fSopenharmony_ci                                                                             const std::string &ifName, int flags,
2262b1b8bc3fSopenharmony_ci                                                                             int scope)
2263b1b8bc3fSopenharmony_ci{
2264b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> locker(mutex_);
2265b1b8bc3fSopenharmony_ci    for (const auto &callback : ifaceStateCallbacks_) {
2266b1b8bc3fSopenharmony_ci        if (callback == nullptr) {
2267b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("callback is null");
2268b1b8bc3fSopenharmony_ci            continue;
2269b1b8bc3fSopenharmony_ci        }
2270b1b8bc3fSopenharmony_ci        callback->OnInterfaceAddressRemoved(addr, ifName, flags, scope);
2271b1b8bc3fSopenharmony_ci    }
2272b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2273b1b8bc3fSopenharmony_ci}
2274b1b8bc3fSopenharmony_ci
2275b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::OnInterfaceAdded(const std::string &iface)
2276b1b8bc3fSopenharmony_ci{
2277b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> locker(mutex_);
2278b1b8bc3fSopenharmony_ci    for (const auto &callback : ifaceStateCallbacks_) {
2279b1b8bc3fSopenharmony_ci        if (callback == nullptr) {
2280b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("callback is null");
2281b1b8bc3fSopenharmony_ci            continue;
2282b1b8bc3fSopenharmony_ci        }
2283b1b8bc3fSopenharmony_ci        callback->OnInterfaceAdded(iface);
2284b1b8bc3fSopenharmony_ci    }
2285b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2286b1b8bc3fSopenharmony_ci}
2287b1b8bc3fSopenharmony_ci
2288b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::OnInterfaceRemoved(const std::string &iface)
2289b1b8bc3fSopenharmony_ci{
2290b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> locker(mutex_);
2291b1b8bc3fSopenharmony_ci    for (const auto &callback : ifaceStateCallbacks_) {
2292b1b8bc3fSopenharmony_ci        if (callback == nullptr) {
2293b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("callback is null");
2294b1b8bc3fSopenharmony_ci            continue;
2295b1b8bc3fSopenharmony_ci        }
2296b1b8bc3fSopenharmony_ci        callback->OnInterfaceRemoved(iface);
2297b1b8bc3fSopenharmony_ci    }
2298b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2299b1b8bc3fSopenharmony_ci}
2300b1b8bc3fSopenharmony_ci
2301b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::OnInterfaceChanged(const std::string &iface, bool up)
2302b1b8bc3fSopenharmony_ci{
2303b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> locker(mutex_);
2304b1b8bc3fSopenharmony_ci    for (const auto &callback : ifaceStateCallbacks_) {
2305b1b8bc3fSopenharmony_ci        if (callback == nullptr) {
2306b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("callback is null");
2307b1b8bc3fSopenharmony_ci            continue;
2308b1b8bc3fSopenharmony_ci        }
2309b1b8bc3fSopenharmony_ci        callback->OnInterfaceChanged(iface, up);
2310b1b8bc3fSopenharmony_ci    }
2311b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2312b1b8bc3fSopenharmony_ci}
2313b1b8bc3fSopenharmony_ci
2314b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::OnInterfaceLinkStateChanged(const std::string &iface, bool up)
2315b1b8bc3fSopenharmony_ci{
2316b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> locker(mutex_);
2317b1b8bc3fSopenharmony_ci    for (const auto &callback : ifaceStateCallbacks_) {
2318b1b8bc3fSopenharmony_ci        if (callback == nullptr) {
2319b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("callback is null");
2320b1b8bc3fSopenharmony_ci            continue;
2321b1b8bc3fSopenharmony_ci        }
2322b1b8bc3fSopenharmony_ci        callback->OnInterfaceLinkStateChanged(iface, up);
2323b1b8bc3fSopenharmony_ci    }
2324b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2325b1b8bc3fSopenharmony_ci}
2326b1b8bc3fSopenharmony_ci
2327b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::OnRouteChanged(bool updated, const std::string &route,
2328b1b8bc3fSopenharmony_ci                                                                  const std::string &gateway, const std::string &ifName)
2329b1b8bc3fSopenharmony_ci{
2330b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2331b1b8bc3fSopenharmony_ci}
2332b1b8bc3fSopenharmony_ci
2333b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult)
2334b1b8bc3fSopenharmony_ci{
2335b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2336b1b8bc3fSopenharmony_ci}
2337b1b8bc3fSopenharmony_ci
2338b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::OnBandwidthReachedLimit(const std::string &limitName,
2339b1b8bc3fSopenharmony_ci                                                                           const std::string &iface)
2340b1b8bc3fSopenharmony_ci{
2341b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2342b1b8bc3fSopenharmony_ci}
2343b1b8bc3fSopenharmony_ci
2344b1b8bc3fSopenharmony_ciint32_t NetConnService::NetInterfaceStateCallback::RegisterInterfaceCallback(
2345b1b8bc3fSopenharmony_ci    const sptr<INetInterfaceStateCallback> &callback)
2346b1b8bc3fSopenharmony_ci{
2347b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
2348b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("callback is null");
2349b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
2350b1b8bc3fSopenharmony_ci    }
2351b1b8bc3fSopenharmony_ci
2352b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> locker(mutex_);
2353b1b8bc3fSopenharmony_ci    for (const auto &iter : ifaceStateCallbacks_) {
2354b1b8bc3fSopenharmony_ci        if (!iter) {
2355b1b8bc3fSopenharmony_ci            continue;
2356b1b8bc3fSopenharmony_ci        }
2357b1b8bc3fSopenharmony_ci        if (iter->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
2358b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("RegisterInterfaceCallback find same callback");
2359b1b8bc3fSopenharmony_ci            return NET_CONN_ERR_SAME_CALLBACK;
2360b1b8bc3fSopenharmony_ci        }
2361b1b8bc3fSopenharmony_ci    }
2362b1b8bc3fSopenharmony_ci    ifaceStateCallbacks_.push_back(callback);
2363b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2364b1b8bc3fSopenharmony_ci}
2365b1b8bc3fSopenharmony_ci
2366b1b8bc3fSopenharmony_ciint32_t NetConnService::NetPolicyCallback::NetUidPolicyChange(uint32_t uid, uint32_t policy)
2367b1b8bc3fSopenharmony_ci{
2368b1b8bc3fSopenharmony_ci    NETMGR_LOG_D("NetUidPolicyChange Uid=%{public}d, policy=%{public}d", uid, policy);
2369b1b8bc3fSopenharmony_ci    if (client_.defaultNetSupplier_ == nullptr) {
2370b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("defaultNetSupplier_ is nullptr");
2371b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
2372b1b8bc3fSopenharmony_ci    }
2373b1b8bc3fSopenharmony_ci    if (client_.netConnEventHandler_) {
2374b1b8bc3fSopenharmony_ci        client_.netConnEventHandler_->PostSyncTask([this, uid, policy]() { SendNetPolicyChange(uid, policy); });
2375b1b8bc3fSopenharmony_ci        return NETMANAGER_SUCCESS;
2376b1b8bc3fSopenharmony_ci    }
2377b1b8bc3fSopenharmony_ci    return NETMANAGER_ERROR;
2378b1b8bc3fSopenharmony_ci}
2379b1b8bc3fSopenharmony_ci
2380b1b8bc3fSopenharmony_civoid NetConnService::NetPolicyCallback::SendNetPolicyChange(uint32_t uid, uint32_t policy)
2381b1b8bc3fSopenharmony_ci{
2382b1b8bc3fSopenharmony_ci    auto it = client_.netUidActivates_.find(uid);
2383b1b8bc3fSopenharmony_ci    if (it != client_.netUidActivates_.end()) {
2384b1b8bc3fSopenharmony_ci        sptr<NetHandle> defaultNetHandle = client_.defaultNetSupplier_->GetNetHandle();
2385b1b8bc3fSopenharmony_ci        bool metered = client_.defaultNetSupplier_->HasNetCap(NET_CAPABILITY_NOT_METERED);
2386b1b8bc3fSopenharmony_ci        bool newBlocked = NetManagerCenter::GetInstance().IsUidNetAccess(uid, metered);
2387b1b8bc3fSopenharmony_ci        std::vector<std::shared_ptr<NetActivate>> &activates = it->second;
2388b1b8bc3fSopenharmony_ci        for (auto &activate : activates) {
2389b1b8bc3fSopenharmony_ci            if (activate->GetNetCallback() && activate->MatchRequestAndNetwork(client_.defaultNetSupplier_)) {
2390b1b8bc3fSopenharmony_ci                NETMGR_LOG_D("NetUidPolicyChange Uid=%{public}d, policy=%{public}d", uid, policy);
2391b1b8bc3fSopenharmony_ci                activate->GetNetCallback()->NetBlockStatusChange(defaultNetHandle, newBlocked);
2392b1b8bc3fSopenharmony_ci            }
2393b1b8bc3fSopenharmony_ci        }
2394b1b8bc3fSopenharmony_ci    }
2395b1b8bc3fSopenharmony_ci}
2396b1b8bc3fSopenharmony_ci
2397b1b8bc3fSopenharmony_ciint32_t NetConnService::AddNetworkRoute(int32_t netId, const std::string &ifName,
2398b1b8bc3fSopenharmony_ci                                        const std::string &destination, const std::string &nextHop)
2399b1b8bc3fSopenharmony_ci{
2400b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().NetworkAddRoute(netId, ifName, destination, nextHop);
2401b1b8bc3fSopenharmony_ci}
2402b1b8bc3fSopenharmony_ci
2403b1b8bc3fSopenharmony_ciint32_t NetConnService::RemoveNetworkRoute(int32_t netId, const std::string &ifName,
2404b1b8bc3fSopenharmony_ci                                           const std::string &destination, const std::string &nextHop)
2405b1b8bc3fSopenharmony_ci{
2406b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().NetworkRemoveRoute(netId, ifName, destination, nextHop);
2407b1b8bc3fSopenharmony_ci}
2408b1b8bc3fSopenharmony_ci
2409b1b8bc3fSopenharmony_ciint32_t NetConnService::AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
2410b1b8bc3fSopenharmony_ci                                            int32_t prefixLength)
2411b1b8bc3fSopenharmony_ci{
2412b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().AddInterfaceAddress(ifName, ipAddr, prefixLength);
2413b1b8bc3fSopenharmony_ci}
2414b1b8bc3fSopenharmony_ci
2415b1b8bc3fSopenharmony_ciint32_t NetConnService::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
2416b1b8bc3fSopenharmony_ci                                            int32_t prefixLength)
2417b1b8bc3fSopenharmony_ci{
2418b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().DelInterfaceAddress(ifName, ipAddr, prefixLength);
2419b1b8bc3fSopenharmony_ci}
2420b1b8bc3fSopenharmony_ci
2421b1b8bc3fSopenharmony_ciint32_t NetConnService::AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
2422b1b8bc3fSopenharmony_ci                                     const std::string &ifName)
2423b1b8bc3fSopenharmony_ci{
2424b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().AddStaticArp(ipAddr, macAddr, ifName);
2425b1b8bc3fSopenharmony_ci}
2426b1b8bc3fSopenharmony_ci
2427b1b8bc3fSopenharmony_ciint32_t NetConnService::DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
2428b1b8bc3fSopenharmony_ci                                     const std::string &ifName)
2429b1b8bc3fSopenharmony_ci{
2430b1b8bc3fSopenharmony_ci    return NetsysController::GetInstance().DelStaticArp(ipAddr, macAddr, ifName);
2431b1b8bc3fSopenharmony_ci}
2432b1b8bc3fSopenharmony_ci
2433b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterSlotType(uint32_t supplierId, int32_t type)
2434b1b8bc3fSopenharmony_ci{
2435b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_SUCCESS;
2436b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2437b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, supplierId, type, &result]() {
2438b1b8bc3fSopenharmony_ci            if (netSuppliers_.find(supplierId) == netSuppliers_.end()) {
2439b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("supplierId[%{public}d] is not exits", supplierId);
2440b1b8bc3fSopenharmony_ci                result =  NETMANAGER_ERR_INVALID_PARAMETER;
2441b1b8bc3fSopenharmony_ci            } else {
2442b1b8bc3fSopenharmony_ci                NETMGR_LOG_I("supplierId[%{public}d] update type[%{public}d].", supplierId, type);
2443b1b8bc3fSopenharmony_ci                sptr<NetSupplier> supplier = netSuppliers_[supplierId];
2444b1b8bc3fSopenharmony_ci                supplier->SetSupplierType(type);
2445b1b8bc3fSopenharmony_ci                result =  NETMANAGER_SUCCESS;
2446b1b8bc3fSopenharmony_ci            }
2447b1b8bc3fSopenharmony_ci        });
2448b1b8bc3fSopenharmony_ci    }
2449b1b8bc3fSopenharmony_ci    return result;
2450b1b8bc3fSopenharmony_ci}
2451b1b8bc3fSopenharmony_ci
2452b1b8bc3fSopenharmony_ciint32_t NetConnService::GetSlotType(std::string &type)
2453b1b8bc3fSopenharmony_ci{
2454b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_SUCCESS;
2455b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2456b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, &type, &result]() {
2457b1b8bc3fSopenharmony_ci            if (defaultNetSupplier_ == nullptr) {
2458b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("supplier is nullptr");
2459b1b8bc3fSopenharmony_ci                result =  NETMANAGER_ERR_LOCAL_PTR_NULL;
2460b1b8bc3fSopenharmony_ci            } else {
2461b1b8bc3fSopenharmony_ci                type = defaultNetSupplier_->GetSupplierType();
2462b1b8bc3fSopenharmony_ci                result =  NETMANAGER_SUCCESS;
2463b1b8bc3fSopenharmony_ci            }
2464b1b8bc3fSopenharmony_ci        });
2465b1b8bc3fSopenharmony_ci    }
2466b1b8bc3fSopenharmony_ci    return result;
2467b1b8bc3fSopenharmony_ci}
2468b1b8bc3fSopenharmony_ci
2469b1b8bc3fSopenharmony_ciint32_t NetConnService::FactoryResetNetwork()
2470b1b8bc3fSopenharmony_ci{
2471b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter FactoryResetNetwork.");
2472b1b8bc3fSopenharmony_ci
2473b1b8bc3fSopenharmony_ci    SetAirplaneMode(false);
2474b1b8bc3fSopenharmony_ci
2475b1b8bc3fSopenharmony_ci    if (netFactoryResetCallback_ == nullptr) {
2476b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netFactoryResetCallback_ is nullptr");
2477b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
2478b1b8bc3fSopenharmony_ci    }
2479b1b8bc3fSopenharmony_ci    netFactoryResetCallback_->NotifyNetFactoryResetAsync();
2480b1b8bc3fSopenharmony_ci
2481b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("End FactoryResetNetwork.");
2482b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2483b1b8bc3fSopenharmony_ci}
2484b1b8bc3fSopenharmony_ci
2485b1b8bc3fSopenharmony_ciint32_t NetConnService::RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback)
2486b1b8bc3fSopenharmony_ci{
2487b1b8bc3fSopenharmony_ci    if (callback == nullptr) {
2488b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("callback is nullptr");
2489b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
2490b1b8bc3fSopenharmony_ci    }
2491b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("Enter RegisterNetFactoryResetCallback.");
2492b1b8bc3fSopenharmony_ci    if (netFactoryResetCallback_ == nullptr) {
2493b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("netFactoryResetCallback_ is nullptr");
2494b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_LOCAL_PTR_NULL;
2495b1b8bc3fSopenharmony_ci    }
2496b1b8bc3fSopenharmony_ci    return netFactoryResetCallback_->RegisterNetFactoryResetCallbackAsync(callback);
2497b1b8bc3fSopenharmony_ci}
2498b1b8bc3fSopenharmony_ci
2499b1b8bc3fSopenharmony_civoid NetConnService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
2500b1b8bc3fSopenharmony_ci{
2501b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("OnAddSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
2502b1b8bc3fSopenharmony_ci    if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
2503b1b8bc3fSopenharmony_ci        if (hasSARemoved_) {
2504b1b8bc3fSopenharmony_ci            OnNetSysRestart();
2505b1b8bc3fSopenharmony_ci            hasSARemoved_ = false;
2506b1b8bc3fSopenharmony_ci        }
2507b1b8bc3fSopenharmony_ci    } else if (systemAbilityId == ACCESS_TOKEN_MANAGER_SERVICE_ID) {
2508b1b8bc3fSopenharmony_ci        if (!registerToService_) {
2509b1b8bc3fSopenharmony_ci            if (!Publish(NetConnService::GetInstance().get())) {
2510b1b8bc3fSopenharmony_ci                NETMGR_LOG_E("Register to sa manager failed");
2511b1b8bc3fSopenharmony_ci            }
2512b1b8bc3fSopenharmony_ci            registerToService_ = true;
2513b1b8bc3fSopenharmony_ci        }
2514b1b8bc3fSopenharmony_ci    } else if (systemAbilityId == COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID) {
2515b1b8bc3fSopenharmony_ci        policyCallback_ = new (std::nothrow) NetPolicyCallback(*this);
2516b1b8bc3fSopenharmony_ci        std::shared_ptr<NetPolicyClient> netPolicy = DelayedSingleton<NetPolicyClient>::GetInstance();
2517b1b8bc3fSopenharmony_ci        int32_t registerRet = netPolicy->RegisterNetPolicyCallback(policyCallback_);
2518b1b8bc3fSopenharmony_ci        if (registerRet != NETMANAGER_SUCCESS) {
2519b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("Register NetPolicyCallback failed, ret =%{public}d", registerRet);
2520b1b8bc3fSopenharmony_ci        }
2521b1b8bc3fSopenharmony_ci    }
2522b1b8bc3fSopenharmony_ci}
2523b1b8bc3fSopenharmony_ci
2524b1b8bc3fSopenharmony_civoid NetConnService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
2525b1b8bc3fSopenharmony_ci{
2526b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("OnRemoveSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
2527b1b8bc3fSopenharmony_ci    if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
2528b1b8bc3fSopenharmony_ci        hasSARemoved_ = true;
2529b1b8bc3fSopenharmony_ci    }
2530b1b8bc3fSopenharmony_ci}
2531b1b8bc3fSopenharmony_ci
2532b1b8bc3fSopenharmony_civoid NetConnService::SubscribeCommonEvent(const std::string &eventName, EventReceiver receiver)
2533b1b8bc3fSopenharmony_ci{
2534b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("eventName=%{public}s", eventName.c_str());
2535b1b8bc3fSopenharmony_ci    EventFwk::MatchingSkills matchingSkills;
2536b1b8bc3fSopenharmony_ci    matchingSkills.AddEvent(eventName);
2537b1b8bc3fSopenharmony_ci    EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2538b1b8bc3fSopenharmony_ci
2539b1b8bc3fSopenharmony_ci    auto subscriberPtr = std::make_shared<NetConnListener>(subscribeInfo, receiver);
2540b1b8bc3fSopenharmony_ci    if (subscriberPtr == nullptr) {
2541b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("subscriberPtr is nullptr");
2542b1b8bc3fSopenharmony_ci        return;
2543b1b8bc3fSopenharmony_ci    }
2544b1b8bc3fSopenharmony_ci    EventFwk::CommonEventManager::SubscribeCommonEvent(subscriberPtr);
2545b1b8bc3fSopenharmony_ci}
2546b1b8bc3fSopenharmony_ci
2547b1b8bc3fSopenharmony_civoid NetConnService::OnReceiveEvent(const EventFwk::CommonEventData &data)
2548b1b8bc3fSopenharmony_ci{
2549b1b8bc3fSopenharmony_ci    auto const &want = data.GetWant();
2550b1b8bc3fSopenharmony_ci    std::string action = want.GetAction();
2551b1b8bc3fSopenharmony_ci    if (action == "usual.event.DATA_SHARE_READY") {
2552b1b8bc3fSopenharmony_ci        NETMGR_LOG_I("on receive data_share ready.");
2553b1b8bc3fSopenharmony_ci        isDataShareReady_ = true;
2554b1b8bc3fSopenharmony_ci        HttpProxy httpProxy;
2555b1b8bc3fSopenharmony_ci        LoadGlobalHttpProxy(httpProxy);
2556b1b8bc3fSopenharmony_ci        UpdateGlobalHttpProxy(httpProxy);
2557b1b8bc3fSopenharmony_ci    }
2558b1b8bc3fSopenharmony_ci#ifdef FEATURE_SUPPORT_POWERMANAGER
2559b1b8bc3fSopenharmony_ci    if (action == "usual.event.POWER_MANAGER_STATE_CHANGED") {
2560b1b8bc3fSopenharmony_ci        int code = data.GetCode();
2561b1b8bc3fSopenharmony_ci        HandlePowerMgrEvent(code);
2562b1b8bc3fSopenharmony_ci    }
2563b1b8bc3fSopenharmony_ci#endif
2564b1b8bc3fSopenharmony_ci}
2565b1b8bc3fSopenharmony_ci
2566b1b8bc3fSopenharmony_cibool NetConnService::IsSupplierMatchRequestAndNetwork(sptr<NetSupplier> ns)
2567b1b8bc3fSopenharmony_ci{
2568b1b8bc3fSopenharmony_ci    if (ns == nullptr) {
2569b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("supplier is nullptr");
2570b1b8bc3fSopenharmony_ci        return false;
2571b1b8bc3fSopenharmony_ci    }
2572b1b8bc3fSopenharmony_ci    NET_ACTIVATE_MAP::iterator iterActive;
2573b1b8bc3fSopenharmony_ci    for (iterActive = netActivates_.begin(); iterActive != netActivates_.end(); ++iterActive) {
2574b1b8bc3fSopenharmony_ci        if (!iterActive->second) {
2575b1b8bc3fSopenharmony_ci            continue;
2576b1b8bc3fSopenharmony_ci        }
2577b1b8bc3fSopenharmony_ci        if (iterActive->second->MatchRequestAndNetwork(ns)) {
2578b1b8bc3fSopenharmony_ci            return true;
2579b1b8bc3fSopenharmony_ci        }
2580b1b8bc3fSopenharmony_ci    }
2581b1b8bc3fSopenharmony_ci
2582b1b8bc3fSopenharmony_ci    return false;
2583b1b8bc3fSopenharmony_ci}
2584b1b8bc3fSopenharmony_ci
2585b1b8bc3fSopenharmony_civoid NetConnService::OnNetSysRestart()
2586b1b8bc3fSopenharmony_ci{
2587b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("OnNetSysRestart");
2588b1b8bc3fSopenharmony_ci
2589b1b8bc3fSopenharmony_ci    NET_SUPPLIER_MAP::iterator iter;
2590b1b8bc3fSopenharmony_ci    for (iter = netSuppliers_.begin(); iter != netSuppliers_.end(); ++iter) {
2591b1b8bc3fSopenharmony_ci        if (iter->second == nullptr) {
2592b1b8bc3fSopenharmony_ci            continue;
2593b1b8bc3fSopenharmony_ci        }
2594b1b8bc3fSopenharmony_ci
2595b1b8bc3fSopenharmony_ci        NETMGR_LOG_D("supplier info, supplier[%{public}d, %{public}s], realScore[%{public}d], isConnected[%{public}d]",
2596b1b8bc3fSopenharmony_ci            iter->second->GetSupplierId(), iter->second->GetNetSupplierIdent().c_str(),
2597b1b8bc3fSopenharmony_ci            iter->second->GetRealScore(), iter->second->IsConnected());
2598b1b8bc3fSopenharmony_ci
2599b1b8bc3fSopenharmony_ci        if ((!iter->second->IsConnected()) || (!IsSupplierMatchRequestAndNetwork(iter->second))) {
2600b1b8bc3fSopenharmony_ci            NETMGR_LOG_D("Supplier[%{public}d] is not connected or not match request.", iter->second->GetSupplierId());
2601b1b8bc3fSopenharmony_ci            continue;
2602b1b8bc3fSopenharmony_ci        }
2603b1b8bc3fSopenharmony_ci
2604b1b8bc3fSopenharmony_ci        iter->second->ResumeNetworkInfo();
2605b1b8bc3fSopenharmony_ci    }
2606b1b8bc3fSopenharmony_ci    std::unique_lock<std::recursive_mutex> locker(netManagerMutex_);
2607b1b8bc3fSopenharmony_ci    if (defaultNetSupplier_ != nullptr) {
2608b1b8bc3fSopenharmony_ci        defaultNetSupplier_->ClearDefault();
2609b1b8bc3fSopenharmony_ci        defaultNetSupplier_ = nullptr;
2610b1b8bc3fSopenharmony_ci    }
2611b1b8bc3fSopenharmony_ci    locker.unlock();
2612b1b8bc3fSopenharmony_ci    FindBestNetworkForAllRequest();
2613b1b8bc3fSopenharmony_ci}
2614b1b8bc3fSopenharmony_ci
2615b1b8bc3fSopenharmony_ciint32_t NetConnService::IsPreferCellularUrl(const std::string& url, bool& preferCellular)
2616b1b8bc3fSopenharmony_ci{
2617b1b8bc3fSopenharmony_ci    static std::vector<std::string> preferredUrlList = GetPreferredUrl();
2618b1b8bc3fSopenharmony_ci    preferCellular = std::any_of(preferredUrlList.begin(), preferredUrlList.end(),
2619b1b8bc3fSopenharmony_ci                                 [&url](const std::string &str) { return url.find(str) != std::string::npos; });
2620b1b8bc3fSopenharmony_ci    return 0;
2621b1b8bc3fSopenharmony_ci}
2622b1b8bc3fSopenharmony_ci
2623b1b8bc3fSopenharmony_cibool NetConnService::IsIfaceNameInUse(const std::string &ifaceName, int32_t netId)
2624b1b8bc3fSopenharmony_ci{
2625b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
2626b1b8bc3fSopenharmony_ci    for (const auto &netSupplier : netSuppliers_) {
2627b1b8bc3fSopenharmony_ci        if (netSupplier.second->GetNetwork()->GetNetId() == netId) {
2628b1b8bc3fSopenharmony_ci            continue;
2629b1b8bc3fSopenharmony_ci        }
2630b1b8bc3fSopenharmony_ci        if (!netSupplier.second->IsAvailable()) {
2631b1b8bc3fSopenharmony_ci            continue;
2632b1b8bc3fSopenharmony_ci        }
2633b1b8bc3fSopenharmony_ci        if (netSupplier.second->GetNetwork()->GetIfaceName() == ifaceName) {
2634b1b8bc3fSopenharmony_ci            return true;
2635b1b8bc3fSopenharmony_ci        }
2636b1b8bc3fSopenharmony_ci    }
2637b1b8bc3fSopenharmony_ci    return false;
2638b1b8bc3fSopenharmony_ci}
2639b1b8bc3fSopenharmony_ci
2640b1b8bc3fSopenharmony_cistd::string NetConnService::GetNetCapabilitiesAsString(const uint32_t supplierId)
2641b1b8bc3fSopenharmony_ci{
2642b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
2643b1b8bc3fSopenharmony_ci    const auto iterNetSuppliers = netSuppliers_.find(supplierId);
2644b1b8bc3fSopenharmony_ci    if (iterNetSuppliers != netSuppliers_.end() && iterNetSuppliers->second != nullptr) {
2645b1b8bc3fSopenharmony_ci        return iterNetSuppliers->second->GetNetCapabilities().ToString(" ");
2646b1b8bc3fSopenharmony_ci    }
2647b1b8bc3fSopenharmony_ci    return {};
2648b1b8bc3fSopenharmony_ci}
2649b1b8bc3fSopenharmony_ci
2650b1b8bc3fSopenharmony_cistd::vector<std::string> NetConnService::GetPreferredUrl()
2651b1b8bc3fSopenharmony_ci{
2652b1b8bc3fSopenharmony_ci    std::vector<std::string> preferCellularUrlList;
2653b1b8bc3fSopenharmony_ci    const std::string preferCellularUrlPath = "/system/etc/prefer_cellular_url_list.txt";
2654b1b8bc3fSopenharmony_ci    std::ifstream preferCellularFile(preferCellularUrlPath);
2655b1b8bc3fSopenharmony_ci    if (preferCellularFile.is_open()) {
2656b1b8bc3fSopenharmony_ci        std::string line;
2657b1b8bc3fSopenharmony_ci        while (getline(preferCellularFile, line)) {
2658b1b8bc3fSopenharmony_ci            preferCellularUrlList.push_back(line);
2659b1b8bc3fSopenharmony_ci        }
2660b1b8bc3fSopenharmony_ci        preferCellularFile.close();
2661b1b8bc3fSopenharmony_ci    } else {
2662b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("open prefer cellular url file failure.");
2663b1b8bc3fSopenharmony_ci    }
2664b1b8bc3fSopenharmony_ci    return preferCellularUrlList;
2665b1b8bc3fSopenharmony_ci}
2666b1b8bc3fSopenharmony_ci
2667b1b8bc3fSopenharmony_civoid NetConnService::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
2668b1b8bc3fSopenharmony_ci{
2669b1b8bc3fSopenharmony_ci    sptr<IRemoteObject> diedRemoted = remoteObject.promote();
2670b1b8bc3fSopenharmony_ci    if (diedRemoted == nullptr) {
2671b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("diedRemoted is null");
2672b1b8bc3fSopenharmony_ci        return;
2673b1b8bc3fSopenharmony_ci    }
2674b1b8bc3fSopenharmony_ci    uint32_t callingUid = static_cast<uint32_t>(IPCSkeleton::GetCallingUid());
2675b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("OnRemoteDied, callingUid=%{public}u", callingUid);
2676b1b8bc3fSopenharmony_ci    sptr<INetConnCallback> callback = iface_cast<INetConnCallback>(diedRemoted);
2677b1b8bc3fSopenharmony_ci    UnregisterNetConnCallback(callback);
2678b1b8bc3fSopenharmony_ci}
2679b1b8bc3fSopenharmony_ci
2680b1b8bc3fSopenharmony_civoid NetConnService::RemoveClientDeathRecipient(const sptr<INetConnCallback> &callback)
2681b1b8bc3fSopenharmony_ci{
2682b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> autoLock(remoteMutex_);
2683b1b8bc3fSopenharmony_ci    auto iter =
2684b1b8bc3fSopenharmony_ci        std::find_if(remoteCallback_.cbegin(), remoteCallback_.cend(), [&callback](const sptr<INetConnCallback> &item) {
2685b1b8bc3fSopenharmony_ci            return item->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr();
2686b1b8bc3fSopenharmony_ci        });
2687b1b8bc3fSopenharmony_ci    if (iter == remoteCallback_.cend()) {
2688b1b8bc3fSopenharmony_ci        return;
2689b1b8bc3fSopenharmony_ci    }
2690b1b8bc3fSopenharmony_ci    callback->AsObject()->RemoveDeathRecipient(deathRecipient_);
2691b1b8bc3fSopenharmony_ci    remoteCallback_.erase(iter);
2692b1b8bc3fSopenharmony_ci}
2693b1b8bc3fSopenharmony_ci
2694b1b8bc3fSopenharmony_civoid NetConnService::AddClientDeathRecipient(const sptr<INetConnCallback> &callback)
2695b1b8bc3fSopenharmony_ci{
2696b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> autoLock(remoteMutex_);
2697b1b8bc3fSopenharmony_ci    if (deathRecipient_ == nullptr) {
2698b1b8bc3fSopenharmony_ci        deathRecipient_ = new (std::nothrow) ConnCallbackDeathRecipient(*this);
2699b1b8bc3fSopenharmony_ci    }
2700b1b8bc3fSopenharmony_ci    if (deathRecipient_ == nullptr) {
2701b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("deathRecipient is null");
2702b1b8bc3fSopenharmony_ci        return;
2703b1b8bc3fSopenharmony_ci    }
2704b1b8bc3fSopenharmony_ci    if (!callback->AsObject()->AddDeathRecipient(deathRecipient_)) {
2705b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("AddClientDeathRecipient failed");
2706b1b8bc3fSopenharmony_ci        return;
2707b1b8bc3fSopenharmony_ci    }
2708b1b8bc3fSopenharmony_ci    auto iter =
2709b1b8bc3fSopenharmony_ci        std::find_if(remoteCallback_.cbegin(), remoteCallback_.cend(), [&callback](const sptr<INetConnCallback> &item) {
2710b1b8bc3fSopenharmony_ci            return item->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr();
2711b1b8bc3fSopenharmony_ci        });
2712b1b8bc3fSopenharmony_ci    if (iter == remoteCallback_.cend()) {
2713b1b8bc3fSopenharmony_ci        remoteCallback_.emplace_back(callback);
2714b1b8bc3fSopenharmony_ci    }
2715b1b8bc3fSopenharmony_ci}
2716b1b8bc3fSopenharmony_ci
2717b1b8bc3fSopenharmony_civoid NetConnService::RemoveALLClientDeathRecipient()
2718b1b8bc3fSopenharmony_ci{
2719b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> autoLock(remoteMutex_);
2720b1b8bc3fSopenharmony_ci    for (auto &item : remoteCallback_) {
2721b1b8bc3fSopenharmony_ci        item->AsObject()->RemoveDeathRecipient(deathRecipient_);
2722b1b8bc3fSopenharmony_ci    }
2723b1b8bc3fSopenharmony_ci    remoteCallback_.clear();
2724b1b8bc3fSopenharmony_ci    deathRecipient_ = nullptr;
2725b1b8bc3fSopenharmony_ci}
2726b1b8bc3fSopenharmony_ci
2727b1b8bc3fSopenharmony_cistd::vector<sptr<NetSupplier>> NetConnService::FindSupplierWithInternetByBearerType(NetBearType bearerType)
2728b1b8bc3fSopenharmony_ci{
2729b1b8bc3fSopenharmony_ci    std::vector<sptr<NetSupplier>> result;
2730b1b8bc3fSopenharmony_ci    NET_SUPPLIER_MAP::iterator iterSupplier;
2731b1b8bc3fSopenharmony_ci    std::lock_guard<std::recursive_mutex> locker(netManagerMutex_);
2732b1b8bc3fSopenharmony_ci    for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) {
2733b1b8bc3fSopenharmony_ci        if (iterSupplier->second == nullptr) {
2734b1b8bc3fSopenharmony_ci            continue;
2735b1b8bc3fSopenharmony_ci        }
2736b1b8bc3fSopenharmony_ci        if (!iterSupplier->second->GetNetCaps().HasNetCap(NET_CAPABILITY_INTERNET)) {
2737b1b8bc3fSopenharmony_ci            continue;
2738b1b8bc3fSopenharmony_ci        }
2739b1b8bc3fSopenharmony_ci        std::set<NetBearType>::iterator iter = iterSupplier->second->GetNetCapabilities().bearerTypes_.find(bearerType);
2740b1b8bc3fSopenharmony_ci        if (iter != iterSupplier->second->GetNetCapabilities().bearerTypes_.end()) {
2741b1b8bc3fSopenharmony_ci            NETMGR_LOG_I("found supplierId[%{public}d] by bearertype[%{public}d].", iterSupplier->first, bearerType);
2742b1b8bc3fSopenharmony_ci            result.push_back(iterSupplier->second);
2743b1b8bc3fSopenharmony_ci        }
2744b1b8bc3fSopenharmony_ci    }
2745b1b8bc3fSopenharmony_ci    return result;
2746b1b8bc3fSopenharmony_ci}
2747b1b8bc3fSopenharmony_ci
2748b1b8bc3fSopenharmony_ciint32_t NetConnService::UpdateSupplierScore(NetBearType bearerType, uint32_t detectionStatus, uint32_t& supplierId)
2749b1b8bc3fSopenharmony_ci{
2750b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
2751b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2752b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, bearerType, detectionStatus, &supplierId, &result]() {
2753b1b8bc3fSopenharmony_ci            result = this->UpdateSupplierScoreAsync(bearerType, detectionStatus, supplierId);
2754b1b8bc3fSopenharmony_ci        });
2755b1b8bc3fSopenharmony_ci    }
2756b1b8bc3fSopenharmony_ci    return result;
2757b1b8bc3fSopenharmony_ci}
2758b1b8bc3fSopenharmony_ci
2759b1b8bc3fSopenharmony_ciint32_t NetConnService::UpdateSupplierScoreAsync(NetBearType bearerType, uint32_t detectionStatus, uint32_t& supplierId)
2760b1b8bc3fSopenharmony_ci{
2761b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("update supplier score by type[%{public}d], detectionStatus[%{public}d], supplierId:%{public}d",
2762b1b8bc3fSopenharmony_ci        bearerType, detectionStatus, supplierId);
2763b1b8bc3fSopenharmony_ci    NetDetectionStatus state = static_cast<NetDetectionStatus>(detectionStatus);
2764b1b8bc3fSopenharmony_ci    if (state == QUALITY_POOR_STATE) {
2765b1b8bc3fSopenharmony_ci        // In poor network, supplierId should be an output parameter.
2766b1b8bc3fSopenharmony_ci        std::vector<sptr<NetSupplier>> suppliers = FindSupplierWithInternetByBearerType(bearerType);
2767b1b8bc3fSopenharmony_ci        if (suppliers.empty()) {
2768b1b8bc3fSopenharmony_ci            NETMGR_LOG_E(" not found supplierId by bearertype[%{public}d].", bearerType);
2769b1b8bc3fSopenharmony_ci            return NETMANAGER_ERR_INVALID_PARAMETER;
2770b1b8bc3fSopenharmony_ci        }
2771b1b8bc3fSopenharmony_ci        uint32_t tmpSupplierId = FindSupplierToReduceScore(suppliers, supplierId);
2772b1b8bc3fSopenharmony_ci        if (tmpSupplierId == INVALID_SUPPLIER_ID) {
2773b1b8bc3fSopenharmony_ci            NETMGR_LOG_E("not found supplierId, default supplier id[%{public}d], netId:[%{public}d]",
2774b1b8bc3fSopenharmony_ci                         defaultNetSupplier_->GetSupplierId(), defaultNetSupplier_->GetNetId());
2775b1b8bc3fSopenharmony_ci            return NETMANAGER_ERR_INVALID_PARAMETER;
2776b1b8bc3fSopenharmony_ci        }
2777b1b8bc3fSopenharmony_ci    }
2778b1b8bc3fSopenharmony_ci    // Check supplier exist by supplierId, and check supplier's type equals to bearerType.
2779b1b8bc3fSopenharmony_ci    auto supplier = FindNetSupplier(supplierId);
2780b1b8bc3fSopenharmony_ci    if (supplier == nullptr || supplier->GetNetSupplierType() != bearerType) {
2781b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("supplier doesn't exist.");
2782b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_INVALID_PARAMETER;
2783b1b8bc3fSopenharmony_ci    }
2784b1b8bc3fSopenharmony_ci    supplier->SetNetValid(state);
2785b1b8bc3fSopenharmony_ci    // Find best network because supplier score changed.
2786b1b8bc3fSopenharmony_ci    FindBestNetworkForAllRequest();
2787b1b8bc3fSopenharmony_ci    // Tell other suppliers to enable if current default supplier is not better than others.
2788b1b8bc3fSopenharmony_ci    if (defaultNetSupplier_ && defaultNetSupplier_->GetSupplierId() == supplierId) {
2789b1b8bc3fSopenharmony_ci        RequestAllNetworkExceptDefault();
2790b1b8bc3fSopenharmony_ci    }
2791b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2792b1b8bc3fSopenharmony_ci}
2793b1b8bc3fSopenharmony_ci
2794b1b8bc3fSopenharmony_ciuint32_t NetConnService::FindSupplierToReduceScore(std::vector<sptr<NetSupplier>>& suppliers, uint32_t& supplierId)
2795b1b8bc3fSopenharmony_ci{
2796b1b8bc3fSopenharmony_ci    uint32_t ret = INVALID_SUPPLIER_ID;
2797b1b8bc3fSopenharmony_ci    if (!defaultNetSupplier_) {
2798b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("default net supplier nullptr");
2799b1b8bc3fSopenharmony_ci        return ret;
2800b1b8bc3fSopenharmony_ci    }
2801b1b8bc3fSopenharmony_ci    std::vector<sptr<NetSupplier>>::iterator iter;
2802b1b8bc3fSopenharmony_ci    for (iter = suppliers.begin(); iter != suppliers.end(); ++iter) {
2803b1b8bc3fSopenharmony_ci        if (defaultNetSupplier_->GetNetId() == (*iter)->GetNetId()) {
2804b1b8bc3fSopenharmony_ci            ret = (*iter)->GetSupplierId();
2805b1b8bc3fSopenharmony_ci            supplierId = ret;
2806b1b8bc3fSopenharmony_ci            break;
2807b1b8bc3fSopenharmony_ci        }
2808b1b8bc3fSopenharmony_ci    }
2809b1b8bc3fSopenharmony_ci    return ret;
2810b1b8bc3fSopenharmony_ci}
2811b1b8bc3fSopenharmony_ci
2812b1b8bc3fSopenharmony_ciNetConnService::NetConnListener::NetConnListener(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
2813b1b8bc3fSopenharmony_ci    EventReceiver receiver) : EventFwk::CommonEventSubscriber(subscribeInfo), eventReceiver_(receiver) {}
2814b1b8bc3fSopenharmony_ci
2815b1b8bc3fSopenharmony_civoid NetConnService::NetConnListener::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
2816b1b8bc3fSopenharmony_ci{
2817b1b8bc3fSopenharmony_ci    if (eventReceiver_ == nullptr) {
2818b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("eventReceiver is nullptr");
2819b1b8bc3fSopenharmony_ci        return;
2820b1b8bc3fSopenharmony_ci    }
2821b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("NetConnListener::OnReceiveEvent(), event:[%{public}s], data:[%{public}s], code:[%{public}d]",
2822b1b8bc3fSopenharmony_ci                 eventData.GetWant().GetAction().c_str(), eventData.GetData().c_str(), eventData.GetCode());
2823b1b8bc3fSopenharmony_ci    eventReceiver_(eventData);
2824b1b8bc3fSopenharmony_ci}
2825b1b8bc3fSopenharmony_ci
2826b1b8bc3fSopenharmony_ciint32_t NetConnService::EnableVnicNetwork(const sptr<NetLinkInfo> &netLinkInfo, const std::set<int32_t> &uids)
2827b1b8bc3fSopenharmony_ci{
2828b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
2829b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2830b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask(
2831b1b8bc3fSopenharmony_ci            [this, &netLinkInfo, &uids, &result]() { result = this->EnableVnicNetworkAsync(netLinkInfo, uids); });
2832b1b8bc3fSopenharmony_ci    }
2833b1b8bc3fSopenharmony_ci    return result;
2834b1b8bc3fSopenharmony_ci}
2835b1b8bc3fSopenharmony_ci
2836b1b8bc3fSopenharmony_ciint32_t NetConnService::EnableVnicNetworkAsync(const sptr<NetLinkInfo> &netLinkInfo, const std::set<int32_t> &uids)
2837b1b8bc3fSopenharmony_ci{
2838b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("enable vnic network");
2839b1b8bc3fSopenharmony_ci
2840b1b8bc3fSopenharmony_ci    if (vnicCreated.load()) {
2841b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Enable Vnic Network already");
2842b1b8bc3fSopenharmony_ci        return NETWORKVPN_ERROR_VNIC_EXIST;
2843b1b8bc3fSopenharmony_ci    }
2844b1b8bc3fSopenharmony_ci
2845b1b8bc3fSopenharmony_ci    uint16_t mtu = netLinkInfo->mtu_;
2846b1b8bc3fSopenharmony_ci    if (netLinkInfo->netAddrList_.empty()) {
2847b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("the netLinkInfo netAddrList is empty");
2848b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_INVALID_NETWORK;
2849b1b8bc3fSopenharmony_ci    }
2850b1b8bc3fSopenharmony_ci
2851b1b8bc3fSopenharmony_ci    const std::string &tunAddr = netLinkInfo->netAddrList_.front().address_;
2852b1b8bc3fSopenharmony_ci    int32_t prefix = netLinkInfo->netAddrList_.front().prefixlen_;
2853b1b8bc3fSopenharmony_ci    if (!CommonUtils::IsValidIPV4(tunAddr)) {
2854b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("the netLinkInfo tunAddr is not valid");
2855b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_INVALID_NETWORK;
2856b1b8bc3fSopenharmony_ci    }
2857b1b8bc3fSopenharmony_ci
2858b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("EnableVnicNetwork tunAddr:[%{public}s], prefix:[%{public}d]", tunAddr.c_str(), prefix);
2859b1b8bc3fSopenharmony_ci    if (NetsysController::GetInstance().CreateVnic(mtu, tunAddr, prefix, uids) != NETMANAGER_SUCCESS) {
2860b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("EnableVnicNetwork CreateVnic failed");
2861b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_OPERATION_FAILED;
2862b1b8bc3fSopenharmony_ci    }
2863b1b8bc3fSopenharmony_ci
2864b1b8bc3fSopenharmony_ci    vnicCreated = true;
2865b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2866b1b8bc3fSopenharmony_ci}
2867b1b8bc3fSopenharmony_ci
2868b1b8bc3fSopenharmony_ciint32_t NetConnService::DisableVnicNetwork()
2869b1b8bc3fSopenharmony_ci{
2870b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
2871b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2872b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask(
2873b1b8bc3fSopenharmony_ci            [this, &result]() { result = this->DisableVnicNetworkAsync(); });
2874b1b8bc3fSopenharmony_ci    }
2875b1b8bc3fSopenharmony_ci    return result;
2876b1b8bc3fSopenharmony_ci}
2877b1b8bc3fSopenharmony_ci
2878b1b8bc3fSopenharmony_ciint32_t NetConnService::DisableVnicNetworkAsync()
2879b1b8bc3fSopenharmony_ci{
2880b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("del internal virtual network");
2881b1b8bc3fSopenharmony_ci
2882b1b8bc3fSopenharmony_ci    if (!vnicCreated.load()) {
2883b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("cannot find vnic network");
2884b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_INVALID_NETWORK;
2885b1b8bc3fSopenharmony_ci    }
2886b1b8bc3fSopenharmony_ci
2887b1b8bc3fSopenharmony_ci    if (NetsysController::GetInstance().DestroyVnic() != NETMANAGER_SUCCESS) {
2888b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_OPERATION_FAILED;
2889b1b8bc3fSopenharmony_ci    }
2890b1b8bc3fSopenharmony_ci    vnicCreated = false;
2891b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2892b1b8bc3fSopenharmony_ci}
2893b1b8bc3fSopenharmony_ci
2894b1b8bc3fSopenharmony_ciint32_t NetConnService::EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif)
2895b1b8bc3fSopenharmony_ci{
2896b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
2897b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2898b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask(
2899b1b8bc3fSopenharmony_ci            [this, &virnicAddr, &iif, &result]() { result = this->EnableDistributedClientNetAsync(virnicAddr, iif); });
2900b1b8bc3fSopenharmony_ci    }
2901b1b8bc3fSopenharmony_ci    return result;
2902b1b8bc3fSopenharmony_ci}
2903b1b8bc3fSopenharmony_ci
2904b1b8bc3fSopenharmony_ciint32_t NetConnService::EnableDistributedClientNetAsync(const std::string &virnicAddr, const std::string &iif)
2905b1b8bc3fSopenharmony_ci{
2906b1b8bc3fSopenharmony_ci    if (iif.empty()) {
2907b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("iif is empty");
2908b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_INVALID_NETWORK;
2909b1b8bc3fSopenharmony_ci    }
2910b1b8bc3fSopenharmony_ci
2911b1b8bc3fSopenharmony_ci    if (!CommonUtils::IsValidIPV4(virnicAddr)) {
2912b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("the virnicAddr is not valid");
2913b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_INVALID_NETWORK;
2914b1b8bc3fSopenharmony_ci    }
2915b1b8bc3fSopenharmony_ci
2916b1b8bc3fSopenharmony_ci    if (NetsysController::GetInstance().EnableDistributedClientNet(virnicAddr, iif) != NETMANAGER_SUCCESS) {
2917b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("EnableDistributedClientNet failed");
2918b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_OPERATION_FAILED;
2919b1b8bc3fSopenharmony_ci    }
2920b1b8bc3fSopenharmony_ci
2921b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2922b1b8bc3fSopenharmony_ci}
2923b1b8bc3fSopenharmony_ci
2924b1b8bc3fSopenharmony_ciint32_t NetConnService::EnableDistributedServerNet(const std::string &iif, const std::string &devIface,
2925b1b8bc3fSopenharmony_ci                                                   const std::string &dstAddr)
2926b1b8bc3fSopenharmony_ci{
2927b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
2928b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2929b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask([this, &iif, &devIface, &dstAddr, &result]() {
2930b1b8bc3fSopenharmony_ci            result = this->EnableDistributedServerNetAsync(iif, devIface, dstAddr);
2931b1b8bc3fSopenharmony_ci        });
2932b1b8bc3fSopenharmony_ci    }
2933b1b8bc3fSopenharmony_ci    return result;
2934b1b8bc3fSopenharmony_ci}
2935b1b8bc3fSopenharmony_ci
2936b1b8bc3fSopenharmony_ciint32_t NetConnService::EnableDistributedServerNetAsync(const std::string &iif, const std::string &devIface,
2937b1b8bc3fSopenharmony_ci                                                        const std::string &dstAddr)
2938b1b8bc3fSopenharmony_ci{
2939b1b8bc3fSopenharmony_ci    if (iif.empty() || devIface.empty()) {
2940b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("iif || devIface is empty");
2941b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_INVALID_NETWORK;
2942b1b8bc3fSopenharmony_ci    }
2943b1b8bc3fSopenharmony_ci
2944b1b8bc3fSopenharmony_ci    if (!CommonUtils::IsValidIPV4(dstAddr)) {
2945b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("the dstAddr is not valid");
2946b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_INVALID_NETWORK;
2947b1b8bc3fSopenharmony_ci    }
2948b1b8bc3fSopenharmony_ci
2949b1b8bc3fSopenharmony_ci    if (NetsysController::GetInstance().EnableDistributedServerNet(iif, devIface, dstAddr) != NETMANAGER_SUCCESS) {
2950b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("EnableDistributedServerNet failed");
2951b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_OPERATION_FAILED;
2952b1b8bc3fSopenharmony_ci    }
2953b1b8bc3fSopenharmony_ci
2954b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2955b1b8bc3fSopenharmony_ci}
2956b1b8bc3fSopenharmony_ci
2957b1b8bc3fSopenharmony_ciint32_t NetConnService::DisableDistributedNet(bool isServer)
2958b1b8bc3fSopenharmony_ci{
2959b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
2960b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2961b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask(
2962b1b8bc3fSopenharmony_ci            [this, isServer, &result]() { result = this->DisableDistributedNetAsync(isServer); });
2963b1b8bc3fSopenharmony_ci    }
2964b1b8bc3fSopenharmony_ci    return result;
2965b1b8bc3fSopenharmony_ci}
2966b1b8bc3fSopenharmony_ci
2967b1b8bc3fSopenharmony_ciint32_t NetConnService::DisableDistributedNetAsync(bool isServer)
2968b1b8bc3fSopenharmony_ci{
2969b1b8bc3fSopenharmony_ci    if (NetsysController::GetInstance().DisableDistributedNet(isServer) != NETMANAGER_SUCCESS) {
2970b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("DisableDistributedNet");
2971b1b8bc3fSopenharmony_ci        return NETMANAGER_ERR_OPERATION_FAILED;
2972b1b8bc3fSopenharmony_ci    }
2973b1b8bc3fSopenharmony_ci
2974b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2975b1b8bc3fSopenharmony_ci}
2976b1b8bc3fSopenharmony_ci
2977b1b8bc3fSopenharmony_ciint32_t NetConnService::CloseSocketsUid(int32_t netId, uint32_t uid)
2978b1b8bc3fSopenharmony_ci{
2979b1b8bc3fSopenharmony_ci    int32_t result = NETMANAGER_ERROR;
2980b1b8bc3fSopenharmony_ci    if (netConnEventHandler_) {
2981b1b8bc3fSopenharmony_ci        netConnEventHandler_->PostSyncTask(
2982b1b8bc3fSopenharmony_ci            [this, netId, uid, &result]() { result = this->CloseSocketsUidAsync(netId, uid); });
2983b1b8bc3fSopenharmony_ci    }
2984b1b8bc3fSopenharmony_ci    return result;
2985b1b8bc3fSopenharmony_ci}
2986b1b8bc3fSopenharmony_ci
2987b1b8bc3fSopenharmony_ciint32_t NetConnService::CloseSocketsUidAsync(int32_t netId, uint32_t uid)
2988b1b8bc3fSopenharmony_ci{
2989b1b8bc3fSopenharmony_ci    auto iterNetwork = networks_.find(netId);
2990b1b8bc3fSopenharmony_ci    if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) {
2991b1b8bc3fSopenharmony_ci        NETMGR_LOG_E("Could not find the corresponding network.");
2992b1b8bc3fSopenharmony_ci        return NET_CONN_ERR_NETID_NOT_FOUND;
2993b1b8bc3fSopenharmony_ci    }
2994b1b8bc3fSopenharmony_ci    iterNetwork->second->CloseSocketsUid(uid);
2995b1b8bc3fSopenharmony_ci    return NETMANAGER_SUCCESS;
2996b1b8bc3fSopenharmony_ci}
2997b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard
2998b1b8bc3fSopenharmony_ci} // namespace OHOS
2999